1ea1625f77d9bd079da56867037d529aa1a3d370
[reactos.git] / reactos / apps / utils / cmd / where.c
1 /*
2 * WHERE.C - file serch functions.
3 *
4 *
5 * History:
6 *
7 * 07/15/95 (Tim Norman)
8 * started.
9 *
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source into
12 * guidelines for recommended programming practice.
13 *
14 * 12/12/95 (Steffan Kaiser & Tim Norman)
15 * added some patches to fix some things and make more efficient
16 *
17 * 1/6/96 (Tim Norman)
18 * fixed a stupid pointer mistake... Thanks to everyone who noticed it!
19 *
20 * 8/1/96 (Tim Norman)
21 * fixed a bug when getenv returns NULL
22 *
23 * 8/7/96 (Steffan Kaiser and Tim Norman)
24 * speed improvements and bug fixes
25 *
26 * 8/27/96 (Tim Norman)
27 * changed code to use pointers directly into PATH environment variable
28 * rather than making our own copy. This saves some memory, but requires
29 * we write our own function to copy pathnames out of the variable.
30 *
31 * 12/23/96 (Aaron Kaufman)
32 * Fixed a bug in get_paths() that did not point to the first PATH in the
33 * environment variable.
34 *
35 * 7/12/97 (Tim Norman)
36 * Apparently, Aaron's bugfix got lost, so I fixed it again.
37 *
38 * 16 July 1998 (John P. Price)
39 * Added stand alone code.
40 *
41 * 17 July 1998 (John P. Price)
42 * Rewrote find_which to use searchpath function
43 *
44 * 24-Jul-1998 (John P Price <linux-guru@gcfl.net>)
45 * - fixed bug where didn't check all extensions when path was specified
46 *
47 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
48 * - added config.h include
49 *
50 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
51 * - fixed so that it find_which returns NULL if filename is not executable
52 * (does not have .bat, .com, or .exe extention). Before command would
53 * to execute any file with any extension (opps!)
54 *
55 * 03-Dec_1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
56 * Changed find_which().
57 *
58 * 07-Dec_1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
59 * Added ".CMD" extension.
60 * Replaced numeric constant by _NR_OF_EXTENSIONS.
61 */
62
63 #define WIN32_LEAN_AND_MEAN
64
65 #include "config.h"
66
67 #include <windows.h>
68 #include <string.h>
69 #include <stdlib.h>
70
71 #include "cmd.h"
72
73
74 static LPTSTR ext[] = {".BAT", ".CMD", ".COM", ".EXE"};
75 static INT nExtCount = sizeof(ext) / sizeof(LPTSTR);
76
77
78 /* searches for file using path info. */
79
80 BOOL find_which (LPCTSTR fname, LPTSTR fullbuffer)
81 {
82 static TCHAR temp[MAX_PATH];
83 LPTSTR fullname;
84 INT x;
85
86 *fullbuffer = _T('\0');
87
88 /* if there an extension and it is in the last path component, then
89 * don't test all the extensions. */
90 if (!(fullname = _tcsrchr (fname, _T('.'))) ||
91 _tcschr (fullname + 1, _T('\\')))
92 {
93 #ifdef _DEBUG
94 DebugPrintf ("No filename extension!\n");
95 #endif
96
97 for (x = 0; x < nExtCount; x++)
98 {
99 _tcscpy (temp, fname);
100 _tcscat (temp, ext[x]);
101 #ifdef _DEBUG
102 DebugPrintf ("Checking for %s\n", temp);
103 #endif
104 if (_tcschr (fname, _T('\\')))
105 {
106 if (IsValidFileName (temp))
107 {
108 _tcscpy (fullbuffer, temp);
109 return TRUE;
110 }
111 }
112 else
113 {
114 _searchenv (temp, _T("PATH"), fullbuffer);
115 if (*fullbuffer != '\0')
116 return TRUE;
117 }
118 }
119 }
120 else
121 {
122 /* there is an extension... don't test other extensions */
123 /* make sure that the extention is one of the four */
124 #ifdef _DEBUG
125 DebugPrintf ("No filename extension!\n");
126 #endif
127 for (x = 0; x < nExtCount; x++)
128 {
129 if (!_tcsicmp (_tcsrchr (fname, _T('.')), ext[x]))
130 {
131 if (_tcschr (fname, _T('\\')))
132 {
133 if (IsValidFileName (fname))
134 {
135 _tcscpy (fullbuffer, fname);
136 #ifdef _DEBUG
137 DebugPrintf ("Found: %s\n", fullbuffer);
138 #endif
139 return TRUE;
140 }
141 }
142 else
143 {
144 #ifdef _DEBUG
145 DebugPrintf ("Checking for %s\n", fname);
146 #endif
147 _searchenv (fname, _T("PATH"), fullbuffer);
148 if (*fullbuffer != _T('\0'))
149 {
150 #ifdef _DEBUG
151 DebugPrintf ("Found: %s\n", fullbuffer);
152 #endif
153 return TRUE;
154 }
155 }
156 }
157 }
158 }
159
160 return FALSE;
161 }