Further server optimizations:
[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 "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"
27
28 DECLARE_DEBUG_CHANNEL(dosfs)
29 DECLARE_DEBUG_CHANNEL(file)
30
31 static DOS_FULL_NAME DIR_Windows;
32 static DOS_FULL_NAME DIR_System;
33
34
35 /***********************************************************************
36  *           DIR_GetPath
37  *
38  * Get a path name from the wine.ini file and make sure it is valid.
39  */
40 static int DIR_GetPath( const char *keyname, const char *defval,
41                         DOS_FULL_NAME *full_name )
42 {
43     char path[MAX_PATHNAME_LEN];
44     BY_HANDLE_FILE_INFORMATION info;
45
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))
50     {
51         MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
52         return 0;
53     }
54     return 1;
55 }
56
57
58 /***********************************************************************
59  *           DIR_Init
60  */
61 int DIR_Init(void)
62 {
63     char path[MAX_PATHNAME_LEN];
64     DOS_FULL_NAME tmp_dir;
65     int drive;
66     const char *cwd;
67
68     if (!getcwd( path, MAX_PATHNAME_LEN ))
69     {
70         perror( "Could not get current directory" );
71         return 0;
72     }
73     cwd = path;
74     if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
75     {
76         MESSAGE("Warning: could not find wine.conf [Drive x] entry "
77             "for current working directory %s; "
78             "starting in windows directory.\n", cwd );
79     }
80     else
81     {
82         DRIVE_SetCurrentDrive( drive );
83         DRIVE_Chdir( drive, cwd );
84     }
85
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 )))
89     {
90         PROFILE_UsageWineIni();
91         return 0;
92     }
93     if (-1 == access( tmp_dir.long_name, W_OK ))
94     {
95         if (errno==EACCES)
96         {
97                 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
98                 PROFILE_UsageWineIni();
99         }
100         else
101                 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
102                     strerror(errno));
103     }
104
105     if (drive == -1)
106     {
107         drive = DIR_Windows.drive;
108         DRIVE_SetCurrentDrive( drive );
109         DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
110     }
111
112     PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
113                              path, sizeof(path) );
114
115     /* Set the environment variables */
116
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 );
122
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 ) );
132
133     return 1;
134 }
135
136
137 /***********************************************************************
138  *           GetTempPath32A   (KERNEL32.292)
139  */
140 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
141 {
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] != '\\'))
148     {
149         path[ret++] = '\\';
150         path[ret]   = '\0';
151     }
152     return ret;
153 }
154
155
156 /***********************************************************************
157  *           GetTempPath32W   (KERNEL32.293)
158  */
159 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
160 {
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] != '\\'))
169     {
170         path[ret++] = '\\';
171         path[ret]   = '\0';
172     }
173     return ret;
174 }
175
176
177 /***********************************************************************
178  *           DIR_GetWindowsUnixDir
179  */
180 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
181 {
182     if (path) lstrcpynA( path, DIR_Windows.long_name, count );
183     return strlen( DIR_Windows.long_name );
184 }
185
186
187 /***********************************************************************
188  *           DIR_GetSystemUnixDir
189  */
190 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
191 {
192     if (path) lstrcpynA( path, DIR_System.long_name, count );
193     return strlen( DIR_System.long_name );
194 }
195
196
197 /***********************************************************************
198  *           GetTempDrive   (KERNEL.92)
199  */
200 BYTE WINAPI GetTempDrive( BYTE ignored )
201 {
202     char *buffer;
203     BYTE ret;
204     UINT len = GetTempPathA( 0, NULL );
205
206     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
207       return DRIVE_GetCurrentDrive() + 'A';
208
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);
214 }
215
216
217 UINT WINAPI WIN16_GetTempDrive( BYTE ignored )
218 {
219     /* A closer look at krnl386.exe shows what the SDK doesn't mention:
220      *
221      * returns:
222      *   AL: driveletter
223      *   AH: ':'                - yes, some kernel code even does stosw with
224      *                            the returned AX.
225      *   DX: 1 for success
226      */
227     return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 );
228 }
229
230
231 /***********************************************************************
232  *           GetWindowsDirectory16   (KERNEL.134)
233  */
234 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
235 {
236     return (UINT16)GetWindowsDirectoryA( path, count );
237 }
238
239
240 /***********************************************************************
241  *           GetWindowsDirectory32A   (KERNEL32.311)
242  */
243 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
244 {
245     if (path) lstrcpynA( path, DIR_Windows.short_name, count );
246     return strlen( DIR_Windows.short_name );
247 }
248
249
250 /***********************************************************************
251  *           GetWindowsDirectory32W   (KERNEL32.312)
252  */
253 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
254 {
255     if (path) lstrcpynAtoW( path, DIR_Windows.short_name, count );
256     return strlen( DIR_Windows.short_name );
257 }
258
259
260 /***********************************************************************
261  *           GetSystemDirectory16   (KERNEL.135)
262  */
263 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
264 {
265     return (UINT16)GetSystemDirectoryA( path, count );
266 }
267
268
269 /***********************************************************************
270  *           GetSystemDirectory32A   (KERNEL32.282)
271  */
272 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
273 {
274     if (path) lstrcpynA( path, DIR_System.short_name, count );
275     return strlen( DIR_System.short_name );
276 }
277
278
279 /***********************************************************************
280  *           GetSystemDirectory32W   (KERNEL32.283)
281  */
282 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
283 {
284     if (path) lstrcpynAtoW( path, DIR_System.short_name, count );
285     return strlen( DIR_System.short_name );
286 }
287
288
289 /***********************************************************************
290  *           CreateDirectory16   (KERNEL.144)
291  */
292 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
293 {
294     TRACE_(file)("(%s,%p)\n", path, dummy );
295     return (BOOL16)CreateDirectoryA( path, NULL );
296 }
297
298
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)
308  */
309 BOOL WINAPI CreateDirectoryA( LPCSTR path,
310                                   LPSECURITY_ATTRIBUTES lpsecattribs )
311 {
312     DOS_FULL_NAME full_name;
313
314     TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
315     if (DOSFS_GetDevice( path ))
316     {
317         TRACE_(file)("cannot use device '%s'!\n",path);
318         SetLastError( ERROR_ACCESS_DENIED );
319         return FALSE;
320     }
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;
330         }
331         return FALSE;
332     }
333     return TRUE;
334 }
335
336
337 /***********************************************************************
338  *           CreateDirectory32W   (KERNEL32.42)
339  */
340 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
341                                   LPSECURITY_ATTRIBUTES lpsecattribs )
342 {
343     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
344     BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
345     HeapFree( GetProcessHeap(), 0, xpath );
346     return ret;
347 }
348
349
350 /***********************************************************************
351  *           CreateDirectoryEx32A   (KERNEL32.40)
352  */
353 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
354                                     LPSECURITY_ATTRIBUTES lpsecattribs)
355 {
356     return CreateDirectoryA(path,lpsecattribs);
357 }
358
359
360 /***********************************************************************
361  *           CreateDirectoryEx32W   (KERNEL32.41)
362  */
363 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
364                                     LPSECURITY_ATTRIBUTES lpsecattribs)
365 {
366     return CreateDirectoryW(path,lpsecattribs);
367 }
368
369
370 /***********************************************************************
371  *           RemoveDirectory16   (KERNEL)
372  */
373 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
374 {
375     return (BOOL16)RemoveDirectoryA( path );
376 }
377
378
379 /***********************************************************************
380  *           RemoveDirectory32A   (KERNEL32.437)
381  */
382 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
383 {
384     DOS_FULL_NAME full_name;
385
386     TRACE_(file)("'%s'\n", path );
387
388     if (DOSFS_GetDevice( path ))
389     {
390         TRACE_(file)("cannot remove device '%s'!\n", path);
391         SetLastError( ERROR_FILE_NOT_FOUND );
392         return FALSE;
393     }
394     if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
395     if (rmdir( full_name.long_name ) == -1)
396     {
397         FILE_SetDosError();
398         return FALSE;
399     }
400     return TRUE;
401 }
402
403
404 /***********************************************************************
405  *           RemoveDirectory32W   (KERNEL32.438)
406  */
407 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
408 {
409     LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
410     BOOL ret = RemoveDirectoryA( xpath );
411     HeapFree( GetProcessHeap(), 0, xpath );
412     return ret;
413 }
414
415
416 /***********************************************************************
417  *           DIR_TryPath
418  *
419  * Helper function for DIR_SearchPath.
420  */
421 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
422                            DOS_FULL_NAME *full_name )
423 {
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;
426
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))
429     {
430         SetLastError( ERROR_PATH_NOT_FOUND );
431         return FALSE;
432     }
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;
442 }
443
444
445 /***********************************************************************
446  *           DIR_TryEnvironmentPath
447  *
448  * Helper function for DIR_SearchPath.
449  */
450 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name )
451 {
452     LPSTR path, next, buffer;
453     BOOL ret = FALSE;
454     INT len = strlen(name);
455     DWORD size = GetEnvironmentVariableA( "PATH", NULL, 0 );
456
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)
462     {
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 );
475     }
476
477 done:
478     HeapFree( GetProcessHeap(), 0, path );
479     return ret;
480 }
481
482
483 /***********************************************************************
484  *           DIR_TryModulePath
485  *
486  * Helper function for DIR_SearchPath.
487  */
488 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name )
489 {
490     PDB *pdb = PROCESS_Current();
491
492     /* FIXME: for now, GetModuleFileName32A can't return more */
493     /* than OFS_MAXPATHNAME. This may change with Win32. */
494
495     char buffer[OFS_MAXPATHNAME];
496     LPSTR p;
497
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';
505     }
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 );
510 }
511
512
513 /***********************************************************************
514  *           DIR_SearchPath
515  *
516  * Implementation of SearchPath32A. 'win32' specifies whether the search
517  * order is Win16 (module path last) or Win32 (module path first).
518  *
519  * FIXME: should return long path names.
520  */
521 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
522                       DOS_FULL_NAME *full_name, BOOL win32 )
523 {
524     DWORD len;
525     LPCSTR p;
526     LPSTR tmp = NULL;
527     BOOL ret = TRUE;
528
529     /* First check the supplied parameters */
530
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 */
538
539     len = strlen(name);
540     if (ext) len += strlen(ext);
541     if (path) len += strlen(path) + 1;
542
543     /* Allocate a buffer for the file name and extension */
544
545     if (path || ext)
546     {
547         if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
548         {
549             SetLastError( ERROR_OUTOFMEMORY );
550             return 0;
551         }
552         if (path)
553         {
554             strcpy( tmp, path );
555             strcat( tmp, "\\" );
556             strcat( tmp, name );
557         }
558         else strcpy( tmp, name );
559         if (ext) strcat( tmp, ext );
560         name = tmp;
561     }
562     
563     /* If we have an explicit path, everything's easy */
564
565     if (path || (*name && (name[1] == ':')) ||
566         strchr( name, '/' ) || strchr( name, '\\' ))
567     {
568         ret = DOSFS_GetFullName( name, TRUE, full_name );
569         goto done;
570     }
571
572     /* Try the path of the current executable (for Win32 search order) */
573
574     if (win32 && DIR_TryModulePath( name, full_name )) goto done;
575
576     /* Try the current directory */
577
578     if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
579
580     /* Try the Windows system directory */
581
582     if (DIR_TryPath( &DIR_System, name, full_name ))
583         goto done;
584
585     /* Try the Windows directory */
586
587     if (DIR_TryPath( &DIR_Windows, name, full_name ))
588         goto done;
589
590     /* Try the path of the current executable (for Win16 search order) */
591
592     if (!win32 && DIR_TryModulePath( name, full_name )) goto done;
593
594     /* Try all directories in path */
595
596     ret = DIR_TryEnvironmentPath( name, full_name );
597
598 done:
599     if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
600     return ret;
601 }
602
603
604 /***********************************************************************
605  * SearchPath32A [KERNEL32.447]
606  *
607  * Searches for a specified file in the search path.
608  *
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 '\')
620  *
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
627  * 
628  * NOTES
629  *    Should call SetLastError(but currently doesn't).
630  */
631 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
632                             LPSTR buffer, LPSTR *lastpart )
633 {
634     LPSTR p, res;
635     DOS_FULL_NAME full_name;
636
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)
643     {
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;
647     }
648     TRACE_(dosfs)("Returning %d\n", strlen(res) + 3 );
649     return strlen(res) + 3;
650 }
651
652
653 /***********************************************************************
654  *           SearchPath32W   (KERNEL32.448)
655  */
656 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
657                             DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
658 {
659     LPWSTR p;
660     LPSTR res;
661     DOS_FULL_NAME full_name;
662
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;
671
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)
677     {
678         if (buflen > 3) lstrcpynAtoW( buffer + 3, res, buflen - 3 ); 
679         for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
680         if (lastpart)
681         {
682             for (p = *lastpart = buffer; *p; p++)
683                 if (*p == '\\') *lastpart = p + 1;
684         }
685     }
686     return strlen(res) + 3;
687 }
688
689