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, 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")))
74 MESSAGE("Invalid path '%s' for %s directory: %s\n", path, keyname, mess);
81 /***********************************************************************
86 char path[MAX_PATHNAME_LEN];
87 DOS_FULL_NAME tmp_dir, profile_dir;
91 if (!getcwd( path, MAX_PATHNAME_LEN ))
93 perror( "Could not get current directory" );
97 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
99 MESSAGE("Warning: could not find wine config [Drive x] entry "
100 "for current working directory %s; "
101 "starting in windows directory.\n", cwd );
105 DRIVE_SetCurrentDrive( drive );
106 DRIVE_Chdir( drive, cwd );
109 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows, TRUE )) ||
110 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System, TRUE )) ||
111 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir, TRUE )))
113 PROFILE_UsageWineIni();
116 if (-1 == access( tmp_dir.long_name, W_OK ))
120 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
121 PROFILE_UsageWineIni();
124 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
125 tmp_dir.long_name, strerror(errno));
130 drive = DIR_Windows.drive;
131 DRIVE_SetCurrentDrive( drive );
132 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
135 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
136 path, sizeof(path) );
137 if (strchr(path, '/'))
139 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
140 PROFILE_UsageWineIni();
144 /* Set the environment variables */
146 SetEnvironmentVariableA( "PATH", path );
147 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
148 SetEnvironmentVariableA( "TMP", tmp_dir.short_name );
149 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
150 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
152 /* set COMSPEC only if it doesn't exist already */
153 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
154 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
156 TRACE("WindowsDir = %s (%s)\n",
157 DIR_Windows.short_name, DIR_Windows.long_name );
158 TRACE("SystemDir = %s (%s)\n",
159 DIR_System.short_name, DIR_System.long_name );
160 TRACE("TempDir = %s (%s)\n",
161 tmp_dir.short_name, tmp_dir.long_name );
162 TRACE("Path = %s\n", path );
163 TRACE("Cwd = %c:\\%s\n",
164 'A' + drive, DRIVE_GetDosCwd( drive ) );
166 if (DIR_GetPath( "profile", "", &profile_dir, FALSE ))
168 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
169 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
172 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
173 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
179 /***********************************************************************
180 * GetTempPathA (KERNEL32.@)
182 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
185 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
186 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
187 if (!(ret = GetCurrentDirectoryA( count, path )))
189 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
198 /***********************************************************************
199 * GetTempPathW (KERNEL32.@)
201 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
203 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
204 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
206 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
207 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
208 if (!(ret = GetCurrentDirectoryW( count, path )))
210 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
219 /***********************************************************************
220 * DIR_GetWindowsUnixDir
222 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
224 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
225 return strlen( DIR_Windows.long_name );
229 /***********************************************************************
230 * DIR_GetSystemUnixDir
232 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
234 if (path) lstrcpynA( path, DIR_System.long_name, count );
235 return strlen( DIR_System.long_name );
239 /***********************************************************************
240 * GetTempDrive (KERNEL.92)
241 * A closer look at krnl386.exe shows what the SDK doesn't mention:
245 * AH: ':' - yes, some kernel code even does stosw with
249 UINT WINAPI GetTempDrive( BYTE ignored )
253 UINT len = GetTempPathA( 0, NULL );
255 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
256 ret = DRIVE_GetCurrentDrive() + 'A';
259 /* FIXME: apparently Windows does something with the ignored byte */
260 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
261 ret = toupper(buffer[0]);
262 HeapFree( GetProcessHeap(), 0, buffer );
264 return MAKELONG( ret | (':' << 8), 1 );
268 /***********************************************************************
269 * GetWindowsDirectory (KERNEL.134)
271 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
273 return (UINT16)GetWindowsDirectoryA( path, count );
277 /***********************************************************************
278 * GetWindowsDirectoryA (KERNEL32.@)
280 * See comment for GetWindowsDirectoryW.
282 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
284 UINT len = strlen( DIR_Windows.short_name ) + 1;
285 if (path && count >= len)
287 strcpy( path, DIR_Windows.short_name );
294 /***********************************************************************
295 * GetWindowsDirectoryW (KERNEL32.@)
298 * If buffer is large enough to hold full path and terminating '\0' character
299 * function copies path to buffer and returns length of the path without '\0'.
300 * Otherwise function returns required size including '\0' character and
301 * does not touch the buffer.
303 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
305 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0 );
306 if (path && count >= len)
308 MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, path, count );
315 /***********************************************************************
316 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
318 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
320 return GetWindowsDirectoryA( path, count );
324 /***********************************************************************
325 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
327 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
329 return GetWindowsDirectoryW( path, count );
333 /***********************************************************************
334 * GetSystemDirectory (KERNEL.135)
336 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
338 return (UINT16)GetSystemDirectoryA( path, count );
342 /***********************************************************************
343 * GetSystemDirectoryA (KERNEL32.@)
345 * See comment for GetWindowsDirectoryW.
347 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
349 UINT len = strlen( DIR_System.short_name ) + 1;
350 if (path && count >= len)
352 strcpy( path, DIR_System.short_name );
359 /***********************************************************************
360 * GetSystemDirectoryW (KERNEL32.@)
362 * See comment for GetWindowsDirectoryW.
364 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
366 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0 );
367 if (path && count >= len)
369 MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, path, count );
376 /***********************************************************************
377 * CreateDirectory (KERNEL.144)
379 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
381 TRACE_(file)("(%s,%p)\n", path, dummy );
382 return (BOOL16)CreateDirectoryA( path, NULL );
386 /***********************************************************************
387 * CreateDirectoryA (KERNEL32.@)
391 * ERROR_DISK_FULL: on full disk
392 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
393 * ERROR_ACCESS_DENIED: on permission problems
394 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
396 BOOL WINAPI CreateDirectoryA( LPCSTR path,
397 LPSECURITY_ATTRIBUTES lpsecattribs )
399 DOS_FULL_NAME full_name;
401 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
402 if (DOSFS_GetDevice( path ))
404 TRACE_(file)("cannot use device '%s'!\n",path);
405 SetLastError( ERROR_ACCESS_DENIED );
408 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
409 if (mkdir( full_name.long_name, 0777 ) == -1) {
410 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
411 /* the FILE_SetDosError() generated error codes don't match the
412 * CreateDirectory ones for some errnos */
414 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
415 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
416 default: FILE_SetDosError();break;
424 /***********************************************************************
425 * CreateDirectoryW (KERNEL32.@)
427 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
428 LPSECURITY_ATTRIBUTES lpsecattribs )
430 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
431 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
432 HeapFree( GetProcessHeap(), 0, xpath );
437 /***********************************************************************
438 * CreateDirectoryExA (KERNEL32.@)
440 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
441 LPSECURITY_ATTRIBUTES lpsecattribs)
443 return CreateDirectoryA(path,lpsecattribs);
447 /***********************************************************************
448 * CreateDirectoryExW (KERNEL32.@)
450 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
451 LPSECURITY_ATTRIBUTES lpsecattribs)
453 return CreateDirectoryW(path,lpsecattribs);
457 /***********************************************************************
458 * RemoveDirectory (KERNEL.145)
460 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
462 return (BOOL16)RemoveDirectoryA( path );
466 /***********************************************************************
467 * RemoveDirectoryA (KERNEL32.@)
469 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
471 DOS_FULL_NAME full_name;
473 TRACE_(file)("'%s'\n", path );
475 if (DOSFS_GetDevice( path ))
477 TRACE_(file)("cannot remove device '%s'!\n", path);
478 SetLastError( ERROR_FILE_NOT_FOUND );
481 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
482 if (rmdir( full_name.long_name ) == -1)
491 /***********************************************************************
492 * RemoveDirectoryW (KERNEL32.@)
494 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
496 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
497 BOOL ret = RemoveDirectoryA( xpath );
498 HeapFree( GetProcessHeap(), 0, xpath );
503 /***********************************************************************
506 * Helper function for DIR_SearchPath.
508 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
509 DOS_FULL_NAME *full_name )
511 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
512 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
514 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
515 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
517 SetLastError( ERROR_PATH_NOT_FOUND );
520 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
521 sizeof(full_name->long_name) - (p_l - full_name->long_name),
522 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
524 strcpy( full_name->long_name, dir->long_name );
526 strcpy( full_name->short_name, dir->short_name );
531 static BOOL DIR_SearchSemicolonedPaths(LPCSTR name, DOS_FULL_NAME *full_name, LPSTR pathlist)
533 LPSTR next, buffer = NULL;
534 INT len = strlen(name), newlen, currlen = 0;
541 while (*cur == ';') cur++;
543 next = strchr( cur, ';' );
544 if (next) *next++ = '\0';
545 newlen = strlen(cur) + len + 2;
546 if (newlen > currlen)
548 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen)))
552 strcpy( buffer, cur );
553 strcat( buffer, "\\" );
554 strcat( buffer, name );
555 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
558 HeapFree( GetProcessHeap(), 0, buffer );
563 /***********************************************************************
564 * DIR_TryEnvironmentPath
566 * Helper function for DIR_SearchPath.
567 * Search in the specified path, or in $PATH if NULL.
569 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
575 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
576 if (!size) return FALSE;
577 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
578 if (envpath) strcpy( path, envpath );
579 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
581 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
584 HeapFree( GetProcessHeap(), 0, path );
589 /***********************************************************************
592 * Helper function for DIR_SearchPath.
594 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
596 /* FIXME: for now, GetModuleFileNameA can't return more */
597 /* than OFS_MAXPATHNAME. This may change with Win32. */
599 char buffer[OFS_MAXPATHNAME];
604 if (!GetCurrentTask()) return FALSE;
605 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
608 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
611 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
612 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
614 return DOSFS_GetFullName( buffer, TRUE, full_name );
618 /***********************************************************************
621 * Helper function for DIR_SearchPath.
623 static BOOL DIR_TryAppPath( LPCSTR name, DOS_FULL_NAME *full_name )
625 HKEY hkAppPaths, hkApp;
626 char lpAppName[MAX_PATHNAME_LEN], lpAppPaths[MAX_PATHNAME_LEN];
631 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", &hkAppPaths) != ERROR_SUCCESS)
634 if (GetModuleFileNameA(0, lpAppName, sizeof(lpAppName)) == 0)
636 WARN("huh, module not found ??\n");
639 lpFileName = strrchr(lpAppName, '\\');
642 else lpFileName++; /* skip '\\' */
643 if (RegOpenKeyA(hkAppPaths, lpFileName, &hkApp) != ERROR_SUCCESS)
645 count = sizeof(lpAppPaths);
646 if (RegQueryValueExA(hkApp, "Path", 0, &type, (LPBYTE)lpAppPaths, &count) != ERROR_SUCCESS)
648 TRACE("successfully opened App Paths for '%s'\n", lpFileName);
650 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
655 RegCloseKey(hkAppPaths);
659 /***********************************************************************
662 * Implementation of SearchPathA. 'win32' specifies whether the search
663 * order is Win16 (module path last) or Win32 (module path first).
665 * FIXME: should return long path names.
667 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
668 DOS_FULL_NAME *full_name, BOOL win32 )
674 /* First check the supplied parameters */
676 p = strrchr( name, '.' );
677 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
678 ext = NULL; /* Ignore the specified extension */
679 if (FILE_contains_path (name))
680 path = NULL; /* Ignore path if name already contains a path */
681 if (path && !*path) path = NULL; /* Ignore empty path */
683 /* Allocate a buffer for the file name and extension */
687 DWORD len = strlen(name) + strlen(ext);
688 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
690 SetLastError( ERROR_OUTOFMEMORY );
698 /* If the name contains an explicit path, everything's easy */
700 if (FILE_contains_path(name))
702 ret = DOSFS_GetFullName( name, TRUE, full_name );
706 /* Search in the specified path */
710 ret = DIR_TryEnvironmentPath( name, full_name, path );
714 /* Try the path of the current executable (for Win32 search order) */
716 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
718 /* Try the current directory */
720 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
722 /* Try the Windows system directory */
724 if (DIR_TryPath( &DIR_System, name, full_name ))
727 /* Try the Windows directory */
729 if (DIR_TryPath( &DIR_Windows, name, full_name ))
732 /* Try the path of the current executable (for Win16 search order) */
734 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
736 /* Try the "App Paths" entry if existing (undocumented ??) */
737 if (DIR_TryAppPath(name, full_name))
740 /* Try all directories in path */
742 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
745 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
750 /***********************************************************************
751 * SearchPathA [KERNEL32.@]
753 * Searches for a specified file in the search path.
756 * path [I] Path to search
757 * name [I] Filename to search for.
758 * ext [I] File extension to append to file name. The first
759 * character must be a period. This parameter is
760 * specified only if the filename given does not
761 * contain an extension.
762 * buflen [I] size of buffer, in characters
763 * buffer [O] buffer for found filename
764 * lastpart [O] address of pointer to last used character in
765 * buffer (the final '\')
768 * Success: length of string copied into buffer, not including
769 * terminating null character. If the filename found is
770 * longer than the length of the buffer, the length of the
771 * filename is returned.
775 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
778 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
779 LPSTR buffer, LPSTR *lastpart )
782 DOS_FULL_NAME full_name;
784 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
786 SetLastError(ERROR_FILE_NOT_FOUND);
789 lstrcpynA( buffer, full_name.short_name, buflen );
790 res = full_name.long_name +
791 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
792 while (*res == '/') res++;
795 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
796 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
797 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
799 TRACE("Returning %d\n", strlen(res) + 3 );
800 return strlen(res) + 3;
804 /***********************************************************************
805 * SearchPathW (KERNEL32.@)
807 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
808 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
812 DOS_FULL_NAME full_name;
814 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
815 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
816 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
817 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
818 HeapFree( GetProcessHeap(), 0, extA );
819 HeapFree( GetProcessHeap(), 0, nameA );
820 HeapFree( GetProcessHeap(), 0, pathA );
823 if (buflen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, buffer, buflen ))
824 buffer[buflen-1] = 0;
825 res = full_name.long_name +
826 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
827 while (*res == '/') res++;
832 if (!MultiByteToWideChar( CP_ACP, 0, res, -1, buffer+3, buflen-3 ))
833 buffer[buflen-1] = 0;
835 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
838 for (p = *lastpart = buffer; *p; p++)
839 if (*p == '\\') *lastpart = p + 1;
842 return strlen(res) + 3;
846 /***********************************************************************
847 * search_alternate_path
850 * FIXME: should return long path names.?
852 static BOOL search_alternate_path(LPCSTR dll_path, LPCSTR name, LPCSTR ext,
853 DOS_FULL_NAME *full_name)
859 /* First check the supplied parameters */
861 p = strrchr( name, '.' );
862 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
863 ext = NULL; /* Ignore the specified extension */
865 /* Allocate a buffer for the file name and extension */
869 DWORD len = strlen(name) + strlen(ext);
870 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
872 SetLastError( ERROR_OUTOFMEMORY );
880 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
882 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
884 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
886 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
889 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
891 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
896 /***********************************************************************
897 * DIR_SearchAlternatePath
899 * Searches for a specified file in the search path.
902 * dll_path [I] Path to search
903 * name [I] Filename to search for.
904 * ext [I] File extension to append to file name. The first
905 * character must be a period. This parameter is
906 * specified only if the filename given does not
907 * contain an extension.
908 * buflen [I] size of buffer, in characters
909 * buffer [O] buffer for found filename
910 * lastpart [O] address of pointer to last used character in
911 * buffer (the final '\') (May be NULL)
914 * Success: length of string copied into buffer, not including
915 * terminating null character. If the filename found is
916 * longer than the length of the buffer, the length of the
917 * filename is returned.
921 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
923 DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
924 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
927 DOS_FULL_NAME full_name;
929 if (!search_alternate_path( dll_path, name, ext, &full_name))
931 SetLastError(ERROR_FILE_NOT_FOUND);
934 lstrcpynA( buffer, full_name.short_name, buflen );
935 res = full_name.long_name +
936 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
937 while (*res == '/') res++;
940 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
941 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
942 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
944 TRACE("Returning %d\n", strlen(res) + 3 );
945 return strlen(res) + 3;