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)
299 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
300 LPSECURITY_ATTRIBUTES lpsecattribs
)
302 DOS_FULL_NAME full_name
;
304 TRACE(file
, "(%s,%p)\n", path
, lpsecattribs
);
305 if (DOSFS_GetDevice( path
))
307 TRACE(file
, "cannot use device '%s'!\n",path
);
308 SetLastError( ERROR_ACCESS_DENIED
);
311 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
312 if ((mkdir( full_name
.long_name
, 0777 ) == -1) && (errno
!= EEXIST
))
314 WARN (file
, "Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
322 /***********************************************************************
323 * CreateDirectory32W (KERNEL32.42)
325 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
326 LPSECURITY_ATTRIBUTES lpsecattribs
)
328 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
329 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
330 HeapFree( GetProcessHeap(), 0, xpath
);
335 /***********************************************************************
336 * CreateDirectoryEx32A (KERNEL32.40)
338 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
339 LPSECURITY_ATTRIBUTES lpsecattribs
)
341 return CreateDirectoryA(path
,lpsecattribs
);
345 /***********************************************************************
346 * CreateDirectoryEx32W (KERNEL32.41)
348 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
349 LPSECURITY_ATTRIBUTES lpsecattribs
)
351 return CreateDirectoryW(path
,lpsecattribs
);
355 /***********************************************************************
356 * RemoveDirectory16 (KERNEL)
358 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
360 return (BOOL16
)RemoveDirectoryA( path
);
364 /***********************************************************************
365 * RemoveDirectory32A (KERNEL32.437)
367 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
369 DOS_FULL_NAME full_name
;
371 TRACE(file
, "'%s'\n", path
);
373 if (DOSFS_GetDevice( path
))
375 TRACE(file
, "cannot remove device '%s'!\n", path
);
376 SetLastError( ERROR_FILE_NOT_FOUND
);
379 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
380 if (rmdir( full_name
.long_name
) == -1)
389 /***********************************************************************
390 * RemoveDirectory32W (KERNEL32.438)
392 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
394 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
395 BOOL ret
= RemoveDirectoryA( xpath
);
396 HeapFree( GetProcessHeap(), 0, xpath
);
401 /***********************************************************************
404 * Helper function for DIR_SearchPath.
406 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
407 DOS_FULL_NAME
*full_name
)
409 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
410 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
412 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
413 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
415 SetLastError( ERROR_PATH_NOT_FOUND
);
418 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
419 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
420 p_s
, DRIVE_GetFlags( dir
->drive
) ))
422 strcpy( full_name
->long_name
, dir
->long_name
);
424 strcpy( full_name
->short_name
, dir
->short_name
);
430 /***********************************************************************
431 * DIR_TryEnvironmentPath
433 * Helper function for DIR_SearchPath.
435 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
437 LPSTR path
, next
, buffer
;
439 INT len
= strlen(name
);
440 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
442 if (!size
) return FALSE
;
443 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
444 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
449 while (*cur
== ';') cur
++;
451 next
= strchr( cur
, ';' );
452 if (next
) *next
++ = '\0';
453 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
455 strcpy( buffer
, cur
);
456 strcat( buffer
, "\\" );
457 strcat( buffer
, name
);
458 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
459 HeapFree( GetProcessHeap(), 0, buffer
);
463 HeapFree( GetProcessHeap(), 0, path
);
468 /***********************************************************************
471 * Helper function for DIR_SearchPath.
473 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
475 PDB
*pdb
= PROCESS_Current();
477 /* FIXME: for now, GetModuleFileName32A can't return more */
478 /* than OFS_MAXPATHNAME. This may change with Win32. */
480 char buffer
[OFS_MAXPATHNAME
];
483 if (pdb
->flags
& PDB32_WIN16_PROC
) {
484 if (!GetCurrentTask()) return FALSE
;
485 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
488 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
491 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
492 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
494 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
498 /***********************************************************************
501 * Implementation of SearchPath32A. 'win32' specifies whether the search
502 * order is Win16 (module path last) or Win32 (module path first).
504 * FIXME: should return long path names.
506 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
507 DOS_FULL_NAME
*full_name
, BOOL win32
)
514 /* First check the supplied parameters */
516 p
= strrchr( name
, '.' );
517 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
518 ext
= NULL
; /* Ignore the specified extension */
519 if ((*name
&& (name
[1] == ':')) ||
520 strchr( name
, '/' ) || strchr( name
, '\\' ))
521 path
= NULL
; /* Ignore path if name already contains a path */
522 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
525 if (ext
) len
+= strlen(ext
);
526 if (path
) len
+= strlen(path
) + 1;
528 /* Allocate a buffer for the file name and extension */
532 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
534 SetLastError( ERROR_OUTOFMEMORY
);
543 else strcpy( tmp
, name
);
544 if (ext
) strcat( tmp
, ext
);
548 /* If we have an explicit path, everything's easy */
550 if (path
|| (*name
&& (name
[1] == ':')) ||
551 strchr( name
, '/' ) || strchr( name
, '\\' ))
553 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
557 /* Try the path of the current executable (for Win32 search order) */
559 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
561 /* Try the current directory */
563 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
565 /* Try the Windows directory */
567 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
570 /* Try the Windows system directory */
572 if (DIR_TryPath( &DIR_System
, name
, full_name
))
575 /* Try the path of the current executable (for Win16 search order) */
577 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
579 /* Try all directories in path */
581 ret
= DIR_TryEnvironmentPath( name
, full_name
);
584 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
589 /***********************************************************************
590 * SearchPath32A [KERNEL32.447]
592 * Searches for a specified file in the search path.
595 * path [I] Path to search
596 * name [I] Filename to search for.
597 * ext [I] File extension to append to file name. The first
598 * character must be a period. This parameter is
599 * specified only if the filename given does not
600 * contain an extension.
601 * buflen [I] size of buffer, in characters
602 * buffer [O] buffer for found filename
603 * lastpart [O] address of pointer to last used character in
604 * buffer (the final '\')
607 * Success: length of string copied into buffer, not including
608 * terminating null character. If the filename found is
609 * longer than the length of the buffer, the length of the
610 * filename is returned.
614 * Should call SetLastError(but currently doesn't).
616 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
617 LPSTR buffer
, LPSTR
*lastpart
)
620 DOS_FULL_NAME full_name
;
622 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
623 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
624 res
= full_name
.long_name
+
625 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
626 while (*res
== '/') res
++;
629 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
630 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
631 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
633 TRACE(dosfs
, "Returning %d\n", (*res
? strlen(res
) + 2 : 3));
634 return *res
? strlen(res
) + 2 : 3;
638 /***********************************************************************
639 * SearchPath32W (KERNEL32.448)
641 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
642 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
646 DOS_FULL_NAME full_name
;
648 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
649 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
650 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
651 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
652 HeapFree( GetProcessHeap(), 0, extA
);
653 HeapFree( GetProcessHeap(), 0, nameA
);
654 HeapFree( GetProcessHeap(), 0, pathA
);
657 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
658 res
= full_name
.long_name
+
659 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
660 while (*res
== '/') res
++;
663 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
664 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
667 for (p
= *lastpart
= buffer
; *p
; p
++)
668 if (*p
== '\\') *lastpart
= p
+ 1;
671 return *res
? strlen(res
) + 2 : 3;