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
35 static char sccsid[] = "@(#)fio.c 8.1 (Berkeley) 6/6/93";
48 * Mail -- a mail program
54 * Set up the input pointers while copying the mail file into /tmp.
61 register int c, count;
62 register char *cp, *cp2;
67 char linebuf[LINESIZE];
69 /* Get temporary file. */
70 (void)sprintf(linebuf, "%s/mail.XXXXXX", tmpdir);
71 if ((c = mkstemp(linebuf)) == -1 ||
72 (mestmp = Fdopen(c, "r+")) == NULL) {
73 (void)fprintf(stderr, "mail: can't open %s\n", linebuf);
76 (void)unlink(linebuf);
82 this.m_flag = MUSED|MNEW;
88 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
89 if (append(&this, mestmp)) {
90 perror("temporary file");
96 count = strlen(linebuf);
97 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
102 linebuf[count - 1] = 0;
103 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
105 if (append(&this, mestmp)) {
106 perror("temporary file");
109 this.m_flag = MUSED|MNEW;
112 this.m_block = blockof(offset);
113 this.m_offset = offsetof(offset);
115 } else if (linebuf[0] == 0) {
118 for (cp = linebuf, cp2 = "status";; cp++) {
119 if ((c = *cp2++) == 0) {
120 while (isspace(*cp++))
126 this.m_flag |= MREAD;
128 this.m_flag &= ~MNEW;
132 if (*cp != c && *cp != toupper(c))
137 this.m_size += count;
139 maybe = linebuf[0] == 0;
144 * Drop the passed line onto the passed output buffer.
145 * If a write error occurs, return -1, else the count of
146 * characters written, including the newline.
149 putline(obuf, linebuf)
156 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
157 (void) putc('\n', obuf);
164 * Read up a line from the specified input into the line
165 * buffer. Return the number of characters read. Do not
166 * include the newline at the end.
169 readline(ibuf, linebuf, linesize)
177 if (fgets(linebuf, linesize, ibuf) == NULL)
180 if (n > 0 && linebuf[n - 1] == '\n')
186 * Return a file buffer all ready to read up the
187 * passed message pointer.
191 register struct message *mp;
195 if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
197 panic("temporary file seek");
203 * Take the data out of the passed ghost file and toss it into
204 * a dynamically allocated message structure.
210 register size = (msgCount + 1) * sizeof (struct message);
213 free((char *) message);
214 if ((message = (struct message *) malloc((unsigned) size)) == 0)
215 panic("Insufficient memory for %d messages", msgCount);
217 size -= sizeof (struct message);
219 (void) lseek(fileno(f), (off_t)sizeof *message, 0);
220 if (read(fileno(f), (char *) message, size) != size)
221 panic("Message temporary file corrupted");
222 message[msgCount].m_size = 0;
223 message[msgCount].m_lines = 0;
228 * Append the passed message descriptor onto the temp file.
229 * If the write fails, return 1, else 0
236 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
240 * Delete a file, but only if the file is a plain file.
248 if (stat(name, &sb) < 0)
250 if (!S_ISREG(sb.st_mode)) {
254 return(unlink(name));
257 static int sigdepth; /* depth of holdsigs() */
260 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
267 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
271 * Release signals SIGHUP, SIGINT, and SIGQUIT.
282 * Determine the size of the file possessed by
291 if (fstat(fileno(iob), &sbuf) < 0)
297 * Evaluate the string given as a new mailbox name.
298 * Supported meta characters:
299 * % for my system mail box
300 * %user for user's system mail box
301 * # for previous file
302 * & invoker's mbox file
303 * +file file in folder directory
304 * any shell meta character
305 * Return the file name as a dynamic string.
311 char xname[PATHSIZE];
312 char cmdbuf[PATHSIZE]; /* also used for file names */
314 register char *cp, *shell;
317 extern union wait wait_status;
320 * The order of evaluation is "%" and "#" expand into constants.
321 * "&" can expand into "+". "+" can expand into shell meta characters.
322 * Shell meta characters expand into constants.
323 * This way, we make no recursive expansion.
327 findmail(name[1] ? name + 1 : myname, xname);
328 return savestr(xname);
332 if (prevfile[0] == 0) {
333 printf("No previous file\n");
336 return savestr(prevfile);
338 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
342 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
343 sprintf(xname, "%s/%s", cmdbuf, name + 1);
344 name = savestr(xname);
346 /* catch the most common shell meta character */
347 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
348 sprintf(xname, "%s%s", homedir, name + 1);
349 name = savestr(xname);
351 if (!anyof(name, "~{[*?$`'\"\\"))
353 if (pipe(pivec) < 0) {
357 sprintf(cmdbuf, "echo %s", name);
358 if ((shell = value("SHELL")) == NOSTR)
359 shell = _PATH_CSHELL;
360 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
367 l = read(pivec[0], xname, BUFSIZ);
369 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
370 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
378 fprintf(stderr, "\"%s\": No match.\n", name);
382 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
386 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
389 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
390 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
393 return savestr(xname);
397 * Determine the current folder directory name.
405 if ((folder = value("folder")) == NOSTR)
408 strcpy(name, folder);
410 sprintf(name, "%s/%s", homedir, folder);
415 * Return the name of the dead.letter file.
422 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
423 cp = expand("~/dead.letter");
424 else if (*cp != '/') {
427 (void) sprintf(buf, "~/%s", cp);