|
|
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 'D':
35: p = index (optarg, '=');
36: if (p == NULL) {
37: fprintf (stderr, "invalid option %s\n", optarg);
38: exit (1);
39: }
40: rl = new (struct replist);
41:
42: /* copy the pathname to rl->source */
43: rl->source = alloc ((unsigned) (p - optarg + 1));
44: p = rl->source;
45: q = optarg;
46: while (*q != '=')
47: *p++ = *q++;
48: *p = '\0';
49:
50: /* now expand rl->source */
51: p = rl->source;
52: rl->source = copy (fullname (p));
53: free (p);
54:
55: /* expand rl->dest */
56: rl->dest = copy (fullname (q + 1));
57:
58: /* link rl into the chain */
59: rl->link = replist;
60: replist = rl;
61: break;
62:
63: case 'K':
64: Kflag++;
65: keyfile = optarg;
66: break;
67:
68: case '?':
69: default:
70: rc++;
71: break;
72: }
73: }
74:
75: if (rc) {
76: fprintf (stderr, "%s: bad argument\n", argv[0]);
77: exit (rc);
78: }
79:
80: if (kflag && Kflag) {
81: fprintf (stderr, "%s: cannot specify both k and K\n", argv[0]);
82: exit (1);
83: }
84:
85: /* read key from terminal if requested */
86: if (kflag) {
87: register char *p;
88: p = getpass ("Key:");
89:
90: /* a null key is treated as no key at all */
91: if (p && *p)
92: setup (p);
93: else
94: kflag = 0;
95: }
96:
97: /* read key from file if requested */
98: if (Kflag) {
99: char key[100];
100: register FILE *kf;
101: register char *p;
102:
103: /* try to open the file */
104: kf = fopen (keyfile, "r");
105: if (kf == NULL) {
106: perror (keyfile);
107: exit (1);
108: }
109:
110: /* read the first line */
111: p = fgets (key, sizeof (key), kf);
112:
113: fclose (kf);
114:
115: /* if EOF, assume no key */
116: if (p == NULL) {
117: Kflag = 0;
118: } else {
119:
120: /* delete the trailing newline */
121: p = key;
122: while (*p != '\n' && *p != '\0')
123: p++;
124: *p = '\0';
125:
126: /* if the key is empty, assume no key */
127: if (key[0] == '\0')
128: Kflag = 0;
129: else
130: setup (key);
131: }
132: }
133:
134: if (func) {
135: /* process the arguments */
136: if (optind >= argc)
137: rc = (*func) (stdin, "standard input");
138: else {
139: register int i;
140: for (i = optind; i < argc; i++) {
141: register char *fn = argv[i];
142: register FILE *f = fopen (fn, "r");
143: if (f) {
144: rc += (*func) (f, argv[i]);
145: fclose (f);
146: } else {
147: fprintf (stderr, "%s: can't open %s\n", argv[0], fn);
148: rc++;
149: }
150: }
151: }
152: }
153:
154: return rc;
155: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.