Remove unused variable wineW.
[wine/gsoc_dplay.git] / files / directory.c
blob99f065d611f34682166ce6340245e612109aeb5b
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 /***********************************************************************
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( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, DOS_FULL_NAME *full_name,
74 LPWSTR longname, INT longname_len, BOOL warn )
76 UNICODE_STRING nameW;
77 DWORD dummy;
78 WCHAR tmp[MAX_PATHNAME_LEN];
79 BY_HANDLE_FILE_INFORMATION info;
80 const WCHAR *path = defval;
81 const char *mess = "does not exist";
83 RtlInitUnicodeString( &nameW, keyname );
84 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
85 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
87 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
88 (!FILE_Stat( full_name->long_name, &info, NULL ) && (mess=strerror(errno)))||
89 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
90 (!(GetLongPathNameW(full_name->short_name, longname, longname_len))) )
92 if (warn)
94 MESSAGE("Invalid path %s for %s directory: %s.\n",
95 debugstr_w(path), debugstr_w(keyname), mess);
96 MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
97 wine_get_config_dir());
99 return 0;
101 return 1;
105 /***********************************************************************
106 * DIR_Init
108 int DIR_Init(void)
110 OBJECT_ATTRIBUTES attr;
111 UNICODE_STRING nameW;
112 HKEY hkey;
113 char path[MAX_PATHNAME_LEN];
114 WCHAR longpath[MAX_PATHNAME_LEN];
115 DOS_FULL_NAME tmp_dir, profile_dir;
116 int drive;
117 const char *cwd;
118 static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
119 'S','o','f','t','w','a','r','e','\\',
120 'W','i','n','e','\\','W','i','n','e','\\',
121 'C','o','n','f','i','g','\\','W','i','n','e',0};
122 static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
123 static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
124 static const WCHAR tempW[] = {'t','e','m','p',0};
125 static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
126 static const WCHAR windows_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
127 static const WCHAR system_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
128 static const WCHAR pathW[] = {'p','a','t','h',0};
129 static const WCHAR path_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',';',
130 'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
131 static const WCHAR path_capsW[] = {'P','A','T','H',0};
132 static const WCHAR temp_capsW[] = {'T','E','M','P',0};
133 static const WCHAR tmp_capsW[] = {'T','M','P',0};
134 static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
135 static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
136 static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
137 static const WCHAR systemrootW[] = {'S','Y','S','T','E','M','R','O','O','T',0};
138 static const WCHAR wcmdW[] = {'\\','w','c','m','d','.','e','x','e',0};
139 static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
140 static const WCHAR empty_strW[] = { 0 };
142 if (!getcwd( path, MAX_PATHNAME_LEN ))
144 perror( "Could not get current directory" );
145 return 0;
147 cwd = path;
148 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
150 MESSAGE("Warning: could not find wine config [Drive x] entry "
151 "for current working directory %s; "
152 "starting in windows directory.\n", cwd );
154 else
156 WCHAR szdrive[3]={drive+'A',':',0};
157 MultiByteToWideChar(CP_UNIXCP, 0, cwd, -1, longpath, MAX_PATHNAME_LEN);
158 DRIVE_SetCurrentDrive( drive );
159 DRIVE_Chdir( drive, longpath );
160 if(GetDriveTypeW(szdrive)==DRIVE_CDROM)
161 chdir("/"); /* change to root directory so as not to lock cdroms */
164 attr.Length = sizeof(attr);
165 attr.RootDirectory = 0;
166 attr.ObjectName = &nameW;
167 attr.Attributes = 0;
168 attr.SecurityDescriptor = NULL;
169 attr.SecurityQualityOfService = NULL;
171 RtlInitUnicodeString( &nameW, wineW );
172 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
174 if (!(DIR_GetPath( hkey, windowsW, windows_dirW, &DIR_Windows, longpath, MAX_PATHNAME_LEN, TRUE )) ||
175 !(DIR_GetPath( hkey, systemW, system_dirW, &DIR_System, longpath, MAX_PATHNAME_LEN, TRUE )) ||
176 !(DIR_GetPath( hkey, tempW, windows_dirW, &tmp_dir, longpath, MAX_PATHNAME_LEN, TRUE )))
178 if (hkey) NtClose( hkey );
179 return 0;
181 if (-1 == access( tmp_dir.long_name, W_OK ))
183 if (errno==EACCES)
185 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
186 "configuration file (%s) is not writeable.\n",
187 tmp_dir.long_name, wine_get_config_dir() );
189 else
190 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
191 tmp_dir.long_name, strerror(errno));
194 if (drive == -1)
196 drive = DIR_Windows.drive;
197 DRIVE_SetCurrentDrive( drive );
198 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
201 /* Set the environment variables */
203 /* set COMSPEC only if it doesn't exist already */
204 if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
206 strcpyW( longpath, DIR_System.short_name );
207 strcatW( longpath, wcmdW );
208 SetEnvironmentVariableW( comspecW, longpath );
211 /* set PATH only if not set already */
212 if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
214 WCHAR tmp[MAX_PATHNAME_LEN];
215 DWORD dummy;
216 const WCHAR *path = path_dirW;
218 RtlInitUnicodeString( &nameW, pathW );
219 if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
220 tmp, sizeof(tmp), &dummy ))
222 path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
225 if (strchrW(path, '/'))
227 MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
228 ExitProcess(1);
230 SetEnvironmentVariableW( path_capsW, path );
231 TRACE("Path = %s\n", debugstr_w(path) );
234 SetEnvironmentVariableW( temp_capsW, tmp_dir.short_name );
235 SetEnvironmentVariableW( tmp_capsW, tmp_dir.short_name );
236 SetEnvironmentVariableW( windirW, DIR_Windows.short_name );
237 SetEnvironmentVariableW( winsysdirW, DIR_System.short_name );
239 TRACE("WindowsDir = %s (%s)\n",
240 debugstr_w(DIR_Windows.short_name), DIR_Windows.long_name );
241 TRACE("SystemDir = %s (%s)\n",
242 debugstr_w(DIR_System.short_name), DIR_System.long_name );
243 TRACE("TempDir = %s (%s)\n",
244 debugstr_w(tmp_dir.short_name), tmp_dir.long_name );
245 TRACE("Cwd = %c:\\%s\n",
246 'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
248 if (DIR_GetPath( hkey, profileW, empty_strW, &profile_dir, longpath, MAX_PATHNAME_LEN, FALSE ))
250 TRACE("USERPROFILE= %s\n", debugstr_w(longpath) );
251 SetEnvironmentVariableW( userprofileW, longpath );
254 TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows.short_name) );
255 SetEnvironmentVariableW( systemrootW, DIR_Windows.short_name );
256 if (hkey) NtClose( hkey );
258 return 1;
262 /***********************************************************************
263 * GetTempPathA (KERNEL32.@)
265 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
267 WCHAR pathW[MAX_PATH];
268 UINT ret;
270 ret = GetTempPathW(MAX_PATH, pathW);
272 if (!ret)
273 return 0;
275 if (ret > MAX_PATH)
277 SetLastError(ERROR_FILENAME_EXCED_RANGE);
278 return 0;
281 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
282 if (ret <= count)
284 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
285 ret--; /* length without 0 */
287 return ret;
291 /***********************************************************************
292 * GetTempPathW (KERNEL32.@)
294 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
296 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
297 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
298 WCHAR tmp_path[MAX_PATH];
299 UINT ret;
301 TRACE("%u,%p\n", count, path);
303 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
304 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
305 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
306 return 0;
308 if (ret > MAX_PATH)
310 SetLastError(ERROR_FILENAME_EXCED_RANGE);
311 return 0;
314 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
315 if (!ret) return 0;
317 if (ret > MAX_PATH - 2)
319 SetLastError(ERROR_FILENAME_EXCED_RANGE);
320 return 0;
323 if (tmp_path[ret-1] != '\\')
325 tmp_path[ret++] = '\\';
326 tmp_path[ret] = '\0';
329 ret++; /* add space for terminating 0 */
331 if (count)
333 lstrcpynW(path, tmp_path, count);
334 if (count >= ret)
335 ret--; /* return length without 0 */
336 else if (count < 4)
337 path[0] = 0; /* avoid returning ambiguous "X:" */
340 TRACE("returning %u, %s\n", ret, debugstr_w(path));
341 return ret;
345 /***********************************************************************
346 * DIR_GetWindowsUnixDir
348 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
350 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
351 return strlen( DIR_Windows.long_name );
355 /***********************************************************************
356 * DIR_GetSystemUnixDir
358 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
360 if (path) lstrcpynA( path, DIR_System.long_name, count );
361 return strlen( DIR_System.long_name );
365 /***********************************************************************
366 * GetTempDrive (KERNEL.92)
367 * A closer look at krnl386.exe shows what the SDK doesn't mention:
369 * returns:
370 * AL: driveletter
371 * AH: ':' - yes, some kernel code even does stosw with
372 * the returned AX.
373 * DX: 1 for success
375 UINT WINAPI GetTempDrive( BYTE ignored )
377 char *buffer;
378 BYTE ret;
379 UINT len = GetTempPathA( 0, NULL );
381 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
382 ret = DRIVE_GetCurrentDrive() + 'A';
383 else
385 /* FIXME: apparently Windows does something with the ignored byte */
386 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
387 ret = toupper(buffer[0]);
388 HeapFree( GetProcessHeap(), 0, buffer );
390 return MAKELONG( ret | (':' << 8), 1 );
394 /***********************************************************************
395 * GetWindowsDirectory (KERNEL.134)
397 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
399 return (UINT16)GetWindowsDirectoryA( path, count );
403 /***********************************************************************
404 * GetWindowsDirectoryW (KERNEL32.@)
406 * See comment for GetWindowsDirectoryA.
408 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
410 UINT len = strlenW( DIR_Windows.short_name ) + 1;
411 if (path && count >= len)
413 strcpyW( path, DIR_Windows.short_name );
414 len--;
416 return len;
420 /***********************************************************************
421 * GetWindowsDirectoryA (KERNEL32.@)
423 * Return value:
424 * If buffer is large enough to hold full path and terminating '\0' character
425 * function copies path to buffer and returns length of the path without '\0'.
426 * Otherwise function returns required size including '\0' character and
427 * does not touch the buffer.
429 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
431 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0, NULL, NULL );
432 if (path && count >= len)
434 WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, path, count, NULL, NULL );
435 len--;
437 return len;
441 /***********************************************************************
442 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
444 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
446 return GetWindowsDirectoryA( path, count );
450 /***********************************************************************
451 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
453 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
455 return GetWindowsDirectoryW( path, count );
459 /***********************************************************************
460 * GetSystemDirectory (KERNEL.135)
462 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
464 return (UINT16)GetSystemDirectoryA( path, count );
468 /***********************************************************************
469 * GetSystemDirectoryW (KERNEL32.@)
471 * See comment for GetWindowsDirectoryA.
473 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
475 UINT len = strlenW( DIR_System.short_name ) + 1;
476 if (path && count >= len)
478 strcpyW( path, DIR_System.short_name );
479 len--;
481 return len;
485 /***********************************************************************
486 * GetSystemDirectoryA (KERNEL32.@)
488 * See comment for GetWindowsDirectoryA.
490 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
492 UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0, NULL, NULL );
493 if (path && count >= len)
495 WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, path, count, NULL, NULL );
496 len--;
498 return len;
502 /***********************************************************************
503 * CreateDirectory (KERNEL.144)
505 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
507 TRACE_(file)("(%s,%p)\n", path, dummy );
508 return (BOOL16)CreateDirectoryA( path, NULL );
512 /***********************************************************************
513 * CreateDirectoryW (KERNEL32.@)
514 * RETURNS:
515 * TRUE : success
516 * FALSE : failure
517 * ERROR_DISK_FULL: on full disk
518 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
519 * ERROR_ACCESS_DENIED: on permission problems
520 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
522 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
523 LPSECURITY_ATTRIBUTES lpsecattribs )
525 DOS_FULL_NAME full_name;
527 if (!path || !*path)
529 SetLastError(ERROR_PATH_NOT_FOUND);
530 return FALSE;
533 TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
535 if (DOSFS_GetDevice( path ))
537 TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
538 SetLastError( ERROR_ACCESS_DENIED );
539 return FALSE;
541 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
542 if (mkdir( full_name.long_name, 0777 ) == -1) {
543 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
544 /* the FILE_SetDosError() generated error codes don't match the
545 * CreateDirectory ones for some errnos */
546 switch (errno) {
547 case EEXIST:
549 if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
550 SetLastError(ERROR_ACCESS_DENIED);
551 else
552 SetLastError(ERROR_ALREADY_EXISTS);
553 break;
555 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
556 default: FILE_SetDosError();break;
558 return FALSE;
560 return TRUE;
564 /***********************************************************************
565 * CreateDirectoryA (KERNEL32.@)
567 BOOL WINAPI CreateDirectoryA( LPCSTR path,
568 LPSECURITY_ATTRIBUTES lpsecattribs )
570 UNICODE_STRING pathW;
571 BOOL ret = FALSE;
573 if (!path || !*path)
575 SetLastError(ERROR_PATH_NOT_FOUND);
576 return FALSE;
579 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
581 ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
582 RtlFreeUnicodeString(&pathW);
584 else
585 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
586 return ret;
590 /***********************************************************************
591 * CreateDirectoryExA (KERNEL32.@)
593 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
594 LPSECURITY_ATTRIBUTES lpsecattribs)
596 return CreateDirectoryA(path,lpsecattribs);
600 /***********************************************************************
601 * CreateDirectoryExW (KERNEL32.@)
603 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
604 LPSECURITY_ATTRIBUTES lpsecattribs)
606 return CreateDirectoryW(path,lpsecattribs);
610 /***********************************************************************
611 * RemoveDirectory (KERNEL.145)
613 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
615 return (BOOL16)RemoveDirectoryA( path );
619 /***********************************************************************
620 * RemoveDirectoryW (KERNEL32.@)
622 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
624 DOS_FULL_NAME full_name;
626 if (!path)
628 SetLastError(ERROR_INVALID_PARAMETER);
629 return FALSE;
632 TRACE_(file)("%s\n", debugstr_w(path));
634 if (DOSFS_GetDevice( path ))
636 TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
637 SetLastError( ERROR_FILE_NOT_FOUND );
638 return FALSE;
640 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
641 if (rmdir( full_name.long_name ) == -1)
643 FILE_SetDosError();
644 return FALSE;
646 return TRUE;
650 /***********************************************************************
651 * RemoveDirectoryA (KERNEL32.@)
653 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
655 UNICODE_STRING pathW;
656 BOOL ret = FALSE;
658 if (!path)
660 SetLastError(ERROR_INVALID_PARAMETER);
661 return FALSE;
664 if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
666 ret = RemoveDirectoryW(pathW.Buffer);
667 RtlFreeUnicodeString(&pathW);
669 else
670 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
671 return ret;
675 /***********************************************************************
676 * DIR_TryPath
678 * Helper function for DIR_SearchPath.
680 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCWSTR name,
681 DOS_FULL_NAME *full_name )
683 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
684 LPWSTR p_s = full_name->short_name + strlenW(dir->short_name) + 1;
686 if ((p_s >= full_name->short_name + sizeof(full_name->short_name)/sizeof(full_name->short_name[0]) - 14) ||
687 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
689 SetLastError( ERROR_PATH_NOT_FOUND );
690 return FALSE;
692 if (!DOSFS_FindUnixName( dir, name, p_l,
693 sizeof(full_name->long_name) - (p_l - full_name->long_name),
694 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
695 return FALSE;
697 full_name->drive = dir->drive;
698 strcpy( full_name->long_name, dir->long_name );
699 p_l[-1] = '/';
700 strcpyW( full_name->short_name, dir->short_name );
701 p_s[-1] = '\\';
702 return TRUE;
705 static BOOL DIR_SearchSemicolonedPaths(LPCWSTR name, DOS_FULL_NAME *full_name, LPWSTR pathlist)
707 LPWSTR next, buffer = NULL;
708 INT len = strlenW(name), newlen, currlen = 0;
709 BOOL ret = FALSE;
711 next = pathlist;
712 while (!ret && next)
714 static const WCHAR bkslashW[] = {'\\',0};
715 LPWSTR cur = next;
716 while (*cur == ';') cur++;
717 if (!*cur) break;
718 next = strchrW( cur, ';' );
719 if (next) *next++ = '\0';
720 newlen = strlenW(cur) + len + 2;
722 if (newlen > currlen)
724 if (buffer)
725 buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen * sizeof(WCHAR));
726 else
727 buffer = HeapAlloc( GetProcessHeap(), 0, newlen * sizeof(WCHAR));
729 if(!buffer)
730 goto done;
731 currlen = newlen;
734 strcpyW( buffer, cur );
735 strcatW( buffer, bkslashW );
736 strcatW( buffer, name );
737 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
739 done:
740 HeapFree( GetProcessHeap(), 0, buffer );
741 return ret;
745 /***********************************************************************
746 * DIR_TryEnvironmentPath
748 * Helper function for DIR_SearchPath.
749 * Search in the specified path, or in $PATH if NULL.
751 static BOOL DIR_TryEnvironmentPath( LPCWSTR name, DOS_FULL_NAME *full_name, LPCWSTR envpath )
753 LPWSTR path;
754 BOOL ret = FALSE;
755 DWORD size;
756 static const WCHAR pathW[] = {'P','A','T','H',0};
758 size = envpath ? strlenW(envpath)+1 : GetEnvironmentVariableW( pathW, NULL, 0 );
759 if (!size) return FALSE;
760 if (!(path = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
761 if (envpath) strcpyW( path, envpath );
762 else if (!GetEnvironmentVariableW( pathW, path, size )) goto done;
764 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
766 done:
767 HeapFree( GetProcessHeap(), 0, path );
768 return ret;
772 /***********************************************************************
773 * DIR_TryModulePath
775 * Helper function for DIR_SearchPath.
777 static BOOL DIR_TryModulePath( LPCWSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
779 WCHAR bufferW[MAX_PATH];
780 LPWSTR p;
782 if (!win32)
784 char buffer[OFS_MAXPATHNAME];
785 if (!GetCurrentTask()) return FALSE;
786 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
787 return FALSE;
788 MultiByteToWideChar(CP_ACP, 0, buffer, -1, bufferW, MAX_PATH);
789 } else {
790 if (!GetModuleFileNameW( 0, bufferW, MAX_PATH ) )
791 return FALSE;
793 if (!(p = strrchrW( bufferW, '\\' ))) return FALSE;
794 if (MAX_PATH - (++p - bufferW) <= strlenW(name)) return FALSE;
795 strcpyW( p, name );
796 return DOSFS_GetFullName( bufferW, TRUE, full_name );
800 /***********************************************************************
801 * DIR_SearchPath
803 * Implementation of SearchPathA. 'win32' specifies whether the search
804 * order is Win16 (module path last) or Win32 (module path first).
806 * FIXME: should return long path names.
808 DWORD DIR_SearchPath( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
809 DOS_FULL_NAME *full_name, BOOL win32 )
811 LPCWSTR p;
812 LPWSTR tmp = NULL;
813 BOOL ret = TRUE;
815 /* First check the supplied parameters */
817 p = strrchrW( name, '.' );
818 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
819 ext = NULL; /* Ignore the specified extension */
820 if (FILE_contains_pathW (name))
821 path = NULL; /* Ignore path if name already contains a path */
822 if (path && !*path) path = NULL; /* Ignore empty path */
824 /* Allocate a buffer for the file name and extension */
826 if (ext)
828 DWORD len = strlenW(name) + strlenW(ext);
829 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
831 SetLastError( ERROR_OUTOFMEMORY );
832 return 0;
834 strcpyW( tmp, name );
835 strcatW( tmp, ext );
836 name = tmp;
839 /* If the name contains an explicit path, everything's easy */
841 if (FILE_contains_pathW(name))
843 ret = DOSFS_GetFullName( name, TRUE, full_name );
844 goto done;
847 /* Search in the specified path */
849 if (path)
851 ret = DIR_TryEnvironmentPath( name, full_name, path );
852 goto done;
855 /* Try the path of the current executable (for Win32 search order) */
857 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
859 /* Try the current directory */
861 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
863 /* Try the Windows system directory */
865 if (DIR_TryPath( &DIR_System, name, full_name ))
866 goto done;
868 /* Try the Windows directory */
870 if (DIR_TryPath( &DIR_Windows, name, full_name ))
871 goto done;
873 /* Try the path of the current executable (for Win16 search order) */
875 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
877 /* Try all directories in path */
879 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
881 done:
882 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
883 return ret;
887 /***********************************************************************
888 * SearchPathW [KERNEL32.@]
890 * Searches for a specified file in the search path.
892 * PARAMS
893 * path [I] Path to search
894 * name [I] Filename to search for.
895 * ext [I] File extension to append to file name. The first
896 * character must be a period. This parameter is
897 * specified only if the filename given does not
898 * contain an extension.
899 * buflen [I] size of buffer, in characters
900 * buffer [O] buffer for found filename
901 * lastpart [O] address of pointer to last used character in
902 * buffer (the final '\')
904 * RETURNS
905 * Success: length of string copied into buffer, not including
906 * terminating null character. If the filename found is
907 * longer than the length of the buffer, the length of the
908 * filename is returned.
909 * Failure: Zero
911 * NOTES
912 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
913 * (tested on NT 4.0)
915 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
916 LPWSTR buffer, LPWSTR *lastpart )
918 LPSTR res;
919 DOS_FULL_NAME full_name;
921 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
923 SetLastError(ERROR_FILE_NOT_FOUND);
924 return 0;
927 TRACE("found %s %s\n", full_name.long_name, debugstr_w(full_name.short_name));
928 TRACE("drive %c: root %s\n", 'A' + full_name.drive, DRIVE_GetRoot(full_name.drive));
930 lstrcpynW( buffer, full_name.short_name, buflen );
931 res = full_name.long_name +
932 strlen(DRIVE_GetRoot( full_name.drive ));
933 while (*res == '/') res++;
934 if (buflen)
936 LPWSTR p;
937 if (buflen > 3)
939 MultiByteToWideChar(CP_UNIXCP, 0, res, -1, buffer + 3, buflen - 3);
940 buffer[buflen - 1] = 0;
942 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
943 if (lastpart) *lastpart = strrchrW( buffer, '\\' ) + 1;
945 TRACE("Returning %s\n", debugstr_w(buffer) );
946 return strlenW(buffer);
950 /***********************************************************************
951 * SearchPathA (KERNEL32.@)
953 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
954 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
956 UNICODE_STRING pathW, nameW, extW;
957 WCHAR bufferW[MAX_PATH];
958 DWORD ret, retW;
960 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
961 else pathW.Buffer = NULL;
962 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
963 else nameW.Buffer = NULL;
964 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
965 else extW.Buffer = NULL;
967 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
969 if (!retW)
970 ret = 0;
971 else if (retW > MAX_PATH)
973 SetLastError(ERROR_FILENAME_EXCED_RANGE);
974 ret = 0;
976 else
978 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
979 if (buflen >= ret)
981 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
982 ret--; /* length without 0 */
983 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
987 RtlFreeUnicodeString(&pathW);
988 RtlFreeUnicodeString(&nameW);
989 RtlFreeUnicodeString(&extW);
990 return ret;