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"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
51 WINE_DECLARE_DEBUG_CHANNEL(file
);
53 static DOS_FULL_NAME DIR_Windows
;
54 static DOS_FULL_NAME DIR_System
;
56 static const WCHAR wineW
[] = {'w','i','n','e',0};
58 /***********************************************************************
61 inline static int FILE_contains_pathW (LPCWSTR name
)
63 return ((*name
&& (name
[1] == ':')) ||
64 strchrW (name
, '/') || strchrW (name
, '\\'));
67 /***********************************************************************
70 * Get a path name from the wine.ini file and make sure it is valid.
72 static int DIR_GetPath( LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
73 LPWSTR longname
, INT longname_len
, BOOL warn
)
75 WCHAR path
[MAX_PATHNAME_LEN
];
76 BY_HANDLE_FILE_INFORMATION info
;
77 const char *mess
= "does not exist";
79 PROFILE_GetWineIniString( wineW
, keyname
, defval
, path
, MAX_PATHNAME_LEN
);
81 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
82 (!FILE_Stat( full_name
->long_name
, &info
, NULL
) && (mess
=strerror(errno
)))||
83 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
84 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
87 MESSAGE("Invalid path %s for %s directory: %s\n",
88 debugstr_w(path
), debugstr_w(keyname
), mess
);
95 /***********************************************************************
100 char path
[MAX_PATHNAME_LEN
];
101 WCHAR longpath
[MAX_PATHNAME_LEN
];
102 DOS_FULL_NAME tmp_dir
, profile_dir
;
105 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
106 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
107 static const WCHAR tempW
[] = {'t','e','m','p',0};
108 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
109 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
110 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
111 static const WCHAR pathW
[] = {'p','a','t','h',0};
112 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
113 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
114 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
115 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
116 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
117 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
118 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
119 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
120 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
121 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
122 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
123 static const WCHAR empty_strW
[] = { 0 };
125 if (!getcwd( path
, MAX_PATHNAME_LEN
))
127 perror( "Could not get current directory" );
131 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
133 MESSAGE("Warning: could not find wine config [Drive x] entry "
134 "for current working directory %s; "
135 "starting in windows directory.\n", cwd
);
139 WCHAR szdrive
[3]={drive
+'A',':',0};
140 MultiByteToWideChar(DRIVE_GetCodepage(drive
), 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
141 DRIVE_SetCurrentDrive( drive
);
142 DRIVE_Chdir( drive
, longpath
);
143 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
144 chdir("/"); /* change to root directory so as not to lock cdroms */
147 if (!(DIR_GetPath( windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
148 !(DIR_GetPath( systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
149 !(DIR_GetPath( tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
151 PROFILE_UsageWineIni();
154 if (-1 == access( tmp_dir
.long_name
, W_OK
))
158 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
159 PROFILE_UsageWineIni();
162 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
163 tmp_dir
.long_name
, strerror(errno
));
168 drive
= DIR_Windows
.drive
;
169 DRIVE_SetCurrentDrive( drive
);
170 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
173 /* Set the environment variables */
175 /* set COMSPEC only if it doesn't exist already */
176 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
178 strcpyW( longpath
, DIR_System
.short_name
);
179 strcatW( longpath
, wcmdW
);
180 SetEnvironmentVariableW( comspecW
, longpath
);
183 /* set PATH only if not set already */
184 if (!GetEnvironmentVariableW( path_capsW
, longpath
, MAX_PATHNAME_LEN
))
186 PROFILE_GetWineIniString(wineW
, pathW
, path_dirW
, longpath
, MAX_PATHNAME_LEN
);
187 if (strchrW(longpath
, '/'))
189 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
190 PROFILE_UsageWineIni();
193 SetEnvironmentVariableW( path_capsW
, longpath
);
196 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
197 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
198 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
199 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
201 TRACE("WindowsDir = %s (%s)\n",
202 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
203 TRACE("SystemDir = %s (%s)\n",
204 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
205 TRACE("TempDir = %s (%s)\n",
206 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
207 TRACE("Path = %s\n", debugstr_w(longpath
) );
208 TRACE("Cwd = %c:\\%s\n",
209 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
211 if (DIR_GetPath( profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
213 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
214 SetEnvironmentVariableW( userprofileW
, longpath
);
217 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
218 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
224 /***********************************************************************
225 * GetTempPathA (KERNEL32.@)
227 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
229 WCHAR pathW
[MAX_PATH
];
232 ret
= GetTempPathW(MAX_PATH
, pathW
);
239 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
243 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
246 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
247 ret
--; /* length without 0 */
253 /***********************************************************************
254 * GetTempPathW (KERNEL32.@)
256 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
258 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
259 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
260 WCHAR tmp_path
[MAX_PATH
];
263 TRACE("%u,%p\n", count
, path
);
265 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
266 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
267 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
272 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
276 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
279 if (ret
> MAX_PATH
- 2)
281 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
285 if (tmp_path
[ret
-1] != '\\')
287 tmp_path
[ret
++] = '\\';
288 tmp_path
[ret
] = '\0';
291 ret
++; /* add space for terminating 0 */
295 lstrcpynW(path
, tmp_path
, count
);
297 ret
--; /* return length without 0 */
299 path
[0] = 0; /* avoid returning ambiguous "X:" */
302 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
307 /***********************************************************************
308 * DIR_GetWindowsUnixDir
310 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
312 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
313 return strlen( DIR_Windows
.long_name
);
317 /***********************************************************************
318 * DIR_GetSystemUnixDir
320 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
322 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
323 return strlen( DIR_System
.long_name
);
327 /***********************************************************************
328 * GetTempDrive (KERNEL.92)
329 * A closer look at krnl386.exe shows what the SDK doesn't mention:
333 * AH: ':' - yes, some kernel code even does stosw with
337 UINT WINAPI
GetTempDrive( BYTE ignored
)
341 UINT len
= GetTempPathA( 0, NULL
);
343 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
344 ret
= DRIVE_GetCurrentDrive() + 'A';
347 /* FIXME: apparently Windows does something with the ignored byte */
348 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
349 ret
= toupper(buffer
[0]);
350 HeapFree( GetProcessHeap(), 0, buffer
);
352 return MAKELONG( ret
| (':' << 8), 1 );
356 /***********************************************************************
357 * GetWindowsDirectory (KERNEL.134)
359 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
361 return (UINT16
)GetWindowsDirectoryA( path
, count
);
365 /***********************************************************************
366 * GetWindowsDirectoryW (KERNEL32.@)
368 * See comment for GetWindowsDirectoryA.
370 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
372 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
373 if (path
&& count
>= len
)
375 strcpyW( path
, DIR_Windows
.short_name
);
382 /***********************************************************************
383 * GetWindowsDirectoryA (KERNEL32.@)
386 * If buffer is large enough to hold full path and terminating '\0' character
387 * function copies path to buffer and returns length of the path without '\0'.
388 * Otherwise function returns required size including '\0' character and
389 * does not touch the buffer.
391 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
393 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
394 if (path
&& count
>= len
)
396 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
403 /***********************************************************************
404 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
406 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
408 return GetWindowsDirectoryA( path
, count
);
412 /***********************************************************************
413 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
415 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
417 return GetWindowsDirectoryW( path
, count
);
421 /***********************************************************************
422 * GetSystemDirectory (KERNEL.135)
424 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
426 return (UINT16
)GetSystemDirectoryA( path
, count
);
430 /***********************************************************************
431 * GetSystemDirectoryW (KERNEL32.@)
433 * See comment for GetWindowsDirectoryA.
435 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
437 UINT len
= strlenW( DIR_System
.short_name
) + 1;
438 if (path
&& count
>= len
)
440 strcpyW( path
, DIR_System
.short_name
);
447 /***********************************************************************
448 * GetSystemDirectoryA (KERNEL32.@)
450 * See comment for GetWindowsDirectoryA.
452 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
454 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
455 if (path
&& count
>= len
)
457 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
464 /***********************************************************************
465 * CreateDirectory (KERNEL.144)
467 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
469 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
470 return (BOOL16
)CreateDirectoryA( path
, NULL
);
474 /***********************************************************************
475 * CreateDirectoryW (KERNEL32.@)
479 * ERROR_DISK_FULL: on full disk
480 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
481 * ERROR_ACCESS_DENIED: on permission problems
482 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
484 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
485 LPSECURITY_ATTRIBUTES lpsecattribs
)
487 DOS_FULL_NAME full_name
;
491 SetLastError(ERROR_PATH_NOT_FOUND
);
495 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
497 if (DOSFS_GetDevice( path
))
499 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
500 SetLastError( ERROR_ACCESS_DENIED
);
503 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
504 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
505 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
506 /* the FILE_SetDosError() generated error codes don't match the
507 * CreateDirectory ones for some errnos */
511 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
512 SetLastError(ERROR_ACCESS_DENIED
);
514 SetLastError(ERROR_ALREADY_EXISTS
);
517 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
518 default: FILE_SetDosError();break;
526 /***********************************************************************
527 * CreateDirectoryA (KERNEL32.@)
529 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
530 LPSECURITY_ATTRIBUTES lpsecattribs
)
532 UNICODE_STRING pathW
;
537 SetLastError(ERROR_PATH_NOT_FOUND
);
541 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
543 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
544 RtlFreeUnicodeString(&pathW
);
547 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
552 /***********************************************************************
553 * CreateDirectoryExA (KERNEL32.@)
555 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
556 LPSECURITY_ATTRIBUTES lpsecattribs
)
558 return CreateDirectoryA(path
,lpsecattribs
);
562 /***********************************************************************
563 * CreateDirectoryExW (KERNEL32.@)
565 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
566 LPSECURITY_ATTRIBUTES lpsecattribs
)
568 return CreateDirectoryW(path
,lpsecattribs
);
572 /***********************************************************************
573 * RemoveDirectory (KERNEL.145)
575 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
577 return (BOOL16
)RemoveDirectoryA( path
);
581 /***********************************************************************
582 * RemoveDirectoryW (KERNEL32.@)
584 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
586 DOS_FULL_NAME full_name
;
590 SetLastError(ERROR_INVALID_PARAMETER
);
594 TRACE_(file
)("%s\n", debugstr_w(path
));
596 if (DOSFS_GetDevice( path
))
598 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
599 SetLastError( ERROR_FILE_NOT_FOUND
);
602 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
603 if (rmdir( full_name
.long_name
) == -1)
612 /***********************************************************************
613 * RemoveDirectoryA (KERNEL32.@)
615 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
617 UNICODE_STRING pathW
;
622 SetLastError(ERROR_INVALID_PARAMETER
);
626 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
628 ret
= RemoveDirectoryW(pathW
.Buffer
);
629 RtlFreeUnicodeString(&pathW
);
632 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
637 /***********************************************************************
640 * Helper function for DIR_SearchPath.
642 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
643 DOS_FULL_NAME
*full_name
)
645 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
646 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
648 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
649 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
651 SetLastError( ERROR_PATH_NOT_FOUND
);
654 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
655 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
656 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
659 full_name
->drive
= dir
->drive
;
660 strcpy( full_name
->long_name
, dir
->long_name
);
662 strcpyW( full_name
->short_name
, dir
->short_name
);
667 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
669 LPWSTR next
, buffer
= NULL
;
670 INT len
= strlenW(name
), newlen
, currlen
= 0;
676 static const WCHAR bkslashW
[] = {'\\',0};
678 while (*cur
== ';') cur
++;
680 next
= strchrW( cur
, ';' );
681 if (next
) *next
++ = '\0';
682 newlen
= strlenW(cur
) + len
+ 2;
684 if (newlen
> currlen
)
686 if (!(buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
))))
691 strcpyW( buffer
, cur
);
692 strcatW( buffer
, bkslashW
);
693 strcatW( buffer
, name
);
694 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
697 HeapFree( GetProcessHeap(), 0, buffer
);
702 /***********************************************************************
703 * DIR_TryEnvironmentPath
705 * Helper function for DIR_SearchPath.
706 * Search in the specified path, or in $PATH if NULL.
708 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
713 static const WCHAR pathW
[] = {'P','A','T','H',0};
715 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
716 if (!size
) return FALSE
;
717 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
718 if (envpath
) strcpyW( path
, envpath
);
719 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
721 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
724 HeapFree( GetProcessHeap(), 0, path
);
729 /***********************************************************************
732 * Helper function for DIR_SearchPath.
734 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
736 WCHAR bufferW
[MAX_PATH
];
741 char buffer
[OFS_MAXPATHNAME
];
742 if (!GetCurrentTask()) return FALSE
;
743 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
745 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
747 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
750 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
751 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
753 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
757 /***********************************************************************
760 * Helper function for DIR_SearchPath.
762 static BOOL
DIR_TryAppPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
)
764 HKEY hkAppPaths
= 0, hkApp
= 0;
765 WCHAR buffer
[MAX_PATHNAME_LEN
], *lpAppPaths
;
769 OBJECT_ATTRIBUTES attr
;
770 UNICODE_STRING nameW
;
771 KEY_VALUE_PARTIAL_INFORMATION
*info
;
772 static const WCHAR PathW
[] = {'P','a','t','h',0};
773 static const WCHAR AppPathsW
[] = {'M','a','c','h','i','n','e','\\',
774 'S','o','f','t','w','a','r','e','\\',
775 'M','i','c','r','o','s','o','f','t','\\',
776 'W','i','n','d','o','w','s','\\',
777 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
778 'A','p','p',' ','P','a','t','h','s',0};
780 attr
.Length
= sizeof(attr
);
781 attr
.RootDirectory
= 0;
782 attr
.ObjectName
= &nameW
;
784 attr
.SecurityDescriptor
= NULL
;
785 attr
.SecurityQualityOfService
= NULL
;
786 RtlInitUnicodeString( &nameW
, AppPathsW
);
787 if (NtOpenKey( &hkAppPaths
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) return FALSE
;
789 if (!GetModuleFileNameW(0, buffer
, MAX_PATHNAME_LEN
))
791 WARN("huh, module not found ??\n");
794 lpFileName
= strrchrW(buffer
, '\\');
795 if (!lpFileName
) lpFileName
= buffer
;
796 else lpFileName
++; /* skip '\\' */
798 attr
.RootDirectory
= hkAppPaths
;
799 RtlInitUnicodeString( &nameW
, lpFileName
);
800 if (NtOpenKey( &hkApp
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) goto end
;
802 RtlInitUnicodeString( &nameW
, PathW
);
803 if (NtQueryValueKey( hkApp
, &nameW
, KeyValuePartialInformation
,
804 buffer
, sizeof(buffer
)-sizeof(WCHAR
), &count
)) goto end
;
805 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
806 lpAppPaths
= (WCHAR
*)info
->Data
;
807 lpAppPaths
[info
->DataLength
/sizeof(WCHAR
)] = 0;
808 res
= DIR_SearchSemicolonedPaths(name
, full_name
, lpAppPaths
);
810 if (hkApp
) NtClose(hkApp
);
811 if (hkAppPaths
) NtClose(hkAppPaths
);
815 /***********************************************************************
818 * Implementation of SearchPathA. 'win32' specifies whether the search
819 * order is Win16 (module path last) or Win32 (module path first).
821 * FIXME: should return long path names.
823 DWORD
DIR_SearchPath( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
824 DOS_FULL_NAME
*full_name
, BOOL win32
)
830 /* First check the supplied parameters */
832 p
= strrchrW( name
, '.' );
833 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
834 ext
= NULL
; /* Ignore the specified extension */
835 if (FILE_contains_pathW (name
))
836 path
= NULL
; /* Ignore path if name already contains a path */
837 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
839 /* Allocate a buffer for the file name and extension */
843 DWORD len
= strlenW(name
) + strlenW(ext
);
844 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
846 SetLastError( ERROR_OUTOFMEMORY
);
849 strcpyW( tmp
, name
);
854 /* If the name contains an explicit path, everything's easy */
856 if (FILE_contains_pathW(name
))
858 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
862 /* Search in the specified path */
866 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
870 /* Try the path of the current executable (for Win32 search order) */
872 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
874 /* Try the current directory */
876 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
878 /* Try the Windows system directory */
880 if (DIR_TryPath( &DIR_System
, name
, full_name
))
883 /* Try the Windows directory */
885 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
888 /* Try the path of the current executable (for Win16 search order) */
890 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
892 /* Try the "App Paths" entry if existing (undocumented ??) */
893 if (DIR_TryAppPath(name
, full_name
))
896 /* Try all directories in path */
898 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
901 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
906 /***********************************************************************
907 * SearchPathW [KERNEL32.@]
909 * Searches for a specified file in the search path.
912 * path [I] Path to search
913 * name [I] Filename to search for.
914 * ext [I] File extension to append to file name. The first
915 * character must be a period. This parameter is
916 * specified only if the filename given does not
917 * contain an extension.
918 * buflen [I] size of buffer, in characters
919 * buffer [O] buffer for found filename
920 * lastpart [O] address of pointer to last used character in
921 * buffer (the final '\')
924 * Success: length of string copied into buffer, not including
925 * terminating null character. If the filename found is
926 * longer than the length of the buffer, the length of the
927 * filename is returned.
931 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
934 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
935 LPWSTR buffer
, LPWSTR
*lastpart
)
938 DOS_FULL_NAME full_name
;
940 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
942 SetLastError(ERROR_FILE_NOT_FOUND
);
946 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
947 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
949 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
950 res
= full_name
.long_name
+
951 strlen(DRIVE_GetRoot( full_name
.drive
));
952 while (*res
== '/') res
++;
958 MultiByteToWideChar(DRIVE_GetCodepage(full_name
.drive
), 0,
959 res
, -1, buffer
+ 3, buflen
- 3);
960 buffer
[buflen
- 1] = 0;
962 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
963 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
965 TRACE("Returning %s\n", debugstr_w(buffer
) );
966 return strlenW(buffer
);
970 /***********************************************************************
971 * SearchPathA (KERNEL32.@)
973 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
974 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
976 UNICODE_STRING pathW
, nameW
, extW
;
977 WCHAR bufferW
[MAX_PATH
];
980 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
981 else pathW
.Buffer
= NULL
;
982 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
983 else nameW
.Buffer
= NULL
;
984 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
985 else extW
.Buffer
= NULL
;
987 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
991 else if (retW
> MAX_PATH
)
993 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
998 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1001 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
1002 ret
--; /* length without 0 */
1003 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
1007 RtlFreeUnicodeString(&pathW
);
1008 RtlFreeUnicodeString(&nameW
);
1009 RtlFreeUnicodeString(&extW
);
1014 /***********************************************************************
1015 * search_alternate_path
1018 * FIXME: should return long path names.?
1020 static BOOL
search_alternate_path(LPCWSTR dll_path
, LPCWSTR name
, LPCWSTR ext
,
1021 DOS_FULL_NAME
*full_name
)
1027 /* First check the supplied parameters */
1029 p
= strrchrW( name
, '.' );
1030 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
1031 ext
= NULL
; /* Ignore the specified extension */
1033 /* Allocate a buffer for the file name and extension */
1037 DWORD len
= strlenW(name
) + strlenW(ext
);
1038 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1040 SetLastError( ERROR_OUTOFMEMORY
);
1043 strcpyW( tmp
, name
);
1044 strcatW( tmp
, ext
);
1048 if (DIR_TryEnvironmentPath (name
, full_name
, dll_path
))
1050 else if (DOSFS_GetFullName (name
, TRUE
, full_name
)) /* current dir */
1052 else if (DIR_TryPath (&DIR_System
, name
, full_name
)) /* System dir */
1054 else if (DIR_TryPath (&DIR_Windows
, name
, full_name
)) /* Windows dir */
1057 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
1059 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
1064 /***********************************************************************
1065 * DIR_SearchAlternatePath
1067 * Searches for a specified file in the search path.
1070 * dll_path [I] Path to search
1071 * name [I] Filename to search for.
1072 * ext [I] File extension to append to file name. The first
1073 * character must be a period. This parameter is
1074 * specified only if the filename given does not
1075 * contain an extension.
1076 * buflen [I] size of buffer, in characters
1077 * buffer [O] buffer for found filename
1078 * lastpart [O] address of pointer to last used character in
1079 * buffer (the final '\') (May be NULL)
1082 * Success: length of string copied into buffer, not including
1083 * terminating null character. If the filename found is
1084 * longer than the length of the buffer, the length of the
1085 * filename is returned.
1089 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1091 * FIXME: convert to unicode
1093 DWORD
DIR_SearchAlternatePath( LPCSTR dll_path
, LPCSTR name
, LPCSTR ext
,
1094 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
1097 DOS_FULL_NAME full_name
;
1099 UNICODE_STRING dll_pathW
, nameW
, extW
;
1101 if (dll_path
) RtlCreateUnicodeStringFromAsciiz(&dll_pathW
, dll_path
);
1102 else dll_pathW
.Buffer
= NULL
;
1103 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1104 else nameW
.Buffer
= NULL
;
1105 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
1106 else extW
.Buffer
= NULL
;
1108 if (search_alternate_path( dll_pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, &full_name
))
1110 ret
= WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, NULL
, 0, NULL
, NULL
);
1113 WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
, NULL
, NULL
);
1114 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
1115 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
1116 ret
--; /* length without 0 */
1120 SetLastError(ERROR_FILE_NOT_FOUND
);
1122 RtlFreeUnicodeString(&dll_pathW
);
1123 RtlFreeUnicodeString(&nameW
);
1124 RtlFreeUnicodeString(&extW
);
1126 TRACE("Returning %ld\n", ret
);