[BACK]Return to fio.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/fio.c, Revision 1.13

1.13    ! millert     1: /*     $OpenBSD: fio.c,v 1.12 1997/08/31 14:32:13 millert Exp $        */
1.6       millert     2: /*     $NetBSD: fio.c,v 1.8 1997/07/07 22:57:55 phil Exp $     */
1.2       deraadt     3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
1.2       deraadt    38: #if 0
1.6       millert    39: static char sccsid[] = "@(#)fio.c      8.2 (Berkeley) 4/20/95";
1.2       deraadt    40: #else
1.13    ! millert    41: static char rcsid[] = "$OpenBSD: fio.c,v 1.12 1997/08/31 14:32:13 millert Exp $";
1.2       deraadt    42: #endif
1.1       deraadt    43: #endif /* not lint */
                     44:
                     45: #include "rcv.h"
                     46: #include <sys/file.h>
                     47: #include <sys/wait.h>
                     48:
                     49: #include <unistd.h>
                     50: #include <paths.h>
                     51: #include <errno.h>
                     52: #include "extern.h"
                     53:
                     54: /*
                     55:  * Mail -- a mail program
                     56:  *
                     57:  * File I/O.
                     58:  */
                     59:
                     60: /*
                     61:  * Set up the input pointers while copying the mail file into /tmp.
                     62:  */
                     63: void
1.6       millert    64: setptr(ibuf, offset)
1.1       deraadt    65:        register FILE *ibuf;
1.6       millert    66:        off_t offset;
1.1       deraadt    67: {
                     68:        register int c, count;
                     69:        register char *cp, *cp2;
                     70:        struct message this;
                     71:        FILE *mestmp;
                     72:        int maybe, inhead;
1.5       deraadt    73:        char linebuf[LINESIZE], pathbuf[PATHSIZE];
1.6       millert    74:        int omsgCount;
1.1       deraadt    75:
                     76:        /* Get temporary file. */
1.6       millert    77:        (void)snprintf(pathbuf, sizeof(pathbuf), "%s/mail.XXXXXXXXXX", tmpdir);
                     78:        if ((c = mkstemp(pathbuf)) == -1 || (mestmp = Fdopen(c, "r+")) == NULL)
                     79:                err(1, "can't open %s", pathbuf);
1.10      millert    80:        (void)rm(pathbuf);
1.1       deraadt    81:
1.6       millert    82:        if (offset == 0) {
                     83:                msgCount = 0;
                     84:        } else {
                     85:                /* Seek into the file to get to the new messages */
1.7       millert    86:                (void)fseek(ibuf, offset, 0);
1.6       millert    87:                /*
                     88:                 * We need to make "offset" a pointer to the end of
                     89:                 * the temp file that has the copy of the mail file.
                     90:                 * If any messages have been edited, this will be
                     91:                 * different from the offset into the mail file.
                     92:                 */
1.7       millert    93:                (void)fseek(otf, 0L, SEEK_END);
1.6       millert    94:                offset = ftell(otf);
                     95:        }
                     96:        omsgCount = msgCount;
1.1       deraadt    97:        maybe = 1;
                     98:        inhead = 0;
                     99:        this.m_flag = MUSED|MNEW;
                    100:        this.m_size = 0;
                    101:        this.m_lines = 0;
                    102:        this.m_block = 0;
                    103:        this.m_offset = 0;
                    104:        for (;;) {
1.6       millert   105:                if (fgets(linebuf, sizeof(linebuf), ibuf) == NULL) {
                    106:                        if (append(&this, mestmp))
                    107:                                err(1, "temporary file");
                    108:                        makemessage(mestmp, omsgCount);
1.1       deraadt   109:                        return;
                    110:                }
                    111:                count = strlen(linebuf);
1.13    ! millert   112:                /*
        !           113:                 * Transforms lines ending in <CR><LF> to just <LF>.
        !           114:                 * This allows mail to be able to read Eudora mailboxes
        !           115:                 * that reside on a DOS partition.
        !           116:                 */
        !           117:                if (count >= 2 && linebuf[count-1] == '\n' &&
        !           118:                    linebuf[count - 2] == '\r')
        !           119:                        linebuf[count - 2] = linebuf[--count];
        !           120:
1.7       millert   121:                (void)fwrite(linebuf, sizeof(*linebuf), count, otf);
1.6       millert   122:                if (ferror(otf))
                    123:                        err(1, "/tmp");
                    124:                linebuf[count - 1] = '\0';
1.1       deraadt   125:                if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                    126:                        msgCount++;
1.6       millert   127:                        if (append(&this, mestmp))
                    128:                                err(1, "temporary file");
1.1       deraadt   129:                        this.m_flag = MUSED|MNEW;
                    130:                        this.m_size = 0;
                    131:                        this.m_lines = 0;
                    132:                        this.m_block = blockof(offset);
                    133:                        this.m_offset = offsetof(offset);
                    134:                        inhead = 1;
                    135:                } else if (linebuf[0] == 0) {
                    136:                        inhead = 0;
                    137:                } else if (inhead) {
                    138:                        for (cp = linebuf, cp2 = "status";; cp++) {
                    139:                                if ((c = *cp2++) == 0) {
                    140:                                        while (isspace(*cp++))
                    141:                                                ;
                    142:                                        if (cp[-1] != ':')
                    143:                                                break;
1.2       deraadt   144:                                        while ((c = *cp++) != '\0')
1.1       deraadt   145:                                                if (c == 'R')
                    146:                                                        this.m_flag |= MREAD;
                    147:                                                else if (c == 'O')
                    148:                                                        this.m_flag &= ~MNEW;
                    149:                                        inhead = 0;
                    150:                                        break;
                    151:                                }
                    152:                                if (*cp != c && *cp != toupper(c))
                    153:                                        break;
                    154:                        }
                    155:                }
                    156:                offset += count;
                    157:                this.m_size += count;
                    158:                this.m_lines++;
                    159:                maybe = linebuf[0] == 0;
                    160:        }
                    161: }
                    162:
                    163: /*
                    164:  * Drop the passed line onto the passed output buffer.
                    165:  * If a write error occurs, return -1, else the count of
1.6       millert   166:  * characters written, including the newline if requested.
1.1       deraadt   167:  */
                    168: int
1.6       millert   169: putline(obuf, linebuf, outlf)
1.1       deraadt   170:        FILE *obuf;
                    171:        char *linebuf;
1.6       millert   172:        int   outlf;
1.1       deraadt   173: {
                    174:        register int c;
                    175:
                    176:        c = strlen(linebuf);
1.7       millert   177:        (void)fwrite(linebuf, sizeof(*linebuf), c, obuf);
1.6       millert   178:        if (outlf) {
1.7       millert   179:                (void)putc('\n', obuf);
1.6       millert   180:                c++;
                    181:        }
1.1       deraadt   182:        if (ferror(obuf))
1.6       millert   183:                return(-1);
                    184:        return(c);
1.1       deraadt   185: }
                    186:
                    187: /*
                    188:  * Read up a line from the specified input into the line
                    189:  * buffer.  Return the number of characters read.  Do not
1.13    ! millert   190:  * include the newline (or carriage return) at the end.
1.1       deraadt   191:  */
                    192: int
                    193: readline(ibuf, linebuf, linesize)
                    194:        FILE *ibuf;
                    195:        char *linebuf;
                    196:        int linesize;
                    197: {
                    198:        register int n;
                    199:
                    200:        clearerr(ibuf);
                    201:        if (fgets(linebuf, linesize, ibuf) == NULL)
1.6       millert   202:                return(-1);
                    203:
1.1       deraadt   204:        n = strlen(linebuf);
                    205:        if (n > 0 && linebuf[n - 1] == '\n')
1.13    ! millert   206:                linebuf[--n] = '\0';
        !           207:        if (n > 0 && linebuf[n - 1] == '\r')
1.1       deraadt   208:                linebuf[--n] = '\0';
1.6       millert   209:        return(n);
1.1       deraadt   210: }
                    211:
                    212: /*
                    213:  * Return a file buffer all ready to read up the
                    214:  * passed message pointer.
                    215:  */
                    216: FILE *
                    217: setinput(mp)
                    218:        register struct message *mp;
                    219: {
                    220:
                    221:        fflush(otf);
                    222:        if (fseek(itf, (long)positionof(mp->m_block, mp->m_offset), 0) < 0) {
1.6       millert   223:                warn("fseek");
1.1       deraadt   224:                panic("temporary file seek");
                    225:        }
1.6       millert   226:        return(itf);
1.1       deraadt   227: }
                    228:
                    229: /*
                    230:  * Take the data out of the passed ghost file and toss it into
                    231:  * a dynamically allocated message structure.
                    232:  */
                    233: void
1.6       millert   234: makemessage(f, omsgCount)
1.1       deraadt   235:        FILE *f;
1.6       millert   236:        int omsgCount;
1.1       deraadt   237: {
1.7       millert   238:        register size_t size = (msgCount + 1) * sizeof(struct message);
1.1       deraadt   239:
1.6       millert   240:        if (omsgCount) {
1.7       millert   241:                message = (struct message *)realloc(message, size);
1.6       millert   242:                if (message == 0)
                    243:                        panic("Insufficient memory for %d messages\n", msgCount);
                    244:        } else {
                    245:                if (message != 0)
1.7       millert   246:                        (void)free(message);
1.11      millert   247:                if ((message = (struct message *)malloc(size)) == NULL)
1.6       millert   248:                        panic("Insufficient memory for %d messages", msgCount);
                    249:                dot = message;
                    250:        }
                    251:        size -= (omsgCount + 1) * sizeof(struct message);
1.1       deraadt   252:        fflush(f);
1.7       millert   253:        (void)lseek(fileno(f), (off_t)sizeof(*message), 0);
1.6       millert   254:        if (read(fileno(f), (void *) &message[omsgCount], size) != size)
1.1       deraadt   255:                panic("Message temporary file corrupted");
                    256:        message[msgCount].m_size = 0;
                    257:        message[msgCount].m_lines = 0;
1.6       millert   258:        (void)Fclose(f);
1.1       deraadt   259: }
                    260:
                    261: /*
                    262:  * Append the passed message descriptor onto the temp file.
                    263:  * If the write fails, return 1, else 0
                    264:  */
                    265: int
                    266: append(mp, f)
                    267:        struct message *mp;
                    268:        FILE *f;
                    269: {
1.6       millert   270:        return(fwrite((char *) mp, sizeof(*mp), 1, f) != 1);
1.1       deraadt   271: }
                    272:
                    273: /*
                    274:  * Delete a file, but only if the file is a plain file.
                    275:  */
                    276: int
                    277: rm(name)
                    278:        char *name;
                    279: {
                    280:        struct stat sb;
                    281:
                    282:        if (stat(name, &sb) < 0)
                    283:                return(-1);
                    284:        if (!S_ISREG(sb.st_mode)) {
                    285:                errno = EISDIR;
                    286:                return(-1);
                    287:        }
                    288:        return(unlink(name));
                    289: }
                    290:
                    291: static int sigdepth;           /* depth of holdsigs() */
1.2       deraadt   292: static sigset_t nset, oset;
1.1       deraadt   293: /*
                    294:  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
                    295:  */
                    296: void
                    297: holdsigs()
                    298: {
                    299:
1.2       deraadt   300:        if (sigdepth++ == 0) {
                    301:                sigemptyset(&nset);
                    302:                sigaddset(&nset, SIGHUP);
                    303:                sigaddset(&nset, SIGINT);
                    304:                sigaddset(&nset, SIGQUIT);
                    305:                sigprocmask(SIG_BLOCK, &nset, &oset);
                    306:        }
1.1       deraadt   307: }
                    308:
                    309: /*
                    310:  * Release signals SIGHUP, SIGINT, and SIGQUIT.
                    311:  */
                    312: void
                    313: relsesigs()
                    314: {
                    315:
                    316:        if (--sigdepth == 0)
1.2       deraadt   317:                sigprocmask(SIG_SETMASK, &oset, NULL);
1.1       deraadt   318: }
                    319:
                    320: /*
                    321:  * Determine the size of the file possessed by
                    322:  * the passed buffer.
                    323:  */
                    324: off_t
                    325: fsize(iob)
                    326:        FILE *iob;
                    327: {
                    328:        struct stat sbuf;
                    329:
                    330:        if (fstat(fileno(iob), &sbuf) < 0)
1.6       millert   331:                return(0);
                    332:        return(sbuf.st_size);
1.1       deraadt   333: }
                    334:
                    335: /*
                    336:  * Evaluate the string given as a new mailbox name.
                    337:  * Supported meta characters:
                    338:  *     %       for my system mail box
                    339:  *     %user   for user's system mail box
                    340:  *     #       for previous file
                    341:  *     &       invoker's mbox file
                    342:  *     +file   file in folder directory
                    343:  *     any shell meta character
                    344:  * Return the file name as a dynamic string.
                    345:  */
                    346: char *
                    347: expand(name)
                    348:        register char *name;
                    349: {
                    350:        char xname[PATHSIZE];
                    351:        char cmdbuf[PATHSIZE];          /* also used for file names */
                    352:        register int pid, l;
                    353:        register char *cp, *shell;
                    354:        int pivec[2];
                    355:        struct stat sbuf;
1.12      millert   356:        extern int wait_status;
1.1       deraadt   357:
                    358:        /*
                    359:         * The order of evaluation is "%" and "#" expand into constants.
                    360:         * "&" can expand into "+".  "+" can expand into shell meta characters.
                    361:         * Shell meta characters expand into constants.
                    362:         * This way, we make no recursive expansion.
                    363:         */
                    364:        switch (*name) {
                    365:        case '%':
1.6       millert   366:                findmail(name[1] ? name + 1 : myname, xname, sizeof(xname));
                    367:                return(savestr(xname));
1.1       deraadt   368:        case '#':
                    369:                if (name[1] != 0)
                    370:                        break;
                    371:                if (prevfile[0] == 0) {
1.6       millert   372:                        puts("No previous file");
1.8       millert   373:                        return(NULL);
1.1       deraadt   374:                }
1.6       millert   375:                return(savestr(prevfile));
1.1       deraadt   376:        case '&':
1.8       millert   377:                if (name[1] == 0 && (name = value("MBOX")) == NULL)
1.1       deraadt   378:                        name = "~/mbox";
                    379:                /* fall through */
                    380:        }
1.6       millert   381:        if (name[0] == '+' && getfold(cmdbuf, sizeof(cmdbuf)) >= 0) {
1.10      millert   382:                (void)snprintf(xname, sizeof(xname), "%s/%s", cmdbuf, name + 1);
1.1       deraadt   383:                name = savestr(xname);
                    384:        }
                    385:        /* catch the most common shell meta character */
                    386:        if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
1.10      millert   387:                (void)snprintf(xname, sizeof(xname), "%s%s", homedir, name + 1);
1.1       deraadt   388:                name = savestr(xname);
                    389:        }
                    390:        if (!anyof(name, "~{[*?$`'\"\\"))
1.6       millert   391:                return(name);
1.1       deraadt   392:        if (pipe(pivec) < 0) {
1.6       millert   393:                warn("pipe");
                    394:                return(name);
1.1       deraadt   395:        }
1.10      millert   396:        (void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
1.8       millert   397:        if ((shell = value("SHELL")) == NULL)
1.1       deraadt   398:                shell = _PATH_CSHELL;
1.8       millert   399:        pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NULL);
1.1       deraadt   400:        if (pid < 0) {
1.6       millert   401:                (void)close(pivec[0]);
                    402:                (void)close(pivec[1]);
1.8       millert   403:                return(NULL);
1.6       millert   404:        }
                    405:        (void)close(pivec[1]);
                    406:        l = read(pivec[0], xname, PATHSIZE);
                    407:        (void)close(pivec[0]);
1.12      millert   408:        if (wait_child(pid) < 0 && WIFSIGNALED(wait_status) &&
                    409:            WTERMSIG(wait_status) != SIGPIPE) {
1.1       deraadt   410:                fprintf(stderr, "\"%s\": Expansion failed.\n", name);
1.8       millert   411:                return(NULL);
1.1       deraadt   412:        }
                    413:        if (l < 0) {
1.6       millert   414:                warn("read");
1.8       millert   415:                return(NULL);
1.1       deraadt   416:        }
                    417:        if (l == 0) {
                    418:                fprintf(stderr, "\"%s\": No match.\n", name);
1.8       millert   419:                return(NULL);
1.1       deraadt   420:        }
1.6       millert   421:        if (l == PATHSIZE) {
1.1       deraadt   422:                fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
1.8       millert   423:                return(NULL);
1.1       deraadt   424:        }
1.6       millert   425:        xname[l] = '\0';
1.1       deraadt   426:        for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
                    427:                ;
                    428:        cp[1] = '\0';
1.3       millert   429:        if (strchr(xname, ' ') && stat(xname, &sbuf) < 0) {
1.1       deraadt   430:                fprintf(stderr, "\"%s\": Ambiguous.\n", name);
1.8       millert   431:                return(NULL);
1.1       deraadt   432:        }
1.6       millert   433:        return(savestr(xname));
1.1       deraadt   434: }
                    435:
                    436: /*
                    437:  * Determine the current folder directory name.
                    438:  */
                    439: int
1.5       deraadt   440: getfold(name, namelen)
1.1       deraadt   441:        char *name;
1.5       deraadt   442:        int namelen;
1.1       deraadt   443: {
                    444:        char *folder;
                    445:
1.8       millert   446:        if ((folder = value("folder")) == NULL)
1.6       millert   447:                return(-1);
1.5       deraadt   448:        if (*folder == '/') {
                    449:                strncpy(name, folder, namelen-1);
                    450:                name[namelen-1] = '\0';
                    451:        } else
1.10      millert   452:                (void)snprintf(name, namelen, "%s/%s", homedir, folder);
1.6       millert   453:        return(0);
1.1       deraadt   454: }
                    455:
                    456: /*
                    457:  * Return the name of the dead.letter file.
                    458:  */
                    459: char *
                    460: getdeadletter()
                    461: {
                    462:        register char *cp;
                    463:
1.8       millert   464:        if ((cp = value("DEAD")) == NULL || (cp = expand(cp)) == NULL)
1.1       deraadt   465:                cp = expand("~/dead.letter");
                    466:        else if (*cp != '/') {
                    467:                char buf[PATHSIZE];
                    468:
1.7       millert   469:                (void)snprintf(buf, sizeof(buf), "~/%s", cp);
1.1       deraadt   470:                cp = expand(buf);
                    471:        }
1.6       millert   472:        return(cp);
1.1       deraadt   473: }