2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
13 #include <sys/types.h>
17 #ifdef HAVE_SYS_ERRNO_H
18 #include <sys/errno.h>
22 #include "wine/winbase16.h"
23 #include "wine/winestring.h"
26 #include "wine/winuser16.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(dosfs);
37 DECLARE_DEBUG_CHANNEL(file);
39 static DOS_FULL_NAME DIR_Windows;
40 static DOS_FULL_NAME DIR_System;
43 /***********************************************************************
46 * Get a path name from the wine.ini file and make sure it is valid.
48 static int DIR_GetPath( const char *keyname, const char *defval,
49 DOS_FULL_NAME *full_name )
51 char path[MAX_PATHNAME_LEN];
52 BY_HANDLE_FILE_INFORMATION info;
54 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
55 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
56 !FILE_Stat( full_name->long_name, &info ) ||
57 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
59 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
66 /***********************************************************************
71 char path[MAX_PATHNAME_LEN];
72 DOS_FULL_NAME tmp_dir, profile_dir;
76 if (!getcwd( path, MAX_PATHNAME_LEN ))
78 perror( "Could not get current directory" );
82 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
84 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
85 "for current working directory %s; "
86 "starting in windows directory.\n", cwd );
90 DRIVE_SetCurrentDrive( drive );
91 DRIVE_Chdir( drive, cwd );
94 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
95 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
96 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
98 PROFILE_UsageWineIni();
101 if (-1 == access( tmp_dir.long_name, W_OK ))
105 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
106 PROFILE_UsageWineIni();
109 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
115 drive = DIR_Windows.drive;
116 DRIVE_SetCurrentDrive( drive );
117 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
120 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
121 path, sizeof(path) );
122 if (strchr(path, '/'))
124 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
125 PROFILE_UsageWineIni();
129 /* Set the environment variables */
131 SetEnvironmentVariableA( "PATH", path );
132 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
133 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
134 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
135 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
137 TRACE("WindowsDir = %s (%s)\n",
138 DIR_Windows.short_name, DIR_Windows.long_name );
139 TRACE("SystemDir = %s (%s)\n",
140 DIR_System.short_name, DIR_System.long_name );
141 TRACE("TempDir = %s (%s)\n",
142 tmp_dir.short_name, tmp_dir.long_name );
143 TRACE("Path = %s\n", path );
144 TRACE("Cwd = %c:\\%s\n",
145 'A' + drive, DRIVE_GetDosCwd( drive ) );
147 if (DIR_GetPath( "profile", "", &profile_dir ))
149 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
150 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
153 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
154 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
160 /***********************************************************************
161 * GetTempPathA (KERNEL32.292)
163 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
166 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
167 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
168 if (!(ret = GetCurrentDirectoryA( count, path )))
170 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
179 /***********************************************************************
180 * GetTempPathW (KERNEL32.293)
182 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
184 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
185 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
187 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
188 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
189 if (!(ret = GetCurrentDirectoryW( count, path )))
191 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
200 /***********************************************************************
201 * DIR_GetWindowsUnixDir
203 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
205 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
206 return strlen( DIR_Windows.long_name );
210 /***********************************************************************
211 * DIR_GetSystemUnixDir
213 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
215 if (path) lstrcpynA( path, DIR_System.long_name, count );
216 return strlen( DIR_System.long_name );
220 /***********************************************************************
221 * GetTempDrive (KERNEL.92)
223 BYTE WINAPI GetTempDrive( BYTE ignored )
227 UINT len = GetTempPathA( 0, NULL );
229 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
230 return DRIVE_GetCurrentDrive() + 'A';
232 /* FIXME: apparently Windows does something with the ignored byte */
233 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
235 HeapFree( GetProcessHeap(), 0, buffer );
240 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
242 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
246 * AH: ':' - yes, some kernel code even does stosw with
250 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
254 /***********************************************************************
255 * GetWindowsDirectory16 (KERNEL.134)
257 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
259 return (UINT16)GetWindowsDirectoryA( path, count );
263 /***********************************************************************
264 * GetWindowsDirectoryA (KERNEL32.311)
266 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
268 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
269 return strlen( DIR_Windows.short_name );
273 /***********************************************************************
274 * GetWindowsDirectoryW (KERNEL32.312)
276 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
278 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
279 return strlen( DIR_Windows.short_name );
283 /***********************************************************************
284 * GetSystemDirectory16 (KERNEL.135)
286 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
288 return (UINT16)GetSystemDirectoryA( path, count );
292 /***********************************************************************
293 * GetSystemDirectoryA (KERNEL32.282)
295 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
297 if (path) lstrcpynA( path, DIR_System.short_name, count );
298 return strlen( DIR_System.short_name );
302 /***********************************************************************
303 * GetSystemDirectoryW (KERNEL32.283)
305 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
307 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
308 return strlen( DIR_System.short_name );
312 /***********************************************************************
313 * CreateDirectory16 (KERNEL.144)
315 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
317 TRACE_(file)("(%s,%p)\n", path, dummy );
318 return (BOOL16)CreateDirectoryA( path, NULL );
322 /***********************************************************************
323 * CreateDirectoryA (KERNEL32.39)
327 * ERROR_DISK_FULL: on full disk
328 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
329 * ERROR_ACCESS_DENIED: on permission problems
330 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
332 BOOL WINAPI CreateDirectoryA( LPCSTR path,
333 LPSECURITY_ATTRIBUTES lpsecattribs )
335 DOS_FULL_NAME full_name;
337 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
338 if (DOSFS_GetDevice( path ))
340 TRACE_(file)("cannot use device '%s'!\n",path);
341 SetLastError( ERROR_ACCESS_DENIED );
344 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
345 if (mkdir( full_name.long_name, 0777 ) == -1) {
346 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
347 /* the FILE_SetDosError() generated error codes don't match the
348 * CreateDirectory ones for some errnos */
350 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
351 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
352 default: FILE_SetDosError();break;
360 /***********************************************************************
361 * CreateDirectoryW (KERNEL32.42)
363 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
364 LPSECURITY_ATTRIBUTES lpsecattribs )
366 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
367 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
368 HeapFree( GetProcessHeap(), 0, xpath );
373 /***********************************************************************
374 * CreateDirectoryExA (KERNEL32.40)
376 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
377 LPSECURITY_ATTRIBUTES lpsecattribs)
379 return CreateDirectoryA(path,lpsecattribs);
383 /***********************************************************************
384 * CreateDirectoryExW (KERNEL32.41)
386 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
387 LPSECURITY_ATTRIBUTES lpsecattribs)
389 return CreateDirectoryW(path,lpsecattribs);
393 /***********************************************************************
394 * RemoveDirectory16 (KERNEL)
396 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
398 return (BOOL16)RemoveDirectoryA( path );
402 /***********************************************************************
403 * RemoveDirectoryA (KERNEL32.437)
405 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
407 DOS_FULL_NAME full_name;
409 TRACE_(file)("'%s'\n", path );
411 if (DOSFS_GetDevice( path ))
413 TRACE_(file)("cannot remove device '%s'!\n", path);
414 SetLastError( ERROR_FILE_NOT_FOUND );
417 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
418 if (rmdir( full_name.long_name ) == -1)
427 /***********************************************************************
428 * RemoveDirectoryW (KERNEL32.438)
430 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
432 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
433 BOOL ret = RemoveDirectoryA( xpath );
434 HeapFree( GetProcessHeap(), 0, xpath );
439 /***********************************************************************
442 * Helper function for DIR_SearchPath.
444 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
445 DOS_FULL_NAME *full_name )
447 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
448 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
450 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
451 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
453 SetLastError( ERROR_PATH_NOT_FOUND );
456 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
457 sizeof(full_name->long_name) - (p_l - full_name->long_name),
458 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
460 strcpy( full_name->long_name, dir->long_name );
462 strcpy( full_name->short_name, dir->short_name );
468 /***********************************************************************
469 * DIR_TryEnvironmentPath
471 * Helper function for DIR_SearchPath.
473 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
475 LPSTR path, next, buffer;
477 INT len = strlen(name);
478 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
480 if (!size) return FALSE;
481 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
482 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
487 while (*cur == ';') cur++;
489 next = strchr( cur, ';' );
490 if (next) *next++ = '\0';
491 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
493 strcpy( buffer, cur );
494 strcat( buffer, "\\" );
495 strcat( buffer, name );
496 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
497 HeapFree( GetProcessHeap(), 0, buffer );
501 HeapFree( GetProcessHeap(), 0, path );
506 /***********************************************************************
509 * Helper function for DIR_SearchPath.
511 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
513 PDB *pdb = PROCESS_Current();
515 /* FIXME: for now, GetModuleFileNameA can't return more */
516 /* than OFS_MAXPATHNAME. This may change with Win32. */
518 char buffer[OFS_MAXPATHNAME];
521 if (pdb->flags & PDB32_WIN16_PROC) {
522 if (!GetCurrentTask()) return FALSE;
523 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
526 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
529 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
530 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
532 return DOSFS_GetFullName( buffer, TRUE, full_name );
536 /***********************************************************************
539 * Implementation of SearchPathA. 'win32' specifies whether the search
540 * order is Win16 (module path last) or Win32 (module path first).
542 * FIXME: should return long path names.
544 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
545 DOS_FULL_NAME *full_name, BOOL win32 )
552 /* First check the supplied parameters */
554 p = strrchr( name, '.' );
555 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
556 ext = NULL; /* Ignore the specified extension */
557 if ((*name && (name[1] == ':')) ||
558 strchr( name, '/' ) || strchr( name, '\\' ))
559 path = NULL; /* Ignore path if name already contains a path */
560 if (path && !*path) path = NULL; /* Ignore empty path */
563 if (ext) len += strlen(ext);
564 if (path) len += strlen(path) + 1;
566 /* Allocate a buffer for the file name and extension */
570 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
572 SetLastError( ERROR_OUTOFMEMORY );
581 else strcpy( tmp, name );
582 if (ext) strcat( tmp, ext );
586 /* If we have an explicit path, everything's easy */
588 if (path || (*name && (name[1] == ':')) ||
589 strchr( name, '/' ) || strchr( name, '\\' ))
591 ret = DOSFS_GetFullName( name, TRUE, full_name );
595 /* Try the path of the current executable (for Win32 search order) */
597 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
599 /* Try the current directory */
601 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
603 /* Try the Windows system directory */
605 if (DIR_TryPath( &DIR_System, name, full_name ))
608 /* Try the Windows directory */
610 if (DIR_TryPath( &DIR_Windows, name, full_name ))
613 /* Try the path of the current executable (for Win16 search order) */
615 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
617 /* Try all directories in path */
619 ret = DIR_TryEnvironmentPath( name, full_name );
622 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
627 /***********************************************************************
628 * SearchPathA [KERNEL32.447]
630 * Searches for a specified file in the search path.
633 * path [I] Path to search
634 * name [I] Filename to search for.
635 * ext [I] File extension to append to file name. The first
636 * character must be a period. This parameter is
637 * specified only if the filename given does not
638 * contain an extension.
639 * buflen [I] size of buffer, in characters
640 * buffer [O] buffer for found filename
641 * lastpart [O] address of pointer to last used character in
642 * buffer (the final '\')
645 * Success: length of string copied into buffer, not including
646 * terminating null character. If the filename found is
647 * longer than the length of the buffer, the length of the
648 * filename is returned.
652 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
655 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
656 LPSTR buffer, LPSTR *lastpart )
659 DOS_FULL_NAME full_name;
661 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
663 SetLastError(ERROR_FILE_NOT_FOUND);
666 lstrcpynA( buffer, full_name.short_name, buflen );
667 res = full_name.long_name +
668 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
669 while (*res == '/') res++;
672 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
673 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
674 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
676 TRACE("Returning %d\n", strlen(res) + 3 );
677 return strlen(res) + 3;
681 /***********************************************************************
682 * SearchPathW (KERNEL32.448)
684 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
685 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
689 DOS_FULL_NAME full_name;
691 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
692 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
693 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
694 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
695 HeapFree( GetProcessHeap(), 0, extA );
696 HeapFree( GetProcessHeap(), 0, nameA );
697 HeapFree( GetProcessHeap(), 0, pathA );
700 lstrcpynAtoW( buffer, full_name.short_name, buflen );
701 res = full_name.long_name +
702 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
703 while (*res == '/') res++;
706 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
707 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
710 for (p = *lastpart = buffer; *p; p++)
711 if (*p == '\\') *lastpart = p + 1;
714 return strlen(res) + 3;