Now inits the policy cache when shell32 is first instantiated.
[wine/dcerpc.git] / files / directory.c
blob5966115de01da85d3e086abbd43207dc6f003892
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 #ifdef HAVE_SYS_ERRNO_H
15 #include <sys/errno.h>
16 #endif
18 #include "winbase.h"
19 #include "wine/winbase16.h"
20 #include "wine/winestring.h"
21 #include "wine/winuser16.h"
22 #include "winerror.h"
23 #include "process.h"
24 #include "drive.h"
25 #include "file.h"
26 #include "heap.h"
27 #include "msdos.h"
28 #include "options.h"
29 #include "debugtools.h"
31 DECLARE_DEBUG_CHANNEL(dosfs)
32 DECLARE_DEBUG_CHANNEL(file)
34 static DOS_FULL_NAME DIR_Windows;
35 static DOS_FULL_NAME DIR_System;
38 /***********************************************************************
39 * DIR_GetPath
41 * Get a path name from the wine.ini file and make sure it is valid.
43 static int DIR_GetPath( const char *keyname, const char *defval,
44 DOS_FULL_NAME *full_name )
46 char path[MAX_PATHNAME_LEN];
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 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
55 return 0;
57 return 1;
61 /***********************************************************************
62 * DIR_Init
64 int DIR_Init(void)
66 char path[MAX_PATHNAME_LEN];
67 DOS_FULL_NAME tmp_dir;
68 int drive;
69 const char *cwd;
71 if (!getcwd( path, MAX_PATHNAME_LEN ))
73 perror( "Could not get current directory" );
74 return 0;
76 cwd = path;
77 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
79 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
80 "for current working directory %s; "
81 "starting in windows directory.\n", cwd );
83 else
85 DRIVE_SetCurrentDrive( drive );
86 DRIVE_Chdir( drive, cwd );
89 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
90 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
91 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
93 PROFILE_UsageWineIni();
94 return 0;
96 if (-1 == access( tmp_dir.long_name, W_OK ))
98 if (errno==EACCES)
100 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
101 PROFILE_UsageWineIni();
103 else
104 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
105 strerror(errno));
108 if (drive == -1)
110 drive = DIR_Windows.drive;
111 DRIVE_SetCurrentDrive( drive );
112 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
115 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
116 path, sizeof(path) );
118 /* Set the environment variables */
120 SetEnvironmentVariableA( "PATH", path );
121 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
122 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
123 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
124 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
126 TRACE_(dosfs)("WindowsDir = %s (%s)\n",
127 DIR_Windows.short_name, DIR_Windows.long_name );
128 TRACE_(dosfs)("SystemDir = %s (%s)\n",
129 DIR_System.short_name, DIR_System.long_name );
130 TRACE_(dosfs)("TempDir = %s (%s)\n",
131 tmp_dir.short_name, tmp_dir.long_name );
132 TRACE_(dosfs)("Path = %s\n", path );
133 TRACE_(dosfs)("Cwd = %c:\\%s\n",
134 'A' + drive, DRIVE_GetDosCwd( drive ) );
136 return 1;
140 /***********************************************************************
141 * GetTempPath32A (KERNEL32.292)
143 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
145 UINT ret;
146 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
147 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
148 if (!(ret = GetCurrentDirectoryA( count, path )))
149 return 0;
150 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
152 path[ret++] = '\\';
153 path[ret] = '\0';
155 return ret;
159 /***********************************************************************
160 * GetTempPath32W (KERNEL32.293)
162 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
164 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
165 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
166 UINT ret;
167 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
168 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
169 if (!(ret = GetCurrentDirectoryW( count, path )))
170 return 0;
171 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
173 path[ret++] = '\\';
174 path[ret] = '\0';
176 return ret;
180 /***********************************************************************
181 * DIR_GetWindowsUnixDir
183 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
185 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
186 return strlen( DIR_Windows.long_name );
190 /***********************************************************************
191 * DIR_GetSystemUnixDir
193 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
195 if (path) lstrcpynA( path, DIR_System.long_name, count );
196 return strlen( DIR_System.long_name );
200 /***********************************************************************
201 * GetTempDrive (KERNEL.92)
203 BYTE WINAPI GetTempDrive( BYTE ignored )
205 char *buffer;
206 BYTE ret;
207 UINT len = GetTempPathA( 0, NULL );
209 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
210 return DRIVE_GetCurrentDrive() + 'A';
212 /* FIXME: apparently Windows does something with the ignored byte */
213 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
214 ret = buffer[0];
215 HeapFree( GetProcessHeap(), 0, buffer );
216 return toupper(ret);
220 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
222 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
224 * returns:
225 * AL: driveletter
226 * AH: ':' - yes, some kernel code even does stosw with
227 * the returned AX.
228 * DX: 1 for success
230 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
234 /***********************************************************************
235 * GetWindowsDirectory16 (KERNEL.134)
237 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
239 return (UINT16)GetWindowsDirectoryA( path, count );
243 /***********************************************************************
244 * GetWindowsDirectory32A (KERNEL32.311)
246 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
248 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
249 return strlen( DIR_Windows.short_name );
253 /***********************************************************************
254 * GetWindowsDirectory32W (KERNEL32.312)
256 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
258 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
259 return strlen( DIR_Windows.short_name );
263 /***********************************************************************
264 * GetSystemDirectory16 (KERNEL.135)
266 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
268 return (UINT16)GetSystemDirectoryA( path, count );
272 /***********************************************************************
273 * GetSystemDirectory32A (KERNEL32.282)
275 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
277 if (path) lstrcpynA( path, DIR_System.short_name, count );
278 return strlen( DIR_System.short_name );
282 /***********************************************************************
283 * GetSystemDirectory32W (KERNEL32.283)
285 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
287 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
288 return strlen( DIR_System.short_name );
292 /***********************************************************************
293 * CreateDirectory16 (KERNEL.144)
295 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
297 TRACE_(file)("(%s,%p)\n", path, dummy );
298 return (BOOL16)CreateDirectoryA( path, NULL );
302 /***********************************************************************
303 * CreateDirectory32A (KERNEL32.39)
304 * RETURNS:
305 * TRUE : success
306 * FALSE : failure
307 * ERROR_DISK_FULL: on full disk
308 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
309 * ERROR_ACCESS_DENIED: on permission problems
310 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
312 BOOL WINAPI CreateDirectoryA( LPCSTR path,
313 LPSECURITY_ATTRIBUTES lpsecattribs )
315 DOS_FULL_NAME full_name;
317 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
318 if (DOSFS_GetDevice( path ))
320 TRACE_(file)("cannot use device '%s'!\n",path);
321 SetLastError( ERROR_ACCESS_DENIED );
322 return FALSE;
324 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
325 if (mkdir( full_name.long_name, 0777 ) == -1) {
326 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
327 /* the FILE_SetDosError() generated error codes don't match the
328 * CreateDirectory ones for some errnos */
329 switch (errno) {
330 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
331 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
332 default: FILE_SetDosError();break;
334 return FALSE;
336 return TRUE;
340 /***********************************************************************
341 * CreateDirectory32W (KERNEL32.42)
343 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
344 LPSECURITY_ATTRIBUTES lpsecattribs )
346 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
347 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
348 HeapFree( GetProcessHeap(), 0, xpath );
349 return ret;
353 /***********************************************************************
354 * CreateDirectoryEx32A (KERNEL32.40)
356 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
357 LPSECURITY_ATTRIBUTES lpsecattribs)
359 return CreateDirectoryA(path,lpsecattribs);
363 /***********************************************************************
364 * CreateDirectoryEx32W (KERNEL32.41)
366 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
367 LPSECURITY_ATTRIBUTES lpsecattribs)
369 return CreateDirectoryW(path,lpsecattribs);
373 /***********************************************************************
374 * RemoveDirectory16 (KERNEL)
376 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
378 return (BOOL16)RemoveDirectoryA( path );
382 /***********************************************************************
383 * RemoveDirectory32A (KERNEL32.437)
385 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
387 DOS_FULL_NAME full_name;
389 TRACE_(file)("'%s'\n", path );
391 if (DOSFS_GetDevice( path ))
393 TRACE_(file)("cannot remove device '%s'!\n", path);
394 SetLastError( ERROR_FILE_NOT_FOUND );
395 return FALSE;
397 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
398 if (rmdir( full_name.long_name ) == -1)
400 FILE_SetDosError();
401 return FALSE;
403 return TRUE;
407 /***********************************************************************
408 * RemoveDirectory32W (KERNEL32.438)
410 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
412 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
413 BOOL ret = RemoveDirectoryA( xpath );
414 HeapFree( GetProcessHeap(), 0, xpath );
415 return ret;
419 /***********************************************************************
420 * DIR_TryPath
422 * Helper function for DIR_SearchPath.
424 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
425 DOS_FULL_NAME *full_name )
427 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
428 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
430 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
431 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
433 SetLastError( ERROR_PATH_NOT_FOUND );
434 return FALSE;
436 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
437 sizeof(full_name->long_name) - (p_l - full_name->long_name),
438 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
439 return FALSE;
440 strcpy( full_name->long_name, dir->long_name );
441 p_l[-1] = '/';
442 strcpy( full_name->short_name, dir->short_name );
443 p_s[-1] = '\\';
444 return TRUE;
448 /***********************************************************************
449 * DIR_TryEnvironmentPath
451 * Helper function for DIR_SearchPath.
453 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
455 LPSTR path, next, buffer;
456 BOOL ret = FALSE;
457 INT len = strlen(name);
458 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
460 if (!size) return FALSE;
461 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
462 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
463 next = path;
464 while (!ret && next)
466 LPSTR cur = next;
467 while (*cur == ';') cur++;
468 if (!*cur) break;
469 next = strchr( cur, ';' );
470 if (next) *next++ = '\0';
471 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
472 goto done;
473 strcpy( buffer, cur );
474 strcat( buffer, "\\" );
475 strcat( buffer, name );
476 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
477 HeapFree( GetProcessHeap(), 0, buffer );
480 done:
481 HeapFree( GetProcessHeap(), 0, path );
482 return ret;
486 /***********************************************************************
487 * DIR_TryModulePath
489 * Helper function for DIR_SearchPath.
491 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
493 PDB *pdb = PROCESS_Current();
495 /* FIXME: for now, GetModuleFileName32A can't return more */
496 /* than OFS_MAXPATHNAME. This may change with Win32. */
498 char buffer[OFS_MAXPATHNAME];
499 LPSTR p;
501 if (pdb->flags & PDB32_WIN16_PROC) {
502 if (!GetCurrentTask()) return FALSE;
503 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
504 buffer[0]='\0';
505 } else {
506 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
507 buffer[0]='\0';
509 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
510 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
511 strcpy( p, name );
512 return DOSFS_GetFullName( buffer, TRUE, full_name );
516 /***********************************************************************
517 * DIR_SearchPath
519 * Implementation of SearchPath32A. 'win32' specifies whether the search
520 * order is Win16 (module path last) or Win32 (module path first).
522 * FIXME: should return long path names.
524 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
525 DOS_FULL_NAME *full_name, BOOL win32 )
527 DWORD len;
528 LPCSTR p;
529 LPSTR tmp = NULL;
530 BOOL ret = TRUE;
532 /* First check the supplied parameters */
534 p = strrchr( name, '.' );
535 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
536 ext = NULL; /* Ignore the specified extension */
537 if ((*name && (name[1] == ':')) ||
538 strchr( name, '/' ) || strchr( name, '\\' ))
539 path = NULL; /* Ignore path if name already contains a path */
540 if (path && !*path) path = NULL; /* Ignore empty path */
542 len = strlen(name);
543 if (ext) len += strlen(ext);
544 if (path) len += strlen(path) + 1;
546 /* Allocate a buffer for the file name and extension */
548 if (path || ext)
550 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
552 SetLastError( ERROR_OUTOFMEMORY );
553 return 0;
555 if (path)
557 strcpy( tmp, path );
558 strcat( tmp, "\\" );
559 strcat( tmp, name );
561 else strcpy( tmp, name );
562 if (ext) strcat( tmp, ext );
563 name = tmp;
566 /* If we have an explicit path, everything's easy */
568 if (path || (*name && (name[1] == ':')) ||
569 strchr( name, '/' ) || strchr( name, '\\' ))
571 ret = DOSFS_GetFullName( name, TRUE, full_name );
572 goto done;
575 /* Try the path of the current executable (for Win32 search order) */
577 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
579 /* Try the current directory */
581 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
583 /* Try the Windows system directory */
585 if (DIR_TryPath( &DIR_System, name, full_name ))
586 goto done;
588 /* Try the Windows directory */
590 if (DIR_TryPath( &DIR_Windows, name, full_name ))
591 goto done;
593 /* Try the path of the current executable (for Win16 search order) */
595 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
597 /* Try all directories in path */
599 ret = DIR_TryEnvironmentPath( name, full_name );
601 done:
602 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
603 return ret;
607 /***********************************************************************
608 * SearchPath32A [KERNEL32.447]
610 * Searches for a specified file in the search path.
612 * PARAMS
613 * path [I] Path to search
614 * name [I] Filename to search for.
615 * ext [I] File extension to append to file name. The first
616 * character must be a period. This parameter is
617 * specified only if the filename given does not
618 * contain an extension.
619 * buflen [I] size of buffer, in characters
620 * buffer [O] buffer for found filename
621 * lastpart [O] address of pointer to last used character in
622 * buffer (the final '\')
624 * RETURNS
625 * Success: length of string copied into buffer, not including
626 * terminating null character. If the filename found is
627 * longer than the length of the buffer, the length of the
628 * filename is returned.
629 * Failure: Zero
631 * NOTES
632 * Should call SetLastError(but currently doesn't).
634 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
635 LPSTR buffer, LPSTR *lastpart )
637 LPSTR p, res;
638 DOS_FULL_NAME full_name;
640 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
641 lstrcpynA( buffer, full_name.short_name, buflen );
642 res = full_name.long_name +
643 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
644 while (*res == '/') res++;
645 if (buflen)
647 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
648 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
649 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
651 TRACE_(dosfs)("Returning %d\n", strlen(res) + 3 );
652 return strlen(res) + 3;
656 /***********************************************************************
657 * SearchPath32W (KERNEL32.448)
659 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
660 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
662 LPWSTR p;
663 LPSTR res;
664 DOS_FULL_NAME full_name;
666 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
667 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
668 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
669 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
670 HeapFree( GetProcessHeap(), 0, extA );
671 HeapFree( GetProcessHeap(), 0, nameA );
672 HeapFree( GetProcessHeap(), 0, pathA );
673 if (!ret) return 0;
675 lstrcpynAtoW( buffer, full_name.short_name, buflen );
676 res = full_name.long_name +
677 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
678 while (*res == '/') res++;
679 if (buflen)
681 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
682 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
683 if (lastpart)
685 for (p = *lastpart = buffer; *p; p++)
686 if (*p == '\\') *lastpart = p + 1;
689 return strlen(res) + 3;