Move __stdcall/__cdecl to the right place.
[wine/gsoc_dplay.git] / files / directory.c
blobd9e1d1d0b2fcd5bac34599eecb1c50c7f99f58d5
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #ifdef HAVE_SYS_ERRNO_H
18 #include <sys/errno.h>
19 #endif
21 #include "winbase.h"
22 #include "wine/winbase16.h"
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winuser16.h"
26 #include "winerror.h"
27 #include "drive.h"
28 #include "file.h"
29 #include "heap.h"
30 #include "msdos.h"
31 #include "options.h"
32 #include "debugtools.h"
34 DEFAULT_DEBUG_CHANNEL(dosfs);
35 DECLARE_DEBUG_CHANNEL(file);
37 static DOS_FULL_NAME DIR_Windows;
38 static DOS_FULL_NAME DIR_System;
41 /***********************************************************************
42 * DIR_GetPath
44 * Get a path name from the wine.ini file and make sure it is valid.
46 static int DIR_GetPath( const char *keyname, const char *defval,
47 DOS_FULL_NAME *full_name, BOOL warn )
49 char path[MAX_PATHNAME_LEN];
50 BY_HANDLE_FILE_INFORMATION info;
51 const char *mess = "does not exist";
53 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
54 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
55 (!FILE_Stat( full_name->long_name, &info ) && (mess=strerror(errno)))||
56 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")))
58 if (warn)
59 MESSAGE("Invalid path '%s' for %s directory: %s\n", path, keyname, mess);
60 return 0;
62 return 1;
66 /***********************************************************************
67 * DIR_Init
69 int DIR_Init(void)
71 char path[MAX_PATHNAME_LEN];
72 DOS_FULL_NAME tmp_dir, profile_dir;
73 int drive;
74 const char *cwd;
76 if (!getcwd( path, MAX_PATHNAME_LEN ))
78 perror( "Could not get current directory" );
79 return 0;
81 cwd = path;
82 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
84 MESSAGE("Warning: could not find wine config [Drive x] entry "
85 "for current working directory %s; "
86 "starting in windows directory.\n", cwd );
88 else
90 DRIVE_SetCurrentDrive( drive );
91 DRIVE_Chdir( drive, cwd );
94 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows, TRUE )) ||
95 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System, TRUE )) ||
96 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir, TRUE )))
98 PROFILE_UsageWineIni();
99 return 0;
101 if (-1 == access( tmp_dir.long_name, W_OK ))
103 if (errno==EACCES)
105 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
106 PROFILE_UsageWineIni();
108 else
109 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
110 tmp_dir.long_name, strerror(errno));
113 if (drive == -1)
115 drive = DIR_Windows.drive;
116 DRIVE_SetCurrentDrive( drive );
117 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
120 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
121 path, sizeof(path) );
122 if (strchr(path, '/'))
124 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
125 PROFILE_UsageWineIni();
126 ExitProcess(1);
129 /* Set the environment variables */
131 SetEnvironmentVariableA( "PATH", path );
132 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
133 SetEnvironmentVariableA( "TMP", tmp_dir.short_name );
134 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
135 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
137 /* set COMSPEC only if it doesn't exist already */
138 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
139 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
141 TRACE("WindowsDir = %s (%s)\n",
142 DIR_Windows.short_name, DIR_Windows.long_name );
143 TRACE("SystemDir = %s (%s)\n",
144 DIR_System.short_name, DIR_System.long_name );
145 TRACE("TempDir = %s (%s)\n",
146 tmp_dir.short_name, tmp_dir.long_name );
147 TRACE("Path = %s\n", path );
148 TRACE("Cwd = %c:\\%s\n",
149 'A' + drive, DRIVE_GetDosCwd( drive ) );
151 if (DIR_GetPath( "profile", "", &profile_dir, FALSE ))
153 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
154 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
157 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
158 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
160 return 1;
164 /***********************************************************************
165 * GetTempPathA (KERNEL32.@)
167 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
169 UINT ret;
170 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
171 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
172 if (!(ret = GetCurrentDirectoryA( count, path )))
173 return 0;
174 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
176 path[ret++] = '\\';
177 path[ret] = '\0';
179 return ret;
183 /***********************************************************************
184 * GetTempPathW (KERNEL32.@)
186 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
188 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
189 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
190 UINT ret;
191 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
192 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
193 if (!(ret = GetCurrentDirectoryW( count, path )))
194 return 0;
195 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
197 path[ret++] = '\\';
198 path[ret] = '\0';
200 return ret;
204 /***********************************************************************
205 * DIR_GetWindowsUnixDir
207 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
209 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
210 return strlen( DIR_Windows.long_name );
214 /***********************************************************************
215 * DIR_GetSystemUnixDir
217 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
219 if (path) lstrcpynA( path, DIR_System.long_name, count );
220 return strlen( DIR_System.long_name );
224 /***********************************************************************
225 * GetTempDrive (KERNEL.92)
226 * A closer look at krnl386.exe shows what the SDK doesn't mention:
228 * returns:
229 * AL: driveletter
230 * AH: ':' - yes, some kernel code even does stosw with
231 * the returned AX.
232 * DX: 1 for success
234 UINT WINAPI GetTempDrive( BYTE ignored )
236 char *buffer;
237 BYTE ret;
238 UINT len = GetTempPathA( 0, NULL );
240 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
241 ret = DRIVE_GetCurrentDrive() + 'A';
242 else
244 /* FIXME: apparently Windows does something with the ignored byte */
245 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
246 ret = toupper(buffer[0]);
247 HeapFree( GetProcessHeap(), 0, buffer );
249 return MAKELONG( ret | (':' << 8), 1 );
253 /***********************************************************************
254 * GetWindowsDirectory (KERNEL.134)
256 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
258 return (UINT16)GetWindowsDirectoryA( path, count );
262 /***********************************************************************
263 * GetWindowsDirectoryA (KERNEL32.@)
265 * See comment for GetWindowsDirectoryW.
267 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
269 UINT len = strlen( DIR_Windows.short_name ) + 1;
270 if (path && count >= len)
272 strcpy( path, DIR_Windows.short_name );
273 len--;
275 return len;
279 /***********************************************************************
280 * GetWindowsDirectoryW (KERNEL32.@)
282 * Return value:
283 * If buffer is large enough to hold full path and terminating '\0' character
284 * function copies path to buffer and returns length of the path without '\0'.
285 * Otherwise function returns required size including '\0' character and
286 * does not touch the buffer.
288 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
290 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0 );
291 if (path && count >= len)
293 MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, path, count );
294 len--;
296 return len;
300 /***********************************************************************
301 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
303 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
305 return GetWindowsDirectoryA( path, count );
309 /***********************************************************************
310 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
312 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
314 return GetWindowsDirectoryW( path, count );
318 /***********************************************************************
319 * GetSystemDirectory (KERNEL.135)
321 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
323 return (UINT16)GetSystemDirectoryA( path, count );
327 /***********************************************************************
328 * GetSystemDirectoryA (KERNEL32.@)
330 * See comment for GetWindowsDirectoryW.
332 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
334 UINT len = strlen( DIR_System.short_name ) + 1;
335 if (path && count >= len)
337 strcpy( path, DIR_System.short_name );
338 len--;
340 return len;
344 /***********************************************************************
345 * GetSystemDirectoryW (KERNEL32.@)
347 * See comment for GetWindowsDirectoryW.
349 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
351 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0 );
352 if (path && count >= len)
354 MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, path, count );
355 len--;
357 return len;
361 /***********************************************************************
362 * CreateDirectory (KERNEL.144)
364 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
366 TRACE_(file)("(%s,%p)\n", path, dummy );
367 return (BOOL16)CreateDirectoryA( path, NULL );
371 /***********************************************************************
372 * CreateDirectoryA (KERNEL32.@)
373 * RETURNS:
374 * TRUE : success
375 * FALSE : failure
376 * ERROR_DISK_FULL: on full disk
377 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
378 * ERROR_ACCESS_DENIED: on permission problems
379 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
381 BOOL WINAPI CreateDirectoryA( LPCSTR path,
382 LPSECURITY_ATTRIBUTES lpsecattribs )
384 DOS_FULL_NAME full_name;
386 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
387 if (DOSFS_GetDevice( path ))
389 TRACE_(file)("cannot use device '%s'!\n",path);
390 SetLastError( ERROR_ACCESS_DENIED );
391 return FALSE;
393 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
394 if (mkdir( full_name.long_name, 0777 ) == -1) {
395 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
396 /* the FILE_SetDosError() generated error codes don't match the
397 * CreateDirectory ones for some errnos */
398 switch (errno) {
399 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
400 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
401 default: FILE_SetDosError();break;
403 return FALSE;
405 return TRUE;
409 /***********************************************************************
410 * CreateDirectoryW (KERNEL32.@)
412 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
413 LPSECURITY_ATTRIBUTES lpsecattribs )
415 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
416 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
417 HeapFree( GetProcessHeap(), 0, xpath );
418 return ret;
422 /***********************************************************************
423 * CreateDirectoryExA (KERNEL32.@)
425 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
426 LPSECURITY_ATTRIBUTES lpsecattribs)
428 return CreateDirectoryA(path,lpsecattribs);
432 /***********************************************************************
433 * CreateDirectoryExW (KERNEL32.@)
435 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
436 LPSECURITY_ATTRIBUTES lpsecattribs)
438 return CreateDirectoryW(path,lpsecattribs);
442 /***********************************************************************
443 * RemoveDirectory (KERNEL.145)
445 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
447 return (BOOL16)RemoveDirectoryA( path );
451 /***********************************************************************
452 * RemoveDirectoryA (KERNEL32.@)
454 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
456 DOS_FULL_NAME full_name;
458 TRACE_(file)("'%s'\n", path );
460 if (DOSFS_GetDevice( path ))
462 TRACE_(file)("cannot remove device '%s'!\n", path);
463 SetLastError( ERROR_FILE_NOT_FOUND );
464 return FALSE;
466 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
467 if (rmdir( full_name.long_name ) == -1)
469 FILE_SetDosError();
470 return FALSE;
472 return TRUE;
476 /***********************************************************************
477 * RemoveDirectoryW (KERNEL32.@)
479 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
481 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
482 BOOL ret = RemoveDirectoryA( xpath );
483 HeapFree( GetProcessHeap(), 0, xpath );
484 return ret;
488 /***********************************************************************
489 * DIR_TryPath
491 * Helper function for DIR_SearchPath.
493 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
494 DOS_FULL_NAME *full_name )
496 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
497 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
499 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
500 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
502 SetLastError( ERROR_PATH_NOT_FOUND );
503 return FALSE;
505 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
506 sizeof(full_name->long_name) - (p_l - full_name->long_name),
507 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
508 return FALSE;
509 strcpy( full_name->long_name, dir->long_name );
510 p_l[-1] = '/';
511 strcpy( full_name->short_name, dir->short_name );
512 p_s[-1] = '\\';
513 return TRUE;
517 /***********************************************************************
518 * DIR_TryEnvironmentPath
520 * Helper function for DIR_SearchPath.
521 * Search in the specified path, or in $PATH if NULL.
523 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
525 LPSTR path, next, buffer;
526 BOOL ret = FALSE;
527 INT len = strlen(name);
528 DWORD size;
530 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
531 if (!size) return FALSE;
532 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
533 if (envpath) strcpy( path, envpath );
534 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
535 next = path;
536 while (!ret && next)
538 LPSTR cur = next;
539 while (*cur == ';') cur++;
540 if (!*cur) break;
541 next = strchr( cur, ';' );
542 if (next) *next++ = '\0';
543 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
544 goto done;
545 strcpy( buffer, cur );
546 strcat( buffer, "\\" );
547 strcat( buffer, name );
548 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
549 HeapFree( GetProcessHeap(), 0, buffer );
552 done:
553 HeapFree( GetProcessHeap(), 0, path );
554 return ret;
558 /***********************************************************************
559 * DIR_TryModulePath
561 * Helper function for DIR_SearchPath.
563 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
565 /* FIXME: for now, GetModuleFileNameA can't return more */
566 /* than OFS_MAXPATHNAME. This may change with Win32. */
568 char buffer[OFS_MAXPATHNAME];
569 LPSTR p;
571 if (!win32)
573 if (!GetCurrentTask()) return FALSE;
574 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
575 buffer[0]='\0';
576 } else {
577 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
578 buffer[0]='\0';
580 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
581 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
582 strcpy( p, name );
583 return DOSFS_GetFullName( buffer, TRUE, full_name );
587 /***********************************************************************
588 * DIR_SearchPath
590 * Implementation of SearchPathA. 'win32' specifies whether the search
591 * order is Win16 (module path last) or Win32 (module path first).
593 * FIXME: should return long path names.
595 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
596 DOS_FULL_NAME *full_name, BOOL win32 )
598 LPCSTR p;
599 LPSTR tmp = NULL;
600 BOOL ret = TRUE;
602 /* First check the supplied parameters */
604 p = strrchr( name, '.' );
605 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
606 ext = NULL; /* Ignore the specified extension */
607 if (FILE_contains_path (name))
608 path = NULL; /* Ignore path if name already contains a path */
609 if (path && !*path) path = NULL; /* Ignore empty path */
611 /* Allocate a buffer for the file name and extension */
613 if (ext)
615 DWORD len = strlen(name) + strlen(ext);
616 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
618 SetLastError( ERROR_OUTOFMEMORY );
619 return 0;
621 strcpy( tmp, name );
622 strcat( tmp, ext );
623 name = tmp;
626 /* If the name contains an explicit path, everything's easy */
628 if (FILE_contains_path(name))
630 ret = DOSFS_GetFullName( name, TRUE, full_name );
631 goto done;
634 /* Search in the specified path */
636 if (path)
638 ret = DIR_TryEnvironmentPath( name, full_name, path );
639 goto done;
642 /* Try the path of the current executable (for Win32 search order) */
644 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
646 /* Try the current directory */
648 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
650 /* Try the Windows system directory */
652 if (DIR_TryPath( &DIR_System, name, full_name ))
653 goto done;
655 /* Try the Windows directory */
657 if (DIR_TryPath( &DIR_Windows, name, full_name ))
658 goto done;
660 /* Try the path of the current executable (for Win16 search order) */
662 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
664 /* Try all directories in path */
666 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
668 done:
669 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
670 return ret;
674 /***********************************************************************
675 * SearchPathA [KERNEL32.@]
677 * Searches for a specified file in the search path.
679 * PARAMS
680 * path [I] Path to search
681 * name [I] Filename to search for.
682 * ext [I] File extension to append to file name. The first
683 * character must be a period. This parameter is
684 * specified only if the filename given does not
685 * contain an extension.
686 * buflen [I] size of buffer, in characters
687 * buffer [O] buffer for found filename
688 * lastpart [O] address of pointer to last used character in
689 * buffer (the final '\')
691 * RETURNS
692 * Success: length of string copied into buffer, not including
693 * terminating null character. If the filename found is
694 * longer than the length of the buffer, the length of the
695 * filename is returned.
696 * Failure: Zero
698 * NOTES
699 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
700 * (tested on NT 4.0)
702 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
703 LPSTR buffer, LPSTR *lastpart )
705 LPSTR p, res;
706 DOS_FULL_NAME full_name;
708 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
710 SetLastError(ERROR_FILE_NOT_FOUND);
711 return 0;
713 lstrcpynA( buffer, full_name.short_name, buflen );
714 res = full_name.long_name +
715 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
716 while (*res == '/') res++;
717 if (buflen)
719 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
720 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
721 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
723 TRACE("Returning %d\n", strlen(res) + 3 );
724 return strlen(res) + 3;
728 /***********************************************************************
729 * SearchPathW (KERNEL32.@)
731 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
732 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
734 LPWSTR p;
735 LPSTR res;
736 DOS_FULL_NAME full_name;
738 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
739 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
740 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
741 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
742 HeapFree( GetProcessHeap(), 0, extA );
743 HeapFree( GetProcessHeap(), 0, nameA );
744 HeapFree( GetProcessHeap(), 0, pathA );
745 if (!ret) return 0;
747 if (buflen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, buffer, buflen ))
748 buffer[buflen-1] = 0;
749 res = full_name.long_name +
750 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
751 while (*res == '/') res++;
752 if (buflen)
754 if (buflen > 3)
756 if (!MultiByteToWideChar( CP_ACP, 0, res, -1, buffer+3, buflen-3 ))
757 buffer[buflen-1] = 0;
759 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
760 if (lastpart)
762 for (p = *lastpart = buffer; *p; p++)
763 if (*p == '\\') *lastpart = p + 1;
766 return strlen(res) + 3;
770 /***********************************************************************
771 * search_alternate_path
774 * FIXME: should return long path names.?
776 static BOOL search_alternate_path(LPCSTR dll_path, LPCSTR name, LPCSTR ext,
777 DOS_FULL_NAME *full_name)
779 LPCSTR p;
780 LPSTR tmp = NULL;
781 BOOL ret = TRUE;
783 /* First check the supplied parameters */
785 p = strrchr( name, '.' );
786 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
787 ext = NULL; /* Ignore the specified extension */
789 /* Allocate a buffer for the file name and extension */
791 if (ext)
793 DWORD len = strlen(name) + strlen(ext);
794 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
796 SetLastError( ERROR_OUTOFMEMORY );
797 return 0;
799 strcpy( tmp, name );
800 strcat( tmp, ext );
801 name = tmp;
804 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
806 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
808 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
810 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
812 else
813 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
815 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
816 return ret;
820 /***********************************************************************
821 * DIR_SearchAlternatePath
823 * Searches for a specified file in the search path.
825 * PARAMS
826 * dll_path [I] Path to search
827 * name [I] Filename to search for.
828 * ext [I] File extension to append to file name. The first
829 * character must be a period. This parameter is
830 * specified only if the filename given does not
831 * contain an extension.
832 * buflen [I] size of buffer, in characters
833 * buffer [O] buffer for found filename
834 * lastpart [O] address of pointer to last used character in
835 * buffer (the final '\') (May be NULL)
837 * RETURNS
838 * Success: length of string copied into buffer, not including
839 * terminating null character. If the filename found is
840 * longer than the length of the buffer, the length of the
841 * filename is returned.
842 * Failure: Zero
844 * NOTES
845 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
847 DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
848 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
850 LPSTR p, res;
851 DOS_FULL_NAME full_name;
853 if (!search_alternate_path( dll_path, name, ext, &full_name))
855 SetLastError(ERROR_FILE_NOT_FOUND);
856 return 0;
858 lstrcpynA( buffer, full_name.short_name, buflen );
859 res = full_name.long_name +
860 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
861 while (*res == '/') res++;
862 if (buflen)
864 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
865 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
866 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
868 TRACE("Returning %d\n", strlen(res) + 3 );
869 return strlen(res) + 3;