ReactOS  0.4.14-dev-604-gcfdd483
subst.c File Reference
#include <stdio.h>
#include <windef.h>
#include <winbase.h>
#include <conutils.h>
#include "resource.h"
Include dependency graph for subst.c:

Go to the source code of this file.

Macros

#define WIN32_NO_STATUS
 

Functions

VOID PrintError (IN DWORD ErrCode)
 
ULONG QuerySubstedDrive (IN WCHAR DriveLetter, IN OUT PWSTR *TargetPath OPTIONAL, IN OUT PULONG Size)
 
VOID DumpSubstedDrives (VOID)
 
INT DeleteSubst (IN PWSTR Drive)
 
INT AddSubst (IN PWSTR Drive, IN PWSTR Path)
 
int wmain (int argc, WCHAR *argv[])
 

Macro Definition Documentation

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 15 of file subst.c.

Function Documentation

◆ AddSubst()

INT AddSubst ( IN PWSTR  Drive,
IN PWSTR  Path 
)

Definition at line 216 of file subst.c.

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 
255 Quit:
256  switch (dwResult)
257  {
258  case ERROR_SUCCESS:
259  break;
260 
261  case ERROR_INVALID_DRIVE:
263  {
265  return 1;
266  }
267 
268  case ERROR_IS_SUBSTED:
269  {
271  return 1;
272  }
273 
276  {
278  return 1;
279  }
280 
281  case ERROR_ACCESS_DENIED:
282  {
284  return 1;
285  }
286 
287  default:
288  {
290  return 1;
291  }
292  }
293 
294  return 0;
295 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
ULONG QuerySubstedDrive(IN WCHAR DriveLetter, IN OUT PWSTR *TargetPath OPTIONAL, IN OUT PULONG Size)
Definition: subst.c:52
VOID PrintError(IN DWORD ErrCode)
Definition: subst.c:25
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:802
PWCHAR Drive
Definition: chkdsk.c:73
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
#define IDS_DRIVE_ALREADY_SUBSTED
Definition: resource.h:13
#define IDS_PATH_NOT_FOUND
Definition: resource.h:11
unsigned long DWORD
Definition: ntddk_ex.h:95
#define StdErr
Definition: stream.h:77
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1250
#define ERROR_INVALID_DRIVE
Definition: winerror.h:118
#define IDS_ACCESS_DENIED
Definition: resource.h:12
BOOL WINAPI DefineDosDeviceW(DWORD dwFlags, LPCWSTR lpDeviceName, LPCWSTR lpTargetPath)
Definition: dosdev.c:232
PRTL_UNICODE_STRING_BUFFER Path
#define ERROR_PATH_NOT_FOUND
Definition: winerror.h:106
#define IDS_INVALID_PARAMETER2
Definition: resource.h:10
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ERROR_IS_SUBSTED
Definition: winerror.h:207

Referenced by wmain().

◆ DeleteSubst()

INT DeleteSubst ( IN PWSTR  Drive)

Definition at line 166 of file subst.c.

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 
187 Quit:
188  switch (dwResult)
189  {
190  case ERROR_SUCCESS:
191  break;
192 
193  // case ERROR_INVALID_DRIVE:
195  {
197  return 1;
198  }
199 
200  case ERROR_ACCESS_DENIED:
201  {
203  return 1;
204  }
205 
206  default:
207  {
209  return 1;
210  }
211  }
212 
213  return 0;
214 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
#define ERROR_SUCCESS
Definition: deptool.c:10
#define DDD_REMOVE_DEFINITION
Definition: winbase.h:505
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
ULONG QuerySubstedDrive(IN WCHAR DriveLetter, IN OUT PWSTR *TargetPath OPTIONAL, IN OUT PULONG Size)
Definition: subst.c:52
VOID PrintError(IN DWORD ErrCode)
Definition: subst.c:25
#define ERROR_ACCESS_DENIED
Definition: compat.h:87
PWCHAR Drive
Definition: chkdsk.c:73
smooth NULL
Definition: ftsmooth.c:416
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
unsigned long DWORD
Definition: ntddk_ex.h:95
#define StdErr
Definition: stream.h:77
static const WCHAR L[]
Definition: oid.c:1250
#define IDS_ACCESS_DENIED
Definition: resource.h:12
BOOL WINAPI DefineDosDeviceW(DWORD dwFlags, LPCWSTR lpDeviceName, LPCWSTR lpTargetPath)
Definition: dosdev.c:232
#define IDS_INVALID_PARAMETER2
Definition: resource.h:10
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define ERROR_IS_SUBSTED
Definition: winerror.h:207

Referenced by wmain().

◆ DumpSubstedDrives()

VOID DumpSubstedDrives ( VOID  )

Definition at line 140 of file subst.c.

141 {
142  WCHAR DriveLetter;
143  PWSTR lpTargetPath = NULL;
144  DWORD dwSize;
145  UCHAR i = 0;
146 
147  dwSize = MAX_PATH;
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 }
uint16_t * PWSTR
Definition: typedefs.h:54
ULONG QuerySubstedDrive(IN WCHAR DriveLetter, IN OUT PWSTR *TargetPath OPTIONAL, IN OUT PULONG Size)
Definition: subst.c:52
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
INT __cdecl ConPrintf(IN PCON_STREAM Stream, IN LPWSTR szStr,...)
Definition: outstream.c:520
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned char UCHAR
Definition: xmlstorage.h:181
static const WCHAR L[]
Definition: oid.c:1250
#define StdOut
Definition: stream.h:76
#define HeapFree(x, y, z)
Definition: compat.h:402
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define ERROR_IS_SUBSTED
Definition: winerror.h:207

Referenced by wmain().

◆ PrintError()

VOID PrintError ( IN DWORD  ErrCode)

Definition at line 25 of file subst.c.

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 }
#define FORMAT_MESSAGE_MAX_WIDTH_MASK
Definition: winbase.h:406
#define ERROR_SUCCESS
Definition: deptool.c:10
uint16_t * PWSTR
Definition: typedefs.h:54
DWORD WINAPI FormatMessageW(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR lpBuffer, DWORD nSize, __ms_va_list *args)
Definition: format_msg.c:583
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
#define IDS_FAILED_WITH_ERRORCODE
Definition: resource.h:6
smooth NULL
Definition: ftsmooth.c:416
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
#define StdErr
Definition: stream.h:77
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:401
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50

Referenced by AddSubst(), and DeleteSubst().

◆ QuerySubstedDrive()

ULONG QuerySubstedDrive ( IN WCHAR  DriveLetter,
IN OUT PWSTR *TargetPath  OPTIONAL,
IN OUT PULONG  Size 
)

Definition at line 52 of file subst.c.

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 */
67  dwSize = MAX_PATH;
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 
82 Retry:
83  /* Try querying DOS device information */
84  CharCount = QueryDosDeviceW(Drive, lpTargetPath, dwSize);
85  if (!CharCount)
86  Result = GetLastError();
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 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
uint16_t * PWSTR
Definition: typedefs.h:54
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
PWCHAR Drive
Definition: chkdsk.c:73
smooth NULL
Definition: ftsmooth.c:416
DWORD WINAPI QueryDosDeviceW(LPCWSTR lpDeviceName, LPWSTR lpTargetPath, DWORD ucchMax)
Definition: dosdev.c:542
_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:426
IN PSCSI_REQUEST_BLOCK IN OUT NTSTATUS IN OUT BOOLEAN * Retry
Definition: class2.h:49
#define GetProcessHeap()
Definition: compat.h:403
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
_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)
static const WCHAR L[]
Definition: oid.c:1250
#define ERROR_INVALID_DRIVE
Definition: winerror.h:118
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
#define HeapReAlloc
Definition: compat.h:401
unsigned int ULONG
Definition: retypes.h:1
#define HeapFree(x, y, z)
Definition: compat.h:402
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define ERROR_IS_SUBSTED
Definition: winerror.h:207
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by AddSubst(), DeleteSubst(), and DumpSubstedDrives().

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 297 of file subst.c.

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 }
INT DeleteSubst(IN PWSTR Drive)
Definition: subst.c:166
static int argc
Definition: ServiceArgs.c:12
INT AddSubst(IN PWSTR Drive, IN PWSTR Path)
Definition: subst.c:216
VOID DumpSubstedDrives(VOID)
Definition: subst.c:140
#define IDS_USAGE
Definition: resource.h:3
#define IDS_INCORRECT_PARAMETER_COUNT
Definition: resource.h:8
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:56
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
#define ConInitStdStreams()
Definition: stream.h:122
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
#define IDS_INVALID_PARAMETER
Definition: resource.h:9
#define StdErr
Definition: stream.h:77
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1250
#define StdOut
Definition: stream.h:76
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)