4 * Copyright 2002, 2003, 2004 Alexandre Julliard
5 * Copyright 2003 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "wine/library.h"
35 #include "ntdll_misc.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(file);
39 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
40 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
41 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
43 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
45 #define MAX_DOS_DRIVES 26
53 /***********************************************************************
56 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
58 static inline int get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
60 const char *config_dir = wine_get_config_dir();
65 buffer = RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") );
66 if (!buffer) return 0;
67 strcpy( buffer, config_dir );
68 strcat( buffer, "/dosdevices/a:" );
69 p = buffer + strlen(buffer) - 2;
71 for (i = ret = 0; i < MAX_DOS_DRIVES; i++)
74 if (!stat( buffer, &st ))
76 info[i].dev = st.st_dev;
77 info[i].ino = st.st_ino;
86 RtlFreeHeap( GetProcessHeap(), 0, buffer );
91 /***********************************************************************
92 * remove_last_component
94 * Remove the last component of the path. Helper for find_drive_root.
96 static inline int remove_last_component( const WCHAR *path, int len )
102 /* find start of the last path component */
104 if (prev <= 1) break; /* reached root */
105 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
106 /* does removing it take us up a level? */
107 if (len - prev != 1 || path[prev] != '.') /* not '.' */
109 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
114 /* strip off trailing slashes */
115 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
122 /***********************************************************************
125 * Find a drive for which the root matches the beginning of the given path.
126 * This can be used to translate a Unix path into a drive + DOS path.
127 * Return value is the drive, or -1 on error. On success, ppath is modified
128 * to point to the beginning of the DOS path.
130 static int find_drive_root( LPCWSTR *ppath )
132 /* Starting with the full path, check if the device and inode match any of
133 * the wine 'drives'. If not then remove the last path component and try
134 * again. If the last component was a '..' then skip a normal component
135 * since it's a directory that's ascended back out of.
137 int drive, lenA, lenW;
139 const WCHAR *path = *ppath;
141 struct drive_info info[MAX_DOS_DRIVES];
143 /* get device and inode of all drives */
144 if (!get_drives_info( info )) return -1;
146 /* strip off trailing slashes */
147 lenW = strlenW(path);
148 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
150 /* convert path to Unix encoding */
151 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
152 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
153 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
155 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
159 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
162 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
164 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
166 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
167 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
168 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
170 RtlFreeHeap( GetProcessHeap(), 0, buffer );
175 if (lenW <= 1) break; /* reached root */
176 lenW = remove_last_component( path, lenW );
178 /* we only need the new length, buffer already contains the converted string */
179 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
182 RtlFreeHeap( GetProcessHeap(), 0, buffer );
187 /***********************************************************************
188 * RtlDetermineDosPathNameType_U (NTDLL.@)
190 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
192 if (IS_SEPARATOR(path[0]))
194 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
195 if (path[2] != '.') return UNC_PATH; /* "//foo" */
196 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
197 if (path[3]) return UNC_PATH; /* "//.foo" */
198 return UNC_DOT_PATH; /* "//." */
202 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
203 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
204 return RELATIVE_DRIVE_PATH; /* "c:foo" */
208 /***********************************************************************
209 * RtlIsDosDeviceName_U (NTDLL.@)
211 * Check if the given DOS path contains a DOS device name.
213 * Returns the length of the device name in the low word and its
214 * position in the high word (both in bytes, not WCHARs), or 0 if no
215 * device name is found.
217 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
219 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
220 static const WCHAR auxW[3] = {'A','U','X'};
221 static const WCHAR comW[3] = {'C','O','M'};
222 static const WCHAR conW[3] = {'C','O','N'};
223 static const WCHAR lptW[3] = {'L','P','T'};
224 static const WCHAR nulW[3] = {'N','U','L'};
225 static const WCHAR prnW[3] = {'P','R','N'};
227 const WCHAR *start, *end, *p;
229 switch(RtlDetermineDosPathNameType_U( dos_name ))
235 if (!strcmpiW( dos_name, consoleW ))
236 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
242 end = dos_name + strlenW(dos_name) - 1;
243 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
245 /* find start of file name */
246 for (start = end; start >= dos_name; start--)
248 if (IS_SEPARATOR(start[0])) break;
249 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
250 if (start[0] == ':' && start[1] != '.') break;
254 /* remove extension */
255 if ((p = strchrW( start, '.' )))
258 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
262 /* no extension, remove trailing spaces */
263 while (end >= dos_name && *end == ' ') end--;
266 /* now we have a potential device name between start and end, check it */
267 switch(end - start + 1)
270 if (strncmpiW( start, auxW, 3 ) &&
271 strncmpiW( start, conW, 3 ) &&
272 strncmpiW( start, nulW, 3 ) &&
273 strncmpiW( start, prnW, 3 )) break;
274 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
276 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
277 if (*end <= '0' || *end > '9') break;
278 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
279 default: /* can't match anything */
286 /**************************************************************************
287 * RtlDosPathNameToNtPathName_U [NTDLL.@]
289 * dos_path: a DOS path name (fully qualified or not)
290 * ntpath: pointer to a UNICODE_STRING to hold the converted
292 * file_part:will point (in ntpath) to the file part in the path
293 * cd: directory reference (optional)
296 * + fill the cd structure
298 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
299 PUNICODE_STRING ntpath,
303 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
304 ULONG sz, ptr_sz, offset;
305 WCHAR local[MAX_PATH];
308 TRACE("(%s,%p,%p,%p)\n",
309 debugstr_w(dos_path), ntpath, file_part, cd);
313 FIXME("Unsupported parameter\n");
314 memset(cd, 0, sizeof(*cd));
317 if (!dos_path || !*dos_path) return FALSE;
319 if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
321 dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
328 ptr_sz = sizeof(local);
330 sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
331 if (sz == 0) return FALSE;
334 ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz);
335 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
338 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
339 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
342 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
346 strcpyW(ntpath->Buffer, NTDosPrefixW);
348 switch (RtlDetermineDosPathNameType_U(ptr))
350 case UNC_PATH: /* \\foo */
354 strcatW(ntpath->Buffer, UncPfxW);
357 case DEVICE_PATH: /* \\.\foo */
360 default: break; /* needed to keep gcc quiet */
363 strcatW(ntpath->Buffer, ptr + offset);
364 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
366 if (file_part && *file_part)
367 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
369 /* FIXME: cd filling */
371 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
375 /******************************************************************
378 * Searchs a file of name 'name' into a ';' separated list of paths
380 * Doesn't seem to search elsewhere than the paths list
381 * Stores the result in buffer (file_part will point to the position
382 * of the file name in the buffer)
384 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
386 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
387 ULONG buffer_size, LPWSTR buffer,
390 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
393 if (type == RELATIVE_PATH)
395 ULONG allocated = 0, needed, filelen;
398 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
400 /* Windows only checks for '.' without worrying about path components */
401 if (strchrW( search, '.' )) ext = NULL;
402 if (ext != NULL) filelen += strlenW(ext);
408 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
409 if (needed + filelen > allocated)
411 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
412 (needed + filelen) * sizeof(WCHAR));
415 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
416 (needed + filelen) * sizeof(WCHAR));
417 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
421 allocated = needed + filelen;
423 memmove(name, paths, needed * sizeof(WCHAR));
424 /* append '\\' if none is present */
425 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
426 strcpyW(&name[needed], search);
427 if (ext) strcatW(&name[needed], ext);
428 if (RtlDoesFileExists_U(name))
430 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
435 RtlFreeHeap(GetProcessHeap(), 0, name);
437 else if (RtlDoesFileExists_U(search))
439 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
446 /******************************************************************
449 * Helper for RtlGetFullPathName_U.
450 * Get rid of . and .. components in the path.
452 static inline void collapse_path( WCHAR *path, UINT mark )
456 /* convert every / into a \ */
457 for (p = path; *p; p++) if (*p == '/') *p = '\\';
459 /* collapse duplicate backslashes */
460 next = path + max( 1, mark );
461 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
471 case '\\': /* .\ component */
473 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
475 case 0: /* final . */
476 if (p > path + mark) p--;
480 if (p[2] == '\\') /* ..\ component */
486 while (p > path + mark && p[-1] != '\\') p--;
488 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
491 else if (!p[2]) /* final .. */
496 while (p > path + mark && p[-1] != '\\') p--;
497 if (p > path + mark) p--;
505 /* skip to the next component */
506 while (*p && *p != '\\') p++;
511 /******************************************************************
512 * get_full_path_helper
514 * Helper for RtlGetFullPathName_U
515 * Note: name and buffer are allowed to point to the same memory spot
517 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
519 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
520 DOS_PATHNAME_TYPE type;
521 LPWSTR ins_str = NULL;
523 const UNICODE_STRING* cd;
528 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
530 switch (type = RtlDetermineDosPathNameType_U(name))
532 case UNC_PATH: /* \\foo */
534 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
535 while (*ptr && IS_SEPARATOR(*ptr)) ptr++;
536 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
540 case DEVICE_PATH: /* \\.\foo */
544 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
545 reqsize = sizeof(WCHAR);
546 tmp[0] = toupperW(name[0]);
552 case RELATIVE_DRIVE_PATH: /* c:foo */
554 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
556 UNICODE_STRING var, val;
562 var.Length = 3 * sizeof(WCHAR);
563 var.MaximumLength = 4 * sizeof(WCHAR);
566 val.MaximumLength = size;
567 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
569 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
572 /* FIXME: Win2k seems to check that the environment variable actually points
573 * to an existing directory. If not, root of the drive is used
574 * (this seems also to be the only spot in RtlGetFullPathName that the
575 * existence of a part of a path is checked)
578 case STATUS_BUFFER_TOO_SMALL:
579 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
580 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
581 ins_str = val.Buffer;
583 case STATUS_VARIABLE_NOT_FOUND:
584 reqsize = 3 * sizeof(WCHAR);
591 ERR("Unsupported status code\n");
599 case RELATIVE_PATH: /* foo */
600 reqsize = cd->Length;
601 ins_str = cd->Buffer;
602 if (cd->Buffer[1] != ':')
604 ptr = strchrW(cd->Buffer + 2, '\\');
605 if (ptr) ptr = strchrW(ptr + 1, '\\');
606 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
607 mark = ptr - cd->Buffer;
612 case ABSOLUTE_PATH: /* \xxx */
613 if (name[0] == '/') /* may be a Unix path */
615 const WCHAR *ptr = name;
616 int drive = find_drive_root( &ptr );
619 reqsize = 3 * sizeof(WCHAR);
620 tmp[0] = 'A' + drive;
629 if (cd->Buffer[1] == ':')
631 reqsize = 2 * sizeof(WCHAR);
632 tmp[0] = cd->Buffer[0];
639 ptr = strchrW(cd->Buffer + 2, '\\');
640 if (ptr) ptr = strchrW(ptr + 1, '\\');
641 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
642 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
643 mark = reqsize / sizeof(WCHAR);
644 ins_str = cd->Buffer;
648 case UNC_DOT_PATH: /* \\. */
649 reqsize = 4 * sizeof(WCHAR);
664 deplen = strlenW(name + dep) * sizeof(WCHAR);
665 if (reqsize + deplen + sizeof(WCHAR) > size)
667 /* not enough space, return need size (including terminating '\0') */
668 reqsize += deplen + sizeof(WCHAR);
672 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
673 if (reqsize) memcpy(buffer, ins_str, reqsize);
676 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
677 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
679 collapse_path( buffer, mark );
680 reqsize = strlenW(buffer) * sizeof(WCHAR);
687 /******************************************************************
688 * RtlGetFullPathName_U (NTDLL.@)
690 * Returns the number of bytes written to buffer (not including the
691 * terminating NULL) if the function succeeds, or the required number of bytes
692 * (including the terminating NULL) if the buffer is too small.
694 * file_part will point to the filename part inside buffer (except if we use
695 * DOS device name, in which case file_in_buf is NULL)
698 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
705 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
707 if (!name || !*name) return 0;
709 if (file_part) *file_part = NULL;
711 /* check for DOS device name */
712 dosdev = RtlIsDosDeviceName_U(name);
715 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
716 DWORD sz = LOWORD(dosdev); /* in bytes */
718 if (8 + sz + 2 > size) return sz + 10;
719 strcpyW(buffer, DeviceRootW);
720 memmove(buffer + 4, name + offset, sz);
721 buffer[4 + sz / sizeof(WCHAR)] = '\0';
722 /* file_part isn't set in this case */
726 reqsize = get_full_path_helper(name, buffer, size);
729 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
730 reqsize = get_full_path_helper(name, tmp, reqsize);
731 if (reqsize > size) /* it may have worked the second time */
733 RtlFreeHeap(GetProcessHeap(), 0, tmp);
734 return reqsize + sizeof(WCHAR);
736 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
737 RtlFreeHeap(GetProcessHeap(), 0, tmp);
741 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
746 /*************************************************************************
747 * RtlGetLongestNtPathLength [NTDLL.@]
749 * Get the longest allowed path length
755 * The longest allowed path length (277 characters under Win2k).
757 DWORD WINAPI RtlGetLongestNtPathLength(void)
762 /******************************************************************
763 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
765 * Returns TRUE iff unicode is a valid DOS (8+3) name.
766 * If the name is valid, oem gets filled with the corresponding OEM string
767 * spaces is set to TRUE if unicode contains spaces
769 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
770 OEM_STRING *oem, BOOLEAN *spaces )
772 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
777 BOOLEAN got_space = FALSE;
781 oem_str.Length = sizeof(buffer);
782 oem_str.MaximumLength = sizeof(buffer);
783 oem_str.Buffer = buffer;
786 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
789 if (oem->Length > 12) return FALSE;
791 /* a starting . is invalid, except for . and .. */
792 if (oem->Buffer[0] == '.')
794 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
795 if (spaces) *spaces = FALSE;
799 for (i = 0; i < oem->Length; i++)
801 switch (oem->Buffer[i])
804 /* leading/trailing spaces not allowed */
805 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
809 if (dot != -1) return FALSE;
813 if (strchr(illegal, oem->Buffer[i])) return FALSE;
817 /* check file part is shorter than 8, extension shorter than 3
818 * dot cannot be last in string
822 if (oem->Length > 8) return FALSE;
826 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
828 if (spaces) *spaces = got_space;
832 /******************************************************************
833 * RtlGetCurrentDirectory_U (NTDLL.@)
836 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
841 TRACE("(%lu %p)\n", buflen, buf);
845 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
846 len = us->Length / sizeof(WCHAR);
847 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
850 if (buflen / sizeof(WCHAR) > len)
852 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
862 return len * sizeof(WCHAR);
865 /******************************************************************
866 * RtlSetCurrentDirectory_U (NTDLL.@)
869 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
871 UNICODE_STRING* curdir;
872 NTSTATUS nts = STATUS_SUCCESS;
876 TRACE("(%s)\n", debugstr_w(dir->Buffer));
880 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
881 size = curdir->MaximumLength;
883 buf = RtlAllocateHeap(GetProcessHeap(), 0, size);
886 nts = STATUS_NO_MEMORY;
890 size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
893 nts = STATUS_OBJECT_NAME_INVALID;
897 switch (RtlDetermineDosPathNameType_U(buf))
899 case ABSOLUTE_DRIVE_PATH:
903 FIXME("Don't support those cases yes\n");
904 nts = STATUS_NOT_IMPLEMENTED;
908 /* FIXME: should check that the directory actually exists,
909 * and fill CurrentDirectoryHandle accordingly
912 /* append trailing \ if missing */
913 if (buf[size / sizeof(WCHAR) - 1] != '\\')
915 buf[size / sizeof(WCHAR)] = '\\';
916 buf[size / sizeof(WCHAR) + 1] = '\0';
917 size += sizeof(WCHAR);
920 memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
921 curdir->Length = size;
924 if (curdir->Buffer[1] == ':')
930 var[1] = curdir->Buffer[0];
933 env.Length = 3 * sizeof(WCHAR);
934 env.MaximumLength = 4 * sizeof(WCHAR);
937 RtlSetEnvironmentVariable(NULL, &env, curdir);
942 if (buf) RtlFreeHeap(GetProcessHeap(), 0, buf);