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
) );
120 /* Set the environment variables */
122 SetEnvironmentVariableA( "PATH", path
);
123 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
124 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
125 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
126 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
128 TRACE("WindowsDir = %s (%s)\n",
129 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
130 TRACE("SystemDir = %s (%s)\n",
131 DIR_System
.short_name
, DIR_System
.long_name
);
132 TRACE("TempDir = %s (%s)\n",
133 tmp_dir
.short_name
, tmp_dir
.long_name
);
134 TRACE("Path = %s\n", path
);
135 TRACE("Cwd = %c:\\%s\n",
136 'A' + drive
, DRIVE_GetDosCwd( drive
) );
142 /***********************************************************************
143 * GetTempPath32A (KERNEL32.292)
145 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
148 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
149 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
150 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
152 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
161 /***********************************************************************
162 * GetTempPath32W (KERNEL32.293)
164 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
166 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
167 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
169 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
170 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
171 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
173 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
182 /***********************************************************************
183 * DIR_GetWindowsUnixDir
185 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
187 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
188 return strlen( DIR_Windows
.long_name
);
192 /***********************************************************************
193 * DIR_GetSystemUnixDir
195 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
197 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
198 return strlen( DIR_System
.long_name
);
202 /***********************************************************************
203 * GetTempDrive (KERNEL.92)
205 BYTE WINAPI
GetTempDrive( BYTE ignored
)
209 UINT len
= GetTempPathA( 0, NULL
);
211 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
212 return DRIVE_GetCurrentDrive() + 'A';
214 /* FIXME: apparently Windows does something with the ignored byte */
215 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
217 HeapFree( GetProcessHeap(), 0, buffer
);
222 UINT WINAPI
WIN16_GetTempDrive( BYTE ignored
)
224 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
228 * AH: ':' - yes, some kernel code even does stosw with
232 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
236 /***********************************************************************
237 * GetWindowsDirectory16 (KERNEL.134)
239 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
241 return (UINT16
)GetWindowsDirectoryA( path
, count
);
245 /***********************************************************************
246 * GetWindowsDirectory32A (KERNEL32.311)
248 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
250 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
251 return strlen( DIR_Windows
.short_name
);
255 /***********************************************************************
256 * GetWindowsDirectory32W (KERNEL32.312)
258 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
260 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
261 return strlen( DIR_Windows
.short_name
);
265 /***********************************************************************
266 * GetSystemDirectory16 (KERNEL.135)
268 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
270 return (UINT16
)GetSystemDirectoryA( path
, count
);
274 /***********************************************************************
275 * GetSystemDirectory32A (KERNEL32.282)
277 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
279 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
280 return strlen( DIR_System
.short_name
);
284 /***********************************************************************
285 * GetSystemDirectory32W (KERNEL32.283)
287 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
289 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
290 return strlen( DIR_System
.short_name
);
294 /***********************************************************************
295 * CreateDirectory16 (KERNEL.144)
297 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
299 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
300 return (BOOL16
)CreateDirectoryA( path
, NULL
);
304 /***********************************************************************
305 * CreateDirectory32A (KERNEL32.39)
309 * ERROR_DISK_FULL: on full disk
310 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
311 * ERROR_ACCESS_DENIED: on permission problems
312 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
314 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
315 LPSECURITY_ATTRIBUTES lpsecattribs
)
317 DOS_FULL_NAME full_name
;
319 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
320 if (DOSFS_GetDevice( path
))
322 TRACE_(file
)("cannot use device '%s'!\n",path
);
323 SetLastError( ERROR_ACCESS_DENIED
);
326 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
327 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
328 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
329 /* the FILE_SetDosError() generated error codes don't match the
330 * CreateDirectory ones for some errnos */
332 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
333 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
334 default: FILE_SetDosError();break;
342 /***********************************************************************
343 * CreateDirectory32W (KERNEL32.42)
345 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
346 LPSECURITY_ATTRIBUTES lpsecattribs
)
348 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
349 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
350 HeapFree( GetProcessHeap(), 0, xpath
);
355 /***********************************************************************
356 * CreateDirectoryEx32A (KERNEL32.40)
358 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
359 LPSECURITY_ATTRIBUTES lpsecattribs
)
361 return CreateDirectoryA(path
,lpsecattribs
);
365 /***********************************************************************
366 * CreateDirectoryEx32W (KERNEL32.41)
368 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
369 LPSECURITY_ATTRIBUTES lpsecattribs
)
371 return CreateDirectoryW(path
,lpsecattribs
);
375 /***********************************************************************
376 * RemoveDirectory16 (KERNEL)
378 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
380 return (BOOL16
)RemoveDirectoryA( path
);
384 /***********************************************************************
385 * RemoveDirectory32A (KERNEL32.437)
387 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
389 DOS_FULL_NAME full_name
;
391 TRACE_(file
)("'%s'\n", path
);
393 if (DOSFS_GetDevice( path
))
395 TRACE_(file
)("cannot remove device '%s'!\n", path
);
396 SetLastError( ERROR_FILE_NOT_FOUND
);
399 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
400 if (rmdir( full_name
.long_name
) == -1)
409 /***********************************************************************
410 * RemoveDirectory32W (KERNEL32.438)
412 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
414 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
415 BOOL ret
= RemoveDirectoryA( xpath
);
416 HeapFree( GetProcessHeap(), 0, xpath
);
421 /***********************************************************************
424 * Helper function for DIR_SearchPath.
426 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
427 DOS_FULL_NAME
*full_name
)
429 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
430 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
432 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
433 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
435 SetLastError( ERROR_PATH_NOT_FOUND
);
438 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
439 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
440 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
442 strcpy( full_name
->long_name
, dir
->long_name
);
444 strcpy( full_name
->short_name
, dir
->short_name
);
450 /***********************************************************************
451 * DIR_TryEnvironmentPath
453 * Helper function for DIR_SearchPath.
455 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
457 LPSTR path
, next
, buffer
;
459 INT len
= strlen(name
);
460 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
462 if (!size
) return FALSE
;
463 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
464 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
469 while (*cur
== ';') cur
++;
471 next
= strchr( cur
, ';' );
472 if (next
) *next
++ = '\0';
473 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
475 strcpy( buffer
, cur
);
476 strcat( buffer
, "\\" );
477 strcat( buffer
, name
);
478 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
479 HeapFree( GetProcessHeap(), 0, buffer
);
483 HeapFree( GetProcessHeap(), 0, path
);
488 /***********************************************************************
491 * Helper function for DIR_SearchPath.
493 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
495 PDB
*pdb
= PROCESS_Current();
497 /* FIXME: for now, GetModuleFileName32A can't return more */
498 /* than OFS_MAXPATHNAME. This may change with Win32. */
500 char buffer
[OFS_MAXPATHNAME
];
503 if (pdb
->flags
& PDB32_WIN16_PROC
) {
504 if (!GetCurrentTask()) return FALSE
;
505 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
508 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
511 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
512 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
514 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
518 /***********************************************************************
521 * Implementation of SearchPath32A. 'win32' specifies whether the search
522 * order is Win16 (module path last) or Win32 (module path first).
524 * FIXME: should return long path names.
526 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
527 DOS_FULL_NAME
*full_name
, BOOL win32
)
534 /* First check the supplied parameters */
536 p
= strrchr( name
, '.' );
537 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
538 ext
= NULL
; /* Ignore the specified extension */
539 if ((*name
&& (name
[1] == ':')) ||
540 strchr( name
, '/' ) || strchr( name
, '\\' ))
541 path
= NULL
; /* Ignore path if name already contains a path */
542 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
545 if (ext
) len
+= strlen(ext
);
546 if (path
) len
+= strlen(path
) + 1;
548 /* Allocate a buffer for the file name and extension */
552 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
554 SetLastError( ERROR_OUTOFMEMORY
);
563 else strcpy( tmp
, name
);
564 if (ext
) strcat( tmp
, ext
);
568 /* If we have an explicit path, everything's easy */
570 if (path
|| (*name
&& (name
[1] == ':')) ||
571 strchr( name
, '/' ) || strchr( name
, '\\' ))
573 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
577 /* Try the path of the current executable (for Win32 search order) */
579 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
581 /* Try the current directory */
583 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
585 /* Try the Windows system directory */
587 if (DIR_TryPath( &DIR_System
, name
, full_name
))
590 /* Try the Windows directory */
592 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
595 /* Try the path of the current executable (for Win16 search order) */
597 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
599 /* Try all directories in path */
601 ret
= DIR_TryEnvironmentPath( name
, full_name
);
604 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
609 /***********************************************************************
610 * SearchPath32A [KERNEL32.447]
612 * Searches for a specified file in the search path.
615 * path [I] Path to search
616 * name [I] Filename to search for.
617 * ext [I] File extension to append to file name. The first
618 * character must be a period. This parameter is
619 * specified only if the filename given does not
620 * contain an extension.
621 * buflen [I] size of buffer, in characters
622 * buffer [O] buffer for found filename
623 * lastpart [O] address of pointer to last used character in
624 * buffer (the final '\')
627 * Success: length of string copied into buffer, not including
628 * terminating null character. If the filename found is
629 * longer than the length of the buffer, the length of the
630 * filename is returned.
634 * Should call SetLastError(but currently doesn't).
636 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
637 LPSTR buffer
, LPSTR
*lastpart
)
640 DOS_FULL_NAME full_name
;
642 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
643 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
644 res
= full_name
.long_name
+
645 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
646 while (*res
== '/') res
++;
649 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
650 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
651 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
653 TRACE("Returning %d\n", strlen(res
) + 3 );
654 return strlen(res
) + 3;
658 /***********************************************************************
659 * SearchPath32W (KERNEL32.448)
661 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
662 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
666 DOS_FULL_NAME full_name
;
668 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
669 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
670 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
671 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
672 HeapFree( GetProcessHeap(), 0, extA
);
673 HeapFree( GetProcessHeap(), 0, nameA
);
674 HeapFree( GetProcessHeap(), 0, pathA
);
677 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
678 res
= full_name
.long_name
+
679 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
680 while (*res
== '/') res
++;
683 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
684 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
687 for (p
= *lastpart
= buffer
; *p
; p
++)
688 if (*p
== '\\') *lastpart
= p
+ 1;
691 return strlen(res
) + 3;