2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
10 #include <sys/types.h>
14 #include <sys/errno.h>
26 static DOS_FULL_NAME DIR_Windows;
27 static DOS_FULL_NAME DIR_System;
30 /***********************************************************************
33 * Get a path name from the wine.ini file and make sure it is valid.
35 static int DIR_GetPath( const char *keyname, const char *defval,
36 DOS_FULL_NAME *full_name )
38 char path[MAX_PATHNAME_LEN];
39 BY_HANDLE_FILE_INFORMATION info;
41 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
42 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
43 !FILE_Stat( full_name->long_name, &info ) ||
44 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
46 MSG("Invalid path '%s' for %s directory\n", path, keyname);
53 /***********************************************************************
58 char path[MAX_PATHNAME_LEN];
59 DOS_FULL_NAME tmp_dir;
63 if (!getcwd( path, MAX_PATHNAME_LEN ))
65 perror( "Could not get current directory" );
69 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
71 MSG("Warning: could not find DOS drive for cwd %s; "
72 "starting in windows directory.\n", cwd );
76 DRIVE_SetCurrentDrive( drive );
77 DRIVE_Chdir( drive, cwd );
80 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
81 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
82 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
84 PROFILE_UsageWineIni();
87 if (-1 == access( tmp_dir.long_name, W_OK ))
91 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
92 PROFILE_UsageWineIni();
95 MSG("Warning: Access to Temporary Directory failed (%s).\n",
101 drive = DIR_Windows.drive;
102 DRIVE_SetCurrentDrive( drive );
103 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
106 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
107 path, sizeof(path) );
109 /* Set the environment variables */
111 SetEnvironmentVariable32A( "PATH", path );
112 SetEnvironmentVariable32A( "COMSPEC", "c:\\command.com" );
113 SetEnvironmentVariable32A( "TEMP", tmp_dir.short_name );
114 SetEnvironmentVariable32A( "windir", DIR_Windows.short_name );
115 SetEnvironmentVariable32A( "winsysdir", DIR_System.short_name );
117 TRACE(dosfs, "WindowsDir = %s (%s)\n",
118 DIR_Windows.short_name, DIR_Windows.long_name );
119 TRACE(dosfs, "SystemDir = %s (%s)\n",
120 DIR_System.short_name, DIR_System.long_name );
121 TRACE(dosfs, "TempDir = %s (%s)\n",
122 tmp_dir.short_name, tmp_dir.long_name );
123 TRACE(dosfs, "Path = %s\n", path );
124 TRACE(dosfs, "Cwd = %c:\\%s\n",
125 'A' + drive, DRIVE_GetDosCwd( drive ) );
131 /***********************************************************************
132 * GetTempPath32A (KERNEL32.292)
134 UINT32 WINAPI GetTempPath32A( UINT32 count, LPSTR path )
137 if (!(ret = GetEnvironmentVariable32A( "TMP", path, count )))
138 if (!(ret = GetEnvironmentVariable32A( "TEMP", path, count )))
139 if (!(ret = GetCurrentDirectory32A( count, path )))
141 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
150 /***********************************************************************
151 * GetTempPath32W (KERNEL32.293)
153 UINT32 WINAPI GetTempPath32W( UINT32 count, LPWSTR path )
155 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
156 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
158 if (!(ret = GetEnvironmentVariable32W( tmp, path, count )))
159 if (!(ret = GetEnvironmentVariable32W( temp, path, count )))
160 if (!(ret = GetCurrentDirectory32W( count, path )))
162 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
171 /***********************************************************************
172 * DIR_GetWindowsUnixDir
174 UINT32 DIR_GetWindowsUnixDir( LPSTR path, UINT32 count )
176 if (path) lstrcpyn32A( path, DIR_Windows.long_name, count );
177 return strlen( DIR_Windows.long_name );
181 /***********************************************************************
182 * DIR_GetSystemUnixDir
184 UINT32 DIR_GetSystemUnixDir( LPSTR path, UINT32 count )
186 if (path) lstrcpyn32A( path, DIR_System.long_name, count );
187 return strlen( DIR_System.long_name );
191 /***********************************************************************
192 * GetTempDrive (KERNEL.92)
194 BYTE WINAPI GetTempDrive( BYTE ignored )
198 UINT32 len = GetTempPath32A( 0, NULL );
200 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
201 return DRIVE_GetCurrentDrive() + 'A';
203 /* FIXME: apparently Windows does something with the ignored byte */
204 if (!GetTempPath32A( len, buffer )) buffer[0] = 'C';
206 HeapFree( GetProcessHeap(), 0, buffer );
211 UINT32 WINAPI WIN16_GetTempDrive( BYTE ignored )
213 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
217 * AH: ':' - yes, some kernel code even does stosw with
221 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
225 /***********************************************************************
226 * GetWindowsDirectory16 (KERNEL.134)
228 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
230 return (UINT16)GetWindowsDirectory32A( path, count );
234 /***********************************************************************
235 * GetWindowsDirectory32A (KERNEL32.311)
237 UINT32 WINAPI GetWindowsDirectory32A( LPSTR path, UINT32 count )
239 if (path) lstrcpyn32A( path, DIR_Windows.short_name, count );
240 return strlen( DIR_Windows.short_name );
244 /***********************************************************************
245 * GetWindowsDirectory32W (KERNEL32.312)
247 UINT32 WINAPI GetWindowsDirectory32W( LPWSTR path, UINT32 count )
249 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
250 return strlen( DIR_Windows.short_name );
254 /***********************************************************************
255 * GetSystemDirectory16 (KERNEL.135)
257 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
259 return (UINT16)GetSystemDirectory32A( path, count );
263 /***********************************************************************
264 * GetSystemDirectory32A (KERNEL32.282)
266 UINT32 WINAPI GetSystemDirectory32A( LPSTR path, UINT32 count )
268 if (path) lstrcpyn32A( path, DIR_System.short_name, count );
269 return strlen( DIR_System.short_name );
273 /***********************************************************************
274 * GetSystemDirectory32W (KERNEL32.283)
276 UINT32 WINAPI GetSystemDirectory32W( LPWSTR path, UINT32 count )
278 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
279 return strlen( DIR_System.short_name );
283 /***********************************************************************
284 * CreateDirectory16 (KERNEL.144)
286 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
288 TRACE(file,"(%s,%p)\n", path, dummy );
289 return (BOOL16)CreateDirectory32A( path, NULL );
293 /***********************************************************************
294 * CreateDirectory32A (KERNEL32.39)
296 BOOL32 WINAPI CreateDirectory32A( LPCSTR path,
297 LPSECURITY_ATTRIBUTES lpsecattribs )
299 DOS_FULL_NAME full_name;
301 TRACE(file, "(%s,%p)\n", path, lpsecattribs );
302 if (DOSFS_GetDevice( path ))
304 TRACE(file, "cannot use device '%s'!\n",path);
305 SetLastError( ERROR_ACCESS_DENIED );
308 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
309 if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
311 WARN (file, "Errno %i trying to create directory %s.\n", errno, full_name.long_name);
319 /***********************************************************************
320 * CreateDirectory32W (KERNEL32.42)
322 BOOL32 WINAPI CreateDirectory32W( LPCWSTR path,
323 LPSECURITY_ATTRIBUTES lpsecattribs )
325 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
326 BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
327 HeapFree( GetProcessHeap(), 0, xpath );
332 /***********************************************************************
333 * CreateDirectoryEx32A (KERNEL32.40)
335 BOOL32 WINAPI CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
336 LPSECURITY_ATTRIBUTES lpsecattribs)
338 return CreateDirectory32A(path,lpsecattribs);
342 /***********************************************************************
343 * CreateDirectoryEx32W (KERNEL32.41)
345 BOOL32 WINAPI CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
346 LPSECURITY_ATTRIBUTES lpsecattribs)
348 return CreateDirectory32W(path,lpsecattribs);
352 /***********************************************************************
353 * RemoveDirectory16 (KERNEL)
355 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
357 return (BOOL16)RemoveDirectory32A( path );
361 /***********************************************************************
362 * RemoveDirectory32A (KERNEL32.437)
364 BOOL32 WINAPI RemoveDirectory32A( LPCSTR path )
366 DOS_FULL_NAME full_name;
368 TRACE(file, "'%s'\n", path );
370 if (DOSFS_GetDevice( path ))
372 TRACE(file, "cannot remove device '%s'!\n", path);
373 SetLastError( ERROR_FILE_NOT_FOUND );
376 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
377 if (rmdir( full_name.long_name ) == -1)
386 /***********************************************************************
387 * RemoveDirectory32W (KERNEL32.438)
389 BOOL32 WINAPI RemoveDirectory32W( LPCWSTR path )
391 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
392 BOOL32 ret = RemoveDirectory32A( xpath );
393 HeapFree( GetProcessHeap(), 0, xpath );
398 /***********************************************************************
401 * Helper function for DIR_SearchPath.
403 static BOOL32 DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
404 DOS_FULL_NAME *full_name )
406 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
407 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
409 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
410 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
412 SetLastError( ERROR_PATH_NOT_FOUND );
415 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
416 sizeof(full_name->long_name) - (p_l - full_name->long_name),
417 p_s, DRIVE_GetFlags( dir->drive ) ))
419 strcpy( full_name->long_name, dir->long_name );
421 strcpy( full_name->short_name, dir->short_name );
427 /***********************************************************************
428 * DIR_TryEnvironmentPath
430 * Helper function for DIR_SearchPath.
432 static BOOL32 DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
434 LPSTR path, next, buffer;
436 INT32 len = strlen(name);
437 DWORD size = GetEnvironmentVariable32A( "PATH", NULL, 0 );
439 if (!size) return FALSE;
440 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
441 if (!GetEnvironmentVariable32A( "PATH", path, size )) goto done;
446 while (*cur == ';') cur++;
448 next = strchr( cur, ';' );
449 if (next) *next++ = '\0';
450 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
452 strcpy( buffer, cur );
453 strcat( buffer, "\\" );
454 strcat( buffer, name );
455 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
456 HeapFree( GetProcessHeap(), 0, buffer );
460 HeapFree( GetProcessHeap(), 0, path );
465 /***********************************************************************
468 * Helper function for DIR_SearchPath.
470 static BOOL32 DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
472 PDB32 *pdb = PROCESS_Current();
474 /* FIXME: for now, GetModuleFileName32A can't return more */
475 /* than OFS_MAXPATHNAME. This may change with Win32. */
477 char buffer[OFS_MAXPATHNAME];
480 if (pdb->flags & PDB32_WIN16_PROC) {
481 if (!GetCurrentTask()) return FALSE;
482 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
485 if (!GetModuleFileName32A( 0, buffer, sizeof(buffer) ))
488 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
489 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
491 return DOSFS_GetFullName( buffer, TRUE, full_name );
495 /***********************************************************************
498 * Implementation of SearchPath32A. 'win32' specifies whether the search
499 * order is Win16 (module path last) or Win32 (module path first).
501 * FIXME: should return long path names.
503 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
504 DOS_FULL_NAME *full_name, BOOL32 win32 )
511 /* First check the supplied parameters */
513 p = strrchr( name, '.' );
514 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
515 ext = NULL; /* Ignore the specified extension */
516 if ((*name && (name[1] == ':')) ||
517 strchr( name, '/' ) || strchr( name, '\\' ))
518 path = NULL; /* Ignore path if name already contains a path */
519 if (path && !*path) path = NULL; /* Ignore empty path */
522 if (ext) len += strlen(ext);
523 if (path) len += strlen(path) + 1;
525 /* Allocate a buffer for the file name and extension */
529 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
531 SetLastError( ERROR_OUTOFMEMORY );
540 else strcpy( tmp, name );
541 if (ext) strcat( tmp, ext );
545 /* If we have an explicit path, everything's easy */
547 if (path || (*name && (name[1] == ':')) ||
548 strchr( name, '/' ) || strchr( name, '\\' ))
550 ret = DOSFS_GetFullName( name, TRUE, full_name );
554 /* Try the path of the current executable (for Win32 search order) */
556 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
558 /* Try the current directory */
560 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
562 /* Try the Windows directory */
564 if (DIR_TryPath( &DIR_Windows, name, full_name ))
567 /* Try the Windows system directory */
569 if (DIR_TryPath( &DIR_System, name, full_name ))
572 /* Try the path of the current executable (for Win16 search order) */
574 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
576 /* Try all directories in path */
578 ret = DIR_TryEnvironmentPath( name, full_name );
581 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
586 /***********************************************************************
587 * SearchPath32A [KERNEL32.447]
589 * Searches for a specified file in the search path.
592 * path [I] Path to search
593 * name [I] Filename to search for.
594 * ext [I] File extension to append to file name. The first
595 * character must be a period. This parameter is
596 * specified only if the filename given does not
597 * contain an extension.
598 * buflen [I] size of buffer, in characters
599 * buffer [O] buffer for found filename
600 * lastpart [O] address of pointer to last used character in
601 * buffer (the final '\')
604 * Success: length of string copied into buffer, not including
605 * terminating null character. If the filename found is
606 * longer than the length of the buffer, the length of the
607 * filename is returned.
611 * Should call SetLastError(but currently doesn't).
613 DWORD WINAPI SearchPath32A( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
614 LPSTR buffer, LPSTR *lastpart )
617 DOS_FULL_NAME full_name;
619 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
620 lstrcpyn32A( buffer, full_name.short_name, buflen );
621 res = full_name.long_name +
622 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
623 while (*res == '/') res++;
626 if (buflen > 3) lstrcpyn32A( buffer + 3, res, buflen - 3 );
627 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
628 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
630 TRACE(dosfs, "Returning %d\n", (*res ? strlen(res) + 2 : 3));
631 return *res ? strlen(res) + 2 : 3;
635 /***********************************************************************
636 * SearchPath32W (KERNEL32.448)
638 DWORD WINAPI SearchPath32W( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
639 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
643 DOS_FULL_NAME full_name;
645 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
646 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
647 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
648 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
649 HeapFree( GetProcessHeap(), 0, extA );
650 HeapFree( GetProcessHeap(), 0, nameA );
651 HeapFree( GetProcessHeap(), 0, pathA );
654 lstrcpynAtoW( buffer, full_name.short_name, buflen );
655 res = full_name.long_name +
656 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
657 while (*res == '/') res++;
660 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
661 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
664 for (p = *lastpart = buffer; *p; p++)
665 if (*p == '\\') *lastpart = p + 1;
668 return *res ? strlen(res) + 2 : 3;