2 * msvcrt.dll drive/directory functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
33 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata_t */
40 static void msvcrt_fttofd( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata_t* ft)
44 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
47 ft->attrib = fd->dwFileAttributes;
49 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
51 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
53 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
55 ft->size = fd->nFileSizeLow;
56 strcpy(ft->name, fd->cFileName);
59 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata_t */
60 static void msvcrt_wfttofd( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata_t* ft)
64 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
67 ft->attrib = fd->dwFileAttributes;
69 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
71 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
73 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
75 ft->size = fd->nFileSizeLow;
76 strcpyW(ft->name, fd->cFileName);
79 /* INTERNAL: Translate WIN32_FIND_DATAA to finddatai64_t */
80 static void msvcrt_fttofdi64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddatai64_t* ft)
84 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
87 ft->attrib = fd->dwFileAttributes;
89 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
91 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
93 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
95 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
96 strcpy(ft->name, fd->cFileName);
99 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64_t */
100 static void msvcrt_fttofd64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64_t* ft)
104 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
107 ft->attrib = fd->dwFileAttributes;
109 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
110 ft->time_create = dw;
111 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
112 ft->time_access = dw;
113 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
115 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
116 strcpy(ft->name, fd->cFileName);
119 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64_t */
120 static void msvcrt_wfttofd64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64_t* ft)
124 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
127 ft->attrib = fd->dwFileAttributes;
129 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
130 ft->time_create = dw;
131 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
132 ft->time_access = dw;
133 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
135 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
136 strcpyW(ft->name, fd->cFileName);
139 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64i32_t */
140 static void msvcrt_fttofd64i32( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64i32_t* ft)
144 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
147 ft->attrib = fd->dwFileAttributes;
149 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
150 ft->time_create = dw;
151 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
152 ft->time_access = dw;
153 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
155 ft->size = fd->nFileSizeLow;
156 strcpy(ft->name, fd->cFileName);
159 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddatai64_t */
160 static void msvcrt_wfttofdi64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddatai64_t* ft)
164 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
167 ft->attrib = fd->dwFileAttributes;
169 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
170 ft->time_create = dw;
171 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
172 ft->time_access = dw;
173 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
175 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
176 strcpyW(ft->name, fd->cFileName);
179 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64i32_t */
180 static void msvcrt_wfttofd64i32( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64i32_t* ft)
184 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
187 ft->attrib = fd->dwFileAttributes;
189 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
190 ft->time_create = dw;
191 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
192 ft->time_access = dw;
193 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
195 ft->size = fd->nFileSizeLow;
196 strcpyW(ft->name, fd->cFileName);
199 /*********************************************************************
202 * Change the current working directory.
205 * newdir [I] Directory to change to
208 * Success: 0. The current working directory is set to newdir.
209 * Failure: -1. errno indicates the error.
212 * See SetCurrentDirectoryA.
214 int CDECL MSVCRT__chdir(const char * newdir)
216 if (!SetCurrentDirectoryA(newdir))
218 msvcrt_set_errno(newdir?GetLastError():0);
224 /*********************************************************************
227 * Unicode version of _chdir.
229 int CDECL _wchdir(const MSVCRT_wchar_t * newdir)
231 if (!SetCurrentDirectoryW(newdir))
233 msvcrt_set_errno(newdir?GetLastError():0);
239 /*********************************************************************
240 * _chdrive (MSVCRT.@)
242 * Change the current drive.
245 * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
248 * Success: 0. The current drive is set to newdrive.
249 * Failure: -1. errno indicates the error.
252 * See SetCurrentDirectoryA.
254 int CDECL _chdrive(int newdrive)
256 WCHAR buffer[3] = {'A', ':', 0};
258 buffer[0] += newdrive - 1;
259 if (!SetCurrentDirectoryW( buffer ))
261 msvcrt_set_errno(GetLastError());
263 *MSVCRT__errno() = MSVCRT_EACCES;
269 /*********************************************************************
270 * _findclose (MSVCRT.@)
272 * Close a handle returned by _findfirst().
275 * hand [I] Handle to close
278 * Success: 0. All resources associated with hand are freed.
279 * Failure: -1. errno indicates the error.
284 int CDECL MSVCRT__findclose(MSVCRT_intptr_t hand)
286 TRACE(":handle %ld\n",hand);
287 if (!FindClose((HANDLE)hand))
289 msvcrt_set_errno(GetLastError());
295 /*********************************************************************
296 * _findfirst (MSVCRT.@)
298 * Open a handle for iterating through a directory.
301 * fspec [I] File specification of files to iterate.
302 * ft [O] Information for the first file found.
305 * Success: A handle suitable for passing to _findnext() and _findclose().
306 * ft is populated with the details of the found file.
307 * Failure: -1. errno indicates the error.
310 * See FindFirstFileA.
312 MSVCRT_intptr_t CDECL MSVCRT__findfirst(const char * fspec, struct MSVCRT__finddata_t* ft)
314 WIN32_FIND_DATAA find_data;
317 hfind = FindFirstFileA(fspec, &find_data);
318 if (hfind == INVALID_HANDLE_VALUE)
320 msvcrt_set_errno(GetLastError());
323 msvcrt_fttofd(&find_data,ft);
324 TRACE(":got handle %p\n",hfind);
325 return (MSVCRT_intptr_t)hfind;
328 /*********************************************************************
329 * _wfindfirst (MSVCRT.@)
331 * Unicode version of _findfirst.
333 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata_t* ft)
335 WIN32_FIND_DATAW find_data;
338 hfind = FindFirstFileW(fspec, &find_data);
339 if (hfind == INVALID_HANDLE_VALUE)
341 msvcrt_set_errno(GetLastError());
344 msvcrt_wfttofd(&find_data,ft);
345 TRACE(":got handle %p\n",hfind);
346 return (MSVCRT_intptr_t)hfind;
349 /*********************************************************************
350 * _findfirsti64 (MSVCRT.@)
352 * 64-bit version of _findfirst.
354 MSVCRT_intptr_t CDECL MSVCRT__findfirsti64(const char * fspec, struct MSVCRT__finddatai64_t* ft)
356 WIN32_FIND_DATAA find_data;
359 hfind = FindFirstFileA(fspec, &find_data);
360 if (hfind == INVALID_HANDLE_VALUE)
362 msvcrt_set_errno(GetLastError());
365 msvcrt_fttofdi64(&find_data,ft);
366 TRACE(":got handle %p\n",hfind);
367 return (MSVCRT_intptr_t)hfind;
370 /*********************************************************************
371 * _findfirst64 (MSVCRT.@)
373 * 64-bit version of _findfirst.
375 MSVCRT_intptr_t CDECL MSVCRT__findfirst64(const char * fspec, struct MSVCRT__finddata64_t* ft)
377 WIN32_FIND_DATAA find_data;
380 hfind = FindFirstFileA(fspec, &find_data);
381 if (hfind == INVALID_HANDLE_VALUE)
383 msvcrt_set_errno(GetLastError());
386 msvcrt_fttofd64(&find_data,ft);
387 TRACE(":got handle %p\n",hfind);
388 return (MSVCRT_intptr_t)hfind;
391 /*********************************************************************
392 * _wfindfirst64 (MSVCRT.@)
394 * Unicode version of _findfirst64.
396 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64_t* ft)
398 WIN32_FIND_DATAW find_data;
401 hfind = FindFirstFileW(fspec, &find_data);
402 if (hfind == INVALID_HANDLE_VALUE)
404 msvcrt_set_errno(GetLastError());
407 msvcrt_wfttofd64(&find_data,ft);
408 TRACE(":got handle %p\n",hfind);
409 return (MSVCRT_intptr_t)hfind;
412 /*********************************************************************
413 * _findfirst64i32 (MSVCRT.@)
415 * 64-bit/32-bit version of _findfirst.
417 MSVCRT_intptr_t CDECL MSVCRT__findfirst64i32(const char * fspec, struct MSVCRT__finddata64i32_t* ft)
419 WIN32_FIND_DATAA find_data;
422 hfind = FindFirstFileA(fspec, &find_data);
423 if (hfind == INVALID_HANDLE_VALUE)
425 msvcrt_set_errno(GetLastError());
428 msvcrt_fttofd64i32(&find_data,ft);
429 TRACE(":got handle %p\n",hfind);
430 return (MSVCRT_intptr_t)hfind;
433 /*********************************************************************
434 * _wfindfirst64i32 (MSVCRT.@)
436 * Unicode version of _findfirst64i32.
438 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64i32(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64i32_t* ft)
440 WIN32_FIND_DATAW find_data;
443 hfind = FindFirstFileW(fspec, &find_data);
444 if (hfind == INVALID_HANDLE_VALUE)
446 msvcrt_set_errno(GetLastError());
449 msvcrt_wfttofd64i32(&find_data,ft);
450 TRACE(":got handle %p\n",hfind);
451 return (MSVCRT_intptr_t)hfind;
454 /*********************************************************************
455 * _wfindfirsti64 (MSVCRT.@)
457 * Unicode version of _findfirsti64.
459 MSVCRT_intptr_t CDECL MSVCRT__wfindfirsti64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddatai64_t* ft)
461 WIN32_FIND_DATAW find_data;
464 hfind = FindFirstFileW(fspec, &find_data);
465 if (hfind == INVALID_HANDLE_VALUE)
467 msvcrt_set_errno(GetLastError());
470 msvcrt_wfttofdi64(&find_data,ft);
471 TRACE(":got handle %p\n",hfind);
472 return (MSVCRT_intptr_t)hfind;
475 /*********************************************************************
476 * _findnext (MSVCRT.@)
478 * Find the next file from a file search handle.
481 * hand [I] Handle to the search returned from _findfirst().
482 * ft [O] Information for the file found.
485 * Success: 0. ft is populated with the details of the found file.
486 * Failure: -1. errno indicates the error.
491 int CDECL MSVCRT__findnext(MSVCRT_intptr_t hand, struct MSVCRT__finddata_t * ft)
493 WIN32_FIND_DATAA find_data;
495 if (!FindNextFileA((HANDLE)hand, &find_data))
497 *MSVCRT__errno() = MSVCRT_ENOENT;
501 msvcrt_fttofd(&find_data,ft);
505 /*********************************************************************
506 * _wfindnext (MSVCRT.@)
508 * Unicode version of _findnext.
510 int CDECL MSVCRT__wfindnext(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata_t * ft)
512 WIN32_FIND_DATAW find_data;
514 if (!FindNextFileW((HANDLE)hand, &find_data))
516 *MSVCRT__errno() = MSVCRT_ENOENT;
520 msvcrt_wfttofd(&find_data,ft);
524 /*********************************************************************
525 * _findnexti64 (MSVCRT.@)
527 * 64-bit version of _findnext.
529 int CDECL MSVCRT__findnexti64(MSVCRT_intptr_t hand, struct MSVCRT__finddatai64_t * ft)
531 WIN32_FIND_DATAA find_data;
533 if (!FindNextFileA((HANDLE)hand, &find_data))
535 *MSVCRT__errno() = MSVCRT_ENOENT;
539 msvcrt_fttofdi64(&find_data,ft);
543 /*********************************************************************
544 * _findnext64 (MSVCRT.@)
546 * 64-bit version of _findnext.
548 int CDECL MSVCRT__findnext64(long hand, struct MSVCRT__finddata64_t * ft)
550 WIN32_FIND_DATAA find_data;
552 if (!FindNextFileA((HANDLE)hand, &find_data))
554 *MSVCRT__errno() = MSVCRT_ENOENT;
558 msvcrt_fttofd64(&find_data,ft);
562 /*********************************************************************
563 * _wfindnext64 (MSVCRT.@)
565 * Unicode version of _wfindnext64.
567 int CDECL MSVCRT__wfindnext64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64_t * ft)
569 WIN32_FIND_DATAW find_data;
571 if (!FindNextFileW((HANDLE)hand, &find_data))
573 *MSVCRT__errno() = MSVCRT_ENOENT;
577 msvcrt_wfttofd64(&find_data,ft);
581 /*********************************************************************
582 * _findnext64i32 (MSVCRT.@)
584 * 64-bit/32-bit version of _findnext.
586 int CDECL MSVCRT__findnext64i32(long hand, struct MSVCRT__finddata64i32_t * ft)
588 WIN32_FIND_DATAA find_data;
590 if (!FindNextFileA((HANDLE)hand, &find_data))
592 *MSVCRT__errno() = MSVCRT_ENOENT;
596 msvcrt_fttofd64i32(&find_data,ft);
600 /*********************************************************************
601 * _wfindnexti64 (MSVCRT.@)
603 * Unicode version of _findnexti64.
605 int CDECL MSVCRT__wfindnexti64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddatai64_t * ft)
607 WIN32_FIND_DATAW find_data;
609 if (!FindNextFileW((HANDLE)hand, &find_data))
611 *MSVCRT__errno() = MSVCRT_ENOENT;
615 msvcrt_wfttofdi64(&find_data,ft);
619 /*********************************************************************
620 * _wfindnext64i32 (MSVCRT.@)
622 * Unicode version of _findnext64i32.
624 int CDECL MSVCRT__wfindnext64i32(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64i32_t * ft)
626 WIN32_FIND_DATAW find_data;
628 if (!FindNextFileW((HANDLE)hand, &find_data))
630 *MSVCRT__errno() = MSVCRT_ENOENT;
634 msvcrt_wfttofd64i32(&find_data,ft);
638 /*********************************************************************
641 * Get the current working directory.
644 * buf [O] Destination for current working directory.
645 * size [I] Size of buf in characters
648 * Success: If buf is NULL, returns an allocated string containing the path.
649 * Otherwise populates buf with the path and returns it.
650 * Failure: NULL. errno indicates the error.
652 char* CDECL _getcwd(char * buf, int size)
655 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
658 return NULL; /* FIXME: Real return value untested */
662 if (size <= dir_len) size = dir_len + 1;
663 if (!(buf = MSVCRT_malloc( size ))) return NULL;
665 else if (dir_len >= size)
667 *MSVCRT__errno() = MSVCRT_ERANGE;
668 return NULL; /* buf too small */
674 /*********************************************************************
675 * _wgetcwd (MSVCRT.@)
677 * Unicode version of _getcwd.
679 MSVCRT_wchar_t* CDECL _wgetcwd(MSVCRT_wchar_t * buf, int size)
681 MSVCRT_wchar_t dir[MAX_PATH];
682 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
685 return NULL; /* FIXME: Real return value untested */
689 if (size <= dir_len) size = dir_len + 1;
690 if (!(buf = MSVCRT_malloc( size * sizeof(WCHAR) ))) return NULL;
694 *MSVCRT__errno() = MSVCRT_ERANGE;
695 return NULL; /* buf too small */
701 /*********************************************************************
702 * _getdrive (MSVCRT.@)
704 * Get the current drive number.
710 * Success: The drive letter number from 1 to 26 ("A:" to "Z:").
713 int CDECL _getdrive(void)
715 WCHAR buffer[MAX_PATH];
716 if (GetCurrentDirectoryW( MAX_PATH, buffer ) &&
717 buffer[0] >= 'A' && buffer[0] <= 'z' && buffer[1] == ':')
718 return toupperW(buffer[0]) - 'A' + 1;
722 /*********************************************************************
723 * _getdcwd (MSVCRT.@)
725 * Get the current working directory on a given disk.
728 * drive [I] Drive letter to get the current working directory from.
729 * buf [O] Destination for the current working directory.
730 * size [I] Length of drive in characters.
733 * Success: If drive is NULL, returns an allocated string containing the path.
734 * Otherwise populates drive with the path and returns it.
735 * Failure: NULL. errno indicates the error.
737 char* CDECL _getdcwd(int drive, char * buf, int size)
741 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
743 if (!drive || drive == _getdrive())
744 return _getcwd(buf,size); /* current */
748 char drivespec[4] = {'A', ':', 0};
751 drivespec[0] += drive - 1;
752 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
754 *MSVCRT__errno() = MSVCRT_EACCES;
758 dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
759 if (dir_len >= size || dir_len < 1)
761 *MSVCRT__errno() = MSVCRT_ERANGE;
762 return NULL; /* buf too small */
765 TRACE(":returning '%s'\n", dir);
767 return _strdup(dir); /* allocate */
774 /*********************************************************************
775 * _wgetdcwd (MSVCRT.@)
777 * Unicode version of _wgetdcwd.
779 MSVCRT_wchar_t* CDECL _wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
781 static MSVCRT_wchar_t* dummy;
783 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
785 if (!drive || drive == _getdrive())
786 return _wgetcwd(buf,size); /* current */
789 MSVCRT_wchar_t dir[MAX_PATH];
790 MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
793 drivespec[0] += drive - 1;
794 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
796 *MSVCRT__errno() = MSVCRT_EACCES;
800 dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
801 if (dir_len >= size || dir_len < 1)
803 *MSVCRT__errno() = MSVCRT_ERANGE;
804 return NULL; /* buf too small */
807 TRACE(":returning %s\n", debugstr_w(dir));
809 return _wcsdup(dir); /* allocate */
815 /*********************************************************************
816 * _getdiskfree (MSVCRT.@)
818 * Get information about the free space on a drive.
821 * disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
822 * info [O] Destination for the resulting information.
825 * Success: 0. info is updated with the free space information.
826 * Failure: An error code from GetLastError().
829 * See GetLastError().
831 unsigned int CDECL MSVCRT__getdiskfree(unsigned int disk, struct MSVCRT__diskfree_t * d)
833 WCHAR drivespec[4] = {'@', ':', '\\', 0};
838 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
840 drivespec[0] += disk; /* make a drive letter */
842 if (GetDiskFreeSpaceW(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
844 d->sectors_per_cluster = ret[0];
845 d->bytes_per_sector = ret[1];
846 d->avail_clusters = ret[2];
847 d->total_clusters = ret[3];
850 err = GetLastError();
851 msvcrt_set_errno(err);
855 /*********************************************************************
858 * Create a directory.
861 * newdir [I] Name of directory to create.
864 * Success: 0. The directory indicated by newdir is created.
865 * Failure: -1. errno indicates the error.
868 * See CreateDirectoryA.
870 int CDECL MSVCRT__mkdir(const char * newdir)
872 if (CreateDirectoryA(newdir,NULL))
874 msvcrt_set_errno(GetLastError());
878 /*********************************************************************
881 * Unicode version of _mkdir.
883 int CDECL _wmkdir(const MSVCRT_wchar_t* newdir)
885 if (CreateDirectoryW(newdir,NULL))
887 msvcrt_set_errno(GetLastError());
891 /*********************************************************************
894 * Delete a directory.
897 * dir [I] Name of directory to delete.
900 * Success: 0. The directory indicated by newdir is deleted.
901 * Failure: -1. errno indicates the error.
904 * See RemoveDirectoryA.
906 int CDECL MSVCRT__rmdir(const char * dir)
908 if (RemoveDirectoryA(dir))
910 msvcrt_set_errno(GetLastError());
914 /*********************************************************************
917 * Unicode version of _rmdir.
919 int CDECL _wrmdir(const MSVCRT_wchar_t * dir)
921 if (RemoveDirectoryW(dir))
923 msvcrt_set_errno(GetLastError());
927 /******************************************************************
928 * _splitpath_s (MSVCRT.@)
930 int _splitpath_s(const char* inpath,
931 char* drive, MSVCRT_size_t sz_drive,
932 char* dir, MSVCRT_size_t sz_dir,
933 char* fname, MSVCRT_size_t sz_fname,
934 char* ext, MSVCRT_size_t sz_ext)
938 if (!inpath || (!drive && sz_drive) ||
939 (drive && !sz_drive) ||
942 (!fname && sz_fname) ||
943 (fname && !sz_fname) ||
947 *MSVCRT__errno() = MSVCRT_EINVAL;
948 return MSVCRT_EINVAL;
951 if (inpath[0] && inpath[1] == ':')
955 if (sz_drive <= 2) goto do_error;
956 drive[0] = inpath[0];
957 drive[1] = inpath[1];
962 else if (drive) drive[0] = '\0';
964 /* look for end of directory part */
966 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
968 if (end) /* got a directory */
972 if (sz_dir <= end - inpath) goto do_error;
973 memcpy( dir, inpath, (end - inpath) );
974 dir[end - inpath] = 0;
978 else if (dir) dir[0] = 0;
980 /* look for extension: what's after the last dot */
982 for (p = inpath; *p; p++) if (*p == '.') end = p;
984 if (!end) end = p; /* there's no extension */
988 if (sz_fname <= end - inpath) goto do_error;
989 memcpy( fname, inpath, (end - inpath) );
990 fname[end - inpath] = 0;
994 if (sz_ext <= strlen(end)) goto do_error;
999 if (drive) drive[0] = '\0';
1000 if (dir) dir[0] = '\0';
1001 if (fname) fname[0]= '\0';
1002 if (ext) ext[0]= '\0';
1003 *MSVCRT__errno() = MSVCRT_ERANGE;
1004 return MSVCRT_ERANGE;
1007 /*********************************************************************
1008 * _splitpath (MSVCRT.@)
1010 void CDECL _splitpath(const char *inpath, char *drv, char *dir,
1011 char *fname, char *ext)
1013 _splitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
1014 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
1017 /******************************************************************
1018 * _wsplitpath_s (MSVCRT.@)
1020 * Secure version of _wsplitpath
1022 int _wsplitpath_s(const MSVCRT_wchar_t* inpath,
1023 MSVCRT_wchar_t* drive, MSVCRT_size_t sz_drive,
1024 MSVCRT_wchar_t* dir, MSVCRT_size_t sz_dir,
1025 MSVCRT_wchar_t* fname, MSVCRT_size_t sz_fname,
1026 MSVCRT_wchar_t* ext, MSVCRT_size_t sz_ext)
1028 const MSVCRT_wchar_t *p, *end;
1030 if (!inpath || (!drive && sz_drive) ||
1031 (drive && !sz_drive) ||
1034 (!fname && sz_fname) ||
1035 (fname && !sz_fname) ||
1039 *MSVCRT__errno() = MSVCRT_EINVAL;
1040 return MSVCRT_EINVAL;
1043 if (inpath[0] && inpath[1] == ':')
1047 if (sz_drive <= 2) goto do_error;
1048 drive[0] = inpath[0];
1049 drive[1] = inpath[1];
1054 else if (drive) drive[0] = '\0';
1056 /* look for end of directory part */
1058 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1060 if (end) /* got a directory */
1064 if (sz_dir <= end - inpath) goto do_error;
1065 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1066 dir[end - inpath] = 0;
1070 else if (dir) dir[0] = 0;
1072 /* look for extension: what's after the last dot */
1074 for (p = inpath; *p; p++) if (*p == '.') end = p;
1076 if (!end) end = p; /* there's no extension */
1080 if (sz_fname <= end - inpath) goto do_error;
1081 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1082 fname[end - inpath] = 0;
1086 if (sz_ext <= strlenW(end)) goto do_error;
1087 strcpyW( ext, end );
1091 if (drive) drive[0] = '\0';
1092 if (dir) dir[0] = '\0';
1093 if (fname) fname[0]= '\0';
1094 if (ext) ext[0]= '\0';
1095 *MSVCRT__errno() = MSVCRT_ERANGE;
1096 return MSVCRT_ERANGE;
1099 /*********************************************************************
1100 * _wsplitpath (MSVCRT.@)
1102 * Unicode version of _splitpath.
1104 void CDECL _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
1105 MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext)
1107 _wsplitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
1108 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
1111 /*********************************************************************
1112 * _wfullpath (MSVCRT.@)
1114 * Unicode version of _fullpath.
1116 MSVCRT_wchar_t * CDECL _wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, MSVCRT_size_t size)
1121 BOOL alloced = FALSE;
1123 if (!relPath || !*relPath)
1124 return _wgetcwd(absPath, size);
1126 if (absPath == NULL)
1128 buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
1137 *MSVCRT__errno() = MSVCRT_ERANGE;
1141 TRACE(":resolving relative path %s\n",debugstr_w(relPath));
1143 rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
1145 if (rc > 0 && rc <= size )
1150 MSVCRT_free(buffer);
1155 /*********************************************************************
1156 * _fullpath (MSVCRT.@)
1158 * Create an absolute path from a relative path.
1161 * absPath [O] Destination for absolute path
1162 * relPath [I] Relative path to convert to absolute
1163 * size [I] Length of absPath in characters.
1166 * Success: If absPath is NULL, returns an allocated string containing the path.
1167 * Otherwise populates absPath with the path and returns it.
1168 * Failure: NULL. errno indicates the error.
1170 char * CDECL _fullpath(char * absPath, const char* relPath, unsigned int size)
1175 BOOL alloced = FALSE;
1177 if (!relPath || !*relPath)
1178 return _getcwd(absPath, size);
1180 if (absPath == NULL)
1182 buffer = MSVCRT_malloc(MAX_PATH);
1191 *MSVCRT__errno() = MSVCRT_ERANGE;
1195 TRACE(":resolving relative path '%s'\n",relPath);
1197 rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
1199 if (rc > 0 && rc <= size)
1204 MSVCRT_free(buffer);
1209 /*********************************************************************
1210 * _makepath (MSVCRT.@)
1212 * Create a pathname.
1215 * path [O] Destination for created pathname
1216 * drive [I] Drive letter (e.g. "A:")
1217 * directory [I] Directory
1218 * filename [I] Name of the file, excluding extension
1219 * extension [I] File extension (e.g. ".TXT")
1222 * Nothing. If path is not large enough to hold the resulting pathname,
1223 * random process memory will be overwritten.
1225 VOID CDECL _makepath(char * path, const char * drive,
1226 const char *directory, const char * filename,
1227 const char * extension)
1231 TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
1232 debugstr_a(filename), debugstr_a(extension) );
1237 if (drive && drive[0])
1242 if (directory && directory[0])
1244 unsigned int len = strlen(directory);
1245 memmove(p, directory, len);
1247 if (p[-1] != '/' && p[-1] != '\\')
1250 if (filename && filename[0])
1252 unsigned int len = strlen(filename);
1253 memmove(p, filename, len);
1256 if (extension && extension[0])
1258 if (extension[0] != '.')
1260 strcpy(p, extension);
1264 TRACE("returning %s\n",path);
1267 /*********************************************************************
1268 * _wmakepath (MSVCRT.@)
1270 * Unicode version of _wmakepath.
1272 VOID CDECL _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
1273 const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
1275 MSVCRT_wchar_t *p = path;
1277 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
1278 debugstr_w(filename), debugstr_w(extension));
1283 if (drive && drive[0])
1288 if (directory && directory[0])
1290 unsigned int len = strlenW(directory);
1291 memmove(p, directory, len * sizeof(MSVCRT_wchar_t));
1293 if (p[-1] != '/' && p[-1] != '\\')
1296 if (filename && filename[0])
1298 unsigned int len = strlenW(filename);
1299 memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1302 if (extension && extension[0])
1304 if (extension[0] != '.')
1306 strcpyW(p, extension);
1311 TRACE("returning %s\n", debugstr_w(path));
1314 /*********************************************************************
1315 * _makepath_s (MSVCRT.@)
1317 * Safe version of _makepath.
1319 int CDECL _makepath_s(char *path, MSVCRT_size_t size, const char *drive,
1320 const char *directory, const char *filename,
1321 const char *extension)
1327 *MSVCRT__errno() = MSVCRT_EINVAL;
1328 return MSVCRT_EINVAL;
1331 if (drive && drive[0])
1341 if (directory && directory[0])
1343 unsigned int len = strlen(directory);
1344 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1345 unsigned int copylen = min(size - 1, len);
1350 memmove(p, directory, copylen);
1358 if (needs_separator)
1368 if (filename && filename[0])
1370 unsigned int len = strlen(filename);
1371 unsigned int copylen = min(size - 1, len);
1376 memmove(p, filename, copylen);
1385 if (extension && extension[0])
1387 unsigned int len = strlen(extension);
1388 unsigned int needs_period = extension[0] != '.';
1389 unsigned int copylen;
1400 copylen = min(size - 1, len);
1401 memcpy(p, extension, copylen);
1414 *MSVCRT__errno() = MSVCRT_ERANGE;
1415 return MSVCRT_ERANGE;
1418 /*********************************************************************
1419 * _wmakepath_s (MSVCRT.@)
1421 * Safe version of _wmakepath.
1423 int CDECL _wmakepath_s(MSVCRT_wchar_t *path, MSVCRT_size_t size, const MSVCRT_wchar_t *drive,
1424 const MSVCRT_wchar_t *directory, const MSVCRT_wchar_t *filename,
1425 const MSVCRT_wchar_t *extension)
1427 MSVCRT_wchar_t *p = path;
1431 *MSVCRT__errno() = MSVCRT_EINVAL;
1432 return MSVCRT_EINVAL;
1435 if (drive && drive[0])
1445 if (directory && directory[0])
1447 unsigned int len = strlenW(directory);
1448 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1449 unsigned int copylen = min(size - 1, len);
1454 memmove(p, directory, copylen * sizeof(MSVCRT_wchar_t));
1462 if (needs_separator)
1472 if (filename && filename[0])
1474 unsigned int len = strlenW(filename);
1475 unsigned int copylen = min(size - 1, len);
1480 memmove(p, filename, copylen * sizeof(MSVCRT_wchar_t));
1489 if (extension && extension[0])
1491 unsigned int len = strlenW(extension);
1492 unsigned int needs_period = extension[0] != '.';
1493 unsigned int copylen;
1504 copylen = min(size - 1, len);
1505 memcpy(p, extension, copylen * sizeof(MSVCRT_wchar_t));
1518 *MSVCRT__errno() = MSVCRT_ERANGE;
1519 return MSVCRT_ERANGE;
1522 /*********************************************************************
1523 * _searchenv (MSVCRT.@)
1525 * Search for a file in a list of paths from an environment variable.
1528 * file [I] Name of the file to search for.
1529 * env [I] Name of the environment variable containing a list of paths.
1530 * buf [O] Destination for the found file path.
1533 * Nothing. If the file is not found, buf will contain an empty string
1536 void CDECL _searchenv(const char* file, const char* env, char *buf)
1539 char curPath[MAX_PATH];
1544 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1546 GetFullPathNameA( file, MAX_PATH, buf, NULL );
1547 /* Sigh. This error is *always* set, regardless of success */
1548 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1552 /* Search given environment variable */
1553 envVal = MSVCRT_getenv(env);
1556 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1561 TRACE(":searching for %s in paths %s\n", file, envVal);
1567 while(*end && *end != ';') end++; /* Find end of next path */
1568 if (penv == end || !*penv)
1570 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1573 memcpy(curPath, penv, end - penv);
1574 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1576 curPath[end - penv] = '\\';
1577 curPath[end - penv + 1] = '\0';
1580 curPath[end - penv] = '\0';
1582 strcat(curPath, file);
1583 TRACE("Checking for file %s\n", curPath);
1584 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1586 strcpy(buf, curPath);
1587 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1590 penv = *end ? end + 1 : end;
1594 /*********************************************************************
1595 * _searchenv_s (MSVCRT.@)
1597 int CDECL _searchenv_s(const char* file, const char* env, char *buf, MSVCRT_size_t count)
1600 char curPath[MAX_PATH];
1602 if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1603 !MSVCRT_CHECK_PMT(count > 0))
1605 *MSVCRT__errno() = MSVCRT_EINVAL;
1606 return MSVCRT_EINVAL;
1612 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1614 if (GetFullPathNameA( file, count, buf, NULL )) return 0;
1615 msvcrt_set_errno(GetLastError());
1619 /* Search given environment variable */
1620 envVal = MSVCRT_getenv(env);
1623 *MSVCRT__errno() = MSVCRT_ENOENT;
1624 return MSVCRT_ENOENT;
1628 TRACE(":searching for %s in paths %s\n", file, envVal);
1634 while(*end && *end != ';') end++; /* Find end of next path */
1635 if (penv == end || !*penv)
1637 *MSVCRT__errno() = MSVCRT_ENOENT;
1638 return MSVCRT_ENOENT;
1640 memcpy(curPath, penv, end - penv);
1641 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1643 curPath[end - penv] = '\\';
1644 curPath[end - penv + 1] = '\0';
1647 curPath[end - penv] = '\0';
1649 strcat(curPath, file);
1650 TRACE("Checking for file %s\n", curPath);
1651 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1653 if (strlen(curPath) + 1 > count)
1655 MSVCRT_INVALID_PMT("buf[count] is too small");
1656 *MSVCRT__errno() = MSVCRT_ERANGE;
1657 return MSVCRT_ERANGE;
1659 strcpy(buf, curPath);
1662 penv = *end ? end + 1 : end;
1666 /*********************************************************************
1667 * _wsearchenv (MSVCRT.@)
1669 * Unicode version of _searchenv
1671 void CDECL _wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1673 MSVCRT_wchar_t *envVal, *penv;
1674 MSVCRT_wchar_t curPath[MAX_PATH];
1679 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1681 GetFullPathNameW( file, MAX_PATH, buf, NULL );
1682 /* Sigh. This error is *always* set, regardless of success */
1683 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1687 /* Search given environment variable */
1688 envVal = _wgetenv(env);
1691 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1696 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1700 MSVCRT_wchar_t *end = penv;
1702 while(*end && *end != ';') end++; /* Find end of next path */
1703 if (penv == end || !*penv)
1705 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1708 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1709 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1711 curPath[end - penv] = '\\';
1712 curPath[end - penv + 1] = '\0';
1715 curPath[end - penv] = '\0';
1717 strcatW(curPath, file);
1718 TRACE("Checking for file %s\n", debugstr_w(curPath));
1719 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1721 strcpyW(buf, curPath);
1722 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1725 penv = *end ? end + 1 : end;
1729 /*********************************************************************
1730 * _wsearchenv_s (MSVCRT.@)
1732 int CDECL _wsearchenv_s(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env,
1733 MSVCRT_wchar_t *buf, MSVCRT_size_t count)
1735 MSVCRT_wchar_t* envVal, *penv;
1736 MSVCRT_wchar_t curPath[MAX_PATH];
1738 if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1739 !MSVCRT_CHECK_PMT(count > 0))
1741 *MSVCRT__errno() = MSVCRT_EINVAL;
1742 return MSVCRT_EINVAL;
1747 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1749 if (GetFullPathNameW( file, count, buf, NULL )) return 0;
1750 msvcrt_set_errno(GetLastError());
1754 /* Search given environment variable */
1755 envVal = _wgetenv(env);
1758 *MSVCRT__errno() = MSVCRT_ENOENT;
1759 return MSVCRT_ENOENT;
1763 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1767 MSVCRT_wchar_t *end = penv;
1769 while(*end && *end != ';') end++; /* Find end of next path */
1770 if (penv == end || !*penv)
1772 *MSVCRT__errno() = MSVCRT_ENOENT;
1773 return MSVCRT_ENOENT;
1775 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1776 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1778 curPath[end - penv] = '\\';
1779 curPath[end - penv + 1] = '\0';
1782 curPath[end - penv] = '\0';
1784 strcatW(curPath, file);
1785 TRACE("Checking for file %s\n", debugstr_w(curPath));
1786 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1788 if (strlenW(curPath) + 1 > count)
1790 MSVCRT_INVALID_PMT("buf[count] is too small");
1791 *MSVCRT__errno() = MSVCRT_ERANGE;
1792 return MSVCRT_ERANGE;
1794 strcpyW(buf, curPath);
1797 penv = *end ? end + 1 : end;