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
)
49 char path
[MAX_PATHNAME_LEN
];
50 BY_HANDLE_FILE_INFORMATION info
;
52 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
53 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
54 !FILE_Stat( full_name
->long_name
, &info
) ||
55 !(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
57 MESSAGE("Invalid path '%s' for %s directory\n", path
, keyname
);
64 /***********************************************************************
69 char path
[MAX_PATHNAME_LEN
];
70 DOS_FULL_NAME tmp_dir
, profile_dir
;
74 if (!getcwd( path
, MAX_PATHNAME_LEN
))
76 perror( "Could not get current directory" );
80 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
82 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
83 "for current working directory %s; "
84 "starting in windows directory.\n", cwd
);
88 DRIVE_SetCurrentDrive( drive
);
89 DRIVE_Chdir( drive
, cwd
);
92 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
93 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
94 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
96 PROFILE_UsageWineIni();
99 if (-1 == access( tmp_dir
.long_name
, W_OK
))
103 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
104 PROFILE_UsageWineIni();
107 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
113 drive
= DIR_Windows
.drive
;
114 DRIVE_SetCurrentDrive( drive
);
115 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
118 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
119 path
, sizeof(path
) );
120 if (strchr(path
, '/'))
122 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
123 PROFILE_UsageWineIni();
127 /* Set the environment variables */
129 SetEnvironmentVariableA( "PATH", path
);
130 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
131 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
132 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
134 /* set COMSPEC only if it doesn't exist already */
135 if (!GetEnvironmentVariableA( "COMSPEC", NULL
, 0 ))
136 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
138 TRACE("WindowsDir = %s (%s)\n",
139 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
140 TRACE("SystemDir = %s (%s)\n",
141 DIR_System
.short_name
, DIR_System
.long_name
);
142 TRACE("TempDir = %s (%s)\n",
143 tmp_dir
.short_name
, tmp_dir
.long_name
);
144 TRACE("Path = %s\n", path
);
145 TRACE("Cwd = %c:\\%s\n",
146 'A' + drive
, DRIVE_GetDosCwd( drive
) );
148 if (DIR_GetPath( "profile", "", &profile_dir
))
150 TRACE("USERPROFILE= %s\n", profile_dir
.short_name
);
151 SetEnvironmentVariableA( "USERPROFILE", profile_dir
.short_name
);
154 TRACE("SYSTEMROOT = %s\n", DIR_Windows
.short_name
);
155 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows
.short_name
);
161 /***********************************************************************
162 * GetTempPathA (KERNEL32.292)
164 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
167 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
168 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
169 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
171 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
180 /***********************************************************************
181 * GetTempPathW (KERNEL32.293)
183 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
185 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
186 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
188 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
189 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
190 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
192 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
201 /***********************************************************************
202 * DIR_GetWindowsUnixDir
204 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
206 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
207 return strlen( DIR_Windows
.long_name
);
211 /***********************************************************************
212 * DIR_GetSystemUnixDir
214 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
216 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
217 return strlen( DIR_System
.long_name
);
221 /***********************************************************************
222 * GetTempDrive (KERNEL.92)
223 * A closer look at krnl386.exe shows what the SDK doesn't mention:
227 * AH: ':' - yes, some kernel code even does stosw with
231 UINT WINAPI
GetTempDrive( BYTE ignored
)
235 UINT len
= GetTempPathA( 0, NULL
);
237 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
238 ret
= DRIVE_GetCurrentDrive() + 'A';
241 /* FIXME: apparently Windows does something with the ignored byte */
242 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
243 ret
= toupper(buffer
[0]);
244 HeapFree( GetProcessHeap(), 0, buffer
);
246 return MAKELONG( ret
| (':' << 8), 1 );
250 /***********************************************************************
251 * GetWindowsDirectory16 (KERNEL.134)
253 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
255 return (UINT16
)GetWindowsDirectoryA( path
, count
);
259 /***********************************************************************
260 * GetWindowsDirectoryA (KERNEL32.311)
262 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
264 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
265 return strlen( DIR_Windows
.short_name
);
269 /***********************************************************************
270 * GetWindowsDirectoryW (KERNEL32.312)
272 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
274 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0 );
277 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
))
284 /***********************************************************************
285 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
287 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
289 return GetWindowsDirectoryA( path
, count
);
293 /***********************************************************************
294 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
296 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
298 return GetWindowsDirectoryW( path
, count
);
302 /***********************************************************************
303 * GetSystemDirectory16 (KERNEL.135)
305 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
307 return (UINT16
)GetSystemDirectoryA( path
, count
);
311 /***********************************************************************
312 * GetSystemDirectoryA (KERNEL32.282)
314 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
316 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
317 return strlen( DIR_System
.short_name
);
321 /***********************************************************************
322 * GetSystemDirectoryW (KERNEL32.283)
324 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
326 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0 );
329 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
))
336 /***********************************************************************
337 * CreateDirectory16 (KERNEL.144)
339 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
341 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
342 return (BOOL16
)CreateDirectoryA( path
, NULL
);
346 /***********************************************************************
347 * CreateDirectoryA (KERNEL32.39)
351 * ERROR_DISK_FULL: on full disk
352 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
353 * ERROR_ACCESS_DENIED: on permission problems
354 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
356 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
357 LPSECURITY_ATTRIBUTES lpsecattribs
)
359 DOS_FULL_NAME full_name
;
361 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
362 if (DOSFS_GetDevice( path
))
364 TRACE_(file
)("cannot use device '%s'!\n",path
);
365 SetLastError( ERROR_ACCESS_DENIED
);
368 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
369 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
370 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
371 /* the FILE_SetDosError() generated error codes don't match the
372 * CreateDirectory ones for some errnos */
374 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
375 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
376 default: FILE_SetDosError();break;
384 /***********************************************************************
385 * CreateDirectoryW (KERNEL32.42)
387 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
388 LPSECURITY_ATTRIBUTES lpsecattribs
)
390 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
391 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
392 HeapFree( GetProcessHeap(), 0, xpath
);
397 /***********************************************************************
398 * CreateDirectoryExA (KERNEL32.40)
400 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
401 LPSECURITY_ATTRIBUTES lpsecattribs
)
403 return CreateDirectoryA(path
,lpsecattribs
);
407 /***********************************************************************
408 * CreateDirectoryExW (KERNEL32.41)
410 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
411 LPSECURITY_ATTRIBUTES lpsecattribs
)
413 return CreateDirectoryW(path
,lpsecattribs
);
417 /***********************************************************************
418 * RemoveDirectory16 (KERNEL)
420 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
422 return (BOOL16
)RemoveDirectoryA( path
);
426 /***********************************************************************
427 * RemoveDirectoryA (KERNEL32.437)
429 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
431 DOS_FULL_NAME full_name
;
433 TRACE_(file
)("'%s'\n", path
);
435 if (DOSFS_GetDevice( path
))
437 TRACE_(file
)("cannot remove device '%s'!\n", path
);
438 SetLastError( ERROR_FILE_NOT_FOUND
);
441 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
442 if (rmdir( full_name
.long_name
) == -1)
451 /***********************************************************************
452 * RemoveDirectoryW (KERNEL32.438)
454 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
456 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
457 BOOL ret
= RemoveDirectoryA( xpath
);
458 HeapFree( GetProcessHeap(), 0, xpath
);
463 /***********************************************************************
466 * Helper function for DIR_SearchPath.
468 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
469 DOS_FULL_NAME
*full_name
)
471 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
472 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
474 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
475 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
477 SetLastError( ERROR_PATH_NOT_FOUND
);
480 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
481 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
482 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
484 strcpy( full_name
->long_name
, dir
->long_name
);
486 strcpy( full_name
->short_name
, dir
->short_name
);
492 /***********************************************************************
493 * DIR_TryEnvironmentPath
495 * Helper function for DIR_SearchPath.
496 * Search in the specified path, or in $PATH if NULL.
498 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
, LPCSTR envpath
)
500 LPSTR path
, next
, buffer
;
502 INT len
= strlen(name
);
505 size
= envpath
? strlen(envpath
)+1 : GetEnvironmentVariableA( "PATH", NULL
, 0 );
506 if (!size
) return FALSE
;
507 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
508 if (envpath
) strcpy( path
, envpath
);
509 else if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
514 while (*cur
== ';') cur
++;
516 next
= strchr( cur
, ';' );
517 if (next
) *next
++ = '\0';
518 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
520 strcpy( buffer
, cur
);
521 strcat( buffer
, "\\" );
522 strcat( buffer
, name
);
523 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
524 HeapFree( GetProcessHeap(), 0, buffer
);
528 HeapFree( GetProcessHeap(), 0, path
);
533 /***********************************************************************
536 * Helper function for DIR_SearchPath.
538 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
540 /* FIXME: for now, GetModuleFileNameA can't return more */
541 /* than OFS_MAXPATHNAME. This may change with Win32. */
543 char buffer
[OFS_MAXPATHNAME
];
548 if (!GetCurrentTask()) return FALSE
;
549 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
552 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
555 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
556 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
558 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
562 /***********************************************************************
565 * Implementation of SearchPathA. 'win32' specifies whether the search
566 * order is Win16 (module path last) or Win32 (module path first).
568 * FIXME: should return long path names.
570 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
571 DOS_FULL_NAME
*full_name
, BOOL win32
)
577 /* First check the supplied parameters */
579 p
= strrchr( name
, '.' );
580 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
581 ext
= NULL
; /* Ignore the specified extension */
582 if ((*name
&& (name
[1] == ':')) ||
583 strchr( name
, '/' ) || strchr( name
, '\\' ))
584 path
= NULL
; /* Ignore path if name already contains a path */
585 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
587 /* Allocate a buffer for the file name and extension */
591 DWORD len
= strlen(name
) + strlen(ext
);
592 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
594 SetLastError( ERROR_OUTOFMEMORY
);
602 /* If the name contains an explicit path, everything's easy */
604 if ((*name
&& (name
[1] == ':')) || strchr( name
, '/' ) || strchr( name
, '\\' ))
606 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
610 /* Search in the specified path */
614 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
618 /* Try the path of the current executable (for Win32 search order) */
620 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
622 /* Try the current directory */
624 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
626 /* Try the Windows system directory */
628 if (DIR_TryPath( &DIR_System
, name
, full_name
))
631 /* Try the Windows directory */
633 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
636 /* Try the path of the current executable (for Win16 search order) */
638 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
640 /* Try all directories in path */
642 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
645 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
650 /***********************************************************************
651 * SearchPathA [KERNEL32.447]
653 * Searches for a specified file in the search path.
656 * path [I] Path to search
657 * name [I] Filename to search for.
658 * ext [I] File extension to append to file name. The first
659 * character must be a period. This parameter is
660 * specified only if the filename given does not
661 * contain an extension.
662 * buflen [I] size of buffer, in characters
663 * buffer [O] buffer for found filename
664 * lastpart [O] address of pointer to last used character in
665 * buffer (the final '\')
668 * Success: length of string copied into buffer, not including
669 * terminating null character. If the filename found is
670 * longer than the length of the buffer, the length of the
671 * filename is returned.
675 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
678 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
679 LPSTR buffer
, LPSTR
*lastpart
)
682 DOS_FULL_NAME full_name
;
684 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
686 SetLastError(ERROR_FILE_NOT_FOUND
);
689 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
690 res
= full_name
.long_name
+
691 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
692 while (*res
== '/') res
++;
695 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
696 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
697 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
699 TRACE("Returning %d\n", strlen(res
) + 3 );
700 return strlen(res
) + 3;
704 /***********************************************************************
705 * SearchPathW (KERNEL32.448)
707 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
708 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
712 DOS_FULL_NAME full_name
;
714 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
715 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
716 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
717 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
718 HeapFree( GetProcessHeap(), 0, extA
);
719 HeapFree( GetProcessHeap(), 0, nameA
);
720 HeapFree( GetProcessHeap(), 0, pathA
);
723 if (buflen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
))
724 buffer
[buflen
-1] = 0;
725 res
= full_name
.long_name
+
726 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
727 while (*res
== '/') res
++;
732 if (!MultiByteToWideChar( CP_ACP
, 0, res
, -1, buffer
+3, buflen
-3 ))
733 buffer
[buflen
-1] = 0;
735 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
738 for (p
= *lastpart
= buffer
; *p
; p
++)
739 if (*p
== '\\') *lastpart
= p
+ 1;
742 return strlen(res
) + 3;