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

1.29    ! deraadt     1: /*     $OpenBSD: msgs.c,v 1.28 2003/10/04 21:55:12 jmc 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.29    ! deraadt    43: static char rcsid[] = "$OpenBSD: msgs.c,v 1.28 2003/10/04 21:55:12 jmc 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]);
1.29    ! deraadt   186:                } else if (isdigit(argv[0][1])) {       /* backward offset */
        !           187:                        rcback = atoi(&(argv[0][1]));
        !           188:                } else {
1.1       deraadt   189:                        ptr = *argv;
1.29    ! deraadt   190:                        while (*ptr) {
        !           191:                                switch (*ptr++) {
        !           192:                                case '-':
        !           193:                                        break;
        !           194:                                case 'c':
        !           195:                                        if (uid != SUPERUSER && uid != DAEMON) {
        !           196:                                                fprintf(stderr, "Sorry\n");
        !           197:                                                exit(1);
        !           198:                                        }
        !           199:                                        clean = YES;
        !           200:                                        break;
        !           201:                                case 'f':       /* silently */
        !           202:                                        hush = YES;
        !           203:                                        break;
        !           204:                                case 'h':       /* headers only */
        !           205:                                        hdrs = YES;
        !           206:                                        break;
        !           207:                                case 'l':       /* local msgs only */
        !           208:                                        locomode = YES;
        !           209:                                        break;
        !           210:                                case 'o':       /* option to save last message */
        !           211:                                        lastcmd = YES;
        !           212:                                        break;
        !           213:                                case 'p':       /* pipe thru 'more' during long msgs */
        !           214:                                        use_pager = YES;
        !           215:                                        break;
        !           216:                                case 'q':       /* query only */
        !           217:                                        qopt = YES;
        !           218:                                        break;
        !           219:                                case 'r':       /* restricted */
        !           220:                                        restricted = YES;
        !           221:                                        break;
        !           222:                                case 's':       /* sending TO msgs */
        !           223:                                        send_msg = YES;
        !           224:                                        break;
        !           225:                                default:
        !           226:                                        fprintf(stderr,
        !           227:                                            "usage: msgs [fhlopqr] [[-]number]\n"
        !           228:                                            "       msgs [-s]\n"
        !           229:                                            "       msgs [-c [-days]]\n");
1.1       deraadt   230:                                        exit(1);
                    231:                                }
                    232:                        }
                    233:                }
                    234:                argc--, argv++;
                    235:        }
                    236:
                    237:        /*
                    238:         * determine current message bounds
                    239:         */
1.5       millert   240:        snprintf(fname, sizeof(fname), "%s/%s", _PATH_MSGS, BOUNDS);
1.1       deraadt   241:        bounds = fopen(fname, "r");
                    242:
1.12      deraadt   243:        if (bounds == NULL) {
1.13      aaron     244:                if (errno == ENOENT) {
                    245:                        if ((bounds = fopen(fname, "w+")) == NULL) {
                    246:                                perror(fname);
                    247:                                exit(1);
                    248:                        }
                    249:                        fprintf(bounds, "1 0\n");
                    250:                        rewind(bounds);
1.29    ! deraadt   251:                } else {
1.13      aaron     252:                        perror(fname);
                    253:                        exit(1);
                    254:                }
1.1       deraadt   255:        }
1.12      deraadt   256:
                    257:        fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
                    258:        fclose(bounds);
                    259:        blast = lastmsg;        /* save upper bound */
1.1       deraadt   260:
                    261:        if (clean)
                    262:                keep = t - (rcback? rcback : NDAYS) DAYS;
                    263:
                    264:        if (clean || bounds == NULL) {  /* relocate message bounds */
                    265:                struct dirent *dp;
                    266:                struct stat stbuf;
                    267:                bool seenany = NO;
                    268:                DIR     *dirp;
                    269:
                    270:                dirp = opendir(_PATH_MSGS);
                    271:                if (dirp == NULL) {
                    272:                        perror(_PATH_MSGS);
                    273:                        exit(errno);
                    274:                }
                    275:                chmod(fname, CMODE);
                    276:
                    277:                firstmsg = 32767;
                    278:                lastmsg = 0;
                    279:
                    280:                for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
1.19      mpech     281:                        int i = 0;
1.1       deraadt   282:
1.17      millert   283:                        cp = dp->d_name;
1.1       deraadt   284:                        if (dp->d_ino == 0)
                    285:                                continue;
                    286:                        if (dp->d_namlen == 0)
                    287:                                continue;
                    288:
                    289:                        if (clean)
1.5       millert   290:                                snprintf(inbuf, sizeof(inbuf), "%s/%s",
1.29    ! deraadt   291:                                    _PATH_MSGS, cp);
1.1       deraadt   292:
                    293:                        while (isdigit(*cp))
                    294:                                i = i * 10 + *cp++ - '0';
                    295:                        if (*cp)
                    296:                                continue;       /* not a message! */
                    297:
                    298:                        if (clean) {
                    299:                                if (stat(inbuf, &stbuf) != 0)
                    300:                                        continue;
1.29    ! deraadt   301:                                if (stbuf.st_mtime < keep &&
        !           302:                                    stbuf.st_mode&S_IWRITE) {
1.1       deraadt   303:                                        unlink(inbuf);
                    304:                                        continue;
                    305:                                }
                    306:                        }
                    307:
                    308:                        if (i > lastmsg)
                    309:                                lastmsg = i;
                    310:                        if (i < firstmsg)
                    311:                                firstmsg = i;
                    312:                        seenany = YES;
                    313:                }
                    314:                closedir(dirp);
                    315:
                    316:                if (!seenany) {
                    317:                        if (blast != 0) /* never lower the upper bound! */
                    318:                                lastmsg = blast;
                    319:                        firstmsg = lastmsg + 1;
1.29    ! deraadt   320:                } else if (blast > lastmsg)
1.1       deraadt   321:                        lastmsg = blast;
                    322:
                    323:                if (!send_msg) {
                    324:                        bounds = fopen(fname, "w");
                    325:                        if (bounds == NULL) {
                    326:                                perror(fname);
                    327:                                exit(errno);
                    328:                        }
                    329:                        chmod(fname, CMODE);
                    330:                        fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
                    331:                        fclose(bounds);
                    332:                }
                    333:        }
                    334:
                    335:        if (send_msg) {
                    336:                /*
                    337:                 * Send mode - place msgs in _PATH_MSGS
                    338:                 */
                    339:                bounds = fopen(fname, "w");
                    340:                if (bounds == NULL) {
                    341:                        perror(fname);
                    342:                        exit(errno);
                    343:                }
                    344:
                    345:                nextmsg = lastmsg + 1;
1.5       millert   346:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, nextmsg);
1.1       deraadt   347:                newmsg = fopen(fname, "w");
                    348:                if (newmsg == NULL) {
                    349:                        perror(fname);
                    350:                        exit(errno);
                    351:                }
                    352:                chmod(fname, 0644);
                    353:
                    354:                fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
                    355:                fclose(bounds);
                    356:
                    357:                sending = YES;
                    358:                if (ruptible)
                    359:                        signal(SIGINT, onintr);
                    360:
                    361:                if (isatty(fileno(stdin))) {
                    362:                        ptr = getpwuid(uid)->pw_name;
                    363:                        printf("Message %d:\nFrom %s %sSubject: ",
1.29    ! deraadt   364:                            nextmsg, ptr, ctime(&t));
1.1       deraadt   365:                        fflush(stdout);
                    366:                        fgets(inbuf, sizeof inbuf, stdin);
                    367:                        putchar('\n');
                    368:                        fflush(stdout);
                    369:                        fprintf(newmsg, "From %s %sSubject: %s\n",
1.29    ! deraadt   370:                            ptr, ctime(&t), inbuf);
1.1       deraadt   371:                        blankline = seensubj = YES;
1.29    ! deraadt   372:                } else
1.1       deraadt   373:                        blankline = seensubj = NO;
                    374:                for (;;) {
                    375:                        fgets(inbuf, sizeof inbuf, stdin);
                    376:                        if (feof(stdin) || ferror(stdin))
                    377:                                break;
                    378:                        blankline = (blankline || (inbuf[0] == '\n'));
1.29    ! deraadt   379:                        seensubj = (seensubj ||
        !           380:                            (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
1.1       deraadt   381:                        fputs(inbuf, newmsg);
                    382:                }
                    383: #ifdef OBJECT
                    384:                if (!seensubj) {
                    385:                        printf("NOTICE: Messages should have a Subject field!\n");
                    386: #ifdef REJECT
                    387:                        unlink(fname);
                    388: #endif
                    389:                        exit(1);
                    390:                }
                    391: #endif
                    392:                exit(ferror(stdin));
                    393:        }
                    394:        if (clean)
                    395:                exit(0);
                    396:
                    397:        /*
                    398:         * prepare to display messages
                    399:         */
                    400:        totty = (isatty(fileno(stdout)) != 0);
                    401:        use_pager = use_pager && totty;
                    402:
1.17      millert   403:        if ((cp = getenv("HOME")) == NULL || *cp == '\0') {
                    404:                fprintf(stderr, "Error, no home directory!\n");
                    405:                exit(1);
                    406:        }
                    407:        snprintf(fname, sizeof(fname), "%s/%s", cp, 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",
1.29    ! deraadt   415:                            firstmsg, lastmsg);
1.1       deraadt   416:                        truncate(fname, (off_t)0);
                    417:                        newrc = YES;
1.29    ! deraadt   418:                } else if (!rcfirst)
1.1       deraadt   419:                        rcfirst = nextmsg - rcback;
1.29    ! deraadt   420:        } else
1.11      deraadt   421:                newrc = YES;
                    422:        msgsrc = fopen(fname, "r+");
                    423:        if (msgsrc == NULL)
                    424:                msgsrc = fopen(fname, "w");
1.1       deraadt   425:        if (msgsrc == NULL) {
                    426:                perror(fname);
                    427:                exit(errno);
                    428:        }
                    429:        if (rcfirst) {
                    430:                if (rcfirst > lastmsg+1) {
                    431:                        printf("Warning: the last message is number %d.\n",
1.29    ! deraadt   432:                            lastmsg);
1.1       deraadt   433:                        rcfirst = nextmsg;
                    434:                }
                    435:                if (rcfirst > firstmsg)
                    436:                        firstmsg = rcfirst;     /* don't set below first msg */
                    437:        }
                    438:        if (newrc) {
                    439:                nextmsg = firstmsg;
1.29    ! deraadt   440:                fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1       deraadt   441:                fprintf(msgsrc, "%d\n", nextmsg);
                    442:                fflush(msgsrc);
                    443:        }
                    444:
                    445:        if (totty) {
                    446:                struct winsize win;
                    447:                if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
                    448:                        Lpp = win.ws_row;
                    449:                if (Lpp <= 0) {
1.10      downsj    450:                        char *ttype = getenv("TERM");
                    451:
                    452:                        if (ttype != (char *)NULL) {
                    453:                                if (tgetent(NULL, ttype) <= 0
                    454:                                    || (Lpp = tgetnum("li")) <= 0) {
                    455:                                        Lpp = NLINES;
                    456:                                }
                    457:                        } else
1.1       deraadt   458:                                Lpp = NLINES;
                    459:                }
                    460:        }
                    461:        Lpp -= 6;       /* for headers, etc. */
                    462:
                    463:        already = NO;
                    464:        prevmsg = firstmsg;
                    465:        printing = YES;
                    466:        if (ruptible)
                    467:                signal(SIGINT, onintr);
                    468:
                    469:        /*
                    470:         * Main program loop
                    471:         */
                    472:        for (msg = firstmsg; msg <= lastmsg; msg++) {
                    473:
1.5       millert   474:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, msg);
1.1       deraadt   475:                newmsg = fopen(fname, "r");
                    476:                if (newmsg == NULL)
                    477:                        continue;
                    478:
                    479:                gfrsub(newmsg);         /* get From and Subject fields */
                    480:                if (locomode && !local) {
                    481:                        fclose(newmsg);
                    482:                        continue;
                    483:                }
                    484:
                    485:                if (qopt) {     /* This has to be located here */
                    486:                        printf("There are new messages.\n");
                    487:                        exit(0);
                    488:                }
                    489:
                    490:                if (already && !hdrs)
                    491:                        putchar('\n');
                    492:
                    493:                /*
                    494:                 * Print header
                    495:                 */
                    496:                if (totty)
                    497:                        signal(SIGTSTP, onsusp);
                    498:                (void) setjmp(tstpbuf);
                    499:                already = YES;
                    500:                nlines = 2;
                    501:                if (seenfrom) {
                    502:                        printf("Message %d:\nFrom %s %s", msg, from, date);
                    503:                        nlines++;
                    504:                }
                    505:                if (seensubj) {
                    506:                        printf("Subject: %s", subj);
                    507:                        nlines++;
1.29    ! deraadt   508:                } else {
1.1       deraadt   509:                        if (seenfrom) {
                    510:                                putchar('\n');
                    511:                                nlines++;
                    512:                        }
1.29    ! deraadt   513:                        while (nlines < 6 &&
        !           514:                            fgets(inbuf, sizeof inbuf, newmsg) &&
        !           515:                            inbuf[0] != '\n') {
1.1       deraadt   516:                                fputs(inbuf, stdout);
                    517:                                nlines++;
                    518:                        }
                    519:                }
                    520:
                    521:                lct = linecnt(newmsg);
                    522:                if (lct)
                    523:                        printf("(%d%slines) ", lct, seensubj? " " : " more ");
                    524:
                    525:                if (hdrs) {
                    526:                        printf("\n-----\n");
                    527:                        fclose(newmsg);
                    528:                        continue;
                    529:                }
                    530:
                    531:                /*
                    532:                 * Ask user for command
                    533:                 */
                    534:                if (totty)
                    535:                        ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
                    536:                else
                    537:                        inbuf[0] = 'y';
                    538:                if (totty)
                    539:                        signal(SIGTSTP, SIG_DFL);
                    540: cmnd:
                    541:                in = inbuf;
                    542:                switch (*in) {
1.29    ! deraadt   543:                case 'x':
        !           544:                case 'X':
        !           545:                        exit(0);
1.1       deraadt   546:
1.29    ! deraadt   547:                case 'q':
        !           548:                case 'Q':
        !           549:                        quitit = YES;
        !           550:                        printf("--Postponed--\n");
        !           551:                        exit(0);
        !           552:                        /* intentional fall-thru */
        !           553:                case 'n':
        !           554:                case 'N':
        !           555:                        if (msg >= nextmsg)
        !           556:                                sep = "Flushed";
        !           557:                        prevmsg = msg;
        !           558:                        break;
1.1       deraadt   559:
1.29    ! deraadt   560:                case 'p':
        !           561:                case 'P':
        !           562:                        use_pager = (*in++ == 'p');
        !           563:                        /* intentional fallthru */
        !           564:                case '\n':
        !           565:                case 'y':
        !           566:                default:
        !           567:                        if (*in == '-') {
        !           568:                                msg = prevmsg-1;
        !           569:                                sep = "replay";
        !           570:                                break;
        !           571:                        }
        !           572:                        if (isdigit(*in)) {
        !           573:                                msg = next(in, sizeof inbuf);
        !           574:                                sep = in;
        !           575:                                break;
        !           576:                        }
1.1       deraadt   577:
1.29    ! deraadt   578:                        prmesg(nlines + lct + (seensubj? 1 : 0));
        !           579:                        prevmsg = msg;
1.1       deraadt   580:                }
                    581:
                    582:                printf("--%s--\n", sep);
                    583:                sep = "-";
                    584:                if (msg >= nextmsg) {
                    585:                        nextmsg = msg + 1;
1.29    ! deraadt   586:                        fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1       deraadt   587:                        fprintf(msgsrc, "%d\n", nextmsg);
                    588:                        fflush(msgsrc);
                    589:                }
                    590:                if (newmsg)
                    591:                        fclose(newmsg);
                    592:                if (quitit)
                    593:                        break;
                    594:        }
                    595:
                    596:        /*
                    597:         * Make sure .rc file gets updated
                    598:         */
                    599:        if (--msg >= nextmsg) {
                    600:                nextmsg = msg + 1;
1.29    ! deraadt   601:                fseeko(msgsrc, (off_t)0, SEEK_SET);
1.1       deraadt   602:                fprintf(msgsrc, "%d\n", nextmsg);
                    603:                fflush(msgsrc);
                    604:        }
                    605:        if (already && !quitit && lastcmd && totty) {
                    606:                /*
                    607:                 * save or reply to last message?
                    608:                 */
                    609:                msg = prevmsg;
                    610:                ask(NOMORE);
                    611:                if (inbuf[0] == '-' || isdigit(inbuf[0]))
                    612:                        goto cmnd;
                    613:        }
                    614:        if (!(already || hush || qopt))
                    615:                printf("No new messages.\n");
                    616:        exit(0);
                    617: }
                    618:
1.9       downsj    619: void
1.27      deraadt   620: prmesg(int length)
1.1       deraadt   621: {
                    622:        FILE *outf;
                    623:        char *env_pager;
                    624:
                    625:        if (use_pager && length > Lpp) {
                    626:                signal(SIGPIPE, SIG_IGN);
                    627:                signal(SIGQUIT, SIG_IGN);
1.14      pjanzen   628:                if ((env_pager = getenv("PAGER")) == NULL || *env_pager == '\0') {
1.11      deraadt   629:                        snprintf(cmdbuf, sizeof(cmdbuf), _PATH_PAGER, Lpp);
                    630:                } else {
1.16      deraadt   631:                        snprintf(cmdbuf, sizeof(cmdbuf), "%s", env_pager);
1.11      deraadt   632:                }
1.1       deraadt   633:                outf = popen(cmdbuf, "w");
                    634:                if (!outf)
                    635:                        outf = stdout;
                    636:                else
                    637:                        setbuf(outf, (char *)NULL);
                    638:        }
                    639:        else
                    640:                outf = stdout;
                    641:
                    642:        if (seensubj)
                    643:                putc('\n', outf);
                    644:
                    645:        while (fgets(inbuf, sizeof inbuf, newmsg)) {
                    646:                fputs(inbuf, outf);
                    647:                if (ferror(outf)) {
                    648:                        clearerr(outf);
                    649:                        break;
                    650:                }
                    651:        }
                    652:
                    653:        if (outf != stdout) {
                    654:                pclose(outf);
                    655:                signal(SIGPIPE, SIG_DFL);
                    656:                signal(SIGQUIT, SIG_DFL);
1.29    ! deraadt   657:        } else {
1.1       deraadt   658:                fflush(stdout);
                    659:        }
                    660:
                    661:        /* trick to force wait on output */
                    662:        tcdrain(fileno(stdout));
                    663: }
                    664:
1.29    ! deraadt   665: /* ARGSUSED */
1.1       deraadt   666: void
1.29    ! deraadt   667: onintr(int signo)
1.1       deraadt   668: {
1.29    ! deraadt   669:        int save_errno = errno;
        !           670:
1.1       deraadt   671:        signal(SIGINT, onintr);
                    672:        if (mailing)
                    673:                unlink(fname);
                    674:        if (sending) {
                    675:                unlink(fname);
1.29    ! deraadt   676:                write(STDOUT_FILENO, "--Killed--\n", strlen("--Killed--\n"));
        !           677:                _exit(1);
1.1       deraadt   678:        }
                    679:        if (printing) {
1.29    ! deraadt   680:                write(STDOUT_FILENO, "\n", 1);
1.1       deraadt   681:                if (hdrs)
1.29    ! deraadt   682:                        _exit(0);
1.1       deraadt   683:                sep = "Interrupt";
                    684:                if (newmsg)
1.29    ! deraadt   685:                        fseeko(newmsg, (off_t)0, SEEK_END);
1.1       deraadt   686:                intrpflg = YES;
                    687:        }
1.29    ! deraadt   688:        errno = save_errno;
1.1       deraadt   689: }
                    690:
                    691: /*
                    692:  * We have just gotten a susp.  Suspend and prepare to resume.
                    693:  */
1.29    ! deraadt   694: /* ARGSUSED */
1.1       deraadt   695: void
1.29    ! deraadt   696: onsusp(int signo)
1.1       deraadt   697: {
1.29    ! deraadt   698:        int save_errno = errno;
1.18      millert   699:        sigset_t emptyset;
                    700:
1.1       deraadt   701:        signal(SIGTSTP, SIG_DFL);
1.18      millert   702:        sigemptyset(&emptyset);
                    703:        sigprocmask(SIG_SETMASK, &emptyset, NULL);
1.1       deraadt   704:        kill(0, SIGTSTP);
                    705:        signal(SIGTSTP, onsusp);
1.29    ! deraadt   706:        errno = save_errno;
        !           707:
1.1       deraadt   708:        if (!mailing)
1.20      art       709:                longjmp(tstpbuf, 1);
1.1       deraadt   710: }
                    711:
1.9       downsj    712: int
1.27      deraadt   713: linecnt(FILE *f)
1.1       deraadt   714: {
1.29    ! deraadt   715:        off_t oldpos = ftello(f);
        !           716:
1.1       deraadt   717:        int l = 0;
                    718:        char lbuf[BUFSIZ];
                    719:
                    720:        while (fgets(lbuf, sizeof lbuf, f))
                    721:                l++;
                    722:        clearerr(f);
1.29    ! deraadt   723:        fseeko(f, oldpos, SEEK_SET);
1.1       deraadt   724:        return (l);
                    725: }
                    726:
1.9       downsj    727: int
1.27      deraadt   728: next(char *buf, int len)
1.1       deraadt   729: {
                    730:        int i;
                    731:        sscanf(buf, "%d", &i);
1.24      deraadt   732:        snprintf(buf, len, "Goto %d", i);
1.1       deraadt   733:        return(--i);
                    734: }
                    735:
1.9       downsj    736: void
1.27      deraadt   737: ask(char *prompt)
1.1       deraadt   738: {
                    739:        char    inch;
1.2       deraadt   740:        int     n, cmsg, fd;
1.1       deraadt   741:        off_t   oldpos;
                    742:        FILE    *cpfrom, *cpto;
                    743:
                    744:        printf("%s ", prompt);
                    745:        fflush(stdout);
                    746:        intrpflg = NO;
                    747:        (void) fgets(inbuf, sizeof inbuf, stdin);
                    748:        if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
                    749:                inbuf[n - 1] = '\0';
                    750:        if (intrpflg)
                    751:                inbuf[0] = 'x';
                    752:
                    753:        /*
                    754:         * Handle 'mail' and 'save' here.
                    755:         */
1.11      deraadt   756:        if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
1.1       deraadt   757:                if (inbuf[1] == '-')
                    758:                        cmsg = prevmsg;
                    759:                else if (isdigit(inbuf[1]))
                    760:                        cmsg = atoi(&inbuf[1]);
                    761:                else
                    762:                        cmsg = msg;
1.5       millert   763:                snprintf(fname, sizeof(fname), "%s/%d", _PATH_MSGS, cmsg);
1.1       deraadt   764:
1.29    ! deraadt   765:                oldpos = ftello(newmsg);
1.1       deraadt   766:
                    767:                cpfrom = fopen(fname, "r");
                    768:                if (!cpfrom) {
                    769:                        printf("Message %d not found\n", cmsg);
                    770:                        ask (prompt);
                    771:                        return;
                    772:                }
                    773:
                    774:                if (inch == 's') {
                    775:                        in = nxtfld(inbuf);
                    776:                        if (*in) {
1.22      ho        777:                                for (n=0;
1.29    ! deraadt   778:                                    in[n] > ' ' && n < sizeof fname - 1;
        !           779:                                    n++) {
1.1       deraadt   780:                                        fname[n] = in[n];
                    781:                                }
                    782:                                fname[n] = NULL;
                    783:                        }
                    784:                        else
1.25      deraadt   785:                                strlcpy(fname, "Messages", sizeof fname);
1.22      ho        786:                        fd = open(fname, O_RDWR|O_EXCL|O_CREAT|O_APPEND, 0666);
1.29    ! deraadt   787:                } else {
1.25      deraadt   788:                        strlcpy(fname, _PATH_TMPFILE, sizeof fname);
1.7       deraadt   789:                        fd = mkstemp(fname);
                    790:                        if (fd != -1) {
                    791:                                snprintf(cmdbuf, sizeof(cmdbuf), _PATH_MAIL, fname);
                    792:                                mailing = YES;
                    793:                        }
                    794:                }
                    795:                if (fd == -1 || (cpto = fdopen(fd, "a")) == NULL) {
                    796:                        if (fd != -1)
1.2       deraadt   797:                                close(fd);
1.1       deraadt   798:                        perror(fname);
                    799:                        mailing = NO;
1.29    ! deraadt   800:                        fseeko(newmsg, oldpos, SEEK_SET);
1.1       deraadt   801:                        ask(prompt);
                    802:                        return;
                    803:                }
                    804:
1.9       downsj    805:                while ((n = fread(inbuf, 1, sizeof inbuf, cpfrom)))
1.1       deraadt   806:                        fwrite(inbuf, 1, n, cpto);
                    807:
                    808:                fclose(cpfrom);
                    809:                fclose(cpto);
1.29    ! deraadt   810:                fseeko(newmsg, oldpos, SEEK_SET);       /* reposition current message */
1.1       deraadt   811:                if (inch == 's')
                    812:                        printf("Message %d saved in \"%s\"\n", cmsg, fname);
                    813:                else {
                    814:                        system(cmdbuf);
                    815:                        unlink(fname);
                    816:                        mailing = NO;
                    817:                }
                    818:                ask(prompt);
                    819:        }
                    820: }
                    821:
1.9       downsj    822: void
1.27      deraadt   823: gfrsub(FILE *infile)
1.1       deraadt   824: {
                    825:        off_t frompos;
                    826:
                    827:        seensubj = seenfrom = NO;
                    828:        local = YES;
                    829:        subj[0] = from[0] = date[0] = NULL;
                    830:
                    831:        /*
                    832:         * Is this a normal message?
                    833:         */
                    834:        if (fgets(inbuf, sizeof inbuf, infile)) {
                    835:                if (strncmp(inbuf, "From", 4)==0) {
                    836:                        /*
                    837:                         * expected form starts with From
                    838:                         */
                    839:                        seenfrom = YES;
1.29    ! deraadt   840:                        frompos = ftello(infile);
1.1       deraadt   841:                        ptr = from;
                    842:                        in = nxtfld(inbuf);
1.8       deraadt   843:                        if (*in) {
                    844:                                while (*in && *in > ' ' &&
                    845:                                    ptr - from < sizeof from -1) {
                    846:                                        if (*in == ':' || *in == '@' || *in == '!')
                    847:                                                local = NO;
                    848:                                        *ptr++ = *in++;
                    849:                                }
1.1       deraadt   850:                        }
                    851:                        *ptr = NULL;
                    852:                        if (*(in = nxtfld(in)))
                    853:                                strncpy(date, in, sizeof date);
                    854:                        else {
                    855:                                date[0] = '\n';
                    856:                                date[1] = NULL;
                    857:                        }
1.29    ! deraadt   858:                } else {
1.1       deraadt   859:                        /*
                    860:                         * not the expected form
                    861:                         */
1.29    ! deraadt   862:                        fseeko(infile, (off_t)0, SEEK_SET);
1.1       deraadt   863:                        return;
                    864:                }
1.29    ! deraadt   865:        } else
1.1       deraadt   866:                /*
                    867:                 * empty file ?
                    868:                 */
                    869:                return;
                    870:
                    871:        /*
                    872:         * look for Subject line until EOF or a blank line
                    873:         */
1.29    ! deraadt   874:        while (fgets(inbuf, sizeof inbuf, infile) &&
        !           875:            !(blankline = (inbuf[0] == '\n'))) {
1.1       deraadt   876:                /*
                    877:                 * extract Subject line
                    878:                 */
                    879:                if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
                    880:                        seensubj = YES;
1.29    ! deraadt   881:                        frompos = ftello(infile);
1.1       deraadt   882:                        strncpy(subj, nxtfld(inbuf), sizeof subj);
                    883:                }
                    884:        }
                    885:        if (!blankline)
                    886:                /*
                    887:                 * ran into EOF
                    888:                 */
1.29    ! deraadt   889:                fseeko(infile, frompos, SEEK_SET);
1.1       deraadt   890:
                    891:        if (!seensubj)
                    892:                /*
                    893:                 * for possible use with Mail
                    894:                 */
                    895:                strncpy(subj, "(No Subject)\n", sizeof subj);
                    896: }
                    897:
                    898: char *
1.27      deraadt   899: nxtfld(char *s)
1.1       deraadt   900: {
1.29    ! deraadt   901:        if (*s)
        !           902:                while (*s && *s > ' ')
        !           903:                        s++;    /* skip over this field */
        !           904:        if (*s)
        !           905:                while (*s && *s <= ' ')
        !           906:                        s++;    /* find start of next field */
1.1       deraadt   907:        return (s);
                    908: }