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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifdef __WIN32OS2__
|
---|
25 | #include <stdlib.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <ctype.h>
|
---|
28 | #include <winbase.h>
|
---|
29 | #include "emxheader.h"
|
---|
30 | #else
|
---|
31 | #include "config.h"
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifndef __MINIVCRT__
|
---|
35 |
|
---|
36 | #include "wine/port.h"
|
---|
37 |
|
---|
38 | #include <time.h>
|
---|
39 | #include "winternl.h"
|
---|
40 | #include "wine/unicode.h"
|
---|
41 | #include "msvcrt.h"
|
---|
42 | #include "msvcrt/errno.h"
|
---|
43 |
|
---|
44 | #include "wine/unicode.h"
|
---|
45 | #include "msvcrt/direct.h"
|
---|
46 | #include "msvcrt/dos.h"
|
---|
47 | #include "msvcrt/io.h"
|
---|
48 | #include "msvcrt/stdlib.h"
|
---|
49 | #include "msvcrt/string.h"
|
---|
50 |
|
---|
51 | #include "wine/debug.h"
|
---|
52 |
|
---|
53 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
---|
54 |
|
---|
55 | #else /* !__MINIVCRT__ */
|
---|
56 |
|
---|
57 | #include "minivcrt.h"
|
---|
58 | #include "minivcrt_internal.h"
|
---|
59 |
|
---|
60 | #include "winternl.h"
|
---|
61 | #include "wine/unicode.h"
|
---|
62 |
|
---|
63 | #include <errno.h>
|
---|
64 | #include <stdlib.h>
|
---|
65 | #include <limits.h>
|
---|
66 |
|
---|
67 | #ifdef __EMX__
|
---|
68 | #include <direct.h>
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #endif /* !__MINIVCRT__ */
|
---|
72 |
|
---|
73 | #ifndef __MINIVCRT__
|
---|
74 |
|
---|
75 | /* INTERNAL: Translate finddata_t to PWIN32_FIND_DATAA */
|
---|
76 | static void msvcrt_fttofd(LPWIN32_FIND_DATAA fd, struct _finddata_t* ft)
|
---|
77 | {
|
---|
78 | DWORD dw;
|
---|
79 |
|
---|
80 | if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
|
---|
81 | ft->attrib = 0;
|
---|
82 | else
|
---|
83 | ft->attrib = fd->dwFileAttributes;
|
---|
84 |
|
---|
85 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftCreationTime, &dw );
|
---|
86 | ft->time_create = dw;
|
---|
87 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
|
---|
88 | ft->time_access = dw;
|
---|
89 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
|
---|
90 | ft->time_write = dw;
|
---|
91 | ft->size = fd->nFileSizeLow;
|
---|
92 | strcpy(ft->name, fd->cFileName);
|
---|
93 | }
|
---|
94 |
|
---|
95 | #endif /* !__MINIVCRT__ */
|
---|
96 |
|
---|
97 | /* INTERNAL: Translate wfinddata_t to PWIN32_FIND_DATAA */
|
---|
98 | static void msvcrt_wfttofd(LPWIN32_FIND_DATAW fd, struct _wfinddata_t* ft)
|
---|
99 | {
|
---|
100 | DWORD dw;
|
---|
101 |
|
---|
102 | if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
|
---|
103 | ft->attrib = 0;
|
---|
104 | else
|
---|
105 | ft->attrib = fd->dwFileAttributes;
|
---|
106 |
|
---|
107 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftCreationTime, &dw );
|
---|
108 | ft->time_create = dw;
|
---|
109 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
|
---|
110 | ft->time_access = dw;
|
---|
111 | RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
|
---|
112 | ft->time_write = dw;
|
---|
113 | ft->size = fd->nFileSizeLow;
|
---|
114 | strcpyW(ft->name, fd->cFileName);
|
---|
115 | }
|
---|
116 |
|
---|
117 | #ifndef __MINIVCRT__
|
---|
118 |
|
---|
119 | /*********************************************************************
|
---|
120 | * _chdir (MSVCRT.@)
|
---|
121 | */
|
---|
122 | int MSVCRT__chdir(const char * newdir)
|
---|
123 | {
|
---|
124 | if (!SetCurrentDirectoryA(newdir))
|
---|
125 | {
|
---|
126 | MSVCRT__set_errno(newdir?GetLastError():0);
|
---|
127 | return -1;
|
---|
128 | }
|
---|
129 | return 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | #endif /* !__MINIVCRT__ */
|
---|
133 |
|
---|
134 | /*********************************************************************
|
---|
135 | * _wchdir (MSVCRT.@)
|
---|
136 | */
|
---|
137 | int _wchdir(const MSVCRT_wchar_t * newdir)
|
---|
138 | {
|
---|
139 | if (!SetCurrentDirectoryW(newdir))
|
---|
140 | {
|
---|
141 | MSVCRT__set_errno(newdir?GetLastError():0);
|
---|
142 | return -1;
|
---|
143 | }
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | #ifndef __MINIVCRT__
|
---|
148 |
|
---|
149 | /*********************************************************************
|
---|
150 | * _chdrive (MSVCRT.@)
|
---|
151 | */
|
---|
152 | int MSVCRT__chdrive(int newdrive)
|
---|
153 | {
|
---|
154 | char buffer[3] = "A:";
|
---|
155 | buffer[0] += newdrive - 1;
|
---|
156 | if (!SetCurrentDirectoryA( buffer ))
|
---|
157 | {
|
---|
158 | MSVCRT__set_errno(GetLastError());
|
---|
159 | if (newdrive <= 0)
|
---|
160 | *MSVCRT__errno() = MSVCRT_EACCES;
|
---|
161 | return -1;
|
---|
162 | }
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*********************************************************************
|
---|
167 | * _findclose (MSVCRT.@)
|
---|
168 | */
|
---|
169 | int _findclose(long hand)
|
---|
170 | {
|
---|
171 | TRACE(":handle %ld\n",hand);
|
---|
172 | if (!FindClose((HANDLE)hand))
|
---|
173 | {
|
---|
174 | MSVCRT__set_errno(GetLastError());
|
---|
175 | return -1;
|
---|
176 | }
|
---|
177 | return 0;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*********************************************************************
|
---|
181 | * _findfirst (MSVCRT.@)
|
---|
182 | */
|
---|
183 | long _findfirst(const char * fspec, struct _finddata_t* ft)
|
---|
184 | {
|
---|
185 | WIN32_FIND_DATAA find_data;
|
---|
186 | HANDLE hfind;
|
---|
187 |
|
---|
188 | hfind = FindFirstFileA(fspec, &find_data);
|
---|
189 | if (hfind == INVALID_HANDLE_VALUE)
|
---|
190 | {
|
---|
191 | MSVCRT__set_errno(GetLastError());
|
---|
192 | return -1;
|
---|
193 | }
|
---|
194 | msvcrt_fttofd(&find_data,ft);
|
---|
195 | TRACE(":got handle %p\n",hfind);
|
---|
196 | return (long)hfind;
|
---|
197 | }
|
---|
198 |
|
---|
199 | #endif /* !__MINIVCRT__ */
|
---|
200 |
|
---|
201 | /*********************************************************************
|
---|
202 | * _wfindfirst (MSVCRT.@)
|
---|
203 | */
|
---|
204 | long _wfindfirst(const MSVCRT_wchar_t * fspec, struct _wfinddata_t* ft)
|
---|
205 | {
|
---|
206 | WIN32_FIND_DATAW find_data;
|
---|
207 | HANDLE hfind;
|
---|
208 |
|
---|
209 | hfind = FindFirstFileW(fspec, &find_data);
|
---|
210 | if (hfind == INVALID_HANDLE_VALUE)
|
---|
211 | {
|
---|
212 | MSVCRT__set_errno(GetLastError());
|
---|
213 | return -1;
|
---|
214 | }
|
---|
215 | msvcrt_wfttofd(&find_data,ft);
|
---|
216 | TRACE(":got handle %p\n",hfind);
|
---|
217 | return (long)hfind;
|
---|
218 | }
|
---|
219 |
|
---|
220 | #ifndef __MINIVCRT__
|
---|
221 |
|
---|
222 | /*********************************************************************
|
---|
223 | * _findnext (MSVCRT.@)
|
---|
224 | */
|
---|
225 | int _findnext(long hand, struct _finddata_t * ft)
|
---|
226 | {
|
---|
227 | WIN32_FIND_DATAA find_data;
|
---|
228 |
|
---|
229 | if (!FindNextFileA((HANDLE)hand, &find_data))
|
---|
230 | {
|
---|
231 | *MSVCRT__errno() = MSVCRT(ENOENT);
|
---|
232 | return -1;
|
---|
233 | }
|
---|
234 |
|
---|
235 | msvcrt_fttofd(&find_data,ft);
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 |
|
---|
239 | #endif /* !__MINIVCRT__ */
|
---|
240 |
|
---|
241 | /*********************************************************************
|
---|
242 | * _wfindnext (MSVCRT.@)
|
---|
243 | */
|
---|
244 | int _wfindnext(long hand, struct _wfinddata_t * ft)
|
---|
245 | {
|
---|
246 | WIN32_FIND_DATAW find_data;
|
---|
247 |
|
---|
248 | if (!FindNextFileW((HANDLE)hand, &find_data))
|
---|
249 | {
|
---|
250 | *MSVCRT__errno() = MSVCRT(ENOENT);
|
---|
251 | return -1;
|
---|
252 | }
|
---|
253 |
|
---|
254 | msvcrt_wfttofd(&find_data,ft);
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 | #ifndef __MINIVCRT__
|
---|
259 |
|
---|
260 | /*********************************************************************
|
---|
261 | * _getcwd (MSVCRT.@)
|
---|
262 | */
|
---|
263 | char* MSVCRT__getcwd(char * buf, int size)
|
---|
264 | {
|
---|
265 | char dir[MAX_PATH];
|
---|
266 | int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
|
---|
267 |
|
---|
268 | if (dir_len < 1)
|
---|
269 | return NULL; /* FIXME: Real return value untested */
|
---|
270 |
|
---|
271 | if (!buf)
|
---|
272 | {
|
---|
273 | if (size < 0)
|
---|
274 | return MSVCRT__strdup(dir);
|
---|
275 | return msvcrt_strndup(dir,size);
|
---|
276 | }
|
---|
277 | if (dir_len >= size)
|
---|
278 | {
|
---|
279 | *MSVCRT__errno() = MSVCRT_ERANGE;
|
---|
280 | return NULL; /* buf too small */
|
---|
281 | }
|
---|
282 | strcpy(buf,dir);
|
---|
283 | return buf;
|
---|
284 | }
|
---|
285 |
|
---|
286 | #endif /* !__MINIVCRT__ */
|
---|
287 |
|
---|
288 | /*********************************************************************
|
---|
289 | * _wgetcwd (MSVCRT.@)
|
---|
290 | */
|
---|
291 | MSVCRT_wchar_t* _wgetcwd(MSVCRT_wchar_t * buf, int size)
|
---|
292 | {
|
---|
293 | MSVCRT_wchar_t dir[MAX_PATH];
|
---|
294 | int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
|
---|
295 |
|
---|
296 | if (dir_len < 1)
|
---|
297 | return NULL; /* FIXME: Real return value untested */
|
---|
298 |
|
---|
299 | if (!buf)
|
---|
300 | {
|
---|
301 | if (size < 0)
|
---|
302 | return _wcsdup(dir);
|
---|
303 | return msvcrt_wstrndup(dir,size);
|
---|
304 | }
|
---|
305 | if (dir_len >= size)
|
---|
306 | {
|
---|
307 | *MSVCRT__errno() = MSVCRT(ERANGE);
|
---|
308 | return NULL; /* buf too small */
|
---|
309 | }
|
---|
310 | strcpyW(buf,dir);
|
---|
311 | return buf;
|
---|
312 | }
|
---|
313 |
|
---|
314 | #ifndef __MINIVCRT__
|
---|
315 |
|
---|
316 | /*********************************************************************
|
---|
317 | * _getdrive (MSVCRT.@)
|
---|
318 | */
|
---|
319 | int MSVCRT__getdrive(void)
|
---|
320 | {
|
---|
321 | char buffer[MAX_PATH];
|
---|
322 | if (!GetCurrentDirectoryA( sizeof(buffer), buffer )) return 0;
|
---|
323 | if (buffer[1] != ':') return 0;
|
---|
324 | return toupper(buffer[0]) - 'A' + 1;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*********************************************************************
|
---|
328 | * _getdcwd (MSVCRT.@)
|
---|
329 | */
|
---|
330 | char* MSVCRT__getdcwd(int drive, char * buf, int size)
|
---|
331 | {
|
---|
332 | static char* dummy;
|
---|
333 |
|
---|
334 | TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
|
---|
335 |
|
---|
336 | if (!drive || drive == MSVCRT__getdrive())
|
---|
337 | return MSVCRT__getcwd(buf,size); /* current */
|
---|
338 | else
|
---|
339 | {
|
---|
340 | char dir[MAX_PATH];
|
---|
341 | char drivespec[4] = {'A', ':', '\\', 0};
|
---|
342 | int dir_len;
|
---|
343 |
|
---|
344 | drivespec[0] += drive - 1;
|
---|
345 | if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
|
---|
346 | {
|
---|
347 | *MSVCRT__errno() = MSVCRT_EACCES;
|
---|
348 | return NULL;
|
---|
349 | }
|
---|
350 |
|
---|
351 | dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
|
---|
352 | if (dir_len >= size || dir_len < 1)
|
---|
353 | {
|
---|
354 | *MSVCRT__errno() = MSVCRT_ERANGE;
|
---|
355 | return NULL; /* buf too small */
|
---|
356 | }
|
---|
357 |
|
---|
358 | TRACE(":returning '%s'\n", dir);
|
---|
359 | if (!buf)
|
---|
360 | return MSVCRT__strdup(dir); /* allocate */
|
---|
361 |
|
---|
362 | strcpy(buf,dir);
|
---|
363 | }
|
---|
364 | return buf;
|
---|
365 | }
|
---|
366 |
|
---|
367 | #endif /* !__MINIVCRT__ */
|
---|
368 |
|
---|
369 | /*********************************************************************
|
---|
370 | * _wgetdcwd (MSVCRT.@)
|
---|
371 | */
|
---|
372 | MSVCRT_wchar_t* _wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
|
---|
373 | {
|
---|
374 | static MSVCRT_wchar_t* dummy;
|
---|
375 |
|
---|
376 | TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
|
---|
377 |
|
---|
378 | if (!drive || drive == MSVCRT__getdrive())
|
---|
379 | return _wgetcwd(buf,size); /* current */
|
---|
380 | else
|
---|
381 | {
|
---|
382 | MSVCRT_wchar_t dir[MAX_PATH];
|
---|
383 | MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
|
---|
384 | int dir_len;
|
---|
385 |
|
---|
386 | drivespec[0] += drive - 1;
|
---|
387 | if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
|
---|
388 | {
|
---|
389 | *MSVCRT__errno() = MSVCRT(EACCES);
|
---|
390 | return NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 | dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
|
---|
394 | if (dir_len >= size || dir_len < 1)
|
---|
395 | {
|
---|
396 | *MSVCRT__errno() = MSVCRT(ERANGE);
|
---|
397 | return NULL; /* buf too small */
|
---|
398 | }
|
---|
399 |
|
---|
400 | TRACE(":returning %s\n", debugstr_w(dir));
|
---|
401 | if (!buf)
|
---|
402 | return _wcsdup(dir); /* allocate */
|
---|
403 | strcpyW(buf,dir);
|
---|
404 | }
|
---|
405 | return buf;
|
---|
406 | }
|
---|
407 |
|
---|
408 | #ifndef __MINIVCRT__
|
---|
409 |
|
---|
410 | /*********************************************************************
|
---|
411 | * _getdiskfree (MSVCRT.@)
|
---|
412 | */
|
---|
413 | unsigned int _getdiskfree(unsigned int disk, struct _diskfree_t* d)
|
---|
414 | {
|
---|
415 | char drivespec[4] = {'@', ':', '\\', 0};
|
---|
416 | DWORD ret[4];
|
---|
417 | unsigned int err;
|
---|
418 |
|
---|
419 | if (disk > 26)
|
---|
420 | return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
|
---|
421 |
|
---|
422 | drivespec[0] += disk; /* make a drive letter */
|
---|
423 |
|
---|
424 | if (GetDiskFreeSpaceA(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
|
---|
425 | {
|
---|
426 | d->sectors_per_cluster = (unsigned)ret[0];
|
---|
427 | d->bytes_per_sector = (unsigned)ret[1];
|
---|
428 | d->avail_clusters = (unsigned)ret[2];
|
---|
429 | d->total_clusters = (unsigned)ret[3];
|
---|
430 | return 0;
|
---|
431 | }
|
---|
432 | err = GetLastError();
|
---|
433 | MSVCRT__set_errno(err);
|
---|
434 | return err;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /*********************************************************************
|
---|
438 | * _mkdir (MSVCRT.@)
|
---|
439 | */
|
---|
440 | int MSVCRT__mkdir(const char * newdir)
|
---|
441 | {
|
---|
442 | if (CreateDirectoryA(newdir,NULL))
|
---|
443 | return 0;
|
---|
444 | MSVCRT__set_errno(GetLastError());
|
---|
445 | return -1;
|
---|
446 | }
|
---|
447 |
|
---|
448 | #endif /* !__MINIVCRT__ */
|
---|
449 |
|
---|
450 | /*********************************************************************
|
---|
451 | * _wmkdir (MSVCRT.@)
|
---|
452 | */
|
---|
453 | int _wmkdir(const MSVCRT_wchar_t* newdir)
|
---|
454 | {
|
---|
455 | if (CreateDirectoryW(newdir,NULL))
|
---|
456 | return 0;
|
---|
457 | MSVCRT__set_errno(GetLastError());
|
---|
458 | return -1;
|
---|
459 | }
|
---|
460 |
|
---|
461 | #ifndef __MINIVCRT__
|
---|
462 |
|
---|
463 | /*********************************************************************
|
---|
464 | * _rmdir (MSVCRT.@)
|
---|
465 | */
|
---|
466 | int MSVCRT__rmdir(const char * dir)
|
---|
467 | {
|
---|
468 | if (RemoveDirectoryA(dir))
|
---|
469 | return 0;
|
---|
470 | MSVCRT__set_errno(GetLastError());
|
---|
471 | return -1;
|
---|
472 | }
|
---|
473 |
|
---|
474 | #endif /* !__MINIVCRT__ */
|
---|
475 |
|
---|
476 | /*********************************************************************
|
---|
477 | * _wrmdir (MSVCRT.@)
|
---|
478 | */
|
---|
479 | int _wrmdir(const MSVCRT_wchar_t * dir)
|
---|
480 | {
|
---|
481 | if (RemoveDirectoryW(dir))
|
---|
482 | return 0;
|
---|
483 | MSVCRT__set_errno(GetLastError());
|
---|
484 | return -1;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /*********************************************************************
|
---|
488 | * _wsplitpath (MSVCRT.@)
|
---|
489 | */
|
---|
490 | void _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
|
---|
491 | MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext )
|
---|
492 | {
|
---|
493 | /* Modified PD code from 'snippets' collection. */
|
---|
494 | MSVCRT_wchar_t ch, *ptr, *p;
|
---|
495 | MSVCRT_wchar_t pathbuff[MAX_PATH],*path=pathbuff;
|
---|
496 |
|
---|
497 | TRACE("MSVCRT: _wsplitpath %s\n",debugstr_w(path));
|
---|
498 | /* FIXME: Should be an strncpyW or something */
|
---|
499 | strcpyW(pathbuff, inpath);
|
---|
500 |
|
---|
501 | /* convert slashes to backslashes for searching */
|
---|
502 | for (ptr = (MSVCRT_wchar_t*)path; *ptr; ++ptr)
|
---|
503 | if (*ptr == '/')
|
---|
504 | *ptr = '\\';
|
---|
505 |
|
---|
506 | /* look for drive spec */
|
---|
507 | if ((ptr = strchrW(path, ':')) != 0)
|
---|
508 | {
|
---|
509 | ++ptr;
|
---|
510 | if (drv)
|
---|
511 | {
|
---|
512 | strncpyW(drv, path, ptr - path);
|
---|
513 | drv[ptr - path] = 0;
|
---|
514 | }
|
---|
515 | path = ptr;
|
---|
516 | }
|
---|
517 | else if (drv)
|
---|
518 | *drv = 0;
|
---|
519 |
|
---|
520 | /* find rightmost backslash or leftmost colon */
|
---|
521 | if ((ptr = strrchrW(path, '\\')) == NULL)
|
---|
522 | ptr = (strchrW(path, ':'));
|
---|
523 |
|
---|
524 | if (!ptr)
|
---|
525 | {
|
---|
526 | ptr = (MSVCRT_wchar_t *)path; /* no path */
|
---|
527 | if (dir)
|
---|
528 | *dir = 0;
|
---|
529 | }
|
---|
530 | else
|
---|
531 | {
|
---|
532 | ++ptr; /* skip the delimiter */
|
---|
533 | if (dir)
|
---|
534 | {
|
---|
535 | ch = *ptr;
|
---|
536 | *ptr = 0;
|
---|
537 | strcpyW(dir, path);
|
---|
538 | *ptr = ch;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | if ((p = strrchrW(ptr, '.')) == NULL)
|
---|
543 | {
|
---|
544 | if (fname)
|
---|
545 | strcpyW(fname, ptr);
|
---|
546 | if (ext)
|
---|
547 | *ext = 0;
|
---|
548 | }
|
---|
549 | else
|
---|
550 | {
|
---|
551 | *p = 0;
|
---|
552 | if (fname)
|
---|
553 | strcpyW(fname, ptr);
|
---|
554 | *p = '.';
|
---|
555 | if (ext)
|
---|
556 | strcpyW(ext, p);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* Fix pathological case - Win returns ':' as part of the
|
---|
560 | * directory when no drive letter is given.
|
---|
561 | */
|
---|
562 | if (drv && drv[0] == ':')
|
---|
563 | {
|
---|
564 | *drv = 0;
|
---|
565 | if (dir)
|
---|
566 | {
|
---|
567 | pathbuff[0] = ':';
|
---|
568 | pathbuff[1] = 0;
|
---|
569 | strcatW(pathbuff,dir);
|
---|
570 | strcpyW(dir, pathbuff);
|
---|
571 | }
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | static char *msvcrt_clean_path(char *path)
|
---|
576 | {
|
---|
577 | char *cur, *sep, *next;
|
---|
578 |
|
---|
579 | /* convert "/" => "\" */
|
---|
580 | cur = path;
|
---|
581 | while (*cur)
|
---|
582 | {
|
---|
583 | if (*cur == '/')
|
---|
584 | *cur = '\\';
|
---|
585 | ++cur;
|
---|
586 | }
|
---|
587 |
|
---|
588 | cur = path;
|
---|
589 | while (*cur)
|
---|
590 | {
|
---|
591 | sep = strchr(cur, '\\');
|
---|
592 | if (!sep)
|
---|
593 | sep = cur + strlen(cur);
|
---|
594 |
|
---|
595 | next = sep;
|
---|
596 | while (*next == '\\')
|
---|
597 | ++next;
|
---|
598 |
|
---|
599 | if (sep - cur == 1 && *cur == '.')
|
---|
600 | {
|
---|
601 | /* eat "." */
|
---|
602 | strcpy(cur, next);
|
---|
603 | }
|
---|
604 | else if (sep - cur == 2 && cur[0] == '.' && cur[1] == '.')
|
---|
605 | {
|
---|
606 | /* go one level up if there is any */
|
---|
607 | if (cur - path > 1 && cur[-2] != ':')
|
---|
608 | {
|
---|
609 | cur -= 2;
|
---|
610 | while (cur > path && *cur != '\\')
|
---|
611 | --cur;
|
---|
612 | if (*cur == '\\')
|
---|
613 | ++cur;
|
---|
614 | }
|
---|
615 | strcpy(cur, next);
|
---|
616 | }
|
---|
617 | else if (next - sep > 1)
|
---|
618 | {
|
---|
619 | /* eat multiple "\\" */
|
---|
620 | cur = sep + 1;
|
---|
621 | strcpy(cur, next);
|
---|
622 | }
|
---|
623 | else
|
---|
624 | {
|
---|
625 | cur = next;
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 | /* strip trailing '\' unless it indicates the root path */
|
---|
630 | if (cur - path > 1 && cur[-1] == '\\' && cur[-2] != ':')
|
---|
631 | *--cur = '\0';
|
---|
632 |
|
---|
633 | return path;
|
---|
634 | }
|
---|
635 |
|
---|
636 | #ifdef __EMX__
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * NOTE: _fullpath() is broken in kLIBC and changes CWD (see
|
---|
640 | * http://svn.netlabs.org/odin32/ticket/63 for details). Enable the
|
---|
641 | * alternative implementation.
|
---|
642 | */
|
---|
643 |
|
---|
644 | /*********************************************************************
|
---|
645 | * _fullpath (MSVCRT.@)
|
---|
646 | */
|
---|
647 | char *MSVCRT__fullpath(char * absPath, const char* relPath, unsigned int size)
|
---|
648 | {
|
---|
649 | char drive[5],dir[MAX_PATH],file[MAX_PATH],ext[MAX_PATH];
|
---|
650 | char res[MAX_PATH];
|
---|
651 | size_t len;
|
---|
652 |
|
---|
653 | res[0] = '\0';
|
---|
654 |
|
---|
655 | if (!relPath || !*relPath)
|
---|
656 | return msvcrt_clean_path(MSVCRT(_getcwd)(absPath, size));
|
---|
657 |
|
---|
658 | if (size < 4)
|
---|
659 | {
|
---|
660 | *MSVCRT__errno() = MSVCRT(ERANGE);
|
---|
661 | return NULL;
|
---|
662 | }
|
---|
663 |
|
---|
664 | TRACE("MSVCRT: _fullpath '%s'\n",relPath);
|
---|
665 |
|
---|
666 | _splitpath(relPath, drive, dir, file, ext);
|
---|
667 |
|
---|
668 | /* Get Directory and drive into 'res' */
|
---|
669 | if (!dir[0] || (dir[0] != '/' && dir[0] != '\\'))
|
---|
670 | {
|
---|
671 | /* Relative or no directory given */
|
---|
672 | _getdcwd(drive[0] ? toupper(drive[0]) - 'A' + 1 : 0, res, MAX_PATH);
|
---|
673 | strcat(res,"\\");
|
---|
674 | if (dir[0])
|
---|
675 | strcat(res,dir);
|
---|
676 | if (drive[0])
|
---|
677 | res[0] = drive[0]; /* If given a drive, preserve the letter case */
|
---|
678 | }
|
---|
679 | else
|
---|
680 | {
|
---|
681 | if (!drive[0])
|
---|
682 | {
|
---|
683 | drive[0] = _getdrive();
|
---|
684 | drive[1] = ':';
|
---|
685 | drive[2] = '\0';
|
---|
686 | }
|
---|
687 | strcpy(res,drive);
|
---|
688 | strcat(res,dir);
|
---|
689 | }
|
---|
690 |
|
---|
691 | strcat(res,"\\");
|
---|
692 | strcat(res, file);
|
---|
693 | strcat(res, ext);
|
---|
694 | msvcrt_clean_path(res);
|
---|
695 |
|
---|
696 | len = strlen(res);
|
---|
697 | if (len >= MAX_PATH || len >= (size_t)size)
|
---|
698 | return NULL; /* FIXME: errno? */
|
---|
699 |
|
---|
700 | if (!absPath)
|
---|
701 | return MSVCRT(_strdup(res));
|
---|
702 | strcpy(absPath,res);
|
---|
703 | return absPath;
|
---|
704 | }
|
---|
705 |
|
---|
706 | #endif /* __EMX__ */
|
---|
707 |
|
---|
708 | #ifndef __MINIVCRT__
|
---|
709 |
|
---|
710 | /*********************************************************************
|
---|
711 | * _makepath (MSVCRT.@)
|
---|
712 | */
|
---|
713 | VOID MSVCRT__makepath(char * path, const char * drive,
|
---|
714 | const char *directory, const char * filename,
|
---|
715 | const char * extension )
|
---|
716 | {
|
---|
717 | char ch;
|
---|
718 | TRACE("got %s %s %s %s\n", drive, directory,
|
---|
719 | filename, extension);
|
---|
720 |
|
---|
721 | if ( !path )
|
---|
722 | return;
|
---|
723 |
|
---|
724 | path[0] = 0;
|
---|
725 | if (drive && drive[0])
|
---|
726 | {
|
---|
727 | path[0] = drive[0];
|
---|
728 | path[1] = ':';
|
---|
729 | path[2] = 0;
|
---|
730 | }
|
---|
731 | if (directory && directory[0])
|
---|
732 | {
|
---|
733 | strcat(path, directory);
|
---|
734 | ch = path[strlen(path)-1];
|
---|
735 | if (ch != '/' && ch != '\\')
|
---|
736 | strcat(path,"\\");
|
---|
737 | }
|
---|
738 | if (filename && filename[0])
|
---|
739 | {
|
---|
740 | strcat(path, filename);
|
---|
741 | if (extension && extension[0])
|
---|
742 | {
|
---|
743 | if ( extension[0] != '.' )
|
---|
744 | strcat(path,".");
|
---|
745 | strcat(path,extension);
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | TRACE("returning %s\n",path);
|
---|
750 | }
|
---|
751 |
|
---|
752 | #endif /* !__MINIVCRT__ */
|
---|
753 |
|
---|
754 | /*********************************************************************
|
---|
755 | * _wmakepath (MSVCRT.@)
|
---|
756 | */
|
---|
757 | VOID _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
|
---|
758 | const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
|
---|
759 | {
|
---|
760 | MSVCRT_wchar_t ch;
|
---|
761 | TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
|
---|
762 | debugstr_w(filename), debugstr_w(extension));
|
---|
763 |
|
---|
764 | if ( !path )
|
---|
765 | return;
|
---|
766 |
|
---|
767 | path[0] = 0;
|
---|
768 | if (drive && drive[0])
|
---|
769 | {
|
---|
770 | path[0] = drive[0];
|
---|
771 | path[1] = ':';
|
---|
772 | path[2] = 0;
|
---|
773 | }
|
---|
774 | if (directory && directory[0])
|
---|
775 | {
|
---|
776 | strcatW(path, directory);
|
---|
777 | ch = path[strlenW(path) - 1];
|
---|
778 | if (ch != '/' && ch != '\\')
|
---|
779 | {
|
---|
780 | static const MSVCRT_wchar_t backslashW[] = {'\\',0};
|
---|
781 | strcatW(path, backslashW);
|
---|
782 | }
|
---|
783 | }
|
---|
784 | if (filename && filename[0])
|
---|
785 | {
|
---|
786 | strcatW(path, filename);
|
---|
787 | if (extension && extension[0])
|
---|
788 | {
|
---|
789 | if ( extension[0] != '.' )
|
---|
790 | {
|
---|
791 | static const MSVCRT_wchar_t dotW[] = {'.',0};
|
---|
792 | strcatW(path, dotW);
|
---|
793 | }
|
---|
794 | strcatW(path, extension);
|
---|
795 | }
|
---|
796 | }
|
---|
797 |
|
---|
798 | TRACE("returning %s\n", debugstr_w(path));
|
---|
799 | }
|
---|
800 |
|
---|
801 | #ifndef __MINIVCRT__
|
---|
802 |
|
---|
803 | /*********************************************************************
|
---|
804 | * _searchenv (MSVCRT.@)
|
---|
805 | */
|
---|
806 | void MSVCRT__searchenv(const char* file, const char* env, char *buf)
|
---|
807 | {
|
---|
808 | char*envVal, *penv;
|
---|
809 | char curPath[MAX_PATH];
|
---|
810 |
|
---|
811 | *buf = '\0';
|
---|
812 |
|
---|
813 | /* Try CWD first */
|
---|
814 | if (GetFileAttributesA( file ) != 0xFFFFFFFF)
|
---|
815 | {
|
---|
816 | GetFullPathNameA( file, MAX_PATH, buf, NULL );
|
---|
817 | /* Sigh. This error is *always* set, regardless of success */
|
---|
818 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
---|
819 | return;
|
---|
820 | }
|
---|
821 |
|
---|
822 | /* Search given environment variable */
|
---|
823 | envVal = MSVCRT_getenv(env);
|
---|
824 | if (!envVal)
|
---|
825 | {
|
---|
826 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
---|
827 | return;
|
---|
828 | }
|
---|
829 |
|
---|
830 | penv = envVal;
|
---|
831 | TRACE(":searching for %s in paths %s\n", file, envVal);
|
---|
832 |
|
---|
833 | do
|
---|
834 | {
|
---|
835 | char *end = penv;
|
---|
836 |
|
---|
837 | while(*end && *end != ';') end++; /* Find end of next path */
|
---|
838 | if (penv == end || !*penv)
|
---|
839 | {
|
---|
840 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
---|
841 | return;
|
---|
842 | }
|
---|
843 | strncpy(curPath, penv, end - penv);
|
---|
844 | if (curPath[end - penv] != '/' || curPath[end - penv] != '\\')
|
---|
845 | {
|
---|
846 | curPath[end - penv] = '\\';
|
---|
847 | curPath[end - penv + 1] = '\0';
|
---|
848 | }
|
---|
849 | else
|
---|
850 | curPath[end - penv] = '\0';
|
---|
851 |
|
---|
852 | strcat(curPath, file);
|
---|
853 | TRACE("Checking for file %s\n", curPath);
|
---|
854 | if (GetFileAttributesA( curPath ) != 0xFFFFFFFF)
|
---|
855 | {
|
---|
856 | strcpy(buf, curPath);
|
---|
857 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
|
---|
858 | return; /* Found */
|
---|
859 | }
|
---|
860 | penv = *end ? end + 1 : end;
|
---|
861 | } while(1);
|
---|
862 | }
|
---|
863 |
|
---|
864 | #endif /* !__MINIVCRT__ */
|
---|
865 |
|
---|
866 | MSVCRT_wchar_t* _wfullpath(MSVCRT_wchar_t* absPath,const MSVCRT_wchar_t* relPath,MSVCRT(size_t) size)
|
---|
867 | {
|
---|
868 | char asciiabsPath[280], asciirelPath[280];
|
---|
869 |
|
---|
870 | TRACE("MSVCRT: _wfullpath %s %d\n", debugstr_w(relPath),size);
|
---|
871 |
|
---|
872 | WideCharToMultiByte(CP_ACP, 0, relPath, -1, (LPSTR)asciirelPath, 280, NULL, NULL);
|
---|
873 |
|
---|
874 | MSVCRT__fullpath(asciiabsPath, asciirelPath, size);
|
---|
875 |
|
---|
876 | MultiByteToWideChar(CP_ACP, 0, asciiabsPath, -1, absPath, size);
|
---|
877 |
|
---|
878 | return absPath;
|
---|
879 | }
|
---|