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};
41 HANDLE (WINAPI *pCreateFileW)( LPCWSTR filename, DWORD access, DWORD sharing,
42 LPSECURITY_ATTRIBUTES sa, DWORD creation,
43 DWORD attributes, HANDLE template );
45 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
47 /***********************************************************************
48 * RtlDetermineDosPathNameType_U (NTDLL.@)
50 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
52 if (IS_SEPARATOR(path[0]))
54 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
55 if (path[2] != '.') return UNC_PATH; /* "//foo" */
56 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
57 if (path[3]) return UNC_PATH; /* "//.foo" */
58 return UNC_DOT_PATH; /* "//." */
62 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
63 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
64 return RELATIVE_DRIVE_PATH; /* "c:foo" */
68 /******************************************************************
71 * FIXME: should not use CreateFileW
73 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
75 HANDLE handle = pCreateFileW( file_name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
76 NULL, OPEN_EXISTING, 0, 0 );
77 if (handle == INVALID_HANDLE_VALUE) return FALSE;
82 /***********************************************************************
83 * RtlIsDosDeviceName_U (NTDLL.@)
85 * Check if the given DOS path contains a DOS device name.
87 * Returns the length of the device name in the low word and its
88 * position in the high word (both in bytes, not WCHARs), or 0 if no
89 * device name is found.
91 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
93 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
94 static const WCHAR auxW[3] = {'A','U','X'};
95 static const WCHAR comW[3] = {'C','O','M'};
96 static const WCHAR conW[3] = {'C','O','N'};
97 static const WCHAR lptW[3] = {'L','P','T'};
98 static const WCHAR nulW[3] = {'N','U','L'};
99 static const WCHAR prnW[3] = {'P','R','N'};
101 const WCHAR *start, *end, *p;
103 switch(RtlDetermineDosPathNameType_U( dos_name ))
109 if (!strcmpiW( dos_name, consoleW ))
110 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
116 end = dos_name + strlenW(dos_name) - 1;
117 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
119 /* find start of file name */
120 for (start = end; start >= dos_name; start--)
122 if (IS_SEPARATOR(start[0])) break;
123 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
124 if (start[0] == ':' && start[1] != '.') break;
128 /* remove extension */
129 if ((p = strchrW( start, '.' )))
132 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
136 /* no extension, remove trailing spaces */
137 while (end >= dos_name && *end == ' ') end--;
140 /* now we have a potential device name between start and end, check it */
141 switch(end - start + 1)
144 if (strncmpiW( start, auxW, 3 ) &&
145 strncmpiW( start, conW, 3 ) &&
146 strncmpiW( start, nulW, 3 ) &&
147 strncmpiW( start, prnW, 3 )) break;
148 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
150 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
151 if (*end <= '0' || *end > '9') break;
152 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
153 default: /* can't match anything */
160 /**************************************************************************
161 * RtlDosPathNameToNtPathName_U [NTDLL.@]
163 * dos_path: a DOS path name (fully qualified or not)
164 * ntpath: pointer to a UNICODE_STRING to hold the converted
166 * file_part:will point (in ntpath) to the file part in the path
167 * cd: directory reference (optional)
170 * + fill the cd structure
172 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PWSTR dos_path,
173 PUNICODE_STRING ntpath,
177 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
178 ULONG sz, ptr_sz, offset;
179 WCHAR local[MAX_PATH];
182 TRACE("(%s,%p,%p,%p)\n",
183 debugstr_w(dos_path), ntpath, file_part, cd);
187 FIXME("Unsupported parameter\n");
188 memset(cd, 0, sizeof(*cd));
191 if (!dos_path || !*dos_path) return FALSE;
193 if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
195 dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
202 ptr_sz = sizeof(local);
204 sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
205 if (sz == 0) return FALSE;
208 ptr = RtlAllocateHeap(ntdll_get_process_heap(), 0, sz);
209 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
212 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
213 ntpath->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, ntpath->MaximumLength);
216 if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
220 strcpyW(ntpath->Buffer, NTDosPrefixW);
222 switch (RtlDetermineDosPathNameType_U(ptr))
224 case UNC_PATH: /* \\foo */
228 strcatW(ntpath->Buffer, UncPfxW);
231 case DEVICE_PATH: /* \\.\foo */
234 default: break; /* needed to keep gcc quiet */
237 strcatW(ntpath->Buffer, ptr + offset);
238 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
240 if (file_part && *file_part)
241 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
243 /* FIXME: cd filling */
245 if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
249 /******************************************************************
252 * Searchs a file of name 'name' into a ';' separated list of paths
254 * Doesn't seem to search elsewhere than the paths list
255 * Stores the result in buffer (file_part will point to the position
256 * of the file name in the buffer)
258 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
260 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
261 ULONG buffer_size, LPWSTR buffer,
264 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
267 if (type == RELATIVE_PATH)
269 ULONG allocated = 0, needed, filelen;
272 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
274 /* Windows only checks for '.' without worrying about path components */
275 if (strchrW( search, '.' )) ext = NULL;
276 if (ext != NULL) filelen += strlenW(ext);
282 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
283 if (needed + filelen > allocated)
285 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
286 (needed + filelen) * sizeof(WCHAR));
289 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
290 (needed + filelen) * sizeof(WCHAR));
291 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
295 allocated = needed + filelen;
297 memmove(name, paths, needed * sizeof(WCHAR));
298 /* append '\\' if none is present */
299 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
300 strcpyW(&name[needed], search);
301 if (ext) strcatW(&name[needed], ext);
302 if (RtlDoesFileExists_U(name))
304 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
309 RtlFreeHeap(ntdll_get_process_heap(), 0, name);
311 else if (RtlDoesFileExists_U(search))
313 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
319 /******************************************************************
320 * get_full_path_helper
322 * Helper for RtlGetFullPathName_U
324 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
326 ULONG reqsize, mark = 0;
327 DOS_PATHNAME_TYPE type;
331 reqsize = sizeof(WCHAR); /* '\0' at the end */
334 cd = &ntdll_get_process_pmts()->CurrentDirectoryName;
336 switch (type = RtlDetermineDosPathNameType_U(name))
338 case UNC_PATH: /* \\foo */
339 case DEVICE_PATH: /* \\.\foo */
340 if (reqsize <= size) buffer[0] = '\0';
343 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
344 reqsize += sizeof(WCHAR);
347 buffer[0] = toupperW(name[0]);
353 case RELATIVE_DRIVE_PATH: /* c:foo */
354 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
357 UNICODE_STRING var, val;
364 var.MaximumLength = 8;
367 val.MaximumLength = size;
372 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
375 /* FIXME: Win2k seems to check that the environment variable actually points
376 * to an existing directory. If not, root of the drive is used
377 * (this seems also to be the only spot in RtlGetFullPathName that the
378 * existence of a part of a path is checked)
381 case STATUS_BUFFER_TOO_SMALL:
382 reqsize += val.Length;
383 /* append trailing \\ */
384 reqsize += sizeof(WCHAR);
387 buffer[reqsize / sizeof(WCHAR) - 2] = '\\';
388 buffer[reqsize / sizeof(WCHAR) - 1] = '\0';
391 case STATUS_VARIABLE_NOT_FOUND:
392 reqsize += 3 * sizeof(WCHAR);
395 buffer[0] = drive[1];
402 ERR("Unsupported status code\n");
410 case RELATIVE_PATH: /* foo */
411 reqsize += cd->Length;
414 memcpy(buffer, cd->Buffer, cd->Length);
415 buffer[cd->Length / sizeof(WCHAR)] = 0;
417 if (cd->Buffer[1] != ':')
419 ptr = strchrW(cd->Buffer + 2, '\\');
420 if (ptr) ptr = strchrW(ptr + 1, '\\');
421 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
422 mark = ptr - cd->Buffer;
426 case ABSOLUTE_PATH: /* \xxx */
427 if (cd->Buffer[1] == ':')
429 reqsize += 2 * sizeof(WCHAR);
432 buffer[0] = cd->Buffer[0];
441 ptr = strchrW(cd->Buffer + 2, '\\');
442 if (ptr) ptr = strchrW(ptr + 1, '\\');
443 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
444 len = (ptr - cd->Buffer) * sizeof(WCHAR);
446 mark = len / sizeof(WCHAR);
449 memcpy(buffer, cd->Buffer, len);
450 buffer[len / sizeof(WCHAR)] = '\0';
457 case UNC_DOT_PATH: /* \\. */
458 reqsize += 4 * sizeof(WCHAR);
475 reqsize += strlenW(name) * sizeof(WCHAR);
476 if (reqsize > size) goto done;
478 strcatW(buffer, name);
480 /* convert every / into a \ */
481 for (ptr = buffer; *ptr; ptr++)
482 if (*ptr == '/') *ptr = '\\';
484 reqsize -= sizeof(WCHAR); /* don't count trailing \0 */
486 /* mark is non NULL for UNC names, so start path collapsing after server & share name
487 * otherwise, it's a fully qualified DOS name, so start after the drive designation
489 for (ptr = buffer + (mark ? mark : 2); ptr < buffer + reqsize / sizeof(WCHAR); )
491 WCHAR* p = strchrW(ptr, '\\');
505 while (prev >= buffer + mark && *prev != '\\') prev--;
506 /* either collapse \foo\.. into \ or \.. into \ */
507 if (prev < buffer + mark) prev = p - 1;
508 reqsize -= (p + 2 - prev) * sizeof(WCHAR);
509 memmove(prev, p + 2, reqsize + sizeof(WCHAR) - (prev - buffer) * sizeof(WCHAR));
514 reqsize -= 2 * sizeof(WCHAR);
520 reqsize -= 2 * sizeof(WCHAR);
521 memmove(p, p + 2, reqsize + sizeof(WCHAR) - (p - buffer) * sizeof(WCHAR));
533 /******************************************************************
534 * RtlGetFullPathName_U (NTDLL.@)
536 * Returns the number of bytes written to buffer (not including the
537 * terminating NULL) if the function succeeds, or the required number of bytes
538 * (including the terminating NULL) if the buffer is too small.
540 * file_part will point to the filename part inside buffer (except if we use
541 * DOS device name, in which case file_in_buf is NULL)
544 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
551 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
553 if (!name || !*name) return 0;
555 if (file_part) *file_part = NULL;
557 /* check for DOS device name */
558 dosdev = RtlIsDosDeviceName_U(name);
561 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
562 DWORD sz = LOWORD(dosdev); /* in bytes */
564 if (8 + sz + 2 > size) return sz + 10;
565 strcpyW(buffer, DeviceRootW);
566 memmove(buffer + 4, name + offset, sz);
567 buffer[4 + sz / sizeof(WCHAR)] = '\0';
568 /* file_part isn't set in this case */
572 reqsize = get_full_path_helper(name, buffer, size);
575 LPWSTR tmp = RtlAllocateHeap(ntdll_get_process_heap(), 0, reqsize);
576 reqsize = get_full_path_helper(name, tmp, reqsize);
577 if (reqsize > size) /* it may have worked the second time */
579 RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
580 return reqsize + sizeof(WCHAR);
582 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
583 RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
587 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
592 /*************************************************************************
593 * RtlGetLongestNtPathLength [NTDLL.@]
595 * Get the longest allowed path length
601 * The longest allowed path length (277 characters under Win2k).
603 DWORD WINAPI RtlGetLongestNtPathLength(void)
608 /******************************************************************
609 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
611 * Returns TRUE iff unicode is a valid DOS (8+3) name.
612 * If the name is valid, oem gets filled with the corresponding OEM string
613 * spaces is set to TRUE if unicode contains spaces
615 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
616 OEM_STRING *oem, BOOLEAN *spaces )
622 BOOLEAN got_space = FALSE;
626 oem_str.Length = sizeof(buffer);
627 oem_str.MaximumLength = sizeof(buffer);
628 oem_str.Buffer = buffer;
631 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
634 if (oem->Length > 12) return FALSE;
636 /* a starting . is invalid, except for . and .. */
637 if (oem->Buffer[0] == '.')
639 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
640 if (spaces) *spaces = FALSE;
644 for (i = 0; i < oem->Length; i++)
646 switch (oem->Buffer[i])
649 /* leading/trailing spaces not allowed */
650 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
654 if (dot != -1) return FALSE;
658 /* FIXME: check for invalid chars */
662 /* check file part is shorter than 8, extension shorter than 3
663 * dot cannot be last in string
667 if (oem->Length > 8) return FALSE;
671 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
673 if (spaces) *spaces = got_space;
677 /******************************************************************
678 * RtlGetCurrentDirectory_U (NTDLL.@)
681 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
686 TRACE("(%lu %p)\n", buflen, buf);
690 us = &ntdll_get_process_pmts()->CurrentDirectoryName;
691 len = us->Length / sizeof(WCHAR);
692 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
695 if (buflen / sizeof(WCHAR) > len)
697 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
707 return len * sizeof(WCHAR);
710 /******************************************************************
711 * RtlSetCurrentDirectory_U (NTDLL.@)
714 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
716 UNICODE_STRING* curdir;
717 NTSTATUS nts = STATUS_SUCCESS;
721 TRACE("(%s)\n", debugstr_w(dir->Buffer));
725 curdir = &ntdll_get_process_pmts()->CurrentDirectoryName;
726 size = curdir->MaximumLength;
728 buf = RtlAllocateHeap(ntdll_get_process_heap(), 0, size);
731 nts = STATUS_NO_MEMORY;
735 size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
738 nts = STATUS_OBJECT_NAME_INVALID;
742 switch (RtlDetermineDosPathNameType_U(buf))
744 case ABSOLUTE_DRIVE_PATH:
748 FIXME("Don't support those cases yes\n");
749 nts = STATUS_NOT_IMPLEMENTED;
753 /* FIXME: should check that the directory actually exists,
754 * and fill CurrentDirectoryHandle accordingly
757 /* append trailing \ if missing */
758 if (buf[size / sizeof(WCHAR) - 1] != '\\')
760 buf[size / sizeof(WCHAR)] = '\\';
761 buf[size / sizeof(WCHAR) + 1] = '\0';
762 size += sizeof(WCHAR);
765 memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
766 curdir->Length = size;
769 if (curdir->Buffer[1] == ':')
775 var[1] = curdir->Buffer[0];
778 env.Length = 3 * sizeof(WCHAR);
779 env.MaximumLength = 4 * sizeof(WCHAR);
782 RtlSetEnvironmentVariable(NULL, &env, curdir);
787 if (buf) RtlFreeHeap(ntdll_get_process_heap(), 0, buf);