wineps: Continue printing if path drawing function returns error.
[wine.git] / dlls / ntdll / path.c
blob8956ff07f6c1cb4bb0f1eac98c40ec6bd5fc883a
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 existing 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]) ||
201 !wcsicmp( dos_path, L"\\\\.\\CON" ))
203 ntpath->Length = wcslen(dos_path) * sizeof(WCHAR);
204 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
205 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
206 if (!ntpath->Buffer) return STATUS_NO_MEMORY;
207 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
208 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
209 ntpath->Buffer[2] = '?';
210 if (file_part)
212 if ((ptr = wcsrchr( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
213 else *file_part = NULL;
215 return STATUS_SUCCESS;
218 dosdev = RtlIsDosDeviceName_U(dos_path);
219 if ((offset = HIWORD(dosdev)))
221 sz = offset + sizeof(WCHAR);
223 if (sz > sizeof(local) &&
224 (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))))
225 return STATUS_NO_MEMORY;
227 memcpy(ptr, dos_path, offset);
228 ptr[offset/sizeof(WCHAR)] = '\0';
230 if (!is_valid_directory(ptr))
232 nts = STATUS_OBJECT_NAME_INVALID;
233 goto out;
236 if (file_part) *file_part = NULL;
238 sz = LOWORD(dosdev);
240 wcscpy(ptr, L"\\\\.\\");
241 memcpy(ptr + 4, dos_path + offset / sizeof(WCHAR), sz);
242 ptr[4 + sz / sizeof(WCHAR)] = '\0';
243 sz += 4 * sizeof(WCHAR);
245 else
247 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
248 if (sz == 0) return STATUS_OBJECT_NAME_INVALID;
250 if (sz > sizeof(local))
252 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return STATUS_NO_MEMORY;
253 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
256 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
257 if (sz > MAXWORD)
259 nts = STATUS_OBJECT_NAME_INVALID;
260 goto out;
263 ntpath->MaximumLength = sz;
264 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
265 if (!ntpath->Buffer)
267 nts = STATUS_NO_MEMORY;
268 goto out;
271 wcscpy(ntpath->Buffer, L"\\??\\");
272 switch (RtlDetermineDosPathNameType_U(ptr))
274 case UNC_PATH: /* \\foo */
275 offset = 2;
276 wcscat(ntpath->Buffer, L"UNC\\");
277 break;
278 case DEVICE_PATH: /* \\.\foo */
279 offset = 4;
280 break;
281 default:
282 offset = 0;
283 break;
286 wcscat(ntpath->Buffer, ptr + offset);
287 ntpath->Length = wcslen(ntpath->Buffer) * sizeof(WCHAR);
289 if (file_part && *file_part)
290 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - wcslen(*file_part);
292 /* FIXME: cd filling */
294 out:
295 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
296 return nts;
299 /**************************************************************************
300 * RtlDosPathNameToNtPathName_U [NTDLL.@]
302 * See RtlDosPathNameToNtPathName_U_WithStatus
304 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
305 PUNICODE_STRING ntpath,
306 PWSTR* file_part,
307 CURDIR* cd)
309 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, cd) == STATUS_SUCCESS;
312 /**************************************************************************
313 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
315 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
317 NTSTATUS WINAPI RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR *dos_path,
318 UNICODE_STRING *ntpath, WCHAR **file_part, RTL_RELATIVE_NAME *relative)
320 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, relative);
322 if (relative)
324 FIXME("Unsupported parameter\n");
325 memset(relative, 0, sizeof(*relative));
328 /* FIXME: fill parameter relative */
330 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, NULL);
333 /**************************************************************************
334 * RtlDosPathNameToRelativeNtPathName_U [NTDLL.@]
336 BOOLEAN WINAPI RtlDosPathNameToRelativeNtPathName_U(const WCHAR *dos_path, UNICODE_STRING *ntpath,
337 WCHAR **file_part, RTL_RELATIVE_NAME *relative)
339 return RtlDosPathNameToRelativeNtPathName_U_WithStatus(dos_path, ntpath, file_part, relative) == STATUS_SUCCESS;
342 /**************************************************************************
343 * RtlReleaseRelativeName [NTDLL.@]
345 void WINAPI RtlReleaseRelativeName(RTL_RELATIVE_NAME *relative)
349 /******************************************************************
350 * RtlDosSearchPath_U
352 * Searches a file of name 'name' into a ';' separated list of paths
353 * (stored in paths)
354 * Doesn't seem to search elsewhere than the paths list
355 * Stores the result in buffer (file_part will point to the position
356 * of the file name in the buffer)
357 * FIXME:
358 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
360 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
361 ULONG buffer_size, LPWSTR buffer,
362 LPWSTR* file_part)
364 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
365 ULONG len = 0;
367 if (type == RELATIVE_PATH)
369 ULONG allocated = 0, needed, filelen;
370 WCHAR *name = NULL;
372 filelen = 1 /* for \ */ + wcslen(search) + 1 /* \0 */;
374 /* Windows only checks for '.' without worrying about path components */
375 if (wcschr( search, '.' )) ext = NULL;
376 if (ext != NULL) filelen += wcslen(ext);
378 while (*paths)
380 LPCWSTR ptr;
382 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
383 if (needed + filelen > allocated)
385 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
386 (needed + filelen) * sizeof(WCHAR));
387 else
389 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
390 (needed + filelen) * sizeof(WCHAR));
391 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
392 name = newname;
394 if (!name) return 0;
395 allocated = needed + filelen;
397 memmove(name, paths, needed * sizeof(WCHAR));
398 /* append '\\' if none is present */
399 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
400 wcscpy(&name[needed], search);
401 if (ext) wcscat(&name[needed], ext);
402 if (RtlDoesFileExists_U(name))
404 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
405 break;
407 paths = ptr;
409 RtlFreeHeap(GetProcessHeap(), 0, name);
411 else if (RtlDoesFileExists_U(search))
413 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
416 return len;
420 /******************************************************************
421 * collapse_path
423 * Helper for RtlGetFullPathName_U.
424 * Get rid of . and .. components in the path.
426 static inline void collapse_path( WCHAR *path, UINT mark )
428 WCHAR *p, *next;
430 /* convert every / into a \ */
431 for (p = path; *p; p++) if (*p == '/') *p = '\\';
433 /* collapse duplicate backslashes */
434 next = path + max( 1, mark );
435 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
436 *next = 0;
438 p = path + mark;
439 while (*p)
441 if (*p == '.')
443 switch(p[1])
445 case '\\': /* .\ component */
446 next = p + 2;
447 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
448 continue;
449 case 0: /* final . */
450 if (p > path + mark) p--;
451 *p = 0;
452 continue;
453 case '.':
454 if (p[2] == '\\') /* ..\ component */
456 next = p + 3;
457 if (p > path + mark)
459 p--;
460 while (p > path + mark && p[-1] != '\\') p--;
462 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
463 continue;
465 else if (!p[2]) /* final .. */
467 if (p > path + mark)
469 p--;
470 while (p > path + mark && p[-1] != '\\') p--;
471 if (p > path + mark) p--;
473 *p = 0;
474 continue;
476 break;
479 /* skip to the next component */
480 while (*p && *p != '\\') p++;
481 if (*p == '\\')
483 /* remove last dot in previous dir name */
484 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (wcslen(p) + 1) * sizeof(WCHAR) );
485 else p++;
489 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
490 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
491 *p = 0;
495 /******************************************************************
496 * skip_unc_prefix
498 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
500 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
502 ptr += 2;
503 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
504 while (IS_SEPARATOR(*ptr)) ptr++;
505 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
506 while (IS_SEPARATOR(*ptr)) ptr++;
507 return ptr;
511 /******************************************************************
512 * get_full_path_helper
514 * Helper for RtlGetFullPathName_U
515 * Note: name and buffer are allowed to point to the same memory spot
517 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
519 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
520 LPWSTR ins_str = NULL;
521 LPCWSTR ptr;
522 const UNICODE_STRING* cd;
523 WCHAR tmp[4];
525 /* return error if name only consists of spaces */
526 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
527 if (!*ptr) return 0;
529 RtlAcquirePebLock();
531 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
532 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
533 else
534 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
536 switch (RtlDetermineDosPathNameType_U(name))
538 case UNC_PATH: /* \\foo */
539 ptr = skip_unc_prefix( name );
540 mark = (ptr - name);
541 break;
543 case DEVICE_PATH: /* \\.\foo */
544 mark = 4;
545 break;
547 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
548 reqsize = sizeof(WCHAR);
549 tmp[0] = name[0];
550 ins_str = tmp;
551 dep = 1;
552 mark = 3;
553 break;
555 case RELATIVE_DRIVE_PATH: /* c:foo */
556 dep = 2;
557 if (wcsnicmp( name, cd->Buffer, 2 ))
559 UNICODE_STRING var, val;
561 tmp[0] = '=';
562 tmp[1] = name[0];
563 tmp[2] = ':';
564 tmp[3] = '\0';
565 var.Length = 3 * sizeof(WCHAR);
566 var.MaximumLength = 4 * sizeof(WCHAR);
567 var.Buffer = tmp;
568 val.Length = 0;
569 val.MaximumLength = size;
570 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
572 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
574 case STATUS_SUCCESS:
575 /* FIXME: Win2k seems to check that the environment variable actually points
576 * to an existing directory. If not, root of the drive is used
577 * (this seems also to be the only spot in RtlGetFullPathName that the
578 * existence of a part of a path is checked)
580 /* fall through */
581 case STATUS_BUFFER_TOO_SMALL:
582 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
583 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
584 ins_str = val.Buffer;
585 break;
586 case STATUS_VARIABLE_NOT_FOUND:
587 reqsize = 3 * sizeof(WCHAR);
588 tmp[0] = name[0];
589 tmp[1] = ':';
590 tmp[2] = '\\';
591 ins_str = tmp;
592 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
593 break;
594 default:
595 ERR("Unsupported status code\n");
596 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
597 break;
599 mark = 3;
600 break;
602 /* fall through */
604 case RELATIVE_PATH: /* foo */
605 reqsize = cd->Length;
606 ins_str = cd->Buffer;
607 if (cd->Buffer[1] != ':')
609 ptr = skip_unc_prefix( cd->Buffer );
610 mark = ptr - cd->Buffer;
612 else mark = 3;
613 break;
615 case ABSOLUTE_PATH: /* \xxx */
616 if (name[0] == '/') /* may be a Unix path */
618 char *unix_name;
619 WCHAR *nt_str;
620 ULONG buflen;
621 NTSTATUS status;
622 UNICODE_STRING str;
623 OBJECT_ATTRIBUTES attr;
625 nt_str = RtlAllocateHeap( GetProcessHeap(), 0, (wcslen(name) + 9) * sizeof(WCHAR) );
626 wcscpy( nt_str, L"\\??\\unix" );
627 wcscat( nt_str, name );
628 RtlInitUnicodeString( &str, nt_str );
629 InitializeObjectAttributes( &attr, &str, 0, 0, NULL );
630 buflen = 3 * wcslen(name) + 1;
631 unix_name = RtlAllocateHeap( GetProcessHeap(), 0, buflen );
632 status = wine_nt_to_unix_file_name( &attr, unix_name, &buflen, FILE_OPEN_IF );
633 if (!status || status == STATUS_NO_SUCH_FILE)
635 buflen = wcslen(name) + 9;
636 status = wine_unix_to_nt_file_name( unix_name, nt_str, &buflen );
638 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
639 if (!status && buflen > 6 && nt_str[5] == ':')
641 reqsize = (buflen - 4) * sizeof(WCHAR);
642 if (reqsize <= size)
644 memcpy( buffer, nt_str + 4, reqsize );
645 collapse_path( buffer, 3 );
646 reqsize -= sizeof(WCHAR);
648 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
649 goto done;
651 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
653 if (cd->Buffer[1] == ':')
655 reqsize = 2 * sizeof(WCHAR);
656 tmp[0] = cd->Buffer[0];
657 tmp[1] = ':';
658 ins_str = tmp;
659 mark = 3;
661 else
663 ptr = skip_unc_prefix( cd->Buffer );
664 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
665 mark = reqsize / sizeof(WCHAR);
666 ins_str = cd->Buffer;
668 break;
670 case UNC_DOT_PATH: /* \\. */
671 reqsize = 4 * sizeof(WCHAR);
672 dep = 3;
673 tmp[0] = '\\';
674 tmp[1] = '\\';
675 tmp[2] = '.';
676 tmp[3] = '\\';
677 ins_str = tmp;
678 mark = 4;
679 break;
681 case INVALID_PATH:
682 goto done;
685 /* enough space ? */
686 deplen = wcslen(name + dep) * sizeof(WCHAR);
687 if (reqsize + deplen + sizeof(WCHAR) > size)
689 /* not enough space, return need size (including terminating '\0') */
690 reqsize += deplen + sizeof(WCHAR);
691 goto done;
694 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
695 if (reqsize) memcpy(buffer, ins_str, reqsize);
697 if (ins_str != tmp && ins_str != cd->Buffer)
698 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
700 collapse_path( buffer, mark );
701 reqsize = wcslen(buffer) * sizeof(WCHAR);
703 done:
704 RtlReleasePebLock();
705 return reqsize;
708 /******************************************************************
709 * RtlGetFullPathName_U (NTDLL.@)
711 * Returns the number of bytes written to buffer (not including the
712 * terminating NULL) if the function succeeds, or the required number of bytes
713 * (including the terminating NULL) if the buffer is too small.
715 * file_part will point to the filename part inside buffer (except if we use
716 * DOS device name, in which case file_in_buf is NULL)
719 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
720 WCHAR** file_part)
722 WCHAR* ptr;
723 DWORD dosdev;
724 DWORD reqsize;
726 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
728 if (!name || !*name) return 0;
730 if (file_part) *file_part = NULL;
732 /* check for DOS device name */
733 dosdev = RtlIsDosDeviceName_U(name);
734 if (dosdev)
736 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
737 DWORD sz = LOWORD(dosdev); /* in bytes */
739 if (8 + sz + 2 > size) return sz + 10;
740 wcscpy(buffer, L"\\\\.\\");
741 memmove(buffer + 4, name + offset, sz);
742 buffer[4 + sz / sizeof(WCHAR)] = '\0';
743 /* file_part isn't set in this case */
744 return sz + 8;
747 reqsize = get_full_path_helper(name, buffer, size);
748 if (!reqsize) return 0;
749 if (reqsize > size)
751 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
752 reqsize = get_full_path_helper(name, tmp, reqsize);
753 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
755 RtlFreeHeap(GetProcessHeap(), 0, tmp);
756 return reqsize + sizeof(WCHAR);
758 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
759 RtlFreeHeap(GetProcessHeap(), 0, tmp);
762 /* find file part */
763 if (file_part && (ptr = wcsrchr(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
764 *file_part = ptr;
765 return reqsize;
768 /*************************************************************************
769 * RtlGetLongestNtPathLength [NTDLL.@]
771 * Get the longest allowed path length
773 * PARAMS
774 * None.
776 * RETURNS
777 * The longest allowed path length (277 characters under Win2k).
779 DWORD WINAPI RtlGetLongestNtPathLength(void)
781 return MAX_NT_PATH_LENGTH;
785 /******************************************************************
786 * RtlDoesFileExists_U (NTDLL.@)
788 BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
790 UNICODE_STRING nt_name;
791 FILE_BASIC_INFORMATION basic_info;
792 OBJECT_ATTRIBUTES attr;
793 NTSTATUS status;
795 if (!RtlDosPathNameToNtPathName_U( file_name, &nt_name, NULL, NULL )) return FALSE;
796 InitializeObjectAttributes( &attr, &nt_name, OBJ_CASE_INSENSITIVE, 0, NULL );
797 status = NtQueryAttributesFile(&attr, &basic_info);
798 RtlFreeUnicodeString( &nt_name );
799 return !status;
803 /******************************************************************
804 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
806 * Returns TRUE iff unicode is a valid DOS (8+3) name.
807 * If the name is valid, oem gets filled with the corresponding OEM string
808 * spaces is set to TRUE if unicode contains spaces
810 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
811 OEM_STRING *oem, BOOLEAN *spaces )
813 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
814 int dot = -1;
815 int i;
816 char buffer[12];
817 OEM_STRING oem_str;
818 BOOLEAN got_space = FALSE;
820 if (!oem)
822 oem_str.Length = sizeof(buffer);
823 oem_str.MaximumLength = sizeof(buffer);
824 oem_str.Buffer = buffer;
825 oem = &oem_str;
827 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
828 return FALSE;
830 if (oem->Length > 12) return FALSE;
832 /* a starting . is invalid, except for . and .. */
833 if (oem->Length > 0 && oem->Buffer[0] == '.')
835 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
836 if (spaces) *spaces = FALSE;
837 return TRUE;
840 for (i = 0; i < oem->Length; i++)
842 switch (oem->Buffer[i])
844 case ' ':
845 /* leading/trailing spaces not allowed */
846 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
847 got_space = TRUE;
848 break;
849 case '.':
850 if (dot != -1) return FALSE;
851 dot = i;
852 break;
853 default:
854 if (strchr(illegal, oem->Buffer[i])) return FALSE;
855 break;
858 /* check file part is shorter than 8, extension shorter than 3
859 * dot cannot be last in string
861 if (dot == -1)
863 if (oem->Length > 8) return FALSE;
865 else
867 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
869 if (spaces) *spaces = got_space;
870 return TRUE;
873 /******************************************************************
874 * RtlGetCurrentDirectory_U (NTDLL.@)
877 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
879 UNICODE_STRING* us;
880 ULONG len;
882 TRACE("(%lu %p)\n", buflen, buf);
884 RtlAcquirePebLock();
886 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
887 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
888 else
889 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
891 len = us->Length / sizeof(WCHAR);
892 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
893 len--;
895 if (buflen / sizeof(WCHAR) > len)
897 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
898 buf[len] = '\0';
900 else
902 len++;
905 RtlReleasePebLock();
907 return len * sizeof(WCHAR);
910 /******************************************************************
911 * RtlSetCurrentDirectory_U (NTDLL.@)
914 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
916 FILE_FS_DEVICE_INFORMATION device_info;
917 OBJECT_ATTRIBUTES attr;
918 UNICODE_STRING newdir;
919 IO_STATUS_BLOCK io;
920 CURDIR *curdir;
921 HANDLE handle;
922 NTSTATUS nts;
923 ULONG size;
924 PWSTR ptr;
926 newdir.Buffer = NULL;
928 RtlAcquirePebLock();
930 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
931 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
932 else
933 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
935 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
937 nts = STATUS_OBJECT_NAME_INVALID;
938 goto out;
941 attr.Length = sizeof(attr);
942 attr.RootDirectory = 0;
943 attr.Attributes = OBJ_CASE_INSENSITIVE;
944 attr.ObjectName = &newdir;
945 attr.SecurityDescriptor = NULL;
946 attr.SecurityQualityOfService = NULL;
948 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
949 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
950 if (nts != STATUS_SUCCESS) goto out;
952 /* don't keep the directory handle open on removable media */
953 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
954 sizeof(device_info), FileFsDeviceInformation ) &&
955 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
957 NtClose( handle );
958 handle = 0;
961 if (curdir->Handle) NtClose( curdir->Handle );
962 curdir->Handle = handle;
964 /* append trailing \ if missing */
965 size = newdir.Length / sizeof(WCHAR);
966 ptr = newdir.Buffer;
967 ptr += 4; /* skip \??\ prefix */
968 size -= 4;
969 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
971 /* convert \??\UNC\ path to \\ prefix */
972 if (size >= 4 && !wcsnicmp(ptr, L"UNC\\", 4))
974 ptr += 2;
975 size -= 2;
976 *ptr = '\\';
979 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
980 curdir->DosPath.Buffer[size] = 0;
981 curdir->DosPath.Length = size * sizeof(WCHAR);
983 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
985 out:
986 RtlFreeUnicodeString( &newdir );
987 RtlReleasePebLock();
988 return nts;