2 * msvcrt.dll drive/directory functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
29 #include "wine/unicode.h"
31 #include "msvcrt/errno.h"
33 #include "wine/unicode.h"
34 #include "msvcrt/direct.h"
35 #include "msvcrt/dos.h"
36 #include "msvcrt/io.h"
37 #include "msvcrt/stdlib.h"
38 #include "msvcrt/string.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
44 /* INTERNAL: Translate finddata_t to PWIN32_FIND_DATAA */
45 static void msvcrt_fttofd(LPWIN32_FIND_DATAA fd, struct _finddata_t* ft)
49 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
52 ft->attrib = fd->dwFileAttributes;
54 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftCreationTime, &dw );
56 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
58 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
60 ft->size = fd->nFileSizeLow;
61 strcpy(ft->name, fd->cFileName);
64 /* INTERNAL: Translate wfinddata_t to PWIN32_FIND_DATAA */
65 static void msvcrt_wfttofd(LPWIN32_FIND_DATAW fd, struct _wfinddata_t* ft)
69 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
72 ft->attrib = fd->dwFileAttributes;
74 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftCreationTime, &dw );
76 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastAccessTime, &dw );
78 RtlTimeToSecondsSince1970( (LARGE_INTEGER *)&fd->ftLastWriteTime, &dw );
80 ft->size = fd->nFileSizeLow;
81 strcpyW(ft->name, fd->cFileName);
84 /*********************************************************************
87 int _chdir(const char * newdir)
89 if (!SetCurrentDirectoryA(newdir))
91 MSVCRT__set_errno(newdir?GetLastError():0);
97 /*********************************************************************
100 int _wchdir(const MSVCRT_wchar_t * newdir)
102 if (!SetCurrentDirectoryW(newdir))
104 MSVCRT__set_errno(newdir?GetLastError():0);
110 /*********************************************************************
111 * _chdrive (MSVCRT.@)
113 int _chdrive(int newdrive)
115 char buffer[3] = "A:";
116 buffer[0] += newdrive - 1;
117 if (!SetCurrentDirectoryA( buffer ))
119 MSVCRT__set_errno(GetLastError());
121 *MSVCRT__errno() = MSVCRT_EACCES;
127 /*********************************************************************
128 * _findclose (MSVCRT.@)
130 int _findclose(long hand)
132 TRACE(":handle %ld\n",hand);
133 if (!FindClose((HANDLE)hand))
135 MSVCRT__set_errno(GetLastError());
141 /*********************************************************************
142 * _findfirst (MSVCRT.@)
144 long _findfirst(const char * fspec, struct _finddata_t* ft)
146 WIN32_FIND_DATAA find_data;
149 hfind = FindFirstFileA(fspec, &find_data);
150 if (hfind == INVALID_HANDLE_VALUE)
152 MSVCRT__set_errno(GetLastError());
155 msvcrt_fttofd(&find_data,ft);
156 TRACE(":got handle %p\n",hfind);
160 /*********************************************************************
161 * _wfindfirst (MSVCRT.@)
163 long _wfindfirst(const MSVCRT_wchar_t * fspec, struct _wfinddata_t* ft)
165 WIN32_FIND_DATAW find_data;
168 hfind = FindFirstFileW(fspec, &find_data);
169 if (hfind == INVALID_HANDLE_VALUE)
171 MSVCRT__set_errno(GetLastError());
174 msvcrt_wfttofd(&find_data,ft);
175 TRACE(":got handle %p\n",hfind);
179 /*********************************************************************
180 * _findnext (MSVCRT.@)
182 int _findnext(long hand, struct _finddata_t * ft)
184 WIN32_FIND_DATAA find_data;
186 if (!FindNextFileA((HANDLE)hand, &find_data))
188 *MSVCRT__errno() = MSVCRT_ENOENT;
192 msvcrt_fttofd(&find_data,ft);
196 /*********************************************************************
197 * _wfindnext (MSVCRT.@)
199 int _wfindnext(long hand, struct _wfinddata_t * ft)
201 WIN32_FIND_DATAW find_data;
203 if (!FindNextFileW((HANDLE)hand, &find_data))
205 *MSVCRT__errno() = MSVCRT_ENOENT;
209 msvcrt_wfttofd(&find_data,ft);
213 /*********************************************************************
216 char* _getcwd(char * buf, int size)
219 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
222 return NULL; /* FIXME: Real return value untested */
228 return msvcrt_strndup(dir,size);
232 *MSVCRT__errno() = MSVCRT_ERANGE;
233 return NULL; /* buf too small */
239 /*********************************************************************
240 * _wgetcwd (MSVCRT.@)
242 MSVCRT_wchar_t* _wgetcwd(MSVCRT_wchar_t * buf, int size)
244 MSVCRT_wchar_t dir[MAX_PATH];
245 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
248 return NULL; /* FIXME: Real return value untested */
254 return msvcrt_wstrndup(dir,size);
258 *MSVCRT__errno() = MSVCRT_ERANGE;
259 return NULL; /* buf too small */
265 /*********************************************************************
266 * _getdrive (MSVCRT.@)
270 char buffer[MAX_PATH];
271 if (!GetCurrentDirectoryA( sizeof(buffer), buffer )) return 0;
272 if (buffer[1] != ':') return 0;
273 return toupper(buffer[0]) - 'A' + 1;
276 /*********************************************************************
277 * _getdcwd (MSVCRT.@)
279 char* _getdcwd(int drive, char * buf, int size)
283 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
285 if (!drive || drive == _getdrive())
286 return _getcwd(buf,size); /* current */
290 char drivespec[4] = {'A', ':', '\\', 0};
293 drivespec[0] += drive - 1;
294 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
296 *MSVCRT__errno() = MSVCRT_EACCES;
300 dir_len = GetFullPathNameA(drivespec,MAX_PATH,dir,&dummy);
301 if (dir_len >= size || dir_len < 1)
303 *MSVCRT__errno() = MSVCRT_ERANGE;
304 return NULL; /* buf too small */
307 TRACE(":returning '%s'\n", dir);
309 return _strdup(dir); /* allocate */
316 /*********************************************************************
317 * _wgetdcwd (MSVCRT.@)
319 MSVCRT_wchar_t* _wgetdcwd(int drive, MSVCRT_wchar_t * buf, int size)
321 static MSVCRT_wchar_t* dummy;
323 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
325 if (!drive || drive == _getdrive())
326 return _wgetcwd(buf,size); /* current */
329 MSVCRT_wchar_t dir[MAX_PATH];
330 MSVCRT_wchar_t drivespec[4] = {'A', ':', '\\', 0};
333 drivespec[0] += drive - 1;
334 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
336 *MSVCRT__errno() = MSVCRT_EACCES;
340 dir_len = GetFullPathNameW(drivespec,MAX_PATH,dir,&dummy);
341 if (dir_len >= size || dir_len < 1)
343 *MSVCRT__errno() = MSVCRT_ERANGE;
344 return NULL; /* buf too small */
347 TRACE(":returning %s\n", debugstr_w(dir));
349 return _wcsdup(dir); /* allocate */
355 /*********************************************************************
356 * _getdiskfree (MSVCRT.@)
358 unsigned int _getdiskfree(unsigned int disk, struct _diskfree_t* d)
360 char drivespec[4] = {'@', ':', '\\', 0};
365 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
367 drivespec[0] += disk; /* make a drive letter */
369 if (GetDiskFreeSpaceA(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
371 d->sectors_per_cluster = (unsigned)ret[0];
372 d->bytes_per_sector = (unsigned)ret[1];
373 d->avail_clusters = (unsigned)ret[2];
374 d->total_clusters = (unsigned)ret[3];
377 err = GetLastError();
378 MSVCRT__set_errno(err);
382 /*********************************************************************
385 int _mkdir(const char * newdir)
387 if (CreateDirectoryA(newdir,NULL))
389 MSVCRT__set_errno(GetLastError());
393 /*********************************************************************
396 int _wmkdir(const MSVCRT_wchar_t* newdir)
398 if (CreateDirectoryW(newdir,NULL))
400 MSVCRT__set_errno(GetLastError());
404 /*********************************************************************
407 int _rmdir(const char * dir)
409 if (RemoveDirectoryA(dir))
411 MSVCRT__set_errno(GetLastError());
415 /*********************************************************************
418 int _wrmdir(const MSVCRT_wchar_t * dir)
420 if (RemoveDirectoryW(dir))
422 MSVCRT__set_errno(GetLastError());
426 /*********************************************************************
427 * _wsplitpath (MSVCRT.@)
429 void _wsplitpath(const MSVCRT_wchar_t *inpath, MSVCRT_wchar_t *drv, MSVCRT_wchar_t *dir,
430 MSVCRT_wchar_t *fname, MSVCRT_wchar_t *ext )
432 /* Modified PD code from 'snippets' collection. */
433 MSVCRT_wchar_t ch, *ptr, *p;
434 MSVCRT_wchar_t pathbuff[MAX_PATH],*path=pathbuff;
436 /* FIXME: Should be an strncpyW or something */
437 strcpyW(pathbuff, inpath);
438 TRACE(":splitting path %s\n",debugstr_w(path));
440 /* convert slashes to backslashes for searching */
441 for (ptr = (MSVCRT_wchar_t*)path; *ptr; ++ptr)
445 /* look for drive spec */
446 if ((ptr = strchrW(path, ':')) != 0)
451 strncpyW(drv, path, ptr - path);
459 /* find rightmost backslash or leftmost colon */
460 if ((ptr = strrchrW(path, '\\')) == NULL)
461 ptr = (strchrW(path, ':'));
465 ptr = (MSVCRT_wchar_t *)path; /* no path */
471 ++ptr; /* skip the delimiter */
481 if ((p = strrchrW(ptr, '.')) == NULL)
498 /* Fix pathological case - Win returns ':' as part of the
499 * directory when no drive letter is given.
501 if (drv && drv[0] == ':')
508 strcatW(pathbuff,dir);
509 strcpyW(dir, pathbuff);
514 /* INTERNAL: Helper for _fullpath. Modified PD code from 'snippets'. */
515 static void wmsvcrt_fln_fix(MSVCRT_wchar_t *path)
517 int dir_flag = 0, root_flag = 0;
518 MSVCRT_wchar_t *r, *p, *q, *s;
519 MSVCRT_wchar_t szbsdot[] = { '\\', '.', 0 };
522 if (NULL == (r = strrchrW(path, ':')))
527 /* Ignore leading slashes */
537 p = r; /* Change "\\" to "\" */
538 while (NULL != (p = strchrW(p, '\\')))
544 while ('.' == *r) /* Scrunch leading ".\" */
548 /* Ignore leading ".." */
549 for (p = (r += 2); *p && (*p != '\\'); ++p)
554 for (p = r + 1 ;*p && (*p != '\\'); ++p)
557 strcpyW(r, p + ((*p) ? 1 : 0));
560 while ('\\' == path[strlenW(path)-1]) /* Strip last '\\' */
563 path[strlenW(path)-1] = '\0';
568 /* Look for "\." in path */
570 while (NULL != (p = strstrW(s, szbsdot)))
574 /* Execute this section if ".." found */
576 while (q > r) /* Backup one level */
589 strcpyW(q + ((*q == '\\') ? 1 : 0),
590 p + 3 + ((*(p + 3)) ? 1 : 0));
597 /* Execute this section if "." found */
599 for ( ;*q && (*q != '\\'); ++q)
605 if (root_flag) /* Embedded ".." could have bubbled up to root */
607 for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
615 MSVCRT_wchar_t szbs[] = { '\\', 0 };
621 /*********************************************************************
622 * _wfullpath (MSVCRT.@)
624 MSVCRT_wchar_t *_wfullpath(MSVCRT_wchar_t * absPath, const MSVCRT_wchar_t* relPath, unsigned int size)
626 MSVCRT_wchar_t drive[5],dir[MAX_PATH],file[MAX_PATH],ext[MAX_PATH];
627 MSVCRT_wchar_t res[MAX_PATH];
629 MSVCRT_wchar_t szbs[] = { '\\', 0 };
634 if (!relPath || !*relPath)
635 return _wgetcwd(absPath, size);
639 *MSVCRT__errno() = MSVCRT_ERANGE;
643 TRACE(":resolving relative path '%s'\n",debugstr_w(relPath));
645 _wsplitpath(relPath, drive, dir, file, ext);
647 /* Get Directory and drive into 'res' */
648 if (!dir[0] || (dir[0] != '/' && dir[0] != '\\'))
650 /* Relative or no directory given */
651 _wgetdcwd(drive[0] ? toupper(drive[0]) - 'A' + 1 : 0, res, MAX_PATH);
656 res[0] = drive[0]; /* If given a drive, preserve the letter case */
667 wmsvcrt_fln_fix(res);
670 if (len >= MAX_PATH || len >= (size_t)size)
671 return NULL; /* FIXME: errno? */
675 strcpyW(absPath,res);
679 /* INTERNAL: Helper for _fullpath. Modified PD code from 'snippets'. */
680 static void msvcrt_fln_fix(char *path)
682 int dir_flag = 0, root_flag = 0;
686 if (NULL == (r = strrchr(path, ':')))
691 /* Ignore leading slashes */
701 p = r; /* Change "\\" to "\" */
702 while (NULL != (p = strchr(p, '\\')))
708 while ('.' == *r) /* Scrunch leading ".\" */
712 /* Ignore leading ".." */
713 for (p = (r += 2); *p && (*p != '\\'); ++p)
718 for (p = r + 1 ;*p && (*p != '\\'); ++p)
721 strcpy(r, p + ((*p) ? 1 : 0));
724 while ('\\' == path[strlen(path)-1]) /* Strip last '\\' */
727 path[strlen(path)-1] = '\0';
732 /* Look for "\." in path */
734 while (NULL != (p = strstr(s, "\\.")))
738 /* Execute this section if ".." found */
740 while (q > r) /* Backup one level */
753 strcpy(q + ((*q == '\\') ? 1 : 0),
754 p + 3 + ((*(p + 3)) ? 1 : 0));
761 /* Execute this section if "." found */
763 for ( ;*q && (*q != '\\'); ++q)
769 if (root_flag) /* Embedded ".." could have bubbled up to root */
771 for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
781 /*********************************************************************
782 * _fullpath (MSVCRT.@)
784 char *_fullpath(char * absPath, const char* relPath, unsigned int size)
786 char drive[5],dir[MAX_PATH],file[MAX_PATH],ext[MAX_PATH];
792 if (!relPath || !*relPath)
793 return _getcwd(absPath, size);
797 *MSVCRT__errno() = MSVCRT_ERANGE;
801 TRACE(":resolving relative path '%s'\n",relPath);
803 _splitpath(relPath, drive, dir, file, ext);
805 /* Get Directory and drive into 'res' */
806 if (!dir[0] || (dir[0] != '/' && dir[0] != '\\'))
808 /* Relative or no directory given */
809 _getdcwd(drive[0] ? toupper(drive[0]) - 'A' + 1 : 0, res, MAX_PATH);
814 res[0] = drive[0]; /* If given a drive, preserve the letter case */
828 if (len >= MAX_PATH || len >= (size_t)size)
829 return NULL; /* FIXME: errno? */
837 /*********************************************************************
838 * _makepath (MSVCRT.@)
840 VOID _makepath(char * path, const char * drive,
841 const char *directory, const char * filename,
842 const char * extension )
845 char tmpPath[MAX_PATH];
846 TRACE("got %s %s %s %s\n", debugstr_a(drive), debugstr_a(directory),
847 debugstr_a(filename), debugstr_a(extension) );
853 if (drive && drive[0])
855 tmpPath[0] = drive[0];
859 if (directory && directory[0])
861 strcat(tmpPath, directory);
862 ch = tmpPath[strlen(tmpPath)-1];
863 if (ch != '/' && ch != '\\')
864 strcat(tmpPath,"\\");
866 if (filename && filename[0])
868 strcat(tmpPath, filename);
869 if (extension && extension[0])
871 if ( extension[0] != '.' )
873 strcat(tmpPath,extension);
877 strcpy( path, tmpPath );
879 TRACE("returning %s\n",path);
882 /*********************************************************************
883 * _wmakepath (MSVCRT.@)
885 VOID _wmakepath(MSVCRT_wchar_t *path, const MSVCRT_wchar_t *drive, const MSVCRT_wchar_t *directory,
886 const MSVCRT_wchar_t *filename, const MSVCRT_wchar_t *extension)
889 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
890 debugstr_w(filename), debugstr_w(extension));
896 if (drive && drive[0])
902 if (directory && directory[0])
904 strcatW(path, directory);
905 ch = path[strlenW(path) - 1];
906 if (ch != '/' && ch != '\\')
908 static const MSVCRT_wchar_t backslashW[] = {'\\',0};
909 strcatW(path, backslashW);
912 if (filename && filename[0])
914 strcatW(path, filename);
915 if (extension && extension[0])
917 if ( extension[0] != '.' )
919 static const MSVCRT_wchar_t dotW[] = {'.',0};
922 strcatW(path, extension);
926 TRACE("returning %s\n", debugstr_w(path));
929 /*********************************************************************
930 * _searchenv (MSVCRT.@)
932 void _searchenv(const char* file, const char* env, char *buf)
935 char curPath[MAX_PATH];
940 if (GetFileAttributesA( file ) != 0xFFFFFFFF)
942 GetFullPathNameA( file, MAX_PATH, buf, NULL );
943 /* Sigh. This error is *always* set, regardless of success */
944 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
948 /* Search given environment variable */
949 envVal = MSVCRT_getenv(env);
952 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
957 TRACE(":searching for %s in paths %s\n", file, envVal);
963 while(*end && *end != ';') end++; /* Find end of next path */
964 if (penv == end || !*penv)
966 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
969 strncpy(curPath, penv, end - penv);
970 if (curPath[end - penv] != '/' || curPath[end - penv] != '\\')
972 curPath[end - penv] = '\\';
973 curPath[end - penv + 1] = '\0';
976 curPath[end - penv] = '\0';
978 strcat(curPath, file);
979 TRACE("Checking for file %s\n", curPath);
980 if (GetFileAttributesA( curPath ) != 0xFFFFFFFF)
982 strcpy(buf, curPath);
983 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
986 penv = *end ? end + 1 : end;