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
12 #include "wine/unicode.h"
16 #include "wine/unicode.h"
17 #include "msvcrt/direct.h"
18 #include "msvcrt/dos.h"
19 #include "msvcrt/io.h"
20 #include "msvcrt/stdlib.h"
21 #include "msvcrt/string.h"
23 DEFAULT_DEBUG_CHANNEL(msvcrt);
25 /* INTERNAL: Translate finddata_t to PWIN32_FIND_DATAA */
26 static void msvcrt_fttofd(LPWIN32_FIND_DATAA fd, struct _finddata_t* ft)
30 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
33 ft->attrib = fd->dwFileAttributes;
35 RtlTimeToSecondsSince1970( &fd->ftCreationTime, &dw );
37 RtlTimeToSecondsSince1970( &fd->ftLastAccessTime, &dw );
39 RtlTimeToSecondsSince1970( &fd->ftLastWriteTime, &dw );
41 ft->size = fd->nFileSizeLow;
42 strcpy(ft->name, fd->cFileName);
45 /* INTERNAL: Translate wfinddata_t to PWIN32_FIND_DATAA */
46 static void msvcrt_wfttofd(LPWIN32_FIND_DATAW fd, struct _wfinddata_t* ft)
50 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
53 ft->attrib = fd->dwFileAttributes;
55 RtlTimeToSecondsSince1970( &fd->ftCreationTime, &dw );
57 RtlTimeToSecondsSince1970( &fd->ftLastAccessTime, &dw );
59 RtlTimeToSecondsSince1970( &fd->ftLastWriteTime, &dw );
61 ft->size = fd->nFileSizeLow;
62 strcpyW(ft->name, fd->cFileName);
65 /*********************************************************************
68 int _chdir(const char * newdir)
70 if (!SetCurrentDirectoryA(newdir))
72 MSVCRT__set_errno(newdir?GetLastError():0);
78 /*********************************************************************
81 int _wchdir(const WCHAR * newdir)
83 if (!SetCurrentDirectoryW(newdir))
85 MSVCRT__set_errno(newdir?GetLastError():0);
91 /*********************************************************************
94 int _chdrive(int newdrive)
96 char buffer[3] = "A:";
97 buffer[0] += newdrive - 1;
98 if (!SetCurrentDirectoryA( buffer ))
100 MSVCRT__set_errno(GetLastError());
102 SET_THREAD_VAR(errno,MSVCRT_EACCES);
108 /*********************************************************************
109 * _findclose (MSVCRT.@)
111 int _findclose(long hand)
113 TRACE(":handle %ld\n",hand);
114 if (!FindClose((HANDLE)hand))
116 MSVCRT__set_errno(GetLastError());
122 /*********************************************************************
123 * _findfirst (MSVCRT.@)
125 long _findfirst(const char * fspec, struct _finddata_t* ft)
127 WIN32_FIND_DATAA find_data;
130 hfind = FindFirstFileA(fspec, &find_data);
131 if (hfind == INVALID_HANDLE_VALUE)
133 MSVCRT__set_errno(GetLastError());
136 msvcrt_fttofd(&find_data,ft);
137 TRACE(":got handle %d\n",hfind);
141 /*********************************************************************
142 * _wfindfirst (MSVCRT.@)
144 long _wfindfirst(const WCHAR * fspec, struct _wfinddata_t* ft)
146 WIN32_FIND_DATAW find_data;
149 hfind = FindFirstFileW(fspec, &find_data);
150 if (hfind == INVALID_HANDLE_VALUE)
152 MSVCRT__set_errno(GetLastError());
155 msvcrt_wfttofd(&find_data,ft);
156 TRACE(":got handle %d\n",hfind);
160 /*********************************************************************
161 * _findnext (MSVCRT.@)
163 int _findnext(long hand, struct _finddata_t * ft)
165 WIN32_FIND_DATAA find_data;
167 if (!FindNextFileA(hand, &find_data))
169 SET_THREAD_VAR(errno,MSVCRT_ENOENT);
173 msvcrt_fttofd(&find_data,ft);
177 /*********************************************************************
178 * _wfindnext (MSVCRT.@)
180 int _wfindnext(long hand, struct _wfinddata_t * ft)
182 WIN32_FIND_DATAW find_data;
184 if (!FindNextFileW(hand, &find_data))
186 SET_THREAD_VAR(errno,MSVCRT_ENOENT);
190 msvcrt_wfttofd(&find_data,ft);
194 /*********************************************************************
197 char* _getcwd(char * buf, int size)
200 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
203 return NULL; /* FIXME: Real return value untested */
209 return msvcrt_strndup(dir,size);
213 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
214 return NULL; /* buf too small */
220 /*********************************************************************
221 * _wgetcwd (MSVCRT.@)
223 WCHAR* _wgetcwd(WCHAR * buf, int size)
225 WCHAR dir[_MAX_PATH];
226 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
229 return NULL; /* FIXME: Real return value untested */
235 return msvcrt_wstrndup(dir,size);
239 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
240 return NULL; /* buf too small */
246 /*********************************************************************
247 * _getdrive (MSVCRT.@)
251 char buffer[MAX_PATH];
252 if (!GetCurrentDirectoryA( sizeof(buffer), buffer )) return 0;
253 if (buffer[1] != ':') return 0;
254 return toupper(buffer[0]) - 'A' + 1;
257 /*********************************************************************
258 * _getdcwd (MSVCRT.@)
260 char* _getdcwd(int drive, char * buf, int size)
264 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
266 if (!drive || drive == _getdrive())
267 return _getcwd(buf,size); /* current */
271 char drivespec[4] = {'A', ':', '\\', 0};
274 drivespec[0] += drive - 1;
275 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
277 SET_THREAD_VAR(errno,MSVCRT_EACCES);
281 dir_len = GetFullPathNameA(drivespec,_MAX_PATH,dir,&dummy);
282 if (dir_len >= size || dir_len < 1)
284 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
285 return NULL; /* buf too small */
288 TRACE(":returning '%s'\n", dir);
290 return _strdup(dir); /* allocate */
297 /*********************************************************************
298 * _wgetdcwd (MSVCRT.@)
300 WCHAR* _wgetdcwd(int drive, WCHAR * buf, int size)
304 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
306 if (!drive || drive == _getdrive())
307 return _wgetcwd(buf,size); /* current */
310 WCHAR dir[_MAX_PATH];
311 WCHAR drivespec[4] = {'A', ':', '\\', 0};
314 drivespec[0] += drive - 1;
315 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
317 SET_THREAD_VAR(errno,MSVCRT_EACCES);
321 dir_len = GetFullPathNameW(drivespec,_MAX_PATH,dir,&dummy);
322 if (dir_len >= size || dir_len < 1)
324 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
325 return NULL; /* buf too small */
328 TRACE(":returning %s\n", debugstr_w(dir));
330 return _wcsdup(dir); /* allocate */
336 /*********************************************************************
337 * _getdiskfree (MSVCRT.@)
339 unsigned int _getdiskfree(unsigned int disk, struct _diskfree_t* d)
341 char drivespec[4] = {'@', ':', '\\', 0};
346 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
348 drivespec[0] += disk; /* make a drive letter */
350 if (GetDiskFreeSpaceA(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
352 d->sectors_per_cluster = (unsigned)ret[0];
353 d->bytes_per_sector = (unsigned)ret[1];
354 d->avail_clusters = (unsigned)ret[2];
355 d->total_clusters = (unsigned)ret[3];
358 err = GetLastError();
359 MSVCRT__set_errno(err);
363 /*********************************************************************
366 int _mkdir(const char * newdir)
368 if (CreateDirectoryA(newdir,NULL))
370 MSVCRT__set_errno(GetLastError());
374 /*********************************************************************
377 int _wmkdir(const WCHAR* newdir)
379 if (CreateDirectoryW(newdir,NULL))
381 MSVCRT__set_errno(GetLastError());
385 /*********************************************************************
388 int _rmdir(const char * dir)
390 if (RemoveDirectoryA(dir))
392 MSVCRT__set_errno(GetLastError());
396 /*********************************************************************
399 int _wrmdir(const WCHAR * dir)
401 if (RemoveDirectoryW(dir))
403 MSVCRT__set_errno(GetLastError());
407 /*********************************************************************
408 * _wsplitpath (MSVCRT.@)
410 void _wsplitpath(const WCHAR *inpath, WCHAR *drv, WCHAR *dir,
411 WCHAR *fname, WCHAR *ext )
413 /* Modified PD code from 'snippets' collection. */
415 WCHAR pathbuff[MAX_PATH],*path=pathbuff;
417 TRACE(":splitting path %s\n",debugstr_w(path));
418 /* FIXME: Should be an strncpyW or something */
419 strcpyW(pathbuff, inpath);
421 /* convert slashes to backslashes for searching */
422 for (ptr = (WCHAR*)path; *ptr; ++ptr)
423 if (*ptr == (WCHAR)L'/')
426 /* look for drive spec */
427 if ((ptr = strchrW(path, (WCHAR)L':')) != (WCHAR)L'\0')
432 strncpyW(drv, path, ptr - path);
433 drv[ptr - path] = (WCHAR)L'\0';
440 /* find rightmost backslash or leftmost colon */
441 if ((ptr = strrchrW(path, (WCHAR)L'\\')) == NULL)
442 ptr = (strchrW(path, (WCHAR)L':'));
446 ptr = (WCHAR *)path; /* no path */
452 ++ptr; /* skip the delimiter */
462 if ((p = strrchrW(ptr, (WCHAR)L'.')) == NULL)
479 /* Fix pathological case - Win returns ':' as part of the
480 * directory when no drive letter is given.
482 if (drv && drv[0] == (WCHAR)L':')
487 pathbuff[0] = (WCHAR)L':';
488 pathbuff[1] = (WCHAR)L'\0';
489 strcatW(pathbuff,dir);
490 strcpyW(dir, pathbuff);
495 /* INTERNAL: Helper for _fullpath. Modified PD code from 'snippets'. */
496 static void msvcrt_fln_fix(char *path)
498 int dir_flag = 0, root_flag = 0;
502 if (NULL == (r = strrchr(path, ':')))
507 /* Ignore leading slashes */
517 p = r; /* Change "\\" to "\" */
518 while (NULL != (p = strchr(p, '\\')))
524 while ('.' == *r) /* Scrunch leading ".\" */
528 /* Ignore leading ".." */
529 for (p = (r += 2); *p && (*p != '\\'); ++p)
534 for (p = r + 1 ;*p && (*p != '\\'); ++p)
537 strcpy(r, p + ((*p) ? 1 : 0));
540 while ('\\' == path[strlen(path)-1]) /* Strip last '\\' */
543 path[strlen(path)-1] = '\0';
548 /* Look for "\." in path */
550 while (NULL != (p = strstr(s, "\\.")))
554 /* Execute this section if ".." found */
556 while (q > r) /* Backup one level */
569 strcpy(q + ((*q == '\\') ? 1 : 0),
570 p + 3 + ((*(p + 3)) ? 1 : 0));
577 /* Execute this section if "." found */
579 for ( ;*q && (*q != '\\'); ++q)
585 if (root_flag) /* Embedded ".." could have bubbled up to root */
587 for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
597 /*********************************************************************
598 * _fullpath (MSVCRT.@)
600 char *_fullpath(char * absPath, const char* relPath, unsigned int size)
602 char drive[5],dir[MAX_PATH],file[MAX_PATH],ext[MAX_PATH];
608 if (!relPath || !*relPath)
609 return _getcwd(absPath, size);
613 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
617 TRACE(":resolving relative path '%s'\n",relPath);
619 _splitpath(relPath, drive, dir, file, ext);
621 /* Get Directory and drive into 'res' */
622 if (!dir[0] || (dir[0] != '/' && dir[0] != '\\'))
624 /* Relative or no directory given */
625 _getdcwd(drive[0] ? toupper(drive[0]) - 'A' + 1 : 0, res, MAX_PATH);
630 res[0] = drive[0]; /* If given a drive, preserve the letter case */
644 if (len >= MAX_PATH || len >= (size_t)size)
645 return NULL; /* FIXME: errno? */
653 /*********************************************************************
654 * _makepath (MSVCRT.@)
656 VOID _makepath(char * path, const char * drive,
657 const char *directory, const char * filename,
658 const char * extension )
661 TRACE("got %s %s %s %s\n", drive, directory,
662 filename, extension);
668 if (drive && drive[0])
674 if (directory && directory[0])
676 strcat(path, directory);
677 ch = path[strlen(path)-1];
678 if (ch != '/' && ch != '\\')
681 if (filename && filename[0])
683 strcat(path, filename);
684 if (extension && extension[0])
686 if ( extension[0] != '.' )
688 strcat(path,extension);
692 TRACE("returning %s\n",path);
696 /*********************************************************************
697 * _searchenv (MSVCRT.@)
699 void _searchenv(const char* file, const char* env, char *buf)
702 char curPath[MAX_PATH];
707 if (GetFileAttributesA( file ) != 0xFFFFFFFF)
709 GetFullPathNameA( file, MAX_PATH, buf, NULL );
710 /* Sigh. This error is *always* set, regardless of success */
711 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
715 /* Search given environment variable */
716 envVal = MSVCRT_getenv(env);
719 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
724 TRACE(":searching for %s in paths %s\n", file, envVal);
730 while(*end && *end != ';') end++; /* Find end of next path */
731 if (penv == end || !*penv)
733 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
736 strncpy(curPath, penv, end - penv);
737 if (curPath[end - penv] != '/' || curPath[end - penv] != '\\')
739 curPath[end - penv] = '\\';
740 curPath[end - penv + 1] = '\0';
743 curPath[end - penv] = '\0';
745 strcat(curPath, file);
746 TRACE("Checking for file %s\n", curPath);
747 if (GetFileAttributesA( curPath ) != 0xFFFFFFFF)
749 strcpy(buf, curPath);
750 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
753 penv = *end ? end + 1 : end;