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
26 #include "wine/unicode.h"
30 #include "wine/unicode.h"
31 #include "msvcrt/direct.h"
32 #include "msvcrt/dos.h"
33 #include "msvcrt/io.h"
34 #include "msvcrt/stdlib.h"
35 #include "msvcrt/string.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 /* INTERNAL: Translate finddata_t to PWIN32_FIND_DATAA */
42 static void msvcrt_fttofd(LPWIN32_FIND_DATAA fd, struct _finddata_t* ft)
46 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
49 ft->attrib = fd->dwFileAttributes;
51 RtlTimeToSecondsSince1970( &fd->ftCreationTime, &dw );
53 RtlTimeToSecondsSince1970( &fd->ftLastAccessTime, &dw );
55 RtlTimeToSecondsSince1970( &fd->ftLastWriteTime, &dw );
57 ft->size = fd->nFileSizeLow;
58 strcpy(ft->name, fd->cFileName);
61 /* INTERNAL: Translate wfinddata_t to PWIN32_FIND_DATAA */
62 static void msvcrt_wfttofd(LPWIN32_FIND_DATAW fd, struct _wfinddata_t* ft)
66 if (fd->dwFileAttributes == FILE_ATTRIBUTE_NORMAL)
69 ft->attrib = fd->dwFileAttributes;
71 RtlTimeToSecondsSince1970( &fd->ftCreationTime, &dw );
73 RtlTimeToSecondsSince1970( &fd->ftLastAccessTime, &dw );
75 RtlTimeToSecondsSince1970( &fd->ftLastWriteTime, &dw );
77 ft->size = fd->nFileSizeLow;
78 strcpyW(ft->name, fd->cFileName);
81 /*********************************************************************
84 int _chdir(const char * newdir)
86 if (!SetCurrentDirectoryA(newdir))
88 MSVCRT__set_errno(newdir?GetLastError():0);
94 /*********************************************************************
97 int _wchdir(const WCHAR * newdir)
99 if (!SetCurrentDirectoryW(newdir))
101 MSVCRT__set_errno(newdir?GetLastError():0);
107 /*********************************************************************
108 * _chdrive (MSVCRT.@)
110 int _chdrive(int newdrive)
112 char buffer[3] = "A:";
113 buffer[0] += newdrive - 1;
114 if (!SetCurrentDirectoryA( buffer ))
116 MSVCRT__set_errno(GetLastError());
118 SET_THREAD_VAR(errno,MSVCRT_EACCES);
124 /*********************************************************************
125 * _findclose (MSVCRT.@)
127 int _findclose(long hand)
129 TRACE(":handle %ld\n",hand);
130 if (!FindClose((HANDLE)hand))
132 MSVCRT__set_errno(GetLastError());
138 /*********************************************************************
139 * _findfirst (MSVCRT.@)
141 long _findfirst(const char * fspec, struct _finddata_t* ft)
143 WIN32_FIND_DATAA find_data;
146 hfind = FindFirstFileA(fspec, &find_data);
147 if (hfind == INVALID_HANDLE_VALUE)
149 MSVCRT__set_errno(GetLastError());
152 msvcrt_fttofd(&find_data,ft);
153 TRACE(":got handle %d\n",hfind);
157 /*********************************************************************
158 * _wfindfirst (MSVCRT.@)
160 long _wfindfirst(const WCHAR * fspec, struct _wfinddata_t* ft)
162 WIN32_FIND_DATAW find_data;
165 hfind = FindFirstFileW(fspec, &find_data);
166 if (hfind == INVALID_HANDLE_VALUE)
168 MSVCRT__set_errno(GetLastError());
171 msvcrt_wfttofd(&find_data,ft);
172 TRACE(":got handle %d\n",hfind);
176 /*********************************************************************
177 * _findnext (MSVCRT.@)
179 int _findnext(long hand, struct _finddata_t * ft)
181 WIN32_FIND_DATAA find_data;
183 if (!FindNextFileA(hand, &find_data))
185 SET_THREAD_VAR(errno,MSVCRT_ENOENT);
189 msvcrt_fttofd(&find_data,ft);
193 /*********************************************************************
194 * _wfindnext (MSVCRT.@)
196 int _wfindnext(long hand, struct _wfinddata_t * ft)
198 WIN32_FIND_DATAW find_data;
200 if (!FindNextFileW(hand, &find_data))
202 SET_THREAD_VAR(errno,MSVCRT_ENOENT);
206 msvcrt_wfttofd(&find_data,ft);
210 /*********************************************************************
213 char* _getcwd(char * buf, int size)
216 int dir_len = GetCurrentDirectoryA(MAX_PATH,dir);
219 return NULL; /* FIXME: Real return value untested */
225 return msvcrt_strndup(dir,size);
229 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
230 return NULL; /* buf too small */
236 /*********************************************************************
237 * _wgetcwd (MSVCRT.@)
239 WCHAR* _wgetcwd(WCHAR * buf, int size)
241 WCHAR dir[_MAX_PATH];
242 int dir_len = GetCurrentDirectoryW(MAX_PATH,dir);
245 return NULL; /* FIXME: Real return value untested */
251 return msvcrt_wstrndup(dir,size);
255 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
256 return NULL; /* buf too small */
262 /*********************************************************************
263 * _getdrive (MSVCRT.@)
267 char buffer[MAX_PATH];
268 if (!GetCurrentDirectoryA( sizeof(buffer), buffer )) return 0;
269 if (buffer[1] != ':') return 0;
270 return toupper(buffer[0]) - 'A' + 1;
273 /*********************************************************************
274 * _getdcwd (MSVCRT.@)
276 char* _getdcwd(int drive, char * buf, int size)
280 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
282 if (!drive || drive == _getdrive())
283 return _getcwd(buf,size); /* current */
287 char drivespec[4] = {'A', ':', '\\', 0};
290 drivespec[0] += drive - 1;
291 if (GetDriveTypeA(drivespec) < DRIVE_REMOVABLE)
293 SET_THREAD_VAR(errno,MSVCRT_EACCES);
297 dir_len = GetFullPathNameA(drivespec,_MAX_PATH,dir,&dummy);
298 if (dir_len >= size || dir_len < 1)
300 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
301 return NULL; /* buf too small */
304 TRACE(":returning '%s'\n", dir);
306 return _strdup(dir); /* allocate */
313 /*********************************************************************
314 * _wgetdcwd (MSVCRT.@)
316 WCHAR* _wgetdcwd(int drive, WCHAR * buf, int size)
320 TRACE(":drive %d(%c), size %d\n",drive, drive + 'A' - 1, size);
322 if (!drive || drive == _getdrive())
323 return _wgetcwd(buf,size); /* current */
326 WCHAR dir[_MAX_PATH];
327 WCHAR drivespec[4] = {'A', ':', '\\', 0};
330 drivespec[0] += drive - 1;
331 if (GetDriveTypeW(drivespec) < DRIVE_REMOVABLE)
333 SET_THREAD_VAR(errno,MSVCRT_EACCES);
337 dir_len = GetFullPathNameW(drivespec,_MAX_PATH,dir,&dummy);
338 if (dir_len >= size || dir_len < 1)
340 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
341 return NULL; /* buf too small */
344 TRACE(":returning %s\n", debugstr_w(dir));
346 return _wcsdup(dir); /* allocate */
352 /*********************************************************************
353 * _getdiskfree (MSVCRT.@)
355 unsigned int _getdiskfree(unsigned int disk, struct _diskfree_t* d)
357 char drivespec[4] = {'@', ':', '\\', 0};
362 return ERROR_INVALID_PARAMETER; /* MSVCRT doesn't set errno here */
364 drivespec[0] += disk; /* make a drive letter */
366 if (GetDiskFreeSpaceA(disk==0?NULL:drivespec,ret,ret+1,ret+2,ret+3))
368 d->sectors_per_cluster = (unsigned)ret[0];
369 d->bytes_per_sector = (unsigned)ret[1];
370 d->avail_clusters = (unsigned)ret[2];
371 d->total_clusters = (unsigned)ret[3];
374 err = GetLastError();
375 MSVCRT__set_errno(err);
379 /*********************************************************************
382 int _mkdir(const char * newdir)
384 if (CreateDirectoryA(newdir,NULL))
386 MSVCRT__set_errno(GetLastError());
390 /*********************************************************************
393 int _wmkdir(const WCHAR* newdir)
395 if (CreateDirectoryW(newdir,NULL))
397 MSVCRT__set_errno(GetLastError());
401 /*********************************************************************
404 int _rmdir(const char * dir)
406 if (RemoveDirectoryA(dir))
408 MSVCRT__set_errno(GetLastError());
412 /*********************************************************************
415 int _wrmdir(const WCHAR * dir)
417 if (RemoveDirectoryW(dir))
419 MSVCRT__set_errno(GetLastError());
423 /*********************************************************************
424 * _wsplitpath (MSVCRT.@)
426 void _wsplitpath(const WCHAR *inpath, WCHAR *drv, WCHAR *dir,
427 WCHAR *fname, WCHAR *ext )
429 /* Modified PD code from 'snippets' collection. */
431 WCHAR pathbuff[MAX_PATH],*path=pathbuff;
433 TRACE(":splitting path %s\n",debugstr_w(path));
434 /* FIXME: Should be an strncpyW or something */
435 strcpyW(pathbuff, inpath);
437 /* convert slashes to backslashes for searching */
438 for (ptr = (WCHAR*)path; *ptr; ++ptr)
439 if (*ptr == (WCHAR)L'/')
442 /* look for drive spec */
443 if ((ptr = strchrW(path, (WCHAR)L':')) != (WCHAR)L'\0')
448 strncpyW(drv, path, ptr - path);
449 drv[ptr - path] = (WCHAR)L'\0';
456 /* find rightmost backslash or leftmost colon */
457 if ((ptr = strrchrW(path, (WCHAR)L'\\')) == NULL)
458 ptr = (strchrW(path, (WCHAR)L':'));
462 ptr = (WCHAR *)path; /* no path */
468 ++ptr; /* skip the delimiter */
478 if ((p = strrchrW(ptr, (WCHAR)L'.')) == NULL)
495 /* Fix pathological case - Win returns ':' as part of the
496 * directory when no drive letter is given.
498 if (drv && drv[0] == (WCHAR)L':')
503 pathbuff[0] = (WCHAR)L':';
504 pathbuff[1] = (WCHAR)L'\0';
505 strcatW(pathbuff,dir);
506 strcpyW(dir, pathbuff);
511 /* INTERNAL: Helper for _fullpath. Modified PD code from 'snippets'. */
512 static void msvcrt_fln_fix(char *path)
514 int dir_flag = 0, root_flag = 0;
518 if (NULL == (r = strrchr(path, ':')))
523 /* Ignore leading slashes */
533 p = r; /* Change "\\" to "\" */
534 while (NULL != (p = strchr(p, '\\')))
540 while ('.' == *r) /* Scrunch leading ".\" */
544 /* Ignore leading ".." */
545 for (p = (r += 2); *p && (*p != '\\'); ++p)
550 for (p = r + 1 ;*p && (*p != '\\'); ++p)
553 strcpy(r, p + ((*p) ? 1 : 0));
556 while ('\\' == path[strlen(path)-1]) /* Strip last '\\' */
559 path[strlen(path)-1] = '\0';
564 /* Look for "\." in path */
566 while (NULL != (p = strstr(s, "\\.")))
570 /* Execute this section if ".." found */
572 while (q > r) /* Backup one level */
585 strcpy(q + ((*q == '\\') ? 1 : 0),
586 p + 3 + ((*(p + 3)) ? 1 : 0));
593 /* Execute this section if "." found */
595 for ( ;*q && (*q != '\\'); ++q)
601 if (root_flag) /* Embedded ".." could have bubbled up to root */
603 for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
613 /*********************************************************************
614 * _fullpath (MSVCRT.@)
616 char *_fullpath(char * absPath, const char* relPath, unsigned int size)
618 char drive[5],dir[MAX_PATH],file[MAX_PATH],ext[MAX_PATH];
624 if (!relPath || !*relPath)
625 return _getcwd(absPath, size);
629 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
633 TRACE(":resolving relative path '%s'\n",relPath);
635 _splitpath(relPath, drive, dir, file, ext);
637 /* Get Directory and drive into 'res' */
638 if (!dir[0] || (dir[0] != '/' && dir[0] != '\\'))
640 /* Relative or no directory given */
641 _getdcwd(drive[0] ? toupper(drive[0]) - 'A' + 1 : 0, res, MAX_PATH);
646 res[0] = drive[0]; /* If given a drive, preserve the letter case */
660 if (len >= MAX_PATH || len >= (size_t)size)
661 return NULL; /* FIXME: errno? */
669 /*********************************************************************
670 * _makepath (MSVCRT.@)
672 VOID _makepath(char * path, const char * drive,
673 const char *directory, const char * filename,
674 const char * extension )
677 TRACE("got %s %s %s %s\n", drive, directory,
678 filename, extension);
684 if (drive && drive[0])
690 if (directory && directory[0])
692 strcat(path, directory);
693 ch = path[strlen(path)-1];
694 if (ch != '/' && ch != '\\')
697 if (filename && filename[0])
699 strcat(path, filename);
700 if (extension && extension[0])
702 if ( extension[0] != '.' )
704 strcat(path,extension);
708 TRACE("returning %s\n",path);
711 /*********************************************************************
712 * _wmakepath (MSVCRT.@)
714 VOID _wmakepath(WCHAR *path, const WCHAR *drive, const WCHAR *directory,
715 const WCHAR *filename, const WCHAR *extension)
718 TRACE("%s %s %s %s\n", debugstr_w(drive), debugstr_w(directory),
719 debugstr_w(filename), debugstr_w(extension));
725 if (drive && drive[0])
731 if (directory && directory[0])
733 strcatW(path, directory);
734 ch = path[strlenW(path) - 1];
735 if (ch != '/' && ch != '\\')
737 static const WCHAR backslashW[] = {'\\',0};
738 strcatW(path, backslashW);
741 if (filename && filename[0])
743 strcatW(path, filename);
744 if (extension && extension[0])
746 if ( extension[0] != '.' )
748 static const WCHAR dotW[] = {'.',0};
751 strcatW(path, extension);
755 TRACE("returning %s\n", debugstr_w(path));
758 /*********************************************************************
759 * _searchenv (MSVCRT.@)
761 void _searchenv(const char* file, const char* env, char *buf)
764 char curPath[MAX_PATH];
769 if (GetFileAttributesA( file ) != 0xFFFFFFFF)
771 GetFullPathNameA( file, MAX_PATH, buf, NULL );
772 /* Sigh. This error is *always* set, regardless of success */
773 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
777 /* Search given environment variable */
778 envVal = MSVCRT_getenv(env);
781 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
786 TRACE(":searching for %s in paths %s\n", file, envVal);
792 while(*end && *end != ';') end++; /* Find end of next path */
793 if (penv == end || !*penv)
795 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
798 strncpy(curPath, penv, end - penv);
799 if (curPath[end - penv] != '/' || curPath[end - penv] != '\\')
801 curPath[end - penv] = '\\';
802 curPath[end - penv + 1] = '\0';
805 curPath[end - penv] = '\0';
807 strcat(curPath, file);
808 TRACE("Checking for file %s\n", curPath);
809 if (GetFileAttributesA( curPath ) != 0xFFFFFFFF)
811 strcpy(buf, curPath);
812 MSVCRT__set_errno(ERROR_FILE_NOT_FOUND);
815 penv = *end ? end + 1 : end;