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 * Get a path name from the wine.ini file and make sure it is valid.
64 static int DIR_GetPath( LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
65 LPWSTR longname
, INT longname_len
, BOOL warn
)
67 WCHAR path
[MAX_PATHNAME_LEN
];
68 BY_HANDLE_FILE_INFORMATION info
;
69 const char *mess
= "does not exist";
71 PROFILE_GetWineIniString( wineW
, keyname
, defval
, path
, MAX_PATHNAME_LEN
);
73 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
74 (!FILE_Stat( full_name
->long_name
, &info
) && (mess
=strerror(errno
)))||
75 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
76 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
79 MESSAGE("Invalid path %s for %s directory: %s\n",
80 debugstr_w(path
), debugstr_w(keyname
), mess
);
87 /***********************************************************************
92 char path
[MAX_PATHNAME_LEN
];
93 WCHAR longpath
[MAX_PATHNAME_LEN
];
94 DOS_FULL_NAME tmp_dir
, profile_dir
;
97 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
98 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
99 static const WCHAR tempW
[] = {'t','e','m','p',0};
100 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
101 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
102 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
103 static const WCHAR pathW
[] = {'p','a','t','h',0};
104 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
105 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
106 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
107 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
108 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
109 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
110 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
111 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
112 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
113 static const WCHAR empty_strW
[] = { 0 };
115 if (!getcwd( path
, MAX_PATHNAME_LEN
))
117 perror( "Could not get current directory" );
121 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
123 MESSAGE("Warning: could not find wine config [Drive x] entry "
124 "for current working directory %s; "
125 "starting in windows directory.\n", cwd
);
129 WCHAR szdrive
[3]={drive
+'A',':',0};
130 MultiByteToWideChar(DRIVE_GetCodepage(drive
), 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
131 DRIVE_SetCurrentDrive( drive
);
132 DRIVE_Chdir( drive
, longpath
);
133 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
134 chdir("/"); /* change to root directory so as not to lock cdroms */
137 if (!(DIR_GetPath( windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
138 !(DIR_GetPath( systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
139 !(DIR_GetPath( tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
141 PROFILE_UsageWineIni();
144 if (-1 == access( tmp_dir
.long_name
, W_OK
))
148 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
149 PROFILE_UsageWineIni();
152 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
153 tmp_dir
.long_name
, strerror(errno
));
158 drive
= DIR_Windows
.drive
;
159 DRIVE_SetCurrentDrive( drive
);
160 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
163 PROFILE_GetWineIniString(wineW
, pathW
, path_dirW
, longpath
, MAX_PATHNAME_LEN
);
164 if (strchrW(longpath
, '/'))
166 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
167 PROFILE_UsageWineIni();
171 /* Set the environment variables */
173 SetEnvironmentVariableW( path_capsW
, longpath
);
174 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
175 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
176 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
177 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
179 /* set COMSPEC only if it doesn't exist already */
180 if (!GetEnvironmentVariableA( "COMSPEC", NULL
, 0 ))
181 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
183 TRACE("WindowsDir = %s (%s)\n",
184 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
185 TRACE("SystemDir = %s (%s)\n",
186 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
187 TRACE("TempDir = %s (%s)\n",
188 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
189 TRACE("Path = %s\n", debugstr_w(longpath
) );
190 TRACE("Cwd = %c:\\%s\n",
191 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
193 if (DIR_GetPath( profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
195 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
196 SetEnvironmentVariableW( userprofileW
, longpath
);
199 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
200 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
206 /***********************************************************************
207 * GetTempPathA (KERNEL32.@)
209 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
211 WCHAR pathW
[MAX_PATH
];
214 ret
= GetTempPathW(MAX_PATH
, pathW
);
221 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
225 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
228 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
229 ret
--; /* length without 0 */
235 /***********************************************************************
236 * GetTempPathW (KERNEL32.@)
238 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
240 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
241 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
242 WCHAR tmp_path
[MAX_PATH
];
245 TRACE("%u,%p\n", count
, path
);
247 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
248 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
249 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
254 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
258 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
261 if (ret
> MAX_PATH
- 2)
263 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
267 if (tmp_path
[ret
-1] != '\\')
269 tmp_path
[ret
++] = '\\';
270 tmp_path
[ret
] = '\0';
273 ret
++; /* add space for terminating 0 */
277 lstrcpynW(path
, tmp_path
, count
);
279 ret
--; /* return length without 0 */
281 path
[0] = 0; /* avoid returning ambiguous "X:" */
284 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
289 /***********************************************************************
290 * DIR_GetWindowsUnixDir
292 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
294 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
295 return strlen( DIR_Windows
.long_name
);
299 /***********************************************************************
300 * DIR_GetSystemUnixDir
302 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
304 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
305 return strlen( DIR_System
.long_name
);
309 /***********************************************************************
310 * GetTempDrive (KERNEL.92)
311 * A closer look at krnl386.exe shows what the SDK doesn't mention:
315 * AH: ':' - yes, some kernel code even does stosw with
319 UINT WINAPI
GetTempDrive( BYTE ignored
)
323 UINT len
= GetTempPathA( 0, NULL
);
325 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
326 ret
= DRIVE_GetCurrentDrive() + 'A';
329 /* FIXME: apparently Windows does something with the ignored byte */
330 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
331 ret
= toupper(buffer
[0]);
332 HeapFree( GetProcessHeap(), 0, buffer
);
334 return MAKELONG( ret
| (':' << 8), 1 );
338 /***********************************************************************
339 * GetWindowsDirectory (KERNEL.134)
341 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
343 return (UINT16
)GetWindowsDirectoryA( path
, count
);
347 /***********************************************************************
348 * GetWindowsDirectoryW (KERNEL32.@)
350 * See comment for GetWindowsDirectoryA.
352 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
354 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
355 if (path
&& count
>= len
)
357 strcpyW( path
, DIR_Windows
.short_name
);
364 /***********************************************************************
365 * GetWindowsDirectoryA (KERNEL32.@)
368 * If buffer is large enough to hold full path and terminating '\0' character
369 * function copies path to buffer and returns length of the path without '\0'.
370 * Otherwise function returns required size including '\0' character and
371 * does not touch the buffer.
373 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
375 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
376 if (path
&& count
>= len
)
378 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
385 /***********************************************************************
386 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
388 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
390 return GetWindowsDirectoryA( path
, count
);
394 /***********************************************************************
395 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
397 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
399 return GetWindowsDirectoryW( path
, count
);
403 /***********************************************************************
404 * GetSystemDirectory (KERNEL.135)
406 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
408 return (UINT16
)GetSystemDirectoryA( path
, count
);
412 /***********************************************************************
413 * GetSystemDirectoryW (KERNEL32.@)
415 * See comment for GetWindowsDirectoryA.
417 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
419 UINT len
= strlenW( DIR_System
.short_name
) + 1;
420 if (path
&& count
>= len
)
422 strcpyW( path
, DIR_System
.short_name
);
429 /***********************************************************************
430 * GetSystemDirectoryA (KERNEL32.@)
432 * See comment for GetWindowsDirectoryA.
434 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
436 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
437 if (path
&& count
>= len
)
439 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
446 /***********************************************************************
447 * CreateDirectory (KERNEL.144)
449 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
451 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
452 return (BOOL16
)CreateDirectoryA( path
, NULL
);
456 /***********************************************************************
457 * CreateDirectoryW (KERNEL32.@)
461 * ERROR_DISK_FULL: on full disk
462 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
463 * ERROR_ACCESS_DENIED: on permission problems
464 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
466 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
467 LPSECURITY_ATTRIBUTES lpsecattribs
)
469 DOS_FULL_NAME full_name
;
473 SetLastError(ERROR_PATH_NOT_FOUND
);
477 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
479 if (DOSFS_GetDevice( path
))
481 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
482 SetLastError( ERROR_ACCESS_DENIED
);
485 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
486 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
487 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
488 /* the FILE_SetDosError() generated error codes don't match the
489 * CreateDirectory ones for some errnos */
493 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
494 SetLastError(ERROR_ACCESS_DENIED
);
496 SetLastError(ERROR_ALREADY_EXISTS
);
499 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
500 default: FILE_SetDosError();break;
508 /***********************************************************************
509 * CreateDirectoryA (KERNEL32.@)
511 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
512 LPSECURITY_ATTRIBUTES lpsecattribs
)
514 UNICODE_STRING pathW
;
519 SetLastError(ERROR_PATH_NOT_FOUND
);
523 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
525 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
526 RtlFreeUnicodeString(&pathW
);
529 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
534 /***********************************************************************
535 * CreateDirectoryExA (KERNEL32.@)
537 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
538 LPSECURITY_ATTRIBUTES lpsecattribs
)
540 return CreateDirectoryA(path
,lpsecattribs
);
544 /***********************************************************************
545 * CreateDirectoryExW (KERNEL32.@)
547 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
548 LPSECURITY_ATTRIBUTES lpsecattribs
)
550 return CreateDirectoryW(path
,lpsecattribs
);
554 /***********************************************************************
555 * RemoveDirectory (KERNEL.145)
557 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
559 return (BOOL16
)RemoveDirectoryA( path
);
563 /***********************************************************************
564 * RemoveDirectoryW (KERNEL32.@)
566 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
568 DOS_FULL_NAME full_name
;
572 SetLastError(ERROR_INVALID_PARAMETER
);
576 TRACE_(file
)("%s\n", debugstr_w(path
));
578 if (DOSFS_GetDevice( path
))
580 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
581 SetLastError( ERROR_FILE_NOT_FOUND
);
584 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
585 if (rmdir( full_name
.long_name
) == -1)
594 /***********************************************************************
595 * RemoveDirectoryA (KERNEL32.@)
597 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
599 UNICODE_STRING pathW
;
604 SetLastError(ERROR_INVALID_PARAMETER
);
608 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
610 ret
= RemoveDirectoryW(pathW
.Buffer
);
611 RtlFreeUnicodeString(&pathW
);
614 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
619 /***********************************************************************
622 * Helper function for DIR_SearchPath.
624 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
625 DOS_FULL_NAME
*full_name
)
627 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
628 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
630 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
631 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
633 SetLastError( ERROR_PATH_NOT_FOUND
);
636 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
637 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
638 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
641 full_name
->drive
= dir
->drive
;
642 strcpy( full_name
->long_name
, dir
->long_name
);
644 strcpyW( full_name
->short_name
, dir
->short_name
);
649 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
651 LPWSTR next
, buffer
= NULL
;
652 INT len
= strlenW(name
), newlen
, currlen
= 0;
658 static const WCHAR bkslashW
[] = {'\\',0};
660 while (*cur
== ';') cur
++;
662 next
= strchrW( cur
, ';' );
663 if (next
) *next
++ = '\0';
664 newlen
= strlenW(cur
) + len
+ 2;
666 if (newlen
> currlen
)
668 if (!(buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
))))
673 strcpyW( buffer
, cur
);
674 strcatW( buffer
, bkslashW
);
675 strcatW( buffer
, name
);
676 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
679 HeapFree( GetProcessHeap(), 0, buffer
);
684 /***********************************************************************
685 * DIR_TryEnvironmentPath
687 * Helper function for DIR_SearchPath.
688 * Search in the specified path, or in $PATH if NULL.
690 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
695 static const WCHAR pathW
[] = {'P','A','T','H',0};
697 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
698 if (!size
) return FALSE
;
699 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
700 if (envpath
) strcpyW( path
, envpath
);
701 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
703 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
706 HeapFree( GetProcessHeap(), 0, path
);
711 /***********************************************************************
714 * Helper function for DIR_SearchPath.
716 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
718 WCHAR bufferW
[MAX_PATH
];
723 char buffer
[OFS_MAXPATHNAME
];
724 if (!GetCurrentTask()) return FALSE
;
725 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
727 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
729 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
732 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
733 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
735 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
739 /***********************************************************************
742 * Helper function for DIR_SearchPath.
744 static BOOL
DIR_TryAppPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
)
746 HKEY hkAppPaths
= 0, hkApp
= 0;
747 WCHAR buffer
[MAX_PATHNAME_LEN
], *lpAppPaths
;
751 OBJECT_ATTRIBUTES attr
;
752 UNICODE_STRING nameW
;
753 KEY_VALUE_PARTIAL_INFORMATION
*info
;
754 static const WCHAR PathW
[] = {'P','a','t','h',0};
755 static const WCHAR AppPathsW
[] = {'M','a','c','h','i','n','e','\\',
756 'S','o','f','t','w','a','r','e','\\',
757 'M','i','c','r','o','s','o','f','t','\\',
758 'W','i','n','d','o','w','s','\\',
759 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
760 'A','p','p',' ','P','a','t','h','s',0};
762 attr
.Length
= sizeof(attr
);
763 attr
.RootDirectory
= 0;
764 attr
.ObjectName
= &nameW
;
766 attr
.SecurityDescriptor
= NULL
;
767 attr
.SecurityQualityOfService
= NULL
;
768 RtlInitUnicodeString( &nameW
, AppPathsW
);
769 if (NtOpenKey( &hkAppPaths
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) return FALSE
;
771 if (!GetModuleFileNameW(0, buffer
, MAX_PATHNAME_LEN
))
773 WARN("huh, module not found ??\n");
776 lpFileName
= strrchrW(buffer
, '\\');
777 if (!lpFileName
) lpFileName
= buffer
;
778 else lpFileName
++; /* skip '\\' */
780 attr
.RootDirectory
= hkAppPaths
;
781 RtlInitUnicodeString( &nameW
, lpFileName
);
782 if (NtOpenKey( &hkApp
, KEY_ALL_ACCESS
, &attr
) != STATUS_SUCCESS
) goto end
;
784 RtlInitUnicodeString( &nameW
, PathW
);
785 if (NtQueryValueKey( hkApp
, &nameW
, KeyValuePartialInformation
,
786 buffer
, sizeof(buffer
)-sizeof(WCHAR
), &count
)) goto end
;
787 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
788 lpAppPaths
= (WCHAR
*)info
->Data
;
789 lpAppPaths
[info
->DataLength
/sizeof(WCHAR
)] = 0;
790 res
= DIR_SearchSemicolonedPaths(name
, full_name
, lpAppPaths
);
792 if (hkApp
) NtClose(hkApp
);
793 if (hkAppPaths
) NtClose(hkAppPaths
);
797 /***********************************************************************
800 * Implementation of SearchPathA. 'win32' specifies whether the search
801 * order is Win16 (module path last) or Win32 (module path first).
803 * FIXME: should return long path names.
805 DWORD
DIR_SearchPath( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
806 DOS_FULL_NAME
*full_name
, BOOL win32
)
812 /* First check the supplied parameters */
814 p
= strrchrW( name
, '.' );
815 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
816 ext
= NULL
; /* Ignore the specified extension */
817 if (FILE_contains_pathW (name
))
818 path
= NULL
; /* Ignore path if name already contains a path */
819 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
821 /* Allocate a buffer for the file name and extension */
825 DWORD len
= strlenW(name
) + strlenW(ext
);
826 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
828 SetLastError( ERROR_OUTOFMEMORY
);
831 strcpyW( tmp
, name
);
836 /* If the name contains an explicit path, everything's easy */
838 if (FILE_contains_pathW(name
))
840 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
844 /* Search in the specified path */
848 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
852 /* Try the path of the current executable (for Win32 search order) */
854 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
856 /* Try the current directory */
858 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
860 /* Try the Windows system directory */
862 if (DIR_TryPath( &DIR_System
, name
, full_name
))
865 /* Try the Windows directory */
867 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
870 /* Try the path of the current executable (for Win16 search order) */
872 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
874 /* Try the "App Paths" entry if existing (undocumented ??) */
875 if (DIR_TryAppPath(name
, full_name
))
878 /* Try all directories in path */
880 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
883 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
888 /***********************************************************************
889 * SearchPathW [KERNEL32.@]
891 * Searches for a specified file in the search path.
894 * path [I] Path to search
895 * name [I] Filename to search for.
896 * ext [I] File extension to append to file name. The first
897 * character must be a period. This parameter is
898 * specified only if the filename given does not
899 * contain an extension.
900 * buflen [I] size of buffer, in characters
901 * buffer [O] buffer for found filename
902 * lastpart [O] address of pointer to last used character in
903 * buffer (the final '\')
906 * Success: length of string copied into buffer, not including
907 * terminating null character. If the filename found is
908 * longer than the length of the buffer, the length of the
909 * filename is returned.
913 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
916 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
917 LPWSTR buffer
, LPWSTR
*lastpart
)
920 DOS_FULL_NAME full_name
;
922 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
924 SetLastError(ERROR_FILE_NOT_FOUND
);
928 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
929 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
931 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
932 res
= full_name
.long_name
+
933 strlen(DRIVE_GetRoot( full_name
.drive
));
934 while (*res
== '/') res
++;
940 MultiByteToWideChar(DRIVE_GetCodepage(full_name
.drive
), 0,
941 res
, -1, buffer
+ 3, buflen
- 3);
942 buffer
[buflen
- 1] = 0;
944 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
945 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
947 TRACE("Returning %s\n", debugstr_w(buffer
) );
948 return strlenW(buffer
);
952 /***********************************************************************
953 * SearchPathA (KERNEL32.@)
955 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
956 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
958 UNICODE_STRING pathW
, nameW
, extW
;
959 WCHAR bufferW
[MAX_PATH
];
962 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
963 else pathW
.Buffer
= NULL
;
964 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
965 else nameW
.Buffer
= NULL
;
966 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
967 else extW
.Buffer
= NULL
;
969 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
973 else if (retW
> MAX_PATH
)
975 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
980 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
983 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
984 ret
--; /* length without 0 */
985 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
989 RtlFreeUnicodeString(&pathW
);
990 RtlFreeUnicodeString(&nameW
);
991 RtlFreeUnicodeString(&extW
);
996 /***********************************************************************
997 * search_alternate_path
1000 * FIXME: should return long path names.?
1002 static BOOL
search_alternate_path(LPCWSTR dll_path
, LPCWSTR name
, LPCWSTR ext
,
1003 DOS_FULL_NAME
*full_name
)
1009 /* First check the supplied parameters */
1011 p
= strrchrW( name
, '.' );
1012 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
1013 ext
= NULL
; /* Ignore the specified extension */
1015 /* Allocate a buffer for the file name and extension */
1019 DWORD len
= strlenW(name
) + strlenW(ext
);
1020 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1022 SetLastError( ERROR_OUTOFMEMORY
);
1025 strcpyW( tmp
, name
);
1026 strcatW( tmp
, ext
);
1030 if (DIR_TryEnvironmentPath (name
, full_name
, dll_path
))
1032 else if (DOSFS_GetFullName (name
, TRUE
, full_name
)) /* current dir */
1034 else if (DIR_TryPath (&DIR_System
, name
, full_name
)) /* System dir */
1036 else if (DIR_TryPath (&DIR_Windows
, name
, full_name
)) /* Windows dir */
1039 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
1041 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
1046 /***********************************************************************
1047 * DIR_SearchAlternatePath
1049 * Searches for a specified file in the search path.
1052 * dll_path [I] Path to search
1053 * name [I] Filename to search for.
1054 * ext [I] File extension to append to file name. The first
1055 * character must be a period. This parameter is
1056 * specified only if the filename given does not
1057 * contain an extension.
1058 * buflen [I] size of buffer, in characters
1059 * buffer [O] buffer for found filename
1060 * lastpart [O] address of pointer to last used character in
1061 * buffer (the final '\') (May be NULL)
1064 * Success: length of string copied into buffer, not including
1065 * terminating null character. If the filename found is
1066 * longer than the length of the buffer, the length of the
1067 * filename is returned.
1071 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1073 * FIXME: convert to unicode
1075 DWORD
DIR_SearchAlternatePath( LPCSTR dll_path
, LPCSTR name
, LPCSTR ext
,
1076 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
1079 DOS_FULL_NAME full_name
;
1081 UNICODE_STRING dll_pathW
, nameW
, extW
;
1083 if (dll_path
) RtlCreateUnicodeStringFromAsciiz(&dll_pathW
, dll_path
);
1084 else dll_pathW
.Buffer
= NULL
;
1085 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
1086 else nameW
.Buffer
= NULL
;
1087 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
1088 else extW
.Buffer
= NULL
;
1090 if (search_alternate_path( dll_pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, &full_name
))
1092 ret
= WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, NULL
, 0, NULL
, NULL
);
1095 WideCharToMultiByte(CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
, NULL
, NULL
);
1096 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
1097 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
1098 ret
--; /* length without 0 */
1102 SetLastError(ERROR_FILE_NOT_FOUND
);
1104 RtlFreeUnicodeString(&dll_pathW
);
1105 RtlFreeUnicodeString(&nameW
);
1106 RtlFreeUnicodeString(&extW
);
1108 TRACE("Returning %ld\n", ret
);