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"
25 #include "wine/winuser16.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(dosfs
);
35 DECLARE_DEBUG_CHANNEL(file
);
37 static DOS_FULL_NAME DIR_Windows
;
38 static DOS_FULL_NAME DIR_System
;
41 /***********************************************************************
44 * Get a path name from the wine.ini file and make sure it is valid.
46 static int DIR_GetPath( const char *keyname
, const char *defval
,
47 DOS_FULL_NAME
*full_name
, BOOL warn
)
49 char path
[MAX_PATHNAME_LEN
];
50 BY_HANDLE_FILE_INFORMATION info
;
51 const char *mess
= "does not exist";
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
) && (mess
=strerror(errno
)))||
56 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")))
59 MESSAGE("Invalid path '%s' for %s directory: %s\n", path
, keyname
, mess
);
66 /***********************************************************************
71 char path
[MAX_PATHNAME_LEN
];
72 DOS_FULL_NAME tmp_dir
, profile_dir
;
76 if (!getcwd( path
, MAX_PATHNAME_LEN
))
78 perror( "Could not get current directory" );
82 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
84 MESSAGE("Warning: could not find wine config [Drive x] entry "
85 "for current working directory %s; "
86 "starting in windows directory.\n", cwd
);
90 DRIVE_SetCurrentDrive( drive
);
91 DRIVE_Chdir( drive
, cwd
);
94 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
, TRUE
)) ||
95 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
, TRUE
)) ||
96 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
, TRUE
)))
98 PROFILE_UsageWineIni();
101 if (-1 == access( tmp_dir
.long_name
, W_OK
))
105 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
106 PROFILE_UsageWineIni();
109 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
110 tmp_dir
.long_name
, strerror(errno
));
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 config!\n");
125 PROFILE_UsageWineIni();
129 /* Set the environment variables */
131 SetEnvironmentVariableA( "PATH", path
);
132 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
133 SetEnvironmentVariableA( "TMP", tmp_dir
.short_name
);
134 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
135 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
137 /* set COMSPEC only if it doesn't exist already */
138 if (!GetEnvironmentVariableA( "COMSPEC", NULL
, 0 ))
139 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
141 TRACE("WindowsDir = %s (%s)\n",
142 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
143 TRACE("SystemDir = %s (%s)\n",
144 DIR_System
.short_name
, DIR_System
.long_name
);
145 TRACE("TempDir = %s (%s)\n",
146 tmp_dir
.short_name
, tmp_dir
.long_name
);
147 TRACE("Path = %s\n", path
);
148 TRACE("Cwd = %c:\\%s\n",
149 'A' + drive
, DRIVE_GetDosCwd( drive
) );
151 if (DIR_GetPath( "profile", "", &profile_dir
, FALSE
))
153 TRACE("USERPROFILE= %s\n", profile_dir
.short_name
);
154 SetEnvironmentVariableA( "USERPROFILE", profile_dir
.short_name
);
157 TRACE("SYSTEMROOT = %s\n", DIR_Windows
.short_name
);
158 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows
.short_name
);
164 /***********************************************************************
165 * GetTempPathA (KERNEL32.292)
167 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
170 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
171 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
172 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
174 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
183 /***********************************************************************
184 * GetTempPathW (KERNEL32.293)
186 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
188 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
189 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
191 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
192 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
193 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
195 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
204 /***********************************************************************
205 * DIR_GetWindowsUnixDir
207 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
209 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
210 return strlen( DIR_Windows
.long_name
);
214 /***********************************************************************
215 * DIR_GetSystemUnixDir
217 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
219 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
220 return strlen( DIR_System
.long_name
);
224 /***********************************************************************
225 * GetTempDrive (KERNEL.92)
226 * A closer look at krnl386.exe shows what the SDK doesn't mention:
230 * AH: ':' - yes, some kernel code even does stosw with
234 UINT WINAPI
GetTempDrive( BYTE ignored
)
238 UINT len
= GetTempPathA( 0, NULL
);
240 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
241 ret
= DRIVE_GetCurrentDrive() + 'A';
244 /* FIXME: apparently Windows does something with the ignored byte */
245 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
246 ret
= toupper(buffer
[0]);
247 HeapFree( GetProcessHeap(), 0, buffer
);
249 return MAKELONG( ret
| (':' << 8), 1 );
253 /***********************************************************************
254 * GetWindowsDirectory16 (KERNEL.134)
256 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
258 return (UINT16
)GetWindowsDirectoryA( path
, count
);
262 /***********************************************************************
263 * GetWindowsDirectoryA (KERNEL32.311)
265 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
267 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
268 return strlen( DIR_Windows
.short_name
);
272 /***********************************************************************
273 * GetWindowsDirectoryW (KERNEL32.312)
275 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
277 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0 );
280 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
))
287 /***********************************************************************
288 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
290 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
292 return GetWindowsDirectoryA( path
, count
);
296 /***********************************************************************
297 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
299 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
301 return GetWindowsDirectoryW( path
, count
);
305 /***********************************************************************
306 * GetSystemDirectory16 (KERNEL.135)
308 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
310 return (UINT16
)GetSystemDirectoryA( path
, count
);
314 /***********************************************************************
315 * GetSystemDirectoryA (KERNEL32.282)
317 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
319 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
320 return strlen( DIR_System
.short_name
);
324 /***********************************************************************
325 * GetSystemDirectoryW (KERNEL32.283)
327 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
329 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0 );
332 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
))
339 /***********************************************************************
340 * CreateDirectory16 (KERNEL.144)
342 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
344 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
345 return (BOOL16
)CreateDirectoryA( path
, NULL
);
349 /***********************************************************************
350 * CreateDirectoryA (KERNEL32.39)
354 * ERROR_DISK_FULL: on full disk
355 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
356 * ERROR_ACCESS_DENIED: on permission problems
357 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
359 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
360 LPSECURITY_ATTRIBUTES lpsecattribs
)
362 DOS_FULL_NAME full_name
;
364 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
365 if (DOSFS_GetDevice( path
))
367 TRACE_(file
)("cannot use device '%s'!\n",path
);
368 SetLastError( ERROR_ACCESS_DENIED
);
371 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
372 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
373 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
374 /* the FILE_SetDosError() generated error codes don't match the
375 * CreateDirectory ones for some errnos */
377 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
378 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
379 default: FILE_SetDosError();break;
387 /***********************************************************************
388 * CreateDirectoryW (KERNEL32.42)
390 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
391 LPSECURITY_ATTRIBUTES lpsecattribs
)
393 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
394 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
395 HeapFree( GetProcessHeap(), 0, xpath
);
400 /***********************************************************************
401 * CreateDirectoryExA (KERNEL32.40)
403 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
404 LPSECURITY_ATTRIBUTES lpsecattribs
)
406 return CreateDirectoryA(path
,lpsecattribs
);
410 /***********************************************************************
411 * CreateDirectoryExW (KERNEL32.41)
413 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
414 LPSECURITY_ATTRIBUTES lpsecattribs
)
416 return CreateDirectoryW(path
,lpsecattribs
);
420 /***********************************************************************
421 * RemoveDirectory16 (KERNEL)
423 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
425 return (BOOL16
)RemoveDirectoryA( path
);
429 /***********************************************************************
430 * RemoveDirectoryA (KERNEL32.437)
432 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
434 DOS_FULL_NAME full_name
;
436 TRACE_(file
)("'%s'\n", path
);
438 if (DOSFS_GetDevice( path
))
440 TRACE_(file
)("cannot remove device '%s'!\n", path
);
441 SetLastError( ERROR_FILE_NOT_FOUND
);
444 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
445 if (rmdir( full_name
.long_name
) == -1)
454 /***********************************************************************
455 * RemoveDirectoryW (KERNEL32.438)
457 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
459 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
460 BOOL ret
= RemoveDirectoryA( xpath
);
461 HeapFree( GetProcessHeap(), 0, xpath
);
466 /***********************************************************************
469 * Helper function for DIR_SearchPath.
471 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
472 DOS_FULL_NAME
*full_name
)
474 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
475 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
477 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
478 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
480 SetLastError( ERROR_PATH_NOT_FOUND
);
483 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
484 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
485 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
487 strcpy( full_name
->long_name
, dir
->long_name
);
489 strcpy( full_name
->short_name
, dir
->short_name
);
495 /***********************************************************************
496 * DIR_TryEnvironmentPath
498 * Helper function for DIR_SearchPath.
499 * Search in the specified path, or in $PATH if NULL.
501 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
, LPCSTR envpath
)
503 LPSTR path
, next
, buffer
;
505 INT len
= strlen(name
);
508 size
= envpath
? strlen(envpath
)+1 : GetEnvironmentVariableA( "PATH", NULL
, 0 );
509 if (!size
) return FALSE
;
510 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
511 if (envpath
) strcpy( path
, envpath
);
512 else if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
517 while (*cur
== ';') cur
++;
519 next
= strchr( cur
, ';' );
520 if (next
) *next
++ = '\0';
521 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
523 strcpy( buffer
, cur
);
524 strcat( buffer
, "\\" );
525 strcat( buffer
, name
);
526 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
527 HeapFree( GetProcessHeap(), 0, buffer
);
531 HeapFree( GetProcessHeap(), 0, path
);
536 /***********************************************************************
539 * Helper function for DIR_SearchPath.
541 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
543 /* FIXME: for now, GetModuleFileNameA can't return more */
544 /* than OFS_MAXPATHNAME. This may change with Win32. */
546 char buffer
[OFS_MAXPATHNAME
];
551 if (!GetCurrentTask()) return FALSE
;
552 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
555 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
558 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
559 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
561 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
565 /***********************************************************************
568 * Implementation of SearchPathA. 'win32' specifies whether the search
569 * order is Win16 (module path last) or Win32 (module path first).
571 * FIXME: should return long path names.
573 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
574 DOS_FULL_NAME
*full_name
, BOOL win32
)
580 /* First check the supplied parameters */
582 p
= strrchr( name
, '.' );
583 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
584 ext
= NULL
; /* Ignore the specified extension */
585 if ((*name
&& (name
[1] == ':')) ||
586 strchr( name
, '/' ) || strchr( name
, '\\' ))
587 path
= NULL
; /* Ignore path if name already contains a path */
588 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
590 /* Allocate a buffer for the file name and extension */
594 DWORD len
= strlen(name
) + strlen(ext
);
595 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
597 SetLastError( ERROR_OUTOFMEMORY
);
605 /* If the name contains an explicit path, everything's easy */
607 if ((*name
&& (name
[1] == ':')) || strchr( name
, '/' ) || strchr( name
, '\\' ))
609 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
613 /* Search in the specified path */
617 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
621 /* Try the path of the current executable (for Win32 search order) */
623 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
625 /* Try the current directory */
627 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
629 /* Try the Windows system directory */
631 if (DIR_TryPath( &DIR_System
, name
, full_name
))
634 /* Try the Windows directory */
636 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
639 /* Try the path of the current executable (for Win16 search order) */
641 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
643 /* Try all directories in path */
645 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
648 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
653 /***********************************************************************
654 * SearchPathA [KERNEL32.447]
656 * Searches for a specified file in the search path.
659 * path [I] Path to search
660 * name [I] Filename to search for.
661 * ext [I] File extension to append to file name. The first
662 * character must be a period. This parameter is
663 * specified only if the filename given does not
664 * contain an extension.
665 * buflen [I] size of buffer, in characters
666 * buffer [O] buffer for found filename
667 * lastpart [O] address of pointer to last used character in
668 * buffer (the final '\')
671 * Success: length of string copied into buffer, not including
672 * terminating null character. If the filename found is
673 * longer than the length of the buffer, the length of the
674 * filename is returned.
678 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
681 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
682 LPSTR buffer
, LPSTR
*lastpart
)
685 DOS_FULL_NAME full_name
;
687 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
689 SetLastError(ERROR_FILE_NOT_FOUND
);
692 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
693 res
= full_name
.long_name
+
694 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
695 while (*res
== '/') res
++;
698 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
699 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
700 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
702 TRACE("Returning %d\n", strlen(res
) + 3 );
703 return strlen(res
) + 3;
707 /***********************************************************************
708 * SearchPathW (KERNEL32.448)
710 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
711 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
715 DOS_FULL_NAME full_name
;
717 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
718 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
719 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
720 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
721 HeapFree( GetProcessHeap(), 0, extA
);
722 HeapFree( GetProcessHeap(), 0, nameA
);
723 HeapFree( GetProcessHeap(), 0, pathA
);
726 if (buflen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
))
727 buffer
[buflen
-1] = 0;
728 res
= full_name
.long_name
+
729 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
730 while (*res
== '/') res
++;
735 if (!MultiByteToWideChar( CP_ACP
, 0, res
, -1, buffer
+3, buflen
-3 ))
736 buffer
[buflen
-1] = 0;
738 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
741 for (p
= *lastpart
= buffer
; *p
; p
++)
742 if (*p
== '\\') *lastpart
= p
+ 1;
745 return strlen(res
) + 3;