msvcrt: Use fpclass constants from public header.
[wine/zf.git] / dlls / ntdll / path.c
blob53470b4f3573a9a8342bda7965f123b5bfd514f3
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 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
35 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
36 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
38 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
40 /***********************************************************************
41 * RtlDetermineDosPathNameType_U (NTDLL.@)
43 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
45 if (IS_SEPARATOR(path[0]))
47 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
48 if (path[2] != '.' && path[2] != '?') return UNC_PATH; /* "//foo" */
49 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" or "//?/foo" */
50 if (path[3]) return UNC_PATH; /* "//.foo" or "//?foo" */
51 return UNC_DOT_PATH; /* "//." or "//?" */
53 else
55 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
56 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
57 return RELATIVE_DRIVE_PATH; /* "c:foo" */
61 /***********************************************************************
62 * RtlIsDosDeviceName_U (NTDLL.@)
64 * Check if the given DOS path contains a DOS device name.
66 * Returns the length of the device name in the low word and its
67 * position in the high word (both in bytes, not WCHARs), or 0 if no
68 * device name is found.
70 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
72 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
73 static const WCHAR auxW[] = {'A','U','X'};
74 static const WCHAR comW[] = {'C','O','M'};
75 static const WCHAR conW[] = {'C','O','N'};
76 static const WCHAR lptW[] = {'L','P','T'};
77 static const WCHAR nulW[] = {'N','U','L'};
78 static const WCHAR prnW[] = {'P','R','N'};
79 static const WCHAR coninW[] = {'C','O','N','I','N','$'};
80 static const WCHAR conoutW[] = {'C','O','N','O','U','T','$'};
82 const WCHAR *start, *end, *p;
84 switch(RtlDetermineDosPathNameType_U( dos_name ))
86 case INVALID_PATH:
87 case UNC_PATH:
88 return 0;
89 case DEVICE_PATH:
90 if (!wcsicmp( dos_name, consoleW ))
91 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
92 return 0;
93 case ABSOLUTE_DRIVE_PATH:
94 case RELATIVE_DRIVE_PATH:
95 start = dos_name + 2; /* skip drive letter */
96 break;
97 default:
98 start = dos_name;
99 break;
102 /* find start of file name */
103 for (p = start; *p; p++) if (IS_SEPARATOR(*p)) start = p + 1;
105 /* truncate at extension and ':' */
106 for (end = start; *end; end++) if (*end == '.' || *end == ':') break;
107 end--;
109 /* remove trailing spaces */
110 while (end >= start && *end == ' ') end--;
112 /* now we have a potential device name between start and end, check it */
113 switch(end - start + 1)
115 case 3:
116 if (wcsnicmp( start, auxW, 3 ) &&
117 wcsnicmp( start, conW, 3 ) &&
118 wcsnicmp( start, nulW, 3 ) &&
119 wcsnicmp( start, prnW, 3 )) break;
120 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
121 case 4:
122 if (wcsnicmp( start, comW, 3 ) && wcsnicmp( start, lptW, 3 )) break;
123 if (*end <= '0' || *end > '9') break;
124 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
125 case 6:
126 if (wcsnicmp( start, coninW, ARRAY_SIZE(coninW) )) break;
127 return MAKELONG( sizeof(coninW), (start - dos_name) * sizeof(WCHAR) );
128 case 7:
129 if (wcsnicmp( start, conoutW, ARRAY_SIZE(conoutW) )) break;
130 return MAKELONG( sizeof(conoutW), (start - dos_name) * sizeof(WCHAR) );
131 default: /* can't match anything */
132 break;
134 return 0;
137 /**************************************************************************
138 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
140 * dos_path: a DOS path name (fully qualified or not)
141 * ntpath: pointer to a UNICODE_STRING to hold the converted
142 * path name
143 * file_part:will point (in ntpath) to the file part in the path
144 * cd: directory reference (optional)
146 * FIXME:
147 * + fill the cd structure
149 NTSTATUS WINAPI RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR *dos_path, UNICODE_STRING *ntpath,
150 WCHAR **file_part, CURDIR *cd)
152 static const WCHAR global_prefix[] = {'\\','\\','?','\\'};
153 static const WCHAR global_prefix2[] = {'\\','?','?','\\'};
154 ULONG sz, offset;
155 WCHAR local[MAX_PATH];
156 LPWSTR ptr;
158 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, cd);
160 if (cd)
162 FIXME("Unsupported parameter\n");
163 memset(cd, 0, sizeof(*cd));
166 if (!dos_path || !*dos_path)
167 return STATUS_OBJECT_NAME_INVALID;
169 if (!memcmp(dos_path, global_prefix, sizeof(global_prefix)) ||
170 (!memcmp(dos_path, global_prefix2, sizeof(global_prefix2)) && dos_path[4]))
172 ntpath->Length = wcslen(dos_path) * sizeof(WCHAR);
173 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
174 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
175 if (!ntpath->Buffer) return STATUS_NO_MEMORY;
176 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
177 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
178 if (file_part)
180 if ((ptr = wcsrchr( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
181 else *file_part = NULL;
183 return STATUS_SUCCESS;
186 ptr = local;
187 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
188 if (sz == 0) return STATUS_OBJECT_NAME_INVALID;
190 if (sz > sizeof(local))
192 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return STATUS_NO_MEMORY;
193 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
195 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
196 if (sz > MAXWORD)
198 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
199 return STATUS_OBJECT_NAME_INVALID;
202 ntpath->MaximumLength = sz;
203 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
204 if (!ntpath->Buffer)
206 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
207 return STATUS_NO_MEMORY;
210 wcscpy(ntpath->Buffer, NTDosPrefixW);
211 switch (RtlDetermineDosPathNameType_U(ptr))
213 case UNC_PATH: /* \\foo */
214 offset = 2;
215 wcscat(ntpath->Buffer, UncPfxW);
216 break;
217 case DEVICE_PATH: /* \\.\foo */
218 offset = 4;
219 break;
220 default:
221 offset = 0;
222 break;
225 wcscat(ntpath->Buffer, ptr + offset);
226 ntpath->Length = wcslen(ntpath->Buffer) * sizeof(WCHAR);
228 if (file_part && *file_part)
229 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - wcslen(*file_part);
231 /* FIXME: cd filling */
233 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
234 return STATUS_SUCCESS;
237 /**************************************************************************
238 * RtlDosPathNameToNtPathName_U [NTDLL.@]
240 * See RtlDosPathNameToNtPathName_U_WithStatus
242 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
243 PUNICODE_STRING ntpath,
244 PWSTR* file_part,
245 CURDIR* cd)
247 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, cd) == STATUS_SUCCESS;
250 /**************************************************************************
251 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
253 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
255 NTSTATUS WINAPI RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR *dos_path,
256 UNICODE_STRING *ntpath, WCHAR **file_part, RTL_RELATIVE_NAME *relative)
258 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, relative);
260 if (relative)
262 FIXME("Unsupported parameter\n");
263 memset(relative, 0, sizeof(*relative));
266 /* FIXME: fill parameter relative */
268 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, NULL);
271 /**************************************************************************
272 * RtlReleaseRelativeName [NTDLL.@]
274 void WINAPI RtlReleaseRelativeName(RTL_RELATIVE_NAME *relative)
278 /******************************************************************
279 * RtlDosSearchPath_U
281 * Searches a file of name 'name' into a ';' separated list of paths
282 * (stored in paths)
283 * Doesn't seem to search elsewhere than the paths list
284 * Stores the result in buffer (file_part will point to the position
285 * of the file name in the buffer)
286 * FIXME:
287 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
289 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
290 ULONG buffer_size, LPWSTR buffer,
291 LPWSTR* file_part)
293 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
294 ULONG len = 0;
296 if (type == RELATIVE_PATH)
298 ULONG allocated = 0, needed, filelen;
299 WCHAR *name = NULL;
301 filelen = 1 /* for \ */ + wcslen(search) + 1 /* \0 */;
303 /* Windows only checks for '.' without worrying about path components */
304 if (wcschr( search, '.' )) ext = NULL;
305 if (ext != NULL) filelen += wcslen(ext);
307 while (*paths)
309 LPCWSTR ptr;
311 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
312 if (needed + filelen > allocated)
314 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
315 (needed + filelen) * sizeof(WCHAR));
316 else
318 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
319 (needed + filelen) * sizeof(WCHAR));
320 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
321 name = newname;
323 if (!name) return 0;
324 allocated = needed + filelen;
326 memmove(name, paths, needed * sizeof(WCHAR));
327 /* append '\\' if none is present */
328 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
329 wcscpy(&name[needed], search);
330 if (ext) wcscat(&name[needed], ext);
331 if (RtlDoesFileExists_U(name))
333 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
334 break;
336 paths = ptr;
338 RtlFreeHeap(GetProcessHeap(), 0, name);
340 else if (RtlDoesFileExists_U(search))
342 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
345 return len;
349 /******************************************************************
350 * collapse_path
352 * Helper for RtlGetFullPathName_U.
353 * Get rid of . and .. components in the path.
355 static inline void collapse_path( WCHAR *path, UINT mark )
357 WCHAR *p, *next;
359 /* convert every / into a \ */
360 for (p = path; *p; p++) if (*p == '/') *p = '\\';
362 /* collapse duplicate backslashes */
363 next = path + max( 1, mark );
364 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
365 *next = 0;
367 p = path + mark;
368 while (*p)
370 if (*p == '.')
372 switch(p[1])
374 case '\\': /* .\ component */
375 next = p + 2;
376 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
377 continue;
378 case 0: /* final . */
379 if (p > path + mark) p--;
380 *p = 0;
381 continue;
382 case '.':
383 if (p[2] == '\\') /* ..\ component */
385 next = p + 3;
386 if (p > path + mark)
388 p--;
389 while (p > path + mark && p[-1] != '\\') p--;
391 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
392 continue;
394 else if (!p[2]) /* final .. */
396 if (p > path + mark)
398 p--;
399 while (p > path + mark && p[-1] != '\\') p--;
400 if (p > path + mark) p--;
402 *p = 0;
403 continue;
405 break;
408 /* skip to the next component */
409 while (*p && *p != '\\') p++;
410 if (*p == '\\')
412 /* remove last dot in previous dir name */
413 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (wcslen(p) + 1) * sizeof(WCHAR) );
414 else p++;
418 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
419 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
420 *p = 0;
424 /******************************************************************
425 * skip_unc_prefix
427 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
429 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
431 ptr += 2;
432 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
433 while (IS_SEPARATOR(*ptr)) ptr++;
434 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
435 while (IS_SEPARATOR(*ptr)) ptr++;
436 return ptr;
440 /******************************************************************
441 * get_full_path_helper
443 * Helper for RtlGetFullPathName_U
444 * Note: name and buffer are allowed to point to the same memory spot
446 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
448 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
449 LPWSTR ins_str = NULL;
450 LPCWSTR ptr;
451 const UNICODE_STRING* cd;
452 WCHAR tmp[4];
454 /* return error if name only consists of spaces */
455 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
456 if (!*ptr) return 0;
458 RtlAcquirePebLock();
460 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
461 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
462 else
463 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
465 switch (RtlDetermineDosPathNameType_U(name))
467 case UNC_PATH: /* \\foo */
468 ptr = skip_unc_prefix( name );
469 mark = (ptr - name);
470 break;
472 case DEVICE_PATH: /* \\.\foo */
473 mark = 4;
474 break;
476 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
477 reqsize = sizeof(WCHAR);
478 tmp[0] = name[0];
479 ins_str = tmp;
480 dep = 1;
481 mark = 3;
482 break;
484 case RELATIVE_DRIVE_PATH: /* c:foo */
485 dep = 2;
486 if (wcsnicmp( name, cd->Buffer, 2 ))
488 UNICODE_STRING var, val;
490 tmp[0] = '=';
491 tmp[1] = name[0];
492 tmp[2] = ':';
493 tmp[3] = '\0';
494 var.Length = 3 * sizeof(WCHAR);
495 var.MaximumLength = 4 * sizeof(WCHAR);
496 var.Buffer = tmp;
497 val.Length = 0;
498 val.MaximumLength = size;
499 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
501 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
503 case STATUS_SUCCESS:
504 /* FIXME: Win2k seems to check that the environment variable actually points
505 * to an existing directory. If not, root of the drive is used
506 * (this seems also to be the only spot in RtlGetFullPathName that the
507 * existence of a part of a path is checked)
509 /* fall through */
510 case STATUS_BUFFER_TOO_SMALL:
511 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
512 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
513 ins_str = val.Buffer;
514 break;
515 case STATUS_VARIABLE_NOT_FOUND:
516 reqsize = 3 * sizeof(WCHAR);
517 tmp[0] = name[0];
518 tmp[1] = ':';
519 tmp[2] = '\\';
520 ins_str = tmp;
521 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
522 break;
523 default:
524 ERR("Unsupported status code\n");
525 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
526 break;
528 mark = 3;
529 break;
531 /* fall through */
533 case RELATIVE_PATH: /* foo */
534 reqsize = cd->Length;
535 ins_str = cd->Buffer;
536 if (cd->Buffer[1] != ':')
538 ptr = skip_unc_prefix( cd->Buffer );
539 mark = ptr - cd->Buffer;
541 else mark = 3;
542 break;
544 case ABSOLUTE_PATH: /* \xxx */
545 if (name[0] == '/') /* may be a Unix path */
547 char *unix_name;
548 WCHAR *nt_str;
549 SIZE_T buflen;
550 NTSTATUS status;
552 unix_name = RtlAllocateHeap( GetProcessHeap(), 0, 3 * wcslen(name) + 1 );
553 ntdll_wcstoumbs( name, wcslen(name) + 1, unix_name, 3 * wcslen(name) + 1, FALSE );
554 buflen = strlen(unix_name) + 10;
555 for (;;)
557 if (!(nt_str = RtlAllocateHeap( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) break;
558 status = wine_unix_to_nt_file_name( unix_name, nt_str, &buflen );
559 if (status != STATUS_BUFFER_TOO_SMALL) break;
560 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
562 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
563 if (!status && buflen > 6 && nt_str[5] == ':')
565 reqsize = (buflen - 4) * sizeof(WCHAR);
566 if (reqsize <= size)
568 memcpy( buffer, nt_str + 4, reqsize );
569 collapse_path( buffer, 3 );
570 reqsize -= sizeof(WCHAR);
572 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
573 goto done;
575 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
577 if (cd->Buffer[1] == ':')
579 reqsize = 2 * sizeof(WCHAR);
580 tmp[0] = cd->Buffer[0];
581 tmp[1] = ':';
582 ins_str = tmp;
583 mark = 3;
585 else
587 ptr = skip_unc_prefix( cd->Buffer );
588 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
589 mark = reqsize / sizeof(WCHAR);
590 ins_str = cd->Buffer;
592 break;
594 case UNC_DOT_PATH: /* \\. */
595 reqsize = 4 * sizeof(WCHAR);
596 dep = 3;
597 tmp[0] = '\\';
598 tmp[1] = '\\';
599 tmp[2] = '.';
600 tmp[3] = '\\';
601 ins_str = tmp;
602 mark = 4;
603 break;
605 case INVALID_PATH:
606 goto done;
609 /* enough space ? */
610 deplen = wcslen(name + dep) * sizeof(WCHAR);
611 if (reqsize + deplen + sizeof(WCHAR) > size)
613 /* not enough space, return need size (including terminating '\0') */
614 reqsize += deplen + sizeof(WCHAR);
615 goto done;
618 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
619 if (reqsize) memcpy(buffer, ins_str, reqsize);
621 if (ins_str != tmp && ins_str != cd->Buffer)
622 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
624 collapse_path( buffer, mark );
625 reqsize = wcslen(buffer) * sizeof(WCHAR);
627 done:
628 RtlReleasePebLock();
629 return reqsize;
632 /******************************************************************
633 * RtlGetFullPathName_U (NTDLL.@)
635 * Returns the number of bytes written to buffer (not including the
636 * terminating NULL) if the function succeeds, or the required number of bytes
637 * (including the terminating NULL) if the buffer is too small.
639 * file_part will point to the filename part inside buffer (except if we use
640 * DOS device name, in which case file_in_buf is NULL)
643 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
644 WCHAR** file_part)
646 WCHAR* ptr;
647 DWORD dosdev;
648 DWORD reqsize;
650 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
652 if (!name || !*name) return 0;
654 if (file_part) *file_part = NULL;
656 /* check for DOS device name */
657 dosdev = RtlIsDosDeviceName_U(name);
658 if (dosdev)
660 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
661 DWORD sz = LOWORD(dosdev); /* in bytes */
663 if (8 + sz + 2 > size) return sz + 10;
664 wcscpy(buffer, DeviceRootW);
665 memmove(buffer + 4, name + offset, sz);
666 buffer[4 + sz / sizeof(WCHAR)] = '\0';
667 /* file_part isn't set in this case */
668 return sz + 8;
671 reqsize = get_full_path_helper(name, buffer, size);
672 if (!reqsize) return 0;
673 if (reqsize > size)
675 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
676 reqsize = get_full_path_helper(name, tmp, reqsize);
677 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
679 RtlFreeHeap(GetProcessHeap(), 0, tmp);
680 return reqsize + sizeof(WCHAR);
682 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
683 RtlFreeHeap(GetProcessHeap(), 0, tmp);
686 /* find file part */
687 if (file_part && (ptr = wcsrchr(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
688 *file_part = ptr;
689 return reqsize;
692 /*************************************************************************
693 * RtlGetLongestNtPathLength [NTDLL.@]
695 * Get the longest allowed path length
697 * PARAMS
698 * None.
700 * RETURNS
701 * The longest allowed path length (277 characters under Win2k).
703 DWORD WINAPI RtlGetLongestNtPathLength(void)
705 return MAX_NT_PATH_LENGTH;
708 /******************************************************************
709 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
711 * Returns TRUE iff unicode is a valid DOS (8+3) name.
712 * If the name is valid, oem gets filled with the corresponding OEM string
713 * spaces is set to TRUE if unicode contains spaces
715 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
716 OEM_STRING *oem, BOOLEAN *spaces )
718 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
719 int dot = -1;
720 int i;
721 char buffer[12];
722 OEM_STRING oem_str;
723 BOOLEAN got_space = FALSE;
725 if (!oem)
727 oem_str.Length = sizeof(buffer);
728 oem_str.MaximumLength = sizeof(buffer);
729 oem_str.Buffer = buffer;
730 oem = &oem_str;
732 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
733 return FALSE;
735 if (oem->Length > 12) return FALSE;
737 /* a starting . is invalid, except for . and .. */
738 if (oem->Length > 0 && oem->Buffer[0] == '.')
740 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
741 if (spaces) *spaces = FALSE;
742 return TRUE;
745 for (i = 0; i < oem->Length; i++)
747 switch (oem->Buffer[i])
749 case ' ':
750 /* leading/trailing spaces not allowed */
751 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
752 got_space = TRUE;
753 break;
754 case '.':
755 if (dot != -1) return FALSE;
756 dot = i;
757 break;
758 default:
759 if (strchr(illegal, oem->Buffer[i])) return FALSE;
760 break;
763 /* check file part is shorter than 8, extension shorter than 3
764 * dot cannot be last in string
766 if (dot == -1)
768 if (oem->Length > 8) return FALSE;
770 else
772 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
774 if (spaces) *spaces = got_space;
775 return TRUE;
778 /******************************************************************
779 * RtlGetCurrentDirectory_U (NTDLL.@)
782 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
784 UNICODE_STRING* us;
785 ULONG len;
787 TRACE("(%u %p)\n", buflen, buf);
789 RtlAcquirePebLock();
791 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
792 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
793 else
794 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
796 len = us->Length / sizeof(WCHAR);
797 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
798 len--;
800 if (buflen / sizeof(WCHAR) > len)
802 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
803 buf[len] = '\0';
805 else
807 len++;
810 RtlReleasePebLock();
812 return len * sizeof(WCHAR);
815 /******************************************************************
816 * RtlSetCurrentDirectory_U (NTDLL.@)
819 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
821 FILE_FS_DEVICE_INFORMATION device_info;
822 OBJECT_ATTRIBUTES attr;
823 UNICODE_STRING newdir;
824 IO_STATUS_BLOCK io;
825 CURDIR *curdir;
826 HANDLE handle;
827 NTSTATUS nts;
828 ULONG size;
829 PWSTR ptr;
831 newdir.Buffer = NULL;
833 RtlAcquirePebLock();
835 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
836 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
837 else
838 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
840 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
842 nts = STATUS_OBJECT_NAME_INVALID;
843 goto out;
846 attr.Length = sizeof(attr);
847 attr.RootDirectory = 0;
848 attr.Attributes = OBJ_CASE_INSENSITIVE;
849 attr.ObjectName = &newdir;
850 attr.SecurityDescriptor = NULL;
851 attr.SecurityQualityOfService = NULL;
853 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
854 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
855 if (nts != STATUS_SUCCESS) goto out;
857 /* don't keep the directory handle open on removable media */
858 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
859 sizeof(device_info), FileFsDeviceInformation ) &&
860 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
862 NtClose( handle );
863 handle = 0;
866 if (curdir->Handle) NtClose( curdir->Handle );
867 curdir->Handle = handle;
869 /* append trailing \ if missing */
870 size = newdir.Length / sizeof(WCHAR);
871 ptr = newdir.Buffer;
872 ptr += 4; /* skip \??\ prefix */
873 size -= 4;
874 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
876 /* convert \??\UNC\ path to \\ prefix */
877 if (size >= 4 && !wcsnicmp(ptr, UncPfxW, 4))
879 ptr += 2;
880 size -= 2;
881 *ptr = '\\';
884 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
885 curdir->DosPath.Buffer[size] = 0;
886 curdir->DosPath.Length = size * sizeof(WCHAR);
888 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
890 out:
891 RtlFreeUnicodeString( &newdir );
892 RtlReleasePebLock();
893 return nts;