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"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
53 WINE_DECLARE_DEBUG_CHANNEL(file
);
55 static DOS_FULL_NAME DIR_Windows
;
56 static DOS_FULL_NAME DIR_System
;
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( HKEY hkey
, LPCWSTR keyname
, LPCWSTR defval
, DOS_FULL_NAME
*full_name
,
73 LPWSTR longname
, INT longname_len
, BOOL warn
)
77 WCHAR tmp
[MAX_PATHNAME_LEN
];
78 BY_HANDLE_FILE_INFORMATION info
;
79 const WCHAR
*path
= defval
;
80 const char *mess
= "does not exist";
82 RtlInitUnicodeString( &nameW
, keyname
);
83 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
84 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
86 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
87 (!FILE_Stat( full_name
->long_name
, &info
, NULL
) && (mess
=strerror(errno
)))||
88 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
89 (!(GetLongPathNameW(full_name
->short_name
, longname
, longname_len
))) )
93 MESSAGE("Invalid path %s for %s directory: %s.\n",
94 debugstr_w(path
), debugstr_w(keyname
), mess
);
95 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
96 wine_get_config_dir());
104 /***********************************************************************
109 OBJECT_ATTRIBUTES attr
;
110 UNICODE_STRING nameW
;
112 char path
[MAX_PATHNAME_LEN
];
113 WCHAR longpath
[MAX_PATHNAME_LEN
];
114 DOS_FULL_NAME tmp_dir
, profile_dir
;
117 static const WCHAR wineW
[] = {'M','a','c','h','i','n','e','\\',
118 'S','o','f','t','w','a','r','e','\\',
119 'W','i','n','e','\\','W','i','n','e','\\',
120 'C','o','n','f','i','g','\\','W','i','n','e',0};
121 static const WCHAR windowsW
[] = {'w','i','n','d','o','w','s',0};
122 static const WCHAR systemW
[] = {'s','y','s','t','e','m',0};
123 static const WCHAR tempW
[] = {'t','e','m','p',0};
124 static const WCHAR profileW
[] = {'p','r','o','f','i','l','e',0};
125 static const WCHAR windows_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',0};
126 static const WCHAR system_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
127 static const WCHAR pathW
[] = {'p','a','t','h',0};
128 static const WCHAR path_dirW
[] = {'c',':','\\','w','i','n','d','o','w','s',';',
129 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
130 static const WCHAR path_capsW
[] = {'P','A','T','H',0};
131 static const WCHAR temp_capsW
[] = {'T','E','M','P',0};
132 static const WCHAR tmp_capsW
[] = {'T','M','P',0};
133 static const WCHAR windirW
[] = {'w','i','n','d','i','r',0};
134 static const WCHAR winsysdirW
[] = {'w','i','n','s','y','s','d','i','r',0};
135 static const WCHAR userprofileW
[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
136 static const WCHAR systemrootW
[] = {'S','Y','S','T','E','M','R','O','O','T',0};
137 static const WCHAR wcmdW
[] = {'\\','w','c','m','d','.','e','x','e',0};
138 static const WCHAR comspecW
[] = {'C','O','M','S','P','E','C',0};
139 static const WCHAR empty_strW
[] = { 0 };
141 if (!getcwd( path
, MAX_PATHNAME_LEN
))
143 perror( "Could not get current directory" );
147 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
149 MESSAGE("Warning: could not find wine config [Drive x] entry "
150 "for current working directory %s; "
151 "starting in windows directory.\n", cwd
);
155 WCHAR szdrive
[3]={drive
+'A',':',0};
156 MultiByteToWideChar(CP_UNIXCP
, 0, cwd
, -1, longpath
, MAX_PATHNAME_LEN
);
157 DRIVE_SetCurrentDrive( drive
);
158 DRIVE_Chdir( drive
, longpath
);
159 if(GetDriveTypeW(szdrive
)==DRIVE_CDROM
)
160 chdir("/"); /* change to root directory so as not to lock cdroms */
163 attr
.Length
= sizeof(attr
);
164 attr
.RootDirectory
= 0;
165 attr
.ObjectName
= &nameW
;
167 attr
.SecurityDescriptor
= NULL
;
168 attr
.SecurityQualityOfService
= NULL
;
170 RtlInitUnicodeString( &nameW
, wineW
);
171 if (NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
)) hkey
= 0;
173 if (!(DIR_GetPath( hkey
, windowsW
, windows_dirW
, &DIR_Windows
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
174 !(DIR_GetPath( hkey
, systemW
, system_dirW
, &DIR_System
, longpath
, MAX_PATHNAME_LEN
, TRUE
)) ||
175 !(DIR_GetPath( hkey
, tempW
, windows_dirW
, &tmp_dir
, longpath
, MAX_PATHNAME_LEN
, TRUE
)))
177 if (hkey
) NtClose( hkey
);
180 if (-1 == access( tmp_dir
.long_name
, W_OK
))
184 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
185 "configuration file (%s) is not writeable.\n",
186 tmp_dir
.long_name
, wine_get_config_dir() );
189 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
190 tmp_dir
.long_name
, strerror(errno
));
195 drive
= DIR_Windows
.drive
;
196 DRIVE_SetCurrentDrive( drive
);
197 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
200 /* Set the environment variables */
202 /* set COMSPEC only if it doesn't exist already */
203 if (!GetEnvironmentVariableW( comspecW
, NULL
, 0 ))
205 strcpyW( longpath
, DIR_System
.short_name
);
206 strcatW( longpath
, wcmdW
);
207 SetEnvironmentVariableW( comspecW
, longpath
);
210 /* set PATH only if not set already */
211 if (!GetEnvironmentVariableW( path_capsW
, NULL
, 0 ))
213 WCHAR tmp
[MAX_PATHNAME_LEN
];
215 const WCHAR
*path
= path_dirW
;
217 RtlInitUnicodeString( &nameW
, pathW
);
218 if (hkey
&& !NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
,
219 tmp
, sizeof(tmp
), &dummy
))
221 path
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
224 if (strchrW(path
, '/'))
226 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
229 SetEnvironmentVariableW( path_capsW
, path
);
230 TRACE("Path = %s\n", debugstr_w(path
) );
233 SetEnvironmentVariableW( temp_capsW
, tmp_dir
.short_name
);
234 SetEnvironmentVariableW( tmp_capsW
, tmp_dir
.short_name
);
235 SetEnvironmentVariableW( windirW
, DIR_Windows
.short_name
);
236 SetEnvironmentVariableW( winsysdirW
, DIR_System
.short_name
);
238 TRACE("WindowsDir = %s (%s)\n",
239 debugstr_w(DIR_Windows
.short_name
), DIR_Windows
.long_name
);
240 TRACE("SystemDir = %s (%s)\n",
241 debugstr_w(DIR_System
.short_name
), DIR_System
.long_name
);
242 TRACE("TempDir = %s (%s)\n",
243 debugstr_w(tmp_dir
.short_name
), tmp_dir
.long_name
);
244 TRACE("Cwd = %c:\\%s\n",
245 'A' + drive
, debugstr_w(DRIVE_GetDosCwd(drive
)) );
247 if (DIR_GetPath( hkey
, profileW
, empty_strW
, &profile_dir
, longpath
, MAX_PATHNAME_LEN
, FALSE
))
249 TRACE("USERPROFILE= %s\n", debugstr_w(longpath
) );
250 SetEnvironmentVariableW( userprofileW
, longpath
);
253 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows
.short_name
) );
254 SetEnvironmentVariableW( systemrootW
, DIR_Windows
.short_name
);
255 if (hkey
) NtClose( hkey
);
261 /***********************************************************************
262 * GetTempPathA (KERNEL32.@)
264 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
266 WCHAR pathW
[MAX_PATH
];
269 ret
= GetTempPathW(MAX_PATH
, pathW
);
276 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
280 ret
= WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, NULL
, 0, NULL
, NULL
);
283 WideCharToMultiByte(CP_ACP
, 0, pathW
, -1, path
, count
, NULL
, NULL
);
284 ret
--; /* length without 0 */
290 /***********************************************************************
291 * GetTempPathW (KERNEL32.@)
293 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
295 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
296 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
297 WCHAR tmp_path
[MAX_PATH
];
300 TRACE("%u,%p\n", count
, path
);
302 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)))
303 if (!(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)))
304 if (!(ret
= GetCurrentDirectoryW( MAX_PATH
, tmp_path
)))
309 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
313 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
316 if (ret
> MAX_PATH
- 2)
318 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
322 if (tmp_path
[ret
-1] != '\\')
324 tmp_path
[ret
++] = '\\';
325 tmp_path
[ret
] = '\0';
328 ret
++; /* add space for terminating 0 */
332 lstrcpynW(path
, tmp_path
, count
);
334 ret
--; /* return length without 0 */
336 path
[0] = 0; /* avoid returning ambiguous "X:" */
339 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
344 /***********************************************************************
345 * DIR_GetWindowsUnixDir
347 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
349 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
350 return strlen( DIR_Windows
.long_name
);
354 /***********************************************************************
355 * DIR_GetSystemUnixDir
357 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
359 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
360 return strlen( DIR_System
.long_name
);
364 /***********************************************************************
365 * GetTempDrive (KERNEL.92)
366 * A closer look at krnl386.exe shows what the SDK doesn't mention:
370 * AH: ':' - yes, some kernel code even does stosw with
374 UINT WINAPI
GetTempDrive( BYTE ignored
)
378 UINT len
= GetTempPathA( 0, NULL
);
380 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
381 ret
= DRIVE_GetCurrentDrive() + 'A';
384 /* FIXME: apparently Windows does something with the ignored byte */
385 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
386 ret
= toupper(buffer
[0]);
387 HeapFree( GetProcessHeap(), 0, buffer
);
389 return MAKELONG( ret
| (':' << 8), 1 );
393 /***********************************************************************
394 * GetWindowsDirectory (KERNEL.134)
396 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
398 return (UINT16
)GetWindowsDirectoryA( path
, count
);
402 /***********************************************************************
403 * GetWindowsDirectoryW (KERNEL32.@)
405 * See comment for GetWindowsDirectoryA.
407 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
409 UINT len
= strlenW( DIR_Windows
.short_name
) + 1;
410 if (path
&& count
>= len
)
412 strcpyW( path
, DIR_Windows
.short_name
);
419 /***********************************************************************
420 * GetWindowsDirectoryA (KERNEL32.@)
423 * If buffer is large enough to hold full path and terminating '\0' character
424 * function copies path to buffer and returns length of the path without '\0'.
425 * Otherwise function returns required size including '\0' character and
426 * does not touch the buffer.
428 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
430 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0, NULL
, NULL
);
431 if (path
&& count
>= len
)
433 WideCharToMultiByte( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
, NULL
, NULL
);
440 /***********************************************************************
441 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
443 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
445 return GetWindowsDirectoryA( path
, count
);
449 /***********************************************************************
450 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
452 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
454 return GetWindowsDirectoryW( path
, count
);
458 /***********************************************************************
459 * GetSystemDirectory (KERNEL.135)
461 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
463 return (UINT16
)GetSystemDirectoryA( path
, count
);
467 /***********************************************************************
468 * GetSystemDirectoryW (KERNEL32.@)
470 * See comment for GetWindowsDirectoryA.
472 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
474 UINT len
= strlenW( DIR_System
.short_name
) + 1;
475 if (path
&& count
>= len
)
477 strcpyW( path
, DIR_System
.short_name
);
484 /***********************************************************************
485 * GetSystemDirectoryA (KERNEL32.@)
487 * See comment for GetWindowsDirectoryA.
489 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
491 UINT len
= WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0, NULL
, NULL
);
492 if (path
&& count
>= len
)
494 WideCharToMultiByte( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
, NULL
, NULL
);
501 /***********************************************************************
502 * CreateDirectory (KERNEL.144)
504 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
506 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
507 return (BOOL16
)CreateDirectoryA( path
, NULL
);
511 /***********************************************************************
512 * CreateDirectoryW (KERNEL32.@)
516 * ERROR_DISK_FULL: on full disk
517 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
518 * ERROR_ACCESS_DENIED: on permission problems
519 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
521 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
522 LPSECURITY_ATTRIBUTES lpsecattribs
)
524 DOS_FULL_NAME full_name
;
528 SetLastError(ERROR_PATH_NOT_FOUND
);
532 TRACE_(file
)("(%s,%p)\n", debugstr_w(path
), lpsecattribs
);
534 if (RtlIsDosDeviceName_U( path
))
536 TRACE_(file
)("cannot use device %s!\n", debugstr_w(path
));
537 SetLastError( ERROR_ACCESS_DENIED
);
540 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
541 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
542 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
543 /* the FILE_SetDosError() generated error codes don't match the
544 * CreateDirectory ones for some errnos */
548 if (!strcmp(DRIVE_GetRoot(full_name
.drive
), full_name
.long_name
))
549 SetLastError(ERROR_ACCESS_DENIED
);
551 SetLastError(ERROR_ALREADY_EXISTS
);
554 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
555 default: FILE_SetDosError();break;
563 /***********************************************************************
564 * CreateDirectoryA (KERNEL32.@)
566 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
567 LPSECURITY_ATTRIBUTES lpsecattribs
)
569 UNICODE_STRING pathW
;
574 SetLastError(ERROR_PATH_NOT_FOUND
);
578 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
580 ret
= CreateDirectoryW(pathW
.Buffer
, lpsecattribs
);
581 RtlFreeUnicodeString(&pathW
);
584 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
589 /***********************************************************************
590 * CreateDirectoryExA (KERNEL32.@)
592 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
593 LPSECURITY_ATTRIBUTES lpsecattribs
)
595 return CreateDirectoryA(path
,lpsecattribs
);
599 /***********************************************************************
600 * CreateDirectoryExW (KERNEL32.@)
602 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
603 LPSECURITY_ATTRIBUTES lpsecattribs
)
605 return CreateDirectoryW(path
,lpsecattribs
);
609 /***********************************************************************
610 * RemoveDirectory (KERNEL.145)
612 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
614 return (BOOL16
)RemoveDirectoryA( path
);
618 /***********************************************************************
619 * RemoveDirectoryW (KERNEL32.@)
621 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
623 DOS_FULL_NAME full_name
;
627 SetLastError(ERROR_INVALID_PARAMETER
);
631 TRACE_(file
)("%s\n", debugstr_w(path
));
633 if (RtlIsDosDeviceName_U( path
))
635 TRACE_(file
)("cannot remove device %s!\n", debugstr_w(path
));
636 SetLastError( ERROR_FILE_NOT_FOUND
);
639 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
640 if (rmdir( full_name
.long_name
) == -1)
649 /***********************************************************************
650 * RemoveDirectoryA (KERNEL32.@)
652 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
654 UNICODE_STRING pathW
;
659 SetLastError(ERROR_INVALID_PARAMETER
);
663 if (RtlCreateUnicodeStringFromAsciiz(&pathW
, path
))
665 ret
= RemoveDirectoryW(pathW
.Buffer
);
666 RtlFreeUnicodeString(&pathW
);
669 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
674 /***********************************************************************
677 * Helper function for DIR_SearchPath.
679 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCWSTR name
,
680 DOS_FULL_NAME
*full_name
)
682 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
683 LPWSTR p_s
= full_name
->short_name
+ strlenW(dir
->short_name
) + 1;
685 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
)/sizeof(full_name
->short_name
[0]) - 14) ||
686 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
688 SetLastError( ERROR_PATH_NOT_FOUND
);
691 if (!DOSFS_FindUnixName( dir
, name
, p_l
,
692 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
693 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
696 full_name
->drive
= dir
->drive
;
697 strcpy( full_name
->long_name
, dir
->long_name
);
699 strcpyW( full_name
->short_name
, dir
->short_name
);
704 static BOOL
DIR_SearchSemicolonedPaths(LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPWSTR pathlist
)
706 LPWSTR next
, buffer
= NULL
;
707 INT len
= strlenW(name
), newlen
, currlen
= 0;
713 static const WCHAR bkslashW
[] = {'\\',0};
715 while (*cur
== ';') cur
++;
717 next
= strchrW( cur
, ';' );
718 if (next
) *next
++ = '\0';
719 newlen
= strlenW(cur
) + len
+ 2;
721 if (newlen
> currlen
)
724 buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
* sizeof(WCHAR
));
726 buffer
= HeapAlloc( GetProcessHeap(), 0, newlen
* sizeof(WCHAR
));
733 strcpyW( buffer
, cur
);
734 strcatW( buffer
, bkslashW
);
735 strcatW( buffer
, name
);
736 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
739 HeapFree( GetProcessHeap(), 0, buffer
);
744 /***********************************************************************
745 * DIR_TryEnvironmentPath
747 * Helper function for DIR_SearchPath.
748 * Search in the specified path, or in $PATH if NULL.
750 static BOOL
DIR_TryEnvironmentPath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, LPCWSTR envpath
)
755 static const WCHAR pathW
[] = {'P','A','T','H',0};
757 size
= envpath
? strlenW(envpath
)+1 : GetEnvironmentVariableW( pathW
, NULL
, 0 );
758 if (!size
) return FALSE
;
759 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) ))) return FALSE
;
760 if (envpath
) strcpyW( path
, envpath
);
761 else if (!GetEnvironmentVariableW( pathW
, path
, size
)) goto done
;
763 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
766 HeapFree( GetProcessHeap(), 0, path
);
771 /***********************************************************************
774 * Helper function for DIR_SearchPath.
776 static BOOL
DIR_TryModulePath( LPCWSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
778 WCHAR bufferW
[MAX_PATH
];
783 char buffer
[OFS_MAXPATHNAME
];
784 if (!GetCurrentTask()) return FALSE
;
785 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
787 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, bufferW
, MAX_PATH
);
789 if (!GetModuleFileNameW( 0, bufferW
, MAX_PATH
) )
792 if (!(p
= strrchrW( bufferW
, '\\' ))) return FALSE
;
793 if (MAX_PATH
- (++p
- bufferW
) <= strlenW(name
)) return FALSE
;
795 return DOSFS_GetFullName( bufferW
, TRUE
, full_name
);
799 /***********************************************************************
802 * Implementation of SearchPathA. 'win32' specifies whether the search
803 * order is Win16 (module path last) or Win32 (module path first).
805 * FIXME: should return long path names.
807 DWORD
DIR_SearchPath( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
808 DOS_FULL_NAME
*full_name
, BOOL win32
)
814 /* First check the supplied parameters */
816 p
= strrchrW( name
, '.' );
817 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
818 ext
= NULL
; /* Ignore the specified extension */
819 if (FILE_contains_pathW (name
))
820 path
= NULL
; /* Ignore path if name already contains a path */
821 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
823 /* Allocate a buffer for the file name and extension */
827 DWORD len
= strlenW(name
) + strlenW(ext
);
828 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
830 SetLastError( ERROR_OUTOFMEMORY
);
833 strcpyW( tmp
, name
);
838 /* If the name contains an explicit path, everything's easy */
840 if (FILE_contains_pathW(name
))
842 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
846 /* Search in the specified path */
850 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
854 /* Try the path of the current executable (for Win32 search order) */
856 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
858 /* Try the current directory */
860 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
862 /* Try the Windows system directory */
864 if (DIR_TryPath( &DIR_System
, name
, full_name
))
867 /* Try the Windows directory */
869 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
872 /* Try the path of the current executable (for Win16 search order) */
874 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
876 /* Try all directories in path */
878 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
881 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
886 /***********************************************************************
887 * SearchPathW [KERNEL32.@]
889 * Searches for a specified file in the search path.
892 * path [I] Path to search
893 * name [I] Filename to search for.
894 * ext [I] File extension to append to file name. The first
895 * character must be a period. This parameter is
896 * specified only if the filename given does not
897 * contain an extension.
898 * buflen [I] size of buffer, in characters
899 * buffer [O] buffer for found filename
900 * lastpart [O] address of pointer to last used character in
901 * buffer (the final '\')
904 * Success: length of string copied into buffer, not including
905 * terminating null character. If the filename found is
906 * longer than the length of the buffer, the length of the
907 * filename is returned.
911 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
914 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
915 LPWSTR buffer
, LPWSTR
*lastpart
)
918 DOS_FULL_NAME full_name
;
920 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
922 SetLastError(ERROR_FILE_NOT_FOUND
);
926 TRACE("found %s %s\n", full_name
.long_name
, debugstr_w(full_name
.short_name
));
927 TRACE("drive %c: root %s\n", 'A' + full_name
.drive
, DRIVE_GetRoot(full_name
.drive
));
929 lstrcpynW( buffer
, full_name
.short_name
, buflen
);
930 res
= full_name
.long_name
+
931 strlen(DRIVE_GetRoot( full_name
.drive
));
932 while (*res
== '/') res
++;
938 MultiByteToWideChar(CP_UNIXCP
, 0, res
, -1, buffer
+ 3, buflen
- 3);
939 buffer
[buflen
- 1] = 0;
941 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
942 if (lastpart
) *lastpart
= strrchrW( buffer
, '\\' ) + 1;
944 TRACE("Returning %s\n", debugstr_w(buffer
) );
945 return strlenW(buffer
);
949 /***********************************************************************
950 * SearchPathA (KERNEL32.@)
952 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
953 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
955 UNICODE_STRING pathW
, nameW
, extW
;
956 WCHAR bufferW
[MAX_PATH
];
959 if (path
) RtlCreateUnicodeStringFromAsciiz(&pathW
, path
);
960 else pathW
.Buffer
= NULL
;
961 if (name
) RtlCreateUnicodeStringFromAsciiz(&nameW
, name
);
962 else nameW
.Buffer
= NULL
;
963 if (ext
) RtlCreateUnicodeStringFromAsciiz(&extW
, ext
);
964 else extW
.Buffer
= NULL
;
966 retW
= SearchPathW(pathW
.Buffer
, nameW
.Buffer
, extW
.Buffer
, MAX_PATH
, bufferW
, NULL
);
970 else if (retW
> MAX_PATH
)
972 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
977 ret
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
980 WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, buflen
, NULL
, NULL
);
981 ret
--; /* length without 0 */
982 if (lastpart
) *lastpart
= strrchr(buffer
, '\\') + 1;
986 RtlFreeUnicodeString(&pathW
);
987 RtlFreeUnicodeString(&nameW
);
988 RtlFreeUnicodeString(&extW
);