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>
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "wine/library.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(file
);
40 static const WCHAR DeviceRootW
[] = {'\\','\\','.','\\',0};
41 static const WCHAR NTDosPrefixW
[] = {'\\','?','?','\\',0};
42 static const WCHAR UncPfxW
[] = {'U','N','C','\\',0};
44 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
46 #define MAX_DOS_DRIVES 26
54 /***********************************************************************
57 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
59 static inline int get_drives_info( struct drive_info info
[MAX_DOS_DRIVES
] )
61 const char *config_dir
= wine_get_config_dir();
66 buffer
= RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir
) + sizeof("/dosdevices/a:") );
67 if (!buffer
) return 0;
68 strcpy( buffer
, config_dir
);
69 strcat( buffer
, "/dosdevices/a:" );
70 p
= buffer
+ strlen(buffer
) - 2;
72 for (i
= ret
= 0; i
< MAX_DOS_DRIVES
; i
++)
75 if (!stat( buffer
, &st
))
77 info
[i
].dev
= st
.st_dev
;
78 info
[i
].ino
= st
.st_ino
;
87 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
92 /***********************************************************************
93 * remove_last_component
95 * Remove the last component of the path. Helper for find_drive_root.
97 static inline int remove_last_component( const WCHAR
*path
, int len
)
103 /* find start of the last path component */
105 if (prev
<= 1) break; /* reached root */
106 while (prev
> 1 && !IS_SEPARATOR(path
[prev
- 1])) prev
--;
107 /* does removing it take us up a level? */
108 if (len
- prev
!= 1 || path
[prev
] != '.') /* not '.' */
110 if (len
- prev
== 2 && path
[prev
] == '.' && path
[prev
+1] == '.') /* is it '..'? */
115 /* strip off trailing slashes */
116 while (prev
> 1 && IS_SEPARATOR(path
[prev
- 1])) prev
--;
123 /***********************************************************************
126 * Find a drive for which the root matches the beginning of the given path.
127 * This can be used to translate a Unix path into a drive + DOS path.
128 * Return value is the drive, or -1 on error. On success, ppath is modified
129 * to point to the beginning of the DOS path.
131 static int find_drive_root( LPCWSTR
*ppath
)
133 /* Starting with the full path, check if the device and inode match any of
134 * the wine 'drives'. If not then remove the last path component and try
135 * again. If the last component was a '..' then skip a normal component
136 * since it's a directory that's ascended back out of.
138 int drive
, lenA
, lenW
;
140 const WCHAR
*path
= *ppath
;
142 struct drive_info info
[MAX_DOS_DRIVES
];
144 /* get device and inode of all drives */
145 if (!get_drives_info( info
)) return -1;
147 /* strip off trailing slashes */
148 lenW
= strlenW(path
);
149 while (lenW
> 1 && IS_SEPARATOR(path
[lenW
- 1])) lenW
--;
151 /* convert path to Unix encoding */
152 lenA
= ntdll_wcstoumbs( 0, path
, lenW
, NULL
, 0, NULL
, NULL
);
153 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, lenA
+ 1 ))) return -1;
154 lenA
= ntdll_wcstoumbs( 0, path
, lenW
, buffer
, lenA
, NULL
, NULL
);
156 for (p
= buffer
; *p
; p
++) if (*p
== '\\') *p
= '/';
160 if (!stat( buffer
, &st
) && S_ISDIR( st
.st_mode
))
163 for (drive
= 0; drive
< MAX_DOS_DRIVES
; drive
++)
165 if ((info
[drive
].dev
== st
.st_dev
) && (info
[drive
].ino
== st
.st_ino
))
167 if (lenW
== 1) lenW
= 0; /* preserve root slash in returned path */
168 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
169 debugstr_w(path
), 'A' + drive
, debugstr_a(buffer
), debugstr_w(path
+ lenW
));
171 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
176 if (lenW
<= 1) break; /* reached root */
177 lenW
= remove_last_component( path
, lenW
);
179 /* we only need the new length, buffer already contains the converted string */
180 lenA
= ntdll_wcstoumbs( 0, path
, lenW
, NULL
, 0, NULL
, NULL
);
183 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
188 /***********************************************************************
189 * RtlDetermineDosPathNameType_U (NTDLL.@)
191 DOS_PATHNAME_TYPE WINAPI
RtlDetermineDosPathNameType_U( PCWSTR path
)
193 if (IS_SEPARATOR(path
[0]))
195 if (!IS_SEPARATOR(path
[1])) return ABSOLUTE_PATH
; /* "/foo" */
196 if (path
[2] != '.') return UNC_PATH
; /* "//foo" */
197 if (IS_SEPARATOR(path
[3])) return DEVICE_PATH
; /* "//./foo" */
198 if (path
[3]) return UNC_PATH
; /* "//.foo" */
199 return UNC_DOT_PATH
; /* "//." */
203 if (!path
[0] || path
[1] != ':') return RELATIVE_PATH
; /* "foo" */
204 if (IS_SEPARATOR(path
[2])) return ABSOLUTE_DRIVE_PATH
; /* "c:/foo" */
205 return RELATIVE_DRIVE_PATH
; /* "c:foo" */
209 /***********************************************************************
210 * RtlIsDosDeviceName_U (NTDLL.@)
212 * Check if the given DOS path contains a DOS device name.
214 * Returns the length of the device name in the low word and its
215 * position in the high word (both in bytes, not WCHARs), or 0 if no
216 * device name is found.
218 ULONG WINAPI
RtlIsDosDeviceName_U( PCWSTR dos_name
)
220 static const WCHAR consoleW
[] = {'\\','\\','.','\\','C','O','N',0};
221 static const WCHAR auxW
[3] = {'A','U','X'};
222 static const WCHAR comW
[3] = {'C','O','M'};
223 static const WCHAR conW
[3] = {'C','O','N'};
224 static const WCHAR lptW
[3] = {'L','P','T'};
225 static const WCHAR nulW
[3] = {'N','U','L'};
226 static const WCHAR prnW
[3] = {'P','R','N'};
228 const WCHAR
*start
, *end
, *p
;
230 switch(RtlDetermineDosPathNameType_U( dos_name
))
236 if (!strcmpiW( dos_name
, consoleW
))
237 return MAKELONG( sizeof(conW
), 4 * sizeof(WCHAR
) ); /* 4 is length of \\.\ prefix */
243 end
= dos_name
+ strlenW(dos_name
) - 1;
244 if (end
>= dos_name
&& *end
== ':') end
--; /* remove trailing ':' */
246 /* find start of file name */
247 for (start
= end
; start
>= dos_name
; start
--)
249 if (IS_SEPARATOR(start
[0])) break;
250 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
251 if (start
[0] == ':' && start
[1] != '.') break;
255 /* remove extension */
256 if ((p
= strchrW( start
, '.' )))
259 if (end
>= dos_name
&& *end
== ':') end
--; /* remove trailing ':' before extension */
263 /* no extension, remove trailing spaces */
264 while (end
>= dos_name
&& *end
== ' ') end
--;
267 /* now we have a potential device name between start and end, check it */
268 switch(end
- start
+ 1)
271 if (strncmpiW( start
, auxW
, 3 ) &&
272 strncmpiW( start
, conW
, 3 ) &&
273 strncmpiW( start
, nulW
, 3 ) &&
274 strncmpiW( start
, prnW
, 3 )) break;
275 return MAKELONG( 3 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
277 if (strncmpiW( start
, comW
, 3 ) && strncmpiW( start
, lptW
, 3 )) break;
278 if (*end
<= '0' || *end
> '9') break;
279 return MAKELONG( 4 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
280 default: /* can't match anything */
287 /**************************************************************************
288 * RtlDosPathNameToNtPathName_U [NTDLL.@]
290 * dos_path: a DOS path name (fully qualified or not)
291 * ntpath: pointer to a UNICODE_STRING to hold the converted
293 * file_part:will point (in ntpath) to the file part in the path
294 * cd: directory reference (optional)
297 * + fill the cd structure
299 BOOLEAN WINAPI
RtlDosPathNameToNtPathName_U(PCWSTR dos_path
,
300 PUNICODE_STRING ntpath
,
304 static const WCHAR LongFileNamePfxW
[4] = {'\\','\\','?','\\'};
306 WCHAR local
[MAX_PATH
];
309 TRACE("(%s,%p,%p,%p)\n",
310 debugstr_w(dos_path
), ntpath
, file_part
, cd
);
314 FIXME("Unsupported parameter\n");
315 memset(cd
, 0, sizeof(*cd
));
318 if (!dos_path
|| !*dos_path
) return FALSE
;
320 if (!strncmpW(dos_path
, LongFileNamePfxW
, 4))
322 ntpath
->Length
= strlenW(dos_path
) * sizeof(WCHAR
);
323 ntpath
->MaximumLength
= ntpath
->Length
+ sizeof(WCHAR
);
324 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
325 if (!ntpath
->Buffer
) return FALSE
;
326 memcpy( ntpath
->Buffer
, dos_path
, ntpath
->MaximumLength
);
327 ntpath
->Buffer
[1] = '?'; /* change \\?\ to \??\ */
332 sz
= RtlGetFullPathName_U(dos_path
, sizeof(local
), ptr
, file_part
);
333 if (sz
== 0) return FALSE
;
334 if (sz
> sizeof(local
))
336 if (!(ptr
= RtlAllocateHeap(GetProcessHeap(), 0, sz
))) return FALSE
;
337 sz
= RtlGetFullPathName_U(dos_path
, sz
, ptr
, file_part
);
340 ntpath
->MaximumLength
= sz
+ (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR
);
341 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
344 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
348 strcpyW(ntpath
->Buffer
, NTDosPrefixW
);
349 switch (RtlDetermineDosPathNameType_U(ptr
))
351 case UNC_PATH
: /* \\foo */
353 strcatW(ntpath
->Buffer
, UncPfxW
);
355 case DEVICE_PATH
: /* \\.\foo */
363 strcatW(ntpath
->Buffer
, ptr
+ offset
);
364 ntpath
->Length
= strlenW(ntpath
->Buffer
) * sizeof(WCHAR
);
366 if (file_part
&& *file_part
)
367 *file_part
= ntpath
->Buffer
+ ntpath
->Length
/ sizeof(WCHAR
) - strlenW(*file_part
);
369 /* FIXME: cd filling */
371 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
375 /******************************************************************
378 * Searchs a file of name 'name' into a ';' separated list of paths
380 * Doesn't seem to search elsewhere than the paths list
381 * Stores the result in buffer (file_part will point to the position
382 * of the file name in the buffer)
384 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
386 ULONG WINAPI
RtlDosSearchPath_U(LPCWSTR paths
, LPCWSTR search
, LPCWSTR ext
,
387 ULONG buffer_size
, LPWSTR buffer
,
390 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U(search
);
393 if (type
== RELATIVE_PATH
)
395 ULONG allocated
= 0, needed
, filelen
;
398 filelen
= 1 /* for \ */ + strlenW(search
) + 1 /* \0 */;
400 /* Windows only checks for '.' without worrying about path components */
401 if (strchrW( search
, '.' )) ext
= NULL
;
402 if (ext
!= NULL
) filelen
+= strlenW(ext
);
408 for (needed
= 0, ptr
= paths
; *ptr
!= 0 && *ptr
++ != ';'; needed
++);
409 if (needed
+ filelen
> allocated
)
411 if (!name
) name
= RtlAllocateHeap(GetProcessHeap(), 0,
412 (needed
+ filelen
) * sizeof(WCHAR
));
415 WCHAR
*newname
= RtlReAllocateHeap(GetProcessHeap(), 0, name
,
416 (needed
+ filelen
) * sizeof(WCHAR
));
417 if (!newname
) RtlFreeHeap(GetProcessHeap(), 0, name
);
421 allocated
= needed
+ filelen
;
423 memmove(name
, paths
, needed
* sizeof(WCHAR
));
424 /* append '\\' if none is present */
425 if (needed
> 0 && name
[needed
- 1] != '\\') name
[needed
++] = '\\';
426 strcpyW(&name
[needed
], search
);
427 if (ext
) strcatW(&name
[needed
], ext
);
428 if (RtlDoesFileExists_U(name
))
430 len
= RtlGetFullPathName_U(name
, buffer_size
, buffer
, file_part
);
435 RtlFreeHeap(GetProcessHeap(), 0, name
);
437 else if (RtlDoesFileExists_U(search
))
439 len
= RtlGetFullPathName_U(search
, buffer_size
, buffer
, file_part
);
446 /******************************************************************
449 * Helper for RtlGetFullPathName_U.
450 * Get rid of . and .. components in the path.
452 static inline void collapse_path( WCHAR
*path
, UINT mark
)
456 /* convert every / into a \ */
457 for (p
= path
; *p
; p
++) if (*p
== '/') *p
= '\\';
459 /* collapse duplicate backslashes */
460 next
= path
+ max( 1, mark
);
461 for (p
= next
; *p
; p
++) if (*p
!= '\\' || next
[-1] != '\\') *next
++ = *p
;
471 case '\\': /* .\ component */
473 memmove( p
, next
, (strlenW(next
) + 1) * sizeof(WCHAR
) );
475 case 0: /* final . */
476 if (p
> path
+ mark
) p
--;
480 if (p
[2] == '\\') /* ..\ component */
486 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
488 memmove( p
, next
, (strlenW(next
) + 1) * sizeof(WCHAR
) );
491 else if (!p
[2]) /* final .. */
496 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
497 if (p
> path
+ mark
) p
--;
505 /* skip to the next component */
506 while (*p
&& *p
!= '\\') p
++;
510 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
511 while (p
> path
+ mark
&& (p
[-1] == ' ' || p
[-1] == '.')) p
--;
516 /******************************************************************
519 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
521 static const WCHAR
*skip_unc_prefix( const WCHAR
*ptr
)
524 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* share name */
525 while (IS_SEPARATOR(*ptr
)) ptr
++;
526 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* dir name */
527 while (IS_SEPARATOR(*ptr
)) ptr
++;
532 /******************************************************************
533 * get_full_path_helper
535 * Helper for RtlGetFullPathName_U
536 * Note: name and buffer are allowed to point to the same memory spot
538 static ULONG
get_full_path_helper(LPCWSTR name
, LPWSTR buffer
, ULONG size
)
540 ULONG reqsize
= 0, mark
= 0, dep
= 0, deplen
;
541 DOS_PATHNAME_TYPE type
;
542 LPWSTR ins_str
= NULL
;
544 const UNICODE_STRING
* cd
;
547 /* return error if name only consists of spaces */
548 for (ptr
= name
; *ptr
; ptr
++) if (*ptr
!= ' ') break;
553 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
554 cd
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
556 cd
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
558 switch (type
= RtlDetermineDosPathNameType_U(name
))
560 case UNC_PATH
: /* \\foo */
561 ptr
= skip_unc_prefix( name
);
565 case DEVICE_PATH
: /* \\.\foo */
569 case ABSOLUTE_DRIVE_PATH
: /* c:\foo */
570 reqsize
= sizeof(WCHAR
);
571 tmp
[0] = toupperW(name
[0]);
577 case RELATIVE_DRIVE_PATH
: /* c:foo */
579 if (toupperW(name
[0]) != toupperW(cd
->Buffer
[0]) || cd
->Buffer
[1] != ':')
581 UNICODE_STRING var
, val
;
587 var
.Length
= 3 * sizeof(WCHAR
);
588 var
.MaximumLength
= 4 * sizeof(WCHAR
);
591 val
.MaximumLength
= size
;
592 val
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, size
);
594 switch (RtlQueryEnvironmentVariable_U(NULL
, &var
, &val
))
597 /* FIXME: Win2k seems to check that the environment variable actually points
598 * to an existing directory. If not, root of the drive is used
599 * (this seems also to be the only spot in RtlGetFullPathName that the
600 * existence of a part of a path is checked)
603 case STATUS_BUFFER_TOO_SMALL
:
604 reqsize
= val
.Length
+ sizeof(WCHAR
); /* append trailing '\\' */
605 val
.Buffer
[val
.Length
/ sizeof(WCHAR
)] = '\\';
606 ins_str
= val
.Buffer
;
608 case STATUS_VARIABLE_NOT_FOUND
:
609 reqsize
= 3 * sizeof(WCHAR
);
616 ERR("Unsupported status code\n");
624 case RELATIVE_PATH
: /* foo */
625 reqsize
= cd
->Length
;
626 ins_str
= cd
->Buffer
;
627 if (cd
->Buffer
[1] != ':')
629 ptr
= skip_unc_prefix( cd
->Buffer
);
630 mark
= ptr
- cd
->Buffer
;
635 case ABSOLUTE_PATH
: /* \xxx */
636 if (name
[0] == '/') /* may be a Unix path */
638 const WCHAR
*ptr
= name
;
639 int drive
= find_drive_root( &ptr
);
642 reqsize
= 3 * sizeof(WCHAR
);
643 tmp
[0] = 'A' + drive
;
652 if (cd
->Buffer
[1] == ':')
654 reqsize
= 2 * sizeof(WCHAR
);
655 tmp
[0] = cd
->Buffer
[0];
662 ptr
= skip_unc_prefix( cd
->Buffer
);
663 reqsize
= (ptr
- cd
->Buffer
) * sizeof(WCHAR
);
664 mark
= reqsize
/ sizeof(WCHAR
);
665 ins_str
= cd
->Buffer
;
669 case UNC_DOT_PATH
: /* \\. */
670 reqsize
= 4 * sizeof(WCHAR
);
685 deplen
= strlenW(name
+ dep
) * sizeof(WCHAR
);
686 if (reqsize
+ deplen
+ sizeof(WCHAR
) > size
)
688 /* not enough space, return need size (including terminating '\0') */
689 reqsize
+= deplen
+ sizeof(WCHAR
);
693 memmove(buffer
+ reqsize
/ sizeof(WCHAR
), name
+ dep
, deplen
+ sizeof(WCHAR
));
694 if (reqsize
) memcpy(buffer
, ins_str
, reqsize
);
697 if (ins_str
&& ins_str
!= tmp
&& ins_str
!= cd
->Buffer
)
698 RtlFreeHeap(GetProcessHeap(), 0, ins_str
);
700 collapse_path( buffer
, mark
);
701 reqsize
= strlenW(buffer
) * sizeof(WCHAR
);
708 /******************************************************************
709 * RtlGetFullPathName_U (NTDLL.@)
711 * Returns the number of bytes written to buffer (not including the
712 * terminating NULL) if the function succeeds, or the required number of bytes
713 * (including the terminating NULL) if the buffer is too small.
715 * file_part will point to the filename part inside buffer (except if we use
716 * DOS device name, in which case file_in_buf is NULL)
719 DWORD WINAPI
RtlGetFullPathName_U(const WCHAR
* name
, ULONG size
, WCHAR
* buffer
,
726 TRACE("(%s %lu %p %p)\n", debugstr_w(name
), size
, buffer
, file_part
);
728 if (!name
|| !*name
) return 0;
730 if (file_part
) *file_part
= NULL
;
732 /* check for DOS device name */
733 dosdev
= RtlIsDosDeviceName_U(name
);
736 DWORD offset
= HIWORD(dosdev
) / sizeof(WCHAR
); /* get it in WCHARs, not bytes */
737 DWORD sz
= LOWORD(dosdev
); /* in bytes */
739 if (8 + sz
+ 2 > size
) return sz
+ 10;
740 strcpyW(buffer
, DeviceRootW
);
741 memmove(buffer
+ 4, name
+ offset
, sz
);
742 buffer
[4 + sz
/ sizeof(WCHAR
)] = '\0';
743 /* file_part isn't set in this case */
747 reqsize
= get_full_path_helper(name
, buffer
, size
);
748 if (!reqsize
) return 0;
751 LPWSTR tmp
= RtlAllocateHeap(GetProcessHeap(), 0, reqsize
);
752 reqsize
= get_full_path_helper(name
, tmp
, reqsize
);
753 if (reqsize
> size
) /* it may have worked the second time */
755 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
756 return reqsize
+ sizeof(WCHAR
);
758 memcpy( buffer
, tmp
, reqsize
+ sizeof(WCHAR
) );
759 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
763 if (file_part
&& (ptr
= strrchrW(buffer
, '\\')) != NULL
&& ptr
>= buffer
+ 2 && *++ptr
)
768 /*************************************************************************
769 * RtlGetLongestNtPathLength [NTDLL.@]
771 * Get the longest allowed path length
777 * The longest allowed path length (277 characters under Win2k).
779 DWORD WINAPI
RtlGetLongestNtPathLength(void)
784 /******************************************************************
785 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
787 * Returns TRUE iff unicode is a valid DOS (8+3) name.
788 * If the name is valid, oem gets filled with the corresponding OEM string
789 * spaces is set to TRUE if unicode contains spaces
791 BOOLEAN WINAPI
RtlIsNameLegalDOS8Dot3( const UNICODE_STRING
*unicode
,
792 OEM_STRING
*oem
, BOOLEAN
*spaces
)
794 static const char* illegal
= "*?<>|\"+=,;[]:/\\\345";
799 BOOLEAN got_space
= FALSE
;
803 oem_str
.Length
= sizeof(buffer
);
804 oem_str
.MaximumLength
= sizeof(buffer
);
805 oem_str
.Buffer
= buffer
;
808 if (RtlUpcaseUnicodeStringToCountedOemString( oem
, unicode
, FALSE
) != STATUS_SUCCESS
)
811 if (oem
->Length
> 12) return FALSE
;
813 /* a starting . is invalid, except for . and .. */
814 if (oem
->Buffer
[0] == '.')
816 if (oem
->Length
!= 1 && (oem
->Length
!= 2 || oem
->Buffer
[1] != '.')) return FALSE
;
817 if (spaces
) *spaces
= FALSE
;
821 for (i
= 0; i
< oem
->Length
; i
++)
823 switch (oem
->Buffer
[i
])
826 /* leading/trailing spaces not allowed */
827 if (!i
|| i
== oem
->Length
-1 || oem
->Buffer
[i
+1] == '.') return FALSE
;
831 if (dot
!= -1) return FALSE
;
835 if (strchr(illegal
, oem
->Buffer
[i
])) return FALSE
;
839 /* check file part is shorter than 8, extension shorter than 3
840 * dot cannot be last in string
844 if (oem
->Length
> 8) return FALSE
;
848 if (dot
> 8 || (oem
->Length
- dot
> 4) || dot
== oem
->Length
- 1) return FALSE
;
850 if (spaces
) *spaces
= got_space
;
854 /******************************************************************
855 * RtlGetCurrentDirectory_U (NTDLL.@)
858 NTSTATUS WINAPI
RtlGetCurrentDirectory_U(ULONG buflen
, LPWSTR buf
)
863 TRACE("(%lu %p)\n", buflen
, buf
);
867 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
868 us
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
870 us
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
872 len
= us
->Length
/ sizeof(WCHAR
);
873 if (us
->Buffer
[len
- 1] == '\\' && us
->Buffer
[len
- 2] != ':')
876 if (buflen
/ sizeof(WCHAR
) > len
)
878 memcpy(buf
, us
->Buffer
, len
* sizeof(WCHAR
));
888 return len
* sizeof(WCHAR
);
891 /******************************************************************
892 * RtlSetCurrentDirectory_U (NTDLL.@)
895 NTSTATUS WINAPI
RtlSetCurrentDirectory_U(const UNICODE_STRING
* dir
)
897 FILE_FS_DEVICE_INFORMATION device_info
;
898 OBJECT_ATTRIBUTES attr
;
899 UNICODE_STRING newdir
;
907 newdir
.Buffer
= NULL
;
911 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
912 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
914 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
916 if (!RtlDosPathNameToNtPathName_U( dir
->Buffer
, &newdir
, NULL
, NULL
))
918 nts
= STATUS_OBJECT_NAME_INVALID
;
922 attr
.Length
= sizeof(attr
);
923 attr
.RootDirectory
= 0;
924 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
925 attr
.ObjectName
= &newdir
;
926 attr
.SecurityDescriptor
= NULL
;
927 attr
.SecurityQualityOfService
= NULL
;
929 nts
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
930 if (nts
!= STATUS_SUCCESS
) goto out
;
932 /* don't keep the directory handle open on removable media */
933 if (!NtQueryVolumeInformationFile( handle
, &io
, &device_info
,
934 sizeof(device_info
), FileFsDeviceInformation
) &&
935 (device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
))
941 if (curdir
->Handle
) NtClose( curdir
->Handle
);
942 curdir
->Handle
= handle
;
944 /* append trailing \ if missing */
945 size
= newdir
.Length
/ sizeof(WCHAR
);
947 ptr
+= 4; /* skip \??\ prefix */
949 if (size
&& ptr
[size
- 1] != '\\') ptr
[size
++] = '\\';
951 memcpy( curdir
->DosPath
.Buffer
, ptr
, size
* sizeof(WCHAR
));
952 curdir
->DosPath
.Buffer
[size
] = 0;
953 curdir
->DosPath
.Length
= size
* sizeof(WCHAR
);
955 TRACE( "curdir now %s %p\n", debugstr_w(curdir
->DosPath
.Buffer
), curdir
->Handle
);
958 RtlFreeUnicodeString( &newdir
);