Compile the shlwapi dll with -DSTRICT.
[wine/gsoc_dplay.git] / files / directory.c
blob023d58fbb4c813ea5319cc2e1494de1fe8222c03
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 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <errno.h>
33 #ifdef HAVE_SYS_ERRNO_H
34 #include <sys/errno.h>
35 #endif
37 #include "winbase.h"
38 #include "wine/winbase16.h"
39 #include "windef.h"
40 #include "wingdi.h"
41 #include "wine/winuser16.h"
42 #include "winerror.h"
43 #include "winternl.h"
44 #include "wine/unicode.h"
45 #include "drive.h"
46 #include "file.h"
47 #include "heap.h"
48 #include "msdos.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
52 WINE_DECLARE_DEBUG_CHANNEL(file);
54 static DOS_FULL_NAME DIR_Windows;
55 static DOS_FULL_NAME DIR_System;
57 static const WCHAR wineW[] = {'w','i','n','e',0};
59 /***********************************************************************
60 * FILE_contains_pathW
62 inline static int FILE_contains_pathW (LPCWSTR name)
64 return ((*name && (name[1] == ':')) ||
65 strchrW (name, '/') || strchrW (name, '\\'));
68 /***********************************************************************
69 * DIR_GetPath
71 * Get a path name from the wine.ini file and make sure it is valid.
73 static int DIR_GetPath( LPCWSTR keyname, LPCWSTR defval, DOS_FULL_NAME *full_name,
74 LPWSTR longname, INT longname_len, BOOL warn )
76 WCHAR path[MAX_PATHNAME_LEN];
77 BY_HANDLE_FILE_INFORMATION info;
78 const char *mess = "does not exist";
80 PROFILE_GetWineIniString( wineW, keyname, defval, path, MAX_PATHNAME_LEN );
82 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
83 (!FILE_Stat( full_name->long_name, &info ) && (mess=strerror(errno)))||
84 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
85 (!(GetLongPathNameW(full_name->short_name, longname, longname_len))) )
87 if (warn)
88 MESSAGE("Invalid path %s for %s directory: %s\n",
89 debugstr_w(path), debugstr_w(keyname), mess);
90 return 0;
92 return 1;
96 /***********************************************************************
97 * DIR_Init
99 int DIR_Init(void)
101 char path[MAX_PATHNAME_LEN];
102 WCHAR longpath[MAX_PATHNAME_LEN];
103 DOS_FULL_NAME tmp_dir, profile_dir;
104 int drive;
105 const char *cwd;
106 static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
107 static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
108 static const WCHAR tempW[] = {'t','e','m','p',0};
109 static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
110 static const WCHAR windows_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
111 static const WCHAR system_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
112 static const WCHAR pathW[] = {'p','a','t','h',0};
113 static const WCHAR path_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',';',
114 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
115 static const WCHAR path_capsW[] = {'P','A','T','H',0};
116 static const WCHAR temp_capsW[] = {'T','E','M','P',0};
117 static const WCHAR tmp_capsW[] = {'T','M','P',0};
118 static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
119 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
120 static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
121 static const WCHAR systemrootW[] = {'S','Y','S','T','E','M','R','O','O','T',0};
122 static const WCHAR empty_strW[] = { 0 };
124 if (!getcwd( path, MAX_PATHNAME_LEN ))
126 perror( "Could not get current directory" );
127 return 0;
129 cwd = path;
130 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
132 MESSAGE("Warning: could not find wine config [Drive x] entry "
133 "for current working directory %s; "
134 "starting in windows directory.\n", cwd );
136 else
138 WCHAR szdrive[3]={drive+'A',':',0};
139 MultiByteToWideChar(DRIVE_GetCodepage(drive), 0, cwd, -1, longpath, MAX_PATHNAME_LEN);
140 DRIVE_SetCurrentDrive( drive );
141 DRIVE_Chdir( drive, longpath );
142 if(GetDriveTypeW(szdrive)==DRIVE_CDROM)
143 chdir("/"); /* change to root directory so as not to lock cdroms */
146 if (!(DIR_GetPath( windowsW, windows_dirW, &DIR_Windows, longpath, MAX_PATHNAME_LEN, TRUE )) ||
147 !(DIR_GetPath( systemW, system_dirW, &DIR_System, longpath, MAX_PATHNAME_LEN, TRUE )) ||
148 !(DIR_GetPath( tempW, windows_dirW, &tmp_dir, longpath, MAX_PATHNAME_LEN, TRUE )))
150 PROFILE_UsageWineIni();
151 return 0;
153 if (-1 == access( tmp_dir.long_name, W_OK ))
155 if (errno==EACCES)
157 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
158 PROFILE_UsageWineIni();
160 else
161 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
162 tmp_dir.long_name, strerror(errno));
165 if (drive == -1)
167 drive = DIR_Windows.drive;
168 DRIVE_SetCurrentDrive( drive );
169 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
172 /* Set the environment variables */
174 /* set PATH only if not set already */
175 if (!GetEnvironmentVariableW( path_capsW, longpath, MAX_PATHNAME_LEN ))
177 PROFILE_GetWineIniString(wineW, pathW, path_dirW, longpath, MAX_PATHNAME_LEN);
178 if (strchrW(longpath, '/'))
180 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
181 PROFILE_UsageWineIni();
182 ExitProcess(1);
184 SetEnvironmentVariableW( path_capsW, longpath );
187 SetEnvironmentVariableW( temp_capsW, tmp_dir.short_name );
188 SetEnvironmentVariableW( tmp_capsW, tmp_dir.short_name );
189 SetEnvironmentVariableW( windirW, DIR_Windows.short_name );
190 SetEnvironmentVariableW( winsysdirW, DIR_System.short_name );
192 /* set COMSPEC only if it doesn't exist already */
193 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
194 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
196 TRACE("WindowsDir = %s (%s)\n",
197 debugstr_w(DIR_Windows.short_name), DIR_Windows.long_name );
198 TRACE("SystemDir = %s (%s)\n",
199 debugstr_w(DIR_System.short_name), DIR_System.long_name );
200 TRACE("TempDir = %s (%s)\n",
201 debugstr_w(tmp_dir.short_name), tmp_dir.long_name );
202 TRACE("Path = %s\n", debugstr_w(longpath) );
203 TRACE("Cwd = %c:\\%s\n",
204 'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
206 if (DIR_GetPath( profileW, empty_strW, &profile_dir, longpath, MAX_PATHNAME_LEN, FALSE ))
208 TRACE("USERPROFILE= %s\n", debugstr_w(longpath) );
209 SetEnvironmentVariableW( userprofileW, longpath );
212 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows.short_name) );
213 SetEnvironmentVariableW( systemrootW, DIR_Windows.short_name );
215 return 1;
219 /***********************************************************************
220 * GetTempPathA (KERNEL32.@)
222 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
224 WCHAR pathW[MAX_PATH];
225 UINT ret;
227 ret = GetTempPathW(MAX_PATH, pathW);
229 if (!ret)
230 return 0;
232 if (ret > MAX_PATH)
234 SetLastError(ERROR_FILENAME_EXCED_RANGE);
235 return 0;
238 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
239 if (ret <= count)
241 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
242 ret--; /* length without 0 */
244 return ret;
248 /***********************************************************************
249 * GetTempPathW (KERNEL32.@)
251 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
253 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
254 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
255 WCHAR tmp_path[MAX_PATH];
256 UINT ret;
258 TRACE("%u,%p\n", count, path);
260 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
261 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
262 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
263 return 0;
265 if (ret > MAX_PATH)
267 SetLastError(ERROR_FILENAME_EXCED_RANGE);
268 return 0;
271 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
272 if (!ret) return 0;
274 if (ret > MAX_PATH - 2)
276 SetLastError(ERROR_FILENAME_EXCED_RANGE);
277 return 0;
280 if (tmp_path[ret-1] != '\\')
282 tmp_path[ret++] = '\\';
283 tmp_path[ret] = '\0';
286 ret++; /* add space for terminating 0 */
288 if (count)
290 lstrcpynW(path, tmp_path, count);
291 if (count >= ret)
292 ret--; /* return length without 0 */
293 else if (count < 4)
294 path[0] = 0; /* avoid returning ambiguous "X:" */
297 TRACE("returning %u, %s\n", ret, debugstr_w(path));
298 return ret;
302 /***********************************************************************
303 * DIR_GetWindowsUnixDir
305 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
307 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
308 return strlen( DIR_Windows.long_name );
312 /***********************************************************************
313 * DIR_GetSystemUnixDir
315 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
317 if (path) lstrcpynA( path, DIR_System.long_name, count );
318 return strlen( DIR_System.long_name );
322 /***********************************************************************
323 * GetTempDrive (KERNEL.92)
324 * A closer look at krnl386.exe shows what the SDK doesn't mention:
326 * returns:
327 * AL: driveletter
328 * AH: ':' - yes, some kernel code even does stosw with
329 * the returned AX.
330 * DX: 1 for success
332 UINT WINAPI GetTempDrive( BYTE ignored )
334 char *buffer;
335 BYTE ret;
336 UINT len = GetTempPathA( 0, NULL );
338 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
339 ret = DRIVE_GetCurrentDrive() + 'A';
340 else
342 /* FIXME: apparently Windows does something with the ignored byte */
343 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
344 ret = toupper(buffer[0]);
345 HeapFree( GetProcessHeap(), 0, buffer );
347 return MAKELONG( ret | (':' << 8), 1 );
351 /***********************************************************************
352 * GetWindowsDirectory (KERNEL.134)
354 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
356 return (UINT16)GetWindowsDirectoryA( path, count );
360 /***********************************************************************
361 * GetWindowsDirectoryW (KERNEL32.@)
363 * See comment for GetWindowsDirectoryA.
365 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
367 UINT len = strlenW( DIR_Windows.short_name ) + 1;
368 if (path && count >= len)
370 strcpyW( path, DIR_Windows.short_name );
371 len--;
373 return len;
377 /***********************************************************************
378 * GetWindowsDirectoryA (KERNEL32.@)
380 * Return value:
381 * If buffer is large enough to hold full path and terminating '\0' character
382 * function copies path to buffer and returns length of the path without '\0'.
383 * Otherwise function returns required size including '\0' character and
384 * does not touch the buffer.
386 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
388 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0, NULL, NULL );
389 if (path && count >= len)
391 WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, path, count, NULL, NULL );
392 len--;
394 return len;
398 /***********************************************************************
399 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
401 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
403 return GetWindowsDirectoryA( path, count );
407 /***********************************************************************
408 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
410 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
412 return GetWindowsDirectoryW( path, count );
416 /***********************************************************************
417 * GetSystemDirectory (KERNEL.135)
419 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
421 return (UINT16)GetSystemDirectoryA( path, count );
425 /***********************************************************************
426 * GetSystemDirectoryW (KERNEL32.@)
428 * See comment for GetWindowsDirectoryA.
430 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
432 UINT len = strlenW( DIR_System.short_name ) + 1;
433 if (path && count >= len)
435 strcpyW( path, DIR_System.short_name );
436 len--;
438 return len;
442 /***********************************************************************
443 * GetSystemDirectoryA (KERNEL32.@)
445 * See comment for GetWindowsDirectoryA.
447 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
449 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0, NULL, NULL );
450 if (path && count >= len)
452 WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, path, count, NULL, NULL );
453 len--;
455 return len;
459 /***********************************************************************
460 * CreateDirectory (KERNEL.144)
462 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
464 TRACE_(file)("(%s,%p)\n", path, dummy );
465 return (BOOL16)CreateDirectoryA( path, NULL );
469 /***********************************************************************
470 * CreateDirectoryW (KERNEL32.@)
471 * RETURNS:
472 * TRUE : success
473 * FALSE : failure
474 * ERROR_DISK_FULL: on full disk
475 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
476 * ERROR_ACCESS_DENIED: on permission problems
477 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
479 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
480 LPSECURITY_ATTRIBUTES lpsecattribs )
482 DOS_FULL_NAME full_name;
484 if (!path || !*path)
486 SetLastError(ERROR_PATH_NOT_FOUND);
487 return FALSE;
490 TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
492 if (DOSFS_GetDevice( path ))
494 TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
495 SetLastError( ERROR_ACCESS_DENIED );
496 return FALSE;
498 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
499 if (mkdir( full_name.long_name, 0777 ) == -1) {
500 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
501 /* the FILE_SetDosError() generated error codes don't match the
502 * CreateDirectory ones for some errnos */
503 switch (errno) {
504 case EEXIST:
506 if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
507 SetLastError(ERROR_ACCESS_DENIED);
508 else
509 SetLastError(ERROR_ALREADY_EXISTS);
510 break;
512 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
513 default: FILE_SetDosError();break;
515 return FALSE;
517 return TRUE;
521 /***********************************************************************
522 * CreateDirectoryA (KERNEL32.@)
524 BOOL WINAPI CreateDirectoryA( LPCSTR path,
525 LPSECURITY_ATTRIBUTES lpsecattribs )
527 UNICODE_STRING pathW;
528 BOOL ret = FALSE;
530 if (!path || !*path)
532 SetLastError(ERROR_PATH_NOT_FOUND);
533 return FALSE;
536 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
538 ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
539 RtlFreeUnicodeString(&pathW);
541 else
542 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
543 return ret;
547 /***********************************************************************
548 * CreateDirectoryExA (KERNEL32.@)
550 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
551 LPSECURITY_ATTRIBUTES lpsecattribs)
553 return CreateDirectoryA(path,lpsecattribs);
557 /***********************************************************************
558 * CreateDirectoryExW (KERNEL32.@)
560 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
561 LPSECURITY_ATTRIBUTES lpsecattribs)
563 return CreateDirectoryW(path,lpsecattribs);
567 /***********************************************************************
568 * RemoveDirectory (KERNEL.145)
570 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
572 return (BOOL16)RemoveDirectoryA( path );
576 /***********************************************************************
577 * RemoveDirectoryW (KERNEL32.@)
579 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
581 DOS_FULL_NAME full_name;
583 if (!path)
585 SetLastError(ERROR_INVALID_PARAMETER);
586 return FALSE;
589 TRACE_(file)("%s\n", debugstr_w(path));
591 if (DOSFS_GetDevice( path ))
593 TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
594 SetLastError( ERROR_FILE_NOT_FOUND );
595 return FALSE;
597 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
598 if (rmdir( full_name.long_name ) == -1)
600 FILE_SetDosError();
601 return FALSE;
603 return TRUE;
607 /***********************************************************************
608 * RemoveDirectoryA (KERNEL32.@)
610 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
612 UNICODE_STRING pathW;
613 BOOL ret = FALSE;
615 if (!path)
617 SetLastError(ERROR_INVALID_PARAMETER);
618 return FALSE;
621 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
623 ret = RemoveDirectoryW(pathW.Buffer);
624 RtlFreeUnicodeString(&pathW);
626 else
627 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
628 return ret;
632 /***********************************************************************
633 * DIR_TryPath
635 * Helper function for DIR_SearchPath.
637 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCWSTR name,
638 DOS_FULL_NAME *full_name )
640 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
641 LPWSTR p_s = full_name->short_name + strlenW(dir->short_name) + 1;
643 if ((p_s >= full_name->short_name + sizeof(full_name->short_name)/sizeof(full_name->short_name[0]) - 14) ||
644 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
646 SetLastError( ERROR_PATH_NOT_FOUND );
647 return FALSE;
649 if (!DOSFS_FindUnixName( dir, name, p_l,
650 sizeof(full_name->long_name) - (p_l - full_name->long_name),
651 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
652 return FALSE;
654 full_name->drive = dir->drive;
655 strcpy( full_name->long_name, dir->long_name );
656 p_l[-1] = '/';
657 strcpyW( full_name->short_name, dir->short_name );
658 p_s[-1] = '\\';
659 return TRUE;
662 static BOOL DIR_SearchSemicolonedPaths(LPCWSTR name, DOS_FULL_NAME *full_name, LPWSTR pathlist)
664 LPWSTR next, buffer = NULL;
665 INT len = strlenW(name), newlen, currlen = 0;
666 BOOL ret = FALSE;
668 next = pathlist;
669 while (!ret && next)
671 static const WCHAR bkslashW[] = {'\\',0};
672 LPWSTR cur = next;
673 while (*cur == ';') cur++;
674 if (!*cur) break;
675 next = strchrW( cur, ';' );
676 if (next) *next++ = '\0';
677 newlen = strlenW(cur) + len + 2;
679 if (newlen > currlen)
681 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen * sizeof(WCHAR))))
682 goto done;
683 currlen = newlen;
686 strcpyW( buffer, cur );
687 strcatW( buffer, bkslashW );
688 strcatW( buffer, name );
689 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
691 done:
692 HeapFree( GetProcessHeap(), 0, buffer );
693 return ret;
697 /***********************************************************************
698 * DIR_TryEnvironmentPath
700 * Helper function for DIR_SearchPath.
701 * Search in the specified path, or in $PATH if NULL.
703 static BOOL DIR_TryEnvironmentPath( LPCWSTR name, DOS_FULL_NAME *full_name, LPCWSTR envpath )
705 LPWSTR path;
706 BOOL ret = FALSE;
707 DWORD size;
708 static const WCHAR pathW[] = {'P','A','T','H',0};
710 size = envpath ? strlenW(envpath)+1 : GetEnvironmentVariableW( pathW, NULL, 0 );
711 if (!size) return FALSE;
712 if (!(path = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
713 if (envpath) strcpyW( path, envpath );
714 else if (!GetEnvironmentVariableW( pathW, path, size )) goto done;
716 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
718 done:
719 HeapFree( GetProcessHeap(), 0, path );
720 return ret;
724 /***********************************************************************
725 * DIR_TryModulePath
727 * Helper function for DIR_SearchPath.
729 static BOOL DIR_TryModulePath( LPCWSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
731 WCHAR bufferW[MAX_PATH];
732 LPWSTR p;
734 if (!win32)
736 char buffer[OFS_MAXPATHNAME];
737 if (!GetCurrentTask()) return FALSE;
738 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
739 return FALSE;
740 MultiByteToWideChar(CP_ACP, 0, buffer, -1, bufferW, MAX_PATH);
741 } else {
742 if (!GetModuleFileNameW( 0, bufferW, MAX_PATH ) )
743 return FALSE;
745 if (!(p = strrchrW( bufferW, '\\' ))) return FALSE;
746 if (MAX_PATH - (++p - bufferW) <= strlenW(name)) return FALSE;
747 strcpyW( p, name );
748 return DOSFS_GetFullName( bufferW, TRUE, full_name );
752 /***********************************************************************
753 * DIR_TryAppPath
755 * Helper function for DIR_SearchPath.
757 static BOOL DIR_TryAppPath( LPCWSTR name, DOS_FULL_NAME *full_name )
759 HKEY hkAppPaths = 0, hkApp = 0;
760 WCHAR buffer[MAX_PATHNAME_LEN], *lpAppPaths;
761 LPWSTR lpFileName;
762 BOOL res = FALSE;
763 DWORD count;
764 OBJECT_ATTRIBUTES attr;
765 UNICODE_STRING nameW;
766 KEY_VALUE_PARTIAL_INFORMATION *info;
767 static const WCHAR PathW[] = {'P','a','t','h',0};
768 static const WCHAR AppPathsW[] = {'M','a','c','h','i','n','e','\\',
769 'S','o','f','t','w','a','r','e','\\',
770 'M','i','c','r','o','s','o','f','t','\\',
771 'W','i','n','d','o','w','s','\\',
772 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
773 'A','p','p',' ','P','a','t','h','s',0};
775 attr.Length = sizeof(attr);
776 attr.RootDirectory = 0;
777 attr.ObjectName = &nameW;
778 attr.Attributes = 0;
779 attr.SecurityDescriptor = NULL;
780 attr.SecurityQualityOfService = NULL;
781 RtlInitUnicodeString( &nameW, AppPathsW );
782 if (NtOpenKey( &hkAppPaths, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return FALSE;
784 if (!GetModuleFileNameW(0, buffer, MAX_PATHNAME_LEN))
786 WARN("huh, module not found ??\n");
787 goto end;
789 lpFileName = strrchrW(buffer, '\\');
790 if (!lpFileName) lpFileName = buffer;
791 else lpFileName++; /* skip '\\' */
793 attr.RootDirectory = hkAppPaths;
794 RtlInitUnicodeString( &nameW, lpFileName );
795 if (NtOpenKey( &hkApp, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) goto end;
797 RtlInitUnicodeString( &nameW, PathW );
798 if (NtQueryValueKey( hkApp, &nameW, KeyValuePartialInformation,
799 buffer, sizeof(buffer)-sizeof(WCHAR), &count )) goto end;
800 info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
801 lpAppPaths = (WCHAR *)info->Data;
802 lpAppPaths[info->DataLength/sizeof(WCHAR)] = 0;
803 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
804 end:
805 if (hkApp) NtClose(hkApp);
806 if (hkAppPaths) NtClose(hkAppPaths);
807 return res;
810 /***********************************************************************
811 * DIR_SearchPath
813 * Implementation of SearchPathA. 'win32' specifies whether the search
814 * order is Win16 (module path last) or Win32 (module path first).
816 * FIXME: should return long path names.
818 DWORD DIR_SearchPath( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
819 DOS_FULL_NAME *full_name, BOOL win32 )
821 LPCWSTR p;
822 LPWSTR tmp = NULL;
823 BOOL ret = TRUE;
825 /* First check the supplied parameters */
827 p = strrchrW( name, '.' );
828 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
829 ext = NULL; /* Ignore the specified extension */
830 if (FILE_contains_pathW (name))
831 path = NULL; /* Ignore path if name already contains a path */
832 if (path && !*path) path = NULL; /* Ignore empty path */
834 /* Allocate a buffer for the file name and extension */
836 if (ext)
838 DWORD len = strlenW(name) + strlenW(ext);
839 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
841 SetLastError( ERROR_OUTOFMEMORY );
842 return 0;
844 strcpyW( tmp, name );
845 strcatW( tmp, ext );
846 name = tmp;
849 /* If the name contains an explicit path, everything's easy */
851 if (FILE_contains_pathW(name))
853 ret = DOSFS_GetFullName( name, TRUE, full_name );
854 goto done;
857 /* Search in the specified path */
859 if (path)
861 ret = DIR_TryEnvironmentPath( name, full_name, path );
862 goto done;
865 /* Try the path of the current executable (for Win32 search order) */
867 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
869 /* Try the current directory */
871 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
873 /* Try the Windows system directory */
875 if (DIR_TryPath( &DIR_System, name, full_name ))
876 goto done;
878 /* Try the Windows directory */
880 if (DIR_TryPath( &DIR_Windows, name, full_name ))
881 goto done;
883 /* Try the path of the current executable (for Win16 search order) */
885 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
887 /* Try the "App Paths" entry if existing (undocumented ??) */
888 if (DIR_TryAppPath(name, full_name))
889 goto done;
891 /* Try all directories in path */
893 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
895 done:
896 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
897 return ret;
901 /***********************************************************************
902 * SearchPathW [KERNEL32.@]
904 * Searches for a specified file in the search path.
906 * PARAMS
907 * path [I] Path to search
908 * name [I] Filename to search for.
909 * ext [I] File extension to append to file name. The first
910 * character must be a period. This parameter is
911 * specified only if the filename given does not
912 * contain an extension.
913 * buflen [I] size of buffer, in characters
914 * buffer [O] buffer for found filename
915 * lastpart [O] address of pointer to last used character in
916 * buffer (the final '\')
918 * RETURNS
919 * Success: length of string copied into buffer, not including
920 * terminating null character. If the filename found is
921 * longer than the length of the buffer, the length of the
922 * filename is returned.
923 * Failure: Zero
925 * NOTES
926 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
927 * (tested on NT 4.0)
929 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
930 LPWSTR buffer, LPWSTR *lastpart )
932 LPSTR res;
933 DOS_FULL_NAME full_name;
935 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
937 SetLastError(ERROR_FILE_NOT_FOUND);
938 return 0;
941 TRACE("found %s %s\n", full_name.long_name, debugstr_w(full_name.short_name));
942 TRACE("drive %c: root %s\n", 'A' + full_name.drive, DRIVE_GetRoot(full_name.drive));
944 lstrcpynW( buffer, full_name.short_name, buflen );
945 res = full_name.long_name +
946 strlen(DRIVE_GetRoot( full_name.drive ));
947 while (*res == '/') res++;
948 if (buflen)
950 LPWSTR p;
951 if (buflen > 3)
953 MultiByteToWideChar(DRIVE_GetCodepage(full_name.drive), 0,
954 res, -1, buffer + 3, buflen - 3);
955 buffer[buflen - 1] = 0;
957 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
958 if (lastpart) *lastpart = strrchrW( buffer, '\\' ) + 1;
960 TRACE("Returning %s\n", debugstr_w(buffer) );
961 return strlenW(buffer);
965 /***********************************************************************
966 * SearchPathA (KERNEL32.@)
968 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
969 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
971 UNICODE_STRING pathW, nameW, extW;
972 WCHAR bufferW[MAX_PATH];
973 DWORD ret, retW;
975 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
976 else pathW.Buffer = NULL;
977 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
978 else nameW.Buffer = NULL;
979 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
980 else extW.Buffer = NULL;
982 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
984 if (!retW)
985 ret = 0;
986 else if (retW > MAX_PATH)
988 SetLastError(ERROR_FILENAME_EXCED_RANGE);
989 ret = 0;
991 else
993 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
994 if (buflen >= ret)
996 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
997 ret--; /* length without 0 */
998 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
1002 RtlFreeUnicodeString(&pathW);
1003 RtlFreeUnicodeString(&nameW);
1004 RtlFreeUnicodeString(&extW);
1005 return ret;
1009 /***********************************************************************
1010 * search_alternate_path
1013 * FIXME: should return long path names.?
1015 static BOOL search_alternate_path(LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
1016 DOS_FULL_NAME *full_name)
1018 LPCWSTR p;
1019 LPWSTR tmp = NULL;
1020 BOOL ret = TRUE;
1022 /* First check the supplied parameters */
1024 p = strrchrW( name, '.' );
1025 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
1026 ext = NULL; /* Ignore the specified extension */
1028 /* Allocate a buffer for the file name and extension */
1030 if (ext)
1032 DWORD len = strlenW(name) + strlenW(ext);
1033 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
1035 SetLastError( ERROR_OUTOFMEMORY );
1036 return 0;
1038 strcpyW( tmp, name );
1039 strcatW( tmp, ext );
1040 name = tmp;
1043 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
1045 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
1047 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
1049 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
1051 else
1052 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
1054 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
1055 return ret;
1059 /***********************************************************************
1060 * DIR_SearchAlternatePath
1062 * Searches for a specified file in the search path.
1064 * PARAMS
1065 * dll_path [I] Path to search
1066 * name [I] Filename to search for.
1067 * ext [I] File extension to append to file name. The first
1068 * character must be a period. This parameter is
1069 * specified only if the filename given does not
1070 * contain an extension.
1071 * buflen [I] size of buffer, in characters
1072 * buffer [O] buffer for found filename
1073 * lastpart [O] address of pointer to last used character in
1074 * buffer (the final '\') (May be NULL)
1076 * RETURNS
1077 * Success: length of string copied into buffer, not including
1078 * terminating null character. If the filename found is
1079 * longer than the length of the buffer, the length of the
1080 * filename is returned.
1081 * Failure: Zero
1083 * NOTES
1084 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1086 * FIXME: convert to unicode
1088 DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
1089 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
1091 LPSTR p;
1092 DOS_FULL_NAME full_name;
1093 DWORD ret = 0;
1094 UNICODE_STRING dll_pathW, nameW, extW;
1096 if (dll_path) RtlCreateUnicodeStringFromAsciiz(&dll_pathW, dll_path);
1097 else dll_pathW.Buffer = NULL;
1098 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
1099 else nameW.Buffer = NULL;
1100 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
1101 else extW.Buffer = NULL;
1103 if (search_alternate_path( dll_pathW.Buffer, nameW.Buffer, extW.Buffer, &full_name))
1105 ret = WideCharToMultiByte(CP_ACP, 0, full_name.short_name, -1, NULL, 0, NULL, NULL);
1106 if (buflen >= ret)
1108 WideCharToMultiByte(CP_ACP, 0, full_name.short_name, -1, buffer, buflen, NULL, NULL);
1109 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
1110 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
1111 ret--; /* length without 0 */
1114 else
1115 SetLastError(ERROR_FILE_NOT_FOUND);
1117 RtlFreeUnicodeString(&dll_pathW);
1118 RtlFreeUnicodeString(&nameW);
1119 RtlFreeUnicodeString(&extW);
1121 TRACE("Returning %ld\n", ret );
1122 return ret;