1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: SearchPathW.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the SearchFileW function. |
10 | ** |
11 | ** |
12 | ** TODO: Write a test where complete path is passed (say c:\?) |
13 | **===================================================================*/ |
14 | //SearchPath |
15 | // |
16 | //The SearchPath function searches for the specified file in the specified path. |
17 | // |
18 | // |
19 | //DWORD SearchPath( |
20 | // LPCTSTR lpPath, |
21 | // LPCTSTR lpFileName, |
22 | // LPCTSTR lpExtension, |
23 | // DWORD nBufferLength, |
24 | // LPTSTR lpBuffer, |
25 | // LPTSTR* lpFilePart |
26 | //); |
27 | // |
28 | //Parameters |
29 | //lpPath |
30 | //[in] Pointer to a null-terminated string that specifies the path to be searched for the file. If this parameter is NULL, the function searches for a matching file in the following directories in the following sequence: |
31 | //The directory from which the application loaded. |
32 | //The current directory. |
33 | //The system directory. Use the GetSystemDirectory function to get the path of this directory. |
34 | //The 16-bit system directory. There is no function that retrieves the path of this directory, but it is searched. |
35 | //The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. |
36 | //The directories that are listed in the PATH environment variable. |
37 | |
38 | //lpFileName |
39 | //[in] Pointer to a null-terminated string that specifies the name of the file to search for. |
40 | |
41 | //lpExtension |
42 | //[in] Pointer to a null-terminated string that specifies an extension to be added to the file name when searching for the file. The first character of the file name extension must be a period (.). The extension is added only if the specified file name does not end with an extension. |
43 | //If a file name extension is not required or if the file name contains an extension, this parameter can be NULL. |
44 | // |
45 | //nBufferLength |
46 | //[in] Size of the buffer that receives the valid path and file name, in TCHARs. |
47 | |
48 | //lpBuffer |
49 | //[out] Pointer to the buffer that receives the path and file name of the file found. |
50 | |
51 | //lpFilePart |
52 | //[out] Pointer to the variable that receives the address (within lpBuffer) of the last component of the valid path and file name, which is the address of the character immediately following the final backslash (\) in the path. |
53 | |
54 | //Return Values |
55 | //If the function succeeds, the value returned is the length, in TCHARs, of the string copied to the buffer, not including the terminating null character. If the return value is greater than nBufferLength, the value returned is the size of the buffer required to hold the path. |
56 | // |
57 | //If the function fails, the return value is zero. To get extended error information, call GetLastError. |
58 | |
59 | |
60 | #include <palsuite.h> |
61 | const char* szDir = "." ; |
62 | |
63 | const char* szNoFileName = "333asdf" ; |
64 | const char* szNoFileNameExt = ".x77t" ; |
65 | |
66 | const char* szFileNameExists = "searchpathw" ; |
67 | const char* szFileNameExtExists = ".c" ; |
68 | |
69 | const char* szFileNameExistsWithExt = "searchpathw.c" ; |
70 | |
71 | char fileloc[_MAX_PATH]; |
72 | |
73 | void removeFileHelper(LPSTR pfile, int location) |
74 | { |
75 | FILE *fp; |
76 | fp = fopen( pfile, "r" ); |
77 | |
78 | if (fp != NULL) |
79 | { |
80 | if(fclose(fp)) |
81 | { |
82 | Fail("ERROR: Failed to close the file [%s], Error Code [%d], location [%d]\n" , pfile, GetLastError(), location); |
83 | } |
84 | |
85 | if(!DeleteFileA(pfile)) |
86 | { |
87 | Fail("ERROR: Failed to delete file [%s], Error Code [%d], location [%d]\n" , pfile, GetLastError(), location); |
88 | } |
89 | else |
90 | { |
91 | // Trace("Success: deleted file [%S], Error Code [%d], location [%d]\n", wfile, GetLastError(), location); |
92 | } |
93 | } |
94 | |
95 | } |
96 | |
97 | void RemoveAll() |
98 | { |
99 | removeFileHelper(fileloc, 1); |
100 | } |
101 | |
102 | int __cdecl main(int argc, char *argv[]) { |
103 | |
104 | WCHAR* lpPath = NULL; |
105 | WCHAR* lpFileName = NULL; |
106 | WCHAR* lpExtension = NULL; |
107 | DWORD nBufferLength = 0; |
108 | WCHAR lpBuffer[_MAX_PATH]; |
109 | WCHAR** lpFilePart = NULL; |
110 | DWORD error = 0; |
111 | DWORD result = 0; |
112 | |
113 | HANDLE hsearchfile; |
114 | char fname[_MAX_FNAME]; |
115 | char ext[_MAX_EXT]; |
116 | char fullPath[_MAX_DIR]; |
117 | char drive[_MAX_DRIVE]; |
118 | char dir[_MAX_DIR]; |
119 | |
120 | |
121 | if(0 != (PAL_Initialize(argc, argv))) |
122 | { |
123 | return FAIL; |
124 | } |
125 | |
126 | /* Initalize the buffer. |
127 | */ |
128 | memset(fullPath, 0, _MAX_DIR); |
129 | |
130 | if (GetTempPathA(_MAX_DIR, fullPath) == 0) |
131 | { |
132 | Fail("ERROR: GetTempPathA failed to get a path\n" ); |
133 | } |
134 | |
135 | memset(fileloc, 0, _MAX_PATH); |
136 | sprintf_s(fileloc, _countof(fileloc), "%s%s" , fullPath, szFileNameExistsWithExt); |
137 | |
138 | RemoveAll(); |
139 | |
140 | hsearchfile = CreateFileA(fileloc, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, |
141 | FILE_ATTRIBUTE_NORMAL, 0); |
142 | |
143 | if (hsearchfile == NULL) |
144 | { |
145 | Trace("ERROR[%ul]: couldn't create %s\n" , GetLastError(), fileloc); |
146 | return FAIL; |
147 | } |
148 | |
149 | CloseHandle(hsearchfile); |
150 | |
151 | // |
152 | // find a file that doesn't exist |
153 | // |
154 | ZeroMemory( lpBuffer, sizeof(lpBuffer)); |
155 | lpPath = convert((LPSTR)fullPath); |
156 | lpFileName = convert((LPSTR)szNoFileName); |
157 | lpExtension = NULL; |
158 | |
159 | if( SearchPathW( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart) != 0 ){ |
160 | error = GetLastError(); |
161 | free(lpPath); |
162 | free(lpFileName); |
163 | Fail ("SearchPathW: ERROR1 -> Found invalid file[%s][%s][%s][%d]\n" , lpPath, szNoFileName, szNoFileNameExt, error); |
164 | } |
165 | |
166 | free(lpPath); |
167 | free(lpFileName); |
168 | |
169 | // |
170 | // find a file that exists, when path is mentioned explicitly |
171 | // |
172 | ZeroMemory( lpBuffer, sizeof(lpBuffer)); |
173 | lpPath = convert((LPSTR)fullPath); |
174 | lpFileName = convert((LPSTR)szFileNameExistsWithExt); |
175 | lpExtension = NULL; |
176 | |
177 | result = SearchPathW( lpPath, lpFileName, lpExtension, nBufferLength, lpBuffer, lpFilePart); |
178 | |
179 | if( result == 0 ){ |
180 | error = GetLastError(); |
181 | free(lpPath); |
182 | free(lpFileName); |
183 | Fail ("SearchPathA: ERROR2 -> Did not Find valid file[%s][%s][%d]\n" , lpPath, szFileNameExistsWithExt, error); |
184 | } |
185 | |
186 | free(lpPath); |
187 | free(lpFileName); |
188 | |
189 | RemoveAll(); |
190 | |
191 | PAL_Terminate(); |
192 | return PASS; |
193 | } |
194 | |