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