Better error message.
[wine.git] / files / directory.c
blobc2ac33109b89160e5ece18da06d2ffc3af736509
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <sys/errno.h>
16 #include "winbase.h"
17 #include "wine/winbase16.h"
18 #include "wine/winestring.h"
19 #include "winerror.h"
20 #include "process.h"
21 #include "drive.h"
22 #include "file.h"
23 #include "heap.h"
24 #include "msdos.h"
25 #include "options.h"
26 #include "debug.h"
28 static DOS_FULL_NAME DIR_Windows;
29 static DOS_FULL_NAME DIR_System;
32 /***********************************************************************
33 * DIR_GetPath
35 * Get a path name from the wine.ini file and make sure it is valid.
37 static int DIR_GetPath( const char *keyname, const char *defval,
38 DOS_FULL_NAME *full_name )
40 char path[MAX_PATHNAME_LEN];
41 BY_HANDLE_FILE_INFORMATION info;
43 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
44 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
45 !FILE_Stat( full_name->long_name, &info ) ||
46 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
48 MSG("Invalid path '%s' for %s directory\n", path, keyname);
49 return 0;
51 return 1;
55 /***********************************************************************
56 * DIR_Init
58 int DIR_Init(void)
60 char path[MAX_PATHNAME_LEN];
61 DOS_FULL_NAME tmp_dir;
62 int drive;
63 const char *cwd;
65 if (!getcwd( path, MAX_PATHNAME_LEN ))
67 perror( "Could not get current directory" );
68 return 0;
70 cwd = path;
71 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
73 MSG("Warning: could not find wine.conf [Drive x] entry "
74 "for current working directory %s; "
75 "starting in windows directory.\n", cwd );
77 else
79 DRIVE_SetCurrentDrive( drive );
80 DRIVE_Chdir( drive, cwd );
83 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
84 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
85 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
87 PROFILE_UsageWineIni();
88 return 0;
90 if (-1 == access( tmp_dir.long_name, W_OK ))
92 if (errno==EACCES)
94 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
95 PROFILE_UsageWineIni();
97 else
98 MSG("Warning: Access to Temporary Directory failed (%s).\n",
99 strerror(errno));
102 if (drive == -1)
104 drive = DIR_Windows.drive;
105 DRIVE_SetCurrentDrive( drive );
106 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
109 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
110 path, sizeof(path) );
112 /* Set the environment variables */
114 SetEnvironmentVariableA( "PATH", path );
115 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
116 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
117 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
118 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
120 TRACE(dosfs, "WindowsDir = %s (%s)\n",
121 DIR_Windows.short_name, DIR_Windows.long_name );
122 TRACE(dosfs, "SystemDir = %s (%s)\n",
123 DIR_System.short_name, DIR_System.long_name );
124 TRACE(dosfs, "TempDir = %s (%s)\n",
125 tmp_dir.short_name, tmp_dir.long_name );
126 TRACE(dosfs, "Path = %s\n", path );
127 TRACE(dosfs, "Cwd = %c:\\%s\n",
128 'A' + drive, DRIVE_GetDosCwd( drive ) );
130 return 1;
134 /***********************************************************************
135 * GetTempPath32A (KERNEL32.292)
137 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
139 UINT ret;
140 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
141 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
142 if (!(ret = GetCurrentDirectoryA( count, path )))
143 return 0;
144 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
146 path[ret++] = '\\';
147 path[ret] = '\0';
149 return ret;
153 /***********************************************************************
154 * GetTempPath32W (KERNEL32.293)
156 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
158 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
159 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
160 UINT ret;
161 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
162 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
163 if (!(ret = GetCurrentDirectoryW( count, path )))
164 return 0;
165 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
167 path[ret++] = '\\';
168 path[ret] = '\0';
170 return ret;
174 /***********************************************************************
175 * DIR_GetWindowsUnixDir
177 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
179 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
180 return strlen( DIR_Windows.long_name );
184 /***********************************************************************
185 * DIR_GetSystemUnixDir
187 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
189 if (path) lstrcpynA( path, DIR_System.long_name, count );
190 return strlen( DIR_System.long_name );
194 /***********************************************************************
195 * GetTempDrive (KERNEL.92)
197 BYTE WINAPI GetTempDrive( BYTE ignored )
199 char *buffer;
200 BYTE ret;
201 UINT len = GetTempPathA( 0, NULL );
203 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
204 return DRIVE_GetCurrentDrive() + 'A';
206 /* FIXME: apparently Windows does something with the ignored byte */
207 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
208 ret = buffer[0];
209 HeapFree( GetProcessHeap(), 0, buffer );
210 return toupper(ret);
214 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
216 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
218 * returns:
219 * AL: driveletter
220 * AH: ':' - yes, some kernel code even does stosw with
221 * the returned AX.
222 * DX: 1 for success
224 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
228 /***********************************************************************
229 * GetWindowsDirectory16 (KERNEL.134)
231 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
233 return (UINT16)GetWindowsDirectoryA( path, count );
237 /***********************************************************************
238 * GetWindowsDirectory32A (KERNEL32.311)
240 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
242 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
243 return strlen( DIR_Windows.short_name );
247 /***********************************************************************
248 * GetWindowsDirectory32W (KERNEL32.312)
250 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
252 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
253 return strlen( DIR_Windows.short_name );
257 /***********************************************************************
258 * GetSystemDirectory16 (KERNEL.135)
260 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
262 return (UINT16)GetSystemDirectoryA( path, count );
266 /***********************************************************************
267 * GetSystemDirectory32A (KERNEL32.282)
269 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
271 if (path) lstrcpynA( path, DIR_System.short_name, count );
272 return strlen( DIR_System.short_name );
276 /***********************************************************************
277 * GetSystemDirectory32W (KERNEL32.283)
279 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
281 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
282 return strlen( DIR_System.short_name );
286 /***********************************************************************
287 * CreateDirectory16 (KERNEL.144)
289 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
291 TRACE(file,"(%s,%p)\n", path, dummy );
292 return (BOOL16)CreateDirectoryA( path, NULL );
296 /***********************************************************************
297 * CreateDirectory32A (KERNEL32.39)
299 BOOL WINAPI CreateDirectoryA( LPCSTR path,
300 LPSECURITY_ATTRIBUTES lpsecattribs )
302 DOS_FULL_NAME full_name;
304 TRACE(file, "(%s,%p)\n", path, lpsecattribs );
305 if (DOSFS_GetDevice( path ))
307 TRACE(file, "cannot use device '%s'!\n",path);
308 SetLastError( ERROR_ACCESS_DENIED );
309 return FALSE;
311 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
312 if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
314 WARN (file, "Errno %i trying to create directory %s.\n", errno, full_name.long_name);
315 FILE_SetDosError();
316 return FALSE;
318 return TRUE;
322 /***********************************************************************
323 * CreateDirectory32W (KERNEL32.42)
325 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
326 LPSECURITY_ATTRIBUTES lpsecattribs )
328 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
329 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
330 HeapFree( GetProcessHeap(), 0, xpath );
331 return ret;
335 /***********************************************************************
336 * CreateDirectoryEx32A (KERNEL32.40)
338 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
339 LPSECURITY_ATTRIBUTES lpsecattribs)
341 return CreateDirectoryA(path,lpsecattribs);
345 /***********************************************************************
346 * CreateDirectoryEx32W (KERNEL32.41)
348 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
349 LPSECURITY_ATTRIBUTES lpsecattribs)
351 return CreateDirectoryW(path,lpsecattribs);
355 /***********************************************************************
356 * RemoveDirectory16 (KERNEL)
358 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
360 return (BOOL16)RemoveDirectoryA( path );
364 /***********************************************************************
365 * RemoveDirectory32A (KERNEL32.437)
367 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
369 DOS_FULL_NAME full_name;
371 TRACE(file, "'%s'\n", path );
373 if (DOSFS_GetDevice( path ))
375 TRACE(file, "cannot remove device '%s'!\n", path);
376 SetLastError( ERROR_FILE_NOT_FOUND );
377 return FALSE;
379 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
380 if (rmdir( full_name.long_name ) == -1)
382 FILE_SetDosError();
383 return FALSE;
385 return TRUE;
389 /***********************************************************************
390 * RemoveDirectory32W (KERNEL32.438)
392 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
394 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
395 BOOL ret = RemoveDirectoryA( xpath );
396 HeapFree( GetProcessHeap(), 0, xpath );
397 return ret;
401 /***********************************************************************
402 * DIR_TryPath
404 * Helper function for DIR_SearchPath.
406 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
407 DOS_FULL_NAME *full_name )
409 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
410 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
412 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
413 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
415 SetLastError( ERROR_PATH_NOT_FOUND );
416 return FALSE;
418 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
419 sizeof(full_name->long_name) - (p_l - full_name->long_name),
420 p_s, DRIVE_GetFlags( dir->drive ) ))
421 return FALSE;
422 strcpy( full_name->long_name, dir->long_name );
423 p_l[-1] = '/';
424 strcpy( full_name->short_name, dir->short_name );
425 p_s[-1] = '\\';
426 return TRUE;
430 /***********************************************************************
431 * DIR_TryEnvironmentPath
433 * Helper function for DIR_SearchPath.
435 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
437 LPSTR path, next, buffer;
438 BOOL ret = FALSE;
439 INT len = strlen(name);
440 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
442 if (!size) return FALSE;
443 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
444 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
445 next = path;
446 while (!ret && next)
448 LPSTR cur = next;
449 while (*cur == ';') cur++;
450 if (!*cur) break;
451 next = strchr( cur, ';' );
452 if (next) *next++ = '\0';
453 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
454 goto done;
455 strcpy( buffer, cur );
456 strcat( buffer, "\\" );
457 strcat( buffer, name );
458 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
459 HeapFree( GetProcessHeap(), 0, buffer );
462 done:
463 HeapFree( GetProcessHeap(), 0, path );
464 return ret;
468 /***********************************************************************
469 * DIR_TryModulePath
471 * Helper function for DIR_SearchPath.
473 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
475 PDB *pdb = PROCESS_Current();
477 /* FIXME: for now, GetModuleFileName32A can't return more */
478 /* than OFS_MAXPATHNAME. This may change with Win32. */
480 char buffer[OFS_MAXPATHNAME];
481 LPSTR p;
483 if (pdb->flags & PDB32_WIN16_PROC) {
484 if (!GetCurrentTask()) return FALSE;
485 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
486 buffer[0]='\0';
487 } else {
488 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
489 buffer[0]='\0';
491 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
492 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
493 strcpy( p, name );
494 return DOSFS_GetFullName( buffer, TRUE, full_name );
498 /***********************************************************************
499 * DIR_SearchPath
501 * Implementation of SearchPath32A. 'win32' specifies whether the search
502 * order is Win16 (module path last) or Win32 (module path first).
504 * FIXME: should return long path names.
506 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
507 DOS_FULL_NAME *full_name, BOOL win32 )
509 DWORD len;
510 LPCSTR p;
511 LPSTR tmp = NULL;
512 BOOL ret = TRUE;
514 /* First check the supplied parameters */
516 p = strrchr( name, '.' );
517 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
518 ext = NULL; /* Ignore the specified extension */
519 if ((*name && (name[1] == ':')) ||
520 strchr( name, '/' ) || strchr( name, '\\' ))
521 path = NULL; /* Ignore path if name already contains a path */
522 if (path && !*path) path = NULL; /* Ignore empty path */
524 len = strlen(name);
525 if (ext) len += strlen(ext);
526 if (path) len += strlen(path) + 1;
528 /* Allocate a buffer for the file name and extension */
530 if (path || ext)
532 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
534 SetLastError( ERROR_OUTOFMEMORY );
535 return 0;
537 if (path)
539 strcpy( tmp, path );
540 strcat( tmp, "\\" );
541 strcat( tmp, name );
543 else strcpy( tmp, name );
544 if (ext) strcat( tmp, ext );
545 name = tmp;
548 /* If we have an explicit path, everything's easy */
550 if (path || (*name && (name[1] == ':')) ||
551 strchr( name, '/' ) || strchr( name, '\\' ))
553 ret = DOSFS_GetFullName( name, TRUE, full_name );
554 goto done;
557 /* Try the path of the current executable (for Win32 search order) */
559 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
561 /* Try the current directory */
563 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
565 /* Try the Windows directory */
567 if (DIR_TryPath( &DIR_Windows, name, full_name ))
568 goto done;
570 /* Try the Windows system directory */
572 if (DIR_TryPath( &DIR_System, name, full_name ))
573 goto done;
575 /* Try the path of the current executable (for Win16 search order) */
577 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
579 /* Try all directories in path */
581 ret = DIR_TryEnvironmentPath( name, full_name );
583 done:
584 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
585 return ret;
589 /***********************************************************************
590 * SearchPath32A [KERNEL32.447]
592 * Searches for a specified file in the search path.
594 * PARAMS
595 * path [I] Path to search
596 * name [I] Filename to search for.
597 * ext [I] File extension to append to file name. The first
598 * character must be a period. This parameter is
599 * specified only if the filename given does not
600 * contain an extension.
601 * buflen [I] size of buffer, in characters
602 * buffer [O] buffer for found filename
603 * lastpart [O] address of pointer to last used character in
604 * buffer (the final '\')
606 * RETURNS
607 * Success: length of string copied into buffer, not including
608 * terminating null character. If the filename found is
609 * longer than the length of the buffer, the length of the
610 * filename is returned.
611 * Failure: Zero
613 * NOTES
614 * Should call SetLastError(but currently doesn't).
616 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
617 LPSTR buffer, LPSTR *lastpart )
619 LPSTR p, res;
620 DOS_FULL_NAME full_name;
622 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
623 lstrcpynA( buffer, full_name.short_name, buflen );
624 res = full_name.long_name +
625 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
626 while (*res == '/') res++;
627 if (buflen)
629 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
630 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
631 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
633 TRACE(dosfs, "Returning %d\n", (*res ? strlen(res) + 2 : 3));
634 return *res ? strlen(res) + 2 : 3;
638 /***********************************************************************
639 * SearchPath32W (KERNEL32.448)
641 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
642 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
644 LPWSTR p;
645 LPSTR res;
646 DOS_FULL_NAME full_name;
648 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
649 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
650 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
651 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
652 HeapFree( GetProcessHeap(), 0, extA );
653 HeapFree( GetProcessHeap(), 0, nameA );
654 HeapFree( GetProcessHeap(), 0, pathA );
655 if (!ret) return 0;
657 lstrcpynAtoW( buffer, full_name.short_name, buflen );
658 res = full_name.long_name +
659 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
660 while (*res == '/') res++;
661 if (buflen)
663 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
664 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
665 if (lastpart)
667 for (p = *lastpart = buffer; *p; p++)
668 if (*p == '\\') *lastpart = p + 1;
671 return *res ? strlen(res) + 2 : 3;