ReactOS 0.4.16-dev-965-gf669426
drive.cpp File Reference
#include <corecrt_internal.h>
#include <ctype.h>
#include <direct.h>
#include <errno.h>
Include dependency graph for drive.cpp:

Go to the source code of this file.

Functions

static int __cdecl get_drive_number_from_path (wchar_t const *const path) throw ()
 
int __cdecl _getdrive ()
 
int __cdecl _chdrive (int const drive_number)
 

Function Documentation

◆ _chdrive()

int __cdecl _chdrive ( int const  drive_number)

Definition at line 58 of file drive.cpp.

59{
60 if (drive_number < 1 || drive_number > 26)
61 {
63 _VALIDATE_RETURN(("Invalid Drive Index", 0), EACCES, -1);
64 }
65
66#pragma warning(suppress:__WARNING_UNUSED_ASSIGNMENT) // 28931 unused assignment of variable drive_letter
67 wchar_t const drive_letter = static_cast<wchar_t>(L'A' + drive_number - 1);
68 wchar_t const drive_string[] = { drive_letter, L':', L'\0' };
69
70 if (!SetCurrentDirectoryW(drive_string))
71 {
73 return -1;
74 }
75
76 return 0;
77}
#define EACCES
Definition: acclib.h:85
void __cdecl __acrt_errno_map_os_error(unsigned long)
Definition: errno.cpp:91
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
BOOL WINAPI SetCurrentDirectoryW(IN LPCWSTR lpPathName)
Definition: path.c:2249
#define _doserrno
Definition: stdlib.h:131
__u8 drive_number
Definition: mkdosfs.c:0
#define L(x)
Definition: ntvdm.h:50
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_INVALID_DRIVE
Definition: winerror.h:118

◆ _getdrive()

int __cdecl _getdrive ( void  )

Definition at line 25 of file drive.cpp.

26{
27 wchar_t cwd[MAX_PATH + 1] = { 0 };
28
29 DWORD const cwd_length = GetCurrentDirectoryW(MAX_PATH + 1, cwd);
30 if (cwd_length <= MAX_PATH)
31 {
33 }
34
35 // Otherwise, we need more space for the path, so we'll need to dynamically
36 // allocate a buffer:
37 __crt_unique_heap_ptr<wchar_t> const heap_cwd(_calloc_crt_t(wchar_t, cwd_length + 1));
38 if (heap_cwd.get() == nullptr)
39 {
40 errno = ENOMEM;
41 return 0;
42 }
43
44 DWORD const heap_cwd_length = GetCurrentDirectoryW(cwd_length + 1, heap_cwd.get());
45 if (heap_cwd_length == 0)
46 {
47 errno = ENOMEM;
48 return 0;
49 }
50
51 return get_drive_number_from_path(heap_cwd.get());
52}
#define ENOMEM
Definition: acclib.h:84
#define GetCurrentDirectoryW(x, y)
Definition: compat.h:756
#define MAX_PATH
Definition: compat.h:34
unsigned long DWORD
Definition: ntddk_ex.h:95
#define errno
Definition: errno.h:18
static int __cdecl get_drive_number_from_path(wchar_t const *const path)
Definition: drive.cpp:13

Referenced by _tgetdcwd(), _tstat64(), cmd_setlocal(), get_drive_number_from_path(), test_utf8(), and throw().

◆ get_drive_number_from_path()

static int __cdecl get_drive_number_from_path ( wchar_t const *const  path)
throw (
)
static

Definition at line 13 of file drive.cpp.

14{
15 if (path[0] == L'\0' || path[1] != L':')
16 return 0;
17
18 return __ascii_towupper(path[0]) - L'A' + 1;
19}
__forceinline int __CRTDECL __ascii_towupper(int const _C)
Definition: ctype.h:185

Referenced by _getdrive().