2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
23 #define MAX_PATH_ELEMENTS 20
25 static char *DIR_WindowsDosDir;
26 static char *DIR_WindowsUnixDir;
27 static char *DIR_SystemDosDir;
28 static char *DIR_SystemUnixDir;
29 static char *DIR_TempDosDir;
30 static char *DIR_TempUnixDir;
32 static char *DIR_DosPath[MAX_PATH_ELEMENTS]; /* Path in DOS format */
33 static char *DIR_UnixPath[MAX_PATH_ELEMENTS]; /* Path in Unix format */
34 static int DIR_PathElements = 0;
36 /***********************************************************************
39 * Get a path name from the wine.ini file and make sure it is valid.
41 static int DIR_GetPath( const char *keyname, const char *defval,
42 char **dos_path, char **unix_path )
44 char path[MAX_PATHNAME_LEN];
45 DOS_FULL_NAME full_name;
47 BY_HANDLE_FILE_INFORMATION info;
49 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
50 if (!DOSFS_GetFullName( path, TRUE, &full_name ) ||
51 !FILE_Stat( full_name.long_name, &info ) ||
52 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
54 fprintf(stderr, "Invalid path '%s' for %s directory\n", path, keyname);
57 *unix_path = HEAP_strdupA( SystemHeap, 0, full_name.long_name );
58 *dos_path = HEAP_strdupA( SystemHeap, 0, full_name.short_name );
63 /***********************************************************************
64 * DIR_ParseWindowsPath
66 void DIR_ParseWindowsPath( char *path )
69 DOS_FULL_NAME full_name;
70 BY_HANDLE_FILE_INFORMATION info;
73 for ( ; path && *path; path = p)
75 p = strchr( path, ';' );
76 if (p) while (*p == ';') *p++ = '\0';
78 if (DIR_PathElements >= MAX_PATH_ELEMENTS)
80 fprintf( stderr, "Warning: path has more than %d elements.\n",
84 if (!DOSFS_GetFullName( path, TRUE, &full_name ) ||
85 !FILE_Stat( full_name.long_name, &info ) ||
86 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
88 fprintf(stderr,"Warning: invalid dir '%s' in path, deleting it.\n",
92 DIR_UnixPath[DIR_PathElements] = HEAP_strdupA( SystemHeap, 0,
93 full_name.long_name );
94 DIR_DosPath[DIR_PathElements] = HEAP_strdupA( SystemHeap, 0,
95 full_name.short_name );
100 for (i = 0; i < DIR_PathElements; i++)
102 TRACE(dosfs, "Path[%d]: %s = %s\n",
103 i, DIR_DosPath[i], DIR_UnixPath[i] );
108 /***********************************************************************
113 char path[MAX_PATHNAME_LEN], *env_p;
117 if (!getcwd( path, MAX_PATHNAME_LEN ))
119 perror( "Could not get current directory" );
123 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
125 fprintf( stderr, "Warning: could not find DOS drive for cwd %s; starting in windows directory.\n",
130 DRIVE_SetCurrentDrive( drive );
131 DRIVE_Chdir( drive, cwd );
134 if (!(DIR_GetPath( "windows", "c:\\windows",
135 &DIR_WindowsDosDir, &DIR_WindowsUnixDir ))) return 0;
136 if (!(DIR_GetPath( "system", "c:\\windows\\system",
137 &DIR_SystemDosDir, &DIR_SystemUnixDir ))) return 0;
138 if (!(DIR_GetPath( "temp", "c:\\windows",
139 &DIR_TempDosDir, &DIR_TempUnixDir ))) return 0;
140 if (-1==access(DIR_TempUnixDir,W_OK)) {
142 fprintf(stderr,"Warning: The Temporary Directory (as specified in wine.conf) is NOT writeable. Please check your configuration.\n");
144 fprintf(stderr,"Warning: Access to Temporary Directory failed (%s).\n",strerror(errno));
149 drive = DIR_WindowsDosDir[0] - 'A';
150 DRIVE_SetCurrentDrive( drive );
151 DRIVE_Chdir( drive, DIR_WindowsDosDir + 2 );
154 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
155 path, sizeof(path) );
156 DIR_ParseWindowsPath( path );
158 TRACE(dosfs, "WindowsDir = %s\n", DIR_WindowsDosDir);
159 TRACE(dosfs, "SystemDir = %s\n", DIR_SystemDosDir);
160 TRACE(dosfs, "TempDir = %s\n", DIR_TempDosDir);
161 TRACE(dosfs, "Cwd = %c:\\%s\n",
162 'A' + drive, DRIVE_GetDosCwd( drive ) );
164 /* Put the temp and Windows and system 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 );
170 env_p = HEAP_xalloc( SystemHeap, 0, strlen(DIR_WindowsDosDir) + 8 );
171 strcpy( env_p, "windir=" );
172 strcpy( env_p + 7, DIR_WindowsDosDir );
174 env_p = HEAP_xalloc( SystemHeap, 0, strlen(DIR_SystemDosDir) + 11 );
175 strcpy( env_p, "winsysdir=" );
176 strcpy( env_p + 10, DIR_SystemDosDir );
183 /***********************************************************************
184 * GetTempPath32A (KERNEL32.292)
186 UINT32 WINAPI GetTempPath32A( UINT32 count, LPSTR path )
188 if (path) lstrcpyn32A( path, DIR_TempDosDir, count );
189 return strlen( DIR_TempDosDir );
193 /***********************************************************************
194 * GetTempPath32W (KERNEL32.293)
196 UINT32 WINAPI GetTempPath32W( UINT32 count, LPWSTR path )
198 if (path) lstrcpynAtoW( path, DIR_TempDosDir, count );
199 return strlen( DIR_TempDosDir );
203 /***********************************************************************
206 UINT32 DIR_GetTempUnixDir( LPSTR path, UINT32 count )
208 if (path) lstrcpyn32A( path, DIR_TempUnixDir, count );
209 return strlen( DIR_TempUnixDir );
213 /***********************************************************************
214 * DIR_GetWindowsUnixDir
216 UINT32 DIR_GetWindowsUnixDir( LPSTR path, UINT32 count )
218 if (path) lstrcpyn32A( path, DIR_WindowsUnixDir, count );
219 return strlen( DIR_WindowsUnixDir );
223 /***********************************************************************
224 * DIR_GetSystemUnixDir
226 UINT32 DIR_GetSystemUnixDir( LPSTR path, UINT32 count )
228 if (path) lstrcpyn32A( path, DIR_SystemUnixDir, count );
229 return strlen( DIR_SystemUnixDir );
233 /***********************************************************************
236 UINT32 DIR_GetDosPath( INT32 element, LPSTR path, UINT32 count )
238 if ((element < 0) || (element >= DIR_PathElements)) return 0;
239 if (path) lstrcpyn32A( path, DIR_DosPath[element], count );
240 return strlen( DIR_DosPath[element] );
244 /***********************************************************************
245 * GetTempDrive (KERNEL.92)
247 BYTE WINAPI GetTempDrive( BYTE ignored )
249 /* FIXME: apparently Windows does something with the ignored byte */
250 return DIR_TempDosDir[0];
254 UINT32 WINAPI WIN16_GetTempDrive( BYTE ignored )
256 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
260 * AH: ':' - yes, some kernel code even does stosw with
264 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
268 /***********************************************************************
269 * GetWindowsDirectory16 (KERNEL.134)
271 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
273 return (UINT16)GetWindowsDirectory32A( path, count );
277 /***********************************************************************
278 * GetWindowsDirectory32A (KERNEL32.311)
280 UINT32 WINAPI GetWindowsDirectory32A( LPSTR path, UINT32 count )
282 if (path) lstrcpyn32A( path, DIR_WindowsDosDir, count );
283 return strlen( DIR_WindowsDosDir );
287 /***********************************************************************
288 * GetWindowsDirectory32W (KERNEL32.312)
290 UINT32 WINAPI GetWindowsDirectory32W( LPWSTR path, UINT32 count )
292 if (path) lstrcpynAtoW( path, DIR_WindowsDosDir, count );
293 return strlen( DIR_WindowsDosDir );
297 /***********************************************************************
298 * GetSystemDirectory16 (KERNEL.135)
300 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
302 return (UINT16)GetSystemDirectory32A( path, count );
306 /***********************************************************************
307 * GetSystemDirectory32A (KERNEL32.282)
309 UINT32 WINAPI GetSystemDirectory32A( LPSTR path, UINT32 count )
311 if (path) lstrcpyn32A( path, DIR_SystemDosDir, count );
312 return strlen( DIR_SystemDosDir );
316 /***********************************************************************
317 * GetSystemDirectory32W (KERNEL32.283)
319 UINT32 WINAPI GetSystemDirectory32W( LPWSTR path, UINT32 count )
321 if (path) lstrcpynAtoW( path, DIR_SystemDosDir, count );
322 return strlen( DIR_SystemDosDir );
326 /***********************************************************************
327 * CreateDirectory16 (KERNEL.144)
329 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
331 TRACE(file,"(%s,%p)\n", path, dummy );
332 return (BOOL16)CreateDirectory32A( path, NULL );
336 /***********************************************************************
337 * CreateDirectory32A (KERNEL32.39)
339 BOOL32 WINAPI CreateDirectory32A( LPCSTR path,
340 LPSECURITY_ATTRIBUTES lpsecattribs )
342 DOS_FULL_NAME full_name;
344 TRACE(file, "(%s,%p)\n", path, lpsecattribs );
345 if (DOSFS_IsDevice( path ))
347 TRACE(file, "cannot use device '%s'!\n",path);
348 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
351 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
352 if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
361 /***********************************************************************
362 * CreateDirectory32W (KERNEL32.42)
364 BOOL32 WINAPI CreateDirectory32W( LPCWSTR path,
365 LPSECURITY_ATTRIBUTES lpsecattribs )
367 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
368 BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
369 HeapFree( GetProcessHeap(), 0, xpath );
374 /***********************************************************************
375 * CreateDirectoryEx32A (KERNEL32.40)
377 BOOL32 WINAPI CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
378 LPSECURITY_ATTRIBUTES lpsecattribs)
380 return CreateDirectory32A(path,lpsecattribs);
384 /***********************************************************************
385 * CreateDirectoryEx32W (KERNEL32.41)
387 BOOL32 WINAPI CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
388 LPSECURITY_ATTRIBUTES lpsecattribs)
390 return CreateDirectory32W(path,lpsecattribs);
394 /***********************************************************************
395 * RemoveDirectory16 (KERNEL)
397 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
399 return (BOOL16)RemoveDirectory32A( path );
403 /***********************************************************************
404 * RemoveDirectory32A (KERNEL32.437)
406 BOOL32 WINAPI RemoveDirectory32A( LPCSTR path )
408 DOS_FULL_NAME full_name;
410 TRACE(file, "'%s'\n", path );
412 if (DOSFS_IsDevice( path ))
414 TRACE(file, "cannot remove device '%s'!\n", path);
415 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
418 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
419 if (rmdir( full_name.long_name ) == -1)
428 /***********************************************************************
429 * RemoveDirectory32W (KERNEL32.438)
431 BOOL32 WINAPI RemoveDirectory32W( LPCWSTR path )
433 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
434 BOOL32 ret = RemoveDirectory32A( xpath );
435 HeapFree( GetProcessHeap(), 0, xpath );
440 /***********************************************************************
443 * Helper function for DIR_SearchPath.
445 static BOOL32 DIR_TryPath( LPCSTR unix_dir, LPCSTR dos_dir, LPCSTR name,
446 DOS_FULL_NAME *full_name )
448 LPSTR p_l = full_name->long_name + strlen(unix_dir) + 1;
449 LPSTR p_s = full_name->short_name + strlen(dos_dir) + 1;
451 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
452 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
454 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
457 if (!DOSFS_FindUnixName( unix_dir, name, p_l,
458 sizeof(full_name->long_name) - (p_l - full_name->long_name),
459 p_s, DRIVE_GetFlags( dos_dir[0] - 'A' ) ))
461 strcpy( full_name->long_name, unix_dir );
463 strcpy( full_name->short_name, dos_dir );
469 /***********************************************************************
472 * Helper function for DIR_SearchPath.
474 static BOOL32 DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
476 /* FIXME: for now, GetModuleFileName32A can't return more */
477 /* than OFS_MAXPATHNAME. This may change with Win32. */
478 char buffer[OFS_MAXPATHNAME];
481 if (!GetCurrentTask()) return FALSE;
482 GetModuleFileName32A( GetCurrentTask(), buffer, sizeof(buffer) );
483 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
484 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
486 return DOSFS_GetFullName( buffer, TRUE, full_name );
490 /***********************************************************************
493 * Implementation of SearchPath32A. 'win32' specifies whether the search
494 * order is Win16 (module path last) or Win32 (module path first).
496 * FIXME: should return long path names.
498 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
499 DOS_FULL_NAME *full_name, BOOL32 win32 )
507 /* First check the supplied parameters */
509 p = strrchr( name, '.' );
510 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
511 ext = NULL; /* Ignore the specified extension */
512 if ((*name && (name[1] == ':')) ||
513 strchr( name, '/' ) || strchr( name, '\\' ))
514 path = NULL; /* Ignore path if name already contains a path */
515 if (path && !*path) path = NULL; /* Ignore empty path */
518 if (ext) len += strlen(ext);
519 if (path) len += strlen(path) + 1;
521 /* Allocate a buffer for the file name and extension */
525 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
527 SetLastError( ERROR_OUTOFMEMORY );
536 else strcpy( tmp, name );
537 if (ext) strcat( tmp, ext );
541 /* If we have an explicit path, everything's easy */
543 if (path || (*name && (name[1] == ':')) ||
544 strchr( name, '/' ) || strchr( name, '\\' ))
546 ret = DOSFS_GetFullName( name, TRUE, full_name );
550 /* Try the path of the current executable (for Win32 search order) */
552 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
554 /* Try the current directory */
556 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
558 /* Try the Windows directory */
560 if (DIR_TryPath( DIR_WindowsUnixDir, DIR_WindowsDosDir, name, full_name ))
563 /* Try the Windows system directory */
565 if (DIR_TryPath( DIR_SystemUnixDir, DIR_SystemDosDir, name, full_name ))
568 /* Try the path of the current executable (for Win16 search order) */
570 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
572 /* Try all directories in path */
574 for (i = 0; i < DIR_PathElements; i++)
576 if (DIR_TryPath( DIR_UnixPath[i], DIR_DosPath[i], name, full_name ))
582 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
587 /***********************************************************************
588 * SearchPath32A (KERNEL32.447)
590 DWORD WINAPI SearchPath32A( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
591 LPSTR buffer, LPSTR *lastpart )
594 DOS_FULL_NAME full_name;
596 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
597 lstrcpyn32A( buffer, full_name.short_name, buflen );
598 res = full_name.long_name +
599 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
600 while (*res == '/') res++;
603 if (buflen > 3) lstrcpyn32A( buffer + 3, res, buflen - 3 );
604 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
605 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
607 return *res ? strlen(res) + 2 : 3;
611 /***********************************************************************
612 * SearchPath32W (KERNEL32.448)
614 DWORD WINAPI SearchPath32W( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
615 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
619 DOS_FULL_NAME full_name;
621 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
622 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
623 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
624 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
625 HeapFree( GetProcessHeap(), 0, extA );
626 HeapFree( GetProcessHeap(), 0, nameA );
627 HeapFree( GetProcessHeap(), 0, pathA );
630 lstrcpynAtoW( buffer, full_name.short_name, buflen );
631 res = full_name.long_name +
632 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
633 while (*res == '/') res++;
636 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
637 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
640 for (p = *lastpart = buffer; *p; p++)
641 if (*p == '\\') *lastpart = p + 1;
644 return *res ? strlen(res) + 2 : 3;