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 /******************************************************************
68 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
70 FIXME("(%s): stub\n", debugstr_w(file_name));
75 /***********************************************************************
76 * RtlIsDosDeviceName_U (NTDLL.@)
78 * Check if the given DOS path contains a DOS device name.
80 * Returns the length of the device name in the low word and its
81 * position in the high word (both in bytes, not WCHARs), or 0 if no
82 * device name is found.
84 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
86 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
87 static const WCHAR auxW[3] = {'A','U','X'};
88 static const WCHAR comW[3] = {'C','O','M'};
89 static const WCHAR conW[3] = {'C','O','N'};
90 static const WCHAR lptW[3] = {'L','P','T'};
91 static const WCHAR nulW[3] = {'N','U','L'};
92 static const WCHAR prnW[3] = {'P','R','N'};
94 const WCHAR *start, *end, *p;
96 switch(RtlDetermineDosPathNameType_U( dos_name ))
102 if (!strcmpiW( dos_name, consoleW ))
103 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
109 end = dos_name + strlenW(dos_name) - 1;
110 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
112 /* find start of file name */
113 for (start = end; start >= dos_name; start--)
115 if (IS_SEPARATOR(start[0])) break;
116 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
117 if (start[0] == ':' && start[1] != '.') break;
121 /* remove extension */
122 if ((p = strchrW( start, '.' )))
125 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
129 /* no extension, remove trailing spaces */
130 while (end >= dos_name && *end == ' ') end--;
133 /* now we have a potential device name between start and end, check it */
134 switch(end - start + 1)
137 if (strncmpiW( start, auxW, 3 ) &&
138 strncmpiW( start, conW, 3 ) &&
139 strncmpiW( start, nulW, 3 ) &&
140 strncmpiW( start, prnW, 3 )) break;
141 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
143 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
144 if (*end <= '0' || *end > '9') break;
145 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
146 default: /* can't match anything */
153 /**************************************************************************
154 * RtlDosPathNameToNtPathName_U [NTDLL.@]
156 * dos_path: a DOS path name (fully qualified or not)
157 * ntpath: pointer to a UNICODE_STRING to hold the converted
159 * file_part:will point (in ntpath) to the file part in the path
160 * cd: directory reference (optional)
163 * + fill the cd structure
165 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PWSTR dos_path,
166 PUNICODE_STRING ntpath,
170 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
171 ULONG sz, ptr_sz, offset;
172 WCHAR local[MAX_PATH];
175 TRACE("(%s,%p,%p,%p)\n",
176 debugstr_w(dos_path), ntpath, file_part, cd);
180 FIXME("Unsupported parameter\n");
181 memset(cd, 0, sizeof(*cd));
184 if (!dos_path || !*dos_path) return FALSE;
186 if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
188 dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
195 ptr_sz = sizeof(local);
197 sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
198 if (sz == 0) return FALSE;
201 ptr = RtlAllocateHeap(ntdll_get_process_heap(), 0, sz);
202 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
205 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
206 ntpath->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, ntpath->MaximumLength);
209 if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
213 strcpyW(ntpath->Buffer, NTDosPrefixW);
215 switch (RtlDetermineDosPathNameType_U(ptr))
217 case UNC_PATH: /* \\foo */
221 strcatW(ntpath->Buffer, UncPfxW);
224 case DEVICE_PATH: /* \\.\foo */
227 default: break; /* needed to keep gcc quiet */
230 strcatW(ntpath->Buffer, ptr + offset);
231 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
233 if (file_part && *file_part)
234 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
236 /* FIXME: cd filling */
238 if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
242 /******************************************************************
245 * Searchs a file of name 'name' into a ';' separated list of paths
247 * Doesn't seem to search elsewhere than the paths list
248 * Stores the result in buffer (file_part will point to the position
249 * of the file name in the buffer)
251 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
253 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
254 ULONG buffer_size, LPWSTR buffer,
257 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
260 if (type == RELATIVE_PATH)
262 ULONG allocated = 0, needed, filelen;
265 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
267 if (strchrW(search, '.') != NULL) ext = NULL;
268 if (ext != NULL) filelen += strlenW(ext);
274 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
275 if (needed + filelen > allocated)
277 name = (WCHAR*)RtlReAllocateHeap(ntdll_get_process_heap(), 0,
278 name, (needed + filelen) * sizeof(WCHAR));
280 allocated = needed + filelen;
282 memmove(name, paths, needed * sizeof(WCHAR));
283 /* append '\\' if none is present */
284 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
285 strcpyW(&name[needed], search);
286 if (ext) strcatW(&name[needed], ext);
287 if (RtlDoesFileExists_U(name))
289 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
294 RtlFreeHeap(ntdll_get_process_heap(), 0, name);
296 else if (RtlDoesFileExists_U(search))
298 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
304 /******************************************************************
305 * get_full_path_helper
307 * Helper for RtlGetFullPathName_U
309 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
311 ULONG reqsize, mark = 0;
312 DOS_PATHNAME_TYPE type;
316 reqsize = sizeof(WCHAR); /* '\0' at the end */
319 cd = &ntdll_get_process_pmts()->CurrentDirectoryName;
321 switch (type = RtlDetermineDosPathNameType_U(name))
323 case UNC_PATH: /* \\foo */
324 case DEVICE_PATH: /* \\.\foo */
325 if (reqsize <= size) buffer[0] = '\0';
328 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
329 reqsize += sizeof(WCHAR);
332 buffer[0] = toupperW(name[0]);
338 case RELATIVE_DRIVE_PATH: /* c:foo */
339 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
342 UNICODE_STRING var, val;
349 var.MaximumLength = 8;
352 val.MaximumLength = size;
357 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
360 /* FIXME: Win2k seems to check that the environment variable actually points
361 * to an existing directory. If not, root of the drive is used
362 * (this seems also to be the only spot in RtlGetFullPathName that the
363 * existence of a part of a path is checked)
366 case STATUS_BUFFER_TOO_SMALL:
367 reqsize += val.Length;
368 /* append trailing \\ */
369 reqsize += sizeof(WCHAR);
372 buffer[reqsize / sizeof(WCHAR) - 2] = '\\';
373 buffer[reqsize / sizeof(WCHAR) - 1] = '\0';
376 case STATUS_VARIABLE_NOT_FOUND:
377 reqsize += 3 * sizeof(WCHAR);
380 buffer[0] = drive[1];
387 ERR("Unsupported status code\n");
395 case RELATIVE_PATH: /* foo */
396 reqsize += cd->Length;
397 mark = cd->Length / sizeof(WCHAR);
399 strcpyW(buffer, cd->Buffer);
402 case ABSOLUTE_PATH: /* \xxx */
403 if (cd->Buffer[1] == ':')
405 reqsize += 2 * sizeof(WCHAR);
408 buffer[0] = cd->Buffer[0];
417 ptr = strchrW(cd->Buffer + 2, '\\');
418 if (ptr) ptr = strchrW(ptr + 1, '\\');
419 if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
420 len = (ptr - cd->Buffer) * sizeof(WCHAR);
422 mark = len / sizeof(WCHAR);
425 memcpy(buffer, cd->Buffer, len);
426 buffer[len / sizeof(WCHAR)] = '\0';
433 case UNC_DOT_PATH: /* \\. */
434 reqsize += 4 * sizeof(WCHAR);
451 reqsize += strlenW(name) * sizeof(WCHAR);
452 if (reqsize > size) goto done;
454 strcatW(buffer, name);
456 /* convert every / into a \ */
457 for (ptr = buffer; ptr < buffer + size / sizeof(WCHAR); ptr++)
458 if (*ptr == '/') *ptr = '\\';
460 reqsize -= sizeof(WCHAR); /* don't count trailing \0 */
462 /* mark is non NULL for UNC names, so start path collapsing after server & share name
463 * otherwise, it's a fully qualified DOS name, so start after the drive designation
465 for (ptr = buffer + (mark ? mark : 2); ptr < buffer + reqsize / sizeof(WCHAR); )
467 WCHAR* p = strchrW(ptr, '\\');
481 while (prev >= buffer + mark && *prev != '\\') prev--;
482 /* either collapse \foo\.. into \ or \.. into \ */
483 if (prev < buffer + mark) prev = p - 1;
484 reqsize -= (p + 2 - prev) * sizeof(WCHAR);
485 memmove(prev, p + 2, buffer + reqsize - prev + sizeof(WCHAR));
489 reqsize -= 2 * sizeof(WCHAR);
495 reqsize -= 2 * sizeof(WCHAR);
496 memmove(ptr + 2, ptr, buffer + reqsize - ptr + sizeof(WCHAR));
508 /******************************************************************
509 * RtlGetFullPathName_U (NTDLL.@)
511 * Returns the number of bytes written to buffer (not including the
512 * terminating NULL) if the function succeeds, or the required number of bytes
513 * (including the terminating NULL) if the buffer is to small.
515 * file_part will point to the filename part inside buffer (except if we use
516 * DOS device name, in which case file_in_buf is NULL)
519 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
525 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
527 if (!name || !*name) return 0;
529 if (file_part) *file_part = NULL;
531 /* check for DOS device name */
532 dosdev = RtlIsDosDeviceName_U(name);
535 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
536 DWORD sz = LOWORD(dosdev); /* in bytes */
538 if (8 + sz + 2 > size) return sz + 10;
539 strcpyW(buffer, DeviceRootW);
540 memmove(buffer + 4, name + offset, sz);
541 buffer[4 + sz / sizeof(WCHAR)] = '\0';
542 /* file_part isn't set in this case */
546 reqsize = get_full_path_helper(name, buffer, size);
549 LPWSTR tmp = RtlAllocateHeap(ntdll_get_process_heap(), 0, reqsize);
550 reqsize = get_full_path_helper(name, tmp, reqsize) + sizeof(WCHAR);
551 RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
557 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
563 /*************************************************************************
564 * RtlGetLongestNtPathLength [NTDLL.@]
566 * Get the longest allowed path length
572 * The longest allowed path length (277 characters under Win2k).
574 DWORD WINAPI RtlGetLongestNtPathLength(void)
579 /******************************************************************
580 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
582 * Returns TRUE iff unicode is a valid DOS (8+3) name.
583 * If the name is valid, oem gets filled with the corresponding OEM string
584 * spaces is set to TRUE if unicode contains spaces
586 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
587 OEM_STRING *oem, BOOLEAN *spaces )
593 BOOLEAN got_space = FALSE;
597 oem_str.Length = sizeof(buffer);
598 oem_str.MaximumLength = sizeof(buffer);
599 oem_str.Buffer = buffer;
602 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
605 if (oem->Length > 12) return FALSE;
607 /* a starting . is invalid, except for . and .. */
608 if (oem->Buffer[0] == '.')
610 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
611 if (spaces) *spaces = FALSE;
615 for (i = 0; i < oem->Length; i++)
617 switch (oem->Buffer[i])
620 /* leading/trailing spaces not allowed */
621 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
625 if (dot != -1) return FALSE;
629 /* FIXME: check for invalid chars */
633 /* check file part is shorter than 8, extension shorter than 3
634 * dot cannot be last in string
638 if (oem->Length > 8) return FALSE;
642 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
644 if (spaces) *spaces = got_space;
648 /******************************************************************
649 * RtlGetCurrentDirectory_U (NTDLL.@)
652 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
657 TRACE("(%lu %p)\n", buflen, buf);
661 us = &ntdll_get_process_pmts()->CurrentDirectoryName;
662 len = us->Length / sizeof(WCHAR);
663 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
666 if (buflen / sizeof(WCHAR) > len)
668 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
678 return len * sizeof(WCHAR);
681 /******************************************************************
682 * RtlSetCurrentDirectory_U (NTDLL.@)
685 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
687 UNICODE_STRING* curdir;
688 NTSTATUS nts = STATUS_SUCCESS;
692 TRACE("(%s)\n", debugstr_w(dir->Buffer));
696 curdir = &ntdll_get_process_pmts()->CurrentDirectoryName;
697 size = curdir->MaximumLength;
699 buf = RtlAllocateHeap(ntdll_get_process_heap(), 0, size);
702 nts = STATUS_NO_MEMORY;
706 size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
709 nts = STATUS_OBJECT_NAME_INVALID;
713 switch (RtlDetermineDosPathNameType_U(buf))
715 case ABSOLUTE_DRIVE_PATH:
719 FIXME("Don't support those cases yes\n");
720 nts = STATUS_NOT_IMPLEMENTED;
724 /* FIXME: should check that the directory actually exists,
725 * and fill CurrentDirectoryHandle accordingly
728 /* append trailing \ if missing */
729 if (buf[size / sizeof(WCHAR) - 1] != '\\')
731 buf[size / sizeof(WCHAR)] = '\\';
732 buf[size / sizeof(WCHAR) + 1] = '\0';
733 size += sizeof(WCHAR);
736 memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
737 curdir->Length = size;
740 if (curdir->Buffer[1] == ':')
746 var[1] = curdir->Buffer[0];
749 env.Length = 3 * sizeof(WCHAR);
750 env.MaximumLength = 4 * sizeof(WCHAR);
753 RtlSetEnvironmentVariable(NULL, &env, curdir);
758 if (buf) RtlFreeHeap(ntdll_get_process_heap(), 0, buf);