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

Annotation of src/usr.bin/msgs/msgs.c, Revision 1.28

1.28    ! jmc         1: /*     $OpenBSD: msgs.c,v 1.27 2003/06/10 22:20:48 deraadt Exp $       */
1.1       deraadt     2: /*     $NetBSD: msgs.c,v 1.7 1995/09/28 06:57:40 tls Exp $     */
                      3:
                      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.
1.26      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: static char copyright[] =
                     35: "@(#) Copyright (c) 1980, 1993\n\
                     36:        The Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)msgs.c     8.2 (Berkeley) 4/28/95";
                     42: #else
1.28    ! jmc        43: static char rcsid[] = "$OpenBSD: msgs.c,v 1.27 2003/06/10 22:20:48 deraadt Exp $";
1.1       deraadt    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: /*
                     48:  * msgs - a user bulletin board program
                     49:  *
                     50:  * usage:
                     51:  *     msgs [fhlopqr] [[-]number]      to read messages
                     52:  *     msgs -s                         to place messages
                     53:  *     msgs -c [-days]                 to clean up the bulletin board
                     54:  *
                     55:  * prompt commands are:
                     56:  *     y       print message
                     57:  *     n       flush message, go to next message
                     58:  *     q       flush message, quit
                     59:  *     p       print message, turn on 'pipe thru more' mode
                     60:  *     P       print message, turn off 'pipe thru more' mode
                     61:  *     -       reprint last message
                     62:  *     s[-][<num>] [<filename>]        save message
                     63:  *     m[-][<num>]     mail with message in temp mbox
                     64:  *     x       exit without flushing this message
                     65:  *     <num>   print message number <num>
                     66:  */
                     67:
                     68: #define OBJECT         /* will object to messages without Subjects */
                     69: #define REJECT /* will reject messages without Subjects
                     70:                           (OBJECT must be defined also) */
1.9       downsj     71: #undef UNBUFFERED      /* use unbuffered output */
1.1       deraadt    72:
                     73: #include <sys/param.h>
                     74: #include <sys/ioctl.h>
                     75: #include <sys/stat.h>
                     76: #include <dirent.h>
                     77: #include <ctype.h>
                     78: #include <errno.h>
1.15      millert    79: #include <fcntl.h>
1.1       deraadt    80: #include <pwd.h>
                     81: #include <setjmp.h>
                     82: #include <signal.h>
                     83: #include <stdio.h>
                     84: #include <stdlib.h>
                     85: #include <string.h>
1.9       downsj     86: #include <term.h>
1.1       deraadt    87: #include <termios.h>
                     88: #include <time.h>
                     89: #include <unistd.h>
                     90: #include "pathnames.h"
                     91:
                     92: #define CMODE  0664            /* bounds file creation mode */
                     93: #define NO     0
                     94: #define YES    1
                     95: #define SUPERUSER      0       /* superuser uid */
                     96: #define DAEMON         1       /* daemon uid */
                     97: #define NLINES 24              /* default number of lines/crt screen */
                     98: #define NDAYS  21              /* default keep time for messages */
                     99: #define DAYS   *24*60*60       /* seconds/day */
                    100: #define MSGSRC ".msgsrc"       /* user's rc file */
                    101: #define BOUNDS "bounds"        /* message bounds file */
                    102: #define NEXT   "Next message? [yq]"
                    103: #define MORE   "More? [ynq]"
                    104: #define NOMORE "(No more) [q] ?"
                    105:
                    106: typedef        char    bool;
                    107:
                    108: FILE   *msgsrc;
                    109: FILE   *newmsg;
                    110: char   *sep = "-";
                    111: char   inbuf[BUFSIZ];
1.8       deraadt   112: char   fname[MAXPATHLEN];
                    113: char   cmdbuf[MAXPATHLEN + MAXPATHLEN];
1.1       deraadt   114: char   subj[128];
                    115: char   from[128];
                    116: char   date[128];
                    117: char   *ptr;
                    118: char   *in;
                    119: bool   local;
                    120: bool   ruptible;
                    121: bool   totty;
                    122: bool   seenfrom;
                    123: bool   seensubj;
                    124: bool   blankline;
                    125: bool   printing = NO;
                    126: bool   mailing = NO;
                    127: bool   quitit = NO;
                    128: bool   sending = NO;
                    129: bool   intrpflg = NO;
                    130: bool   restricted = NO;
                    131: int    uid;
                    132: int    msg;
                    133: int    prevmsg;
                    134: int    lct;
                    135: int    nlines;
                    136: int    Lpp = 0;
                    137: time_t t;
                    138: time_t keep;
                    139:
1.21      millert   140: void prmesg(int);
                    141: void onintr(int);
                    142: void onsusp(int);
                    143: int linecnt(FILE *);
1.24      deraadt   144: int next(char *, int);
1.21      millert   145: void ask(char *);
                    146: void gfrsub(FILE *);
                    147: char *nxtfld(char *);
1.1       deraadt   148:
                    149: /* option initialization */
                    150: bool   hdrs = NO;
                    151: bool   qopt = NO;
                    152: bool   hush = NO;
                    153: bool   send_msg = NO;
                    154: bool   locomode = NO;
                    155: bool   use_pager = NO;
                    156: bool   clean = NO;
                    157: bool   lastcmd = NO;
                    158: jmp_buf        tstpbuf;
                    159:
1.9       downsj    160: int
1.27      deraadt   161: main(int argc, char *argv[])
1.1       deraadt   162: {
                    163:        bool newrc, already;
                    164:        int rcfirst = 0;                /* first message to print (from .rc) */
                    165:        int rcback = 0;                 /* amount to back off of rcfirst */
                    166:        int firstmsg, nextmsg, lastmsg = 0;
                    167:        int blast = 0;
                    168:        FILE *bounds;
1.17      millert   169:        char *cp;
1.1       deraadt   170:
                    171: #ifdef UNBUFFERED
                    172:        setbuf(stdout, NULL);
                    173: #endif
                    174:
                    175:        time(&t);
1.6       tholo     176:        seteuid(uid = getuid());
                    177:        setuid(uid);
1.1       deraadt   178:        ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
                    179:        if (ruptible)
                    180:                signal(SIGINT, SIG_DFL);
                    181:
                    182:        argc--, argv++;
                    183:        while (argc > 0) {
                    184:                if (isdigit(argv[0][0])) {      /* starting message # */
                    185:                        rcfirst = atoi(argv[0]);
                    186:                }
                    187:                else if (isdigit(argv[0][1])) { /* backward offset */
                    188:                        rcback = atoi( &( argv[0][1] ) );
                    189:                }
                    190:                else {
                    191:                        ptr = *argv;
                    192:                        while (*ptr) switch (*ptr++) {
                    193:
                    194:                        case '-':
                    195:                                break;
                    196:
                    197:                        case 'c':
                    198:                                if (uid != SUPERUSER && uid != DAEMON) {
                    199:                                        fprintf(stderr, "Sorry\n");
                    200:                                        exit(1);
                    201:                                }
                    202:                                clean = YES;
                    203:                                break;
                    204:
                    205:                        case 'f':               /* silently */
                    206:                                hush = YES;
                    207:                                break;
                    208:
                    209:                        case 'h':               /* headers only */
                    210:                                hdrs = YES;
                    211:                                break;
                    212:
                    213:                        case 'l':               /* local msgs only */
                    214:                                locomode = YES;
                    215:                                break;
                    216:
                    217:                        case 'o':               /* option to save last message */
                    218:                                lastcmd = YES;
                    219:                                break;
                    220:
                    221:                        case 'p':               /* pipe thru 'more' during long msgs */
                    222:                                use_pager = YES;
                    223:                                break;
                    224:
                    225:                        case 'q':               /* query only */
                    226:                                qopt = YES;
                    227:                                break;
                    228:
1.11      deraadt   229:                        case 'r':               /* restricted */
                    230:                                restricted = YES;
                    231:                                break;
1.1       deraadt   232:
                    233:
                    234:                        case 's':               /* sending TO msgs */
                    235:                                send_msg = YES;
                    236:                                break;
                    237:
                    238:                        default:
                    239:                                fprintf(stderr,
1.28    ! jmc       240:                                        "usage: msgs [fhlopqr] [[-]number]\n"
        !           241:                                        "       msgs [-s]\n"
        !           242:                                        "       msgs [-c [-days]]\n");
1.1       deraadt   243:                                exit(1);
                    244:                        }
                    245:                }
                    246:                argc--, argv++;
                    247:        }
                    248:
                    249:        /*
                    250:         * determine current message bounds
                    251:         */
1.5       millert   252:        snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
1.1       deraadt   253:        bounds = fopen(fname, "r");
                    254:
1.12      deraadt   255:        if (bounds == NULL) {
1.13      aaron     256:                if (errno == ENOENT) {
                    257:                        if ((bounds = fopen(fname, "w+")) == NULL) {
                    258:                                perror(fname);
                    259:                                exit(1);
                    260:                        }
                    261:                        fprintf(bounds, "1 0\n");
                    262:                        rewind(bounds);
                    263:                }
                    264:                else {
                    265:                        perror(fname);
                    266:                        exit(1);
                    267:                }
1.1       deraadt   268:        }
1.12      deraadt   269:
                    270:        fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
                    271:        fclose(bounds);
                    272:        blast = lastmsg;        /* save upper bound */
1.1       deraadt   273:
                    274:        if (clean)
                    275:                keep = t - (rcback? rcback : NDAYS) DAYS;
                    276:
                    277:        if (clean || bounds == NULL) {  /* relocate message bounds */
                    278:                struct dirent *dp;
                    279:                struct stat stbuf;
                    280:                bool seenany = NO;
                    281:                DIR     *dirp;
                    282:
                    283:                dirp = opendir(_PATH_MSGS);
                    284:                if (dirp == NULL) {
                    285:                        perror(_PATH_MSGS);
                    286:                        exit(errno);
                    287:                }
                    288:                chmod(fname, CMODE);
                    289:
                    290:                firstmsg = 32767;
                    291:                lastmsg = 0;
                    292:
                    293:                for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
1.19      mpech     294:                        int i = 0;
1.1       deraadt   295:
1.17      millert   296:                        cp = dp->d_name;
1.1       deraadt   297:                        if (dp->d_ino == 0)
                    298:                                continue;
                    299:                        if (dp->d_namlen == 0)
                    300:                                continue;
                    301:
                    302:                        if (clean)
1.5       millert   303:                                snprintf(inbuf, sizeof(inbuf), "%s/%s",
                    304:                                         _PATH_MSGS, cp);
1.1       deraadt   305:
                    306:                        while (isdigit(*cp))
                    307:                                i = i * 10 + *cp++ - '0';
                    308:                        if (*cp)
                    309:                                continue;       /* not a message! */
                    310:
                    311:                        if (clean) {
                    312:                                if (stat(inbuf, &stbuf) != 0)
                    313:                                        continue;
                    314:                                if (stbuf.st_mtime < keep
                    315:                                    && stbuf.st_mode&S_IWRITE) {
                    316:                                        unlink(inbuf);
                    317:                                        continue;
                    318:                                }
                    319:                        }
                    320:
                    321:                        if (i > lastmsg)
                    322:                                lastmsg = i;
                    323:                        if (i < firstmsg)
                    324:                                firstmsg = i;
                    325:                        seenany = YES;
                    326:                }
                    327:                closedir(dirp);
                    328:
                    329:                if (!seenany) {
                    330:                        if (blast != 0) /* never lower the upper bound! */
                    331:                                lastmsg = blast;
                    332:                        firstmsg = lastmsg + 1;
                    333:                }
                    334:                else if (blast > lastmsg)
                    335:                        lastmsg = blast;
                    336:
                    337:                if (!send_msg) {
                    338:                        bounds = fopen(fname, "w");
                    339:                        if (bounds == NULL) {
                    340:                                perror(fname);
                    341:                                exit(errno);
                    342:                        }
                    343:                        chmod(fname, CMODE);
                    344:                        fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
                    345:                        fclose(bounds);
                    346:                }
                    347:        }
                    348:
                    349:        if (send_msg) {
                    350:                /*
                    351:                 * Send mode - place msgs in _PATH_MSGS
                    352:                 */
                    353:                bounds = fopen(fname, "w");
                    354:                if (bounds == NULL) {
                    355:                        perror(fname);
                    356:                        exit(errno);
                    357:                }
                    358:
                    359:                nextmsg = lastmsg + 1;
1.5       millert   360:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
1.1       deraadt   361:                newmsg = fopen(fname, "w");
                    362:                if (newmsg == NULL) {
                    363:                        perror(fname);
                    364:                        exit(errno);
                    365:                }
                    366:                chmod(fname, 0644);
                    367:
                    368:                fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
                    369:                fclose(bounds);
                    370:
                    371:                sending = YES;
                    372:                if (ruptible)
                    373:                        signal(SIGINT, onintr);
                    374:
                    375:                if (isatty(fileno(stdin))) {
                    376:                        ptr = getpwuid(uid)->pw_name;
                    377:                        printf("Message %d:\nFrom %s %sSubject: ",
                    378:                                nextmsg, ptr, ctime(&t));
                    379:                        fflush(stdout);
                    380:                        fgets(inbuf, sizeof inbuf, stdin);
                    381:                        putchar('\n');
                    382:                        fflush(stdout);
                    383:                        fprintf(newmsg, "From %s %sSubject: %s\n",
                    384:                                ptr, ctime(&t), inbuf);
                    385:                        blankline = seensubj = YES;
                    386:                }
                    387:                else
                    388:                        blankline = seensubj = NO;
                    389:                for (;;) {
                    390:                        fgets(inbuf, sizeof inbuf, stdin);
                    391:                        if (feof(stdin) || ferror(stdin))
                    392:                                break;
                    393:                        blankline = (blankline || (inbuf[0] == '\n'));
                    394:                        seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
                    395:                        fputs(inbuf, newmsg);
                    396:                }
                    397: #ifdef OBJECT
                    398:                if (!seensubj) {
                    399:                        printf("NOTICE: Messages should have a Subject field!\n");
                    400: #ifdef REJECT
                    401:                        unlink(fname);
                    402: #endif
                    403:                        exit(1);
                    404:                }
                    405: #endif
                    406:                exit(ferror(stdin));
                    407:        }
                    408:        if (clean)
                    409:                exit(0);
                    410:
                    411:        /*
                    412:         * prepare to display messages
                    413:         */
                    414:        totty = (isatty(fileno(stdout)) != 0);
                    415:        use_pager = use_pager && totty;
                    416:
1.17      millert   417:        if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
                    418:                fprintf(stderr, "Error, no home directory!\n");
                    419:                exit(1);
                    420:        }
                    421:        snprintf(fname, sizeof(fname), "%s/%s", cp, MSGSRC);
1.1       deraadt   422:        msgsrc = fopen(fname, "r");
                    423:        if (msgsrc) {
                    424:                newrc = NO;
1.11      deraadt   425:                fscanf(msgsrc, "%d\n", &nextmsg);
                    426:                fclose(msgsrc);
                    427:                if (nextmsg > lastmsg+1) {
1.1       deraadt   428:                        printf("Warning: bounds have been reset (%d, %d)\n",
                    429:                                firstmsg, lastmsg);
                    430:                        truncate(fname, (off_t)0);
                    431:                        newrc = YES;
                    432:                }
                    433:                else if (!rcfirst)
                    434:                        rcfirst = nextmsg - rcback;
                    435:        }
1.11      deraadt   436:        else
                    437:                newrc = YES;
                    438:        msgsrc = fopen(fname, "r+");
                    439:        if (msgsrc == NULL)
                    440:                msgsrc = fopen(fname, "w");
1.1       deraadt   441:        if (msgsrc == NULL) {
                    442:                perror(fname);
                    443:                exit(errno);
                    444:        }
                    445:        if (rcfirst) {
                    446:                if (rcfirst > lastmsg+1) {
                    447:                        printf("Warning: the last message is number %d.\n",
                    448:                                lastmsg);
                    449:                        rcfirst = nextmsg;
                    450:                }
                    451:                if (rcfirst > firstmsg)
                    452:                        firstmsg = rcfirst;     /* don't set below first msg */
                    453:        }
                    454:        if (newrc) {
                    455:                nextmsg = firstmsg;
                    456:                fseek(msgsrc, 0L, 0);
                    457:                fprintf(msgsrc, "%d\n", nextmsg);
                    458:                fflush(msgsrc);
                    459:        }
                    460:
                    461:        if (totty) {
                    462:                struct winsize win;
                    463:                if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
                    464:                        Lpp = win.ws_row;
                    465:                if (Lpp <= 0) {
1.10      downsj    466:                        char *ttype = getenv("TERM");
                    467:
                    468:                        if (ttype != (char *)NULL) {
                    469:                                if (tgetent(NULL, ttype) <= 0
                    470:                                    || (Lpp = tgetnum("li")) <= 0) {
                    471:                                        Lpp = NLINES;
                    472:                                }
                    473:                        } else
1.1       deraadt   474:                                Lpp = NLINES;
                    475:                }
                    476:        }
                    477:        Lpp -= 6;       /* for headers, etc. */
                    478:
                    479:        already = NO;
                    480:        prevmsg = firstmsg;
                    481:        printing = YES;
                    482:        if (ruptible)
                    483:                signal(SIGINT, onintr);
                    484:
                    485:        /*
                    486:         * Main program loop
                    487:         */
                    488:        for (msg = firstmsg; msg <= lastmsg; msg++) {
                    489:
1.5       millert   490:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
1.1       deraadt   491:                newmsg = fopen(fname, "r");
                    492:                if (newmsg == NULL)
                    493:                        continue;
                    494:
                    495:                gfrsub(newmsg);         /* get From and Subject fields */
                    496:                if (locomode && !local) {
                    497:                        fclose(newmsg);
                    498:                        continue;
                    499:                }
                    500:
                    501:                if (qopt) {     /* This has to be located here */
                    502:                        printf("There are new messages.\n");
                    503:                        exit(0);
                    504:                }
                    505:
                    506:                if (already && !hdrs)
                    507:                        putchar('\n');
                    508:
                    509:                /*
                    510:                 * Print header
                    511:                 */
                    512:                if (totty)
                    513:                        signal(SIGTSTP, onsusp);
                    514:                (void) setjmp(tstpbuf);
                    515:                already = YES;
                    516:                nlines = 2;
                    517:                if (seenfrom) {
                    518:                        printf("Message %d:\nFrom %s %s", msg, from, date);
                    519:                        nlines++;
                    520:                }
                    521:                if (seensubj) {
                    522:                        printf("Subject: %s", subj);
                    523:                        nlines++;
                    524:                }
                    525:                else {
                    526:                        if (seenfrom) {
                    527:                                putchar('\n');
                    528:                                nlines++;
                    529:                        }
                    530:                        while (nlines < 6
                    531:                            && fgets(inbuf, sizeof inbuf, newmsg)
                    532:                            && inbuf[0] != '\n') {
                    533:                                fputs(inbuf, stdout);
                    534:                                nlines++;
                    535:                        }
                    536:                }
                    537:
                    538:                lct = linecnt(newmsg);
                    539:                if (lct)
                    540:                        printf("(%d%slines) ", lct, seensubj? " " : " more ");
                    541:
                    542:                if (hdrs) {
                    543:                        printf("\n-----\n");
                    544:                        fclose(newmsg);
                    545:                        continue;
                    546:                }
                    547:
                    548:                /*
                    549:                 * Ask user for command
                    550:                 */
                    551:                if (totty)
                    552:                        ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
                    553:                else
                    554:                        inbuf[0] = 'y';
                    555:                if (totty)
                    556:                        signal(SIGTSTP, SIG_DFL);
                    557: cmnd:
                    558:                in = inbuf;
                    559:                switch (*in) {
                    560:                        case 'x':
                    561:                        case 'X':
                    562:                                exit(0);
                    563:
                    564:                        case 'q':
                    565:                        case 'Q':
                    566:                                quitit = YES;
                    567:                                printf("--Postponed--\n");
                    568:                                exit(0);
                    569:                                /* intentional fall-thru */
                    570:                        case 'n':
                    571:                        case 'N':
                    572:                                if (msg >= nextmsg) sep = "Flushed";
                    573:                                prevmsg = msg;
                    574:                                break;
                    575:
                    576:                        case 'p':
                    577:                        case 'P':
                    578:                                use_pager = (*in++ == 'p');
                    579:                                /* intentional fallthru */
                    580:                        case '\n':
                    581:                        case 'y':
                    582:                        default:
                    583:                                if (*in == '-') {
                    584:                                        msg = prevmsg-1;
                    585:                                        sep = "replay";
                    586:                                        break;
                    587:                                }
                    588:                                if (isdigit(*in)) {
1.24      deraadt   589:                                        msg = next(in, sizeof inbuf);
1.1       deraadt   590:                                        sep = in;
                    591:                                        break;
                    592:                                }
                    593:
                    594:                                prmesg(nlines + lct + (seensubj? 1 : 0));
                    595:                                prevmsg = msg;
                    596:
                    597:                }
                    598:
                    599:                printf("--%s--\n", sep);
                    600:                sep = "-";
                    601:                if (msg >= nextmsg) {
                    602:                        nextmsg = msg + 1;
                    603:                        fseek(msgsrc, 0L, 0);
                    604:                        fprintf(msgsrc, "%d\n", nextmsg);
                    605:                        fflush(msgsrc);
                    606:                }
                    607:                if (newmsg)
                    608:                        fclose(newmsg);
                    609:                if (quitit)
                    610:                        break;
                    611:        }
                    612:
                    613:        /*
                    614:         * Make sure .rc file gets updated
                    615:         */
                    616:        if (--msg >= nextmsg) {
                    617:                nextmsg = msg + 1;
                    618:                fseek(msgsrc, 0L, 0);
                    619:                fprintf(msgsrc, "%d\n", nextmsg);
                    620:                fflush(msgsrc);
                    621:        }
                    622:        if (already && !quitit && lastcmd && totty) {
                    623:                /*
                    624:                 * save or reply to last message?
                    625:                 */
                    626:                msg = prevmsg;
                    627:                ask(NOMORE);
                    628:                if (inbuf[0] == '-' || isdigit(inbuf[0]))
                    629:                        goto cmnd;
                    630:        }
                    631:        if (!(already || hush || qopt))
                    632:                printf("No new messages.\n");
                    633:        exit(0);
                    634: }
                    635:
1.9       downsj    636: void
1.27      deraadt   637: prmesg(int length)
1.1       deraadt   638: {
                    639:        FILE *outf;
                    640:        char *env_pager;
                    641:
                    642:        if (use_pager && length > Lpp) {
                    643:                signal(SIGPIPE, SIG_IGN);
                    644:                signal(SIGQUIT, SIG_IGN);
1.14      pjanzen   645:                if ((env_pager = getenv("PAGER")) == NULL || *env_pager == '\0') {
1.11      deraadt   646:                        snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
                    647:                } else {
1.16      deraadt   648:                        snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
1.11      deraadt   649:                }
1.1       deraadt   650:                outf = popen(cmdbuf, "w");
                    651:                if (!outf)
                    652:                        outf = stdout;
                    653:                else
                    654:                        setbuf(outf, (char *)NULL);
                    655:        }
                    656:        else
                    657:                outf = stdout;
                    658:
                    659:        if (seensubj)
                    660:                putc('\n', outf);
                    661:
                    662:        while (fgets(inbuf, sizeof inbuf, newmsg)) {
                    663:                fputs(inbuf, outf);
                    664:                if (ferror(outf)) {
                    665:                        clearerr(outf);
                    666:                        break;
                    667:                }
                    668:        }
                    669:
                    670:        if (outf != stdout) {
                    671:                pclose(outf);
                    672:                signal(SIGPIPE, SIG_DFL);
                    673:                signal(SIGQUIT, SIG_DFL);
                    674:        }
                    675:        else {
                    676:                fflush(stdout);
                    677:        }
                    678:
                    679:        /* trick to force wait on output */
                    680:        tcdrain(fileno(stdout));
                    681: }
                    682:
                    683: void
1.27      deraadt   684: onintr(int unused)
1.1       deraadt   685: {
                    686:        signal(SIGINT, onintr);
                    687:        if (mailing)
                    688:                unlink(fname);
                    689:        if (sending) {
                    690:                unlink(fname);
                    691:                puts("--Killed--");
                    692:                exit(1);
                    693:        }
                    694:        if (printing) {
                    695:                putchar('\n');
                    696:                if (hdrs)
                    697:                        exit(0);
                    698:                sep = "Interrupt";
                    699:                if (newmsg)
                    700:                        fseek(newmsg, 0L, 2);
                    701:                intrpflg = YES;
                    702:        }
                    703: }
                    704:
                    705: /*
                    706:  * We have just gotten a susp.  Suspend and prepare to resume.
                    707:  */
                    708: void
1.27      deraadt   709: onsusp(int unused)
1.1       deraadt   710: {
1.18      millert   711:        sigset_t emptyset;
                    712:
1.1       deraadt   713:        signal(SIGTSTP, SIG_DFL);
1.18      millert   714:        sigemptyset(&emptyset);
                    715:        sigprocmask(SIG_SETMASK, &emptyset, NULL);
1.1       deraadt   716:        kill(0, SIGTSTP);
                    717:        signal(SIGTSTP, onsusp);
                    718:        if (!mailing)
1.20      art       719:                longjmp(tstpbuf, 1);
1.1       deraadt   720: }
                    721:
1.9       downsj    722: int
1.27      deraadt   723: linecnt(FILE *f)
1.1       deraadt   724: {
                    725:        off_t oldpos = ftell(f);
                    726:        int l = 0;
                    727:        char lbuf[BUFSIZ];
                    728:
                    729:        while (fgets(lbuf, sizeof lbuf, f))
                    730:                l++;
                    731:        clearerr(f);
                    732:        fseek(f, oldpos, 0);
                    733:        return (l);
                    734: }
                    735:
1.9       downsj    736: int
1.27      deraadt   737: next(char *buf, int len)
1.1       deraadt   738: {
                    739:        int i;
                    740:        sscanf(buf, "%d", &i);
1.24      deraadt   741:        snprintf(buf, len, "Goto %d", i);
1.1       deraadt   742:        return(--i);
                    743: }
                    744:
1.9       downsj    745: void
1.27      deraadt   746: ask(char *prompt)
1.1       deraadt   747: {
                    748:        char    inch;
1.2       deraadt   749:        int     n, cmsg, fd;
1.1       deraadt   750:        off_t   oldpos;
                    751:        FILE    *cpfrom, *cpto;
                    752:
                    753:        printf("%s ", prompt);
                    754:        fflush(stdout);
                    755:        intrpflg = NO;
                    756:        (void) fgets(inbuf, sizeof inbuf, stdin);
                    757:        if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
                    758:                inbuf[n - 1] = '\0';
                    759:        if (intrpflg)
                    760:                inbuf[0] = 'x';
                    761:
                    762:        /*
                    763:         * Handle 'mail' and 'save' here.
                    764:         */
1.11      deraadt   765:        if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
1.1       deraadt   766:                if (inbuf[1] == '-')
                    767:                        cmsg = prevmsg;
                    768:                else if (isdigit(inbuf[1]))
                    769:                        cmsg = atoi(&inbuf[1]);
                    770:                else
                    771:                        cmsg = msg;
1.5       millert   772:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
1.1       deraadt   773:
                    774:                oldpos = ftell(newmsg);
                    775:
                    776:                cpfrom = fopen(fname, "r");
                    777:                if (!cpfrom) {
                    778:                        printf("Message %d not found\n", cmsg);
                    779:                        ask (prompt);
                    780:                        return;
                    781:                }
                    782:
                    783:                if (inch == 's') {
                    784:                        in = nxtfld(inbuf);
                    785:                        if (*in) {
1.22      ho        786:                                for (n=0;
1.23      ho        787:                                     in[n] > ' ' && n < sizeof fname - 1;
1.22      ho        788:                                     n++) {
1.1       deraadt   789:                                        fname[n] = in[n];
                    790:                                }
                    791:                                fname[n] = NULL;
                    792:                        }
                    793:                        else
1.25      deraadt   794:                                strlcpy(fname, "Messages", sizeof fname);
1.22      ho        795:                        fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND, 0666);
1.1       deraadt   796:                }
                    797:                else {
1.25      deraadt   798:                        strlcpy(fname, _PATH_TMPFILE, sizeof fname);
1.7       deraadt   799:                        fd = mkstemp(fname);
                    800:                        if (fd != -1) {
                    801:                                snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, fname);
                    802:                                mailing = YES;
                    803:                        }
                    804:                }
                    805:                if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
                    806:                        if (fd != -1)
1.2       deraadt   807:                                close(fd);
1.1       deraadt   808:                        perror(fname);
                    809:                        mailing = NO;
                    810:                        fseek(newmsg, oldpos, 0);
                    811:                        ask(prompt);
                    812:                        return;
                    813:                }
                    814:
1.9       downsj    815:                while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
1.1       deraadt   816:                        fwrite(inbuf, 1, n, cpto);
                    817:
                    818:                fclose(cpfrom);
                    819:                fclose(cpto);
                    820:                fseek(newmsg, oldpos, 0);       /* reposition current message */
                    821:                if (inch == 's')
                    822:                        printf("Message %d saved in \"%s\"\n", cmsg, fname);
                    823:                else {
                    824:                        system(cmdbuf);
                    825:                        unlink(fname);
                    826:                        mailing = NO;
                    827:                }
                    828:                ask(prompt);
                    829:        }
                    830: }
                    831:
1.9       downsj    832: void
1.27      deraadt   833: gfrsub(FILE *infile)
1.1       deraadt   834: {
                    835:        off_t frompos;
                    836:
                    837:        seensubj = seenfrom = NO;
                    838:        local = YES;
                    839:        subj[0] = from[0] = date[0] = NULL;
                    840:
                    841:        /*
                    842:         * Is this a normal message?
                    843:         */
                    844:        if (fgets(inbuf, sizeof inbuf, infile)) {
                    845:                if (strncmp(inbuf, "From", 4)==0) {
                    846:                        /*
                    847:                         * expected form starts with From
                    848:                         */
                    849:                        seenfrom = YES;
                    850:                        frompos = ftell(infile);
                    851:                        ptr = from;
                    852:                        in = nxtfld(inbuf);
1.8       deraadt   853:                        if (*in) {
                    854:                                while (*in && *in > ' ' &&
                    855:                                    ptr - from < sizeof from -1) {
                    856:                                        if (*in == ':' || *in == '@' || *in == '!')
                    857:                                                local = NO;
                    858:                                        *ptr++ = *in++;
                    859:                                }
1.1       deraadt   860:                        }
                    861:                        *ptr = NULL;
                    862:                        if (*(in = nxtfld(in)))
                    863:                                strncpy(date, in, sizeof date);
                    864:                        else {
                    865:                                date[0] = '\n';
                    866:                                date[1] = NULL;
                    867:                        }
                    868:                }
                    869:                else {
                    870:                        /*
                    871:                         * not the expected form
                    872:                         */
                    873:                        fseek(infile, 0L, 0);
                    874:                        return;
                    875:                }
                    876:        }
                    877:        else
                    878:                /*
                    879:                 * empty file ?
                    880:                 */
                    881:                return;
                    882:
                    883:        /*
                    884:         * look for Subject line until EOF or a blank line
                    885:         */
                    886:        while (fgets(inbuf, sizeof inbuf, infile)
                    887:            && !(blankline = (inbuf[0] == '\n'))) {
                    888:                /*
                    889:                 * extract Subject line
                    890:                 */
                    891:                if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
                    892:                        seensubj = YES;
                    893:                        frompos = ftell(infile);
                    894:                        strncpy(subj, nxtfld(inbuf), sizeof subj);
                    895:                }
                    896:        }
                    897:        if (!blankline)
                    898:                /*
                    899:                 * ran into EOF
                    900:                 */
                    901:                fseek(infile, frompos, 0);
                    902:
                    903:        if (!seensubj)
                    904:                /*
                    905:                 * for possible use with Mail
                    906:                 */
                    907:                strncpy(subj, "(No Subject)\n", sizeof subj);
                    908: }
                    909:
                    910: char *
1.27      deraadt   911: nxtfld(char *s)
1.1       deraadt   912: {
                    913:        if (*s) while (*s && *s > ' ') s++;     /* skip over this field */
                    914:        if (*s) while (*s && *s <= ' ') s++;    /* find start of next field */
                    915:        return (s);
                    916: }