msvcrt: Make WEOF returned from swscanf signed.
[wine] / dlls / msvcrt / dir.c
1 /*
2  * msvcrt.dll drive/directory functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <time.h>
29
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"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38
39 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata_t  */
40 static void msvcrt_fttofd( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata_t* ft)
41 {
42   DWORD dw;
43
44   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
45     ft->attrib = 0;
46   else
47     ft->attrib = fd->dwFileAttributes;
48
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);
57 }
58
59 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata32_t  */
60 static void msvcrt_fttofd32( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata32_t* ft)
61 {
62   DWORD dw;
63
64   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
65     ft->attrib = 0;
66   else
67     ft->attrib = fd->dwFileAttributes;
68
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);
77 }
78
79 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata_t  */
80 static void msvcrt_wfttofd( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata_t* ft)
81 {
82   DWORD dw;
83
84   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
85     ft->attrib = 0;
86   else
87     ft->attrib = fd->dwFileAttributes;
88
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);
97 }
98
99 /* INTERNAL: Translate WIN32_FIND_DATAA to finddatai64_t  */
100 static void msvcrt_fttofdi64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddatai64_t* ft)
101 {
102   DWORD dw;
103
104   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
105     ft->attrib = 0;
106   else
107     ft->attrib = fd->dwFileAttributes;
108
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);
117 }
118
119 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64_t  */
120 static void msvcrt_fttofd64( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64_t* ft)
121 {
122   DWORD dw;
123
124   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
125     ft->attrib = 0;
126   else
127     ft->attrib = fd->dwFileAttributes;
128
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);
137 }
138
139 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64_t  */
140 static void msvcrt_wfttofd64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64_t* ft)
141 {
142   DWORD dw;
143
144   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
145     ft->attrib = 0;
146   else
147     ft->attrib = fd->dwFileAttributes;
148
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);
157 }
158
159 /* INTERNAL: Translate WIN32_FIND_DATAA to finddata64i32_t  */
160 static void msvcrt_fttofd64i32( const WIN32_FIND_DATAA *fd, struct MSVCRT__finddata64i32_t* ft)
161 {
162   DWORD dw;
163
164   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
165     ft->attrib = 0;
166   else
167     ft->attrib = fd->dwFileAttributes;
168
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);
177 }
178
179 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddatai64_t  */
180 static void msvcrt_wfttofdi64( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddatai64_t* ft)
181 {
182   DWORD dw;
183
184   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
185     ft->attrib = 0;
186   else
187     ft->attrib = fd->dwFileAttributes;
188
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);
197 }
198
199 /* INTERNAL: Translate WIN32_FIND_DATAW to wfinddata64i32_t  */
200 static void msvcrt_wfttofd64i32( const WIN32_FIND_DATAW *fd, struct MSVCRT__wfinddata64i32_t* ft)
201 {
202   DWORD dw;
203
204   if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
205     ft->attrib = 0;
206   else
207     ft->attrib = fd->dwFileAttributes;
208
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);
217 }
218
219 /*********************************************************************
220  *              _chdir (MSVCRT.@)
221  *
222  * Change the current working directory.
223  *
224  * PARAMS
225  *  newdir [I] Directory to change to
226  *
227  * RETURNS
228  *  Success: 0. The current working directory is set to newdir.
229  *  Failure: -1. errno indicates the error.
230  *
231  * NOTES
232  *  See SetCurrentDirectoryA.
233  */
234 int CDECL MSVCRT__chdir(const char * newdir)
235 {
236   if (!SetCurrentDirectoryA(newdir))
237   {
238     msvcrt_set_errno(newdir?GetLastError():0);
239     return -1;
240   }
241   return 0;
242 }
243
244 /*********************************************************************
245  *              _wchdir (MSVCRT.@)
246  *
247  * Unicode version of _chdir.
248  */
249 int CDECL MSVCRT__wchdir(const MSVCRT_wchar_t * newdir)
250 {
251   if (!SetCurrentDirectoryW(newdir))
252   {
253     msvcrt_set_errno(newdir?GetLastError():0);
254     return -1;
255   }
256   return 0;
257 }
258
259 /*********************************************************************
260  *              _chdrive (MSVCRT.@)
261  *
262  * Change the current drive.
263  *
264  * PARAMS
265  *  newdrive [I] Drive number to change to (1 = 'A', 2 = 'B', ...)
266  *
267  * RETURNS
268  *  Success: 0. The current drive is set to newdrive.
269  *  Failure: -1. errno indicates the error.
270  *
271  * NOTES
272  *  See SetCurrentDirectoryA.
273  */
274 int CDECL MSVCRT__chdrive(int newdrive)
275 {
276   WCHAR buffer[] = {'A', ':', 0};
277
278   buffer[0] += newdrive - 1;
279   if (!SetCurrentDirectoryW( buffer ))
280   {
281     msvcrt_set_errno(GetLastError());
282     if (newdrive <= 0)
283       *MSVCRT__errno() = MSVCRT_EACCES;
284     return -1;
285   }
286   return 0;
287 }
288
289 /*********************************************************************
290  *              _findclose (MSVCRT.@)
291  *
292  * Close a handle returned by _findfirst().
293  *
294  * PARAMS
295  *  hand [I] Handle to close
296  *
297  * RETURNS
298  *  Success: 0. All resources associated with hand are freed.
299  *  Failure: -1. errno indicates the error.
300  *
301  * NOTES
302  *  See FindClose.
303  */
304 int CDECL MSVCRT__findclose(MSVCRT_intptr_t hand)
305 {
306   TRACE(":handle %ld\n",hand);
307   if (!FindClose((HANDLE)hand))
308   {
309     msvcrt_set_errno(GetLastError());
310     return -1;
311   }
312   return 0;
313 }
314
315 /*********************************************************************
316  *              _findfirst (MSVCRT.@)
317  *
318  * Open a handle for iterating through a directory.
319  *
320  * PARAMS
321  *  fspec [I] File specification of files to iterate.
322  *  ft    [O] Information for the first file found.
323  *
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.
328  *
329  * NOTES
330  *  See FindFirstFileA.
331  */
332 MSVCRT_intptr_t CDECL MSVCRT__findfirst(const char * fspec, struct MSVCRT__finddata_t* ft)
333 {
334   WIN32_FIND_DATAA find_data;
335   HANDLE hfind;
336
337   hfind  = FindFirstFileA(fspec, &find_data);
338   if (hfind == INVALID_HANDLE_VALUE)
339   {
340     msvcrt_set_errno(GetLastError());
341     return -1;
342   }
343   msvcrt_fttofd(&find_data,ft);
344   TRACE(":got handle %p\n",hfind);
345   return (MSVCRT_intptr_t)hfind;
346 }
347
348 /*********************************************************************
349  *              _findfirst32 (MSVCRT.@)
350  */
351 MSVCRT_intptr_t CDECL MSVCRT__findfirst32(const char * fspec, struct MSVCRT__finddata32_t* ft)
352 {
353   WIN32_FIND_DATAA find_data;
354   HANDLE hfind;
355
356   hfind  = FindFirstFileA(fspec, &find_data);
357   if (hfind == INVALID_HANDLE_VALUE)
358   {
359     msvcrt_set_errno(GetLastError());
360     return -1;
361   }
362   msvcrt_fttofd32(&find_data, ft);
363   TRACE(":got handle %p\n", hfind);
364   return (MSVCRT_intptr_t)hfind;
365 }
366
367 /*********************************************************************
368  *              _wfindfirst (MSVCRT.@)
369  *
370  * Unicode version of _findfirst.
371  */
372 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata_t* ft)
373 {
374   WIN32_FIND_DATAW find_data;
375   HANDLE hfind;
376
377   hfind  = FindFirstFileW(fspec, &find_data);
378   if (hfind == INVALID_HANDLE_VALUE)
379   {
380     msvcrt_set_errno(GetLastError());
381     return -1;
382   }
383   msvcrt_wfttofd(&find_data,ft);
384   TRACE(":got handle %p\n",hfind);
385   return (MSVCRT_intptr_t)hfind;
386 }
387
388 /*********************************************************************
389  *              _findfirsti64 (MSVCRT.@)
390  *
391  * 64-bit version of _findfirst.
392  */
393 MSVCRT_intptr_t CDECL MSVCRT__findfirsti64(const char * fspec, struct MSVCRT__finddatai64_t* ft)
394 {
395   WIN32_FIND_DATAA find_data;
396   HANDLE hfind;
397
398   hfind  = FindFirstFileA(fspec, &find_data);
399   if (hfind == INVALID_HANDLE_VALUE)
400   {
401     msvcrt_set_errno(GetLastError());
402     return -1;
403   }
404   msvcrt_fttofdi64(&find_data,ft);
405   TRACE(":got handle %p\n",hfind);
406   return (MSVCRT_intptr_t)hfind;
407 }
408
409 /*********************************************************************
410  *              _findfirst64 (MSVCRT.@)
411  *
412  * 64-bit version of _findfirst.
413  */
414 MSVCRT_intptr_t CDECL MSVCRT__findfirst64(const char * fspec, struct MSVCRT__finddata64_t* ft)
415 {
416   WIN32_FIND_DATAA find_data;
417   HANDLE hfind;
418
419   hfind  = FindFirstFileA(fspec, &find_data);
420   if (hfind == INVALID_HANDLE_VALUE)
421   {
422     msvcrt_set_errno(GetLastError());
423     return -1;
424   }
425   msvcrt_fttofd64(&find_data,ft);
426   TRACE(":got handle %p\n",hfind);
427   return (MSVCRT_intptr_t)hfind;
428 }
429
430 /*********************************************************************
431  *              _wfindfirst64 (MSVCRT.@)
432  *
433  * Unicode version of _findfirst64.
434  */
435 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64_t* ft)
436 {
437   WIN32_FIND_DATAW find_data;
438   HANDLE hfind;
439
440   hfind  = FindFirstFileW(fspec, &find_data);
441   if (hfind == INVALID_HANDLE_VALUE)
442   {
443     msvcrt_set_errno(GetLastError());
444     return -1;
445   }
446   msvcrt_wfttofd64(&find_data,ft);
447   TRACE(":got handle %p\n",hfind);
448   return (MSVCRT_intptr_t)hfind;
449 }
450
451 /*********************************************************************
452  *              _findfirst64i32 (MSVCRT.@)
453  *
454  * 64-bit/32-bit version of _findfirst.
455  */
456 MSVCRT_intptr_t CDECL MSVCRT__findfirst64i32(const char * fspec, struct MSVCRT__finddata64i32_t* ft)
457 {
458   WIN32_FIND_DATAA find_data;
459   HANDLE hfind;
460
461   hfind  = FindFirstFileA(fspec, &find_data);
462   if (hfind == INVALID_HANDLE_VALUE)
463   {
464     msvcrt_set_errno(GetLastError());
465     return -1;
466   }
467   msvcrt_fttofd64i32(&find_data,ft);
468   TRACE(":got handle %p\n",hfind);
469   return (MSVCRT_intptr_t)hfind;
470 }
471
472 /*********************************************************************
473  *              _wfindfirst64i32 (MSVCRT.@)
474  *
475  * Unicode version of _findfirst64i32.
476  */
477 MSVCRT_intptr_t CDECL MSVCRT__wfindfirst64i32(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddata64i32_t* ft)
478 {
479   WIN32_FIND_DATAW find_data;
480   HANDLE hfind;
481
482   hfind  = FindFirstFileW(fspec, &find_data);
483   if (hfind == INVALID_HANDLE_VALUE)
484   {
485     msvcrt_set_errno(GetLastError());
486     return -1;
487   }
488   msvcrt_wfttofd64i32(&find_data,ft);
489   TRACE(":got handle %p\n",hfind);
490   return (MSVCRT_intptr_t)hfind;
491 }
492
493 /*********************************************************************
494  *              _wfindfirsti64 (MSVCRT.@)
495  *
496  * Unicode version of _findfirsti64.
497  */
498 MSVCRT_intptr_t CDECL MSVCRT__wfindfirsti64(const MSVCRT_wchar_t * fspec, struct MSVCRT__wfinddatai64_t* ft)
499 {
500   WIN32_FIND_DATAW find_data;
501   HANDLE hfind;
502
503   hfind  = FindFirstFileW(fspec, &find_data);
504   if (hfind == INVALID_HANDLE_VALUE)
505   {
506     msvcrt_set_errno(GetLastError());
507     return -1;
508   }
509   msvcrt_wfttofdi64(&find_data,ft);
510   TRACE(":got handle %p\n",hfind);
511   return (MSVCRT_intptr_t)hfind;
512 }
513
514 /*********************************************************************
515  *              _findnext (MSVCRT.@)
516  *
517  * Find the next file from a file search handle.
518  *
519  * PARAMS
520  *  hand  [I] Handle to the search returned from _findfirst().
521  *  ft    [O] Information for the file found.
522  *
523  * RETURNS
524  *  Success: 0. ft is populated with the details of the found file.
525  *  Failure: -1. errno indicates the error.
526  *
527  * NOTES
528  *  See FindNextFileA.
529  */
530 int CDECL MSVCRT__findnext(MSVCRT_intptr_t hand, struct MSVCRT__finddata_t * ft)
531 {
532   WIN32_FIND_DATAA find_data;
533
534   if (!FindNextFileA((HANDLE)hand, &find_data))
535   {
536     *MSVCRT__errno() = MSVCRT_ENOENT;
537     return -1;
538   }
539
540   msvcrt_fttofd(&find_data,ft);
541   return 0;
542 }
543
544 /*********************************************************************
545  *               _findnext32 (MSVCRT.@)
546  */
547 int CDECL MSVCRT__findnext32(MSVCRT_intptr_t hand, struct MSVCRT__finddata32_t * ft)
548 {
549   WIN32_FIND_DATAA find_data;
550
551   if (!FindNextFileA((HANDLE)hand, &find_data))
552   {
553     *MSVCRT__errno() = MSVCRT_ENOENT;
554     return -1;
555   }
556
557   msvcrt_fttofd32(&find_data, ft);
558   return 0;
559 }
560
561 /*********************************************************************
562  *              _wfindnext (MSVCRT.@)
563  *
564  * Unicode version of _findnext.
565  */
566 int CDECL MSVCRT__wfindnext(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata_t * ft)
567 {
568   WIN32_FIND_DATAW find_data;
569
570   if (!FindNextFileW((HANDLE)hand, &find_data))
571   {
572     *MSVCRT__errno() = MSVCRT_ENOENT;
573     return -1;
574   }
575
576   msvcrt_wfttofd(&find_data,ft);
577   return 0;
578 }
579
580 /*********************************************************************
581  *              _findnexti64 (MSVCRT.@)
582  *
583  * 64-bit version of _findnext.
584  */
585 int CDECL MSVCRT__findnexti64(MSVCRT_intptr_t hand, struct MSVCRT__finddatai64_t * ft)
586 {
587   WIN32_FIND_DATAA find_data;
588
589   if (!FindNextFileA((HANDLE)hand, &find_data))
590   {
591     *MSVCRT__errno() = MSVCRT_ENOENT;
592     return -1;
593   }
594
595   msvcrt_fttofdi64(&find_data,ft);
596   return 0;
597 }
598
599 /*********************************************************************
600  *              _findnext64 (MSVCRT.@)
601  *
602  * 64-bit version of _findnext.
603  */
604 int CDECL MSVCRT__findnext64(long hand, struct MSVCRT__finddata64_t * ft)
605 {
606   WIN32_FIND_DATAA find_data;
607
608   if (!FindNextFileA((HANDLE)hand, &find_data))
609   {
610     *MSVCRT__errno() = MSVCRT_ENOENT;
611     return -1;
612   }
613
614   msvcrt_fttofd64(&find_data,ft);
615   return 0;
616 }
617
618 /*********************************************************************
619  *              _wfindnext64 (MSVCRT.@)
620  *
621  * Unicode version of _wfindnext64.
622  */
623 int CDECL MSVCRT__wfindnext64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64_t * ft)
624 {
625   WIN32_FIND_DATAW find_data;
626
627   if (!FindNextFileW((HANDLE)hand, &find_data))
628   {
629     *MSVCRT__errno() = MSVCRT_ENOENT;
630     return -1;
631   }
632
633   msvcrt_wfttofd64(&find_data,ft);
634   return 0;
635 }
636
637 /*********************************************************************
638  *              _findnext64i32 (MSVCRT.@)
639  *
640  * 64-bit/32-bit version of _findnext.
641  */
642 int CDECL MSVCRT__findnext64i32(long hand, struct MSVCRT__finddata64i32_t * ft)
643 {
644   WIN32_FIND_DATAA find_data;
645
646   if (!FindNextFileA((HANDLE)hand, &find_data))
647   {
648     *MSVCRT__errno() = MSVCRT_ENOENT;
649     return -1;
650   }
651
652   msvcrt_fttofd64i32(&find_data,ft);
653   return 0;
654 }
655
656 /*********************************************************************
657  *              _wfindnexti64 (MSVCRT.@)
658  *
659  * Unicode version of _findnexti64.
660  */
661 int CDECL MSVCRT__wfindnexti64(MSVCRT_intptr_t hand, struct MSVCRT__wfinddatai64_t * ft)
662 {
663   WIN32_FIND_DATAW find_data;
664
665   if (!FindNextFileW((HANDLE)hand, &find_data))
666   {
667     *MSVCRT__errno() = MSVCRT_ENOENT;
668     return -1;
669   }
670
671   msvcrt_wfttofdi64(&find_data,ft);
672   return 0;
673 }
674
675 /*********************************************************************
676  *              _wfindnext64i32 (MSVCRT.@)
677  *
678  * Unicode version of _findnext64i32.
679  */
680 int CDECL MSVCRT__wfindnext64i32(MSVCRT_intptr_t hand, struct MSVCRT__wfinddata64i32_t * ft)
681 {
682   WIN32_FIND_DATAW find_data;
683
684   if (!FindNextFileW((HANDLE)hand, &find_data))
685   {
686     *MSVCRT__errno() = MSVCRT_ENOENT;
687     return -1;
688   }
689
690   msvcrt_wfttofd64i32(&find_data,ft);
691   return 0;
692 }
693
694 /*********************************************************************
695  *              _getcwd (MSVCRT.@)
696  *
697  * Get the current working directory.
698  *
699  * PARAMS
700  *  buf  [O] Destination for current working directory.
701  *  size [I] Size of buf in characters
702  *
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.
707  */
708 char* CDECL MSVCRT__getcwd(char * buf, int size)
709 {
710   char dir[MAX_PATH];
711   int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
712
713   if (dir_len < 1)
714     return NULL; /* FIXME: Real return value untested */
715
716   if (!buf)
717   {
718       if (size <= dir_len) size = dir_len + 1;
719       if (!(buf = MSVCRT_malloc( size ))) return NULL;
720   }
721   else if (dir_len >= size)
722   {
723     *MSVCRT__errno() = MSVCRT_ERANGE;
724     return NULL; /* buf too small */
725   }
726   strcpy(buf,dir);
727   return buf;
728 }
729
730 /*********************************************************************
731  *              _wgetcwd (MSVCRT.@)
732  *
733  * Unicode version of _getcwd.
734  */
735 MSVCRT_wchar_t* CDECL MSVCRT__wgetcwd(MSVCRT_wchar_t * buf, int size)
736 {
737   MSVCRT_wchar_t dir[MAX_PATH];
738   int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
739
740   if (dir_len < 1)
741     return NULL; /* FIXME: Real return value untested */
742
743   if (!buf)
744   {
745       if (size <= dir_len) size = dir_len + 1;
746       if (!(buf = MSVCRT_malloc( size * sizeof(WCHAR) ))) return NULL;
747   }
748   if (dir_len >= size)
749   {
750     *MSVCRT__errno() = MSVCRT_ERANGE;
751     return NULL; /* buf too small */
752   }
753   strcpyW(buf,dir);
754   return buf;
755 }
756
757 /*********************************************************************
758  *              _getdrive (MSVCRT.@)
759  *
760  * Get the current drive number.
761  *
762  * PARAMS
763  *  None.
764  *
765  * RETURNS
766  *  Success: The drive letter number from 1 to 26 ("A:" to "Z:").
767  *  Failure: 0.
768  */
769 int CDECL MSVCRT__getdrive(void)
770 {
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;
776 }
777
778 /*********************************************************************
779  *              _getdcwd (MSVCRT.@)
780  *
781  * Get the current working directory on a given disk.
782  * 
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.
787  *
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.
792  */
793 char* CDECL MSVCRT__getdcwd(int drive, char * buf, int size)
794 {
795   static char* dummy;
796
797   TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
798
799   if (!drive || drive == MSVCRT__getdrive())
800     return MSVCRT__getcwd(buf,size); /* current */
801   else
802   {
803     char dir[MAX_PATH];
804     char drivespec[] = {'A', ':', 0};
805     int dir_len;
806
807     drivespec[0] += drive - 1;
808     if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
809     {
810       *MSVCRT__errno() = MSVCRT_EACCES;
811       return NULL;
812     }
813
814     dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
815     if (dir_len >= size || dir_len < 1)
816     {
817       *MSVCRT__errno() = MSVCRT_ERANGE;
818       return NULL; /* buf too small */
819     }
820
821     TRACE(":returning '%s'\n", dir);
822     if (!buf)
823       return MSVCRT__strdup(dir); /* allocate */
824
825     strcpy(buf,dir);
826   }
827   return buf;
828 }
829
830 /*********************************************************************
831  *              _wgetdcwd (MSVCRT.@)
832  *
833  * Unicode version of _wgetdcwd.
834  */
835 MSVCRT_wchar_t* CDECL MSVCRT__wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
836 {
837   static MSVCRT_wchar_t* dummy;
838
839   TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
840
841   if (!drive || drive == MSVCRT__getdrive())
842     return MSVCRT__wgetcwd(buf,size); /* current */
843   else
844   {
845     MSVCRT_wchar_t dir[MAX_PATH];
846     MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
847     int dir_len;
848
849     drivespec[0] += drive - 1;
850     if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
851     {
852       *MSVCRT__errno() = MSVCRT_EACCES;
853       return NULL;
854     }
855
856     dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
857     if (dir_len >= size || dir_len < 1)
858     {
859       *MSVCRT__errno() = MSVCRT_ERANGE;
860       return NULL; /* buf too small */
861     }
862
863     TRACE(":returning %s\n", debugstr_w(dir));
864     if (!buf)
865       return MSVCRT__wcsdup(dir); /* allocate */
866     strcpyW(buf,dir);
867   }
868   return buf;
869 }
870
871 /*********************************************************************
872  *              _getdiskfree (MSVCRT.@)
873  *
874  * Get information about the free space on a drive.
875  *
876  * PARAMS
877  *  disk [I] Drive number to get information about (1 = 'A', 2 = 'B', ...)
878  *  info [O] Destination for the resulting information.
879  *
880  * RETURNS
881  *  Success: 0. info is updated with the free space information.
882  *  Failure: An error code from GetLastError().
883  *
884  * NOTES
885  *  See GetLastError().
886  */
887 unsigned int CDECL MSVCRT__getdiskfree(unsigned int disk, struct MSVCRT__diskfree_t * d)
888 {
889   WCHAR drivespec[] = {'@', ':', '\\', 0};
890   DWORD ret[4];
891   unsigned int err;
892
893   if (disk > 26)
894     return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
895
896   drivespec[0] += disk; /* make a drive letter */
897
898   if (GetDiskFreeSpaceW(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
899   {
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;
905   }
906   err = GetLastError();
907   msvcrt_set_errno(err);
908   return err;
909 }
910
911 /*********************************************************************
912  *              _mkdir (MSVCRT.@)
913  *
914  * Create a directory.
915  *
916  * PARAMS
917  *  newdir [I] Name of directory to create.
918  *
919  * RETURNS
920  *  Success: 0. The directory indicated by newdir is created.
921  *  Failure: -1. errno indicates the error.
922  *
923  * NOTES
924  *  See CreateDirectoryA.
925  */
926 int CDECL MSVCRT__mkdir(const char * newdir)
927 {
928   if (CreateDirectoryA(newdir,NULL))
929     return 0;
930   msvcrt_set_errno(GetLastError());
931   return -1;
932 }
933
934 /*********************************************************************
935  *              _wmkdir (MSVCRT.@)
936  *
937  * Unicode version of _mkdir.
938  */
939 int CDECL MSVCRT__wmkdir(const MSVCRT_wchar_t* newdir)
940 {
941   if (CreateDirectoryW(newdir,NULL))
942     return 0;
943   msvcrt_set_errno(GetLastError());
944   return -1;
945 }
946
947 /*********************************************************************
948  *              _rmdir (MSVCRT.@)
949  *
950  * Delete a directory.
951  *
952  * PARAMS
953  *  dir [I] Name of directory to delete.
954  *
955  * RETURNS
956  *  Success: 0. The directory indicated by newdir is deleted.
957  *  Failure: -1. errno indicates the error.
958  *
959  * NOTES
960  *  See RemoveDirectoryA.
961  */
962 int CDECL MSVCRT__rmdir(const char * dir)
963 {
964   if (RemoveDirectoryA(dir))
965     return 0;
966   msvcrt_set_errno(GetLastError());
967   return -1;
968 }
969
970 /*********************************************************************
971  *              _wrmdir (MSVCRT.@)
972  *
973  * Unicode version of _rmdir.
974  */
975 int CDECL MSVCRT__wrmdir(const MSVCRT_wchar_t * dir)
976 {
977   if (RemoveDirectoryW(dir))
978     return 0;
979   msvcrt_set_errno(GetLastError());
980   return -1;
981 }
982
983 /******************************************************************
984  *              _splitpath_s (MSVCRT.@)
985  */
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)
991 {
992     const char *p, *end;
993
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))
1002     {
1003         *MSVCRT__errno() = MSVCRT_EINVAL;
1004         return MSVCRT_EINVAL;
1005     }
1006
1007     if (inpath[0] && inpath[1] == ':')
1008     {
1009         if (drive)
1010         {
1011             if (sz_drive <= 2) goto do_error;
1012             drive[0] = inpath[0];
1013             drive[1] = inpath[1];
1014             drive[2] = 0;
1015         }
1016         inpath += 2;
1017     }
1018     else if (drive) drive[0] = '\0';
1019
1020     /* look for end of directory part */
1021     end = NULL;
1022     for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1023
1024     if (end)  /* got a directory */
1025     {
1026         if (dir)
1027         {
1028             if (sz_dir <= end - inpath) goto do_error;
1029             memcpy( dir, inpath, (end - inpath) );
1030             dir[end - inpath] = 0;
1031         }
1032         inpath = end;
1033     }
1034     else if (dir) dir[0] = 0;
1035
1036     /* look for extension: what's after the last dot */
1037     end = NULL;
1038     for (p = inpath; *p; p++) if (*p == '.') end = p;
1039
1040     if (!end) end = p; /* there's no extension */
1041
1042     if (fname)
1043     {
1044         if (sz_fname <= end - inpath) goto do_error;
1045         memcpy( fname, inpath, (end - inpath) );
1046         fname[end - inpath] = 0;
1047     }
1048     if (ext)
1049     {
1050         if (sz_ext <= strlen(end)) goto do_error;
1051         strcpy( ext, end );
1052     }
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;
1061 }
1062
1063 /*********************************************************************
1064  *              _splitpath (MSVCRT.@)
1065  */
1066 void CDECL _splitpath(const char *inpath, char *drv, char *dir,
1067         char *fname, char *ext)
1068 {
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);
1071 }
1072
1073 /******************************************************************
1074  *              _wsplitpath_s (MSVCRT.@)
1075  *
1076  * Secure version of _wsplitpath
1077  */
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)
1083 {
1084     const MSVCRT_wchar_t *p, *end;
1085
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))
1094     {
1095         *MSVCRT__errno() = MSVCRT_EINVAL;
1096         return MSVCRT_EINVAL;
1097     }
1098
1099     if (inpath[0] && inpath[1] == ':')
1100     {
1101         if (drive)
1102         {
1103             if (sz_drive <= 2) goto do_error;
1104             drive[0] = inpath[0];
1105             drive[1] = inpath[1];
1106             drive[2] = 0;
1107         }
1108         inpath += 2;
1109     }
1110     else if (drive) drive[0] = '\0';
1111
1112     /* look for end of directory part */
1113     end = NULL;
1114     for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1115
1116     if (end)  /* got a directory */
1117     {
1118         if (dir)
1119         {
1120             if (sz_dir <= end - inpath) goto do_error;
1121             memcpy( dir, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1122             dir[end - inpath] = 0;
1123         }
1124         inpath = end;
1125     }
1126     else if (dir) dir[0] = 0;
1127
1128     /* look for extension: what's after the last dot */
1129     end = NULL;
1130     for (p = inpath; *p; p++) if (*p == '.') end = p;
1131
1132     if (!end) end = p; /* there's no extension */
1133
1134     if (fname)
1135     {
1136         if (sz_fname <= end - inpath) goto do_error;
1137         memcpy( fname, inpath, (end - inpath) * sizeof(MSVCRT_wchar_t) );
1138         fname[end - inpath] = 0;
1139     }
1140     if (ext)
1141     {
1142         if (sz_ext <= strlenW(end)) goto do_error;
1143         strcpyW( ext, end );
1144     }
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;
1153 }
1154
1155 /*********************************************************************
1156  *              _wsplitpath (MSVCRT.@)
1157  *
1158  * Unicode version of _splitpath.
1159  */
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)
1162 {
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);
1165 }
1166
1167 /*********************************************************************
1168  *              _wfullpath (MSVCRT.@)
1169  *
1170  * Unicode version of _fullpath.
1171  */
1172 MSVCRT_wchar_t * CDECL _wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, MSVCRT_size_t size)
1173 {
1174   DWORD rc;
1175   WCHAR* buffer;
1176   WCHAR* lastpart;
1177   BOOL alloced = FALSE;
1178
1179   if (!relPath || !*relPath)
1180     return MSVCRT__wgetcwd(absPath, size);
1181
1182   if (absPath == NULL)
1183   {
1184       buffer = MSVCRT_malloc(MAX_PATH * sizeof(WCHAR));
1185       size = MAX_PATH;
1186       alloced = TRUE;
1187   }
1188   else
1189       buffer = absPath;
1190
1191   if (size < 4)
1192   {
1193     *MSVCRT__errno() = MSVCRT_ERANGE;
1194     return NULL;
1195   }
1196
1197   TRACE(":resolving relative path %s\n",debugstr_w(relPath));
1198
1199   rc = GetFullPathNameW(relPath,size,buffer,&lastpart);
1200
1201   if (rc > 0 && rc <= size )
1202     return buffer;
1203   else
1204   {
1205       if (alloced)
1206           MSVCRT_free(buffer);
1207         return NULL;
1208   }
1209 }
1210
1211 /*********************************************************************
1212  *              _fullpath (MSVCRT.@)
1213  *
1214  * Create an absolute path from a relative path.
1215  *
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.
1220  *
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.
1225  */
1226 char * CDECL _fullpath(char * absPath, const char* relPath, unsigned int size)
1227 {
1228   DWORD rc;
1229   char* lastpart;
1230   char* buffer;
1231   BOOL alloced = FALSE;
1232
1233   if (!relPath || !*relPath)
1234     return MSVCRT__getcwd(absPath, size);
1235
1236   if (absPath == NULL)
1237   {
1238       buffer = MSVCRT_malloc(MAX_PATH);
1239       size = MAX_PATH;
1240       alloced = TRUE;
1241   }
1242   else
1243       buffer = absPath;
1244
1245   if (size < 4)
1246   {
1247     *MSVCRT__errno() = MSVCRT_ERANGE;
1248     return NULL;
1249   }
1250
1251   TRACE(":resolving relative path '%s'\n",relPath);
1252
1253   rc = GetFullPathNameA(relPath,size,buffer,&lastpart);
1254
1255   if (rc > 0 && rc <= size)
1256     return buffer;
1257   else
1258   {
1259       if (alloced)
1260           MSVCRT_free(buffer);
1261         return NULL;
1262   }
1263 }
1264
1265 /*********************************************************************
1266  *              _makepath (MSVCRT.@)
1267  *
1268  * Create a pathname.
1269  *
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")
1276  *
1277  * RETURNS
1278  *  Nothing. If path is not large enough to hold the resulting pathname,
1279  *  random process memory will be overwritten.
1280  */
1281 VOID CDECL _makepath(char * path, const char * drive,
1282                      const char *directory, const char * filename,
1283                      const char * extension)
1284 {
1285     char *p = path;
1286
1287     TRACE("(%s %s %s %s)\n", debugstr_a(drive), debugstr_a(directory),
1288           debugstr_a(filename), debugstr_a(extension) );
1289
1290     if ( !path )
1291         return;
1292
1293     if (drive && drive[0])
1294     {
1295         *p++ = drive[0];
1296         *p++ = ':';
1297     }
1298     if (directory && directory[0])
1299     {
1300         unsigned int len = strlen(directory);
1301         memmove(p, directory, len);
1302         p += len;
1303         if (p[-1] != '/' && p[-1] != '\\')
1304             *p++ = '\\';
1305     }
1306     if (filename && filename[0])
1307     {
1308         unsigned int len = strlen(filename);
1309         memmove(p, filename, len);
1310         p += len;
1311     }
1312     if (extension && extension[0])
1313     {
1314         if (extension[0] != '.')
1315             *p++ = '.';
1316         strcpy(p, extension);
1317     }
1318     else
1319         *p = '\0';
1320     TRACE("returning %s\n",path);
1321 }
1322
1323 /*********************************************************************
1324  *              _wmakepath (MSVCRT.@)
1325  *
1326  * Unicode version of _wmakepath.
1327  */
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)
1330 {
1331     MSVCRT_wchar_t *p = path;
1332
1333     TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
1334           debugstr_w(filename), debugstr_w(extension));
1335
1336     if ( !path )
1337         return;
1338
1339     if (drive && drive[0])
1340     {
1341         *p++ = drive[0];
1342         *p++ = ':';
1343     }
1344     if (directory && directory[0])
1345     {
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++ = '\\';
1351     }
1352     if (filename && filename[0])
1353     {
1354         unsigned int len = strlenW(filename);
1355         memmove(p, filename, len * sizeof(MSVCRT_wchar_t));
1356         p += len;
1357     }
1358     if (extension && extension[0])
1359     {
1360         if (extension[0] != '.')
1361             *p++ = '.';
1362         strcpyW(p, extension);
1363     }
1364     else
1365         *p = '\0';
1366
1367     TRACE("returning %s\n", debugstr_w(path));
1368 }
1369
1370 /*********************************************************************
1371  *              _makepath_s (MSVCRT.@)
1372  *
1373  * Safe version of _makepath.
1374  */
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)
1378 {
1379     char *p = path;
1380
1381     if (!path || !size)
1382     {
1383         *MSVCRT__errno() = MSVCRT_EINVAL;
1384         return MSVCRT_EINVAL;
1385     }
1386
1387     if (drive && drive[0])
1388     {
1389         if (size <= 2)
1390             goto range;
1391
1392         *p++ = drive[0];
1393         *p++ = ':';
1394         size -= 2;
1395     }
1396
1397     if (directory && directory[0])
1398     {
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);
1402
1403         if (size < 2)
1404             goto range;
1405
1406         memmove(p, directory, copylen);
1407
1408         if (size <= len)
1409             goto range;
1410
1411         p += copylen;
1412         size -= copylen;
1413
1414         if (needs_separator)
1415         {
1416             if (size < 2)
1417                 goto range;
1418
1419             *p++ = '\\';
1420             size -= 1;
1421         }
1422     }
1423
1424     if (filename && filename[0])
1425     {
1426         unsigned int len = strlen(filename);
1427         unsigned int copylen = min(size - 1, len);
1428
1429         if (size < 2)
1430             goto range;
1431
1432         memmove(p, filename, copylen);
1433
1434         if (size <= len)
1435             goto range;
1436
1437         p += len;
1438         size -= len;
1439     }
1440
1441     if (extension && extension[0])
1442     {
1443         unsigned int len = strlen(extension);
1444         unsigned int needs_period = extension[0] != '.';
1445         unsigned int copylen;
1446
1447         if (size < 2)
1448             goto range;
1449
1450         if (needs_period)
1451         {
1452             *p++ = '.';
1453             size -= 1;
1454         }
1455
1456         copylen = min(size - 1, len);
1457         memcpy(p, extension, copylen);
1458
1459         if (size <= len)
1460             goto range;
1461
1462         p += copylen;
1463     }
1464
1465     *p = '\0';
1466     return 0;
1467
1468 range:
1469     path[0] = '\0';
1470     *MSVCRT__errno() = MSVCRT_ERANGE;
1471     return MSVCRT_ERANGE;
1472 }
1473
1474 /*********************************************************************
1475  *              _wmakepath_s (MSVCRT.@)
1476  *
1477  * Safe version of _wmakepath.
1478  */
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)
1482 {
1483     MSVCRT_wchar_t *p = path;
1484
1485     if (!path || !size)
1486     {
1487         *MSVCRT__errno() = MSVCRT_EINVAL;
1488         return MSVCRT_EINVAL;
1489     }
1490
1491     if (drive && drive[0])
1492     {
1493         if (size <= 2)
1494             goto range;
1495
1496         *p++ = drive[0];
1497         *p++ = ':';
1498         size -= 2;
1499     }
1500
1501     if (directory && directory[0])
1502     {
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);
1506
1507         if (size < 2)
1508             goto range;
1509
1510         memmove(p, directory, copylen * sizeof(MSVCRT_wchar_t));
1511
1512         if (size <= len)
1513             goto range;
1514
1515         p += copylen;
1516         size -= copylen;
1517
1518         if (needs_separator)
1519         {
1520             if (size < 2)
1521                 goto range;
1522
1523             *p++ = '\\';
1524             size -= 1;
1525         }
1526     }
1527
1528     if (filename && filename[0])
1529     {
1530         unsigned int len = strlenW(filename);
1531         unsigned int copylen = min(size - 1, len);
1532
1533         if (size < 2)
1534             goto range;
1535
1536         memmove(p, filename, copylen * sizeof(MSVCRT_wchar_t));
1537
1538         if (size <= len)
1539             goto range;
1540
1541         p += len;
1542         size -= len;
1543     }
1544
1545     if (extension && extension[0])
1546     {
1547         unsigned int len = strlenW(extension);
1548         unsigned int needs_period = extension[0] != '.';
1549         unsigned int copylen;
1550
1551         if (size < 2)
1552             goto range;
1553
1554         if (needs_period)
1555         {
1556             *p++ = '.';
1557             size -= 1;
1558         }
1559
1560         copylen = min(size - 1, len);
1561         memcpy(p, extension, copylen * sizeof(MSVCRT_wchar_t));
1562
1563         if (size <= len)
1564             goto range;
1565
1566         p += copylen;
1567     }
1568
1569     *p = '\0';
1570     return 0;
1571
1572 range:
1573     path[0] = '\0';
1574     *MSVCRT__errno() = MSVCRT_ERANGE;
1575     return MSVCRT_ERANGE;
1576 }
1577
1578 /*********************************************************************
1579  *              _searchenv (MSVCRT.@)
1580  *
1581  * Search for a file in a list of paths from an environment variable.
1582  *
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.
1587  *
1588  * RETURNS
1589  *  Nothing. If the file is not found, buf will contain an empty string
1590  *  and errno is set.
1591  */
1592 void CDECL MSVCRT__searchenv(const char* file, const char* env, char *buf)
1593 {
1594   char*envVal, *penv;
1595   char curPath[MAX_PATH];
1596
1597   *buf = '\0';
1598
1599   /* Try CWD first */
1600   if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1601   {
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;
1606   }
1607
1608   /* Search given environment variable */
1609   envVal = MSVCRT_getenv(env);
1610   if (!envVal)
1611   {
1612     msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1613     return;
1614   }
1615
1616   penv = envVal;
1617   TRACE(":searching for %s in paths %s\n", file, envVal);
1618
1619   do
1620   {
1621     char *end = penv;
1622
1623     while(*end && *end != ';') end++; /* Find end of next path */
1624     if (penv == end || !*penv)
1625     {
1626       msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1627       return;
1628     }
1629     memcpy(curPath, penv, end - penv);
1630     if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1631     {
1632       curPath[end - penv] = '\\';
1633       curPath[end - penv + 1] = '\0';
1634     }
1635     else
1636       curPath[end - penv] = '\0';
1637
1638     strcat(curPath, file);
1639     TRACE("Checking for file %s\n", curPath);
1640     if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1641     {
1642       strcpy(buf, curPath);
1643       msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1644       return; /* Found */
1645     }
1646     penv = *end ? end + 1 : end;
1647   } while(1);
1648 }
1649
1650 /*********************************************************************
1651  *              _searchenv_s (MSVCRT.@)
1652  */
1653 int CDECL _searchenv_s(const char* file, const char* env, char *buf, MSVCRT_size_t count)
1654 {
1655   char*envVal, *penv;
1656   char curPath[MAX_PATH];
1657
1658   if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1659       !MSVCRT_CHECK_PMT(count > 0))
1660   {
1661       *MSVCRT__errno() = MSVCRT_EINVAL;
1662       return MSVCRT_EINVAL;
1663   }
1664
1665   *buf = '\0';
1666
1667   /* Try CWD first */
1668   if (GetFileAttributesA( file ) != INVALID_FILE_ATTRIBUTES)
1669   {
1670     if (GetFullPathNameA( file, count, buf, NULL )) return 0;
1671     msvcrt_set_errno(GetLastError());
1672     return 0;
1673   }
1674
1675   /* Search given environment variable */
1676   envVal = MSVCRT_getenv(env);
1677   if (!envVal)
1678   {
1679     *MSVCRT__errno() = MSVCRT_ENOENT;
1680     return MSVCRT_ENOENT;
1681   }
1682
1683   penv = envVal;
1684   TRACE(":searching for %s in paths %s\n", file, envVal);
1685
1686   do
1687   {
1688     char *end = penv;
1689
1690     while(*end && *end != ';') end++; /* Find end of next path */
1691     if (penv == end || !*penv)
1692     {
1693       *MSVCRT__errno() = MSVCRT_ENOENT;
1694       return MSVCRT_ENOENT;
1695     }
1696     memcpy(curPath, penv, end - penv);
1697     if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1698     {
1699       curPath[end - penv] = '\\';
1700       curPath[end - penv + 1] = '\0';
1701     }
1702     else
1703       curPath[end - penv] = '\0';
1704
1705     strcat(curPath, file);
1706     TRACE("Checking for file %s\n", curPath);
1707     if (GetFileAttributesA( curPath ) != INVALID_FILE_ATTRIBUTES)
1708     {
1709       if (strlen(curPath) + 1 > count)
1710       {
1711           MSVCRT_INVALID_PMT("buf[count] is too small");
1712           *MSVCRT__errno() = MSVCRT_ERANGE;
1713           return MSVCRT_ERANGE;
1714       }
1715       strcpy(buf, curPath);
1716       return 0;
1717     }
1718     penv = *end ? end + 1 : end;
1719   } while(1);
1720 }
1721
1722 /*********************************************************************
1723  *      _wsearchenv (MSVCRT.@)
1724  *
1725  * Unicode version of _searchenv
1726  */
1727 void CDECL MSVCRT__wsearchenv(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env, MSVCRT_wchar_t *buf)
1728 {
1729   MSVCRT_wchar_t *envVal, *penv;
1730   MSVCRT_wchar_t curPath[MAX_PATH];
1731
1732   *buf = '\0';
1733
1734   /* Try CWD first */
1735   if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1736   {
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;
1741   }
1742
1743   /* Search given environment variable */
1744   envVal = MSVCRT__wgetenv(env);
1745   if (!envVal)
1746   {
1747     msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1748     return;
1749   }
1750
1751   penv = envVal;
1752   TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1753
1754   do
1755   {
1756     MSVCRT_wchar_t *end = penv;
1757
1758     while(*end && *end != ';') end++; /* Find end of next path */
1759     if (penv == end || !*penv)
1760     {
1761       msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1762       return;
1763     }
1764     memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1765     if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1766     {
1767       curPath[end - penv] = '\\';
1768       curPath[end - penv + 1] = '\0';
1769     }
1770     else
1771       curPath[end - penv] = '\0';
1772
1773     strcatW(curPath, file);
1774     TRACE("Checking for file %s\n", debugstr_w(curPath));
1775     if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1776     {
1777       strcpyW(buf, curPath);
1778       msvcrt_set_errno(ERROR_FILE_NOT_FOUND);
1779       return; /* Found */
1780     }
1781     penv = *end ? end + 1 : end;
1782   } while(1);
1783 }
1784
1785 /*********************************************************************
1786  *              _wsearchenv_s (MSVCRT.@)
1787  */
1788 int CDECL _wsearchenv_s(const MSVCRT_wchar_t* file, const MSVCRT_wchar_t* env,
1789                         MSVCRT_wchar_t *buf, MSVCRT_size_t count)
1790 {
1791   MSVCRT_wchar_t*       envVal, *penv;
1792   MSVCRT_wchar_t        curPath[MAX_PATH];
1793
1794   if (!MSVCRT_CHECK_PMT(file != NULL) || !MSVCRT_CHECK_PMT(buf != NULL) ||
1795       !MSVCRT_CHECK_PMT(count > 0))
1796   {
1797       *MSVCRT__errno() = MSVCRT_EINVAL;
1798       return MSVCRT_EINVAL;
1799   }
1800   *buf = '\0';
1801
1802   /* Try CWD first */
1803   if (GetFileAttributesW( file ) != INVALID_FILE_ATTRIBUTES)
1804   {
1805     if (GetFullPathNameW( file, count, buf, NULL )) return 0;
1806     msvcrt_set_errno(GetLastError());
1807     return 0;
1808   }
1809
1810   /* Search given environment variable */
1811   envVal = MSVCRT__wgetenv(env);
1812   if (!envVal)
1813   {
1814     *MSVCRT__errno() = MSVCRT_ENOENT;
1815     return MSVCRT_ENOENT;
1816   }
1817
1818   penv = envVal;
1819   TRACE(":searching for %s in paths %s\n", debugstr_w(file), debugstr_w(envVal));
1820
1821   do
1822   {
1823     MSVCRT_wchar_t *end = penv;
1824
1825     while(*end && *end != ';') end++; /* Find end of next path */
1826     if (penv == end || !*penv)
1827     {
1828       *MSVCRT__errno() = MSVCRT_ENOENT;
1829       return MSVCRT_ENOENT;
1830     }
1831     memcpy(curPath, penv, (end - penv) * sizeof(MSVCRT_wchar_t));
1832     if (curPath[end - penv] != '/' && curPath[end - penv] != '\\')
1833     {
1834       curPath[end - penv] = '\\';
1835       curPath[end - penv + 1] = '\0';
1836     }
1837     else
1838       curPath[end - penv] = '\0';
1839
1840     strcatW(curPath, file);
1841     TRACE("Checking for file %s\n", debugstr_w(curPath));
1842     if (GetFileAttributesW( curPath ) != INVALID_FILE_ATTRIBUTES)
1843     {
1844       if (strlenW(curPath) + 1 > count)
1845       {
1846           MSVCRT_INVALID_PMT("buf[count] is too small");
1847           *MSVCRT__errno() = MSVCRT_ERANGE;
1848           return MSVCRT_ERANGE;
1849       }
1850       strcpyW(buf, curPath);
1851       return 0;
1852     }
1853     penv = *end ? end + 1 : end;
1854   } while(1);
1855 }