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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)fio.c 8.2 (Berkeley) 4/20/95
30 * $FreeBSD: src/usr.bin/mail/fio.c,v 1.2.6.4 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/fio.c,v 1.4 2004/09/08 03:01:11 joerg Exp $
44 * Mail -- a mail program
49 extern int wait_status;
52 * Set up the input pointers while copying the mail file into /tmp.
55 setptr(FILE *ibuf, off_t offset)
62 char linebuf[LINESIZE], pathbuf[PATHSIZE];
65 /* Get temporary file. */
66 snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
67 if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
68 err(1, "can't open %s", pathbuf);
74 /* Seek into the file to get to the new messages */
75 fseeko(ibuf, offset, SEEK_SET);
77 * We need to make "offset" a pointer to the end of
78 * the temp file that has the copy of the mail file.
79 * If any messages have been edited, this will be
80 * different from the offset into the mail file.
82 fseeko(otf, (off_t)0, SEEK_END);
88 this.m_flag = MUSED|MNEW;
94 if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
95 if (append(&this, mestmp))
96 errx(1, "temporary file");
97 makemessage(mestmp, omsgCount);
100 count = strlen(linebuf);
102 * Transforms lines ending in <CR><LF> to just <LF>.
103 * This allows mail to be able to read Eudora mailboxes.
105 if (count >= 2 && linebuf[count - 1] == '\n' &&
106 linebuf[count - 2] == '\r') {
108 linebuf[count - 1] = '\n';
111 fwrite(linebuf, sizeof(*linebuf), count, otf);
115 linebuf[count - 1] = '\0';
116 if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
118 if (append(&this, mestmp))
119 errx(1, "temporary file");
120 this.m_flag = MUSED|MNEW;
123 this.m_block = blockof(offset);
124 this.m_offset = boffsetof(offset);
126 } else if (linebuf[0] == 0) {
129 for (cp = linebuf, cp2 = "status";; cp++) {
130 if ((c = *cp2++) == '\0') {
131 while (isspace((unsigned char)*cp++))
135 while ((c = *cp++) != '\0')
137 this.m_flag |= MREAD;
139 this.m_flag &= ~MNEW;
143 if (*cp != c && *cp != toupper((unsigned char)c))
148 this.m_size += count;
150 maybe = linebuf[0] == 0;
155 * Drop the passed line onto the passed output buffer.
156 * If a write error occurs, return -1, else the count of
157 * characters written, including the newline if requested.
160 putline(FILE *obuf, char *linebuf, int outlf)
165 fwrite(linebuf, sizeof(*linebuf), c, obuf);
176 * Read up a line from the specified input into the line
177 * buffer. Return the number of characters read. Do not
178 * include the newline (or carriage return) at the end.
181 readline(FILE *ibuf, char *linebuf, int linesize)
186 if (fgets(linebuf, linesize, ibuf) == NULL)
189 if (n > 0 && linebuf[n - 1] == '\n')
191 if (n > 0 && linebuf[n - 1] == '\r')
197 * Return a file buffer all ready to read up the
198 * passed message pointer.
201 setinput(struct message *mp)
205 positionof(mp->m_block, mp->m_offset), SEEK_SET) < 0)
211 * Take the data out of the passed ghost file and toss it into
212 * a dynamically allocated message structure.
215 makemessage(FILE *f, int omsgCount)
218 struct message *nmessage;
220 size = (msgCount + 1) * sizeof(struct message);
221 nmessage = (struct message *)realloc(message, size);
222 if (nmessage == NULL)
223 errx(1, "Insufficient memory for %d messages\n",
225 if (omsgCount == 0 || message == NULL)
228 dot = nmessage + (dot - message);
230 size -= (omsgCount + 1) * sizeof(struct message);
232 lseek(fileno(f), (off_t)sizeof(*message), 0);
233 if (read(fileno(f), (char *)&message[omsgCount], size) != size)
234 errx(1, "Message temporary file corrupted");
235 message[msgCount].m_size = 0;
236 message[msgCount].m_lines = 0;
241 * Append the passed message descriptor onto the temp file.
242 * If the write fails, return 1, else 0
245 append(struct message *mp, FILE *f)
247 return (fwrite((char *)mp, sizeof(*mp), 1, f) != 1);
251 * Delete a file, but only if the file is a plain file.
258 if (stat(name, &sb) < 0)
260 if (!S_ISREG(sb.st_mode)) {
264 return (unlink(name));
267 static int sigdepth; /* depth of holdsigs() */
268 static sigset_t nset, oset;
270 * Hold signals SIGHUP, SIGINT, and SIGQUIT.
275 if (sigdepth++ == 0) {
277 sigaddset(&nset, SIGHUP);
278 sigaddset(&nset, SIGINT);
279 sigaddset(&nset, SIGQUIT);
280 sigprocmask(SIG_BLOCK, &nset, &oset);
285 * Release signals SIGHUP, SIGINT, and SIGQUIT.
291 sigprocmask(SIG_SETMASK, &oset, NULL);
295 * Determine the size of the file possessed by
303 if (fstat(fileno(iob), &sbuf) < 0)
305 return (sbuf.st_size);
309 * Evaluate the string given as a new mailbox name.
310 * Supported meta characters:
311 * % for my system mail box
312 * %user for user's system mail box
313 * # for previous file
314 * & invoker's mbox file
315 * +file file in folder directory
316 * any shell meta character
317 * Return the file name as a dynamic string.
322 char xname[PATHSIZE];
323 char cmdbuf[PATHSIZE]; /* also used for file names */
330 * The order of evaluation is "%" and "#" expand into constants.
331 * "&" can expand into "+". "+" can expand into shell meta characters.
332 * Shell meta characters expand into constants.
333 * This way, we make no recursive expansion.
337 findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
338 return (savestr(xname));
342 if (prevfile[0] == 0) {
343 printf("No previous file\n");
346 return (savestr(prevfile));
348 if (name[1] == 0 && (name = value("MBOX")) == NULL)
352 if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
353 snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
354 name = savestr(xname);
356 /* catch the most common shell meta character */
357 if (name[0] == '~' && homedir != NULL &&
358 (name[1] == '/' || name[1] == '\0')) {
359 snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
360 name = savestr(xname);
362 if (!strpbrk(name, "~{[*?$`'\"\\"))
364 if (pipe(pivec) < 0) {
368 snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
369 if ((sh = value("SHELL")) == NULL)
371 pid = start_command(sh, 0, -1, pivec[1], "-c", cmdbuf, NULL);
378 l = read(pivec[0], xname, BUFSIZ);
380 if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) &&
381 WTERMSIG(wait_status) != SIGPIPE) {
382 fprintf(stderr, "\"%s\": Expansion failed.\n", name);
390 fprintf(stderr, "\"%s\": No match.\n", name);
394 fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
398 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
401 if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
402 fprintf(stderr, "\"%s\": Ambiguous.\n", name);
405 return (savestr(xname));
409 * Determine the current folder directory name.
412 getfold(char *name, int namelen)
417 if ((folder = value("folder")) == NULL)
420 copylen = strlcpy(name, folder, namelen);
422 copylen = snprintf(name, namelen, "%s/%s",
423 homedir ? homedir : ".", folder);
424 return (copylen < 0 || copylen >= namelen ? (-1) : (0));
428 * Return the name of the dead.letter file.
435 if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
436 cp = expand("~/dead.letter");
437 else if (*cp != '/') {
440 snprintf(buf, sizeof(buf), "~/%s", cp);