Release 971012
[wine.git] / files / directory.c
blob0f45fa968ea630da1b12481d079cac264eee343b
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <errno.h>
14 #include "windows.h"
15 #include "winerror.h"
16 #include "drive.h"
17 #include "file.h"
18 #include "heap.h"
19 #include "msdos.h"
20 #include "options.h"
21 #include "stddebug.h"
22 #include "debug.h"
24 #define MAX_PATH_ELEMENTS 20
26 static char *DIR_WindowsDosDir;
27 static char *DIR_WindowsUnixDir;
28 static char *DIR_SystemDosDir;
29 static char *DIR_SystemUnixDir;
30 static char *DIR_TempDosDir;
31 static char *DIR_TempUnixDir;
33 static char *DIR_DosPath[MAX_PATH_ELEMENTS]; /* Path in DOS format */
34 static char *DIR_UnixPath[MAX_PATH_ELEMENTS]; /* Path in Unix format */
35 static int DIR_PathElements = 0;
37 /***********************************************************************
38 * DIR_GetPath
40 * Get a path name from the wine.ini file and make sure it is valid.
42 static int DIR_GetPath( const char *keyname, const char *defval,
43 char **dos_path, char **unix_path )
45 char path[MAX_PATHNAME_LEN];
46 DOS_FULL_NAME full_name;
48 BY_HANDLE_FILE_INFORMATION info;
50 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
51 if (!DOSFS_GetFullName( path, TRUE, &full_name ) ||
52 !FILE_Stat( full_name.long_name, &info ) ||
53 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
55 fprintf(stderr, "Invalid path '%s' for %s directory\n", path, keyname);
56 return 0;
58 *unix_path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
59 *dos_path = HEAP_strdupA( SystemHeap, 0, full_name.short_name );
60 return 1;
64 /***********************************************************************
65 * DIR_ParseWindowsPath
67 void DIR_ParseWindowsPath( char *path )
69 char *p;
70 DOS_FULL_NAME full_name;
71 BY_HANDLE_FILE_INFORMATION info;
72 int i;
74 for ( ; path && *path; path = p)
76 p = strchr( path, ';' );
77 if (p) while (*p == ';') *p++ = '\0';
79 if (DIR_PathElements >= MAX_PATH_ELEMENTS)
81 fprintf( stderr, "Warning: path has more than %d elements.\n",
82 MAX_PATH_ELEMENTS );
83 break;
85 if (!DOSFS_GetFullName( path, TRUE, &full_name ) ||
86 !FILE_Stat( full_name.long_name, &info ) ||
87 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
89 fprintf(stderr,"Warning: invalid dir '%s' in path, deleting it.\n",
90 path );
91 continue;
93 DIR_UnixPath[DIR_PathElements] = HEAP_strdupA( SystemHeap, 0,
94 full_name.long_name );
95 DIR_DosPath[DIR_PathElements] = HEAP_strdupA( SystemHeap, 0,
96 full_name.short_name );
97 DIR_PathElements++;
100 if (debugging_dosfs)
101 for (i = 0; i < DIR_PathElements; i++)
103 dprintf_dosfs( stddeb, "Path[%d]: %s = %s\n",
104 i, DIR_DosPath[i], DIR_UnixPath[i] );
109 /***********************************************************************
110 * DIR_Init
112 int DIR_Init(void)
114 char path[MAX_PATHNAME_LEN], *env_p;
115 int drive;
116 const char *cwd;
118 if (!getcwd( path, MAX_PATHNAME_LEN ))
120 perror( "Could not get current directory" );
121 return 0;
123 cwd = path;
124 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
126 fprintf( stderr, "Warning: could not find DOS drive for cwd %s; starting in windows directory.\n",
127 cwd );
129 else
131 DRIVE_SetCurrentDrive( drive );
132 DRIVE_Chdir( drive, cwd );
135 if (!(DIR_GetPath( "windows", "c:\\windows",
136 &DIR_WindowsDosDir, &DIR_WindowsUnixDir ))) return 0;
137 if (!(DIR_GetPath( "system", "c:\\windows\\system",
138 &DIR_SystemDosDir, &DIR_SystemUnixDir ))) return 0;
139 if (!(DIR_GetPath( "temp", "c:\\windows",
140 &DIR_TempDosDir, &DIR_TempUnixDir ))) return 0;
141 if (-1==access(DIR_TempUnixDir,W_OK)) {
142 if (errno==EACCES)
143 fprintf(stderr,"Warning: The Temporary Directory (as specified in wine.conf) is NOT writeable. Please check your configuration.\n");
144 else
145 fprintf(stderr,"Warning: Access to Temporary Directory failed (%s).\n",strerror(errno));
148 if (drive == -1)
150 drive = DIR_WindowsDosDir[0] - 'A';
151 DRIVE_SetCurrentDrive( drive );
152 DRIVE_Chdir( drive, DIR_WindowsDosDir + 2 );
155 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
156 path, sizeof(path) );
157 DIR_ParseWindowsPath( path );
159 dprintf_dosfs( stddeb, "WindowsDir = %s\nSystemDir = %s\n",
160 DIR_WindowsDosDir, DIR_SystemDosDir );
161 dprintf_dosfs( stddeb, "TempDir = %s\nCwd = %c:\\%s\n",
162 DIR_TempDosDir, 'A' + drive, DRIVE_GetDosCwd( drive ) );
164 /* Put the temp and Windows directories into the environment */
166 env_p = HEAP_xalloc( SystemHeap, 0, strlen(DIR_TempDosDir) + 6 );
167 strcpy( env_p, "TEMP=" );
168 strcpy( env_p + 5, DIR_TempDosDir );
169 putenv( env_p );
170 env_p = HEAP_xalloc( SystemHeap, 0, strlen(DIR_WindowsDosDir) + 8 );
171 strcpy( env_p, "windir=" );
172 strcpy( env_p + 7, DIR_WindowsDosDir );
173 putenv( env_p );
175 return 1;
179 /***********************************************************************
180 * GetTempPath32A (KERNEL32.292)
182 UINT32 WINAPI GetTempPath32A( UINT32 count, LPSTR path )
184 if (path) lstrcpyn32A( path, DIR_TempDosDir, count );
185 return strlen( DIR_TempDosDir );
189 /***********************************************************************
190 * GetTempPath32W (KERNEL32.293)
192 UINT32 WINAPI GetTempPath32W( UINT32 count, LPWSTR path )
194 if (path) lstrcpynAtoW( path, DIR_TempDosDir, count );
195 return strlen( DIR_TempDosDir );
199 /***********************************************************************
200 * DIR_GetTempUnixDir
202 UINT32 DIR_GetTempUnixDir( LPSTR path, UINT32 count )
204 if (path) lstrcpyn32A( path, DIR_TempUnixDir, count );
205 return strlen( DIR_TempUnixDir );
209 /***********************************************************************
210 * DIR_GetWindowsUnixDir
212 UINT32 DIR_GetWindowsUnixDir( LPSTR path, UINT32 count )
214 if (path) lstrcpyn32A( path, DIR_WindowsUnixDir, count );
215 return strlen( DIR_WindowsUnixDir );
219 /***********************************************************************
220 * DIR_GetSystemUnixDir
222 UINT32 DIR_GetSystemUnixDir( LPSTR path, UINT32 count )
224 if (path) lstrcpyn32A( path, DIR_SystemUnixDir, count );
225 return strlen( DIR_SystemUnixDir );
229 /***********************************************************************
230 * DIR_GetDosPath
232 UINT32 DIR_GetDosPath( INT32 element, LPSTR path, UINT32 count )
234 if ((element < 0) || (element >= DIR_PathElements)) return 0;
235 if (path) lstrcpyn32A( path, DIR_DosPath[element], count );
236 return strlen( DIR_DosPath[element] );
240 /***********************************************************************
241 * GetTempDrive (KERNEL.92)
243 BYTE WINAPI GetTempDrive( BYTE ignored )
245 /* FIXME: apparently Windows does something with the ignored byte */
246 return DIR_TempDosDir[0];
250 UINT32 WINAPI WIN16_GetTempDrive( BYTE ignored )
252 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
254 * returns:
255 * AL: driveletter
256 * AH: ':' - yes, some kernel code even does stosw with
257 * the returned AX.
258 * DX: 1 for success
260 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
264 /***********************************************************************
265 * GetWindowsDirectory16 (KERNEL.134)
267 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
269 return (UINT16)GetWindowsDirectory32A( path, count );
273 /***********************************************************************
274 * GetWindowsDirectory32A (KERNEL32.311)
276 UINT32 WINAPI GetWindowsDirectory32A( LPSTR path, UINT32 count )
278 if (path) lstrcpyn32A( path, DIR_WindowsDosDir, count );
279 return strlen( DIR_WindowsDosDir );
283 /***********************************************************************
284 * GetWindowsDirectory32W (KERNEL32.312)
286 UINT32 WINAPI GetWindowsDirectory32W( LPWSTR path, UINT32 count )
288 if (path) lstrcpynAtoW( path, DIR_WindowsDosDir, count );
289 return strlen( DIR_WindowsDosDir );
293 /***********************************************************************
294 * GetSystemDirectory16 (KERNEL.135)
296 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
298 return (UINT16)GetSystemDirectory32A( path, count );
302 /***********************************************************************
303 * GetSystemDirectory32A (KERNEL32.282)
305 UINT32 WINAPI GetSystemDirectory32A( LPSTR path, UINT32 count )
307 if (path) lstrcpyn32A( path, DIR_SystemDosDir, count );
308 return strlen( DIR_SystemDosDir );
312 /***********************************************************************
313 * GetSystemDirectory32W (KERNEL32.283)
315 UINT32 WINAPI GetSystemDirectory32W( LPWSTR path, UINT32 count )
317 if (path) lstrcpynAtoW( path, DIR_SystemDosDir, count );
318 return strlen( DIR_SystemDosDir );
322 /***********************************************************************
323 * CreateDirectory16 (KERNEL.144)
325 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
327 dprintf_file( stddeb,"CreateDirectory16(%s,%p)\n", path, dummy );
328 return (BOOL16)CreateDirectory32A( path, NULL );
332 /***********************************************************************
333 * CreateDirectory32A (KERNEL32.39)
335 BOOL32 WINAPI CreateDirectory32A( LPCSTR path,
336 LPSECURITY_ATTRIBUTES lpsecattribs )
338 DOS_FULL_NAME full_name;
339 LPCSTR unixName;
341 dprintf_file( stddeb, "CreateDirectory32A(%s,%p)\n", path, lpsecattribs );
342 if ((unixName = DOSFS_IsDevice( path )) != NULL)
344 dprintf_file(stddeb, "CreateDirectory: device '%s'!\n", unixName);
345 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
346 return FALSE;
348 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
349 if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
351 FILE_SetDosError();
352 return FALSE;
354 return TRUE;
358 /***********************************************************************
359 * CreateDirectory32W (KERNEL32.42)
361 BOOL32 WINAPI CreateDirectory32W( LPCWSTR path,
362 LPSECURITY_ATTRIBUTES lpsecattribs )
364 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
365 BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
366 HeapFree( GetProcessHeap(), 0, xpath );
367 return ret;
371 /***********************************************************************
372 * CreateDirectoryEx32A (KERNEL32.40)
374 BOOL32 WINAPI CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
375 LPSECURITY_ATTRIBUTES lpsecattribs)
377 return CreateDirectory32A(path,lpsecattribs);
381 /***********************************************************************
382 * CreateDirectoryEx32W (KERNEL32.41)
384 BOOL32 WINAPI CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
385 LPSECURITY_ATTRIBUTES lpsecattribs)
387 return CreateDirectory32W(path,lpsecattribs);
391 /***********************************************************************
392 * RemoveDirectory16 (KERNEL)
394 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
396 return (BOOL16)RemoveDirectory32A( path );
400 /***********************************************************************
401 * RemoveDirectory32A (KERNEL32.437)
403 BOOL32 WINAPI RemoveDirectory32A( LPCSTR path )
405 DOS_FULL_NAME full_name;
406 LPCSTR unixName;
408 dprintf_file(stddeb, "RemoveDirectory: '%s'\n", path );
410 if ((unixName = DOSFS_IsDevice( path )) != NULL)
412 dprintf_file(stddeb, "RemoveDirectory: device '%s'!\n", unixName);
413 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
414 return FALSE;
416 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
417 if (rmdir( full_name.long_name ) == -1)
419 FILE_SetDosError();
420 return FALSE;
422 return TRUE;
426 /***********************************************************************
427 * RemoveDirectory32W (KERNEL32.438)
429 BOOL32 WINAPI RemoveDirectory32W( LPCWSTR path )
431 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
432 BOOL32 ret = RemoveDirectory32A( xpath );
433 HeapFree( GetProcessHeap(), 0, xpath );
434 return ret;
438 /***********************************************************************
439 * DIR_TryPath
441 * Helper function for DIR_SearchPath.
443 static BOOL32 DIR_TryPath( LPCSTR unix_dir, LPCSTR dos_dir, LPCSTR name,
444 DOS_FULL_NAME *full_name )
446 LPSTR p_l = full_name->long_name + strlen(unix_dir) + 1;
447 LPSTR p_s = full_name->short_name + strlen(dos_dir) + 1;
449 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
450 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
452 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
453 return FALSE;
455 if (!DOSFS_FindUnixName( unix_dir, name, p_l,
456 sizeof(full_name->long_name) - (p_l - full_name->long_name),
457 p_s, DRIVE_GetFlags( dos_dir[0] - 'A' ) ))
458 return FALSE;
459 strcpy( full_name->long_name, unix_dir );
460 p_l[-1] = '/';
461 strcpy( full_name->short_name, dos_dir );
462 p_s[-1] = '\\';
463 return TRUE;
467 /***********************************************************************
468 * DIR_TryModulePath
470 * Helper function for DIR_SearchPath.
472 static BOOL32 DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
474 /* FIXME: for now, GetModuleFileName32A can't return more */
475 /* than OFS_MAXPATHNAME. This may change with Win32. */
476 char buffer[OFS_MAXPATHNAME];
477 LPSTR p;
479 if (!GetCurrentTask()) return FALSE;
480 GetModuleFileName32A( GetCurrentTask(), buffer, sizeof(buffer) );
481 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
482 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
483 strcpy( p, name );
484 return DOSFS_GetFullName( buffer, TRUE, full_name );
488 /***********************************************************************
489 * DIR_SearchPath
491 * Implementation of SearchPath32A. 'win32' specifies whether the search
492 * order is Win16 (module path last) or Win32 (module path first).
494 * FIXME: should return long path names.
496 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
497 DOS_FULL_NAME *full_name, BOOL32 win32 )
499 DWORD len;
500 LPCSTR p;
501 int i;
502 LPSTR tmp = NULL;
503 BOOL32 ret = TRUE;
505 /* First check the supplied parameters */
507 p = strrchr( name, '.' );
508 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
509 ext = NULL; /* Ignore the specified extension */
510 if ((*name && (name[1] == ':')) ||
511 strchr( name, '/' ) || strchr( name, '\\' ))
512 path = NULL; /* Ignore path if name already contains a path */
513 if (path && !*path) path = NULL; /* Ignore empty path */
515 len = strlen(name);
516 if (ext) len += strlen(ext);
517 if (path) len += strlen(path) + 1;
519 /* Allocate a buffer for the file name and extension */
521 if (path || ext)
523 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
525 SetLastError( ERROR_OUTOFMEMORY );
526 return 0;
528 if (path)
530 strcpy( tmp, path );
531 strcat( tmp, "\\" );
532 strcat( tmp, name );
534 else strcpy( tmp, name );
535 if (ext) strcat( tmp, ext );
536 name = tmp;
539 /* If we have an explicit path, everything's easy */
541 if (path || (*name && (name[1] == ':')) ||
542 strchr( name, '/' ) || strchr( name, '\\' ))
544 ret = DOSFS_GetFullName( name, TRUE, full_name );
545 goto done;
548 /* Try the path of the current executable (for Win32 search order) */
550 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
552 /* Try the current directory */
554 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
556 /* Try the Windows directory */
558 if (DIR_TryPath( DIR_WindowsUnixDir, DIR_WindowsDosDir, name, full_name ))
559 goto done;
561 /* Try the Windows system directory */
563 if (DIR_TryPath( DIR_SystemUnixDir, DIR_SystemDosDir, name, full_name ))
564 goto done;
566 /* Try the path of the current executable (for Win16 search order) */
568 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
570 /* Try all directories in path */
572 for (i = 0; i < DIR_PathElements; i++)
574 if (DIR_TryPath( DIR_UnixPath[i], DIR_DosPath[i], name, full_name ))
575 goto done;
578 ret = FALSE;
579 done:
580 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
581 return ret;
585 /***********************************************************************
586 * SearchPath32A (KERNEL32.447)
588 DWORD WINAPI SearchPath32A( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
589 LPSTR buffer, LPSTR *lastpart )
591 LPSTR p, res;
592 DOS_FULL_NAME full_name;
594 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
595 lstrcpyn32A( buffer, full_name.short_name, buflen );
596 res = full_name.long_name +
597 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
598 while (*res == '/') res++;
599 if (buflen)
601 if (buflen > 3) lstrcpyn32A( buffer + 3, res, buflen - 3 );
602 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
603 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
605 return *res ? strlen(res) + 2 : 3;
609 /***********************************************************************
610 * SearchPath32W (KERNEL32.448)
612 DWORD WINAPI SearchPath32W( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
613 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
615 LPWSTR p;
616 LPSTR res;
617 DOS_FULL_NAME full_name;
619 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
620 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
621 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
622 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
623 HeapFree( GetProcessHeap(), 0, extA );
624 HeapFree( GetProcessHeap(), 0, nameA );
625 HeapFree( GetProcessHeap(), 0, pathA );
626 if (!ret) return 0;
628 lstrcpynAtoW( buffer, full_name.short_name, buflen );
629 res = full_name.long_name +
630 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
631 while (*res == '/') res++;
632 if (buflen)
634 if (buflen > 3) lstrcpynAtoW( buffer + 3, res + 1, buflen - 3 );
635 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
636 if (lastpart)
638 for (p = *lastpart = buffer; *p; p++)
639 if (*p == '\\') *lastpart = p + 1;
642 return *res ? strlen(res) + 2 : 3;