Release 980726
[wine/multimedia.git] / files / directory.c
blob9835a7d01c6137b50d86e634821b324a08707e26
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>
15 #include "windows.h"
16 #include "winerror.h"
17 #include "process.h"
18 #include "drive.h"
19 #include "file.h"
20 #include "heap.h"
21 #include "msdos.h"
22 #include "options.h"
23 #include "debug.h"
25 static DOS_FULL_NAME DIR_Windows;
26 static DOS_FULL_NAME DIR_System;
29 /***********************************************************************
30 * DIR_GetPath
32 * Get a path name from the wine.ini file and make sure it is valid.
34 static int DIR_GetPath( const char *keyname, const char *defval,
35 DOS_FULL_NAME *full_name )
37 char path[MAX_PATHNAME_LEN];
38 BY_HANDLE_FILE_INFORMATION info;
40 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
41 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
42 !FILE_Stat( full_name->long_name, &info ) ||
43 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
45 MSG("Invalid path '%s' for %s directory\n", path, keyname);
46 return 0;
48 return 1;
52 /***********************************************************************
53 * DIR_Init
55 int DIR_Init(void)
57 char path[MAX_PATHNAME_LEN];
58 DOS_FULL_NAME tmp_dir;
59 int drive;
60 const char *cwd;
62 if (!getcwd( path, MAX_PATHNAME_LEN ))
64 perror( "Could not get current directory" );
65 return 0;
67 cwd = path;
68 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
70 MSG("Warning: could not find DOS drive for cwd %s; "
71 "starting in windows directory.\n", cwd );
73 else
75 DRIVE_SetCurrentDrive( drive );
76 DRIVE_Chdir( drive, cwd );
79 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )))
80 return 0;
81 if (!(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )))
82 return 0;
83 if (!(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
84 return 0;
85 if (-1 == access( tmp_dir.long_name, W_OK ))
87 if (errno==EACCES)
88 MSG("Warning: The Temporary Directory (as specified in wine.conf) is NOT writeable. Please check your configuration.\n");
89 else
90 MSG("Warning: Access to Temporary Directory failed (%s).\n",
91 strerror(errno));
94 if (drive == -1)
96 drive = DIR_Windows.drive;
97 DRIVE_SetCurrentDrive( drive );
98 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
101 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
102 path, sizeof(path) );
104 /* Set the environment variables */
106 SetEnvironmentVariable32A( "PATH", path );
107 SetEnvironmentVariable32A( "TEMP", tmp_dir.short_name );
108 SetEnvironmentVariable32A( "windir", DIR_Windows.short_name );
109 SetEnvironmentVariable32A( "winsysdir", DIR_System.short_name );
111 TRACE(dosfs, "WindowsDir = %s (%s)\n",
112 DIR_Windows.short_name, DIR_Windows.long_name );
113 TRACE(dosfs, "SystemDir = %s (%s)\n",
114 DIR_System.short_name, DIR_System.long_name );
115 TRACE(dosfs, "TempDir = %s (%s)\n",
116 tmp_dir.short_name, tmp_dir.long_name );
117 TRACE(dosfs, "Path = %s\n", path );
118 TRACE(dosfs, "Cwd = %c:\\%s\n",
119 'A' + drive, DRIVE_GetDosCwd( drive ) );
121 return 1;
125 /***********************************************************************
126 * GetTempPath32A (KERNEL32.292)
128 UINT32 WINAPI GetTempPath32A( UINT32 count, LPSTR path )
130 UINT32 ret;
131 if (!(ret = GetEnvironmentVariable32A( "TMP", path, count )))
132 if (!(ret = GetEnvironmentVariable32A( "TEMP", path, count )))
133 if (!(ret = GetCurrentDirectory32A( count, path )))
134 return 0;
135 if ((ret < count - 1) && (path[ret-1] != '\\'))
137 path[ret++] = '\\';
138 path[ret] = '\0';
140 return ret;
144 /***********************************************************************
145 * GetTempPath32W (KERNEL32.293)
147 UINT32 WINAPI GetTempPath32W( UINT32 count, LPWSTR path )
149 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
150 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
151 UINT32 ret;
152 if (!(ret = GetEnvironmentVariable32W( tmp, path, count )))
153 if (!(ret = GetEnvironmentVariable32W( temp, path, count )))
154 if (!(ret = GetCurrentDirectory32W( count, path )))
155 return 0;
156 if ((ret < count - 1) && (path[ret-1] != '\\'))
158 path[ret++] = '\\';
159 path[ret] = '\0';
161 return ret;
165 /***********************************************************************
166 * DIR_GetWindowsUnixDir
168 UINT32 DIR_GetWindowsUnixDir( LPSTR path, UINT32 count )
170 if (path) lstrcpyn32A( path, DIR_Windows.long_name, count );
171 return strlen( DIR_Windows.long_name );
175 /***********************************************************************
176 * DIR_GetSystemUnixDir
178 UINT32 DIR_GetSystemUnixDir( LPSTR path, UINT32 count )
180 if (path) lstrcpyn32A( path, DIR_System.long_name, count );
181 return strlen( DIR_System.long_name );
185 /***********************************************************************
186 * GetTempDrive (KERNEL.92)
188 BYTE WINAPI GetTempDrive( BYTE ignored )
190 char buffer[2];
191 /* FIXME: apparently Windows does something with the ignored byte */
192 if (!GetTempPath32A( sizeof(buffer), buffer )) buffer[0] = 'C';
193 return toupper(buffer[0]);
197 UINT32 WINAPI WIN16_GetTempDrive( BYTE ignored )
199 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
201 * returns:
202 * AL: driveletter
203 * AH: ':' - yes, some kernel code even does stosw with
204 * the returned AX.
205 * DX: 1 for success
207 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
211 /***********************************************************************
212 * GetWindowsDirectory16 (KERNEL.134)
214 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
216 return (UINT16)GetWindowsDirectory32A( path, count );
220 /***********************************************************************
221 * GetWindowsDirectory32A (KERNEL32.311)
223 UINT32 WINAPI GetWindowsDirectory32A( LPSTR path, UINT32 count )
225 if (path) lstrcpyn32A( path, DIR_Windows.short_name, count );
226 return strlen( DIR_Windows.short_name );
230 /***********************************************************************
231 * GetWindowsDirectory32W (KERNEL32.312)
233 UINT32 WINAPI GetWindowsDirectory32W( LPWSTR path, UINT32 count )
235 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
236 return strlen( DIR_Windows.short_name );
240 /***********************************************************************
241 * GetSystemDirectory16 (KERNEL.135)
243 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
245 return (UINT16)GetSystemDirectory32A( path, count );
249 /***********************************************************************
250 * GetSystemDirectory32A (KERNEL32.282)
252 UINT32 WINAPI GetSystemDirectory32A( LPSTR path, UINT32 count )
254 if (path) lstrcpyn32A( path, DIR_System.short_name, count );
255 return strlen( DIR_System.short_name );
259 /***********************************************************************
260 * GetSystemDirectory32W (KERNEL32.283)
262 UINT32 WINAPI GetSystemDirectory32W( LPWSTR path, UINT32 count )
264 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
265 return strlen( DIR_System.short_name );
269 /***********************************************************************
270 * CreateDirectory16 (KERNEL.144)
272 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
274 TRACE(file,"(%s,%p)\n", path, dummy );
275 return (BOOL16)CreateDirectory32A( path, NULL );
279 /***********************************************************************
280 * CreateDirectory32A (KERNEL32.39)
282 BOOL32 WINAPI CreateDirectory32A( LPCSTR path,
283 LPSECURITY_ATTRIBUTES lpsecattribs )
285 DOS_FULL_NAME full_name;
287 TRACE(file, "(%s,%p)\n", path, lpsecattribs );
288 if (DOSFS_GetDevice( path ))
290 TRACE(file, "cannot use device '%s'!\n",path);
291 DOS_ERROR( ER_AccessDenied, EC_AccessDenied, SA_Abort, EL_Disk );
292 return FALSE;
294 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
295 if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
297 WARN (file, "Errno %i trying to create directory %s.\n", errno, full_name.long_name);
298 FILE_SetDosError();
299 return FALSE;
301 return TRUE;
305 /***********************************************************************
306 * CreateDirectory32W (KERNEL32.42)
308 BOOL32 WINAPI CreateDirectory32W( LPCWSTR path,
309 LPSECURITY_ATTRIBUTES lpsecattribs )
311 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
312 BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
313 HeapFree( GetProcessHeap(), 0, xpath );
314 return ret;
318 /***********************************************************************
319 * CreateDirectoryEx32A (KERNEL32.40)
321 BOOL32 WINAPI CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
322 LPSECURITY_ATTRIBUTES lpsecattribs)
324 return CreateDirectory32A(path,lpsecattribs);
328 /***********************************************************************
329 * CreateDirectoryEx32W (KERNEL32.41)
331 BOOL32 WINAPI CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
332 LPSECURITY_ATTRIBUTES lpsecattribs)
334 return CreateDirectory32W(path,lpsecattribs);
338 /***********************************************************************
339 * RemoveDirectory16 (KERNEL)
341 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
343 return (BOOL16)RemoveDirectory32A( path );
347 /***********************************************************************
348 * RemoveDirectory32A (KERNEL32.437)
350 BOOL32 WINAPI RemoveDirectory32A( LPCSTR path )
352 DOS_FULL_NAME full_name;
354 TRACE(file, "'%s'\n", path );
356 if (DOSFS_GetDevice( path ))
358 TRACE(file, "cannot remove device '%s'!\n", path);
359 DOS_ERROR( ER_FileNotFound, EC_NotFound, SA_Abort, EL_Disk );
360 return FALSE;
362 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
363 if (rmdir( full_name.long_name ) == -1)
365 FILE_SetDosError();
366 return FALSE;
368 return TRUE;
372 /***********************************************************************
373 * RemoveDirectory32W (KERNEL32.438)
375 BOOL32 WINAPI RemoveDirectory32W( LPCWSTR path )
377 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
378 BOOL32 ret = RemoveDirectory32A( xpath );
379 HeapFree( GetProcessHeap(), 0, xpath );
380 return ret;
384 /***********************************************************************
385 * DIR_TryPath
387 * Helper function for DIR_SearchPath.
389 static BOOL32 DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
390 DOS_FULL_NAME *full_name )
392 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
393 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
395 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
396 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
398 DOS_ERROR( ER_PathNotFound, EC_NotFound, SA_Abort, EL_Disk );
399 return FALSE;
401 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
402 sizeof(full_name->long_name) - (p_l - full_name->long_name),
403 p_s, DRIVE_GetFlags( dir->drive ) ))
404 return FALSE;
405 strcpy( full_name->long_name, dir->long_name );
406 p_l[-1] = '/';
407 strcpy( full_name->short_name, dir->short_name );
408 p_s[-1] = '\\';
409 return TRUE;
413 /***********************************************************************
414 * DIR_TryEnvironmentPath
416 * Helper function for DIR_SearchPath.
418 static BOOL32 DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
420 LPSTR path, next, buffer;
421 BOOL32 ret = FALSE;
422 INT32 len = strlen(name);
423 DWORD size = GetEnvironmentVariable32A( "PATH", NULL, 0 );
425 if (!size) return FALSE;
426 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
427 if (!GetEnvironmentVariable32A( "PATH", path, size )) goto done;
428 next = path;
429 while (!ret && next)
431 LPSTR cur = next;
432 while (*cur == ';') cur++;
433 if (!*cur) break;
434 next = strchr( cur, ';' );
435 if (next) *next++ = '\0';
436 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
437 goto done;
438 strcpy( buffer, cur );
439 strcat( buffer, "\\" );
440 strcat( buffer, name );
441 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
442 HeapFree( GetProcessHeap(), 0, buffer );
445 done:
446 HeapFree( GetProcessHeap(), 0, path );
447 return ret;
451 /***********************************************************************
452 * DIR_TryModulePath
454 * Helper function for DIR_SearchPath.
456 static BOOL32 DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
458 PDB32 *pdb = PROCESS_Current();
460 /* FIXME: for now, GetModuleFileName32A can't return more */
461 /* than OFS_MAXPATHNAME. This may change with Win32. */
463 char buffer[OFS_MAXPATHNAME];
464 LPSTR p;
466 if (pdb->flags & PDB32_WIN16_PROC) {
467 if (!GetCurrentTask()) return FALSE;
468 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
469 buffer[0]='\0';
470 } else {
471 if (!GetModuleFileName32A( 0, buffer, sizeof(buffer) ))
472 buffer[0]='\0';
474 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
475 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
476 strcpy( p, name );
477 return DOSFS_GetFullName( buffer, TRUE, full_name );
481 /***********************************************************************
482 * DIR_SearchPath
484 * Implementation of SearchPath32A. 'win32' specifies whether the search
485 * order is Win16 (module path last) or Win32 (module path first).
487 * FIXME: should return long path names.
489 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
490 DOS_FULL_NAME *full_name, BOOL32 win32 )
492 DWORD len;
493 LPCSTR p;
494 LPSTR tmp = NULL;
495 BOOL32 ret = TRUE;
497 /* First check the supplied parameters */
499 p = strrchr( name, '.' );
500 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
501 ext = NULL; /* Ignore the specified extension */
502 if ((*name && (name[1] == ':')) ||
503 strchr( name, '/' ) || strchr( name, '\\' ))
504 path = NULL; /* Ignore path if name already contains a path */
505 if (path && !*path) path = NULL; /* Ignore empty path */
507 len = strlen(name);
508 if (ext) len += strlen(ext);
509 if (path) len += strlen(path) + 1;
511 /* Allocate a buffer for the file name and extension */
513 if (path || ext)
515 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
517 SetLastError( ERROR_OUTOFMEMORY );
518 return 0;
520 if (path)
522 strcpy( tmp, path );
523 strcat( tmp, "\\" );
524 strcat( tmp, name );
526 else strcpy( tmp, name );
527 if (ext) strcat( tmp, ext );
528 name = tmp;
531 /* If we have an explicit path, everything's easy */
533 if (path || (*name && (name[1] == ':')) ||
534 strchr( name, '/' ) || strchr( name, '\\' ))
536 ret = DOSFS_GetFullName( name, TRUE, full_name );
537 goto done;
540 /* Try the path of the current executable (for Win32 search order) */
542 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
544 /* Try the current directory */
546 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
548 /* Try the Windows directory */
550 if (DIR_TryPath( &DIR_Windows, name, full_name ))
551 goto done;
553 /* Try the Windows system directory */
555 if (DIR_TryPath( &DIR_System, name, full_name ))
556 goto done;
558 /* Try the path of the current executable (for Win16 search order) */
560 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
562 /* Try all directories in path */
564 ret = DIR_TryEnvironmentPath( name, full_name );
566 done:
567 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
568 return ret;
572 /***********************************************************************
573 * SearchPath32A [KERNEL32.447]
575 * Searches for a specified file in the search path.
577 * PARAMS
578 * path [I] Path to search
579 * name [I] Filename to search for.
580 * ext [I] File extension to append to file name. The first
581 * character must be a period. This parameter is
582 * specified only if the filename given does not
583 * contain an extension.
584 * buflen [I] size of buffer, in characters
585 * buffer [O] buffer for found filename
586 * lastpart [O] address of pointer to last used character in
587 * buffer (the final '\')
589 * RETURNS
590 * Success: length of string copied into buffer, not including
591 * terminating null character. If the filename found is
592 * longer than the length of the buffer, the length of the
593 * filename is returned.
594 * Failure: Zero
596 * NOTES
597 * Should call SetLastError(but currently doesn't).
599 DWORD WINAPI SearchPath32A( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
600 LPSTR buffer, LPSTR *lastpart )
602 LPSTR p, res;
603 DOS_FULL_NAME full_name;
605 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
606 lstrcpyn32A( buffer, full_name.short_name, buflen );
607 res = full_name.long_name +
608 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
609 while (*res == '/') res++;
610 if (buflen)
612 if (buflen > 3) lstrcpyn32A( buffer + 3, res, buflen - 3 );
613 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
614 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
616 TRACE(dosfs, "Returning %d\n", (*res ? strlen(res) + 2 : 3));
617 return *res ? strlen(res) + 2 : 3;
621 /***********************************************************************
622 * SearchPath32W (KERNEL32.448)
624 DWORD WINAPI SearchPath32W( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
625 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
627 LPWSTR p;
628 LPSTR res;
629 DOS_FULL_NAME full_name;
631 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
632 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
633 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
634 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
635 HeapFree( GetProcessHeap(), 0, extA );
636 HeapFree( GetProcessHeap(), 0, nameA );
637 HeapFree( GetProcessHeap(), 0, pathA );
638 if (!ret) return 0;
640 lstrcpynAtoW( buffer, full_name.short_name, buflen );
641 res = full_name.long_name +
642 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
643 while (*res == '/') res++;
644 if (buflen)
646 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
647 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
648 if (lastpart)
650 for (p = *lastpart = buffer; *p; p++)
651 if (*p == '\\') *lastpart = p + 1;
654 return *res ? strlen(res) + 2 : 3;