Cast time_t parameters to fprintf(...,"%l",...) to long.
[wine/multimedia.git] / files / directory.c
blobbf869d3f96eafe6878d44257921b0230267aa7d4
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 <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <errno.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
36 #endif
38 #include "ntstatus.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42 #include "wingdi.h"
43 #include "wine/winuser16.h"
44 #include "winerror.h"
45 #include "winreg.h"
46 #include "winternl.h"
47 #include "wine/unicode.h"
48 #include "drive.h"
49 #include "file.h"
50 #include "msdos.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
54 WINE_DECLARE_DEBUG_CHANNEL(file);
56 static DOS_FULL_NAME DIR_Windows;
57 static DOS_FULL_NAME DIR_System;
59 static const WCHAR wineW[] = {'w','i','n','e',0};
61 /***********************************************************************
62 * FILE_contains_pathW
64 inline static int FILE_contains_pathW (LPCWSTR name)
66 return ((*name && (name[1] == ':')) ||
67 strchrW (name, '/') || strchrW (name, '\\'));
70 /***********************************************************************
71 * DIR_GetPath
73 * Get a path name from the wine.ini file and make sure it is valid.
75 static int DIR_GetPath( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, DOS_FULL_NAME *full_name,
76 LPWSTR longname, INT longname_len, BOOL warn )
78 UNICODE_STRING nameW;
79 DWORD dummy;
80 WCHAR tmp[MAX_PATHNAME_LEN];
81 BY_HANDLE_FILE_INFORMATION info;
82 const WCHAR *path = defval;
83 const char *mess = "does not exist";
85 RtlInitUnicodeString( &nameW, keyname );
86 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
87 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
89 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
90 (!FILE_Stat( full_name->long_name, &info, NULL ) && (mess=strerror(errno)))||
91 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
92 (!(GetLongPathNameW(full_name->short_name, longname, longname_len))) )
94 if (warn)
96 MESSAGE("Invalid path %s for %s directory: %s.\n",
97 debugstr_w(path), debugstr_w(keyname), mess);
98 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
99 wine_get_config_dir());
101 return 0;
103 return 1;
107 /***********************************************************************
108 * DIR_Init
110 int DIR_Init(void)
112 OBJECT_ATTRIBUTES attr;
113 UNICODE_STRING nameW;
114 HKEY hkey;
115 char path[MAX_PATHNAME_LEN];
116 WCHAR longpath[MAX_PATHNAME_LEN];
117 DOS_FULL_NAME tmp_dir, profile_dir;
118 int drive;
119 const char *cwd;
120 static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
121 'S','o','f','t','w','a','r','e','\\',
122 'W','i','n','e','\\','W','i','n','e','\\',
123 'C','o','n','f','i','g','\\','W','i','n','e',0};
124 static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
125 static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
126 static const WCHAR tempW[] = {'t','e','m','p',0};
127 static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
128 static const WCHAR windows_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
129 static const WCHAR system_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
130 static const WCHAR pathW[] = {'p','a','t','h',0};
131 static const WCHAR path_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',';',
132 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
133 static const WCHAR path_capsW[] = {'P','A','T','H',0};
134 static const WCHAR temp_capsW[] = {'T','E','M','P',0};
135 static const WCHAR tmp_capsW[] = {'T','M','P',0};
136 static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
137 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
138 static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
139 static const WCHAR systemrootW[] = {'S','Y','S','T','E','M','R','O','O','T',0};
140 static const WCHAR wcmdW[] = {'\\','w','c','m','d','.','e','x','e',0};
141 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
142 static const WCHAR empty_strW[] = { 0 };
144 if (!getcwd( path, MAX_PATHNAME_LEN ))
146 perror( "Could not get current directory" );
147 return 0;
149 cwd = path;
150 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
152 MESSAGE("Warning: could not find wine config [Drive x] entry "
153 "for current working directory %s; "
154 "starting in windows directory.\n", cwd );
156 else
158 WCHAR szdrive[3]={drive+'A',':',0};
159 MultiByteToWideChar(DRIVE_GetCodepage(drive), 0, cwd, -1, longpath, MAX_PATHNAME_LEN);
160 DRIVE_SetCurrentDrive( drive );
161 DRIVE_Chdir( drive, longpath );
162 if(GetDriveTypeW(szdrive)==DRIVE_CDROM)
163 chdir("/"); /* change to root directory so as not to lock cdroms */
166 attr.Length = sizeof(attr);
167 attr.RootDirectory = 0;
168 attr.ObjectName = &nameW;
169 attr.Attributes = 0;
170 attr.SecurityDescriptor = NULL;
171 attr.SecurityQualityOfService = NULL;
173 RtlInitUnicodeString( &nameW, wineW );
174 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
176 if (!(DIR_GetPath( hkey, windowsW, windows_dirW, &DIR_Windows, longpath, MAX_PATHNAME_LEN, TRUE )) ||
177 !(DIR_GetPath( hkey, systemW, system_dirW, &DIR_System, longpath, MAX_PATHNAME_LEN, TRUE )) ||
178 !(DIR_GetPath( hkey, tempW, windows_dirW, &tmp_dir, longpath, MAX_PATHNAME_LEN, TRUE )))
180 if (hkey) NtClose( hkey );
181 return 0;
183 if (-1 == access( tmp_dir.long_name, W_OK ))
185 if (errno==EACCES)
187 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
188 "configuration file (%s) is not writeable.\n",
189 tmp_dir.long_name, wine_get_config_dir() );
191 else
192 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
193 tmp_dir.long_name, strerror(errno));
196 if (drive == -1)
198 drive = DIR_Windows.drive;
199 DRIVE_SetCurrentDrive( drive );
200 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
203 /* Set the environment variables */
205 /* set COMSPEC only if it doesn't exist already */
206 if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
208 strcpyW( longpath, DIR_System.short_name );
209 strcatW( longpath, wcmdW );
210 SetEnvironmentVariableW( comspecW, longpath );
213 /* set PATH only if not set already */
214 if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
216 WCHAR tmp[MAX_PATHNAME_LEN];
217 DWORD dummy;
218 const WCHAR *path = path_dirW;
220 RtlInitUnicodeString( &nameW, pathW );
221 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
222 tmp, sizeof(tmp), &dummy ))
224 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
227 if (strchrW(path, '/'))
229 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
230 ExitProcess(1);
232 SetEnvironmentVariableW( path_capsW, path );
233 TRACE("Path = %s\n", debugstr_w(path) );
236 SetEnvironmentVariableW( temp_capsW, tmp_dir.short_name );
237 SetEnvironmentVariableW( tmp_capsW, tmp_dir.short_name );
238 SetEnvironmentVariableW( windirW, DIR_Windows.short_name );
239 SetEnvironmentVariableW( winsysdirW, DIR_System.short_name );
241 TRACE("WindowsDir = %s (%s)\n",
242 debugstr_w(DIR_Windows.short_name), DIR_Windows.long_name );
243 TRACE("SystemDir = %s (%s)\n",
244 debugstr_w(DIR_System.short_name), DIR_System.long_name );
245 TRACE("TempDir = %s (%s)\n",
246 debugstr_w(tmp_dir.short_name), tmp_dir.long_name );
247 TRACE("Cwd = %c:\\%s\n",
248 'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
250 if (DIR_GetPath( hkey, profileW, empty_strW, &profile_dir, longpath, MAX_PATHNAME_LEN, FALSE ))
252 TRACE("USERPROFILE= %s\n", debugstr_w(longpath) );
253 SetEnvironmentVariableW( userprofileW, longpath );
256 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows.short_name) );
257 SetEnvironmentVariableW( systemrootW, DIR_Windows.short_name );
258 if (hkey) NtClose( hkey );
260 return 1;
264 /***********************************************************************
265 * GetTempPathA (KERNEL32.@)
267 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
269 WCHAR pathW[MAX_PATH];
270 UINT ret;
272 ret = GetTempPathW(MAX_PATH, pathW);
274 if (!ret)
275 return 0;
277 if (ret > MAX_PATH)
279 SetLastError(ERROR_FILENAME_EXCED_RANGE);
280 return 0;
283 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
284 if (ret <= count)
286 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
287 ret--; /* length without 0 */
289 return ret;
293 /***********************************************************************
294 * GetTempPathW (KERNEL32.@)
296 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
298 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
299 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
300 WCHAR tmp_path[MAX_PATH];
301 UINT ret;
303 TRACE("%u,%p\n", count, path);
305 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
306 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
307 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
308 return 0;
310 if (ret > MAX_PATH)
312 SetLastError(ERROR_FILENAME_EXCED_RANGE);
313 return 0;
316 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
317 if (!ret) return 0;
319 if (ret > MAX_PATH - 2)
321 SetLastError(ERROR_FILENAME_EXCED_RANGE);
322 return 0;
325 if (tmp_path[ret-1] != '\\')
327 tmp_path[ret++] = '\\';
328 tmp_path[ret] = '\0';
331 ret++; /* add space for terminating 0 */
333 if (count)
335 lstrcpynW(path, tmp_path, count);
336 if (count >= ret)
337 ret--; /* return length without 0 */
338 else if (count < 4)
339 path[0] = 0; /* avoid returning ambiguous "X:" */
342 TRACE("returning %u, %s\n", ret, debugstr_w(path));
343 return ret;
347 /***********************************************************************
348 * DIR_GetWindowsUnixDir
350 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
352 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
353 return strlen( DIR_Windows.long_name );
357 /***********************************************************************
358 * DIR_GetSystemUnixDir
360 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
362 if (path) lstrcpynA( path, DIR_System.long_name, count );
363 return strlen( DIR_System.long_name );
367 /***********************************************************************
368 * GetTempDrive (KERNEL.92)
369 * A closer look at krnl386.exe shows what the SDK doesn't mention:
371 * returns:
372 * AL: driveletter
373 * AH: ':' - yes, some kernel code even does stosw with
374 * the returned AX.
375 * DX: 1 for success
377 UINT WINAPI GetTempDrive( BYTE ignored )
379 char *buffer;
380 BYTE ret;
381 UINT len = GetTempPathA( 0, NULL );
383 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
384 ret = DRIVE_GetCurrentDrive() + 'A';
385 else
387 /* FIXME: apparently Windows does something with the ignored byte */
388 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
389 ret = toupper(buffer[0]);
390 HeapFree( GetProcessHeap(), 0, buffer );
392 return MAKELONG( ret | (':' << 8), 1 );
396 /***********************************************************************
397 * GetWindowsDirectory (KERNEL.134)
399 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
401 return (UINT16)GetWindowsDirectoryA( path, count );
405 /***********************************************************************
406 * GetWindowsDirectoryW (KERNEL32.@)
408 * See comment for GetWindowsDirectoryA.
410 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
412 UINT len = strlenW( DIR_Windows.short_name ) + 1;
413 if (path && count >= len)
415 strcpyW( path, DIR_Windows.short_name );
416 len--;
418 return len;
422 /***********************************************************************
423 * GetWindowsDirectoryA (KERNEL32.@)
425 * Return value:
426 * If buffer is large enough to hold full path and terminating '\0' character
427 * function copies path to buffer and returns length of the path without '\0'.
428 * Otherwise function returns required size including '\0' character and
429 * does not touch the buffer.
431 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
433 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0, NULL, NULL );
434 if (path && count >= len)
436 WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, path, count, NULL, NULL );
437 len--;
439 return len;
443 /***********************************************************************
444 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
446 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
448 return GetWindowsDirectoryA( path, count );
452 /***********************************************************************
453 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
455 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
457 return GetWindowsDirectoryW( path, count );
461 /***********************************************************************
462 * GetSystemDirectory (KERNEL.135)
464 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
466 return (UINT16)GetSystemDirectoryA( path, count );
470 /***********************************************************************
471 * GetSystemDirectoryW (KERNEL32.@)
473 * See comment for GetWindowsDirectoryA.
475 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
477 UINT len = strlenW( DIR_System.short_name ) + 1;
478 if (path && count >= len)
480 strcpyW( path, DIR_System.short_name );
481 len--;
483 return len;
487 /***********************************************************************
488 * GetSystemDirectoryA (KERNEL32.@)
490 * See comment for GetWindowsDirectoryA.
492 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
494 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0, NULL, NULL );
495 if (path && count >= len)
497 WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, path, count, NULL, NULL );
498 len--;
500 return len;
504 /***********************************************************************
505 * CreateDirectory (KERNEL.144)
507 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
509 TRACE_(file)("(%s,%p)\n", path, dummy );
510 return (BOOL16)CreateDirectoryA( path, NULL );
514 /***********************************************************************
515 * CreateDirectoryW (KERNEL32.@)
516 * RETURNS:
517 * TRUE : success
518 * FALSE : failure
519 * ERROR_DISK_FULL: on full disk
520 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
521 * ERROR_ACCESS_DENIED: on permission problems
522 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
524 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
525 LPSECURITY_ATTRIBUTES lpsecattribs )
527 DOS_FULL_NAME full_name;
529 if (!path || !*path)
531 SetLastError(ERROR_PATH_NOT_FOUND);
532 return FALSE;
535 TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
537 if (DOSFS_GetDevice( path ))
539 TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
540 SetLastError( ERROR_ACCESS_DENIED );
541 return FALSE;
543 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
544 if (mkdir( full_name.long_name, 0777 ) == -1) {
545 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
546 /* the FILE_SetDosError() generated error codes don't match the
547 * CreateDirectory ones for some errnos */
548 switch (errno) {
549 case EEXIST:
551 if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
552 SetLastError(ERROR_ACCESS_DENIED);
553 else
554 SetLastError(ERROR_ALREADY_EXISTS);
555 break;
557 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
558 default: FILE_SetDosError();break;
560 return FALSE;
562 return TRUE;
566 /***********************************************************************
567 * CreateDirectoryA (KERNEL32.@)
569 BOOL WINAPI CreateDirectoryA( LPCSTR path,
570 LPSECURITY_ATTRIBUTES lpsecattribs )
572 UNICODE_STRING pathW;
573 BOOL ret = FALSE;
575 if (!path || !*path)
577 SetLastError(ERROR_PATH_NOT_FOUND);
578 return FALSE;
581 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
583 ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
584 RtlFreeUnicodeString(&pathW);
586 else
587 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
588 return ret;
592 /***********************************************************************
593 * CreateDirectoryExA (KERNEL32.@)
595 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
596 LPSECURITY_ATTRIBUTES lpsecattribs)
598 return CreateDirectoryA(path,lpsecattribs);
602 /***********************************************************************
603 * CreateDirectoryExW (KERNEL32.@)
605 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
606 LPSECURITY_ATTRIBUTES lpsecattribs)
608 return CreateDirectoryW(path,lpsecattribs);
612 /***********************************************************************
613 * RemoveDirectory (KERNEL.145)
615 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
617 return (BOOL16)RemoveDirectoryA( path );
621 /***********************************************************************
622 * RemoveDirectoryW (KERNEL32.@)
624 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
626 DOS_FULL_NAME full_name;
628 if (!path)
630 SetLastError(ERROR_INVALID_PARAMETER);
631 return FALSE;
634 TRACE_(file)("%s\n", debugstr_w(path));
636 if (DOSFS_GetDevice( path ))
638 TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
639 SetLastError( ERROR_FILE_NOT_FOUND );
640 return FALSE;
642 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
643 if (rmdir( full_name.long_name ) == -1)
645 FILE_SetDosError();
646 return FALSE;
648 return TRUE;
652 /***********************************************************************
653 * RemoveDirectoryA (KERNEL32.@)
655 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
657 UNICODE_STRING pathW;
658 BOOL ret = FALSE;
660 if (!path)
662 SetLastError(ERROR_INVALID_PARAMETER);
663 return FALSE;
666 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
668 ret = RemoveDirectoryW(pathW.Buffer);
669 RtlFreeUnicodeString(&pathW);
671 else
672 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
673 return ret;
677 /***********************************************************************
678 * DIR_TryPath
680 * Helper function for DIR_SearchPath.
682 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCWSTR name,
683 DOS_FULL_NAME *full_name )
685 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
686 LPWSTR p_s = full_name->short_name + strlenW(dir->short_name) + 1;
688 if ((p_s >= full_name->short_name + sizeof(full_name->short_name)/sizeof(full_name->short_name[0]) - 14) ||
689 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
691 SetLastError( ERROR_PATH_NOT_FOUND );
692 return FALSE;
694 if (!DOSFS_FindUnixName( dir, name, p_l,
695 sizeof(full_name->long_name) - (p_l - full_name->long_name),
696 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
697 return FALSE;
699 full_name->drive = dir->drive;
700 strcpy( full_name->long_name, dir->long_name );
701 p_l[-1] = '/';
702 strcpyW( full_name->short_name, dir->short_name );
703 p_s[-1] = '\\';
704 return TRUE;
707 static BOOL DIR_SearchSemicolonedPaths(LPCWSTR name, DOS_FULL_NAME *full_name, LPWSTR pathlist)
709 LPWSTR next, buffer = NULL;
710 INT len = strlenW(name), newlen, currlen = 0;
711 BOOL ret = FALSE;
713 next = pathlist;
714 while (!ret && next)
716 static const WCHAR bkslashW[] = {'\\',0};
717 LPWSTR cur = next;
718 while (*cur == ';') cur++;
719 if (!*cur) break;
720 next = strchrW( cur, ';' );
721 if (next) *next++ = '\0';
722 newlen = strlenW(cur) + len + 2;
724 if (newlen > currlen)
726 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen * sizeof(WCHAR))))
727 goto done;
728 currlen = newlen;
731 strcpyW( buffer, cur );
732 strcatW( buffer, bkslashW );
733 strcatW( buffer, name );
734 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
736 done:
737 HeapFree( GetProcessHeap(), 0, buffer );
738 return ret;
742 /***********************************************************************
743 * DIR_TryEnvironmentPath
745 * Helper function for DIR_SearchPath.
746 * Search in the specified path, or in $PATH if NULL.
748 static BOOL DIR_TryEnvironmentPath( LPCWSTR name, DOS_FULL_NAME *full_name, LPCWSTR envpath )
750 LPWSTR path;
751 BOOL ret = FALSE;
752 DWORD size;
753 static const WCHAR pathW[] = {'P','A','T','H',0};
755 size = envpath ? strlenW(envpath)+1 : GetEnvironmentVariableW( pathW, NULL, 0 );
756 if (!size) return FALSE;
757 if (!(path = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
758 if (envpath) strcpyW( path, envpath );
759 else if (!GetEnvironmentVariableW( pathW, path, size )) goto done;
761 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
763 done:
764 HeapFree( GetProcessHeap(), 0, path );
765 return ret;
769 /***********************************************************************
770 * DIR_TryModulePath
772 * Helper function for DIR_SearchPath.
774 static BOOL DIR_TryModulePath( LPCWSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
776 WCHAR bufferW[MAX_PATH];
777 LPWSTR p;
779 if (!win32)
781 char buffer[OFS_MAXPATHNAME];
782 if (!GetCurrentTask()) return FALSE;
783 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
784 return FALSE;
785 MultiByteToWideChar(CP_ACP, 0, buffer, -1, bufferW, MAX_PATH);
786 } else {
787 if (!GetModuleFileNameW( 0, bufferW, MAX_PATH ) )
788 return FALSE;
790 if (!(p = strrchrW( bufferW, '\\' ))) return FALSE;
791 if (MAX_PATH - (++p - bufferW) <= strlenW(name)) return FALSE;
792 strcpyW( p, name );
793 return DOSFS_GetFullName( bufferW, TRUE, full_name );
797 /***********************************************************************
798 * DIR_SearchPath
800 * Implementation of SearchPathA. 'win32' specifies whether the search
801 * order is Win16 (module path last) or Win32 (module path first).
803 * FIXME: should return long path names.
805 DWORD DIR_SearchPath( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
806 DOS_FULL_NAME *full_name, BOOL win32 )
808 LPCWSTR p;
809 LPWSTR tmp = NULL;
810 BOOL ret = TRUE;
812 /* First check the supplied parameters */
814 p = strrchrW( name, '.' );
815 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
816 ext = NULL; /* Ignore the specified extension */
817 if (FILE_contains_pathW (name))
818 path = NULL; /* Ignore path if name already contains a path */
819 if (path && !*path) path = NULL; /* Ignore empty path */
821 /* Allocate a buffer for the file name and extension */
823 if (ext)
825 DWORD len = strlenW(name) + strlenW(ext);
826 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
828 SetLastError( ERROR_OUTOFMEMORY );
829 return 0;
831 strcpyW( tmp, name );
832 strcatW( tmp, ext );
833 name = tmp;
836 /* If the name contains an explicit path, everything's easy */
838 if (FILE_contains_pathW(name))
840 ret = DOSFS_GetFullName( name, TRUE, full_name );
841 goto done;
844 /* Search in the specified path */
846 if (path)
848 ret = DIR_TryEnvironmentPath( name, full_name, path );
849 goto done;
852 /* Try the path of the current executable (for Win32 search order) */
854 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
856 /* Try the current directory */
858 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
860 /* Try the Windows system directory */
862 if (DIR_TryPath( &DIR_System, name, full_name ))
863 goto done;
865 /* Try the Windows directory */
867 if (DIR_TryPath( &DIR_Windows, name, full_name ))
868 goto done;
870 /* Try the path of the current executable (for Win16 search order) */
872 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
874 /* Try all directories in path */
876 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
878 done:
879 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
880 return ret;
884 /***********************************************************************
885 * SearchPathW [KERNEL32.@]
887 * Searches for a specified file in the search path.
889 * PARAMS
890 * path [I] Path to search
891 * name [I] Filename to search for.
892 * ext [I] File extension to append to file name. The first
893 * character must be a period. This parameter is
894 * specified only if the filename given does not
895 * contain an extension.
896 * buflen [I] size of buffer, in characters
897 * buffer [O] buffer for found filename
898 * lastpart [O] address of pointer to last used character in
899 * buffer (the final '\')
901 * RETURNS
902 * Success: length of string copied into buffer, not including
903 * terminating null character. If the filename found is
904 * longer than the length of the buffer, the length of the
905 * filename is returned.
906 * Failure: Zero
908 * NOTES
909 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
910 * (tested on NT 4.0)
912 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
913 LPWSTR buffer, LPWSTR *lastpart )
915 LPSTR res;
916 DOS_FULL_NAME full_name;
918 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
920 SetLastError(ERROR_FILE_NOT_FOUND);
921 return 0;
924 TRACE("found %s %s\n", full_name.long_name, debugstr_w(full_name.short_name));
925 TRACE("drive %c: root %s\n", 'A' + full_name.drive, DRIVE_GetRoot(full_name.drive));
927 lstrcpynW( buffer, full_name.short_name, buflen );
928 res = full_name.long_name +
929 strlen(DRIVE_GetRoot( full_name.drive ));
930 while (*res == '/') res++;
931 if (buflen)
933 LPWSTR p;
934 if (buflen > 3)
936 MultiByteToWideChar(DRIVE_GetCodepage(full_name.drive), 0,
937 res, -1, buffer + 3, buflen - 3);
938 buffer[buflen - 1] = 0;
940 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
941 if (lastpart) *lastpart = strrchrW( buffer, '\\' ) + 1;
943 TRACE("Returning %s\n", debugstr_w(buffer) );
944 return strlenW(buffer);
948 /***********************************************************************
949 * SearchPathA (KERNEL32.@)
951 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
952 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
954 UNICODE_STRING pathW, nameW, extW;
955 WCHAR bufferW[MAX_PATH];
956 DWORD ret, retW;
958 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
959 else pathW.Buffer = NULL;
960 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
961 else nameW.Buffer = NULL;
962 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
963 else extW.Buffer = NULL;
965 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
967 if (!retW)
968 ret = 0;
969 else if (retW > MAX_PATH)
971 SetLastError(ERROR_FILENAME_EXCED_RANGE);
972 ret = 0;
974 else
976 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
977 if (buflen >= ret)
979 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
980 ret--; /* length without 0 */
981 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
985 RtlFreeUnicodeString(&pathW);
986 RtlFreeUnicodeString(&nameW);
987 RtlFreeUnicodeString(&extW);
988 return ret;
992 /***********************************************************************
993 * search_alternate_path
996 * FIXME: should return long path names.?
998 static BOOL search_alternate_path(LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
999 DOS_FULL_NAME *full_name)
1001 LPCWSTR p;
1002 LPWSTR tmp = NULL;
1003 BOOL ret = TRUE;
1005 /* First check the supplied parameters */
1007 p = strrchrW( name, '.' );
1008 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
1009 ext = NULL; /* Ignore the specified extension */
1011 /* Allocate a buffer for the file name and extension */
1013 if (ext)
1015 DWORD len = strlenW(name) + strlenW(ext);
1016 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
1018 SetLastError( ERROR_OUTOFMEMORY );
1019 return 0;
1021 strcpyW( tmp, name );
1022 strcatW( tmp, ext );
1023 name = tmp;
1026 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
1028 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
1030 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
1032 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
1034 else
1035 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
1037 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
1038 return ret;
1042 /***********************************************************************
1043 * DIR_SearchAlternatePath
1045 * Searches for a specified file in the search path.
1047 * PARAMS
1048 * dll_path [I] Path to search
1049 * name [I] Filename to search for.
1050 * ext [I] File extension to append to file name. The first
1051 * character must be a period. This parameter is
1052 * specified only if the filename given does not
1053 * contain an extension.
1054 * buflen [I] size of buffer, in characters
1055 * buffer [O] buffer for found filename
1057 * RETURNS
1058 * Success: length of string copied into buffer, not including
1059 * terminating null character. If the filename found is
1060 * longer than the length of the buffer, the length of the
1061 * filename is returned.
1062 * Failure: Zero
1064 * NOTES
1065 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1067 * FIXME: convert to unicode
1069 DWORD DIR_SearchAlternatePath( LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
1070 DWORD buflen, LPWSTR buffer )
1072 DOS_FULL_NAME full_name;
1073 DWORD ret = 0;
1075 if (search_alternate_path( dll_path, name, ext, &full_name))
1077 lstrcpynW( buffer, full_name.short_name, buflen );
1078 ret = strlenW(buffer);
1080 else
1081 SetLastError(ERROR_FILE_NOT_FOUND);
1083 TRACE("Returning %ld\n", ret );
1084 return ret;