home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / SEARCHP.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  8.7 KB  |  254 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - searchp.cas
  3.  *
  4.  * function(s)
  5.  *        CopyUpr      - copy string and convert to upper-case
  6.  *        CheckFile    - build absolute pathname and check existence
  7.  *        __searchpath - return an absolute DOS path for the given file name.
  8.  *        searchpath   - searches the DOS path
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*[]------------------------------------------------------------[]*/
  12. /*|                                                              |*/
  13. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  14. /*|                                                              |*/
  15. /*|                                                              |*/
  16. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  17. /*|     All Rights Reserved.                                     |*/
  18. /*|                                                              |*/
  19. /*[]------------------------------------------------------------[]*/
  20.  
  21. #pragma inline
  22. #include <asmrules.h>
  23. #include <dir.h>
  24. #include <_dir.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27.  
  28. static  char    PathFile[MAXPATH];
  29. static  char    drive[MAXDRIVE+1];
  30. static  char    dir[MAXDIR+1];
  31. static  char    fname[MAXFILE+1];
  32. static  char    ext[MAXEXT+1];
  33.  
  34. /*-----------------------------------------------------------------------*
  35.  
  36. Name            CopyUpr - copy string and convert to upper-case
  37.  
  38. Usage           void * pascal near CopyUpr(char *dst, char *src)
  39.  
  40. Description     Copies a string to another, converting all lowercase
  41.                 characters to upper case.
  42.  
  43. Return value    Returns the address of the terminating null byte in "dst".
  44.  
  45. *------------------------------------------------------------------------*/
  46. static void * pascal near CopyUpr(char *dst, char *src)
  47. {
  48.         pushDS_
  49. asm     LDS_    si, src
  50. asm     LES_    di, dst
  51. asm     cld
  52. #if !LDATA
  53. asm    push    ds
  54. asm    pop    es
  55. #endif
  56. Copying:
  57. asm     lodsb
  58. asm     cmp     al, 'a'
  59. asm     jb      Converted
  60. asm     cmp     al, 'z'
  61. asm     ja      Converted
  62. asm     sub     al, 'a' - 'A'
  63. Converted:
  64. asm     stosb
  65. asm     or      al, al
  66. asm     jnz     Copying
  67. asm     dec     di
  68. asm    xchg    ax,di
  69.         popDS_
  70. #pragma warn -sus
  71.     return (void _es *) _AX;
  72. #pragma warn .sus
  73. }
  74.  
  75.  
  76. /*-----------------------------------------------------------------------*
  77.  
  78. Name            CheckFile - build absolute pathname and check existence
  79.  
  80. Usage           static int pascal near CheckFile(char *pathP, char *driveP,
  81.                                                  char *dirP, char *nameP,
  82.                                                  char *extP, int mode);
  83.  
  84. Description     Builds an absolute pathname with the given components,
  85.                 and then checks for the given file existency.
  86.  
  87. Return value    Returns a non-zero if the given file exists.
  88.  
  89. *------------------------------------------------------------------------*/
  90. static int pascal near CheckFile(char *pathP, char *driveP, char *dirP,
  91.                                  char *nameP, char *extP, int mode)
  92. {
  93.         register char   *bufP;
  94.         struct  ffblk   ffbuf;
  95.  
  96.         bufP = pathP;
  97.         if (*driveP == '\0')
  98.                 *driveP = 'A' + getdisk();
  99.         else
  100.                 *driveP &= ~0x20;
  101.         *bufP++ = *driveP;
  102.         *bufP++ = ':';
  103.  
  104.         if (*dirP != '\\' && *dirP != '/') {
  105.                 *bufP++ = '\\';
  106.                 getcurdir(*driveP - '@', bufP);
  107.                 if(*bufP) {
  108.                     bufP = CopyUpr(bufP, bufP);
  109.                     *bufP++ = '\\';
  110.                 }
  111.         }
  112.         bufP = CopyUpr(bufP, dirP);
  113.         if (*(bufP - 1) != '\\' && *(bufP - 1) != '/')
  114.                 *bufP++ = '\\';
  115.  
  116.         bufP = CopyUpr(bufP, nameP);
  117.  
  118.         if (extP)
  119.                 CopyUpr(bufP, extP);
  120.  
  121.         return (findfirst(pathP, &ffbuf, (mode & _PROGRAM) ? 0x27 : 0x37) + 1);
  122. }
  123.  
  124.  
  125. /*-----------------------------------------------------------------------*
  126.  
  127. Name            __searchpath - return an absolute DOS path for the given file
  128.                                name.
  129.  
  130. Usage           char    *pascal __searchpath(const char *pathP, int mode);
  131.  
  132. Prototype in    dir.h
  133.  
  134. Description     __searchpath attempts to locate a file, given by filename.
  135.                 If the value for mode specified is _USEPATH, the MS-DOS path
  136.                 is searched. A pointer to the complete path-name string is
  137.                 returned as the function value.
  138.  
  139.                 The current directory of the current drive is checked first. If
  140.                 the file is not found there and _USEPATH is specified, the PATH
  141.                 environment variable is fetched, and each directory in the
  142.                 path is searched in turn until the file is found or the path
  143.                 is exhausted.
  144.  
  145.                 When the file is located, a string is returned containing the
  146.                 full path name. This string can be used in a call to open or
  147.                 exec... to access the file.
  148.  
  149.                 The string returned is located in a static buffer and is
  150.                 destroyed on each subsequent call to __searchpath.
  151.  
  152. Return value    A pointer to a filename string is returned if the
  153.                 file is successfully located; otherwise, __searchpath returns
  154.                 NULL.
  155.  
  156. *------------------------------------------------------------------------*/
  157. char    *pascal near __searchpath(const char *pathP, int mode)
  158. {
  159.         register char   *bufP = PathFile;
  160.         register char   *envP = NULL;
  161.                  int    flag;
  162.  
  163.         /*      Preliminary checking            */
  164.         flag = 0;
  165.         if ((pathP != NULL) || (*pathP != 0))
  166.                 flag = fnsplit(pathP, drive, dir, fname, ext);
  167.         if ((flag & (WILDCARDS + FILENAME)) != FILENAME)
  168.                 return (NULL);
  169.  
  170. /*      If  looking  for  a  program  file,  limit  the  search  if a
  171.         directory or an extension is specified
  172. */
  173.         if (mode & _PROGRAM) {
  174.                 if (flag & DIRECTORY)
  175.                         mode &= ~_USEPATH;
  176.                 if (flag & EXTENSION)
  177.                         mode &= ~_PROGRAM;
  178.         }
  179.  
  180.         /*      Get "PATH" environment variable if allowed      */
  181.         if (mode & _USEPATH)
  182.                 envP = getenv("PATH");
  183.  
  184. /*      Try to locate "pathP" in current  directory, then try in all
  185.         directories specified  by the environment variable  "PATH" and
  186.         return a pointer  to the full path if  found, otherwise NULL
  187.         is returned.
  188. */
  189.         while (1) {
  190.  
  191.                 /* Check if the file exists */
  192.                 if (CheckFile(bufP, drive, dir, fname, ext, mode))
  193.                         break;
  194.  
  195.                 /* If PROGRAM file, try with ".COM" and ".EXE" extension */
  196.                 if (mode & _PROGRAM) {
  197.                         if (CheckFile(bufP, drive, dir, fname, ".COM", mode))
  198.                                 break;
  199.                         if (CheckFile(bufP, drive, dir, fname, ".EXE", mode))
  200.                                 break;
  201.                 }
  202.  
  203.                 /* Stops if no environment or end of it */
  204.                 if (envP == NULL || *envP == '\0') {
  205.                         bufP = NULL;
  206.                         break;
  207.                 }
  208.  
  209.                 /* Isolate drive name from environment */
  210.                 flag = 0;
  211.                 if (envP[1] == ':') {
  212.                         drive[flag++] = *envP++;
  213.                         drive[flag++] = *envP++;
  214.                 }
  215.                 drive[flag] = 0;
  216.  
  217.                 /* Isolate directory name from environment */
  218.                 for (flag = 0; (dir[flag] = *envP++) != 0; flag++)
  219.                         if (dir[flag] == ';') {
  220.                                 dir[flag] = 0;
  221.                 envP++;
  222.                                 break;
  223.                         }
  224.         envP--;        /* point back at '\0' or past ';' */
  225.  
  226.         /* if only drive specified, set dir to root */
  227.         if (dir[0] == '\0') {
  228.           dir[0] = '\\';
  229.           dir[1] = '\0';
  230.         }
  231.         }
  232.         return (bufP);
  233. }
  234.  
  235.  
  236. /*-----------------------------------------------------------------------*
  237.  
  238. Name            searchpath - searches the DOS path
  239.  
  240. Usage           char *searchpath(const char *filename);
  241.  
  242. Prototype in    dir.h
  243.  
  244. Description     searchpath simply calls __searchpath to search the current
  245.                 directory and MS DOS path for filename.
  246.  
  247. Return value    see __searchpath.
  248.  
  249. *------------------------------------------------------------------------*/
  250. char * _CType searchpath(const char *file)
  251. {
  252.         return __searchpath(file, _USEPATH);
  253. }
  254.