[11253] | 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 | |
---|
[13589] | 16 | static const char rcsid[] = "$Id: athdir.c,v 1.4 1999-09-15 23:57:14 danw Exp $"; |
---|
[11253] | 17 | |
---|
[7756] | 18 | #include <stdio.h> |
---|
| 19 | #include <stdlib.h> |
---|
| 20 | #include <string.h> |
---|
[11253] | 21 | #include <sys/param.h> |
---|
[7756] | 22 | #include <sys/types.h> |
---|
| 23 | #include <sys/stat.h> |
---|
[11253] | 24 | #include <athdir.h> |
---|
[7756] | 25 | |
---|
| 26 | char *progName; |
---|
| 27 | |
---|
[13589] | 28 | void usage(void); |
---|
| 29 | void repeatflag(char *option); |
---|
[7756] | 30 | |
---|
[13589] | 31 | int main(int argc, char **argv) |
---|
[7756] | 32 | { |
---|
[13589] | 33 | int num_dirs = 0, flags = 0; |
---|
[11253] | 34 | char **dir_list, *type = NULL, *athsys = NULL, *hosttype = NULL; |
---|
| 35 | char *auxconvention = NULL, *recsep = NULL, **ptr; |
---|
| 36 | char **path_list; |
---|
[13589] | 37 | int i; |
---|
| 38 | int match = 0; |
---|
[7756] | 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) |
---|
[11253] | 50 | dir_list = malloc((argc + 1) * sizeof(char *)); |
---|
[7756] | 51 | |
---|
[11253] | 52 | if (dir_list == NULL) |
---|
| 53 | { |
---|
| 54 | fprintf(stderr, "%s: out of memory\n", progName); |
---|
| 55 | exit(1); |
---|
| 56 | } |
---|
[7756] | 57 | |
---|
| 58 | if (argv[1][0] != '-') |
---|
| 59 | { |
---|
| 60 | if (argc > 3) |
---|
| 61 | usage(); |
---|
| 62 | |
---|
[11253] | 63 | dir_list[num_dirs++] = argv[1]; |
---|
[7756] | 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': |
---|
[11253] | 90 | if (type) |
---|
| 91 | repeatflag(*argv); |
---|
[7756] | 92 | argv++; |
---|
| 93 | if (*argv == NULL) |
---|
| 94 | usage(); |
---|
| 95 | type = *argv++; |
---|
| 96 | break; |
---|
| 97 | |
---|
| 98 | case 'p': |
---|
[11253] | 99 | if (num_dirs != 0) |
---|
| 100 | repeatflag(*argv); |
---|
[7756] | 101 | |
---|
| 102 | argv++; |
---|
| 103 | if (*argv == NULL) |
---|
| 104 | usage(); |
---|
| 105 | while (*argv != NULL && **argv != '-') |
---|
[11253] | 106 | dir_list[num_dirs++] = *argv++; |
---|
[7756] | 107 | break; |
---|
| 108 | |
---|
| 109 | case 'e': |
---|
[11253] | 110 | if (flags & ATHDIR_SUPPRESSEDITORIALS) |
---|
| 111 | repeatflag(*argv); |
---|
[7756] | 112 | argv++; |
---|
[11253] | 113 | flags |= ATHDIR_SUPPRESSEDITORIALS; |
---|
[7756] | 114 | break; |
---|
| 115 | |
---|
| 116 | case 'c': |
---|
[11253] | 117 | if (flags & ATHDIR_SUPPRESSSEARCH) |
---|
| 118 | repeatflag(*argv); |
---|
[7756] | 119 | argv++; |
---|
[11253] | 120 | flags |= ATHDIR_SUPPRESSSEARCH; |
---|
[7756] | 121 | break; |
---|
| 122 | |
---|
| 123 | case 'l': |
---|
[11253] | 124 | if (flags & ATHDIR_LISTSEARCHDIRECTORIES) |
---|
| 125 | repeatflag(*argv); |
---|
[7756] | 126 | argv++; |
---|
[11253] | 127 | flags |= ATHDIR_LISTSEARCHDIRECTORIES; |
---|
[7756] | 128 | break; |
---|
| 129 | |
---|
| 130 | case 's': |
---|
[11253] | 131 | if (athsys) |
---|
| 132 | repeatflag(*argv); |
---|
[7756] | 133 | argv++; |
---|
| 134 | if (*argv == NULL) |
---|
| 135 | usage(); |
---|
| 136 | athsys = *argv++; |
---|
| 137 | break; |
---|
| 138 | |
---|
| 139 | case 'm': |
---|
[11253] | 140 | if (hosttype != NULL) |
---|
| 141 | repeatflag(*argv); |
---|
[7756] | 142 | argv++; |
---|
| 143 | if (*argv == NULL) |
---|
| 144 | usage(); |
---|
| 145 | hosttype = *argv++; |
---|
| 146 | break; |
---|
| 147 | |
---|
| 148 | case 'f': |
---|
[11253] | 149 | if (auxconvention) |
---|
| 150 | repeatflag(*argv); |
---|
[7756] | 151 | argv++; |
---|
| 152 | if (*argv == NULL) |
---|
| 153 | usage(); |
---|
[11253] | 154 | auxconvention = *argv++; |
---|
[7756] | 155 | break; |
---|
| 156 | |
---|
| 157 | case 'r': |
---|
| 158 | if (recsep != NULL) |
---|
[11253] | 159 | repeatflag(*argv); |
---|
[7756] | 160 | argv++; |
---|
| 161 | if (*argv == NULL) |
---|
| 162 | usage(); |
---|
| 163 | recsep = *argv++; |
---|
| 164 | break; |
---|
| 165 | |
---|
| 166 | case 'd': |
---|
[11253] | 167 | if (flags & ATHDIR_MACHINEDEPENDENT) |
---|
| 168 | repeatflag(*argv); |
---|
[7756] | 169 | argv++; |
---|
[11253] | 170 | flags |= ATHDIR_MACHINEDEPENDENT; |
---|
[7756] | 171 | break; |
---|
| 172 | |
---|
| 173 | case 'i': |
---|
[11253] | 174 | if (flags & ATHDIR_MACHINEINDEPENDENT) |
---|
| 175 | repeatflag(*argv); |
---|
[7756] | 176 | argv++; |
---|
[11253] | 177 | flags |= ATHDIR_MACHINEINDEPENDENT; |
---|
[7756] | 178 | break; |
---|
| 179 | |
---|
| 180 | default: |
---|
| 181 | fprintf(stderr, "%s: unknown option: %s\n", progName, *argv); |
---|
| 182 | usage(); |
---|
| 183 | break; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[11253] | 188 | if (!num_dirs) |
---|
| 189 | dir_list[num_dirs++] = NULL; |
---|
[7756] | 190 | |
---|
[11253] | 191 | /* Default record separator is a newline. */ |
---|
[7756] | 192 | if (!recsep) |
---|
| 193 | recsep = "\n"; |
---|
| 194 | |
---|
[11253] | 195 | for (i = 0; i < num_dirs; i++) |
---|
[7756] | 196 | { |
---|
[11253] | 197 | path_list = athdir_get_paths(dir_list[i], type, athsys, NULL, hosttype, |
---|
| 198 | auxconvention, flags); |
---|
| 199 | if (path_list != NULL) |
---|
[7756] | 200 | { |
---|
[11253] | 201 | for (ptr = path_list; *ptr != NULL; ptr++) |
---|
[7756] | 202 | { |
---|
[11253] | 203 | if (match == 1) |
---|
| 204 | fprintf(stdout, "%s", recsep); |
---|
| 205 | match = 1; |
---|
| 206 | fprintf(stdout, "%s", *ptr); |
---|
[7756] | 207 | } |
---|
[11253] | 208 | |
---|
| 209 | athdir_free_paths(path_list); |
---|
[7756] | 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
[11253] | 213 | if (match) |
---|
[7756] | 214 | fprintf(stdout, "\n"); |
---|
| 215 | |
---|
| 216 | #ifdef DEBUG |
---|
| 217 | fprintf(stdout, "%s ", progName); |
---|
| 218 | if (type != NULL) |
---|
| 219 | fprintf(stdout, "-t %s ", type); |
---|
[11253] | 220 | if (num_dirs) |
---|
[7756] | 221 | { |
---|
| 222 | fprintf(stdout, "-p "); |
---|
[11253] | 223 | for (i = 0; i < num_dirs; i++) |
---|
| 224 | fprintf(stdout, "%s ", dir_list[i]); |
---|
[7756] | 225 | } |
---|
[11253] | 226 | if (ATHDIR_SUPPRESSEDITORIALS & flags) |
---|
[7756] | 227 | fprintf(stdout, "-e "); |
---|
[11253] | 228 | if (ATHDIR_SUPPRESSSEARCH & flags) |
---|
[7756] | 229 | fprintf(stdout, "-c "); |
---|
[11253] | 230 | if (ATHDIR_LISTSEARCHDIRECTORIES & flags) |
---|
[7756] | 231 | fprintf(stdout, "-l "); |
---|
| 232 | fprintf(stdout, "\n"); |
---|
| 233 | #endif |
---|
| 234 | |
---|
[13589] | 235 | return match == 0; |
---|
[7756] | 236 | } |
---|
[13589] | 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 | } |
---|