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