home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
WHICH.CMM
< prev
next >
Wrap
Text File
|
1996-01-24
|
2KB
|
76 lines
//********************************************************************
//*** Which.cmm - Show when command would be executed by searching ***
//*** ver.2 for command along path. ***
//********************************************************************
#include <netware.lib>
#include <filename.lib>
Instructions()
{
printf("\a\n")
puts(`Which - Determine which command in PATH will execute, and what order`)
puts(``)
puts(`SYNTAX: Which <CommandSpec>`)
puts(``)
puts(`WHERE: CommandSpec is any file spec. If CommandSpec has no extension then`)
printf(` will search executables in this order:`)
for ( i = 0; i <= GetArraySpan(executable_extensions); i++ )
printf(" %s",executable_extensions[i])
puts(``)
puts(`EXAMPLES: Which EDIT`)
puts(` Which SESSION.C*`)
exit(EXIT_FAILURE);
}
/* ---------------------------------------------------------------------- */
main(argc,argv)
{
if ( argc != 2 ) Instructions();
NameParts = SplitFileName(argv[1]);
if ( NameParts.dir[0] ) {
printf("\aWHICH will not work with directory specifications.\n");
exit(EXIT_FAILURE);
}
// if extension then use it, else use our extension list
if ( NameParts.ext[0] ) {
Extensions[0] = NameParts.ext;
ExtensionCount = 1;
} else {
Extensions = executable_extensions;
ExtensionCount = 1 + GetArraySpan(executable_extensions);
}
// get prioritized list of all directories
PathList = build_path_list();
PathCount = (PathList==NULL)?0:GetArraySpan(PathList)+1;
// for each directory, find all of the matching files
FindCount = 0;
for ( PathIdx = 0; PathIdx < PathCount; PathIdx++ ) {
for ( ExtensionIdx = 0; ExtensionIdx < ExtensionCount; ExtensionIdx++ ) {
FindCount += FindFileInDirectory(PathList[PathIdx],NameParts.name,Extensions[ExtensionIdx]);
}
}
if ( !FindCount ) {
printf("\aNo commands found for %s\n",argv[1]);
exit(EXIT_FAILURE);
}
}
FindFileInDirectory(DirSpec,NameSpec,ExtSpec)
{
sprintf(FindSpec,"%s%s%s",DirSpec,NameSpec,ExtSpec);
if ( !(FindList = Directory(FindSpec,False,_A_RDONLY|_A_ARCH)) )
return 0;
Count = 1 + GetArraySpan(FindList);
for ( i = 0; i < Count; i++ )
printf(" %s\n",FindList[i].name);
return Count;
}