home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / utils / which.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  1.7 KB  |  76 lines

  1. 27-Aug-85 19:17:33-MDT,1774;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Tue 27 Aug 85 19:17:29-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a001107; 27 Aug 85 20:58 EDT
  5. From: "Larry J. Barello" <larry@tikal.uucp>
  6. Newsgroups: net.sources
  7. Subject: Son of 'which'
  8. Message-ID: <225@tikal.UUCP>
  9. Date: 26 Aug 85 14:40:19 GMT
  10. To:       unix-sources@BRL-TGR.ARPA
  11.  
  12. A week or so I posted a quick C version of the UCB shell script
  13. "which".  It takes as arguments command names and searches your path
  14. for instances of them.  The version I posted had some cute bugs: it
  15. didn't work if you didn't have a path or if there were a null
  16. component in the path (Thanks to Tom Truscott).  To the folks with v8
  17. shell: wish I had it, sounds like it make a lot of utilities, like
  18. this one, useless.
  19.  
  20. Here is a fancified version of the original one.
  21.  
  22.     ..!uw-beaver!teltone!larry
  23.  
  24. -------- cut here (duh) -------
  25.  
  26. #include <stdio.h>
  27.  
  28. char *getenv();
  29. char *index();
  30.  
  31. int
  32. main(ac,av)
  33. char **av;
  34. {
  35.     char *origpath, *path, *cp;
  36.     char buf[200];
  37.     char patbuf[512];
  38.     int quit, found;
  39.  
  40.     if (ac < 2) {
  41.     fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
  42.     exit(1);
  43.     }
  44.     if ((origpath = getenv("PATH")) == 0)
  45.     origpath = ".";
  46.  
  47.     av[ac] = 0;
  48.     for(av++ ; *av; av++) {
  49.  
  50.     strcpy(patbuf, origpath);
  51.     cp = path = patbuf;
  52.     quit = found = 0;
  53.  
  54.     while(!quit) {
  55.         cp = index(path, ':');
  56.         if (cp == NULL) 
  57.         quit++;
  58.         else
  59.         *cp = '\0';
  60.  
  61.         sprintf(buf, "%s/%s", (*path ? path:"."), *av);
  62.         path = ++cp;
  63.  
  64.         if (access(buf, 1) == 0) {
  65.         printf("%s\n", buf);
  66.         found++;
  67.         }
  68.     }
  69.     if (!found) 
  70.         printf("No %s in %s\n", *av, origpath);
  71.     }
  72.     exit(0);
  73. }
  74.  
  75. ------------------
  76.