home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 125.img / PRO-C4.ZIP / BENCH1.ZIP / BENCH / SRCHPATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-28  |  2.2 KB  |  116 lines

  1. /* ==( bench/srchpath.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   Nig  19-Dec-89                        */
  9. /* Modified  Geo   1-Feb-90  See comments below    */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13. /*
  14.  *  Modifications
  15.  *
  16. */
  17.  
  18. # include <stdio.h>
  19. # include <bench.h>
  20.  
  21.  
  22. /*
  23.  * GEO, I am sure that this isn't satisfactory in some
  24.  * cases, perhaps you can have a look at it for me. Nig
  25. */
  26. # ifdef MSDOS
  27. # define PATHSEPERATOR   ";"
  28. # else
  29. # define PATHSEPERATOR   ":"
  30. # endif
  31.  
  32.  
  33. /* 
  34.  * Scan the PATH as defined by the environment, and
  35.  * return the full name with path if found on the 
  36.  * path, or the file name without path if not found
  37.  * on the path.
  38. */
  39. void search_path(fname, srchpath, ext)
  40. char *fname, *srchpath, *ext;
  41. {
  42.     char path[256];
  43.     char envpath[256];
  44.     char *str;
  45.     char *token;
  46.  
  47.     strcpy(srchpath, fname);
  48.     stripext(srchpath);
  49.  
  50.     str = getenv("PATH");
  51.     if (str != NULL)
  52.         strncpy(envpath, str, sizeof(envpath));
  53.  
  54.     token = strtok(envpath, PATHSEPERATOR);
  55.  
  56.     while (token != NULL)
  57.     {
  58.         strcpy(path, token);
  59.         strcat(path, DIRSLASHES);
  60.         strcat(path, fname);
  61.         strcat(path, ext);
  62.  
  63.         if (access(path, 0) == 0)
  64.         {
  65.             /* Pathname exists - not checking R/W for now */
  66.             stripext(path);
  67.             strcpy(srchpath, path);
  68.             break;
  69.         }
  70.         token = strtok(NULL, PATHSEPERATOR);
  71.     }
  72. }
  73.  
  74.  
  75. void stripext(str)
  76. char *str;
  77. {
  78.     char *ptr;
  79.     int done = 0;
  80.  
  81.     for(ptr = str + strlen(str) - 1; ptr > str; ptr--)
  82.     {
  83.         if (*ptr == '/' || *ptr == '\\')
  84.             break;
  85.         if (*ptr == '.' && *(ptr - 1) != '/' && *(ptr - 1) != '\\')
  86.         {
  87.             *ptr = '\0';
  88.             break;
  89.         }
  90.     }
  91. }
  92.  
  93. void addslash(str)
  94. char *str;
  95. {
  96.     /* Doesn't check empty strings */
  97.     str += strlen(str) - 1;
  98.  
  99.     if (*str == DIRSLASH)
  100.         return;
  101.  
  102.     *++str = DIRSLASH;
  103.     *++str = '\0';
  104. }
  105.  
  106. void eatslash(str)
  107. char *str;
  108. {
  109.     str += strlen(str) - 1;
  110.  
  111.     if (*str != DIRSLASH)
  112.         return;
  113.  
  114.     *str = '\0';
  115. }
  116.