If using the default values, also set dwType to REG_SZ as our default
[wine] / dlls / ntdll / path.c
1 /*
2  * Ntdll path functions
3  *
4  * Copyright 2002, 2003 Alexandre Julliard
5  * Copyright 2003 Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25
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"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(file);
35
36 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
37 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
38 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
39
40 /* FIXME: hack! */
41 HANDLE (WINAPI *pCreateFileW)( LPCWSTR filename, DWORD access, DWORD sharing,
42                                LPSECURITY_ATTRIBUTES sa, DWORD creation,
43                                DWORD attributes, HANDLE template );
44
45 #define IS_SEPARATOR(ch)  ((ch) == '\\' || (ch) == '/')
46
47 /***********************************************************************
48  *             RtlDetermineDosPathNameType_U   (NTDLL.@)
49  */
50 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
51 {
52     if (IS_SEPARATOR(path[0]))
53     {
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;                                    /* "//." */
59     }
60     else
61     {
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" */
65     }
66 }
67
68 /******************************************************************
69  *              RtlDoesFileExists_U
70  *
71  * FIXME: should not use CreateFileW
72  */
73 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
74 {
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;
80 }
81
82 /***********************************************************************
83  *             RtlIsDosDeviceName_U   (NTDLL.@)
84  *
85  * Check if the given DOS path contains a DOS device name.
86  *
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.
90  */
91 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
92 {
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'};
100
101     const WCHAR *start, *end, *p;
102
103     switch(RtlDetermineDosPathNameType_U( dos_name ))
104     {
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;
114     }
115
116     end = dos_name + strlenW(dos_name) - 1;
117     if (end >= dos_name && *end == ':') end--;  /* remove trailing ':' */
118
119     /* find start of file name */
120     for (start = end; start >= dos_name; start--)
121     {
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;
125     }
126     start++;
127
128     /* remove extension */
129     if ((p = strchrW( start, '.' )))
130     {
131         end = p - 1;
132         if (end >= dos_name && *end == ':') end--;  /* remove trailing ':' before extension */
133     }
134     else
135     {
136         /* no extension, remove trailing spaces */
137         while (end >= dos_name && *end == ' ') end--;
138     }
139
140     /* now we have a potential device name between start and end, check it */
141     switch(end - start + 1)
142     {
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;
155     }
156     return 0;
157 }
158
159
160 /**************************************************************************
161  *                 RtlDosPathNameToNtPathName_U         [NTDLL.@]
162  *
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)
168  *
169  * FIXME:
170  *      + fill the cd structure
171  */
172 BOOLEAN  WINAPI RtlDosPathNameToNtPathName_U(PWSTR dos_path,
173                                              PUNICODE_STRING ntpath,
174                                              PWSTR* file_part,
175                                              CURDIR* cd)
176 {
177     static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
178     ULONG sz, ptr_sz, offset;
179     WCHAR local[MAX_PATH];
180     LPWSTR ptr;
181
182     TRACE("(%s,%p,%p,%p)\n",
183           debugstr_w(dos_path), ntpath, file_part, cd);
184
185     if (cd)
186     {
187         FIXME("Unsupported parameter\n");
188         memset(cd, 0, sizeof(*cd));
189     }
190
191     if (!dos_path || !*dos_path) return FALSE;
192
193     if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
194     {
195         dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
196         ptr = NULL;
197         ptr_sz = 0;
198     }
199     else
200     {
201         ptr = local;
202         ptr_sz = sizeof(local);
203     }
204     sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
205     if (sz == 0) return FALSE;
206     if (sz > ptr_sz)
207     {
208         ptr = RtlAllocateHeap(ntdll_get_process_heap(), 0, sz);
209         sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
210     }
211
212     ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
213     ntpath->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, ntpath->MaximumLength);
214     if (!ntpath->Buffer)
215     {
216         if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
217         return FALSE;
218     }
219
220     strcpyW(ntpath->Buffer, NTDosPrefixW);
221     offset = 0;
222     switch (RtlDetermineDosPathNameType_U(ptr))
223     {
224     case UNC_PATH: /* \\foo */
225         if (ptr[2] != '?')
226         {
227             offset = 2;
228             strcatW(ntpath->Buffer, UncPfxW);
229         }
230         break;
231     case DEVICE_PATH: /* \\.\foo */
232         offset = 4;
233         break;
234     default: break; /* needed to keep gcc quiet */
235     }
236
237     strcatW(ntpath->Buffer, ptr + offset);
238     ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
239
240     if (file_part && *file_part)
241         *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
242
243     /* FIXME: cd filling */
244
245     if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
246     return TRUE;
247 }
248
249 /******************************************************************
250  *              RtlDosSearchPath_U
251  *
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 ???)
259  */
260 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext, 
261                                 ULONG buffer_size, LPWSTR buffer, 
262                                 LPWSTR* file_part)
263 {
264     DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
265     ULONG len = 0;
266
267     if (type == RELATIVE_PATH)
268     {
269         ULONG allocated = 0, needed, filelen;
270         WCHAR *name = NULL;
271
272         filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
273
274         /* Windows only checks for '.' without worrying about path components */
275         if (strchrW( search, '.' )) ext = NULL;
276         if (ext != NULL) filelen += strlenW(ext);
277
278         while (*paths)
279         {
280             LPCWSTR ptr;
281
282             for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
283             if (needed + filelen > allocated)
284             {
285                 name = (WCHAR*)RtlReAllocateHeap(ntdll_get_process_heap(), 0,
286                                                  name, (needed + filelen) * sizeof(WCHAR));
287                 if (!name) return 0;
288                 allocated = needed + filelen;
289             }
290             memmove(name, paths, needed * sizeof(WCHAR));
291             /* append '\\' if none is present */
292             if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
293             strcpyW(&name[needed], search);
294             if (ext) strcatW(&name[needed], ext);
295             if (RtlDoesFileExists_U(name))
296             {
297                 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
298                 break;
299             }
300             paths = ptr;
301         }
302         RtlFreeHeap(ntdll_get_process_heap(), 0, name);
303     }
304     else if (RtlDoesFileExists_U(search))
305     {
306         len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
307     }
308
309     return len;
310 }
311
312 /******************************************************************
313  *              get_full_path_helper
314  *
315  * Helper for RtlGetFullPathName_U
316  */
317 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
318 {
319     ULONG               reqsize, mark = 0;
320     DOS_PATHNAME_TYPE   type;
321     LPWSTR              ptr;
322     UNICODE_STRING*     cd;
323
324     reqsize = sizeof(WCHAR); /* '\0' at the end */
325
326     RtlAcquirePebLock();
327     cd = &ntdll_get_process_pmts()->CurrentDirectoryName;
328
329     switch (type = RtlDetermineDosPathNameType_U(name))
330     {
331     case UNC_PATH:              /* \\foo   */
332     case DEVICE_PATH:           /* \\.\foo */
333         if (reqsize <= size) buffer[0] = '\0';
334         break;
335
336     case ABSOLUTE_DRIVE_PATH:   /* c:\foo  */
337         reqsize += sizeof(WCHAR);
338         if (reqsize <= size)
339         {
340             buffer[0] = toupperW(name[0]);
341             buffer[1] = '\0';
342         }
343         name++;
344         break;
345
346     case RELATIVE_DRIVE_PATH:   /* c:foo   */
347         if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
348         {
349             WCHAR               drive[4];
350             UNICODE_STRING      var, val;
351
352             drive[0] = '=';
353             drive[1] = name[0];
354             drive[2] = ':';
355             drive[3] = '\0';
356             var.Length = 6;
357             var.MaximumLength = 8;
358             var.Buffer = drive;
359             val.Length = 0;
360             val.MaximumLength = size;
361             val.Buffer = buffer;
362
363             name += 2;
364
365             switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
366             {
367             case STATUS_SUCCESS:
368                 /* FIXME: Win2k seems to check that the environment variable actually points 
369                  * to an existing directory. If not, root of the drive is used
370                  * (this seems also to be the only spot in RtlGetFullPathName that the 
371                  * existence of a part of a path is checked)
372                  */
373                 /* fall thru */
374             case STATUS_BUFFER_TOO_SMALL:
375                 reqsize += val.Length;
376                 /* append trailing \\ */
377                 reqsize += sizeof(WCHAR);
378                 if (reqsize <= size)
379                 {
380                     buffer[reqsize / sizeof(WCHAR) - 2] = '\\';
381                     buffer[reqsize / sizeof(WCHAR) - 1] = '\0';
382                 }
383                 break;
384             case STATUS_VARIABLE_NOT_FOUND:
385                 reqsize += 3 * sizeof(WCHAR);
386                 if (reqsize <= size)
387                 {
388                     buffer[0] = drive[1];
389                     buffer[1] = ':';
390                     buffer[2] = '\\';
391                     buffer[3] = '\0';
392                 }
393                 break;
394             default:
395                 ERR("Unsupported status code\n");
396                 break;
397             }
398             break;
399         }
400         name += 2;
401         /* fall through */
402
403     case RELATIVE_PATH:         /* foo     */
404         reqsize += cd->Length;
405         if (reqsize <= size)
406         {
407             memcpy(buffer, cd->Buffer, cd->Length);
408             buffer[cd->Length / sizeof(WCHAR)] = 0;
409         }
410         if (cd->Buffer[1] != ':')
411         {
412             ptr = strchrW(cd->Buffer + 2, '\\');
413             if (ptr) ptr = strchrW(ptr + 1, '\\');
414             if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
415             mark = ptr - cd->Buffer;
416         }
417         break;
418
419     case ABSOLUTE_PATH:         /* \xxx    */
420         if (cd->Buffer[1] == ':')
421         {
422             reqsize += 2 * sizeof(WCHAR);
423             if (reqsize <= size)
424             {
425                 buffer[0] = cd->Buffer[0];
426                 buffer[1] = ':';
427                 buffer[2] = '\0';
428             }
429         }
430         else
431         {
432             unsigned    len;
433
434             ptr = strchrW(cd->Buffer + 2, '\\');
435             if (ptr) ptr = strchrW(ptr + 1, '\\');
436             if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
437             len = (ptr - cd->Buffer) * sizeof(WCHAR);
438             reqsize += len;
439             mark = len / sizeof(WCHAR);
440             if (reqsize <= size)
441             {
442                 memcpy(buffer, cd->Buffer, len);
443                 buffer[len / sizeof(WCHAR)] = '\0';
444             }
445             else
446                 buffer[0] = '\0';
447         }
448         break;
449
450     case UNC_DOT_PATH:         /* \\.     */
451         reqsize += 4 * sizeof(WCHAR);
452         name += 3;
453         if (reqsize <= size)
454         {
455             buffer[0] = '\\';
456             buffer[1] = '\\';
457             buffer[2] = '.';
458             buffer[3] = '\\';
459             buffer[4] = '\0';
460         }
461         break;
462
463     case INVALID_PATH:
464         reqsize = 0;
465         goto done;
466     }
467
468     reqsize += strlenW(name) * sizeof(WCHAR);
469     if (reqsize > size) goto done;
470
471     strcatW(buffer, name);
472
473     /* convert every / into a \ */
474     for (ptr = buffer; ptr < buffer + size / sizeof(WCHAR); ptr++) 
475         if (*ptr == '/') *ptr = '\\';
476
477     reqsize -= sizeof(WCHAR); /* don't count trailing \0 */
478
479     /* mark is non NULL for UNC names, so start path collapsing after server & share name
480      * otherwise, it's a fully qualified DOS name, so start after the drive designation
481      */
482     for (ptr = buffer + (mark ? mark : 2); ptr < buffer + reqsize / sizeof(WCHAR); )
483     {
484         WCHAR* p = strchrW(ptr, '\\');
485         if (!p) break;
486
487         p++;
488         if (p[0] == '.')
489         {
490             switch (p[1])
491             {
492             case '.':
493                 switch (p[2])
494                 {
495                 case '\\':
496                     {
497                         WCHAR*      prev = p - 2;
498                         while (prev >= buffer + mark && *prev != '\\') prev--;
499                         /* either collapse \foo\.. into \ or \.. into \ */
500                         if (prev < buffer + mark) prev = p - 1;
501                         reqsize -= (p + 2 - prev) * sizeof(WCHAR);
502                         memmove(prev, p + 2, reqsize + sizeof(WCHAR) - (prev - buffer) * sizeof(WCHAR));
503                         p = prev;
504                     }
505                     break;
506                 case '\0':
507                     reqsize -= 2 * sizeof(WCHAR);
508                     *p = 0;
509                     break;
510                 }
511                 break;
512             case '\\':
513                 reqsize -= 2 * sizeof(WCHAR);
514                 memmove(p, p + 2, reqsize + sizeof(WCHAR) - (p - buffer) * sizeof(WCHAR));
515                 break;
516             }
517         }
518         ptr = p;
519     }
520
521 done:
522     RtlReleasePebLock();
523     return reqsize;
524 }
525
526 /******************************************************************
527  *              RtlGetFullPathName_U  (NTDLL.@)
528  *
529  * Returns the number of bytes written to buffer (not including the
530  * terminating NULL) if the function succeeds, or the required number of bytes
531  * (including the terminating NULL) if the buffer is too small.
532  *
533  * file_part will point to the filename part inside buffer (except if we use
534  * DOS device name, in which case file_in_buf is NULL)
535  *
536  */
537 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
538                                   WCHAR** file_part)
539 {
540     WCHAR*      ptr;
541     DWORD       dosdev;
542     DWORD       reqsize;
543
544     TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
545
546     if (!name || !*name) return 0;
547
548     if (file_part) *file_part = NULL;
549
550     /* check for DOS device name */
551     dosdev = RtlIsDosDeviceName_U(name);
552     if (dosdev)
553     {
554         DWORD   offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
555         DWORD   sz = LOWORD(dosdev); /* in bytes */
556
557         if (8 + sz + 2 > size) return sz + 10;
558         strcpyW(buffer, DeviceRootW);
559         memmove(buffer + 4, name + offset, sz);
560         buffer[4 + sz / sizeof(WCHAR)] = '\0';
561         /* file_part isn't set in this case */
562         return sz + 8;
563     }
564
565     reqsize = get_full_path_helper(name, buffer, size);
566     if (reqsize > size)
567     {
568         LPWSTR tmp = RtlAllocateHeap(ntdll_get_process_heap(), 0, reqsize);
569         reqsize = get_full_path_helper(name, tmp, reqsize);
570         if (reqsize > size)  /* it may have worked the second time */
571         {
572             RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
573             return reqsize + sizeof(WCHAR);
574         }
575         memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
576         RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
577     }
578
579     /* find file part */
580     if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
581         *file_part = ptr;
582     return reqsize;
583 }
584
585 /*************************************************************************
586  * RtlGetLongestNtPathLength    [NTDLL.@]
587  *
588  * Get the longest allowed path length
589  *
590  * PARAMS
591  *  None.
592  *
593  * RETURNS
594  *  The longest allowed path length (277 characters under Win2k).
595  */
596 DWORD WINAPI RtlGetLongestNtPathLength(void)
597 {
598     return 277;
599 }
600
601 /******************************************************************
602  *             RtlIsNameLegalDOS8Dot3   (NTDLL.@)
603  *
604  * Returns TRUE iff unicode is a valid DOS (8+3) name.
605  * If the name is valid, oem gets filled with the corresponding OEM string
606  * spaces is set to TRUE if unicode contains spaces
607  */
608 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
609                                        OEM_STRING *oem, BOOLEAN *spaces )
610 {
611     int dot = -1;
612     unsigned int i;
613     char buffer[12];
614     OEM_STRING oem_str;
615     BOOLEAN got_space = FALSE;
616
617     if (!oem)
618     {
619         oem_str.Length = sizeof(buffer);
620         oem_str.MaximumLength = sizeof(buffer);
621         oem_str.Buffer = buffer;
622         oem = &oem_str;
623     }
624     if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
625         return FALSE;
626
627     if (oem->Length > 12) return FALSE;
628
629     /* a starting . is invalid, except for . and .. */
630     if (oem->Buffer[0] == '.')
631     {
632         if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
633         if (spaces) *spaces = FALSE;
634         return TRUE;
635     }
636
637     for (i = 0; i < oem->Length; i++)
638     {
639         switch (oem->Buffer[i])
640         {
641         case ' ':
642             /* leading/trailing spaces not allowed */
643             if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
644             got_space = TRUE;
645             break;
646         case '.':
647             if (dot != -1) return FALSE;
648             dot = i;
649             break;
650         default:
651             /* FIXME: check for invalid chars */
652             break;
653         }
654     }
655     /* check file part is shorter than 8, extension shorter than 3
656      * dot cannot be last in string
657      */
658     if (dot == -1)
659     {
660         if (oem->Length > 8) return FALSE;
661     }
662     else
663     {
664         if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
665     }
666     if (spaces) *spaces = got_space;
667     return TRUE;
668 }
669
670 /******************************************************************
671  *              RtlGetCurrentDirectory_U (NTDLL.@)
672  *
673  */
674 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
675 {
676     UNICODE_STRING*     us;
677     ULONG               len;
678
679     TRACE("(%lu %p)\n", buflen, buf);
680
681     RtlAcquirePebLock();
682
683     us = &ntdll_get_process_pmts()->CurrentDirectoryName;
684     len = us->Length / sizeof(WCHAR);
685     if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
686         len--;
687
688     if (buflen / sizeof(WCHAR) > len)
689     {
690         memcpy(buf, us->Buffer, len * sizeof(WCHAR));
691         buf[len] = '\0';
692     }
693     else
694     {
695         len++;
696     }
697
698     RtlReleasePebLock();
699
700     return len * sizeof(WCHAR);
701 }
702
703 /******************************************************************
704  *              RtlSetCurrentDirectory_U (NTDLL.@)
705  *
706  */
707 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
708 {
709     UNICODE_STRING*     curdir;
710     NTSTATUS            nts = STATUS_SUCCESS;
711     ULONG               size;
712     PWSTR               buf = NULL;
713
714     TRACE("(%s)\n", debugstr_w(dir->Buffer));
715
716     RtlAcquirePebLock();
717
718     curdir = &ntdll_get_process_pmts()->CurrentDirectoryName;
719     size = curdir->MaximumLength;
720
721     buf = RtlAllocateHeap(ntdll_get_process_heap(), 0, size);
722     if (buf == NULL)
723     {
724         nts = STATUS_NO_MEMORY;
725         goto out;
726     }
727
728     size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
729     if (!size)
730     {
731         nts = STATUS_OBJECT_NAME_INVALID;
732         goto out;
733     }
734
735     switch (RtlDetermineDosPathNameType_U(buf))
736     {
737     case ABSOLUTE_DRIVE_PATH:
738     case UNC_PATH:
739         break;
740     default:
741         FIXME("Don't support those cases yes\n");
742         nts = STATUS_NOT_IMPLEMENTED;
743         goto out;
744     }
745
746     /* FIXME: should check that the directory actually exists,
747      * and fill CurrentDirectoryHandle accordingly 
748      */
749
750     /* append trailing \ if missing */
751     if (buf[size / sizeof(WCHAR) - 1] != '\\')
752     {
753         buf[size / sizeof(WCHAR)] = '\\';
754         buf[size / sizeof(WCHAR) + 1] = '\0';
755         size += sizeof(WCHAR);
756     }
757
758     memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
759     curdir->Length = size;
760
761 #if 0
762     if (curdir->Buffer[1] == ':')
763     {
764         UNICODE_STRING  env;
765         WCHAR           var[4];
766
767         var[0] = '=';
768         var[1] = curdir->Buffer[0];
769         var[2] = ':';
770         var[3] = 0;
771         env.Length = 3 * sizeof(WCHAR);
772         env.MaximumLength = 4 * sizeof(WCHAR);
773         env.Buffer = var;
774
775         RtlSetEnvironmentVariable(NULL, &env, curdir);
776     }
777 #endif
778
779  out:
780     if (buf) RtlFreeHeap(ntdll_get_process_heap(), 0, buf);
781
782     RtlReleasePebLock();
783
784     return nts;
785 }