- fix mipmap texture creation
[wine/wine64.git] / files / directory.c
blobfd13de238f1f3bc8ef4ab455969d5fdc25e99c23
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, NULL ) && (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 wcmdW[] = {'\\','w','c','m','d','.','e','x','e',0};
123 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
124 static const WCHAR empty_strW[] = { 0 };
126 if (!getcwd( path, MAX_PATHNAME_LEN ))
128 perror( "Could not get current directory" );
129 return 0;
131 cwd = path;
132 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
134 MESSAGE("Warning: could not find wine config [Drive x] entry "
135 "for current working directory %s; "
136 "starting in windows directory.\n", cwd );
138 else
140 WCHAR szdrive[3]={drive+'A',':',0};
141 MultiByteToWideChar(DRIVE_GetCodepage(drive), 0, cwd, -1, longpath, MAX_PATHNAME_LEN);
142 DRIVE_SetCurrentDrive( drive );
143 DRIVE_Chdir( drive, longpath );
144 if(GetDriveTypeW(szdrive)==DRIVE_CDROM)
145 chdir("/"); /* change to root directory so as not to lock cdroms */
148 if (!(DIR_GetPath( windowsW, windows_dirW, &DIR_Windows, longpath, MAX_PATHNAME_LEN, TRUE )) ||
149 !(DIR_GetPath( systemW, system_dirW, &DIR_System, longpath, MAX_PATHNAME_LEN, TRUE )) ||
150 !(DIR_GetPath( tempW, windows_dirW, &tmp_dir, longpath, MAX_PATHNAME_LEN, TRUE )))
152 PROFILE_UsageWineIni();
153 return 0;
155 if (-1 == access( tmp_dir.long_name, W_OK ))
157 if (errno==EACCES)
159 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
160 PROFILE_UsageWineIni();
162 else
163 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
164 tmp_dir.long_name, strerror(errno));
167 if (drive == -1)
169 drive = DIR_Windows.drive;
170 DRIVE_SetCurrentDrive( drive );
171 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
174 /* Set the environment variables */
176 /* set COMSPEC only if it doesn't exist already */
177 if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
179 strcpyW( longpath, DIR_System.short_name );
180 strcatW( longpath, wcmdW );
181 SetEnvironmentVariableW( comspecW, longpath );
184 /* set PATH only if not set already */
185 if (!GetEnvironmentVariableW( path_capsW, longpath, MAX_PATHNAME_LEN ))
187 PROFILE_GetWineIniString(wineW, pathW, path_dirW, longpath, MAX_PATHNAME_LEN);
188 if (strchrW(longpath, '/'))
190 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
191 PROFILE_UsageWineIni();
192 ExitProcess(1);
194 SetEnvironmentVariableW( path_capsW, longpath );
197 SetEnvironmentVariableW( temp_capsW, tmp_dir.short_name );
198 SetEnvironmentVariableW( tmp_capsW, tmp_dir.short_name );
199 SetEnvironmentVariableW( windirW, DIR_Windows.short_name );
200 SetEnvironmentVariableW( winsysdirW, DIR_System.short_name );
202 TRACE("WindowsDir = %s (%s)\n",
203 debugstr_w(DIR_Windows.short_name), DIR_Windows.long_name );
204 TRACE("SystemDir = %s (%s)\n",
205 debugstr_w(DIR_System.short_name), DIR_System.long_name );
206 TRACE("TempDir = %s (%s)\n",
207 debugstr_w(tmp_dir.short_name), tmp_dir.long_name );
208 TRACE("Path = %s\n", debugstr_w(longpath) );
209 TRACE("Cwd = %c:\\%s\n",
210 'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
212 if (DIR_GetPath( profileW, empty_strW, &profile_dir, longpath, MAX_PATHNAME_LEN, FALSE ))
214 TRACE("USERPROFILE= %s\n", debugstr_w(longpath) );
215 SetEnvironmentVariableW( userprofileW, longpath );
218 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows.short_name) );
219 SetEnvironmentVariableW( systemrootW, DIR_Windows.short_name );
221 return 1;
225 /***********************************************************************
226 * GetTempPathA (KERNEL32.@)
228 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
230 WCHAR pathW[MAX_PATH];
231 UINT ret;
233 ret = GetTempPathW(MAX_PATH, pathW);
235 if (!ret)
236 return 0;
238 if (ret > MAX_PATH)
240 SetLastError(ERROR_FILENAME_EXCED_RANGE);
241 return 0;
244 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
245 if (ret <= count)
247 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
248 ret--; /* length without 0 */
250 return ret;
254 /***********************************************************************
255 * GetTempPathW (KERNEL32.@)
257 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
259 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
260 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
261 WCHAR tmp_path[MAX_PATH];
262 UINT ret;
264 TRACE("%u,%p\n", count, path);
266 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
267 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
268 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
269 return 0;
271 if (ret > MAX_PATH)
273 SetLastError(ERROR_FILENAME_EXCED_RANGE);
274 return 0;
277 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
278 if (!ret) return 0;
280 if (ret > MAX_PATH - 2)
282 SetLastError(ERROR_FILENAME_EXCED_RANGE);
283 return 0;
286 if (tmp_path[ret-1] != '\\')
288 tmp_path[ret++] = '\\';
289 tmp_path[ret] = '\0';
292 ret++; /* add space for terminating 0 */
294 if (count)
296 lstrcpynW(path, tmp_path, count);
297 if (count >= ret)
298 ret--; /* return length without 0 */
299 else if (count < 4)
300 path[0] = 0; /* avoid returning ambiguous "X:" */
303 TRACE("returning %u, %s\n", ret, debugstr_w(path));
304 return ret;
308 /***********************************************************************
309 * DIR_GetWindowsUnixDir
311 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
313 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
314 return strlen( DIR_Windows.long_name );
318 /***********************************************************************
319 * DIR_GetSystemUnixDir
321 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
323 if (path) lstrcpynA( path, DIR_System.long_name, count );
324 return strlen( DIR_System.long_name );
328 /***********************************************************************
329 * GetTempDrive (KERNEL.92)
330 * A closer look at krnl386.exe shows what the SDK doesn't mention:
332 * returns:
333 * AL: driveletter
334 * AH: ':' - yes, some kernel code even does stosw with
335 * the returned AX.
336 * DX: 1 for success
338 UINT WINAPI GetTempDrive( BYTE ignored )
340 char *buffer;
341 BYTE ret;
342 UINT len = GetTempPathA( 0, NULL );
344 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
345 ret = DRIVE_GetCurrentDrive() + 'A';
346 else
348 /* FIXME: apparently Windows does something with the ignored byte */
349 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
350 ret = toupper(buffer[0]);
351 HeapFree( GetProcessHeap(), 0, buffer );
353 return MAKELONG( ret | (':' << 8), 1 );
357 /***********************************************************************
358 * GetWindowsDirectory (KERNEL.134)
360 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
362 return (UINT16)GetWindowsDirectoryA( path, count );
366 /***********************************************************************
367 * GetWindowsDirectoryW (KERNEL32.@)
369 * See comment for GetWindowsDirectoryA.
371 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
373 UINT len = strlenW( DIR_Windows.short_name ) + 1;
374 if (path && count >= len)
376 strcpyW( path, DIR_Windows.short_name );
377 len--;
379 return len;
383 /***********************************************************************
384 * GetWindowsDirectoryA (KERNEL32.@)
386 * Return value:
387 * If buffer is large enough to hold full path and terminating '\0' character
388 * function copies path to buffer and returns length of the path without '\0'.
389 * Otherwise function returns required size including '\0' character and
390 * does not touch the buffer.
392 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
394 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0, NULL, NULL );
395 if (path && count >= len)
397 WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, path, count, NULL, NULL );
398 len--;
400 return len;
404 /***********************************************************************
405 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
407 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
409 return GetWindowsDirectoryA( path, count );
413 /***********************************************************************
414 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
416 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
418 return GetWindowsDirectoryW( path, count );
422 /***********************************************************************
423 * GetSystemDirectory (KERNEL.135)
425 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
427 return (UINT16)GetSystemDirectoryA( path, count );
431 /***********************************************************************
432 * GetSystemDirectoryW (KERNEL32.@)
434 * See comment for GetWindowsDirectoryA.
436 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
438 UINT len = strlenW( DIR_System.short_name ) + 1;
439 if (path && count >= len)
441 strcpyW( path, DIR_System.short_name );
442 len--;
444 return len;
448 /***********************************************************************
449 * GetSystemDirectoryA (KERNEL32.@)
451 * See comment for GetWindowsDirectoryA.
453 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
455 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0, NULL, NULL );
456 if (path && count >= len)
458 WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, path, count, NULL, NULL );
459 len--;
461 return len;
465 /***********************************************************************
466 * CreateDirectory (KERNEL.144)
468 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
470 TRACE_(file)("(%s,%p)\n", path, dummy );
471 return (BOOL16)CreateDirectoryA( path, NULL );
475 /***********************************************************************
476 * CreateDirectoryW (KERNEL32.@)
477 * RETURNS:
478 * TRUE : success
479 * FALSE : failure
480 * ERROR_DISK_FULL: on full disk
481 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
482 * ERROR_ACCESS_DENIED: on permission problems
483 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
485 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
486 LPSECURITY_ATTRIBUTES lpsecattribs )
488 DOS_FULL_NAME full_name;
490 if (!path || !*path)
492 SetLastError(ERROR_PATH_NOT_FOUND);
493 return FALSE;
496 TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
498 if (DOSFS_GetDevice( path ))
500 TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
501 SetLastError( ERROR_ACCESS_DENIED );
502 return FALSE;
504 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
505 if (mkdir( full_name.long_name, 0777 ) == -1) {
506 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
507 /* the FILE_SetDosError() generated error codes don't match the
508 * CreateDirectory ones for some errnos */
509 switch (errno) {
510 case EEXIST:
512 if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
513 SetLastError(ERROR_ACCESS_DENIED);
514 else
515 SetLastError(ERROR_ALREADY_EXISTS);
516 break;
518 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
519 default: FILE_SetDosError();break;
521 return FALSE;
523 return TRUE;
527 /***********************************************************************
528 * CreateDirectoryA (KERNEL32.@)
530 BOOL WINAPI CreateDirectoryA( LPCSTR path,
531 LPSECURITY_ATTRIBUTES lpsecattribs )
533 UNICODE_STRING pathW;
534 BOOL ret = FALSE;
536 if (!path || !*path)
538 SetLastError(ERROR_PATH_NOT_FOUND);
539 return FALSE;
542 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
544 ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
545 RtlFreeUnicodeString(&pathW);
547 else
548 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
549 return ret;
553 /***********************************************************************
554 * CreateDirectoryExA (KERNEL32.@)
556 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
557 LPSECURITY_ATTRIBUTES lpsecattribs)
559 return CreateDirectoryA(path,lpsecattribs);
563 /***********************************************************************
564 * CreateDirectoryExW (KERNEL32.@)
566 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
567 LPSECURITY_ATTRIBUTES lpsecattribs)
569 return CreateDirectoryW(path,lpsecattribs);
573 /***********************************************************************
574 * RemoveDirectory (KERNEL.145)
576 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
578 return (BOOL16)RemoveDirectoryA( path );
582 /***********************************************************************
583 * RemoveDirectoryW (KERNEL32.@)
585 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
587 DOS_FULL_NAME full_name;
589 if (!path)
591 SetLastError(ERROR_INVALID_PARAMETER);
592 return FALSE;
595 TRACE_(file)("%s\n", debugstr_w(path));
597 if (DOSFS_GetDevice( path ))
599 TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
600 SetLastError( ERROR_FILE_NOT_FOUND );
601 return FALSE;
603 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
604 if (rmdir( full_name.long_name ) == -1)
606 FILE_SetDosError();
607 return FALSE;
609 return TRUE;
613 /***********************************************************************
614 * RemoveDirectoryA (KERNEL32.@)
616 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
618 UNICODE_STRING pathW;
619 BOOL ret = FALSE;
621 if (!path)
623 SetLastError(ERROR_INVALID_PARAMETER);
624 return FALSE;
627 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
629 ret = RemoveDirectoryW(pathW.Buffer);
630 RtlFreeUnicodeString(&pathW);
632 else
633 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
634 return ret;
638 /***********************************************************************
639 * DIR_TryPath
641 * Helper function for DIR_SearchPath.
643 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCWSTR name,
644 DOS_FULL_NAME *full_name )
646 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
647 LPWSTR p_s = full_name->short_name + strlenW(dir->short_name) + 1;
649 if ((p_s >= full_name->short_name + sizeof(full_name->short_name)/sizeof(full_name->short_name[0]) - 14) ||
650 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
652 SetLastError( ERROR_PATH_NOT_FOUND );
653 return FALSE;
655 if (!DOSFS_FindUnixName( dir, name, p_l,
656 sizeof(full_name->long_name) - (p_l - full_name->long_name),
657 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
658 return FALSE;
660 full_name->drive = dir->drive;
661 strcpy( full_name->long_name, dir->long_name );
662 p_l[-1] = '/';
663 strcpyW( full_name->short_name, dir->short_name );
664 p_s[-1] = '\\';
665 return TRUE;
668 static BOOL DIR_SearchSemicolonedPaths(LPCWSTR name, DOS_FULL_NAME *full_name, LPWSTR pathlist)
670 LPWSTR next, buffer = NULL;
671 INT len = strlenW(name), newlen, currlen = 0;
672 BOOL ret = FALSE;
674 next = pathlist;
675 while (!ret && next)
677 static const WCHAR bkslashW[] = {'\\',0};
678 LPWSTR cur = next;
679 while (*cur == ';') cur++;
680 if (!*cur) break;
681 next = strchrW( cur, ';' );
682 if (next) *next++ = '\0';
683 newlen = strlenW(cur) + len + 2;
685 if (newlen > currlen)
687 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen * sizeof(WCHAR))))
688 goto done;
689 currlen = newlen;
692 strcpyW( buffer, cur );
693 strcatW( buffer, bkslashW );
694 strcatW( buffer, name );
695 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
697 done:
698 HeapFree( GetProcessHeap(), 0, buffer );
699 return ret;
703 /***********************************************************************
704 * DIR_TryEnvironmentPath
706 * Helper function for DIR_SearchPath.
707 * Search in the specified path, or in $PATH if NULL.
709 static BOOL DIR_TryEnvironmentPath( LPCWSTR name, DOS_FULL_NAME *full_name, LPCWSTR envpath )
711 LPWSTR path;
712 BOOL ret = FALSE;
713 DWORD size;
714 static const WCHAR pathW[] = {'P','A','T','H',0};
716 size = envpath ? strlenW(envpath)+1 : GetEnvironmentVariableW( pathW, NULL, 0 );
717 if (!size) return FALSE;
718 if (!(path = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
719 if (envpath) strcpyW( path, envpath );
720 else if (!GetEnvironmentVariableW( pathW, path, size )) goto done;
722 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
724 done:
725 HeapFree( GetProcessHeap(), 0, path );
726 return ret;
730 /***********************************************************************
731 * DIR_TryModulePath
733 * Helper function for DIR_SearchPath.
735 static BOOL DIR_TryModulePath( LPCWSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
737 WCHAR bufferW[MAX_PATH];
738 LPWSTR p;
740 if (!win32)
742 char buffer[OFS_MAXPATHNAME];
743 if (!GetCurrentTask()) return FALSE;
744 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
745 return FALSE;
746 MultiByteToWideChar(CP_ACP, 0, buffer, -1, bufferW, MAX_PATH);
747 } else {
748 if (!GetModuleFileNameW( 0, bufferW, MAX_PATH ) )
749 return FALSE;
751 if (!(p = strrchrW( bufferW, '\\' ))) return FALSE;
752 if (MAX_PATH - (++p - bufferW) <= strlenW(name)) return FALSE;
753 strcpyW( p, name );
754 return DOSFS_GetFullName( bufferW, TRUE, full_name );
758 /***********************************************************************
759 * DIR_TryAppPath
761 * Helper function for DIR_SearchPath.
763 static BOOL DIR_TryAppPath( LPCWSTR name, DOS_FULL_NAME *full_name )
765 HKEY hkAppPaths = 0, hkApp = 0;
766 WCHAR buffer[MAX_PATHNAME_LEN], *lpAppPaths;
767 LPWSTR lpFileName;
768 BOOL res = FALSE;
769 DWORD count;
770 OBJECT_ATTRIBUTES attr;
771 UNICODE_STRING nameW;
772 KEY_VALUE_PARTIAL_INFORMATION *info;
773 static const WCHAR PathW[] = {'P','a','t','h',0};
774 static const WCHAR AppPathsW[] = {'M','a','c','h','i','n','e','\\',
775 'S','o','f','t','w','a','r','e','\\',
776 'M','i','c','r','o','s','o','f','t','\\',
777 'W','i','n','d','o','w','s','\\',
778 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
779 'A','p','p',' ','P','a','t','h','s',0};
781 attr.Length = sizeof(attr);
782 attr.RootDirectory = 0;
783 attr.ObjectName = &nameW;
784 attr.Attributes = 0;
785 attr.SecurityDescriptor = NULL;
786 attr.SecurityQualityOfService = NULL;
787 RtlInitUnicodeString( &nameW, AppPathsW );
788 if (NtOpenKey( &hkAppPaths, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) return FALSE;
790 if (!GetModuleFileNameW(0, buffer, MAX_PATHNAME_LEN))
792 WARN("huh, module not found ??\n");
793 goto end;
795 lpFileName = strrchrW(buffer, '\\');
796 if (!lpFileName) lpFileName = buffer;
797 else lpFileName++; /* skip '\\' */
799 attr.RootDirectory = hkAppPaths;
800 RtlInitUnicodeString( &nameW, lpFileName );
801 if (NtOpenKey( &hkApp, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS) goto end;
803 RtlInitUnicodeString( &nameW, PathW );
804 if (NtQueryValueKey( hkApp, &nameW, KeyValuePartialInformation,
805 buffer, sizeof(buffer)-sizeof(WCHAR), &count )) goto end;
806 info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
807 lpAppPaths = (WCHAR *)info->Data;
808 lpAppPaths[info->DataLength/sizeof(WCHAR)] = 0;
809 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
810 end:
811 if (hkApp) NtClose(hkApp);
812 if (hkAppPaths) NtClose(hkAppPaths);
813 return res;
816 /***********************************************************************
817 * DIR_SearchPath
819 * Implementation of SearchPathA. 'win32' specifies whether the search
820 * order is Win16 (module path last) or Win32 (module path first).
822 * FIXME: should return long path names.
824 DWORD DIR_SearchPath( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
825 DOS_FULL_NAME *full_name, BOOL win32 )
827 LPCWSTR p;
828 LPWSTR tmp = NULL;
829 BOOL ret = TRUE;
831 /* First check the supplied parameters */
833 p = strrchrW( name, '.' );
834 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
835 ext = NULL; /* Ignore the specified extension */
836 if (FILE_contains_pathW (name))
837 path = NULL; /* Ignore path if name already contains a path */
838 if (path && !*path) path = NULL; /* Ignore empty path */
840 /* Allocate a buffer for the file name and extension */
842 if (ext)
844 DWORD len = strlenW(name) + strlenW(ext);
845 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
847 SetLastError( ERROR_OUTOFMEMORY );
848 return 0;
850 strcpyW( tmp, name );
851 strcatW( tmp, ext );
852 name = tmp;
855 /* If the name contains an explicit path, everything's easy */
857 if (FILE_contains_pathW(name))
859 ret = DOSFS_GetFullName( name, TRUE, full_name );
860 goto done;
863 /* Search in the specified path */
865 if (path)
867 ret = DIR_TryEnvironmentPath( name, full_name, path );
868 goto done;
871 /* Try the path of the current executable (for Win32 search order) */
873 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
875 /* Try the current directory */
877 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
879 /* Try the Windows system directory */
881 if (DIR_TryPath( &DIR_System, name, full_name ))
882 goto done;
884 /* Try the Windows directory */
886 if (DIR_TryPath( &DIR_Windows, name, full_name ))
887 goto done;
889 /* Try the path of the current executable (for Win16 search order) */
891 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
893 /* Try the "App Paths" entry if existing (undocumented ??) */
894 if (DIR_TryAppPath(name, full_name))
895 goto done;
897 /* Try all directories in path */
899 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
901 done:
902 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
903 return ret;
907 /***********************************************************************
908 * SearchPathW [KERNEL32.@]
910 * Searches for a specified file in the search path.
912 * PARAMS
913 * path [I] Path to search
914 * name [I] Filename to search for.
915 * ext [I] File extension to append to file name. The first
916 * character must be a period. This parameter is
917 * specified only if the filename given does not
918 * contain an extension.
919 * buflen [I] size of buffer, in characters
920 * buffer [O] buffer for found filename
921 * lastpart [O] address of pointer to last used character in
922 * buffer (the final '\')
924 * RETURNS
925 * Success: length of string copied into buffer, not including
926 * terminating null character. If the filename found is
927 * longer than the length of the buffer, the length of the
928 * filename is returned.
929 * Failure: Zero
931 * NOTES
932 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
933 * (tested on NT 4.0)
935 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
936 LPWSTR buffer, LPWSTR *lastpart )
938 LPSTR res;
939 DOS_FULL_NAME full_name;
941 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
943 SetLastError(ERROR_FILE_NOT_FOUND);
944 return 0;
947 TRACE("found %s %s\n", full_name.long_name, debugstr_w(full_name.short_name));
948 TRACE("drive %c: root %s\n", 'A' + full_name.drive, DRIVE_GetRoot(full_name.drive));
950 lstrcpynW( buffer, full_name.short_name, buflen );
951 res = full_name.long_name +
952 strlen(DRIVE_GetRoot( full_name.drive ));
953 while (*res == '/') res++;
954 if (buflen)
956 LPWSTR p;
957 if (buflen > 3)
959 MultiByteToWideChar(DRIVE_GetCodepage(full_name.drive), 0,
960 res, -1, buffer + 3, buflen - 3);
961 buffer[buflen - 1] = 0;
963 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
964 if (lastpart) *lastpart = strrchrW( buffer, '\\' ) + 1;
966 TRACE("Returning %s\n", debugstr_w(buffer) );
967 return strlenW(buffer);
971 /***********************************************************************
972 * SearchPathA (KERNEL32.@)
974 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
975 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
977 UNICODE_STRING pathW, nameW, extW;
978 WCHAR bufferW[MAX_PATH];
979 DWORD ret, retW;
981 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
982 else pathW.Buffer = NULL;
983 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
984 else nameW.Buffer = NULL;
985 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
986 else extW.Buffer = NULL;
988 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
990 if (!retW)
991 ret = 0;
992 else if (retW > MAX_PATH)
994 SetLastError(ERROR_FILENAME_EXCED_RANGE);
995 ret = 0;
997 else
999 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1000 if (buflen >= ret)
1002 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
1003 ret--; /* length without 0 */
1004 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
1008 RtlFreeUnicodeString(&pathW);
1009 RtlFreeUnicodeString(&nameW);
1010 RtlFreeUnicodeString(&extW);
1011 return ret;
1015 /***********************************************************************
1016 * search_alternate_path
1019 * FIXME: should return long path names.?
1021 static BOOL search_alternate_path(LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
1022 DOS_FULL_NAME *full_name)
1024 LPCWSTR p;
1025 LPWSTR tmp = NULL;
1026 BOOL ret = TRUE;
1028 /* First check the supplied parameters */
1030 p = strrchrW( name, '.' );
1031 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
1032 ext = NULL; /* Ignore the specified extension */
1034 /* Allocate a buffer for the file name and extension */
1036 if (ext)
1038 DWORD len = strlenW(name) + strlenW(ext);
1039 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
1041 SetLastError( ERROR_OUTOFMEMORY );
1042 return 0;
1044 strcpyW( tmp, name );
1045 strcatW( tmp, ext );
1046 name = tmp;
1049 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
1051 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
1053 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
1055 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
1057 else
1058 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
1060 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
1061 return ret;
1065 /***********************************************************************
1066 * DIR_SearchAlternatePath
1068 * Searches for a specified file in the search path.
1070 * PARAMS
1071 * dll_path [I] Path to search
1072 * name [I] Filename to search for.
1073 * ext [I] File extension to append to file name. The first
1074 * character must be a period. This parameter is
1075 * specified only if the filename given does not
1076 * contain an extension.
1077 * buflen [I] size of buffer, in characters
1078 * buffer [O] buffer for found filename
1079 * lastpart [O] address of pointer to last used character in
1080 * buffer (the final '\') (May be NULL)
1082 * RETURNS
1083 * Success: length of string copied into buffer, not including
1084 * terminating null character. If the filename found is
1085 * longer than the length of the buffer, the length of the
1086 * filename is returned.
1087 * Failure: Zero
1089 * NOTES
1090 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1092 * FIXME: convert to unicode
1094 DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
1095 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
1097 LPSTR p;
1098 DOS_FULL_NAME full_name;
1099 DWORD ret = 0;
1100 UNICODE_STRING dll_pathW, nameW, extW;
1102 if (dll_path) RtlCreateUnicodeStringFromAsciiz(&dll_pathW, dll_path);
1103 else dll_pathW.Buffer = NULL;
1104 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
1105 else nameW.Buffer = NULL;
1106 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
1107 else extW.Buffer = NULL;
1109 if (search_alternate_path( dll_pathW.Buffer, nameW.Buffer, extW.Buffer, &full_name))
1111 ret = WideCharToMultiByte(CP_ACP, 0, full_name.short_name, -1, NULL, 0, NULL, NULL);
1112 if (buflen >= ret)
1114 WideCharToMultiByte(CP_ACP, 0, full_name.short_name, -1, buffer, buflen, NULL, NULL);
1115 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
1116 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
1117 ret--; /* length without 0 */
1120 else
1121 SetLastError(ERROR_FILE_NOT_FOUND);
1123 RtlFreeUnicodeString(&dll_pathW);
1124 RtlFreeUnicodeString(&nameW);
1125 RtlFreeUnicodeString(&extW);
1127 TRACE("Returning %ld\n", ret );
1128 return ret;