2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
14 #include <sys/errno.h>
17 #include "wine/winbase16.h"
18 #include "wine/winestring.h"
28 static DOS_FULL_NAME DIR_Windows
;
29 static DOS_FULL_NAME DIR_System
;
32 /***********************************************************************
35 * Get a path name from the wine.ini file and make sure it is valid.
37 static int DIR_GetPath( const char *keyname
, const char *defval
,
38 DOS_FULL_NAME
*full_name
)
40 char path
[MAX_PATHNAME_LEN
];
41 BY_HANDLE_FILE_INFORMATION info
;
43 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
44 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
45 !FILE_Stat( full_name
->long_name
, &info
) ||
46 !(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
48 MSG("Invalid path '%s' for %s directory\n", path
, keyname
);
55 /***********************************************************************
60 char path
[MAX_PATHNAME_LEN
];
61 DOS_FULL_NAME tmp_dir
;
65 if (!getcwd( path
, MAX_PATHNAME_LEN
))
67 perror( "Could not get current directory" );
71 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
73 MSG("Warning: could not find wine.conf [Drive x] entry "
74 "for current working directory %s; "
75 "starting in windows directory.\n", cwd
);
79 DRIVE_SetCurrentDrive( drive
);
80 DRIVE_Chdir( drive
, cwd
);
83 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
84 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
85 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
87 PROFILE_UsageWineIni();
90 if (-1 == access( tmp_dir
.long_name
, W_OK
))
94 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
95 PROFILE_UsageWineIni();
98 MSG("Warning: Access to Temporary Directory failed (%s).\n",
104 drive
= DIR_Windows
.drive
;
105 DRIVE_SetCurrentDrive( drive
);
106 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
109 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
110 path
, sizeof(path
) );
112 /* Set the environment variables */
114 SetEnvironmentVariableA( "PATH", path
);
115 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
116 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
117 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
118 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
120 TRACE(dosfs
, "WindowsDir = %s (%s)\n",
121 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
122 TRACE(dosfs
, "SystemDir = %s (%s)\n",
123 DIR_System
.short_name
, DIR_System
.long_name
);
124 TRACE(dosfs
, "TempDir = %s (%s)\n",
125 tmp_dir
.short_name
, tmp_dir
.long_name
);
126 TRACE(dosfs
, "Path = %s\n", path
);
127 TRACE(dosfs
, "Cwd = %c:\\%s\n",
128 'A' + drive
, DRIVE_GetDosCwd( drive
) );
134 /***********************************************************************
135 * GetTempPath32A (KERNEL32.292)
137 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
140 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
141 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
142 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
144 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
153 /***********************************************************************
154 * GetTempPath32W (KERNEL32.293)
156 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
158 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
159 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
161 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
162 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
163 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
165 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
174 /***********************************************************************
175 * DIR_GetWindowsUnixDir
177 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
179 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
180 return strlen( DIR_Windows
.long_name
);
184 /***********************************************************************
185 * DIR_GetSystemUnixDir
187 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
189 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
190 return strlen( DIR_System
.long_name
);
194 /***********************************************************************
195 * GetTempDrive (KERNEL.92)
197 BYTE WINAPI
GetTempDrive( BYTE ignored
)
201 UINT len
= GetTempPathA( 0, NULL
);
203 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
204 return DRIVE_GetCurrentDrive() + 'A';
206 /* FIXME: apparently Windows does something with the ignored byte */
207 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
209 HeapFree( GetProcessHeap(), 0, buffer
);
214 UINT WINAPI
WIN16_GetTempDrive( BYTE ignored
)
216 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
220 * AH: ':' - yes, some kernel code even does stosw with
224 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
228 /***********************************************************************
229 * GetWindowsDirectory16 (KERNEL.134)
231 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
233 return (UINT16
)GetWindowsDirectoryA( path
, count
);
237 /***********************************************************************
238 * GetWindowsDirectory32A (KERNEL32.311)
240 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
242 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
243 return strlen( DIR_Windows
.short_name
);
247 /***********************************************************************
248 * GetWindowsDirectory32W (KERNEL32.312)
250 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
252 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
253 return strlen( DIR_Windows
.short_name
);
257 /***********************************************************************
258 * GetSystemDirectory16 (KERNEL.135)
260 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
262 return (UINT16
)GetSystemDirectoryA( path
, count
);
266 /***********************************************************************
267 * GetSystemDirectory32A (KERNEL32.282)
269 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
271 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
272 return strlen( DIR_System
.short_name
);
276 /***********************************************************************
277 * GetSystemDirectory32W (KERNEL32.283)
279 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
281 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
282 return strlen( DIR_System
.short_name
);
286 /***********************************************************************
287 * CreateDirectory16 (KERNEL.144)
289 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
291 TRACE(file
,"(%s,%p)\n", path
, dummy
);
292 return (BOOL16
)CreateDirectoryA( path
, NULL
);
296 /***********************************************************************
297 * CreateDirectory32A (KERNEL32.39)
301 * ERROR_DISK_FULL: on full disk
302 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
303 * ERROR_ACCESS_DENIED: on permission problems
304 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
306 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
307 LPSECURITY_ATTRIBUTES lpsecattribs
)
309 DOS_FULL_NAME full_name
;
311 TRACE(file
, "(%s,%p)\n", path
, lpsecattribs
);
312 if (DOSFS_GetDevice( path
))
314 TRACE(file
, "cannot use device '%s'!\n",path
);
315 SetLastError( ERROR_ACCESS_DENIED
);
318 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
319 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
320 WARN (file
, "Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
321 /* the FILE_SetDosError() generated error codes don't match the
322 * CreateDirectory ones for some errnos */
324 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
325 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
326 default: FILE_SetDosError();break;
334 /***********************************************************************
335 * CreateDirectory32W (KERNEL32.42)
337 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
338 LPSECURITY_ATTRIBUTES lpsecattribs
)
340 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
341 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
342 HeapFree( GetProcessHeap(), 0, xpath
);
347 /***********************************************************************
348 * CreateDirectoryEx32A (KERNEL32.40)
350 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
351 LPSECURITY_ATTRIBUTES lpsecattribs
)
353 return CreateDirectoryA(path
,lpsecattribs
);
357 /***********************************************************************
358 * CreateDirectoryEx32W (KERNEL32.41)
360 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
361 LPSECURITY_ATTRIBUTES lpsecattribs
)
363 return CreateDirectoryW(path
,lpsecattribs
);
367 /***********************************************************************
368 * RemoveDirectory16 (KERNEL)
370 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
372 return (BOOL16
)RemoveDirectoryA( path
);
376 /***********************************************************************
377 * RemoveDirectory32A (KERNEL32.437)
379 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
381 DOS_FULL_NAME full_name
;
383 TRACE(file
, "'%s'\n", path
);
385 if (DOSFS_GetDevice( path
))
387 TRACE(file
, "cannot remove device '%s'!\n", path
);
388 SetLastError( ERROR_FILE_NOT_FOUND
);
391 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
392 if (rmdir( full_name
.long_name
) == -1)
401 /***********************************************************************
402 * RemoveDirectory32W (KERNEL32.438)
404 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
406 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
407 BOOL ret
= RemoveDirectoryA( xpath
);
408 HeapFree( GetProcessHeap(), 0, xpath
);
413 /***********************************************************************
416 * Helper function for DIR_SearchPath.
418 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
419 DOS_FULL_NAME
*full_name
)
421 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
422 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
424 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
425 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
427 SetLastError( ERROR_PATH_NOT_FOUND
);
430 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
431 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
432 p_s
, DRIVE_GetFlags( dir
->drive
) ))
434 strcpy( full_name
->long_name
, dir
->long_name
);
436 strcpy( full_name
->short_name
, dir
->short_name
);
442 /***********************************************************************
443 * DIR_TryEnvironmentPath
445 * Helper function for DIR_SearchPath.
447 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
449 LPSTR path
, next
, buffer
;
451 INT len
= strlen(name
);
452 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
454 if (!size
) return FALSE
;
455 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
456 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
461 while (*cur
== ';') cur
++;
463 next
= strchr( cur
, ';' );
464 if (next
) *next
++ = '\0';
465 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
467 strcpy( buffer
, cur
);
468 strcat( buffer
, "\\" );
469 strcat( buffer
, name
);
470 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
471 HeapFree( GetProcessHeap(), 0, buffer
);
475 HeapFree( GetProcessHeap(), 0, path
);
480 /***********************************************************************
483 * Helper function for DIR_SearchPath.
485 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
487 PDB
*pdb
= PROCESS_Current();
489 /* FIXME: for now, GetModuleFileName32A can't return more */
490 /* than OFS_MAXPATHNAME. This may change with Win32. */
492 char buffer
[OFS_MAXPATHNAME
];
495 if (pdb
->flags
& PDB32_WIN16_PROC
) {
496 if (!GetCurrentTask()) return FALSE
;
497 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
500 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
503 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
504 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
506 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
510 /***********************************************************************
513 * Implementation of SearchPath32A. 'win32' specifies whether the search
514 * order is Win16 (module path last) or Win32 (module path first).
516 * FIXME: should return long path names.
518 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
519 DOS_FULL_NAME
*full_name
, BOOL win32
)
526 /* First check the supplied parameters */
528 p
= strrchr( name
, '.' );
529 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
530 ext
= NULL
; /* Ignore the specified extension */
531 if ((*name
&& (name
[1] == ':')) ||
532 strchr( name
, '/' ) || strchr( name
, '\\' ))
533 path
= NULL
; /* Ignore path if name already contains a path */
534 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
537 if (ext
) len
+= strlen(ext
);
538 if (path
) len
+= strlen(path
) + 1;
540 /* Allocate a buffer for the file name and extension */
544 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
546 SetLastError( ERROR_OUTOFMEMORY
);
555 else strcpy( tmp
, name
);
556 if (ext
) strcat( tmp
, ext
);
560 /* If we have an explicit path, everything's easy */
562 if (path
|| (*name
&& (name
[1] == ':')) ||
563 strchr( name
, '/' ) || strchr( name
, '\\' ))
565 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
569 /* Try the path of the current executable (for Win32 search order) */
571 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
573 /* Try the current directory */
575 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
577 /* Try the Windows directory */
579 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
582 /* Try the Windows system directory */
584 if (DIR_TryPath( &DIR_System
, name
, full_name
))
587 /* Try the path of the current executable (for Win16 search order) */
589 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
591 /* Try all directories in path */
593 ret
= DIR_TryEnvironmentPath( name
, full_name
);
596 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
601 /***********************************************************************
602 * SearchPath32A [KERNEL32.447]
604 * Searches for a specified file in the search path.
607 * path [I] Path to search
608 * name [I] Filename to search for.
609 * ext [I] File extension to append to file name. The first
610 * character must be a period. This parameter is
611 * specified only if the filename given does not
612 * contain an extension.
613 * buflen [I] size of buffer, in characters
614 * buffer [O] buffer for found filename
615 * lastpart [O] address of pointer to last used character in
616 * buffer (the final '\')
619 * Success: length of string copied into buffer, not including
620 * terminating null character. If the filename found is
621 * longer than the length of the buffer, the length of the
622 * filename is returned.
626 * Should call SetLastError(but currently doesn't).
628 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
629 LPSTR buffer
, LPSTR
*lastpart
)
632 DOS_FULL_NAME full_name
;
634 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
635 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
636 res
= full_name
.long_name
+
637 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
638 while (*res
== '/') res
++;
641 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
642 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
643 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
645 TRACE(dosfs
, "Returning %d\n", strlen(res
) + 3 );
646 return strlen(res
) + 3;
650 /***********************************************************************
651 * SearchPath32W (KERNEL32.448)
653 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
654 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
658 DOS_FULL_NAME full_name
;
660 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
661 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
662 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
663 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
664 HeapFree( GetProcessHeap(), 0, extA
);
665 HeapFree( GetProcessHeap(), 0, nameA
);
666 HeapFree( GetProcessHeap(), 0, pathA
);
669 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
670 res
= full_name
.long_name
+
671 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
672 while (*res
== '/') res
++;
675 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
676 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
679 for (p
= *lastpart
= buffer
; *p
; p
++)
680 if (*p
== '\\') *lastpart
= p
+ 1;
683 return strlen(res
) + 3;