#include #include #include #include /* which-vc.c This implementation of 'which' takes advantage of a built-in Win32 function, SearchPath, and presumably provides the best possible emulation of Microsoft's executable-file finder. I, David Lee Lambert, authorize the distribution of this code under the GPL. */ const char * CopyRightString = "Copyright 2002 David Lee Lambert"; int main(int argc, char * argv[], char * envp[]) { char *execname = NULL; #define MAX 80 char path[MAX], *fnp; char *ext[4] = {".com",".exe",".bat", NULL}; int retlen, found = 0, i; char * dot; /* parse command-line */ if (argc == 2) execname = argv[1]; else { printf("%s: Wrong # of args (%d) --" "syntax= which \n", argv[0], argc); puts(CopyRightString); putchar('\n'); return 2; } dot = strchr(execname, '.'); if (dot) { retlen = SearchPath(NULL, execname, NULL, MAX, path, &fnp); if (retlen > 0) { printf("%s\n", path); found = 1; } } else { for (i = 0; i < 4; i++) { retlen = SearchPath(NULL, execname, ext[i], MAX, path, &fnp); if (retlen > 0) { printf("%s\n", path); found = 1; } } } if ( !found ) { printf("not found\n"); return 1; } return 0; }