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
33 * @(#)fio.c 8.2 (Berkeley) 4/20/95
34 * $FreeBSD: src/usr.bin/mail/fio.c,v 1.2.6.4 2003/01/06 05:46:03 mikeh Exp $
35 * $DragonFly: src/usr.bin/mail/fio.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
48 * Mail -- a mail program
53 extern int wait_status;
56 * Set up the input pointers while copying the mail file into /tmp.
68 char linebuf[LINESIZE], pathbuf[PATHSIZE];
71 /* Get temporary file. */
72 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
73 if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
74 err(1, "can't open %s", pathbuf);
80 /* Seek into the file to get to the new messages */
81 (void)fseeko(ibuf, offset, SEEK_SET);
83 * We need to make "offset" a pointer to the end of
84 * the temp file that has the copy of the mail file.
85 * If any messages have been edited, this will be
86 * different from the offset into the mail file.
88 (void)fseeko(otf, (off_t)0, SEEK_END);
94 this.m_flag = MUSED|MNEW;
100 if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
101 if (append(&this, mestmp))
102 errx(1, "temporary file");
103 makemessage(mestmp, omsgCount);
106 count = strlen(linebuf);
108 * Transforms lines ending in <CR><LF> to just <LF>.
109 * This allows mail to be able to read Eudora mailboxes.
111 if (count >= 2 && linebuf[count - 1] == '\n' &&
112 linebuf[count - 2] == '\r') {
114 linebuf[count - 1] = '\n';
117 (void)fwrite(linebuf, sizeof(*linebuf), count, otf);
121 linebuf[count - 1] = '\0';
122 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
124 if (append(&this, mestmp))
125 errx(1, "temporary file");
126 this.m_flag = MUSED|MNEW;
129 this.m_block = blockof(offset);
130 this.m_offset = boffsetof(offset);
132 } else if (linebuf[0] == 0) {
135 for (cp = linebuf, cp2 = "status";; cp++) {
136 if ((c = *cp2++) == '\0') {
137 while (isspace((unsigned char)*cp++))
141 while ((c = *cp++) != '\0')
143 this.m_flag |= MREAD;
145 this.m_flag &= ~MNEW;
149 if (*cp != c && *cp != toupper((unsigned char)c))
154 this.m_size += count;
156 maybe = linebuf[0] == 0;
161 * Drop the passed line onto the passed output buffer.
162 * If a write error occurs, return -1, else the count of
163 * characters written, including the newline if requested.
166 putline(obuf, linebuf, outlf)
174 (void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
185 * Read up a line from the specified input into the line
186 * buffer. Return the number of characters read. Do not
187 * include the newline (or carriage return) at the end.
190 readline(ibuf, linebuf, linesize)
198 if (fgets(linebuf, linesize, ibuf) == NULL)
201 if (n > 0 && linebuf[n - 1] == '\n')
203 if (n > 0 && linebuf[n - 1] == '\r')
209 * Return a file buffer all ready to read up the
210 * passed message pointer.
219 positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
225 * Take the data out of the passed ghost file and toss it into
226 * a dynamically allocated message structure.
229 makemessage(f, omsgCount)
234 struct message *nmessage;
236 size = (msgCount + 1) * sizeof(struct message);
237 nmessage = (struct message *)realloc(message, size);
238 if (nmessage == NULL)
239 errx(1, "Insufficient memory for %d messages\n",
241 if (omsgCount == 0 || message == NULL)
244 dot = nmessage + (dot - message);
246 size -= (omsgCount + 1) * sizeof(struct message);
248 (void)lseek(fileno(f), (off_t)sizeof(*message), 0);
249 if (read(fileno(f), (char *)&message[omsgCount], size) != size)
250 errx(1, "Message temporary file corrupted");
251 message[msgCount].m_size = 0;
252 message[msgCount].m_lines = 0;
257 * Append the passed message descriptor onto the temp file.
258 * If the write fails, return 1, else 0
265 return (fwrite((char *)mp, sizeof(*mp), 1, f) != 1);
269 * Delete a file, but only if the file is a plain file.
277 if (stat(name, &sb) < 0)
279 if (!S_ISREG(sb.st_mode)) {
283 return (unlink(name));
286 static int sigdepth; /* depth of holdsigs() */
287 static sigset_t nset, oset;
289 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
295 if (sigdepth++ == 0) {
296 (void)sigemptyset(&nset);
297 (void)sigaddset(&nset, SIGHUP);
298 (void)sigaddset(&nset, SIGINT);
299 (void)sigaddset(&nset, SIGQUIT);
300 (void)sigprocmask(SIG_BLOCK, &nset, &oset);
305 * Release signals SIGHUP, SIGINT, and SIGQUIT.
312 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
316 * Determine the size of the file possessed by
325 if (fstat(fileno(iob), &sbuf) < 0)
327 return (sbuf.st_size);
331 * Evaluate the string given as a new mailbox name.
332 * Supported meta characters:
333 * % for my system mail box
334 * %user for user's system mail box
335 * # for previous file
336 * & invoker's mbox file
337 * +file file in folder directory
338 * any shell meta character
339 * Return the file name as a dynamic string.
345 char xname[PATHSIZE];
346 char cmdbuf[PATHSIZE]; /* also used for file names */
353 * The order of evaluation is "%" and "#" expand into constants.
354 * "&" can expand into "+". "+" can expand into shell meta characters.
355 * Shell meta characters expand into constants.
356 * This way, we make no recursive expansion.
360 findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
361 return (savestr(xname));
365 if (prevfile[0] == 0) {
366 printf("No previous file\n");
369 return (savestr(prevfile));
371 if (name[1] == 0 && (name = value("MBOX")) == NULL)
375 if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
376 (void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
377 name = savestr(xname);
379 /* catch the most common shell meta character */
380 if (name[0] == '~' && homedir != NULL &&
381 (name[1] == '/' || name[1] == '\0')) {
382 (void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
383 name = savestr(xname);
385 if (!strpbrk(name, "~{[*?$`'\"\\"))
387 if (pipe(pivec) < 0) {
391 (void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
392 if ((sh = value("SHELL")) == NULL)
394 pid = start_command(sh, 0, -1, pivec[1], "-c", cmdbuf, NULL);
396 (void)close(pivec[0]);
397 (void)close(pivec[1]);
400 (void)close(pivec[1]);
401 l = read(pivec[0], xname, BUFSIZ);
402 (void)close(pivec[0]);
403 if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) &&
404 WTERMSIG(wait_status) != SIGPIPE) {
405 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
413 fprintf(stderr, "\"%s\": No match.\n", name);
417 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
421 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
424 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
425 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
428 return (savestr(xname));
432 * Determine the current folder directory name.
435 getfold(name, namelen)
442 if ((folder = value("folder")) == NULL)
445 copylen = strlcpy(name, folder, namelen);
447 copylen = snprintf(name, namelen, "%s/%s",
448 homedir ? homedir : ".", folder);
449 return (copylen < 0 || copylen >= namelen ? (-1) : (0));
453 * Return the name of the dead.letter file.
460 if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
461 cp = expand("~/dead.letter");
462 else if (*cp != '/') {
465 (void)snprintf(buf, sizeof(buf), "~/%s", cp);