hidclass.sys: Remove old reports from WINE_HIDP_PREPARSED_DATA.
[wine.git] / dlls / ntdll / path.c
blob131b0e33683d94c29d7f1b6fa00badbcbb941750
1 /*
2 * Ntdll path functions
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
22 #include <stdarg.h>
23 #include <sys/types.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winioctl.h"
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 "//?" */
49 else
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 ))
81 case INVALID_PATH:
82 case UNC_PATH:
83 return 0;
84 case DEVICE_PATH:
85 if (!wcsicmp( dos_name, L"\\\\.\\CON" ))
86 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
87 return 0;
88 case ABSOLUTE_DRIVE_PATH:
89 case RELATIVE_DRIVE_PATH:
90 start = dos_name + 2; /* skip drive letter */
91 break;
92 default:
93 start = dos_name;
94 break;
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;
102 end--;
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)
110 case 3:
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) );
116 case 4:
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) );
120 case 6:
121 if (wcsnicmp( start, coninW, ARRAY_SIZE(coninW) )) break;
122 return MAKELONG( sizeof(coninW), (start - dos_name) * sizeof(WCHAR) );
123 case 7:
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 */
127 break;
129 return 0;
132 /******************************************************************
133 * is_valid_directory
135 * Helper for RtlDosPathNameToNtPathName_U_WithStatus.
136 * Test if the path is an exisiting directory.
138 static BOOL is_valid_directory(LPCWSTR path)
140 OBJECT_ATTRIBUTES attr;
141 UNICODE_STRING ntpath;
142 IO_STATUS_BLOCK io;
143 HANDLE handle;
144 NTSTATUS nts;
146 if (!RtlDosPathNameToNtPathName_U(path, &ntpath, NULL, NULL))
147 return FALSE;
149 attr.Length = sizeof(attr);
150 attr.RootDirectory = 0;
151 attr.Attributes = OBJ_CASE_INSENSITIVE;
152 attr.ObjectName = &ntpath;
153 attr.SecurityDescriptor = NULL;
154 attr.SecurityQualityOfService = NULL;
156 nts = NtOpenFile(&handle, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr, &io,
157 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
158 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
159 RtlFreeUnicodeString(&ntpath);
160 if (nts != STATUS_SUCCESS)
161 return FALSE;
162 NtClose(handle);
163 return TRUE;
166 /**************************************************************************
167 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
169 * dos_path: a DOS path name (fully qualified or not)
170 * ntpath: pointer to a UNICODE_STRING to hold the converted
171 * path name
172 * file_part:will point (in ntpath) to the file part in the path
173 * cd: directory reference (optional)
175 * FIXME:
176 * + fill the cd structure
178 NTSTATUS WINAPI RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR *dos_path, UNICODE_STRING *ntpath,
179 WCHAR **file_part, CURDIR *cd)
181 static const WCHAR global_prefix[] = {'\\','\\','?','\\'};
182 static const WCHAR global_prefix2[] = {'\\','?','?','\\'};
183 NTSTATUS nts = STATUS_SUCCESS;
184 ULONG sz, offset, dosdev;
185 WCHAR local[MAX_PATH];
186 LPWSTR ptr = local;
188 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, cd);
190 if (cd)
192 FIXME("Unsupported parameter\n");
193 memset(cd, 0, sizeof(*cd));
196 if (!dos_path || !*dos_path)
197 return STATUS_OBJECT_NAME_INVALID;
199 if (!memcmp(dos_path, global_prefix, sizeof(global_prefix)) ||
200 (!memcmp(dos_path, global_prefix2, sizeof(global_prefix2)) && dos_path[4]))
202 ntpath->Length = wcslen(dos_path) * sizeof(WCHAR);
203 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
204 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
205 if (!ntpath->Buffer) return STATUS_NO_MEMORY;
206 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
207 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
208 if (file_part)
210 if ((ptr = wcsrchr( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
211 else *file_part = NULL;
213 return STATUS_SUCCESS;
216 dosdev = RtlIsDosDeviceName_U(dos_path);
217 if ((offset = HIWORD(dosdev)))
219 sz = offset + sizeof(WCHAR);
221 if (sz > sizeof(local) &&
222 (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))))
223 return STATUS_NO_MEMORY;
225 memcpy(ptr, dos_path, offset);
226 ptr[offset/sizeof(WCHAR)] = '\0';
228 if (!is_valid_directory(ptr))
230 nts = STATUS_OBJECT_NAME_INVALID;
231 goto out;
234 if (file_part) *file_part = NULL;
236 sz = LOWORD(dosdev);
238 wcscpy(ptr, L"\\\\.\\");
239 memcpy(ptr + 4, dos_path + offset / sizeof(WCHAR), sz);
240 ptr[4 + sz / sizeof(WCHAR)] = '\0';
241 sz += 4 * sizeof(WCHAR);
243 else
245 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
246 if (sz == 0) return STATUS_OBJECT_NAME_INVALID;
248 if (sz > sizeof(local))
250 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return STATUS_NO_MEMORY;
251 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
254 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
255 if (sz > MAXWORD)
257 nts = STATUS_OBJECT_NAME_INVALID;
258 goto out;
261 ntpath->MaximumLength = sz;
262 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
263 if (!ntpath->Buffer)
265 nts = STATUS_NO_MEMORY;
266 goto out;
269 wcscpy(ntpath->Buffer, L"\\??\\");
270 switch (RtlDetermineDosPathNameType_U(ptr))
272 case UNC_PATH: /* \\foo */
273 offset = 2;
274 wcscat(ntpath->Buffer, L"UNC\\");
275 break;
276 case DEVICE_PATH: /* \\.\foo */
277 offset = 4;
278 break;
279 default:
280 offset = 0;
281 break;
284 wcscat(ntpath->Buffer, ptr + offset);
285 ntpath->Length = wcslen(ntpath->Buffer) * sizeof(WCHAR);
287 if (file_part && *file_part)
288 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - wcslen(*file_part);
290 /* FIXME: cd filling */
292 out:
293 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
294 return nts;
297 /**************************************************************************
298 * RtlDosPathNameToNtPathName_U [NTDLL.@]
300 * See RtlDosPathNameToNtPathName_U_WithStatus
302 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
303 PUNICODE_STRING ntpath,
304 PWSTR* file_part,
305 CURDIR* cd)
307 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, cd) == STATUS_SUCCESS;
310 /**************************************************************************
311 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
313 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
315 NTSTATUS WINAPI RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR *dos_path,
316 UNICODE_STRING *ntpath, WCHAR **file_part, RTL_RELATIVE_NAME *relative)
318 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, relative);
320 if (relative)
322 FIXME("Unsupported parameter\n");
323 memset(relative, 0, sizeof(*relative));
326 /* FIXME: fill parameter relative */
328 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, NULL);
331 /**************************************************************************
332 * RtlReleaseRelativeName [NTDLL.@]
334 void WINAPI RtlReleaseRelativeName(RTL_RELATIVE_NAME *relative)
338 /******************************************************************
339 * RtlDosSearchPath_U
341 * Searches a file of name 'name' into a ';' separated list of paths
342 * (stored in paths)
343 * Doesn't seem to search elsewhere than the paths list
344 * Stores the result in buffer (file_part will point to the position
345 * of the file name in the buffer)
346 * FIXME:
347 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
349 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
350 ULONG buffer_size, LPWSTR buffer,
351 LPWSTR* file_part)
353 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
354 ULONG len = 0;
356 if (type == RELATIVE_PATH)
358 ULONG allocated = 0, needed, filelen;
359 WCHAR *name = NULL;
361 filelen = 1 /* for \ */ + wcslen(search) + 1 /* \0 */;
363 /* Windows only checks for '.' without worrying about path components */
364 if (wcschr( search, '.' )) ext = NULL;
365 if (ext != NULL) filelen += wcslen(ext);
367 while (*paths)
369 LPCWSTR ptr;
371 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
372 if (needed + filelen > allocated)
374 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
375 (needed + filelen) * sizeof(WCHAR));
376 else
378 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
379 (needed + filelen) * sizeof(WCHAR));
380 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
381 name = newname;
383 if (!name) return 0;
384 allocated = needed + filelen;
386 memmove(name, paths, needed * sizeof(WCHAR));
387 /* append '\\' if none is present */
388 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
389 wcscpy(&name[needed], search);
390 if (ext) wcscat(&name[needed], ext);
391 if (RtlDoesFileExists_U(name))
393 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
394 break;
396 paths = ptr;
398 RtlFreeHeap(GetProcessHeap(), 0, name);
400 else if (RtlDoesFileExists_U(search))
402 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
405 return len;
409 /******************************************************************
410 * collapse_path
412 * Helper for RtlGetFullPathName_U.
413 * Get rid of . and .. components in the path.
415 static inline void collapse_path( WCHAR *path, UINT mark )
417 WCHAR *p, *next;
419 /* convert every / into a \ */
420 for (p = path; *p; p++) if (*p == '/') *p = '\\';
422 /* collapse duplicate backslashes */
423 next = path + max( 1, mark );
424 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
425 *next = 0;
427 p = path + mark;
428 while (*p)
430 if (*p == '.')
432 switch(p[1])
434 case '\\': /* .\ component */
435 next = p + 2;
436 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
437 continue;
438 case 0: /* final . */
439 if (p > path + mark) p--;
440 *p = 0;
441 continue;
442 case '.':
443 if (p[2] == '\\') /* ..\ component */
445 next = p + 3;
446 if (p > path + mark)
448 p--;
449 while (p > path + mark && p[-1] != '\\') p--;
451 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
452 continue;
454 else if (!p[2]) /* final .. */
456 if (p > path + mark)
458 p--;
459 while (p > path + mark && p[-1] != '\\') p--;
460 if (p > path + mark) p--;
462 *p = 0;
463 continue;
465 break;
468 /* skip to the next component */
469 while (*p && *p != '\\') p++;
470 if (*p == '\\')
472 /* remove last dot in previous dir name */
473 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (wcslen(p) + 1) * sizeof(WCHAR) );
474 else p++;
478 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
479 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
480 *p = 0;
484 /******************************************************************
485 * skip_unc_prefix
487 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
489 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
491 ptr += 2;
492 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
493 while (IS_SEPARATOR(*ptr)) ptr++;
494 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
495 while (IS_SEPARATOR(*ptr)) ptr++;
496 return ptr;
500 /******************************************************************
501 * get_full_path_helper
503 * Helper for RtlGetFullPathName_U
504 * Note: name and buffer are allowed to point to the same memory spot
506 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
508 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
509 LPWSTR ins_str = NULL;
510 LPCWSTR ptr;
511 const UNICODE_STRING* cd;
512 WCHAR tmp[4];
514 /* return error if name only consists of spaces */
515 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
516 if (!*ptr) return 0;
518 RtlAcquirePebLock();
520 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
521 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
522 else
523 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
525 switch (RtlDetermineDosPathNameType_U(name))
527 case UNC_PATH: /* \\foo */
528 ptr = skip_unc_prefix( name );
529 mark = (ptr - name);
530 break;
532 case DEVICE_PATH: /* \\.\foo */
533 mark = 4;
534 break;
536 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
537 reqsize = sizeof(WCHAR);
538 tmp[0] = name[0];
539 ins_str = tmp;
540 dep = 1;
541 mark = 3;
542 break;
544 case RELATIVE_DRIVE_PATH: /* c:foo */
545 dep = 2;
546 if (wcsnicmp( name, cd->Buffer, 2 ))
548 UNICODE_STRING var, val;
550 tmp[0] = '=';
551 tmp[1] = name[0];
552 tmp[2] = ':';
553 tmp[3] = '\0';
554 var.Length = 3 * sizeof(WCHAR);
555 var.MaximumLength = 4 * sizeof(WCHAR);
556 var.Buffer = tmp;
557 val.Length = 0;
558 val.MaximumLength = size;
559 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
561 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
563 case STATUS_SUCCESS:
564 /* FIXME: Win2k seems to check that the environment variable actually points
565 * to an existing directory. If not, root of the drive is used
566 * (this seems also to be the only spot in RtlGetFullPathName that the
567 * existence of a part of a path is checked)
569 /* fall through */
570 case STATUS_BUFFER_TOO_SMALL:
571 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
572 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
573 ins_str = val.Buffer;
574 break;
575 case STATUS_VARIABLE_NOT_FOUND:
576 reqsize = 3 * sizeof(WCHAR);
577 tmp[0] = name[0];
578 tmp[1] = ':';
579 tmp[2] = '\\';
580 ins_str = tmp;
581 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
582 break;
583 default:
584 ERR("Unsupported status code\n");
585 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
586 break;
588 mark = 3;
589 break;
591 /* fall through */
593 case RELATIVE_PATH: /* foo */
594 reqsize = cd->Length;
595 ins_str = cd->Buffer;
596 if (cd->Buffer[1] != ':')
598 ptr = skip_unc_prefix( cd->Buffer );
599 mark = ptr - cd->Buffer;
601 else mark = 3;
602 break;
604 case ABSOLUTE_PATH: /* \xxx */
605 if (name[0] == '/') /* may be a Unix path */
607 char *unix_name;
608 WCHAR *nt_str;
609 ULONG buflen;
610 NTSTATUS status;
611 UNICODE_STRING str;
612 OBJECT_ATTRIBUTES attr;
614 nt_str = RtlAllocateHeap( GetProcessHeap(), 0, (wcslen(name) + 9) * sizeof(WCHAR) );
615 wcscpy( nt_str, L"\\??\\unix" );
616 wcscat( nt_str, name );
617 RtlInitUnicodeString( &str, nt_str );
618 InitializeObjectAttributes( &attr, &str, 0, 0, NULL );
619 buflen = 3 * wcslen(name) + 1;
620 unix_name = RtlAllocateHeap( GetProcessHeap(), 0, buflen );
621 status = wine_nt_to_unix_file_name( &attr, unix_name, &buflen, FILE_OPEN_IF );
622 if (!status || status == STATUS_NO_SUCH_FILE)
624 buflen = wcslen(name) + 9;
625 status = wine_unix_to_nt_file_name( unix_name, nt_str, &buflen );
627 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
628 if (!status && buflen > 6 && nt_str[5] == ':')
630 reqsize = (buflen - 4) * sizeof(WCHAR);
631 if (reqsize <= size)
633 memcpy( buffer, nt_str + 4, reqsize );
634 collapse_path( buffer, 3 );
635 reqsize -= sizeof(WCHAR);
637 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
638 goto done;
640 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
642 if (cd->Buffer[1] == ':')
644 reqsize = 2 * sizeof(WCHAR);
645 tmp[0] = cd->Buffer[0];
646 tmp[1] = ':';
647 ins_str = tmp;
648 mark = 3;
650 else
652 ptr = skip_unc_prefix( cd->Buffer );
653 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
654 mark = reqsize / sizeof(WCHAR);
655 ins_str = cd->Buffer;
657 break;
659 case UNC_DOT_PATH: /* \\. */
660 reqsize = 4 * sizeof(WCHAR);
661 dep = 3;
662 tmp[0] = '\\';
663 tmp[1] = '\\';
664 tmp[2] = '.';
665 tmp[3] = '\\';
666 ins_str = tmp;
667 mark = 4;
668 break;
670 case INVALID_PATH:
671 goto done;
674 /* enough space ? */
675 deplen = wcslen(name + dep) * sizeof(WCHAR);
676 if (reqsize + deplen + sizeof(WCHAR) > size)
678 /* not enough space, return need size (including terminating '\0') */
679 reqsize += deplen + sizeof(WCHAR);
680 goto done;
683 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
684 if (reqsize) memcpy(buffer, ins_str, reqsize);
686 if (ins_str != tmp && ins_str != cd->Buffer)
687 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
689 collapse_path( buffer, mark );
690 reqsize = wcslen(buffer) * sizeof(WCHAR);
692 done:
693 RtlReleasePebLock();
694 return reqsize;
697 /******************************************************************
698 * RtlGetFullPathName_U (NTDLL.@)
700 * Returns the number of bytes written to buffer (not including the
701 * terminating NULL) if the function succeeds, or the required number of bytes
702 * (including the terminating NULL) if the buffer is too small.
704 * file_part will point to the filename part inside buffer (except if we use
705 * DOS device name, in which case file_in_buf is NULL)
708 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
709 WCHAR** file_part)
711 WCHAR* ptr;
712 DWORD dosdev;
713 DWORD reqsize;
715 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
717 if (!name || !*name) return 0;
719 if (file_part) *file_part = NULL;
721 /* check for DOS device name */
722 dosdev = RtlIsDosDeviceName_U(name);
723 if (dosdev)
725 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
726 DWORD sz = LOWORD(dosdev); /* in bytes */
728 if (8 + sz + 2 > size) return sz + 10;
729 wcscpy(buffer, L"\\\\.\\");
730 memmove(buffer + 4, name + offset, sz);
731 buffer[4 + sz / sizeof(WCHAR)] = '\0';
732 /* file_part isn't set in this case */
733 return sz + 8;
736 reqsize = get_full_path_helper(name, buffer, size);
737 if (!reqsize) return 0;
738 if (reqsize > size)
740 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
741 reqsize = get_full_path_helper(name, tmp, reqsize);
742 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
744 RtlFreeHeap(GetProcessHeap(), 0, tmp);
745 return reqsize + sizeof(WCHAR);
747 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
748 RtlFreeHeap(GetProcessHeap(), 0, tmp);
751 /* find file part */
752 if (file_part && (ptr = wcsrchr(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
753 *file_part = ptr;
754 return reqsize;
757 /*************************************************************************
758 * RtlGetLongestNtPathLength [NTDLL.@]
760 * Get the longest allowed path length
762 * PARAMS
763 * None.
765 * RETURNS
766 * The longest allowed path length (277 characters under Win2k).
768 DWORD WINAPI RtlGetLongestNtPathLength(void)
770 return MAX_NT_PATH_LENGTH;
774 /******************************************************************
775 * RtlDoesFileExists_U (NTDLL.@)
777 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
779 UNICODE_STRING nt_name;
780 FILE_BASIC_INFORMATION basic_info;
781 OBJECT_ATTRIBUTES attr;
782 NTSTATUS status;
784 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
785 InitializeObjectAttributes( &attr, &nt_name, OBJ_CASE_INSENSITIVE, 0, NULL );
786 status = NtQueryAttributesFile(&attr, &basic_info);
787 RtlFreeUnicodeString( &nt_name );
788 return !status;
792 /******************************************************************
793 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
795 * Returns TRUE iff unicode is a valid DOS (8+3) name.
796 * If the name is valid, oem gets filled with the corresponding OEM string
797 * spaces is set to TRUE if unicode contains spaces
799 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
800 OEM_STRING *oem, BOOLEAN *spaces )
802 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
803 int dot = -1;
804 int i;
805 char buffer[12];
806 OEM_STRING oem_str;
807 BOOLEAN got_space = FALSE;
809 if (!oem)
811 oem_str.Length = sizeof(buffer);
812 oem_str.MaximumLength = sizeof(buffer);
813 oem_str.Buffer = buffer;
814 oem = &oem_str;
816 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
817 return FALSE;
819 if (oem->Length > 12) return FALSE;
821 /* a starting . is invalid, except for . and .. */
822 if (oem->Length > 0 && oem->Buffer[0] == '.')
824 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
825 if (spaces) *spaces = FALSE;
826 return TRUE;
829 for (i = 0; i < oem->Length; i++)
831 switch (oem->Buffer[i])
833 case ' ':
834 /* leading/trailing spaces not allowed */
835 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
836 got_space = TRUE;
837 break;
838 case '.':
839 if (dot != -1) return FALSE;
840 dot = i;
841 break;
842 default:
843 if (strchr(illegal, oem->Buffer[i])) return FALSE;
844 break;
847 /* check file part is shorter than 8, extension shorter than 3
848 * dot cannot be last in string
850 if (dot == -1)
852 if (oem->Length > 8) return FALSE;
854 else
856 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
858 if (spaces) *spaces = got_space;
859 return TRUE;
862 /******************************************************************
863 * RtlGetCurrentDirectory_U (NTDLL.@)
866 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
868 UNICODE_STRING* us;
869 ULONG len;
871 TRACE("(%u %p)\n", buflen, buf);
873 RtlAcquirePebLock();
875 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
876 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
877 else
878 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
880 len = us->Length / sizeof(WCHAR);
881 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
882 len--;
884 if (buflen / sizeof(WCHAR) > len)
886 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
887 buf[len] = '\0';
889 else
891 len++;
894 RtlReleasePebLock();
896 return len * sizeof(WCHAR);
899 /******************************************************************
900 * RtlSetCurrentDirectory_U (NTDLL.@)
903 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
905 FILE_FS_DEVICE_INFORMATION device_info;
906 OBJECT_ATTRIBUTES attr;
907 UNICODE_STRING newdir;
908 IO_STATUS_BLOCK io;
909 CURDIR *curdir;
910 HANDLE handle;
911 NTSTATUS nts;
912 ULONG size;
913 PWSTR ptr;
915 newdir.Buffer = NULL;
917 RtlAcquirePebLock();
919 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
920 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
921 else
922 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
924 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
926 nts = STATUS_OBJECT_NAME_INVALID;
927 goto out;
930 attr.Length = sizeof(attr);
931 attr.RootDirectory = 0;
932 attr.Attributes = OBJ_CASE_INSENSITIVE;
933 attr.ObjectName = &newdir;
934 attr.SecurityDescriptor = NULL;
935 attr.SecurityQualityOfService = NULL;
937 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
938 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
939 if (nts != STATUS_SUCCESS) goto out;
941 /* don't keep the directory handle open on removable media */
942 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
943 sizeof(device_info), FileFsDeviceInformation ) &&
944 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
946 NtClose( handle );
947 handle = 0;
950 if (curdir->Handle) NtClose( curdir->Handle );
951 curdir->Handle = handle;
953 /* append trailing \ if missing */
954 size = newdir.Length / sizeof(WCHAR);
955 ptr = newdir.Buffer;
956 ptr += 4; /* skip \??\ prefix */
957 size -= 4;
958 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
960 /* convert \??\UNC\ path to \\ prefix */
961 if (size >= 4 && !wcsnicmp(ptr, L"UNC\\", 4))
963 ptr += 2;
964 size -= 2;
965 *ptr = '\\';
968 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
969 curdir->DosPath.Buffer[size] = 0;
970 curdir->DosPath.Length = size * sizeof(WCHAR);
972 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
974 out:
975 RtlFreeUnicodeString( &newdir );
976 RtlReleasePebLock();
977 return nts;