1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: athdir.c,v 1.4 1999-09-15 23:57:14 danw Exp $"; |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <string.h> |
---|
21 | #include <sys/param.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/stat.h> |
---|
24 | #include <athdir.h> |
---|
25 | |
---|
26 | char *progName; |
---|
27 | |
---|
28 | void usage(void); |
---|
29 | void repeatflag(char *option); |
---|
30 | |
---|
31 | int main(int argc, char **argv) |
---|
32 | { |
---|
33 | int num_dirs = 0, flags = 0; |
---|
34 | char **dir_list, *type = NULL, *athsys = NULL, *hosttype = NULL; |
---|
35 | char *auxconvention = NULL, *recsep = NULL, **ptr; |
---|
36 | char **path_list; |
---|
37 | int i; |
---|
38 | int match = 0; |
---|
39 | |
---|
40 | progName = strrchr(argv[0], '/'); |
---|
41 | if (progName != NULL) |
---|
42 | progName++; |
---|
43 | else |
---|
44 | progName = argv[0]; |
---|
45 | |
---|
46 | if (argc == 1) |
---|
47 | usage(); |
---|
48 | |
---|
49 | if (argc) |
---|
50 | dir_list = malloc((argc + 1) * sizeof(char *)); |
---|
51 | |
---|
52 | if (dir_list == NULL) |
---|
53 | { |
---|
54 | fprintf(stderr, "%s: out of memory\n", progName); |
---|
55 | exit(1); |
---|
56 | } |
---|
57 | |
---|
58 | if (argv[1][0] != '-') |
---|
59 | { |
---|
60 | if (argc > 3) |
---|
61 | usage(); |
---|
62 | |
---|
63 | dir_list[num_dirs++] = argv[1]; |
---|
64 | |
---|
65 | if (argv[2]) |
---|
66 | { |
---|
67 | if (argv[2][0] == '-') |
---|
68 | usage(); |
---|
69 | |
---|
70 | type = argv[2]; |
---|
71 | } |
---|
72 | else |
---|
73 | type = "bin"; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | argv++; |
---|
78 | |
---|
79 | while (*argv) |
---|
80 | { |
---|
81 | if (**argv != '-' || (*argv)[2] != '\0') |
---|
82 | { |
---|
83 | fprintf(stderr, "%s: unknown option: %s\n", progName, *argv); |
---|
84 | usage(); |
---|
85 | } |
---|
86 | |
---|
87 | switch((*argv)[1]) |
---|
88 | { |
---|
89 | case 't': |
---|
90 | if (type) |
---|
91 | repeatflag(*argv); |
---|
92 | argv++; |
---|
93 | if (*argv == NULL) |
---|
94 | usage(); |
---|
95 | type = *argv++; |
---|
96 | break; |
---|
97 | |
---|
98 | case 'p': |
---|
99 | if (num_dirs != 0) |
---|
100 | repeatflag(*argv); |
---|
101 | |
---|
102 | argv++; |
---|
103 | if (*argv == NULL) |
---|
104 | usage(); |
---|
105 | while (*argv != NULL && **argv != '-') |
---|
106 | dir_list[num_dirs++] = *argv++; |
---|
107 | break; |
---|
108 | |
---|
109 | case 'e': |
---|
110 | if (flags & ATHDIR_SUPPRESSEDITORIALS) |
---|
111 | repeatflag(*argv); |
---|
112 | argv++; |
---|
113 | flags |= ATHDIR_SUPPRESSEDITORIALS; |
---|
114 | break; |
---|
115 | |
---|
116 | case 'c': |
---|
117 | if (flags & ATHDIR_SUPPRESSSEARCH) |
---|
118 | repeatflag(*argv); |
---|
119 | argv++; |
---|
120 | flags |= ATHDIR_SUPPRESSSEARCH; |
---|
121 | break; |
---|
122 | |
---|
123 | case 'l': |
---|
124 | if (flags & ATHDIR_LISTSEARCHDIRECTORIES) |
---|
125 | repeatflag(*argv); |
---|
126 | argv++; |
---|
127 | flags |= ATHDIR_LISTSEARCHDIRECTORIES; |
---|
128 | break; |
---|
129 | |
---|
130 | case 's': |
---|
131 | if (athsys) |
---|
132 | repeatflag(*argv); |
---|
133 | argv++; |
---|
134 | if (*argv == NULL) |
---|
135 | usage(); |
---|
136 | athsys = *argv++; |
---|
137 | break; |
---|
138 | |
---|
139 | case 'm': |
---|
140 | if (hosttype != NULL) |
---|
141 | repeatflag(*argv); |
---|
142 | argv++; |
---|
143 | if (*argv == NULL) |
---|
144 | usage(); |
---|
145 | hosttype = *argv++; |
---|
146 | break; |
---|
147 | |
---|
148 | case 'f': |
---|
149 | if (auxconvention) |
---|
150 | repeatflag(*argv); |
---|
151 | argv++; |
---|
152 | if (*argv == NULL) |
---|
153 | usage(); |
---|
154 | auxconvention = *argv++; |
---|
155 | break; |
---|
156 | |
---|
157 | case 'r': |
---|
158 | if (recsep != NULL) |
---|
159 | repeatflag(*argv); |
---|
160 | argv++; |
---|
161 | if (*argv == NULL) |
---|
162 | usage(); |
---|
163 | recsep = *argv++; |
---|
164 | break; |
---|
165 | |
---|
166 | case 'd': |
---|
167 | if (flags & ATHDIR_MACHINEDEPENDENT) |
---|
168 | repeatflag(*argv); |
---|
169 | argv++; |
---|
170 | flags |= ATHDIR_MACHINEDEPENDENT; |
---|
171 | break; |
---|
172 | |
---|
173 | case 'i': |
---|
174 | if (flags & ATHDIR_MACHINEINDEPENDENT) |
---|
175 | repeatflag(*argv); |
---|
176 | argv++; |
---|
177 | flags |= ATHDIR_MACHINEINDEPENDENT; |
---|
178 | break; |
---|
179 | |
---|
180 | default: |
---|
181 | fprintf(stderr, "%s: unknown option: %s\n", progName, *argv); |
---|
182 | usage(); |
---|
183 | break; |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | if (!num_dirs) |
---|
189 | dir_list[num_dirs++] = NULL; |
---|
190 | |
---|
191 | /* Default record separator is a newline. */ |
---|
192 | if (!recsep) |
---|
193 | recsep = "\n"; |
---|
194 | |
---|
195 | for (i = 0; i < num_dirs; i++) |
---|
196 | { |
---|
197 | path_list = athdir_get_paths(dir_list[i], type, athsys, NULL, hosttype, |
---|
198 | auxconvention, flags); |
---|
199 | if (path_list != NULL) |
---|
200 | { |
---|
201 | for (ptr = path_list; *ptr != NULL; ptr++) |
---|
202 | { |
---|
203 | if (match == 1) |
---|
204 | fprintf(stdout, "%s", recsep); |
---|
205 | match = 1; |
---|
206 | fprintf(stdout, "%s", *ptr); |
---|
207 | } |
---|
208 | |
---|
209 | athdir_free_paths(path_list); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | if (match) |
---|
214 | fprintf(stdout, "\n"); |
---|
215 | |
---|
216 | #ifdef DEBUG |
---|
217 | fprintf(stdout, "%s ", progName); |
---|
218 | if (type != NULL) |
---|
219 | fprintf(stdout, "-t %s ", type); |
---|
220 | if (num_dirs) |
---|
221 | { |
---|
222 | fprintf(stdout, "-p "); |
---|
223 | for (i = 0; i < num_dirs; i++) |
---|
224 | fprintf(stdout, "%s ", dir_list[i]); |
---|
225 | } |
---|
226 | if (ATHDIR_SUPPRESSEDITORIALS & flags) |
---|
227 | fprintf(stdout, "-e "); |
---|
228 | if (ATHDIR_SUPPRESSSEARCH & flags) |
---|
229 | fprintf(stdout, "-c "); |
---|
230 | if (ATHDIR_LISTSEARCHDIRECTORIES & flags) |
---|
231 | fprintf(stdout, "-l "); |
---|
232 | fprintf(stdout, "\n"); |
---|
233 | #endif |
---|
234 | |
---|
235 | return match == 0; |
---|
236 | } |
---|
237 | |
---|
238 | void usage(void) |
---|
239 | { |
---|
240 | fprintf(stderr, "usage: %s path [type]\n", progName); |
---|
241 | fprintf(stderr, |
---|
242 | " or: %s [-t type] [-p path ...] [-e] [-c] [-l] [-d | -i]\n", |
---|
243 | progName); |
---|
244 | fprintf(stderr, |
---|
245 | " [-r recsep] [-f format] [-s sysname] [-m machtype]\n"); |
---|
246 | exit(1); |
---|
247 | } |
---|
248 | |
---|
249 | void repeatflag(char *option) |
---|
250 | { |
---|
251 | fprintf(stderr, "%s: %s already specified.\n", progName, option); |
---|
252 | usage(); |
---|
253 | } |
---|