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 char rcsid[] = "$Id: athdir.c,v 1.3 1998-03-17 03:46:02 cfields 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 | usage() |
---|
29 | { |
---|
30 | fprintf(stderr, "usage: %s path [type]\n", progName); |
---|
31 | fprintf(stderr, |
---|
32 | " or: %s [-t type] [-p path ...] [-e] [-c] [-l] [-d | -i]\n", |
---|
33 | progName); |
---|
34 | fprintf(stderr, |
---|
35 | " [-r recsep] [-f format] [-s sysname] [-m machtype]\n"); |
---|
36 | exit(1); |
---|
37 | } |
---|
38 | |
---|
39 | void repeatflag(char *option) |
---|
40 | { |
---|
41 | fprintf(stderr, "%s: %s already specified.\n", progName, *option); |
---|
42 | usage(); |
---|
43 | } |
---|
44 | |
---|
45 | main(int argc, char **argv) |
---|
46 | { |
---|
47 | int num_dirs = 0, flags = 0, mflag = 0; |
---|
48 | char **dir_list, *type = NULL, *athsys = NULL, *hosttype = NULL; |
---|
49 | char *auxconvention = NULL, *recsep = NULL, **ptr; |
---|
50 | char **path_list; |
---|
51 | int i, j, t; |
---|
52 | int failed, match = 0; |
---|
53 | |
---|
54 | progName = strrchr(argv[0], '/'); |
---|
55 | if (progName != NULL) |
---|
56 | progName++; |
---|
57 | else |
---|
58 | progName = argv[0]; |
---|
59 | |
---|
60 | if (argc == 1) |
---|
61 | usage(); |
---|
62 | |
---|
63 | if (argc) |
---|
64 | dir_list = malloc((argc + 1) * sizeof(char *)); |
---|
65 | |
---|
66 | if (dir_list == NULL) |
---|
67 | { |
---|
68 | fprintf(stderr, "%s: out of memory\n", progName); |
---|
69 | exit(1); |
---|
70 | } |
---|
71 | |
---|
72 | if (argv[1][0] != '-') |
---|
73 | { |
---|
74 | if (argc > 3) |
---|
75 | usage(); |
---|
76 | |
---|
77 | dir_list[num_dirs++] = argv[1]; |
---|
78 | |
---|
79 | if (argv[2]) |
---|
80 | { |
---|
81 | if (argv[2][0] == '-') |
---|
82 | usage(); |
---|
83 | |
---|
84 | type = argv[2]; |
---|
85 | } |
---|
86 | else |
---|
87 | type = "bin"; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | argv++; |
---|
92 | |
---|
93 | while (*argv) |
---|
94 | { |
---|
95 | if (**argv != '-' || (*argv)[2] != '\0') |
---|
96 | { |
---|
97 | fprintf(stderr, "%s: unknown option: %s\n", progName, *argv); |
---|
98 | usage(); |
---|
99 | } |
---|
100 | |
---|
101 | switch((*argv)[1]) |
---|
102 | { |
---|
103 | case 't': |
---|
104 | if (type) |
---|
105 | repeatflag(*argv); |
---|
106 | argv++; |
---|
107 | if (*argv == NULL) |
---|
108 | usage(); |
---|
109 | type = *argv++; |
---|
110 | break; |
---|
111 | |
---|
112 | case 'p': |
---|
113 | if (num_dirs != 0) |
---|
114 | repeatflag(*argv); |
---|
115 | |
---|
116 | argv++; |
---|
117 | if (*argv == NULL) |
---|
118 | usage(); |
---|
119 | while (*argv != NULL && **argv != '-') |
---|
120 | dir_list[num_dirs++] = *argv++; |
---|
121 | break; |
---|
122 | |
---|
123 | case 'e': |
---|
124 | if (flags & ATHDIR_SUPPRESSEDITORIALS) |
---|
125 | repeatflag(*argv); |
---|
126 | argv++; |
---|
127 | flags |= ATHDIR_SUPPRESSEDITORIALS; |
---|
128 | break; |
---|
129 | |
---|
130 | case 'c': |
---|
131 | if (flags & ATHDIR_SUPPRESSSEARCH) |
---|
132 | repeatflag(*argv); |
---|
133 | argv++; |
---|
134 | flags |= ATHDIR_SUPPRESSSEARCH; |
---|
135 | break; |
---|
136 | |
---|
137 | case 'l': |
---|
138 | if (flags & ATHDIR_LISTSEARCHDIRECTORIES) |
---|
139 | repeatflag(*argv); |
---|
140 | argv++; |
---|
141 | flags |= ATHDIR_LISTSEARCHDIRECTORIES; |
---|
142 | break; |
---|
143 | |
---|
144 | case 's': |
---|
145 | if (athsys) |
---|
146 | repeatflag(*argv); |
---|
147 | argv++; |
---|
148 | if (*argv == NULL) |
---|
149 | usage(); |
---|
150 | athsys = *argv++; |
---|
151 | break; |
---|
152 | |
---|
153 | case 'm': |
---|
154 | if (hosttype != NULL) |
---|
155 | repeatflag(*argv); |
---|
156 | argv++; |
---|
157 | if (*argv == NULL) |
---|
158 | usage(); |
---|
159 | hosttype = *argv++; |
---|
160 | break; |
---|
161 | |
---|
162 | case 'f': |
---|
163 | if (auxconvention) |
---|
164 | repeatflag(*argv); |
---|
165 | argv++; |
---|
166 | if (*argv == NULL) |
---|
167 | usage(); |
---|
168 | auxconvention = *argv++; |
---|
169 | break; |
---|
170 | |
---|
171 | case 'r': |
---|
172 | if (recsep != NULL) |
---|
173 | repeatflag(*argv); |
---|
174 | argv++; |
---|
175 | if (*argv == NULL) |
---|
176 | usage(); |
---|
177 | recsep = *argv++; |
---|
178 | break; |
---|
179 | |
---|
180 | case 'd': |
---|
181 | if (flags & ATHDIR_MACHINEDEPENDENT) |
---|
182 | repeatflag(*argv); |
---|
183 | argv++; |
---|
184 | flags |= ATHDIR_MACHINEDEPENDENT; |
---|
185 | break; |
---|
186 | |
---|
187 | case 'i': |
---|
188 | if (flags & ATHDIR_MACHINEINDEPENDENT) |
---|
189 | repeatflag(*argv); |
---|
190 | argv++; |
---|
191 | flags |= ATHDIR_MACHINEINDEPENDENT; |
---|
192 | break; |
---|
193 | |
---|
194 | default: |
---|
195 | fprintf(stderr, "%s: unknown option: %s\n", progName, *argv); |
---|
196 | usage(); |
---|
197 | break; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | if (!num_dirs) |
---|
203 | dir_list[num_dirs++] = NULL; |
---|
204 | |
---|
205 | /* Default record separator is a newline. */ |
---|
206 | if (!recsep) |
---|
207 | recsep = "\n"; |
---|
208 | |
---|
209 | for (i = 0; i < num_dirs; i++) |
---|
210 | { |
---|
211 | path_list = athdir_get_paths(dir_list[i], type, athsys, NULL, hosttype, |
---|
212 | auxconvention, flags); |
---|
213 | if (path_list != NULL) |
---|
214 | { |
---|
215 | for (ptr = path_list; *ptr != NULL; ptr++) |
---|
216 | { |
---|
217 | if (match == 1) |
---|
218 | fprintf(stdout, "%s", recsep); |
---|
219 | match = 1; |
---|
220 | fprintf(stdout, "%s", *ptr); |
---|
221 | } |
---|
222 | |
---|
223 | athdir_free_paths(path_list); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | if (match) |
---|
228 | fprintf(stdout, "\n"); |
---|
229 | |
---|
230 | #ifdef DEBUG |
---|
231 | fprintf(stdout, "%s ", progName); |
---|
232 | if (type != NULL) |
---|
233 | fprintf(stdout, "-t %s ", type); |
---|
234 | if (num_dirs) |
---|
235 | { |
---|
236 | fprintf(stdout, "-p "); |
---|
237 | for (i = 0; i < num_dirs; i++) |
---|
238 | fprintf(stdout, "%s ", dir_list[i]); |
---|
239 | } |
---|
240 | if (ATHDIR_SUPPRESSEDITORIALS & flags) |
---|
241 | fprintf(stdout, "-e "); |
---|
242 | if (ATHDIR_SUPPRESSSEARCH & flags) |
---|
243 | fprintf(stdout, "-c "); |
---|
244 | if (ATHDIR_LISTSEARCHDIRECTORIES & flags) |
---|
245 | fprintf(stdout, "-l "); |
---|
246 | fprintf(stdout, "\n"); |
---|
247 | #endif |
---|
248 | |
---|
249 | exit(match == 0); |
---|
250 | } |
---|