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 * _wsplitpath (MSVCRT.@)
751 * Unicode version of _splitpath.
753 void CDECL _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
754 MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext )
756 const MSVCRT_wchar_t *p, *end;
758 if (inpath[0] && inpath[1] == ':')
768 else if (drv) drv[0] = 0;
770 /* look for end of directory part */
772 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
774 if (end) /* got a directory */
778 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
779 dir[end - inpath] = 0;
783 else if (dir) dir[0] = 0;
785 /* look for extension: what's after the last dot */
787 for (p = inpath; *p; p++) if (*p == '.') end = p;
789 if (!end) end = p; /* there's no extension */
793 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
794 fname[end - inpath] = 0;
796 if (ext) strcpyW( ext, end );
799 /******************************************************************
800 * _wsplitpath_s (MSVCRT.@)
802 * Secure version of _wsplitpath
804 int _wsplitpath_s(const MSVCRT_wchar_t* inpath,
805 MSVCRT_wchar_t* drive, MSVCRT_size_t sz_drive,
806 MSVCRT_wchar_t* dir, MSVCRT_size_t sz_dir,
807 MSVCRT_wchar_t* fname, MSVCRT_size_t sz_fname,
808 MSVCRT_wchar_t* ext, MSVCRT_size_t sz_ext)
810 const MSVCRT_wchar_t *p, *end;
812 if (!inpath) return MSVCRT_EINVAL;
813 if (!drive && sz_drive) return MSVCRT_EINVAL;
814 if (drive && !sz_drive) return MSVCRT_EINVAL;
815 if (!dir && sz_dir) return MSVCRT_EINVAL;
816 if (dir && !sz_dir) return MSVCRT_EINVAL;
817 if (!fname && sz_fname) return MSVCRT_EINVAL;
818 if (fname && !sz_fname) return MSVCRT_EINVAL;
819 if (!ext && sz_ext) return MSVCRT_EINVAL;
820 if (ext && !sz_ext) return MSVCRT_EINVAL;
822 if (inpath[0] && inpath[1] == ':')
826 if (sz_drive <= 2) goto do_error;
827 drive[0] = inpath[0];
828 drive[1] = inpath[1];
833 else if (drive) drive[0] = '\0';
835 /* look for end of directory part */
837 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
839 if (end) /* got a directory */
843 if (sz_dir <= end - inpath) goto do_error;
844 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
845 dir[end - inpath] = 0;
849 else if (dir) dir[0] = 0;
851 /* look for extension: what's after the last dot */
853 for (p = inpath; *p; p++) if (*p == '.') end = p;
855 if (!end) end = p; /* there's no extension */
859 if (sz_fname <= end - inpath) goto do_error;
860 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
861 fname[end - inpath] = 0;
865 if (sz_ext <= strlenW(end)) goto do_error;
870 if (drive) drive[0] = '\0';
871 if (dir) dir[0] = '\0';
872 if (fname) fname[0]= '\0';
873 if (ext) ext[0]= '\0';
874 return MSVCRT_ERANGE;
877 /*********************************************************************
878 * _wfullpath (MSVCRT.@)
880 * Unicode version of _fullpath.
882 MSVCRT_wchar_t * CDECL _wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, MSVCRT_size_t size)
887 BOOL alloced = FALSE;
889 if (!relPath || !*relPath)
890 return _wgetcwd(absPath, size);
894 buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
903 *MSVCRT__errno() = MSVCRT_ERANGE;
907 TRACE(":resolving relative path %s\n",debugstr_w(relPath));
909 rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
911 if (rc > 0 && rc <= size )
921 /*********************************************************************
922 * _fullpath (MSVCRT.@)
924 * Create an absolute path from a relative path.
927 * absPath [O] Destination for absolute path
928 * relPath [I] Relative path to convert to absolute
929 * size [I] Length of absPath in characters.
932 * Success: If absPath is NULL, returns an allocated string containing the path.
933 * Otherwise populates absPath with the path and returns it.
934 * Failure: NULL. errno indicates the error.
936 char * CDECL _fullpath(char * absPath, const char* relPath, unsigned int size)
941 BOOL alloced = FALSE;
943 if (!relPath || !*relPath)
944 return _getcwd(absPath, size);
948 buffer = MSVCRT_malloc(MAX_PATH);
957 *MSVCRT__errno() = MSVCRT_ERANGE;
961 TRACE(":resolving relative path '%s'\n",relPath);
963 rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
965 if (rc > 0 && rc <= size)
975 /*********************************************************************
976 * _makepath (MSVCRT.@)
981 * path [O] Destination for created pathname
982 * drive [I] Drive letter (e.g. "A:")
983 * directory [I] Directory
984 * filename [I] Name of the file, excluding extension
985 * extension [I] File extension (e.g. ".TXT")
988 * Nothing. If path is not large enough to hold the resulting pathname,
989 * random process memory will be overwritten.
991 VOID CDECL _makepath(char * path, const char * drive,
992 const char *directory, const char * filename,
993 const char * extension)
997 TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
998 debugstr_a(filename), debugstr_a(extension) );
1003 if (drive && drive[0])
1008 if (directory && directory[0])
1010 unsigned int len = strlen(directory);
1011 memmove(p, directory, len);
1013 if (p[-1] != '/' && p[-1] != '\\')
1016 if (filename && filename[0])
1018 unsigned int len = strlen(filename);
1019 memmove(p, filename, len);
1022 if (extension && extension[0])
1024 if (extension[0] != '.')
1026 strcpy(p, extension);
1030 TRACE("returning %s\n",path);
1033 /*********************************************************************
1034 * _wmakepath (MSVCRT.@)
1036 * Unicode version of _wmakepath.
1038 VOID CDECL _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
1039 const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
1041 MSVCRT_wchar_t *p = path;
1043 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
1044 debugstr_w(filename), debugstr_w(extension));
1049 if (drive && drive[0])
1054 if (directory && directory[0])
1056 unsigned int len = strlenW(directory);
1057 memmove(p, directory, len * sizeof(MSVCRT_wchar_t));
1059 if (p[-1] != '/' && p[-1] != '\\')
1062 if (filename && filename[0])
1064 unsigned int len = strlenW(filename);
1065 memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1068 if (extension && extension[0])
1070 if (extension[0] != '.')
1072 strcpyW(p, extension);
1077 TRACE("returning %s\n", debugstr_w(path));
1080 /*********************************************************************
1081 * _searchenv (MSVCRT.@)
1083 * Search for a file in a list of paths from an environment variable.
1086 * file [I] Name of the file to search for.
1087 * env [I] Name of the environment variable containing a list of paths.
1088 * buf [O] Destination for the found file path.
1091 * Nothing. If the file is not found, buf will contain an empty string
1094 void CDECL _searchenv(const char* file, const char* env, char *buf)
1097 char curPath[MAX_PATH];
1102 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1104 GetFullPathNameA( file, MAX_PATH, buf, NULL );
1105 /* Sigh. This error is *always* set, regardless of success */
1106 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1110 /* Search given environment variable */
1111 envVal = MSVCRT_getenv(env);
1114 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1119 TRACE(":searching for %s in paths %s\n", file, envVal);
1125 while(*end && *end != ';') end++; /* Find end of next path */
1126 if (penv == end || !*penv)
1128 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1131 memcpy(curPath, penv, end - penv);
1132 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1134 curPath[end - penv] = '\\';
1135 curPath[end - penv + 1] = '\0';
1138 curPath[end - penv] = '\0';
1140 strcat(curPath, file);
1141 TRACE("Checking for file %s\n", curPath);
1142 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1144 strcpy(buf, curPath);
1145 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1148 penv = *end ? end + 1 : end;
1152 /*********************************************************************
1153 * _wsearchenv (MSVCRT.@)
1155 * Unicode version of _searchenv
1157 void CDECL _wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1159 MSVCRT_wchar_t *envVal, *penv;
1160 MSVCRT_wchar_t curPath[MAX_PATH];
1165 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1167 GetFullPathNameW( file, MAX_PATH, buf, NULL );
1168 /* Sigh. This error is *always* set, regardless of success */
1169 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1173 /* Search given environment variable */
1174 envVal = _wgetenv(env);
1177 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1182 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1186 MSVCRT_wchar_t *end = penv;
1188 while(*end && *end != ';') end++; /* Find end of next path */
1189 if (penv == end || !*penv)
1191 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1194 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1195 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1197 curPath[end - penv] = '\\';
1198 curPath[end - penv + 1] = '\0';
1201 curPath[end - penv] = '\0';
1203 strcatW(curPath, file);
1204 TRACE("Checking for file %s\n", debugstr_w(curPath));
1205 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1207 strcpyW(buf, curPath);
1208 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1211 penv = *end ? end + 1 : end;