2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)fio.c 8.1 (Berkeley) 6/6/93";
50 * Mail -- a mail program
56 * Set up the input pointers while copying the mail file into /tmp.
63 register int c, count;
64 register char *cp, *cp2;
69 char linebuf[LINESIZE];
71 /* Get temporary file. */
72 (void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir);
73 if ((c = mkstemp(linebuf)) == -1 ||
74 (mestmp = Fdopen(c, "r+")) == NULL) {
75 errx(1, "can't open %s", linebuf);
77 (void)unlink(linebuf);
83 this.m_flag = MUSED|MNEW;
89 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
90 if (append(&this, mestmp)) {
91 perror("temporary file");
97 count = strlen(linebuf);
98 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
103 linebuf[count - 1] = 0;
104 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
106 if (append(&this, mestmp)) {
107 perror("temporary file");
110 this.m_flag = MUSED|MNEW;
113 this.m_block = blockof(offset);
114 this.m_offset = boffsetof(offset);
116 } else if (linebuf[0] == 0) {
119 for (cp = linebuf, cp2 = "status";; cp++) {
120 if ((c = *cp2++) == 0) {
121 while (isspace(*cp++))
127 this.m_flag |= MREAD;
129 this.m_flag &= ~MNEW;
133 if (*cp != c && *cp != toupper(c))
138 this.m_size += count;
140 maybe = linebuf[0] == 0;
145 * Drop the passed line onto the passed output buffer.
146 * If a write error occurs, return -1, else the count of
147 * characters written, including the newline.
150 putline(obuf, linebuf)
157 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
158 (void) putc('\n', obuf);
165 * Read up a line from the specified input into the line
166 * buffer. Return the number of characters read. Do not
167 * include the newline at the end.
170 readline(ibuf, linebuf, linesize)
178 if (fgets(linebuf, linesize, ibuf) == NULL)
181 if (n > 0 && linebuf[n - 1] == '\n')
187 * Return a file buffer all ready to read up the
188 * passed message pointer.
192 register struct message *mp;
196 if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
198 panic("Temporary file seek");
204 * Take the data out of the passed ghost file and toss it into
205 * a dynamically allocated message structure.
211 register size = (msgCount + 1) * sizeof (struct message);
214 free((char *) message);
215 if ((message = (struct message *) malloc((unsigned) size)) == 0)
216 panic("Insufficient memory for %d messages", msgCount);
218 size -= sizeof (struct message);
220 (void) lseek(fileno(f), (off_t)sizeof *message, 0);
221 if (read(fileno(f), (char *) message, size) != size)
222 panic("Message temporary file corrupted");
223 message[msgCount].m_size = 0;
224 message[msgCount].m_lines = 0;
229 * Append the passed message descriptor onto the temp file.
230 * If the write fails, return 1, else 0
237 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
241 * Delete a file, but only if the file is a plain file.
249 if (stat(name, &sb) < 0)
251 if (!S_ISREG(sb.st_mode)) {
255 return(unlink(name));
258 static int sigdepth; /* depth of holdsigs() */
261 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
268 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
272 * Release signals SIGHUP, SIGINT, and SIGQUIT.
283 * Determine the size of the file possessed by
292 if (fstat(fileno(iob), &sbuf) < 0)
298 * Evaluate the string given as a new mailbox name.
299 * Supported meta characters:
300 * % for my system mail box
301 * %user for user's system mail box
302 * # for previous file
303 * & invoker's mbox file
304 * +file file in folder directory
305 * any shell meta character
306 * Return the file name as a dynamic string.
312 char xname[PATHSIZE];
313 char cmdbuf[PATHSIZE]; /* also used for file names */
315 register char *cp, *shell;
318 extern union wait wait_status;
321 * The order of evaluation is "%" and "#" expand into constants.
322 * "&" can expand into "+". "+" can expand into shell meta characters.
323 * Shell meta characters expand into constants.
324 * This way, we make no recursive expansion.
328 findmail(name[1] ? name + 1 : myname, xname);
329 return savestr(xname);
333 if (prevfile[0] == 0) {
334 printf("No previous file\n");
337 return savestr(prevfile);
339 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
343 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
344 sprintf(xname, "%s/%s", cmdbuf, name + 1);
345 name = savestr(xname);
347 /* catch the most common shell meta character */
348 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
349 sprintf(xname, "%s%s", homedir, name + 1);
350 name = savestr(xname);
352 if (!anyof(name, "~{[*?$`'\"\\"))
354 if (pipe(pivec) < 0) {
358 sprintf(cmdbuf, "echo %s", name);
359 if ((shell = value("SHELL")) == NOSTR)
360 shell = _PATH_CSHELL;
361 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
368 l = read(pivec[0], xname, BUFSIZ);
370 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
371 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
379 fprintf(stderr, "\"%s\": No match.\n", name);
383 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
387 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
390 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
391 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
394 return savestr(xname);
398 * Determine the current folder directory name.
406 if ((folder = value("folder")) == NOSTR)
409 strcpy(name, folder);
411 sprintf(name, "%s/%s", homedir, folder);
416 * Return the name of the dead.letter file.
423 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
424 cp = expand("~/dead.letter");
425 else if (*cp != '/') {
428 (void) sprintf(buf, "~/%s", cp);