Annotation of researchv10no/cmd/asd/args.c, revision 1.1.1.1

1.1       root        1: #include "asd.h"
                      2: 
                      3: getargs (argc, argv, optkey, func)
                      4:        int argc;
                      5:        char **argv;
                      6:        char *optkey;
                      7:        int (*func)();
                      8: {
                      9:        register int c;
                     10:        int rc = 0;
                     11: 
                     12:        while ((c = getopt (argc, argv, optkey)) != EOF) {
                     13:                register struct replist *rl;
                     14:                register char *p, *q;
                     15: 
                     16:                switch (c) {
                     17: 
                     18:                case 'b':
                     19:                        bflag++;
                     20:                        break;
                     21: 
                     22:                case 'k':
                     23:                        kflag++;
                     24:                        break;
                     25: 
                     26:                case 'n':
                     27:                        nflag++;
                     28:                        break;
                     29:                
                     30:                case 'v':
                     31:                        vflag++;
                     32:                        break;
                     33: 
                     34:                case 'x':
                     35:                        if (xstr) {
                     36:                                fprintf (stderr, "duplicate -x ignored\n");
                     37:                                rc++;
                     38:                        } else if (Xstr) {
                     39:                                fprintf (stderr, "cannot have both -x and -X\n");
                     40:                                rc++;
                     41:                        } else
                     42:                                xstr = optarg;
                     43:                        break;
                     44: 
                     45:                case 'X':
                     46:                        if (Xstr) {
                     47:                                fprintf (stderr, "duplicate -X ignored\n");
                     48:                                rc++;
                     49:                        } else if (xstr) {
                     50:                                fprintf (stderr, "cannot have both -x and -X\n");
                     51:                                rc++;
                     52:                        } else
                     53:                                Xstr = copy (transname (optarg));
                     54:                        break;
                     55: 
                     56:                case 'D':
                     57:                        p = strchr (optarg, '=');
                     58:                        if (p == NULL) {
                     59:                                fprintf (stderr, "invalid option %s\n", optarg);
                     60:                                exit (1);
                     61:                        }
                     62:                        rl = new (struct replist);
                     63: 
                     64:                        /* copy the pathname to rl->source */
                     65:                        rl->source = alloc ((unsigned) (p - optarg + 1));
                     66:                        p = rl->source;
                     67:                        q = optarg;
                     68:                        while (*q != '=')
                     69:                                *p++ = *q++;
                     70:                        *p = '\0';
                     71: 
                     72:                        /* now expand rl->source */
                     73:                        p = rl->source;
                     74:                        rl->source = copy (fullname (p));
                     75:                        free (p);
                     76: 
                     77:                        /* expand rl->dest */
                     78:                        rl->dest = copy (fullname (q + 1));
                     79: 
                     80:                        /* link rl into the chain */
                     81:                        rl->link = replist;
                     82:                        replist = rl;
                     83:                        break;
                     84: 
                     85:                case 'K':
                     86:                        Kflag++;
                     87:                        keyfile = optarg;
                     88:                        break;
                     89: 
                     90:                case '?':
                     91:                default:
                     92:                        rc++;
                     93:                        break;
                     94:                }
                     95:        }
                     96: 
                     97:        if (rc) {
                     98:                fprintf (stderr, "%s: bad argument\n", argv[0]);
                     99:                exit (rc);
                    100:        }
                    101: 
                    102:        if (kflag && Kflag) {
                    103:                fprintf (stderr, "%s: cannot specify both k and K\n", argv[0]);
                    104:                exit (1);
                    105:        }
                    106: 
                    107:        /* read key from terminal if requested */
                    108:        if (kflag) {
                    109:                register char *p;
                    110:                p = getpass ("Key:");
                    111: 
                    112:                /* a null key is treated as no key at all */
                    113:                if (p && *p)
                    114:                        setup (p);
                    115:                else
                    116:                        kflag = 0;
                    117:        }
                    118:        
                    119:        /* read key from file if requested */
                    120:        if (Kflag) {
                    121:                char key[100];
                    122:                register FILE *kf;
                    123:                register char *p;
                    124: 
                    125:                /* try to open the file */
                    126:                kf = fopen (keyfile, "r");
                    127:                if (kf == NULL) {
                    128:                        perror (keyfile);
                    129:                        exit (1);
                    130:                }
                    131: 
                    132:                /* read the first line */
                    133:                p = fgets (key, sizeof (key), kf);
                    134: 
                    135:                fclose (kf);
                    136: 
                    137:                /* if EOF, assume no key */
                    138:                if (p == NULL) {
                    139:                        Kflag = 0;
                    140:                } else {
                    141: 
                    142:                        /* delete the trailing newline */
                    143:                        p = key;
                    144:                        while (*p != '\n' && *p != '\0')
                    145:                                p++;
                    146:                        *p = '\0';
                    147: 
                    148:                        /* if the key is empty, assume no key */
                    149:                        if (key[0] == '\0')
                    150:                                Kflag = 0;
                    151:                        else
                    152:                                setup (key);
                    153:                }
                    154:        }
                    155: 
                    156:        if (func) {
                    157:                /* process the arguments */
                    158:                if (optind >= argc)
                    159:                        rc = (*func) (stdin, "standard input");
                    160:                else {
                    161:                        register int i;
                    162:                        for (i = optind; i < argc; i++) {
                    163:                                register char *fn = argv[i];
                    164:                                register FILE *f = fopen (fn, "r");
                    165:                                if (f) {
                    166:                                        rc += (*func) (f, argv[i]);
                    167:                                        fclose (f);
                    168:                                } else {
                    169:                                        fprintf (stderr, "%s: can't open %s\n", argv[0], fn);
                    170:                                        rc++;
                    171:                                }
                    172:                        }
                    173:                }
                    174:        }
                    175: 
                    176:        return rc;
                    177: }

unix.superglobalmegacorp.com

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