Annotation of researchv10no/cmd/spool/spool.c, revision 1.1.1.1

1.1       root        1: #include       <signal.h>
                      2: #include       <sys/types.h>
                      3: #include       <sys/stat.h>
                      4: 
                      5: #define        ONL     0
                      6: #define        TOSS    1
                      7: int    INCHAR  = 0;            /*index of incremented character in
                      8:                                        temporary file names. */
                      9: 
                     10: char   version[] = "Version 2/6/79";
                     11: 
                     12: char   grade;
                     13: char   remote[]= "$    remote  **,onl";
                     14: char   toss[]  = "$    sysout  toss";
                     15: int    remotsw;                /*toss-output flag*/
                     16: char   *mailfile = 0;
                     17: char   wantmail = 0;
                     18: char   *pp     = 0;            /*recipient of mail*/
                     19: char   *identf = 0;            /*ident card info*/
                     20: int    uidf    = 0;
                     21: char   gcosid[13];             /*gcos userid*/
                     22: char   cpflag = 'l';           /*copy/link flag*/
                     23: int    rmflag  = 0;            /*remove flag*/
                     24: int    debug   = 0;
                     25: int    gcdebug = 0;            /*GCOS debug switch*/
                     26: int    archsw = 0;             /*archive switch*/
                     27: 
                     28: int    argc;
                     29: char   **argv;
                     30: char   *arg;
                     31: char   buf[80];                /*used by card */
                     32: int    nact = 0;               /*number of non-null files to process.*/
                     33: int    gsize   = 20;           /*size of current file in GCOS blocks.*/
                     34: long   usize   = 20*1200;      /*size of current file in bytes.*/
                     35: FILE   *tff;           /*temporary control card file*/
                     36: FILE   *nfile();
                     37: char   *getarg();
                     38: char   *sprintf();
                     39: 
                     40: 
                     41: comopt(o)              /*routine to test for common options.*/
                     42: char o;
                     43: {
                     44:        switch (o){
                     45: 
                     46:        case 'c':
                     47:                cpflag = 'c';
                     48:                break;
                     49: 
                     50:        case 'i':
                     51:                identf = getarg('i');
                     52:                break;
                     53: 
                     54:        case 'm':
                     55:                wantmail++;
                     56:                if(arg[2])
                     57:                        pp = &arg[2];
                     58:                break;
                     59:        
                     60:        case 'n':               /*new option to suppress mail. MRW*/
                     61:                wantmail = 0;
                     62:                break;
                     63: 
                     64:        case 'o':
                     65:                remotsw = ONL;
                     66:                break;
                     67: 
                     68:        case 'r':
                     69:                rmflag++;
                     70:                break;
                     71: 
                     72:        case 's':
                     73:                if(arg[2] < '1' || arg[2] > '3')
                     74:                        goto unknown;
                     75:                grade = arg[2];
                     76:                break;
                     77: 
                     78:        case 't':
                     79:                if(arg[2])
                     80:                        goto unknown;
                     81:                remotsw = TOSS;
                     82:                break;
                     83: 
                     84:        case '#':
                     85:                debug = 1;
                     86:                break;
                     87: 
                     88:        case 'Z':                       /*GCOS debugging switch*/
                     89:                gcdebug = 1;
                     90:                break;
                     91: 
                     92: unknown:
                     93:        default:
                     94:                return(0);
                     95:        }
                     96:        return(1);
                     97: }
                     98: 
                     99: 
                    100: #if LPR == 0
                    101: 
                    102: spool1()               /*set up common initial GCOS control cards.*/
                    103: {
                    104:        if(debug)
                    105:                tff = stdout;
                    106:        else
                    107:                if((tff = nfile(tfname)) == NULL){
                    108:                        fprintf(stderr, "%s: Can't create %s.\n", NAME, tfname);
                    109:                        out();
                    110:                }
                    111:        card('S', "");
                    112:        card('L', sprintf(buf, "$       sgrade  %c   %s", grade, version ) );
                    113:        if(ident())
                    114:                out();
                    115:        card('L', remote);
                    116:        if(remotsw == TOSS)
                    117:                card('L', toss);
                    118: }
                    119: 
                    120: 
                    121: spool2()                       /*add final control cards, and spool job.*/
                    122: {
                    123:        if(wantmail)
                    124:                card('N', mailfile);
                    125:        card('L', "$    endjob");
                    126:        if(debug)
                    127:                out();
                    128:        fclose(tff);
                    129:        if(nact) {
                    130:                dfname[INCHAR]++;
                    131:                if(link(tfname, dfname) < 0){
                    132:                        fprintf(stderr, "%s: Cannot rename %s\n", NAME, tfname);
                    133:                        out();
                    134:                }
                    135:                unlink(tfname);
                    136:                execl("/usr/lib/dpd", "dpd", 0);
                    137:                execl("/etc/dpd", "dpd", 0);
                    138:                fprintf(stderr, "%s: Can't find dpd.\nFiles left in spooling dir.\n", NAME);
                    139:                exit(1);
                    140:        }
                    141: }
                    142: 
                    143: #endif
                    144: 
                    145: 
                    146: #if FGET == 0
                    147: 
                    148: filargs()              /*process file arguments for dpr, gcat, fsend, lpr.*/
                    149: {
                    150:        int i;
                    151:        FILE *f;
                    152: 
                    153:        if(argc == 1){
                    154:                if(mailfile == 0)
                    155:                        mailfile = "pipe.end";
                    156:                if(copy(stdin, mailfile, GCAT) == -1)
                    157:                        out();
                    158:                if(archsw)
                    159:                        archive();
                    160:        }
                    161:        while(--argc) {
                    162:                arg = *++argv;
                    163:                switch(cpflag){
                    164: 
                    165:                case 'l':
                    166:                        if(lfname[INCHAR]++ >= 'z')
                    167:                                cpflag = rmflag ? 'c' : 'n';
                    168:                        else if(link(arg, lfname) == 0){
                    169:                                if(size(arg,arg) <= 0)
                    170:                                        continue;
                    171:                                nuact(arg);
                    172:                                card(BF, lfname);
                    173:                                card('U', lfname);
                    174:                                break;
                    175:                        }
                    176: 
                    177:                case 'n':
                    178:                        if(*arg == '/' && !rmflag){
                    179:                                if(size(arg,arg) <= 0)
                    180:                                        continue;
                    181:                                nuact(arg);
                    182:                                card(BF, arg);
                    183:                                break;
                    184:                        }
                    185: 
                    186:                case 'c':
                    187:                        f = fopen(arg, "r");
                    188:                        if(f == NULL){
                    189:                                fprintf(stderr, "%s: Cannot open %s\n", NAME, arg);
                    190:                                continue;
                    191:                        }
                    192:                        i = copy(f, arg, GCAT);
                    193:                        fclose(f);
                    194:                        if(i == -1)
                    195:                                continue;
                    196:                        break;
                    197:                }
                    198:                if(archsw)
                    199:                        archive();
                    200:                if(rmflag){
                    201:                        if(unlink(arg) < 0)
                    202:                                fprintf(stderr, "%s: Cannot remove %s\n", NAME, arg);
                    203:                }
                    204:                if(mailfile == 0)
                    205:                        mailfile = arg;
                    206:        }
                    207: }
                    208: 
                    209: #endif
                    210: 
                    211: 
                    212: FILE *nfile(name)              /*generate a new file name, and open file.*/
                    213: char *name;
                    214: {
                    215:        FILE *f;
                    216: 
                    217:        if(name[INCHAR] >= 'z')
                    218:                return(NULL);
                    219:        name[INCHAR]++;
                    220:        if(!access(name, 0) || (f = fopen(name, "w")) == NULL)
                    221:                return(NULL);
                    222:        return(f);
                    223: }
                    224: 
                    225: #if FGET == 0
                    226:  
                    227: copy(f, gname, gcatsw)
                    228: FILE   *f;
                    229: char   *gname;
                    230: int    gcatsw;
                    231: {
                    232:        int c;
                    233:        FILE *ff;
                    234:        long cnt;
                    235: 
                    236:        if((ff = nfile(cfname)) == NULL){
                    237:                fprintf(stderr, "%s: Too many copy files; %s not copied\n", NAME, gname);
                    238:                return(-1);
                    239:        }
                    240:        cnt = 0;
                    241:        while((c = getc(f)) != EOF){
                    242:                if(gcatsw)
                    243:                    if(c != 0){
                    244:                        fprintf(stderr, "%s: Bad input from %s.\n", NAME, gname);
                    245:                        out();
                    246:                    }else  gcatsw = 0;
                    247:                if((putc(c, ff) == EOF) && ferror(ff)){
                    248:                        fprintf(stderr, "%s: Write error on copy of %s.\n", NAME, gname);
                    249:                        break;
                    250:                }
                    251:                cnt++;
                    252:                if(cnt > MAXCOPY){
                    253:                        fprintf(stderr, "%s: Copy file %s is too large\n", NAME, gname);
                    254:                        fprintf(ff, "\nFile too long, output truncated.\n\f");
                    255:                        break;
                    256:                }
                    257:        }
                    258:        fclose(ff);
                    259:        if(size(cfname,gname) <= 0)
                    260:                return(-1);
                    261:        nuact(gname);
                    262:        card(BF, cfname);
                    263:        card('U', cfname);
                    264:        return(0);
                    265: }
                    266: 
                    267: #endif
                    268: 
                    269: card(c, s)
                    270: int c;
                    271: char   *s;
                    272: {
                    273:        putc( c, tff );
                    274: 
                    275:        while( (c = *s++) != '\0') putc( c, tff );
                    276: 
                    277:        c = putc( '\n', tff );
                    278: 
                    279:        if(c == EOF){
                    280:                fprintf(stderr, "%s: Error writing control file.\n", NAME);
                    281:                out();
                    282:                }
                    283: }
                    284: 
                    285: size(file, name)
                    286: char   *file, *name;
                    287: {
                    288:        struct stat stbuf;
                    289: 
                    290:        if(stat(file,&stbuf) < 0){
                    291:                fprintf(stderr, "%s: Cannot open %s\n", NAME, file);
                    292:                return(-1);
                    293:        }
                    294:        if(!stbuf.st_size){
                    295:                fprintf(stderr, "%s: File %s is empty.\n", NAME, name);
                    296:                return(0);
                    297:        }
                    298:        usize = stbuf.st_size;
                    299:        gsize = usize / 1200;
                    300:        gsize++;
                    301:        nact++;
                    302:        return(gsize);
                    303: }
                    304: 
                    305: 
                    306: char *
                    307: getarg(c)              /*get modifier for complex options --
                    308:                            from either same or next argument. MRW
                    309:                            e.g. either "-ffile" or "-f file"*/
                    310: char   c;
                    311: {
                    312: 
                    313:        if(arg[2])
                    314:                return(&arg[2]);
                    315:        else if(--argc>1)
                    316:                return(arg = (++argv)[1]);
                    317:        fprintf(stderr, "%s: Incomplete -%c option\n", NAME,c);
                    318:        out();
                    319: }
                    320: 
                    321: #include       <pwd.h>
                    322: struct passwd *getpwuid();
                    323: 
                    324: ident()
                    325: {
                    326:        int c, i, j, n, test, jsave;
                    327:        struct passwd *b1;
                    328:        static char b2[100];
                    329: 
                    330:        if((b1 = getpwuid(getuid())) == NULL) {
                    331:                fprintf(stderr, "%s: Invalid user id\n", NAME);
                    332:                return(1);
                    333:        }
                    334:        j = 0;
                    335: #if LPR == 0
                    336:        while(c = "$    ident   "[j])
                    337:                b2[j++] = c;
                    338: 
                    339:        i = 0;
                    340:        if(identf) 
                    341:                while(c = identf[i++])
                    342:                        b2[j++] = c;
                    343:        else{
                    344:                jsave = j;              /*use either usg or pwb-style passwd. MRW*/
                    345:                while((c = b1->pw_gecos[i++]) && c != ':')
                    346:                        if(c == ')')
                    347:                                j = jsave;
                    348:                        else
                    349:                                b2[j++] = c;
                    350:        }
                    351:        b2[j++] = ',';
                    352: #endif
                    353: 
                    354:        i = 0;
                    355:        if(!pp)
                    356:                pp = &b2[j];
                    357:        while(c = b1->pw_name[i++])
                    358:                b2[j++] = c;
                    359:        b2[j] = '\0';
                    360: 
                    361: #if LPR == 0
                    362:        i = 0;
                    363:        n = 3;
                    364:        while(--n) {
                    365:                test = 0;
                    366:                while((c=b2[i++]) && c != ',') {
                    367:                        if('0' <= c && c <= '9') test += c -'0';
                    368:                        else test = 0;
                    369:                }
                    370:                if(test == 0) {
                    371:                        b2[j] = '\0';
                    372:                        fprintf(stderr, "%s: Invalid IDENT information - %s\n", NAME, b2);
                    373:                        return (1);
                    374:                }
                    375:        }
                    376: 
                    377:        if(!uidf) {
                    378:                n = 0;
                    379:                while((c = b2[i++]) && c != ',') {
                    380:                        if(n >= 12) break;
                    381:                        gcosid[n++] = c;
                    382:                }
                    383:                gcosid[n++] = '\0';
                    384:        }
                    385: #endif
                    386:        card('L', b2);
                    387:        if(wantmail){
                    388:                card('M',pp);
                    389:                if(identf)
                    390:                        card('Q', b2);  /*mail back $IDENT card.*/
                    391:        }
                    392:        return (0);
                    393: }
                    394: 
                    395: pidfn()                        /*rewrite using mktemp. MRW*/
                    396: {
                    397:        int out();
                    398: 
                    399:        while(tfname[INCHAR] != 'X')
                    400:                INCHAR++;
                    401:        INCHAR--;
                    402:        mktemp(cfname);
                    403:        mktemp(dfname);
                    404:        mktemp(lfname);
                    405:        mktemp(tfname);
                    406:        mktemp(zfname);
                    407:        if(signal(SIGHUP, SIG_IGN) != SIG_IGN)
                    408:                signal(SIGHUP, out);
                    409:        if(signal(SIGINT, SIG_IGN) != SIG_IGN)
                    410:                signal(SIGINT, out);
                    411:        if(signal(SIGQUIT, SIG_IGN) != SIG_IGN)
                    412:                signal(SIGQUIT, out);
                    413:        if(signal(SIGTERM, SIG_IGN) != SIG_IGN)
                    414:                signal(SIGTERM, out);
                    415: }
                    416: 
                    417: out()
                    418: {
                    419:        register i;
                    420: 
                    421:        signal(SIGHUP, SIG_IGN);
                    422:        signal(SIGINT, SIG_IGN);
                    423:        signal(SIGQUIT, SIG_IGN);
                    424:        signal(SIGTERM, SIG_IGN);
                    425:        i = INCHAR;
                    426:        for(; cfname[i] != FIRSTCHAR; cfname[i]--) 
                    427:                unlink(cfname);
                    428:        if(dfname[i] != FIRSTCHAR)
                    429:                unlink(dfname);
                    430:        for(; lfname[i] != FIRSTCHAR; lfname[i]--) 
                    431:                unlink(lfname);
                    432:        if(tfname[i] != FIRSTCHAR)
                    433:                unlink(tfname);
                    434:        for(; zfname[i] != FIRSTCHAR; zfname[i]--) 
                    435:                unlink(zfname);
                    436:        exit(1);
                    437: }

unix.superglobalmegacorp.com

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