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
);
92 chdir("/"); /* every other directory operations are done by WINE */
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( "TEMP", tmp_dir
.short_name
);
133 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
134 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
136 /* set COMSPEC only if it doesn't exist already */
137 if (!GetEnvironmentVariableA( "COMSPEC", NULL
, 0 ))
138 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
140 TRACE("WindowsDir = %s (%s)\n",
141 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
142 TRACE("SystemDir = %s (%s)\n",
143 DIR_System
.short_name
, DIR_System
.long_name
);
144 TRACE("TempDir = %s (%s)\n",
145 tmp_dir
.short_name
, tmp_dir
.long_name
);
146 TRACE("Path = %s\n", path
);
147 TRACE("Cwd = %c:\\%s\n",
148 'A' + drive
, DRIVE_GetDosCwd( drive
) );
150 if (DIR_GetPath( "profile", "", &profile_dir
))
152 TRACE("USERPROFILE= %s\n", profile_dir
.short_name
);
153 SetEnvironmentVariableA( "USERPROFILE", profile_dir
.short_name
);
156 TRACE("SYSTEMROOT = %s\n", DIR_Windows
.short_name
);
157 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows
.short_name
);
163 /***********************************************************************
164 * GetTempPathA (KERNEL32.292)
166 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
169 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
170 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
171 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
173 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
182 /***********************************************************************
183 * GetTempPathW (KERNEL32.293)
185 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
187 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
188 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
190 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
191 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
192 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
194 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
203 /***********************************************************************
204 * DIR_GetWindowsUnixDir
206 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
208 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
209 return strlen( DIR_Windows
.long_name
);
213 /***********************************************************************
214 * DIR_GetSystemUnixDir
216 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
218 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
219 return strlen( DIR_System
.long_name
);
223 /***********************************************************************
224 * GetTempDrive (KERNEL.92)
226 BYTE WINAPI
GetTempDrive( BYTE ignored
)
230 UINT len
= GetTempPathA( 0, NULL
);
232 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
233 return DRIVE_GetCurrentDrive() + 'A';
235 /* FIXME: apparently Windows does something with the ignored byte */
236 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
238 HeapFree( GetProcessHeap(), 0, buffer
);
243 UINT WINAPI
WIN16_GetTempDrive( BYTE ignored
)
245 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
249 * AH: ':' - yes, some kernel code even does stosw with
253 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
257 /***********************************************************************
258 * GetWindowsDirectory16 (KERNEL.134)
260 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
262 return (UINT16
)GetWindowsDirectoryA( path
, count
);
266 /***********************************************************************
267 * GetWindowsDirectoryA (KERNEL32.311)
269 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
271 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
272 return strlen( DIR_Windows
.short_name
);
276 /***********************************************************************
277 * GetWindowsDirectoryW (KERNEL32.312)
279 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
281 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
282 return strlen( DIR_Windows
.short_name
);
286 /***********************************************************************
287 * GetSystemDirectory16 (KERNEL.135)
289 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
291 return (UINT16
)GetSystemDirectoryA( path
, count
);
295 /***********************************************************************
296 * GetSystemDirectoryA (KERNEL32.282)
298 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
300 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
301 return strlen( DIR_System
.short_name
);
305 /***********************************************************************
306 * GetSystemDirectoryW (KERNEL32.283)
308 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
310 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
311 return strlen( DIR_System
.short_name
);
315 /***********************************************************************
316 * CreateDirectory16 (KERNEL.144)
318 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
320 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
321 return (BOOL16
)CreateDirectoryA( path
, NULL
);
325 /***********************************************************************
326 * CreateDirectoryA (KERNEL32.39)
330 * ERROR_DISK_FULL: on full disk
331 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
332 * ERROR_ACCESS_DENIED: on permission problems
333 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
335 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
336 LPSECURITY_ATTRIBUTES lpsecattribs
)
338 DOS_FULL_NAME full_name
;
340 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
341 if (DOSFS_GetDevice( path
))
343 TRACE_(file
)("cannot use device '%s'!\n",path
);
344 SetLastError( ERROR_ACCESS_DENIED
);
347 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
348 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
349 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
350 /* the FILE_SetDosError() generated error codes don't match the
351 * CreateDirectory ones for some errnos */
353 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
354 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
355 default: FILE_SetDosError();break;
363 /***********************************************************************
364 * CreateDirectoryW (KERNEL32.42)
366 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
367 LPSECURITY_ATTRIBUTES lpsecattribs
)
369 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
370 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
371 HeapFree( GetProcessHeap(), 0, xpath
);
376 /***********************************************************************
377 * CreateDirectoryExA (KERNEL32.40)
379 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
380 LPSECURITY_ATTRIBUTES lpsecattribs
)
382 return CreateDirectoryA(path
,lpsecattribs
);
386 /***********************************************************************
387 * CreateDirectoryExW (KERNEL32.41)
389 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
390 LPSECURITY_ATTRIBUTES lpsecattribs
)
392 return CreateDirectoryW(path
,lpsecattribs
);
396 /***********************************************************************
397 * RemoveDirectory16 (KERNEL)
399 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
401 return (BOOL16
)RemoveDirectoryA( path
);
405 /***********************************************************************
406 * RemoveDirectoryA (KERNEL32.437)
408 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
410 DOS_FULL_NAME full_name
;
412 TRACE_(file
)("'%s'\n", path
);
414 if (DOSFS_GetDevice( path
))
416 TRACE_(file
)("cannot remove device '%s'!\n", path
);
417 SetLastError( ERROR_FILE_NOT_FOUND
);
420 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
421 if (rmdir( full_name
.long_name
) == -1)
430 /***********************************************************************
431 * RemoveDirectoryW (KERNEL32.438)
433 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
435 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
436 BOOL ret
= RemoveDirectoryA( xpath
);
437 HeapFree( GetProcessHeap(), 0, xpath
);
442 /***********************************************************************
445 * Helper function for DIR_SearchPath.
447 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
448 DOS_FULL_NAME
*full_name
)
450 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
451 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
453 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
454 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
456 SetLastError( ERROR_PATH_NOT_FOUND
);
459 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
460 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
461 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
463 strcpy( full_name
->long_name
, dir
->long_name
);
465 strcpy( full_name
->short_name
, dir
->short_name
);
471 /***********************************************************************
472 * DIR_TryEnvironmentPath
474 * Helper function for DIR_SearchPath.
476 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
478 LPSTR path
, next
, buffer
;
480 INT len
= strlen(name
);
481 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
483 if (!size
) return FALSE
;
484 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
485 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
490 while (*cur
== ';') cur
++;
492 next
= strchr( cur
, ';' );
493 if (next
) *next
++ = '\0';
494 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
496 strcpy( buffer
, cur
);
497 strcat( buffer
, "\\" );
498 strcat( buffer
, name
);
499 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
500 HeapFree( GetProcessHeap(), 0, buffer
);
504 HeapFree( GetProcessHeap(), 0, path
);
509 /***********************************************************************
512 * Helper function for DIR_SearchPath.
514 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
516 /* FIXME: for now, GetModuleFileNameA can't return more */
517 /* than OFS_MAXPATHNAME. This may change with Win32. */
519 char buffer
[OFS_MAXPATHNAME
];
524 if (!GetCurrentTask()) return FALSE
;
525 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
528 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
531 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
532 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
534 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
538 /***********************************************************************
541 * Implementation of SearchPathA. 'win32' specifies whether the search
542 * order is Win16 (module path last) or Win32 (module path first).
544 * FIXME: should return long path names.
546 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
547 DOS_FULL_NAME
*full_name
, BOOL win32
)
554 /* First check the supplied parameters */
556 p
= strrchr( name
, '.' );
557 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
558 ext
= NULL
; /* Ignore the specified extension */
559 if ((*name
&& (name
[1] == ':')) ||
560 strchr( name
, '/' ) || strchr( name
, '\\' ))
561 path
= NULL
; /* Ignore path if name already contains a path */
562 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
565 if (ext
) len
+= strlen(ext
);
566 if (path
) len
+= strlen(path
) + 1;
568 /* Allocate a buffer for the file name and extension */
572 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
574 SetLastError( ERROR_OUTOFMEMORY
);
583 else strcpy( tmp
, name
);
584 if (ext
) strcat( tmp
, ext
);
588 /* If we have an explicit path, everything's easy */
590 if (path
|| (*name
&& (name
[1] == ':')) ||
591 strchr( name
, '/' ) || strchr( name
, '\\' ))
593 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
597 /* Try the path of the current executable (for Win32 search order) */
599 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
601 /* Try the current directory */
603 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
605 /* Try the Windows system directory */
607 if (DIR_TryPath( &DIR_System
, name
, full_name
))
610 /* Try the Windows directory */
612 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
615 /* Try the path of the current executable (for Win16 search order) */
617 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
619 /* Try all directories in path */
621 ret
= DIR_TryEnvironmentPath( name
, full_name
);
624 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
629 /***********************************************************************
630 * SearchPathA [KERNEL32.447]
632 * Searches for a specified file in the search path.
635 * path [I] Path to search
636 * name [I] Filename to search for.
637 * ext [I] File extension to append to file name. The first
638 * character must be a period. This parameter is
639 * specified only if the filename given does not
640 * contain an extension.
641 * buflen [I] size of buffer, in characters
642 * buffer [O] buffer for found filename
643 * lastpart [O] address of pointer to last used character in
644 * buffer (the final '\')
647 * Success: length of string copied into buffer, not including
648 * terminating null character. If the filename found is
649 * longer than the length of the buffer, the length of the
650 * filename is returned.
654 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
657 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
658 LPSTR buffer
, LPSTR
*lastpart
)
661 DOS_FULL_NAME full_name
;
663 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
665 SetLastError(ERROR_FILE_NOT_FOUND
);
668 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
669 res
= full_name
.long_name
+
670 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
671 while (*res
== '/') res
++;
674 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
675 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
676 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
678 TRACE("Returning %d\n", strlen(res
) + 3 );
679 return strlen(res
) + 3;
683 /***********************************************************************
684 * SearchPathW (KERNEL32.448)
686 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
687 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
691 DOS_FULL_NAME full_name
;
693 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
694 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
695 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
696 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
697 HeapFree( GetProcessHeap(), 0, extA
);
698 HeapFree( GetProcessHeap(), 0, nameA
);
699 HeapFree( GetProcessHeap(), 0, pathA
);
702 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
703 res
= full_name
.long_name
+
704 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
705 while (*res
== '/') res
++;
708 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
709 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
712 for (p
= *lastpart
= buffer
; *p
; p
++)
713 if (*p
== '\\') *lastpart
= p
+ 1;
716 return strlen(res
) + 3;