2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
33 #ifdef HAVE_SYS_ERRNO_H
34 #include <sys/errno.h>
38 #include "wine/winbase16.h"
41 #include "wine/winuser16.h"
44 #include "wine/unicode.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
52 WINE_DECLARE_DEBUG_CHANNEL(file
);
54 static DOS_FULL_NAME DIR_Windows
;
55 static DOS_FULL_NAME DIR_System
;
57 static const WCHAR wineW
[] = {'w','i','n','e',0};
59 /***********************************************************************
62 inline static int FILE_contains_pathW (LPCWSTR name
)
64 return ((*name
&& (name
[1] == ':')) ||
65 strchrW (name
, '/') || strchrW (name
, '\\'));
68 /***********************************************************************
71 * Get a path name from the wine.ini file and make sure it is valid.
73 static int DIR_GetPath( LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
74 LPWSTR longname
, INT longname_len
, BOOL warn
)
76 WCHAR path
[MAX_PATHNAME_LEN
];
77 BY_HANDLE_FILE_INFORMATION info
;
78 const char *mess
= "does not exist";
80 PROFILE_GetWineIniString( wineW
, keyname
, defval
, path
, MAX_PATHNAME_LEN
);
82 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
83 (!FILE_Stat( full_name
->long_name
, &info
, NULL
) && (mess
=strerror(errno
)))||
84 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
85 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
88 MESSAGE("Invalid path %s for %s directory: %s\n",
89 debugstr_w(path
), debugstr_w(keyname
), mess
);
96 /***********************************************************************
101 char path
[MAX_PATHNAME_LEN
];
102 WCHAR longpath
[MAX_PATHNAME_LEN
];
103 DOS_FULL_NAME tmp_dir
, profile_dir
;
106 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
107 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
108 static const WCHAR tempW
[] = {'t','e','m','p',0};
109 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
110 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
111 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
112 static const WCHAR pathW
[] = {'p','a','t','h',0};
113 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
114 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
115 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
116 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
117 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
118 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
119 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
120 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
121 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
122 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
123 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
124 static const WCHAR empty_strW
[] = { 0 };
126 if (!getcwd( path
, MAX_PATHNAME_LEN
))
128 perror( "Could not get current directory" );
132 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
134 MESSAGE("Warning: could not find wine config [Drive x] entry "
135 "for current working directory %s; "
136 "starting in windows directory.\n", cwd
);
140 WCHAR szdrive
[3]={drive
+'A',':',0};
141 MultiByteToWideChar(DRIVE_GetCodepage(drive
), 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
142 DRIVE_SetCurrentDrive( drive
);
143 DRIVE_Chdir( drive
, longpath
);
144 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
145 chdir("/"); /* change to root directory so as not to lock cdroms */
148 if (!(DIR_GetPath( windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
149 !(DIR_GetPath( systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
150 !(DIR_GetPath( tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
152 PROFILE_UsageWineIni();
155 if (-1 == access( tmp_dir
.long_name
, W_OK
))
159 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
160 PROFILE_UsageWineIni();
163 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
164 tmp_dir
.long_name
, strerror(errno
));
169 drive
= DIR_Windows
.drive
;
170 DRIVE_SetCurrentDrive( drive
);
171 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
174 /* Set the environment variables */
176 /* set COMSPEC only if it doesn't exist already */
177 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
179 strcpyW( longpath
, DIR_System
.short_name
);
180 strcatW( longpath
, wcmdW
);
181 SetEnvironmentVariableW( comspecW
, longpath
);
184 /* set PATH only if not set already */
185 if (!GetEnvironmentVariableW( path_capsW
, longpath
, MAX_PATHNAME_LEN
))
187 PROFILE_GetWineIniString(wineW
, pathW
, path_dirW
, longpath
, MAX_PATHNAME_LEN
);
188 if (strchrW(longpath
, '/'))
190 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
191 PROFILE_UsageWineIni();
194 SetEnvironmentVariableW( path_capsW
, longpath
);
197 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
198 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
199 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
200 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
202 TRACE("WindowsDir = %s (%s)\n",
203 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
204 TRACE("SystemDir = %s (%s)\n",
205 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
206 TRACE("TempDir = %s (%s)\n",
207 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
208 TRACE("Path = %s\n", debugstr_w(longpath
) );
209 TRACE("Cwd = %c:\\%s\n",
210 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
212 if (DIR_GetPath( profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
214 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
215 SetEnvironmentVariableW( userprofileW
, longpath
);
218 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
219 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
225 /***********************************************************************
226 * GetTempPathA (KERNEL32.@)
228 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
230 WCHAR pathW
[MAX_PATH
];
233 ret
= GetTempPathW(MAX_PATH
, pathW
);
240 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
244 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
247 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
248 ret
--; /* length without 0 */
254 /***********************************************************************
255 * GetTempPathW (KERNEL32.@)
257 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
259 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
260 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
261 WCHAR tmp_path
[MAX_PATH
];
264 TRACE("%u,%p\n", count
, path
);
266 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
267 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
268 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
273 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
277 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
280 if (ret
> MAX_PATH
- 2)
282 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
286 if (tmp_path
[ret
-1] != '\\')
288 tmp_path
[ret
++] = '\\';
289 tmp_path
[ret
] = '\0';
292 ret
++; /* add space for terminating 0 */
296 lstrcpynW(path
, tmp_path
, count
);
298 ret
--; /* return length without 0 */
300 path
[0] = 0; /* avoid returning ambiguous "X:" */
303 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
308 /***********************************************************************
309 * DIR_GetWindowsUnixDir
311 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
313 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
314 return strlen( DIR_Windows
.long_name
);
318 /***********************************************************************
319 * DIR_GetSystemUnixDir
321 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
323 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
324 return strlen( DIR_System
.long_name
);
328 /***********************************************************************
329 * GetTempDrive (KERNEL.92)
330 * A closer look at krnl386.exe shows what the SDK doesn't mention:
334 * AH: ':' - yes, some kernel code even does stosw with
338 UINT WINAPI
GetTempDrive( BYTE ignored
)
342 UINT len
= GetTempPathA( 0, NULL
);
344 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
345 ret
= DRIVE_GetCurrentDrive() + 'A';
348 /* FIXME: apparently Windows does something with the ignored byte */
349 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
350 ret
= toupper(buffer
[0]);
351 HeapFree( GetProcessHeap(), 0, buffer
);
353 return MAKELONG( ret
| (':' << 8), 1 );
357 /***********************************************************************
358 * GetWindowsDirectory (KERNEL.134)
360 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
362 return (UINT16
)GetWindowsDirectoryA( path
, count
);
366 /***********************************************************************
367 * GetWindowsDirectoryW (KERNEL32.@)
369 * See comment for GetWindowsDirectoryA.
371 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
373 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
374 if (path
&& count
>= len
)
376 strcpyW( path
, DIR_Windows
.short_name
);
383 /***********************************************************************
384 * GetWindowsDirectoryA (KERNEL32.@)
387 * If buffer is large enough to hold full path and terminating '\0' character
388 * function copies path to buffer and returns length of the path without '\0'.
389 * Otherwise function returns required size including '\0' character and
390 * does not touch the buffer.
392 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
394 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
395 if (path
&& count
>= len
)
397 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
404 /***********************************************************************
405 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
407 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
409 return GetWindowsDirectoryA( path
, count
);
413 /***********************************************************************
414 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
416 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
418 return GetWindowsDirectoryW( path
, count
);
422 /***********************************************************************
423 * GetSystemDirectory (KERNEL.135)
425 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
427 return (UINT16
)GetSystemDirectoryA( path
, count
);
431 /***********************************************************************
432 * GetSystemDirectoryW (KERNEL32.@)
434 * See comment for GetWindowsDirectoryA.
436 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
438 UINT len
= strlenW( DIR_System
.short_name
) + 1;
439 if (path
&& count
>= len
)
441 strcpyW( path
, DIR_System
.short_name
);
448 /***********************************************************************
449 * GetSystemDirectoryA (KERNEL32.@)
451 * See comment for GetWindowsDirectoryA.
453 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
455 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
456 if (path
&& count
>= len
)
458 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
465 /***********************************************************************
466 * CreateDirectory (KERNEL.144)
468 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
470 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
471 return (BOOL16
)CreateDirectoryA( path
, NULL
);
475 /***********************************************************************
476 * CreateDirectoryW (KERNEL32.@)
480 * ERROR_DISK_FULL: on full disk
481 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
482 * ERROR_ACCESS_DENIED: on permission problems
483 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
485 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
486 LPSECURITY_ATTRIBUTES lpsecattribs
)
488 DOS_FULL_NAME full_name
;
492 SetLastError(ERROR_PATH_NOT_FOUND
);
496 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
498 if (DOSFS_GetDevice( path
))
500 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
501 SetLastError( ERROR_ACCESS_DENIED
);
504 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
505 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
506 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
507 /* the FILE_SetDosError() generated error codes don't match the
508 * CreateDirectory ones for some errnos */
512 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
513 SetLastError(ERROR_ACCESS_DENIED
);
515 SetLastError(ERROR_ALREADY_EXISTS
);
518 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
519 default: FILE_SetDosError();break;
527 /***********************************************************************
528 * CreateDirectoryA (KERNEL32.@)
530 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
531 LPSECURITY_ATTRIBUTES lpsecattribs
)
533 UNICODE_STRING pathW
;
538 SetLastError(ERROR_PATH_NOT_FOUND
);
542 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
544 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
545 RtlFreeUnicodeString(&pathW
);
548 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
553 /***********************************************************************
554 * CreateDirectoryExA (KERNEL32.@)
556 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
557 LPSECURITY_ATTRIBUTES lpsecattribs
)
559 return CreateDirectoryA(path
,lpsecattribs
);
563 /***********************************************************************
564 * CreateDirectoryExW (KERNEL32.@)
566 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
567 LPSECURITY_ATTRIBUTES lpsecattribs
)
569 return CreateDirectoryW(path
,lpsecattribs
);
573 /***********************************************************************
574 * RemoveDirectory (KERNEL.145)
576 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
578 return (BOOL16
)RemoveDirectoryA( path
);
582 /***********************************************************************
583 * RemoveDirectoryW (KERNEL32.@)
585 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
587 DOS_FULL_NAME full_name
;
591 SetLastError(ERROR_INVALID_PARAMETER
);
595 TRACE_(file
)("%s\n", debugstr_w(path
));
597 if (DOSFS_GetDevice( path
))
599 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
600 SetLastError( ERROR_FILE_NOT_FOUND
);
603 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
604 if (rmdir( full_name
.long_name
) == -1)
613 /***********************************************************************
614 * RemoveDirectoryA (KERNEL32.@)
616 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
618 UNICODE_STRING pathW
;
623 SetLastError(ERROR_INVALID_PARAMETER
);
627 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
629 ret
= RemoveDirectoryW(pathW
.Buffer
);
630 RtlFreeUnicodeString(&pathW
);
633 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
638 /***********************************************************************
641 * Helper function for DIR_SearchPath.
643 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
644 DOS_FULL_NAME
*full_name
)
646 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
647 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
649 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
650 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
652 SetLastError( ERROR_PATH_NOT_FOUND
);
655 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
656 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
657 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
660 full_name
->drive
= dir
->drive
;
661 strcpy( full_name
->long_name
, dir
->long_name
);
663 strcpyW( full_name
->short_name
, dir
->short_name
);
668 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
670 LPWSTR next
, buffer
= NULL
;
671 INT len
= strlenW(name
), newlen
, currlen
= 0;
677 static const WCHAR bkslashW
[] = {'\\',0};
679 while (*cur
== ';') cur
++;
681 next
= strchrW( cur
, ';' );
682 if (next
) *next
++ = '\0';
683 newlen
= strlenW(cur
) + len
+ 2;
685 if (newlen
> currlen
)
687 if (!(buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
))))
692 strcpyW( buffer
, cur
);
693 strcatW( buffer
, bkslashW
);
694 strcatW( buffer
, name
);
695 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
698 HeapFree( GetProcessHeap(), 0, buffer
);
703 /***********************************************************************
704 * DIR_TryEnvironmentPath
706 * Helper function for DIR_SearchPath.
707 * Search in the specified path, or in $PATH if NULL.
709 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
714 static const WCHAR pathW
[] = {'P','A','T','H',0};
716 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
717 if (!size
) return FALSE
;
718 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
719 if (envpath
) strcpyW( path
, envpath
);
720 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
722 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
725 HeapFree( GetProcessHeap(), 0, path
);
730 /***********************************************************************
733 * Helper function for DIR_SearchPath.
735 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
737 WCHAR bufferW
[MAX_PATH
];
742 char buffer
[OFS_MAXPATHNAME
];
743 if (!GetCurrentTask()) return FALSE
;
744 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
746 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
748 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
751 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
752 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
754 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
758 /***********************************************************************
761 * Helper function for DIR_SearchPath.
763 static BOOL
DIR_TryAppPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
)
765 HKEY hkAppPaths
= 0, hkApp
= 0;
766 WCHAR buffer
[MAX_PATHNAME_LEN
], *lpAppPaths
;
770 OBJECT_ATTRIBUTES attr
;
771 UNICODE_STRING nameW
;
772 KEY_VALUE_PARTIAL_INFORMATION
*info
;
773 static const WCHAR PathW
[] = {'P','a','t','h',0};
774 static const WCHAR AppPathsW
[] = {'M','a','c','h','i','n','e','\\',
775 'S','o','f','t','w','a','r','e','\\',
776 'M','i','c','r','o','s','o','f','t','\\',
777 'W','i','n','d','o','w','s','\\',
778 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
779 'A','p','p',' ','P','a','t','h','s',0};
781 attr
.Length
= sizeof(attr
);
782 attr
.RootDirectory
= 0;
783 attr
.ObjectName
= &nameW
;
785 attr
.SecurityDescriptor
= NULL
;
786 attr
.SecurityQualityOfService
= NULL
;
787 RtlInitUnicodeString( &nameW
, AppPathsW
);
788 if (NtOpenKey( &hkAppPaths
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) return FALSE
;
790 if (!GetModuleFileNameW(0, buffer
, MAX_PATHNAME_LEN
))
792 WARN("huh, module not found ??\n");
795 lpFileName
= strrchrW(buffer
, '\\');
796 if (!lpFileName
) lpFileName
= buffer
;
797 else lpFileName
++; /* skip '\\' */
799 attr
.RootDirectory
= hkAppPaths
;
800 RtlInitUnicodeString( &nameW
, lpFileName
);
801 if (NtOpenKey( &hkApp
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) goto end
;
803 RtlInitUnicodeString( &nameW
, PathW
);
804 if (NtQueryValueKey( hkApp
, &nameW
, KeyValuePartialInformation
,
805 buffer
, sizeof(buffer
)-sizeof(WCHAR
), &count
)) goto end
;
806 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
807 lpAppPaths
= (WCHAR
*)info
->Data
;
808 lpAppPaths
[info
->DataLength
/sizeof(WCHAR
)] = 0;
809 res
= DIR_SearchSemicolonedPaths(name
, full_name
, lpAppPaths
);
811 if (hkApp
) NtClose(hkApp
);
812 if (hkAppPaths
) NtClose(hkAppPaths
);
816 /***********************************************************************
819 * Implementation of SearchPathA. 'win32' specifies whether the search
820 * order is Win16 (module path last) or Win32 (module path first).
822 * FIXME: should return long path names.
824 DWORD
DIR_SearchPath( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
825 DOS_FULL_NAME
*full_name
, BOOL win32
)
831 /* First check the supplied parameters */
833 p
= strrchrW( name
, '.' );
834 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
835 ext
= NULL
; /* Ignore the specified extension */
836 if (FILE_contains_pathW (name
))
837 path
= NULL
; /* Ignore path if name already contains a path */
838 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
840 /* Allocate a buffer for the file name and extension */
844 DWORD len
= strlenW(name
) + strlenW(ext
);
845 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
847 SetLastError( ERROR_OUTOFMEMORY
);
850 strcpyW( tmp
, name
);
855 /* If the name contains an explicit path, everything's easy */
857 if (FILE_contains_pathW(name
))
859 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
863 /* Search in the specified path */
867 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
871 /* Try the path of the current executable (for Win32 search order) */
873 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
875 /* Try the current directory */
877 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
879 /* Try the Windows system directory */
881 if (DIR_TryPath( &DIR_System
, name
, full_name
))
884 /* Try the Windows directory */
886 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
889 /* Try the path of the current executable (for Win16 search order) */
891 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
893 /* Try the "App Paths" entry if existing (undocumented ??) */
894 if (DIR_TryAppPath(name
, full_name
))
897 /* Try all directories in path */
899 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
902 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
907 /***********************************************************************
908 * SearchPathW [KERNEL32.@]
910 * Searches for a specified file in the search path.
913 * path [I] Path to search
914 * name [I] Filename to search for.
915 * ext [I] File extension to append to file name. The first
916 * character must be a period. This parameter is
917 * specified only if the filename given does not
918 * contain an extension.
919 * buflen [I] size of buffer, in characters
920 * buffer [O] buffer for found filename
921 * lastpart [O] address of pointer to last used character in
922 * buffer (the final '\')
925 * Success: length of string copied into buffer, not including
926 * terminating null character. If the filename found is
927 * longer than the length of the buffer, the length of the
928 * filename is returned.
932 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
935 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
936 LPWSTR buffer
, LPWSTR
*lastpart
)
939 DOS_FULL_NAME full_name
;
941 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
943 SetLastError(ERROR_FILE_NOT_FOUND
);
947 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
948 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
950 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
951 res
= full_name
.long_name
+
952 strlen(DRIVE_GetRoot( full_name
.drive
));
953 while (*res
== '/') res
++;
959 MultiByteToWideChar(DRIVE_GetCodepage(full_name
.drive
), 0,
960 res
, -1, buffer
+ 3, buflen
- 3);
961 buffer
[buflen
- 1] = 0;
963 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
964 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
966 TRACE("Returning %s\n", debugstr_w(buffer
) );
967 return strlenW(buffer
);
971 /***********************************************************************
972 * SearchPathA (KERNEL32.@)
974 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
975 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
977 UNICODE_STRING pathW
, nameW
, extW
;
978 WCHAR bufferW
[MAX_PATH
];
981 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
982 else pathW
.Buffer
= NULL
;
983 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
984 else nameW
.Buffer
= NULL
;
985 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
986 else extW
.Buffer
= NULL
;
988 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
992 else if (retW
> MAX_PATH
)
994 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
999 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1002 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
1003 ret
--; /* length without 0 */
1004 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
1008 RtlFreeUnicodeString(&pathW
);
1009 RtlFreeUnicodeString(&nameW
);
1010 RtlFreeUnicodeString(&extW
);
1015 /***********************************************************************
1016 * search_alternate_path
1019 * FIXME: should return long path names.?
1021 static BOOL
search_alternate_path(LPCWSTR dll_path
, LPCWSTR name
, LPCWSTR ext
,
1022 DOS_FULL_NAME
*full_name
)
1028 /* First check the supplied parameters */
1030 p
= strrchrW( name
, '.' );
1031 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
1032 ext
= NULL
; /* Ignore the specified extension */
1034 /* Allocate a buffer for the file name and extension */
1038 DWORD len
= strlenW(name
) + strlenW(ext
);
1039 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1041 SetLastError( ERROR_OUTOFMEMORY
);
1044 strcpyW( tmp
, name
);
1045 strcatW( tmp
, ext
);
1049 if (DIR_TryEnvironmentPath (name
, full_name
, dll_path
))
1051 else if (DOSFS_GetFullName (name
, TRUE
, full_name
)) /* current dir */
1053 else if (DIR_TryPath (&DIR_System
, name
, full_name
)) /* System dir */
1055 else if (DIR_TryPath (&DIR_Windows
, name
, full_name
)) /* Windows dir */
1058 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
1060 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
1065 /***********************************************************************
1066 * DIR_SearchAlternatePath
1068 * Searches for a specified file in the search path.
1071 * dll_path [I] Path to search
1072 * name [I] Filename to search for.
1073 * ext [I] File extension to append to file name. The first
1074 * character must be a period. This parameter is
1075 * specified only if the filename given does not
1076 * contain an extension.
1077 * buflen [I] size of buffer, in characters
1078 * buffer [O] buffer for found filename
1079 * lastpart [O] address of pointer to last used character in
1080 * buffer (the final '\') (May be NULL)
1083 * Success: length of string copied into buffer, not including
1084 * terminating null character. If the filename found is
1085 * longer than the length of the buffer, the length of the
1086 * filename is returned.
1090 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1092 * FIXME: convert to unicode
1094 DWORD
DIR_SearchAlternatePath( LPCSTR dll_path
, LPCSTR name
, LPCSTR ext
,
1095 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
1098 DOS_FULL_NAME full_name
;
1100 UNICODE_STRING dll_pathW
, nameW
, extW
;
1102 if (dll_path
) RtlCreateUnicodeStringFromAsciiz(&dll_pathW
, dll_path
);
1103 else dll_pathW
.Buffer
= NULL
;
1104 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1105 else nameW
.Buffer
= NULL
;
1106 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
1107 else extW
.Buffer
= NULL
;
1109 if (search_alternate_path( dll_pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, &full_name
))
1111 ret
= WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, NULL
, 0, NULL
, NULL
);
1114 WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
, NULL
, NULL
);
1115 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
1116 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
1117 ret
--; /* length without 0 */
1121 SetLastError(ERROR_FILE_NOT_FOUND
);
1123 RtlFreeUnicodeString(&dll_pathW
);
1124 RtlFreeUnicodeString(&nameW
);
1125 RtlFreeUnicodeString(&extW
);
1127 TRACE("Returning %ld\n", ret
);