Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenwhere.c
Go to the documentation of this file.
00001 /* 00002 * WHERE.C - file search functions. 00003 * 00004 * 00005 * History: 00006 * 00007 * 07/15/95 (Tim Norman) 00008 * started. 00009 * 00010 * 08/08/95 (Matt Rains) 00011 * i have cleaned up the source code. changes now bring this source 00012 * into guidelines for recommended programming practice. 00013 * 00014 * 12/12/95 (Steffan Kaiser & Tim Norman) 00015 * added some patches to fix some things and make more efficient 00016 * 00017 * 1/6/96 (Tim Norman) 00018 * fixed a stupid pointer mistake... 00019 * Thanks to everyone who noticed it! 00020 * 00021 * 8/1/96 (Tim Norman) 00022 * fixed a bug when getenv returns NULL 00023 * 00024 * 8/7/96 (Steffan Kaiser and Tim Norman) 00025 * speed improvements and bug fixes 00026 * 00027 * 8/27/96 (Tim Norman) 00028 * changed code to use pointers directly into PATH environment 00029 * variable rather than making our own copy. This saves some memory, 00030 * but requires we write our own function to copy pathnames out of 00031 * the variable. 00032 * 00033 * 12/23/96 (Aaron Kaufman) 00034 * Fixed a bug in get_paths() that did not point to the first PATH 00035 * in the environment variable. 00036 * 00037 * 7/12/97 (Tim Norman) 00038 * Apparently, Aaron's bugfix got lost, so I fixed it again. 00039 * 00040 * 16 July 1998 (John P. Price) 00041 * Added stand alone code. 00042 * 00043 * 17 July 1998 (John P. Price) 00044 * Rewrote find_which to use searchpath function 00045 * 00046 * 24-Jul-1998 (John P Price <linux-guru@gcfl.net>) 00047 * fixed bug where didn't check all extensions when path was specified 00048 * 00049 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>) 00050 * added config.h include 00051 * 00052 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>) 00053 * fixed so that it find_which returns NULL if filename is not 00054 * executable (does not have .bat, .com, or .exe extention). 00055 * Before command would to execute any file with any extension (opps!) 00056 * 00057 * 03-Dec-1998 (Eric Kohl) 00058 * Changed find_which(). 00059 * 00060 * 07-Dec-1998 (Eric Kohl) 00061 * Added ".CMD" extension. 00062 * Replaced numeric constant by _NR_OF_EXTENSIONS. 00063 * 00064 * 26-Feb-1999 (Eric Kohl) 00065 * Replaced find_which() by SearchForExecutable(). 00066 * Now files are searched using the right extension order. 00067 * 00068 * 20-Apr-1999 (Eric Kohl) 00069 * Some minor changes and improvements. 00070 * 00071 * 10-Jul-2004 (Jens Collin <jens.collin@lakhei.com>) 00072 * Fixed searxhing for files with specific extensions in PATHEXT order.. 00073 * 00074 */ 00075 00076 #include <precomp.h> 00077 00078 00079 /* initial size of environment variable buffer */ 00080 #define ENV_BUFFER_SIZE 1024 00081 00082 00083 /* searches for file using path info. */ 00084 00085 BOOL 00086 SearchForExecutableSingle (LPCTSTR pFileName, LPTSTR pFullName, LPTSTR pPathExt, LPTSTR pDirectory) 00087 { 00088 TCHAR szPathBuffer[CMDLINE_LENGTH], *pszPathEnd; 00089 LPTSTR s,f; 00090 /* initialize full name buffer */ 00091 *pFullName = _T('\0'); 00092 00093 TRACE ("SearchForExecutableSingle: \'%s\' in dir: \'%s\'\n", 00094 debugstr_aw(pFileName), debugstr_aw(pDirectory)); 00095 00096 pszPathEnd = szPathBuffer; 00097 if (pDirectory != NULL) 00098 { 00099 _tcscpy(szPathBuffer, pDirectory); 00100 pszPathEnd += _tcslen(pszPathEnd); 00101 *pszPathEnd++ = _T('\\'); 00102 } 00103 _tcscpy(pszPathEnd, pFileName); 00104 pszPathEnd += _tcslen(pszPathEnd); 00105 00106 if (IsExistingFile (szPathBuffer)) 00107 { 00108 TRACE ("Found: \'%s\'\n", debugstr_aw(szPathBuffer)); 00109 _tcscpy (pFullName, szPathBuffer); 00110 return TRUE; 00111 } 00112 00113 s = pPathExt; 00114 while (s && *s) 00115 { 00116 f = _tcschr (s, _T(';')); 00117 00118 if (f) 00119 { 00120 _tcsncpy (pszPathEnd, s, (size_t)(f-s)); 00121 pszPathEnd[f-s] = _T('\0'); 00122 s = f + 1; 00123 } 00124 else 00125 { 00126 _tcscpy (pszPathEnd, s); 00127 s = NULL; 00128 } 00129 00130 if (IsExistingFile (szPathBuffer)) 00131 { 00132 TRACE ("Found: \'%s\'\n", debugstr_aw(szPathBuffer)); 00133 _tcscpy (pFullName, szPathBuffer); 00134 return TRUE; 00135 } 00136 } 00137 return FALSE; 00138 } 00139 00140 00141 BOOL 00142 SearchForExecutable (LPCTSTR pFileName, LPTSTR pFullName) 00143 { 00144 static TCHAR pszDefaultPathExt[] = _T(".com;.exe;.bat;.cmd"); 00145 LPTSTR pszPathExt, pszPath; 00146 LPTSTR pCh; 00147 DWORD dwBuffer; 00148 TRACE ("SearchForExecutable: \'%s\'\n", debugstr_aw(pFileName)); 00149 00150 /* load environment varable PATHEXT */ 00151 pszPathExt = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); 00152 dwBuffer = GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, ENV_BUFFER_SIZE); 00153 if (dwBuffer > ENV_BUFFER_SIZE) 00154 { 00155 pszPathExt = (LPTSTR)cmd_realloc (pszPathExt, dwBuffer * sizeof (TCHAR)); 00156 GetEnvironmentVariable (_T("PATHEXT"), pszPathExt, dwBuffer); 00157 _tcslwr(pszPathExt); 00158 } 00159 else if (0 == dwBuffer) 00160 { 00161 _tcscpy(pszPathExt, pszDefaultPathExt); 00162 } 00163 else 00164 { 00165 _tcslwr(pszPathExt); 00166 } 00167 00168 /* Check if valid directly on specified path */ 00169 if (SearchForExecutableSingle(pFileName, pFullName, pszPathExt, NULL)) 00170 { 00171 cmd_free(pszPathExt); 00172 return TRUE; 00173 } 00174 00175 /* If an explicit directory was given, stop here - no need to search PATH. */ 00176 if (pFileName[1] == _T(':') || _tcschr(pFileName, _T('\\'))) 00177 { 00178 cmd_free(pszPathExt); 00179 return FALSE; 00180 } 00181 00182 /* load environment varable PATH into buffer */ 00183 pszPath = (LPTSTR)cmd_alloc (ENV_BUFFER_SIZE * sizeof(TCHAR)); 00184 dwBuffer = GetEnvironmentVariable (_T("PATH"), pszPath, ENV_BUFFER_SIZE); 00185 if (dwBuffer > ENV_BUFFER_SIZE) 00186 { 00187 pszPath = (LPTSTR)cmd_realloc (pszPath, dwBuffer * sizeof (TCHAR)); 00188 GetEnvironmentVariable (_T("PATH"), pszPath, dwBuffer); 00189 } 00190 00191 TRACE ("SearchForExecutable(): Loaded PATH: %s\n", debugstr_aw(pszPath)); 00192 00193 /* search in PATH */ 00194 pCh = _tcstok(pszPath, _T(";")); 00195 while (pCh) 00196 { 00197 if (SearchForExecutableSingle(pFileName, pFullName, pszPathExt, pCh)) 00198 { 00199 cmd_free(pszPath); 00200 cmd_free(pszPathExt); 00201 return TRUE; 00202 } 00203 pCh = _tcstok(NULL, _T(";")); 00204 } 00205 00206 cmd_free(pszPath); 00207 cmd_free(pszPathExt); 00208 return FALSE; 00209 } 00210 00211 /* EOF */ Generated on Fri May 25 2012 04:16:34 for ReactOS by
1.7.6.1
|