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>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
36 #include "wine/winbase16.h"
39 #include "wine/winuser16.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(dosfs
);
50 WINE_DECLARE_DEBUG_CHANNEL(file
);
52 static DOS_FULL_NAME DIR_Windows
;
53 static DOS_FULL_NAME DIR_System
;
56 /***********************************************************************
59 * Get a path name from the wine.ini file and make sure it is valid.
61 static int DIR_GetPath( const char *keyname
, const char *defval
,
62 DOS_FULL_NAME
*full_name
, char * longname
, BOOL warn
)
64 char path
[MAX_PATHNAME_LEN
];
65 BY_HANDLE_FILE_INFORMATION info
;
66 const char *mess
= "does not exist";
68 PROFILE_GetWineIniString( "wine", keyname
, defval
, path
, sizeof(path
) );
69 if (!DOSFS_GetFullName( path
, TRUE
, full_name
) ||
70 (!FILE_Stat( full_name
->long_name
, &info
) && (mess
=strerror(errno
)))||
71 (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) && (mess
="not a directory")) ||
72 (!(GetLongPathNameA(full_name
->short_name
, longname
, MAX_PATHNAME_LEN
))) )
75 MESSAGE("Invalid path '%s' for %s directory: %s\n", path
, keyname
, mess
);
82 /***********************************************************************
87 char path
[MAX_PATHNAME_LEN
];
88 char longpath
[MAX_PATHNAME_LEN
];
89 DOS_FULL_NAME tmp_dir
, profile_dir
;
93 if (!getcwd( path
, MAX_PATHNAME_LEN
))
95 perror( "Could not get current directory" );
99 if ((drive
= DRIVE_FindDriveRoot( &cwd
)) == -1)
101 MESSAGE("Warning: could not find wine config [Drive x] entry "
102 "for current working directory %s; "
103 "starting in windows directory.\n", cwd
);
107 DRIVE_SetCurrentDrive( drive
);
108 DRIVE_Chdir( drive
, cwd
);
111 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows
, longpath
, TRUE
)) ||
112 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System
, longpath
, TRUE
)) ||
113 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir
, longpath
, TRUE
)))
115 PROFILE_UsageWineIni();
118 if (-1 == access( tmp_dir
.long_name
, W_OK
))
122 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir
.long_name
);
123 PROFILE_UsageWineIni();
126 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
127 tmp_dir
.long_name
, strerror(errno
));
132 drive
= DIR_Windows
.drive
;
133 DRIVE_SetCurrentDrive( drive
);
134 DRIVE_Chdir( drive
, DIR_Windows
.short_name
+ 2 );
137 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
138 path
, sizeof(path
) );
139 if (strchr(path
, '/'))
141 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
142 PROFILE_UsageWineIni();
146 /* Set the environment variables */
148 SetEnvironmentVariableA( "PATH", path
);
149 SetEnvironmentVariableA( "TEMP", tmp_dir
.short_name
);
150 SetEnvironmentVariableA( "TMP", tmp_dir
.short_name
);
151 SetEnvironmentVariableA( "windir", DIR_Windows
.short_name
);
152 SetEnvironmentVariableA( "winsysdir", DIR_System
.short_name
);
154 /* set COMSPEC only if it doesn't exist already */
155 if (!GetEnvironmentVariableA( "COMSPEC", NULL
, 0 ))
156 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
158 TRACE("WindowsDir = %s (%s)\n",
159 DIR_Windows
.short_name
, DIR_Windows
.long_name
);
160 TRACE("SystemDir = %s (%s)\n",
161 DIR_System
.short_name
, DIR_System
.long_name
);
162 TRACE("TempDir = %s (%s)\n",
163 tmp_dir
.short_name
, tmp_dir
.long_name
);
164 TRACE("Path = %s\n", path
);
165 TRACE("Cwd = %c:\\%s\n",
166 'A' + drive
, DRIVE_GetDosCwd( drive
) );
168 if (DIR_GetPath( "profile", "", &profile_dir
, longpath
, FALSE
))
170 TRACE("USERPROFILE= %s\n", longpath
);
171 SetEnvironmentVariableA( "USERPROFILE", longpath
);
174 TRACE("SYSTEMROOT = %s\n", DIR_Windows
.short_name
);
175 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows
.short_name
);
181 /***********************************************************************
182 * GetTempPathA (KERNEL32.@)
184 UINT WINAPI
GetTempPathA( UINT count
, LPSTR path
)
187 if (!(ret
= GetEnvironmentVariableA( "TMP", path
, count
)))
188 if (!(ret
= GetEnvironmentVariableA( "TEMP", path
, count
)))
189 if (!(ret
= GetCurrentDirectoryA( count
, path
)))
191 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
200 /***********************************************************************
201 * GetTempPathW (KERNEL32.@)
203 UINT WINAPI
GetTempPathW( UINT count
, LPWSTR path
)
205 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
206 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
208 if (!(ret
= GetEnvironmentVariableW( tmp
, path
, count
)))
209 if (!(ret
= GetEnvironmentVariableW( temp
, path
, count
)))
210 if (!(ret
= GetCurrentDirectoryW( count
, path
)))
212 if (count
&& (ret
< count
- 1) && (path
[ret
-1] != '\\'))
221 /***********************************************************************
222 * DIR_GetWindowsUnixDir
224 UINT
DIR_GetWindowsUnixDir( LPSTR path
, UINT count
)
226 if (path
) lstrcpynA( path
, DIR_Windows
.long_name
, count
);
227 return strlen( DIR_Windows
.long_name
);
231 /***********************************************************************
232 * DIR_GetSystemUnixDir
234 UINT
DIR_GetSystemUnixDir( LPSTR path
, UINT count
)
236 if (path
) lstrcpynA( path
, DIR_System
.long_name
, count
);
237 return strlen( DIR_System
.long_name
);
241 /***********************************************************************
242 * GetTempDrive (KERNEL.92)
243 * A closer look at krnl386.exe shows what the SDK doesn't mention:
247 * AH: ':' - yes, some kernel code even does stosw with
251 UINT WINAPI
GetTempDrive( BYTE ignored
)
255 UINT len
= GetTempPathA( 0, NULL
);
257 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )) )
258 ret
= DRIVE_GetCurrentDrive() + 'A';
261 /* FIXME: apparently Windows does something with the ignored byte */
262 if (!GetTempPathA( len
, buffer
)) buffer
[0] = 'C';
263 ret
= toupper(buffer
[0]);
264 HeapFree( GetProcessHeap(), 0, buffer
);
266 return MAKELONG( ret
| (':' << 8), 1 );
270 /***********************************************************************
271 * GetWindowsDirectory (KERNEL.134)
273 UINT16 WINAPI
GetWindowsDirectory16( LPSTR path
, UINT16 count
)
275 return (UINT16
)GetWindowsDirectoryA( path
, count
);
279 /***********************************************************************
280 * GetWindowsDirectoryA (KERNEL32.@)
282 * See comment for GetWindowsDirectoryW.
284 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
286 UINT len
= strlen( DIR_Windows
.short_name
) + 1;
287 if (path
&& count
>= len
)
289 strcpy( path
, DIR_Windows
.short_name
);
296 /***********************************************************************
297 * GetWindowsDirectoryW (KERNEL32.@)
300 * If buffer is large enough to hold full path and terminating '\0' character
301 * function copies path to buffer and returns length of the path without '\0'.
302 * Otherwise function returns required size including '\0' character and
303 * does not touch the buffer.
305 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
307 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, NULL
, 0 );
308 if (path
&& count
>= len
)
310 MultiByteToWideChar( CP_ACP
, 0, DIR_Windows
.short_name
, -1, path
, count
);
317 /***********************************************************************
318 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
320 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
322 return GetWindowsDirectoryA( path
, count
);
326 /***********************************************************************
327 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
329 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
331 return GetWindowsDirectoryW( path
, count
);
335 /***********************************************************************
336 * GetSystemDirectory (KERNEL.135)
338 UINT16 WINAPI
GetSystemDirectory16( LPSTR path
, UINT16 count
)
340 return (UINT16
)GetSystemDirectoryA( path
, count
);
344 /***********************************************************************
345 * GetSystemDirectoryA (KERNEL32.@)
347 * See comment for GetWindowsDirectoryW.
349 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
351 UINT len
= strlen( DIR_System
.short_name
) + 1;
352 if (path
&& count
>= len
)
354 strcpy( path
, DIR_System
.short_name
);
361 /***********************************************************************
362 * GetSystemDirectoryW (KERNEL32.@)
364 * See comment for GetWindowsDirectoryW.
366 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
368 UINT len
= MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, NULL
, 0 );
369 if (path
&& count
>= len
)
371 MultiByteToWideChar( CP_ACP
, 0, DIR_System
.short_name
, -1, path
, count
);
378 /***********************************************************************
379 * CreateDirectory (KERNEL.144)
381 BOOL16 WINAPI
CreateDirectory16( LPCSTR path
, LPVOID dummy
)
383 TRACE_(file
)("(%s,%p)\n", path
, dummy
);
384 return (BOOL16
)CreateDirectoryA( path
, NULL
);
388 /***********************************************************************
389 * CreateDirectoryA (KERNEL32.@)
393 * ERROR_DISK_FULL: on full disk
394 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
395 * ERROR_ACCESS_DENIED: on permission problems
396 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
398 BOOL WINAPI
CreateDirectoryA( LPCSTR path
,
399 LPSECURITY_ATTRIBUTES lpsecattribs
)
401 DOS_FULL_NAME full_name
;
403 TRACE_(file
)("(%s,%p)\n", path
, lpsecattribs
);
404 if (DOSFS_GetDevice( path
))
406 TRACE_(file
)("cannot use device '%s'!\n",path
);
407 SetLastError( ERROR_ACCESS_DENIED
);
410 if (!DOSFS_GetFullName( path
, FALSE
, &full_name
)) return 0;
411 if (mkdir( full_name
.long_name
, 0777 ) == -1) {
412 WARN_(file
)("Error '%s' trying to create directory '%s'\n", strerror(errno
), full_name
.long_name
);
413 /* the FILE_SetDosError() generated error codes don't match the
414 * CreateDirectory ones for some errnos */
416 case EEXIST
: SetLastError(ERROR_ALREADY_EXISTS
); break;
417 case ENOSPC
: SetLastError(ERROR_DISK_FULL
); break;
418 default: FILE_SetDosError();break;
426 /***********************************************************************
427 * CreateDirectoryW (KERNEL32.@)
429 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
,
430 LPSECURITY_ATTRIBUTES lpsecattribs
)
432 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
433 BOOL ret
= CreateDirectoryA( xpath
, lpsecattribs
);
434 HeapFree( GetProcessHeap(), 0, xpath
);
439 /***********************************************************************
440 * CreateDirectoryExA (KERNEL32.@)
442 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
,
443 LPSECURITY_ATTRIBUTES lpsecattribs
)
445 return CreateDirectoryA(path
,lpsecattribs
);
449 /***********************************************************************
450 * CreateDirectoryExW (KERNEL32.@)
452 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
,
453 LPSECURITY_ATTRIBUTES lpsecattribs
)
455 return CreateDirectoryW(path
,lpsecattribs
);
459 /***********************************************************************
460 * RemoveDirectory (KERNEL.145)
462 BOOL16 WINAPI
RemoveDirectory16( LPCSTR path
)
464 return (BOOL16
)RemoveDirectoryA( path
);
468 /***********************************************************************
469 * RemoveDirectoryA (KERNEL32.@)
471 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
473 DOS_FULL_NAME full_name
;
475 TRACE_(file
)("'%s'\n", path
);
477 if (DOSFS_GetDevice( path
))
479 TRACE_(file
)("cannot remove device '%s'!\n", path
);
480 SetLastError( ERROR_FILE_NOT_FOUND
);
483 if (!DOSFS_GetFullName( path
, TRUE
, &full_name
)) return FALSE
;
484 if (rmdir( full_name
.long_name
) == -1)
493 /***********************************************************************
494 * RemoveDirectoryW (KERNEL32.@)
496 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
498 LPSTR xpath
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
499 BOOL ret
= RemoveDirectoryA( xpath
);
500 HeapFree( GetProcessHeap(), 0, xpath
);
505 /***********************************************************************
508 * Helper function for DIR_SearchPath.
510 static BOOL
DIR_TryPath( const DOS_FULL_NAME
*dir
, LPCSTR name
,
511 DOS_FULL_NAME
*full_name
)
513 LPSTR p_l
= full_name
->long_name
+ strlen(dir
->long_name
) + 1;
514 LPSTR p_s
= full_name
->short_name
+ strlen(dir
->short_name
) + 1;
516 if ((p_s
>= full_name
->short_name
+ sizeof(full_name
->short_name
) - 14) ||
517 (p_l
>= full_name
->long_name
+ sizeof(full_name
->long_name
) - 1))
519 SetLastError( ERROR_PATH_NOT_FOUND
);
522 if (!DOSFS_FindUnixName( dir
->long_name
, name
, p_l
,
523 sizeof(full_name
->long_name
) - (p_l
- full_name
->long_name
),
524 p_s
, !(DRIVE_GetFlags(dir
->drive
) & DRIVE_CASE_SENSITIVE
) ))
526 strcpy( full_name
->long_name
, dir
->long_name
);
528 strcpy( full_name
->short_name
, dir
->short_name
);
533 static BOOL
DIR_SearchSemicolonedPaths(LPCSTR name
, DOS_FULL_NAME
*full_name
, LPSTR pathlist
)
535 LPSTR next
, buffer
= NULL
;
536 INT len
= strlen(name
), newlen
, currlen
= 0;
543 while (*cur
== ';') cur
++;
545 next
= strchr( cur
, ';' );
546 if (next
) *next
++ = '\0';
547 newlen
= strlen(cur
) + len
+ 2;
548 if (newlen
> currlen
)
550 if (!(buffer
= HeapReAlloc( GetProcessHeap(), 0, buffer
, newlen
)))
554 strcpy( buffer
, cur
);
555 strcat( buffer
, "\\" );
556 strcat( buffer
, name
);
557 ret
= DOSFS_GetFullName( buffer
, TRUE
, full_name
);
560 HeapFree( GetProcessHeap(), 0, buffer
);
565 /***********************************************************************
566 * DIR_TryEnvironmentPath
568 * Helper function for DIR_SearchPath.
569 * Search in the specified path, or in $PATH if NULL.
571 static BOOL
DIR_TryEnvironmentPath( LPCSTR name
, DOS_FULL_NAME
*full_name
, LPCSTR envpath
)
577 size
= envpath
? strlen(envpath
)+1 : GetEnvironmentVariableA( "PATH", NULL
, 0 );
578 if (!size
) return FALSE
;
579 if (!(path
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
580 if (envpath
) strcpy( path
, envpath
);
581 else if (!GetEnvironmentVariableA( "PATH", path
, size
)) goto done
;
583 ret
= DIR_SearchSemicolonedPaths(name
, full_name
, path
);
586 HeapFree( GetProcessHeap(), 0, path
);
591 /***********************************************************************
594 * Helper function for DIR_SearchPath.
596 static BOOL
DIR_TryModulePath( LPCSTR name
, DOS_FULL_NAME
*full_name
, BOOL win32
)
598 /* FIXME: for now, GetModuleFileNameA can't return more */
599 /* than OFS_MAXPATHNAME. This may change with Win32. */
601 char buffer
[OFS_MAXPATHNAME
];
606 if (!GetCurrentTask()) return FALSE
;
607 if (!GetModuleFileName16( GetCurrentTask(), buffer
, sizeof(buffer
) ))
610 if (!GetModuleFileNameA( 0, buffer
, sizeof(buffer
) ))
613 if (!(p
= strrchr( buffer
, '\\' ))) return FALSE
;
614 if (sizeof(buffer
) - (++p
- buffer
) <= strlen(name
)) return FALSE
;
616 return DOSFS_GetFullName( buffer
, TRUE
, full_name
);
620 /***********************************************************************
623 * Helper function for DIR_SearchPath.
625 static BOOL
DIR_TryAppPath( LPCSTR name
, DOS_FULL_NAME
*full_name
)
627 HKEY hkAppPaths
, hkApp
;
628 char lpAppName
[MAX_PATHNAME_LEN
], lpAppPaths
[MAX_PATHNAME_LEN
];
633 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", &hkAppPaths
) != ERROR_SUCCESS
)
636 if (GetModuleFileNameA(0, lpAppName
, sizeof(lpAppName
)) == 0)
638 WARN("huh, module not found ??\n");
641 lpFileName
= strrchr(lpAppName
, '\\');
644 else lpFileName
++; /* skip '\\' */
645 if (RegOpenKeyA(hkAppPaths
, lpFileName
, &hkApp
) != ERROR_SUCCESS
)
647 count
= sizeof(lpAppPaths
);
648 if (RegQueryValueExA(hkApp
, "Path", 0, &type
, (LPBYTE
)lpAppPaths
, &count
) != ERROR_SUCCESS
)
650 TRACE("successfully opened App Paths for '%s'\n", lpFileName
);
652 res
= DIR_SearchSemicolonedPaths(name
, full_name
, lpAppPaths
);
657 RegCloseKey(hkAppPaths
);
661 /***********************************************************************
664 * Implementation of SearchPathA. 'win32' specifies whether the search
665 * order is Win16 (module path last) or Win32 (module path first).
667 * FIXME: should return long path names.
669 DWORD
DIR_SearchPath( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
670 DOS_FULL_NAME
*full_name
, BOOL win32
)
676 /* First check the supplied parameters */
678 p
= strrchr( name
, '.' );
679 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
680 ext
= NULL
; /* Ignore the specified extension */
681 if (FILE_contains_path (name
))
682 path
= NULL
; /* Ignore path if name already contains a path */
683 if (path
&& !*path
) path
= NULL
; /* Ignore empty path */
685 /* Allocate a buffer for the file name and extension */
689 DWORD len
= strlen(name
) + strlen(ext
);
690 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
692 SetLastError( ERROR_OUTOFMEMORY
);
700 /* If the name contains an explicit path, everything's easy */
702 if (FILE_contains_path(name
))
704 ret
= DOSFS_GetFullName( name
, TRUE
, full_name
);
708 /* Search in the specified path */
712 ret
= DIR_TryEnvironmentPath( name
, full_name
, path
);
716 /* Try the path of the current executable (for Win32 search order) */
718 if (win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
720 /* Try the current directory */
722 if (DOSFS_GetFullName( name
, TRUE
, full_name
)) goto done
;
724 /* Try the Windows system directory */
726 if (DIR_TryPath( &DIR_System
, name
, full_name
))
729 /* Try the Windows directory */
731 if (DIR_TryPath( &DIR_Windows
, name
, full_name
))
734 /* Try the path of the current executable (for Win16 search order) */
736 if (!win32
&& DIR_TryModulePath( name
, full_name
, win32
)) goto done
;
738 /* Try the "App Paths" entry if existing (undocumented ??) */
739 if (DIR_TryAppPath(name
, full_name
))
742 /* Try all directories in path */
744 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
747 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
752 /***********************************************************************
753 * SearchPathA [KERNEL32.@]
755 * Searches for a specified file in the search path.
758 * path [I] Path to search
759 * name [I] Filename to search for.
760 * ext [I] File extension to append to file name. The first
761 * character must be a period. This parameter is
762 * specified only if the filename given does not
763 * contain an extension.
764 * buflen [I] size of buffer, in characters
765 * buffer [O] buffer for found filename
766 * lastpart [O] address of pointer to last used character in
767 * buffer (the final '\')
770 * Success: length of string copied into buffer, not including
771 * terminating null character. If the filename found is
772 * longer than the length of the buffer, the length of the
773 * filename is returned.
777 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
780 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
, DWORD buflen
,
781 LPSTR buffer
, LPSTR
*lastpart
)
784 DOS_FULL_NAME full_name
;
786 if (!DIR_SearchPath( path
, name
, ext
, &full_name
, TRUE
))
788 SetLastError(ERROR_FILE_NOT_FOUND
);
791 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
792 res
= full_name
.long_name
+
793 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
794 while (*res
== '/') res
++;
797 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
798 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
799 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
801 TRACE("Returning %d\n", strlen(res
) + 3 );
802 return strlen(res
) + 3;
806 /***********************************************************************
807 * SearchPathW (KERNEL32.@)
809 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
,
810 DWORD buflen
, LPWSTR buffer
, LPWSTR
*lastpart
)
814 DOS_FULL_NAME full_name
;
816 LPSTR pathA
= HEAP_strdupWtoA( GetProcessHeap(), 0, path
);
817 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
818 LPSTR extA
= HEAP_strdupWtoA( GetProcessHeap(), 0, ext
);
819 DWORD ret
= DIR_SearchPath( pathA
, nameA
, extA
, &full_name
, TRUE
);
820 HeapFree( GetProcessHeap(), 0, extA
);
821 HeapFree( GetProcessHeap(), 0, nameA
);
822 HeapFree( GetProcessHeap(), 0, pathA
);
825 if (buflen
> 0 && !MultiByteToWideChar( CP_ACP
, 0, full_name
.short_name
, -1, buffer
, buflen
))
826 buffer
[buflen
-1] = 0;
827 res
= full_name
.long_name
+
828 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
829 while (*res
== '/') res
++;
834 if (!MultiByteToWideChar( CP_ACP
, 0, res
, -1, buffer
+3, buflen
-3 ))
835 buffer
[buflen
-1] = 0;
837 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
840 for (p
= *lastpart
= buffer
; *p
; p
++)
841 if (*p
== '\\') *lastpart
= p
+ 1;
844 return strlen(res
) + 3;
848 /***********************************************************************
849 * search_alternate_path
852 * FIXME: should return long path names.?
854 static BOOL
search_alternate_path(LPCSTR dll_path
, LPCSTR name
, LPCSTR ext
,
855 DOS_FULL_NAME
*full_name
)
861 /* First check the supplied parameters */
863 p
= strrchr( name
, '.' );
864 if (p
&& !strchr( p
, '/' ) && !strchr( p
, '\\' ))
865 ext
= NULL
; /* Ignore the specified extension */
867 /* Allocate a buffer for the file name and extension */
871 DWORD len
= strlen(name
) + strlen(ext
);
872 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 )))
874 SetLastError( ERROR_OUTOFMEMORY
);
882 if (DIR_TryEnvironmentPath (name
, full_name
, dll_path
))
884 else if (DOSFS_GetFullName (name
, TRUE
, full_name
)) /* current dir */
886 else if (DIR_TryPath (&DIR_System
, name
, full_name
)) /* System dir */
888 else if (DIR_TryPath (&DIR_Windows
, name
, full_name
)) /* Windows dir */
891 ret
= DIR_TryEnvironmentPath( name
, full_name
, NULL
);
893 if (tmp
) HeapFree( GetProcessHeap(), 0, tmp
);
898 /***********************************************************************
899 * DIR_SearchAlternatePath
901 * Searches for a specified file in the search path.
904 * dll_path [I] Path to search
905 * name [I] Filename to search for.
906 * ext [I] File extension to append to file name. The first
907 * character must be a period. This parameter is
908 * specified only if the filename given does not
909 * contain an extension.
910 * buflen [I] size of buffer, in characters
911 * buffer [O] buffer for found filename
912 * lastpart [O] address of pointer to last used character in
913 * buffer (the final '\') (May be NULL)
916 * Success: length of string copied into buffer, not including
917 * terminating null character. If the filename found is
918 * longer than the length of the buffer, the length of the
919 * filename is returned.
923 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
925 DWORD
DIR_SearchAlternatePath( LPCSTR dll_path
, LPCSTR name
, LPCSTR ext
,
926 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
929 DOS_FULL_NAME full_name
;
931 if (!search_alternate_path( dll_path
, name
, ext
, &full_name
))
933 SetLastError(ERROR_FILE_NOT_FOUND
);
936 lstrcpynA( buffer
, full_name
.short_name
, buflen
);
937 res
= full_name
.long_name
+
938 strlen(DRIVE_GetRoot( full_name
.short_name
[0] - 'A' ));
939 while (*res
== '/') res
++;
942 if (buflen
> 3) lstrcpynA( buffer
+ 3, res
, buflen
- 3 );
943 for (p
= buffer
; *p
; p
++) if (*p
== '/') *p
= '\\';
944 if (lastpart
) *lastpart
= strrchr( buffer
, '\\' ) + 1;
946 TRACE("Returning %d\n", strlen(res
) + 3 );
947 return strlen(res
) + 3;