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>
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/library.h"
38 #include "ntdll_misc.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(file);
42 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
43 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
44 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
46 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
48 #define MAX_DOS_DRIVES 26
56 /***********************************************************************
59 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
61 static inline int get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
63 const char *config_dir = wine_get_config_dir();
68 buffer = RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") );
69 if (!buffer) return 0;
70 strcpy( buffer, config_dir );
71 strcat( buffer, "/dosdevices/a:" );
72 p = buffer + strlen(buffer) - 2;
74 for (i = ret = 0; i < MAX_DOS_DRIVES; i++)
77 if (!stat( buffer, &st ))
79 info[i].dev = st.st_dev;
80 info[i].ino = st.st_ino;
89 RtlFreeHeap( GetProcessHeap(), 0, buffer );
94 /***********************************************************************
95 * remove_last_component
97 * Remove the last component of the path. Helper for find_drive_root.
99 static inline int remove_last_component( const WCHAR *path, int len )
105 /* find start of the last path component */
107 if (prev <= 1) break; /* reached root */
108 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
109 /* does removing it take us up a level? */
110 if (len - prev != 1 || path[prev] != '.') /* not '.' */
112 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
117 /* strip off trailing slashes */
118 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
125 /***********************************************************************
128 * Find a drive for which the root matches the beginning of the given path.
129 * This can be used to translate a Unix path into a drive + DOS path.
130 * Return value is the drive, or -1 on error. On success, ppath is modified
131 * to point to the beginning of the DOS path.
133 static int find_drive_root( LPCWSTR *ppath )
135 /* Starting with the full path, check if the device and inode match any of
136 * the wine 'drives'. If not then remove the last path component and try
137 * again. If the last component was a '..' then skip a normal component
138 * since it's a directory that's ascended back out of.
140 int drive, lenA, lenW;
142 const WCHAR *path = *ppath;
144 struct drive_info info[MAX_DOS_DRIVES];
146 /* get device and inode of all drives */
147 if (!get_drives_info( info )) return -1;
149 /* strip off trailing slashes */
150 lenW = strlenW(path);
151 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
153 /* convert path to Unix encoding */
154 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
155 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
156 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
158 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
162 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
165 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
167 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
169 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
170 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
171 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
173 RtlFreeHeap( GetProcessHeap(), 0, buffer );
178 if (lenW <= 1) break; /* reached root */
179 lenW = remove_last_component( path, lenW );
181 /* we only need the new length, buffer already contains the converted string */
182 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
185 RtlFreeHeap( GetProcessHeap(), 0, buffer );
190 /***********************************************************************
191 * RtlDetermineDosPathNameType_U (NTDLL.@)
193 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
195 if (IS_SEPARATOR(path[0]))
197 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
198 if (path[2] != '.') return UNC_PATH; /* "//foo" */
199 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
200 if (path[3]) return UNC_PATH; /* "//.foo" */
201 return UNC_DOT_PATH; /* "//." */
205 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
206 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
207 return RELATIVE_DRIVE_PATH; /* "c:foo" */
211 /***********************************************************************
212 * RtlIsDosDeviceName_U (NTDLL.@)
214 * Check if the given DOS path contains a DOS device name.
216 * Returns the length of the device name in the low word and its
217 * position in the high word (both in bytes, not WCHARs), or 0 if no
218 * device name is found.
220 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
222 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
223 static const WCHAR auxW[3] = {'A','U','X'};
224 static const WCHAR comW[3] = {'C','O','M'};
225 static const WCHAR conW[3] = {'C','O','N'};
226 static const WCHAR lptW[3] = {'L','P','T'};
227 static const WCHAR nulW[3] = {'N','U','L'};
228 static const WCHAR prnW[3] = {'P','R','N'};
230 const WCHAR *start, *end, *p;
232 switch(RtlDetermineDosPathNameType_U( dos_name ))
238 if (!strcmpiW( dos_name, consoleW ))
239 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
245 end = dos_name + strlenW(dos_name) - 1;
246 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
248 /* find start of file name */
249 for (start = end; start >= dos_name; start--)
251 if (IS_SEPARATOR(start[0])) break;
252 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
253 if (start[0] == ':' && start[1] != '.') break;
257 /* remove extension */
258 if ((p = strchrW( start, '.' )))
261 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
265 /* no extension, remove trailing spaces */
266 while (end >= dos_name && *end == ' ') end--;
269 /* now we have a potential device name between start and end, check it */
270 switch(end - start + 1)
273 if (strncmpiW( start, auxW, 3 ) &&
274 strncmpiW( start, conW, 3 ) &&
275 strncmpiW( start, nulW, 3 ) &&
276 strncmpiW( start, prnW, 3 )) break;
277 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
279 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
280 if (*end <= '0' || *end > '9') break;
281 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
282 default: /* can't match anything */
289 /**************************************************************************
290 * RtlDosPathNameToNtPathName_U [NTDLL.@]
292 * dos_path: a DOS path name (fully qualified or not)
293 * ntpath: pointer to a UNICODE_STRING to hold the converted
295 * file_part:will point (in ntpath) to the file part in the path
296 * cd: directory reference (optional)
299 * + fill the cd structure
301 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
302 PUNICODE_STRING ntpath,
306 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
308 WCHAR local[MAX_PATH];
311 TRACE("(%s,%p,%p,%p)\n",
312 debugstr_w(dos_path), ntpath, file_part, cd);
316 FIXME("Unsupported parameter\n");
317 memset(cd, 0, sizeof(*cd));
320 if (!dos_path || !*dos_path) return FALSE;
322 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
324 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
325 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
326 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
327 if (!ntpath->Buffer) return FALSE;
328 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
329 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
334 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
335 if (sz == 0) return FALSE;
336 if (sz > sizeof(local))
338 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return FALSE;
339 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
342 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
343 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
346 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
350 strcpyW(ntpath->Buffer, NTDosPrefixW);
351 switch (RtlDetermineDosPathNameType_U(ptr))
353 case UNC_PATH: /* \\foo */
355 strcatW(ntpath->Buffer, UncPfxW);
357 case DEVICE_PATH: /* \\.\foo */
365 strcatW(ntpath->Buffer, ptr + offset);
366 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
368 if (file_part && *file_part)
369 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
371 /* FIXME: cd filling */
373 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
377 /******************************************************************
380 * Searchs a file of name 'name' into a ';' separated list of paths
382 * Doesn't seem to search elsewhere than the paths list
383 * Stores the result in buffer (file_part will point to the position
384 * of the file name in the buffer)
386 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
388 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
389 ULONG buffer_size, LPWSTR buffer,
392 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
395 if (type == RELATIVE_PATH)
397 ULONG allocated = 0, needed, filelen;
400 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
402 /* Windows only checks for '.' without worrying about path components */
403 if (strchrW( search, '.' )) ext = NULL;
404 if (ext != NULL) filelen += strlenW(ext);
410 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
411 if (needed + filelen > allocated)
413 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
414 (needed + filelen) * sizeof(WCHAR));
417 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
418 (needed + filelen) * sizeof(WCHAR));
419 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
423 allocated = needed + filelen;
425 memmove(name, paths, needed * sizeof(WCHAR));
426 /* append '\\' if none is present */
427 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
428 strcpyW(&name[needed], search);
429 if (ext) strcatW(&name[needed], ext);
430 if (RtlDoesFileExists_U(name))
432 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
437 RtlFreeHeap(GetProcessHeap(), 0, name);
439 else if (RtlDoesFileExists_U(search))
441 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
448 /******************************************************************
451 * Helper for RtlGetFullPathName_U.
452 * Get rid of . and .. components in the path.
454 static inline void collapse_path( WCHAR *path, UINT mark )
458 /* convert every / into a \ */
459 for (p = path; *p; p++) if (*p == '/') *p = '\\';
461 /* collapse duplicate backslashes */
462 next = path + max( 1, mark );
463 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
473 case '\\': /* .\ component */
475 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
477 case 0: /* final . */
478 if (p > path + mark) p--;
482 if (p[2] == '\\') /* ..\ component */
488 while (p > path + mark && p[-1] != '\\') p--;
490 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
493 else if (!p[2]) /* final .. */
498 while (p > path + mark && p[-1] != '\\') p--;
499 if (p > path + mark) p--;
507 /* skip to the next component */
508 while (*p && *p != '\\') p++;
512 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
513 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
518 /******************************************************************
521 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
523 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
526 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
527 while (IS_SEPARATOR(*ptr)) ptr++;
528 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
529 while (IS_SEPARATOR(*ptr)) ptr++;
534 /******************************************************************
535 * get_full_path_helper
537 * Helper for RtlGetFullPathName_U
538 * Note: name and buffer are allowed to point to the same memory spot
540 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
542 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
543 DOS_PATHNAME_TYPE type;
544 LPWSTR ins_str = NULL;
546 const UNICODE_STRING* cd;
549 /* return error if name only consists of spaces */
550 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
555 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
556 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
558 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
560 switch (type = RtlDetermineDosPathNameType_U(name))
562 case UNC_PATH: /* \\foo */
563 ptr = skip_unc_prefix( name );
567 case DEVICE_PATH: /* \\.\foo */
571 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
572 reqsize = sizeof(WCHAR);
573 tmp[0] = toupperW(name[0]);
579 case RELATIVE_DRIVE_PATH: /* c:foo */
581 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
583 UNICODE_STRING var, val;
589 var.Length = 3 * sizeof(WCHAR);
590 var.MaximumLength = 4 * sizeof(WCHAR);
593 val.MaximumLength = size;
594 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
596 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
599 /* FIXME: Win2k seems to check that the environment variable actually points
600 * to an existing directory. If not, root of the drive is used
601 * (this seems also to be the only spot in RtlGetFullPathName that the
602 * existence of a part of a path is checked)
605 case STATUS_BUFFER_TOO_SMALL:
606 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
607 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
608 ins_str = val.Buffer;
610 case STATUS_VARIABLE_NOT_FOUND:
611 reqsize = 3 * sizeof(WCHAR);
618 ERR("Unsupported status code\n");
626 case RELATIVE_PATH: /* foo */
627 reqsize = cd->Length;
628 ins_str = cd->Buffer;
629 if (cd->Buffer[1] != ':')
631 ptr = skip_unc_prefix( cd->Buffer );
632 mark = ptr - cd->Buffer;
637 case ABSOLUTE_PATH: /* \xxx */
638 if (name[0] == '/') /* may be a Unix path */
640 const WCHAR *ptr = name;
641 int drive = find_drive_root( &ptr );
644 reqsize = 3 * sizeof(WCHAR);
645 tmp[0] = 'A' + drive;
654 if (cd->Buffer[1] == ':')
656 reqsize = 2 * sizeof(WCHAR);
657 tmp[0] = cd->Buffer[0];
664 ptr = skip_unc_prefix( cd->Buffer );
665 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
666 mark = reqsize / sizeof(WCHAR);
667 ins_str = cd->Buffer;
671 case UNC_DOT_PATH: /* \\. */
672 reqsize = 4 * sizeof(WCHAR);
687 deplen = strlenW(name + dep) * sizeof(WCHAR);
688 if (reqsize + deplen + sizeof(WCHAR) > size)
690 /* not enough space, return need size (including terminating '\0') */
691 reqsize += deplen + sizeof(WCHAR);
695 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
696 if (reqsize) memcpy(buffer, ins_str, reqsize);
699 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
700 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
702 collapse_path( buffer, mark );
703 reqsize = strlenW(buffer) * sizeof(WCHAR);
710 /******************************************************************
711 * RtlGetFullPathName_U (NTDLL.@)
713 * Returns the number of bytes written to buffer (not including the
714 * terminating NULL) if the function succeeds, or the required number of bytes
715 * (including the terminating NULL) if the buffer is too small.
717 * file_part will point to the filename part inside buffer (except if we use
718 * DOS device name, in which case file_in_buf is NULL)
721 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
728 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
730 if (!name || !*name) return 0;
732 if (file_part) *file_part = NULL;
734 /* check for DOS device name */
735 dosdev = RtlIsDosDeviceName_U(name);
738 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
739 DWORD sz = LOWORD(dosdev); /* in bytes */
741 if (8 + sz + 2 > size) return sz + 10;
742 strcpyW(buffer, DeviceRootW);
743 memmove(buffer + 4, name + offset, sz);
744 buffer[4 + sz / sizeof(WCHAR)] = '\0';
745 /* file_part isn't set in this case */
749 reqsize = get_full_path_helper(name, buffer, size);
750 if (!reqsize) return 0;
753 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
754 reqsize = get_full_path_helper(name, tmp, reqsize);
755 if (reqsize > size) /* it may have worked the second time */
757 RtlFreeHeap(GetProcessHeap(), 0, tmp);
758 return reqsize + sizeof(WCHAR);
760 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
761 RtlFreeHeap(GetProcessHeap(), 0, tmp);
765 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
770 /*************************************************************************
771 * RtlGetLongestNtPathLength [NTDLL.@]
773 * Get the longest allowed path length
779 * The longest allowed path length (277 characters under Win2k).
781 DWORD WINAPI RtlGetLongestNtPathLength(void)
786 /******************************************************************
787 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
789 * Returns TRUE iff unicode is a valid DOS (8+3) name.
790 * If the name is valid, oem gets filled with the corresponding OEM string
791 * spaces is set to TRUE if unicode contains spaces
793 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
794 OEM_STRING *oem, BOOLEAN *spaces )
796 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
801 BOOLEAN got_space = FALSE;
805 oem_str.Length = sizeof(buffer);
806 oem_str.MaximumLength = sizeof(buffer);
807 oem_str.Buffer = buffer;
810 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
813 if (oem->Length > 12) return FALSE;
815 /* a starting . is invalid, except for . and .. */
816 if (oem->Buffer[0] == '.')
818 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
819 if (spaces) *spaces = FALSE;
823 for (i = 0; i < oem->Length; i++)
825 switch (oem->Buffer[i])
828 /* leading/trailing spaces not allowed */
829 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
833 if (dot != -1) return FALSE;
837 if (strchr(illegal, oem->Buffer[i])) return FALSE;
841 /* check file part is shorter than 8, extension shorter than 3
842 * dot cannot be last in string
846 if (oem->Length > 8) return FALSE;
850 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
852 if (spaces) *spaces = got_space;
856 /******************************************************************
857 * RtlGetCurrentDirectory_U (NTDLL.@)
860 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
865 TRACE("(%lu %p)\n", buflen, buf);
869 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
870 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
872 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
874 len = us->Length / sizeof(WCHAR);
875 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
878 if (buflen / sizeof(WCHAR) > len)
880 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
890 return len * sizeof(WCHAR);
893 /******************************************************************
894 * RtlSetCurrentDirectory_U (NTDLL.@)
897 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
899 FILE_FS_DEVICE_INFORMATION device_info;
900 OBJECT_ATTRIBUTES attr;
901 UNICODE_STRING newdir;
909 newdir.Buffer = NULL;
913 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
914 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
916 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
918 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
920 nts = STATUS_OBJECT_NAME_INVALID;
924 attr.Length = sizeof(attr);
925 attr.RootDirectory = 0;
926 attr.Attributes = OBJ_CASE_INSENSITIVE;
927 attr.ObjectName = &newdir;
928 attr.SecurityDescriptor = NULL;
929 attr.SecurityQualityOfService = NULL;
931 nts = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
932 if (nts != STATUS_SUCCESS) goto out;
934 /* don't keep the directory handle open on removable media */
935 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
936 sizeof(device_info), FileFsDeviceInformation ) &&
937 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
943 if (curdir->Handle) NtClose( curdir->Handle );
944 curdir->Handle = handle;
946 /* append trailing \ if missing */
947 size = newdir.Length / sizeof(WCHAR);
949 ptr += 4; /* skip \??\ prefix */
951 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
953 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
954 curdir->DosPath.Buffer[size] = 0;
955 curdir->DosPath.Length = size * sizeof(WCHAR);
957 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
960 RtlFreeUnicodeString( &newdir );