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