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 DOS drive for cwd %s; "
74 "starting in windows directory.\n", cwd
);
78 DRIVE_SetCurrentDrive( drive
);
79 DRIVE_Chdir( drive
, cwd
);
82 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
)) ||
83 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
)) ||
84 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
)))
86 PROFILE_UsageWineIni();
89 if (-1 == access( tmp_dir
.long_name
, W_OK
))
93 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
94 PROFILE_UsageWineIni();
97 MSG("Warning: Access to Temporary Directory failed (%s).\n",
103 drive
= DIR_Windows
.drive
;
104 DRIVE_SetCurrentDrive( drive
);
105 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
108 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
109 path
, sizeof(path
) );
111 /* Set the environment variables */
113 SetEnvironmentVariableA( "PATH", path
);
114 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
115 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
116 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
117 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
119 TRACE(dosfs
, "WindowsDir = %s (%s)\n",
120 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
121 TRACE(dosfs
, "SystemDir = %s (%s)\n",
122 DIR_System
.short_name
, DIR_System
.long_name
);
123 TRACE(dosfs
, "TempDir = %s (%s)\n",
124 tmp_dir
.short_name
, tmp_dir
.long_name
);
125 TRACE(dosfs
, "Path = %s\n", path
);
126 TRACE(dosfs
, "Cwd = %c:\\%s\n",
127 'A' + drive
, DRIVE_GetDosCwd( drive
) );
133 /***********************************************************************
134 * GetTempPath32A (KERNEL32.292)
136 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
139 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
140 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
141 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
143 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
152 /***********************************************************************
153 * GetTempPath32W (KERNEL32.293)
155 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
157 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
158 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
160 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
161 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
162 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
164 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
173 /***********************************************************************
174 * DIR_GetWindowsUnixDir
176 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
178 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
179 return strlen( DIR_Windows
.long_name
);
183 /***********************************************************************
184 * DIR_GetSystemUnixDir
186 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
188 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
189 return strlen( DIR_System
.long_name
);
193 /***********************************************************************
194 * GetTempDrive (KERNEL.92)
196 BYTE WINAPI
GetTempDrive( BYTE ignored
)
200 UINT len
= GetTempPathA( 0, NULL
);
202 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
203 return DRIVE_GetCurrentDrive() + 'A';
205 /* FIXME: apparently Windows does something with the ignored byte */
206 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
208 HeapFree( GetProcessHeap(), 0, buffer
);
213 UINT WINAPI
WIN16_GetTempDrive( BYTE ignored
)
215 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
219 * AH: ':' - yes, some kernel code even does stosw with
223 return MAKELONG( GetTempDrive(ignored
) | (':' << 8), 1 );
227 /***********************************************************************
228 * GetWindowsDirectory16 (KERNEL.134)
230 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
232 return (UINT16
)GetWindowsDirectoryA( path
, count
);
236 /***********************************************************************
237 * GetWindowsDirectory32A (KERNEL32.311)
239 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
241 if (path
) lstrcpynA( path
, DIR_Windows
.short_name
, count
);
242 return strlen( DIR_Windows
.short_name
);
246 /***********************************************************************
247 * GetWindowsDirectory32W (KERNEL32.312)
249 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
251 if (path
) lstrcpynAtoW( path
, DIR_Windows
.short_name
, count
);
252 return strlen( DIR_Windows
.short_name
);
256 /***********************************************************************
257 * GetSystemDirectory16 (KERNEL.135)
259 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
261 return (UINT16
)GetSystemDirectoryA( path
, count
);
265 /***********************************************************************
266 * GetSystemDirectory32A (KERNEL32.282)
268 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
270 if (path
) lstrcpynA( path
, DIR_System
.short_name
, count
);
271 return strlen( DIR_System
.short_name
);
275 /***********************************************************************
276 * GetSystemDirectory32W (KERNEL32.283)
278 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
280 if (path
) lstrcpynAtoW( path
, DIR_System
.short_name
, count
);
281 return strlen( DIR_System
.short_name
);
285 /***********************************************************************
286 * CreateDirectory16 (KERNEL.144)
288 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
290 TRACE(file
,"(%s,%p)\n", path
, dummy
);
291 return (BOOL16
)CreateDirectoryA( path
, NULL
);
295 /***********************************************************************
296 * CreateDirectory32A (KERNEL32.39)
298 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
299 LPSECURITY_ATTRIBUTES lpsecattribs
)
301 DOS_FULL_NAME full_name
;
303 TRACE(file
, "(%s,%p)\n", path
, lpsecattribs
);
304 if (DOSFS_GetDevice( path
))
306 TRACE(file
, "cannot use device '%s'!\n",path
);
307 SetLastError( ERROR_ACCESS_DENIED
);
310 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
311 if ((mkdir( full_name
.long_name
, 0777 ) == -1) && (errno
!= EEXIST
))
313 WARN (file
, "Errno %i trying to create directory %s.\n", errno
, full_name
.long_name
);
321 /***********************************************************************
322 * CreateDirectory32W (KERNEL32.42)
324 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
325 LPSECURITY_ATTRIBUTES lpsecattribs
)
327 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
328 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
329 HeapFree( GetProcessHeap(), 0, xpath
);
334 /***********************************************************************
335 * CreateDirectoryEx32A (KERNEL32.40)
337 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
338 LPSECURITY_ATTRIBUTES lpsecattribs
)
340 return CreateDirectoryA(path
,lpsecattribs
);
344 /***********************************************************************
345 * CreateDirectoryEx32W (KERNEL32.41)
347 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
348 LPSECURITY_ATTRIBUTES lpsecattribs
)
350 return CreateDirectoryW(path
,lpsecattribs
);
354 /***********************************************************************
355 * RemoveDirectory16 (KERNEL)
357 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
359 return (BOOL16
)RemoveDirectoryA( path
);
363 /***********************************************************************
364 * RemoveDirectory32A (KERNEL32.437)
366 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
368 DOS_FULL_NAME full_name
;
370 TRACE(file
, "'%s'\n", path
);
372 if (DOSFS_GetDevice( path
))
374 TRACE(file
, "cannot remove device '%s'!\n", path
);
375 SetLastError( ERROR_FILE_NOT_FOUND
);
378 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
379 if (rmdir( full_name
.long_name
) == -1)
388 /***********************************************************************
389 * RemoveDirectory32W (KERNEL32.438)
391 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
393 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
394 BOOL ret
= RemoveDirectoryA( xpath
);
395 HeapFree( GetProcessHeap(), 0, xpath
);
400 /***********************************************************************
403 * Helper function for DIR_SearchPath.
405 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
406 DOS_FULL_NAME
*full_name
)
408 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
409 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
411 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
412 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
414 SetLastError( ERROR_PATH_NOT_FOUND
);
417 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
418 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
419 p_s
, DRIVE_GetFlags( dir
->drive
) ))
421 strcpy( full_name
->long_name
, dir
->long_name
);
423 strcpy( full_name
->short_name
, dir
->short_name
);
429 /***********************************************************************
430 * DIR_TryEnvironmentPath
432 * Helper function for DIR_SearchPath.
434 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
436 LPSTR path
, next
, buffer
;
438 INT len
= strlen(name
);
439 DWORD size
= GetEnvironmentVariableA( "PATH", NULL
, 0 );
441 if (!size
) return FALSE
;
442 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
443 if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
448 while (*cur
== ';') cur
++;
450 next
= strchr( cur
, ';' );
451 if (next
) *next
++ = '\0';
452 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, strlen(cur
) + len
+ 2)))
454 strcpy( buffer
, cur
);
455 strcat( buffer
, "\\" );
456 strcat( buffer
, name
);
457 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
458 HeapFree( GetProcessHeap(), 0, buffer
);
462 HeapFree( GetProcessHeap(), 0, path
);
467 /***********************************************************************
470 * Helper function for DIR_SearchPath.
472 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
474 PDB
*pdb
= PROCESS_Current();
476 /* FIXME: for now, GetModuleFileName32A can't return more */
477 /* than OFS_MAXPATHNAME. This may change with Win32. */
479 char buffer
[OFS_MAXPATHNAME
];
482 if (pdb
->flags
& PDB32_WIN16_PROC
) {
483 if (!GetCurrentTask()) return FALSE
;
484 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
487 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
490 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
491 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
493 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
497 /***********************************************************************
500 * Implementation of SearchPath32A. 'win32' specifies whether the search
501 * order is Win16 (module path last) or Win32 (module path first).
503 * FIXME: should return long path names.
505 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
506 DOS_FULL_NAME
*full_name
, BOOL win32
)
513 /* First check the supplied parameters */
515 p
= strrchr( name
, '.' );
516 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
517 ext
= NULL
; /* Ignore the specified extension */
518 if ((*name
&& (name
[1] == ':')) ||
519 strchr( name
, '/' ) || strchr( name
, '\\' ))
520 path
= NULL
; /* Ignore path if name already contains a path */
521 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
524 if (ext
) len
+= strlen(ext
);
525 if (path
) len
+= strlen(path
) + 1;
527 /* Allocate a buffer for the file name and extension */
531 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
533 SetLastError( ERROR_OUTOFMEMORY
);
542 else strcpy( tmp
, name
);
543 if (ext
) strcat( tmp
, ext
);
547 /* If we have an explicit path, everything's easy */
549 if (path
|| (*name
&& (name
[1] == ':')) ||
550 strchr( name
, '/' ) || strchr( name
, '\\' ))
552 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
556 /* Try the path of the current executable (for Win32 search order) */
558 if (win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
560 /* Try the current directory */
562 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
564 /* Try the Windows directory */
566 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
569 /* Try the Windows system directory */
571 if (DIR_TryPath( &DIR_System
, name
, full_name
))
574 /* Try the path of the current executable (for Win16 search order) */
576 if (!win32
&& DIR_TryModulePath( name
, full_name
)) goto done
;
578 /* Try all directories in path */
580 ret
= DIR_TryEnvironmentPath( name
, full_name
);
583 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
588 /***********************************************************************
589 * SearchPath32A [KERNEL32.447]
591 * Searches for a specified file in the search path.
594 * path [I] Path to search
595 * name [I] Filename to search for.
596 * ext [I] File extension to append to file name. The first
597 * character must be a period. This parameter is
598 * specified only if the filename given does not
599 * contain an extension.
600 * buflen [I] size of buffer, in characters
601 * buffer [O] buffer for found filename
602 * lastpart [O] address of pointer to last used character in
603 * buffer (the final '\')
606 * Success: length of string copied into buffer, not including
607 * terminating null character. If the filename found is
608 * longer than the length of the buffer, the length of the
609 * filename is returned.
613 * Should call SetLastError(but currently doesn't).
615 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
616 LPSTR buffer
, LPSTR
*lastpart
)
619 DOS_FULL_NAME full_name
;
621 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
)) return 0;
622 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
623 res
= full_name
.long_name
+
624 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
625 while (*res
== '/') res
++;
628 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
629 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
630 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
632 TRACE(dosfs
, "Returning %d\n", (*res
? strlen(res
) + 2 : 3));
633 return *res
? strlen(res
) + 2 : 3;
637 /***********************************************************************
638 * SearchPath32W (KERNEL32.448)
640 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
641 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
645 DOS_FULL_NAME full_name
;
647 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
648 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
649 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
650 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
651 HeapFree( GetProcessHeap(), 0, extA
);
652 HeapFree( GetProcessHeap(), 0, nameA
);
653 HeapFree( GetProcessHeap(), 0, pathA
);
656 lstrcpynAtoW( buffer
, full_name
.short_name
, buflen
);
657 res
= full_name
.long_name
+
658 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
659 while (*res
== '/') res
++;
662 if (buflen
> 3) lstrcpynAtoW( buffer
+ 3, res
, buflen
- 3 );
663 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
666 for (p
= *lastpart
= buffer
; *p
; p
++)
667 if (*p
== '\\') *lastpart
= p
+ 1;
670 return *res
? strlen(res
) + 2 : 3;