more crt, crtdll and msvcrt cleanup
[reactos.git] / reactos / lib / crt / direct / getdcwd.c
1 #include "precomp.h"
2 #include <direct.h>
3 #include <internal/debug.h>
4 #include <tchar.h>
5
6 /*
7 * @implemented
8 *
9 * _getdcwd (MSVCRT.@)
10 *
11 * Get the current working directory on a given disk.
12 *
13 * PARAMS
14 * drive [I] Drive letter to get the current working directory from.
15 * buf [O] Destination for the current working directory.
16 * size [I] Length of drive in characters.
17 *
18 * RETURNS
19 * Success: If drive is NULL, returns an allocated string containing the path.
20 * Otherwise populates drive with the path and returns it.
21 * Failure: NULL. errno indicates the error.
22 */
23 _TCHAR* _tgetdcwd(int drive, _TCHAR * buf, int size)
24 {
25 static _TCHAR* dummy;
26
27 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
28
29 if (!drive || drive == _getdrive())
30 return _tgetcwd(buf,size); /* current */
31 else
32 {
33 _TCHAR dir[MAX_PATH];
34 _TCHAR drivespec[] = _T("A:");
35 int dir_len;
36
37 drivespec[0] += drive - 1;
38 if (GetDriveType(drivespec) < DRIVE_REMOVABLE)
39 {
40 __set_errno(EACCES);
41 return NULL;
42 }
43
44 /* GetFullPathName for X: means "get working directory on drive X",
45 * just like passing X: to SetCurrentDirectory means "switch to working
46 * directory on drive X". -Gunnar */
47 dir_len = GetFullPathName(drivespec,MAX_PATH,dir,&dummy);
48 if (dir_len >= size || dir_len < 1)
49 {
50 __set_errno(ERANGE);
51 return NULL; /* buf too small */
52 }
53
54 TRACE(":returning '%s'\n", dir);
55 if (!buf)
56 return _tcsdup(dir); /* allocate */
57
58 _tcscpy(buf,dir);
59 }
60 return buf;
61 }
62