Cast time_t to long for printing.
[wine] / files / directory.c
1 /*
2  * DOS directories functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #include <errno.h>
34 #ifdef HAVE_SYS_ERRNO_H
35 #include <sys/errno.h>
36 #endif
37
38 #include "ntstatus.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42 #include "wingdi.h"
43 #include "wine/winuser16.h"
44 #include "winerror.h"
45 #include "winreg.h"
46 #include "winternl.h"
47 #include "wine/unicode.h"
48 #include "drive.h"
49 #include "file.h"
50 #include "msdos.h"
51 #include "wine/debug.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
54 WINE_DECLARE_DEBUG_CHANNEL(file);
55
56 static DOS_FULL_NAME DIR_Windows;
57 static DOS_FULL_NAME DIR_System;
58
59 static const WCHAR wineW[] = {'w','i','n','e',0};
60
61 /***********************************************************************
62  *           FILE_contains_pathW
63  */
64 inline static int FILE_contains_pathW (LPCWSTR name)
65 {
66     return ((*name && (name[1] == ':')) ||
67             strchrW (name, '/') || strchrW (name, '\\'));
68 }
69
70 /***********************************************************************
71  *           DIR_GetPath
72  *
73  * Get a path name from the wine.ini file and make sure it is valid.
74  */
75 static int DIR_GetPath( HKEY hkey, LPCWSTR keyname, LPCWSTR defval, DOS_FULL_NAME *full_name,
76                         LPWSTR longname, INT longname_len, BOOL warn )
77 {
78     UNICODE_STRING nameW;
79     DWORD dummy;
80     WCHAR tmp[MAX_PATHNAME_LEN];
81     BY_HANDLE_FILE_INFORMATION info;
82     const WCHAR *path = defval;
83     const char *mess = "does not exist";
84
85     RtlInitUnicodeString( &nameW, keyname );
86     if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
87         path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
88
89     if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
90         (!FILE_Stat( full_name->long_name, &info, NULL ) && (mess=strerror(errno)))||
91         (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
92         (!(GetLongPathNameW(full_name->short_name, longname, longname_len))) )
93     {
94         if (warn)
95         {
96             MESSAGE("Invalid path %s for %s directory: %s.\n",
97                     debugstr_w(path), debugstr_w(keyname), mess);
98             MESSAGE("Perhaps you have not properly edited your Wine configuration file (%s/config)\n",
99                     wine_get_config_dir());
100         }
101         return 0;
102     }
103     return 1;
104 }
105
106
107 /***********************************************************************
108  *           DIR_Init
109  */
110 int DIR_Init(void)
111 {
112     OBJECT_ATTRIBUTES attr;
113     UNICODE_STRING nameW;
114     HKEY hkey;
115     char path[MAX_PATHNAME_LEN];
116     WCHAR longpath[MAX_PATHNAME_LEN];
117     DOS_FULL_NAME tmp_dir, profile_dir;
118     int drive;
119     const char *cwd;
120     static const WCHAR wineW[] = {'M','a','c','h','i','n','e','\\',
121                                   'S','o','f','t','w','a','r','e','\\',
122                                   'W','i','n','e','\\','W','i','n','e','\\',
123                                   'C','o','n','f','i','g','\\','W','i','n','e',0};
124     static const WCHAR windowsW[] = {'w','i','n','d','o','w','s',0};
125     static const WCHAR systemW[] = {'s','y','s','t','e','m',0};
126     static const WCHAR tempW[] = {'t','e','m','p',0};
127     static const WCHAR profileW[] = {'p','r','o','f','i','l','e',0};
128     static const WCHAR windows_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',0};
129     static const WCHAR system_dirW[] = {'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
130     static const WCHAR pathW[] = {'p','a','t','h',0};
131     static const WCHAR path_dirW[] = {'c',':','\\','w','i','n','d','o','w','s',';',
132                                       'c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m',0};
133     static const WCHAR path_capsW[] = {'P','A','T','H',0};
134     static const WCHAR temp_capsW[] = {'T','E','M','P',0};
135     static const WCHAR tmp_capsW[] = {'T','M','P',0};
136     static const WCHAR windirW[] = {'w','i','n','d','i','r',0};
137     static const WCHAR winsysdirW[] = {'w','i','n','s','y','s','d','i','r',0};
138     static const WCHAR userprofileW[] = {'U','S','E','R','P','R','O','F','I','L','E',0};
139     static const WCHAR systemrootW[] = {'S','Y','S','T','E','M','R','O','O','T',0};
140     static const WCHAR wcmdW[] = {'\\','w','c','m','d','.','e','x','e',0};
141     static const WCHAR comspecW[] = {'C','O','M','S','P','E','C',0};
142     static const WCHAR empty_strW[] = { 0 };
143
144     if (!getcwd( path, MAX_PATHNAME_LEN ))
145     {
146         perror( "Could not get current directory" );
147         return 0;
148     }
149     cwd = path;
150     if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
151     {
152         MESSAGE("Warning: could not find wine config [Drive x] entry "
153             "for current working directory %s; "
154             "starting in windows directory.\n", cwd );
155     }
156     else
157     {
158         WCHAR szdrive[3]={drive+'A',':',0};
159         MultiByteToWideChar(DRIVE_GetCodepage(drive), 0, cwd, -1, longpath, MAX_PATHNAME_LEN);
160         DRIVE_SetCurrentDrive( drive );
161         DRIVE_Chdir( drive, longpath );
162         if(GetDriveTypeW(szdrive)==DRIVE_CDROM)
163             chdir("/"); /* change to root directory so as not to lock cdroms */
164     }
165
166     attr.Length = sizeof(attr);
167     attr.RootDirectory = 0;
168     attr.ObjectName = &nameW;
169     attr.Attributes = 0;
170     attr.SecurityDescriptor = NULL;
171     attr.SecurityQualityOfService = NULL;
172
173     RtlInitUnicodeString( &nameW, wineW );
174     if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
175
176     if (!(DIR_GetPath( hkey, windowsW, windows_dirW, &DIR_Windows, longpath, MAX_PATHNAME_LEN, TRUE )) ||
177         !(DIR_GetPath( hkey, systemW, system_dirW, &DIR_System, longpath, MAX_PATHNAME_LEN, TRUE )) ||
178         !(DIR_GetPath( hkey, tempW, windows_dirW, &tmp_dir, longpath, MAX_PATHNAME_LEN, TRUE )))
179     {
180         if (hkey) NtClose( hkey );
181         return 0;
182     }
183     if (-1 == access( tmp_dir.long_name, W_OK ))
184     {
185         if (errno==EACCES)
186         {
187                 MESSAGE("Warning: the temporary directory '%s' specified in your\n"
188                         "configuration file (%s) is not writeable.\n",
189                         tmp_dir.long_name, wine_get_config_dir() );
190         }
191         else
192                 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
193                     tmp_dir.long_name, strerror(errno));
194     }
195
196     if (drive == -1)
197     {
198         drive = DIR_Windows.drive;
199         DRIVE_SetCurrentDrive( drive );
200         DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
201     }
202
203     /* Set the environment variables */
204
205     /* set COMSPEC only if it doesn't exist already */
206     if (!GetEnvironmentVariableW( comspecW, NULL, 0 ))
207     {
208         strcpyW( longpath, DIR_System.short_name );
209         strcatW( longpath, wcmdW );
210         SetEnvironmentVariableW( comspecW, longpath );
211     }
212
213     /* set PATH only if not set already */
214     if (!GetEnvironmentVariableW( path_capsW, NULL, 0 ))
215     {
216         WCHAR tmp[MAX_PATHNAME_LEN];
217         DWORD dummy;
218         const WCHAR *path = path_dirW;
219
220         RtlInitUnicodeString( &nameW, pathW );
221         if (hkey && !NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation,
222                                       tmp, sizeof(tmp), &dummy ))
223         {
224             path = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
225         }
226
227         if (strchrW(path, '/'))
228         {
229             MESSAGE("Fix your wine config (%s/config) to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n", wine_get_config_dir() );
230             ExitProcess(1);
231         }
232         SetEnvironmentVariableW( path_capsW, path );
233         TRACE("Path       = %s\n", debugstr_w(path) );
234     }
235
236     SetEnvironmentVariableW( temp_capsW, tmp_dir.short_name );
237     SetEnvironmentVariableW( tmp_capsW, tmp_dir.short_name );
238     SetEnvironmentVariableW( windirW, DIR_Windows.short_name );
239     SetEnvironmentVariableW( winsysdirW, DIR_System.short_name );
240
241     TRACE("WindowsDir = %s (%s)\n",
242           debugstr_w(DIR_Windows.short_name), DIR_Windows.long_name );
243     TRACE("SystemDir  = %s (%s)\n",
244           debugstr_w(DIR_System.short_name), DIR_System.long_name );
245     TRACE("TempDir    = %s (%s)\n",
246           debugstr_w(tmp_dir.short_name), tmp_dir.long_name );
247     TRACE("Cwd        = %c:\\%s\n",
248           'A' + drive, debugstr_w(DRIVE_GetDosCwd(drive)) );
249
250     if (DIR_GetPath( hkey, profileW, empty_strW, &profile_dir, longpath, MAX_PATHNAME_LEN, FALSE ))
251     {
252         TRACE("USERPROFILE= %s\n", debugstr_w(longpath) );
253         SetEnvironmentVariableW( userprofileW, longpath );
254     }
255
256     TRACE("SYSTEMROOT = %s\n", debugstr_w(DIR_Windows.short_name) );
257     SetEnvironmentVariableW( systemrootW, DIR_Windows.short_name );
258     if (hkey) NtClose( hkey );
259
260     return 1;
261 }
262
263
264 /***********************************************************************
265  *           GetTempPathA   (KERNEL32.@)
266  */
267 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
268 {
269     WCHAR pathW[MAX_PATH];
270     UINT ret;
271
272     ret = GetTempPathW(MAX_PATH, pathW);
273
274     if (!ret)
275         return 0;
276
277     if (ret > MAX_PATH)
278     {
279         SetLastError(ERROR_FILENAME_EXCED_RANGE);
280         return 0;
281     }
282
283     ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
284     if (ret <= count)
285     {
286         WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
287         ret--; /* length without 0 */
288     }
289     return ret;
290 }
291
292
293 /***********************************************************************
294  *           GetTempPathW   (KERNEL32.@)
295  */
296 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
297 {
298     static const WCHAR tmp[]  = { 'T', 'M', 'P', 0 };
299     static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
300     WCHAR tmp_path[MAX_PATH];
301     UINT ret;
302
303     TRACE("%u,%p\n", count, path);
304
305     if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
306         if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
307             if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
308                 return 0;
309
310     if (ret > MAX_PATH)
311     {
312         SetLastError(ERROR_FILENAME_EXCED_RANGE);
313         return 0;
314     }
315
316     ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
317     if (!ret) return 0;
318
319     if (ret > MAX_PATH - 2)
320     {
321         SetLastError(ERROR_FILENAME_EXCED_RANGE);
322         return 0;
323     }
324
325     if (tmp_path[ret-1] != '\\')
326     {
327         tmp_path[ret++] = '\\';
328         tmp_path[ret]   = '\0';
329     }
330
331     ret++; /* add space for terminating 0 */
332
333     if (count)
334     {
335         lstrcpynW(path, tmp_path, count);
336         if (count >= ret)
337             ret--; /* return length without 0 */
338         else if (count < 4)
339             path[0] = 0; /* avoid returning ambiguous "X:" */
340     }
341
342     TRACE("returning %u, %s\n", ret, debugstr_w(path));
343     return ret;
344 }
345
346
347 /***********************************************************************
348  *           DIR_GetWindowsUnixDir
349  */
350 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
351 {
352     if (path) lstrcpynA( path, DIR_Windows.long_name, count );
353     return strlen( DIR_Windows.long_name );
354 }
355
356
357 /***********************************************************************
358  *           DIR_GetSystemUnixDir
359  */
360 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
361 {
362     if (path) lstrcpynA( path, DIR_System.long_name, count );
363     return strlen( DIR_System.long_name );
364 }
365
366
367 /***********************************************************************
368  *           GetTempDrive   (KERNEL.92)
369  * A closer look at krnl386.exe shows what the SDK doesn't mention:
370  *
371  * returns:
372  *   AL: driveletter
373  *   AH: ':'            - yes, some kernel code even does stosw with
374  *                            the returned AX.
375  *   DX: 1 for success
376  */
377 UINT WINAPI GetTempDrive( BYTE ignored )
378 {
379     char *buffer;
380     BYTE ret;
381     UINT len = GetTempPathA( 0, NULL );
382
383     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
384         ret = DRIVE_GetCurrentDrive() + 'A';
385     else
386     {
387         /* FIXME: apparently Windows does something with the ignored byte */
388         if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
389         ret = toupper(buffer[0]);
390         HeapFree( GetProcessHeap(), 0, buffer );
391     }
392     return MAKELONG( ret | (':' << 8), 1 );
393 }
394
395
396 /***********************************************************************
397  *           GetWindowsDirectory   (KERNEL.134)
398  */
399 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
400 {
401     return (UINT16)GetWindowsDirectoryA( path, count );
402 }
403
404
405 /***********************************************************************
406  *           GetWindowsDirectoryW   (KERNEL32.@)
407  *
408  * See comment for GetWindowsDirectoryA.
409  */
410 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
411 {
412     UINT len = strlenW( DIR_Windows.short_name ) + 1;
413     if (path && count >= len)
414     {
415         strcpyW( path, DIR_Windows.short_name );
416         len--;
417     }
418     return len;
419 }
420
421
422 /***********************************************************************
423  *           GetWindowsDirectoryA   (KERNEL32.@)
424  *
425  * Return value:
426  * If buffer is large enough to hold full path and terminating '\0' character
427  * function copies path to buffer and returns length of the path without '\0'.
428  * Otherwise function returns required size including '\0' character and
429  * does not touch the buffer.
430  */
431 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
432 {
433     UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0, NULL, NULL );
434     if (path && count >= len)
435     {
436         WideCharToMultiByte( CP_ACP, 0, DIR_Windows.short_name, -1, path, count, NULL, NULL );
437         len--;
438     }
439     return len;
440 }
441
442
443 /***********************************************************************
444  *           GetSystemWindowsDirectoryA   (KERNEL32.@) W2K, TS4.0SP4
445  */
446 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
447 {
448     return GetWindowsDirectoryA( path, count );
449 }
450
451
452 /***********************************************************************
453  *           GetSystemWindowsDirectoryW   (KERNEL32.@) W2K, TS4.0SP4
454  */
455 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
456 {
457     return GetWindowsDirectoryW( path, count );
458 }
459
460
461 /***********************************************************************
462  *           GetSystemDirectory   (KERNEL.135)
463  */
464 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
465 {
466     return (UINT16)GetSystemDirectoryA( path, count );
467 }
468
469
470 /***********************************************************************
471  *           GetSystemDirectoryW   (KERNEL32.@)
472  *
473  * See comment for GetWindowsDirectoryA.
474  */
475 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
476 {
477     UINT len = strlenW( DIR_System.short_name ) + 1;
478     if (path && count >= len)
479     {
480         strcpyW( path, DIR_System.short_name );
481         len--;
482     }
483     return len;
484 }
485
486
487 /***********************************************************************
488  *           GetSystemDirectoryA   (KERNEL32.@)
489  *
490  * See comment for GetWindowsDirectoryA.
491  */
492 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
493 {
494     UINT len = WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0, NULL, NULL );
495     if (path && count >= len)
496     {
497         WideCharToMultiByte( CP_ACP, 0, DIR_System.short_name, -1, path, count, NULL, NULL );
498         len--;
499     }
500     return len;
501 }
502
503
504 /***********************************************************************
505  *           CreateDirectory   (KERNEL.144)
506  */
507 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
508 {
509     TRACE_(file)("(%s,%p)\n", path, dummy );
510     return (BOOL16)CreateDirectoryA( path, NULL );
511 }
512
513
514 /***********************************************************************
515  *           CreateDirectoryW   (KERNEL32.@)
516  * RETURNS:
517  *      TRUE : success
518  *      FALSE : failure
519  *              ERROR_DISK_FULL:        on full disk
520  *              ERROR_ALREADY_EXISTS:   if directory name exists (even as file)
521  *              ERROR_ACCESS_DENIED:    on permission problems
522  *              ERROR_FILENAME_EXCED_RANGE: too long filename(s)
523  */
524 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
525                                   LPSECURITY_ATTRIBUTES lpsecattribs )
526 {
527     DOS_FULL_NAME full_name;
528
529     if (!path || !*path)
530     {
531         SetLastError(ERROR_PATH_NOT_FOUND);
532         return FALSE;
533     }
534
535     TRACE_(file)("(%s,%p)\n", debugstr_w(path), lpsecattribs );
536
537     if (DOSFS_GetDevice( path ))
538     {
539         TRACE_(file)("cannot use device %s!\n", debugstr_w(path));
540         SetLastError( ERROR_ACCESS_DENIED );
541         return FALSE;
542     }
543     if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
544     if (mkdir( full_name.long_name, 0777 ) == -1) {
545         WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
546         /* the FILE_SetDosError() generated error codes don't match the
547          * CreateDirectory ones for some errnos */
548         switch (errno) {
549         case EEXIST:
550         {
551             if (!strcmp(DRIVE_GetRoot(full_name.drive), full_name.long_name))
552                 SetLastError(ERROR_ACCESS_DENIED);
553             else
554                 SetLastError(ERROR_ALREADY_EXISTS);
555             break;
556         }
557         case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
558         default: FILE_SetDosError();break;
559         }
560         return FALSE;
561     }
562     return TRUE;
563 }
564
565
566 /***********************************************************************
567  *           CreateDirectoryA   (KERNEL32.@)
568  */
569 BOOL WINAPI CreateDirectoryA( LPCSTR path,
570                                   LPSECURITY_ATTRIBUTES lpsecattribs )
571 {
572     UNICODE_STRING pathW;
573     BOOL ret = FALSE;
574
575     if (!path || !*path)
576     {
577         SetLastError(ERROR_PATH_NOT_FOUND);
578         return FALSE;
579     }
580
581     if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
582     {
583         ret = CreateDirectoryW(pathW.Buffer, lpsecattribs);
584         RtlFreeUnicodeString(&pathW);
585     }
586     else
587         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
588     return ret;
589 }
590
591
592 /***********************************************************************
593  *           CreateDirectoryExA   (KERNEL32.@)
594  */
595 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
596                                     LPSECURITY_ATTRIBUTES lpsecattribs)
597 {
598     return CreateDirectoryA(path,lpsecattribs);
599 }
600
601
602 /***********************************************************************
603  *           CreateDirectoryExW   (KERNEL32.@)
604  */
605 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
606                                     LPSECURITY_ATTRIBUTES lpsecattribs)
607 {
608     return CreateDirectoryW(path,lpsecattribs);
609 }
610
611
612 /***********************************************************************
613  *           RemoveDirectory   (KERNEL.145)
614  */
615 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
616 {
617     return (BOOL16)RemoveDirectoryA( path );
618 }
619
620
621 /***********************************************************************
622  *           RemoveDirectoryW   (KERNEL32.@)
623  */
624 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
625 {
626     DOS_FULL_NAME full_name;
627
628     if (!path)
629     {
630         SetLastError(ERROR_INVALID_PARAMETER);
631         return FALSE;
632     }
633
634     TRACE_(file)("%s\n", debugstr_w(path));
635
636     if (DOSFS_GetDevice( path ))
637     {
638         TRACE_(file)("cannot remove device %s!\n", debugstr_w(path));
639         SetLastError( ERROR_FILE_NOT_FOUND );
640         return FALSE;
641     }
642     if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
643     if (rmdir( full_name.long_name ) == -1)
644     {
645         FILE_SetDosError();
646         return FALSE;
647     }
648     return TRUE;
649 }
650
651
652 /***********************************************************************
653  *           RemoveDirectoryA   (KERNEL32.@)
654  */
655 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
656 {
657     UNICODE_STRING pathW;
658     BOOL ret = FALSE;
659
660     if (!path)
661     {
662         SetLastError(ERROR_INVALID_PARAMETER);
663         return FALSE;
664     }
665
666     if (RtlCreateUnicodeStringFromAsciiz(&pathW, path))
667     {
668         ret = RemoveDirectoryW(pathW.Buffer);
669         RtlFreeUnicodeString(&pathW);
670     }
671     else
672         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
673     return ret;
674 }
675
676
677 /***********************************************************************
678  *           DIR_TryPath
679  *
680  * Helper function for DIR_SearchPath.
681  */
682 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCWSTR name,
683                            DOS_FULL_NAME *full_name )
684 {
685     LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
686     LPWSTR p_s = full_name->short_name + strlenW(dir->short_name) + 1;
687
688     if ((p_s >= full_name->short_name + sizeof(full_name->short_name)/sizeof(full_name->short_name[0]) - 14) ||
689         (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
690     {
691         SetLastError( ERROR_PATH_NOT_FOUND );
692         return FALSE;
693     }
694     if (!DOSFS_FindUnixName( dir, name, p_l,
695                    sizeof(full_name->long_name) - (p_l - full_name->long_name),
696                    p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
697         return FALSE;
698
699     full_name->drive = dir->drive;
700     strcpy( full_name->long_name, dir->long_name );
701     p_l[-1] = '/';
702     strcpyW( full_name->short_name, dir->short_name );
703     p_s[-1] = '\\';
704     return TRUE;
705 }
706
707 static BOOL DIR_SearchSemicolonedPaths(LPCWSTR name, DOS_FULL_NAME *full_name, LPWSTR pathlist)
708 {
709     LPWSTR next, buffer = NULL;
710     INT len = strlenW(name), newlen, currlen = 0;
711     BOOL ret = FALSE;
712
713     next = pathlist;
714     while (!ret && next)
715     {
716         static const WCHAR bkslashW[] = {'\\',0};
717         LPWSTR cur = next;
718         while (*cur == ';') cur++;
719         if (!*cur) break;
720         next = strchrW( cur, ';' );
721         if (next) *next++ = '\0';
722         newlen = strlenW(cur) + len + 2;
723
724         if (newlen > currlen)
725         {
726             if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen * sizeof(WCHAR))))
727                 goto done;
728             currlen = newlen;
729         }
730
731         strcpyW( buffer, cur );
732         strcatW( buffer, bkslashW );
733         strcatW( buffer, name );
734         ret = DOSFS_GetFullName( buffer, TRUE, full_name );
735     }
736 done:
737     HeapFree( GetProcessHeap(), 0, buffer );
738     return ret;
739 }
740
741
742 /***********************************************************************
743  *           DIR_TryEnvironmentPath
744  *
745  * Helper function for DIR_SearchPath.
746  * Search in the specified path, or in $PATH if NULL.
747  */
748 static BOOL DIR_TryEnvironmentPath( LPCWSTR name, DOS_FULL_NAME *full_name, LPCWSTR envpath )
749 {
750     LPWSTR path;
751     BOOL ret = FALSE;
752     DWORD size;
753     static const WCHAR pathW[] = {'P','A','T','H',0};
754
755     size = envpath ? strlenW(envpath)+1 : GetEnvironmentVariableW( pathW, NULL, 0 );
756     if (!size) return FALSE;
757     if (!(path = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
758     if (envpath) strcpyW( path, envpath );
759     else if (!GetEnvironmentVariableW( pathW, path, size )) goto done;
760
761     ret = DIR_SearchSemicolonedPaths(name, full_name, path);
762
763 done:
764     HeapFree( GetProcessHeap(), 0, path );
765     return ret;
766 }
767
768
769 /***********************************************************************
770  *           DIR_TryModulePath
771  *
772  * Helper function for DIR_SearchPath.
773  */
774 static BOOL DIR_TryModulePath( LPCWSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
775 {
776     WCHAR bufferW[MAX_PATH];
777     LPWSTR p;
778
779     if (!win32)
780     {
781         char buffer[OFS_MAXPATHNAME];
782         if (!GetCurrentTask()) return FALSE;
783         if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
784             return FALSE;
785         MultiByteToWideChar(CP_ACP, 0, buffer, -1, bufferW, MAX_PATH);
786     } else {
787         if (!GetModuleFileNameW( 0, bufferW, MAX_PATH ) )
788             return FALSE;
789     }
790     if (!(p = strrchrW( bufferW, '\\' ))) return FALSE;
791     if (MAX_PATH - (++p - bufferW) <= strlenW(name)) return FALSE;
792     strcpyW( p, name );
793     return DOSFS_GetFullName( bufferW, TRUE, full_name );
794 }
795
796
797 /***********************************************************************
798  *           DIR_SearchPath
799  *
800  * Implementation of SearchPathA. 'win32' specifies whether the search
801  * order is Win16 (module path last) or Win32 (module path first).
802  *
803  * FIXME: should return long path names.
804  */
805 DWORD DIR_SearchPath( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
806                       DOS_FULL_NAME *full_name, BOOL win32 )
807 {
808     LPCWSTR p;
809     LPWSTR tmp = NULL;
810     BOOL ret = TRUE;
811
812     /* First check the supplied parameters */
813
814     p = strrchrW( name, '.' );
815     if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
816         ext = NULL;  /* Ignore the specified extension */
817     if (FILE_contains_pathW (name))
818         path = NULL;  /* Ignore path if name already contains a path */
819     if (path && !*path) path = NULL;  /* Ignore empty path */
820
821     /* Allocate a buffer for the file name and extension */
822
823     if (ext)
824     {
825         DWORD len = strlenW(name) + strlenW(ext);
826         if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
827         {
828             SetLastError( ERROR_OUTOFMEMORY );
829             return 0;
830         }
831         strcpyW( tmp, name );
832         strcatW( tmp, ext );
833         name = tmp;
834     }
835
836     /* If the name contains an explicit path, everything's easy */
837
838     if (FILE_contains_pathW(name))
839     {
840         ret = DOSFS_GetFullName( name, TRUE, full_name );
841         goto done;
842     }
843
844     /* Search in the specified path */
845
846     if (path)
847     {
848         ret = DIR_TryEnvironmentPath( name, full_name, path );
849         goto done;
850     }
851
852     /* Try the path of the current executable (for Win32 search order) */
853
854     if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
855
856     /* Try the current directory */
857
858     if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
859
860     /* Try the Windows system directory */
861
862     if (DIR_TryPath( &DIR_System, name, full_name ))
863         goto done;
864
865     /* Try the Windows directory */
866
867     if (DIR_TryPath( &DIR_Windows, name, full_name ))
868         goto done;
869
870     /* Try the path of the current executable (for Win16 search order) */
871
872     if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
873
874     /* Try all directories in path */
875
876     ret = DIR_TryEnvironmentPath( name, full_name, NULL );
877
878 done:
879     if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
880     return ret;
881 }
882
883
884 /***********************************************************************
885  * SearchPathW [KERNEL32.@]
886  *
887  * Searches for a specified file in the search path.
888  *
889  * PARAMS
890  *    path      [I] Path to search
891  *    name      [I] Filename to search for.
892  *    ext       [I] File extension to append to file name. The first
893  *                  character must be a period. This parameter is
894  *                  specified only if the filename given does not
895  *                  contain an extension.
896  *    buflen    [I] size of buffer, in characters
897  *    buffer    [O] buffer for found filename
898  *    lastpart  [O] address of pointer to last used character in
899  *                  buffer (the final '\')
900  *
901  * RETURNS
902  *    Success: length of string copied into buffer, not including
903  *             terminating null character. If the filename found is
904  *             longer than the length of the buffer, the length of the
905  *             filename is returned.
906  *    Failure: Zero
907  *
908  * NOTES
909  *    If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
910  *    (tested on NT 4.0)
911  */
912 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
913                           LPWSTR buffer, LPWSTR *lastpart )
914 {
915     LPSTR res;
916     DOS_FULL_NAME full_name;
917
918     if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
919     {
920         SetLastError(ERROR_FILE_NOT_FOUND);
921         return 0;
922     }
923
924 TRACE("found %s %s\n", full_name.long_name, debugstr_w(full_name.short_name));
925 TRACE("drive %c: root %s\n", 'A' + full_name.drive, DRIVE_GetRoot(full_name.drive));
926
927     lstrcpynW( buffer, full_name.short_name, buflen );
928     res = full_name.long_name +
929               strlen(DRIVE_GetRoot( full_name.drive ));
930     while (*res == '/') res++;
931     if (buflen)
932     {
933         LPWSTR p;
934         if (buflen > 3)
935         {
936             MultiByteToWideChar(DRIVE_GetCodepage(full_name.drive), 0,
937                                 res, -1, buffer + 3, buflen - 3);
938             buffer[buflen - 1] = 0;
939         }
940         for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
941         if (lastpart) *lastpart = strrchrW( buffer, '\\' ) + 1;
942     }
943     TRACE("Returning %s\n", debugstr_w(buffer) );
944     return strlenW(buffer);
945 }
946
947
948 /***********************************************************************
949  *           SearchPathA   (KERNEL32.@)
950  */
951 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
952                           DWORD buflen, LPSTR buffer, LPSTR *lastpart )
953 {
954     UNICODE_STRING pathW, nameW, extW;
955     WCHAR bufferW[MAX_PATH];
956     DWORD ret, retW;
957
958     if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
959     else pathW.Buffer = NULL;
960     if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
961     else nameW.Buffer = NULL;
962     if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
963     else extW.Buffer = NULL;
964
965     retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
966
967     if (!retW)
968         ret = 0;
969     else if (retW > MAX_PATH)
970     {
971         SetLastError(ERROR_FILENAME_EXCED_RANGE);
972         ret = 0;
973     }
974     else
975     {
976         ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
977         if (buflen >= ret)
978         {
979             WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
980             ret--; /* length without 0 */
981             if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
982         }
983     }
984
985     RtlFreeUnicodeString(&pathW);
986     RtlFreeUnicodeString(&nameW);
987     RtlFreeUnicodeString(&extW);
988     return ret;
989 }
990
991
992 /***********************************************************************
993  *           search_alternate_path
994  *
995  *
996  * FIXME: should return long path names.?
997  */
998 static BOOL search_alternate_path(LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
999                                   DOS_FULL_NAME *full_name)
1000 {
1001     LPCWSTR p;
1002     LPWSTR tmp = NULL;
1003     BOOL ret = TRUE;
1004
1005     /* First check the supplied parameters */
1006
1007     p = strrchrW( name, '.' );
1008     if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
1009         ext = NULL;  /* Ignore the specified extension */
1010
1011     /* Allocate a buffer for the file name and extension */
1012
1013     if (ext)
1014     {
1015         DWORD len = strlenW(name) + strlenW(ext);
1016         if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
1017         {
1018             SetLastError( ERROR_OUTOFMEMORY );
1019             return 0;
1020         }
1021         strcpyW( tmp, name );
1022         strcatW( tmp, ext );
1023         name = tmp;
1024     }
1025
1026     if (DIR_TryEnvironmentPath (name, full_name, dll_path))
1027         ;
1028     else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
1029         ;
1030     else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
1031         ;
1032     else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
1033         ;
1034     else
1035         ret = DIR_TryEnvironmentPath( name, full_name, NULL );
1036
1037     if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
1038     return ret;
1039 }
1040
1041
1042 /***********************************************************************
1043  * DIR_SearchAlternatePath
1044  *
1045  * Searches for a specified file in the search path.
1046  *
1047  * PARAMS
1048  *    dll_path  [I] Path to search
1049  *    name      [I] Filename to search for.
1050  *    ext       [I] File extension to append to file name. The first
1051  *                  character must be a period. This parameter is
1052  *                  specified only if the filename given does not
1053  *                  contain an extension.
1054  *    buflen    [I] size of buffer, in characters
1055  *    buffer    [O] buffer for found filename
1056  *
1057  * RETURNS
1058  *    Success: length of string copied into buffer, not including
1059  *             terminating null character. If the filename found is
1060  *             longer than the length of the buffer, the length of the
1061  *             filename is returned.
1062  *    Failure: Zero
1063  *
1064  * NOTES
1065  *    If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
1066  *
1067  * FIXME: convert to unicode
1068  */
1069 DWORD DIR_SearchAlternatePath( LPCWSTR dll_path, LPCWSTR name, LPCWSTR ext,
1070                                DWORD buflen, LPWSTR buffer )
1071 {
1072     DOS_FULL_NAME full_name;
1073     DWORD ret = 0;
1074
1075     if (search_alternate_path( dll_path, name, ext, &full_name))
1076     {
1077         lstrcpynW( buffer, full_name.short_name, buflen );
1078         ret = strlenW(buffer);
1079     }
1080     else
1081         SetLastError(ERROR_FILE_NOT_FOUND);
1082
1083     TRACE("Returning %ld\n", ret );
1084     return ret;
1085 }