2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
14 #include <sys/errno.h>
26 static DOS_FULL_NAME DIR_Windows
;
27 static DOS_FULL_NAME DIR_System
;
30 /***********************************************************************
33 * Get a path name from the wine.ini file and make sure it is valid.
35 static int DIR_GetPath( const char *keyname
, const char *defval
,
36 DOS_FULL_NAME
*full_name
)
38 char path
[MAX_PATHNAME_LEN
];
39 BY_HANDLE_FILE_INFORMATION info
;
41 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
42 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
43 !FILE_Stat( full_name
->long_name
, &info
) ||
44 !(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
46 MSG("Invalid path '%s' for %s directory\n", path
, keyname
);
53 /***********************************************************************
58 char path
[MAX_PATHNAME_LEN
];
59 DOS_FULL_NAME tmp_dir
;
63 if (!getcwd( path
, MAX_PATHNAME_LEN
))
65 perror( "Could not get current directory" );
69 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
71 MSG("Warning: could not find DOS drive for cwd %s; "
72 "starting in windows directory.\n", cwd
);
76 DRIVE_SetCurrentDrive( drive
);
77 DRIVE_Chdir( drive
, cwd
);
80 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
81 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
82 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
84 PROFILE_UsageWineIni();
87 if (-1 == access( tmp_dir
.long_name
, W_OK
))
91 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
92 PROFILE_UsageWineIni();
95 MSG("Warning: Access to Temporary Directory failed (%s).\n",
101 drive
= DIR_Windows
.drive
;
102 DRIVE_SetCurrentDrive( drive
);
103 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
106 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
107 path
, sizeof(path
) );
109 /* Set the environment variables */
111 SetEnvironmentVariable32A( "PATH", path
);
112 SetEnvironmentVariable32A( "COMSPEC", "c:\\command.com" );
113 SetEnvironmentVariable32A( "TEMP", tmp_dir
.short_name
);
114 SetEnvironmentVariable32A( "windir", DIR_Windows
.short_name
);
115 SetEnvironmentVariable32A( "winsysdir", DIR_System
.short_name
);
117 TRACE(dosfs
, "WindowsDir = %s (%s)\n",
118 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
119 TRACE(dosfs
, "SystemDir = %s (%s)\n",
120 DIR_System
.short_name
, DIR_System
.long_name
);
121 TRACE(dosfs
, "TempDir = %s (%s)\n",
122 tmp_dir
.short_name
, tmp_dir
.long_name
);
123 TRACE(dosfs
, "Path = %s\n", path
);
124 TRACE(dosfs
, "Cwd = %c:\\%s\n",
125 'A' + drive
, DRIVE_GetDosCwd( drive
) );
131 /***********************************************************************
132 * GetTempPath32A (KERNEL32.292)
134 UINT32 WINAPI
GetTempPath32A( UINT32 count
, LPSTR path
)
137 if (!(ret
= GetEnvironmentVariable32A( "TMP", path
, count
)))
138 if (!(ret
= GetEnvironmentVariable32A( "TEMP", path
, count
)))
139 if (!(ret
= GetCurrentDirectory32A( count
, path
)))
141 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
150 /***********************************************************************
151 * GetTempPath32W (KERNEL32.293)
153 UINT32 WINAPI
GetTempPath32W( UINT32 count
, LPWSTR path
)
155 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
156 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
158 if (!(ret
= GetEnvironmentVariable32W( tmp
, path
, count
)))
159 if (!(ret
= GetEnvironmentVariable32W( temp
, path
, count
)))
160 if (!(ret
= GetCurrentDirectory32W( count
, path
)))
162 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
171 /***********************************************************************
172 * DIR_GetWindowsUnixDir
174 UINT32
DIR_GetWindowsUnixDir( LPSTR path
, UINT32 count
)
176 if (path
) lstrcpyn32A( path
, DIR_Windows
.long_name
, count
);
177 return strlen( DIR_Windows
.long_name
);
181 /***********************************************************************
182 * DIR_GetSystemUnixDir
184 UINT32
DIR_GetSystemUnixDir( LPSTR path
, UINT32 count
)
186 if (path
) lstrcpyn32A( path
, DIR_System
.long_name
, count
);
187 return strlen( DIR_System
.long_name
);
191 /***********************************************************************
192 * GetTempDrive (KERNEL.92)
194 BYTE WINAPI
GetTempDrive( BYTE ignored
)
197 /* FIXME: apparently Windows does something with the ignored byte */
198 if (!GetTempPath32A( sizeof(buffer
), buffer
)) buffer
[0] = 'C';
199 return toupper(buffer
[0]);
203 UINT32 WINAPI
WIN16_GetTempDrive( BYTE ignored
)
205 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
209 * AH: ':' - yes, some kernel code even does stosw with
213 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
217 /***********************************************************************
218 * GetWindowsDirectory16 (KERNEL.134)
220 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
222 return (UINT16
)GetWindowsDirectory32A( path
, count
);
226 /***********************************************************************
227 * GetWindowsDirectory32A (KERNEL32.311)
229 UINT32 WINAPI
GetWindowsDirectory32A( LPSTR path
, UINT32 count
)
231 if (path
) lstrcpyn32A( path
, DIR_Windows
.short_name
, count
);
232 return strlen( DIR_Windows
.short_name
);
236 /***********************************************************************
237 * GetWindowsDirectory32W (KERNEL32.312)
239 UINT32 WINAPI
GetWindowsDirectory32W( LPWSTR path
, UINT32 count
)
241 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
242 return strlen( DIR_Windows
.short_name
);
246 /***********************************************************************
247 * GetSystemDirectory16 (KERNEL.135)
249 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
251 return (UINT16
)GetSystemDirectory32A( path
, count
);
255 /***********************************************************************
256 * GetSystemDirectory32A (KERNEL32.282)
258 UINT32 WINAPI
GetSystemDirectory32A( LPSTR path
, UINT32 count
)
260 if (path
) lstrcpyn32A( path
, DIR_System
.short_name
, count
);
261 return strlen( DIR_System
.short_name
);
265 /***********************************************************************
266 * GetSystemDirectory32W (KERNEL32.283)
268 UINT32 WINAPI
GetSystemDirectory32W( LPWSTR path
, UINT32 count
)
270 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
271 return strlen( DIR_System
.short_name
);
275 /***********************************************************************
276 * CreateDirectory16 (KERNEL.144)
278 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
280 TRACE(file
,"(%s,%p)\n", path
, dummy
);
281 return (BOOL16
)CreateDirectory32A( path
, NULL
);
285 /***********************************************************************
286 * CreateDirectory32A (KERNEL32.39)
288 BOOL32 WINAPI
CreateDirectory32A( LPCSTR path
,
289 LPSECURITY_ATTRIBUTES lpsecattribs
)
291 DOS_FULL_NAME full_name
;
293 TRACE(file
, "(%s,%p)\n", path
, lpsecattribs
);
294 if (DOSFS_GetDevice( path
))
296 TRACE(file
, "cannot use device '%s'!\n",path
);
297 SetLastError( ERROR_ACCESS_DENIED
);
300 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
301 if ((mkdir( full_name
.long_name
, 0777 ) == -1) && (errno
!= EEXIST
))
303 WARN (file
, "Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
311 /***********************************************************************
312 * CreateDirectory32W (KERNEL32.42)
314 BOOL32 WINAPI
CreateDirectory32W( LPCWSTR path
,
315 LPSECURITY_ATTRIBUTES lpsecattribs
)
317 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
318 BOOL32 ret
= CreateDirectory32A( xpath
, lpsecattribs
);
319 HeapFree( GetProcessHeap(), 0, xpath
);
324 /***********************************************************************
325 * CreateDirectoryEx32A (KERNEL32.40)
327 BOOL32 WINAPI
CreateDirectoryEx32A( LPCSTR
template, LPCSTR path
,
328 LPSECURITY_ATTRIBUTES lpsecattribs
)
330 return CreateDirectory32A(path
,lpsecattribs
);
334 /***********************************************************************
335 * CreateDirectoryEx32W (KERNEL32.41)
337 BOOL32 WINAPI
CreateDirectoryEx32W( LPCWSTR
template, LPCWSTR path
,
338 LPSECURITY_ATTRIBUTES lpsecattribs
)
340 return CreateDirectory32W(path
,lpsecattribs
);
344 /***********************************************************************
345 * RemoveDirectory16 (KERNEL)
347 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
349 return (BOOL16
)RemoveDirectory32A( path
);
353 /***********************************************************************
354 * RemoveDirectory32A (KERNEL32.437)
356 BOOL32 WINAPI
RemoveDirectory32A( LPCSTR path
)
358 DOS_FULL_NAME full_name
;
360 TRACE(file
, "'%s'\n", path
);
362 if (DOSFS_GetDevice( path
))
364 TRACE(file
, "cannot remove device '%s'!\n", path
);
365 SetLastError( ERROR_FILE_NOT_FOUND
);
368 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
369 if (rmdir( full_name
.long_name
) == -1)
378 /***********************************************************************
379 * RemoveDirectory32W (KERNEL32.438)
381 BOOL32 WINAPI
RemoveDirectory32W( LPCWSTR path
)
383 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
384 BOOL32 ret
= RemoveDirectory32A( xpath
);
385 HeapFree( GetProcessHeap(), 0, xpath
);
390 /***********************************************************************
393 * Helper function for DIR_SearchPath.
395 static BOOL32
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
396 DOS_FULL_NAME
*full_name
)
398 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
399 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
401 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
402 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
404 SetLastError( ERROR_PATH_NOT_FOUND
);
407 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
408 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
409 p_s
, DRIVE_GetFlags( dir
->drive
) ))
411 strcpy( full_name
->long_name
, dir
->long_name
);
413 strcpy( full_name
->short_name
, dir
->short_name
);
419 /***********************************************************************
420 * DIR_TryEnvironmentPath
422 * Helper function for DIR_SearchPath.
424 static BOOL32
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
426 LPSTR path
, next
, buffer
;
428 INT32 len
= strlen(name
);
429 DWORD size
= GetEnvironmentVariable32A( "PATH", NULL
, 0 );
431 if (!size
) return FALSE
;
432 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
433 if (!GetEnvironmentVariable32A( "PATH", path
, size
)) goto done
;
438 while (*cur
== ';') cur
++;
440 next
= strchr( cur
, ';' );
441 if (next
) *next
++ = '\0';
442 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
444 strcpy( buffer
, cur
);
445 strcat( buffer
, "\\" );
446 strcat( buffer
, name
);
447 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
448 HeapFree( GetProcessHeap(), 0, buffer
);
452 HeapFree( GetProcessHeap(), 0, path
);
457 /***********************************************************************
460 * Helper function for DIR_SearchPath.
462 static BOOL32
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
464 PDB32
*pdb
= PROCESS_Current();
466 /* FIXME: for now, GetModuleFileName32A can't return more */
467 /* than OFS_MAXPATHNAME. This may change with Win32. */
469 char buffer
[OFS_MAXPATHNAME
];
472 if (pdb
->flags
& PDB32_WIN16_PROC
) {
473 if (!GetCurrentTask()) return FALSE
;
474 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
477 if (!GetModuleFileName32A( 0, buffer
, sizeof(buffer
) ))
480 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
481 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
483 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
487 /***********************************************************************
490 * Implementation of SearchPath32A. 'win32' specifies whether the search
491 * order is Win16 (module path last) or Win32 (module path first).
493 * FIXME: should return long path names.
495 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
496 DOS_FULL_NAME
*full_name
, BOOL32 win32
)
503 /* First check the supplied parameters */
505 p
= strrchr( name
, '.' );
506 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
507 ext
= NULL
; /* Ignore the specified extension */
508 if ((*name
&& (name
[1] == ':')) ||
509 strchr( name
, '/' ) || strchr( name
, '\\' ))
510 path
= NULL
; /* Ignore path if name already contains a path */
511 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
514 if (ext
) len
+= strlen(ext
);
515 if (path
) len
+= strlen(path
) + 1;
517 /* Allocate a buffer for the file name and extension */
521 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
523 SetLastError( ERROR_OUTOFMEMORY
);
532 else strcpy( tmp
, name
);
533 if (ext
) strcat( tmp
, ext
);
537 /* If we have an explicit path, everything's easy */
539 if (path
|| (*name
&& (name
[1] == ':')) ||
540 strchr( name
, '/' ) || strchr( name
, '\\' ))
542 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
546 /* Try the path of the current executable (for Win32 search order) */
548 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
550 /* Try the current directory */
552 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
554 /* Try the Windows directory */
556 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
559 /* Try the Windows system directory */
561 if (DIR_TryPath( &DIR_System
, name
, full_name
))
564 /* Try the path of the current executable (for Win16 search order) */
566 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
568 /* Try all directories in path */
570 ret
= DIR_TryEnvironmentPath( name
, full_name
);
573 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
578 /***********************************************************************
579 * SearchPath32A [KERNEL32.447]
581 * Searches for a specified file in the search path.
584 * path [I] Path to search
585 * name [I] Filename to search for.
586 * ext [I] File extension to append to file name. The first
587 * character must be a period. This parameter is
588 * specified only if the filename given does not
589 * contain an extension.
590 * buflen [I] size of buffer, in characters
591 * buffer [O] buffer for found filename
592 * lastpart [O] address of pointer to last used character in
593 * buffer (the final '\')
596 * Success: length of string copied into buffer, not including
597 * terminating null character. If the filename found is
598 * longer than the length of the buffer, the length of the
599 * filename is returned.
603 * Should call SetLastError(but currently doesn't).
605 DWORD WINAPI
SearchPath32A( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
606 LPSTR buffer
, LPSTR
*lastpart
)
609 DOS_FULL_NAME full_name
;
611 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
612 lstrcpyn32A( buffer
, full_name
.short_name
, buflen
);
613 res
= full_name
.long_name
+
614 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
615 while (*res
== '/') res
++;
618 if (buflen
> 3) lstrcpyn32A( buffer
+ 3, res
, buflen
- 3 );
619 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
620 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
622 TRACE(dosfs
, "Returning %d\n", (*res
? strlen(res
) + 2 : 3));
623 return *res
? strlen(res
) + 2 : 3;
627 /***********************************************************************
628 * SearchPath32W (KERNEL32.448)
630 DWORD WINAPI
SearchPath32W( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
631 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
635 DOS_FULL_NAME full_name
;
637 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
638 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
639 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
640 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
641 HeapFree( GetProcessHeap(), 0, extA
);
642 HeapFree( GetProcessHeap(), 0, nameA
);
643 HeapFree( GetProcessHeap(), 0, pathA
);
646 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
647 res
= full_name
.long_name
+
648 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
649 while (*res
== '/') res
++;
652 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
653 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
656 for (p
= *lastpart
= buffer
; *p
; p
++)
657 if (*p
== '\\') *lastpart
= p
+ 1;
660 return *res
? strlen(res
) + 2 : 3;