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 static const WCHAR DeviceRootW
[] = {'\\','\\','.','\\',0};
35 static const WCHAR NTDosPrefixW
[] = {'\\','?','?','\\',0};
36 static const WCHAR UncPfxW
[] = {'U','N','C','\\',0};
38 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
40 /***********************************************************************
41 * RtlDetermineDosPathNameType_U (NTDLL.@)
43 DOS_PATHNAME_TYPE WINAPI
RtlDetermineDosPathNameType_U( PCWSTR path
)
45 if (IS_SEPARATOR(path
[0]))
47 if (!IS_SEPARATOR(path
[1])) return ABSOLUTE_PATH
; /* "/foo" */
48 if (path
[2] != '.' && path
[2] != '?') return UNC_PATH
; /* "//foo" */
49 if (IS_SEPARATOR(path
[3])) return DEVICE_PATH
; /* "//./foo" or "//?/foo" */
50 if (path
[3]) return UNC_PATH
; /* "//.foo" or "//?foo" */
51 return UNC_DOT_PATH
; /* "//." or "//?" */
55 if (!path
[0] || path
[1] != ':') return RELATIVE_PATH
; /* "foo" */
56 if (IS_SEPARATOR(path
[2])) return ABSOLUTE_DRIVE_PATH
; /* "c:/foo" */
57 return RELATIVE_DRIVE_PATH
; /* "c:foo" */
61 /***********************************************************************
62 * RtlIsDosDeviceName_U (NTDLL.@)
64 * Check if the given DOS path contains a DOS device name.
66 * Returns the length of the device name in the low word and its
67 * position in the high word (both in bytes, not WCHARs), or 0 if no
68 * device name is found.
70 ULONG WINAPI
RtlIsDosDeviceName_U( PCWSTR dos_name
)
72 static const WCHAR consoleW
[] = {'\\','\\','.','\\','C','O','N',0};
73 static const WCHAR auxW
[] = {'A','U','X'};
74 static const WCHAR comW
[] = {'C','O','M'};
75 static const WCHAR conW
[] = {'C','O','N'};
76 static const WCHAR lptW
[] = {'L','P','T'};
77 static const WCHAR nulW
[] = {'N','U','L'};
78 static const WCHAR prnW
[] = {'P','R','N'};
79 static const WCHAR coninW
[] = {'C','O','N','I','N','$'};
80 static const WCHAR conoutW
[] = {'C','O','N','O','U','T','$'};
82 const WCHAR
*start
, *end
, *p
;
84 switch(RtlDetermineDosPathNameType_U( dos_name
))
90 if (!wcsicmp( dos_name
, consoleW
))
91 return MAKELONG( sizeof(conW
), 4 * sizeof(WCHAR
) ); /* 4 is length of \\.\ prefix */
93 case ABSOLUTE_DRIVE_PATH
:
94 case RELATIVE_DRIVE_PATH
:
95 start
= dos_name
+ 2; /* skip drive letter */
102 /* find start of file name */
103 for (p
= start
; *p
; p
++) if (IS_SEPARATOR(*p
)) start
= p
+ 1;
105 /* truncate at extension and ':' */
106 for (end
= start
; *end
; end
++) if (*end
== '.' || *end
== ':') break;
109 /* remove trailing spaces */
110 while (end
>= start
&& *end
== ' ') end
--;
112 /* now we have a potential device name between start and end, check it */
113 switch(end
- start
+ 1)
116 if (wcsnicmp( start
, auxW
, 3 ) &&
117 wcsnicmp( start
, conW
, 3 ) &&
118 wcsnicmp( start
, nulW
, 3 ) &&
119 wcsnicmp( start
, prnW
, 3 )) break;
120 return MAKELONG( 3 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
122 if (wcsnicmp( start
, comW
, 3 ) && wcsnicmp( start
, lptW
, 3 )) break;
123 if (*end
<= '0' || *end
> '9') break;
124 return MAKELONG( 4 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
126 if (wcsnicmp( start
, coninW
, ARRAY_SIZE(coninW
) )) break;
127 return MAKELONG( sizeof(coninW
), (start
- dos_name
) * sizeof(WCHAR
) );
129 if (wcsnicmp( start
, conoutW
, ARRAY_SIZE(conoutW
) )) break;
130 return MAKELONG( sizeof(conoutW
), (start
- dos_name
) * sizeof(WCHAR
) );
131 default: /* can't match anything */
137 /**************************************************************************
138 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
140 * dos_path: a DOS path name (fully qualified or not)
141 * ntpath: pointer to a UNICODE_STRING to hold the converted
143 * file_part:will point (in ntpath) to the file part in the path
144 * cd: directory reference (optional)
147 * + fill the cd structure
149 NTSTATUS WINAPI
RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR
*dos_path
, UNICODE_STRING
*ntpath
,
150 WCHAR
**file_part
, CURDIR
*cd
)
152 static const WCHAR global_prefix
[] = {'\\','\\','?','\\'};
153 static const WCHAR global_prefix2
[] = {'\\','?','?','\\'};
155 WCHAR local
[MAX_PATH
];
158 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path
), ntpath
, file_part
, cd
);
162 FIXME("Unsupported parameter\n");
163 memset(cd
, 0, sizeof(*cd
));
166 if (!dos_path
|| !*dos_path
)
167 return STATUS_OBJECT_NAME_INVALID
;
169 if (!memcmp(dos_path
, global_prefix
, sizeof(global_prefix
)) ||
170 (!memcmp(dos_path
, global_prefix2
, sizeof(global_prefix2
)) && dos_path
[4]))
172 ntpath
->Length
= wcslen(dos_path
) * sizeof(WCHAR
);
173 ntpath
->MaximumLength
= ntpath
->Length
+ sizeof(WCHAR
);
174 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
175 if (!ntpath
->Buffer
) return STATUS_NO_MEMORY
;
176 memcpy( ntpath
->Buffer
, dos_path
, ntpath
->MaximumLength
);
177 ntpath
->Buffer
[1] = '?'; /* change \\?\ to \??\ */
180 if ((ptr
= wcsrchr( ntpath
->Buffer
, '\\' )) && ptr
[1]) *file_part
= ptr
+ 1;
181 else *file_part
= NULL
;
183 return STATUS_SUCCESS
;
187 sz
= RtlGetFullPathName_U(dos_path
, sizeof(local
), ptr
, file_part
);
188 if (sz
== 0) return STATUS_OBJECT_NAME_INVALID
;
190 if (sz
> sizeof(local
))
192 if (!(ptr
= RtlAllocateHeap(GetProcessHeap(), 0, sz
))) return STATUS_NO_MEMORY
;
193 sz
= RtlGetFullPathName_U(dos_path
, sz
, ptr
, file_part
);
195 sz
+= (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR
);
198 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
199 return STATUS_OBJECT_NAME_INVALID
;
202 ntpath
->MaximumLength
= sz
;
203 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
206 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
207 return STATUS_NO_MEMORY
;
210 wcscpy(ntpath
->Buffer
, NTDosPrefixW
);
211 switch (RtlDetermineDosPathNameType_U(ptr
))
213 case UNC_PATH
: /* \\foo */
215 wcscat(ntpath
->Buffer
, UncPfxW
);
217 case DEVICE_PATH
: /* \\.\foo */
225 wcscat(ntpath
->Buffer
, ptr
+ offset
);
226 ntpath
->Length
= wcslen(ntpath
->Buffer
) * sizeof(WCHAR
);
228 if (file_part
&& *file_part
)
229 *file_part
= ntpath
->Buffer
+ ntpath
->Length
/ sizeof(WCHAR
) - wcslen(*file_part
);
231 /* FIXME: cd filling */
233 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
234 return STATUS_SUCCESS
;
237 /**************************************************************************
238 * RtlDosPathNameToNtPathName_U [NTDLL.@]
240 * See RtlDosPathNameToNtPathName_U_WithStatus
242 BOOLEAN WINAPI
RtlDosPathNameToNtPathName_U(PCWSTR dos_path
,
243 PUNICODE_STRING ntpath
,
247 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path
, ntpath
, file_part
, cd
) == STATUS_SUCCESS
;
250 /**************************************************************************
251 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
253 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
255 NTSTATUS WINAPI
RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR
*dos_path
,
256 UNICODE_STRING
*ntpath
, WCHAR
**file_part
, RTL_RELATIVE_NAME
*relative
)
258 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path
), ntpath
, file_part
, relative
);
262 FIXME("Unsupported parameter\n");
263 memset(relative
, 0, sizeof(*relative
));
266 /* FIXME: fill parameter relative */
268 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path
, ntpath
, file_part
, NULL
);
271 /**************************************************************************
272 * RtlReleaseRelativeName [NTDLL.@]
274 void WINAPI
RtlReleaseRelativeName(RTL_RELATIVE_NAME
*relative
)
278 /******************************************************************
281 * Searches a file of name 'name' into a ';' separated list of paths
283 * Doesn't seem to search elsewhere than the paths list
284 * Stores the result in buffer (file_part will point to the position
285 * of the file name in the buffer)
287 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
289 ULONG WINAPI
RtlDosSearchPath_U(LPCWSTR paths
, LPCWSTR search
, LPCWSTR ext
,
290 ULONG buffer_size
, LPWSTR buffer
,
293 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U(search
);
296 if (type
== RELATIVE_PATH
)
298 ULONG allocated
= 0, needed
, filelen
;
301 filelen
= 1 /* for \ */ + wcslen(search
) + 1 /* \0 */;
303 /* Windows only checks for '.' without worrying about path components */
304 if (wcschr( search
, '.' )) ext
= NULL
;
305 if (ext
!= NULL
) filelen
+= wcslen(ext
);
311 for (needed
= 0, ptr
= paths
; *ptr
!= 0 && *ptr
++ != ';'; needed
++);
312 if (needed
+ filelen
> allocated
)
314 if (!name
) name
= RtlAllocateHeap(GetProcessHeap(), 0,
315 (needed
+ filelen
) * sizeof(WCHAR
));
318 WCHAR
*newname
= RtlReAllocateHeap(GetProcessHeap(), 0, name
,
319 (needed
+ filelen
) * sizeof(WCHAR
));
320 if (!newname
) RtlFreeHeap(GetProcessHeap(), 0, name
);
324 allocated
= needed
+ filelen
;
326 memmove(name
, paths
, needed
* sizeof(WCHAR
));
327 /* append '\\' if none is present */
328 if (needed
> 0 && name
[needed
- 1] != '\\') name
[needed
++] = '\\';
329 wcscpy(&name
[needed
], search
);
330 if (ext
) wcscat(&name
[needed
], ext
);
331 if (RtlDoesFileExists_U(name
))
333 len
= RtlGetFullPathName_U(name
, buffer_size
, buffer
, file_part
);
338 RtlFreeHeap(GetProcessHeap(), 0, name
);
340 else if (RtlDoesFileExists_U(search
))
342 len
= RtlGetFullPathName_U(search
, buffer_size
, buffer
, file_part
);
349 /******************************************************************
352 * Helper for RtlGetFullPathName_U.
353 * Get rid of . and .. components in the path.
355 static inline void collapse_path( WCHAR
*path
, UINT mark
)
359 /* convert every / into a \ */
360 for (p
= path
; *p
; p
++) if (*p
== '/') *p
= '\\';
362 /* collapse duplicate backslashes */
363 next
= path
+ max( 1, mark
);
364 for (p
= next
; *p
; p
++) if (*p
!= '\\' || next
[-1] != '\\') *next
++ = *p
;
374 case '\\': /* .\ component */
376 memmove( p
, next
, (wcslen(next
) + 1) * sizeof(WCHAR
) );
378 case 0: /* final . */
379 if (p
> path
+ mark
) p
--;
383 if (p
[2] == '\\') /* ..\ component */
389 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
391 memmove( p
, next
, (wcslen(next
) + 1) * sizeof(WCHAR
) );
394 else if (!p
[2]) /* final .. */
399 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
400 if (p
> path
+ mark
) p
--;
408 /* skip to the next component */
409 while (*p
&& *p
!= '\\') p
++;
412 /* remove last dot in previous dir name */
413 if (p
> path
+ mark
&& p
[-1] == '.') memmove( p
-1, p
, (wcslen(p
) + 1) * sizeof(WCHAR
) );
418 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
419 while (p
> path
+ mark
&& (p
[-1] == ' ' || p
[-1] == '.')) p
--;
424 /******************************************************************
427 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
429 static const WCHAR
*skip_unc_prefix( const WCHAR
*ptr
)
432 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* share name */
433 while (IS_SEPARATOR(*ptr
)) ptr
++;
434 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* dir name */
435 while (IS_SEPARATOR(*ptr
)) ptr
++;
440 /******************************************************************
441 * get_full_path_helper
443 * Helper for RtlGetFullPathName_U
444 * Note: name and buffer are allowed to point to the same memory spot
446 static ULONG
get_full_path_helper(LPCWSTR name
, LPWSTR buffer
, ULONG size
)
448 ULONG reqsize
= 0, mark
= 0, dep
= 0, deplen
;
449 LPWSTR ins_str
= NULL
;
451 const UNICODE_STRING
* cd
;
454 /* return error if name only consists of spaces */
455 for (ptr
= name
; *ptr
; ptr
++) if (*ptr
!= ' ') break;
460 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
461 cd
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
463 cd
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
465 switch (RtlDetermineDosPathNameType_U(name
))
467 case UNC_PATH
: /* \\foo */
468 ptr
= skip_unc_prefix( name
);
472 case DEVICE_PATH
: /* \\.\foo */
476 case ABSOLUTE_DRIVE_PATH
: /* c:\foo */
477 reqsize
= sizeof(WCHAR
);
484 case RELATIVE_DRIVE_PATH
: /* c:foo */
486 if (wcsnicmp( name
, cd
->Buffer
, 2 ))
488 UNICODE_STRING var
, val
;
494 var
.Length
= 3 * sizeof(WCHAR
);
495 var
.MaximumLength
= 4 * sizeof(WCHAR
);
498 val
.MaximumLength
= size
;
499 val
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, size
);
501 switch (RtlQueryEnvironmentVariable_U(NULL
, &var
, &val
))
504 /* FIXME: Win2k seems to check that the environment variable actually points
505 * to an existing directory. If not, root of the drive is used
506 * (this seems also to be the only spot in RtlGetFullPathName that the
507 * existence of a part of a path is checked)
510 case STATUS_BUFFER_TOO_SMALL
:
511 reqsize
= val
.Length
+ sizeof(WCHAR
); /* append trailing '\\' */
512 val
.Buffer
[val
.Length
/ sizeof(WCHAR
)] = '\\';
513 ins_str
= val
.Buffer
;
515 case STATUS_VARIABLE_NOT_FOUND
:
516 reqsize
= 3 * sizeof(WCHAR
);
521 RtlFreeHeap(GetProcessHeap(), 0, val
.Buffer
);
524 ERR("Unsupported status code\n");
525 RtlFreeHeap(GetProcessHeap(), 0, val
.Buffer
);
533 case RELATIVE_PATH
: /* foo */
534 reqsize
= cd
->Length
;
535 ins_str
= cd
->Buffer
;
536 if (cd
->Buffer
[1] != ':')
538 ptr
= skip_unc_prefix( cd
->Buffer
);
539 mark
= ptr
- cd
->Buffer
;
544 case ABSOLUTE_PATH
: /* \xxx */
545 if (name
[0] == '/') /* may be a Unix path */
552 unix_name
= RtlAllocateHeap( GetProcessHeap(), 0, 3 * wcslen(name
) + 1 );
553 ntdll_wcstoumbs( name
, wcslen(name
) + 1, unix_name
, 3 * wcslen(name
) + 1, FALSE
);
554 buflen
= strlen(unix_name
) + 10;
557 if (!(nt_str
= RtlAllocateHeap( GetProcessHeap(), 0, buflen
* sizeof(WCHAR
) ))) break;
558 status
= wine_unix_to_nt_file_name( unix_name
, nt_str
, &buflen
);
559 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
560 RtlFreeHeap( GetProcessHeap(), 0, nt_str
);
562 RtlFreeHeap( GetProcessHeap(), 0, unix_name
);
563 if (!status
&& buflen
> 6 && nt_str
[5] == ':')
565 reqsize
= (buflen
- 4) * sizeof(WCHAR
);
568 memcpy( buffer
, nt_str
+ 4, reqsize
);
569 collapse_path( buffer
, 3 );
570 reqsize
-= sizeof(WCHAR
);
572 RtlFreeHeap( GetProcessHeap(), 0, nt_str
);
575 RtlFreeHeap( GetProcessHeap(), 0, nt_str
);
577 if (cd
->Buffer
[1] == ':')
579 reqsize
= 2 * sizeof(WCHAR
);
580 tmp
[0] = cd
->Buffer
[0];
587 ptr
= skip_unc_prefix( cd
->Buffer
);
588 reqsize
= (ptr
- cd
->Buffer
) * sizeof(WCHAR
);
589 mark
= reqsize
/ sizeof(WCHAR
);
590 ins_str
= cd
->Buffer
;
594 case UNC_DOT_PATH
: /* \\. */
595 reqsize
= 4 * sizeof(WCHAR
);
610 deplen
= wcslen(name
+ dep
) * sizeof(WCHAR
);
611 if (reqsize
+ deplen
+ sizeof(WCHAR
) > size
)
613 /* not enough space, return need size (including terminating '\0') */
614 reqsize
+= deplen
+ sizeof(WCHAR
);
618 memmove(buffer
+ reqsize
/ sizeof(WCHAR
), name
+ dep
, deplen
+ sizeof(WCHAR
));
619 if (reqsize
) memcpy(buffer
, ins_str
, reqsize
);
621 if (ins_str
!= tmp
&& ins_str
!= cd
->Buffer
)
622 RtlFreeHeap(GetProcessHeap(), 0, ins_str
);
624 collapse_path( buffer
, mark
);
625 reqsize
= wcslen(buffer
) * sizeof(WCHAR
);
632 /******************************************************************
633 * RtlGetFullPathName_U (NTDLL.@)
635 * Returns the number of bytes written to buffer (not including the
636 * terminating NULL) if the function succeeds, or the required number of bytes
637 * (including the terminating NULL) if the buffer is too small.
639 * file_part will point to the filename part inside buffer (except if we use
640 * DOS device name, in which case file_in_buf is NULL)
643 DWORD WINAPI
RtlGetFullPathName_U(const WCHAR
* name
, ULONG size
, WCHAR
* buffer
,
650 TRACE("(%s %u %p %p)\n", debugstr_w(name
), size
, buffer
, file_part
);
652 if (!name
|| !*name
) return 0;
654 if (file_part
) *file_part
= NULL
;
656 /* check for DOS device name */
657 dosdev
= RtlIsDosDeviceName_U(name
);
660 DWORD offset
= HIWORD(dosdev
) / sizeof(WCHAR
); /* get it in WCHARs, not bytes */
661 DWORD sz
= LOWORD(dosdev
); /* in bytes */
663 if (8 + sz
+ 2 > size
) return sz
+ 10;
664 wcscpy(buffer
, DeviceRootW
);
665 memmove(buffer
+ 4, name
+ offset
, sz
);
666 buffer
[4 + sz
/ sizeof(WCHAR
)] = '\0';
667 /* file_part isn't set in this case */
671 reqsize
= get_full_path_helper(name
, buffer
, size
);
672 if (!reqsize
) return 0;
675 LPWSTR tmp
= RtlAllocateHeap(GetProcessHeap(), 0, reqsize
);
676 reqsize
= get_full_path_helper(name
, tmp
, reqsize
);
677 if (reqsize
+ sizeof(WCHAR
) > size
) /* it may have worked the second time */
679 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
680 return reqsize
+ sizeof(WCHAR
);
682 memcpy( buffer
, tmp
, reqsize
+ sizeof(WCHAR
) );
683 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
687 if (file_part
&& (ptr
= wcsrchr(buffer
, '\\')) != NULL
&& ptr
>= buffer
+ 2 && *++ptr
)
692 /*************************************************************************
693 * RtlGetLongestNtPathLength [NTDLL.@]
695 * Get the longest allowed path length
701 * The longest allowed path length (277 characters under Win2k).
703 DWORD WINAPI
RtlGetLongestNtPathLength(void)
705 return MAX_NT_PATH_LENGTH
;
708 /******************************************************************
709 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
711 * Returns TRUE iff unicode is a valid DOS (8+3) name.
712 * If the name is valid, oem gets filled with the corresponding OEM string
713 * spaces is set to TRUE if unicode contains spaces
715 BOOLEAN WINAPI
RtlIsNameLegalDOS8Dot3( const UNICODE_STRING
*unicode
,
716 OEM_STRING
*oem
, BOOLEAN
*spaces
)
718 static const char illegal
[] = "*?<>|\"+=,;[]:/\\\345";
723 BOOLEAN got_space
= FALSE
;
727 oem_str
.Length
= sizeof(buffer
);
728 oem_str
.MaximumLength
= sizeof(buffer
);
729 oem_str
.Buffer
= buffer
;
732 if (RtlUpcaseUnicodeStringToCountedOemString( oem
, unicode
, FALSE
) != STATUS_SUCCESS
)
735 if (oem
->Length
> 12) return FALSE
;
737 /* a starting . is invalid, except for . and .. */
738 if (oem
->Length
> 0 && oem
->Buffer
[0] == '.')
740 if (oem
->Length
!= 1 && (oem
->Length
!= 2 || oem
->Buffer
[1] != '.')) return FALSE
;
741 if (spaces
) *spaces
= FALSE
;
745 for (i
= 0; i
< oem
->Length
; i
++)
747 switch (oem
->Buffer
[i
])
750 /* leading/trailing spaces not allowed */
751 if (!i
|| i
== oem
->Length
-1 || oem
->Buffer
[i
+1] == '.') return FALSE
;
755 if (dot
!= -1) return FALSE
;
759 if (strchr(illegal
, oem
->Buffer
[i
])) return FALSE
;
763 /* check file part is shorter than 8, extension shorter than 3
764 * dot cannot be last in string
768 if (oem
->Length
> 8) return FALSE
;
772 if (dot
> 8 || (oem
->Length
- dot
> 4) || dot
== oem
->Length
- 1) return FALSE
;
774 if (spaces
) *spaces
= got_space
;
778 /******************************************************************
779 * RtlGetCurrentDirectory_U (NTDLL.@)
782 ULONG WINAPI
RtlGetCurrentDirectory_U(ULONG buflen
, LPWSTR buf
)
787 TRACE("(%u %p)\n", buflen
, buf
);
791 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
792 us
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
794 us
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
796 len
= us
->Length
/ sizeof(WCHAR
);
797 if (us
->Buffer
[len
- 1] == '\\' && us
->Buffer
[len
- 2] != ':')
800 if (buflen
/ sizeof(WCHAR
) > len
)
802 memcpy(buf
, us
->Buffer
, len
* sizeof(WCHAR
));
812 return len
* sizeof(WCHAR
);
815 /******************************************************************
816 * RtlSetCurrentDirectory_U (NTDLL.@)
819 NTSTATUS WINAPI
RtlSetCurrentDirectory_U(const UNICODE_STRING
* dir
)
821 FILE_FS_DEVICE_INFORMATION device_info
;
822 OBJECT_ATTRIBUTES attr
;
823 UNICODE_STRING newdir
;
831 newdir
.Buffer
= NULL
;
835 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
836 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
838 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
840 if (!RtlDosPathNameToNtPathName_U( dir
->Buffer
, &newdir
, NULL
, NULL
))
842 nts
= STATUS_OBJECT_NAME_INVALID
;
846 attr
.Length
= sizeof(attr
);
847 attr
.RootDirectory
= 0;
848 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
849 attr
.ObjectName
= &newdir
;
850 attr
.SecurityDescriptor
= NULL
;
851 attr
.SecurityQualityOfService
= NULL
;
853 nts
= NtOpenFile( &handle
, FILE_TRAVERSE
| SYNCHRONIZE
, &attr
, &io
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
854 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
855 if (nts
!= STATUS_SUCCESS
) goto out
;
857 /* don't keep the directory handle open on removable media */
858 if (!NtQueryVolumeInformationFile( handle
, &io
, &device_info
,
859 sizeof(device_info
), FileFsDeviceInformation
) &&
860 (device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
))
866 if (curdir
->Handle
) NtClose( curdir
->Handle
);
867 curdir
->Handle
= handle
;
869 /* append trailing \ if missing */
870 size
= newdir
.Length
/ sizeof(WCHAR
);
872 ptr
+= 4; /* skip \??\ prefix */
874 if (size
&& ptr
[size
- 1] != '\\') ptr
[size
++] = '\\';
876 /* convert \??\UNC\ path to \\ prefix */
877 if (size
>= 4 && !wcsnicmp(ptr
, UncPfxW
, 4))
884 memcpy( curdir
->DosPath
.Buffer
, ptr
, size
* sizeof(WCHAR
));
885 curdir
->DosPath
.Buffer
[size
] = 0;
886 curdir
->DosPath
.Length
= size
* sizeof(WCHAR
);
888 TRACE( "curdir now %s %p\n", debugstr_w(curdir
->DosPath
.Buffer
), curdir
->Handle
);
891 RtlFreeUnicodeString( &newdir
);