home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / LIBS / CMDLINE / SOURCE / SRCHPATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-25  |  1.3 KB  |  63 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "cmdline.h"
  4. /*
  5.  * Pull the next path off the path search list
  6.  */
  7. static char *parsepath(char *path, char *buffer)
  8. {
  9.   char *pos = path;
  10.  
  11.   /* Quit if hit a ';' */
  12.   while (*pos) {
  13.         if (*pos == ';') {
  14.             pos++;
  15.             break;
  16.     }
  17.         *buffer++ = *pos++;
  18.     }
  19.   *buffer = 0;
  20.  
  21.   /* Return a null pointer if no more data */
  22.   if (*pos)
  23.       return(pos);
  24.  
  25.   return(0);
  26. }
  27. /*
  28.  * For each library:
  29.  * Search local directory and all directories in the search path
  30.  *  until it is found or run out of directories
  31.  */
  32. FILE *SearchPath(char *string, char *searchpath, char *mode)
  33. {
  34.         FILE *in;
  35.         char *newpath = searchpath;
  36.  
  37.         /* Search local path */
  38.     in = fopen((char *) string,mode);
  39.         if (in) {
  40.             return(in);
  41.         }
  42.         else {
  43.             /* If no path specified we search along the search path */
  44.             if (!strchr(string,'\\')) {
  45.               char buffer[200];
  46.                 while (newpath) {
  47.                     /* Create a file name along this path */
  48.                   newpath = parsepath(newpath,buffer);
  49.                     if (buffer[strlen(buffer)-1] != '\\')
  50.                         strcat(buffer,"\\");
  51.                   strcat(buffer, (char *)string);
  52.  
  53.                     /* Check this path */
  54.                     in = fopen(buffer, mode);
  55.                     if (in) {
  56.                         strcpy(string,buffer);
  57.                         return(in);
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     return(0);
  63. }