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);
120 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddatai64_t */
121 static void msvcrt_wfttofdi64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddatai64_t* ft)
125 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
128 ft->attrib = fd->dwFileAttributes;
130 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
131 ft->time_create = dw;
132 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
133 ft->time_access = dw;
134 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
136 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
137 strcpyW(ft->name, fd->cFileName);
140 /*********************************************************************
143 * Change the current working directory.
146 * newdir [I] Directory to change to
149 * Success: 0. The current working directory is set to newdir.
150 * Failure: -1. errno indicates the error.
153 * See SetCurrentDirectoryA.
155 int CDECL MSVCRT__chdir(const char * newdir)
157 if (!SetCurrentDirectoryA(newdir))
159 msvcrt_set_errno(newdir?GetLastError():0);
165 /*********************************************************************
168 * Unicode version of _chdir.
170 int CDECL _wchdir(const MSVCRT_wchar_t * newdir)
172 if (!SetCurrentDirectoryW(newdir))
174 msvcrt_set_errno(newdir?GetLastError():0);
180 /*********************************************************************
181 * _chdrive (MSVCRT.@)
183 * Change the current drive.
186 * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
189 * Success: 0. The current drive is set to newdrive.
190 * Failure: -1. errno indicates the error.
193 * See SetCurrentDirectoryA.
195 int CDECL _chdrive(int newdrive)
197 WCHAR buffer[3] = {'A', ':', 0};
199 buffer[0] += newdrive - 1;
200 if (!SetCurrentDirectoryW( buffer ))
202 msvcrt_set_errno(GetLastError());
204 *MSVCRT__errno() = MSVCRT_EACCES;
210 /*********************************************************************
211 * _findclose (MSVCRT.@)
213 * Close a handle returned by _findfirst().
216 * hand [I] Handle to close
219 * Success: 0. All resources associated with hand are freed.
220 * Failure: -1. errno indicates the error.
225 int CDECL MSVCRT__findclose(MSVCRT_intptr_t hand)
227 TRACE(":handle %ld\n",hand);
228 if (!FindClose((HANDLE)hand))
230 msvcrt_set_errno(GetLastError());
236 /*********************************************************************
237 * _findfirst (MSVCRT.@)
239 * Open a handle for iterating through a directory.
242 * fspec [I] File specification of files to iterate.
243 * ft [O] Information for the first file found.
246 * Success: A handle suitable for passing to _findnext() and _findclose().
247 * ft is populated with the details of the found file.
248 * Failure: -1. errno indicates the error.
251 * See FindFirstFileA.
253 MSVCRT_intptr_t CDECL MSVCRT__findfirst(const char * fspec, struct MSVCRT__finddata_t* ft)
255 WIN32_FIND_DATAA find_data;
258 hfind = FindFirstFileA(fspec, &find_data);
259 if (hfind == INVALID_HANDLE_VALUE)
261 msvcrt_set_errno(GetLastError());
264 msvcrt_fttofd(&find_data,ft);
265 TRACE(":got handle %p\n",hfind);
266 return (MSVCRT_intptr_t)hfind;
269 /*********************************************************************
270 * _wfindfirst (MSVCRT.@)
272 * Unicode version of _findfirst.
274 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata_t* ft)
276 WIN32_FIND_DATAW find_data;
279 hfind = FindFirstFileW(fspec, &find_data);
280 if (hfind == INVALID_HANDLE_VALUE)
282 msvcrt_set_errno(GetLastError());
285 msvcrt_wfttofd(&find_data,ft);
286 TRACE(":got handle %p\n",hfind);
287 return (MSVCRT_intptr_t)hfind;
290 /*********************************************************************
291 * _findfirsti64 (MSVCRT.@)
293 * 64-bit version of _findfirst.
295 MSVCRT_intptr_t CDECL MSVCRT__findfirsti64(const char * fspec, struct MSVCRT__finddatai64_t* ft)
297 WIN32_FIND_DATAA find_data;
300 hfind = FindFirstFileA(fspec, &find_data);
301 if (hfind == INVALID_HANDLE_VALUE)
303 msvcrt_set_errno(GetLastError());
306 msvcrt_fttofdi64(&find_data,ft);
307 TRACE(":got handle %p\n",hfind);
308 return (MSVCRT_intptr_t)hfind;
311 /*********************************************************************
312 * _findfirst64 (MSVCRT.@)
314 * 64-bit version of _findfirst.
316 MSVCRT_intptr_t CDECL MSVCRT__findfirst64(const char * fspec, struct MSVCRT__finddata64_t* ft)
318 WIN32_FIND_DATAA find_data;
321 hfind = FindFirstFileA(fspec, &find_data);
322 if (hfind == INVALID_HANDLE_VALUE)
324 msvcrt_set_errno(GetLastError());
327 msvcrt_fttofd64(&find_data,ft);
328 TRACE(":got handle %p\n",hfind);
329 return (MSVCRT_intptr_t)hfind;
332 /*********************************************************************
333 * _wfindfirsti64 (MSVCRT.@)
335 * Unicode version of _findfirsti64.
337 MSVCRT_intptr_t CDECL MSVCRT__wfindfirsti64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddatai64_t* ft)
339 WIN32_FIND_DATAW find_data;
342 hfind = FindFirstFileW(fspec, &find_data);
343 if (hfind == INVALID_HANDLE_VALUE)
345 msvcrt_set_errno(GetLastError());
348 msvcrt_wfttofdi64(&find_data,ft);
349 TRACE(":got handle %p\n",hfind);
350 return (MSVCRT_intptr_t)hfind;
353 /*********************************************************************
354 * _findnext (MSVCRT.@)
356 * Find the next file from a file search handle.
359 * hand [I] Handle to the search returned from _findfirst().
360 * ft [O] Information for the file found.
363 * Success: 0. ft is populated with the details of the found file.
364 * Failure: -1. errno indicates the error.
369 int CDECL MSVCRT__findnext(MSVCRT_intptr_t hand, struct MSVCRT__finddata_t * ft)
371 WIN32_FIND_DATAA find_data;
373 if (!FindNextFileA((HANDLE)hand, &find_data))
375 *MSVCRT__errno() = MSVCRT_ENOENT;
379 msvcrt_fttofd(&find_data,ft);
383 /*********************************************************************
384 * _wfindnext (MSVCRT.@)
386 * Unicode version of _findnext.
388 int CDECL MSVCRT__wfindnext(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata_t * ft)
390 WIN32_FIND_DATAW find_data;
392 if (!FindNextFileW((HANDLE)hand, &find_data))
394 *MSVCRT__errno() = MSVCRT_ENOENT;
398 msvcrt_wfttofd(&find_data,ft);
402 /*********************************************************************
403 * _findnexti64 (MSVCRT.@)
405 * 64-bit version of _findnext.
407 int CDECL MSVCRT__findnexti64(MSVCRT_intptr_t hand, struct MSVCRT__finddatai64_t * ft)
409 WIN32_FIND_DATAA find_data;
411 if (!FindNextFileA((HANDLE)hand, &find_data))
413 *MSVCRT__errno() = MSVCRT_ENOENT;
417 msvcrt_fttofdi64(&find_data,ft);
421 /*********************************************************************
422 * _findnext64 (MSVCRT.@)
424 * 64-bit version of _findnext.
426 int CDECL MSVCRT__findnext64(long hand, struct MSVCRT__finddata64_t * ft)
428 WIN32_FIND_DATAA find_data;
430 if (!FindNextFileA((HANDLE)hand, &find_data))
432 *MSVCRT__errno() = MSVCRT_ENOENT;
436 msvcrt_fttofd64(&find_data,ft);
440 /*********************************************************************
441 * _wfindnexti64 (MSVCRT.@)
443 * Unicode version of _findnexti64.
445 int CDECL MSVCRT__wfindnexti64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddatai64_t * ft)
447 WIN32_FIND_DATAW find_data;
449 if (!FindNextFileW((HANDLE)hand, &find_data))
451 *MSVCRT__errno() = MSVCRT_ENOENT;
455 msvcrt_wfttofdi64(&find_data,ft);
459 /*********************************************************************
462 * Get the current working directory.
465 * buf [O] Destination for current working directory.
466 * size [I] Size of buf in characters
469 * Success: If buf is NULL, returns an allocated string containing the path.
470 * Otherwise populates buf with the path and returns it.
471 * Failure: NULL. errno indicates the error.
473 char* CDECL _getcwd(char * buf, int size)
476 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
479 return NULL; /* FIXME: Real return value untested */
483 if (size <= dir_len) size = dir_len + 1;
484 if (!(buf = MSVCRT_malloc( size ))) return NULL;
486 else if (dir_len >= size)
488 *MSVCRT__errno() = MSVCRT_ERANGE;
489 return NULL; /* buf too small */
495 /*********************************************************************
496 * _wgetcwd (MSVCRT.@)
498 * Unicode version of _getcwd.
500 MSVCRT_wchar_t* CDECL _wgetcwd(MSVCRT_wchar_t * buf, int size)
502 MSVCRT_wchar_t dir[MAX_PATH];
503 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
506 return NULL; /* FIXME: Real return value untested */
510 if (size <= dir_len) size = dir_len + 1;
511 if (!(buf = MSVCRT_malloc( size * sizeof(WCHAR) ))) return NULL;
515 *MSVCRT__errno() = MSVCRT_ERANGE;
516 return NULL; /* buf too small */
522 /*********************************************************************
523 * _getdrive (MSVCRT.@)
525 * Get the current drive number.
531 * Success: The drive letter number from 1 to 26 ("A:" to "Z:").
534 int CDECL _getdrive(void)
536 WCHAR buffer[MAX_PATH];
537 if (GetCurrentDirectoryW( MAX_PATH, buffer ) &&
538 buffer[0] >= 'A' && buffer[0] <= 'z' && buffer[1] == ':')
539 return toupperW(buffer[0]) - 'A' + 1;
543 /*********************************************************************
544 * _getdcwd (MSVCRT.@)
546 * Get the current working directory on a given disk.
549 * drive [I] Drive letter to get the current working directory from.
550 * buf [O] Destination for the current working directory.
551 * size [I] Length of drive in characters.
554 * Success: If drive is NULL, returns an allocated string containing the path.
555 * Otherwise populates drive with the path and returns it.
556 * Failure: NULL. errno indicates the error.
558 char* CDECL _getdcwd(int drive, char * buf, int size)
562 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
564 if (!drive || drive == _getdrive())
565 return _getcwd(buf,size); /* current */
569 char drivespec[4] = {'A', ':', 0};
572 drivespec[0] += drive - 1;
573 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
575 *MSVCRT__errno() = MSVCRT_EACCES;
579 dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
580 if (dir_len >= size || dir_len < 1)
582 *MSVCRT__errno() = MSVCRT_ERANGE;
583 return NULL; /* buf too small */
586 TRACE(":returning '%s'\n", dir);
588 return _strdup(dir); /* allocate */
595 /*********************************************************************
596 * _wgetdcwd (MSVCRT.@)
598 * Unicode version of _wgetdcwd.
600 MSVCRT_wchar_t* CDECL _wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
602 static MSVCRT_wchar_t* dummy;
604 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
606 if (!drive || drive == _getdrive())
607 return _wgetcwd(buf,size); /* current */
610 MSVCRT_wchar_t dir[MAX_PATH];
611 MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
614 drivespec[0] += drive - 1;
615 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
617 *MSVCRT__errno() = MSVCRT_EACCES;
621 dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
622 if (dir_len >= size || dir_len < 1)
624 *MSVCRT__errno() = MSVCRT_ERANGE;
625 return NULL; /* buf too small */
628 TRACE(":returning %s\n", debugstr_w(dir));
630 return _wcsdup(dir); /* allocate */
636 /*********************************************************************
637 * _getdiskfree (MSVCRT.@)
639 * Get information about the free space on a drive.
642 * disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
643 * info [O] Destination for the resulting information.
646 * Success: 0. info is updated with the free space information.
647 * Failure: An error code from GetLastError().
650 * See GetLastError().
652 unsigned int CDECL MSVCRT__getdiskfree(unsigned int disk, struct MSVCRT__diskfree_t * d)
654 WCHAR drivespec[4] = {'@', ':', '\\', 0};
659 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
661 drivespec[0] += disk; /* make a drive letter */
663 if (GetDiskFreeSpaceW(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
665 d->sectors_per_cluster = ret[0];
666 d->bytes_per_sector = ret[1];
667 d->avail_clusters = ret[2];
668 d->total_clusters = ret[3];
671 err = GetLastError();
672 msvcrt_set_errno(err);
676 /*********************************************************************
679 * Create a directory.
682 * newdir [I] Name of directory to create.
685 * Success: 0. The directory indicated by newdir is created.
686 * Failure: -1. errno indicates the error.
689 * See CreateDirectoryA.
691 int CDECL MSVCRT__mkdir(const char * newdir)
693 if (CreateDirectoryA(newdir,NULL))
695 msvcrt_set_errno(GetLastError());
699 /*********************************************************************
702 * Unicode version of _mkdir.
704 int CDECL _wmkdir(const MSVCRT_wchar_t* newdir)
706 if (CreateDirectoryW(newdir,NULL))
708 msvcrt_set_errno(GetLastError());
712 /*********************************************************************
715 * Delete a directory.
718 * dir [I] Name of directory to delete.
721 * Success: 0. The directory indicated by newdir is deleted.
722 * Failure: -1. errno indicates the error.
725 * See RemoveDirectoryA.
727 int CDECL MSVCRT__rmdir(const char * dir)
729 if (RemoveDirectoryA(dir))
731 msvcrt_set_errno(GetLastError());
735 /*********************************************************************
738 * Unicode version of _rmdir.
740 int CDECL _wrmdir(const MSVCRT_wchar_t * dir)
742 if (RemoveDirectoryW(dir))
744 msvcrt_set_errno(GetLastError());
748 /******************************************************************
749 * _splitpath_s (MSVCRT.@)
751 int _splitpath_s(const char* inpath,
752 char* drive, MSVCRT_size_t sz_drive,
753 char* dir, MSVCRT_size_t sz_dir,
754 char* fname, MSVCRT_size_t sz_fname,
755 char* ext, MSVCRT_size_t sz_ext)
759 if (!inpath || (!drive && sz_drive) ||
760 (drive && !sz_drive) ||
763 (!fname && sz_fname) ||
764 (fname && !sz_fname) ||
768 *MSVCRT__errno() = MSVCRT_EINVAL;
769 return MSVCRT_EINVAL;
772 if (inpath[0] && inpath[1] == ':')
776 if (sz_drive <= 2) goto do_error;
777 drive[0] = inpath[0];
778 drive[1] = inpath[1];
783 else if (drive) drive[0] = '\0';
785 /* look for end of directory part */
787 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
789 if (end) /* got a directory */
793 if (sz_dir <= end - inpath) goto do_error;
794 memcpy( dir, inpath, (end - inpath) );
795 dir[end - inpath] = 0;
799 else if (dir) dir[0] = 0;
801 /* look for extension: what's after the last dot */
803 for (p = inpath; *p; p++) if (*p == '.') end = p;
805 if (!end) end = p; /* there's no extension */
809 if (sz_fname <= end - inpath) goto do_error;
810 memcpy( fname, inpath, (end - inpath) );
811 fname[end - inpath] = 0;
815 if (sz_ext <= strlen(end)) goto do_error;
820 if (drive) drive[0] = '\0';
821 if (dir) dir[0] = '\0';
822 if (fname) fname[0]= '\0';
823 if (ext) ext[0]= '\0';
824 *MSVCRT__errno() = MSVCRT_ERANGE;
825 return MSVCRT_ERANGE;
828 /*********************************************************************
829 * _splitpath (MSVCRT.@)
831 void CDECL _splitpath(const char *inpath, char *drv, char *dir,
832 char *fname, char *ext)
834 _splitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
835 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
838 /******************************************************************
839 * _wsplitpath_s (MSVCRT.@)
841 * Secure version of _wsplitpath
843 int _wsplitpath_s(const MSVCRT_wchar_t* inpath,
844 MSVCRT_wchar_t* drive, MSVCRT_size_t sz_drive,
845 MSVCRT_wchar_t* dir, MSVCRT_size_t sz_dir,
846 MSVCRT_wchar_t* fname, MSVCRT_size_t sz_fname,
847 MSVCRT_wchar_t* ext, MSVCRT_size_t sz_ext)
849 const MSVCRT_wchar_t *p, *end;
851 if (!inpath || (!drive && sz_drive) ||
852 (drive && !sz_drive) ||
855 (!fname && sz_fname) ||
856 (fname && !sz_fname) ||
860 *MSVCRT__errno() = MSVCRT_EINVAL;
861 return MSVCRT_EINVAL;
864 if (inpath[0] && inpath[1] == ':')
868 if (sz_drive <= 2) goto do_error;
869 drive[0] = inpath[0];
870 drive[1] = inpath[1];
875 else if (drive) drive[0] = '\0';
877 /* look for end of directory part */
879 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
881 if (end) /* got a directory */
885 if (sz_dir <= end - inpath) goto do_error;
886 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
887 dir[end - inpath] = 0;
891 else if (dir) dir[0] = 0;
893 /* look for extension: what's after the last dot */
895 for (p = inpath; *p; p++) if (*p == '.') end = p;
897 if (!end) end = p; /* there's no extension */
901 if (sz_fname <= end - inpath) goto do_error;
902 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
903 fname[end - inpath] = 0;
907 if (sz_ext <= strlenW(end)) goto do_error;
912 if (drive) drive[0] = '\0';
913 if (dir) dir[0] = '\0';
914 if (fname) fname[0]= '\0';
915 if (ext) ext[0]= '\0';
916 *MSVCRT__errno() = MSVCRT_ERANGE;
917 return MSVCRT_ERANGE;
920 /*********************************************************************
921 * _wsplitpath (MSVCRT.@)
923 * Unicode version of _splitpath.
925 void CDECL _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
926 MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext)
928 _wsplitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
929 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
932 /*********************************************************************
933 * _wfullpath (MSVCRT.@)
935 * Unicode version of _fullpath.
937 MSVCRT_wchar_t * CDECL _wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, MSVCRT_size_t size)
942 BOOL alloced = FALSE;
944 if (!relPath || !*relPath)
945 return _wgetcwd(absPath, size);
949 buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
958 *MSVCRT__errno() = MSVCRT_ERANGE;
962 TRACE(":resolving relative path %s\n",debugstr_w(relPath));
964 rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
966 if (rc > 0 && rc <= size )
976 /*********************************************************************
977 * _fullpath (MSVCRT.@)
979 * Create an absolute path from a relative path.
982 * absPath [O] Destination for absolute path
983 * relPath [I] Relative path to convert to absolute
984 * size [I] Length of absPath in characters.
987 * Success: If absPath is NULL, returns an allocated string containing the path.
988 * Otherwise populates absPath with the path and returns it.
989 * Failure: NULL. errno indicates the error.
991 char * CDECL _fullpath(char * absPath, const char* relPath, unsigned int size)
996 BOOL alloced = FALSE;
998 if (!relPath || !*relPath)
999 return _getcwd(absPath, size);
1001 if (absPath == NULL)
1003 buffer = MSVCRT_malloc(MAX_PATH);
1012 *MSVCRT__errno() = MSVCRT_ERANGE;
1016 TRACE(":resolving relative path '%s'\n",relPath);
1018 rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
1020 if (rc > 0 && rc <= size)
1025 MSVCRT_free(buffer);
1030 /*********************************************************************
1031 * _makepath (MSVCRT.@)
1033 * Create a pathname.
1036 * path [O] Destination for created pathname
1037 * drive [I] Drive letter (e.g. "A:")
1038 * directory [I] Directory
1039 * filename [I] Name of the file, excluding extension
1040 * extension [I] File extension (e.g. ".TXT")
1043 * Nothing. If path is not large enough to hold the resulting pathname,
1044 * random process memory will be overwritten.
1046 VOID CDECL _makepath(char * path, const char * drive,
1047 const char *directory, const char * filename,
1048 const char * extension)
1052 TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
1053 debugstr_a(filename), debugstr_a(extension) );
1058 if (drive && drive[0])
1063 if (directory && directory[0])
1065 unsigned int len = strlen(directory);
1066 memmove(p, directory, len);
1068 if (p[-1] != '/' && p[-1] != '\\')
1071 if (filename && filename[0])
1073 unsigned int len = strlen(filename);
1074 memmove(p, filename, len);
1077 if (extension && extension[0])
1079 if (extension[0] != '.')
1081 strcpy(p, extension);
1085 TRACE("returning %s\n",path);
1088 /*********************************************************************
1089 * _wmakepath (MSVCRT.@)
1091 * Unicode version of _wmakepath.
1093 VOID CDECL _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
1094 const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
1096 MSVCRT_wchar_t *p = path;
1098 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
1099 debugstr_w(filename), debugstr_w(extension));
1104 if (drive && drive[0])
1109 if (directory && directory[0])
1111 unsigned int len = strlenW(directory);
1112 memmove(p, directory, len * sizeof(MSVCRT_wchar_t));
1114 if (p[-1] != '/' && p[-1] != '\\')
1117 if (filename && filename[0])
1119 unsigned int len = strlenW(filename);
1120 memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1123 if (extension && extension[0])
1125 if (extension[0] != '.')
1127 strcpyW(p, extension);
1132 TRACE("returning %s\n", debugstr_w(path));
1135 /*********************************************************************
1136 * _makepath_s (MSVCRT.@)
1138 * Safe version of _makepath.
1140 int CDECL _makepath_s(char *path, MSVCRT_size_t size, const char *drive,
1141 const char *directory, const char *filename,
1142 const char *extension)
1148 *MSVCRT__errno() = MSVCRT_EINVAL;
1149 return MSVCRT_EINVAL;
1152 if (drive && drive[0])
1162 if (directory && directory[0])
1164 unsigned int len = strlen(directory);
1165 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1166 unsigned int copylen = min(size - 1, len);
1171 memmove(p, directory, copylen);
1179 if (needs_separator)
1189 if (filename && filename[0])
1191 unsigned int len = strlen(filename);
1192 unsigned int copylen = min(size - 1, len);
1197 memmove(p, filename, copylen);
1206 if (extension && extension[0])
1208 unsigned int len = strlen(extension);
1209 unsigned int needs_period = extension[0] != '.';
1210 unsigned int copylen;
1221 copylen = min(size - 1, len);
1222 memcpy(p, extension, copylen);
1235 *MSVCRT__errno() = MSVCRT_ERANGE;
1236 return MSVCRT_ERANGE;
1239 /*********************************************************************
1240 * _wmakepath_s (MSVCRT.@)
1242 * Safe version of _wmakepath.
1244 int CDECL _wmakepath_s(MSVCRT_wchar_t *path, MSVCRT_size_t size, const MSVCRT_wchar_t *drive,
1245 const MSVCRT_wchar_t *directory, const MSVCRT_wchar_t *filename,
1246 const MSVCRT_wchar_t *extension)
1248 MSVCRT_wchar_t *p = path;
1252 *MSVCRT__errno() = MSVCRT_EINVAL;
1253 return MSVCRT_EINVAL;
1256 if (drive && drive[0])
1266 if (directory && directory[0])
1268 unsigned int len = strlenW(directory);
1269 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1270 unsigned int copylen = min(size - 1, len);
1275 memmove(p, directory, copylen * sizeof(MSVCRT_wchar_t));
1283 if (needs_separator)
1293 if (filename && filename[0])
1295 unsigned int len = strlenW(filename);
1296 unsigned int copylen = min(size - 1, len);
1301 memmove(p, filename, copylen * sizeof(MSVCRT_wchar_t));
1310 if (extension && extension[0])
1312 unsigned int len = strlenW(extension);
1313 unsigned int needs_period = extension[0] != '.';
1314 unsigned int copylen;
1325 copylen = min(size - 1, len);
1326 memcpy(p, extension, copylen * sizeof(MSVCRT_wchar_t));
1339 *MSVCRT__errno() = MSVCRT_ERANGE;
1340 return MSVCRT_ERANGE;
1343 /*********************************************************************
1344 * _searchenv (MSVCRT.@)
1346 * Search for a file in a list of paths from an environment variable.
1349 * file [I] Name of the file to search for.
1350 * env [I] Name of the environment variable containing a list of paths.
1351 * buf [O] Destination for the found file path.
1354 * Nothing. If the file is not found, buf will contain an empty string
1357 void CDECL _searchenv(const char* file, const char* env, char *buf)
1360 char curPath[MAX_PATH];
1365 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1367 GetFullPathNameA( file, MAX_PATH, buf, NULL );
1368 /* Sigh. This error is *always* set, regardless of success */
1369 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1373 /* Search given environment variable */
1374 envVal = MSVCRT_getenv(env);
1377 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1382 TRACE(":searching for %s in paths %s\n", file, envVal);
1388 while(*end && *end != ';') end++; /* Find end of next path */
1389 if (penv == end || !*penv)
1391 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1394 memcpy(curPath, penv, end - penv);
1395 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1397 curPath[end - penv] = '\\';
1398 curPath[end - penv + 1] = '\0';
1401 curPath[end - penv] = '\0';
1403 strcat(curPath, file);
1404 TRACE("Checking for file %s\n", curPath);
1405 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1407 strcpy(buf, curPath);
1408 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1411 penv = *end ? end + 1 : end;
1415 /*********************************************************************
1416 * _wsearchenv (MSVCRT.@)
1418 * Unicode version of _searchenv
1420 void CDECL _wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1422 MSVCRT_wchar_t *envVal, *penv;
1423 MSVCRT_wchar_t curPath[MAX_PATH];
1428 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1430 GetFullPathNameW( file, MAX_PATH, buf, NULL );
1431 /* Sigh. This error is *always* set, regardless of success */
1432 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1436 /* Search given environment variable */
1437 envVal = _wgetenv(env);
1440 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1445 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1449 MSVCRT_wchar_t *end = penv;
1451 while(*end && *end != ';') end++; /* Find end of next path */
1452 if (penv == end || !*penv)
1454 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1457 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1458 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1460 curPath[end - penv] = '\\';
1461 curPath[end - penv + 1] = '\0';
1464 curPath[end - penv] = '\0';
1466 strcatW(curPath, file);
1467 TRACE("Checking for file %s\n", debugstr_w(curPath));
1468 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1470 strcpyW(buf, curPath);
1471 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1474 penv = *end ? end + 1 : end;