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
28 #include <sys/types.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
41 #include "wine/winbase16.h"
43 #include "wine/winuser16.h"
47 #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 /***********************************************************************
60 inline static int FILE_contains_pathW (LPCWSTR name
)
62 return ((*name
&& (name
[1] == ':')) ||
63 strchrW (name
, '/') || strchrW (name
, '\\'));
66 /***********************************************************************
69 * Get a path name from the wine.ini file and make sure it is valid.
71 static int DIR_GetPath( HKEY hkey
, LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
72 LPWSTR longname
, INT longname_len
, BOOL warn
)
76 WCHAR tmp
[MAX_PATHNAME_LEN
];
77 BY_HANDLE_FILE_INFORMATION info
;
78 const WCHAR
*path
= defval
;
79 const char *mess
= "does not exist";
81 RtlInitUnicodeString( &nameW
, keyname
);
82 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
83 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
85 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
86 (!FILE_Stat( full_name
->long_name
, &info
, NULL
) && (mess
=strerror(errno
)))||
87 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
88 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
92 MESSAGE("Invalid path %s for %s directory: %s.\n",
93 debugstr_w(path
), debugstr_w(keyname
), mess
);
94 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
95 wine_get_config_dir());
103 /***********************************************************************
108 OBJECT_ATTRIBUTES attr
;
109 UNICODE_STRING nameW
;
111 char path
[MAX_PATHNAME_LEN
];
112 WCHAR longpath
[MAX_PATHNAME_LEN
];
113 DOS_FULL_NAME tmp_dir
, profile_dir
;
116 static const WCHAR wineW
[] = {'M','a','c','h','i','n','e','\\',
117 'S','o','f','t','w','a','r','e','\\',
118 'W','i','n','e','\\','W','i','n','e','\\',
119 'C','o','n','f','i','g','\\','W','i','n','e',0};
120 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
121 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
122 static const WCHAR tempW
[] = {'t','e','m','p',0};
123 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
124 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
125 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
126 static const WCHAR pathW
[] = {'p','a','t','h',0};
127 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
128 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
129 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
130 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
131 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
132 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
133 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
134 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
135 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
136 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
137 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
138 static const WCHAR empty_strW
[] = { 0 };
140 if (!getcwd( path
, MAX_PATHNAME_LEN
))
142 perror( "Could not get current directory" );
146 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
148 MESSAGE("Warning: could not find wine config [Drive x] entry "
149 "for current working directory %s; "
150 "starting in windows directory.\n", cwd
);
154 WCHAR szdrive
[3]={drive
+'A',':',0};
155 MultiByteToWideChar(CP_UNIXCP
, 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
156 DRIVE_SetCurrentDrive( drive
);
157 DRIVE_Chdir( drive
, longpath
);
158 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
159 chdir("/"); /* change to root directory so as not to lock cdroms */
162 attr
.Length
= sizeof(attr
);
163 attr
.RootDirectory
= 0;
164 attr
.ObjectName
= &nameW
;
166 attr
.SecurityDescriptor
= NULL
;
167 attr
.SecurityQualityOfService
= NULL
;
169 RtlInitUnicodeString( &nameW
, wineW
);
170 if (NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) hkey
= 0;
172 if (!(DIR_GetPath( hkey
, windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
173 !(DIR_GetPath( hkey
, systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
174 !(DIR_GetPath( hkey
, tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
176 if (hkey
) NtClose( hkey
);
179 if (-1 == access( tmp_dir
.long_name
, W_OK
))
183 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
184 "configuration file (%s) is not writeable.\n",
185 tmp_dir
.long_name
, wine_get_config_dir() );
188 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
189 tmp_dir
.long_name
, strerror(errno
));
194 drive
= DIR_Windows
.drive
;
195 DRIVE_SetCurrentDrive( drive
);
196 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
199 /* Set the environment variables */
201 /* set COMSPEC only if it doesn't exist already */
202 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
204 strcpyW( longpath
, DIR_System
.short_name
);
205 strcatW( longpath
, wcmdW
);
206 SetEnvironmentVariableW( comspecW
, longpath
);
209 /* set PATH only if not set already */
210 if (!GetEnvironmentVariableW( path_capsW
, NULL
, 0 ))
212 WCHAR tmp
[MAX_PATHNAME_LEN
];
214 const WCHAR
*path
= path_dirW
;
216 RtlInitUnicodeString( &nameW
, pathW
);
217 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
218 tmp
, sizeof(tmp
), &dummy
))
220 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
223 if (strchrW(path
, '/'))
225 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
228 SetEnvironmentVariableW( path_capsW
, path
);
229 TRACE("Path = %s\n", debugstr_w(path
) );
232 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
233 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
234 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
235 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
237 TRACE("WindowsDir = %s (%s)\n",
238 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
239 TRACE("SystemDir = %s (%s)\n",
240 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
241 TRACE("TempDir = %s (%s)\n",
242 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
243 TRACE("Cwd = %c:\\%s\n",
244 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
246 if (DIR_GetPath( hkey
, profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
248 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
249 SetEnvironmentVariableW( userprofileW
, longpath
);
252 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
253 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
254 if (hkey
) NtClose( hkey
);
260 /***********************************************************************
261 * GetTempPathA (KERNEL32.@)
263 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
265 WCHAR pathW
[MAX_PATH
];
268 ret
= GetTempPathW(MAX_PATH
, pathW
);
275 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
279 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
282 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
283 ret
--; /* length without 0 */
289 /***********************************************************************
290 * GetTempPathW (KERNEL32.@)
292 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
294 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
295 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
296 WCHAR tmp_path
[MAX_PATH
];
299 TRACE("%u,%p\n", count
, path
);
301 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
302 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
303 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
308 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
312 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
315 if (ret
> MAX_PATH
- 2)
317 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
321 if (tmp_path
[ret
-1] != '\\')
323 tmp_path
[ret
++] = '\\';
324 tmp_path
[ret
] = '\0';
327 ret
++; /* add space for terminating 0 */
331 lstrcpynW(path
, tmp_path
, count
);
333 ret
--; /* return length without 0 */
335 path
[0] = 0; /* avoid returning ambiguous "X:" */
338 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
343 /***********************************************************************
344 * GetTempDrive (KERNEL.92)
345 * A closer look at krnl386.exe shows what the SDK doesn't mention:
349 * AH: ':' - yes, some kernel code even does stosw with
353 UINT WINAPI
GetTempDrive( BYTE ignored
)
357 UINT len
= GetTempPathA( 0, NULL
);
359 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
360 ret
= DRIVE_GetCurrentDrive() + 'A';
363 /* FIXME: apparently Windows does something with the ignored byte */
364 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
365 ret
= toupper(buffer
[0]);
366 HeapFree( GetProcessHeap(), 0, buffer
);
368 return MAKELONG( ret
| (':' << 8), 1 );
372 /***********************************************************************
373 * GetWindowsDirectoryW (KERNEL32.@)
375 * See comment for GetWindowsDirectoryA.
377 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
379 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
380 if (path
&& count
>= len
)
382 strcpyW( path
, DIR_Windows
.short_name
);
389 /***********************************************************************
390 * GetWindowsDirectoryA (KERNEL32.@)
393 * If buffer is large enough to hold full path and terminating '\0' character
394 * function copies path to buffer and returns length of the path without '\0'.
395 * Otherwise function returns required size including '\0' character and
396 * does not touch the buffer.
398 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
400 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
401 if (path
&& count
>= len
)
403 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
410 /***********************************************************************
411 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
413 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
415 return GetWindowsDirectoryA( path
, count
);
419 /***********************************************************************
420 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
422 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
424 return GetWindowsDirectoryW( path
, count
);
428 /***********************************************************************
429 * GetSystemDirectoryW (KERNEL32.@)
431 * See comment for GetWindowsDirectoryA.
433 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
435 UINT len
= strlenW( DIR_System
.short_name
) + 1;
436 if (path
&& count
>= len
)
438 strcpyW( path
, DIR_System
.short_name
);
445 /***********************************************************************
446 * GetSystemDirectoryA (KERNEL32.@)
448 * See comment for GetWindowsDirectoryA.
450 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
452 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
453 if (path
&& count
>= len
)
455 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
462 /***********************************************************************
463 * CreateDirectoryW (KERNEL32.@)
467 * ERROR_DISK_FULL: on full disk
468 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
469 * ERROR_ACCESS_DENIED: on permission problems
470 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
472 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
473 LPSECURITY_ATTRIBUTES lpsecattribs
)
475 DOS_FULL_NAME full_name
;
479 SetLastError(ERROR_PATH_NOT_FOUND
);
483 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
485 if (RtlIsDosDeviceName_U( path
))
487 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
488 SetLastError( ERROR_ACCESS_DENIED
);
491 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
492 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
493 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
494 /* the FILE_SetDosError() generated error codes don't match the
495 * CreateDirectory ones for some errnos */
499 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
500 SetLastError(ERROR_ACCESS_DENIED
);
502 SetLastError(ERROR_ALREADY_EXISTS
);
505 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
506 default: FILE_SetDosError();break;
514 /***********************************************************************
515 * CreateDirectoryA (KERNEL32.@)
517 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
518 LPSECURITY_ATTRIBUTES lpsecattribs
)
520 UNICODE_STRING pathW
;
525 SetLastError(ERROR_PATH_NOT_FOUND
);
529 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
531 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
532 RtlFreeUnicodeString(&pathW
);
535 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
540 /***********************************************************************
541 * CreateDirectoryExA (KERNEL32.@)
543 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
544 LPSECURITY_ATTRIBUTES lpsecattribs
)
546 return CreateDirectoryA(path
,lpsecattribs
);
550 /***********************************************************************
551 * CreateDirectoryExW (KERNEL32.@)
553 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
554 LPSECURITY_ATTRIBUTES lpsecattribs
)
556 return CreateDirectoryW(path
,lpsecattribs
);
560 /***********************************************************************
561 * RemoveDirectoryW (KERNEL32.@)
563 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
565 DOS_FULL_NAME full_name
;
569 SetLastError(ERROR_INVALID_PARAMETER
);
573 TRACE_(file
)("%s\n", debugstr_w(path
));
575 if (RtlIsDosDeviceName_U( path
))
577 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
578 SetLastError( ERROR_FILE_NOT_FOUND
);
581 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
582 if (rmdir( full_name
.long_name
) == -1)
591 /***********************************************************************
592 * RemoveDirectoryA (KERNEL32.@)
594 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
596 UNICODE_STRING pathW
;
601 SetLastError(ERROR_INVALID_PARAMETER
);
605 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
607 ret
= RemoveDirectoryW(pathW
.Buffer
);
608 RtlFreeUnicodeString(&pathW
);
611 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
616 /***********************************************************************
619 * Helper function for DIR_SearchPath.
621 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
622 DOS_FULL_NAME
*full_name
)
624 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
625 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
627 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
628 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
630 SetLastError( ERROR_PATH_NOT_FOUND
);
633 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
634 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
), p_s
))
637 full_name
->drive
= dir
->drive
;
638 strcpy( full_name
->long_name
, dir
->long_name
);
640 strcpyW( full_name
->short_name
, dir
->short_name
);
645 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
647 LPWSTR next
, buffer
= NULL
;
648 INT len
= strlenW(name
), newlen
, currlen
= 0;
654 static const WCHAR bkslashW
[] = {'\\',0};
656 while (*cur
== ';') cur
++;
658 next
= strchrW( cur
, ';' );
659 if (next
) *next
++ = '\0';
660 newlen
= strlenW(cur
) + len
+ 2;
662 if (newlen
> currlen
)
665 buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
));
667 buffer
= HeapAlloc( GetProcessHeap(), 0, newlen
* sizeof(WCHAR
));
674 strcpyW( buffer
, cur
);
675 strcatW( buffer
, bkslashW
);
676 strcatW( buffer
, name
);
677 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
680 HeapFree( GetProcessHeap(), 0, buffer
);
685 /***********************************************************************
686 * DIR_TryEnvironmentPath
688 * Helper function for DIR_SearchPath.
689 * Search in the specified path, or in $PATH if NULL.
691 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
696 static const WCHAR pathW
[] = {'P','A','T','H',0};
698 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
699 if (!size
) return FALSE
;
700 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
701 if (envpath
) strcpyW( path
, envpath
);
702 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
704 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
707 HeapFree( GetProcessHeap(), 0, path
);
712 /***********************************************************************
715 * Helper function for DIR_SearchPath.
717 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
719 WCHAR bufferW
[MAX_PATH
];
724 char buffer
[OFS_MAXPATHNAME
];
725 if (!GetCurrentTask()) return FALSE
;
726 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
728 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
730 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
733 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
734 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
736 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
740 /***********************************************************************
743 * Implementation of SearchPathA. 'win32' specifies whether the search
744 * order is Win16 (module path last) or Win32 (module path first).
746 * FIXME: should return long path names.
748 DWORD
DIR_SearchPath( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
749 DOS_FULL_NAME
*full_name
, BOOL win32
)
755 /* First check the supplied parameters */
757 p
= strrchrW( name
, '.' );
758 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
759 ext
= NULL
; /* Ignore the specified extension */
760 if (FILE_contains_pathW (name
))
761 path
= NULL
; /* Ignore path if name already contains a path */
762 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
764 /* Allocate a buffer for the file name and extension */
768 DWORD len
= strlenW(name
) + strlenW(ext
);
769 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
771 SetLastError( ERROR_OUTOFMEMORY
);
774 strcpyW( tmp
, name
);
779 /* If the name contains an explicit path, everything's easy */
781 if (FILE_contains_pathW(name
))
783 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
787 /* Search in the specified path */
791 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
795 /* Try the path of the current executable (for Win32 search order) */
797 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
799 /* Try the current directory */
801 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
803 /* Try the Windows system directory */
805 if (DIR_TryPath( &DIR_System
, name
, full_name
))
808 /* Try the Windows directory */
810 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
813 /* Try the path of the current executable (for Win16 search order) */
815 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
817 /* Try all directories in path */
819 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
822 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
827 /***********************************************************************
828 * SearchPathW [KERNEL32.@]
830 * Searches for a specified file in the search path.
833 * path [I] Path to search
834 * name [I] Filename to search for.
835 * ext [I] File extension to append to file name. The first
836 * character must be a period. This parameter is
837 * specified only if the filename given does not
838 * contain an extension.
839 * buflen [I] size of buffer, in characters
840 * buffer [O] buffer for found filename
841 * lastpart [O] address of pointer to last used character in
842 * buffer (the final '\')
845 * Success: length of string copied into buffer, not including
846 * terminating null character. If the filename found is
847 * longer than the length of the buffer, the length of the
848 * filename is returned.
852 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
855 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
856 LPWSTR buffer
, LPWSTR
*lastpart
)
859 DOS_FULL_NAME full_name
;
861 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
863 SetLastError(ERROR_FILE_NOT_FOUND
);
867 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
868 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
870 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
871 res
= full_name
.long_name
+
872 strlen(DRIVE_GetRoot( full_name
.drive
));
873 while (*res
== '/') res
++;
879 MultiByteToWideChar(CP_UNIXCP
, 0, res
, -1, buffer
+ 3, buflen
- 3);
880 buffer
[buflen
- 1] = 0;
882 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
883 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
885 TRACE("Returning %s\n", debugstr_w(buffer
) );
886 return strlenW(buffer
);
890 /***********************************************************************
891 * SearchPathA (KERNEL32.@)
893 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
894 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
896 UNICODE_STRING pathW
, nameW
, extW
;
897 WCHAR bufferW
[MAX_PATH
];
900 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
901 else pathW
.Buffer
= NULL
;
902 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
903 else nameW
.Buffer
= NULL
;
904 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
905 else extW
.Buffer
= NULL
;
907 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
911 else if (retW
> MAX_PATH
)
913 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
918 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
921 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
922 ret
--; /* length without 0 */
923 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
927 RtlFreeUnicodeString(&pathW
);
928 RtlFreeUnicodeString(&nameW
);
929 RtlFreeUnicodeString(&extW
);