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>
33 #ifdef HAVE_SYS_ERRNO_H
34 #include <sys/errno.h>
38 #include "wine/winbase16.h"
41 #include "wine/winuser16.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
51 WINE_DECLARE_DEBUG_CHANNEL(file);
53 static DOS_FULL_NAME DIR_Windows;
54 static DOS_FULL_NAME DIR_System;
57 /***********************************************************************
60 * Get a path name from the wine.ini file and make sure it is valid.
62 static int DIR_GetPath( const char *keyname, const char *defval,
63 DOS_FULL_NAME *full_name, char * longname, BOOL warn )
65 char path[MAX_PATHNAME_LEN];
66 BY_HANDLE_FILE_INFORMATION info;
67 const char *mess = "does not exist";
69 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
70 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
71 (!FILE_Stat( full_name->long_name, &info ) && (mess=strerror(errno)))||
72 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
73 (!(GetLongPathNameA(full_name->short_name, longname, MAX_PATHNAME_LEN))) )
76 MESSAGE("Invalid path '%s' for %s directory: %s\n", path, keyname, mess);
83 /***********************************************************************
88 char path[MAX_PATHNAME_LEN];
89 char longpath[MAX_PATHNAME_LEN];
90 DOS_FULL_NAME tmp_dir, profile_dir;
94 if (!getcwd( path, MAX_PATHNAME_LEN ))
96 perror( "Could not get current directory" );
100 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
102 MESSAGE("Warning: could not find wine config [Drive x] entry "
103 "for current working directory %s; "
104 "starting in windows directory.\n", cwd );
108 DRIVE_SetCurrentDrive( drive );
109 DRIVE_Chdir( drive, cwd );
112 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows, longpath, TRUE )) ||
113 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System, longpath, TRUE )) ||
114 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir, longpath, TRUE )))
116 PROFILE_UsageWineIni();
119 if (-1 == access( tmp_dir.long_name, W_OK ))
123 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
124 PROFILE_UsageWineIni();
127 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
128 tmp_dir.long_name, strerror(errno));
133 drive = DIR_Windows.drive;
134 DRIVE_SetCurrentDrive( drive );
135 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
138 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
139 path, sizeof(path) );
140 if (strchr(path, '/'))
142 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
143 PROFILE_UsageWineIni();
147 /* Set the environment variables */
149 SetEnvironmentVariableA( "PATH", path );
150 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
151 SetEnvironmentVariableA( "TMP", tmp_dir.short_name );
152 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
153 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
155 /* set COMSPEC only if it doesn't exist already */
156 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
157 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
159 TRACE("WindowsDir = %s (%s)\n",
160 DIR_Windows.short_name, DIR_Windows.long_name );
161 TRACE("SystemDir = %s (%s)\n",
162 DIR_System.short_name, DIR_System.long_name );
163 TRACE("TempDir = %s (%s)\n",
164 tmp_dir.short_name, tmp_dir.long_name );
165 TRACE("Path = %s\n", path );
166 TRACE("Cwd = %c:\\%s\n",
167 'A' + drive, DRIVE_GetDosCwd( drive ) );
169 if (DIR_GetPath( "profile", "", &profile_dir, longpath, FALSE ))
171 TRACE("USERPROFILE= %s\n", longpath );
172 SetEnvironmentVariableA( "USERPROFILE", longpath );
175 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
176 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
182 /***********************************************************************
183 * GetTempPathA (KERNEL32.@)
185 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
188 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
189 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
190 if (!(ret = GetCurrentDirectoryA( count, path )))
192 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
201 /***********************************************************************
202 * GetTempPathW (KERNEL32.@)
204 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
206 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
207 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
209 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
210 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
211 if (!(ret = GetCurrentDirectoryW( count, path )))
213 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
222 /***********************************************************************
223 * DIR_GetWindowsUnixDir
225 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
227 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
228 return strlen( DIR_Windows.long_name );
232 /***********************************************************************
233 * DIR_GetSystemUnixDir
235 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
237 if (path) lstrcpynA( path, DIR_System.long_name, count );
238 return strlen( DIR_System.long_name );
242 /***********************************************************************
243 * GetTempDrive (KERNEL.92)
244 * A closer look at krnl386.exe shows what the SDK doesn't mention:
248 * AH: ':' - yes, some kernel code even does stosw with
252 UINT WINAPI GetTempDrive( BYTE ignored )
256 UINT len = GetTempPathA( 0, NULL );
258 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
259 ret = DRIVE_GetCurrentDrive() + 'A';
262 /* FIXME: apparently Windows does something with the ignored byte */
263 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
264 ret = toupper(buffer[0]);
265 HeapFree( GetProcessHeap(), 0, buffer );
267 return MAKELONG( ret | (':' << 8), 1 );
271 /***********************************************************************
272 * GetWindowsDirectory (KERNEL.134)
274 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
276 return (UINT16)GetWindowsDirectoryA( path, count );
280 /***********************************************************************
281 * GetWindowsDirectoryA (KERNEL32.@)
283 * See comment for GetWindowsDirectoryW.
285 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
287 UINT len = strlen( DIR_Windows.short_name ) + 1;
288 if (path && count >= len)
290 strcpy( path, DIR_Windows.short_name );
297 /***********************************************************************
298 * GetWindowsDirectoryW (KERNEL32.@)
301 * If buffer is large enough to hold full path and terminating '\0' character
302 * function copies path to buffer and returns length of the path without '\0'.
303 * Otherwise function returns required size including '\0' character and
304 * does not touch the buffer.
306 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
308 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0 );
309 if (path && count >= len)
311 MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, path, count );
318 /***********************************************************************
319 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
321 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
323 return GetWindowsDirectoryA( path, count );
327 /***********************************************************************
328 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
330 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
332 return GetWindowsDirectoryW( path, count );
336 /***********************************************************************
337 * GetSystemDirectory (KERNEL.135)
339 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
341 return (UINT16)GetSystemDirectoryA( path, count );
345 /***********************************************************************
346 * GetSystemDirectoryA (KERNEL32.@)
348 * See comment for GetWindowsDirectoryW.
350 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
352 UINT len = strlen( DIR_System.short_name ) + 1;
353 if (path && count >= len)
355 strcpy( path, DIR_System.short_name );
362 /***********************************************************************
363 * GetSystemDirectoryW (KERNEL32.@)
365 * See comment for GetWindowsDirectoryW.
367 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
369 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0 );
370 if (path && count >= len)
372 MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, path, count );
379 /***********************************************************************
380 * CreateDirectory (KERNEL.144)
382 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
384 TRACE_(file)("(%s,%p)\n", path, dummy );
385 return (BOOL16)CreateDirectoryA( path, NULL );
389 /***********************************************************************
390 * CreateDirectoryA (KERNEL32.@)
394 * ERROR_DISK_FULL: on full disk
395 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
396 * ERROR_ACCESS_DENIED: on permission problems
397 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
399 BOOL WINAPI CreateDirectoryA( LPCSTR path,
400 LPSECURITY_ATTRIBUTES lpsecattribs )
402 DOS_FULL_NAME full_name;
404 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
405 if (DOSFS_GetDevice( path ))
407 TRACE_(file)("cannot use device '%s'!\n",path);
408 SetLastError( ERROR_ACCESS_DENIED );
411 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
412 if (mkdir( full_name.long_name, 0777 ) == -1) {
413 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
414 /* the FILE_SetDosError() generated error codes don't match the
415 * CreateDirectory ones for some errnos */
417 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
418 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
419 default: FILE_SetDosError();break;
427 /***********************************************************************
428 * CreateDirectoryW (KERNEL32.@)
430 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
431 LPSECURITY_ATTRIBUTES lpsecattribs )
433 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
434 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
435 HeapFree( GetProcessHeap(), 0, xpath );
440 /***********************************************************************
441 * CreateDirectoryExA (KERNEL32.@)
443 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
444 LPSECURITY_ATTRIBUTES lpsecattribs)
446 return CreateDirectoryA(path,lpsecattribs);
450 /***********************************************************************
451 * CreateDirectoryExW (KERNEL32.@)
453 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
454 LPSECURITY_ATTRIBUTES lpsecattribs)
456 return CreateDirectoryW(path,lpsecattribs);
460 /***********************************************************************
461 * RemoveDirectory (KERNEL.145)
463 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
465 return (BOOL16)RemoveDirectoryA( path );
469 /***********************************************************************
470 * RemoveDirectoryA (KERNEL32.@)
472 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
474 DOS_FULL_NAME full_name;
476 TRACE_(file)("'%s'\n", path );
478 if (DOSFS_GetDevice( path ))
480 TRACE_(file)("cannot remove device '%s'!\n", path);
481 SetLastError( ERROR_FILE_NOT_FOUND );
484 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
485 if (rmdir( full_name.long_name ) == -1)
494 /***********************************************************************
495 * RemoveDirectoryW (KERNEL32.@)
497 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
499 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
500 BOOL ret = RemoveDirectoryA( xpath );
501 HeapFree( GetProcessHeap(), 0, xpath );
506 /***********************************************************************
509 * Helper function for DIR_SearchPath.
511 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
512 DOS_FULL_NAME *full_name )
514 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
515 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
517 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
518 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
520 SetLastError( ERROR_PATH_NOT_FOUND );
523 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
524 sizeof(full_name->long_name) - (p_l - full_name->long_name),
525 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
527 strcpy( full_name->long_name, dir->long_name );
529 strcpy( full_name->short_name, dir->short_name );
534 static BOOL DIR_SearchSemicolonedPaths(LPCSTR name, DOS_FULL_NAME *full_name, LPSTR pathlist)
536 LPSTR next, buffer = NULL;
537 INT len = strlen(name), newlen, currlen = 0;
544 while (*cur == ';') cur++;
546 next = strchr( cur, ';' );
547 if (next) *next++ = '\0';
548 newlen = strlen(cur) + len + 2;
549 if (newlen > currlen)
551 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen)))
555 strcpy( buffer, cur );
556 strcat( buffer, "\\" );
557 strcat( buffer, name );
558 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
561 HeapFree( GetProcessHeap(), 0, buffer );
566 /***********************************************************************
567 * DIR_TryEnvironmentPath
569 * Helper function for DIR_SearchPath.
570 * Search in the specified path, or in $PATH if NULL.
572 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
578 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
579 if (!size) return FALSE;
580 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
581 if (envpath) strcpy( path, envpath );
582 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
584 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
587 HeapFree( GetProcessHeap(), 0, path );
592 /***********************************************************************
595 * Helper function for DIR_SearchPath.
597 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
599 /* FIXME: for now, GetModuleFileNameA can't return more */
600 /* than OFS_MAXPATHNAME. This may change with Win32. */
602 char buffer[OFS_MAXPATHNAME];
607 if (!GetCurrentTask()) return FALSE;
608 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
611 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
614 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
615 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
617 return DOSFS_GetFullName( buffer, TRUE, full_name );
621 /***********************************************************************
624 * Helper function for DIR_SearchPath.
626 static BOOL DIR_TryAppPath( LPCSTR name, DOS_FULL_NAME *full_name )
628 HKEY hkAppPaths = 0, hkApp = 0;
629 char lpAppName[MAX_PATHNAME_LEN], lpAppPaths[MAX_PATHNAME_LEN];
634 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", &hkAppPaths) != ERROR_SUCCESS)
637 if (GetModuleFileNameA(0, lpAppName, sizeof(lpAppName)) == 0)
639 WARN("huh, module not found ??\n");
642 lpFileName = strrchr(lpAppName, '\\');
645 else lpFileName++; /* skip '\\' */
646 if (RegOpenKeyA(hkAppPaths, lpFileName, &hkApp) != ERROR_SUCCESS)
648 count = sizeof(lpAppPaths);
649 if (RegQueryValueExA(hkApp, "Path", 0, &type, (LPBYTE)lpAppPaths, &count) != ERROR_SUCCESS)
651 TRACE("successfully opened App Paths for '%s'\n", lpFileName);
653 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
658 RegCloseKey(hkAppPaths);
662 /***********************************************************************
665 * Implementation of SearchPathA. 'win32' specifies whether the search
666 * order is Win16 (module path last) or Win32 (module path first).
668 * FIXME: should return long path names.
670 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
671 DOS_FULL_NAME *full_name, BOOL win32 )
677 /* First check the supplied parameters */
679 p = strrchr( name, '.' );
680 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
681 ext = NULL; /* Ignore the specified extension */
682 if (FILE_contains_path (name))
683 path = NULL; /* Ignore path if name already contains a path */
684 if (path && !*path) path = NULL; /* Ignore empty path */
686 /* Allocate a buffer for the file name and extension */
690 DWORD len = strlen(name) + strlen(ext);
691 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
693 SetLastError( ERROR_OUTOFMEMORY );
701 /* If the name contains an explicit path, everything's easy */
703 if (FILE_contains_path(name))
705 ret = DOSFS_GetFullName( name, TRUE, full_name );
709 /* Search in the specified path */
713 ret = DIR_TryEnvironmentPath( name, full_name, path );
717 /* Try the path of the current executable (for Win32 search order) */
719 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
721 /* Try the current directory */
723 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
725 /* Try the Windows system directory */
727 if (DIR_TryPath( &DIR_System, name, full_name ))
730 /* Try the Windows directory */
732 if (DIR_TryPath( &DIR_Windows, name, full_name ))
735 /* Try the path of the current executable (for Win16 search order) */
737 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
739 /* Try the "App Paths" entry if existing (undocumented ??) */
740 if (DIR_TryAppPath(name, full_name))
743 /* Try all directories in path */
745 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
748 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
753 /***********************************************************************
754 * SearchPathA [KERNEL32.@]
756 * Searches for a specified file in the search path.
759 * path [I] Path to search
760 * name [I] Filename to search for.
761 * ext [I] File extension to append to file name. The first
762 * character must be a period. This parameter is
763 * specified only if the filename given does not
764 * contain an extension.
765 * buflen [I] size of buffer, in characters
766 * buffer [O] buffer for found filename
767 * lastpart [O] address of pointer to last used character in
768 * buffer (the final '\')
771 * Success: length of string copied into buffer, not including
772 * terminating null character. If the filename found is
773 * longer than the length of the buffer, the length of the
774 * filename is returned.
778 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
781 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
782 LPSTR buffer, LPSTR *lastpart )
785 DOS_FULL_NAME full_name;
787 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
789 SetLastError(ERROR_FILE_NOT_FOUND);
792 lstrcpynA( buffer, full_name.short_name, buflen );
793 res = full_name.long_name +
794 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
795 while (*res == '/') res++;
798 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
799 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
800 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
802 TRACE("Returning %d\n", strlen(res) + 3 );
803 return strlen(res) + 3;
807 /***********************************************************************
808 * SearchPathW (KERNEL32.@)
810 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
811 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
815 DOS_FULL_NAME full_name;
817 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
818 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
819 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
820 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
821 HeapFree( GetProcessHeap(), 0, extA );
822 HeapFree( GetProcessHeap(), 0, nameA );
823 HeapFree( GetProcessHeap(), 0, pathA );
826 if (buflen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, buffer, buflen ))
827 buffer[buflen-1] = 0;
828 res = full_name.long_name +
829 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
830 while (*res == '/') res++;
835 if (!MultiByteToWideChar( CP_ACP, 0, res, -1, buffer+3, buflen-3 ))
836 buffer[buflen-1] = 0;
838 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
841 for (p = *lastpart = buffer; *p; p++)
842 if (*p == '\\') *lastpart = p + 1;
845 return strlen(res) + 3;
849 /***********************************************************************
850 * search_alternate_path
853 * FIXME: should return long path names.?
855 static BOOL search_alternate_path(LPCSTR dll_path, LPCSTR name, LPCSTR ext,
856 DOS_FULL_NAME *full_name)
862 /* First check the supplied parameters */
864 p = strrchr( name, '.' );
865 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
866 ext = NULL; /* Ignore the specified extension */
868 /* Allocate a buffer for the file name and extension */
872 DWORD len = strlen(name) + strlen(ext);
873 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
875 SetLastError( ERROR_OUTOFMEMORY );
883 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
885 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
887 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
889 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
892 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
894 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
899 /***********************************************************************
900 * DIR_SearchAlternatePath
902 * Searches for a specified file in the search path.
905 * dll_path [I] Path to search
906 * name [I] Filename to search for.
907 * ext [I] File extension to append to file name. The first
908 * character must be a period. This parameter is
909 * specified only if the filename given does not
910 * contain an extension.
911 * buflen [I] size of buffer, in characters
912 * buffer [O] buffer for found filename
913 * lastpart [O] address of pointer to last used character in
914 * buffer (the final '\') (May be NULL)
917 * Success: length of string copied into buffer, not including
918 * terminating null character. If the filename found is
919 * longer than the length of the buffer, the length of the
920 * filename is returned.
924 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
926 DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
927 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
930 DOS_FULL_NAME full_name;
932 if (!search_alternate_path( dll_path, name, ext, &full_name))
934 SetLastError(ERROR_FILE_NOT_FOUND);
937 lstrcpynA( buffer, full_name.short_name, buflen );
938 res = full_name.long_name +
939 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
940 while (*res == '/') res++;
943 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
944 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
945 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
947 TRACE("Returning %d\n", strlen(res) + 3 );
948 return strlen(res) + 3;