[9] | 1 | /* which - search paths for executable */
|
---|
| 2 |
|
---|
| 3 | #define DELIMITER ':'
|
---|
| 4 |
|
---|
| 5 | #include <sys/types.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include <string.h>
|
---|
| 8 | #include <unistd.h>
|
---|
| 9 | #include <stdio.h>
|
---|
| 10 |
|
---|
| 11 | _PROTOTYPE(int main, (int argc, char **argv));
|
---|
| 12 |
|
---|
| 13 | int main(ac, av)
|
---|
| 14 | int ac;
|
---|
| 15 | char **av;
|
---|
| 16 | {
|
---|
| 17 | char *path, *cp;
|
---|
| 18 | char buf[400];
|
---|
| 19 | char prog[400];
|
---|
| 20 | char patbuf[512];
|
---|
| 21 | int quit, none;
|
---|
| 22 | int excode = 0;
|
---|
| 23 |
|
---|
| 24 | if (ac < 2) {
|
---|
| 25 | fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
|
---|
| 26 | exit(1);
|
---|
| 27 | }
|
---|
| 28 | av[ac] = 0;
|
---|
| 29 | for (av++; *av; av++) {
|
---|
| 30 |
|
---|
| 31 | quit = 0;
|
---|
| 32 | none = 1;
|
---|
| 33 | if ((path = getenv("PATH")) == NULL) {
|
---|
| 34 | fprintf(stderr, "Null path.\n");
|
---|
| 35 | exit(0);
|
---|
| 36 | }
|
---|
| 37 | strcpy(patbuf, path);
|
---|
| 38 | path = patbuf;
|
---|
| 39 | cp = path;
|
---|
| 40 |
|
---|
| 41 | while (1) {
|
---|
| 42 | cp = strchr(path, DELIMITER);
|
---|
| 43 | if (cp == NULL)
|
---|
| 44 | quit++;
|
---|
| 45 | else
|
---|
| 46 | *cp = '\0';
|
---|
| 47 |
|
---|
| 48 | if (strcmp(path, "") == 0 && quit == 0) {
|
---|
| 49 | sprintf(buf, "%s./%s", path, *av);
|
---|
| 50 | } else
|
---|
| 51 | sprintf(buf, "%s/%s", path, *av);
|
---|
| 52 |
|
---|
| 53 | /* Fprintf(stderr,"Trying %s, path %s\n",buf,path); */
|
---|
| 54 |
|
---|
| 55 | path = ++cp;
|
---|
| 56 |
|
---|
| 57 | if (access(buf, 1) == 0) {
|
---|
| 58 | printf("%s\n", buf);
|
---|
| 59 | none = 0;
|
---|
| 60 | }
|
---|
| 61 | sprintf(prog, "%s.%s", buf, "prg");
|
---|
| 62 | if (access(prog, 1) == 0) {
|
---|
| 63 | printf("%s\n", prog);
|
---|
| 64 | none = 0;
|
---|
| 65 | }
|
---|
| 66 | sprintf(prog, "%s.%s", buf, "ttp");
|
---|
| 67 | if (access(prog, 1) == 0) {
|
---|
| 68 | printf("%s\n", prog);
|
---|
| 69 | none = 0;
|
---|
| 70 | }
|
---|
| 71 | sprintf(prog, "%s.%s", buf, "tos");
|
---|
| 72 | if (access(prog, 1) == 0) {
|
---|
| 73 | printf("%s\n", prog);
|
---|
| 74 | none = 0;
|
---|
| 75 | }
|
---|
| 76 | if (quit) {
|
---|
| 77 | if (none) {
|
---|
| 78 | fprintf(stderr, "No %s in %s\n", *av, getenv("PATH"));
|
---|
| 79 | excode = 1;
|
---|
| 80 | }
|
---|
| 81 | break;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return(excode);
|
---|
| 86 | }
|
---|