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

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