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

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