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 *****************************************************************/
10 #define WIN32_NO_STATUS
23 /* FUNCTIONS ****************************************************************/
25 void PrintError(DWORD ErrCode
)
27 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
28 TCHAR
*buffer
= (TCHAR
*) calloc(2048,
34 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
37 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
41 LoadString(GetModuleHandle(NULL
),
42 IDS_FAILED_WITH_ERROCODE
,
44 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
58 void DisplaySubstUsage(void)
60 TCHAR szHelp
[RC_STRING_MAX_SIZE
] = {0};
62 LoadString(GetModuleHandle(NULL
),
65 sizeof(szHelp
) / sizeof(szHelp
[0]));
66 _tprintf(_T("%s"), szHelp
);
69 BOOLEAN
IsSubstedDrive(TCHAR
*Drive
)
71 BOOLEAN Result
= FALSE
;
72 LPTSTR lpTargetPath
= NULL
;
73 DWORD CharCount
, dwSize
;
75 if (_tcslen(Drive
) > 2)
78 dwSize
= sizeof(TCHAR
) * MAX_PATH
;
79 lpTargetPath
= (LPTSTR
) malloc(sizeof(TCHAR
) * MAX_PATH
);
82 CharCount
= QueryDosDevice(Drive
,
84 dwSize
/ sizeof(TCHAR
));
86 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
90 lpTargetPath
= (LPTSTR
) malloc(dwSize
);
93 CharCount
= QueryDosDevice(Drive
,
95 dwSize
/ sizeof(TCHAR
));
101 if ( _tcsncmp(lpTargetPath
, _T("\\??\\"), 4) == 0 &&
102 ( (lpTargetPath
[4] >= _T('A') &&
103 lpTargetPath
[4] <= _T('Z')) ||
104 (lpTargetPath
[4] >= _T('a') &&
105 lpTargetPath
[4] <= _T('z')) ) )
115 void DumpSubstedDrives(void)
117 TCHAR Drive
[3] = _T("A:");
118 LPTSTR lpTargetPath
= NULL
;
119 DWORD CharCount
, dwSize
;
122 dwSize
= sizeof(TCHAR
) * MAX_PATH
;
123 lpTargetPath
= (LPTSTR
) malloc(sizeof(TCHAR
) * MAX_PATH
);
129 Drive
[0] = _T('A') + i
;
130 CharCount
= QueryDosDevice(Drive
,
132 dwSize
/ sizeof(TCHAR
));
133 while (! CharCount
&&
134 GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
138 lpTargetPath
= (LPTSTR
) malloc(dwSize
);
141 CharCount
= QueryDosDevice(Drive
,
143 dwSize
/ sizeof(TCHAR
));
154 if ( _tcsncmp(lpTargetPath
, _T("\\??\\"), 4) == 0 &&
155 ( (lpTargetPath
[4] >= _T('A') &&
156 lpTargetPath
[4] <= _T('Z')) ||
157 (lpTargetPath
[4] >= _T('a') &&
158 lpTargetPath
[4] <= _T('z')) ) )
160 _tprintf(_T("%s\\: => %s\n"),
170 int DeleteSubst(TCHAR
* Drive
)
173 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
175 LoadString(GetModuleHandle(NULL
),
176 IDS_INVALID_PARAMETER2
,
178 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
180 if (_tcslen(Drive
) > 2)
182 _tprintf(szFmtString
,
187 if (! IsSubstedDrive(Drive
))
189 _tprintf(szFmtString
,
194 Result
= DefineDosDevice(DDD_REMOVE_DEFINITION
,
199 PrintError(GetLastError());
205 int AddSubst(TCHAR
* Drive
, TCHAR
*Path
)
208 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
210 LoadString(GetModuleHandle(NULL
),
211 IDS_INVALID_PARAMETER2
,
213 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
214 if (_tcslen(Drive
) != 2)
216 _tprintf(szFmtString
,
221 if (Drive
[1] != _T(':'))
223 _tprintf(szFmtString
,
228 if (IsSubstedDrive(Drive
))
230 LoadString(GetModuleHandle(NULL
),
231 IDS_DRIVE_ALREAD_SUBSTED
,
233 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
234 _tprintf(szFmtString
);
238 Result
= DefineDosDevice(0,
243 PrintError(GetLastError());
249 int _tmain(int argc
, TCHAR
* argv
[])
252 TCHAR szFmtString
[RC_STRING_MAX_SIZE
] = {0};
254 for (i
= 0; i
< argc
; i
++)
256 if (!_tcsicmp(argv
[i
], _T("/?")))
267 LoadString(GetModuleHandle(NULL
),
268 IDS_INVALID_PARAMETER
,
270 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
271 _tprintf(szFmtString
,
281 LoadString(GetModuleHandle(NULL
),
282 IDS_INCORRECT_PARAMETER_COUNT
,
284 sizeof(szFmtString
) / sizeof(szFmtString
[0]));
285 _tprintf(szFmtString
,
290 if (! _tcsicmp(argv
[1], _T("/D")))
291 return DeleteSubst(argv
[2]);
292 if (! _tcsicmp(argv
[2], _T("/D")))
293 return DeleteSubst(argv
[1]);
294 return AddSubst(argv
[1], argv
[2]);