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
;
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")))
58 MESSAGE("Invalid path '%s' for %s directory: %s\n", path
, keyname
, mess
);
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 config [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
);
93 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
94 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
95 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
97 PROFILE_UsageWineIni();
100 if (-1 == access( tmp_dir
.long_name
, W_OK
))
104 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
105 PROFILE_UsageWineIni();
108 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
109 tmp_dir
.long_name
, strerror(errno
));
114 drive
= DIR_Windows
.drive
;
115 DRIVE_SetCurrentDrive( drive
);
116 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
119 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
120 path
, sizeof(path
) );
121 if (strchr(path
, '/'))
123 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine config!\n");
124 PROFILE_UsageWineIni();
128 /* Set the environment variables */
130 SetEnvironmentVariableA( "PATH", path
);
131 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
132 SetEnvironmentVariableA( "TMP", 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)
225 * A closer look at krnl386.exe shows what the SDK doesn't mention:
229 * AH: ':' - yes, some kernel code even does stosw with
233 UINT WINAPI
GetTempDrive( BYTE ignored
)
237 UINT len
= GetTempPathA( 0, NULL
);
239 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
240 ret
= DRIVE_GetCurrentDrive() + 'A';
243 /* FIXME: apparently Windows does something with the ignored byte */
244 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
245 ret
= toupper(buffer
[0]);
246 HeapFree( GetProcessHeap(), 0, buffer
);
248 return MAKELONG( ret
| (':' << 8), 1 );
252 /***********************************************************************
253 * GetWindowsDirectory16 (KERNEL.134)
255 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
257 return (UINT16
)GetWindowsDirectoryA( path
, count
);
261 /***********************************************************************
262 * GetWindowsDirectoryA (KERNEL32.311)
264 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
266 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
267 return strlen( DIR_Windows
.short_name
);
271 /***********************************************************************
272 * GetWindowsDirectoryW (KERNEL32.312)
274 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
276 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0 );
279 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
))
286 /***********************************************************************
287 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
289 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
291 return GetWindowsDirectoryA( path
, count
);
295 /***********************************************************************
296 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
298 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
300 return GetWindowsDirectoryW( path
, count
);
304 /***********************************************************************
305 * GetSystemDirectory16 (KERNEL.135)
307 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
309 return (UINT16
)GetSystemDirectoryA( path
, count
);
313 /***********************************************************************
314 * GetSystemDirectoryA (KERNEL32.282)
316 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
318 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
319 return strlen( DIR_System
.short_name
);
323 /***********************************************************************
324 * GetSystemDirectoryW (KERNEL32.283)
326 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
328 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0 );
331 if (!MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
))
338 /***********************************************************************
339 * CreateDirectory16 (KERNEL.144)
341 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
343 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
344 return (BOOL16
)CreateDirectoryA( path
, NULL
);
348 /***********************************************************************
349 * CreateDirectoryA (KERNEL32.39)
353 * ERROR_DISK_FULL: on full disk
354 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
355 * ERROR_ACCESS_DENIED: on permission problems
356 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
358 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
359 LPSECURITY_ATTRIBUTES lpsecattribs
)
361 DOS_FULL_NAME full_name
;
363 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
364 if (DOSFS_GetDevice( path
))
366 TRACE_(file
)("cannot use device '%s'!\n",path
);
367 SetLastError( ERROR_ACCESS_DENIED
);
370 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
371 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
372 WARN_(file
)("Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
373 /* the FILE_SetDosError() generated error codes don't match the
374 * CreateDirectory ones for some errnos */
376 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
377 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
378 default: FILE_SetDosError();break;
386 /***********************************************************************
387 * CreateDirectoryW (KERNEL32.42)
389 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
390 LPSECURITY_ATTRIBUTES lpsecattribs
)
392 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
393 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
394 HeapFree( GetProcessHeap(), 0, xpath
);
399 /***********************************************************************
400 * CreateDirectoryExA (KERNEL32.40)
402 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
403 LPSECURITY_ATTRIBUTES lpsecattribs
)
405 return CreateDirectoryA(path
,lpsecattribs
);
409 /***********************************************************************
410 * CreateDirectoryExW (KERNEL32.41)
412 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
413 LPSECURITY_ATTRIBUTES lpsecattribs
)
415 return CreateDirectoryW(path
,lpsecattribs
);
419 /***********************************************************************
420 * RemoveDirectory16 (KERNEL)
422 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
424 return (BOOL16
)RemoveDirectoryA( path
);
428 /***********************************************************************
429 * RemoveDirectoryA (KERNEL32.437)
431 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
433 DOS_FULL_NAME full_name
;
435 TRACE_(file
)("'%s'\n", path
);
437 if (DOSFS_GetDevice( path
))
439 TRACE_(file
)("cannot remove device '%s'!\n", path
);
440 SetLastError( ERROR_FILE_NOT_FOUND
);
443 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
444 if (rmdir( full_name
.long_name
) == -1)
453 /***********************************************************************
454 * RemoveDirectoryW (KERNEL32.438)
456 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
458 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
459 BOOL ret
= RemoveDirectoryA( xpath
);
460 HeapFree( GetProcessHeap(), 0, xpath
);
465 /***********************************************************************
468 * Helper function for DIR_SearchPath.
470 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
471 DOS_FULL_NAME
*full_name
)
473 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
474 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
476 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
477 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
479 SetLastError( ERROR_PATH_NOT_FOUND
);
482 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
483 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
484 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
486 strcpy( full_name
->long_name
, dir
->long_name
);
488 strcpy( full_name
->short_name
, dir
->short_name
);
494 /***********************************************************************
495 * DIR_TryEnvironmentPath
497 * Helper function for DIR_SearchPath.
498 * Search in the specified path, or in $PATH if NULL.
500 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
, LPCSTR envpath
)
502 LPSTR path
, next
, buffer
;
504 INT len
= strlen(name
);
507 size
= envpath
? strlen(envpath
)+1 : GetEnvironmentVariableA( "PATH", NULL
, 0 );
508 if (!size
) return FALSE
;
509 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
510 if (envpath
) strcpy( path
, envpath
);
511 else if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
516 while (*cur
== ';') cur
++;
518 next
= strchr( cur
, ';' );
519 if (next
) *next
++ = '\0';
520 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
522 strcpy( buffer
, cur
);
523 strcat( buffer
, "\\" );
524 strcat( buffer
, name
);
525 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
526 HeapFree( GetProcessHeap(), 0, buffer
);
530 HeapFree( GetProcessHeap(), 0, path
);
535 /***********************************************************************
538 * Helper function for DIR_SearchPath.
540 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
542 /* FIXME: for now, GetModuleFileNameA can't return more */
543 /* than OFS_MAXPATHNAME. This may change with Win32. */
545 char buffer
[OFS_MAXPATHNAME
];
550 if (!GetCurrentTask()) return FALSE
;
551 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
554 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
557 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
558 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
560 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
564 /***********************************************************************
567 * Implementation of SearchPathA. 'win32' specifies whether the search
568 * order is Win16 (module path last) or Win32 (module path first).
570 * FIXME: should return long path names.
572 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
573 DOS_FULL_NAME
*full_name
, BOOL win32
)
579 /* First check the supplied parameters */
581 p
= strrchr( name
, '.' );
582 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
583 ext
= NULL
; /* Ignore the specified extension */
584 if ((*name
&& (name
[1] == ':')) ||
585 strchr( name
, '/' ) || strchr( name
, '\\' ))
586 path
= NULL
; /* Ignore path if name already contains a path */
587 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
589 /* Allocate a buffer for the file name and extension */
593 DWORD len
= strlen(name
) + strlen(ext
);
594 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
596 SetLastError( ERROR_OUTOFMEMORY
);
604 /* If the name contains an explicit path, everything's easy */
606 if ((*name
&& (name
[1] == ':')) || strchr( name
, '/' ) || strchr( name
, '\\' ))
608 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
612 /* Search in the specified path */
616 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
620 /* Try the path of the current executable (for Win32 search order) */
622 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
624 /* Try the current directory */
626 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
628 /* Try the Windows system directory */
630 if (DIR_TryPath( &DIR_System
, name
, full_name
))
633 /* Try the Windows directory */
635 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
638 /* Try the path of the current executable (for Win16 search order) */
640 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
642 /* Try all directories in path */
644 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
647 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
652 /***********************************************************************
653 * SearchPathA [KERNEL32.447]
655 * Searches for a specified file in the search path.
658 * path [I] Path to search
659 * name [I] Filename to search for.
660 * ext [I] File extension to append to file name. The first
661 * character must be a period. This parameter is
662 * specified only if the filename given does not
663 * contain an extension.
664 * buflen [I] size of buffer, in characters
665 * buffer [O] buffer for found filename
666 * lastpart [O] address of pointer to last used character in
667 * buffer (the final '\')
670 * Success: length of string copied into buffer, not including
671 * terminating null character. If the filename found is
672 * longer than the length of the buffer, the length of the
673 * filename is returned.
677 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
680 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
681 LPSTR buffer
, LPSTR
*lastpart
)
684 DOS_FULL_NAME full_name
;
686 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
688 SetLastError(ERROR_FILE_NOT_FOUND
);
691 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
692 res
= full_name
.long_name
+
693 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
694 while (*res
== '/') res
++;
697 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
698 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
699 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
701 TRACE("Returning %d\n", strlen(res
) + 3 );
702 return strlen(res
) + 3;
706 /***********************************************************************
707 * SearchPathW (KERNEL32.448)
709 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
710 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
714 DOS_FULL_NAME full_name
;
716 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
717 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
718 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
719 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
720 HeapFree( GetProcessHeap(), 0, extA
);
721 HeapFree( GetProcessHeap(), 0, nameA
);
722 HeapFree( GetProcessHeap(), 0, pathA
);
725 if (buflen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
))
726 buffer
[buflen
-1] = 0;
727 res
= full_name
.long_name
+
728 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
729 while (*res
== '/') res
++;
734 if (!MultiByteToWideChar( CP_ACP
, 0, res
, -1, buffer
+3, buflen
-3 ))
735 buffer
[buflen
-1] = 0;
737 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
740 for (p
= *lastpart
= buffer
; *p
; p
++)
741 if (*p
== '\\') *lastpart
= p
+ 1;
744 return strlen(res
) + 3;