mshtml: Use proper helpers in IHTMLStyle::color implementation.
[wine.git] / dlls / msvcrt / dir.c
blob8043cba655d5afb3f10b12ebb0563d424049175e
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_DATAA to finddata32_t */
60 static void msvcrt_fttofd32( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata32_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 strcpy(ft->name, fd->cFileName);
79 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata_t */
80 static void msvcrt_wfttofd( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata_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 = fd->nFileSizeLow;
96 strcpyW(ft->name, fd->cFileName);
99 /* INTERNAL: Translate WIN32_FIND_DATAA to finddatai64_t */
100 static void msvcrt_fttofdi64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddatai64_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);
119 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64_t */
120 static void msvcrt_fttofd64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64_t* ft)
122 DWORD dw;
124 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
125 ft->attrib = 0;
126 else
127 ft->attrib = fd->dwFileAttributes;
129 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
130 ft->time_create = dw;
131 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
132 ft->time_access = dw;
133 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
134 ft->time_write = dw;
135 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
136 strcpy(ft->name, fd->cFileName);
139 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64_t */
140 static void msvcrt_wfttofd64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64_t* ft)
142 DWORD dw;
144 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
145 ft->attrib = 0;
146 else
147 ft->attrib = fd->dwFileAttributes;
149 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
150 ft->time_create = dw;
151 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
152 ft->time_access = dw;
153 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
154 ft->time_write = dw;
155 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
156 strcpyW(ft->name, fd->cFileName);
159 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64i32_t */
160 static void msvcrt_fttofd64i32( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64i32_t* ft)
162 DWORD dw;
164 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
165 ft->attrib = 0;
166 else
167 ft->attrib = fd->dwFileAttributes;
169 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
170 ft->time_create = dw;
171 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
172 ft->time_access = dw;
173 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
174 ft->time_write = dw;
175 ft->size = fd->nFileSizeLow;
176 strcpy(ft->name, fd->cFileName);
179 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddatai64_t */
180 static void msvcrt_wfttofdi64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddatai64_t* ft)
182 DWORD dw;
184 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
185 ft->attrib = 0;
186 else
187 ft->attrib = fd->dwFileAttributes;
189 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
190 ft->time_create = dw;
191 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
192 ft->time_access = dw;
193 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
194 ft->time_write = dw;
195 ft->size = ((__int64)fd->nFileSizeHigh) << 32 | fd->nFileSizeLow;
196 strcpyW(ft->name, fd->cFileName);
199 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64i32_t */
200 static void msvcrt_wfttofd64i32( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64i32_t* ft)
202 DWORD dw;
204 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
205 ft->attrib = 0;
206 else
207 ft->attrib = fd->dwFileAttributes;
209 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftCreationTime, &dw );
210 ft->time_create = dw;
211 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
212 ft->time_access = dw;
213 RtlTimeToSecondsSince1970( (const LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
214 ft->time_write = dw;
215 ft->size = fd->nFileSizeLow;
216 strcpyW(ft->name, fd->cFileName);
219 /*********************************************************************
220 * _chdir (MSVCRT.@)
222 * Change the current working directory.
224 * PARAMS
225 * newdir [I] Directory to change to
227 * RETURNS
228 * Success: 0. The current working directory is set to newdir.
229 * Failure: -1. errno indicates the error.
231 * NOTES
232 * See SetCurrentDirectoryA.
234 int CDECL MSVCRT__chdir(const char * newdir)
236 if (!SetCurrentDirectoryA(newdir))
238 msvcrt_set_errno(newdir?GetLastError():0);
239 return -1;
241 return 0;
244 /*********************************************************************
245 * _wchdir (MSVCRT.@)
247 * Unicode version of _chdir.
249 int CDECL MSVCRT__wchdir(const MSVCRT_wchar_t * newdir)
251 if (!SetCurrentDirectoryW(newdir))
253 msvcrt_set_errno(newdir?GetLastError():0);
254 return -1;
256 return 0;
259 /*********************************************************************
260 * _chdrive (MSVCRT.@)
262 * Change the current drive.
264 * PARAMS
265 * newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
267 * RETURNS
268 * Success: 0. The current drive is set to newdrive.
269 * Failure: -1. errno indicates the error.
271 * NOTES
272 * See SetCurrentDirectoryA.
274 int CDECL MSVCRT__chdrive(int newdrive)
276 WCHAR buffer[] = {'A', ':', 0};
278 buffer[0] += newdrive - 1;
279 if (!SetCurrentDirectoryW( buffer ))
281 msvcrt_set_errno(GetLastError());
282 if (newdrive <= 0)
283 *MSVCRT__errno() = MSVCRT_EACCES;
284 return -1;
286 return 0;
289 /*********************************************************************
290 * _findclose (MSVCRT.@)
292 * Close a handle returned by _findfirst().
294 * PARAMS
295 * hand [I] Handle to close
297 * RETURNS
298 * Success: 0. All resources associated with hand are freed.
299 * Failure: -1. errno indicates the error.
301 * NOTES
302 * See FindClose.
304 int CDECL MSVCRT__findclose(MSVCRT_intptr_t hand)
306 TRACE(":handle %ld\n",hand);
307 if (!FindClose((HANDLE)hand))
309 msvcrt_set_errno(GetLastError());
310 return -1;
312 return 0;
315 /*********************************************************************
316 * _findfirst (MSVCRT.@)
318 * Open a handle for iterating through a directory.
320 * PARAMS
321 * fspec [I] File specification of files to iterate.
322 * ft [O] Information for the first file found.
324 * RETURNS
325 * Success: A handle suitable for passing to _findnext() and _findclose().
326 * ft is populated with the details of the found file.
327 * Failure: -1. errno indicates the error.
329 * NOTES
330 * See FindFirstFileA.
332 MSVCRT_intptr_t CDECL MSVCRT__findfirst(const char * fspec, struct MSVCRT__finddata_t* ft)
334 WIN32_FIND_DATAA find_data;
335 HANDLE hfind;
337 hfind = FindFirstFileA(fspec, &find_data);
338 if (hfind == INVALID_HANDLE_VALUE)
340 msvcrt_set_errno(GetLastError());
341 return -1;
343 msvcrt_fttofd(&find_data,ft);
344 TRACE(":got handle %p\n",hfind);
345 return (MSVCRT_intptr_t)hfind;
348 /*********************************************************************
349 * _findfirst32 (MSVCRT.@)
351 MSVCRT_intptr_t CDECL MSVCRT__findfirst32(const char * fspec, struct MSVCRT__finddata32_t* ft)
353 WIN32_FIND_DATAA find_data;
354 HANDLE hfind;
356 hfind = FindFirstFileA(fspec, &find_data);
357 if (hfind == INVALID_HANDLE_VALUE)
359 msvcrt_set_errno(GetLastError());
360 return -1;
362 msvcrt_fttofd32(&find_data, ft);
363 TRACE(":got handle %p\n", hfind);
364 return (MSVCRT_intptr_t)hfind;
367 /*********************************************************************
368 * _wfindfirst (MSVCRT.@)
370 * Unicode version of _findfirst.
372 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata_t* ft)
374 WIN32_FIND_DATAW find_data;
375 HANDLE hfind;
377 hfind = FindFirstFileW(fspec, &find_data);
378 if (hfind == INVALID_HANDLE_VALUE)
380 msvcrt_set_errno(GetLastError());
381 return -1;
383 msvcrt_wfttofd(&find_data,ft);
384 TRACE(":got handle %p\n",hfind);
385 return (MSVCRT_intptr_t)hfind;
388 /*********************************************************************
389 * _findfirsti64 (MSVCRT.@)
391 * 64-bit version of _findfirst.
393 MSVCRT_intptr_t CDECL MSVCRT__findfirsti64(const char * fspec, struct MSVCRT__finddatai64_t* ft)
395 WIN32_FIND_DATAA find_data;
396 HANDLE hfind;
398 hfind = FindFirstFileA(fspec, &find_data);
399 if (hfind == INVALID_HANDLE_VALUE)
401 msvcrt_set_errno(GetLastError());
402 return -1;
404 msvcrt_fttofdi64(&find_data,ft);
405 TRACE(":got handle %p\n",hfind);
406 return (MSVCRT_intptr_t)hfind;
409 /*********************************************************************
410 * _findfirst64 (MSVCRT.@)
412 * 64-bit version of _findfirst.
414 MSVCRT_intptr_t CDECL MSVCRT__findfirst64(const char * fspec, struct MSVCRT__finddata64_t* ft)
416 WIN32_FIND_DATAA find_data;
417 HANDLE hfind;
419 hfind = FindFirstFileA(fspec, &find_data);
420 if (hfind == INVALID_HANDLE_VALUE)
422 msvcrt_set_errno(GetLastError());
423 return -1;
425 msvcrt_fttofd64(&find_data,ft);
426 TRACE(":got handle %p\n",hfind);
427 return (MSVCRT_intptr_t)hfind;
430 /*********************************************************************
431 * _wfindfirst64 (MSVCRT.@)
433 * Unicode version of _findfirst64.
435 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64_t* ft)
437 WIN32_FIND_DATAW find_data;
438 HANDLE hfind;
440 hfind = FindFirstFileW(fspec, &find_data);
441 if (hfind == INVALID_HANDLE_VALUE)
443 msvcrt_set_errno(GetLastError());
444 return -1;
446 msvcrt_wfttofd64(&find_data,ft);
447 TRACE(":got handle %p\n",hfind);
448 return (MSVCRT_intptr_t)hfind;
451 /*********************************************************************
452 * _findfirst64i32 (MSVCRT.@)
454 * 64-bit/32-bit version of _findfirst.
456 MSVCRT_intptr_t CDECL MSVCRT__findfirst64i32(const char * fspec, struct MSVCRT__finddata64i32_t* ft)
458 WIN32_FIND_DATAA find_data;
459 HANDLE hfind;
461 hfind = FindFirstFileA(fspec, &find_data);
462 if (hfind == INVALID_HANDLE_VALUE)
464 msvcrt_set_errno(GetLastError());
465 return -1;
467 msvcrt_fttofd64i32(&find_data,ft);
468 TRACE(":got handle %p\n",hfind);
469 return (MSVCRT_intptr_t)hfind;
472 /*********************************************************************
473 * _wfindfirst64i32 (MSVCRT.@)
475 * Unicode version of _findfirst64i32.
477 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64i32(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64i32_t* ft)
479 WIN32_FIND_DATAW find_data;
480 HANDLE hfind;
482 hfind = FindFirstFileW(fspec, &find_data);
483 if (hfind == INVALID_HANDLE_VALUE)
485 msvcrt_set_errno(GetLastError());
486 return -1;
488 msvcrt_wfttofd64i32(&find_data,ft);
489 TRACE(":got handle %p\n",hfind);
490 return (MSVCRT_intptr_t)hfind;
493 /*********************************************************************
494 * _wfindfirsti64 (MSVCRT.@)
496 * Unicode version of _findfirsti64.
498 MSVCRT_intptr_t CDECL MSVCRT__wfindfirsti64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddatai64_t* ft)
500 WIN32_FIND_DATAW find_data;
501 HANDLE hfind;
503 hfind = FindFirstFileW(fspec, &find_data);
504 if (hfind == INVALID_HANDLE_VALUE)
506 msvcrt_set_errno(GetLastError());
507 return -1;
509 msvcrt_wfttofdi64(&find_data,ft);
510 TRACE(":got handle %p\n",hfind);
511 return (MSVCRT_intptr_t)hfind;
514 /*********************************************************************
515 * _findnext (MSVCRT.@)
517 * Find the next file from a file search handle.
519 * PARAMS
520 * hand [I] Handle to the search returned from _findfirst().
521 * ft [O] Information for the file found.
523 * RETURNS
524 * Success: 0. ft is populated with the details of the found file.
525 * Failure: -1. errno indicates the error.
527 * NOTES
528 * See FindNextFileA.
530 int CDECL MSVCRT__findnext(MSVCRT_intptr_t hand, struct MSVCRT__finddata_t * ft)
532 WIN32_FIND_DATAA find_data;
534 if (!FindNextFileA((HANDLE)hand, &find_data))
536 *MSVCRT__errno() = MSVCRT_ENOENT;
537 return -1;
540 msvcrt_fttofd(&find_data,ft);
541 return 0;
544 /*********************************************************************
545 * _findnext32 (MSVCRT.@)
547 int CDECL MSVCRT__findnext32(MSVCRT_intptr_t hand, struct MSVCRT__finddata32_t * ft)
549 WIN32_FIND_DATAA find_data;
551 if (!FindNextFileA((HANDLE)hand, &find_data))
553 *MSVCRT__errno() = MSVCRT_ENOENT;
554 return -1;
557 msvcrt_fttofd32(&find_data, ft);
558 return 0;
561 /*********************************************************************
562 * _wfindnext (MSVCRT.@)
564 * Unicode version of _findnext.
566 int CDECL MSVCRT__wfindnext(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata_t * ft)
568 WIN32_FIND_DATAW find_data;
570 if (!FindNextFileW((HANDLE)hand, &find_data))
572 *MSVCRT__errno() = MSVCRT_ENOENT;
573 return -1;
576 msvcrt_wfttofd(&find_data,ft);
577 return 0;
580 /*********************************************************************
581 * _findnexti64 (MSVCRT.@)
583 * 64-bit version of _findnext.
585 int CDECL MSVCRT__findnexti64(MSVCRT_intptr_t hand, struct MSVCRT__finddatai64_t * ft)
587 WIN32_FIND_DATAA find_data;
589 if (!FindNextFileA((HANDLE)hand, &find_data))
591 *MSVCRT__errno() = MSVCRT_ENOENT;
592 return -1;
595 msvcrt_fttofdi64(&find_data,ft);
596 return 0;
599 /*********************************************************************
600 * _findnext64 (MSVCRT.@)
602 * 64-bit version of _findnext.
604 int CDECL MSVCRT__findnext64(MSVCRT_intptr_t hand, struct MSVCRT__finddata64_t * ft)
606 WIN32_FIND_DATAA find_data;
608 if (!FindNextFileA((HANDLE)hand, &find_data))
610 *MSVCRT__errno() = MSVCRT_ENOENT;
611 return -1;
614 msvcrt_fttofd64(&find_data,ft);
615 return 0;
618 /*********************************************************************
619 * _wfindnext64 (MSVCRT.@)
621 * Unicode version of _wfindnext64.
623 int CDECL MSVCRT__wfindnext64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64_t * ft)
625 WIN32_FIND_DATAW find_data;
627 if (!FindNextFileW((HANDLE)hand, &find_data))
629 *MSVCRT__errno() = MSVCRT_ENOENT;
630 return -1;
633 msvcrt_wfttofd64(&find_data,ft);
634 return 0;
637 /*********************************************************************
638 * _findnext64i32 (MSVCRT.@)
640 * 64-bit/32-bit version of _findnext.
642 int CDECL MSVCRT__findnext64i32(MSVCRT_intptr_t hand, struct MSVCRT__finddata64i32_t * ft)
644 WIN32_FIND_DATAA find_data;
646 if (!FindNextFileA((HANDLE)hand, &find_data))
648 *MSVCRT__errno() = MSVCRT_ENOENT;
649 return -1;
652 msvcrt_fttofd64i32(&find_data,ft);
653 return 0;
656 /*********************************************************************
657 * _wfindnexti64 (MSVCRT.@)
659 * Unicode version of _findnexti64.
661 int CDECL MSVCRT__wfindnexti64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddatai64_t * ft)
663 WIN32_FIND_DATAW find_data;
665 if (!FindNextFileW((HANDLE)hand, &find_data))
667 *MSVCRT__errno() = MSVCRT_ENOENT;
668 return -1;
671 msvcrt_wfttofdi64(&find_data,ft);
672 return 0;
675 /*********************************************************************
676 * _wfindnext64i32 (MSVCRT.@)
678 * Unicode version of _findnext64i32.
680 int CDECL MSVCRT__wfindnext64i32(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64i32_t * ft)
682 WIN32_FIND_DATAW find_data;
684 if (!FindNextFileW((HANDLE)hand, &find_data))
686 *MSVCRT__errno() = MSVCRT_ENOENT;
687 return -1;
690 msvcrt_wfttofd64i32(&find_data,ft);
691 return 0;
694 /*********************************************************************
695 * _getcwd (MSVCRT.@)
697 * Get the current working directory.
699 * PARAMS
700 * buf [O] Destination for current working directory.
701 * size [I] Size of buf in characters
703 * RETURNS
704 * Success: If buf is NULL, returns an allocated string containing the path.
705 * Otherwise populates buf with the path and returns it.
706 * Failure: NULL. errno indicates the error.
708 char* CDECL MSVCRT__getcwd(char * buf, int size)
710 char dir[MAX_PATH];
711 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
713 if (dir_len < 1)
714 return NULL; /* FIXME: Real return value untested */
716 if (!buf)
718 if (size <= dir_len) size = dir_len + 1;
719 if (!(buf = MSVCRT_malloc( size ))) return NULL;
721 else if (dir_len >= size)
723 *MSVCRT__errno() = MSVCRT_ERANGE;
724 return NULL; /* buf too small */
726 strcpy(buf,dir);
727 return buf;
730 /*********************************************************************
731 * _wgetcwd (MSVCRT.@)
733 * Unicode version of _getcwd.
735 MSVCRT_wchar_t* CDECL MSVCRT__wgetcwd(MSVCRT_wchar_t * buf, int size)
737 MSVCRT_wchar_t dir[MAX_PATH];
738 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
740 if (dir_len < 1)
741 return NULL; /* FIXME: Real return value untested */
743 if (!buf)
745 if (size <= dir_len) size = dir_len + 1;
746 if (!(buf = MSVCRT_malloc( size * sizeof(WCHAR) ))) return NULL;
748 if (dir_len >= size)
750 *MSVCRT__errno() = MSVCRT_ERANGE;
751 return NULL; /* buf too small */
753 strcpyW(buf,dir);
754 return buf;
757 /*********************************************************************
758 * _getdrive (MSVCRT.@)
760 * Get the current drive number.
762 * PARAMS
763 * None.
765 * RETURNS
766 * Success: The drive letter number from 1 to 26 ("A:" to "Z:").
767 * Failure: 0.
769 int CDECL MSVCRT__getdrive(void)
771 WCHAR buffer[MAX_PATH];
772 if (GetCurrentDirectoryW( MAX_PATH, buffer ) &&
773 buffer[0] >= 'A' && buffer[0] <= 'z' && buffer[1] == ':')
774 return toupperW(buffer[0]) - 'A' + 1;
775 return 0;
778 /*********************************************************************
779 * _getdcwd (MSVCRT.@)
781 * Get the current working directory on a given disk.
783 * PARAMS
784 * drive [I] Drive letter to get the current working directory from.
785 * buf [O] Destination for the current working directory.
786 * size [I] Length of drive in characters.
788 * RETURNS
789 * Success: If drive is NULL, returns an allocated string containing the path.
790 * Otherwise populates drive with the path and returns it.
791 * Failure: NULL. errno indicates the error.
793 char* CDECL MSVCRT__getdcwd(int drive, char * buf, int size)
795 static char* dummy;
797 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
799 if (!drive || drive == MSVCRT__getdrive())
800 return MSVCRT__getcwd(buf,size); /* current */
801 else
803 char dir[MAX_PATH];
804 char drivespec[] = {'A', ':', 0};
805 int dir_len;
807 drivespec[0] += drive - 1;
808 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
810 *MSVCRT__errno() = MSVCRT_EACCES;
811 return NULL;
814 dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
815 if (dir_len >= size || dir_len < 1)
817 *MSVCRT__errno() = MSVCRT_ERANGE;
818 return NULL; /* buf too small */
821 TRACE(":returning '%s'\n", dir);
822 if (!buf)
823 return MSVCRT__strdup(dir); /* allocate */
825 strcpy(buf,dir);
827 return buf;
830 /*********************************************************************
831 * _wgetdcwd (MSVCRT.@)
833 * Unicode version of _wgetdcwd.
835 MSVCRT_wchar_t* CDECL MSVCRT__wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
837 static MSVCRT_wchar_t* dummy;
839 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
841 if (!drive || drive == MSVCRT__getdrive())
842 return MSVCRT__wgetcwd(buf,size); /* current */
843 else
845 MSVCRT_wchar_t dir[MAX_PATH];
846 MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
847 int dir_len;
849 drivespec[0] += drive - 1;
850 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
852 *MSVCRT__errno() = MSVCRT_EACCES;
853 return NULL;
856 dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
857 if (dir_len >= size || dir_len < 1)
859 *MSVCRT__errno() = MSVCRT_ERANGE;
860 return NULL; /* buf too small */
863 TRACE(":returning %s\n", debugstr_w(dir));
864 if (!buf)
865 return MSVCRT__wcsdup(dir); /* allocate */
866 strcpyW(buf,dir);
868 return buf;
871 /*********************************************************************
872 * _getdiskfree (MSVCRT.@)
874 * Get information about the free space on a drive.
876 * PARAMS
877 * disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
878 * info [O] Destination for the resulting information.
880 * RETURNS
881 * Success: 0. info is updated with the free space information.
882 * Failure: An error code from GetLastError().
884 * NOTES
885 * See GetLastError().
887 unsigned int CDECL MSVCRT__getdiskfree(unsigned int disk, struct MSVCRT__diskfree_t * d)
889 WCHAR drivespec[] = {'@', ':', '\\', 0};
890 DWORD ret[4];
891 unsigned int err;
893 if (disk > 26)
894 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
896 drivespec[0] += disk; /* make a drive letter */
898 if (GetDiskFreeSpaceW(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
900 d->sectors_per_cluster = ret[0];
901 d->bytes_per_sector = ret[1];
902 d->avail_clusters = ret[2];
903 d->total_clusters = ret[3];
904 return 0;
906 err = GetLastError();
907 msvcrt_set_errno(err);
908 return err;
911 /*********************************************************************
912 * _mkdir (MSVCRT.@)
914 * Create a directory.
916 * PARAMS
917 * newdir [I] Name of directory to create.
919 * RETURNS
920 * Success: 0. The directory indicated by newdir is created.
921 * Failure: -1. errno indicates the error.
923 * NOTES
924 * See CreateDirectoryA.
926 int CDECL MSVCRT__mkdir(const char * newdir)
928 if (CreateDirectoryA(newdir,NULL))
929 return 0;
930 msvcrt_set_errno(GetLastError());
931 return -1;
934 /*********************************************************************
935 * _wmkdir (MSVCRT.@)
937 * Unicode version of _mkdir.
939 int CDECL MSVCRT__wmkdir(const MSVCRT_wchar_t* newdir)
941 if (CreateDirectoryW(newdir,NULL))
942 return 0;
943 msvcrt_set_errno(GetLastError());
944 return -1;
947 /*********************************************************************
948 * _rmdir (MSVCRT.@)
950 * Delete a directory.
952 * PARAMS
953 * dir [I] Name of directory to delete.
955 * RETURNS
956 * Success: 0. The directory indicated by newdir is deleted.
957 * Failure: -1. errno indicates the error.
959 * NOTES
960 * See RemoveDirectoryA.
962 int CDECL MSVCRT__rmdir(const char * dir)
964 if (RemoveDirectoryA(dir))
965 return 0;
966 msvcrt_set_errno(GetLastError());
967 return -1;
970 /*********************************************************************
971 * _wrmdir (MSVCRT.@)
973 * Unicode version of _rmdir.
975 int CDECL MSVCRT__wrmdir(const MSVCRT_wchar_t * dir)
977 if (RemoveDirectoryW(dir))
978 return 0;
979 msvcrt_set_errno(GetLastError());
980 return -1;
983 /******************************************************************
984 * _splitpath_s (MSVCRT.@)
986 int CDECL _splitpath_s(const char* inpath,
987 char* drive, MSVCRT_size_t sz_drive,
988 char* dir, MSVCRT_size_t sz_dir,
989 char* fname, MSVCRT_size_t sz_fname,
990 char* ext, MSVCRT_size_t sz_ext)
992 const char *p, *end;
994 if (!inpath || (!drive && sz_drive) ||
995 (drive && !sz_drive) ||
996 (!dir && sz_dir) ||
997 (dir && !sz_dir) ||
998 (!fname && sz_fname) ||
999 (fname && !sz_fname) ||
1000 (!ext && sz_ext) ||
1001 (ext && !sz_ext))
1003 *MSVCRT__errno() = MSVCRT_EINVAL;
1004 return MSVCRT_EINVAL;
1007 if (inpath[0] && inpath[1] == ':')
1009 if (drive)
1011 if (sz_drive <= 2) goto do_error;
1012 drive[0] = inpath[0];
1013 drive[1] = inpath[1];
1014 drive[2] = 0;
1016 inpath += 2;
1018 else if (drive) drive[0] = '\0';
1020 /* look for end of directory part */
1021 end = NULL;
1022 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1024 if (end) /* got a directory */
1026 if (dir)
1028 if (sz_dir <= end - inpath) goto do_error;
1029 memcpy( dir, inpath, (end - inpath) );
1030 dir[end - inpath] = 0;
1032 inpath = end;
1034 else if (dir) dir[0] = 0;
1036 /* look for extension: what's after the last dot */
1037 end = NULL;
1038 for (p = inpath; *p; p++) if (*p == '.') end = p;
1040 if (!end) end = p; /* there's no extension */
1042 if (fname)
1044 if (sz_fname <= end - inpath) goto do_error;
1045 memcpy( fname, inpath, (end - inpath) );
1046 fname[end - inpath] = 0;
1048 if (ext)
1050 if (sz_ext <= strlen(end)) goto do_error;
1051 strcpy( ext, end );
1053 return 0;
1054 do_error:
1055 if (drive) drive[0] = '\0';
1056 if (dir) dir[0] = '\0';
1057 if (fname) fname[0]= '\0';
1058 if (ext) ext[0]= '\0';
1059 *MSVCRT__errno() = MSVCRT_ERANGE;
1060 return MSVCRT_ERANGE;
1063 /*********************************************************************
1064 * _splitpath (MSVCRT.@)
1066 void CDECL _splitpath(const char *inpath, char *drv, char *dir,
1067 char *fname, char *ext)
1069 _splitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
1070 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
1073 /******************************************************************
1074 * _wsplitpath_s (MSVCRT.@)
1076 * Secure version of _wsplitpath
1078 int CDECL _wsplitpath_s(const MSVCRT_wchar_t* inpath,
1079 MSVCRT_wchar_t* drive, MSVCRT_size_t sz_drive,
1080 MSVCRT_wchar_t* dir, MSVCRT_size_t sz_dir,
1081 MSVCRT_wchar_t* fname, MSVCRT_size_t sz_fname,
1082 MSVCRT_wchar_t* ext, MSVCRT_size_t sz_ext)
1084 const MSVCRT_wchar_t *p, *end;
1086 if (!inpath || (!drive && sz_drive) ||
1087 (drive && !sz_drive) ||
1088 (!dir && sz_dir) ||
1089 (dir && !sz_dir) ||
1090 (!fname && sz_fname) ||
1091 (fname && !sz_fname) ||
1092 (!ext && sz_ext) ||
1093 (ext && !sz_ext))
1095 *MSVCRT__errno() = MSVCRT_EINVAL;
1096 return MSVCRT_EINVAL;
1099 if (inpath[0] && inpath[1] == ':')
1101 if (drive)
1103 if (sz_drive <= 2) goto do_error;
1104 drive[0] = inpath[0];
1105 drive[1] = inpath[1];
1106 drive[2] = 0;
1108 inpath += 2;
1110 else if (drive) drive[0] = '\0';
1112 /* look for end of directory part */
1113 end = NULL;
1114 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1116 if (end) /* got a directory */
1118 if (dir)
1120 if (sz_dir <= end - inpath) goto do_error;
1121 memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1122 dir[end - inpath] = 0;
1124 inpath = end;
1126 else if (dir) dir[0] = 0;
1128 /* look for extension: what's after the last dot */
1129 end = NULL;
1130 for (p = inpath; *p; p++) if (*p == '.') end = p;
1132 if (!end) end = p; /* there's no extension */
1134 if (fname)
1136 if (sz_fname <= end - inpath) goto do_error;
1137 memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1138 fname[end - inpath] = 0;
1140 if (ext)
1142 if (sz_ext <= strlenW(end)) goto do_error;
1143 strcpyW( ext, end );
1145 return 0;
1146 do_error:
1147 if (drive) drive[0] = '\0';
1148 if (dir) dir[0] = '\0';
1149 if (fname) fname[0]= '\0';
1150 if (ext) ext[0]= '\0';
1151 *MSVCRT__errno() = MSVCRT_ERANGE;
1152 return MSVCRT_ERANGE;
1155 /*********************************************************************
1156 * _wsplitpath (MSVCRT.@)
1158 * Unicode version of _splitpath.
1160 void CDECL _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
1161 MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext)
1163 _wsplitpath_s(inpath, drv, drv?MSVCRT__MAX_DRIVE:0, dir, dir?MSVCRT__MAX_DIR:0,
1164 fname, fname?MSVCRT__MAX_FNAME:0, ext, ext?MSVCRT__MAX_EXT:0);
1167 /*********************************************************************
1168 * _wfullpath (MSVCRT.@)
1170 * Unicode version of _fullpath.
1172 MSVCRT_wchar_t * CDECL _wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, MSVCRT_size_t size)
1174 DWORD rc;
1175 WCHAR* buffer;
1176 WCHAR* lastpart;
1177 BOOL alloced = FALSE;
1179 if (!relPath || !*relPath)
1180 return MSVCRT__wgetcwd(absPath, size);
1182 if (absPath == NULL)
1184 buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
1185 size = MAX_PATH;
1186 alloced = TRUE;
1188 else
1189 buffer = absPath;
1191 if (size < 4)
1193 *MSVCRT__errno() = MSVCRT_ERANGE;
1194 return NULL;
1197 TRACE(":resolving relative path %s\n",debugstr_w(relPath));
1199 rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
1201 if (rc > 0 && rc <= size )
1202 return buffer;
1203 else
1205 if (alloced)
1206 MSVCRT_free(buffer);
1207 return NULL;
1211 /*********************************************************************
1212 * _fullpath (MSVCRT.@)
1214 * Create an absolute path from a relative path.
1216 * PARAMS
1217 * absPath [O] Destination for absolute path
1218 * relPath [I] Relative path to convert to absolute
1219 * size [I] Length of absPath in characters.
1221 * RETURNS
1222 * Success: If absPath is NULL, returns an allocated string containing the path.
1223 * Otherwise populates absPath with the path and returns it.
1224 * Failure: NULL. errno indicates the error.
1226 char * CDECL _fullpath(char * absPath, const char* relPath, unsigned int size)
1228 DWORD rc;
1229 char* lastpart;
1230 char* buffer;
1231 BOOL alloced = FALSE;
1233 if (!relPath || !*relPath)
1234 return MSVCRT__getcwd(absPath, size);
1236 if (absPath == NULL)
1238 buffer = MSVCRT_malloc(MAX_PATH);
1239 size = MAX_PATH;
1240 alloced = TRUE;
1242 else
1243 buffer = absPath;
1245 if (size < 4)
1247 *MSVCRT__errno() = MSVCRT_ERANGE;
1248 return NULL;
1251 TRACE(":resolving relative path '%s'\n",relPath);
1253 rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
1255 if (rc > 0 && rc <= size)
1256 return buffer;
1257 else
1259 if (alloced)
1260 MSVCRT_free(buffer);
1261 return NULL;
1265 /*********************************************************************
1266 * _makepath (MSVCRT.@)
1268 * Create a pathname.
1270 * PARAMS
1271 * path [O] Destination for created pathname
1272 * drive [I] Drive letter (e.g. "A:")
1273 * directory [I] Directory
1274 * filename [I] Name of the file, excluding extension
1275 * extension [I] File extension (e.g. ".TXT")
1277 * RETURNS
1278 * Nothing. If path is not large enough to hold the resulting pathname,
1279 * random process memory will be overwritten.
1281 VOID CDECL _makepath(char * path, const char * drive,
1282 const char *directory, const char * filename,
1283 const char * extension)
1285 char *p = path;
1287 TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
1288 debugstr_a(filename), debugstr_a(extension) );
1290 if ( !path )
1291 return;
1293 if (drive && drive[0])
1295 *p++ = drive[0];
1296 *p++ = ':';
1298 if (directory && directory[0])
1300 unsigned int len = strlen(directory);
1301 memmove(p, directory, len);
1302 p += len;
1303 if (p[-1] != '/' && p[-1] != '\\')
1304 *p++ = '\\';
1306 if (filename && filename[0])
1308 unsigned int len = strlen(filename);
1309 memmove(p, filename, len);
1310 p += len;
1312 if (extension && extension[0])
1314 if (extension[0] != '.')
1315 *p++ = '.';
1316 strcpy(p, extension);
1318 else
1319 *p = '\0';
1320 TRACE("returning %s\n",path);
1323 /*********************************************************************
1324 * _wmakepath (MSVCRT.@)
1326 * Unicode version of _wmakepath.
1328 VOID CDECL _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
1329 const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
1331 MSVCRT_wchar_t *p = path;
1333 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
1334 debugstr_w(filename), debugstr_w(extension));
1336 if ( !path )
1337 return;
1339 if (drive && drive[0])
1341 *p++ = drive[0];
1342 *p++ = ':';
1344 if (directory && directory[0])
1346 unsigned int len = strlenW(directory);
1347 memmove(p, directory, len * sizeof(MSVCRT_wchar_t));
1348 p += len;
1349 if (p[-1] != '/' && p[-1] != '\\')
1350 *p++ = '\\';
1352 if (filename && filename[0])
1354 unsigned int len = strlenW(filename);
1355 memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1356 p += len;
1358 if (extension && extension[0])
1360 if (extension[0] != '.')
1361 *p++ = '.';
1362 strcpyW(p, extension);
1364 else
1365 *p = '\0';
1367 TRACE("returning %s\n", debugstr_w(path));
1370 /*********************************************************************
1371 * _makepath_s (MSVCRT.@)
1373 * Safe version of _makepath.
1375 int CDECL _makepath_s(char *path, MSVCRT_size_t size, const char *drive,
1376 const char *directory, const char *filename,
1377 const char *extension)
1379 char *p = path;
1381 if (!path || !size)
1383 *MSVCRT__errno() = MSVCRT_EINVAL;
1384 return MSVCRT_EINVAL;
1387 if (drive && drive[0])
1389 if (size <= 2)
1390 goto range;
1392 *p++ = drive[0];
1393 *p++ = ':';
1394 size -= 2;
1397 if (directory && directory[0])
1399 unsigned int len = strlen(directory);
1400 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1401 unsigned int copylen = min(size - 1, len);
1403 if (size < 2)
1404 goto range;
1406 memmove(p, directory, copylen);
1408 if (size <= len)
1409 goto range;
1411 p += copylen;
1412 size -= copylen;
1414 if (needs_separator)
1416 if (size < 2)
1417 goto range;
1419 *p++ = '\\';
1420 size -= 1;
1424 if (filename && filename[0])
1426 unsigned int len = strlen(filename);
1427 unsigned int copylen = min(size - 1, len);
1429 if (size < 2)
1430 goto range;
1432 memmove(p, filename, copylen);
1434 if (size <= len)
1435 goto range;
1437 p += len;
1438 size -= len;
1441 if (extension && extension[0])
1443 unsigned int len = strlen(extension);
1444 unsigned int needs_period = extension[0] != '.';
1445 unsigned int copylen;
1447 if (size < 2)
1448 goto range;
1450 if (needs_period)
1452 *p++ = '.';
1453 size -= 1;
1456 copylen = min(size - 1, len);
1457 memcpy(p, extension, copylen);
1459 if (size <= len)
1460 goto range;
1462 p += copylen;
1465 *p = '\0';
1466 return 0;
1468 range:
1469 path[0] = '\0';
1470 *MSVCRT__errno() = MSVCRT_ERANGE;
1471 return MSVCRT_ERANGE;
1474 /*********************************************************************
1475 * _wmakepath_s (MSVCRT.@)
1477 * Safe version of _wmakepath.
1479 int CDECL _wmakepath_s(MSVCRT_wchar_t *path, MSVCRT_size_t size, const MSVCRT_wchar_t *drive,
1480 const MSVCRT_wchar_t *directory, const MSVCRT_wchar_t *filename,
1481 const MSVCRT_wchar_t *extension)
1483 MSVCRT_wchar_t *p = path;
1485 if (!path || !size)
1487 *MSVCRT__errno() = MSVCRT_EINVAL;
1488 return MSVCRT_EINVAL;
1491 if (drive && drive[0])
1493 if (size <= 2)
1494 goto range;
1496 *p++ = drive[0];
1497 *p++ = ':';
1498 size -= 2;
1501 if (directory && directory[0])
1503 unsigned int len = strlenW(directory);
1504 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1505 unsigned int copylen = min(size - 1, len);
1507 if (size < 2)
1508 goto range;
1510 memmove(p, directory, copylen * sizeof(MSVCRT_wchar_t));
1512 if (size <= len)
1513 goto range;
1515 p += copylen;
1516 size -= copylen;
1518 if (needs_separator)
1520 if (size < 2)
1521 goto range;
1523 *p++ = '\\';
1524 size -= 1;
1528 if (filename && filename[0])
1530 unsigned int len = strlenW(filename);
1531 unsigned int copylen = min(size - 1, len);
1533 if (size < 2)
1534 goto range;
1536 memmove(p, filename, copylen * sizeof(MSVCRT_wchar_t));
1538 if (size <= len)
1539 goto range;
1541 p += len;
1542 size -= len;
1545 if (extension && extension[0])
1547 unsigned int len = strlenW(extension);
1548 unsigned int needs_period = extension[0] != '.';
1549 unsigned int copylen;
1551 if (size < 2)
1552 goto range;
1554 if (needs_period)
1556 *p++ = '.';
1557 size -= 1;
1560 copylen = min(size - 1, len);
1561 memcpy(p, extension, copylen * sizeof(MSVCRT_wchar_t));
1563 if (size <= len)
1564 goto range;
1566 p += copylen;
1569 *p = '\0';
1570 return 0;
1572 range:
1573 path[0] = '\0';
1574 *MSVCRT__errno() = MSVCRT_ERANGE;
1575 return MSVCRT_ERANGE;
1578 /*********************************************************************
1579 * _searchenv (MSVCRT.@)
1581 * Search for a file in a list of paths from an environment variable.
1583 * PARAMS
1584 * file [I] Name of the file to search for.
1585 * env [I] Name of the environment variable containing a list of paths.
1586 * buf [O] Destination for the found file path.
1588 * RETURNS
1589 * Nothing. If the file is not found, buf will contain an empty string
1590 * and errno is set.
1592 void CDECL MSVCRT__searchenv(const char* file, const char* env, char *buf)
1594 char*envVal, *penv;
1595 char curPath[MAX_PATH];
1597 *buf = '\0';
1599 /* Try CWD first */
1600 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1602 GetFullPathNameA( file, MAX_PATH, buf, NULL );
1603 /* Sigh. This error is *always* set, regardless of success */
1604 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1605 return;
1608 /* Search given environment variable */
1609 envVal = MSVCRT_getenv(env);
1610 if (!envVal)
1612 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1613 return;
1616 penv = envVal;
1617 TRACE(":searching for %s in paths %s\n", file, envVal);
1621 char *end = penv;
1623 while(*end && *end != ';') end++; /* Find end of next path */
1624 if (penv == end || !*penv)
1626 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1627 return;
1629 memcpy(curPath, penv, end - penv);
1630 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1632 curPath[end - penv] = '\\';
1633 curPath[end - penv + 1] = '\0';
1635 else
1636 curPath[end - penv] = '\0';
1638 strcat(curPath, file);
1639 TRACE("Checking for file %s\n", curPath);
1640 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1642 strcpy(buf, curPath);
1643 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1644 return; /* Found */
1646 penv = *end ? end + 1 : end;
1647 } while(1);
1650 /*********************************************************************
1651 * _searchenv_s (MSVCRT.@)
1653 int CDECL _searchenv_s(const char* file, const char* env, char *buf, MSVCRT_size_t count)
1655 char*envVal, *penv;
1656 char curPath[MAX_PATH];
1658 if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1659 !MSVCRT_CHECK_PMT(count > 0))
1661 *MSVCRT__errno() = MSVCRT_EINVAL;
1662 return MSVCRT_EINVAL;
1665 *buf = '\0';
1667 /* Try CWD first */
1668 if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1670 if (GetFullPathNameA( file, count, buf, NULL )) return 0;
1671 msvcrt_set_errno(GetLastError());
1672 return 0;
1675 /* Search given environment variable */
1676 envVal = MSVCRT_getenv(env);
1677 if (!envVal)
1679 *MSVCRT__errno() = MSVCRT_ENOENT;
1680 return MSVCRT_ENOENT;
1683 penv = envVal;
1684 TRACE(":searching for %s in paths %s\n", file, envVal);
1688 char *end = penv;
1690 while(*end && *end != ';') end++; /* Find end of next path */
1691 if (penv == end || !*penv)
1693 *MSVCRT__errno() = MSVCRT_ENOENT;
1694 return MSVCRT_ENOENT;
1696 memcpy(curPath, penv, end - penv);
1697 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1699 curPath[end - penv] = '\\';
1700 curPath[end - penv + 1] = '\0';
1702 else
1703 curPath[end - penv] = '\0';
1705 strcat(curPath, file);
1706 TRACE("Checking for file %s\n", curPath);
1707 if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1709 if (strlen(curPath) + 1 > count)
1711 MSVCRT_INVALID_PMT("buf[count] is too small");
1712 *MSVCRT__errno() = MSVCRT_ERANGE;
1713 return MSVCRT_ERANGE;
1715 strcpy(buf, curPath);
1716 return 0;
1718 penv = *end ? end + 1 : end;
1719 } while(1);
1722 /*********************************************************************
1723 * _wsearchenv (MSVCRT.@)
1725 * Unicode version of _searchenv
1727 void CDECL MSVCRT__wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1729 MSVCRT_wchar_t *envVal, *penv;
1730 MSVCRT_wchar_t curPath[MAX_PATH];
1732 *buf = '\0';
1734 /* Try CWD first */
1735 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1737 GetFullPathNameW( file, MAX_PATH, buf, NULL );
1738 /* Sigh. This error is *always* set, regardless of success */
1739 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1740 return;
1743 /* Search given environment variable */
1744 envVal = MSVCRT__wgetenv(env);
1745 if (!envVal)
1747 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1748 return;
1751 penv = envVal;
1752 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1756 MSVCRT_wchar_t *end = penv;
1758 while(*end && *end != ';') end++; /* Find end of next path */
1759 if (penv == end || !*penv)
1761 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1762 return;
1764 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1765 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1767 curPath[end - penv] = '\\';
1768 curPath[end - penv + 1] = '\0';
1770 else
1771 curPath[end - penv] = '\0';
1773 strcatW(curPath, file);
1774 TRACE("Checking for file %s\n", debugstr_w(curPath));
1775 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1777 strcpyW(buf, curPath);
1778 msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1779 return; /* Found */
1781 penv = *end ? end + 1 : end;
1782 } while(1);
1785 /*********************************************************************
1786 * _wsearchenv_s (MSVCRT.@)
1788 int CDECL _wsearchenv_s(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env,
1789 MSVCRT_wchar_t *buf, MSVCRT_size_t count)
1791 MSVCRT_wchar_t* envVal, *penv;
1792 MSVCRT_wchar_t curPath[MAX_PATH];
1794 if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1795 !MSVCRT_CHECK_PMT(count > 0))
1797 *MSVCRT__errno() = MSVCRT_EINVAL;
1798 return MSVCRT_EINVAL;
1800 *buf = '\0';
1802 /* Try CWD first */
1803 if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1805 if (GetFullPathNameW( file, count, buf, NULL )) return 0;
1806 msvcrt_set_errno(GetLastError());
1807 return 0;
1810 /* Search given environment variable */
1811 envVal = MSVCRT__wgetenv(env);
1812 if (!envVal)
1814 *MSVCRT__errno() = MSVCRT_ENOENT;
1815 return MSVCRT_ENOENT;
1818 penv = envVal;
1819 TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1823 MSVCRT_wchar_t *end = penv;
1825 while(*end && *end != ';') end++; /* Find end of next path */
1826 if (penv == end || !*penv)
1828 *MSVCRT__errno() = MSVCRT_ENOENT;
1829 return MSVCRT_ENOENT;
1831 memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1832 if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1834 curPath[end - penv] = '\\';
1835 curPath[end - penv + 1] = '\0';
1837 else
1838 curPath[end - penv] = '\0';
1840 strcatW(curPath, file);
1841 TRACE("Checking for file %s\n", debugstr_w(curPath));
1842 if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1844 if (strlenW(curPath) + 1 > count)
1846 MSVCRT_INVALID_PMT("buf[count] is too small");
1847 *MSVCRT__errno() = MSVCRT_ERANGE;
1848 return MSVCRT_ERANGE;
1850 strcpyW(buf, curPath);
1851 return 0;
1853 penv = *end ? end + 1 : end;
1854 } while(1);