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 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
288 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
290 return GetWindowsDirectoryA( path
, count
);
294 /***********************************************************************
295 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
297 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
299 return GetWindowsDirectoryW( path
, count
);
303 /***********************************************************************
304 * GetSystemDirectory16 (KERNEL.135)
306 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
308 return (UINT16
)GetSystemDirectoryA( path
, count
);
312 /***********************************************************************
313 * GetSystemDirectoryA (KERNEL32.282)
315 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
317 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
318 return strlen( DIR_System
.short_name
);
322 /***********************************************************************
323 * GetSystemDirectoryW (KERNEL32.283)
325 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
327 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
328 return strlen( DIR_System
.short_name
);
332 /***********************************************************************
333 * CreateDirectory16 (KERNEL.144)
335 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
337 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
338 return (BOOL16
)CreateDirectoryA( path
, NULL
);
342 /***********************************************************************
343 * CreateDirectoryA (KERNEL32.39)
347 * ERROR_DISK_FULL: on full disk
348 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
349 * ERROR_ACCESS_DENIED: on permission problems
350 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
352 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
353 LPSECURITY_ATTRIBUTES lpsecattribs
)
355 DOS_FULL_NAME full_name
;
357 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
358 if (DOSFS_GetDevice( path
))
360 TRACE_(file
)("cannot use device '%s'!\n",path
);
361 SetLastError( ERROR_ACCESS_DENIED
);
364 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
365 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
366 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
367 /* the FILE_SetDosError() generated error codes don't match the
368 * CreateDirectory ones for some errnos */
370 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
371 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
372 default: FILE_SetDosError();break;
380 /***********************************************************************
381 * CreateDirectoryW (KERNEL32.42)
383 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
384 LPSECURITY_ATTRIBUTES lpsecattribs
)
386 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
387 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
388 HeapFree( GetProcessHeap(), 0, xpath
);
393 /***********************************************************************
394 * CreateDirectoryExA (KERNEL32.40)
396 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
397 LPSECURITY_ATTRIBUTES lpsecattribs
)
399 return CreateDirectoryA(path
,lpsecattribs
);
403 /***********************************************************************
404 * CreateDirectoryExW (KERNEL32.41)
406 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
407 LPSECURITY_ATTRIBUTES lpsecattribs
)
409 return CreateDirectoryW(path
,lpsecattribs
);
413 /***********************************************************************
414 * RemoveDirectory16 (KERNEL)
416 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
418 return (BOOL16
)RemoveDirectoryA( path
);
422 /***********************************************************************
423 * RemoveDirectoryA (KERNEL32.437)
425 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
427 DOS_FULL_NAME full_name
;
429 TRACE_(file
)("'%s'\n", path
);
431 if (DOSFS_GetDevice( path
))
433 TRACE_(file
)("cannot remove device '%s'!\n", path
);
434 SetLastError( ERROR_FILE_NOT_FOUND
);
437 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
438 if (rmdir( full_name
.long_name
) == -1)
447 /***********************************************************************
448 * RemoveDirectoryW (KERNEL32.438)
450 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
452 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
453 BOOL ret
= RemoveDirectoryA( xpath
);
454 HeapFree( GetProcessHeap(), 0, xpath
);
459 /***********************************************************************
462 * Helper function for DIR_SearchPath.
464 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
465 DOS_FULL_NAME
*full_name
)
467 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
468 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
470 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
471 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
473 SetLastError( ERROR_PATH_NOT_FOUND
);
476 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
477 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
478 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
480 strcpy( full_name
->long_name
, dir
->long_name
);
482 strcpy( full_name
->short_name
, dir
->short_name
);
488 /***********************************************************************
489 * DIR_TryEnvironmentPath
491 * Helper function for DIR_SearchPath.
492 * Search in the specified path, or in $PATH if NULL.
494 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
, LPCSTR envpath
)
496 LPSTR path
, next
, buffer
;
498 INT len
= strlen(name
);
501 size
= envpath
? strlen(envpath
)+1 : GetEnvironmentVariableA( "PATH", NULL
, 0 );
502 if (!size
) return FALSE
;
503 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
504 if (envpath
) strcpy( path
, envpath
);
505 else if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
510 while (*cur
== ';') cur
++;
512 next
= strchr( cur
, ';' );
513 if (next
) *next
++ = '\0';
514 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
516 strcpy( buffer
, cur
);
517 strcat( buffer
, "\\" );
518 strcat( buffer
, name
);
519 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
520 HeapFree( GetProcessHeap(), 0, buffer
);
524 HeapFree( GetProcessHeap(), 0, path
);
529 /***********************************************************************
532 * Helper function for DIR_SearchPath.
534 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
536 /* FIXME: for now, GetModuleFileNameA can't return more */
537 /* than OFS_MAXPATHNAME. This may change with Win32. */
539 char buffer
[OFS_MAXPATHNAME
];
544 if (!GetCurrentTask()) return FALSE
;
545 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
548 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
551 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
552 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
554 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
558 /***********************************************************************
561 * Implementation of SearchPathA. 'win32' specifies whether the search
562 * order is Win16 (module path last) or Win32 (module path first).
564 * FIXME: should return long path names.
566 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
567 DOS_FULL_NAME
*full_name
, BOOL win32
)
573 /* First check the supplied parameters */
575 p
= strrchr( name
, '.' );
576 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
577 ext
= NULL
; /* Ignore the specified extension */
578 if ((*name
&& (name
[1] == ':')) ||
579 strchr( name
, '/' ) || strchr( name
, '\\' ))
580 path
= NULL
; /* Ignore path if name already contains a path */
581 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
583 /* Allocate a buffer for the file name and extension */
587 DWORD len
= strlen(name
) + strlen(ext
);
588 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
590 SetLastError( ERROR_OUTOFMEMORY
);
598 /* If the name contains an explicit path, everything's easy */
600 if ((*name
&& (name
[1] == ':')) || strchr( name
, '/' ) || strchr( name
, '\\' ))
602 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
606 /* Search in the specified path */
610 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
614 /* Try the path of the current executable (for Win32 search order) */
616 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
618 /* Try the current directory */
620 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
622 /* Try the Windows system directory */
624 if (DIR_TryPath( &DIR_System
, name
, full_name
))
627 /* Try the Windows directory */
629 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
632 /* Try the path of the current executable (for Win16 search order) */
634 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
636 /* Try all directories in path */
638 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
641 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
646 /***********************************************************************
647 * SearchPathA [KERNEL32.447]
649 * Searches for a specified file in the search path.
652 * path [I] Path to search
653 * name [I] Filename to search for.
654 * ext [I] File extension to append to file name. The first
655 * character must be a period. This parameter is
656 * specified only if the filename given does not
657 * contain an extension.
658 * buflen [I] size of buffer, in characters
659 * buffer [O] buffer for found filename
660 * lastpart [O] address of pointer to last used character in
661 * buffer (the final '\')
664 * Success: length of string copied into buffer, not including
665 * terminating null character. If the filename found is
666 * longer than the length of the buffer, the length of the
667 * filename is returned.
671 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
674 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
675 LPSTR buffer
, LPSTR
*lastpart
)
678 DOS_FULL_NAME full_name
;
680 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
682 SetLastError(ERROR_FILE_NOT_FOUND
);
685 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
686 res
= full_name
.long_name
+
687 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
688 while (*res
== '/') res
++;
691 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
692 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
693 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
695 TRACE("Returning %d\n", strlen(res
) + 3 );
696 return strlen(res
) + 3;
700 /***********************************************************************
701 * SearchPathW (KERNEL32.448)
703 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
704 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
708 DOS_FULL_NAME full_name
;
710 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
711 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
712 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
713 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
714 HeapFree( GetProcessHeap(), 0, extA
);
715 HeapFree( GetProcessHeap(), 0, nameA
);
716 HeapFree( GetProcessHeap(), 0, pathA
);
719 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
720 res
= full_name
.long_name
+
721 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
722 while (*res
== '/') res
++;
725 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
726 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
729 for (p
= *lastpart
= buffer
; *p
; p
++)
730 if (*p
== '\\') *lastpart
= p
+ 1;
733 return strlen(res
) + 3;