Release 20020509.
[wine/gsoc_dplay.git] / files / directory.c
blob0ee2cd10f2661df139f5ba346e09237b582874d9
1 /*
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
21 #include "config.h"
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #ifdef HAVE_SYS_ERRNO_H
32 #include <sys/errno.h>
33 #endif
35 #include "winbase.h"
36 #include "wine/winbase16.h"
37 #include "windef.h"
38 #include "wingdi.h"
39 #include "wine/winuser16.h"
40 #include "winerror.h"
41 #include "winreg.h"
42 #include "drive.h"
43 #include "file.h"
44 #include "heap.h"
45 #include "msdos.h"
46 #include "options.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 /***********************************************************************
57 * DIR_GetPath
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))) )
74 if (warn)
75 MESSAGE("Invalid path '%s' for %s directory: %s\n", path, keyname, mess);
76 return 0;
78 return 1;
82 /***********************************************************************
83 * DIR_Init
85 int DIR_Init(void)
87 char path[MAX_PATHNAME_LEN];
88 char longpath[MAX_PATHNAME_LEN];
89 DOS_FULL_NAME tmp_dir, profile_dir;
90 int drive;
91 const char *cwd;
93 if (!getcwd( path, MAX_PATHNAME_LEN ))
95 perror( "Could not get current directory" );
96 return 0;
98 cwd = path;
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 );
105 else
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();
116 return 0;
118 if (-1 == access( tmp_dir.long_name, W_OK ))
120 if (errno==EACCES)
122 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
123 PROFILE_UsageWineIni();
125 else
126 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
127 tmp_dir.long_name, strerror(errno));
130 if (drive == -1)
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();
143 ExitProcess(1);
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 );
177 return 1;
181 /***********************************************************************
182 * GetTempPathA (KERNEL32.@)
184 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
186 UINT ret;
187 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
188 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
189 if (!(ret = GetCurrentDirectoryA( count, path )))
190 return 0;
191 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
193 path[ret++] = '\\';
194 path[ret] = '\0';
196 return ret;
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 };
207 UINT ret;
208 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
209 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
210 if (!(ret = GetCurrentDirectoryW( count, path )))
211 return 0;
212 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
214 path[ret++] = '\\';
215 path[ret] = '\0';
217 return ret;
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:
245 * returns:
246 * AL: driveletter
247 * AH: ':' - yes, some kernel code even does stosw with
248 * the returned AX.
249 * DX: 1 for success
251 UINT WINAPI GetTempDrive( BYTE ignored )
253 char *buffer;
254 BYTE ret;
255 UINT len = GetTempPathA( 0, NULL );
257 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
258 ret = DRIVE_GetCurrentDrive() + 'A';
259 else
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 );
290 len--;
292 return len;
296 /***********************************************************************
297 * GetWindowsDirectoryW (KERNEL32.@)
299 * Return value:
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 );
311 len--;
313 return len;
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 );
355 len--;
357 return len;
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 );
372 len--;
374 return len;
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.@)
390 * RETURNS:
391 * TRUE : success
392 * FALSE : failure
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 );
408 return FALSE;
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 */
415 switch (errno) {
416 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
417 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
418 default: FILE_SetDosError();break;
420 return FALSE;
422 return TRUE;
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 );
435 return ret;
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 );
481 return FALSE;
483 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
484 if (rmdir( full_name.long_name ) == -1)
486 FILE_SetDosError();
487 return FALSE;
489 return TRUE;
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 );
501 return ret;
505 /***********************************************************************
506 * DIR_TryPath
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 );
520 return FALSE;
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) ))
525 return FALSE;
526 strcpy( full_name->long_name, dir->long_name );
527 p_l[-1] = '/';
528 strcpy( full_name->short_name, dir->short_name );
529 p_s[-1] = '\\';
530 return TRUE;
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;
537 BOOL ret = FALSE;
539 next = pathlist;
540 while (!ret && next)
542 LPSTR cur = next;
543 while (*cur == ';') cur++;
544 if (!*cur) break;
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)))
551 goto done;
552 currlen = newlen;
554 strcpy( buffer, cur );
555 strcat( buffer, "\\" );
556 strcat( buffer, name );
557 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
559 done:
560 HeapFree( GetProcessHeap(), 0, buffer );
561 return ret;
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 )
573 LPSTR path;
574 BOOL ret = FALSE;
575 DWORD size;
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);
585 done:
586 HeapFree( GetProcessHeap(), 0, path );
587 return ret;
591 /***********************************************************************
592 * DIR_TryModulePath
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];
602 LPSTR p;
604 if (!win32)
606 if (!GetCurrentTask()) return FALSE;
607 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
608 buffer[0]='\0';
609 } else {
610 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
611 buffer[0]='\0';
613 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
614 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
615 strcpy( p, name );
616 return DOSFS_GetFullName( buffer, TRUE, full_name );
620 /***********************************************************************
621 * DIR_TryAppPath
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];
629 LPSTR lpFileName;
630 BOOL res = FALSE;
631 DWORD type, count;
633 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", &hkAppPaths) != ERROR_SUCCESS)
634 return FALSE;
636 if (GetModuleFileNameA(0, lpAppName, sizeof(lpAppName)) == 0)
638 WARN("huh, module not found ??\n");
639 goto end;
641 lpFileName = strrchr(lpAppName, '\\');
642 if (!lpFileName)
643 goto end;
644 else lpFileName++; /* skip '\\' */
645 if (RegOpenKeyA(hkAppPaths, lpFileName, &hkApp) != ERROR_SUCCESS)
646 goto end;
647 count = sizeof(lpAppPaths);
648 if (RegQueryValueExA(hkApp, "Path", 0, &type, (LPBYTE)lpAppPaths, &count) != ERROR_SUCCESS)
649 goto end;
650 TRACE("successfully opened App Paths for '%s'\n", lpFileName);
652 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
653 end:
654 if (hkApp)
655 RegCloseKey(hkApp);
656 if (hkAppPaths)
657 RegCloseKey(hkAppPaths);
658 return res;
661 /***********************************************************************
662 * DIR_SearchPath
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 )
672 LPCSTR p;
673 LPSTR tmp = NULL;
674 BOOL ret = TRUE;
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 */
687 if (ext)
689 DWORD len = strlen(name) + strlen(ext);
690 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
692 SetLastError( ERROR_OUTOFMEMORY );
693 return 0;
695 strcpy( tmp, name );
696 strcat( tmp, ext );
697 name = tmp;
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 );
705 goto done;
708 /* Search in the specified path */
710 if (path)
712 ret = DIR_TryEnvironmentPath( name, full_name, path );
713 goto done;
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 ))
727 goto done;
729 /* Try the Windows directory */
731 if (DIR_TryPath( &DIR_Windows, name, full_name ))
732 goto done;
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))
740 goto done;
742 /* Try all directories in path */
744 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
746 done:
747 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
748 return ret;
752 /***********************************************************************
753 * SearchPathA [KERNEL32.@]
755 * Searches for a specified file in the search path.
757 * PARAMS
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 '\')
769 * RETURNS
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.
774 * Failure: Zero
776 * NOTES
777 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
778 * (tested on NT 4.0)
780 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
781 LPSTR buffer, LPSTR *lastpart )
783 LPSTR p, res;
784 DOS_FULL_NAME full_name;
786 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
788 SetLastError(ERROR_FILE_NOT_FOUND);
789 return 0;
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++;
795 if (buflen)
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 )
812 LPWSTR p;
813 LPSTR res;
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 );
823 if (!ret) return 0;
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++;
830 if (buflen)
832 if (buflen > 3)
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 = '\\';
838 if (lastpart)
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)
857 LPCSTR p;
858 LPSTR tmp = NULL;
859 BOOL ret = TRUE;
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 */
869 if (ext)
871 DWORD len = strlen(name) + strlen(ext);
872 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
874 SetLastError( ERROR_OUTOFMEMORY );
875 return 0;
877 strcpy( tmp, name );
878 strcat( tmp, ext );
879 name = tmp;
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 */
890 else
891 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
893 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
894 return ret;
898 /***********************************************************************
899 * DIR_SearchAlternatePath
901 * Searches for a specified file in the search path.
903 * PARAMS
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)
915 * RETURNS
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.
920 * Failure: Zero
922 * NOTES
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 )
928 LPSTR p, res;
929 DOS_FULL_NAME full_name;
931 if (!search_alternate_path( dll_path, name, ext, &full_name))
933 SetLastError(ERROR_FILE_NOT_FOUND);
934 return 0;
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++;
940 if (buflen)
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;