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"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
54 WINE_DECLARE_DEBUG_CHANNEL(file
);
56 static DOS_FULL_NAME DIR_Windows
;
57 static DOS_FULL_NAME DIR_System
;
59 static const WCHAR wineW
[] = {'w','i','n','e',0};
61 /***********************************************************************
64 inline static int FILE_contains_pathW (LPCWSTR name
)
66 return ((*name
&& (name
[1] == ':')) ||
67 strchrW (name
, '/') || strchrW (name
, '\\'));
70 /***********************************************************************
73 * Get a path name from the wine.ini file and make sure it is valid.
75 static int DIR_GetPath( HKEY hkey
, LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
76 LPWSTR longname
, INT longname_len
, BOOL warn
)
80 WCHAR tmp
[MAX_PATHNAME_LEN
];
81 BY_HANDLE_FILE_INFORMATION info
;
82 const WCHAR
*path
= defval
;
83 const char *mess
= "does not exist";
85 RtlInitUnicodeString( &nameW
, keyname
);
86 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
87 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
89 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
90 (!FILE_Stat( full_name
->long_name
, &info
, NULL
) && (mess
=strerror(errno
)))||
91 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
92 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
96 MESSAGE("Invalid path %s for %s directory: %s.\n",
97 debugstr_w(path
), debugstr_w(keyname
), mess
);
98 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
99 wine_get_config_dir());
107 /***********************************************************************
112 OBJECT_ATTRIBUTES attr
;
113 UNICODE_STRING nameW
;
115 char path
[MAX_PATHNAME_LEN
];
116 WCHAR longpath
[MAX_PATHNAME_LEN
];
117 DOS_FULL_NAME tmp_dir
, profile_dir
;
120 static const WCHAR wineW
[] = {'M','a','c','h','i','n','e','\\',
121 'S','o','f','t','w','a','r','e','\\',
122 'W','i','n','e','\\','W','i','n','e','\\',
123 'C','o','n','f','i','g','\\','W','i','n','e',0};
124 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
125 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
126 static const WCHAR tempW
[] = {'t','e','m','p',0};
127 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
128 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
129 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
130 static const WCHAR pathW
[] = {'p','a','t','h',0};
131 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
132 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
133 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
134 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
135 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
136 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
137 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
138 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
139 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
140 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
141 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
142 static const WCHAR empty_strW
[] = { 0 };
144 if (!getcwd( path
, MAX_PATHNAME_LEN
))
146 perror( "Could not get current directory" );
150 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
152 MESSAGE("Warning: could not find wine config [Drive x] entry "
153 "for current working directory %s; "
154 "starting in windows directory.\n", cwd
);
158 WCHAR szdrive
[3]={drive
+'A',':',0};
159 MultiByteToWideChar(DRIVE_GetCodepage(drive
), 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
160 DRIVE_SetCurrentDrive( drive
);
161 DRIVE_Chdir( drive
, longpath
);
162 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
163 chdir("/"); /* change to root directory so as not to lock cdroms */
166 attr
.Length
= sizeof(attr
);
167 attr
.RootDirectory
= 0;
168 attr
.ObjectName
= &nameW
;
170 attr
.SecurityDescriptor
= NULL
;
171 attr
.SecurityQualityOfService
= NULL
;
173 RtlInitUnicodeString( &nameW
, wineW
);
174 if (NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) hkey
= 0;
176 if (!(DIR_GetPath( hkey
, windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
177 !(DIR_GetPath( hkey
, systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
178 !(DIR_GetPath( hkey
, tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
180 if (hkey
) NtClose( hkey
);
183 if (-1 == access( tmp_dir
.long_name
, W_OK
))
187 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
188 "configuration file (%s) is not writeable.\n",
189 tmp_dir
.long_name
, wine_get_config_dir() );
192 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
193 tmp_dir
.long_name
, strerror(errno
));
198 drive
= DIR_Windows
.drive
;
199 DRIVE_SetCurrentDrive( drive
);
200 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
203 /* Set the environment variables */
205 /* set COMSPEC only if it doesn't exist already */
206 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
208 strcpyW( longpath
, DIR_System
.short_name
);
209 strcatW( longpath
, wcmdW
);
210 SetEnvironmentVariableW( comspecW
, longpath
);
213 /* set PATH only if not set already */
214 if (!GetEnvironmentVariableW( path_capsW
, NULL
, 0 ))
216 WCHAR tmp
[MAX_PATHNAME_LEN
];
218 const WCHAR
*path
= path_dirW
;
220 RtlInitUnicodeString( &nameW
, pathW
);
221 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
222 tmp
, sizeof(tmp
), &dummy
))
224 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
227 if (strchrW(path
, '/'))
229 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
232 SetEnvironmentVariableW( path_capsW
, path
);
233 TRACE("Path = %s\n", debugstr_w(path
) );
236 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
237 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
238 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
239 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
241 TRACE("WindowsDir = %s (%s)\n",
242 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
243 TRACE("SystemDir = %s (%s)\n",
244 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
245 TRACE("TempDir = %s (%s)\n",
246 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
247 TRACE("Cwd = %c:\\%s\n",
248 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
250 if (DIR_GetPath( hkey
, profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
252 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
253 SetEnvironmentVariableW( userprofileW
, longpath
);
256 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
257 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
258 if (hkey
) NtClose( hkey
);
264 /***********************************************************************
265 * GetTempPathA (KERNEL32.@)
267 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
269 WCHAR pathW
[MAX_PATH
];
272 ret
= GetTempPathW(MAX_PATH
, pathW
);
279 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
283 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
286 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
287 ret
--; /* length without 0 */
293 /***********************************************************************
294 * GetTempPathW (KERNEL32.@)
296 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
298 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
299 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
300 WCHAR tmp_path
[MAX_PATH
];
303 TRACE("%u,%p\n", count
, path
);
305 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
306 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
307 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
312 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
316 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
319 if (ret
> MAX_PATH
- 2)
321 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
325 if (tmp_path
[ret
-1] != '\\')
327 tmp_path
[ret
++] = '\\';
328 tmp_path
[ret
] = '\0';
331 ret
++; /* add space for terminating 0 */
335 lstrcpynW(path
, tmp_path
, count
);
337 ret
--; /* return length without 0 */
339 path
[0] = 0; /* avoid returning ambiguous "X:" */
342 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
347 /***********************************************************************
348 * DIR_GetWindowsUnixDir
350 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
352 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
353 return strlen( DIR_Windows
.long_name
);
357 /***********************************************************************
358 * DIR_GetSystemUnixDir
360 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
362 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
363 return strlen( DIR_System
.long_name
);
367 /***********************************************************************
368 * GetTempDrive (KERNEL.92)
369 * A closer look at krnl386.exe shows what the SDK doesn't mention:
373 * AH: ':' - yes, some kernel code even does stosw with
377 UINT WINAPI
GetTempDrive( BYTE ignored
)
381 UINT len
= GetTempPathA( 0, NULL
);
383 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
384 ret
= DRIVE_GetCurrentDrive() + 'A';
387 /* FIXME: apparently Windows does something with the ignored byte */
388 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
389 ret
= toupper(buffer
[0]);
390 HeapFree( GetProcessHeap(), 0, buffer
);
392 return MAKELONG( ret
| (':' << 8), 1 );
396 /***********************************************************************
397 * GetWindowsDirectory (KERNEL.134)
399 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
401 return (UINT16
)GetWindowsDirectoryA( path
, count
);
405 /***********************************************************************
406 * GetWindowsDirectoryW (KERNEL32.@)
408 * See comment for GetWindowsDirectoryA.
410 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
412 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
413 if (path
&& count
>= len
)
415 strcpyW( path
, DIR_Windows
.short_name
);
422 /***********************************************************************
423 * GetWindowsDirectoryA (KERNEL32.@)
426 * If buffer is large enough to hold full path and terminating '\0' character
427 * function copies path to buffer and returns length of the path without '\0'.
428 * Otherwise function returns required size including '\0' character and
429 * does not touch the buffer.
431 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
433 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
434 if (path
&& count
>= len
)
436 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
443 /***********************************************************************
444 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
446 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
448 return GetWindowsDirectoryA( path
, count
);
452 /***********************************************************************
453 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
455 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
457 return GetWindowsDirectoryW( path
, count
);
461 /***********************************************************************
462 * GetSystemDirectory (KERNEL.135)
464 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
466 return (UINT16
)GetSystemDirectoryA( path
, count
);
470 /***********************************************************************
471 * GetSystemDirectoryW (KERNEL32.@)
473 * See comment for GetWindowsDirectoryA.
475 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
477 UINT len
= strlenW( DIR_System
.short_name
) + 1;
478 if (path
&& count
>= len
)
480 strcpyW( path
, DIR_System
.short_name
);
487 /***********************************************************************
488 * GetSystemDirectoryA (KERNEL32.@)
490 * See comment for GetWindowsDirectoryA.
492 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
494 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
495 if (path
&& count
>= len
)
497 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
504 /***********************************************************************
505 * CreateDirectory (KERNEL.144)
507 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
509 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
510 return (BOOL16
)CreateDirectoryA( path
, NULL
);
514 /***********************************************************************
515 * CreateDirectoryW (KERNEL32.@)
519 * ERROR_DISK_FULL: on full disk
520 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
521 * ERROR_ACCESS_DENIED: on permission problems
522 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
524 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
525 LPSECURITY_ATTRIBUTES lpsecattribs
)
527 DOS_FULL_NAME full_name
;
531 SetLastError(ERROR_PATH_NOT_FOUND
);
535 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
537 if (DOSFS_GetDevice( path
))
539 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
540 SetLastError( ERROR_ACCESS_DENIED
);
543 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
544 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
545 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
546 /* the FILE_SetDosError() generated error codes don't match the
547 * CreateDirectory ones for some errnos */
551 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
552 SetLastError(ERROR_ACCESS_DENIED
);
554 SetLastError(ERROR_ALREADY_EXISTS
);
557 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
558 default: FILE_SetDosError();break;
566 /***********************************************************************
567 * CreateDirectoryA (KERNEL32.@)
569 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
570 LPSECURITY_ATTRIBUTES lpsecattribs
)
572 UNICODE_STRING pathW
;
577 SetLastError(ERROR_PATH_NOT_FOUND
);
581 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
583 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
584 RtlFreeUnicodeString(&pathW
);
587 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
592 /***********************************************************************
593 * CreateDirectoryExA (KERNEL32.@)
595 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
596 LPSECURITY_ATTRIBUTES lpsecattribs
)
598 return CreateDirectoryA(path
,lpsecattribs
);
602 /***********************************************************************
603 * CreateDirectoryExW (KERNEL32.@)
605 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
606 LPSECURITY_ATTRIBUTES lpsecattribs
)
608 return CreateDirectoryW(path
,lpsecattribs
);
612 /***********************************************************************
613 * RemoveDirectory (KERNEL.145)
615 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
617 return (BOOL16
)RemoveDirectoryA( path
);
621 /***********************************************************************
622 * RemoveDirectoryW (KERNEL32.@)
624 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
626 DOS_FULL_NAME full_name
;
630 SetLastError(ERROR_INVALID_PARAMETER
);
634 TRACE_(file
)("%s\n", debugstr_w(path
));
636 if (DOSFS_GetDevice( path
))
638 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
639 SetLastError( ERROR_FILE_NOT_FOUND
);
642 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
643 if (rmdir( full_name
.long_name
) == -1)
652 /***********************************************************************
653 * RemoveDirectoryA (KERNEL32.@)
655 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
657 UNICODE_STRING pathW
;
662 SetLastError(ERROR_INVALID_PARAMETER
);
666 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
668 ret
= RemoveDirectoryW(pathW
.Buffer
);
669 RtlFreeUnicodeString(&pathW
);
672 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
677 /***********************************************************************
680 * Helper function for DIR_SearchPath.
682 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
683 DOS_FULL_NAME
*full_name
)
685 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
686 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
688 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
689 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
691 SetLastError( ERROR_PATH_NOT_FOUND
);
694 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
695 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
696 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
699 full_name
->drive
= dir
->drive
;
700 strcpy( full_name
->long_name
, dir
->long_name
);
702 strcpyW( full_name
->short_name
, dir
->short_name
);
707 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
709 LPWSTR next
, buffer
= NULL
;
710 INT len
= strlenW(name
), newlen
, currlen
= 0;
716 static const WCHAR bkslashW
[] = {'\\',0};
718 while (*cur
== ';') cur
++;
720 next
= strchrW( cur
, ';' );
721 if (next
) *next
++ = '\0';
722 newlen
= strlenW(cur
) + len
+ 2;
724 if (newlen
> currlen
)
726 if (!(buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
))))
731 strcpyW( buffer
, cur
);
732 strcatW( buffer
, bkslashW
);
733 strcatW( buffer
, name
);
734 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
737 HeapFree( GetProcessHeap(), 0, buffer
);
742 /***********************************************************************
743 * DIR_TryEnvironmentPath
745 * Helper function for DIR_SearchPath.
746 * Search in the specified path, or in $PATH if NULL.
748 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
753 static const WCHAR pathW
[] = {'P','A','T','H',0};
755 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
756 if (!size
) return FALSE
;
757 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
758 if (envpath
) strcpyW( path
, envpath
);
759 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
761 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
764 HeapFree( GetProcessHeap(), 0, path
);
769 /***********************************************************************
772 * Helper function for DIR_SearchPath.
774 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
776 WCHAR bufferW
[MAX_PATH
];
781 char buffer
[OFS_MAXPATHNAME
];
782 if (!GetCurrentTask()) return FALSE
;
783 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
785 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
787 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
790 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
791 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
793 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
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 all directories in path */
876 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
879 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
884 /***********************************************************************
885 * SearchPathW [KERNEL32.@]
887 * Searches for a specified file in the search path.
890 * path [I] Path to search
891 * name [I] Filename to search for.
892 * ext [I] File extension to append to file name. The first
893 * character must be a period. This parameter is
894 * specified only if the filename given does not
895 * contain an extension.
896 * buflen [I] size of buffer, in characters
897 * buffer [O] buffer for found filename
898 * lastpart [O] address of pointer to last used character in
899 * buffer (the final '\')
902 * Success: length of string copied into buffer, not including
903 * terminating null character. If the filename found is
904 * longer than the length of the buffer, the length of the
905 * filename is returned.
909 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
912 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
913 LPWSTR buffer
, LPWSTR
*lastpart
)
916 DOS_FULL_NAME full_name
;
918 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
920 SetLastError(ERROR_FILE_NOT_FOUND
);
924 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
925 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
927 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
928 res
= full_name
.long_name
+
929 strlen(DRIVE_GetRoot( full_name
.drive
));
930 while (*res
== '/') res
++;
936 MultiByteToWideChar(DRIVE_GetCodepage(full_name
.drive
), 0,
937 res
, -1, buffer
+ 3, buflen
- 3);
938 buffer
[buflen
- 1] = 0;
940 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
941 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
943 TRACE("Returning %s\n", debugstr_w(buffer
) );
944 return strlenW(buffer
);
948 /***********************************************************************
949 * SearchPathA (KERNEL32.@)
951 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
952 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
954 UNICODE_STRING pathW
, nameW
, extW
;
955 WCHAR bufferW
[MAX_PATH
];
958 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
959 else pathW
.Buffer
= NULL
;
960 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
961 else nameW
.Buffer
= NULL
;
962 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
963 else extW
.Buffer
= NULL
;
965 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
969 else if (retW
> MAX_PATH
)
971 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
976 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
979 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
980 ret
--; /* length without 0 */
981 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
985 RtlFreeUnicodeString(&pathW
);
986 RtlFreeUnicodeString(&nameW
);
987 RtlFreeUnicodeString(&extW
);
992 /***********************************************************************
993 * search_alternate_path
996 * FIXME: should return long path names.?
998 static BOOL
search_alternate_path(LPCWSTR dll_path
, LPCWSTR name
, LPCWSTR ext
,
999 DOS_FULL_NAME
*full_name
)
1005 /* First check the supplied parameters */
1007 p
= strrchrW( name
, '.' );
1008 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
1009 ext
= NULL
; /* Ignore the specified extension */
1011 /* Allocate a buffer for the file name and extension */
1015 DWORD len
= strlenW(name
) + strlenW(ext
);
1016 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1018 SetLastError( ERROR_OUTOFMEMORY
);
1021 strcpyW( tmp
, name
);
1022 strcatW( tmp
, ext
);
1026 if (DIR_TryEnvironmentPath (name
, full_name
, dll_path
))
1028 else if (DOSFS_GetFullName (name
, TRUE
, full_name
)) /* current dir */
1030 else if (DIR_TryPath (&DIR_System
, name
, full_name
)) /* System dir */
1032 else if (DIR_TryPath (&DIR_Windows
, name
, full_name
)) /* Windows dir */
1035 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
1037 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
1042 /***********************************************************************
1043 * DIR_SearchAlternatePath
1045 * Searches for a specified file in the search path.
1048 * dll_path [I] Path to search
1049 * name [I] Filename to search for.
1050 * ext [I] File extension to append to file name. The first
1051 * character must be a period. This parameter is
1052 * specified only if the filename given does not
1053 * contain an extension.
1054 * buflen [I] size of buffer, in characters
1055 * buffer [O] buffer for found filename
1058 * Success: length of string copied into buffer, not including
1059 * terminating null character. If the filename found is
1060 * longer than the length of the buffer, the length of the
1061 * filename is returned.
1065 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1067 * FIXME: convert to unicode
1069 DWORD
DIR_SearchAlternatePath( LPCWSTR dll_path
, LPCWSTR name
, LPCWSTR ext
,
1070 DWORD buflen
, LPWSTR buffer
)
1072 DOS_FULL_NAME full_name
;
1075 if (search_alternate_path( dll_path
, name
, ext
, &full_name
))
1077 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
1078 ret
= strlenW(buffer
);
1081 SetLastError(ERROR_FILE_NOT_FOUND
);
1083 TRACE("Returning %ld\n", ret
);