Rewrote the collapsing of . and .. in RtlGetFullPathName_U for better
[wine.git] / dlls / ntdll / path.c
blob22a8697f527e1336a5c2dd77801b671ce798fed3
1 /*
2 * Ntdll path functions
4 * Copyright 2002, 2003 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
22 #include "config.h"
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(file);
36 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
37 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
38 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
40 /* FIXME: hack! */
41 HANDLE (WINAPI *pCreateFileW)( LPCWSTR filename, DWORD access, DWORD sharing,
42 LPSECURITY_ATTRIBUTES sa, DWORD creation,
43 DWORD attributes, HANDLE template );
45 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
47 /***********************************************************************
48 * RtlDetermineDosPathNameType_U (NTDLL.@)
50 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
52 if (IS_SEPARATOR(path[0]))
54 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
55 if (path[2] != '.') return UNC_PATH; /* "//foo" */
56 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
57 if (path[3]) return UNC_PATH; /* "//.foo" */
58 return UNC_DOT_PATH; /* "//." */
60 else
62 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
63 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
64 return RELATIVE_DRIVE_PATH; /* "c:foo" */
68 /******************************************************************
69 * RtlDoesFileExists_U
71 * FIXME: should not use CreateFileW
73 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
75 HANDLE handle = pCreateFileW( file_name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
76 NULL, OPEN_EXISTING, 0, 0 );
77 if (handle == INVALID_HANDLE_VALUE) return FALSE;
78 NtClose( handle );
79 return TRUE;
82 /***********************************************************************
83 * RtlIsDosDeviceName_U (NTDLL.@)
85 * Check if the given DOS path contains a DOS device name.
87 * Returns the length of the device name in the low word and its
88 * position in the high word (both in bytes, not WCHARs), or 0 if no
89 * device name is found.
91 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
93 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
94 static const WCHAR auxW[3] = {'A','U','X'};
95 static const WCHAR comW[3] = {'C','O','M'};
96 static const WCHAR conW[3] = {'C','O','N'};
97 static const WCHAR lptW[3] = {'L','P','T'};
98 static const WCHAR nulW[3] = {'N','U','L'};
99 static const WCHAR prnW[3] = {'P','R','N'};
101 const WCHAR *start, *end, *p;
103 switch(RtlDetermineDosPathNameType_U( dos_name ))
105 case INVALID_PATH:
106 case UNC_PATH:
107 return 0;
108 case DEVICE_PATH:
109 if (!strcmpiW( dos_name, consoleW ))
110 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
111 return 0;
112 default:
113 break;
116 end = dos_name + strlenW(dos_name) - 1;
117 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
119 /* find start of file name */
120 for (start = end; start >= dos_name; start--)
122 if (IS_SEPARATOR(start[0])) break;
123 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
124 if (start[0] == ':' && start[1] != '.') break;
126 start++;
128 /* remove extension */
129 if ((p = strchrW( start, '.' )))
131 end = p - 1;
132 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
134 else
136 /* no extension, remove trailing spaces */
137 while (end >= dos_name && *end == ' ') end--;
140 /* now we have a potential device name between start and end, check it */
141 switch(end - start + 1)
143 case 3:
144 if (strncmpiW( start, auxW, 3 ) &&
145 strncmpiW( start, conW, 3 ) &&
146 strncmpiW( start, nulW, 3 ) &&
147 strncmpiW( start, prnW, 3 )) break;
148 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
149 case 4:
150 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
151 if (*end <= '0' || *end > '9') break;
152 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
153 default: /* can't match anything */
154 break;
156 return 0;
160 /**************************************************************************
161 * RtlDosPathNameToNtPathName_U [NTDLL.@]
163 * dos_path: a DOS path name (fully qualified or not)
164 * ntpath: pointer to a UNICODE_STRING to hold the converted
165 * path name
166 * file_part:will point (in ntpath) to the file part in the path
167 * cd: directory reference (optional)
169 * FIXME:
170 * + fill the cd structure
172 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PWSTR dos_path,
173 PUNICODE_STRING ntpath,
174 PWSTR* file_part,
175 CURDIR* cd)
177 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
178 ULONG sz, ptr_sz, offset;
179 WCHAR local[MAX_PATH];
180 LPWSTR ptr;
182 TRACE("(%s,%p,%p,%p)\n",
183 debugstr_w(dos_path), ntpath, file_part, cd);
185 if (cd)
187 FIXME("Unsupported parameter\n");
188 memset(cd, 0, sizeof(*cd));
191 if (!dos_path || !*dos_path) return FALSE;
193 if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
195 dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
196 ptr = NULL;
197 ptr_sz = 0;
199 else
201 ptr = local;
202 ptr_sz = sizeof(local);
204 sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
205 if (sz == 0) return FALSE;
206 if (sz > ptr_sz)
208 ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz);
209 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
212 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
213 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
214 if (!ntpath->Buffer)
216 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
217 return FALSE;
220 strcpyW(ntpath->Buffer, NTDosPrefixW);
221 offset = 0;
222 switch (RtlDetermineDosPathNameType_U(ptr))
224 case UNC_PATH: /* \\foo */
225 if (ptr[2] != '?')
227 offset = 2;
228 strcatW(ntpath->Buffer, UncPfxW);
230 break;
231 case DEVICE_PATH: /* \\.\foo */
232 offset = 4;
233 break;
234 default: break; /* needed to keep gcc quiet */
237 strcatW(ntpath->Buffer, ptr + offset);
238 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
240 if (file_part && *file_part)
241 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
243 /* FIXME: cd filling */
245 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
246 return TRUE;
249 /******************************************************************
250 * RtlDosSearchPath_U
252 * Searchs a file of name 'name' into a ';' separated list of paths
253 * (stored in paths)
254 * Doesn't seem to search elsewhere than the paths list
255 * Stores the result in buffer (file_part will point to the position
256 * of the file name in the buffer)
257 * FIXME:
258 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
260 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
261 ULONG buffer_size, LPWSTR buffer,
262 LPWSTR* file_part)
264 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
265 ULONG len = 0;
267 if (type == RELATIVE_PATH)
269 ULONG allocated = 0, needed, filelen;
270 WCHAR *name = NULL;
272 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
274 /* Windows only checks for '.' without worrying about path components */
275 if (strchrW( search, '.' )) ext = NULL;
276 if (ext != NULL) filelen += strlenW(ext);
278 while (*paths)
280 LPCWSTR ptr;
282 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
283 if (needed + filelen > allocated)
285 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
286 (needed + filelen) * sizeof(WCHAR));
287 else
289 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
290 (needed + filelen) * sizeof(WCHAR));
291 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
292 name = newname;
294 if (!name) return 0;
295 allocated = needed + filelen;
297 memmove(name, paths, needed * sizeof(WCHAR));
298 /* append '\\' if none is present */
299 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
300 strcpyW(&name[needed], search);
301 if (ext) strcatW(&name[needed], ext);
302 if (RtlDoesFileExists_U(name))
304 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
305 break;
307 paths = ptr;
309 RtlFreeHeap(GetProcessHeap(), 0, name);
311 else if (RtlDoesFileExists_U(search))
313 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
316 return len;
320 /******************************************************************
321 * collapse_path
323 * Helper for RtlGetFullPathName_U.
324 * Get rid of . and .. components in the path.
326 static inline void collapse_path( WCHAR *path )
328 WCHAR *p, *next;
330 p = path;
332 while (*p)
334 if (*p == '.')
336 switch(p[1])
338 case '\\': /* .\ component */
339 next = p + 2;
340 while (*next == '\\') next++;
341 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
342 continue;
343 case 0: /* final . */
344 while (p > path && p[-1] == '\\') p--;
345 *p = 0;
346 continue;
347 case '.':
348 if (p[2] == '\\') /* ..\ component */
350 next = p + 3;
351 while (*next == '\\') next++;
352 while (p > path && p[-1] == '\\') p--;
353 while (p > path && p[-1] != '\\') p--;
354 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
355 continue;
357 else if (!p[2]) /* final .. */
359 while (p > path && p[-1] == '\\') p--;
360 while (p > path && p[-1] != '\\') p--;
361 while (p > path && p[-1] == '\\') p--;
362 *p = 0;
363 continue;
365 break;
368 /* skip to the next component */
369 while (*p && *p != '\\') p++;
370 while (*p == '\\') p++;
375 /******************************************************************
376 * get_full_path_helper
378 * Helper for RtlGetFullPathName_U
379 * Note: name and buffer are allowed to point to the same memory spot
381 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
383 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
384 DOS_PATHNAME_TYPE type;
385 LPWSTR p, ins_str = NULL;
386 LPCWSTR ptr;
387 const UNICODE_STRING* cd;
388 WCHAR tmp[4];
390 RtlAcquirePebLock();
392 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
394 switch (type = RtlDetermineDosPathNameType_U(name))
396 case UNC_PATH: /* \\foo */
397 ptr = name + 2;
398 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
399 while (*ptr && IS_SEPARATOR(*ptr)) ptr++;
400 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
401 mark = (ptr - name);
402 break;
404 case DEVICE_PATH: /* \\.\foo */
405 mark = 4;
406 break;
408 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
409 reqsize = sizeof(WCHAR);
410 tmp[0] = toupperW(name[0]);
411 ins_str = tmp;
412 dep = 1;
413 mark = 3;
414 break;
416 case RELATIVE_DRIVE_PATH: /* c:foo */
417 dep = 2;
418 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
420 UNICODE_STRING var, val;
422 tmp[0] = '=';
423 tmp[1] = name[0];
424 tmp[2] = ':';
425 tmp[3] = '\0';
426 var.Length = 3 * sizeof(WCHAR);
427 var.MaximumLength = 4 * sizeof(WCHAR);
428 var.Buffer = tmp;
429 val.Length = 0;
430 val.MaximumLength = size;
431 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
433 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
435 case STATUS_SUCCESS:
436 /* FIXME: Win2k seems to check that the environment variable actually points
437 * to an existing directory. If not, root of the drive is used
438 * (this seems also to be the only spot in RtlGetFullPathName that the
439 * existence of a part of a path is checked)
441 /* fall thru */
442 case STATUS_BUFFER_TOO_SMALL:
443 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
444 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
445 ins_str = val.Buffer;
446 break;
447 case STATUS_VARIABLE_NOT_FOUND:
448 reqsize = 3 * sizeof(WCHAR);
449 tmp[0] = name[0];
450 tmp[1] = ':';
451 tmp[2] = '\\';
452 ins_str = tmp;
453 break;
454 default:
455 ERR("Unsupported status code\n");
456 break;
458 mark = 3;
459 break;
461 /* fall through */
463 case RELATIVE_PATH: /* foo */
464 reqsize = cd->Length;
465 ins_str = cd->Buffer;
466 if (cd->Buffer[1] != ':')
468 ptr = strchrW(cd->Buffer + 2, '\\');
469 if (ptr) ptr = strchrW(ptr + 1, '\\');
470 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
471 mark = ptr - cd->Buffer;
473 else mark = 3;
474 break;
476 case ABSOLUTE_PATH: /* \xxx */
477 if (cd->Buffer[1] == ':')
479 reqsize = 2 * sizeof(WCHAR);
480 tmp[0] = cd->Buffer[0];
481 tmp[1] = ':';
482 ins_str = tmp;
483 mark = 3;
485 else
487 ptr = strchrW(cd->Buffer + 2, '\\');
488 if (ptr) ptr = strchrW(ptr + 1, '\\');
489 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
490 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
491 mark = reqsize / sizeof(WCHAR);
492 ins_str = cd->Buffer;
494 break;
496 case UNC_DOT_PATH: /* \\. */
497 reqsize = 4 * sizeof(WCHAR);
498 dep = 3;
499 tmp[0] = '\\';
500 tmp[1] = '\\';
501 tmp[2] = '.';
502 tmp[3] = '\\';
503 ins_str = tmp;
504 mark = 4;
505 break;
507 case INVALID_PATH:
508 goto done;
511 /* enough space ? */
512 deplen = strlenW(name + dep) * sizeof(WCHAR);
513 if (reqsize + deplen + sizeof(WCHAR) > size)
515 /* not enough space, return need size (including terminating '\0') */
516 reqsize += deplen + sizeof(WCHAR);
517 goto done;
520 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
521 if (reqsize) memcpy(buffer, ins_str, reqsize);
522 reqsize += deplen;
524 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
525 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
527 /* convert every / into a \ */
528 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
530 collapse_path( buffer + mark );
531 reqsize = strlenW(buffer) * sizeof(WCHAR);
533 done:
534 RtlReleasePebLock();
535 return reqsize;
538 /******************************************************************
539 * RtlGetFullPathName_U (NTDLL.@)
541 * Returns the number of bytes written to buffer (not including the
542 * terminating NULL) if the function succeeds, or the required number of bytes
543 * (including the terminating NULL) if the buffer is too small.
545 * file_part will point to the filename part inside buffer (except if we use
546 * DOS device name, in which case file_in_buf is NULL)
549 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
550 WCHAR** file_part)
552 WCHAR* ptr;
553 DWORD dosdev;
554 DWORD reqsize;
556 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
558 if (!name || !*name) return 0;
560 if (file_part) *file_part = NULL;
562 /* check for DOS device name */
563 dosdev = RtlIsDosDeviceName_U(name);
564 if (dosdev)
566 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
567 DWORD sz = LOWORD(dosdev); /* in bytes */
569 if (8 + sz + 2 > size) return sz + 10;
570 strcpyW(buffer, DeviceRootW);
571 memmove(buffer + 4, name + offset, sz);
572 buffer[4 + sz / sizeof(WCHAR)] = '\0';
573 /* file_part isn't set in this case */
574 return sz + 8;
577 reqsize = get_full_path_helper(name, buffer, size);
578 if (reqsize > size)
580 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
581 reqsize = get_full_path_helper(name, tmp, reqsize);
582 if (reqsize > size) /* it may have worked the second time */
584 RtlFreeHeap(GetProcessHeap(), 0, tmp);
585 return reqsize + sizeof(WCHAR);
587 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
588 RtlFreeHeap(GetProcessHeap(), 0, tmp);
591 /* find file part */
592 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
593 *file_part = ptr;
594 return reqsize;
597 /*************************************************************************
598 * RtlGetLongestNtPathLength [NTDLL.@]
600 * Get the longest allowed path length
602 * PARAMS
603 * None.
605 * RETURNS
606 * The longest allowed path length (277 characters under Win2k).
608 DWORD WINAPI RtlGetLongestNtPathLength(void)
610 return 277;
613 /******************************************************************
614 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
616 * Returns TRUE iff unicode is a valid DOS (8+3) name.
617 * If the name is valid, oem gets filled with the corresponding OEM string
618 * spaces is set to TRUE if unicode contains spaces
620 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
621 OEM_STRING *oem, BOOLEAN *spaces )
623 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
624 int dot = -1;
625 unsigned int i;
626 char buffer[12];
627 OEM_STRING oem_str;
628 BOOLEAN got_space = FALSE;
630 if (!oem)
632 oem_str.Length = sizeof(buffer);
633 oem_str.MaximumLength = sizeof(buffer);
634 oem_str.Buffer = buffer;
635 oem = &oem_str;
637 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
638 return FALSE;
640 if (oem->Length > 12) return FALSE;
642 /* a starting . is invalid, except for . and .. */
643 if (oem->Buffer[0] == '.')
645 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
646 if (spaces) *spaces = FALSE;
647 return TRUE;
650 for (i = 0; i < oem->Length; i++)
652 switch (oem->Buffer[i])
654 case ' ':
655 /* leading/trailing spaces not allowed */
656 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
657 got_space = TRUE;
658 break;
659 case '.':
660 if (dot != -1) return FALSE;
661 dot = i;
662 break;
663 default:
664 if (strchr(illegal, oem->Buffer[i])) return FALSE;
665 break;
668 /* check file part is shorter than 8, extension shorter than 3
669 * dot cannot be last in string
671 if (dot == -1)
673 if (oem->Length > 8) return FALSE;
675 else
677 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
679 if (spaces) *spaces = got_space;
680 return TRUE;
683 /******************************************************************
684 * RtlGetCurrentDirectory_U (NTDLL.@)
687 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
689 UNICODE_STRING* us;
690 ULONG len;
692 TRACE("(%lu %p)\n", buflen, buf);
694 RtlAcquirePebLock();
696 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
697 len = us->Length / sizeof(WCHAR);
698 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
699 len--;
701 if (buflen / sizeof(WCHAR) > len)
703 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
704 buf[len] = '\0';
706 else
708 len++;
711 RtlReleasePebLock();
713 return len * sizeof(WCHAR);
716 /******************************************************************
717 * RtlSetCurrentDirectory_U (NTDLL.@)
720 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
722 UNICODE_STRING* curdir;
723 NTSTATUS nts = STATUS_SUCCESS;
724 ULONG size;
725 PWSTR buf = NULL;
727 TRACE("(%s)\n", debugstr_w(dir->Buffer));
729 RtlAcquirePebLock();
731 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectoryName;
732 size = curdir->MaximumLength;
734 buf = RtlAllocateHeap(GetProcessHeap(), 0, size);
735 if (buf == NULL)
737 nts = STATUS_NO_MEMORY;
738 goto out;
741 size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
742 if (!size)
744 nts = STATUS_OBJECT_NAME_INVALID;
745 goto out;
748 switch (RtlDetermineDosPathNameType_U(buf))
750 case ABSOLUTE_DRIVE_PATH:
751 case UNC_PATH:
752 break;
753 default:
754 FIXME("Don't support those cases yes\n");
755 nts = STATUS_NOT_IMPLEMENTED;
756 goto out;
759 /* FIXME: should check that the directory actually exists,
760 * and fill CurrentDirectoryHandle accordingly
763 /* append trailing \ if missing */
764 if (buf[size / sizeof(WCHAR) - 1] != '\\')
766 buf[size / sizeof(WCHAR)] = '\\';
767 buf[size / sizeof(WCHAR) + 1] = '\0';
768 size += sizeof(WCHAR);
771 memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
772 curdir->Length = size;
774 #if 0
775 if (curdir->Buffer[1] == ':')
777 UNICODE_STRING env;
778 WCHAR var[4];
780 var[0] = '=';
781 var[1] = curdir->Buffer[0];
782 var[2] = ':';
783 var[3] = 0;
784 env.Length = 3 * sizeof(WCHAR);
785 env.MaximumLength = 4 * sizeof(WCHAR);
786 env.Buffer = var;
788 RtlSetEnvironmentVariable(NULL, &env, curdir);
790 #endif
792 out:
793 if (buf) RtlFreeHeap(GetProcessHeap(), 0, buf);
795 RtlReleasePebLock();
797 return nts;