home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / lib / dlibssrc / findfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-05  |  1.2 KB  |  37 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3.  
  4. char *findfile(afn, ext)
  5. register char *afn, *ext;
  6. /*
  7.  *    Return full file spec for <afn> if found. If <afn> has no extension,
  8.  *    extensions from <ext> are tried until a match is found, or the list
  9.  *    ends.  <ext> is a list of extensions separated by '\0' characters
  10.  *    and ending with an additional '\0', ie. "ttp\0tos\0ttp\0" (note that
  11.  *    the final null is by the compiler to the string constant.  If <afn>
  12.  *    alerady has an extension, <ext> is not used.  If no matching files
  13.  *    are found, NULL is returned.  The pointer returned when a match is
  14.  *    found points to a buffer which is internal to fullpath().  If you
  15.  *    want to save the value returned, you must make a copy before the
  16.  *    buffer is overwritten by subsequent calls.
  17.  */
  18. {
  19.     char *fullpath(), *strrchr(), *strchr(), *strcpy();
  20.     register char *p;
  21.  
  22.     afn = fullpath(NULL, afn);
  23.     if((p = strrchr(afn, '\\')) &&
  24.        (p = strchr(p, '.')))        /* .EXT specified */
  25.         return(access(afn, _ACCr) ? afn : NULL);
  26.     p = strrchr(afn, '\0');
  27.     *p++ = '.';
  28.     do {                        /* try list .EXT's */
  29.         strcpy(p, ext);
  30.         if(access(afn, _ACCr))
  31.             return(afn);
  32.         while(*ext++)
  33.             ;
  34.     } while(*ext);
  35.     return(NULL);
  36. }
  37.