1 /* PROJECT: ReactOS Kernel
2 * LICENSE: GPL - See COPYING in the top level directory
3 * FILE: base/system/subst/subst.c
4 * PURPOSE: Associates a path with a drive letter
5 * PROGRAMMERS: Sam Arun Raj
8 /* INCLUDES *****************************************************************/
19 /* FUNCTIONS ****************************************************************/
21 void PrintError(DWORD ErrCode
)
23 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
24 TCHAR
*buffer
= (TCHAR
*) calloc(2048,
30 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
33 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
37 LoadString(GetModuleHandle(NULL
),
38 IDS_FAILED_WITH_ERROCODE
,
40 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
54 void DisplaySubstUsage(void)
56 TCHAR szHelp
[RC_STRING_MAX_SIZE
] = {0};
58 LoadString(GetModuleHandle(NULL
),
61 sizeof(szHelp
) / sizeof(szHelp
[0]));
62 _tprintf(_T("%s"), szHelp
);
65 BOOLEAN
IsSubstedDrive(TCHAR
*Drive
)
67 BOOLEAN Result
= FALSE
;
68 LPTSTR lpTargetPath
= NULL
;
69 DWORD CharCount
, dwSize
;
71 if (_tcslen(Drive
) > 2)
74 dwSize
= sizeof(TCHAR
) * MAX_PATH
;
75 lpTargetPath
= (LPTSTR
) malloc(sizeof(TCHAR
) * MAX_PATH
);
78 CharCount
= QueryDosDevice(Drive
,
80 dwSize
/ sizeof(TCHAR
));
82 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
86 lpTargetPath
= (LPTSTR
) malloc(dwSize
);
89 CharCount
= QueryDosDevice(Drive
,
91 dwSize
/ sizeof(TCHAR
));
97 if ( _tcsncmp(lpTargetPath
, _T("\\??\\"), 4) == 0 &&
98 ( (lpTargetPath
[4] >= _T('A') &&
99 lpTargetPath
[4] <= _T('Z')) ||
100 (lpTargetPath
[4] >= _T('a') &&
101 lpTargetPath
[4] <= _T('z')) ) )
111 void DumpSubstedDrives(void)
113 TCHAR Drive
[3] = _T("A:");
114 LPTSTR lpTargetPath
= NULL
;
115 DWORD CharCount
, dwSize
;
118 dwSize
= sizeof(TCHAR
) * MAX_PATH
;
119 lpTargetPath
= (LPTSTR
) malloc(sizeof(TCHAR
) * MAX_PATH
);
125 Drive
[0] = _T('A') + i
;
126 CharCount
= QueryDosDevice(Drive
,
128 dwSize
/ sizeof(TCHAR
));
129 while (! CharCount
&&
130 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
134 lpTargetPath
= (LPTSTR
) malloc(dwSize
);
137 CharCount
= QueryDosDevice(Drive
,
139 dwSize
/ sizeof(TCHAR
));
150 if ( _tcsncmp(lpTargetPath
, _T("\\??\\"), 4) == 0 &&
151 ( (lpTargetPath
[4] >= _T('A') &&
152 lpTargetPath
[4] <= _T('Z')) ||
153 (lpTargetPath
[4] >= _T('a') &&
154 lpTargetPath
[4] <= _T('z')) ) )
156 _tprintf(_T("%s\\: => %s\n"),
166 int DeleteSubst(TCHAR
* Drive
)
169 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
171 LoadString(GetModuleHandle(NULL
),
172 IDS_INVALID_PARAMETER2
,
174 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
176 if (_tcslen(Drive
) > 2)
178 _tprintf(szFmtString
,
183 if (! IsSubstedDrive(Drive
))
185 _tprintf(szFmtString
,
190 Result
= DefineDosDevice(DDD_REMOVE_DEFINITION
,
195 PrintError(GetLastError());
201 int AddSubst(TCHAR
* Drive
, TCHAR
*Path
)
204 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
206 LoadString(GetModuleHandle(NULL
),
207 IDS_INVALID_PARAMETER2
,
209 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
210 if (_tcslen(Drive
) != 2)
212 _tprintf(szFmtString
,
217 if (Drive
[1] != _T(':'))
219 _tprintf(szFmtString
,
224 if (IsSubstedDrive(Drive
))
226 LoadString(GetModuleHandle(NULL
),
227 IDS_DRIVE_ALREAD_SUBSTED
,
229 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
230 _tprintf(szFmtString
);
234 Result
= DefineDosDevice(0,
239 PrintError(GetLastError());
245 int _tmain(int argc
, TCHAR
* argv
[])
248 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
250 for (i
= 0; i
< argc
; i
++)
252 if (!_tcsicmp(argv
[i
], _T("/?")))
263 LoadString(GetModuleHandle(NULL
),
264 IDS_INVALID_PARAMETER
,
266 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
267 _tprintf(szFmtString
,
277 LoadString(GetModuleHandle(NULL
),
278 IDS_INCORRECT_PARAMETER_COUNT
,
280 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
281 _tprintf(szFmtString
,
286 if (! _tcsicmp(argv
[1], _T("/D")))
287 return DeleteSubst(argv
[2]);
288 if (! _tcsicmp(argv
[2], _T("/D")))
289 return DeleteSubst(argv
[1]);
290 return AddSubst(argv
[1], argv
[2]);