Implemented waitable timers.
[wine.git] / files / directory.c
blob353c99aaf2dcedc6dbb7f7d0011bb0fc11152f1c
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 DECLARE_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) );
120 /* Set the environment variables */
122 SetEnvironmentVariableA( "PATH", path );
123 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
124 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
125 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
126 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
128 TRACE_(dosfs)("WindowsDir = %s (%s)\n",
129 DIR_Windows.short_name, DIR_Windows.long_name );
130 TRACE_(dosfs)("SystemDir = %s (%s)\n",
131 DIR_System.short_name, DIR_System.long_name );
132 TRACE_(dosfs)("TempDir = %s (%s)\n",
133 tmp_dir.short_name, tmp_dir.long_name );
134 TRACE_(dosfs)("Path = %s\n", path );
135 TRACE_(dosfs)("Cwd = %c:\\%s\n",
136 'A' + drive, DRIVE_GetDosCwd( drive ) );
138 return 1;
142 /***********************************************************************
143 * GetTempPath32A (KERNEL32.292)
145 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
147 UINT ret;
148 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
149 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
150 if (!(ret = GetCurrentDirectoryA( count, path )))
151 return 0;
152 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
154 path[ret++] = '\\';
155 path[ret] = '\0';
157 return ret;
161 /***********************************************************************
162 * GetTempPath32W (KERNEL32.293)
164 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
166 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
167 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
168 UINT ret;
169 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
170 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
171 if (!(ret = GetCurrentDirectoryW( count, path )))
172 return 0;
173 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
175 path[ret++] = '\\';
176 path[ret] = '\0';
178 return ret;
182 /***********************************************************************
183 * DIR_GetWindowsUnixDir
185 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
187 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
188 return strlen( DIR_Windows.long_name );
192 /***********************************************************************
193 * DIR_GetSystemUnixDir
195 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
197 if (path) lstrcpynA( path, DIR_System.long_name, count );
198 return strlen( DIR_System.long_name );
202 /***********************************************************************
203 * GetTempDrive (KERNEL.92)
205 BYTE WINAPI GetTempDrive( BYTE ignored )
207 char *buffer;
208 BYTE ret;
209 UINT len = GetTempPathA( 0, NULL );
211 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
212 return DRIVE_GetCurrentDrive() + 'A';
214 /* FIXME: apparently Windows does something with the ignored byte */
215 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
216 ret = buffer[0];
217 HeapFree( GetProcessHeap(), 0, buffer );
218 return toupper(ret);
222 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
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 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
236 /***********************************************************************
237 * GetWindowsDirectory16 (KERNEL.134)
239 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
241 return (UINT16)GetWindowsDirectoryA( path, count );
245 /***********************************************************************
246 * GetWindowsDirectory32A (KERNEL32.311)
248 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
250 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
251 return strlen( DIR_Windows.short_name );
255 /***********************************************************************
256 * GetWindowsDirectory32W (KERNEL32.312)
258 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
260 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
261 return strlen( DIR_Windows.short_name );
265 /***********************************************************************
266 * GetSystemDirectory16 (KERNEL.135)
268 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
270 return (UINT16)GetSystemDirectoryA( path, count );
274 /***********************************************************************
275 * GetSystemDirectory32A (KERNEL32.282)
277 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
279 if (path) lstrcpynA( path, DIR_System.short_name, count );
280 return strlen( DIR_System.short_name );
284 /***********************************************************************
285 * GetSystemDirectory32W (KERNEL32.283)
287 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
289 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
290 return strlen( DIR_System.short_name );
294 /***********************************************************************
295 * CreateDirectory16 (KERNEL.144)
297 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
299 TRACE_(file)("(%s,%p)\n", path, dummy );
300 return (BOOL16)CreateDirectoryA( path, NULL );
304 /***********************************************************************
305 * CreateDirectory32A (KERNEL32.39)
306 * RETURNS:
307 * TRUE : success
308 * FALSE : failure
309 * ERROR_DISK_FULL: on full disk
310 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
311 * ERROR_ACCESS_DENIED: on permission problems
312 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
314 BOOL WINAPI CreateDirectoryA( LPCSTR path,
315 LPSECURITY_ATTRIBUTES lpsecattribs )
317 DOS_FULL_NAME full_name;
319 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
320 if (DOSFS_GetDevice( path ))
322 TRACE_(file)("cannot use device '%s'!\n",path);
323 SetLastError( ERROR_ACCESS_DENIED );
324 return FALSE;
326 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
327 if (mkdir( full_name.long_name, 0777 ) == -1) {
328 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
329 /* the FILE_SetDosError() generated error codes don't match the
330 * CreateDirectory ones for some errnos */
331 switch (errno) {
332 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
333 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
334 default: FILE_SetDosError();break;
336 return FALSE;
338 return TRUE;
342 /***********************************************************************
343 * CreateDirectory32W (KERNEL32.42)
345 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
346 LPSECURITY_ATTRIBUTES lpsecattribs )
348 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
349 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
350 HeapFree( GetProcessHeap(), 0, xpath );
351 return ret;
355 /***********************************************************************
356 * CreateDirectoryEx32A (KERNEL32.40)
358 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
359 LPSECURITY_ATTRIBUTES lpsecattribs)
361 return CreateDirectoryA(path,lpsecattribs);
365 /***********************************************************************
366 * CreateDirectoryEx32W (KERNEL32.41)
368 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
369 LPSECURITY_ATTRIBUTES lpsecattribs)
371 return CreateDirectoryW(path,lpsecattribs);
375 /***********************************************************************
376 * RemoveDirectory16 (KERNEL)
378 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
380 return (BOOL16)RemoveDirectoryA( path );
384 /***********************************************************************
385 * RemoveDirectory32A (KERNEL32.437)
387 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
389 DOS_FULL_NAME full_name;
391 TRACE_(file)("'%s'\n", path );
393 if (DOSFS_GetDevice( path ))
395 TRACE_(file)("cannot remove device '%s'!\n", path);
396 SetLastError( ERROR_FILE_NOT_FOUND );
397 return FALSE;
399 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
400 if (rmdir( full_name.long_name ) == -1)
402 FILE_SetDosError();
403 return FALSE;
405 return TRUE;
409 /***********************************************************************
410 * RemoveDirectory32W (KERNEL32.438)
412 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
414 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
415 BOOL ret = RemoveDirectoryA( xpath );
416 HeapFree( GetProcessHeap(), 0, xpath );
417 return ret;
421 /***********************************************************************
422 * DIR_TryPath
424 * Helper function for DIR_SearchPath.
426 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
427 DOS_FULL_NAME *full_name )
429 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
430 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
432 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
433 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
435 SetLastError( ERROR_PATH_NOT_FOUND );
436 return FALSE;
438 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
439 sizeof(full_name->long_name) - (p_l - full_name->long_name),
440 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
441 return FALSE;
442 strcpy( full_name->long_name, dir->long_name );
443 p_l[-1] = '/';
444 strcpy( full_name->short_name, dir->short_name );
445 p_s[-1] = '\\';
446 return TRUE;
450 /***********************************************************************
451 * DIR_TryEnvironmentPath
453 * Helper function for DIR_SearchPath.
455 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
457 LPSTR path, next, buffer;
458 BOOL ret = FALSE;
459 INT len = strlen(name);
460 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
462 if (!size) return FALSE;
463 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
464 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
465 next = path;
466 while (!ret && next)
468 LPSTR cur = next;
469 while (*cur == ';') cur++;
470 if (!*cur) break;
471 next = strchr( cur, ';' );
472 if (next) *next++ = '\0';
473 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
474 goto done;
475 strcpy( buffer, cur );
476 strcat( buffer, "\\" );
477 strcat( buffer, name );
478 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
479 HeapFree( GetProcessHeap(), 0, buffer );
482 done:
483 HeapFree( GetProcessHeap(), 0, path );
484 return ret;
488 /***********************************************************************
489 * DIR_TryModulePath
491 * Helper function for DIR_SearchPath.
493 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
495 PDB *pdb = PROCESS_Current();
497 /* FIXME: for now, GetModuleFileName32A can't return more */
498 /* than OFS_MAXPATHNAME. This may change with Win32. */
500 char buffer[OFS_MAXPATHNAME];
501 LPSTR p;
503 if (pdb->flags & PDB32_WIN16_PROC) {
504 if (!GetCurrentTask()) return FALSE;
505 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
506 buffer[0]='\0';
507 } else {
508 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
509 buffer[0]='\0';
511 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
512 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
513 strcpy( p, name );
514 return DOSFS_GetFullName( buffer, TRUE, full_name );
518 /***********************************************************************
519 * DIR_SearchPath
521 * Implementation of SearchPath32A. 'win32' specifies whether the search
522 * order is Win16 (module path last) or Win32 (module path first).
524 * FIXME: should return long path names.
526 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
527 DOS_FULL_NAME *full_name, BOOL win32 )
529 DWORD len;
530 LPCSTR p;
531 LPSTR tmp = NULL;
532 BOOL ret = TRUE;
534 /* First check the supplied parameters */
536 p = strrchr( name, '.' );
537 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
538 ext = NULL; /* Ignore the specified extension */
539 if ((*name && (name[1] == ':')) ||
540 strchr( name, '/' ) || strchr( name, '\\' ))
541 path = NULL; /* Ignore path if name already contains a path */
542 if (path && !*path) path = NULL; /* Ignore empty path */
544 len = strlen(name);
545 if (ext) len += strlen(ext);
546 if (path) len += strlen(path) + 1;
548 /* Allocate a buffer for the file name and extension */
550 if (path || ext)
552 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
554 SetLastError( ERROR_OUTOFMEMORY );
555 return 0;
557 if (path)
559 strcpy( tmp, path );
560 strcat( tmp, "\\" );
561 strcat( tmp, name );
563 else strcpy( tmp, name );
564 if (ext) strcat( tmp, ext );
565 name = tmp;
568 /* If we have an explicit path, everything's easy */
570 if (path || (*name && (name[1] == ':')) ||
571 strchr( name, '/' ) || strchr( name, '\\' ))
573 ret = DOSFS_GetFullName( name, TRUE, full_name );
574 goto done;
577 /* Try the path of the current executable (for Win32 search order) */
579 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
581 /* Try the current directory */
583 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
585 /* Try the Windows system directory */
587 if (DIR_TryPath( &DIR_System, name, full_name ))
588 goto done;
590 /* Try the Windows directory */
592 if (DIR_TryPath( &DIR_Windows, name, full_name ))
593 goto done;
595 /* Try the path of the current executable (for Win16 search order) */
597 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
599 /* Try all directories in path */
601 ret = DIR_TryEnvironmentPath( name, full_name );
603 done:
604 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
605 return ret;
609 /***********************************************************************
610 * SearchPath32A [KERNEL32.447]
612 * Searches for a specified file in the search path.
614 * PARAMS
615 * path [I] Path to search
616 * name [I] Filename to search for.
617 * ext [I] File extension to append to file name. The first
618 * character must be a period. This parameter is
619 * specified only if the filename given does not
620 * contain an extension.
621 * buflen [I] size of buffer, in characters
622 * buffer [O] buffer for found filename
623 * lastpart [O] address of pointer to last used character in
624 * buffer (the final '\')
626 * RETURNS
627 * Success: length of string copied into buffer, not including
628 * terminating null character. If the filename found is
629 * longer than the length of the buffer, the length of the
630 * filename is returned.
631 * Failure: Zero
633 * NOTES
634 * Should call SetLastError(but currently doesn't).
636 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
637 LPSTR buffer, LPSTR *lastpart )
639 LPSTR p, res;
640 DOS_FULL_NAME full_name;
642 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
643 lstrcpynA( buffer, full_name.short_name, buflen );
644 res = full_name.long_name +
645 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
646 while (*res == '/') res++;
647 if (buflen)
649 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
650 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
651 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
653 TRACE_(dosfs)("Returning %d\n", strlen(res) + 3 );
654 return strlen(res) + 3;
658 /***********************************************************************
659 * SearchPath32W (KERNEL32.448)
661 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
662 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
664 LPWSTR p;
665 LPSTR res;
666 DOS_FULL_NAME full_name;
668 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
669 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
670 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
671 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
672 HeapFree( GetProcessHeap(), 0, extA );
673 HeapFree( GetProcessHeap(), 0, nameA );
674 HeapFree( GetProcessHeap(), 0, pathA );
675 if (!ret) return 0;
677 lstrcpynAtoW( buffer, full_name.short_name, buflen );
678 res = full_name.long_name +
679 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
680 while (*res == '/') res++;
681 if (buflen)
683 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
684 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
685 if (lastpart)
687 for (p = *lastpart = buffer; *p; p++)
688 if (*p == '\\') *lastpart = p + 1;
691 return strlen(res) + 3;