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 searched using the right extension order.
68 * 20-Apr-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
69 * Some minor changes and improvements.
81 /* initial size of environment variable buffer */
82 #define ENV_BUFFER_SIZE 1024
85 static LPTSTR ext
[] = {".bat", ".cmd", ".com", ".exe"};
86 static INT nExtCount
= sizeof(ext
) / sizeof(LPTSTR
);
89 /* searches for file using path info. */
92 SearchForExecutable (LPCTSTR pFileName
, LPTSTR pFullName
)
94 TCHAR szPathBuffer
[MAX_PATH
];
95 LPTSTR pszBuffer
= NULL
;
100 /* load environment varable PATH into buffer */
101 pszBuffer
= (LPTSTR
)malloc (ENV_BUFFER_SIZE
* sizeof(TCHAR
));
102 dwBuffer
= GetEnvironmentVariable (_T("PATH"), pszBuffer
, ENV_BUFFER_SIZE
);
103 if (dwBuffer
> ENV_BUFFER_SIZE
)
105 pszBuffer
= (LPTSTR
)realloc (pszBuffer
, dwBuffer
* sizeof (TCHAR
));
106 GetEnvironmentVariable (_T("PATH"), pszBuffer
, dwBuffer
* sizeof (TCHAR
));
109 /* initialize full name buffer */
110 *pFullName
= _T('\0');
112 if (!(p
= _tcsrchr (pFileName
, _T('.'))) ||
113 _tcschr (p
+ 1, _T('\\')))
115 /* There is no extension ==> test all the extensions. */
117 DebugPrintf (_T("No filename extension!\n"));
120 /* search in current directory */
121 len
= GetCurrentDirectory (MAX_PATH
, szPathBuffer
);
122 if (szPathBuffer
[len
- 1] != _T('\\'))
124 szPathBuffer
[len
] = _T('\\');
125 szPathBuffer
[len
+ 1] = _T('\0');
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 len
= _tcslen(szPathBuffer
);
169 if (szPathBuffer
[len
- 1] != _T('\\'))
171 szPathBuffer
[len
] = _T('\\');
172 szPathBuffer
[len
+ 1] = _T('\0');
174 _tcscat (szPathBuffer
, pFileName
);
176 p
= szPathBuffer
+ _tcslen (szPathBuffer
);
178 for (n
= 0; n
< nExtCount
; n
++)
183 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
186 if (IsValidFileName (szPathBuffer
))
189 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
192 _tcscpy (pFullName
, szPathBuffer
);
200 /* There is an extension and it is in the last path component, */
201 /* so don't test all the extensions. */
203 DebugPrintf (_T("Filename extension!\n"));
206 /* search in current directory */
207 len
= GetCurrentDirectory (MAX_PATH
, szPathBuffer
);
208 if (szPathBuffer
[len
- 1] != _T('\\'))
210 szPathBuffer
[len
] = _T('\\');
211 szPathBuffer
[len
+ 1] = _T('\0');
213 _tcscat (szPathBuffer
, pFileName
);
216 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
218 if (IsValidFileName (szPathBuffer
))
221 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
224 _tcscpy (pFullName
, szPathBuffer
);
233 f
= _tcschr (s
, _T(';'));
237 _tcsncpy (szPathBuffer
, s
, (size_t)(f
-s
));
238 szPathBuffer
[f
-s
] = _T('\0');
243 _tcscpy (szPathBuffer
, s
);
247 len
= _tcslen(szPathBuffer
);
248 if (szPathBuffer
[len
- 1] != _T('\\'))
250 szPathBuffer
[len
] = _T('\\');
251 szPathBuffer
[len
+ 1] = _T('\0');
253 _tcscat (szPathBuffer
, pFileName
);
256 DebugPrintf (_T("Testing: \'%s\'\n"), szPathBuffer
);
258 if (IsValidFileName (szPathBuffer
))
261 DebugPrintf (_T("Found: \'%s\'\n"), szPathBuffer
);
264 _tcscpy (pFullName
, szPathBuffer
);