2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
25 static DOS_FULL_NAME DIR_Windows
;
26 static DOS_FULL_NAME DIR_System
;
29 /***********************************************************************
32 * Get a path name from the wine.ini file and make sure it is valid.
34 static int DIR_GetPath( const char *keyname
, const char *defval
,
35 DOS_FULL_NAME
*full_name
)
37 char path
[MAX_PATHNAME_LEN
];
38 BY_HANDLE_FILE_INFORMATION info
;
40 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
41 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
42 !FILE_Stat( full_name
->long_name
, &info
) ||
43 !(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
45 MSG("Invalid path '%s' for %s directory\n", path
, keyname
);
52 /***********************************************************************
57 char path
[MAX_PATHNAME_LEN
];
58 DOS_FULL_NAME tmp_dir
;
62 if (!getcwd( path
, MAX_PATHNAME_LEN
))
64 perror( "Could not get current directory" );
68 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
70 MSG("Warning: could not find DOS drive for cwd %s; "
71 "starting in windows directory.\n", cwd
);
75 DRIVE_SetCurrentDrive( drive
);
76 DRIVE_Chdir( drive
, cwd
);
79 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)))
81 if (!(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)))
83 if (!(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
85 if (-1 == access( tmp_dir
.long_name
, W_OK
))
88 MSG("Warning: The Temporary Directory (as specified in wine.conf) is NOT writeable. Please check your configuration.\n");
90 MSG("Warning: Access to Temporary Directory failed (%s).\n",
96 drive
= DIR_Windows
.drive
;
97 DRIVE_SetCurrentDrive( drive
);
98 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
101 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
102 path
, sizeof(path
) );
104 /* Set the environment variables */
106 SetEnvironmentVariable32A( "PATH", path
);
107 SetEnvironmentVariable32A( "TEMP", tmp_dir
.short_name
);
108 SetEnvironmentVariable32A( "windir", DIR_Windows
.short_name
);
109 SetEnvironmentVariable32A( "winsysdir", DIR_System
.short_name
);
111 TRACE(dosfs
, "WindowsDir = %s (%s)\n",
112 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
113 TRACE(dosfs
, "SystemDir = %s (%s)\n",
114 DIR_System
.short_name
, DIR_System
.long_name
);
115 TRACE(dosfs
, "TempDir = %s (%s)\n",
116 tmp_dir
.short_name
, tmp_dir
.long_name
);
117 TRACE(dosfs
, "Path = %s\n", path
);
118 TRACE(dosfs
, "Cwd = %c:\\%s\n",
119 'A' + drive
, DRIVE_GetDosCwd( drive
) );
125 /***********************************************************************
126 * GetTempPath32A (KERNEL32.292)
128 UINT32 WINAPI
GetTempPath32A( UINT32 count
, LPSTR path
)
131 if (!(ret
= GetEnvironmentVariable32A( "TMP", path
, count
)))
132 if (!(ret
= GetEnvironmentVariable32A( "TEMP", path
, count
)))
133 if (!(ret
= GetCurrentDirectory32A( count
, path
)))
135 if ((ret
< count
- 1) && (path
[ret
-1] != '\\'))
144 /***********************************************************************
145 * GetTempPath32W (KERNEL32.293)
147 UINT32 WINAPI
GetTempPath32W( UINT32 count
, LPWSTR path
)
149 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
150 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
152 if (!(ret
= GetEnvironmentVariable32W( tmp
, path
, count
)))
153 if (!(ret
= GetEnvironmentVariable32W( temp
, path
, count
)))
154 if (!(ret
= GetCurrentDirectory32W( count
, path
)))
156 if ((ret
< count
- 1) && (path
[ret
-1] != '\\'))
165 /***********************************************************************
166 * DIR_GetWindowsUnixDir
168 UINT32
DIR_GetWindowsUnixDir( LPSTR path
, UINT32 count
)
170 if (path
) lstrcpyn32A( path
, DIR_Windows
.long_name
, count
);
171 return strlen( DIR_Windows
.long_name
);
175 /***********************************************************************
176 * DIR_GetSystemUnixDir
178 UINT32
DIR_GetSystemUnixDir( LPSTR path
, UINT32 count
)
180 if (path
) lstrcpyn32A( path
, DIR_System
.long_name
, count
);
181 return strlen( DIR_System
.long_name
);
185 /***********************************************************************
186 * GetTempDrive (KERNEL.92)
188 BYTE WINAPI
GetTempDrive( BYTE ignored
)
191 /* FIXME: apparently Windows does something with the ignored byte */
192 if (!GetTempPath32A( sizeof(buffer
), buffer
)) buffer
[0] = 'C';
193 return toupper(buffer
[0]);
197 UINT32 WINAPI
WIN16_GetTempDrive( BYTE ignored
)
199 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
203 * AH: ':' - yes, some kernel code even does stosw with
207 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
211 /***********************************************************************
212 * GetWindowsDirectory16 (KERNEL.134)
214 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
216 return (UINT16
)GetWindowsDirectory32A( path
, count
);
220 /***********************************************************************
221 * GetWindowsDirectory32A (KERNEL32.311)
223 UINT32 WINAPI
GetWindowsDirectory32A( LPSTR path
, UINT32 count
)
225 if (path
) lstrcpyn32A( path
, DIR_Windows
.short_name
, count
);
226 return strlen( DIR_Windows
.short_name
);
230 /***********************************************************************
231 * GetWindowsDirectory32W (KERNEL32.312)
233 UINT32 WINAPI
GetWindowsDirectory32W( LPWSTR path
, UINT32 count
)
235 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
236 return strlen( DIR_Windows
.short_name
);
240 /***********************************************************************
241 * GetSystemDirectory16 (KERNEL.135)
243 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
245 return (UINT16
)GetSystemDirectory32A( path
, count
);
249 /***********************************************************************
250 * GetSystemDirectory32A (KERNEL32.282)
252 UINT32 WINAPI
GetSystemDirectory32A( LPSTR path
, UINT32 count
)
254 if (path
) lstrcpyn32A( path
, DIR_System
.short_name
, count
);
255 return strlen( DIR_System
.short_name
);
259 /***********************************************************************
260 * GetSystemDirectory32W (KERNEL32.283)
262 UINT32 WINAPI
GetSystemDirectory32W( LPWSTR path
, UINT32 count
)
264 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
265 return strlen( DIR_System
.short_name
);
269 /***********************************************************************
270 * CreateDirectory16 (KERNEL.144)
272 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
274 TRACE(file
,"(%s,%p)\n", path
, dummy
);
275 return (BOOL16
)CreateDirectory32A( path
, NULL
);
279 /***********************************************************************
280 * CreateDirectory32A (KERNEL32.39)
282 BOOL32 WINAPI
CreateDirectory32A( LPCSTR path
,
283 LPSECURITY_ATTRIBUTES lpsecattribs
)
285 DOS_FULL_NAME full_name
;
287 TRACE(file
, "(%s,%p)\n", path
, lpsecattribs
);
288 if (DOSFS_GetDevice( path
))
290 TRACE(file
, "cannot use device '%s'!\n",path
);
291 DOS_ERROR( ER_AccessDenied
, EC_AccessDenied
, SA_Abort
, EL_Disk
);
294 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
295 if ((mkdir( full_name
.long_name
, 0777 ) == -1) && (errno
!= EEXIST
))
297 WARN (file
, "Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
305 /***********************************************************************
306 * CreateDirectory32W (KERNEL32.42)
308 BOOL32 WINAPI
CreateDirectory32W( LPCWSTR path
,
309 LPSECURITY_ATTRIBUTES lpsecattribs
)
311 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
312 BOOL32 ret
= CreateDirectory32A( xpath
, lpsecattribs
);
313 HeapFree( GetProcessHeap(), 0, xpath
);
318 /***********************************************************************
319 * CreateDirectoryEx32A (KERNEL32.40)
321 BOOL32 WINAPI
CreateDirectoryEx32A( LPCSTR
template, LPCSTR path
,
322 LPSECURITY_ATTRIBUTES lpsecattribs
)
324 return CreateDirectory32A(path
,lpsecattribs
);
328 /***********************************************************************
329 * CreateDirectoryEx32W (KERNEL32.41)
331 BOOL32 WINAPI
CreateDirectoryEx32W( LPCWSTR
template, LPCWSTR path
,
332 LPSECURITY_ATTRIBUTES lpsecattribs
)
334 return CreateDirectory32W(path
,lpsecattribs
);
338 /***********************************************************************
339 * RemoveDirectory16 (KERNEL)
341 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
343 return (BOOL16
)RemoveDirectory32A( path
);
347 /***********************************************************************
348 * RemoveDirectory32A (KERNEL32.437)
350 BOOL32 WINAPI
RemoveDirectory32A( LPCSTR path
)
352 DOS_FULL_NAME full_name
;
354 TRACE(file
, "'%s'\n", path
);
356 if (DOSFS_GetDevice( path
))
358 TRACE(file
, "cannot remove device '%s'!\n", path
);
359 DOS_ERROR( ER_FileNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
362 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
363 if (rmdir( full_name
.long_name
) == -1)
372 /***********************************************************************
373 * RemoveDirectory32W (KERNEL32.438)
375 BOOL32 WINAPI
RemoveDirectory32W( LPCWSTR path
)
377 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
378 BOOL32 ret
= RemoveDirectory32A( xpath
);
379 HeapFree( GetProcessHeap(), 0, xpath
);
384 /***********************************************************************
387 * Helper function for DIR_SearchPath.
389 static BOOL32
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
390 DOS_FULL_NAME
*full_name
)
392 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
393 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
395 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
396 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
398 DOS_ERROR( ER_PathNotFound
, EC_NotFound
, SA_Abort
, EL_Disk
);
401 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
402 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
403 p_s
, DRIVE_GetFlags( dir
->drive
) ))
405 strcpy( full_name
->long_name
, dir
->long_name
);
407 strcpy( full_name
->short_name
, dir
->short_name
);
413 /***********************************************************************
414 * DIR_TryEnvironmentPath
416 * Helper function for DIR_SearchPath.
418 static BOOL32
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
420 LPSTR path
, next
, buffer
;
422 INT32 len
= strlen(name
);
423 DWORD size
= GetEnvironmentVariable32A( "PATH", NULL
, 0 );
425 if (!size
) return FALSE
;
426 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
427 if (!GetEnvironmentVariable32A( "PATH", path
, size
)) goto done
;
432 while (*cur
== ';') cur
++;
434 next
= strchr( cur
, ';' );
435 if (next
) *next
++ = '\0';
436 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
438 strcpy( buffer
, cur
);
439 strcat( buffer
, "\\" );
440 strcat( buffer
, name
);
441 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
442 HeapFree( GetProcessHeap(), 0, buffer
);
446 HeapFree( GetProcessHeap(), 0, path
);
451 /***********************************************************************
454 * Helper function for DIR_SearchPath.
456 static BOOL32
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
458 PDB32
*pdb
= PROCESS_Current();
460 /* FIXME: for now, GetModuleFileName32A can't return more */
461 /* than OFS_MAXPATHNAME. This may change with Win32. */
463 char buffer
[OFS_MAXPATHNAME
];
466 if (pdb
->flags
& PDB32_WIN16_PROC
) {
467 if (!GetCurrentTask()) return FALSE
;
468 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
471 if (!GetModuleFileName32A( 0, buffer
, sizeof(buffer
) ))
474 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
475 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
477 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
481 /***********************************************************************
484 * Implementation of SearchPath32A. 'win32' specifies whether the search
485 * order is Win16 (module path last) or Win32 (module path first).
487 * FIXME: should return long path names.
489 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
490 DOS_FULL_NAME
*full_name
, BOOL32 win32
)
497 /* First check the supplied parameters */
499 p
= strrchr( name
, '.' );
500 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
501 ext
= NULL
; /* Ignore the specified extension */
502 if ((*name
&& (name
[1] == ':')) ||
503 strchr( name
, '/' ) || strchr( name
, '\\' ))
504 path
= NULL
; /* Ignore path if name already contains a path */
505 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
508 if (ext
) len
+= strlen(ext
);
509 if (path
) len
+= strlen(path
) + 1;
511 /* Allocate a buffer for the file name and extension */
515 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
517 SetLastError( ERROR_OUTOFMEMORY
);
526 else strcpy( tmp
, name
);
527 if (ext
) strcat( tmp
, ext
);
531 /* If we have an explicit path, everything's easy */
533 if (path
|| (*name
&& (name
[1] == ':')) ||
534 strchr( name
, '/' ) || strchr( name
, '\\' ))
536 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
540 /* Try the path of the current executable (for Win32 search order) */
542 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
544 /* Try the current directory */
546 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
548 /* Try the Windows directory */
550 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
553 /* Try the Windows system directory */
555 if (DIR_TryPath( &DIR_System
, name
, full_name
))
558 /* Try the path of the current executable (for Win16 search order) */
560 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
562 /* Try all directories in path */
564 ret
= DIR_TryEnvironmentPath( name
, full_name
);
567 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
572 /***********************************************************************
573 * SearchPath32A [KERNEL32.447]
575 * Searches for a specified file in the search path.
578 * path [I] Path to search
579 * name [I] Filename to search for.
580 * ext [I] File extension to append to file name. The first
581 * character must be a period. This parameter is
582 * specified only if the filename given does not
583 * contain an extension.
584 * buflen [I] size of buffer, in characters
585 * buffer [O] buffer for found filename
586 * lastpart [O] address of pointer to last used character in
587 * buffer (the final '\')
590 * Success: length of string copied into buffer, not including
591 * terminating null character. If the filename found is
592 * longer than the length of the buffer, the length of the
593 * filename is returned.
597 * Should call SetLastError(but currently doesn't).
599 DWORD WINAPI
SearchPath32A( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
600 LPSTR buffer
, LPSTR
*lastpart
)
603 DOS_FULL_NAME full_name
;
605 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
606 lstrcpyn32A( buffer
, full_name
.short_name
, buflen
);
607 res
= full_name
.long_name
+
608 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
609 while (*res
== '/') res
++;
612 if (buflen
> 3) lstrcpyn32A( buffer
+ 3, res
, buflen
- 3 );
613 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
614 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
616 TRACE(dosfs
, "Returning %d\n", (*res
? strlen(res
) + 2 : 3));
617 return *res
? strlen(res
) + 2 : 3;
621 /***********************************************************************
622 * SearchPath32W (KERNEL32.448)
624 DWORD WINAPI
SearchPath32W( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
625 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
629 DOS_FULL_NAME full_name
;
631 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
632 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
633 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
634 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
635 HeapFree( GetProcessHeap(), 0, extA
);
636 HeapFree( GetProcessHeap(), 0, nameA
);
637 HeapFree( GetProcessHeap(), 0, pathA
);
640 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
641 res
= full_name
.long_name
+
642 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
643 while (*res
== '/') res
++;
646 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
647 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
650 for (p
= *lastpart
= buffer
; *p
; p
++)
651 if (*p
== '\\') *lastpart
= p
+ 1;
654 return *res
? strlen(res
) + 2 : 3;