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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <sys/types.h>
26 #define WIN32_NO_STATUS
29 #include "wine/debug.h"
30 #include "ntdll_misc.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(file
);
34 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
36 /***********************************************************************
37 * RtlDetermineDosPathNameType_U (NTDLL.@)
39 DOS_PATHNAME_TYPE WINAPI
RtlDetermineDosPathNameType_U( PCWSTR path
)
41 if (IS_SEPARATOR(path
[0]))
43 if (!IS_SEPARATOR(path
[1])) return ABSOLUTE_PATH
; /* "/foo" */
44 if (path
[2] != '.' && path
[2] != '?') return UNC_PATH
; /* "//foo" */
45 if (IS_SEPARATOR(path
[3])) return DEVICE_PATH
; /* "//./foo" or "//?/foo" */
46 if (path
[3]) return UNC_PATH
; /* "//.foo" or "//?foo" */
47 return UNC_DOT_PATH
; /* "//." or "//?" */
51 if (!path
[0] || path
[1] != ':') return RELATIVE_PATH
; /* "foo" */
52 if (IS_SEPARATOR(path
[2])) return ABSOLUTE_DRIVE_PATH
; /* "c:/foo" */
53 return RELATIVE_DRIVE_PATH
; /* "c:foo" */
57 /***********************************************************************
58 * RtlIsDosDeviceName_U (NTDLL.@)
60 * Check if the given DOS path contains a DOS device name.
62 * Returns the length of the device name in the low word and its
63 * position in the high word (both in bytes, not WCHARs), or 0 if no
64 * device name is found.
66 ULONG WINAPI
RtlIsDosDeviceName_U( PCWSTR dos_name
)
68 static const WCHAR auxW
[] = {'A','U','X'};
69 static const WCHAR comW
[] = {'C','O','M'};
70 static const WCHAR conW
[] = {'C','O','N'};
71 static const WCHAR lptW
[] = {'L','P','T'};
72 static const WCHAR nulW
[] = {'N','U','L'};
73 static const WCHAR prnW
[] = {'P','R','N'};
74 static const WCHAR coninW
[] = {'C','O','N','I','N','$'};
75 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$'};
77 const WCHAR
*start
, *end
, *p
;
79 switch(RtlDetermineDosPathNameType_U( dos_name
))
85 if (!wcsicmp( dos_name
, L
"\\\\.\\CON" ))
86 return MAKELONG( sizeof(conW
), 4 * sizeof(WCHAR
) ); /* 4 is length of \\.\ prefix */
88 case ABSOLUTE_DRIVE_PATH
:
89 case RELATIVE_DRIVE_PATH
:
90 start
= dos_name
+ 2; /* skip drive letter */
97 /* find start of file name */
98 for (p
= start
; *p
; p
++) if (IS_SEPARATOR(*p
)) start
= p
+ 1;
100 /* truncate at extension and ':' */
101 for (end
= start
; *end
; end
++) if (*end
== '.' || *end
== ':') break;
104 /* remove trailing spaces */
105 while (end
>= start
&& *end
== ' ') end
--;
107 /* now we have a potential device name between start and end, check it */
108 switch(end
- start
+ 1)
111 if (wcsnicmp( start
, auxW
, 3 ) &&
112 wcsnicmp( start
, conW
, 3 ) &&
113 wcsnicmp( start
, nulW
, 3 ) &&
114 wcsnicmp( start
, prnW
, 3 )) break;
115 return MAKELONG( 3 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
117 if (wcsnicmp( start
, comW
, 3 ) && wcsnicmp( start
, lptW
, 3 )) break;
118 if (*end
<= '0' || *end
> '9') break;
119 return MAKELONG( 4 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
121 if (wcsnicmp( start
, coninW
, ARRAY_SIZE(coninW
) )) break;
122 return MAKELONG( sizeof(coninW
), (start
- dos_name
) * sizeof(WCHAR
) );
124 if (wcsnicmp( start
, conoutW
, ARRAY_SIZE(conoutW
) )) break;
125 return MAKELONG( sizeof(conoutW
), (start
- dos_name
) * sizeof(WCHAR
) );
126 default: /* can't match anything */
132 /**************************************************************************
133 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
135 * dos_path: a DOS path name (fully qualified or not)
136 * ntpath: pointer to a UNICODE_STRING to hold the converted
138 * file_part:will point (in ntpath) to the file part in the path
139 * cd: directory reference (optional)
142 * + fill the cd structure
144 NTSTATUS WINAPI
RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR
*dos_path
, UNICODE_STRING
*ntpath
,
145 WCHAR
**file_part
, CURDIR
*cd
)
147 static const WCHAR global_prefix
[] = {'\\','\\','?','\\'};
148 static const WCHAR global_prefix2
[] = {'\\','?','?','\\'};
150 WCHAR local
[MAX_PATH
];
153 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path
), ntpath
, file_part
, cd
);
157 FIXME("Unsupported parameter\n");
158 memset(cd
, 0, sizeof(*cd
));
161 if (!dos_path
|| !*dos_path
)
162 return STATUS_OBJECT_NAME_INVALID
;
164 if (!memcmp(dos_path
, global_prefix
, sizeof(global_prefix
)) ||
165 (!memcmp(dos_path
, global_prefix2
, sizeof(global_prefix2
)) && dos_path
[4]))
167 ntpath
->Length
= wcslen(dos_path
) * sizeof(WCHAR
);
168 ntpath
->MaximumLength
= ntpath
->Length
+ sizeof(WCHAR
);
169 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
170 if (!ntpath
->Buffer
) return STATUS_NO_MEMORY
;
171 memcpy( ntpath
->Buffer
, dos_path
, ntpath
->MaximumLength
);
172 ntpath
->Buffer
[1] = '?'; /* change \\?\ to \??\ */
175 if ((ptr
= wcsrchr( ntpath
->Buffer
, '\\' )) && ptr
[1]) *file_part
= ptr
+ 1;
176 else *file_part
= NULL
;
178 return STATUS_SUCCESS
;
182 sz
= RtlGetFullPathName_U(dos_path
, sizeof(local
), ptr
, file_part
);
183 if (sz
== 0) return STATUS_OBJECT_NAME_INVALID
;
185 if (sz
> sizeof(local
))
187 if (!(ptr
= RtlAllocateHeap(GetProcessHeap(), 0, sz
))) return STATUS_NO_MEMORY
;
188 sz
= RtlGetFullPathName_U(dos_path
, sz
, ptr
, file_part
);
190 sz
+= (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR
);
193 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
194 return STATUS_OBJECT_NAME_INVALID
;
197 ntpath
->MaximumLength
= sz
;
198 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
201 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
202 return STATUS_NO_MEMORY
;
205 wcscpy(ntpath
->Buffer
, L
"\\??\\");
206 switch (RtlDetermineDosPathNameType_U(ptr
))
208 case UNC_PATH
: /* \\foo */
210 wcscat(ntpath
->Buffer
, L
"UNC\\");
212 case DEVICE_PATH
: /* \\.\foo */
220 wcscat(ntpath
->Buffer
, ptr
+ offset
);
221 ntpath
->Length
= wcslen(ntpath
->Buffer
) * sizeof(WCHAR
);
223 if (file_part
&& *file_part
)
224 *file_part
= ntpath
->Buffer
+ ntpath
->Length
/ sizeof(WCHAR
) - wcslen(*file_part
);
226 /* FIXME: cd filling */
228 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
229 return STATUS_SUCCESS
;
232 /**************************************************************************
233 * RtlDosPathNameToNtPathName_U [NTDLL.@]
235 * See RtlDosPathNameToNtPathName_U_WithStatus
237 BOOLEAN WINAPI
RtlDosPathNameToNtPathName_U(PCWSTR dos_path
,
238 PUNICODE_STRING ntpath
,
242 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path
, ntpath
, file_part
, cd
) == STATUS_SUCCESS
;
245 /**************************************************************************
246 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
248 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
250 NTSTATUS WINAPI
RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR
*dos_path
,
251 UNICODE_STRING
*ntpath
, WCHAR
**file_part
, RTL_RELATIVE_NAME
*relative
)
253 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path
), ntpath
, file_part
, relative
);
257 FIXME("Unsupported parameter\n");
258 memset(relative
, 0, sizeof(*relative
));
261 /* FIXME: fill parameter relative */
263 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path
, ntpath
, file_part
, NULL
);
266 /**************************************************************************
267 * RtlReleaseRelativeName [NTDLL.@]
269 void WINAPI
RtlReleaseRelativeName(RTL_RELATIVE_NAME
*relative
)
273 /******************************************************************
276 * Searches a file of name 'name' into a ';' separated list of paths
278 * Doesn't seem to search elsewhere than the paths list
279 * Stores the result in buffer (file_part will point to the position
280 * of the file name in the buffer)
282 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
284 ULONG WINAPI
RtlDosSearchPath_U(LPCWSTR paths
, LPCWSTR search
, LPCWSTR ext
,
285 ULONG buffer_size
, LPWSTR buffer
,
288 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U(search
);
291 if (type
== RELATIVE_PATH
)
293 ULONG allocated
= 0, needed
, filelen
;
296 filelen
= 1 /* for \ */ + wcslen(search
) + 1 /* \0 */;
298 /* Windows only checks for '.' without worrying about path components */
299 if (wcschr( search
, '.' )) ext
= NULL
;
300 if (ext
!= NULL
) filelen
+= wcslen(ext
);
306 for (needed
= 0, ptr
= paths
; *ptr
!= 0 && *ptr
++ != ';'; needed
++);
307 if (needed
+ filelen
> allocated
)
309 if (!name
) name
= RtlAllocateHeap(GetProcessHeap(), 0,
310 (needed
+ filelen
) * sizeof(WCHAR
));
313 WCHAR
*newname
= RtlReAllocateHeap(GetProcessHeap(), 0, name
,
314 (needed
+ filelen
) * sizeof(WCHAR
));
315 if (!newname
) RtlFreeHeap(GetProcessHeap(), 0, name
);
319 allocated
= needed
+ filelen
;
321 memmove(name
, paths
, needed
* sizeof(WCHAR
));
322 /* append '\\' if none is present */
323 if (needed
> 0 && name
[needed
- 1] != '\\') name
[needed
++] = '\\';
324 wcscpy(&name
[needed
], search
);
325 if (ext
) wcscat(&name
[needed
], ext
);
326 if (RtlDoesFileExists_U(name
))
328 len
= RtlGetFullPathName_U(name
, buffer_size
, buffer
, file_part
);
333 RtlFreeHeap(GetProcessHeap(), 0, name
);
335 else if (RtlDoesFileExists_U(search
))
337 len
= RtlGetFullPathName_U(search
, buffer_size
, buffer
, file_part
);
344 /******************************************************************
347 * Helper for RtlGetFullPathName_U.
348 * Get rid of . and .. components in the path.
350 static inline void collapse_path( WCHAR
*path
, UINT mark
)
354 /* convert every / into a \ */
355 for (p
= path
; *p
; p
++) if (*p
== '/') *p
= '\\';
357 /* collapse duplicate backslashes */
358 next
= path
+ max( 1, mark
);
359 for (p
= next
; *p
; p
++) if (*p
!= '\\' || next
[-1] != '\\') *next
++ = *p
;
369 case '\\': /* .\ component */
371 memmove( p
, next
, (wcslen(next
) + 1) * sizeof(WCHAR
) );
373 case 0: /* final . */
374 if (p
> path
+ mark
) p
--;
378 if (p
[2] == '\\') /* ..\ component */
384 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
386 memmove( p
, next
, (wcslen(next
) + 1) * sizeof(WCHAR
) );
389 else if (!p
[2]) /* final .. */
394 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
395 if (p
> path
+ mark
) p
--;
403 /* skip to the next component */
404 while (*p
&& *p
!= '\\') p
++;
407 /* remove last dot in previous dir name */
408 if (p
> path
+ mark
&& p
[-1] == '.') memmove( p
-1, p
, (wcslen(p
) + 1) * sizeof(WCHAR
) );
413 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
414 while (p
> path
+ mark
&& (p
[-1] == ' ' || p
[-1] == '.')) p
--;
419 /******************************************************************
422 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
424 static const WCHAR
*skip_unc_prefix( const WCHAR
*ptr
)
427 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* share name */
428 while (IS_SEPARATOR(*ptr
)) ptr
++;
429 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* dir name */
430 while (IS_SEPARATOR(*ptr
)) ptr
++;
435 /******************************************************************
436 * get_full_path_helper
438 * Helper for RtlGetFullPathName_U
439 * Note: name and buffer are allowed to point to the same memory spot
441 static ULONG
get_full_path_helper(LPCWSTR name
, LPWSTR buffer
, ULONG size
)
443 ULONG reqsize
= 0, mark
= 0, dep
= 0, deplen
;
444 LPWSTR ins_str
= NULL
;
446 const UNICODE_STRING
* cd
;
449 /* return error if name only consists of spaces */
450 for (ptr
= name
; *ptr
; ptr
++) if (*ptr
!= ' ') break;
455 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
456 cd
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
458 cd
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
460 switch (RtlDetermineDosPathNameType_U(name
))
462 case UNC_PATH
: /* \\foo */
463 ptr
= skip_unc_prefix( name
);
467 case DEVICE_PATH
: /* \\.\foo */
471 case ABSOLUTE_DRIVE_PATH
: /* c:\foo */
472 reqsize
= sizeof(WCHAR
);
479 case RELATIVE_DRIVE_PATH
: /* c:foo */
481 if (wcsnicmp( name
, cd
->Buffer
, 2 ))
483 UNICODE_STRING var
, val
;
489 var
.Length
= 3 * sizeof(WCHAR
);
490 var
.MaximumLength
= 4 * sizeof(WCHAR
);
493 val
.MaximumLength
= size
;
494 val
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, size
);
496 switch (RtlQueryEnvironmentVariable_U(NULL
, &var
, &val
))
499 /* FIXME: Win2k seems to check that the environment variable actually points
500 * to an existing directory. If not, root of the drive is used
501 * (this seems also to be the only spot in RtlGetFullPathName that the
502 * existence of a part of a path is checked)
505 case STATUS_BUFFER_TOO_SMALL
:
506 reqsize
= val
.Length
+ sizeof(WCHAR
); /* append trailing '\\' */
507 val
.Buffer
[val
.Length
/ sizeof(WCHAR
)] = '\\';
508 ins_str
= val
.Buffer
;
510 case STATUS_VARIABLE_NOT_FOUND
:
511 reqsize
= 3 * sizeof(WCHAR
);
516 RtlFreeHeap(GetProcessHeap(), 0, val
.Buffer
);
519 ERR("Unsupported status code\n");
520 RtlFreeHeap(GetProcessHeap(), 0, val
.Buffer
);
528 case RELATIVE_PATH
: /* foo */
529 reqsize
= cd
->Length
;
530 ins_str
= cd
->Buffer
;
531 if (cd
->Buffer
[1] != ':')
533 ptr
= skip_unc_prefix( cd
->Buffer
);
534 mark
= ptr
- cd
->Buffer
;
539 case ABSOLUTE_PATH
: /* \xxx */
540 if (name
[0] == '/') /* may be a Unix path */
548 nt_str
= RtlAllocateHeap( GetProcessHeap(), 0, (wcslen(name
) + 9) * sizeof(WCHAR
) );
549 wcscpy( nt_str
, L
"\\??\\unix" );
550 wcscat( nt_str
, name
);
551 RtlInitUnicodeString( &str
, nt_str
);
552 buflen
= 3 * wcslen(name
) + 1;
553 unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, buflen
);
554 status
= wine_nt_to_unix_file_name( &str
, unix_name
, &buflen
, FILE_OPEN_IF
);
555 if (!status
|| status
== STATUS_NO_SUCH_FILE
)
557 buflen
= wcslen(name
) + 9;
558 status
= wine_unix_to_nt_file_name( unix_name
, nt_str
, &buflen
);
560 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
561 if (!status
&& buflen
> 6 && nt_str
[5] == ':')
563 reqsize
= (buflen
- 4) * sizeof(WCHAR
);
566 memcpy( buffer
, nt_str
+ 4, reqsize
);
567 collapse_path( buffer
, 3 );
568 reqsize
-= sizeof(WCHAR
);
570 RtlFreeHeap( GetProcessHeap(), 0, nt_str
);
573 RtlFreeHeap( GetProcessHeap(), 0, nt_str
);
575 if (cd
->Buffer
[1] == ':')
577 reqsize
= 2 * sizeof(WCHAR
);
578 tmp
[0] = cd
->Buffer
[0];
585 ptr
= skip_unc_prefix( cd
->Buffer
);
586 reqsize
= (ptr
- cd
->Buffer
) * sizeof(WCHAR
);
587 mark
= reqsize
/ sizeof(WCHAR
);
588 ins_str
= cd
->Buffer
;
592 case UNC_DOT_PATH
: /* \\. */
593 reqsize
= 4 * sizeof(WCHAR
);
608 deplen
= wcslen(name
+ dep
) * sizeof(WCHAR
);
609 if (reqsize
+ deplen
+ sizeof(WCHAR
) > size
)
611 /* not enough space, return need size (including terminating '\0') */
612 reqsize
+= deplen
+ sizeof(WCHAR
);
616 memmove(buffer
+ reqsize
/ sizeof(WCHAR
), name
+ dep
, deplen
+ sizeof(WCHAR
));
617 if (reqsize
) memcpy(buffer
, ins_str
, reqsize
);
619 if (ins_str
!= tmp
&& ins_str
!= cd
->Buffer
)
620 RtlFreeHeap(GetProcessHeap(), 0, ins_str
);
622 collapse_path( buffer
, mark
);
623 reqsize
= wcslen(buffer
) * sizeof(WCHAR
);
630 /******************************************************************
631 * RtlGetFullPathName_U (NTDLL.@)
633 * Returns the number of bytes written to buffer (not including the
634 * terminating NULL) if the function succeeds, or the required number of bytes
635 * (including the terminating NULL) if the buffer is too small.
637 * file_part will point to the filename part inside buffer (except if we use
638 * DOS device name, in which case file_in_buf is NULL)
641 DWORD WINAPI
RtlGetFullPathName_U(const WCHAR
* name
, ULONG size
, WCHAR
* buffer
,
648 TRACE("(%s %u %p %p)\n", debugstr_w(name
), size
, buffer
, file_part
);
650 if (!name
|| !*name
) return 0;
652 if (file_part
) *file_part
= NULL
;
654 /* check for DOS device name */
655 dosdev
= RtlIsDosDeviceName_U(name
);
658 DWORD offset
= HIWORD(dosdev
) / sizeof(WCHAR
); /* get it in WCHARs, not bytes */
659 DWORD sz
= LOWORD(dosdev
); /* in bytes */
661 if (8 + sz
+ 2 > size
) return sz
+ 10;
662 wcscpy(buffer
, L
"\\\\.\\");
663 memmove(buffer
+ 4, name
+ offset
, sz
);
664 buffer
[4 + sz
/ sizeof(WCHAR
)] = '\0';
665 /* file_part isn't set in this case */
669 reqsize
= get_full_path_helper(name
, buffer
, size
);
670 if (!reqsize
) return 0;
673 LPWSTR tmp
= RtlAllocateHeap(GetProcessHeap(), 0, reqsize
);
674 reqsize
= get_full_path_helper(name
, tmp
, reqsize
);
675 if (reqsize
+ sizeof(WCHAR
) > size
) /* it may have worked the second time */
677 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
678 return reqsize
+ sizeof(WCHAR
);
680 memcpy( buffer
, tmp
, reqsize
+ sizeof(WCHAR
) );
681 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
685 if (file_part
&& (ptr
= wcsrchr(buffer
, '\\')) != NULL
&& ptr
>= buffer
+ 2 && *++ptr
)
690 /*************************************************************************
691 * RtlGetLongestNtPathLength [NTDLL.@]
693 * Get the longest allowed path length
699 * The longest allowed path length (277 characters under Win2k).
701 DWORD WINAPI
RtlGetLongestNtPathLength(void)
703 return MAX_NT_PATH_LENGTH
;
706 /******************************************************************
707 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
709 * Returns TRUE iff unicode is a valid DOS (8+3) name.
710 * If the name is valid, oem gets filled with the corresponding OEM string
711 * spaces is set to TRUE if unicode contains spaces
713 BOOLEAN WINAPI
RtlIsNameLegalDOS8Dot3( const UNICODE_STRING
*unicode
,
714 OEM_STRING
*oem
, BOOLEAN
*spaces
)
716 static const char illegal
[] = "*?<>|\"+=,;[]:/\\\345";
721 BOOLEAN got_space
= FALSE
;
725 oem_str
.Length
= sizeof(buffer
);
726 oem_str
.MaximumLength
= sizeof(buffer
);
727 oem_str
.Buffer
= buffer
;
730 if (RtlUpcaseUnicodeStringToCountedOemString( oem
, unicode
, FALSE
) != STATUS_SUCCESS
)
733 if (oem
->Length
> 12) return FALSE
;
735 /* a starting . is invalid, except for . and .. */
736 if (oem
->Length
> 0 && oem
->Buffer
[0] == '.')
738 if (oem
->Length
!= 1 && (oem
->Length
!= 2 || oem
->Buffer
[1] != '.')) return FALSE
;
739 if (spaces
) *spaces
= FALSE
;
743 for (i
= 0; i
< oem
->Length
; i
++)
745 switch (oem
->Buffer
[i
])
748 /* leading/trailing spaces not allowed */
749 if (!i
|| i
== oem
->Length
-1 || oem
->Buffer
[i
+1] == '.') return FALSE
;
753 if (dot
!= -1) return FALSE
;
757 if (strchr(illegal
, oem
->Buffer
[i
])) return FALSE
;
761 /* check file part is shorter than 8, extension shorter than 3
762 * dot cannot be last in string
766 if (oem
->Length
> 8) return FALSE
;
770 if (dot
> 8 || (oem
->Length
- dot
> 4) || dot
== oem
->Length
- 1) return FALSE
;
772 if (spaces
) *spaces
= got_space
;
776 /******************************************************************
777 * RtlGetCurrentDirectory_U (NTDLL.@)
780 ULONG WINAPI
RtlGetCurrentDirectory_U(ULONG buflen
, LPWSTR buf
)
785 TRACE("(%u %p)\n", buflen
, buf
);
789 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
790 us
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
792 us
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
794 len
= us
->Length
/ sizeof(WCHAR
);
795 if (us
->Buffer
[len
- 1] == '\\' && us
->Buffer
[len
- 2] != ':')
798 if (buflen
/ sizeof(WCHAR
) > len
)
800 memcpy(buf
, us
->Buffer
, len
* sizeof(WCHAR
));
810 return len
* sizeof(WCHAR
);
813 /******************************************************************
814 * RtlSetCurrentDirectory_U (NTDLL.@)
817 NTSTATUS WINAPI
RtlSetCurrentDirectory_U(const UNICODE_STRING
* dir
)
819 FILE_FS_DEVICE_INFORMATION device_info
;
820 OBJECT_ATTRIBUTES attr
;
821 UNICODE_STRING newdir
;
829 newdir
.Buffer
= NULL
;
833 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
834 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
836 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
838 if (!RtlDosPathNameToNtPathName_U( dir
->Buffer
, &newdir
, NULL
, NULL
))
840 nts
= STATUS_OBJECT_NAME_INVALID
;
844 attr
.Length
= sizeof(attr
);
845 attr
.RootDirectory
= 0;
846 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
847 attr
.ObjectName
= &newdir
;
848 attr
.SecurityDescriptor
= NULL
;
849 attr
.SecurityQualityOfService
= NULL
;
851 nts
= NtOpenFile( &handle
, FILE_TRAVERSE
| SYNCHRONIZE
, &attr
, &io
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
852 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
853 if (nts
!= STATUS_SUCCESS
) goto out
;
855 /* don't keep the directory handle open on removable media */
856 if (!NtQueryVolumeInformationFile( handle
, &io
, &device_info
,
857 sizeof(device_info
), FileFsDeviceInformation
) &&
858 (device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
))
864 if (curdir
->Handle
) NtClose( curdir
->Handle
);
865 curdir
->Handle
= handle
;
867 /* append trailing \ if missing */
868 size
= newdir
.Length
/ sizeof(WCHAR
);
870 ptr
+= 4; /* skip \??\ prefix */
872 if (size
&& ptr
[size
- 1] != '\\') ptr
[size
++] = '\\';
874 /* convert \??\UNC\ path to \\ prefix */
875 if (size
>= 4 && !wcsnicmp(ptr
, L
"UNC\\", 4))
882 memcpy( curdir
->DosPath
.Buffer
, ptr
, size
* sizeof(WCHAR
));
883 curdir
->DosPath
.Buffer
[size
] = 0;
884 curdir
->DosPath
.Length
= size
* sizeof(WCHAR
);
886 TRACE( "curdir now %s %p\n", debugstr_w(curdir
->DosPath
.Buffer
), curdir
->Handle
);
889 RtlFreeUnicodeString( &newdir
);