Annotation of 43BSD/contrib/news/misc/unshar, revision 1.1.1.1

1.1       root        1: From [email protected] Sun Oct 28 16:56:42 1984
                      2: Relay-Version: version B 2.10.2 10/19/84; site seismo.UUCP
                      3: Posting-Version: version B 2.10.2 9/5/84; site unmvax.UUCP
                      4: Path: seismo!cmcl2!lanl!unm-cvax!unmvax!lee
                      5: From: [email protected]
                      6: Newsgroups: net.sources
                      7: Subject: Program to un-shar netmaps without using a shell..
                      8: Message-ID: <[email protected]>
                      9: Date: 28 Oct 84 21:56:42 GMT
                     10: Date-Received: 29 Oct 84 11:14:42 GMT
                     11: Distribution: net
                     12: Organization: Univ. of New Mexico, Albuquerque
                     13: Lines: 336
                     14: 
                     15: #ifndef        lint
                     16: char   *Rcsid = "$Header: getmaps.c,v 1.4 84/10/13 18:19:13 lee Exp $";
                     17: #endif
                     18: 
                     19: /*
                     20:  * getmaps
                     21:  *
                     22:  * Get the net maps from USENET as published by Karen and Mark Horton, in
                     23:  * "shar" format. Because of paranoia the sh is not used but instead a DFA
                     24:  * recognizing the appropriate commands.
                     25:  *
                     26:  * lee Ward            10/13/84
                     27:  */
                     28: 
                     29: #include <stdio.h>
                     30: #include <ctype.h>
                     31: #include <sys/types.h>
                     32: #include <sys/stat.h>
                     33: #include <sys/dir.h>
                     34: 
                     35: char   *mapgrp = "/usr/spool/news/net/news/map";
                     36: char   *mapdir = "/usr/lee/netmap/maps";
                     37: char   *seqfil = "/usr/lee/netmap/.seq";
                     38: char   *logfil = "/usr/lee/netmap/log";
                     39: 
                     40: char   *usestr = "[-l logfil] [-g group] [-s seqfile] [-a archiv-dir]";
                     41: 
                     42: FILE   *logsd = NULL;
                     43: 
                     44: void   domaps(), myabort(), log(), mkmaps(), getwrd(), logtime();
                     45: 
                     46: main(argc, argv)
                     47:        int     argc;
                     48:        char    *argv[];
                     49: {
                     50:        int     x;
                     51:        FILE    *seqsd;
                     52:        char    seqbuf[BUFSIZ];
                     53: 
                     54:        for (x = 1; x < argc; x++) {
                     55:                if (*argv[x]++ != '-') {
                     56:                        fprintf(stderr, "Bad usage\n");
                     57:                        fprintf(stderr, "Usage: %s %s\n", argv[0], usestr);
                     58:                        exit(-1);
                     59:                }
                     60:                switch (*argv[x]) {
                     61: 
                     62:                case 'l':
                     63:                        logfil = argv[++x];
                     64:                        break;
                     65:                case 'g':
                     66:                        mapgrp = argv[++x];
                     67:                        break;
                     68:                case 's':
                     69:                        seqfil = argv[++x];
                     70:                        break;
                     71:                case 'a':
                     72:                        mapdir = argv[++x];
                     73:                        break;
                     74:                default:
                     75:                        fprintf(stderr, "Bad switch\n");
                     76:                        fprintf(stderr, "Usage: %s %s\n", argv[0], usestr);
                     77:                        exit(-1);
                     78:                }
                     79:        }
                     80: 
                     81:        logsd = fopen(logfil, "a");
                     82: 
                     83:        logtime("Start");
                     84: 
                     85:        if (chdir(mapdir) != 0)
                     86:                myabort("Could not change directory to %s", mapdir);
                     87: 
                     88:        seqbuf[0] = NULL;
                     89:        if ((seqsd = fopen(seqfil, "r")) != NULL) {
                     90:                if ((x = fread(seqbuf, sizeof(char), sizeof(seqbuf),
                     91:                    seqsd)) != 0)
                     92:                        seqbuf[x - 1] = NULL;
                     93:                (void )fclose(seqsd);
                     94:        }
                     95:        if ((seqsd = fopen(seqfil, "a")) == NULL)
                     96:                myabort("Could not open seq file for writing");
                     97:        (void )fseek(seqsd, 0L, 0);
                     98: 
                     99:        domaps(mapgrp, seqbuf, seqsd);
                    100:        (void )fclose(seqsd);
                    101: 
                    102:        logtime("End");
                    103: }
                    104: 
                    105: void
                    106: domaps(grp, seqbuf, seqsd)
                    107:        char    *grp, *seqbuf;
                    108:        FILE    *seqsd;
                    109: {
                    110:        char    nbuf[BUFSIZ], *nptr, *tptr;
                    111:        struct direct **filst;
                    112:        int     nfils, x;
                    113:        struct stat stbuf;
                    114:        extern int scandir(), alphasort();
                    115:        extern char *strcpy(), *strncat();
                    116: 
                    117:        if ((nfils = scandir(grp, &filst, (int (*)())NULL, alphasort)) == -1)
                    118:                myabort("scandir failed");
                    119: 
                    120:        (void )strcpy(nbuf, grp);
                    121:        nptr = nbuf + strlen(nbuf);
                    122:        *nptr++ = '/';
                    123:        *nptr = NULL;
                    124:        nbuf[BUFSIZ] = NULL;
                    125: 
                    126:        for (x = 0; x < nfils; x++) {
                    127:                if (strcmp(".", filst[x]->d_name) == 0 ||
                    128:                    strcmp("..", filst[x]->d_name) == 0)
                    129:                        continue;
                    130:                tptr = filst[x]->d_name;
                    131:                while(*tptr && isdigit(*tptr))
                    132:                        tptr++;
                    133:                if (*tptr != NULL)
                    134:                        continue;
                    135:                *nptr = NULL;
                    136:                (void )strncat(nptr, filst[x]->d_name,
                    137:                    BUFSIZ - (nptr - nbuf) - 1);
                    138:                if (stat(nbuf, &stbuf) != 0) {
                    139:                        log("Could not stat %s", nbuf);
                    140:                        continue;
                    141:                }
                    142:                if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
                    143:                        continue;
                    144:                if (strcmp(seqbuf, filst[x]->d_name) >= 0)
                    145:                        continue;
                    146: 
                    147:                mkmaps(nbuf);
                    148:                (void )fseek(seqsd, 0L, 0);
                    149:                (void )fwrite(filst[x]->d_name, sizeof(char),
                    150:                    strlen(filst[x]->d_name), seqsd);
                    151:                (void )fputc('\n', seqsd);
                    152:                (void )fflush(seqsd);
                    153:        }
                    154: }
                    155: 
                    156: void
                    157: mkmaps(file)
                    158:        char    *file;
                    159: {
                    160:        char    buf[BUFSIZ], tofil[BUFSIZ], delim[BUFSIZ];
                    161:        int     state, sizdel;
                    162:        FILE    *isd, *osd;
                    163:        extern FILE *fopen();
                    164: 
                    165: #define        SEARCH          1
                    166: #define        INAMAP          2
                    167: #define        SKIPPING        3
                    168: 
                    169:        if ((isd = fopen(file, "r")) == NULL) {
                    170:                log("Could not open %s. Skipping...", file);
                    171:                return;
                    172:        }
                    173:        log("Unarchive %s", file);
                    174: 
                    175:        state = SEARCH;
                    176:        while (fgets(buf, sizeof(buf) - 1, isd) != NULL) {
                    177:                buf[sizeof(buf)] = NULL;
                    178:                if (state == SEARCH) {
                    179:                        if (gotcat(buf, tofil, BUFSIZ, delim, BUFSIZ)) {
                    180:                                state = INAMAP;
                    181:                                sizdel = strlen(delim);
                    182:                                if ((osd = fopen(tofil, "w")) == NULL) {
                    183:                                        log("Could not open %s", tofil);
                    184:                                        state = SKIPPING;
                    185:                                }
                    186:                        }
                    187:                        continue;
                    188:                }
                    189:                if (strncmp(buf, delim, sizdel) == 0) {
                    190:                        state = SEARCH;
                    191:                        if (osd != NULL)
                    192:                                (void )fclose(osd);
                    193:                        continue;
                    194:                }
                    195:                if (state == SKIPPING)
                    196:                        continue;
                    197:                fputs(buf, osd);
                    198:        }
                    199:        if (state != SEARCH)
                    200:                log("Read/sync error on %s", file);
                    201:        (void )fclose(isd);
                    202: 
                    203: #undef SEARCH
                    204: #undef INAMAP
                    205: #undef SKIPPING
                    206: }
                    207: 
                    208: /*
                    209:  * gotcat
                    210:  *
                    211:  * Use a DFA to recognize
                    212:  *     cat << DELIM > OUT
                    213:  * or
                    214:  *     cat > OUT << DELIM
                    215:  *
                    216:  */
                    217: 
                    218: /* Transition table for the DFA */
                    219: int    ttbl[9][4] = {
                    220:                1,-1,-1,-1,
                    221:                -1,6,2,-1,
                    222:                -1,-1,-1,3,
                    223:                -1,4,-1,-1,
                    224:                -1,-1,-1,5,
                    225:                -1,-1,-1,-1,
                    226:                -1,-1,-1,7,
                    227:                -1,-1,8,-1,
                    228:                -1,-1,-1,5,
                    229:        };
                    230: 
                    231: gotcat(buf, tofil, tofilln, delim, delimln)
                    232:        char    *buf,
                    233:                *tofil,
                    234:                *delim;
                    235:        int     tofilln,
                    236:                delimln;
                    237: {
                    238:        int     state;
                    239:        char    *ptr;
                    240: 
                    241:        state = 0;                      /* Start state */
                    242:        while (state != -1 && state != 5) {
                    243:                /* Eat up white */
                    244:                while (*buf != '\n' && (*buf == ' ' || *buf == '\t'))
                    245:                        buf++;
                    246:                if (*buf == '>') {
                    247:                        buf++;
                    248:                        state = ttbl[state][1];
                    249:                        continue;
                    250:                }
                    251:                if (*buf == '<' && *(buf + 1) == '<') {
                    252:                        buf += 2;
                    253:                        state = ttbl[state][2];
                    254:                        continue;
                    255:                }
                    256:                if (*buf == 'c' && *(buf + 1) == 'a' && *(buf + 2) == 't') {
                    257:                        buf += 3;
                    258:                        state = ttbl[state][0];
                    259:                        continue;
                    260:                }
                    261:                ptr = buf;
                    262:                while (*buf != '\n' && *buf != ' ' && *buf != '\t')
                    263:                        buf++;
                    264:                if (state == 2 || state == 8)
                    265:                        getwrd(ptr, buf, delim, delimln);
                    266:                else if (state == 6 || state == 4)
                    267:                        getwrd(ptr, buf, tofil, tofilln);
                    268:                state = ttbl[state][3];
                    269:        }
                    270: 
                    271:        if (state == 5)
                    272:                return(1);
                    273:        return(0);
                    274: }
                    275: 
                    276: void
                    277: getwrd(fc, lc, buf, maxlen)
                    278:        char    *fc,
                    279:                *lc,
                    280:                *buf;
                    281:        int     maxlen;
                    282: {
                    283:        char    *ptr, *t1ptr, *t2ptr;
                    284: 
                    285:        maxlen--;
                    286:        maxlen = lc - fc > maxlen ? maxlen : lc - fc;
                    287:        ptr = buf;
                    288:        t1ptr = fc;
                    289:        while (maxlen-- != 0)
                    290:                *ptr++ = *t1ptr++;
                    291:        *ptr = NULL;
                    292: 
                    293:        /* Strip quotes */
                    294:        ptr = buf;
                    295:        while (*ptr != NULL) {
                    296:                if (*ptr == '\\' && (*(ptr + 1) == '\'' || *(ptr + 1) == '"'))
                    297:                        ptr += 2;
                    298:                else if (*ptr == '\'' || *ptr == '"') {
                    299:                        t1ptr = ptr;
                    300:                        t2ptr = ptr + 1;
                    301:                        while ((*t1ptr++ = *t2ptr++) != NULL)
                    302:                                ;
                    303:                } else
                    304:                        ptr++;
                    305:        }
                    306: }
                    307: /*VARARGS1*/
                    308: void
                    309: myabort(s, a, b, c, d, e, f, g, h, i, j, k, l)
                    310:        char    *s;
                    311: {
                    312: 
                    313:        if (logsd != NULL) {
                    314:                fputs("ABORT - ", logsd);
                    315:                fprintf(logsd, s, a, b, c, d, e, f, g, h, i, j, k, l);
                    316:                (void )fputc('\n', logsd);
                    317:                logtime("End");
                    318:        }
                    319:        exit(-1);
                    320: }
                    321: 
                    322: /*VARARGS1*/
                    323: void
                    324: log(s, a, b, c, d, e, f, g, h, i, j, k, l)
                    325:        char    *s;
                    326: {
                    327: 
                    328:        if (logsd == NULL)
                    329:                return;
                    330:        fprintf(logsd, s, a, b, c, d, e, f, g, h, i, j, k, l);
                    331:        (void )fputc('\n', logsd);
                    332:        (void )fflush(logsd);
                    333: }
                    334: 
                    335: void
                    336: logtime(s)
                    337:        char    *s;
                    338: {
                    339:        time_t  clock;
                    340:        extern char *ctime();
                    341: 
                    342:        if (logsd == NULL)
                    343:                return;
                    344:        (void )time(&clock);
                    345:        fprintf(logsd, "%s %s", s, ctime(&clock));
                    346:        (void )fflush(logsd);
                    347: }
                    348: -- 
                    349:                        --Lee (Ward)
                    350:                        {ucbvax,convex,gatech,pur-ee}!unmvax!lee
                    351: 
                    352: 

unix.superglobalmegacorp.com

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