ReactOS 0.4.16-dev-91-g764881a
subst.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Subst Command
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/subst/subst.c
5 * PURPOSE: Maps a path with a drive letter
6 * PROGRAMMERS: Sam Arun Raj
7 * Peter Hater
8 * Hermes Belusca-Maito
9 */
10
11/* INCLUDES *****************************************************************/
12
13#include <stdio.h>
14
15#define WIN32_NO_STATUS
16#include <windef.h>
17#include <winbase.h>
18
19#include <conutils.h>
20
21#include "resource.h"
22
23/* FUNCTIONS ****************************************************************/
24
26{
27 // DWORD dwLength = 0;
28 PWSTR pMsgBuf = NULL;
29
30#if 0
31 if (ErrCode == ERROR_SUCCESS)
32 return;
33#endif
34
35 /* Retrieve the message string without appending extra newlines */
36 // dwLength =
39 NULL,
40 ErrCode,
42 (PWSTR)&pMsgBuf,
43 0, NULL);
44 if (pMsgBuf /* && dwLength */)
45 {
47 ErrCode, pMsgBuf);
48 LocalFree(pMsgBuf);
49 }
50}
51
53 IN OUT PWSTR* TargetPath OPTIONAL,
55{
57 WCHAR Drive[] = L"A:";
58 DWORD dwSize, CharCount = 0;
59 PWSTR lpTargetPath = NULL, tmp;
60
61 Drive[0] = DriveLetter;
62
63 /* Check whether the user has given a pointer to a target path buffer */
64 if (!TargetPath)
65 {
66 /* No, therefore use a local buffer */
68 lpTargetPath = (PWSTR)HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
69 if (!lpTargetPath)
71 }
72 else
73 {
74 /* Just use the user-given pointer to a buffer; Size should point to a valid ULONG */
75 if (!Size)
77
78 lpTargetPath = *TargetPath;
79 dwSize = *Size;
80 }
81
82Retry:
83 /* Try querying DOS device information */
84 CharCount = QueryDosDeviceW(Drive, lpTargetPath, dwSize);
85 if (!CharCount)
87
88 if (!CharCount && (Result == ERROR_INSUFFICIENT_BUFFER))
89 {
90 /* Reallocate the buffer with double size */
91 dwSize *= 2;
92 tmp = (PWSTR)HeapReAlloc(GetProcessHeap(), 0, lpTargetPath, dwSize * sizeof(WCHAR));
93 if (!tmp)
94 {
95 /* Memory problem, bail out */
96 CharCount = 0;
98 }
99 else
100 {
101 /* Retry again */
102 lpTargetPath = tmp;
103 goto Retry;
104 }
105 }
106
107 if (CharCount)
108 {
109 if ( wcsncmp(lpTargetPath, L"\\??\\", 4) == 0 &&
110 ( (lpTargetPath[4] >= L'A' && lpTargetPath[4] <= L'Z') ||
111 (lpTargetPath[4] >= L'a' && lpTargetPath[4] <= L'z') ) )
112 {
113 /* The drive exists and is SUBSTed */
115 }
116#if 0
117 else
118 {
119 /* The drive exists but is not SUBSTed */
121 }
122#endif
123 }
124
125 if (!TargetPath)
126 {
127 /* Free the local buffer */
128 HeapFree(GetProcessHeap(), 0, lpTargetPath);
129 }
130 else
131 {
132 /* Update the user-given pointers */
133 *TargetPath = lpTargetPath;
134 *Size = dwSize;
135 }
136
137 return Result;
138}
139
141{
142 WCHAR DriveLetter;
143 PWSTR lpTargetPath = NULL;
145 UCHAR i = 0;
146
148 lpTargetPath = (PWSTR)HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
149 if (!lpTargetPath)
150 return;
151
152 while (i < 26)
153 {
154 DriveLetter = L'A' + i;
155 if (QuerySubstedDrive(DriveLetter, &lpTargetPath, &dwSize) == ERROR_IS_SUBSTED)
156 {
157 ConPrintf(StdOut, L"%c:\\: => %s\n", DriveLetter, lpTargetPath + 4);
158 }
159
160 i++;
161 }
162
163 HeapFree(GetProcessHeap(), 0, lpTargetPath);
164}
165
167{
168 DWORD dwResult;
169
170 if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
171 {
172 dwResult = ERROR_INVALID_PARAMETER;
173 goto Quit;
174 }
175
177 {
178 dwResult = ERROR_INVALID_PARAMETER;
179 goto Quit;
180 }
181
183 dwResult = GetLastError();
184 else
185 dwResult = ERROR_SUCCESS;
186
187Quit:
188 switch (dwResult)
189 {
190 case ERROR_SUCCESS:
191 break;
192
193 // case ERROR_INVALID_DRIVE:
195 {
197 return 1;
198 }
199
201 {
203 return 1;
204 }
205
206 default:
207 {
209 return 1;
210 }
211 }
212
213 return 0;
214}
215
217{
218 DWORD dwResult, dwPathAttr;
219
220 if ((wcslen(Drive) != 2) || (Drive[1] != L':'))
221 {
222 dwResult = ERROR_INVALID_PARAMETER;
223 goto Quit;
224 }
225
226 /*
227 * Even if DefineDosDevice allows to map files to drive letters (yes yes!!)
228 * it is not the purpose of SUBST to allow that. Therefore check whether
229 * the given path exists and really is a path to a directory, and if not,
230 * just fail with an error.
231 */
232 dwPathAttr = GetFileAttributesW(Path);
233 if ( (dwPathAttr == INVALID_FILE_ATTRIBUTES) ||
234 !(dwPathAttr & FILE_ATTRIBUTE_DIRECTORY) )
235 {
236 dwResult = ERROR_PATH_NOT_FOUND;
237 goto Quit;
238 }
239
240 /*
241 * QuerySubstedDrive (via QueryDosDevice) returns ERROR_FILE_NOT_FOUND only
242 * if there is no already existing drive mapping. For all other results
243 * (existing drive, be it already subst'ed or not, or other errors...)
244 * no attempt at defining a drive mapping should be done.
245 */
246 dwResult = QuerySubstedDrive(Drive[0], NULL, NULL);
247 if (dwResult != ERROR_FILE_NOT_FOUND)
248 goto Quit;
249
250 if (!DefineDosDeviceW(0, Drive, Path))
251 dwResult = GetLastError();
252 else
253 dwResult = ERROR_SUCCESS;
254
255Quit:
256 switch (dwResult)
257 {
258 case ERROR_SUCCESS:
259 break;
260
263 {
265 return 1;
266 }
267
268 case ERROR_IS_SUBSTED:
269 {
271 return 1;
272 }
273
276 {
278 return 1;
279 }
280
282 {
284 return 1;
285 }
286
287 default:
288 {
290 return 1;
291 }
292 }
293
294 return 0;
295}
296
297int wmain(int argc, WCHAR* argv[])
298{
299 INT i;
300
301 /* Initialize the Console Standard Streams */
303
304 for (i = 0; i < argc; i++)
305 {
306 if (!_wcsicmp(argv[i], L"/?"))
307 {
309 return 0;
310 }
311 }
312
313 if (argc < 3)
314 {
315 if (argc >= 2)
316 {
318 return 1;
319 }
321 return 0;
322 }
323
324 if (argc > 3)
325 {
327 return 1;
328 }
329
330 if (!_wcsicmp(argv[1], L"/D"))
331 return DeleteSubst(argv[2]);
332 if (!_wcsicmp(argv[2], L"/D"))
333 return DeleteSubst(argv[1]);
334 return AddSubst(argv[1], argv[2]);
335}
336
337/* EOF */
PRTL_UNICODE_STRING_BUFFER Path
static int argc
Definition: ServiceArgs.c:12
#define IDS_USAGE
Definition: resource.h:3
#define ConInitStdStreams()
Definition: fc.c:13
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define StdOut
Definition: fc.c:14
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define StdErr
Definition: fc.c:15
void ConResPuts(FILE *fp, UINT nID)
Definition: fc.c:27
PWCHAR Drive
Definition: chkdsk.c:73
#define IDS_PATH_NOT_FOUND
Definition: resource.h:11
#define IDS_INCORRECT_PARAMETER_COUNT
Definition: resource.h:8
#define IDS_FAILED_WITH_ERRORCODE
Definition: resource.h:6
#define IDS_INVALID_PARAMETER
Definition: resource.h:9
#define IDS_INVALID_PARAMETER2
Definition: resource.h:10
#define IDS_DRIVE_ALREADY_SUBSTED
Definition: resource.h:13
#define IDS_ACCESS_DENIED
Definition: resource.h:12
_In_ PSCSI_REQUEST_BLOCK _Out_ NTSTATUS _Inout_ BOOLEAN * Retry
Definition: classpnp.h:312
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define HeapAlloc
Definition: compat.h:733
#define HeapReAlloc
Definition: compat.h:734
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
BOOL WINAPI DefineDosDeviceW(DWORD dwFlags, LPCWSTR lpDeviceName, LPCWSTR lpTargetPath)
Definition: dosdev.c:232
DWORD WINAPI QueryDosDeviceW(LPCWSTR lpDeviceName, LPWSTR lpTargetPath, DWORD ucchMax)
Definition: dosdev.c:542
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:652
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
#define argv
Definition: mplay32.c:18
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define L(x)
Definition: ntvdm.h:50
int wmain()
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
VOID DumpSubstedDrives(VOID)
Definition: subst.c:140
ULONG QuerySubstedDrive(IN WCHAR DriveLetter, IN OUT PWSTR *TargetPath OPTIONAL, IN OUT PULONG Size)
Definition: subst.c:52
INT DeleteSubst(IN PWSTR Drive)
Definition: subst.c:166
INT AddSubst(IN PWSTR Drive, IN PWSTR Path)
Definition: subst.c:216
VOID PrintError(IN DWORD ErrCode)
Definition: subst.c:25
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50
uint16_t * PWSTR
Definition: typedefs.h:56
uint32_t * PULONG
Definition: typedefs.h:59
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define FORMAT_MESSAGE_MAX_WIDTH_MASK
Definition: winbase.h:425
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:420
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:423
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:419
#define DDD_REMOVE_DEFINITION
Definition: winbase.h:524
#define ERROR_IS_SUBSTED
Definition: winerror.h:207
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
#define ERROR_INVALID_DRIVE
Definition: winerror.h:118
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180