home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume03 / which < prev    next >
Encoding:
Text File  |  1991-08-27  |  2.7 KB  |  139 lines

  1. Path: uunet!lll-winken!lll-tis!ames!necntc!ncoast!allbery
  2. From: mjr@welchsun2.UUCP (Marcus J. Ranum)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i024: fast 'which(ucb)' command
  5. Message-ID: <8805242335.AA03648@welchsun2.sun.com>
  6. Date: 24 May 88 23:35:12 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: mjr@welchsun2.UUCP (Marcus J. Ranum)
  9. Lines: 126
  10. Approved: allbery@ncoast.UUCP
  11.  
  12. comp.sources.misc: Volume 3, Issue 24
  13. Submitted-By: "Marcus J. Ranum" <mjr@welchsun2.UUCP>
  14. Archive-Name: which
  15.  
  16.     A really quickie version of the 'which(ucb)' command. No excuses
  17. for the looks, but it runs and is (as far as I can tell) "compatible"
  18. and a lot faster than having to invoke the C-shell. For SYSV compile it
  19. with -DSYSV - I think it should work.
  20.  
  21. uunet!mimsy!aplcen!osiris!welchvax!mjr
  22. mjr@jhuigf.BITNET
  23.  
  24. -------cut------cut--------cut--------cut--------cut--------cut-------
  25. #ifndef lint
  26. static char *RCSid="$Header: which.c,v 1.2 88/05/24 19:29:53 mjr Exp $";
  27. #endif
  28.  
  29.  /*
  30.  *    Marcus Ranum, December 10, 1987 - a really fast and dirty hack-
  31.  *    VERY tired of waiting for that idiot C shell script.
  32.  *    actually this code is hacked from several of my other programs,
  33.  *    hence the incoherence.
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/param.h>
  40.  
  41. char    *path = 0;
  42.  
  43. static void
  44. prpath(str)
  45. char    *str;
  46. {
  47.     char    *cp    = str;
  48.     while(cp && *cp) {
  49.         if(*cp == ':')
  50.             putchar(' ');
  51.         else
  52.             putchar(*cp);
  53.         cp++;
  54.     }
  55.     putchar('\n');
  56. }
  57.  
  58. main(ac,av)
  59. int    ac;
  60. char    *av[];
  61. {
  62.     int    ret;
  63.     extern    char    *getenv();
  64.  
  65.     if((path = getenv("PATH")) == NULL) {
  66.         (void)fprintf(stderr,"no PATH in environment\n");
  67.         exit(1);
  68.     }
  69.  
  70.  
  71.     for(ret =1; ret < ac; ret++){
  72.         char    *todo, *xpath();
  73.  
  74.         if(!(todo = xpath(av[ret]))) {
  75.             (void)printf("no \"%s\" in ",av[ret]);
  76.             prpath(path);
  77.         } else {
  78.             puts(todo);
  79.         }
  80.     }
  81.     exit(0);
  82. }
  83.  
  84. static char    *
  85. xpath(exe)
  86. char    *exe;
  87. {
  88.     char    *ptr, *doxpath();
  89. #ifndef SYSV
  90.     extern    char    *index();
  91. #else
  92.     extern    char    *strchr();
  93. #endif
  94.  
  95.     /* read the path and search for the first thing that matches */
  96.     /* the name of the program */
  97.  
  98.     /* first check if absolute or relative path */
  99. #ifndef SYSV
  100.     if(exe[0] == '/' || index(exe, '/'))
  101. #else
  102.     if(exe[0] == '/' || strchr(exe, '/'))
  103. #endif
  104.         return(doxpath(exe));
  105.  
  106.  
  107.     ptr = path;
  108.  
  109.     /* somewhat gnarly */
  110.     while(*ptr) {
  111.         char    pbuf[MAXPATHLEN];    /* thing to build path in */
  112.         char    *p = pbuf, *p2 = exe;
  113.  
  114.         while(*ptr && *ptr != ':')
  115.             *p++ = *ptr++;
  116.         *p++ = '/';
  117.         while(*p2)
  118.             *p++ = *p2++;
  119.         *p++ = '\0';
  120.         if (p2 = doxpath(pbuf))
  121.             return(p2);
  122.         if(*ptr == ':')
  123.             ptr++;
  124.     } 
  125.     return((char *)NULL);
  126. }
  127.  
  128. static char    *
  129. doxpath(path)
  130. char    *path;
  131. {
  132.     struct stat    sbuf;
  133.  
  134.     if (!stat(path, &sbuf))
  135.         if ((sbuf.st_mode & S_IFMT) == S_IFREG && (sbuf.st_mode & ~S_IFMT & S_IEXEC))
  136.             return(path);
  137.     return((char *) 0);
  138. }
  139.