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