Added the ability to see names of the virtual key codes.
[wine.git] / files / directory.c
blobd1e1d9529ef37d7bdac422ed771d83c10af6b284
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #ifdef HAVE_SYS_ERRNO_H
18 #include <sys/errno.h>
19 #endif
21 #include "winbase.h"
22 #include "wine/winbase16.h"
23 #include "wine/winestring.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "wine/winuser16.h"
27 #include "winerror.h"
28 #include "drive.h"
29 #include "file.h"
30 #include "heap.h"
31 #include "msdos.h"
32 #include "options.h"
33 #include "debugtools.h"
35 DEFAULT_DEBUG_CHANNEL(dosfs);
36 DECLARE_DEBUG_CHANNEL(file);
38 static DOS_FULL_NAME DIR_Windows;
39 static DOS_FULL_NAME DIR_System;
42 /***********************************************************************
43 * DIR_GetPath
45 * Get a path name from the wine.ini file and make sure it is valid.
47 static int DIR_GetPath( const char *keyname, const char *defval,
48 DOS_FULL_NAME *full_name )
50 char path[MAX_PATHNAME_LEN];
51 BY_HANDLE_FILE_INFORMATION info;
53 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
54 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
55 !FILE_Stat( full_name->long_name, &info ) ||
56 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
58 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
59 return 0;
61 return 1;
65 /***********************************************************************
66 * DIR_Init
68 int DIR_Init(void)
70 char path[MAX_PATHNAME_LEN];
71 DOS_FULL_NAME tmp_dir, profile_dir;
72 int drive;
73 const char *cwd;
75 if (!getcwd( path, MAX_PATHNAME_LEN ))
77 perror( "Could not get current directory" );
78 return 0;
80 cwd = path;
81 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
83 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
84 "for current working directory %s; "
85 "starting in windows directory.\n", cwd );
87 else
89 DRIVE_SetCurrentDrive( drive );
90 DRIVE_Chdir( drive, cwd );
93 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
94 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
95 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
97 PROFILE_UsageWineIni();
98 return 0;
100 if (-1 == access( tmp_dir.long_name, W_OK ))
102 if (errno==EACCES)
104 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
105 PROFILE_UsageWineIni();
107 else
108 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
109 strerror(errno));
112 if (drive == -1)
114 drive = DIR_Windows.drive;
115 DRIVE_SetCurrentDrive( drive );
116 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
119 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
120 path, sizeof(path) );
121 if (strchr(path, '/'))
123 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
124 PROFILE_UsageWineIni();
125 ExitProcess(1);
128 /* Set the environment variables */
130 SetEnvironmentVariableA( "PATH", path );
131 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
132 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
133 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
135 /* set COMSPEC only if it doesn't exist already */
136 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
137 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
139 TRACE("WindowsDir = %s (%s)\n",
140 DIR_Windows.short_name, DIR_Windows.long_name );
141 TRACE("SystemDir = %s (%s)\n",
142 DIR_System.short_name, DIR_System.long_name );
143 TRACE("TempDir = %s (%s)\n",
144 tmp_dir.short_name, tmp_dir.long_name );
145 TRACE("Path = %s\n", path );
146 TRACE("Cwd = %c:\\%s\n",
147 'A' + drive, DRIVE_GetDosCwd( drive ) );
149 if (DIR_GetPath( "profile", "", &profile_dir ))
151 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
152 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
155 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
156 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
158 return 1;
162 /***********************************************************************
163 * GetTempPathA (KERNEL32.292)
165 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
167 UINT ret;
168 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
169 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
170 if (!(ret = GetCurrentDirectoryA( count, path )))
171 return 0;
172 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
174 path[ret++] = '\\';
175 path[ret] = '\0';
177 return ret;
181 /***********************************************************************
182 * GetTempPathW (KERNEL32.293)
184 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
186 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
187 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
188 UINT ret;
189 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
190 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
191 if (!(ret = GetCurrentDirectoryW( count, path )))
192 return 0;
193 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
195 path[ret++] = '\\';
196 path[ret] = '\0';
198 return ret;
202 /***********************************************************************
203 * DIR_GetWindowsUnixDir
205 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
207 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
208 return strlen( DIR_Windows.long_name );
212 /***********************************************************************
213 * DIR_GetSystemUnixDir
215 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
217 if (path) lstrcpynA( path, DIR_System.long_name, count );
218 return strlen( DIR_System.long_name );
222 /***********************************************************************
223 * GetTempDrive (KERNEL.92)
225 BYTE WINAPI GetTempDrive( BYTE ignored )
227 char *buffer;
228 BYTE ret;
229 UINT len = GetTempPathA( 0, NULL );
231 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
232 return DRIVE_GetCurrentDrive() + 'A';
234 /* FIXME: apparently Windows does something with the ignored byte */
235 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
236 ret = buffer[0];
237 HeapFree( GetProcessHeap(), 0, buffer );
238 return toupper(ret);
242 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
244 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
246 * returns:
247 * AL: driveletter
248 * AH: ':' - yes, some kernel code even does stosw with
249 * the returned AX.
250 * DX: 1 for success
252 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
256 /***********************************************************************
257 * GetWindowsDirectory16 (KERNEL.134)
259 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
261 return (UINT16)GetWindowsDirectoryA( path, count );
265 /***********************************************************************
266 * GetWindowsDirectoryA (KERNEL32.311)
268 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
270 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
271 return strlen( DIR_Windows.short_name );
275 /***********************************************************************
276 * GetWindowsDirectoryW (KERNEL32.312)
278 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
280 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
281 return strlen( DIR_Windows.short_name );
285 /***********************************************************************
286 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
288 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
290 return GetWindowsDirectoryA( path, count );
294 /***********************************************************************
295 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
297 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
299 return GetWindowsDirectoryW( path, count );
303 /***********************************************************************
304 * GetSystemDirectory16 (KERNEL.135)
306 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
308 return (UINT16)GetSystemDirectoryA( path, count );
312 /***********************************************************************
313 * GetSystemDirectoryA (KERNEL32.282)
315 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
317 if (path) lstrcpynA( path, DIR_System.short_name, count );
318 return strlen( DIR_System.short_name );
322 /***********************************************************************
323 * GetSystemDirectoryW (KERNEL32.283)
325 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
327 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
328 return strlen( DIR_System.short_name );
332 /***********************************************************************
333 * CreateDirectory16 (KERNEL.144)
335 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
337 TRACE_(file)("(%s,%p)\n", path, dummy );
338 return (BOOL16)CreateDirectoryA( path, NULL );
342 /***********************************************************************
343 * CreateDirectoryA (KERNEL32.39)
344 * RETURNS:
345 * TRUE : success
346 * FALSE : failure
347 * ERROR_DISK_FULL: on full disk
348 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
349 * ERROR_ACCESS_DENIED: on permission problems
350 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
352 BOOL WINAPI CreateDirectoryA( LPCSTR path,
353 LPSECURITY_ATTRIBUTES lpsecattribs )
355 DOS_FULL_NAME full_name;
357 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
358 if (DOSFS_GetDevice( path ))
360 TRACE_(file)("cannot use device '%s'!\n",path);
361 SetLastError( ERROR_ACCESS_DENIED );
362 return FALSE;
364 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
365 if (mkdir( full_name.long_name, 0777 ) == -1) {
366 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
367 /* the FILE_SetDosError() generated error codes don't match the
368 * CreateDirectory ones for some errnos */
369 switch (errno) {
370 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
371 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
372 default: FILE_SetDosError();break;
374 return FALSE;
376 return TRUE;
380 /***********************************************************************
381 * CreateDirectoryW (KERNEL32.42)
383 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
384 LPSECURITY_ATTRIBUTES lpsecattribs )
386 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
387 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
388 HeapFree( GetProcessHeap(), 0, xpath );
389 return ret;
393 /***********************************************************************
394 * CreateDirectoryExA (KERNEL32.40)
396 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
397 LPSECURITY_ATTRIBUTES lpsecattribs)
399 return CreateDirectoryA(path,lpsecattribs);
403 /***********************************************************************
404 * CreateDirectoryExW (KERNEL32.41)
406 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
407 LPSECURITY_ATTRIBUTES lpsecattribs)
409 return CreateDirectoryW(path,lpsecattribs);
413 /***********************************************************************
414 * RemoveDirectory16 (KERNEL)
416 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
418 return (BOOL16)RemoveDirectoryA( path );
422 /***********************************************************************
423 * RemoveDirectoryA (KERNEL32.437)
425 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
427 DOS_FULL_NAME full_name;
429 TRACE_(file)("'%s'\n", path );
431 if (DOSFS_GetDevice( path ))
433 TRACE_(file)("cannot remove device '%s'!\n", path);
434 SetLastError( ERROR_FILE_NOT_FOUND );
435 return FALSE;
437 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
438 if (rmdir( full_name.long_name ) == -1)
440 FILE_SetDosError();
441 return FALSE;
443 return TRUE;
447 /***********************************************************************
448 * RemoveDirectoryW (KERNEL32.438)
450 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
452 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
453 BOOL ret = RemoveDirectoryA( xpath );
454 HeapFree( GetProcessHeap(), 0, xpath );
455 return ret;
459 /***********************************************************************
460 * DIR_TryPath
462 * Helper function for DIR_SearchPath.
464 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
465 DOS_FULL_NAME *full_name )
467 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
468 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
470 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
471 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
473 SetLastError( ERROR_PATH_NOT_FOUND );
474 return FALSE;
476 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
477 sizeof(full_name->long_name) - (p_l - full_name->long_name),
478 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
479 return FALSE;
480 strcpy( full_name->long_name, dir->long_name );
481 p_l[-1] = '/';
482 strcpy( full_name->short_name, dir->short_name );
483 p_s[-1] = '\\';
484 return TRUE;
488 /***********************************************************************
489 * DIR_TryEnvironmentPath
491 * Helper function for DIR_SearchPath.
492 * Search in the specified path, or in $PATH if NULL.
494 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
496 LPSTR path, next, buffer;
497 BOOL ret = FALSE;
498 INT len = strlen(name);
499 DWORD size;
501 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
502 if (!size) return FALSE;
503 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
504 if (envpath) strcpy( path, envpath );
505 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
506 next = path;
507 while (!ret && next)
509 LPSTR cur = next;
510 while (*cur == ';') cur++;
511 if (!*cur) break;
512 next = strchr( cur, ';' );
513 if (next) *next++ = '\0';
514 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
515 goto done;
516 strcpy( buffer, cur );
517 strcat( buffer, "\\" );
518 strcat( buffer, name );
519 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
520 HeapFree( GetProcessHeap(), 0, buffer );
523 done:
524 HeapFree( GetProcessHeap(), 0, path );
525 return ret;
529 /***********************************************************************
530 * DIR_TryModulePath
532 * Helper function for DIR_SearchPath.
534 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
536 /* FIXME: for now, GetModuleFileNameA can't return more */
537 /* than OFS_MAXPATHNAME. This may change with Win32. */
539 char buffer[OFS_MAXPATHNAME];
540 LPSTR p;
542 if (!win32)
544 if (!GetCurrentTask()) return FALSE;
545 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
546 buffer[0]='\0';
547 } else {
548 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
549 buffer[0]='\0';
551 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
552 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
553 strcpy( p, name );
554 return DOSFS_GetFullName( buffer, TRUE, full_name );
558 /***********************************************************************
559 * DIR_SearchPath
561 * Implementation of SearchPathA. 'win32' specifies whether the search
562 * order is Win16 (module path last) or Win32 (module path first).
564 * FIXME: should return long path names.
566 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
567 DOS_FULL_NAME *full_name, BOOL win32 )
569 LPCSTR p;
570 LPSTR tmp = NULL;
571 BOOL ret = TRUE;
573 /* First check the supplied parameters */
575 p = strrchr( name, '.' );
576 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
577 ext = NULL; /* Ignore the specified extension */
578 if ((*name && (name[1] == ':')) ||
579 strchr( name, '/' ) || strchr( name, '\\' ))
580 path = NULL; /* Ignore path if name already contains a path */
581 if (path && !*path) path = NULL; /* Ignore empty path */
583 /* Allocate a buffer for the file name and extension */
585 if (ext)
587 DWORD len = strlen(name) + strlen(ext);
588 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
590 SetLastError( ERROR_OUTOFMEMORY );
591 return 0;
593 strcpy( tmp, name );
594 strcat( tmp, ext );
595 name = tmp;
598 /* If the name contains an explicit path, everything's easy */
600 if ((*name && (name[1] == ':')) || strchr( name, '/' ) || strchr( name, '\\' ))
602 ret = DOSFS_GetFullName( name, TRUE, full_name );
603 goto done;
606 /* Search in the specified path */
608 if (path)
610 ret = DIR_TryEnvironmentPath( name, full_name, path );
611 goto done;
614 /* Try the path of the current executable (for Win32 search order) */
616 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
618 /* Try the current directory */
620 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
622 /* Try the Windows system directory */
624 if (DIR_TryPath( &DIR_System, name, full_name ))
625 goto done;
627 /* Try the Windows directory */
629 if (DIR_TryPath( &DIR_Windows, name, full_name ))
630 goto done;
632 /* Try the path of the current executable (for Win16 search order) */
634 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
636 /* Try all directories in path */
638 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
640 done:
641 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
642 return ret;
646 /***********************************************************************
647 * SearchPathA [KERNEL32.447]
649 * Searches for a specified file in the search path.
651 * PARAMS
652 * path [I] Path to search
653 * name [I] Filename to search for.
654 * ext [I] File extension to append to file name. The first
655 * character must be a period. This parameter is
656 * specified only if the filename given does not
657 * contain an extension.
658 * buflen [I] size of buffer, in characters
659 * buffer [O] buffer for found filename
660 * lastpart [O] address of pointer to last used character in
661 * buffer (the final '\')
663 * RETURNS
664 * Success: length of string copied into buffer, not including
665 * terminating null character. If the filename found is
666 * longer than the length of the buffer, the length of the
667 * filename is returned.
668 * Failure: Zero
670 * NOTES
671 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
672 * (tested on NT 4.0)
674 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
675 LPSTR buffer, LPSTR *lastpart )
677 LPSTR p, res;
678 DOS_FULL_NAME full_name;
680 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
682 SetLastError(ERROR_FILE_NOT_FOUND);
683 return 0;
685 lstrcpynA( buffer, full_name.short_name, buflen );
686 res = full_name.long_name +
687 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
688 while (*res == '/') res++;
689 if (buflen)
691 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
692 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
693 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
695 TRACE("Returning %d\n", strlen(res) + 3 );
696 return strlen(res) + 3;
700 /***********************************************************************
701 * SearchPathW (KERNEL32.448)
703 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
704 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
706 LPWSTR p;
707 LPSTR res;
708 DOS_FULL_NAME full_name;
710 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
711 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
712 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
713 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
714 HeapFree( GetProcessHeap(), 0, extA );
715 HeapFree( GetProcessHeap(), 0, nameA );
716 HeapFree( GetProcessHeap(), 0, pathA );
717 if (!ret) return 0;
719 lstrcpynAtoW( buffer, full_name.short_name, buflen );
720 res = full_name.long_name +
721 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
722 while (*res == '/') res++;
723 if (buflen)
725 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
726 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
727 if (lastpart)
729 for (p = *lastpart = buffer; *p; p++)
730 if (*p == '\\') *lastpart = p + 1;
733 return strlen(res) + 3;