wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / ntdll / path.c
blob4cb64cbe516c405fbf98d1a083402d91e3c7abc7
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;
546 UNICODE_STRING str;
548 nt_str = RtlAllocateHeap( GetProcessHeap(), 0, (wcslen(name) + 9) * sizeof(WCHAR) );
549 wcscpy( nt_str, L"\\??\\unix" );
550 wcscat( nt_str, name );
551 RtlInitUnicodeString( &str, nt_str );
552 buflen = 3 * wcslen(name) + 1;
553 unix_name = RtlAllocateHeap( GetProcessHeap(), 0, buflen );
554 status = wine_nt_to_unix_file_name( &str, unix_name, &buflen, FILE_OPEN_IF );
555 if (!status || status == STATUS_NO_SUCH_FILE)
557 buflen = wcslen(name) + 9;
558 status = wine_unix_to_nt_file_name( unix_name, nt_str, &buflen );
560 RtlFreeHeap( GetProcessHeap(), 0, unix_name );
561 if (!status && buflen > 6 && nt_str[5] == ':')
563 reqsize = (buflen - 4) * sizeof(WCHAR);
564 if (reqsize <= size)
566 memcpy( buffer, nt_str + 4, reqsize );
567 collapse_path( buffer, 3 );
568 reqsize -= sizeof(WCHAR);
570 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
571 goto done;
573 RtlFreeHeap( GetProcessHeap(), 0, nt_str );
575 if (cd->Buffer[1] == ':')
577 reqsize = 2 * sizeof(WCHAR);
578 tmp[0] = cd->Buffer[0];
579 tmp[1] = ':';
580 ins_str = tmp;
581 mark = 3;
583 else
585 ptr = skip_unc_prefix( cd->Buffer );
586 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
587 mark = reqsize / sizeof(WCHAR);
588 ins_str = cd->Buffer;
590 break;
592 case UNC_DOT_PATH: /* \\. */
593 reqsize = 4 * sizeof(WCHAR);
594 dep = 3;
595 tmp[0] = '\\';
596 tmp[1] = '\\';
597 tmp[2] = '.';
598 tmp[3] = '\\';
599 ins_str = tmp;
600 mark = 4;
601 break;
603 case INVALID_PATH:
604 goto done;
607 /* enough space ? */
608 deplen = wcslen(name + dep) * sizeof(WCHAR);
609 if (reqsize + deplen + sizeof(WCHAR) > size)
611 /* not enough space, return need size (including terminating '\0') */
612 reqsize += deplen + sizeof(WCHAR);
613 goto done;
616 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
617 if (reqsize) memcpy(buffer, ins_str, reqsize);
619 if (ins_str != tmp && ins_str != cd->Buffer)
620 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
622 collapse_path( buffer, mark );
623 reqsize = wcslen(buffer) * sizeof(WCHAR);
625 done:
626 RtlReleasePebLock();
627 return reqsize;
630 /******************************************************************
631 * RtlGetFullPathName_U (NTDLL.@)
633 * Returns the number of bytes written to buffer (not including the
634 * terminating NULL) if the function succeeds, or the required number of bytes
635 * (including the terminating NULL) if the buffer is too small.
637 * file_part will point to the filename part inside buffer (except if we use
638 * DOS device name, in which case file_in_buf is NULL)
641 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
642 WCHAR** file_part)
644 WCHAR* ptr;
645 DWORD dosdev;
646 DWORD reqsize;
648 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
650 if (!name || !*name) return 0;
652 if (file_part) *file_part = NULL;
654 /* check for DOS device name */
655 dosdev = RtlIsDosDeviceName_U(name);
656 if (dosdev)
658 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
659 DWORD sz = LOWORD(dosdev); /* in bytes */
661 if (8 + sz + 2 > size) return sz + 10;
662 wcscpy(buffer, L"\\\\.\\");
663 memmove(buffer + 4, name + offset, sz);
664 buffer[4 + sz / sizeof(WCHAR)] = '\0';
665 /* file_part isn't set in this case */
666 return sz + 8;
669 reqsize = get_full_path_helper(name, buffer, size);
670 if (!reqsize) return 0;
671 if (reqsize > size)
673 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
674 reqsize = get_full_path_helper(name, tmp, reqsize);
675 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
677 RtlFreeHeap(GetProcessHeap(), 0, tmp);
678 return reqsize + sizeof(WCHAR);
680 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
681 RtlFreeHeap(GetProcessHeap(), 0, tmp);
684 /* find file part */
685 if (file_part && (ptr = wcsrchr(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
686 *file_part = ptr;
687 return reqsize;
690 /*************************************************************************
691 * RtlGetLongestNtPathLength [NTDLL.@]
693 * Get the longest allowed path length
695 * PARAMS
696 * None.
698 * RETURNS
699 * The longest allowed path length (277 characters under Win2k).
701 DWORD WINAPI RtlGetLongestNtPathLength(void)
703 return MAX_NT_PATH_LENGTH;
706 /******************************************************************
707 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
709 * Returns TRUE iff unicode is a valid DOS (8+3) name.
710 * If the name is valid, oem gets filled with the corresponding OEM string
711 * spaces is set to TRUE if unicode contains spaces
713 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
714 OEM_STRING *oem, BOOLEAN *spaces )
716 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
717 int dot = -1;
718 int i;
719 char buffer[12];
720 OEM_STRING oem_str;
721 BOOLEAN got_space = FALSE;
723 if (!oem)
725 oem_str.Length = sizeof(buffer);
726 oem_str.MaximumLength = sizeof(buffer);
727 oem_str.Buffer = buffer;
728 oem = &oem_str;
730 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
731 return FALSE;
733 if (oem->Length > 12) return FALSE;
735 /* a starting . is invalid, except for . and .. */
736 if (oem->Length > 0 && oem->Buffer[0] == '.')
738 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
739 if (spaces) *spaces = FALSE;
740 return TRUE;
743 for (i = 0; i < oem->Length; i++)
745 switch (oem->Buffer[i])
747 case ' ':
748 /* leading/trailing spaces not allowed */
749 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
750 got_space = TRUE;
751 break;
752 case '.':
753 if (dot != -1) return FALSE;
754 dot = i;
755 break;
756 default:
757 if (strchr(illegal, oem->Buffer[i])) return FALSE;
758 break;
761 /* check file part is shorter than 8, extension shorter than 3
762 * dot cannot be last in string
764 if (dot == -1)
766 if (oem->Length > 8) return FALSE;
768 else
770 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
772 if (spaces) *spaces = got_space;
773 return TRUE;
776 /******************************************************************
777 * RtlGetCurrentDirectory_U (NTDLL.@)
780 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
782 UNICODE_STRING* us;
783 ULONG len;
785 TRACE("(%u %p)\n", buflen, buf);
787 RtlAcquirePebLock();
789 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
790 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
791 else
792 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
794 len = us->Length / sizeof(WCHAR);
795 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
796 len--;
798 if (buflen / sizeof(WCHAR) > len)
800 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
801 buf[len] = '\0';
803 else
805 len++;
808 RtlReleasePebLock();
810 return len * sizeof(WCHAR);
813 /******************************************************************
814 * RtlSetCurrentDirectory_U (NTDLL.@)
817 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
819 FILE_FS_DEVICE_INFORMATION device_info;
820 OBJECT_ATTRIBUTES attr;
821 UNICODE_STRING newdir;
822 IO_STATUS_BLOCK io;
823 CURDIR *curdir;
824 HANDLE handle;
825 NTSTATUS nts;
826 ULONG size;
827 PWSTR ptr;
829 newdir.Buffer = NULL;
831 RtlAcquirePebLock();
833 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
834 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
835 else
836 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
838 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
840 nts = STATUS_OBJECT_NAME_INVALID;
841 goto out;
844 attr.Length = sizeof(attr);
845 attr.RootDirectory = 0;
846 attr.Attributes = OBJ_CASE_INSENSITIVE;
847 attr.ObjectName = &newdir;
848 attr.SecurityDescriptor = NULL;
849 attr.SecurityQualityOfService = NULL;
851 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
852 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
853 if (nts != STATUS_SUCCESS) goto out;
855 /* don't keep the directory handle open on removable media */
856 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
857 sizeof(device_info), FileFsDeviceInformation ) &&
858 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
860 NtClose( handle );
861 handle = 0;
864 if (curdir->Handle) NtClose( curdir->Handle );
865 curdir->Handle = handle;
867 /* append trailing \ if missing */
868 size = newdir.Length / sizeof(WCHAR);
869 ptr = newdir.Buffer;
870 ptr += 4; /* skip \??\ prefix */
871 size -= 4;
872 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
874 /* convert \??\UNC\ path to \\ prefix */
875 if (size >= 4 && !wcsnicmp(ptr, L"UNC\\", 4))
877 ptr += 2;
878 size -= 2;
879 *ptr = '\\';
882 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
883 curdir->DosPath.Buffer[size] = 0;
884 curdir->DosPath.Length = size * sizeof(WCHAR);
886 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
888 out:
889 RtlFreeUnicodeString( &newdir );
890 RtlReleasePebLock();
891 return nts;