wined3d: Use glFinish() for synchronisation when cleaning up a destroyed context...
[wine.git] / dlls / ntdll / path.c
blob86760f178e1948029edbc3a068a3f9aa73270d9a
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 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
135 * dos_path: a DOS path name (fully qualified or not)
136 * ntpath: pointer to a UNICODE_STRING to hold the converted
137 * path name
138 * file_part:will point (in ntpath) to the file part in the path
139 * cd: directory reference (optional)
141 * FIXME:
142 * + fill the cd structure
144 NTSTATUS WINAPI RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR *dos_path, UNICODE_STRING *ntpath,
145 WCHAR **file_part, CURDIR *cd)
147 static const WCHAR global_prefix[] = {'\\','\\','?','\\'};
148 static const WCHAR global_prefix2[] = {'\\','?','?','\\'};
149 ULONG sz, offset;
150 WCHAR local[MAX_PATH];
151 LPWSTR ptr;
153 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, cd);
155 if (cd)
157 FIXME("Unsupported parameter\n");
158 memset(cd, 0, sizeof(*cd));
161 if (!dos_path || !*dos_path)
162 return STATUS_OBJECT_NAME_INVALID;
164 if (!memcmp(dos_path, global_prefix, sizeof(global_prefix)) ||
165 (!memcmp(dos_path, global_prefix2, sizeof(global_prefix2)) && dos_path[4]))
167 ntpath->Length = wcslen(dos_path) * sizeof(WCHAR);
168 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
169 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
170 if (!ntpath->Buffer) return STATUS_NO_MEMORY;
171 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
172 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
173 if (file_part)
175 if ((ptr = wcsrchr( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
176 else *file_part = NULL;
178 return STATUS_SUCCESS;
181 ptr = local;
182 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
183 if (sz == 0) return STATUS_OBJECT_NAME_INVALID;
185 if (sz > sizeof(local))
187 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return STATUS_NO_MEMORY;
188 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
190 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
191 if (sz > MAXWORD)
193 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
194 return STATUS_OBJECT_NAME_INVALID;
197 ntpath->MaximumLength = sz;
198 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
199 if (!ntpath->Buffer)
201 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
202 return STATUS_NO_MEMORY;
205 wcscpy(ntpath->Buffer, L"\\??\\");
206 switch (RtlDetermineDosPathNameType_U(ptr))
208 case UNC_PATH: /* \\foo */
209 offset = 2;
210 wcscat(ntpath->Buffer, L"UNC\\");
211 break;
212 case DEVICE_PATH: /* \\.\foo */
213 offset = 4;
214 break;
215 default:
216 offset = 0;
217 break;
220 wcscat(ntpath->Buffer, ptr + offset);
221 ntpath->Length = wcslen(ntpath->Buffer) * sizeof(WCHAR);
223 if (file_part && *file_part)
224 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - wcslen(*file_part);
226 /* FIXME: cd filling */
228 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
229 return STATUS_SUCCESS;
232 /**************************************************************************
233 * RtlDosPathNameToNtPathName_U [NTDLL.@]
235 * See RtlDosPathNameToNtPathName_U_WithStatus
237 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
238 PUNICODE_STRING ntpath,
239 PWSTR* file_part,
240 CURDIR* cd)
242 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, cd) == STATUS_SUCCESS;
245 /**************************************************************************
246 * RtlDosPathNameToRelativeNtPathName_U_WithStatus [NTDLL.@]
248 * See RtlDosPathNameToNtPathName_U_WithStatus (except the last parameter)
250 NTSTATUS WINAPI RtlDosPathNameToRelativeNtPathName_U_WithStatus(const WCHAR *dos_path,
251 UNICODE_STRING *ntpath, WCHAR **file_part, RTL_RELATIVE_NAME *relative)
253 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, relative);
255 if (relative)
257 FIXME("Unsupported parameter\n");
258 memset(relative, 0, sizeof(*relative));
261 /* FIXME: fill parameter relative */
263 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, NULL);
266 /**************************************************************************
267 * RtlReleaseRelativeName [NTDLL.@]
269 void WINAPI RtlReleaseRelativeName(RTL_RELATIVE_NAME *relative)
273 /******************************************************************
274 * RtlDosSearchPath_U
276 * Searches a file of name 'name' into a ';' separated list of paths
277 * (stored in paths)
278 * Doesn't seem to search elsewhere than the paths list
279 * Stores the result in buffer (file_part will point to the position
280 * of the file name in the buffer)
281 * FIXME:
282 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
284 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
285 ULONG buffer_size, LPWSTR buffer,
286 LPWSTR* file_part)
288 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
289 ULONG len = 0;
291 if (type == RELATIVE_PATH)
293 ULONG allocated = 0, needed, filelen;
294 WCHAR *name = NULL;
296 filelen = 1 /* for \ */ + wcslen(search) + 1 /* \0 */;
298 /* Windows only checks for '.' without worrying about path components */
299 if (wcschr( search, '.' )) ext = NULL;
300 if (ext != NULL) filelen += wcslen(ext);
302 while (*paths)
304 LPCWSTR ptr;
306 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
307 if (needed + filelen > allocated)
309 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
310 (needed + filelen) * sizeof(WCHAR));
311 else
313 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
314 (needed + filelen) * sizeof(WCHAR));
315 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
316 name = newname;
318 if (!name) return 0;
319 allocated = needed + filelen;
321 memmove(name, paths, needed * sizeof(WCHAR));
322 /* append '\\' if none is present */
323 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
324 wcscpy(&name[needed], search);
325 if (ext) wcscat(&name[needed], ext);
326 if (RtlDoesFileExists_U(name))
328 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
329 break;
331 paths = ptr;
333 RtlFreeHeap(GetProcessHeap(), 0, name);
335 else if (RtlDoesFileExists_U(search))
337 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
340 return len;
344 /******************************************************************
345 * collapse_path
347 * Helper for RtlGetFullPathName_U.
348 * Get rid of . and .. components in the path.
350 static inline void collapse_path( WCHAR *path, UINT mark )
352 WCHAR *p, *next;
354 /* convert every / into a \ */
355 for (p = path; *p; p++) if (*p == '/') *p = '\\';
357 /* collapse duplicate backslashes */
358 next = path + max( 1, mark );
359 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
360 *next = 0;
362 p = path + mark;
363 while (*p)
365 if (*p == '.')
367 switch(p[1])
369 case '\\': /* .\ component */
370 next = p + 2;
371 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
372 continue;
373 case 0: /* final . */
374 if (p > path + mark) p--;
375 *p = 0;
376 continue;
377 case '.':
378 if (p[2] == '\\') /* ..\ component */
380 next = p + 3;
381 if (p > path + mark)
383 p--;
384 while (p > path + mark && p[-1] != '\\') p--;
386 memmove( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
387 continue;
389 else if (!p[2]) /* final .. */
391 if (p > path + mark)
393 p--;
394 while (p > path + mark && p[-1] != '\\') p--;
395 if (p > path + mark) p--;
397 *p = 0;
398 continue;
400 break;
403 /* skip to the next component */
404 while (*p && *p != '\\') p++;
405 if (*p == '\\')
407 /* remove last dot in previous dir name */
408 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (wcslen(p) + 1) * sizeof(WCHAR) );
409 else p++;
413 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
414 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
415 *p = 0;
419 /******************************************************************
420 * skip_unc_prefix
422 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
424 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
426 ptr += 2;
427 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
428 while (IS_SEPARATOR(*ptr)) ptr++;
429 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
430 while (IS_SEPARATOR(*ptr)) ptr++;
431 return ptr;
435 /******************************************************************
436 * get_full_path_helper
438 * Helper for RtlGetFullPathName_U
439 * Note: name and buffer are allowed to point to the same memory spot
441 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
443 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
444 LPWSTR ins_str = NULL;
445 LPCWSTR ptr;
446 const UNICODE_STRING* cd;
447 WCHAR tmp[4];
449 /* return error if name only consists of spaces */
450 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
451 if (!*ptr) return 0;
453 RtlAcquirePebLock();
455 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
456 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
457 else
458 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
460 switch (RtlDetermineDosPathNameType_U(name))
462 case UNC_PATH: /* \\foo */
463 ptr = skip_unc_prefix( name );
464 mark = (ptr - name);
465 break;
467 case DEVICE_PATH: /* \\.\foo */
468 mark = 4;
469 break;
471 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
472 reqsize = sizeof(WCHAR);
473 tmp[0] = name[0];
474 ins_str = tmp;
475 dep = 1;
476 mark = 3;
477 break;
479 case RELATIVE_DRIVE_PATH: /* c:foo */
480 dep = 2;
481 if (wcsnicmp( name, cd->Buffer, 2 ))
483 UNICODE_STRING var, val;
485 tmp[0] = '=';
486 tmp[1] = name[0];
487 tmp[2] = ':';
488 tmp[3] = '\0';
489 var.Length = 3 * sizeof(WCHAR);
490 var.MaximumLength = 4 * sizeof(WCHAR);
491 var.Buffer = tmp;
492 val.Length = 0;
493 val.MaximumLength = size;
494 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
496 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
498 case STATUS_SUCCESS:
499 /* FIXME: Win2k seems to check that the environment variable actually points
500 * to an existing directory. If not, root of the drive is used
501 * (this seems also to be the only spot in RtlGetFullPathName that the
502 * existence of a part of a path is checked)
504 /* fall through */
505 case STATUS_BUFFER_TOO_SMALL:
506 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
507 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
508 ins_str = val.Buffer;
509 break;
510 case STATUS_VARIABLE_NOT_FOUND:
511 reqsize = 3 * sizeof(WCHAR);
512 tmp[0] = name[0];
513 tmp[1] = ':';
514 tmp[2] = '\\';
515 ins_str = tmp;
516 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
517 break;
518 default:
519 ERR("Unsupported status code\n");
520 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
521 break;
523 mark = 3;
524 break;
526 /* fall through */
528 case RELATIVE_PATH: /* foo */
529 reqsize = cd->Length;
530 ins_str = cd->Buffer;
531 if (cd->Buffer[1] != ':')
533 ptr = skip_unc_prefix( cd->Buffer );
534 mark = ptr - cd->Buffer;
536 else mark = 3;
537 break;
539 case ABSOLUTE_PATH: /* \xxx */
540 if (name[0] == '/') /* may be a Unix path */
542 char *unix_name;
543 WCHAR *nt_str;
544 SIZE_T buflen;
545 NTSTATUS status;
547 unix_name = RtlAllocateHeap( GetProcessHeap(), 0, 3 * wcslen(name) + 1 );
548 ntdll_wcstoumbs( name, wcslen(name) + 1, unix_name, 3 * wcslen(name) + 1, FALSE );
549 buflen = strlen(unix_name) + 10;
550 for (;;)
552 if (!(nt_str = RtlAllocateHeap( GetProcessHeap(), 0, buflen * sizeof(WCHAR) ))) break;
553 status = wine_unix_to_nt_file_name( unix_name, nt_str, &buflen );
554 if (status != STATUS_BUFFER_TOO_SMALL) break;
555 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
557 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
558 if (!status && buflen > 6 && nt_str[5] == ':')
560 reqsize = (buflen - 4) * sizeof(WCHAR);
561 if (reqsize <= size)
563 memcpy( buffer, nt_str + 4, reqsize );
564 collapse_path( buffer, 3 );
565 reqsize -= sizeof(WCHAR);
567 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
568 goto done;
570 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
572 if (cd->Buffer[1] == ':')
574 reqsize = 2 * sizeof(WCHAR);
575 tmp[0] = cd->Buffer[0];
576 tmp[1] = ':';
577 ins_str = tmp;
578 mark = 3;
580 else
582 ptr = skip_unc_prefix( cd->Buffer );
583 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
584 mark = reqsize / sizeof(WCHAR);
585 ins_str = cd->Buffer;
587 break;
589 case UNC_DOT_PATH: /* \\. */
590 reqsize = 4 * sizeof(WCHAR);
591 dep = 3;
592 tmp[0] = '\\';
593 tmp[1] = '\\';
594 tmp[2] = '.';
595 tmp[3] = '\\';
596 ins_str = tmp;
597 mark = 4;
598 break;
600 case INVALID_PATH:
601 goto done;
604 /* enough space ? */
605 deplen = wcslen(name + dep) * sizeof(WCHAR);
606 if (reqsize + deplen + sizeof(WCHAR) > size)
608 /* not enough space, return need size (including terminating '\0') */
609 reqsize += deplen + sizeof(WCHAR);
610 goto done;
613 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
614 if (reqsize) memcpy(buffer, ins_str, reqsize);
616 if (ins_str != tmp && ins_str != cd->Buffer)
617 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
619 collapse_path( buffer, mark );
620 reqsize = wcslen(buffer) * sizeof(WCHAR);
622 done:
623 RtlReleasePebLock();
624 return reqsize;
627 /******************************************************************
628 * RtlGetFullPathName_U (NTDLL.@)
630 * Returns the number of bytes written to buffer (not including the
631 * terminating NULL) if the function succeeds, or the required number of bytes
632 * (including the terminating NULL) if the buffer is too small.
634 * file_part will point to the filename part inside buffer (except if we use
635 * DOS device name, in which case file_in_buf is NULL)
638 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
639 WCHAR** file_part)
641 WCHAR* ptr;
642 DWORD dosdev;
643 DWORD reqsize;
645 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
647 if (!name || !*name) return 0;
649 if (file_part) *file_part = NULL;
651 /* check for DOS device name */
652 dosdev = RtlIsDosDeviceName_U(name);
653 if (dosdev)
655 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
656 DWORD sz = LOWORD(dosdev); /* in bytes */
658 if (8 + sz + 2 > size) return sz + 10;
659 wcscpy(buffer, L"\\\\.\\");
660 memmove(buffer + 4, name + offset, sz);
661 buffer[4 + sz / sizeof(WCHAR)] = '\0';
662 /* file_part isn't set in this case */
663 return sz + 8;
666 reqsize = get_full_path_helper(name, buffer, size);
667 if (!reqsize) return 0;
668 if (reqsize > size)
670 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
671 reqsize = get_full_path_helper(name, tmp, reqsize);
672 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
674 RtlFreeHeap(GetProcessHeap(), 0, tmp);
675 return reqsize + sizeof(WCHAR);
677 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
678 RtlFreeHeap(GetProcessHeap(), 0, tmp);
681 /* find file part */
682 if (file_part && (ptr = wcsrchr(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
683 *file_part = ptr;
684 return reqsize;
687 /*************************************************************************
688 * RtlGetLongestNtPathLength [NTDLL.@]
690 * Get the longest allowed path length
692 * PARAMS
693 * None.
695 * RETURNS
696 * The longest allowed path length (277 characters under Win2k).
698 DWORD WINAPI RtlGetLongestNtPathLength(void)
700 return MAX_NT_PATH_LENGTH;
703 /******************************************************************
704 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
706 * Returns TRUE iff unicode is a valid DOS (8+3) name.
707 * If the name is valid, oem gets filled with the corresponding OEM string
708 * spaces is set to TRUE if unicode contains spaces
710 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
711 OEM_STRING *oem, BOOLEAN *spaces )
713 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
714 int dot = -1;
715 int i;
716 char buffer[12];
717 OEM_STRING oem_str;
718 BOOLEAN got_space = FALSE;
720 if (!oem)
722 oem_str.Length = sizeof(buffer);
723 oem_str.MaximumLength = sizeof(buffer);
724 oem_str.Buffer = buffer;
725 oem = &oem_str;
727 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
728 return FALSE;
730 if (oem->Length > 12) return FALSE;
732 /* a starting . is invalid, except for . and .. */
733 if (oem->Length > 0 && oem->Buffer[0] == '.')
735 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
736 if (spaces) *spaces = FALSE;
737 return TRUE;
740 for (i = 0; i < oem->Length; i++)
742 switch (oem->Buffer[i])
744 case ' ':
745 /* leading/trailing spaces not allowed */
746 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
747 got_space = TRUE;
748 break;
749 case '.':
750 if (dot != -1) return FALSE;
751 dot = i;
752 break;
753 default:
754 if (strchr(illegal, oem->Buffer[i])) return FALSE;
755 break;
758 /* check file part is shorter than 8, extension shorter than 3
759 * dot cannot be last in string
761 if (dot == -1)
763 if (oem->Length > 8) return FALSE;
765 else
767 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
769 if (spaces) *spaces = got_space;
770 return TRUE;
773 /******************************************************************
774 * RtlGetCurrentDirectory_U (NTDLL.@)
777 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
779 UNICODE_STRING* us;
780 ULONG len;
782 TRACE("(%u %p)\n", buflen, buf);
784 RtlAcquirePebLock();
786 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
787 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
788 else
789 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
791 len = us->Length / sizeof(WCHAR);
792 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
793 len--;
795 if (buflen / sizeof(WCHAR) > len)
797 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
798 buf[len] = '\0';
800 else
802 len++;
805 RtlReleasePebLock();
807 return len * sizeof(WCHAR);
810 /******************************************************************
811 * RtlSetCurrentDirectory_U (NTDLL.@)
814 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
816 FILE_FS_DEVICE_INFORMATION device_info;
817 OBJECT_ATTRIBUTES attr;
818 UNICODE_STRING newdir;
819 IO_STATUS_BLOCK io;
820 CURDIR *curdir;
821 HANDLE handle;
822 NTSTATUS nts;
823 ULONG size;
824 PWSTR ptr;
826 newdir.Buffer = NULL;
828 RtlAcquirePebLock();
830 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
831 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
832 else
833 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
835 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
837 nts = STATUS_OBJECT_NAME_INVALID;
838 goto out;
841 attr.Length = sizeof(attr);
842 attr.RootDirectory = 0;
843 attr.Attributes = OBJ_CASE_INSENSITIVE;
844 attr.ObjectName = &newdir;
845 attr.SecurityDescriptor = NULL;
846 attr.SecurityQualityOfService = NULL;
848 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
849 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
850 if (nts != STATUS_SUCCESS) goto out;
852 /* don't keep the directory handle open on removable media */
853 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
854 sizeof(device_info), FileFsDeviceInformation ) &&
855 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
857 NtClose( handle );
858 handle = 0;
861 if (curdir->Handle) NtClose( curdir->Handle );
862 curdir->Handle = handle;
864 /* append trailing \ if missing */
865 size = newdir.Length / sizeof(WCHAR);
866 ptr = newdir.Buffer;
867 ptr += 4; /* skip \??\ prefix */
868 size -= 4;
869 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
871 /* convert \??\UNC\ path to \\ prefix */
872 if (size >= 4 && !wcsnicmp(ptr, L"UNC\\", 4))
874 ptr += 2;
875 size -= 2;
876 *ptr = '\\';
879 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
880 curdir->DosPath.Buffer[size] = 0;
881 curdir->DosPath.Length = size * sizeof(WCHAR);
883 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
885 out:
886 RtlFreeUnicodeString( &newdir );
887 RtlReleasePebLock();
888 return nts;