Fixed an error and an off-by-one bug in DSA_SetItem(). This
[wine/dcerpc.git] / files / directory.c
blobb1833eaf227ce3095e951a05cc0ebfa40edaf37f
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 #include <sys/errno.h>
16 #include "winbase.h"
17 #include "wine/winbase16.h"
18 #include "wine/winestring.h"
19 #include "winerror.h"
20 #include "process.h"
21 #include "drive.h"
22 #include "file.h"
23 #include "heap.h"
24 #include "msdos.h"
25 #include "options.h"
26 #include "debugtools.h"
28 DECLARE_DEBUG_CHANNEL(dosfs)
29 DECLARE_DEBUG_CHANNEL(file)
31 static DOS_FULL_NAME DIR_Windows;
32 static DOS_FULL_NAME DIR_System;
35 /***********************************************************************
36 * DIR_GetPath
38 * Get a path name from the wine.ini file and make sure it is valid.
40 static int DIR_GetPath( const char *keyname, const char *defval,
41 DOS_FULL_NAME *full_name )
43 char path[MAX_PATHNAME_LEN];
44 BY_HANDLE_FILE_INFORMATION info;
46 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
47 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
48 !FILE_Stat( full_name->long_name, &info ) ||
49 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
51 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
52 return 0;
54 return 1;
58 /***********************************************************************
59 * DIR_Init
61 int DIR_Init(void)
63 char path[MAX_PATHNAME_LEN];
64 DOS_FULL_NAME tmp_dir;
65 int drive;
66 const char *cwd;
68 if (!getcwd( path, MAX_PATHNAME_LEN ))
70 perror( "Could not get current directory" );
71 return 0;
73 cwd = path;
74 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
76 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
77 "for current working directory %s; "
78 "starting in windows directory.\n", cwd );
80 else
82 DRIVE_SetCurrentDrive( drive );
83 DRIVE_Chdir( drive, cwd );
86 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
87 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
88 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
90 PROFILE_UsageWineIni();
91 return 0;
93 if (-1 == access( tmp_dir.long_name, W_OK ))
95 if (errno==EACCES)
97 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
98 PROFILE_UsageWineIni();
100 else
101 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
102 strerror(errno));
105 if (drive == -1)
107 drive = DIR_Windows.drive;
108 DRIVE_SetCurrentDrive( drive );
109 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
112 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
113 path, sizeof(path) );
115 /* Set the environment variables */
117 SetEnvironmentVariableA( "PATH", path );
118 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
119 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
120 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
121 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
123 TRACE_(dosfs)("WindowsDir = %s (%s)\n",
124 DIR_Windows.short_name, DIR_Windows.long_name );
125 TRACE_(dosfs)("SystemDir = %s (%s)\n",
126 DIR_System.short_name, DIR_System.long_name );
127 TRACE_(dosfs)("TempDir = %s (%s)\n",
128 tmp_dir.short_name, tmp_dir.long_name );
129 TRACE_(dosfs)("Path = %s\n", path );
130 TRACE_(dosfs)("Cwd = %c:\\%s\n",
131 'A' + drive, DRIVE_GetDosCwd( drive ) );
133 return 1;
137 /***********************************************************************
138 * GetTempPath32A (KERNEL32.292)
140 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
142 UINT ret;
143 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
144 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
145 if (!(ret = GetCurrentDirectoryA( count, path )))
146 return 0;
147 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
149 path[ret++] = '\\';
150 path[ret] = '\0';
152 return ret;
156 /***********************************************************************
157 * GetTempPath32W (KERNEL32.293)
159 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
161 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
162 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
163 UINT ret;
164 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
165 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
166 if (!(ret = GetCurrentDirectoryW( count, path )))
167 return 0;
168 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
170 path[ret++] = '\\';
171 path[ret] = '\0';
173 return ret;
177 /***********************************************************************
178 * DIR_GetWindowsUnixDir
180 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
182 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
183 return strlen( DIR_Windows.long_name );
187 /***********************************************************************
188 * DIR_GetSystemUnixDir
190 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
192 if (path) lstrcpynA( path, DIR_System.long_name, count );
193 return strlen( DIR_System.long_name );
197 /***********************************************************************
198 * GetTempDrive (KERNEL.92)
200 BYTE WINAPI GetTempDrive( BYTE ignored )
202 char *buffer;
203 BYTE ret;
204 UINT len = GetTempPathA( 0, NULL );
206 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
207 return DRIVE_GetCurrentDrive() + 'A';
209 /* FIXME: apparently Windows does something with the ignored byte */
210 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
211 ret = buffer[0];
212 HeapFree( GetProcessHeap(), 0, buffer );
213 return toupper(ret);
217 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
219 /* A closer look at krnl386.exe shows what the SDK doesn't mention:
221 * returns:
222 * AL: driveletter
223 * AH: ':' - yes, some kernel code even does stosw with
224 * the returned AX.
225 * DX: 1 for success
227 return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
231 /***********************************************************************
232 * GetWindowsDirectory16 (KERNEL.134)
234 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
236 return (UINT16)GetWindowsDirectoryA( path, count );
240 /***********************************************************************
241 * GetWindowsDirectory32A (KERNEL32.311)
243 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
245 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
246 return strlen( DIR_Windows.short_name );
250 /***********************************************************************
251 * GetWindowsDirectory32W (KERNEL32.312)
253 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
255 if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
256 return strlen( DIR_Windows.short_name );
260 /***********************************************************************
261 * GetSystemDirectory16 (KERNEL.135)
263 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
265 return (UINT16)GetSystemDirectoryA( path, count );
269 /***********************************************************************
270 * GetSystemDirectory32A (KERNEL32.282)
272 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
274 if (path) lstrcpynA( path, DIR_System.short_name, count );
275 return strlen( DIR_System.short_name );
279 /***********************************************************************
280 * GetSystemDirectory32W (KERNEL32.283)
282 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
284 if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
285 return strlen( DIR_System.short_name );
289 /***********************************************************************
290 * CreateDirectory16 (KERNEL.144)
292 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
294 TRACE_(file)("(%s,%p)\n", path, dummy );
295 return (BOOL16)CreateDirectoryA( path, NULL );
299 /***********************************************************************
300 * CreateDirectory32A (KERNEL32.39)
301 * RETURNS:
302 * TRUE : success
303 * FALSE : failure
304 * ERROR_DISK_FULL: on full disk
305 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
306 * ERROR_ACCESS_DENIED: on permission problems
307 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
309 BOOL WINAPI CreateDirectoryA( LPCSTR path,
310 LPSECURITY_ATTRIBUTES lpsecattribs )
312 DOS_FULL_NAME full_name;
314 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
315 if (DOSFS_GetDevice( path ))
317 TRACE_(file)("cannot use device '%s'!\n",path);
318 SetLastError( ERROR_ACCESS_DENIED );
319 return FALSE;
321 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
322 if (mkdir( full_name.long_name, 0777 ) == -1) {
323 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
324 /* the FILE_SetDosError() generated error codes don't match the
325 * CreateDirectory ones for some errnos */
326 switch (errno) {
327 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
328 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
329 default: FILE_SetDosError();break;
331 return FALSE;
333 return TRUE;
337 /***********************************************************************
338 * CreateDirectory32W (KERNEL32.42)
340 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
341 LPSECURITY_ATTRIBUTES lpsecattribs )
343 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
344 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
345 HeapFree( GetProcessHeap(), 0, xpath );
346 return ret;
350 /***********************************************************************
351 * CreateDirectoryEx32A (KERNEL32.40)
353 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
354 LPSECURITY_ATTRIBUTES lpsecattribs)
356 return CreateDirectoryA(path,lpsecattribs);
360 /***********************************************************************
361 * CreateDirectoryEx32W (KERNEL32.41)
363 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
364 LPSECURITY_ATTRIBUTES lpsecattribs)
366 return CreateDirectoryW(path,lpsecattribs);
370 /***********************************************************************
371 * RemoveDirectory16 (KERNEL)
373 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
375 return (BOOL16)RemoveDirectoryA( path );
379 /***********************************************************************
380 * RemoveDirectory32A (KERNEL32.437)
382 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
384 DOS_FULL_NAME full_name;
386 TRACE_(file)("'%s'\n", path );
388 if (DOSFS_GetDevice( path ))
390 TRACE_(file)("cannot remove device '%s'!\n", path);
391 SetLastError( ERROR_FILE_NOT_FOUND );
392 return FALSE;
394 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
395 if (rmdir( full_name.long_name ) == -1)
397 FILE_SetDosError();
398 return FALSE;
400 return TRUE;
404 /***********************************************************************
405 * RemoveDirectory32W (KERNEL32.438)
407 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
409 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
410 BOOL ret = RemoveDirectoryA( xpath );
411 HeapFree( GetProcessHeap(), 0, xpath );
412 return ret;
416 /***********************************************************************
417 * DIR_TryPath
419 * Helper function for DIR_SearchPath.
421 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
422 DOS_FULL_NAME *full_name )
424 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
425 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
427 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
428 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
430 SetLastError( ERROR_PATH_NOT_FOUND );
431 return FALSE;
433 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
434 sizeof(full_name->long_name) - (p_l - full_name->long_name),
435 p_s, DRIVE_GetFlags( dir->drive ) ))
436 return FALSE;
437 strcpy( full_name->long_name, dir->long_name );
438 p_l[-1] = '/';
439 strcpy( full_name->short_name, dir->short_name );
440 p_s[-1] = '\\';
441 return TRUE;
445 /***********************************************************************
446 * DIR_TryEnvironmentPath
448 * Helper function for DIR_SearchPath.
450 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
452 LPSTR path, next, buffer;
453 BOOL ret = FALSE;
454 INT len = strlen(name);
455 DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
457 if (!size) return FALSE;
458 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
459 if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
460 next = path;
461 while (!ret && next)
463 LPSTR cur = next;
464 while (*cur == ';') cur++;
465 if (!*cur) break;
466 next = strchr( cur, ';' );
467 if (next) *next++ = '\0';
468 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
469 goto done;
470 strcpy( buffer, cur );
471 strcat( buffer, "\\" );
472 strcat( buffer, name );
473 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
474 HeapFree( GetProcessHeap(), 0, buffer );
477 done:
478 HeapFree( GetProcessHeap(), 0, path );
479 return ret;
483 /***********************************************************************
484 * DIR_TryModulePath
486 * Helper function for DIR_SearchPath.
488 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
490 PDB *pdb = PROCESS_Current();
492 /* FIXME: for now, GetModuleFileName32A can't return more */
493 /* than OFS_MAXPATHNAME. This may change with Win32. */
495 char buffer[OFS_MAXPATHNAME];
496 LPSTR p;
498 if (pdb->flags & PDB32_WIN16_PROC) {
499 if (!GetCurrentTask()) return FALSE;
500 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
501 buffer[0]='\0';
502 } else {
503 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
504 buffer[0]='\0';
506 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
507 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
508 strcpy( p, name );
509 return DOSFS_GetFullName( buffer, TRUE, full_name );
513 /***********************************************************************
514 * DIR_SearchPath
516 * Implementation of SearchPath32A. 'win32' specifies whether the search
517 * order is Win16 (module path last) or Win32 (module path first).
519 * FIXME: should return long path names.
521 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
522 DOS_FULL_NAME *full_name, BOOL win32 )
524 DWORD len;
525 LPCSTR p;
526 LPSTR tmp = NULL;
527 BOOL ret = TRUE;
529 /* First check the supplied parameters */
531 p = strrchr( name, '.' );
532 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
533 ext = NULL; /* Ignore the specified extension */
534 if ((*name && (name[1] == ':')) ||
535 strchr( name, '/' ) || strchr( name, '\\' ))
536 path = NULL; /* Ignore path if name already contains a path */
537 if (path && !*path) path = NULL; /* Ignore empty path */
539 len = strlen(name);
540 if (ext) len += strlen(ext);
541 if (path) len += strlen(path) + 1;
543 /* Allocate a buffer for the file name and extension */
545 if (path || ext)
547 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
549 SetLastError( ERROR_OUTOFMEMORY );
550 return 0;
552 if (path)
554 strcpy( tmp, path );
555 strcat( tmp, "\\" );
556 strcat( tmp, name );
558 else strcpy( tmp, name );
559 if (ext) strcat( tmp, ext );
560 name = tmp;
563 /* If we have an explicit path, everything's easy */
565 if (path || (*name && (name[1] == ':')) ||
566 strchr( name, '/' ) || strchr( name, '\\' ))
568 ret = DOSFS_GetFullName( name, TRUE, full_name );
569 goto done;
572 /* Try the path of the current executable (for Win32 search order) */
574 if (win32 && DIR_TryModulePath( name, full_name )) goto done;
576 /* Try the current directory */
578 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
580 /* Try the Windows system directory */
582 if (DIR_TryPath( &DIR_System, name, full_name ))
583 goto done;
585 /* Try the Windows directory */
587 if (DIR_TryPath( &DIR_Windows, name, full_name ))
588 goto done;
590 /* Try the path of the current executable (for Win16 search order) */
592 if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
594 /* Try all directories in path */
596 ret = DIR_TryEnvironmentPath( name, full_name );
598 done:
599 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
600 return ret;
604 /***********************************************************************
605 * SearchPath32A [KERNEL32.447]
607 * Searches for a specified file in the search path.
609 * PARAMS
610 * path [I] Path to search
611 * name [I] Filename to search for.
612 * ext [I] File extension to append to file name. The first
613 * character must be a period. This parameter is
614 * specified only if the filename given does not
615 * contain an extension.
616 * buflen [I] size of buffer, in characters
617 * buffer [O] buffer for found filename
618 * lastpart [O] address of pointer to last used character in
619 * buffer (the final '\')
621 * RETURNS
622 * Success: length of string copied into buffer, not including
623 * terminating null character. If the filename found is
624 * longer than the length of the buffer, the length of the
625 * filename is returned.
626 * Failure: Zero
628 * NOTES
629 * Should call SetLastError(but currently doesn't).
631 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
632 LPSTR buffer, LPSTR *lastpart )
634 LPSTR p, res;
635 DOS_FULL_NAME full_name;
637 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
638 lstrcpynA( buffer, full_name.short_name, buflen );
639 res = full_name.long_name +
640 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
641 while (*res == '/') res++;
642 if (buflen)
644 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
645 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
646 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
648 TRACE_(dosfs)("Returning %d\n", strlen(res) + 3 );
649 return strlen(res) + 3;
653 /***********************************************************************
654 * SearchPath32W (KERNEL32.448)
656 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
657 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
659 LPWSTR p;
660 LPSTR res;
661 DOS_FULL_NAME full_name;
663 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
664 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
665 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
666 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
667 HeapFree( GetProcessHeap(), 0, extA );
668 HeapFree( GetProcessHeap(), 0, nameA );
669 HeapFree( GetProcessHeap(), 0, pathA );
670 if (!ret) return 0;
672 lstrcpynAtoW( buffer, full_name.short_name, buflen );
673 res = full_name.long_name +
674 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
675 while (*res == '/') res++;
676 if (buflen)
678 if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 );
679 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
680 if (lastpart)
682 for (p = *lastpart = buffer; *p; p++)
683 if (*p == '\\') *lastpart = p + 1;
686 return strlen(res) + 3;