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"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(dosfs);
36 DECLARE_DEBUG_CHANNEL(file);
38 static DOS_FULL_NAME DIR_Windows;
39 static DOS_FULL_NAME DIR_System;
42 /***********************************************************************
45 * Get a path name from the wine.ini file and make sure it is valid.
47 static int DIR_GetPath( const char *keyname, const char *defval,
48 DOS_FULL_NAME *full_name )
50 char path[MAX_PATHNAME_LEN];
51 BY_HANDLE_FILE_INFORMATION info;
53 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
54 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
55 !FILE_Stat( full_name->long_name, &info ) ||
56 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
58 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
65 /***********************************************************************
70 char path[MAX_PATHNAME_LEN];
71 DOS_FULL_NAME tmp_dir, profile_dir;
75 if (!getcwd( path, MAX_PATHNAME_LEN ))
77 perror( "Could not get current directory" );
81 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
83 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
84 "for current working directory %s; "
85 "starting in windows directory.\n", cwd );
89 DRIVE_SetCurrentDrive( drive );
90 DRIVE_Chdir( drive, cwd );
93 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
94 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
95 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
97 PROFILE_UsageWineIni();
100 if (-1 == access( tmp_dir.long_name, W_OK ))
104 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
105 PROFILE_UsageWineIni();
108 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
114 drive = DIR_Windows.drive;
115 DRIVE_SetCurrentDrive( drive );
116 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
119 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
120 path, sizeof(path) );
121 if (strchr(path, '/'))
123 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
124 PROFILE_UsageWineIni();
128 /* Set the environment variables */
130 SetEnvironmentVariableA( "PATH", path );
131 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
132 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
133 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
135 /* set COMSPEC only if it doesn't exist already */
136 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
137 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
139 TRACE("WindowsDir = %s (%s)\n",
140 DIR_Windows.short_name, DIR_Windows.long_name );
141 TRACE("SystemDir = %s (%s)\n",
142 DIR_System.short_name, DIR_System.long_name );
143 TRACE("TempDir = %s (%s)\n",
144 tmp_dir.short_name, tmp_dir.long_name );
145 TRACE("Path = %s\n", path );
146 TRACE("Cwd = %c:\\%s\n",
147 'A' + drive, DRIVE_GetDosCwd( drive ) );
149 if (DIR_GetPath( "profile", "", &profile_dir ))
151 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
152 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
155 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
156 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
162 /***********************************************************************
163 * GetTempPathA (KERNEL32.292)
165 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
168 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
169 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
170 if (!(ret = GetCurrentDirectoryA( count, path )))
172 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
181 /***********************************************************************
182 * GetTempPathW (KERNEL32.293)
184 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
186 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
187 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
189 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
190 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
191 if (!(ret = GetCurrentDirectoryW( count, path )))
193 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
202 /***********************************************************************
203 * DIR_GetWindowsUnixDir
205 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
207 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
208 return strlen( DIR_Windows.long_name );
212 /***********************************************************************
213 * DIR_GetSystemUnixDir
215 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
217 if (path) lstrcpynA( path, DIR_System.long_name, count );
218 return strlen( DIR_System.long_name );
222 /***********************************************************************
223 * GetTempDrive (KERNEL.92)
225 BYTE WINAPI GetTempDrive( BYTE ignored )
229 UINT len = GetTempPathA( 0, NULL );
231 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
232 return DRIVE_GetCurrentDrive() + 'A';
234 /* FIXME: apparently Windows does something with the ignored byte */
235 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
237 HeapFree( GetProcessHeap(), 0, buffer );
242 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
244 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
248 * AH: ':' - yes, some kernel code even does stosw with
252 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
256 /***********************************************************************
257 * GetWindowsDirectory16 (KERNEL.134)
259 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
261 return (UINT16)GetWindowsDirectoryA( path, count );
265 /***********************************************************************
266 * GetWindowsDirectoryA (KERNEL32.311)
268 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
270 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
271 return strlen( DIR_Windows.short_name );
275 /***********************************************************************
276 * GetWindowsDirectoryW (KERNEL32.312)
278 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
280 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
281 return strlen( DIR_Windows.short_name );
285 /***********************************************************************
286 * GetSystemDirectory16 (KERNEL.135)
288 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
290 return (UINT16)GetSystemDirectoryA( path, count );
294 /***********************************************************************
295 * GetSystemDirectoryA (KERNEL32.282)
297 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
299 if (path) lstrcpynA( path, DIR_System.short_name, count );
300 return strlen( DIR_System.short_name );
304 /***********************************************************************
305 * GetSystemDirectoryW (KERNEL32.283)
307 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
309 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
310 return strlen( DIR_System.short_name );
314 /***********************************************************************
315 * CreateDirectory16 (KERNEL.144)
317 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
319 TRACE_(file)("(%s,%p)\n", path, dummy );
320 return (BOOL16)CreateDirectoryA( path, NULL );
324 /***********************************************************************
325 * CreateDirectoryA (KERNEL32.39)
329 * ERROR_DISK_FULL: on full disk
330 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
331 * ERROR_ACCESS_DENIED: on permission problems
332 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
334 BOOL WINAPI CreateDirectoryA( LPCSTR path,
335 LPSECURITY_ATTRIBUTES lpsecattribs )
337 DOS_FULL_NAME full_name;
339 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
340 if (DOSFS_GetDevice( path ))
342 TRACE_(file)("cannot use device '%s'!\n",path);
343 SetLastError( ERROR_ACCESS_DENIED );
346 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
347 if (mkdir( full_name.long_name, 0777 ) == -1) {
348 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
349 /* the FILE_SetDosError() generated error codes don't match the
350 * CreateDirectory ones for some errnos */
352 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
353 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
354 default: FILE_SetDosError();break;
362 /***********************************************************************
363 * CreateDirectoryW (KERNEL32.42)
365 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
366 LPSECURITY_ATTRIBUTES lpsecattribs )
368 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
369 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
370 HeapFree( GetProcessHeap(), 0, xpath );
375 /***********************************************************************
376 * CreateDirectoryExA (KERNEL32.40)
378 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
379 LPSECURITY_ATTRIBUTES lpsecattribs)
381 return CreateDirectoryA(path,lpsecattribs);
385 /***********************************************************************
386 * CreateDirectoryExW (KERNEL32.41)
388 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
389 LPSECURITY_ATTRIBUTES lpsecattribs)
391 return CreateDirectoryW(path,lpsecattribs);
395 /***********************************************************************
396 * RemoveDirectory16 (KERNEL)
398 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
400 return (BOOL16)RemoveDirectoryA( path );
404 /***********************************************************************
405 * RemoveDirectoryA (KERNEL32.437)
407 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
409 DOS_FULL_NAME full_name;
411 TRACE_(file)("'%s'\n", path );
413 if (DOSFS_GetDevice( path ))
415 TRACE_(file)("cannot remove device '%s'!\n", path);
416 SetLastError( ERROR_FILE_NOT_FOUND );
419 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
420 if (rmdir( full_name.long_name ) == -1)
429 /***********************************************************************
430 * RemoveDirectoryW (KERNEL32.438)
432 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
434 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
435 BOOL ret = RemoveDirectoryA( xpath );
436 HeapFree( GetProcessHeap(), 0, xpath );
441 /***********************************************************************
444 * Helper function for DIR_SearchPath.
446 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
447 DOS_FULL_NAME *full_name )
449 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
450 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
452 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
453 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
455 SetLastError( ERROR_PATH_NOT_FOUND );
458 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
459 sizeof(full_name->long_name) - (p_l - full_name->long_name),
460 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
462 strcpy( full_name->long_name, dir->long_name );
464 strcpy( full_name->short_name, dir->short_name );
470 /***********************************************************************
471 * DIR_TryEnvironmentPath
473 * Helper function for DIR_SearchPath.
475 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
477 LPSTR path, next, buffer;
479 INT len = strlen(name);
480 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
482 if (!size) return FALSE;
483 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
484 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
489 while (*cur == ';') cur++;
491 next = strchr( cur, ';' );
492 if (next) *next++ = '\0';
493 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
495 strcpy( buffer, cur );
496 strcat( buffer, "\\" );
497 strcat( buffer, name );
498 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
499 HeapFree( GetProcessHeap(), 0, buffer );
503 HeapFree( GetProcessHeap(), 0, path );
508 /***********************************************************************
511 * Helper function for DIR_SearchPath.
513 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
515 /* FIXME: for now, GetModuleFileNameA can't return more */
516 /* than OFS_MAXPATHNAME. This may change with Win32. */
518 char buffer[OFS_MAXPATHNAME];
523 if (!GetCurrentTask()) return FALSE;
524 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
527 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
530 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
531 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
533 return DOSFS_GetFullName( buffer, TRUE, full_name );
537 /***********************************************************************
540 * Implementation of SearchPathA. 'win32' specifies whether the search
541 * order is Win16 (module path last) or Win32 (module path first).
543 * FIXME: should return long path names.
545 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
546 DOS_FULL_NAME *full_name, BOOL win32 )
553 /* First check the supplied parameters */
555 p = strrchr( name, '.' );
556 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
557 ext = NULL; /* Ignore the specified extension */
558 if ((*name && (name[1] == ':')) ||
559 strchr( name, '/' ) || strchr( name, '\\' ))
560 path = NULL; /* Ignore path if name already contains a path */
561 if (path && !*path) path = NULL; /* Ignore empty path */
564 if (ext) len += strlen(ext);
565 if (path) len += strlen(path) + 1;
567 /* Allocate a buffer for the file name and extension */
571 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
573 SetLastError( ERROR_OUTOFMEMORY );
582 else strcpy( tmp, name );
583 if (ext) strcat( tmp, ext );
587 /* If we have an explicit path, everything's easy */
589 if (path || (*name && (name[1] == ':')) ||
590 strchr( name, '/' ) || strchr( name, '\\' ))
592 ret = DOSFS_GetFullName( name, TRUE, full_name );
596 /* Try the path of the current executable (for Win32 search order) */
598 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
600 /* Try the current directory */
602 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
604 /* Try the Windows system directory */
606 if (DIR_TryPath( &DIR_System, name, full_name ))
609 /* Try the Windows directory */
611 if (DIR_TryPath( &DIR_Windows, name, full_name ))
614 /* Try the path of the current executable (for Win16 search order) */
616 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
618 /* Try all directories in path */
620 ret = DIR_TryEnvironmentPath( name, full_name );
623 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
628 /***********************************************************************
629 * SearchPathA [KERNEL32.447]
631 * Searches for a specified file in the search path.
634 * path [I] Path to search
635 * name [I] Filename to search for.
636 * ext [I] File extension to append to file name. The first
637 * character must be a period. This parameter is
638 * specified only if the filename given does not
639 * contain an extension.
640 * buflen [I] size of buffer, in characters
641 * buffer [O] buffer for found filename
642 * lastpart [O] address of pointer to last used character in
643 * buffer (the final '\')
646 * Success: length of string copied into buffer, not including
647 * terminating null character. If the filename found is
648 * longer than the length of the buffer, the length of the
649 * filename is returned.
653 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
656 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
657 LPSTR buffer, LPSTR *lastpart )
660 DOS_FULL_NAME full_name;
662 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
664 SetLastError(ERROR_FILE_NOT_FOUND);
667 lstrcpynA( buffer, full_name.short_name, buflen );
668 res = full_name.long_name +
669 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
670 while (*res == '/') res++;
673 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
674 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
675 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
677 TRACE("Returning %d\n", strlen(res) + 3 );
678 return strlen(res) + 3;
682 /***********************************************************************
683 * SearchPathW (KERNEL32.448)
685 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
686 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
690 DOS_FULL_NAME full_name;
692 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
693 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
694 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
695 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
696 HeapFree( GetProcessHeap(), 0, extA );
697 HeapFree( GetProcessHeap(), 0, nameA );
698 HeapFree( GetProcessHeap(), 0, pathA );
701 lstrcpynAtoW( buffer, full_name.short_name, buflen );
702 res = full_name.long_name +
703 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
704 while (*res == '/') res++;
707 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
708 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
711 for (p = *lastpart = buffer; *p; p++)
712 if (*p == '\\') *lastpart = p + 1;
715 return strlen(res) + 3;