2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
12 #include <sys/types.h>
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
21 #include "wine/winbase16.h"
22 #include "wine/winestring.h"
23 #include "wine/winuser16.h"
31 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(dosfs
)
34 DECLARE_DEBUG_CHANNEL(file
)
36 static DOS_FULL_NAME DIR_Windows
;
37 static DOS_FULL_NAME DIR_System
;
40 /***********************************************************************
43 * Get a path name from the wine.ini file and make sure it is valid.
45 static int DIR_GetPath( const char *keyname
, const char *defval
,
46 DOS_FULL_NAME
*full_name
)
48 char path
[MAX_PATHNAME_LEN
];
49 BY_HANDLE_FILE_INFORMATION info
;
51 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
52 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
53 !FILE_Stat( full_name
->long_name
, &info
) ||
54 !(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
56 MESSAGE("Invalid path '%s' for %s directory\n", path
, keyname
);
63 /***********************************************************************
68 char path
[MAX_PATHNAME_LEN
];
69 DOS_FULL_NAME tmp_dir
;
73 if (!getcwd( path
, MAX_PATHNAME_LEN
))
75 perror( "Could not get current directory" );
79 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
81 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
82 "for current working directory %s; "
83 "starting in windows directory.\n", cwd
);
87 DRIVE_SetCurrentDrive( drive
);
88 DRIVE_Chdir( drive
, cwd
);
91 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
92 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
93 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
95 PROFILE_UsageWineIni();
98 if (-1 == access( tmp_dir
.long_name
, W_OK
))
102 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
103 PROFILE_UsageWineIni();
106 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
112 drive
= DIR_Windows
.drive
;
113 DRIVE_SetCurrentDrive( drive
);
114 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
117 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
118 path
, sizeof(path
) );
119 if (strchr(path
, '/'))
121 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
122 PROFILE_UsageWineIni();
126 /* Set the environment variables */
128 SetEnvironmentVariableA( "PATH", path
);
129 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
130 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
131 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
132 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
134 TRACE("WindowsDir = %s (%s)\n",
135 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
136 TRACE("SystemDir = %s (%s)\n",
137 DIR_System
.short_name
, DIR_System
.long_name
);
138 TRACE("TempDir = %s (%s)\n",
139 tmp_dir
.short_name
, tmp_dir
.long_name
);
140 TRACE("Path = %s\n", path
);
141 TRACE("Cwd = %c:\\%s\n",
142 'A' + drive
, DRIVE_GetDosCwd( drive
) );
148 /***********************************************************************
149 * GetTempPath32A (KERNEL32.292)
151 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
154 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
155 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
156 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
158 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
167 /***********************************************************************
168 * GetTempPath32W (KERNEL32.293)
170 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
172 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
173 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
175 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
176 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
177 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
179 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
188 /***********************************************************************
189 * DIR_GetWindowsUnixDir
191 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
193 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
194 return strlen( DIR_Windows
.long_name
);
198 /***********************************************************************
199 * DIR_GetSystemUnixDir
201 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
203 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
204 return strlen( DIR_System
.long_name
);
208 /***********************************************************************
209 * GetTempDrive (KERNEL.92)
211 BYTE WINAPI
GetTempDrive( BYTE ignored
)
215 UINT len
= GetTempPathA( 0, NULL
);
217 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
218 return DRIVE_GetCurrentDrive() + 'A';
220 /* FIXME: apparently Windows does something with the ignored byte */
221 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
223 HeapFree( GetProcessHeap(), 0, buffer
);
228 UINT WINAPI
WIN16_GetTempDrive( BYTE ignored
)
230 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
234 * AH: ':' - yes, some kernel code even does stosw with
238 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
242 /***********************************************************************
243 * GetWindowsDirectory16 (KERNEL.134)
245 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
247 return (UINT16
)GetWindowsDirectoryA( path
, count
);
251 /***********************************************************************
252 * GetWindowsDirectory32A (KERNEL32.311)
254 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
256 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
257 return strlen( DIR_Windows
.short_name
);
261 /***********************************************************************
262 * GetWindowsDirectory32W (KERNEL32.312)
264 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
266 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
267 return strlen( DIR_Windows
.short_name
);
271 /***********************************************************************
272 * GetSystemDirectory16 (KERNEL.135)
274 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
276 return (UINT16
)GetSystemDirectoryA( path
, count
);
280 /***********************************************************************
281 * GetSystemDirectory32A (KERNEL32.282)
283 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
285 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
286 return strlen( DIR_System
.short_name
);
290 /***********************************************************************
291 * GetSystemDirectory32W (KERNEL32.283)
293 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
295 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
296 return strlen( DIR_System
.short_name
);
300 /***********************************************************************
301 * CreateDirectory16 (KERNEL.144)
303 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
305 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
306 return (BOOL16
)CreateDirectoryA( path
, NULL
);
310 /***********************************************************************
311 * CreateDirectory32A (KERNEL32.39)
315 * ERROR_DISK_FULL: on full disk
316 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
317 * ERROR_ACCESS_DENIED: on permission problems
318 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
320 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
321 LPSECURITY_ATTRIBUTES lpsecattribs
)
323 DOS_FULL_NAME full_name
;
325 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
326 if (DOSFS_GetDevice( path
))
328 TRACE_(file
)("cannot use device '%s'!\n",path
);
329 SetLastError( ERROR_ACCESS_DENIED
);
332 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
333 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
334 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
335 /* the FILE_SetDosError() generated error codes don't match the
336 * CreateDirectory ones for some errnos */
338 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
339 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
340 default: FILE_SetDosError();break;
348 /***********************************************************************
349 * CreateDirectory32W (KERNEL32.42)
351 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
352 LPSECURITY_ATTRIBUTES lpsecattribs
)
354 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
355 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
356 HeapFree( GetProcessHeap(), 0, xpath
);
361 /***********************************************************************
362 * CreateDirectoryEx32A (KERNEL32.40)
364 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
365 LPSECURITY_ATTRIBUTES lpsecattribs
)
367 return CreateDirectoryA(path
,lpsecattribs
);
371 /***********************************************************************
372 * CreateDirectoryEx32W (KERNEL32.41)
374 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
375 LPSECURITY_ATTRIBUTES lpsecattribs
)
377 return CreateDirectoryW(path
,lpsecattribs
);
381 /***********************************************************************
382 * RemoveDirectory16 (KERNEL)
384 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
386 return (BOOL16
)RemoveDirectoryA( path
);
390 /***********************************************************************
391 * RemoveDirectory32A (KERNEL32.437)
393 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
395 DOS_FULL_NAME full_name
;
397 TRACE_(file
)("'%s'\n", path
);
399 if (DOSFS_GetDevice( path
))
401 TRACE_(file
)("cannot remove device '%s'!\n", path
);
402 SetLastError( ERROR_FILE_NOT_FOUND
);
405 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
406 if (rmdir( full_name
.long_name
) == -1)
415 /***********************************************************************
416 * RemoveDirectory32W (KERNEL32.438)
418 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
420 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
421 BOOL ret
= RemoveDirectoryA( xpath
);
422 HeapFree( GetProcessHeap(), 0, xpath
);
427 /***********************************************************************
430 * Helper function for DIR_SearchPath.
432 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
433 DOS_FULL_NAME
*full_name
)
435 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
436 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
438 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
439 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
441 SetLastError( ERROR_PATH_NOT_FOUND
);
444 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
445 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
446 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
448 strcpy( full_name
->long_name
, dir
->long_name
);
450 strcpy( full_name
->short_name
, dir
->short_name
);
456 /***********************************************************************
457 * DIR_TryEnvironmentPath
459 * Helper function for DIR_SearchPath.
461 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
463 LPSTR path
, next
, buffer
;
465 INT len
= strlen(name
);
466 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
468 if (!size
) return FALSE
;
469 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
470 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
475 while (*cur
== ';') cur
++;
477 next
= strchr( cur
, ';' );
478 if (next
) *next
++ = '\0';
479 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
481 strcpy( buffer
, cur
);
482 strcat( buffer
, "\\" );
483 strcat( buffer
, name
);
484 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
485 HeapFree( GetProcessHeap(), 0, buffer
);
489 HeapFree( GetProcessHeap(), 0, path
);
494 /***********************************************************************
497 * Helper function for DIR_SearchPath.
499 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
501 PDB
*pdb
= PROCESS_Current();
503 /* FIXME: for now, GetModuleFileName32A can't return more */
504 /* than OFS_MAXPATHNAME. This may change with Win32. */
506 char buffer
[OFS_MAXPATHNAME
];
509 if (pdb
->flags
& PDB32_WIN16_PROC
) {
510 if (!GetCurrentTask()) return FALSE
;
511 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
514 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
517 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
518 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
520 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
524 /***********************************************************************
527 * Implementation of SearchPath32A. 'win32' specifies whether the search
528 * order is Win16 (module path last) or Win32 (module path first).
530 * FIXME: should return long path names.
532 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
533 DOS_FULL_NAME
*full_name
, BOOL win32
)
540 /* First check the supplied parameters */
542 p
= strrchr( name
, '.' );
543 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
544 ext
= NULL
; /* Ignore the specified extension */
545 if ((*name
&& (name
[1] == ':')) ||
546 strchr( name
, '/' ) || strchr( name
, '\\' ))
547 path
= NULL
; /* Ignore path if name already contains a path */
548 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
551 if (ext
) len
+= strlen(ext
);
552 if (path
) len
+= strlen(path
) + 1;
554 /* Allocate a buffer for the file name and extension */
558 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
560 SetLastError( ERROR_OUTOFMEMORY
);
569 else strcpy( tmp
, name
);
570 if (ext
) strcat( tmp
, ext
);
574 /* If we have an explicit path, everything's easy */
576 if (path
|| (*name
&& (name
[1] == ':')) ||
577 strchr( name
, '/' ) || strchr( name
, '\\' ))
579 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
583 /* Try the path of the current executable (for Win32 search order) */
585 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
587 /* Try the current directory */
589 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
591 /* Try the Windows system directory */
593 if (DIR_TryPath( &DIR_System
, name
, full_name
))
596 /* Try the Windows directory */
598 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
601 /* Try the path of the current executable (for Win16 search order) */
603 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
605 /* Try all directories in path */
607 ret
= DIR_TryEnvironmentPath( name
, full_name
);
610 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
615 /***********************************************************************
616 * SearchPath32A [KERNEL32.447]
618 * Searches for a specified file in the search path.
621 * path [I] Path to search
622 * name [I] Filename to search for.
623 * ext [I] File extension to append to file name. The first
624 * character must be a period. This parameter is
625 * specified only if the filename given does not
626 * contain an extension.
627 * buflen [I] size of buffer, in characters
628 * buffer [O] buffer for found filename
629 * lastpart [O] address of pointer to last used character in
630 * buffer (the final '\')
633 * Success: length of string copied into buffer, not including
634 * terminating null character. If the filename found is
635 * longer than the length of the buffer, the length of the
636 * filename is returned.
640 * Should call SetLastError(but currently doesn't).
642 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
643 LPSTR buffer
, LPSTR
*lastpart
)
646 DOS_FULL_NAME full_name
;
648 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
649 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
650 res
= full_name
.long_name
+
651 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
652 while (*res
== '/') res
++;
655 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
656 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
657 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
659 TRACE("Returning %d\n", strlen(res
) + 3 );
660 return strlen(res
) + 3;
664 /***********************************************************************
665 * SearchPath32W (KERNEL32.448)
667 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
668 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
672 DOS_FULL_NAME full_name
;
674 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
675 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
676 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
677 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
678 HeapFree( GetProcessHeap(), 0, extA
);
679 HeapFree( GetProcessHeap(), 0, nameA
);
680 HeapFree( GetProcessHeap(), 0, pathA
);
683 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
684 res
= full_name
.long_name
+
685 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
686 while (*res
== '/') res
++;
689 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
690 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
693 for (p
= *lastpart
= buffer
; *p
; p
++)
694 if (*p
== '\\') *lastpart
= p
+ 1;
697 return strlen(res
) + 3;