- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / files / directory.c
1 /*
2  * DOS directories functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
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>
15
16 #include "windows.h"
17 #include "winerror.h"
18 #include "process.h"
19 #include "drive.h"
20 #include "file.h"
21 #include "heap.h"
22 #include "msdos.h"
23 #include "options.h"
24 #include "debug.h"
25
26 static DOS_FULL_NAME DIR_Windows;
27 static DOS_FULL_NAME DIR_System;
28
29
30 /***********************************************************************
31  *           DIR_GetPath
32  *
33  * Get a path name from the wine.ini file and make sure it is valid.
34  */
35 static int DIR_GetPath( const char *keyname, const char *defval,
36                         DOS_FULL_NAME *full_name )
37 {
38     char path[MAX_PATHNAME_LEN];
39     BY_HANDLE_FILE_INFORMATION info;
40
41     PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
42     if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
43         !FILE_Stat( full_name->long_name, &info ) ||
44         !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
45     {
46         MSG("Invalid path '%s' for %s directory\n", path, keyname);
47         return 0;
48     }
49     return 1;
50 }
51
52
53 /***********************************************************************
54  *           DIR_Init
55  */
56 int DIR_Init(void)
57 {
58     char path[MAX_PATHNAME_LEN];
59     DOS_FULL_NAME tmp_dir;
60     int drive;
61     const char *cwd;
62
63     if (!getcwd( path, MAX_PATHNAME_LEN ))
64     {
65         perror( "Could not get current directory" );
66         return 0;
67     }
68     cwd = path;
69     if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
70     {
71         MSG("Warning: could not find DOS drive for cwd %s; "
72             "starting in windows directory.\n", cwd );
73     }
74     else
75     {
76         DRIVE_SetCurrentDrive( drive );
77         DRIVE_Chdir( drive, cwd );
78     }
79
80     if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
81         !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
82         !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
83     {
84         PROFILE_UsageWineIni();
85         return 0;
86     }
87     if (-1 == access( tmp_dir.long_name, W_OK ))
88     {
89         if (errno==EACCES)
90         {
91                 MSG("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
92                 PROFILE_UsageWineIni();
93         }
94         else
95                 MSG("Warning: Access to Temporary Directory failed (%s).\n",
96                     strerror(errno));
97     }
98
99     if (drive == -1)
100     {
101         drive = DIR_Windows.drive;
102         DRIVE_SetCurrentDrive( drive );
103         DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
104     }
105
106     PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
107                              path, sizeof(path) );
108
109     /* Set the environment variables */
110
111     SetEnvironmentVariable32A( "PATH", path );
112     SetEnvironmentVariable32A( "COMSPEC", "c:\\command.com" );
113     SetEnvironmentVariable32A( "TEMP", tmp_dir.short_name );
114     SetEnvironmentVariable32A( "windir", DIR_Windows.short_name );
115     SetEnvironmentVariable32A( "winsysdir", DIR_System.short_name );
116
117     TRACE(dosfs, "WindowsDir = %s (%s)\n",
118           DIR_Windows.short_name, DIR_Windows.long_name );
119     TRACE(dosfs, "SystemDir  = %s (%s)\n",
120           DIR_System.short_name, DIR_System.long_name );
121     TRACE(dosfs, "TempDir    = %s (%s)\n",
122           tmp_dir.short_name, tmp_dir.long_name );
123     TRACE(dosfs, "Path       = %s\n", path );
124     TRACE(dosfs, "Cwd        = %c:\\%s\n",
125           'A' + drive, DRIVE_GetDosCwd( drive ) );
126
127     return 1;
128 }
129
130
131 /***********************************************************************
132  *           GetTempPath32A   (KERNEL32.292)
133  */
134 UINT32 WINAPI GetTempPath32A( UINT32 count, LPSTR path )
135 {
136     UINT32 ret;
137     if (!(ret = GetEnvironmentVariable32A( "TMP", path, count )))
138         if (!(ret = GetEnvironmentVariable32A( "TEMP", path, count )))
139             if (!(ret = GetCurrentDirectory32A( count, path )))
140                 return 0;
141     if (count && (ret < count - 1) && (path[ret-1] != '\\'))
142     {
143         path[ret++] = '\\';
144         path[ret]   = '\0';
145     }
146     return ret;
147 }
148
149
150 /***********************************************************************
151  *           GetTempPath32W   (KERNEL32.293)
152  */
153 UINT32 WINAPI GetTempPath32W( UINT32 count, LPWSTR path )
154 {
155     static const WCHAR tmp[]  = { 'T', 'M', 'P', 0 };
156     static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
157     UINT32 ret;
158     if (!(ret = GetEnvironmentVariable32W( tmp, path, count )))
159         if (!(ret = GetEnvironmentVariable32W( temp, path, count )))
160             if (!(ret = GetCurrentDirectory32W( count, path )))
161                 return 0;
162     if (count && (ret < count - 1) && (path[ret-1] != '\\'))
163     {
164         path[ret++] = '\\';
165         path[ret]   = '\0';
166     }
167     return ret;
168 }
169
170
171 /***********************************************************************
172  *           DIR_GetWindowsUnixDir
173  */
174 UINT32 DIR_GetWindowsUnixDir( LPSTR path, UINT32 count )
175 {
176     if (path) lstrcpyn32A( path, DIR_Windows.long_name, count );
177     return strlen( DIR_Windows.long_name );
178 }
179
180
181 /***********************************************************************
182  *           DIR_GetSystemUnixDir
183  */
184 UINT32 DIR_GetSystemUnixDir( LPSTR path, UINT32 count )
185 {
186     if (path) lstrcpyn32A( path, DIR_System.long_name, count );
187     return strlen( DIR_System.long_name );
188 }
189
190
191 /***********************************************************************
192  *           GetTempDrive   (KERNEL.92)
193  */
194 BYTE WINAPI GetTempDrive( BYTE ignored )
195 {
196     char *buffer;
197     BYTE ret;
198     UINT32 len = GetTempPath32A( 0, NULL );
199
200     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
201       return DRIVE_GetCurrentDrive() + 'A';
202
203     /* FIXME: apparently Windows does something with the ignored byte */
204     if (!GetTempPath32A( len, buffer )) buffer[0] = 'C';
205     ret = buffer[0];
206     HeapFree( GetProcessHeap(), 0, buffer );
207     return toupper(ret);
208 }
209
210
211 UINT32 WINAPI WIN16_GetTempDrive( BYTE ignored )
212 {
213     /* A closer look at krnl386.exe shows what the SDK doesn't mention:
214      *
215      * returns:
216      *   AL: driveletter
217      *   AH: ':'                - yes, some kernel code even does stosw with
218      *                            the returned AX.
219      *   DX: 1 for success
220      */
221     return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
222 }
223
224
225 /***********************************************************************
226  *           GetWindowsDirectory16   (KERNEL.134)
227  */
228 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
229 {
230     return (UINT16)GetWindowsDirectory32A( path, count );
231 }
232
233
234 /***********************************************************************
235  *           GetWindowsDirectory32A   (KERNEL32.311)
236  */
237 UINT32 WINAPI GetWindowsDirectory32A( LPSTR path, UINT32 count )
238 {
239     if (path) lstrcpyn32A( path, DIR_Windows.short_name, count );
240     return strlen( DIR_Windows.short_name );
241 }
242
243
244 /***********************************************************************
245  *           GetWindowsDirectory32W   (KERNEL32.312)
246  */
247 UINT32 WINAPI GetWindowsDirectory32W( LPWSTR path, UINT32 count )
248 {
249     if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
250     return strlen( DIR_Windows.short_name );
251 }
252
253
254 /***********************************************************************
255  *           GetSystemDirectory16   (KERNEL.135)
256  */
257 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
258 {
259     return (UINT16)GetSystemDirectory32A( path, count );
260 }
261
262
263 /***********************************************************************
264  *           GetSystemDirectory32A   (KERNEL32.282)
265  */
266 UINT32 WINAPI GetSystemDirectory32A( LPSTR path, UINT32 count )
267 {
268     if (path) lstrcpyn32A( path, DIR_System.short_name, count );
269     return strlen( DIR_System.short_name );
270 }
271
272
273 /***********************************************************************
274  *           GetSystemDirectory32W   (KERNEL32.283)
275  */
276 UINT32 WINAPI GetSystemDirectory32W( LPWSTR path, UINT32 count )
277 {
278     if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
279     return strlen( DIR_System.short_name );
280 }
281
282
283 /***********************************************************************
284  *           CreateDirectory16   (KERNEL.144)
285  */
286 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
287 {
288     TRACE(file,"(%s,%p)\n", path, dummy );
289     return (BOOL16)CreateDirectory32A( path, NULL );
290 }
291
292
293 /***********************************************************************
294  *           CreateDirectory32A   (KERNEL32.39)
295  */
296 BOOL32 WINAPI CreateDirectory32A( LPCSTR path,
297                                   LPSECURITY_ATTRIBUTES lpsecattribs )
298 {
299     DOS_FULL_NAME full_name;
300
301     TRACE(file, "(%s,%p)\n", path, lpsecattribs );
302     if (DOSFS_GetDevice( path ))
303     {
304         TRACE(file, "cannot use device '%s'!\n",path);
305         SetLastError( ERROR_ACCESS_DENIED );
306         return FALSE;
307     }
308     if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
309     if ((mkdir( full_name.long_name, 0777 ) == -1) && (errno != EEXIST))
310     {
311       WARN (file, "Errno %i trying to create directory %s.\n", errno, full_name.long_name);
312         FILE_SetDosError();
313         return FALSE;
314     }
315     return TRUE;
316 }
317
318
319 /***********************************************************************
320  *           CreateDirectory32W   (KERNEL32.42)
321  */
322 BOOL32 WINAPI CreateDirectory32W( LPCWSTR path,
323                                   LPSECURITY_ATTRIBUTES lpsecattribs )
324 {
325     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
326     BOOL32 ret = CreateDirectory32A( xpath, lpsecattribs );
327     HeapFree( GetProcessHeap(), 0, xpath );
328     return ret;
329 }
330
331
332 /***********************************************************************
333  *           CreateDirectoryEx32A   (KERNEL32.40)
334  */
335 BOOL32 WINAPI CreateDirectoryEx32A( LPCSTR template, LPCSTR path,
336                                     LPSECURITY_ATTRIBUTES lpsecattribs)
337 {
338     return CreateDirectory32A(path,lpsecattribs);
339 }
340
341
342 /***********************************************************************
343  *           CreateDirectoryEx32W   (KERNEL32.41)
344  */
345 BOOL32 WINAPI CreateDirectoryEx32W( LPCWSTR template, LPCWSTR path,
346                                     LPSECURITY_ATTRIBUTES lpsecattribs)
347 {
348     return CreateDirectory32W(path,lpsecattribs);
349 }
350
351
352 /***********************************************************************
353  *           RemoveDirectory16   (KERNEL)
354  */
355 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
356 {
357     return (BOOL16)RemoveDirectory32A( path );
358 }
359
360
361 /***********************************************************************
362  *           RemoveDirectory32A   (KERNEL32.437)
363  */
364 BOOL32 WINAPI RemoveDirectory32A( LPCSTR path )
365 {
366     DOS_FULL_NAME full_name;
367
368     TRACE(file, "'%s'\n", path );
369
370     if (DOSFS_GetDevice( path ))
371     {
372         TRACE(file, "cannot remove device '%s'!\n", path);
373         SetLastError( ERROR_FILE_NOT_FOUND );
374         return FALSE;
375     }
376     if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
377     if (rmdir( full_name.long_name ) == -1)
378     {
379         FILE_SetDosError();
380         return FALSE;
381     }
382     return TRUE;
383 }
384
385
386 /***********************************************************************
387  *           RemoveDirectory32W   (KERNEL32.438)
388  */
389 BOOL32 WINAPI RemoveDirectory32W( LPCWSTR path )
390 {
391     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
392     BOOL32 ret = RemoveDirectory32A( xpath );
393     HeapFree( GetProcessHeap(), 0, xpath );
394     return ret;
395 }
396
397
398 /***********************************************************************
399  *           DIR_TryPath
400  *
401  * Helper function for DIR_SearchPath.
402  */
403 static BOOL32 DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
404                            DOS_FULL_NAME *full_name )
405 {
406     LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
407     LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
408
409     if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
410         (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
411     {
412         SetLastError( ERROR_PATH_NOT_FOUND );
413         return FALSE;
414     }
415     if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
416                    sizeof(full_name->long_name) - (p_l - full_name->long_name),
417                    p_s, DRIVE_GetFlags( dir->drive ) ))
418         return FALSE;
419     strcpy( full_name->long_name, dir->long_name );
420     p_l[-1] = '/';
421     strcpy( full_name->short_name, dir->short_name );
422     p_s[-1] = '\\';
423     return TRUE;
424 }
425
426
427 /***********************************************************************
428  *           DIR_TryEnvironmentPath
429  *
430  * Helper function for DIR_SearchPath.
431  */
432 static BOOL32 DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
433 {
434     LPSTR path, next, buffer;
435     BOOL32 ret = FALSE;
436     INT32 len = strlen(name);
437     DWORD size = GetEnvironmentVariable32A( "PATH", NULL, 0 );
438
439     if (!size) return FALSE;
440     if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
441     if (!GetEnvironmentVariable32A( "PATH", path, size )) goto done;
442     next = path;
443     while (!ret && next)
444     {
445         LPSTR cur = next;
446         while (*cur == ';') cur++;
447         if (!*cur) break;
448         next = strchr( cur, ';' );
449         if (next) *next++ = '\0';
450         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
451             goto done;
452         strcpy( buffer, cur );
453         strcat( buffer, "\\" );
454         strcat( buffer, name );
455         ret = DOSFS_GetFullName( buffer, TRUE, full_name );
456         HeapFree( GetProcessHeap(), 0, buffer );
457     }
458
459 done:
460     HeapFree( GetProcessHeap(), 0, path );
461     return ret;
462 }
463
464
465 /***********************************************************************
466  *           DIR_TryModulePath
467  *
468  * Helper function for DIR_SearchPath.
469  */
470 static BOOL32 DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
471 {
472     PDB32       *pdb = PROCESS_Current();
473
474     /* FIXME: for now, GetModuleFileName32A can't return more */
475     /* than OFS_MAXPATHNAME. This may change with Win32. */
476
477     char buffer[OFS_MAXPATHNAME];
478     LPSTR p;
479
480     if (pdb->flags & PDB32_WIN16_PROC) {
481         if (!GetCurrentTask()) return FALSE;
482         if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
483                 buffer[0]='\0';
484     } else {
485         if (!GetModuleFileName32A( 0, buffer, sizeof(buffer) ))
486                 buffer[0]='\0';
487     }
488     if (!(p = strrchr( buffer, '\\' ))) return FALSE;
489     if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
490     strcpy( p, name );
491     return DOSFS_GetFullName( buffer, TRUE, full_name );
492 }
493
494
495 /***********************************************************************
496  *           DIR_SearchPath
497  *
498  * Implementation of SearchPath32A. 'win32' specifies whether the search
499  * order is Win16 (module path last) or Win32 (module path first).
500  *
501  * FIXME: should return long path names.
502  */
503 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
504                       DOS_FULL_NAME *full_name, BOOL32 win32 )
505 {
506     DWORD len;
507     LPCSTR p;
508     LPSTR tmp = NULL;
509     BOOL32 ret = TRUE;
510
511     /* First check the supplied parameters */
512
513     p = strrchr( name, '.' );
514     if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
515         ext = NULL;  /* Ignore the specified extension */
516     if ((*name && (name[1] == ':')) ||
517         strchr( name, '/' ) || strchr( name, '\\' ))
518         path = NULL;  /* Ignore path if name already contains a path */
519     if (path && !*path) path = NULL;  /* Ignore empty path */
520
521     len = strlen(name);
522     if (ext) len += strlen(ext);
523     if (path) len += strlen(path) + 1;
524
525     /* Allocate a buffer for the file name and extension */
526
527     if (path || ext)
528     {
529         if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
530         {
531             SetLastError( ERROR_OUTOFMEMORY );
532             return 0;
533         }
534         if (path)
535         {
536             strcpy( tmp, path );
537             strcat( tmp, "\\" );
538             strcat( tmp, name );
539         }
540         else strcpy( tmp, name );
541         if (ext) strcat( tmp, ext );
542         name = tmp;
543     }
544     
545     /* If we have an explicit path, everything's easy */
546
547     if (path || (*name && (name[1] == ':')) ||
548         strchr( name, '/' ) || strchr( name, '\\' ))
549     {
550         ret = DOSFS_GetFullName( name, TRUE, full_name );
551         goto done;
552     }
553
554     /* Try the path of the current executable (for Win32 search order) */
555
556     if (win32 && DIR_TryModulePath( name, full_name )) goto done;
557
558     /* Try the current directory */
559
560     if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
561
562     /* Try the Windows directory */
563
564     if (DIR_TryPath( &DIR_Windows, name, full_name ))
565         goto done;
566
567     /* Try the Windows system directory */
568
569     if (DIR_TryPath( &DIR_System, name, full_name ))
570         goto done;
571
572     /* Try the path of the current executable (for Win16 search order) */
573
574     if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
575
576     /* Try all directories in path */
577
578     ret = DIR_TryEnvironmentPath( name, full_name );
579
580 done:
581     if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
582     return ret;
583 }
584
585
586 /***********************************************************************
587  * SearchPath32A [KERNEL32.447]
588  *
589  * Searches for a specified file in the search path.
590  *
591  * PARAMS
592  *    path      [I] Path to search
593  *    name      [I] Filename to search for.
594  *    ext       [I] File extension to append to file name. The first
595  *                  character must be a period. This parameter is
596  *                  specified only if the filename given does not
597  *                  contain an extension.
598  *    buflen    [I] size of buffer, in characters
599  *    buffer    [O] buffer for found filename
600  *    lastpart  [O] address of pointer to last used character in
601  *                  buffer (the final '\')
602  *
603  * RETURNS
604  *    Success: length of string copied into buffer, not including
605  *             terminating null character. If the filename found is
606  *             longer than the length of the buffer, the length of the
607  *             filename is returned.
608  *    Failure: Zero
609  * 
610  * NOTES
611  *    Should call SetLastError(but currently doesn't).
612  */
613 DWORD WINAPI SearchPath32A( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
614                             LPSTR buffer, LPSTR *lastpart )
615 {
616     LPSTR p, res;
617     DOS_FULL_NAME full_name;
618
619     if (!DIR_SearchPath( path, name, ext, &full_name, TRUE )) return 0;
620     lstrcpyn32A( buffer, full_name.short_name, buflen );
621     res = full_name.long_name +
622               strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
623     while (*res == '/') res++;
624     if (buflen)
625     {
626         if (buflen > 3) lstrcpyn32A( buffer + 3, res, buflen - 3 );
627         for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
628         if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
629     }
630     TRACE(dosfs, "Returning %d\n", (*res ? strlen(res) + 2 : 3));
631     return *res ? strlen(res) + 2 : 3;
632 }
633
634
635 /***********************************************************************
636  *           SearchPath32W   (KERNEL32.448)
637  */
638 DWORD WINAPI SearchPath32W( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
639                             DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
640 {
641     LPWSTR p;
642     LPSTR res;
643     DOS_FULL_NAME full_name;
644
645     LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
646     LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
647     LPSTR extA  = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
648     DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
649     HeapFree( GetProcessHeap(), 0, extA );
650     HeapFree( GetProcessHeap(), 0, nameA );
651     HeapFree( GetProcessHeap(), 0, pathA );
652     if (!ret) return 0;
653
654     lstrcpynAtoW( buffer, full_name.short_name, buflen );
655     res = full_name.long_name +
656               strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
657     while (*res == '/') res++;
658     if (buflen)
659     {
660         if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 ); 
661         for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
662         if (lastpart)
663         {
664             for (p = *lastpart = buffer; *p; p++)
665                 if (*p == '\\') *lastpart = p + 1;
666         }
667     }
668     return *res ? strlen(res) + 2 : 3;
669 }
670
671