Searches for a file using environment paths.
#include <stdlib.h>
void _searchenv( char *filename, char *varname, char *pathname );
filename | Name of file to search for | |
varname | Environment to search | |
pathname | Buffer to store complete path |
The _searchenv routine searches for the target file in the specified domain. The varname variable can be any environment variable that specifies a list of directory paths, such as PATH, LIB, INCLUDE, or other user-defined variables. The _searchenv function is case-sensitive, so the varname variable should match the case of the environment variable.
The routine first searches for the file in the current working directory. If it doesn't find the file, it next looks through the directories specified by the environment variable.
If the target file is found in one of the directories, the newly created path is copied into the buffer pointed to by pathname. You must ensure that there is sufficient space for the constructed path name. If the filename file is not found, pathname will contain an empty null-terminated string.
The _searchenv function does not return a value.
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* SEARCHEN.C: This program searches for a file in a directory
* specified by an environment variable.
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char pathbuffer[_MAX_PATH];
char searchfile[] = "CL.EXE";
char envvar[] = "PATH";
/* Search for file in PATH environment variable: */
_searchenv( searchfile, envvar, pathbuffer );
if( *pathbuffer != '\0' )
printf( "Path for %s: %s\n", searchfile, pathbuffer );
else
printf( "%s not found\n", searchfile );
}
Path for CL.EXE: C:\BIN\CL.EXE