[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.11

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