home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - searchp.cas
- *
- * function(s)
- * CopyUpr - copy string and convert to upper-case
- * CheckFile - build absolute pathname and check existence
- * __searchpath - return an absolute DOS path for the given file name.
- * searchpath - searches the DOS path
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C Run Time Library - Version 3.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1987,1988,1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
- #pragma inline
- #include <asmrules.h>
- #include <dir.h>
- #include <_dir.h>
- #include <string.h>
- #include <stdlib.h>
-
- static char PathFile[MAXPATH];
- static char drive[MAXDRIVE+1];
- static char dir[MAXDIR+1];
- static char fname[MAXFILE+1];
- static char ext[MAXEXT+1];
-
- /*-----------------------------------------------------------------------*
-
- Name CopyUpr - copy string and convert to upper-case
-
- Usage void * pascal near CopyUpr(char *dst, char *src)
-
- Description Copies a string to another, converting all lowercase
- characters to upper case.
-
- Return value Returns the address of the terminating null byte in "dst".
-
- *------------------------------------------------------------------------*/
- static void * pascal near CopyUpr(char *dst, char *src)
- {
- pushDS_
- asm LDS_ si, src
- asm LES_ di, dst
- asm cld
- #if !LDATA
- asm push ds
- asm pop es
- #endif
- Copying:
- asm lodsb
- asm cmp al, 'a'
- asm jb Converted
- asm cmp al, 'z'
- asm ja Converted
- asm sub al, 'a' - 'A'
- Converted:
- asm stosb
- asm or al, al
- asm jnz Copying
- asm dec di
- asm xchg ax,di
- popDS_
- #pragma warn -sus
- return (void _es *) _AX;
- #pragma warn .sus
- }
-
-
- /*-----------------------------------------------------------------------*
-
- Name CheckFile - build absolute pathname and check existence
-
- Usage static int pascal near CheckFile(char *pathP, char *driveP,
- char *dirP, char *nameP,
- char *extP, int mode);
-
- Description Builds an absolute pathname with the given components,
- and then checks for the given file existency.
-
- Return value Returns a non-zero if the given file exists.
-
- *------------------------------------------------------------------------*/
- static int pascal near CheckFile(char *pathP, char *driveP, char *dirP,
- char *nameP, char *extP, int mode)
- {
- register char *bufP;
- struct ffblk ffbuf;
-
- bufP = pathP;
- if (*driveP == '\0')
- *driveP = 'A' + getdisk();
- else
- *driveP &= ~0x20;
- *bufP++ = *driveP;
- *bufP++ = ':';
-
- if (*dirP != '\\' && *dirP != '/') {
- *bufP++ = '\\';
- getcurdir(*driveP - '@', bufP);
- if(*bufP) {
- bufP = CopyUpr(bufP, bufP);
- *bufP++ = '\\';
- }
- }
- bufP = CopyUpr(bufP, dirP);
- if (*(bufP - 1) != '\\' && *(bufP - 1) != '/')
- *bufP++ = '\\';
-
- bufP = CopyUpr(bufP, nameP);
-
- if (extP)
- CopyUpr(bufP, extP);
-
- return (findfirst(pathP, &ffbuf, (mode & _PROGRAM) ? 0x27 : 0x37) + 1);
- }
-
-
- /*-----------------------------------------------------------------------*
-
- Name __searchpath - return an absolute DOS path for the given file
- name.
-
- Usage char *pascal __searchpath(const char *pathP, int mode);
-
- Prototype in dir.h
-
- Description __searchpath attempts to locate a file, given by filename.
- If the value for mode specified is _USEPATH, the MS-DOS path
- is searched. A pointer to the complete path-name string is
- returned as the function value.
-
- The current directory of the current drive is checked first. If
- the file is not found there and _USEPATH is specified, the PATH
- environment variable is fetched, and each directory in the
- path is searched in turn until the file is found or the path
- is exhausted.
-
- When the file is located, a string is returned containing the
- full path name. This string can be used in a call to open or
- exec... to access the file.
-
- The string returned is located in a static buffer and is
- destroyed on each subsequent call to __searchpath.
-
- Return value A pointer to a filename string is returned if the
- file is successfully located; otherwise, __searchpath returns
- NULL.
-
- *------------------------------------------------------------------------*/
- char *pascal near __searchpath(const char *pathP, int mode)
- {
- register char *bufP = PathFile;
- register char *envP = NULL;
- int flag;
-
- /* Preliminary checking */
- flag = 0;
- if ((pathP != NULL) || (*pathP != 0))
- flag = fnsplit(pathP, drive, dir, fname, ext);
- if ((flag & (WILDCARDS + FILENAME)) != FILENAME)
- return (NULL);
-
- /* If looking for a program file, limit the search if a
- directory or an extension is specified
- */
- if (mode & _PROGRAM) {
- if (flag & DIRECTORY)
- mode &= ~_USEPATH;
- if (flag & EXTENSION)
- mode &= ~_PROGRAM;
- }
-
- /* Get "PATH" environment variable if allowed */
- if (mode & _USEPATH)
- envP = getenv("PATH");
-
- /* Try to locate "pathP" in current directory, then try in all
- directories specified by the environment variable "PATH" and
- return a pointer to the full path if found, otherwise NULL
- is returned.
- */
- while (1) {
-
- /* Check if the file exists */
- if (CheckFile(bufP, drive, dir, fname, ext, mode))
- break;
-
- /* If PROGRAM file, try with ".COM" and ".EXE" extension */
- if (mode & _PROGRAM) {
- if (CheckFile(bufP, drive, dir, fname, ".COM", mode))
- break;
- if (CheckFile(bufP, drive, dir, fname, ".EXE", mode))
- break;
- }
-
- /* Stops if no environment or end of it */
- if (envP == NULL || *envP == '\0') {
- bufP = NULL;
- break;
- }
-
- /* Isolate drive name from environment */
- flag = 0;
- if (envP[1] == ':') {
- drive[flag++] = *envP++;
- drive[flag++] = *envP++;
- }
- drive[flag] = 0;
-
- /* Isolate directory name from environment */
- for (flag = 0; (dir[flag] = *envP++) != 0; flag++)
- if (dir[flag] == ';') {
- dir[flag] = 0;
- envP++;
- break;
- }
- envP--; /* point back at '\0' or past ';' */
-
- /* if only drive specified, set dir to root */
- if (dir[0] == '\0') {
- dir[0] = '\\';
- dir[1] = '\0';
- }
- }
- return (bufP);
- }
-
-
- /*-----------------------------------------------------------------------*
-
- Name searchpath - searches the DOS path
-
- Usage char *searchpath(const char *filename);
-
- Prototype in dir.h
-
- Description searchpath simply calls __searchpath to search the current
- directory and MS DOS path for filename.
-
- Return value see __searchpath.
-
- *------------------------------------------------------------------------*/
- char * _CType searchpath(const char *file)
- {
- return __searchpath(file, _USEPATH);
- }
-