4 * Copyright 2002, 2003 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
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(file
);
36 static const WCHAR DeviceRootW
[] = {'\\','\\','.','\\',0};
37 static const WCHAR NTDosPrefixW
[] = {'\\','?','?','\\',0};
38 static const WCHAR UncPfxW
[] = {'U','N','C','\\',0};
40 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
42 /***********************************************************************
43 * RtlDetermineDosPathNameType_U (NTDLL.@)
45 DOS_PATHNAME_TYPE WINAPI
RtlDetermineDosPathNameType_U( PCWSTR path
)
47 if (IS_SEPARATOR(path
[0]))
49 if (!IS_SEPARATOR(path
[1])) return ABSOLUTE_PATH
; /* "/foo" */
50 if (path
[2] != '.') return UNC_PATH
; /* "//foo" */
51 if (IS_SEPARATOR(path
[3])) return DEVICE_PATH
; /* "//./foo" */
52 if (path
[3]) return UNC_PATH
; /* "//.foo" */
53 return UNC_DOT_PATH
; /* "//." */
57 if (!path
[0] || path
[1] != ':') return RELATIVE_PATH
; /* "foo" */
58 if (IS_SEPARATOR(path
[2])) return ABSOLUTE_DRIVE_PATH
; /* "c:/foo" */
59 return RELATIVE_DRIVE_PATH
; /* "c:foo" */
63 /******************************************************************
66 * FIXME: should not use CreateFileW
68 BOOLEAN WINAPI
RtlDoesFileExists_U(LPCWSTR file_name
)
70 HANDLE handle
= CreateFileW( file_name
, 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
71 NULL
, OPEN_EXISTING
, 0, 0 );
72 if (handle
== INVALID_HANDLE_VALUE
) return FALSE
;
77 /***********************************************************************
78 * RtlIsDosDeviceName_U (NTDLL.@)
80 * Check if the given DOS path contains a DOS device name.
82 * Returns the length of the device name in the low word and its
83 * position in the high word (both in bytes, not WCHARs), or 0 if no
84 * device name is found.
86 ULONG WINAPI
RtlIsDosDeviceName_U( PCWSTR dos_name
)
88 static const WCHAR consoleW
[] = {'\\','\\','.','\\','C','O','N',0};
89 static const WCHAR auxW
[3] = {'A','U','X'};
90 static const WCHAR comW
[3] = {'C','O','M'};
91 static const WCHAR conW
[3] = {'C','O','N'};
92 static const WCHAR lptW
[3] = {'L','P','T'};
93 static const WCHAR nulW
[3] = {'N','U','L'};
94 static const WCHAR prnW
[3] = {'P','R','N'};
96 const WCHAR
*start
, *end
, *p
;
98 switch(RtlDetermineDosPathNameType_U( dos_name
))
104 if (!strcmpiW( dos_name
, consoleW
))
105 return MAKELONG( sizeof(conW
), 4 * sizeof(WCHAR
) ); /* 4 is length of \\.\ prefix */
111 end
= dos_name
+ strlenW(dos_name
) - 1;
112 if (end
>= dos_name
&& *end
== ':') end
--; /* remove trailing ':' */
114 /* find start of file name */
115 for (start
= end
; start
>= dos_name
; start
--)
117 if (IS_SEPARATOR(start
[0])) break;
118 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
119 if (start
[0] == ':' && start
[1] != '.') break;
123 /* remove extension */
124 if ((p
= strchrW( start
, '.' )))
127 if (end
>= dos_name
&& *end
== ':') end
--; /* remove trailing ':' before extension */
131 /* no extension, remove trailing spaces */
132 while (end
>= dos_name
&& *end
== ' ') end
--;
135 /* now we have a potential device name between start and end, check it */
136 switch(end
- start
+ 1)
139 if (strncmpiW( start
, auxW
, 3 ) &&
140 strncmpiW( start
, conW
, 3 ) &&
141 strncmpiW( start
, nulW
, 3 ) &&
142 strncmpiW( start
, prnW
, 3 )) break;
143 return MAKELONG( 3 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
145 if (strncmpiW( start
, comW
, 3 ) && strncmpiW( start
, lptW
, 3 )) break;
146 if (*end
<= '0' || *end
> '9') break;
147 return MAKELONG( 4 * sizeof(WCHAR
), (start
- dos_name
) * sizeof(WCHAR
) );
148 default: /* can't match anything */
155 /**************************************************************************
156 * RtlDosPathNameToNtPathName_U [NTDLL.@]
158 * dos_path: a DOS path name (fully qualified or not)
159 * ntpath: pointer to a UNICODE_STRING to hold the converted
161 * file_part:will point (in ntpath) to the file part in the path
162 * cd: directory reference (optional)
165 * + fill the cd structure
167 BOOLEAN WINAPI
RtlDosPathNameToNtPathName_U(PWSTR dos_path
,
168 PUNICODE_STRING ntpath
,
172 static const WCHAR LongFileNamePfxW
[4] = {'\\','\\','?','\\'};
173 ULONG sz
, ptr_sz
, offset
;
174 WCHAR local
[MAX_PATH
];
177 TRACE("(%s,%p,%p,%p)\n",
178 debugstr_w(dos_path
), ntpath
, file_part
, cd
);
182 FIXME("Unsupported parameter\n");
183 memset(cd
, 0, sizeof(*cd
));
186 if (!dos_path
|| !*dos_path
) return FALSE
;
188 if (!memcmp(dos_path
, LongFileNamePfxW
, sizeof(LongFileNamePfxW
)))
190 dos_path
+= sizeof(LongFileNamePfxW
) / sizeof(WCHAR
);
197 ptr_sz
= sizeof(local
);
199 sz
= RtlGetFullPathName_U(dos_path
, ptr_sz
, ptr
, file_part
);
200 if (sz
== 0) return FALSE
;
203 ptr
= RtlAllocateHeap(ntdll_get_process_heap(), 0, sz
);
204 sz
= RtlGetFullPathName_U(dos_path
, sz
, ptr
, file_part
);
207 ntpath
->MaximumLength
= sz
+ (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR
);
208 ntpath
->Buffer
= RtlAllocateHeap(ntdll_get_process_heap(), 0, ntpath
->MaximumLength
);
211 if (ptr
!= local
) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr
);
215 strcpyW(ntpath
->Buffer
, NTDosPrefixW
);
217 switch (RtlDetermineDosPathNameType_U(ptr
))
219 case UNC_PATH
: /* \\foo */
223 strcatW(ntpath
->Buffer
, UncPfxW
);
226 case DEVICE_PATH
: /* \\.\foo */
229 default: break; /* needed to keep gcc quiet */
232 strcatW(ntpath
->Buffer
, ptr
+ offset
);
233 ntpath
->Length
= strlenW(ntpath
->Buffer
) * sizeof(WCHAR
);
235 if (file_part
&& *file_part
)
236 *file_part
= ntpath
->Buffer
+ ntpath
->Length
/ sizeof(WCHAR
) - strlenW(*file_part
);
238 /* FIXME: cd filling */
240 if (ptr
!= local
) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr
);
244 /******************************************************************
247 * Searchs a file of name 'name' into a ';' separated list of paths
249 * Doesn't seem to search elsewhere than the paths list
250 * Stores the result in buffer (file_part will point to the position
251 * of the file name in the buffer)
253 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
255 ULONG WINAPI
RtlDosSearchPath_U(LPCWSTR paths
, LPCWSTR search
, LPCWSTR ext
,
256 ULONG buffer_size
, LPWSTR buffer
,
259 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U(search
);
262 if (type
== RELATIVE_PATH
)
264 ULONG allocated
= 0, needed
, filelen
;
265 WCHAR
*p
, *name
= NULL
;
267 filelen
= 1 /* for \ */ + strlenW(search
) + 1 /* \0 */;
269 p
= strrchrW( search
, '.' );
270 if (p
&& !strchrW( p
, '\\' ) && !strchrW( p
, '/')) ext
= NULL
;
271 if (ext
!= NULL
) filelen
+= strlenW(ext
);
277 for (needed
= 0, ptr
= paths
; *ptr
!= 0 && *ptr
++ != ';'; needed
++);
278 if (needed
+ filelen
> allocated
)
280 name
= (WCHAR
*)RtlReAllocateHeap(ntdll_get_process_heap(), 0,
281 name
, (needed
+ filelen
) * sizeof(WCHAR
));
283 allocated
= needed
+ filelen
;
285 memmove(name
, paths
, needed
* sizeof(WCHAR
));
286 /* append '\\' if none is present */
287 if (needed
> 0 && name
[needed
- 1] != '\\') name
[needed
++] = '\\';
288 strcpyW(&name
[needed
], search
);
289 if (ext
) strcatW(&name
[needed
], ext
);
290 if (RtlDoesFileExists_U(name
))
292 len
= RtlGetFullPathName_U(name
, buffer_size
, buffer
, file_part
);
297 RtlFreeHeap(ntdll_get_process_heap(), 0, name
);
299 else if (RtlDoesFileExists_U(search
))
301 len
= RtlGetFullPathName_U(search
, buffer_size
, buffer
, file_part
);
307 /******************************************************************
308 * get_full_path_helper
310 * Helper for RtlGetFullPathName_U
312 static ULONG
get_full_path_helper(LPCWSTR name
, LPWSTR buffer
, ULONG size
)
314 ULONG reqsize
, mark
= 0;
315 DOS_PATHNAME_TYPE type
;
319 reqsize
= sizeof(WCHAR
); /* '\0' at the end */
322 cd
= &ntdll_get_process_pmts()->CurrentDirectoryName
;
324 switch (type
= RtlDetermineDosPathNameType_U(name
))
326 case UNC_PATH
: /* \\foo */
327 case DEVICE_PATH
: /* \\.\foo */
328 if (reqsize
<= size
) buffer
[0] = '\0';
331 case ABSOLUTE_DRIVE_PATH
: /* c:\foo */
332 reqsize
+= sizeof(WCHAR
);
335 buffer
[0] = toupperW(name
[0]);
341 case RELATIVE_DRIVE_PATH
: /* c:foo */
342 if (toupperW(name
[0]) != toupperW(cd
->Buffer
[0]) || cd
->Buffer
[1] != ':')
345 UNICODE_STRING var
, val
;
352 var
.MaximumLength
= 8;
355 val
.MaximumLength
= size
;
360 switch (RtlQueryEnvironmentVariable_U(NULL
, &var
, &val
))
363 /* FIXME: Win2k seems to check that the environment variable actually points
364 * to an existing directory. If not, root of the drive is used
365 * (this seems also to be the only spot in RtlGetFullPathName that the
366 * existence of a part of a path is checked)
369 case STATUS_BUFFER_TOO_SMALL
:
370 reqsize
+= val
.Length
;
371 /* append trailing \\ */
372 reqsize
+= sizeof(WCHAR
);
375 buffer
[reqsize
/ sizeof(WCHAR
) - 2] = '\\';
376 buffer
[reqsize
/ sizeof(WCHAR
) - 1] = '\0';
379 case STATUS_VARIABLE_NOT_FOUND
:
380 reqsize
+= 3 * sizeof(WCHAR
);
383 buffer
[0] = drive
[1];
390 ERR("Unsupported status code\n");
398 case RELATIVE_PATH
: /* foo */
399 reqsize
+= cd
->Length
;
400 if (reqsize
<= size
) strcpyW(buffer
, cd
->Buffer
);
401 if (cd
->Buffer
[1] != ':')
403 ptr
= strchrW(cd
->Buffer
+ 2, '\\');
404 if (ptr
) ptr
= strchrW(ptr
+ 1, '\\');
405 if (!ptr
) ptr
= cd
->Buffer
+ strlenW(cd
->Buffer
);
406 mark
= ptr
- cd
->Buffer
;
410 case ABSOLUTE_PATH
: /* \xxx */
411 if (cd
->Buffer
[1] == ':')
413 reqsize
+= 2 * sizeof(WCHAR
);
416 buffer
[0] = cd
->Buffer
[0];
425 ptr
= strchrW(cd
->Buffer
+ 2, '\\');
426 if (ptr
) ptr
= strchrW(ptr
+ 1, '\\');
427 if (!ptr
) ptr
= cd
->Buffer
+ strlenW(cd
->Buffer
);
428 len
= (ptr
- cd
->Buffer
) * sizeof(WCHAR
);
430 mark
= len
/ sizeof(WCHAR
);
433 memcpy(buffer
, cd
->Buffer
, len
);
434 buffer
[len
/ sizeof(WCHAR
)] = '\0';
441 case UNC_DOT_PATH
: /* \\. */
442 reqsize
+= 4 * sizeof(WCHAR
);
459 reqsize
+= strlenW(name
) * sizeof(WCHAR
);
460 if (reqsize
> size
) goto done
;
462 strcatW(buffer
, name
);
464 /* convert every / into a \ */
465 for (ptr
= buffer
; ptr
< buffer
+ size
/ sizeof(WCHAR
); ptr
++)
466 if (*ptr
== '/') *ptr
= '\\';
468 reqsize
-= sizeof(WCHAR
); /* don't count trailing \0 */
470 /* mark is non NULL for UNC names, so start path collapsing after server & share name
471 * otherwise, it's a fully qualified DOS name, so start after the drive designation
473 for (ptr
= buffer
+ (mark
? mark
: 2); ptr
< buffer
+ reqsize
/ sizeof(WCHAR
); )
475 WCHAR
* p
= strchrW(ptr
, '\\');
489 while (prev
>= buffer
+ mark
&& *prev
!= '\\') prev
--;
490 /* either collapse \foo\.. into \ or \.. into \ */
491 if (prev
< buffer
+ mark
) prev
= p
- 1;
492 reqsize
-= (p
+ 2 - prev
) * sizeof(WCHAR
);
493 memmove(prev
, p
+ 2, buffer
+ reqsize
- prev
+ sizeof(WCHAR
));
497 reqsize
-= 2 * sizeof(WCHAR
);
503 reqsize
-= 2 * sizeof(WCHAR
);
504 memmove(ptr
+ 2, ptr
, buffer
+ reqsize
- ptr
+ sizeof(WCHAR
));
516 /******************************************************************
517 * RtlGetFullPathName_U (NTDLL.@)
519 * Returns the number of bytes written to buffer (not including the
520 * terminating NULL) if the function succeeds, or the required number of bytes
521 * (including the terminating NULL) if the buffer is to small.
523 * file_part will point to the filename part inside buffer (except if we use
524 * DOS device name, in which case file_in_buf is NULL)
527 DWORD WINAPI
RtlGetFullPathName_U(const WCHAR
* name
, ULONG size
, WCHAR
* buffer
,
533 TRACE("(%s %lu %p %p)\n", debugstr_w(name
), size
, buffer
, file_part
);
535 if (!name
|| !*name
) return 0;
537 if (file_part
) *file_part
= NULL
;
539 /* check for DOS device name */
540 dosdev
= RtlIsDosDeviceName_U(name
);
543 DWORD offset
= HIWORD(dosdev
) / sizeof(WCHAR
); /* get it in WCHARs, not bytes */
544 DWORD sz
= LOWORD(dosdev
); /* in bytes */
546 if (8 + sz
+ 2 > size
) return sz
+ 10;
547 strcpyW(buffer
, DeviceRootW
);
548 memmove(buffer
+ 4, name
+ offset
, sz
);
549 buffer
[4 + sz
/ sizeof(WCHAR
)] = '\0';
550 /* file_part isn't set in this case */
554 reqsize
= get_full_path_helper(name
, buffer
, size
);
557 LPWSTR tmp
= RtlAllocateHeap(ntdll_get_process_heap(), 0, reqsize
);
558 reqsize
= get_full_path_helper(name
, tmp
, reqsize
) + sizeof(WCHAR
);
559 RtlFreeHeap(ntdll_get_process_heap(), 0, tmp
);
565 if (file_part
&& (ptr
= strrchrW(buffer
, '\\')) != NULL
&& ptr
>= buffer
+ 2 && *++ptr
)
571 /*************************************************************************
572 * RtlGetLongestNtPathLength [NTDLL.@]
574 * Get the longest allowed path length
580 * The longest allowed path length (277 characters under Win2k).
582 DWORD WINAPI
RtlGetLongestNtPathLength(void)
587 /******************************************************************
588 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
590 * Returns TRUE iff unicode is a valid DOS (8+3) name.
591 * If the name is valid, oem gets filled with the corresponding OEM string
592 * spaces is set to TRUE if unicode contains spaces
594 BOOLEAN WINAPI
RtlIsNameLegalDOS8Dot3( const UNICODE_STRING
*unicode
,
595 OEM_STRING
*oem
, BOOLEAN
*spaces
)
601 BOOLEAN got_space
= FALSE
;
605 oem_str
.Length
= sizeof(buffer
);
606 oem_str
.MaximumLength
= sizeof(buffer
);
607 oem_str
.Buffer
= buffer
;
610 if (RtlUpcaseUnicodeStringToCountedOemString( oem
, unicode
, FALSE
) != STATUS_SUCCESS
)
613 if (oem
->Length
> 12) return FALSE
;
615 /* a starting . is invalid, except for . and .. */
616 if (oem
->Buffer
[0] == '.')
618 if (oem
->Length
!= 1 && (oem
->Length
!= 2 || oem
->Buffer
[1] != '.')) return FALSE
;
619 if (spaces
) *spaces
= FALSE
;
623 for (i
= 0; i
< oem
->Length
; i
++)
625 switch (oem
->Buffer
[i
])
628 /* leading/trailing spaces not allowed */
629 if (!i
|| i
== oem
->Length
-1 || oem
->Buffer
[i
+1] == '.') return FALSE
;
633 if (dot
!= -1) return FALSE
;
637 /* FIXME: check for invalid chars */
641 /* check file part is shorter than 8, extension shorter than 3
642 * dot cannot be last in string
646 if (oem
->Length
> 8) return FALSE
;
650 if (dot
> 8 || (oem
->Length
- dot
> 4) || dot
== oem
->Length
- 1) return FALSE
;
652 if (spaces
) *spaces
= got_space
;
656 /******************************************************************
657 * RtlGetCurrentDirectory_U (NTDLL.@)
660 NTSTATUS WINAPI
RtlGetCurrentDirectory_U(ULONG buflen
, LPWSTR buf
)
665 TRACE("(%lu %p)\n", buflen
, buf
);
669 us
= &ntdll_get_process_pmts()->CurrentDirectoryName
;
670 len
= us
->Length
/ sizeof(WCHAR
);
671 if (us
->Buffer
[len
- 1] == '\\' && us
->Buffer
[len
- 2] != ':')
674 if (buflen
/ sizeof(WCHAR
) > len
)
676 memcpy(buf
, us
->Buffer
, len
* sizeof(WCHAR
));
686 return len
* sizeof(WCHAR
);
689 /******************************************************************
690 * RtlSetCurrentDirectory_U (NTDLL.@)
693 NTSTATUS WINAPI
RtlSetCurrentDirectory_U(const UNICODE_STRING
* dir
)
695 UNICODE_STRING
* curdir
;
696 NTSTATUS nts
= STATUS_SUCCESS
;
700 TRACE("(%s)\n", debugstr_w(dir
->Buffer
));
704 curdir
= &ntdll_get_process_pmts()->CurrentDirectoryName
;
705 size
= curdir
->MaximumLength
;
707 buf
= RtlAllocateHeap(ntdll_get_process_heap(), 0, size
);
710 nts
= STATUS_NO_MEMORY
;
714 size
= RtlGetFullPathName_U(dir
->Buffer
, size
, buf
, 0);
717 nts
= STATUS_OBJECT_NAME_INVALID
;
721 switch (RtlDetermineDosPathNameType_U(buf
))
723 case ABSOLUTE_DRIVE_PATH
:
727 FIXME("Don't support those cases yes\n");
728 nts
= STATUS_NOT_IMPLEMENTED
;
732 /* FIXME: should check that the directory actually exists,
733 * and fill CurrentDirectoryHandle accordingly
736 /* append trailing \ if missing */
737 if (buf
[size
/ sizeof(WCHAR
) - 1] != '\\')
739 buf
[size
/ sizeof(WCHAR
)] = '\\';
740 buf
[size
/ sizeof(WCHAR
) + 1] = '\0';
741 size
+= sizeof(WCHAR
);
744 memmove(curdir
->Buffer
, buf
, size
+ sizeof(WCHAR
));
745 curdir
->Length
= size
;
748 if (curdir
->Buffer
[1] == ':')
754 var
[1] = curdir
->Buffer
[0];
757 env
.Length
= 3 * sizeof(WCHAR
);
758 env
.MaximumLength
= 4 * sizeof(WCHAR
);
761 RtlSetEnvironmentVariable(NULL
, &env
, curdir
);
766 if (buf
) RtlFreeHeap(ntdll_get_process_heap(), 0, buf
);