msvcrt: Implement _makepath_s.
[wine/hacks.git] / dlls / msvcrt / dir.c
blobe3862f3ad20a42b93377c8acd3479488ab303834
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <time.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "wine/unicode.h"
34 #include "msvcrt.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)
42 DWORD dw;
44 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
45 ft->attrib = 0;
46 else
47 ft->attrib = fd->dwFileAttributes;
49 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
50 ft->time_create = dw;
51 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
52 ft->time_access = dw;
53 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
54 ft->time_write = 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)
62 DWORD dw;
64 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
65 ft->attrib = 0;
66 else
67 ft->attrib = fd->dwFileAttributes;
69 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
70 ft->time_create = dw;
71 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
72 ft->time_access = dw;
73 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
74 ft->time_write = 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)
82 DWORD dw;
84 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
85 ft->attrib = 0;
86 else
87 ft->attrib = fd->dwFileAttributes;
89 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
90 ft->time_create = dw;
91 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
92 ft->time_access = dw;
93 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
94 ft->time_write = 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)
102 DWORD dw;
104 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
105 ft->attrib = 0;
106 else
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 );
114 ft->time_write = 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)
123 DWORD dw;
125 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
126 ft->attrib = 0;
127 else
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 );
135 ft->time_write = dw;
136 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
137 strcpyW(ft->name, fd->cFileName);
140 /*********************************************************************
141 * _chdir (MSVCRT.@)
143 * Change the current working directory.
145 * PARAMS
146 * newdir [I] Directory to change to
148 * RETURNS
149 * Success: 0. The current working directory is set to newdir.
150 * Failure: -1. errno indicates the error.
152 * NOTES
153 * See SetCurrentDirectoryA.
155 int CDECL MSVCRT__chdir(const char * newdir)
157 if (!SetCurrentDirectoryA(newdir))
159 msvcrt_set_errno(newdir?GetLastError():0);
160 return -1;
162 return 0;
165 /*********************************************************************
166 * _wchdir (MSVCRT.@)
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);
175 return -1;
177 return 0;
180 /*********************************************************************
181 * _chdrive (MSVCRT.@)
183 * Change the current drive.
185 * PARAMS
186 * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
188 * RETURNS
189 * Success: 0. The current drive is set to newdrive.
190 * Failure: -1. errno indicates the error.
192 * NOTES
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());
203 if (newdrive <= 0)
204 *MSVCRT__errno() = MSVCRT_EACCES;
205 return -1;
207 return 0;
210 /*********************************************************************
211 * _findclose (MSVCRT.@)
213 * Close a handle returned by _findfirst().
215 * PARAMS
216 * hand [I] Handle to close
218 * RETURNS
219 * Success: 0. All resources associated with hand are freed.
220 * Failure: -1. errno indicates the error.
222 * NOTES
223 * See FindClose.
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());
231 return -1;
233 return 0;
236 /*********************************************************************
237 * _findfirst (MSVCRT.@)
239 * Open a handle for iterating through a directory.
241 * PARAMS
242 * fspec [I] File specification of files to iterate.
243 * ft [O] Information for the first file found.
245 * RETURNS
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.
250 * NOTES
251 * See FindFirstFileA.
253 MSVCRT_intptr_t CDECL MSVCRT__findfirst(const char * fspec, struct MSVCRT__finddata_t* ft)
255 WIN32_FIND_DATAA find_data;
256 HANDLE hfind;
258 hfind = FindFirstFileA(fspec, &find_data);
259 if (hfind == INVALID_HANDLE_VALUE)
261 msvcrt_set_errno(GetLastError());
262 return -1;
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;
277 HANDLE hfind;
279 hfind = FindFirstFileW(fspec, &find_data);
280 if (hfind == INVALID_HANDLE_VALUE)
282 msvcrt_set_errno(GetLastError());
283 return -1;
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;
298 HANDLE hfind;
300 hfind = FindFirstFileA(fspec, &find_data);
301 if (hfind == INVALID_HANDLE_VALUE)
303 msvcrt_set_errno(GetLastError());
304 return -1;
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;
319 HANDLE hfind;
321 hfind = FindFirstFileA(fspec, &find_data);
322 if (hfind == INVALID_HANDLE_VALUE)
324 msvcrt_set_errno(GetLastError());
325 return -1;
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;
340 HANDLE hfind;
342 hfind = FindFirstFileW(fspec, &find_data);
343 if (hfind == INVALID_HANDLE_VALUE)
345 msvcrt_set_errno(GetLastError());
346 return -1;
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.
358 * PARAMS
359 * hand [I] Handle to the search returned from _findfirst().
360 * ft [O] Information for the file found.
362 * RETURNS
363 * Success: 0. ft is populated with the details of the found file.
364 * Failure: -1. errno indicates the error.
366 * NOTES
367 * See FindNextFileA.
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;
376 return -1;
379 msvcrt_fttofd(&find_data,ft);
380 return 0;
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;
395 return -1;
398 msvcrt_wfttofd(&find_data,ft);
399 return 0;
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;
414 return -1;
417 msvcrt_fttofdi64(&find_data,ft);
418 return 0;
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;
433 return -1;
436 msvcrt_fttofd64(&find_data,ft);
437 return 0;
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;
452 return -1;
455 msvcrt_wfttofdi64(&find_data,ft);
456 return 0;
459 /*********************************************************************
460 * _getcwd (MSVCRT.@)
462 * Get the current working directory.
464 * PARAMS
465 * buf [O] Destination for current working directory.
466 * size [I] Size of buf in characters
468 * RETURNS
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)
475 char dir[MAX_PATH];
476 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
478 if (dir_len < 1)
479 return NULL; /* FIXME: Real return value untested */
481 if (!buf)
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 */
491 strcpy(buf,dir);
492 return buf;
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);
505 if (dir_len < 1)
506 return NULL; /* FIXME: Real return value untested */
508 if (!buf)
510 if (size <= dir_len) size = dir_len + 1;
511 if (!(buf = MSVCRT_malloc( size * sizeof(WCHAR) ))) return NULL;
513 if (dir_len >= size)
515 *MSVCRT__errno() = MSVCRT_ERANGE;
516 return NULL; /* buf too small */
518 strcpyW(buf,dir);
519 return buf;
522 /*********************************************************************
523 * _getdrive (MSVCRT.@)
525 * Get the current drive number.
527 * PARAMS
528 * None.
530 * RETURNS
531 * Success: The drive letter number from 1 to 26 ("A:" to "Z:").
532 * Failure: 0.
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;
540 return 0;
543 /*********************************************************************
544 * _getdcwd (MSVCRT.@)
546 * Get the current working directory on a given disk.
548 * PARAMS
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.
553 * RETURNS
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)
560 static char* dummy;
562 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
564 if (!drive || drive == _getdrive())
565 return _getcwd(buf,size); /* current */
566 else
568 char dir[MAX_PATH];
569 char drivespec[4] = {'A', ':', 0};
570 int dir_len;
572 drivespec[0] += drive - 1;
573 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
575 *MSVCRT__errno() = MSVCRT_EACCES;
576 return NULL;
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);
587 if (!buf)
588 return _strdup(dir); /* allocate */
590 strcpy(buf,dir);
592 return buf;
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 */
608 else
610 MSVCRT_wchar_t dir[MAX_PATH];
611 MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
612 int dir_len;
614 drivespec[0] += drive - 1;
615 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
617 *MSVCRT__errno() = MSVCRT_EACCES;
618 return NULL;
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));
629 if (!buf)
630 return _wcsdup(dir); /* allocate */
631 strcpyW(buf,dir);
633 return buf;
636 /*********************************************************************
637 * _getdiskfree (MSVCRT.@)
639 * Get information about the free space on a drive.
641 * PARAMS
642 * disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
643 * info [O] Destination for the resulting information.
645 * RETURNS
646 * Success: 0. info is updated with the free space information.
647 * Failure: An error code from GetLastError().
649 * NOTES
650 * See GetLastError().
652 unsigned int CDECL MSVCRT__getdiskfree(unsigned int disk, struct MSVCRT__diskfree_t * d)
654 WCHAR drivespec[4] = {'@', ':', '\\', 0};
655 DWORD ret[4];
656 unsigned int err;
658 if (disk > 26)
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];
669 return 0;
671 err = GetLastError();
672 msvcrt_set_errno(err);
673 return err;
676 /*********************************************************************
677 * _mkdir (MSVCRT.@)
679 * Create a directory.
681 * PARAMS
682 * newdir [I] Name of directory to create.
684 * RETURNS
685 * Success: 0. The directory indicated by newdir is created.
686 * Failure: -1. errno indicates the error.
688 * NOTES
689 * See CreateDirectoryA.
691 int CDECL MSVCRT__mkdir(const char * newdir)
693 if (CreateDirectoryA(newdir,NULL))
694 return 0;
695 msvcrt_set_errno(GetLastError());
696 return -1;
699 /*********************************************************************
700 * _wmkdir (MSVCRT.@)
702 * Unicode version of _mkdir.
704 int CDECL _wmkdir(const MSVCRT_wchar_t* newdir)
706 if (CreateDirectoryW(newdir,NULL))
707 return 0;
708 msvcrt_set_errno(GetLastError());
709 return -1;
712 /*********************************************************************
713 * _rmdir (MSVCRT.@)
715 * Delete a directory.
717 * PARAMS
718 * dir [I] Name of directory to delete.
720 * RETURNS
721 * Success: 0. The directory indicated by newdir is deleted.
722 * Failure: -1. errno indicates the error.
724 * NOTES
725 * See RemoveDirectoryA.
727 int CDECL MSVCRT__rmdir(const char * dir)
729 if (RemoveDirectoryA(dir))
730 return 0;
731 msvcrt_set_errno(GetLastError());
732 return -1;
735 /*********************************************************************
736 * _wrmdir (MSVCRT.@)
738 * Unicode version of _rmdir.
740 int CDECL _wrmdir(const MSVCRT_wchar_t * dir)
742 if (RemoveDirectoryW(dir))
743 return 0;
744 msvcrt_set_errno(GetLastError());
745 return -1;
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] == ':')
760 if (drv)
762 drv[0] = inpath[0];
763 drv[1] = inpath[1];
764 drv[2] = 0;
766 inpath += 2;
768 else if (drv) drv[0] = 0;
770 /* look for end of directory part */
771 end = NULL;
772 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
774 if (end) /* got a directory */
776 if (dir)
778 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
779 dir[end - inpath] = 0;
781 inpath = end;
783 else if (dir) dir[0] = 0;
785 /* look for extension: what's after the last dot */
786 end = NULL;
787 for (p = inpath; *p; p++) if (*p == '.') end = p;
789 if (!end) end = p; /* there's no extension */
791 if (fname)
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] == ':')
824 if (drive)
826 if (sz_drive <= 2) goto do_error;
827 drive[0] = inpath[0];
828 drive[1] = inpath[1];
829 drive[2] = 0;
831 inpath += 2;
833 else if (drive) drive[0] = '\0';
835 /* look for end of directory part */
836 end = NULL;
837 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
839 if (end) /* got a directory */
841 if (dir)
843 if (sz_dir <= end - inpath) goto do_error;
844 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
845 dir[end - inpath] = 0;
847 inpath = end;
849 else if (dir) dir[0] = 0;
851 /* look for extension: what's after the last dot */
852 end = NULL;
853 for (p = inpath; *p; p++) if (*p == '.') end = p;
855 if (!end) end = p; /* there's no extension */
857 if (fname)
859 if (sz_fname <= end - inpath) goto do_error;
860 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
861 fname[end - inpath] = 0;
863 if (ext)
865 if (sz_ext <= strlenW(end)) goto do_error;
866 strcpyW( ext, end );
868 return 0;
869 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)
884 DWORD rc;
885 WCHAR* buffer;
886 WCHAR* lastpart;
887 BOOL alloced = FALSE;
889 if (!relPath || !*relPath)
890 return _wgetcwd(absPath, size);
892 if (absPath == NULL)
894 buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
895 size = MAX_PATH;
896 alloced = TRUE;
898 else
899 buffer = absPath;
901 if (size < 4)
903 *MSVCRT__errno() = MSVCRT_ERANGE;
904 return NULL;
907 TRACE(":resolving relative path %s\n",debugstr_w(relPath));
909 rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
911 if (rc > 0 && rc <= size )
912 return buffer;
913 else
915 if (alloced)
916 MSVCRT_free(buffer);
917 return NULL;
921 /*********************************************************************
922 * _fullpath (MSVCRT.@)
924 * Create an absolute path from a relative path.
926 * PARAMS
927 * absPath [O] Destination for absolute path
928 * relPath [I] Relative path to convert to absolute
929 * size [I] Length of absPath in characters.
931 * RETURNS
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)
938 DWORD rc;
939 char* lastpart;
940 char* buffer;
941 BOOL alloced = FALSE;
943 if (!relPath || !*relPath)
944 return _getcwd(absPath, size);
946 if (absPath == NULL)
948 buffer = MSVCRT_malloc(MAX_PATH);
949 size = MAX_PATH;
950 alloced = TRUE;
952 else
953 buffer = absPath;
955 if (size < 4)
957 *MSVCRT__errno() = MSVCRT_ERANGE;
958 return NULL;
961 TRACE(":resolving relative path '%s'\n",relPath);
963 rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
965 if (rc > 0 && rc <= size)
966 return buffer;
967 else
969 if (alloced)
970 MSVCRT_free(buffer);
971 return NULL;
975 /*********************************************************************
976 * _makepath (MSVCRT.@)
978 * Create a pathname.
980 * PARAMS
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")
987 * RETURNS
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)
995 char *p = path;
997 TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
998 debugstr_a(filename), debugstr_a(extension) );
1000 if ( !path )
1001 return;
1003 if (drive && drive[0])
1005 *p++ = drive[0];
1006 *p++ = ':';
1008 if (directory && directory[0])
1010 unsigned int len = strlen(directory);
1011 memmove(p, directory, len);
1012 p += len;
1013 if (p[-1] != '/' && p[-1] != '\\')
1014 *p++ = '\\';
1016 if (filename && filename[0])
1018 unsigned int len = strlen(filename);
1019 memmove(p, filename, len);
1020 p += len;
1022 if (extension && extension[0])
1024 if (extension[0] != '.')
1025 *p++ = '.';
1026 strcpy(p, extension);
1028 else
1029 *p = '\0';
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));
1046 if ( !path )
1047 return;
1049 if (drive && drive[0])
1051 *p++ = drive[0];
1052 *p++ = ':';
1054 if (directory && directory[0])
1056 unsigned int len = strlenW(directory);
1057 memmove(p, directory, len * sizeof(MSVCRT_wchar_t));
1058 p += len;
1059 if (p[-1] != '/' && p[-1] != '\\')
1060 *p++ = '\\';
1062 if (filename && filename[0])
1064 unsigned int len = strlenW(filename);
1065 memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1066 p += len;
1068 if (extension && extension[0])
1070 if (extension[0] != '.')
1071 *p++ = '.';
1072 strcpyW(p, extension);
1074 else
1075 *p = '\0';
1077 TRACE("returning %s\n", debugstr_w(path));
1080 /*********************************************************************
1081 * _makepath_s (MSVCRT.@)
1083 * Safe version of _makepath.
1085 int CDECL _makepath_s(char *path, MSVCRT_size_t size, const char *drive,
1086 const char *directory, const char *filename,
1087 const char *extension)
1089 char *p = path;
1091 if (!path || !size)
1093 *MSVCRT__errno() = MSVCRT_EINVAL;
1094 return MSVCRT_EINVAL;
1097 if (drive && drive[0])
1099 if (size <= 2)
1100 goto range;
1102 *p++ = drive[0];
1103 *p++ = ':';
1104 size -= 2;
1107 if (directory && directory[0])
1109 unsigned int len = strlen(directory);
1110 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1111 unsigned int copylen = min(size - 1, len);
1113 if (size < 2)
1114 goto range;
1116 memmove(p, directory, copylen);
1118 if (size <= len)
1119 goto range;
1121 p += copylen;
1122 size -= copylen;
1124 if (needs_separator)
1126 if (size < 2)
1127 goto range;
1129 *p++ = '\\';
1130 size -= 1;
1134 if (filename && filename[0])
1136 unsigned int len = strlen(filename);
1137 unsigned int copylen = min(size - 1, len);
1139 if (size < 2)
1140 goto range;
1142 memmove(p, filename, copylen);
1144 if (size <= len)
1145 goto range;
1147 p += len;
1148 size -= len;
1151 if (extension && extension[0])
1153 unsigned int len = strlen(extension);
1154 unsigned int needs_period = extension[0] != '.';
1155 unsigned int copylen;
1157 if (size < 2)
1158 goto range;
1160 if (needs_period)
1162 *p++ = '.';
1163 size -= 1;
1166 copylen = min(size - 1, len);
1167 memcpy(p, extension, copylen);
1169 if (size <= len)
1170 goto range;
1172 p += copylen;
1175 *p = '\0';
1176 return 0;
1178 range:
1179 path[0] = '\0';
1180 *MSVCRT__errno() = MSVCRT_ERANGE;
1181 return MSVCRT_ERANGE;
1184 /*********************************************************************
1185 * _searchenv (MSVCRT.@)
1187 * Search for a file in a list of paths from an environment variable.
1189 * PARAMS
1190 * file [I] Name of the file to search for.
1191 * env [I] Name of the environment variable containing a list of paths.
1192 * buf [O] Destination for the found file path.
1194 * RETURNS
1195 * Nothing. If the file is not found, buf will contain an empty string
1196 * and errno is set.
1198 void CDECL _searchenv(const char* file, const char* env, char *buf)
1200 char*envVal, *penv;
1201 char curPath[MAX_PATH];
1203 *buf = '\0';
1205 /* Try CWD first */
1206 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1208 GetFullPathNameA( file, MAX_PATH, buf, NULL );
1209 /* Sigh. This error is *always* set, regardless of success */
1210 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1211 return;
1214 /* Search given environment variable */
1215 envVal = MSVCRT_getenv(env);
1216 if (!envVal)
1218 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1219 return;
1222 penv = envVal;
1223 TRACE(":searching for %s in paths %s\n", file, envVal);
1227 char *end = penv;
1229 while(*end && *end != ';') end++; /* Find end of next path */
1230 if (penv == end || !*penv)
1232 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1233 return;
1235 memcpy(curPath, penv, end - penv);
1236 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1238 curPath[end - penv] = '\\';
1239 curPath[end - penv + 1] = '\0';
1241 else
1242 curPath[end - penv] = '\0';
1244 strcat(curPath, file);
1245 TRACE("Checking for file %s\n", curPath);
1246 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1248 strcpy(buf, curPath);
1249 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1250 return; /* Found */
1252 penv = *end ? end + 1 : end;
1253 } while(1);
1256 /*********************************************************************
1257 * _wsearchenv (MSVCRT.@)
1259 * Unicode version of _searchenv
1261 void CDECL _wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1263 MSVCRT_wchar_t *envVal, *penv;
1264 MSVCRT_wchar_t curPath[MAX_PATH];
1266 *buf = '\0';
1268 /* Try CWD first */
1269 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1271 GetFullPathNameW( file, MAX_PATH, buf, NULL );
1272 /* Sigh. This error is *always* set, regardless of success */
1273 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1274 return;
1277 /* Search given environment variable */
1278 envVal = _wgetenv(env);
1279 if (!envVal)
1281 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1282 return;
1285 penv = envVal;
1286 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1290 MSVCRT_wchar_t *end = penv;
1292 while(*end && *end != ';') end++; /* Find end of next path */
1293 if (penv == end || !*penv)
1295 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1296 return;
1298 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1299 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1301 curPath[end - penv] = '\\';
1302 curPath[end - penv + 1] = '\0';
1304 else
1305 curPath[end - penv] = '\0';
1307 strcatW(curPath, file);
1308 TRACE("Checking for file %s\n", debugstr_w(curPath));
1309 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1311 strcpyW(buf, curPath);
1312 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1313 return; /* Found */
1315 penv = *end ? end + 1 : end;
1316 } while(1);