which.c

Go to the documentation of this file.
00001 /* which - search paths for executable */
00002 
00003 #define DELIMITER ':'
00004 
00005 #include <sys/types.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <unistd.h>
00009 #include <stdio.h>
00010 
00011 _PROTOTYPE(int main, (int argc, char **argv));
00012 
00013 int main(ac, av)
00014 int ac;
00015 char **av;
00016 {
00017   char *path, *cp;
00018   char buf[400];
00019   char prog[400];
00020   char patbuf[512];
00021   int quit, none;
00022   int excode = 0;
00023 
00024   if (ac < 2) {
00025         fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
00026         exit(1);
00027   }
00028   av[ac] = 0;
00029   for (av++; *av; av++) {
00030 
00031         quit = 0;
00032         none = 1;
00033         if ((path = getenv("PATH")) == NULL) {
00034                 fprintf(stderr, "Null path.\n");
00035                 exit(0);
00036         }
00037         strcpy(patbuf, path);
00038         path = patbuf;
00039         cp = path;
00040 
00041         while (1) {
00042                 cp = strchr(path, DELIMITER);
00043                 if (cp == NULL)
00044                         quit++;
00045                 else
00046                         *cp = '\0';
00047 
00048                 if (strcmp(path, "") == 0 && quit == 0) {
00049                         sprintf(buf, "%s./%s", path, *av);
00050                 } else
00051                         sprintf(buf, "%s/%s", path, *av);
00052 
00053                 /* Fprintf(stderr,"Trying %s, path %s\n",buf,path); */
00054 
00055                 path = ++cp;
00056 
00057                 if (access(buf, 1) == 0) {
00058                         printf("%s\n", buf);
00059                         none = 0;
00060                 }
00061                 sprintf(prog, "%s.%s", buf, "prg");
00062                 if (access(prog, 1) == 0) {
00063                         printf("%s\n", prog);
00064                         none = 0;
00065                 }
00066                 sprintf(prog, "%s.%s", buf, "ttp");
00067                 if (access(prog, 1) == 0) {
00068                         printf("%s\n", prog);
00069                         none = 0;
00070                 }
00071                 sprintf(prog, "%s.%s", buf, "tos");
00072                 if (access(prog, 1) == 0) {
00073                         printf("%s\n", prog);
00074                         none = 0;
00075                 }
00076                 if (quit) {
00077                         if (none) {
00078                                 fprintf(stderr, "No %s in %s\n", *av, getenv("PATH"));
00079                                 excode = 1;
00080                         }
00081                         break;
00082                 }
00083         }
00084   }
00085   return(excode);
00086 }

Generated on Fri Apr 14 22:57:13 2006 for minix by  doxygen 1.4.6