Compile fix for gcc 2.7.2.3.
[wine/multimedia.git] / files / directory.c
blob7bf312fba0e0c885dfc2f02f0725bfdadedb31f4
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
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
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"
34 DEFAULT_DEBUG_CHANNEL(dosfs);
35 DECLARE_DEBUG_CHANNEL(file);
37 static DOS_FULL_NAME DIR_Windows;
38 static DOS_FULL_NAME DIR_System;
41 /***********************************************************************
42 * DIR_GetPath
44 * Get a path name from the wine.ini file and make sure it is valid.
46 static int DIR_GetPath( const char *keyname, const char *defval,
47 DOS_FULL_NAME *full_name )
49 char path[MAX_PATHNAME_LEN];
50 BY_HANDLE_FILE_INFORMATION info;
52 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
53 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
54 !FILE_Stat( full_name->long_name, &info ) ||
55 !(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
57 MESSAGE("Invalid path '%s' for %s directory\n", path, keyname);
58 return 0;
60 return 1;
64 /***********************************************************************
65 * DIR_Init
67 int DIR_Init(void)
69 char path[MAX_PATHNAME_LEN];
70 DOS_FULL_NAME tmp_dir, profile_dir;
71 int drive;
72 const char *cwd;
74 if (!getcwd( path, MAX_PATHNAME_LEN ))
76 perror( "Could not get current directory" );
77 return 0;
79 cwd = path;
80 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
82 MESSAGE("Warning: could not find wine.conf [Drive x] entry "
83 "for current working directory %s; "
84 "starting in windows directory.\n", cwd );
86 else
88 DRIVE_SetCurrentDrive( drive );
89 DRIVE_Chdir( drive, cwd );
92 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows )) ||
93 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System )) ||
94 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir )))
96 PROFILE_UsageWineIni();
97 return 0;
99 if (-1 == access( tmp_dir.long_name, W_OK ))
101 if (errno==EACCES)
103 MESSAGE("Warning: The Temporary Directory (as specified in your configuration file) is NOT writeable.\n");
104 PROFILE_UsageWineIni();
106 else
107 MESSAGE("Warning: Access to Temporary Directory failed (%s).\n",
108 strerror(errno));
111 if (drive == -1)
113 drive = DIR_Windows.drive;
114 DRIVE_SetCurrentDrive( drive );
115 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
118 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
119 path, sizeof(path) );
120 if (strchr(path, '/'))
122 MESSAGE("No '/' allowed in [wine] 'Path=' statement of wine.conf !\n");
123 PROFILE_UsageWineIni();
124 ExitProcess(1);
127 /* Set the environment variables */
129 SetEnvironmentVariableA( "PATH", path );
130 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
131 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
132 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
134 /* set COMSPEC only if it doesn't exist already */
135 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
136 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
138 TRACE("WindowsDir = %s (%s)\n",
139 DIR_Windows.short_name, DIR_Windows.long_name );
140 TRACE("SystemDir = %s (%s)\n",
141 DIR_System.short_name, DIR_System.long_name );
142 TRACE("TempDir = %s (%s)\n",
143 tmp_dir.short_name, tmp_dir.long_name );
144 TRACE("Path = %s\n", path );
145 TRACE("Cwd = %c:\\%s\n",
146 'A' + drive, DRIVE_GetDosCwd( drive ) );
148 if (DIR_GetPath( "profile", "", &profile_dir ))
150 TRACE("USERPROFILE= %s\n", profile_dir.short_name );
151 SetEnvironmentVariableA( "USERPROFILE", profile_dir.short_name );
154 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
155 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
157 return 1;
161 /***********************************************************************
162 * GetTempPathA (KERNEL32.292)
164 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
166 UINT ret;
167 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
168 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
169 if (!(ret = GetCurrentDirectoryA( count, path )))
170 return 0;
171 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
173 path[ret++] = '\\';
174 path[ret] = '\0';
176 return ret;
180 /***********************************************************************
181 * GetTempPathW (KERNEL32.293)
183 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
185 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
186 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
187 UINT ret;
188 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
189 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
190 if (!(ret = GetCurrentDirectoryW( count, path )))
191 return 0;
192 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
194 path[ret++] = '\\';
195 path[ret] = '\0';
197 return ret;
201 /***********************************************************************
202 * DIR_GetWindowsUnixDir
204 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
206 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
207 return strlen( DIR_Windows.long_name );
211 /***********************************************************************
212 * DIR_GetSystemUnixDir
214 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
216 if (path) lstrcpynA( path, DIR_System.long_name, count );
217 return strlen( DIR_System.long_name );
221 /***********************************************************************
222 * GetTempDrive (KERNEL.92)
223 * A closer look at krnl386.exe shows what the SDK doesn't mention:
225 * returns:
226 * AL: driveletter
227 * AH: ':' - yes, some kernel code even does stosw with
228 * the returned AX.
229 * DX: 1 for success
231 UINT WINAPI GetTempDrive( BYTE ignored )
233 char *buffer;
234 BYTE ret;
235 UINT len = GetTempPathA( 0, NULL );
237 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
238 ret = DRIVE_GetCurrentDrive() + 'A';
239 else
241 /* FIXME: apparently Windows does something with the ignored byte */
242 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
243 ret = toupper(buffer[0]);
244 HeapFree( GetProcessHeap(), 0, buffer );
246 return MAKELONG( ret | (':' << 8), 1 );
250 /***********************************************************************
251 * GetWindowsDirectory16 (KERNEL.134)
253 UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
255 return (UINT16)GetWindowsDirectoryA( path, count );
259 /***********************************************************************
260 * GetWindowsDirectoryA (KERNEL32.311)
262 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
264 if (path) lstrcpynA( path, DIR_Windows.short_name, count );
265 return strlen( DIR_Windows.short_name );
269 /***********************************************************************
270 * GetWindowsDirectoryW (KERNEL32.312)
272 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
274 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0 );
275 if (path && count)
277 if (!MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, path, count ))
278 path[count-1] = 0;
280 return len;
284 /***********************************************************************
285 * GetSystemWindowsDirectoryA (KERNEL32) W2K, TS4.0SP4
287 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
289 return GetWindowsDirectoryA( path, count );
293 /***********************************************************************
294 * GetSystemWindowsDirectoryW (KERNEL32) W2K, TS4.0SP4
296 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
298 return GetWindowsDirectoryW( path, count );
302 /***********************************************************************
303 * GetSystemDirectory16 (KERNEL.135)
305 UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
307 return (UINT16)GetSystemDirectoryA( path, count );
311 /***********************************************************************
312 * GetSystemDirectoryA (KERNEL32.282)
314 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
316 if (path) lstrcpynA( path, DIR_System.short_name, count );
317 return strlen( DIR_System.short_name );
321 /***********************************************************************
322 * GetSystemDirectoryW (KERNEL32.283)
324 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
326 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0 );
327 if (path && count)
329 if (!MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, path, count ))
330 path[count-1] = 0;
332 return len;
336 /***********************************************************************
337 * CreateDirectory16 (KERNEL.144)
339 BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
341 TRACE_(file)("(%s,%p)\n", path, dummy );
342 return (BOOL16)CreateDirectoryA( path, NULL );
346 /***********************************************************************
347 * CreateDirectoryA (KERNEL32.39)
348 * RETURNS:
349 * TRUE : success
350 * FALSE : failure
351 * ERROR_DISK_FULL: on full disk
352 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
353 * ERROR_ACCESS_DENIED: on permission problems
354 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
356 BOOL WINAPI CreateDirectoryA( LPCSTR path,
357 LPSECURITY_ATTRIBUTES lpsecattribs )
359 DOS_FULL_NAME full_name;
361 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
362 if (DOSFS_GetDevice( path ))
364 TRACE_(file)("cannot use device '%s'!\n",path);
365 SetLastError( ERROR_ACCESS_DENIED );
366 return FALSE;
368 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
369 if (mkdir( full_name.long_name, 0777 ) == -1) {
370 WARN_(file)("Errno %i trying to create directory %s.\n", errno, full_name.long_name);
371 /* the FILE_SetDosError() generated error codes don't match the
372 * CreateDirectory ones for some errnos */
373 switch (errno) {
374 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
375 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
376 default: FILE_SetDosError();break;
378 return FALSE;
380 return TRUE;
384 /***********************************************************************
385 * CreateDirectoryW (KERNEL32.42)
387 BOOL WINAPI CreateDirectoryW( LPCWSTR path,
388 LPSECURITY_ATTRIBUTES lpsecattribs )
390 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
391 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
392 HeapFree( GetProcessHeap(), 0, xpath );
393 return ret;
397 /***********************************************************************
398 * CreateDirectoryExA (KERNEL32.40)
400 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
401 LPSECURITY_ATTRIBUTES lpsecattribs)
403 return CreateDirectoryA(path,lpsecattribs);
407 /***********************************************************************
408 * CreateDirectoryExW (KERNEL32.41)
410 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
411 LPSECURITY_ATTRIBUTES lpsecattribs)
413 return CreateDirectoryW(path,lpsecattribs);
417 /***********************************************************************
418 * RemoveDirectory16 (KERNEL)
420 BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
422 return (BOOL16)RemoveDirectoryA( path );
426 /***********************************************************************
427 * RemoveDirectoryA (KERNEL32.437)
429 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
431 DOS_FULL_NAME full_name;
433 TRACE_(file)("'%s'\n", path );
435 if (DOSFS_GetDevice( path ))
437 TRACE_(file)("cannot remove device '%s'!\n", path);
438 SetLastError( ERROR_FILE_NOT_FOUND );
439 return FALSE;
441 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
442 if (rmdir( full_name.long_name ) == -1)
444 FILE_SetDosError();
445 return FALSE;
447 return TRUE;
451 /***********************************************************************
452 * RemoveDirectoryW (KERNEL32.438)
454 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
456 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
457 BOOL ret = RemoveDirectoryA( xpath );
458 HeapFree( GetProcessHeap(), 0, xpath );
459 return ret;
463 /***********************************************************************
464 * DIR_TryPath
466 * Helper function for DIR_SearchPath.
468 static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
469 DOS_FULL_NAME *full_name )
471 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
472 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
474 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
475 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
477 SetLastError( ERROR_PATH_NOT_FOUND );
478 return FALSE;
480 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
481 sizeof(full_name->long_name) - (p_l - full_name->long_name),
482 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
483 return FALSE;
484 strcpy( full_name->long_name, dir->long_name );
485 p_l[-1] = '/';
486 strcpy( full_name->short_name, dir->short_name );
487 p_s[-1] = '\\';
488 return TRUE;
492 /***********************************************************************
493 * DIR_TryEnvironmentPath
495 * Helper function for DIR_SearchPath.
496 * Search in the specified path, or in $PATH if NULL.
498 static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
500 LPSTR path, next, buffer;
501 BOOL ret = FALSE;
502 INT len = strlen(name);
503 DWORD size;
505 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
506 if (!size) return FALSE;
507 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
508 if (envpath) strcpy( path, envpath );
509 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
510 next = path;
511 while (!ret && next)
513 LPSTR cur = next;
514 while (*cur == ';') cur++;
515 if (!*cur) break;
516 next = strchr( cur, ';' );
517 if (next) *next++ = '\0';
518 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, strlen(cur) + len + 2)))
519 goto done;
520 strcpy( buffer, cur );
521 strcat( buffer, "\\" );
522 strcat( buffer, name );
523 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
524 HeapFree( GetProcessHeap(), 0, buffer );
527 done:
528 HeapFree( GetProcessHeap(), 0, path );
529 return ret;
533 /***********************************************************************
534 * DIR_TryModulePath
536 * Helper function for DIR_SearchPath.
538 static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
540 /* FIXME: for now, GetModuleFileNameA can't return more */
541 /* than OFS_MAXPATHNAME. This may change with Win32. */
543 char buffer[OFS_MAXPATHNAME];
544 LPSTR p;
546 if (!win32)
548 if (!GetCurrentTask()) return FALSE;
549 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
550 buffer[0]='\0';
551 } else {
552 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
553 buffer[0]='\0';
555 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
556 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
557 strcpy( p, name );
558 return DOSFS_GetFullName( buffer, TRUE, full_name );
562 /***********************************************************************
563 * DIR_SearchPath
565 * Implementation of SearchPathA. 'win32' specifies whether the search
566 * order is Win16 (module path last) or Win32 (module path first).
568 * FIXME: should return long path names.
570 DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
571 DOS_FULL_NAME *full_name, BOOL win32 )
573 LPCSTR p;
574 LPSTR tmp = NULL;
575 BOOL ret = TRUE;
577 /* First check the supplied parameters */
579 p = strrchr( name, '.' );
580 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
581 ext = NULL; /* Ignore the specified extension */
582 if ((*name && (name[1] == ':')) ||
583 strchr( name, '/' ) || strchr( name, '\\' ))
584 path = NULL; /* Ignore path if name already contains a path */
585 if (path && !*path) path = NULL; /* Ignore empty path */
587 /* Allocate a buffer for the file name and extension */
589 if (ext)
591 DWORD len = strlen(name) + strlen(ext);
592 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
594 SetLastError( ERROR_OUTOFMEMORY );
595 return 0;
597 strcpy( tmp, name );
598 strcat( tmp, ext );
599 name = tmp;
602 /* If the name contains an explicit path, everything's easy */
604 if ((*name && (name[1] == ':')) || strchr( name, '/' ) || strchr( name, '\\' ))
606 ret = DOSFS_GetFullName( name, TRUE, full_name );
607 goto done;
610 /* Search in the specified path */
612 if (path)
614 ret = DIR_TryEnvironmentPath( name, full_name, path );
615 goto done;
618 /* Try the path of the current executable (for Win32 search order) */
620 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
622 /* Try the current directory */
624 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
626 /* Try the Windows system directory */
628 if (DIR_TryPath( &DIR_System, name, full_name ))
629 goto done;
631 /* Try the Windows directory */
633 if (DIR_TryPath( &DIR_Windows, name, full_name ))
634 goto done;
636 /* Try the path of the current executable (for Win16 search order) */
638 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
640 /* Try all directories in path */
642 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
644 done:
645 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
646 return ret;
650 /***********************************************************************
651 * SearchPathA [KERNEL32.447]
653 * Searches for a specified file in the search path.
655 * PARAMS
656 * path [I] Path to search
657 * name [I] Filename to search for.
658 * ext [I] File extension to append to file name. The first
659 * character must be a period. This parameter is
660 * specified only if the filename given does not
661 * contain an extension.
662 * buflen [I] size of buffer, in characters
663 * buffer [O] buffer for found filename
664 * lastpart [O] address of pointer to last used character in
665 * buffer (the final '\')
667 * RETURNS
668 * Success: length of string copied into buffer, not including
669 * terminating null character. If the filename found is
670 * longer than the length of the buffer, the length of the
671 * filename is returned.
672 * Failure: Zero
674 * NOTES
675 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
676 * (tested on NT 4.0)
678 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
679 LPSTR buffer, LPSTR *lastpart )
681 LPSTR p, res;
682 DOS_FULL_NAME full_name;
684 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
686 SetLastError(ERROR_FILE_NOT_FOUND);
687 return 0;
689 lstrcpynA( buffer, full_name.short_name, buflen );
690 res = full_name.long_name +
691 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
692 while (*res == '/') res++;
693 if (buflen)
695 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
696 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
697 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
699 TRACE("Returning %d\n", strlen(res) + 3 );
700 return strlen(res) + 3;
704 /***********************************************************************
705 * SearchPathW (KERNEL32.448)
707 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
708 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
710 LPWSTR p;
711 LPSTR res;
712 DOS_FULL_NAME full_name;
714 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
715 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
716 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
717 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
718 HeapFree( GetProcessHeap(), 0, extA );
719 HeapFree( GetProcessHeap(), 0, nameA );
720 HeapFree( GetProcessHeap(), 0, pathA );
721 if (!ret) return 0;
723 if (buflen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, buffer, buflen ))
724 buffer[buflen-1] = 0;
725 res = full_name.long_name +
726 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
727 while (*res == '/') res++;
728 if (buflen)
730 if (buflen > 3)
732 if (!MultiByteToWideChar( CP_ACP, 0, res, -1, buffer+3, buflen-3 ))
733 buffer[buflen-1] = 0;
735 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
736 if (lastpart)
738 for (p = *lastpart = buffer; *p; p++)
739 if (*p == '\\') *lastpart = p + 1;
742 return strlen(res) + 3;