Annotation of 3BSD/cmd/ucbmail/main.c, revision 1.1

1.1     ! root        1: #
        !             2: 
        !             3: #include "rcv.h"
        !             4: #include <sys/stat.h>
        !             5: 
        !             6: /*
        !             7:  * Mail -- a mail program
        !             8:  *
        !             9:  * Startup -- interface with user.
        !            10:  */
        !            11: 
        !            12: /*
        !            13:  * Find out who the user is, copy his mail file (if exists) into
        !            14:  * /tmp/Rxxxxx and set up the message pointers.  Then, print out the
        !            15:  * message headers and read user commands.
        !            16:  *
        !            17:  * Command line syntax:
        !            18:  *     Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
        !            19:  * or:
        !            20:  *     Mail [ -i ] [ -r address ] [ -h number ] people ...
        !            21:  */
        !            22: 
        !            23: main(argc, argv)
        !            24:        char **argv;
        !            25: {
        !            26:        register char *ef;
        !            27:        register int i, argp;
        !            28:        int mustsend, uflag;
        !            29:        FILE *ibuf;
        !            30:        extern char tempMesg[], _sobuf[];
        !            31: 
        !            32: #ifdef signal
        !            33:        Siginit();
        !            34: #endif
        !            35: 
        !            36:        /*
        !            37:         * Set up a reasonable environment.  We clobber the last
        !            38:         * element of argument list for compatibility with version 6,
        !            39:         * figure out whether we are being run interactively, set up
        !            40:         * all the temporary files, buffer standard output, and so forth.
        !            41:         */
        !            42: 
        !            43:        uflag = 0;
        !            44:        argv[argc] = (char *) -1;
        !            45:        mypid = getpid();
        !            46:        intty = isatty(0);
        !            47:        outtty = isatty(1);
        !            48:        image = -1;
        !            49:        setbuf(stdout, _sobuf);
        !            50: 
        !            51:        /*
        !            52:         * Now, determine how we are being used.
        !            53:         * We successively pick off instances of -r, -h, -f, and -i.
        !            54:         * If there is anything left, it is the base of the list
        !            55:         * of users to mail to.  Argp will be set to point to the
        !            56:         * first of these users.
        !            57:         */
        !            58: 
        !            59:        ef = NOSTR;
        !            60:        argp = -1;
        !            61:        mustsend = 0;
        !            62:        for (i = 1; i < argc; i++) {
        !            63: 
        !            64:                /*
        !            65:                 * If current argument is not a flag, then the
        !            66:                 * rest of the arguments must be recipients.
        !            67:                 */
        !            68: 
        !            69:                if (*argv[i] != '-') {
        !            70:                        argp = i;
        !            71:                        break;
        !            72:                }
        !            73:                switch (argv[i][1]) {
        !            74:                case 'r':
        !            75:                        /*
        !            76:                         * Next argument is address to be sent along
        !            77:                         * to the mailer.
        !            78:                         */
        !            79:                        if (i >= argc - 1) {
        !            80:                                fprintf(stderr, "Address required after -r\n");
        !            81:                                exit(1);
        !            82:                        }
        !            83:                        mustsend++;
        !            84:                        rflag = argv[i+1];
        !            85:                        i++;
        !            86:                        break;
        !            87: 
        !            88:                case 'u':
        !            89:                        /*
        !            90:                         * Next argument is person to pretend to be.
        !            91:                         */
        !            92:                        uflag++;
        !            93:                        if (i >= argc - 1) {
        !            94:                                fprintf(stderr, "You obviously dont know what you're doing\n");
        !            95:                                exit(1);
        !            96:                        }
        !            97:                        strcpy(myname, argv[i+1]);
        !            98:                        i++;
        !            99:                        break;
        !           100: 
        !           101:                case 'i':
        !           102:                        /*
        !           103:                         * User wants to ignore interrupts.
        !           104:                         * Set the variable "ignore"
        !           105:                         */
        !           106:                        assign("ignore", "");
        !           107:                        break;
        !           108: 
        !           109:                case 'd':
        !           110:                        debug++;
        !           111:                        break;
        !           112: 
        !           113:                case 'h':
        !           114:                        /*
        !           115:                         * Specified sequence number for network.
        !           116:                         * This is the number of "hops" made so
        !           117:                         * far (count of times message has been
        !           118:                         * forwarded) to help avoid infinite mail loops.
        !           119:                         */
        !           120:                        if (i >= argc - 1) {
        !           121:                                fprintf(stderr, "Number required for -h\n");
        !           122:                                exit(1);
        !           123:                        }
        !           124:                        mustsend++;
        !           125:                        hflag = atoi(argv[i+1]);
        !           126:                        if (hflag == 0) {
        !           127:                                fprintf(stderr, "-h needs non-zero number\n");
        !           128:                                exit(1);
        !           129:                        }
        !           130:                        i++;
        !           131:                        break;
        !           132: 
        !           133:                case 'f':
        !           134:                        /*
        !           135:                         * User is specifying file to "edit" with Mail,
        !           136:                         * as opposed to reading system mailbox.
        !           137:                         * If no argument is given after -f, we read his
        !           138:                         * mbox file in his home directory.
        !           139:                         */
        !           140:                        if (i >= argc - 1)
        !           141:                                ef = mbox;
        !           142:                        else
        !           143:                                ef = argv[i + 1];
        !           144:                        i++;
        !           145:                        break;
        !           146: 
        !           147:                default:
        !           148:                        fprintf(stderr, "Unknown flag: %s\n", argv[i]);
        !           149:                        exit(1);
        !           150:                }
        !           151:        }
        !           152: 
        !           153:        /*
        !           154:         * Check for inconsistent arguments.
        !           155:         */
        !           156: 
        !           157:        if (ef != NOSTR && argp != -1) {
        !           158:                fprintf(stderr, "Cannot give -f and people to send to.\n");
        !           159:                exit(1);
        !           160:        }
        !           161:        if (mustsend && argp == -1) {
        !           162:                fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n");
        !           163:                exit(1);
        !           164:        }
        !           165:        tinit();
        !           166:        if (argp != -1) {
        !           167:                commands();
        !           168:                mail(&argv[argp]);
        !           169: 
        !           170:                /*
        !           171:                 * why wait?
        !           172:                 */
        !           173: 
        !           174:                exit(0);
        !           175:        }
        !           176: 
        !           177:        /*
        !           178:         * Ok, we are reading mail.
        !           179:         * Decide whether we are editing a mailbox or reading
        !           180:         * the system mailbox, and open up the right stuff.
        !           181:         */
        !           182: 
        !           183:        rcvmode++;
        !           184:        if (ef != NOSTR) {
        !           185:                edit++;
        !           186:                editfile = mailname = ef;
        !           187:                if ((ibuf = fopen(mailname, "r")) == NULL) {
        !           188:                        perror(mailname);
        !           189:                        exit(1);
        !           190:                }
        !           191:                if ((i = open(mailname, 1)) < 0)
        !           192:                        printf("Warning: \"%s\" not writable.\n", mailname);
        !           193:                else
        !           194:                        close(i);
        !           195:        }
        !           196:        else {
        !           197:                if ((ibuf = fopen(mailname, "r")) == NULL) {
        !           198:                        if (uflag)
        !           199:                                printf("No mail for %s\n", myname);
        !           200:                        else
        !           201:                                printf("No mail.\n");
        !           202:                        exit(0);
        !           203:                }
        !           204:        }
        !           205: 
        !           206:        /*
        !           207:         * Copy the messages into /tmp
        !           208:         * and set pointers.
        !           209:         */
        !           210: 
        !           211:        mailsize = fsize(ibuf);
        !           212:        if ((otf = fopen(tempMesg, "w")) == NULL) {
        !           213:                perror(tempMesg);
        !           214:                exit(1);
        !           215:        }
        !           216:        if ((itf = fopen(tempMesg, "r")) == NULL) {
        !           217:                perror(tempMesg);
        !           218:                exit(1);
        !           219:        }
        !           220:        remove(tempMesg);
        !           221:        setptr(ibuf);
        !           222:        fclose(ibuf);
        !           223: 
        !           224:        /*
        !           225:         * print headings and accept user commands.
        !           226:         */
        !           227: 
        !           228:        if (msgCount == 0) {
        !           229:                if (uflag)
        !           230:                        printf("No mail for %s\n", myname);
        !           231:                else
        !           232:                        printf("No messages.\n");
        !           233:                exit(1);
        !           234:        }
        !           235:        commands();
        !           236:        if (!edit)
        !           237:                quit();
        !           238:        exit(0);
        !           239: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.