ReactOS  0.4.14-dev-552-g2fad488
where.c File Reference
#include "precomp.h"
Include dependency graph for where.c:

Go to the source code of this file.

Macros

#define ENV_BUFFER_SIZE   1024
 

Functions

BOOL SearchForExecutableSingle (LPCTSTR pFileName, LPTSTR pFullName, LPTSTR pPathExt, LPTSTR pDirectory)
 
BOOL SearchForExecutable (LPCTSTR pFileName, LPTSTR pFullName)
 

Macro Definition Documentation

◆ ENV_BUFFER_SIZE

#define ENV_BUFFER_SIZE   1024

Definition at line 80 of file where.c.

Function Documentation

◆ SearchForExecutable()

BOOL SearchForExecutable ( LPCTSTR  pFileName,
LPTSTR  pFullName 
)

Definition at line 142 of file where.c.

143 {
144  static TCHAR pszDefaultPathExt[] = _T(".com;.exe;.bat;.cmd");
145  LPTSTR pszPathExt, pszPath;
146  LPTSTR pCh;
147  DWORD dwBuffer;
148  TRACE ("SearchForExecutable: \'%s\'\n", debugstr_aw(pFileName));
149 
150  /* load environment variable PATHEXT */
151  pszPathExt = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
152  if (!pszPathExt)
153  {
154  WARN("Cannot allocate memory for pszPathExt!\n");
155  return FALSE;
156  }
157 
158  dwBuffer = GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, ENV_BUFFER_SIZE);
159  if (dwBuffer > ENV_BUFFER_SIZE)
160  {
161  LPTSTR pszOldPathExt = pszPathExt;
162  pszPathExt = (LPTSTR)cmd_realloc (pszPathExt, dwBuffer * sizeof (TCHAR));
163  if (!pszPathExt)
164  {
165  WARN("Cannot reallocate memory for pszPathExt!\n");
166  cmd_free(pszOldPathExt);
167  return FALSE;
168  }
169  GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, dwBuffer);
170  _tcslwr(pszPathExt);
171  }
172  else if (0 == dwBuffer)
173  {
174  _tcscpy(pszPathExt, pszDefaultPathExt);
175  }
176  else
177  {
178  _tcslwr(pszPathExt);
179  }
180 
181  /* Check if valid directly on specified path */
182  if (SearchForExecutableSingle(pFileName, pFullName, pszPathExt, NULL))
183  {
184  cmd_free(pszPathExt);
185  return TRUE;
186  }
187 
188  /* If an explicit directory was given, stop here - no need to search PATH. */
189  if (pFileName[1] == _T(':') || _tcschr(pFileName, _T('\\')))
190  {
191  cmd_free(pszPathExt);
192  return FALSE;
193  }
194 
195  /* load environment variable PATH into buffer */
196  pszPath = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR));
197  if (!pszPath)
198  {
199  WARN("Cannot allocate memory for pszPath!\n");
200  return FALSE;
201  }
202 
203  dwBuffer = GetEnvironmentVariable (_T("PATH"), pszPath, ENV_BUFFER_SIZE);
204  if (dwBuffer > ENV_BUFFER_SIZE)
205  {
206  LPTSTR pszOldPath = pszPath;
207  pszPath = (LPTSTR)cmd_realloc (pszPath, dwBuffer * sizeof (TCHAR));
208  if (!pszPath)
209  {
210  WARN("Cannot reallocate memory for pszPath!\n");
211  cmd_free(pszOldPath);
212  cmd_free(pszPathExt);
213  return FALSE;
214  }
215  GetEnvironmentVariable (_T("PATH"), pszPath, dwBuffer);
216  }
217 
218  TRACE ("SearchForExecutable(): Loaded PATH: %s\n", debugstr_aw(pszPath));
219 
220  /* search in PATH */
221  pCh = _tcstok(pszPath, _T(";"));
222  while (pCh)
223  {
224  if (SearchForExecutableSingle(pFileName, pFullName, pszPathExt, pCh))
225  {
226  cmd_free(pszPath);
227  cmd_free(pszPathExt);
228  return TRUE;
229  }
230  pCh = _tcstok(NULL, _T(";"));
231  }
232 
233  cmd_free(pszPath);
234  cmd_free(pszPathExt);
235  return FALSE;
236 }
#define TRUE
Definition: types.h:120
#define ENV_BUFFER_SIZE
Definition: where.c:80
#define WARN(fmt,...)
Definition: debug.h:111
#define debugstr_aw
Definition: precomp.h:43
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
CHAR * LPTSTR
Definition: xmlstorage.h:192
smooth NULL
Definition: ftsmooth.c:416
#define _tcslwr
Definition: tchar.h:1465
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
unsigned long DWORD
Definition: ntddk_ex.h:95
#define cmd_realloc(ptr, size)
Definition: cmddbg.h:30
#define cmd_alloc(size)
Definition: cmddbg.h:29
#define cmd_free(ptr)
Definition: cmddbg.h:31
#define _tcstok
Definition: tchar.h:1416
#define GetEnvironmentVariable
Definition: winbase.h:3648
BOOL SearchForExecutableSingle(LPCTSTR pFileName, LPTSTR pFullName, LPTSTR pPathExt, LPTSTR pDirectory)
Definition: where.c:86

Referenced by cmd_start(), Execute(), and GetCmdLineCommand().

◆ SearchForExecutableSingle()

BOOL SearchForExecutableSingle ( LPCTSTR  pFileName,
LPTSTR  pFullName,
LPTSTR  pPathExt,
LPTSTR  pDirectory 
)

Definition at line 86 of file where.c.

87 {
88  TCHAR szPathBuffer[CMDLINE_LENGTH], *pszPathEnd;
89  LPTSTR s,f;
90  /* initialize full name buffer */
91  *pFullName = _T('\0');
92 
93  TRACE ("SearchForExecutableSingle: \'%s\' in dir: \'%s\'\n",
94  debugstr_aw(pFileName), debugstr_aw(pDirectory));
95 
96  pszPathEnd = szPathBuffer;
97  if (pDirectory != NULL)
98  {
99  _tcscpy(szPathBuffer, pDirectory);
100  pszPathEnd += _tcslen(pszPathEnd);
101  *pszPathEnd++ = _T('\\');
102  }
103  _tcscpy(pszPathEnd, pFileName);
104  pszPathEnd += _tcslen(pszPathEnd);
105 
106  if (IsExistingFile (szPathBuffer))
107  {
108  TRACE ("Found: \'%s\'\n", debugstr_aw(szPathBuffer));
109  _tcscpy (pFullName, szPathBuffer);
110  return TRUE;
111  }
112 
113  s = pPathExt;
114  while (s && *s)
115  {
116  f = _tcschr (s, _T(';'));
117 
118  if (f)
119  {
120  _tcsncpy (pszPathEnd, s, (size_t)(f-s));
121  pszPathEnd[f-s] = _T('\0');
122  s = f + 1;
123  }
124  else
125  {
126  _tcscpy (pszPathEnd, s);
127  s = NULL;
128  }
129 
130  if (IsExistingFile (szPathBuffer))
131  {
132  TRACE ("Found: \'%s\'\n", debugstr_aw(szPathBuffer));
133  _tcscpy (pFullName, szPathBuffer);
134  return TRUE;
135  }
136  }
137  return FALSE;
138 }
#define TRUE
Definition: types.h:120
_TCHAR * _tcsncpy(_TCHAR *dst, const _TCHAR *src, size_t n)
Definition: tcsncpy.h:9
#define debugstr_aw
Definition: precomp.h:43
#define CMDLINE_LENGTH
Definition: help.h:12
_TCHAR * _tcscpy(_TCHAR *to, const _TCHAR *from)
Definition: tcscpy.h:8
CHAR * LPTSTR
Definition: xmlstorage.h:192
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
smooth NULL
Definition: ftsmooth.c:416
_TCHAR * _tcschr(const _TCHAR *s, _XINT c)
Definition: tcschr.h:4
char TCHAR
Definition: xmlstorage.h:189
GLfloat f
Definition: glext.h:7540
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
GLdouble s
Definition: gl.h:2039
#define f
Definition: ke_i.h:83
BOOL IsExistingFile(LPCTSTR)
Definition: misc.c:500

Referenced by SearchForExecutable().