2 * WHERE.C - file search functions.
7 * 07/15/95 (Tim Norman)
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
14 * 12/12/95 (Steffan Kaiser & Tim Norman)
15 * added some patches to fix some things and make more efficient
18 * fixed a stupid pointer mistake...
19 * Thanks to everyone who noticed it!
22 * fixed a bug when getenv returns NULL
24 * 8/7/96 (Steffan Kaiser and Tim Norman)
25 * speed improvements and bug fixes
27 * 8/27/96 (Tim Norman)
28 * changed code to use pointers directly into PATH environment
29 * variable rather than making our own copy. This saves some memory,
30 * but requires we write our own function to copy pathnames out of
33 * 12/23/96 (Aaron Kaufman)
34 * Fixed a bug in get_paths() that did not point to the first PATH
35 * in the environment variable.
37 * 7/12/97 (Tim Norman)
38 * Apparently, Aaron's bugfix got lost, so I fixed it again.
40 * 16 July 1998 (John P. Price)
41 * Added stand alone code.
43 * 17 July 1998 (John P. Price)
44 * Rewrote find_which to use searchpath function
46 * 24-Jul-1998 (John P Price <linux-guru@gcfl.net>)
47 * fixed bug where didn't check all extensions when path was specified
49 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
50 * added config.h include
52 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
53 * fixed so that it find_which returns NULL if filename is not
54 * executable (does not have .bat, .com, or .exe extention).
55 * Before command would to execute any file with any extension (opps!)
57 * 03-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
58 * Changed find_which().
60 * 07-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
61 * Added ".CMD" extension.
62 * Replaced numeric constant by _NR_OF_EXTENSIONS.
64 * 26-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
65 * Replaced find_which() by SearchForExecutable().
66 * Now files are serched with the right order of extensions.
78 /* initial size of environment variable buffer */
79 #define ENV_BUFFER_SIZE 1024
82 static LPTSTR ext
[] = {".BAT", ".CMD", ".COM", ".EXE"};
83 static INT nExtCount
= sizeof(ext
) / sizeof(LPTSTR
);
86 /* searches for file using path info. */
89 SearchForExecutable (LPCTSTR pFileName
, LPTSTR pFullName
)
91 TCHAR szPathBuffer
[MAX_PATH
];
97 /* load environment varable PATH into buffer */
98 pszBuffer
= (LPTSTR
)malloc (ENV_BUFFER_SIZE
* sizeof(TCHAR
));
99 dwBuffer
= GetEnvironmentVariable (_T("PATH"), pszBuffer
, ENV_BUFFER_SIZE
);
102 ConErrPrintf (_T("Not PATH environment variable found!\n"));
105 else if (dwBuffer
> ENV_BUFFER_SIZE
)
107 pszBuffer
= (LPTSTR
)realloc (pszBuffer
, dwBuffer
* sizeof (TCHAR
));
108 GetEnvironmentVariable (_T("PATH"), pszBuffer
, dwBuffer
* sizeof (TCHAR
));
112 /* initialize full name buffer */
113 *pFullName
= _T('\0');
115 if (!(p
= _tcsrchr (pFileName
, _T('.'))) ||
116 _tcschr (p
+ 1, _T('\\')))
118 /* There is no extension ==> test all the extensions. */
120 DebugPrintf (_T("No filename extension!\n"));
123 /* search in current directory */
124 GetCurrentDirectory (MAX_PATH
, szPathBuffer
);
125 if (szPathBuffer
[_tcslen(szPathBuffer
)-1] != _T('\\'))
126 _tcscat (szPathBuffer
, _T("\\"));
127 _tcscat (szPathBuffer
, pFileName
);
129 p
= szPathBuffer
+ _tcslen (szPathBuffer
);
131 for (n
= 0; n
< nExtCount
; n
++)
136 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
139 if (IsValidFileName (szPathBuffer
))
142 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
145 _tcscpy (pFullName
, szPathBuffer
);
154 f
= _tcschr (s
, _T(';'));
158 _tcsncpy (szPathBuffer
, s
, (size_t)(f
-s
));
159 szPathBuffer
[f
-s
] = _T('\0');
164 _tcscpy (szPathBuffer
, s
);
168 if (szPathBuffer
[_tcslen(szPathBuffer
)-1] != _T('\\'))
169 _tcscat (szPathBuffer
, _T("\\"));
170 _tcscat (szPathBuffer
, pFileName
);
172 p
= szPathBuffer
+ _tcslen (szPathBuffer
);
174 for (n
= 0; n
< nExtCount
; n
++)
179 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
182 if (IsValidFileName (szPathBuffer
))
185 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
188 _tcscpy (pFullName
, szPathBuffer
);
196 /* There is an extension and it is in the last path component, */
197 /* so don't test all the extensions. */
199 DebugPrintf (_T("Filename extension!\n"));
202 /* search in current directory */
203 GetCurrentDirectory (MAX_PATH
, szPathBuffer
);
204 if (szPathBuffer
[_tcslen(szPathBuffer
)-1] != _T('\\'))
205 _tcscat (szPathBuffer
, _T("\\"));
206 _tcscat (szPathBuffer
, pFileName
);
209 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
211 if (IsValidFileName (szPathBuffer
))
214 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
217 _tcscpy (pFullName
, szPathBuffer
);
226 f
= _tcschr (s
, _T(';'));
230 _tcsncpy (szPathBuffer
, s
, (size_t)(f
-s
));
231 szPathBuffer
[f
-s
] = _T('\0');
236 _tcscpy (szPathBuffer
, s
);
240 if (szPathBuffer
[_tcslen(szPathBuffer
)-1] != _T('\\'))
241 _tcscat (szPathBuffer
, _T("\\"));
242 _tcscat (szPathBuffer
, pFileName
);
245 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
247 if (IsValidFileName (szPathBuffer
))
250 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
253 _tcscpy (pFullName
, szPathBuffer
);