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>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "wine/library.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(file);
41 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
42 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
43 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
45 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
47 #define MAX_DOS_DRIVES 26
55 /***********************************************************************
58 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
60 static inline int get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
62 const char *config_dir = wine_get_config_dir();
67 buffer = RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") );
68 if (!buffer) return 0;
69 strcpy( buffer, config_dir );
70 strcat( buffer, "/dosdevices/a:" );
71 p = buffer + strlen(buffer) - 2;
73 for (i = ret = 0; i < MAX_DOS_DRIVES; i++)
76 if (!stat( buffer, &st ))
78 info[i].dev = st.st_dev;
79 info[i].ino = st.st_ino;
88 RtlFreeHeap( GetProcessHeap(), 0, buffer );
93 /***********************************************************************
94 * remove_last_component
96 * Remove the last component of the path. Helper for find_drive_root.
98 static inline int remove_last_component( const WCHAR *path, int len )
104 /* find start of the last path component */
106 if (prev <= 1) break; /* reached root */
107 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
108 /* does removing it take us up a level? */
109 if (len - prev != 1 || path[prev] != '.') /* not '.' */
111 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
116 /* strip off trailing slashes */
117 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
124 /***********************************************************************
127 * Find a drive for which the root matches the beginning of the given path.
128 * This can be used to translate a Unix path into a drive + DOS path.
129 * Return value is the drive, or -1 on error. On success, ppath is modified
130 * to point to the beginning of the DOS path.
132 static int find_drive_root( LPCWSTR *ppath )
134 /* Starting with the full path, check if the device and inode match any of
135 * the wine 'drives'. If not then remove the last path component and try
136 * again. If the last component was a '..' then skip a normal component
137 * since it's a directory that's ascended back out of.
139 int drive, lenA, lenW;
141 const WCHAR *path = *ppath;
143 struct drive_info info[MAX_DOS_DRIVES];
145 /* get device and inode of all drives */
146 if (!get_drives_info( info )) return -1;
148 /* strip off trailing slashes */
149 lenW = strlenW(path);
150 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
152 /* convert path to Unix encoding */
153 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
154 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
155 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
157 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
161 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
164 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
166 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
168 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
169 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
170 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
172 RtlFreeHeap( GetProcessHeap(), 0, buffer );
177 if (lenW <= 1) break; /* reached root */
178 lenW = remove_last_component( path, lenW );
180 /* we only need the new length, buffer already contains the converted string */
181 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
184 RtlFreeHeap( GetProcessHeap(), 0, buffer );
189 /***********************************************************************
190 * RtlDetermineDosPathNameType_U (NTDLL.@)
192 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
194 if (IS_SEPARATOR(path[0]))
196 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
197 if (path[2] != '.') return UNC_PATH; /* "//foo" */
198 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
199 if (path[3]) return UNC_PATH; /* "//.foo" */
200 return UNC_DOT_PATH; /* "//." */
204 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
205 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
206 return RELATIVE_DRIVE_PATH; /* "c:foo" */
210 /***********************************************************************
211 * RtlIsDosDeviceName_U (NTDLL.@)
213 * Check if the given DOS path contains a DOS device name.
215 * Returns the length of the device name in the low word and its
216 * position in the high word (both in bytes, not WCHARs), or 0 if no
217 * device name is found.
219 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
221 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
222 static const WCHAR auxW[3] = {'A','U','X'};
223 static const WCHAR comW[3] = {'C','O','M'};
224 static const WCHAR conW[3] = {'C','O','N'};
225 static const WCHAR lptW[3] = {'L','P','T'};
226 static const WCHAR nulW[3] = {'N','U','L'};
227 static const WCHAR prnW[3] = {'P','R','N'};
229 const WCHAR *start, *end, *p;
231 switch(RtlDetermineDosPathNameType_U( dos_name ))
237 if (!strcmpiW( dos_name, consoleW ))
238 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
244 end = dos_name + strlenW(dos_name) - 1;
245 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
247 /* find start of file name */
248 for (start = end; start >= dos_name; start--)
250 if (IS_SEPARATOR(start[0])) break;
251 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
252 if (start[0] == ':' && start[1] != '.') break;
256 /* remove extension */
257 if ((p = strchrW( start, '.' )))
260 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
264 /* no extension, remove trailing spaces */
265 while (end >= dos_name && *end == ' ') end--;
268 /* now we have a potential device name between start and end, check it */
269 switch(end - start + 1)
272 if (strncmpiW( start, auxW, 3 ) &&
273 strncmpiW( start, conW, 3 ) &&
274 strncmpiW( start, nulW, 3 ) &&
275 strncmpiW( start, prnW, 3 )) break;
276 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
278 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
279 if (*end <= '0' || *end > '9') break;
280 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
281 default: /* can't match anything */
288 /**************************************************************************
289 * RtlDosPathNameToNtPathName_U [NTDLL.@]
291 * dos_path: a DOS path name (fully qualified or not)
292 * ntpath: pointer to a UNICODE_STRING to hold the converted
294 * file_part:will point (in ntpath) to the file part in the path
295 * cd: directory reference (optional)
298 * + fill the cd structure
300 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
301 PUNICODE_STRING ntpath,
305 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
307 WCHAR local[MAX_PATH];
310 TRACE("(%s,%p,%p,%p)\n",
311 debugstr_w(dos_path), ntpath, file_part, cd);
315 FIXME("Unsupported parameter\n");
316 memset(cd, 0, sizeof(*cd));
319 if (!dos_path || !*dos_path) return FALSE;
321 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
323 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
324 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
325 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
326 if (!ntpath->Buffer) return FALSE;
327 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
328 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
333 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
334 if (sz == 0) return FALSE;
335 if (sz > sizeof(local))
337 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return FALSE;
338 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
341 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
342 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
345 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
349 strcpyW(ntpath->Buffer, NTDosPrefixW);
350 switch (RtlDetermineDosPathNameType_U(ptr))
352 case UNC_PATH: /* \\foo */
354 strcatW(ntpath->Buffer, UncPfxW);
356 case DEVICE_PATH: /* \\.\foo */
364 strcatW(ntpath->Buffer, ptr + offset);
365 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
367 if (file_part && *file_part)
368 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
370 /* FIXME: cd filling */
372 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
376 /******************************************************************
379 * Searchs a file of name 'name' into a ';' separated list of paths
381 * Doesn't seem to search elsewhere than the paths list
382 * Stores the result in buffer (file_part will point to the position
383 * of the file name in the buffer)
385 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
387 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
388 ULONG buffer_size, LPWSTR buffer,
391 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
394 if (type == RELATIVE_PATH)
396 ULONG allocated = 0, needed, filelen;
399 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
401 /* Windows only checks for '.' without worrying about path components */
402 if (strchrW( search, '.' )) ext = NULL;
403 if (ext != NULL) filelen += strlenW(ext);
409 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
410 if (needed + filelen > allocated)
412 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
413 (needed + filelen) * sizeof(WCHAR));
416 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
417 (needed + filelen) * sizeof(WCHAR));
418 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
422 allocated = needed + filelen;
424 memmove(name, paths, needed * sizeof(WCHAR));
425 /* append '\\' if none is present */
426 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
427 strcpyW(&name[needed], search);
428 if (ext) strcatW(&name[needed], ext);
429 if (RtlDoesFileExists_U(name))
431 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
436 RtlFreeHeap(GetProcessHeap(), 0, name);
438 else if (RtlDoesFileExists_U(search))
440 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
447 /******************************************************************
450 * Helper for RtlGetFullPathName_U.
451 * Get rid of . and .. components in the path.
453 static inline void collapse_path( WCHAR *path, UINT mark )
457 /* convert every / into a \ */
458 for (p = path; *p; p++) if (*p == '/') *p = '\\';
460 /* collapse duplicate backslashes */
461 next = path + max( 1, mark );
462 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
472 case '\\': /* .\ component */
474 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
476 case 0: /* final . */
477 if (p > path + mark) p--;
481 if (p[2] == '\\') /* ..\ component */
487 while (p > path + mark && p[-1] != '\\') p--;
489 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
492 else if (!p[2]) /* final .. */
497 while (p > path + mark && p[-1] != '\\') p--;
498 if (p > path + mark) p--;
506 /* skip to the next component */
507 while (*p && *p != '\\') p++;
511 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
512 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
517 /******************************************************************
520 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
522 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
525 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
526 while (IS_SEPARATOR(*ptr)) ptr++;
527 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
528 while (IS_SEPARATOR(*ptr)) ptr++;
533 /******************************************************************
534 * get_full_path_helper
536 * Helper for RtlGetFullPathName_U
537 * Note: name and buffer are allowed to point to the same memory spot
539 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
541 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
542 DOS_PATHNAME_TYPE type;
543 LPWSTR ins_str = NULL;
545 const UNICODE_STRING* cd;
548 /* return error if name only consists of spaces */
549 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
554 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
555 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
557 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
559 switch (type = RtlDetermineDosPathNameType_U(name))
561 case UNC_PATH: /* \\foo */
562 ptr = skip_unc_prefix( name );
566 case DEVICE_PATH: /* \\.\foo */
570 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
571 reqsize = sizeof(WCHAR);
572 tmp[0] = toupperW(name[0]);
578 case RELATIVE_DRIVE_PATH: /* c:foo */
580 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
582 UNICODE_STRING var, val;
588 var.Length = 3 * sizeof(WCHAR);
589 var.MaximumLength = 4 * sizeof(WCHAR);
592 val.MaximumLength = size;
593 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
595 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
598 /* FIXME: Win2k seems to check that the environment variable actually points
599 * to an existing directory. If not, root of the drive is used
600 * (this seems also to be the only spot in RtlGetFullPathName that the
601 * existence of a part of a path is checked)
604 case STATUS_BUFFER_TOO_SMALL:
605 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
606 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
607 ins_str = val.Buffer;
609 case STATUS_VARIABLE_NOT_FOUND:
610 reqsize = 3 * sizeof(WCHAR);
617 ERR("Unsupported status code\n");
625 case RELATIVE_PATH: /* foo */
626 reqsize = cd->Length;
627 ins_str = cd->Buffer;
628 if (cd->Buffer[1] != ':')
630 ptr = skip_unc_prefix( cd->Buffer );
631 mark = ptr - cd->Buffer;
636 case ABSOLUTE_PATH: /* \xxx */
637 if (name[0] == '/') /* may be a Unix path */
639 const WCHAR *ptr = name;
640 int drive = find_drive_root( &ptr );
643 reqsize = 3 * sizeof(WCHAR);
644 tmp[0] = 'A' + drive;
653 if (cd->Buffer[1] == ':')
655 reqsize = 2 * sizeof(WCHAR);
656 tmp[0] = cd->Buffer[0];
663 ptr = skip_unc_prefix( cd->Buffer );
664 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
665 mark = reqsize / sizeof(WCHAR);
666 ins_str = cd->Buffer;
670 case UNC_DOT_PATH: /* \\. */
671 reqsize = 4 * sizeof(WCHAR);
686 deplen = strlenW(name + dep) * sizeof(WCHAR);
687 if (reqsize + deplen + sizeof(WCHAR) > size)
689 /* not enough space, return need size (including terminating '\0') */
690 reqsize += deplen + sizeof(WCHAR);
694 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
695 if (reqsize) memcpy(buffer, ins_str, reqsize);
698 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
699 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
701 collapse_path( buffer, mark );
702 reqsize = strlenW(buffer) * sizeof(WCHAR);
709 /******************************************************************
710 * RtlGetFullPathName_U (NTDLL.@)
712 * Returns the number of bytes written to buffer (not including the
713 * terminating NULL) if the function succeeds, or the required number of bytes
714 * (including the terminating NULL) if the buffer is too small.
716 * file_part will point to the filename part inside buffer (except if we use
717 * DOS device name, in which case file_in_buf is NULL)
720 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
727 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
729 if (!name || !*name) return 0;
731 if (file_part) *file_part = NULL;
733 /* check for DOS device name */
734 dosdev = RtlIsDosDeviceName_U(name);
737 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
738 DWORD sz = LOWORD(dosdev); /* in bytes */
740 if (8 + sz + 2 > size) return sz + 10;
741 strcpyW(buffer, DeviceRootW);
742 memmove(buffer + 4, name + offset, sz);
743 buffer[4 + sz / sizeof(WCHAR)] = '\0';
744 /* file_part isn't set in this case */
748 reqsize = get_full_path_helper(name, buffer, size);
749 if (!reqsize) return 0;
752 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
753 reqsize = get_full_path_helper(name, tmp, reqsize);
754 if (reqsize > size) /* it may have worked the second time */
756 RtlFreeHeap(GetProcessHeap(), 0, tmp);
757 return reqsize + sizeof(WCHAR);
759 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
760 RtlFreeHeap(GetProcessHeap(), 0, tmp);
764 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
769 /*************************************************************************
770 * RtlGetLongestNtPathLength [NTDLL.@]
772 * Get the longest allowed path length
778 * The longest allowed path length (277 characters under Win2k).
780 DWORD WINAPI RtlGetLongestNtPathLength(void)
785 /******************************************************************
786 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
788 * Returns TRUE iff unicode is a valid DOS (8+3) name.
789 * If the name is valid, oem gets filled with the corresponding OEM string
790 * spaces is set to TRUE if unicode contains spaces
792 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
793 OEM_STRING *oem, BOOLEAN *spaces )
795 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
800 BOOLEAN got_space = FALSE;
804 oem_str.Length = sizeof(buffer);
805 oem_str.MaximumLength = sizeof(buffer);
806 oem_str.Buffer = buffer;
809 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
812 if (oem->Length > 12) return FALSE;
814 /* a starting . is invalid, except for . and .. */
815 if (oem->Buffer[0] == '.')
817 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
818 if (spaces) *spaces = FALSE;
822 for (i = 0; i < oem->Length; i++)
824 switch (oem->Buffer[i])
827 /* leading/trailing spaces not allowed */
828 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
832 if (dot != -1) return FALSE;
836 if (strchr(illegal, oem->Buffer[i])) return FALSE;
840 /* check file part is shorter than 8, extension shorter than 3
841 * dot cannot be last in string
845 if (oem->Length > 8) return FALSE;
849 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
851 if (spaces) *spaces = got_space;
855 /******************************************************************
856 * RtlGetCurrentDirectory_U (NTDLL.@)
859 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
864 TRACE("(%lu %p)\n", buflen, buf);
868 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
869 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
871 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
873 len = us->Length / sizeof(WCHAR);
874 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
877 if (buflen / sizeof(WCHAR) > len)
879 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
889 return len * sizeof(WCHAR);
892 /******************************************************************
893 * RtlSetCurrentDirectory_U (NTDLL.@)
896 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
898 FILE_FS_DEVICE_INFORMATION device_info;
899 OBJECT_ATTRIBUTES attr;
900 UNICODE_STRING newdir;
908 newdir.Buffer = NULL;
912 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
913 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
915 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
917 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
919 nts = STATUS_OBJECT_NAME_INVALID;
923 attr.Length = sizeof(attr);
924 attr.RootDirectory = 0;
925 attr.Attributes = OBJ_CASE_INSENSITIVE;
926 attr.ObjectName = &newdir;
927 attr.SecurityDescriptor = NULL;
928 attr.SecurityQualityOfService = NULL;
930 nts = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
931 if (nts != STATUS_SUCCESS) goto out;
933 /* don't keep the directory handle open on removable media */
934 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
935 sizeof(device_info), FileFsDeviceInformation ) &&
936 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
942 if (curdir->Handle) NtClose( curdir->Handle );
943 curdir->Handle = handle;
945 /* append trailing \ if missing */
946 size = newdir.Length / sizeof(WCHAR);
948 ptr += 4; /* skip \??\ prefix */
950 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
952 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
953 curdir->DosPath.Buffer[size] = 0;
954 curdir->DosPath.Length = size * sizeof(WCHAR);
956 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
959 RtlFreeUnicodeString( &newdir );