1 /* 2 * Copyright 1990 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 1980 Regents of the University of California. 8 * All rights reserved. The Berkeley software License Agreement 9 * specifies the terms and conditions for redistribution. 10 */ 11 12 #pragma ident "%Z%%M% %I% %E% SMI" 13 14 15 #include <sys/param.h> 16 #include <dirent.h> 17 #include <stdio.h> 18 #include <ctype.h> 19 20 static char *bindirs[] = { 21 "/etc", 22 "/sbin", 23 "/usr/bin", 24 "/usr/ccs/bin", 25 "/usr/ccs/lib", 26 "/usr/lang", 27 "/usr/lbin", 28 "/usr/lib", 29 "/usr/sbin", 30 "/usr/ucb", 31 "/usr/ucblib", 32 "/usr/ucbinclude", 33 "/usr/games", 34 "/usr/local", 35 "/usr/local/bin", 36 "/usr/new", 37 "/usr/old", 38 "/usr/hosts", 39 "/usr/include", 40 "/usr/etc", 41 0 42 }; 43 static char *mandirs[] = { 44 "/usr/man/man1", 45 "/usr/man/man1b", 46 "/usr/man/man1c", 47 "/usr/man/man1f", 48 "/usr/man/man1m", 49 "/usr/man/man1s", 50 "/usr/man/man2", 51 "/usr/man/man3", 52 "/usr/man/man3b", 53 "/usr/man/man3c", 54 "/usr/man/man3e", 55 "/usr/man/man3g", 56 "/usr/man/man3j", 57 "/usr/man/man3k", 58 "/usr/man/man3l", 59 "/usr/man/man3m", 60 "/usr/man/man3n", 61 "/usr/man/man3s", 62 "/usr/man/man3w", 63 "/usr/man/man3x", 64 "/usr/man/man3x11", 65 "/usr/man/man3xt", 66 "/usr/man/man4", 67 "/usr/man/man4b", 68 "/usr/man/man5", 69 "/usr/man/man6", 70 "/usr/man/man7", 71 "/usr/man/man7b", 72 "/usr/man/man8", 73 "/usr/man/man9e", 74 "/usr/man/man9f", 75 "/usr/man/man9s", 76 "/usr/man/manl", 77 "/usr/man/mann", 78 "/usr/man/mano", 79 0 80 }; 81 static char *srcdirs[] = { 82 "/usr/src/cmd", 83 "/usr/src/head", 84 "/usr/src/lib", 85 "/usr/src/lib/libc", 86 "/usr/src/lib/libc/port", 87 "/usr/src/lib/libc/port/gen", 88 "/usr/src/lib/libc/port/print", 89 "/usr/src/lib/libc/port/stdio", 90 "/usr/src/lib/libc/port/sys", 91 "/usr/src/lib/libc/sparc", 92 "/usr/src/lib/libc/sparc/gen", 93 "/usr/src/lib/libc/sparc/sys", 94 "/usr/src/ucbcmd", 95 "/usr/src/ucblib", 96 "/usr/src/ucbinclude", 97 "/usr/src/uts", 98 "/usr/src/uts/common", 99 "/usr/src/uts/sun", 100 "/usr/src/uts/sun4", 101 "/usr/src/uts/sun4c", 102 "/usr/src/uts/sparc", 103 "/usr/src/local", 104 "/usr/src/new", 105 "/usr/src/old", 106 0 107 }; 108 109 char sflag = 1; 110 char bflag = 1; 111 char mflag = 1; 112 char **Sflag; 113 int Scnt; 114 char **Bflag; 115 int Bcnt; 116 char **Mflag; 117 int Mcnt; 118 char uflag; 119 /* 120 * whereis name 121 * look for source, documentation and binaries 122 */ 123 main(argc, argv) 124 int argc; 125 char *argv[]; 126 { 127 128 argc--, argv++; 129 if (argc == 0) { 130 usage: 131 fprintf(stderr, "whereis [ -sbmu ] [ -SBM dir ... -f ] name...\n"); 132 exit(1); 133 } 134 do 135 if (argv[0][0] == '-') { 136 register char *cp = argv[0] + 1; 137 while (*cp) switch (*cp++) { 138 139 case 'f': 140 break; 141 142 case 'S': 143 getlist(&argc, &argv, &Sflag, &Scnt); 144 break; 145 146 case 'B': 147 getlist(&argc, &argv, &Bflag, &Bcnt); 148 break; 149 150 case 'M': 151 getlist(&argc, &argv, &Mflag, &Mcnt); 152 break; 153 154 case 's': 155 zerof(); 156 sflag++; 157 continue; 158 159 case 'u': 160 uflag++; 161 continue; 162 163 case 'b': 164 zerof(); 165 bflag++; 166 continue; 167 168 case 'm': 169 zerof(); 170 mflag++; 171 continue; 172 173 default: 174 goto usage; 175 } 176 argv++; 177 } else 178 lookup(*argv++); 179 while (--argc > 0); 180 exit(0); 181 /* NOTREACHED */ 182 } 183 184 getlist(argcp, argvp, flagp, cntp) 185 char ***argvp; 186 int *argcp; 187 char ***flagp; 188 int *cntp; 189 { 190 191 (*argvp)++; 192 *flagp = *argvp; 193 *cntp = 0; 194 for ((*argcp)--; *argcp > 0 && (*argvp)[0][0] != '-'; (*argcp)--) 195 (*cntp)++, (*argvp)++; 196 (*argcp)++; 197 (*argvp)--; 198 } 199 200 201 zerof() 202 { 203 204 if (sflag && bflag && mflag) 205 sflag = bflag = mflag = 0; 206 } 207 int count; 208 int print; 209 210 211 lookup(cp) 212 register char *cp; 213 { 214 register char *dp; 215 216 for (dp = cp; *dp; dp++) 217 continue; 218 for (; dp > cp; dp--) { 219 if (*dp == '.') { 220 *dp = 0; 221 break; 222 } 223 } 224 for (dp = cp; *dp; dp++) 225 if (*dp == '/') 226 cp = dp + 1; 227 if (uflag) { 228 print = 0; 229 count = 0; 230 } else 231 print = 1; 232 again: 233 if (print) 234 printf("%s:", cp); 235 if (sflag) { 236 looksrc(cp); 237 if (uflag && print == 0 && count != 1) { 238 print = 1; 239 goto again; 240 } 241 } 242 count = 0; 243 if (bflag) { 244 lookbin(cp); 245 if (uflag && print == 0 && count != 1) { 246 print = 1; 247 goto again; 248 } 249 } 250 count = 0; 251 if (mflag) { 252 lookman(cp); 253 if (uflag && print == 0 && count != 1) { 254 print = 1; 255 goto again; 256 } 257 } 258 if (print) 259 printf("\n"); 260 } 261 262 looksrc(cp) 263 char *cp; 264 { 265 if (Sflag == 0) { 266 find(srcdirs, cp); 267 } else 268 findv(Sflag, Scnt, cp); 269 } 270 271 lookbin(cp) 272 char *cp; 273 { 274 if (Bflag == 0) 275 find(bindirs, cp); 276 else 277 findv(Bflag, Bcnt, cp); 278 } 279 280 lookman(cp) 281 char *cp; 282 { 283 if (Mflag == 0) { 284 find(mandirs, cp); 285 } else 286 findv(Mflag, Mcnt, cp); 287 } 288 289 findv(dirv, dirc, cp) 290 char **dirv; 291 int dirc; 292 char *cp; 293 { 294 295 while (dirc > 0) 296 findin(*dirv++, cp), dirc--; 297 } 298 299 find(dirs, cp) 300 char **dirs; 301 char *cp; 302 { 303 304 while (*dirs) 305 findin(*dirs++, cp); 306 } 307 308 findin(dir, cp) 309 char *dir, *cp; 310 { 311 DIR *dirp; 312 struct dirent *dp; 313 314 dirp = opendir(dir); 315 if (dirp == NULL) 316 return; 317 while ((dp = readdir(dirp)) != NULL) { 318 if (itsit(cp, dp->d_name)) { 319 count++; 320 if (print) 321 printf(" %s/%s", dir, dp->d_name); 322 } 323 } 324 closedir(dirp); 325 } 326 327 itsit(cp, dp) 328 register char *cp, *dp; 329 { 330 register int i = strlen(dp); 331 332 if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp+2)) 333 return (1); 334 while (*cp && *dp && *cp == *dp) 335 cp++, dp++, i--; 336 if (*cp == 0 && *dp == 0) 337 return (1); 338 while (isdigit(*dp)) 339 dp++; 340 if (*cp == 0 && *dp++ == '.') { 341 --i; 342 while (i > 0 && *dp) 343 if (--i, *dp++ == '.') 344 return (*dp++ == 'C' && *dp++ == 0); 345 return (1); 346 } 347 return (0); 348 } 349