Use a linked list instead of a DPA for the hook list.
[wine.git] / files / directory.c
blob220a8113cb4f7978ea885b3547bfd24c6c812044
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 <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #ifdef HAVE_SYS_ERRNO_H
17 #include <sys/errno.h>
18 #endif
20 #include "winbase.h"
21 #include "wine/winbase16.h"
22 #include "wine/winestring.h"
23 #include "wine/winuser16.h"
24 #include "winerror.h"
25 #include "process.h"
26 #include "drive.h"
27 #include "file.h"
28 #include "heap.h"
29 #include "msdos.h"
30 #include "options.h"
31 #include "debugtools.h"
33 DEFAULT_DEBUG_CHANNEL(dosfs)
34 DECLARE_DEBUG_CHANNEL(file)
36 static DOS_FULL_NAME DIR_Windows;
37 static DOS_FULL_NAME DIR_System;
40 /***********************************************************************
41 * DIR_GetPath
43 * Get a path name from the wine.ini file and make sure it is valid.
45 static int DIR_GetPath( const char *keyname, const char *defval,
46 DOS_FULL_NAME *full_name )
48 char path[MAX_PATHNAME_LEN];
49 BY_HANDLE_FILE_INFORMATION info;
51 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
52 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
53 !FILE_Stat( full_name->long_name, &info ) ||
54 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
56 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
57 return 0;
59 return 1;
63 /***********************************************************************
64 * DIR_Init
66 int DIR_Init(void)
68 char path[MAX_PATHNAME_LEN];
69 DOS_FULL_NAME tmp_dir;
70 int drive;
71 const char *cwd;
73 if (!getcwd( path, MAX_PATHNAME_LEN ))
75 perror( "Could not get current directory" );
76 return 0;
78 cwd = path;
79 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
81 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
82 "for current working directory %s; "
83 "starting in windows directory.\n", cwd );
85 else
87 DRIVE_SetCurrentDrive( drive );
88 DRIVE_Chdir( drive, cwd );
91 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
92 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
93 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
95 PROFILE_UsageWineIni();
96 return 0;
98 if (-1 == access( tmp_dir.long_name, W_OK ))
100 if (errno==EACCES)
102 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
103 PROFILE_UsageWineIni();
105 else
106 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
107 strerror(errno));
110 if (drive == -1)
112 drive = DIR_Windows.drive;
113 DRIVE_SetCurrentDrive( drive );
114 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
117 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
118 path, sizeof(path) );
119 if (strchr(path, '/'))
121 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
122 PROFILE_UsageWineIni();
123 ExitProcess(1);
126 /* Set the environment variables */
128 SetEnvironmentVariableA( "PATH", path );
129 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
130 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
131 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
132 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
134 TRACE("WindowsDir = %s (%s)\n",
135 DIR_Windows.short_name, DIR_Windows.long_name );
136 TRACE("SystemDir = %s (%s)\n",
137 DIR_System.short_name, DIR_System.long_name );
138 TRACE("TempDir = %s (%s)\n",
139 tmp_dir.short_name, tmp_dir.long_name );
140 TRACE("Path = %s\n", path );
141 TRACE("Cwd = %c:\\%s\n",
142 'A' + drive, DRIVE_GetDosCwd( drive ) );
144 return 1;
148 /***********************************************************************
149 * GetTempPath32A (KERNEL32.292)
151 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
153 UINT ret;
154 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
155 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
156 if (!(ret = GetCurrentDirectoryA( count, path )))
157 return 0;
158 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
160 path[ret++] = '\\';
161 path[ret] = '\0';
163 return ret;
167 /***********************************************************************
168 * GetTempPath32W (KERNEL32.293)
170 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
172 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
173 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
174 UINT ret;
175 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
176 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
177 if (!(ret = GetCurrentDirectoryW( count, path )))
178 return 0;
179 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
181 path[ret++] = '\\';
182 path[ret] = '\0';
184 return ret;
188 /***********************************************************************
189 * DIR_GetWindowsUnixDir
191 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
193 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
194 return strlen( DIR_Windows.long_name );
198 /***********************************************************************
199 * DIR_GetSystemUnixDir
201 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
203 if (path) lstrcpynA( path, DIR_System.long_name, count );
204 return strlen( DIR_System.long_name );
208 /***********************************************************************
209 * GetTempDrive (KERNEL.92)
211 BYTE WINAPI GetTempDrive( BYTE ignored )
213 char *buffer;
214 BYTE ret;
215 UINT len = GetTempPathA( 0, NULL );
217 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
218 return DRIVE_GetCurrentDrive() + 'A';
220 /* FIXME: apparently Windows does something with the ignored byte */
221 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
222 ret = buffer[0];
223 HeapFree( GetProcessHeap(), 0, buffer );
224 return toupper(ret);
228 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
230 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
232 * returns:
233 * AL: driveletter
234 * AH: ':' - yes, some kernel code even does stosw with
235 * the returned AX.
236 * DX: 1 for success
238 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
242 /***********************************************************************
243 * GetWindowsDirectory16 (KERNEL.134)
245 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
247 return (UINT16)GetWindowsDirectoryA( path, count );
251 /***********************************************************************
252 * GetWindowsDirectory32A (KERNEL32.311)
254 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
256 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
257 return strlen( DIR_Windows.short_name );
261 /***********************************************************************
262 * GetWindowsDirectory32W (KERNEL32.312)
264 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
266 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
267 return strlen( DIR_Windows.short_name );
271 /***********************************************************************
272 * GetSystemDirectory16 (KERNEL.135)
274 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
276 return (UINT16)GetSystemDirectoryA( path, count );
280 /***********************************************************************
281 * GetSystemDirectory32A (KERNEL32.282)
283 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
285 if (path) lstrcpynA( path, DIR_System.short_name, count );
286 return strlen( DIR_System.short_name );
290 /***********************************************************************
291 * GetSystemDirectory32W (KERNEL32.283)
293 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
295 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
296 return strlen( DIR_System.short_name );
300 /***********************************************************************
301 * CreateDirectory16 (KERNEL.144)
303 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
305 TRACE_(file)("(%s,%p)\n", path, dummy );
306 return (BOOL16)CreateDirectoryA( path, NULL );
310 /***********************************************************************
311 * CreateDirectory32A (KERNEL32.39)
312 * RETURNS:
313 * TRUE : success
314 * FALSE : failure
315 * ERROR_DISK_FULL: on full disk
316 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
317 * ERROR_ACCESS_DENIED: on permission problems
318 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
320 BOOL WINAPI CreateDirectoryA( LPCSTR path,
321 LPSECURITY_ATTRIBUTES lpsecattribs )
323 DOS_FULL_NAME full_name;
325 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
326 if (DOSFS_GetDevice( path ))
328 TRACE_(file)("cannot use device '%s'!\n",path);
329 SetLastError( ERROR_ACCESS_DENIED );
330 return FALSE;
332 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
333 if (mkdir( full_name.long_name, 0777 ) == -1) {
334 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
335 /* the FILE_SetDosError() generated error codes don't match the
336 * CreateDirectory ones for some errnos */
337 switch (errno) {
338 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
339 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
340 default: FILE_SetDosError();break;
342 return FALSE;
344 return TRUE;
348 /***********************************************************************
349 * CreateDirectory32W (KERNEL32.42)
351 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
352 LPSECURITY_ATTRIBUTES lpsecattribs )
354 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
355 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
356 HeapFree( GetProcessHeap(), 0, xpath );
357 return ret;
361 /***********************************************************************
362 * CreateDirectoryEx32A (KERNEL32.40)
364 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
365 LPSECURITY_ATTRIBUTES lpsecattribs)
367 return CreateDirectoryA(path,lpsecattribs);
371 /***********************************************************************
372 * CreateDirectoryEx32W (KERNEL32.41)
374 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
375 LPSECURITY_ATTRIBUTES lpsecattribs)
377 return CreateDirectoryW(path,lpsecattribs);
381 /***********************************************************************
382 * RemoveDirectory16 (KERNEL)
384 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
386 return (BOOL16)RemoveDirectoryA( path );
390 /***********************************************************************
391 * RemoveDirectory32A (KERNEL32.437)
393 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
395 DOS_FULL_NAME full_name;
397 TRACE_(file)("'%s'\n", path );
399 if (DOSFS_GetDevice( path ))
401 TRACE_(file)("cannot remove device '%s'!\n", path);
402 SetLastError( ERROR_FILE_NOT_FOUND );
403 return FALSE;
405 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
406 if (rmdir( full_name.long_name ) == -1)
408 FILE_SetDosError();
409 return FALSE;
411 return TRUE;
415 /***********************************************************************
416 * RemoveDirectory32W (KERNEL32.438)
418 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
420 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
421 BOOL ret = RemoveDirectoryA( xpath );
422 HeapFree( GetProcessHeap(), 0, xpath );
423 return ret;
427 /***********************************************************************
428 * DIR_TryPath
430 * Helper function for DIR_SearchPath.
432 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
433 DOS_FULL_NAME *full_name )
435 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
436 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
438 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
439 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
441 SetLastError( ERROR_PATH_NOT_FOUND );
442 return FALSE;
444 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
445 sizeof(full_name->long_name) - (p_l - full_name->long_name),
446 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
447 return FALSE;
448 strcpy( full_name->long_name, dir->long_name );
449 p_l[-1] = '/';
450 strcpy( full_name->short_name, dir->short_name );
451 p_s[-1] = '\\';
452 return TRUE;
456 /***********************************************************************
457 * DIR_TryEnvironmentPath
459 * Helper function for DIR_SearchPath.
461 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
463 LPSTR path, next, buffer;
464 BOOL ret = FALSE;
465 INT len = strlen(name);
466 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
468 if (!size) return FALSE;
469 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
470 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
471 next = path;
472 while (!ret && next)
474 LPSTR cur = next;
475 while (*cur == ';') cur++;
476 if (!*cur) break;
477 next = strchr( cur, ';' );
478 if (next) *next++ = '\0';
479 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
480 goto done;
481 strcpy( buffer, cur );
482 strcat( buffer, "\\" );
483 strcat( buffer, name );
484 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
485 HeapFree( GetProcessHeap(), 0, buffer );
488 done:
489 HeapFree( GetProcessHeap(), 0, path );
490 return ret;
494 /***********************************************************************
495 * DIR_TryModulePath
497 * Helper function for DIR_SearchPath.
499 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
501 PDB *pdb = PROCESS_Current();
503 /* FIXME: for now, GetModuleFileName32A can't return more */
504 /* than OFS_MAXPATHNAME. This may change with Win32. */
506 char buffer[OFS_MAXPATHNAME];
507 LPSTR p;
509 if (pdb->flags & PDB32_WIN16_PROC) {
510 if (!GetCurrentTask()) return FALSE;
511 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
512 buffer[0]='\0';
513 } else {
514 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
515 buffer[0]='\0';
517 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
518 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
519 strcpy( p, name );
520 return DOSFS_GetFullName( buffer, TRUE, full_name );
524 /***********************************************************************
525 * DIR_SearchPath
527 * Implementation of SearchPath32A. 'win32' specifies whether the search
528 * order is Win16 (module path last) or Win32 (module path first).
530 * FIXME: should return long path names.
532 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
533 DOS_FULL_NAME *full_name, BOOL win32 )
535 DWORD len;
536 LPCSTR p;
537 LPSTR tmp = NULL;
538 BOOL ret = TRUE;
540 /* First check the supplied parameters */
542 p = strrchr( name, '.' );
543 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
544 ext = NULL; /* Ignore the specified extension */
545 if ((*name && (name[1] == ':')) ||
546 strchr( name, '/' ) || strchr( name, '\\' ))
547 path = NULL; /* Ignore path if name already contains a path */
548 if (path && !*path) path = NULL; /* Ignore empty path */
550 len = strlen(name);
551 if (ext) len += strlen(ext);
552 if (path) len += strlen(path) + 1;
554 /* Allocate a buffer for the file name and extension */
556 if (path || ext)
558 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
560 SetLastError( ERROR_OUTOFMEMORY );
561 return 0;
563 if (path)
565 strcpy( tmp, path );
566 strcat( tmp, "\\" );
567 strcat( tmp, name );
569 else strcpy( tmp, name );
570 if (ext) strcat( tmp, ext );
571 name = tmp;
574 /* If we have an explicit path, everything's easy */
576 if (path || (*name && (name[1] == ':')) ||
577 strchr( name, '/' ) || strchr( name, '\\' ))
579 ret = DOSFS_GetFullName( name, TRUE, full_name );
580 goto done;
583 /* Try the path of the current executable (for Win32 search order) */
585 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
587 /* Try the current directory */
589 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
591 /* Try the Windows system directory */
593 if (DIR_TryPath( &DIR_System, name, full_name ))
594 goto done;
596 /* Try the Windows directory */
598 if (DIR_TryPath( &DIR_Windows, name, full_name ))
599 goto done;
601 /* Try the path of the current executable (for Win16 search order) */
603 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
605 /* Try all directories in path */
607 ret = DIR_TryEnvironmentPath( name, full_name );
609 done:
610 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
611 return ret;
615 /***********************************************************************
616 * SearchPath32A [KERNEL32.447]
618 * Searches for a specified file in the search path.
620 * PARAMS
621 * path [I] Path to search
622 * name [I] Filename to search for.
623 * ext [I] File extension to append to file name. The first
624 * character must be a period. This parameter is
625 * specified only if the filename given does not
626 * contain an extension.
627 * buflen [I] size of buffer, in characters
628 * buffer [O] buffer for found filename
629 * lastpart [O] address of pointer to last used character in
630 * buffer (the final '\')
632 * RETURNS
633 * Success: length of string copied into buffer, not including
634 * terminating null character. If the filename found is
635 * longer than the length of the buffer, the length of the
636 * filename is returned.
637 * Failure: Zero
639 * NOTES
640 * Should call SetLastError(but currently doesn't).
642 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
643 LPSTR buffer, LPSTR *lastpart )
645 LPSTR p, res;
646 DOS_FULL_NAME full_name;
648 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
649 lstrcpynA( buffer, full_name.short_name, buflen );
650 res = full_name.long_name +
651 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
652 while (*res == '/') res++;
653 if (buflen)
655 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
656 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
657 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
659 TRACE("Returning %d\n", strlen(res) + 3 );
660 return strlen(res) + 3;
664 /***********************************************************************
665 * SearchPath32W (KERNEL32.448)
667 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
668 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
670 LPWSTR p;
671 LPSTR res;
672 DOS_FULL_NAME full_name;
674 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
675 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
676 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
677 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
678 HeapFree( GetProcessHeap(), 0, extA );
679 HeapFree( GetProcessHeap(), 0, nameA );
680 HeapFree( GetProcessHeap(), 0, pathA );
681 if (!ret) return 0;
683 lstrcpynAtoW( buffer, full_name.short_name, buflen );
684 res = full_name.long_name +
685 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
686 while (*res == '/') res++;
687 if (buflen)
689 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
690 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
691 if (lastpart)
693 for (p = *lastpart = buffer; *p; p++)
694 if (*p == '\\') *lastpart = p + 1;
697 return strlen(res) + 3;