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_DATAW to wfinddatai64_t */
100 static void msvcrt_wfttofdi64( const WIN32_FIND_DATAW
*fd
, struct MSVCRT__wfinddatai64_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 strcpyW(ft
->name
, fd
->cFileName
);
119 /*********************************************************************
122 * Change the current working directory.
125 * newdir [I] Directory to change to
128 * Success: 0. The current working directory is set to newdir.
129 * Failure: -1. errno indicates the error.
132 * See SetCurrentDirectoryA.
134 int CDECL
MSVCRT__chdir(const char * newdir
)
136 if (!SetCurrentDirectoryA(newdir
))
138 msvcrt_set_errno(newdir
?GetLastError():0);
144 /*********************************************************************
147 * Unicode version of _chdir.
149 int CDECL
_wchdir(const MSVCRT_wchar_t
* newdir
)
151 if (!SetCurrentDirectoryW(newdir
))
153 msvcrt_set_errno(newdir
?GetLastError():0);
159 /*********************************************************************
160 * _chdrive (MSVCRT.@)
162 * Change the current drive.
165 * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
168 * Success: 0. The current drive is set to newdrive.
169 * Failure: -1. errno indicates the error.
172 * See SetCurrentDirectoryA.
174 int CDECL
_chdrive(int newdrive
)
176 WCHAR buffer
[3] = {'A', ':', 0};
178 buffer
[0] += newdrive
- 1;
179 if (!SetCurrentDirectoryW( buffer
))
181 msvcrt_set_errno(GetLastError());
183 *MSVCRT__errno() = MSVCRT_EACCES
;
189 /*********************************************************************
190 * _findclose (MSVCRT.@)
192 * Close a handle returned by _findfirst().
195 * hand [I] Handle to close
198 * Success: 0. All resources associated with hand are freed.
199 * Failure: -1. errno indicates the error.
204 int CDECL
MSVCRT__findclose(MSVCRT_intptr_t hand
)
206 TRACE(":handle %ld\n",hand
);
207 if (!FindClose((HANDLE
)hand
))
209 msvcrt_set_errno(GetLastError());
215 /*********************************************************************
216 * _findfirst (MSVCRT.@)
218 * Open a handle for iterating through a directory.
221 * fspec [I] File specification of files to iterate.
222 * ft [O] Information for the first file found.
225 * Success: A handle suitable for passing to _findnext() and _findclose().
226 * ft is populated with the details of the found file.
227 * Failure: -1. errno indicates the error.
230 * See FindFirstFileA.
232 MSVCRT_intptr_t CDECL
MSVCRT__findfirst(const char * fspec
, struct MSVCRT__finddata_t
* ft
)
234 WIN32_FIND_DATAA find_data
;
237 hfind
= FindFirstFileA(fspec
, &find_data
);
238 if (hfind
== INVALID_HANDLE_VALUE
)
240 msvcrt_set_errno(GetLastError());
243 msvcrt_fttofd(&find_data
,ft
);
244 TRACE(":got handle %p\n",hfind
);
245 return (MSVCRT_intptr_t
)hfind
;
248 /*********************************************************************
249 * _wfindfirst (MSVCRT.@)
251 * Unicode version of _findfirst.
253 MSVCRT_intptr_t CDECL
MSVCRT__wfindfirst(const MSVCRT_wchar_t
* fspec
, struct MSVCRT__wfinddata_t
* ft
)
255 WIN32_FIND_DATAW find_data
;
258 hfind
= FindFirstFileW(fspec
, &find_data
);
259 if (hfind
== INVALID_HANDLE_VALUE
)
261 msvcrt_set_errno(GetLastError());
264 msvcrt_wfttofd(&find_data
,ft
);
265 TRACE(":got handle %p\n",hfind
);
266 return (MSVCRT_intptr_t
)hfind
;
269 /*********************************************************************
270 * _findfirsti64 (MSVCRT.@)
272 * 64-bit version of _findfirst.
274 MSVCRT_intptr_t CDECL
MSVCRT__findfirsti64(const char * fspec
, struct MSVCRT__finddatai64_t
* ft
)
276 WIN32_FIND_DATAA find_data
;
279 hfind
= FindFirstFileA(fspec
, &find_data
);
280 if (hfind
== INVALID_HANDLE_VALUE
)
282 msvcrt_set_errno(GetLastError());
285 msvcrt_fttofdi64(&find_data
,ft
);
286 TRACE(":got handle %p\n",hfind
);
287 return (MSVCRT_intptr_t
)hfind
;
290 /*********************************************************************
291 * _wfindfirsti64 (MSVCRT.@)
293 * Unicode version of _findfirsti64.
295 MSVCRT_intptr_t CDECL
MSVCRT__wfindfirsti64(const MSVCRT_wchar_t
* fspec
, struct MSVCRT__wfinddatai64_t
* ft
)
297 WIN32_FIND_DATAW find_data
;
300 hfind
= FindFirstFileW(fspec
, &find_data
);
301 if (hfind
== INVALID_HANDLE_VALUE
)
303 msvcrt_set_errno(GetLastError());
306 msvcrt_wfttofdi64(&find_data
,ft
);
307 TRACE(":got handle %p\n",hfind
);
308 return (MSVCRT_intptr_t
)hfind
;
311 /*********************************************************************
312 * _findnext (MSVCRT.@)
314 * Find the next file from a file search handle.
317 * hand [I] Handle to the search returned from _findfirst().
318 * ft [O] Information for the file found.
321 * Success: 0. ft is populated with the details of the found file.
322 * Failure: -1. errno indicates the error.
327 int CDECL
MSVCRT__findnext(MSVCRT_intptr_t hand
, struct MSVCRT__finddata_t
* ft
)
329 WIN32_FIND_DATAA find_data
;
331 if (!FindNextFileA((HANDLE
)hand
, &find_data
))
333 *MSVCRT__errno() = MSVCRT_ENOENT
;
337 msvcrt_fttofd(&find_data
,ft
);
341 /*********************************************************************
342 * _wfindnext (MSVCRT.@)
344 * Unicode version of _findnext.
346 int CDECL
MSVCRT__wfindnext(MSVCRT_intptr_t hand
, struct MSVCRT__wfinddata_t
* ft
)
348 WIN32_FIND_DATAW find_data
;
350 if (!FindNextFileW((HANDLE
)hand
, &find_data
))
352 *MSVCRT__errno() = MSVCRT_ENOENT
;
356 msvcrt_wfttofd(&find_data
,ft
);
360 /*********************************************************************
361 * _findnexti64 (MSVCRT.@)
363 * 64-bit version of _findnext.
365 int CDECL
MSVCRT__findnexti64(MSVCRT_intptr_t hand
, struct MSVCRT__finddatai64_t
* ft
)
367 WIN32_FIND_DATAA find_data
;
369 if (!FindNextFileA((HANDLE
)hand
, &find_data
))
371 *MSVCRT__errno() = MSVCRT_ENOENT
;
375 msvcrt_fttofdi64(&find_data
,ft
);
379 /*********************************************************************
380 * _wfindnexti64 (MSVCRT.@)
382 * Unicode version of _findnexti64.
384 int CDECL
MSVCRT__wfindnexti64(MSVCRT_intptr_t hand
, struct MSVCRT__wfinddatai64_t
* ft
)
386 WIN32_FIND_DATAW find_data
;
388 if (!FindNextFileW((HANDLE
)hand
, &find_data
))
390 *MSVCRT__errno() = MSVCRT_ENOENT
;
394 msvcrt_wfttofdi64(&find_data
,ft
);
398 /*********************************************************************
401 * Get the current working directory.
404 * buf [O] Destination for current working directory.
405 * size [I] Size of buf in characters
408 * Success: If buf is NULL, returns an allocated string containing the path.
409 * Otherwise populates buf with the path and returns it.
410 * Failure: NULL. errno indicates the error.
412 char* CDECL
_getcwd(char * buf
, int size
)
415 int dir_len
= GetCurrentDirectoryA(MAX_PATH
,dir
);
418 return NULL
; /* FIXME: Real return value untested */
422 if (size
<= dir_len
) size
= dir_len
+ 1;
423 if (!(buf
= MSVCRT_malloc( size
))) return NULL
;
425 else if (dir_len
>= size
)
427 *MSVCRT__errno() = MSVCRT_ERANGE
;
428 return NULL
; /* buf too small */
434 /*********************************************************************
435 * _wgetcwd (MSVCRT.@)
437 * Unicode version of _getcwd.
439 MSVCRT_wchar_t
* CDECL
_wgetcwd(MSVCRT_wchar_t
* buf
, int size
)
441 MSVCRT_wchar_t dir
[MAX_PATH
];
442 int dir_len
= GetCurrentDirectoryW(MAX_PATH
,dir
);
445 return NULL
; /* FIXME: Real return value untested */
449 if (size
<= dir_len
) size
= dir_len
+ 1;
450 if (!(buf
= MSVCRT_malloc( size
* sizeof(WCHAR
) ))) return NULL
;
454 *MSVCRT__errno() = MSVCRT_ERANGE
;
455 return NULL
; /* buf too small */
461 /*********************************************************************
462 * _getdrive (MSVCRT.@)
464 * Get the current drive number.
470 * Success: The drive letter number from 1 to 26 ("A:" to "Z:").
473 int CDECL
_getdrive(void)
475 WCHAR buffer
[MAX_PATH
];
476 if (GetCurrentDirectoryW( MAX_PATH
, buffer
) &&
477 buffer
[0] >= 'A' && buffer
[0] <= 'z' && buffer
[1] == ':')
478 return toupperW(buffer
[0]) - 'A' + 1;
482 /*********************************************************************
483 * _getdcwd (MSVCRT.@)
485 * Get the current working directory on a given disk.
488 * drive [I] Drive letter to get the current working directory from.
489 * buf [O] Destination for the current working directory.
490 * size [I] Length of drive in characters.
493 * Success: If drive is NULL, returns an allocated string containing the path.
494 * Otherwise populates drive with the path and returns it.
495 * Failure: NULL. errno indicates the error.
497 char* CDECL
_getdcwd(int drive
, char * buf
, int size
)
501 TRACE(":drive %d(%c), size %d\n",drive
, drive
+ 'A' - 1, size
);
503 if (!drive
|| drive
== _getdrive())
504 return _getcwd(buf
,size
); /* current */
508 char drivespec
[4] = {'A', ':', 0};
511 drivespec
[0] += drive
- 1;
512 if (GetDriveTypeA(drivespec
) < DRIVE_REMOVABLE
)
514 *MSVCRT__errno() = MSVCRT_EACCES
;
518 dir_len
= GetFullPathNameA(drivespec
,MAX_PATH
,dir
,&dummy
);
519 if (dir_len
>= size
|| dir_len
< 1)
521 *MSVCRT__errno() = MSVCRT_ERANGE
;
522 return NULL
; /* buf too small */
525 TRACE(":returning '%s'\n", dir
);
527 return _strdup(dir
); /* allocate */
534 /*********************************************************************
535 * _wgetdcwd (MSVCRT.@)
537 * Unicode version of _wgetdcwd.
539 MSVCRT_wchar_t
* CDECL
_wgetdcwd(int drive
, MSVCRT_wchar_t
* buf
, int size
)
541 static MSVCRT_wchar_t
* dummy
;
543 TRACE(":drive %d(%c), size %d\n",drive
, drive
+ 'A' - 1, size
);
545 if (!drive
|| drive
== _getdrive())
546 return _wgetcwd(buf
,size
); /* current */
549 MSVCRT_wchar_t dir
[MAX_PATH
];
550 MSVCRT_wchar_t drivespec
[4] = {'A', ':', '\\', 0};
553 drivespec
[0] += drive
- 1;
554 if (GetDriveTypeW(drivespec
) < DRIVE_REMOVABLE
)
556 *MSVCRT__errno() = MSVCRT_EACCES
;
560 dir_len
= GetFullPathNameW(drivespec
,MAX_PATH
,dir
,&dummy
);
561 if (dir_len
>= size
|| dir_len
< 1)
563 *MSVCRT__errno() = MSVCRT_ERANGE
;
564 return NULL
; /* buf too small */
567 TRACE(":returning %s\n", debugstr_w(dir
));
569 return _wcsdup(dir
); /* allocate */
575 /*********************************************************************
576 * _getdiskfree (MSVCRT.@)
578 * Get information about the free space on a drive.
581 * disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
582 * info [O] Destination for the resulting information.
585 * Success: 0. info is updated with the free space information.
586 * Failure: An error code from GetLastError().
589 * See GetLastError().
591 unsigned int CDECL
MSVCRT__getdiskfree(unsigned int disk
, struct MSVCRT__diskfree_t
* d
)
593 WCHAR drivespec
[4] = {'@', ':', '\\', 0};
598 return ERROR_INVALID_PARAMETER
; /* MSVCRT doesn't set errno here */
600 drivespec
[0] += disk
; /* make a drive letter */
602 if (GetDiskFreeSpaceW(disk
==0?NULL
:drivespec
,ret
,ret
+1,ret
+2,ret
+3))
604 d
->sectors_per_cluster
= ret
[0];
605 d
->bytes_per_sector
= ret
[1];
606 d
->avail_clusters
= ret
[2];
607 d
->total_clusters
= ret
[3];
610 err
= GetLastError();
611 msvcrt_set_errno(err
);
615 /*********************************************************************
618 * Create a directory.
621 * newdir [I] Name of directory to create.
624 * Success: 0. The directory indicated by newdir is created.
625 * Failure: -1. errno indicates the error.
628 * See CreateDirectoryA.
630 int CDECL
MSVCRT__mkdir(const char * newdir
)
632 if (CreateDirectoryA(newdir
,NULL
))
634 msvcrt_set_errno(GetLastError());
638 /*********************************************************************
641 * Unicode version of _mkdir.
643 int CDECL
_wmkdir(const MSVCRT_wchar_t
* newdir
)
645 if (CreateDirectoryW(newdir
,NULL
))
647 msvcrt_set_errno(GetLastError());
651 /*********************************************************************
654 * Delete a directory.
657 * dir [I] Name of directory to delete.
660 * Success: 0. The directory indicated by newdir is deleted.
661 * Failure: -1. errno indicates the error.
664 * See RemoveDirectoryA.
666 int CDECL
MSVCRT__rmdir(const char * dir
)
668 if (RemoveDirectoryA(dir
))
670 msvcrt_set_errno(GetLastError());
674 /*********************************************************************
677 * Unicode version of _rmdir.
679 int CDECL
_wrmdir(const MSVCRT_wchar_t
* dir
)
681 if (RemoveDirectoryW(dir
))
683 msvcrt_set_errno(GetLastError());
687 /*********************************************************************
688 * _wsplitpath (MSVCRT.@)
690 * Unicode version of _splitpath.
692 void CDECL
_wsplitpath(const MSVCRT_wchar_t
*inpath
, MSVCRT_wchar_t
*drv
, MSVCRT_wchar_t
*dir
,
693 MSVCRT_wchar_t
*fname
, MSVCRT_wchar_t
*ext
)
695 const MSVCRT_wchar_t
*p
, *end
;
697 if (inpath
[0] && inpath
[1] == ':')
707 else if (drv
) drv
[0] = 0;
709 /* look for end of directory part */
711 for (p
= inpath
; *p
; p
++) if (*p
== '/' || *p
== '\\') end
= p
+ 1;
713 if (end
) /* got a directory */
717 memcpy( dir
, inpath
, (end
- inpath
) * sizeof(MSVCRT_wchar_t
) );
718 dir
[end
- inpath
] = 0;
722 else if (dir
) dir
[0] = 0;
724 /* look for extension: what's after the last dot */
726 for (p
= inpath
; *p
; p
++) if (*p
== '.') end
= p
;
728 if (!end
) end
= p
; /* there's no extension */
732 memcpy( fname
, inpath
, (end
- inpath
) * sizeof(MSVCRT_wchar_t
) );
733 fname
[end
- inpath
] = 0;
735 if (ext
) strcpyW( ext
, end
);
738 /******************************************************************
739 * _wsplitpath_s (MSVCRT.@)
741 * Secure version of _wsplitpath
743 int _wsplitpath_s(const MSVCRT_wchar_t
* inpath
,
744 MSVCRT_wchar_t
* drive
, MSVCRT_size_t sz_drive
,
745 MSVCRT_wchar_t
* dir
, MSVCRT_size_t sz_dir
,
746 MSVCRT_wchar_t
* fname
, MSVCRT_size_t sz_fname
,
747 MSVCRT_wchar_t
* ext
, MSVCRT_size_t sz_ext
)
749 const MSVCRT_wchar_t
*p
, *end
;
751 if (!inpath
) return MSVCRT_EINVAL
;
752 if (!drive
&& sz_drive
) return MSVCRT_EINVAL
;
753 if (drive
&& !sz_drive
) return MSVCRT_EINVAL
;
754 if (!dir
&& sz_dir
) return MSVCRT_EINVAL
;
755 if (dir
&& !sz_dir
) return MSVCRT_EINVAL
;
756 if (!fname
&& sz_fname
) return MSVCRT_EINVAL
;
757 if (fname
&& !sz_fname
) return MSVCRT_EINVAL
;
758 if (!ext
&& sz_ext
) return MSVCRT_EINVAL
;
759 if (ext
&& !sz_ext
) return MSVCRT_EINVAL
;
761 if (inpath
[0] && inpath
[1] == ':')
765 if (sz_drive
<= 2) goto do_error
;
766 drive
[0] = inpath
[0];
767 drive
[1] = inpath
[1];
772 else if (drive
) drive
[0] = '\0';
774 /* look for end of directory part */
776 for (p
= inpath
; *p
; p
++) if (*p
== '/' || *p
== '\\') end
= p
+ 1;
778 if (end
) /* got a directory */
782 if (sz_dir
<= end
- inpath
) goto do_error
;
783 memcpy( dir
, inpath
, (end
- inpath
) * sizeof(MSVCRT_wchar_t
) );
784 dir
[end
- inpath
] = 0;
788 else if (dir
) dir
[0] = 0;
790 /* look for extension: what's after the last dot */
792 for (p
= inpath
; *p
; p
++) if (*p
== '.') end
= p
;
794 if (!end
) end
= p
; /* there's no extension */
798 if (sz_fname
<= end
- inpath
) goto do_error
;
799 memcpy( fname
, inpath
, (end
- inpath
) * sizeof(MSVCRT_wchar_t
) );
800 fname
[end
- inpath
] = 0;
804 if (sz_ext
<= strlenW(end
)) goto do_error
;
809 if (drive
) drive
[0] = '\0';
810 if (dir
) dir
[0] = '\0';
811 if (fname
) fname
[0]= '\0';
812 if (ext
) ext
[0]= '\0';
813 return MSVCRT_ERANGE
;
816 /*********************************************************************
817 * _wfullpath (MSVCRT.@)
819 * Unicode version of _fullpath.
821 MSVCRT_wchar_t
* CDECL
_wfullpath(MSVCRT_wchar_t
* absPath
, const MSVCRT_wchar_t
* relPath
, MSVCRT_size_t size
)
826 BOOL alloced
= FALSE
;
828 if (!relPath
|| !*relPath
)
829 return _wgetcwd(absPath
, size
);
833 buffer
= MSVCRT_malloc(MAX_PATH
* sizeof(WCHAR
));
842 *MSVCRT__errno() = MSVCRT_ERANGE
;
846 TRACE(":resolving relative path %s\n",debugstr_w(relPath
));
848 rc
= GetFullPathNameW(relPath
,size
,buffer
,&lastpart
);
850 if (rc
> 0 && rc
<= size
)
860 /*********************************************************************
861 * _fullpath (MSVCRT.@)
863 * Create an absolute path from a relative path.
866 * absPath [O] Destination for absolute path
867 * relPath [I] Relative path to convert to absolute
868 * size [I] Length of absPath in characters.
871 * Success: If absPath is NULL, returns an allocated string containing the path.
872 * Otherwise populates absPath with the path and returns it.
873 * Failure: NULL. errno indicates the error.
875 char * CDECL
_fullpath(char * absPath
, const char* relPath
, unsigned int size
)
880 BOOL alloced
= FALSE
;
882 if (!relPath
|| !*relPath
)
883 return _getcwd(absPath
, size
);
887 buffer
= MSVCRT_malloc(MAX_PATH
);
896 *MSVCRT__errno() = MSVCRT_ERANGE
;
900 TRACE(":resolving relative path '%s'\n",relPath
);
902 rc
= GetFullPathNameA(relPath
,size
,buffer
,&lastpart
);
904 if (rc
> 0 && rc
<= size
)
914 /*********************************************************************
915 * _makepath (MSVCRT.@)
920 * path [O] Destination for created pathname
921 * drive [I] Drive letter (e.g. "A:")
922 * directory [I] Directory
923 * filename [I] Name of the file, excluding extension
924 * extension [I] File extension (e.g. ".TXT")
927 * Nothing. If path is not large enough to hold the resulting pathname,
928 * random process memory will be overwritten.
930 VOID CDECL
_makepath(char * path
, const char * drive
,
931 const char *directory
, const char * filename
,
932 const char * extension
)
936 TRACE("(%s %s %s %s)\n", debugstr_a(drive
), debugstr_a(directory
),
937 debugstr_a(filename
), debugstr_a(extension
) );
942 if (drive
&& drive
[0])
947 if (directory
&& directory
[0])
949 unsigned int len
= strlen(directory
);
950 memmove(p
, directory
, len
);
952 if (p
[-1] != '/' && p
[-1] != '\\')
955 if (filename
&& filename
[0])
957 unsigned int len
= strlen(filename
);
958 memmove(p
, filename
, len
);
961 if (extension
&& extension
[0])
963 if (extension
[0] != '.')
965 strcpy(p
, extension
);
969 TRACE("returning %s\n",path
);
972 /*********************************************************************
973 * _wmakepath (MSVCRT.@)
975 * Unicode version of _wmakepath.
977 VOID CDECL
_wmakepath(MSVCRT_wchar_t
*path
, const MSVCRT_wchar_t
*drive
, const MSVCRT_wchar_t
*directory
,
978 const MSVCRT_wchar_t
*filename
, const MSVCRT_wchar_t
*extension
)
980 MSVCRT_wchar_t
*p
= path
;
982 TRACE("%s %s %s %s\n", debugstr_w(drive
), debugstr_w(directory
),
983 debugstr_w(filename
), debugstr_w(extension
));
988 if (drive
&& drive
[0])
993 if (directory
&& directory
[0])
995 unsigned int len
= strlenW(directory
);
996 memmove(p
, directory
, len
* sizeof(MSVCRT_wchar_t
));
998 if (p
[-1] != '/' && p
[-1] != '\\')
1001 if (filename
&& filename
[0])
1003 unsigned int len
= strlenW(filename
);
1004 memmove(p
, filename
, len
* sizeof(MSVCRT_wchar_t
));
1007 if (extension
&& extension
[0])
1009 if (extension
[0] != '.')
1011 strcpyW(p
, extension
);
1016 TRACE("returning %s\n", debugstr_w(path
));
1019 /*********************************************************************
1020 * _searchenv (MSVCRT.@)
1022 * Search for a file in a list of paths from an environment variable.
1025 * file [I] Name of the file to search for.
1026 * env [I] Name of the environment variable containing a list of paths.
1027 * buf [O] Destination for the found file path.
1030 * Nothing. If the file is not found, buf will contain an empty string
1033 void CDECL
_searchenv(const char* file
, const char* env
, char *buf
)
1036 char curPath
[MAX_PATH
];
1041 if (GetFileAttributesA( file
) != INVALID_FILE_ATTRIBUTES
)
1043 GetFullPathNameA( file
, MAX_PATH
, buf
, NULL
);
1044 /* Sigh. This error is *always* set, regardless of success */
1045 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1049 /* Search given environment variable */
1050 envVal
= MSVCRT_getenv(env
);
1053 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1058 TRACE(":searching for %s in paths %s\n", file
, envVal
);
1064 while(*end
&& *end
!= ';') end
++; /* Find end of next path */
1065 if (penv
== end
|| !*penv
)
1067 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1070 memcpy(curPath
, penv
, end
- penv
);
1071 if (curPath
[end
- penv
] != '/' && curPath
[end
- penv
] != '\\')
1073 curPath
[end
- penv
] = '\\';
1074 curPath
[end
- penv
+ 1] = '\0';
1077 curPath
[end
- penv
] = '\0';
1079 strcat(curPath
, file
);
1080 TRACE("Checking for file %s\n", curPath
);
1081 if (GetFileAttributesA( curPath
) != INVALID_FILE_ATTRIBUTES
)
1083 strcpy(buf
, curPath
);
1084 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1087 penv
= *end
? end
+ 1 : end
;
1091 /*********************************************************************
1092 * _wsearchenv (MSVCRT.@)
1094 * Unicode version of _searchenv
1096 void CDECL
_wsearchenv(const MSVCRT_wchar_t
* file
, const MSVCRT_wchar_t
* env
, MSVCRT_wchar_t
*buf
)
1098 MSVCRT_wchar_t
*envVal
, *penv
;
1099 MSVCRT_wchar_t curPath
[MAX_PATH
];
1104 if (GetFileAttributesW( file
) != INVALID_FILE_ATTRIBUTES
)
1106 GetFullPathNameW( file
, MAX_PATH
, buf
, NULL
);
1107 /* Sigh. This error is *always* set, regardless of success */
1108 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1112 /* Search given environment variable */
1113 envVal
= _wgetenv(env
);
1116 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1121 TRACE(":searching for %s in paths %s\n", debugstr_w(file
), debugstr_w(envVal
));
1125 MSVCRT_wchar_t
*end
= penv
;
1127 while(*end
&& *end
!= ';') end
++; /* Find end of next path */
1128 if (penv
== end
|| !*penv
)
1130 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1133 memcpy(curPath
, penv
, (end
- penv
) * sizeof(MSVCRT_wchar_t
));
1134 if (curPath
[end
- penv
] != '/' && curPath
[end
- penv
] != '\\')
1136 curPath
[end
- penv
] = '\\';
1137 curPath
[end
- penv
+ 1] = '\0';
1140 curPath
[end
- penv
] = '\0';
1142 strcatW(curPath
, file
);
1143 TRACE("Checking for file %s\n", debugstr_w(curPath
));
1144 if (GetFileAttributesW( curPath
) != INVALID_FILE_ATTRIBUTES
)
1146 strcpyW(buf
, curPath
);
1147 msvcrt_set_errno(ERROR_FILE_NOT_FOUND
);
1150 penv
= *end
? end
+ 1 : end
;