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 errx(1, "can't open %s", linebuf);
75 (void)unlink(linebuf);
81 this.m_flag = MUSED|MNEW;
87 if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
88 if (append(&this, mestmp)) {
89 perror("temporary file");
95 count = strlen(linebuf);
96 (void) fwrite(linebuf, sizeof *linebuf, count, otf);
101 linebuf[count - 1] = 0;
102 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
104 if (append(&this, mestmp)) {
105 perror("temporary file");
108 this.m_flag = MUSED|MNEW;
111 this.m_block = blockof(offset);
112 this.m_offset = offsetof(offset);
114 } else if (linebuf[0] == 0) {
117 for (cp = linebuf, cp2 = "status";; cp++) {
118 if ((c = *cp2++) == 0) {
119 while (isspace(*cp++))
125 this.m_flag |= MREAD;
127 this.m_flag &= ~MNEW;
131 if (*cp != c && *cp != toupper(c))
136 this.m_size += count;
138 maybe = linebuf[0] == 0;
143 * Drop the passed line onto the passed output buffer.
144 * If a write error occurs, return -1, else the count of
145 * characters written, including the newline.
148 putline(obuf, linebuf)
155 (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
156 (void) putc('\n', obuf);
163 * Read up a line from the specified input into the line
164 * buffer. Return the number of characters read. Do not
165 * include the newline at the end.
168 readline(ibuf, linebuf, linesize)
176 if (fgets(linebuf, linesize, ibuf) == NULL)
179 if (n > 0 && linebuf[n - 1] == '\n')
185 * Return a file buffer all ready to read up the
186 * passed message pointer.
190 register struct message *mp;
194 if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
196 panic("Temporary file seek");
202 * Take the data out of the passed ghost file and toss it into
203 * a dynamically allocated message structure.
209 register size = (msgCount + 1) * sizeof (struct message);
212 free((char *) message);
213 if ((message = (struct message *) malloc((unsigned) size)) == 0)
214 panic("Insufficient memory for %d messages", msgCount);
216 size -= sizeof (struct message);
218 (void) lseek(fileno(f), (off_t)sizeof *message, 0);
219 if (read(fileno(f), (char *) message, size) != size)
220 panic("Message temporary file corrupted");
221 message[msgCount].m_size = 0;
222 message[msgCount].m_lines = 0;
227 * Append the passed message descriptor onto the temp file.
228 * If the write fails, return 1, else 0
235 return fwrite((char *) mp, sizeof *mp, 1, f) != 1;
239 * Delete a file, but only if the file is a plain file.
247 if (stat(name, &sb) < 0)
249 if (!S_ISREG(sb.st_mode)) {
253 return(unlink(name));
256 static int sigdepth; /* depth of holdsigs() */
259 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
266 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
270 * Release signals SIGHUP, SIGINT, and SIGQUIT.
281 * Determine the size of the file possessed by
290 if (fstat(fileno(iob), &sbuf) < 0)
296 * Evaluate the string given as a new mailbox name.
297 * Supported meta characters:
298 * % for my system mail box
299 * %user for user's system mail box
300 * # for previous file
301 * & invoker's mbox file
302 * +file file in folder directory
303 * any shell meta character
304 * Return the file name as a dynamic string.
310 char xname[PATHSIZE];
311 char cmdbuf[PATHSIZE]; /* also used for file names */
313 register char *cp, *shell;
316 extern union wait wait_status;
319 * The order of evaluation is "%" and "#" expand into constants.
320 * "&" can expand into "+". "+" can expand into shell meta characters.
321 * Shell meta characters expand into constants.
322 * This way, we make no recursive expansion.
326 findmail(name[1] ? name + 1 : myname, xname);
327 return savestr(xname);
331 if (prevfile[0] == 0) {
332 printf("No previous file\n");
335 return savestr(prevfile);
337 if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
341 if (name[0] == '+' && getfold(cmdbuf) >= 0) {
342 sprintf(xname, "%s/%s", cmdbuf, name + 1);
343 name = savestr(xname);
345 /* catch the most common shell meta character */
346 if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
347 sprintf(xname, "%s%s", homedir, name + 1);
348 name = savestr(xname);
350 if (!anyof(name, "~{[*?$`'\"\\"))
352 if (pipe(pivec) < 0) {
356 sprintf(cmdbuf, "echo %s", name);
357 if ((shell = value("SHELL")) == NOSTR)
358 shell = _PATH_CSHELL;
359 pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
366 l = read(pivec[0], xname, BUFSIZ);
368 if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
369 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
377 fprintf(stderr, "\"%s\": No match.\n", name);
381 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
385 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
388 if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
389 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
392 return savestr(xname);
396 * Determine the current folder directory name.
404 if ((folder = value("folder")) == NOSTR)
407 strcpy(name, folder);
409 sprintf(name, "%s/%s", homedir, folder);
414 * Return the name of the dead.letter file.
421 if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
422 cp = expand("~/dead.letter");
423 else if (*cp != '/') {
426 (void) sprintf(buf, "~/%s", cp);