Made HEAP_strdup* functions inline (temporary).
[wine/multimedia.git] / files / directory.c
blob4559c3ec4c9a127ee580e9cd2bfda6019bfcf7a7
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 "wine/winestring.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "wine/winuser16.h"
27 #include "winerror.h"
28 #include "drive.h"
29 #include "file.h"
30 #include "heap.h"
31 #include "msdos.h"
32 #include "options.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(dosfs);
36 DECLARE_DEBUG_CHANNEL(file);
38 static DOS_FULL_NAME DIR_Windows;
39 static DOS_FULL_NAME DIR_System;
42 /***********************************************************************
43 * DIR_GetPath
45 * Get a path name from the wine.ini file and make sure it is valid.
47 static int DIR_GetPath( const char *keyname, const char *defval,
48 DOS_FULL_NAME *full_name )
50 char path[MAX_PATHNAME_LEN];
51 BY_HANDLE_FILE_INFORMATION info;
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 ) ||
56 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
58 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
59 return 0;
61 return 1;
65 /***********************************************************************
66 * DIR_Init
68 int DIR_Init(void)
70 char path[MAX_PATHNAME_LEN];
71 DOS_FULL_NAME tmp_dir, profile_dir;
72 int drive;
73 const char *cwd;
75 if (!getcwd( path, MAX_PATHNAME_LEN ))
77 perror( "Could not get current directory" );
78 return 0;
80 cwd = path;
81 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
83 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
84 "for current working directory %s; "
85 "starting in windows directory.\n", cwd );
87 else
89 DRIVE_SetCurrentDrive( drive );
90 DRIVE_Chdir( drive, cwd );
93 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
94 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
95 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
97 PROFILE_UsageWineIni();
98 return 0;
100 if (-1 == access( tmp_dir.long_name, W_OK ))
102 if (errno==EACCES)
104 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
105 PROFILE_UsageWineIni();
107 else
108 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
109 strerror(errno));
112 if (drive == -1)
114 drive = DIR_Windows.drive;
115 DRIVE_SetCurrentDrive( drive );
116 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
119 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
120 path, sizeof(path) );
121 if (strchr(path, '/'))
123 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
124 PROFILE_UsageWineIni();
125 ExitProcess(1);
128 /* Set the environment variables */
130 SetEnvironmentVariableA( "PATH", path );
131 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
132 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
133 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
135 /* set COMSPEC only if it doesn't exist already */
136 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
137 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
139 TRACE("WindowsDir = %s (%s)\n",
140 DIR_Windows.short_name, DIR_Windows.long_name );
141 TRACE("SystemDir = %s (%s)\n",
142 DIR_System.short_name, DIR_System.long_name );
143 TRACE("TempDir = %s (%s)\n",
144 tmp_dir.short_name, tmp_dir.long_name );
145 TRACE("Path = %s\n", path );
146 TRACE("Cwd = %c:\\%s\n",
147 'A' + drive, DRIVE_GetDosCwd( drive ) );
149 if (DIR_GetPath( "profile", "", &profile_dir ))
151 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
152 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
155 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
156 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
158 return 1;
162 /***********************************************************************
163 * GetTempPathA (KERNEL32.292)
165 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
167 UINT ret;
168 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
169 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
170 if (!(ret = GetCurrentDirectoryA( count, path )))
171 return 0;
172 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
174 path[ret++] = '\\';
175 path[ret] = '\0';
177 return ret;
181 /***********************************************************************
182 * GetTempPathW (KERNEL32.293)
184 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
186 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
187 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
188 UINT ret;
189 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
190 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
191 if (!(ret = GetCurrentDirectoryW( count, path )))
192 return 0;
193 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
195 path[ret++] = '\\';
196 path[ret] = '\0';
198 return ret;
202 /***********************************************************************
203 * DIR_GetWindowsUnixDir
205 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
207 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
208 return strlen( DIR_Windows.long_name );
212 /***********************************************************************
213 * DIR_GetSystemUnixDir
215 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
217 if (path) lstrcpynA( path, DIR_System.long_name, count );
218 return strlen( DIR_System.long_name );
222 /***********************************************************************
223 * GetTempDrive (KERNEL.92)
224 * A closer look at krnl386.exe shows what the SDK doesn't mention:
226 * returns:
227 * AL: driveletter
228 * AH: ':' - yes, some kernel code even does stosw with
229 * the returned AX.
230 * DX: 1 for success
232 UINT WINAPI GetTempDrive( BYTE ignored )
234 char *buffer;
235 BYTE ret;
236 UINT len = GetTempPathA( 0, NULL );
238 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
239 ret = DRIVE_GetCurrentDrive() + 'A';
240 else
242 /* FIXME: apparently Windows does something with the ignored byte */
243 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
244 ret = toupper(buffer[0]);
245 HeapFree( GetProcessHeap(), 0, buffer );
247 return MAKELONG( ret | (':' << 8), 1 );
251 /***********************************************************************
252 * GetWindowsDirectory16 (KERNEL.134)
254 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
256 return (UINT16)GetWindowsDirectoryA( path, count );
260 /***********************************************************************
261 * GetWindowsDirectoryA (KERNEL32.311)
263 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
265 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
266 return strlen( DIR_Windows.short_name );
270 /***********************************************************************
271 * GetWindowsDirectoryW (KERNEL32.312)
273 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
275 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
276 return strlen( DIR_Windows.short_name );
280 /***********************************************************************
281 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
283 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
285 return GetWindowsDirectoryA( path, count );
289 /***********************************************************************
290 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
292 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
294 return GetWindowsDirectoryW( path, count );
298 /***********************************************************************
299 * GetSystemDirectory16 (KERNEL.135)
301 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
303 return (UINT16)GetSystemDirectoryA( path, count );
307 /***********************************************************************
308 * GetSystemDirectoryA (KERNEL32.282)
310 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
312 if (path) lstrcpynA( path, DIR_System.short_name, count );
313 return strlen( DIR_System.short_name );
317 /***********************************************************************
318 * GetSystemDirectoryW (KERNEL32.283)
320 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
322 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
323 return strlen( DIR_System.short_name );
327 /***********************************************************************
328 * CreateDirectory16 (KERNEL.144)
330 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
332 TRACE_(file)("(%s,%p)\n", path, dummy );
333 return (BOOL16)CreateDirectoryA( path, NULL );
337 /***********************************************************************
338 * CreateDirectoryA (KERNEL32.39)
339 * RETURNS:
340 * TRUE : success
341 * FALSE : failure
342 * ERROR_DISK_FULL: on full disk
343 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
344 * ERROR_ACCESS_DENIED: on permission problems
345 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
347 BOOL WINAPI CreateDirectoryA( LPCSTR path,
348 LPSECURITY_ATTRIBUTES lpsecattribs )
350 DOS_FULL_NAME full_name;
352 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
353 if (DOSFS_GetDevice( path ))
355 TRACE_(file)("cannot use device '%s'!\n",path);
356 SetLastError( ERROR_ACCESS_DENIED );
357 return FALSE;
359 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
360 if (mkdir( full_name.long_name, 0777 ) == -1) {
361 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
362 /* the FILE_SetDosError() generated error codes don't match the
363 * CreateDirectory ones for some errnos */
364 switch (errno) {
365 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
366 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
367 default: FILE_SetDosError();break;
369 return FALSE;
371 return TRUE;
375 /***********************************************************************
376 * CreateDirectoryW (KERNEL32.42)
378 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
379 LPSECURITY_ATTRIBUTES lpsecattribs )
381 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
382 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
383 HeapFree( GetProcessHeap(), 0, xpath );
384 return ret;
388 /***********************************************************************
389 * CreateDirectoryExA (KERNEL32.40)
391 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
392 LPSECURITY_ATTRIBUTES lpsecattribs)
394 return CreateDirectoryA(path,lpsecattribs);
398 /***********************************************************************
399 * CreateDirectoryExW (KERNEL32.41)
401 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
402 LPSECURITY_ATTRIBUTES lpsecattribs)
404 return CreateDirectoryW(path,lpsecattribs);
408 /***********************************************************************
409 * RemoveDirectory16 (KERNEL)
411 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
413 return (BOOL16)RemoveDirectoryA( path );
417 /***********************************************************************
418 * RemoveDirectoryA (KERNEL32.437)
420 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
422 DOS_FULL_NAME full_name;
424 TRACE_(file)("'%s'\n", path );
426 if (DOSFS_GetDevice( path ))
428 TRACE_(file)("cannot remove device '%s'!\n", path);
429 SetLastError( ERROR_FILE_NOT_FOUND );
430 return FALSE;
432 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
433 if (rmdir( full_name.long_name ) == -1)
435 FILE_SetDosError();
436 return FALSE;
438 return TRUE;
442 /***********************************************************************
443 * RemoveDirectoryW (KERNEL32.438)
445 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
447 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
448 BOOL ret = RemoveDirectoryA( xpath );
449 HeapFree( GetProcessHeap(), 0, xpath );
450 return ret;
454 /***********************************************************************
455 * DIR_TryPath
457 * Helper function for DIR_SearchPath.
459 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
460 DOS_FULL_NAME *full_name )
462 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
463 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
465 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
466 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
468 SetLastError( ERROR_PATH_NOT_FOUND );
469 return FALSE;
471 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
472 sizeof(full_name->long_name) - (p_l - full_name->long_name),
473 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
474 return FALSE;
475 strcpy( full_name->long_name, dir->long_name );
476 p_l[-1] = '/';
477 strcpy( full_name->short_name, dir->short_name );
478 p_s[-1] = '\\';
479 return TRUE;
483 /***********************************************************************
484 * DIR_TryEnvironmentPath
486 * Helper function for DIR_SearchPath.
487 * Search in the specified path, or in $PATH if NULL.
489 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
491 LPSTR path, next, buffer;
492 BOOL ret = FALSE;
493 INT len = strlen(name);
494 DWORD size;
496 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
497 if (!size) return FALSE;
498 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
499 if (envpath) strcpy( path, envpath );
500 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
501 next = path;
502 while (!ret && next)
504 LPSTR cur = next;
505 while (*cur == ';') cur++;
506 if (!*cur) break;
507 next = strchr( cur, ';' );
508 if (next) *next++ = '\0';
509 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
510 goto done;
511 strcpy( buffer, cur );
512 strcat( buffer, "\\" );
513 strcat( buffer, name );
514 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
515 HeapFree( GetProcessHeap(), 0, buffer );
518 done:
519 HeapFree( GetProcessHeap(), 0, path );
520 return ret;
524 /***********************************************************************
525 * DIR_TryModulePath
527 * Helper function for DIR_SearchPath.
529 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
531 /* FIXME: for now, GetModuleFileNameA can't return more */
532 /* than OFS_MAXPATHNAME. This may change with Win32. */
534 char buffer[OFS_MAXPATHNAME];
535 LPSTR p;
537 if (!win32)
539 if (!GetCurrentTask()) return FALSE;
540 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
541 buffer[0]='\0';
542 } else {
543 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
544 buffer[0]='\0';
546 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
547 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
548 strcpy( p, name );
549 return DOSFS_GetFullName( buffer, TRUE, full_name );
553 /***********************************************************************
554 * DIR_SearchPath
556 * Implementation of SearchPathA. 'win32' specifies whether the search
557 * order is Win16 (module path last) or Win32 (module path first).
559 * FIXME: should return long path names.
561 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
562 DOS_FULL_NAME *full_name, BOOL win32 )
564 LPCSTR p;
565 LPSTR tmp = NULL;
566 BOOL ret = TRUE;
568 /* First check the supplied parameters */
570 p = strrchr( name, '.' );
571 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
572 ext = NULL; /* Ignore the specified extension */
573 if ((*name && (name[1] == ':')) ||
574 strchr( name, '/' ) || strchr( name, '\\' ))
575 path = NULL; /* Ignore path if name already contains a path */
576 if (path && !*path) path = NULL; /* Ignore empty path */
578 /* Allocate a buffer for the file name and extension */
580 if (ext)
582 DWORD len = strlen(name) + strlen(ext);
583 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
585 SetLastError( ERROR_OUTOFMEMORY );
586 return 0;
588 strcpy( tmp, name );
589 strcat( tmp, ext );
590 name = tmp;
593 /* If the name contains an explicit path, everything's easy */
595 if ((*name && (name[1] == ':')) || strchr( name, '/' ) || strchr( name, '\\' ))
597 ret = DOSFS_GetFullName( name, TRUE, full_name );
598 goto done;
601 /* Search in the specified path */
603 if (path)
605 ret = DIR_TryEnvironmentPath( name, full_name, path );
606 goto done;
609 /* Try the path of the current executable (for Win32 search order) */
611 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
613 /* Try the current directory */
615 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
617 /* Try the Windows system directory */
619 if (DIR_TryPath( &DIR_System, name, full_name ))
620 goto done;
622 /* Try the Windows directory */
624 if (DIR_TryPath( &DIR_Windows, name, full_name ))
625 goto done;
627 /* Try the path of the current executable (for Win16 search order) */
629 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
631 /* Try all directories in path */
633 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
635 done:
636 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
637 return ret;
641 /***********************************************************************
642 * SearchPathA [KERNEL32.447]
644 * Searches for a specified file in the search path.
646 * PARAMS
647 * path [I] Path to search
648 * name [I] Filename to search for.
649 * ext [I] File extension to append to file name. The first
650 * character must be a period. This parameter is
651 * specified only if the filename given does not
652 * contain an extension.
653 * buflen [I] size of buffer, in characters
654 * buffer [O] buffer for found filename
655 * lastpart [O] address of pointer to last used character in
656 * buffer (the final '\')
658 * RETURNS
659 * Success: length of string copied into buffer, not including
660 * terminating null character. If the filename found is
661 * longer than the length of the buffer, the length of the
662 * filename is returned.
663 * Failure: Zero
665 * NOTES
666 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
667 * (tested on NT 4.0)
669 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
670 LPSTR buffer, LPSTR *lastpart )
672 LPSTR p, res;
673 DOS_FULL_NAME full_name;
675 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
677 SetLastError(ERROR_FILE_NOT_FOUND);
678 return 0;
680 lstrcpynA( buffer, full_name.short_name, buflen );
681 res = full_name.long_name +
682 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
683 while (*res == '/') res++;
684 if (buflen)
686 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
687 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
688 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
690 TRACE("Returning %d\n", strlen(res) + 3 );
691 return strlen(res) + 3;
695 /***********************************************************************
696 * SearchPathW (KERNEL32.448)
698 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
699 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
701 LPWSTR p;
702 LPSTR res;
703 DOS_FULL_NAME full_name;
705 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
706 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
707 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
708 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
709 HeapFree( GetProcessHeap(), 0, extA );
710 HeapFree( GetProcessHeap(), 0, nameA );
711 HeapFree( GetProcessHeap(), 0, pathA );
712 if (!ret) return 0;
714 lstrcpynAtoW( buffer, full_name.short_name, buflen );
715 res = full_name.long_name +
716 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
717 while (*res == '/') res++;
718 if (buflen)
720 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
721 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
722 if (lastpart)
724 for (p = *lastpart = buffer; *p; p++)
725 if (*p == '\\') *lastpart = p + 1;
728 return strlen(res) + 3;