Fixed debug formatting.
[wine/multimedia.git] / files / directory.c
blob8ef48c71592d2042baae6e79085b63fc7d79652b
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 "process.h"
29 #include "drive.h"
30 #include "file.h"
31 #include "heap.h"
32 #include "msdos.h"
33 #include "options.h"
34 #include "debugtools.h"
36 DEFAULT_DEBUG_CHANNEL(dosfs);
37 DECLARE_DEBUG_CHANNEL(file);
39 static DOS_FULL_NAME DIR_Windows;
40 static DOS_FULL_NAME DIR_System;
43 /***********************************************************************
44 * DIR_GetPath
46 * Get a path name from the wine.ini file and make sure it is valid.
48 static int DIR_GetPath( const char *keyname, const char *defval,
49 DOS_FULL_NAME *full_name )
51 char path[MAX_PATHNAME_LEN];
52 BY_HANDLE_FILE_INFORMATION info;
54 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
55 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
56 !FILE_Stat( full_name->long_name, &info ) ||
57 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
59 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
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.conf [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 )) ||
95 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
96 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
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 (as specified in your configuration file) is NOT writeable.\n");
106 PROFILE_UsageWineIni();
108 else
109 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
110 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("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
125 PROFILE_UsageWineIni();
126 ExitProcess(1);
129 /* Set the environment variables */
131 SetEnvironmentVariableA( "PATH", path );
132 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
133 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
134 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
135 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
137 TRACE("WindowsDir = %s (%s)\n",
138 DIR_Windows.short_name, DIR_Windows.long_name );
139 TRACE("SystemDir = %s (%s)\n",
140 DIR_System.short_name, DIR_System.long_name );
141 TRACE("TempDir = %s (%s)\n",
142 tmp_dir.short_name, tmp_dir.long_name );
143 TRACE("Path = %s\n", path );
144 TRACE("Cwd = %c:\\%s\n",
145 'A' + drive, DRIVE_GetDosCwd( drive ) );
147 if (DIR_GetPath( "profile", "", &profile_dir ))
149 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
150 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
153 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
154 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
156 return 1;
160 /***********************************************************************
161 * GetTempPath32A (KERNEL32.292)
163 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
165 UINT ret;
166 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
167 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
168 if (!(ret = GetCurrentDirectoryA( count, path )))
169 return 0;
170 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
172 path[ret++] = '\\';
173 path[ret] = '\0';
175 return ret;
179 /***********************************************************************
180 * GetTempPath32W (KERNEL32.293)
182 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
184 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
185 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
186 UINT ret;
187 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
188 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
189 if (!(ret = GetCurrentDirectoryW( count, path )))
190 return 0;
191 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
193 path[ret++] = '\\';
194 path[ret] = '\0';
196 return ret;
200 /***********************************************************************
201 * DIR_GetWindowsUnixDir
203 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
205 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
206 return strlen( DIR_Windows.long_name );
210 /***********************************************************************
211 * DIR_GetSystemUnixDir
213 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
215 if (path) lstrcpynA( path, DIR_System.long_name, count );
216 return strlen( DIR_System.long_name );
220 /***********************************************************************
221 * GetTempDrive (KERNEL.92)
223 BYTE WINAPI GetTempDrive( BYTE ignored )
225 char *buffer;
226 BYTE ret;
227 UINT len = GetTempPathA( 0, NULL );
229 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
230 return DRIVE_GetCurrentDrive() + 'A';
232 /* FIXME: apparently Windows does something with the ignored byte */
233 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
234 ret = buffer[0];
235 HeapFree( GetProcessHeap(), 0, buffer );
236 return toupper(ret);
240 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
242 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
244 * returns:
245 * AL: driveletter
246 * AH: ':' - yes, some kernel code even does stosw with
247 * the returned AX.
248 * DX: 1 for success
250 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
254 /***********************************************************************
255 * GetWindowsDirectory16 (KERNEL.134)
257 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
259 return (UINT16)GetWindowsDirectoryA( path, count );
263 /***********************************************************************
264 * GetWindowsDirectory32A (KERNEL32.311)
266 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
268 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
269 return strlen( DIR_Windows.short_name );
273 /***********************************************************************
274 * GetWindowsDirectory32W (KERNEL32.312)
276 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
278 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
279 return strlen( DIR_Windows.short_name );
283 /***********************************************************************
284 * GetSystemDirectory16 (KERNEL.135)
286 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
288 return (UINT16)GetSystemDirectoryA( path, count );
292 /***********************************************************************
293 * GetSystemDirectory32A (KERNEL32.282)
295 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
297 if (path) lstrcpynA( path, DIR_System.short_name, count );
298 return strlen( DIR_System.short_name );
302 /***********************************************************************
303 * GetSystemDirectory32W (KERNEL32.283)
305 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
307 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
308 return strlen( DIR_System.short_name );
312 /***********************************************************************
313 * CreateDirectory16 (KERNEL.144)
315 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
317 TRACE_(file)("(%s,%p)\n", path, dummy );
318 return (BOOL16)CreateDirectoryA( path, NULL );
322 /***********************************************************************
323 * CreateDirectory32A (KERNEL32.39)
324 * RETURNS:
325 * TRUE : success
326 * FALSE : failure
327 * ERROR_DISK_FULL: on full disk
328 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
329 * ERROR_ACCESS_DENIED: on permission problems
330 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
332 BOOL WINAPI CreateDirectoryA( LPCSTR path,
333 LPSECURITY_ATTRIBUTES lpsecattribs )
335 DOS_FULL_NAME full_name;
337 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
338 if (DOSFS_GetDevice( path ))
340 TRACE_(file)("cannot use device '%s'!\n",path);
341 SetLastError( ERROR_ACCESS_DENIED );
342 return FALSE;
344 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
345 if (mkdir( full_name.long_name, 0777 ) == -1) {
346 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
347 /* the FILE_SetDosError() generated error codes don't match the
348 * CreateDirectory ones for some errnos */
349 switch (errno) {
350 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
351 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
352 default: FILE_SetDosError();break;
354 return FALSE;
356 return TRUE;
360 /***********************************************************************
361 * CreateDirectory32W (KERNEL32.42)
363 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
364 LPSECURITY_ATTRIBUTES lpsecattribs )
366 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
367 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
368 HeapFree( GetProcessHeap(), 0, xpath );
369 return ret;
373 /***********************************************************************
374 * CreateDirectoryEx32A (KERNEL32.40)
376 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
377 LPSECURITY_ATTRIBUTES lpsecattribs)
379 return CreateDirectoryA(path,lpsecattribs);
383 /***********************************************************************
384 * CreateDirectoryEx32W (KERNEL32.41)
386 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
387 LPSECURITY_ATTRIBUTES lpsecattribs)
389 return CreateDirectoryW(path,lpsecattribs);
393 /***********************************************************************
394 * RemoveDirectory16 (KERNEL)
396 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
398 return (BOOL16)RemoveDirectoryA( path );
402 /***********************************************************************
403 * RemoveDirectory32A (KERNEL32.437)
405 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
407 DOS_FULL_NAME full_name;
409 TRACE_(file)("'%s'\n", path );
411 if (DOSFS_GetDevice( path ))
413 TRACE_(file)("cannot remove device '%s'!\n", path);
414 SetLastError( ERROR_FILE_NOT_FOUND );
415 return FALSE;
417 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
418 if (rmdir( full_name.long_name ) == -1)
420 FILE_SetDosError();
421 return FALSE;
423 return TRUE;
427 /***********************************************************************
428 * RemoveDirectory32W (KERNEL32.438)
430 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
432 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
433 BOOL ret = RemoveDirectoryA( xpath );
434 HeapFree( GetProcessHeap(), 0, xpath );
435 return ret;
439 /***********************************************************************
440 * DIR_TryPath
442 * Helper function for DIR_SearchPath.
444 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
445 DOS_FULL_NAME *full_name )
447 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
448 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
450 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
451 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
453 SetLastError( ERROR_PATH_NOT_FOUND );
454 return FALSE;
456 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
457 sizeof(full_name->long_name) - (p_l - full_name->long_name),
458 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
459 return FALSE;
460 strcpy( full_name->long_name, dir->long_name );
461 p_l[-1] = '/';
462 strcpy( full_name->short_name, dir->short_name );
463 p_s[-1] = '\\';
464 return TRUE;
468 /***********************************************************************
469 * DIR_TryEnvironmentPath
471 * Helper function for DIR_SearchPath.
473 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
475 LPSTR path, next, buffer;
476 BOOL ret = FALSE;
477 INT len = strlen(name);
478 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
480 if (!size) return FALSE;
481 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
482 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
483 next = path;
484 while (!ret && next)
486 LPSTR cur = next;
487 while (*cur == ';') cur++;
488 if (!*cur) break;
489 next = strchr( cur, ';' );
490 if (next) *next++ = '\0';
491 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
492 goto done;
493 strcpy( buffer, cur );
494 strcat( buffer, "\\" );
495 strcat( buffer, name );
496 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
497 HeapFree( GetProcessHeap(), 0, buffer );
500 done:
501 HeapFree( GetProcessHeap(), 0, path );
502 return ret;
506 /***********************************************************************
507 * DIR_TryModulePath
509 * Helper function for DIR_SearchPath.
511 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
513 PDB *pdb = PROCESS_Current();
515 /* FIXME: for now, GetModuleFileName32A can't return more */
516 /* than OFS_MAXPATHNAME. This may change with Win32. */
518 char buffer[OFS_MAXPATHNAME];
519 LPSTR p;
521 if (pdb->flags & PDB32_WIN16_PROC) {
522 if (!GetCurrentTask()) return FALSE;
523 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
524 buffer[0]='\0';
525 } else {
526 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
527 buffer[0]='\0';
529 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
530 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
531 strcpy( p, name );
532 return DOSFS_GetFullName( buffer, TRUE, full_name );
536 /***********************************************************************
537 * DIR_SearchPath
539 * Implementation of SearchPath32A. 'win32' specifies whether the search
540 * order is Win16 (module path last) or Win32 (module path first).
542 * FIXME: should return long path names.
544 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
545 DOS_FULL_NAME *full_name, BOOL win32 )
547 DWORD len;
548 LPCSTR p;
549 LPSTR tmp = NULL;
550 BOOL ret = TRUE;
552 /* First check the supplied parameters */
554 p = strrchr( name, '.' );
555 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
556 ext = NULL; /* Ignore the specified extension */
557 if ((*name && (name[1] == ':')) ||
558 strchr( name, '/' ) || strchr( name, '\\' ))
559 path = NULL; /* Ignore path if name already contains a path */
560 if (path && !*path) path = NULL; /* Ignore empty path */
562 len = strlen(name);
563 if (ext) len += strlen(ext);
564 if (path) len += strlen(path) + 1;
566 /* Allocate a buffer for the file name and extension */
568 if (path || ext)
570 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
572 SetLastError( ERROR_OUTOFMEMORY );
573 return 0;
575 if (path)
577 strcpy( tmp, path );
578 strcat( tmp, "\\" );
579 strcat( tmp, name );
581 else strcpy( tmp, name );
582 if (ext) strcat( tmp, ext );
583 name = tmp;
586 /* If we have an explicit path, everything's easy */
588 if (path || (*name && (name[1] == ':')) ||
589 strchr( name, '/' ) || strchr( name, '\\' ))
591 ret = DOSFS_GetFullName( name, TRUE, full_name );
592 goto done;
595 /* Try the path of the current executable (for Win32 search order) */
597 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
599 /* Try the current directory */
601 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
603 /* Try the Windows system directory */
605 if (DIR_TryPath( &DIR_System, name, full_name ))
606 goto done;
608 /* Try the Windows directory */
610 if (DIR_TryPath( &DIR_Windows, name, full_name ))
611 goto done;
613 /* Try the path of the current executable (for Win16 search order) */
615 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
617 /* Try all directories in path */
619 ret = DIR_TryEnvironmentPath( name, full_name );
621 done:
622 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
623 return ret;
627 /***********************************************************************
628 * SearchPath32A [KERNEL32.447]
630 * Searches for a specified file in the search path.
632 * PARAMS
633 * path [I] Path to search
634 * name [I] Filename to search for.
635 * ext [I] File extension to append to file name. The first
636 * character must be a period. This parameter is
637 * specified only if the filename given does not
638 * contain an extension.
639 * buflen [I] size of buffer, in characters
640 * buffer [O] buffer for found filename
641 * lastpart [O] address of pointer to last used character in
642 * buffer (the final '\')
644 * RETURNS
645 * Success: length of string copied into buffer, not including
646 * terminating null character. If the filename found is
647 * longer than the length of the buffer, the length of the
648 * filename is returned.
649 * Failure: Zero
651 * NOTES
652 * Should call SetLastError(but currently doesn't).
654 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
655 LPSTR buffer, LPSTR *lastpart )
657 LPSTR p, res;
658 DOS_FULL_NAME full_name;
660 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
661 lstrcpynA( buffer, full_name.short_name, buflen );
662 res = full_name.long_name +
663 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
664 while (*res == '/') res++;
665 if (buflen)
667 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
668 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
669 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
671 TRACE("Returning %d\n", strlen(res) + 3 );
672 return strlen(res) + 3;
676 /***********************************************************************
677 * SearchPath32W (KERNEL32.448)
679 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
680 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
682 LPWSTR p;
683 LPSTR res;
684 DOS_FULL_NAME full_name;
686 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
687 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
688 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
689 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
690 HeapFree( GetProcessHeap(), 0, extA );
691 HeapFree( GetProcessHeap(), 0, nameA );
692 HeapFree( GetProcessHeap(), 0, pathA );
693 if (!ret) return 0;
695 lstrcpynAtoW( buffer, full_name.short_name, buflen );
696 res = full_name.long_name +
697 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
698 while (*res == '/') res++;
699 if (buflen)
701 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
702 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
703 if (lastpart)
705 for (p = *lastpart = buffer; *p; p++)
706 if (*p == '\\') *lastpart = p + 1;
709 return strlen(res) + 3;