Wait for the object multiple times until TIMEOUT is met.
[wine/multimedia.git] / dlls / ntdll / path.c
blob740c01383d2551edabd69b4e99d4aa1b222a3b83
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdarg.h>
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
30 #include "windef.h"
31 #include "winioctl.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "wine/library.h"
35 #include "thread.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(file);
40 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
41 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
42 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
44 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
46 #define MAX_DOS_DRIVES 26
48 struct drive_info
50 dev_t dev;
51 ino_t ino;
54 /***********************************************************************
55 * get_drives_info
57 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
59 static inline int get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
61 const char *config_dir = wine_get_config_dir();
62 char *buffer, *p;
63 struct stat st;
64 int i, ret;
66 buffer = RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") );
67 if (!buffer) return 0;
68 strcpy( buffer, config_dir );
69 strcat( buffer, "/dosdevices/a:" );
70 p = buffer + strlen(buffer) - 2;
72 for (i = ret = 0; i < MAX_DOS_DRIVES; i++)
74 *p = 'a' + i;
75 if (!stat( buffer, &st ))
77 info[i].dev = st.st_dev;
78 info[i].ino = st.st_ino;
79 ret++;
81 else
83 info[i].dev = 0;
84 info[i].ino = 0;
87 RtlFreeHeap( GetProcessHeap(), 0, buffer );
88 return ret;
92 /***********************************************************************
93 * remove_last_component
95 * Remove the last component of the path. Helper for find_drive_root.
97 static inline int remove_last_component( const WCHAR *path, int len )
99 int level = 0;
101 while (level < 1)
103 /* find start of the last path component */
104 int prev = len;
105 if (prev <= 1) break; /* reached root */
106 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
107 /* does removing it take us up a level? */
108 if (len - prev != 1 || path[prev] != '.') /* not '.' */
110 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
111 level--;
112 else
113 level++;
115 /* strip off trailing slashes */
116 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
117 len = prev;
119 return len;
123 /***********************************************************************
124 * find_drive_root
126 * Find a drive for which the root matches the beginning of the given path.
127 * This can be used to translate a Unix path into a drive + DOS path.
128 * Return value is the drive, or -1 on error. On success, ppath is modified
129 * to point to the beginning of the DOS path.
131 static int find_drive_root( LPCWSTR *ppath )
133 /* Starting with the full path, check if the device and inode match any of
134 * the wine 'drives'. If not then remove the last path component and try
135 * again. If the last component was a '..' then skip a normal component
136 * since it's a directory that's ascended back out of.
138 int drive, lenA, lenW;
139 char *buffer, *p;
140 const WCHAR *path = *ppath;
141 struct stat st;
142 struct drive_info info[MAX_DOS_DRIVES];
144 /* get device and inode of all drives */
145 if (!get_drives_info( info )) return -1;
147 /* strip off trailing slashes */
148 lenW = strlenW(path);
149 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
151 /* convert path to Unix encoding */
152 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
153 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
154 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
155 buffer[lenA] = 0;
156 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
158 for (;;)
160 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
162 /* Find the drive */
163 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
165 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
167 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
168 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
169 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
170 *ppath += lenW;
171 RtlFreeHeap( GetProcessHeap(), 0, buffer );
172 return drive;
176 if (lenW <= 1) break; /* reached root */
177 lenW = remove_last_component( path, lenW );
179 /* we only need the new length, buffer already contains the converted string */
180 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
181 buffer[lenA] = 0;
183 RtlFreeHeap( GetProcessHeap(), 0, buffer );
184 return -1;
188 /***********************************************************************
189 * RtlDetermineDosPathNameType_U (NTDLL.@)
191 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
193 if (IS_SEPARATOR(path[0]))
195 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
196 if (path[2] != '.') return UNC_PATH; /* "//foo" */
197 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
198 if (path[3]) return UNC_PATH; /* "//.foo" */
199 return UNC_DOT_PATH; /* "//." */
201 else
203 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
204 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
205 return RELATIVE_DRIVE_PATH; /* "c:foo" */
209 /***********************************************************************
210 * RtlIsDosDeviceName_U (NTDLL.@)
212 * Check if the given DOS path contains a DOS device name.
214 * Returns the length of the device name in the low word and its
215 * position in the high word (both in bytes, not WCHARs), or 0 if no
216 * device name is found.
218 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
220 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
221 static const WCHAR auxW[3] = {'A','U','X'};
222 static const WCHAR comW[3] = {'C','O','M'};
223 static const WCHAR conW[3] = {'C','O','N'};
224 static const WCHAR lptW[3] = {'L','P','T'};
225 static const WCHAR nulW[3] = {'N','U','L'};
226 static const WCHAR prnW[3] = {'P','R','N'};
228 const WCHAR *start, *end, *p;
230 switch(RtlDetermineDosPathNameType_U( dos_name ))
232 case INVALID_PATH:
233 case UNC_PATH:
234 return 0;
235 case DEVICE_PATH:
236 if (!strcmpiW( dos_name, consoleW ))
237 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
238 return 0;
239 default:
240 break;
243 end = dos_name + strlenW(dos_name) - 1;
244 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
246 /* find start of file name */
247 for (start = end; start >= dos_name; start--)
249 if (IS_SEPARATOR(start[0])) break;
250 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
251 if (start[0] == ':' && start[1] != '.') break;
253 start++;
255 /* remove extension */
256 if ((p = strchrW( start, '.' )))
258 end = p - 1;
259 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
261 else
263 /* no extension, remove trailing spaces */
264 while (end >= dos_name && *end == ' ') end--;
267 /* now we have a potential device name between start and end, check it */
268 switch(end - start + 1)
270 case 3:
271 if (strncmpiW( start, auxW, 3 ) &&
272 strncmpiW( start, conW, 3 ) &&
273 strncmpiW( start, nulW, 3 ) &&
274 strncmpiW( start, prnW, 3 )) break;
275 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
276 case 4:
277 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
278 if (*end <= '0' || *end > '9') break;
279 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
280 default: /* can't match anything */
281 break;
283 return 0;
287 /**************************************************************************
288 * RtlDosPathNameToNtPathName_U [NTDLL.@]
290 * dos_path: a DOS path name (fully qualified or not)
291 * ntpath: pointer to a UNICODE_STRING to hold the converted
292 * path name
293 * file_part:will point (in ntpath) to the file part in the path
294 * cd: directory reference (optional)
296 * FIXME:
297 * + fill the cd structure
299 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
300 PUNICODE_STRING ntpath,
301 PWSTR* file_part,
302 CURDIR* cd)
304 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
305 ULONG sz, offset;
306 WCHAR local[MAX_PATH];
307 LPWSTR ptr;
309 TRACE("(%s,%p,%p,%p)\n",
310 debugstr_w(dos_path), ntpath, file_part, cd);
312 if (cd)
314 FIXME("Unsupported parameter\n");
315 memset(cd, 0, sizeof(*cd));
318 if (!dos_path || !*dos_path) return FALSE;
320 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
322 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
323 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
324 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
325 if (!ntpath->Buffer) return FALSE;
326 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
327 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
328 return TRUE;
331 ptr = local;
332 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
333 if (sz == 0) return FALSE;
334 if (sz > sizeof(local))
336 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return FALSE;
337 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
340 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
341 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
342 if (!ntpath->Buffer)
344 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
345 return FALSE;
348 strcpyW(ntpath->Buffer, NTDosPrefixW);
349 switch (RtlDetermineDosPathNameType_U(ptr))
351 case UNC_PATH: /* \\foo */
352 offset = 2;
353 strcatW(ntpath->Buffer, UncPfxW);
354 break;
355 case DEVICE_PATH: /* \\.\foo */
356 offset = 4;
357 break;
358 default:
359 offset = 0;
360 break;
363 strcatW(ntpath->Buffer, ptr + offset);
364 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
366 if (file_part && *file_part)
367 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
369 /* FIXME: cd filling */
371 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
372 return TRUE;
375 /******************************************************************
376 * RtlDosSearchPath_U
378 * Searchs a file of name 'name' into a ';' separated list of paths
379 * (stored in paths)
380 * Doesn't seem to search elsewhere than the paths list
381 * Stores the result in buffer (file_part will point to the position
382 * of the file name in the buffer)
383 * FIXME:
384 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
386 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
387 ULONG buffer_size, LPWSTR buffer,
388 LPWSTR* file_part)
390 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
391 ULONG len = 0;
393 if (type == RELATIVE_PATH)
395 ULONG allocated = 0, needed, filelen;
396 WCHAR *name = NULL;
398 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
400 /* Windows only checks for '.' without worrying about path components */
401 if (strchrW( search, '.' )) ext = NULL;
402 if (ext != NULL) filelen += strlenW(ext);
404 while (*paths)
406 LPCWSTR ptr;
408 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
409 if (needed + filelen > allocated)
411 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
412 (needed + filelen) * sizeof(WCHAR));
413 else
415 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
416 (needed + filelen) * sizeof(WCHAR));
417 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
418 name = newname;
420 if (!name) return 0;
421 allocated = needed + filelen;
423 memmove(name, paths, needed * sizeof(WCHAR));
424 /* append '\\' if none is present */
425 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
426 strcpyW(&name[needed], search);
427 if (ext) strcatW(&name[needed], ext);
428 if (RtlDoesFileExists_U(name))
430 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
431 break;
433 paths = ptr;
435 RtlFreeHeap(GetProcessHeap(), 0, name);
437 else if (RtlDoesFileExists_U(search))
439 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
442 return len;
446 /******************************************************************
447 * collapse_path
449 * Helper for RtlGetFullPathName_U.
450 * Get rid of . and .. components in the path.
452 static inline void collapse_path( WCHAR *path, UINT mark )
454 WCHAR *p, *next;
456 /* convert every / into a \ */
457 for (p = path; *p; p++) if (*p == '/') *p = '\\';
459 /* collapse duplicate backslashes */
460 next = path + max( 1, mark );
461 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
462 *next = 0;
464 p = path + mark;
465 while (*p)
467 if (*p == '.')
469 switch(p[1])
471 case '\\': /* .\ component */
472 next = p + 2;
473 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
474 continue;
475 case 0: /* final . */
476 if (p > path + mark) p--;
477 *p = 0;
478 continue;
479 case '.':
480 if (p[2] == '\\') /* ..\ component */
482 next = p + 3;
483 if (p > path + mark)
485 p--;
486 while (p > path + mark && p[-1] != '\\') p--;
488 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
489 continue;
491 else if (!p[2]) /* final .. */
493 if (p > path + mark)
495 p--;
496 while (p > path + mark && p[-1] != '\\') p--;
497 if (p > path + mark) p--;
499 *p = 0;
500 continue;
502 break;
505 /* skip to the next component */
506 while (*p && *p != '\\') p++;
507 if (*p == '\\')
509 /* remove last dot in previous dir name */
510 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (strlenW(p) + 1) * sizeof(WCHAR) );
511 else p++;
515 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
516 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
517 *p = 0;
521 /******************************************************************
522 * skip_unc_prefix
524 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
526 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
528 ptr += 2;
529 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
530 while (IS_SEPARATOR(*ptr)) ptr++;
531 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
532 while (IS_SEPARATOR(*ptr)) ptr++;
533 return ptr;
537 /******************************************************************
538 * get_full_path_helper
540 * Helper for RtlGetFullPathName_U
541 * Note: name and buffer are allowed to point to the same memory spot
543 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
545 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
546 DOS_PATHNAME_TYPE type;
547 LPWSTR ins_str = NULL;
548 LPCWSTR ptr;
549 const UNICODE_STRING* cd;
550 WCHAR tmp[4];
552 /* return error if name only consists of spaces */
553 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
554 if (!*ptr) return 0;
556 RtlAcquirePebLock();
558 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
559 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
560 else
561 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
563 switch (type = RtlDetermineDosPathNameType_U(name))
565 case UNC_PATH: /* \\foo */
566 ptr = skip_unc_prefix( name );
567 mark = (ptr - name);
568 break;
570 case DEVICE_PATH: /* \\.\foo */
571 mark = 4;
572 break;
574 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
575 reqsize = sizeof(WCHAR);
576 tmp[0] = toupperW(name[0]);
577 ins_str = tmp;
578 dep = 1;
579 mark = 3;
580 break;
582 case RELATIVE_DRIVE_PATH: /* c:foo */
583 dep = 2;
584 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
586 UNICODE_STRING var, val;
588 tmp[0] = '=';
589 tmp[1] = name[0];
590 tmp[2] = ':';
591 tmp[3] = '\0';
592 var.Length = 3 * sizeof(WCHAR);
593 var.MaximumLength = 4 * sizeof(WCHAR);
594 var.Buffer = tmp;
595 val.Length = 0;
596 val.MaximumLength = size;
597 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
599 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
601 case STATUS_SUCCESS:
602 /* FIXME: Win2k seems to check that the environment variable actually points
603 * to an existing directory. If not, root of the drive is used
604 * (this seems also to be the only spot in RtlGetFullPathName that the
605 * existence of a part of a path is checked)
607 /* fall thru */
608 case STATUS_BUFFER_TOO_SMALL:
609 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
610 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
611 ins_str = val.Buffer;
612 break;
613 case STATUS_VARIABLE_NOT_FOUND:
614 reqsize = 3 * sizeof(WCHAR);
615 tmp[0] = name[0];
616 tmp[1] = ':';
617 tmp[2] = '\\';
618 ins_str = tmp;
619 break;
620 default:
621 ERR("Unsupported status code\n");
622 break;
624 mark = 3;
625 break;
627 /* fall through */
629 case RELATIVE_PATH: /* foo */
630 reqsize = cd->Length;
631 ins_str = cd->Buffer;
632 if (cd->Buffer[1] != ':')
634 ptr = skip_unc_prefix( cd->Buffer );
635 mark = ptr - cd->Buffer;
637 else mark = 3;
638 break;
640 case ABSOLUTE_PATH: /* \xxx */
641 if (name[0] == '/') /* may be a Unix path */
643 const WCHAR *ptr = name;
644 int drive = find_drive_root( &ptr );
645 if (drive != -1)
647 reqsize = 3 * sizeof(WCHAR);
648 tmp[0] = 'A' + drive;
649 tmp[1] = ':';
650 tmp[2] = '\\';
651 ins_str = tmp;
652 mark = 3;
653 dep = ptr - name;
654 break;
657 if (cd->Buffer[1] == ':')
659 reqsize = 2 * sizeof(WCHAR);
660 tmp[0] = cd->Buffer[0];
661 tmp[1] = ':';
662 ins_str = tmp;
663 mark = 3;
665 else
667 ptr = skip_unc_prefix( cd->Buffer );
668 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
669 mark = reqsize / sizeof(WCHAR);
670 ins_str = cd->Buffer;
672 break;
674 case UNC_DOT_PATH: /* \\. */
675 reqsize = 4 * sizeof(WCHAR);
676 dep = 3;
677 tmp[0] = '\\';
678 tmp[1] = '\\';
679 tmp[2] = '.';
680 tmp[3] = '\\';
681 ins_str = tmp;
682 mark = 4;
683 break;
685 case INVALID_PATH:
686 goto done;
689 /* enough space ? */
690 deplen = strlenW(name + dep) * sizeof(WCHAR);
691 if (reqsize + deplen + sizeof(WCHAR) > size)
693 /* not enough space, return need size (including terminating '\0') */
694 reqsize += deplen + sizeof(WCHAR);
695 goto done;
698 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
699 if (reqsize) memcpy(buffer, ins_str, reqsize);
700 reqsize += deplen;
702 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
703 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
705 collapse_path( buffer, mark );
706 reqsize = strlenW(buffer) * sizeof(WCHAR);
708 done:
709 RtlReleasePebLock();
710 return reqsize;
713 /******************************************************************
714 * RtlGetFullPathName_U (NTDLL.@)
716 * Returns the number of bytes written to buffer (not including the
717 * terminating NULL) if the function succeeds, or the required number of bytes
718 * (including the terminating NULL) if the buffer is too small.
720 * file_part will point to the filename part inside buffer (except if we use
721 * DOS device name, in which case file_in_buf is NULL)
724 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
725 WCHAR** file_part)
727 WCHAR* ptr;
728 DWORD dosdev;
729 DWORD reqsize;
731 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
733 if (!name || !*name) return 0;
735 if (file_part) *file_part = NULL;
737 /* check for DOS device name */
738 dosdev = RtlIsDosDeviceName_U(name);
739 if (dosdev)
741 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
742 DWORD sz = LOWORD(dosdev); /* in bytes */
744 if (8 + sz + 2 > size) return sz + 10;
745 strcpyW(buffer, DeviceRootW);
746 memmove(buffer + 4, name + offset, sz);
747 buffer[4 + sz / sizeof(WCHAR)] = '\0';
748 /* file_part isn't set in this case */
749 return sz + 8;
752 reqsize = get_full_path_helper(name, buffer, size);
753 if (!reqsize) return 0;
754 if (reqsize > size)
756 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
757 reqsize = get_full_path_helper(name, tmp, reqsize);
758 if (reqsize > size) /* it may have worked the second time */
760 RtlFreeHeap(GetProcessHeap(), 0, tmp);
761 return reqsize + sizeof(WCHAR);
763 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
764 RtlFreeHeap(GetProcessHeap(), 0, tmp);
767 /* find file part */
768 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
769 *file_part = ptr;
770 return reqsize;
773 /*************************************************************************
774 * RtlGetLongestNtPathLength [NTDLL.@]
776 * Get the longest allowed path length
778 * PARAMS
779 * None.
781 * RETURNS
782 * The longest allowed path length (277 characters under Win2k).
784 DWORD WINAPI RtlGetLongestNtPathLength(void)
786 return 277;
789 /******************************************************************
790 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
792 * Returns TRUE iff unicode is a valid DOS (8+3) name.
793 * If the name is valid, oem gets filled with the corresponding OEM string
794 * spaces is set to TRUE if unicode contains spaces
796 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
797 OEM_STRING *oem, BOOLEAN *spaces )
799 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
800 int dot = -1;
801 int i;
802 char buffer[12];
803 OEM_STRING oem_str;
804 BOOLEAN got_space = FALSE;
806 if (!oem)
808 oem_str.Length = sizeof(buffer);
809 oem_str.MaximumLength = sizeof(buffer);
810 oem_str.Buffer = buffer;
811 oem = &oem_str;
813 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
814 return FALSE;
816 if (oem->Length > 12) return FALSE;
818 /* a starting . is invalid, except for . and .. */
819 if (oem->Buffer[0] == '.')
821 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
822 if (spaces) *spaces = FALSE;
823 return TRUE;
826 for (i = 0; i < oem->Length; i++)
828 switch (oem->Buffer[i])
830 case ' ':
831 /* leading/trailing spaces not allowed */
832 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
833 got_space = TRUE;
834 break;
835 case '.':
836 if (dot != -1) return FALSE;
837 dot = i;
838 break;
839 default:
840 if (strchr(illegal, oem->Buffer[i])) return FALSE;
841 break;
844 /* check file part is shorter than 8, extension shorter than 3
845 * dot cannot be last in string
847 if (dot == -1)
849 if (oem->Length > 8) return FALSE;
851 else
853 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
855 if (spaces) *spaces = got_space;
856 return TRUE;
859 /******************************************************************
860 * RtlGetCurrentDirectory_U (NTDLL.@)
863 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
865 UNICODE_STRING* us;
866 ULONG len;
868 TRACE("(%lu %p)\n", buflen, buf);
870 RtlAcquirePebLock();
872 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
873 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
874 else
875 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
877 len = us->Length / sizeof(WCHAR);
878 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
879 len--;
881 if (buflen / sizeof(WCHAR) > len)
883 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
884 buf[len] = '\0';
886 else
888 len++;
891 RtlReleasePebLock();
893 return len * sizeof(WCHAR);
896 /******************************************************************
897 * RtlSetCurrentDirectory_U (NTDLL.@)
900 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
902 FILE_FS_DEVICE_INFORMATION device_info;
903 OBJECT_ATTRIBUTES attr;
904 UNICODE_STRING newdir;
905 IO_STATUS_BLOCK io;
906 CURDIR *curdir;
907 HANDLE handle;
908 NTSTATUS nts;
909 ULONG size;
910 PWSTR ptr;
912 newdir.Buffer = NULL;
914 RtlAcquirePebLock();
916 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
917 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
918 else
919 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
921 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
923 nts = STATUS_OBJECT_NAME_INVALID;
924 goto out;
927 attr.Length = sizeof(attr);
928 attr.RootDirectory = 0;
929 attr.Attributes = OBJ_CASE_INSENSITIVE;
930 attr.ObjectName = &newdir;
931 attr.SecurityDescriptor = NULL;
932 attr.SecurityQualityOfService = NULL;
934 nts = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
935 if (nts != STATUS_SUCCESS) goto out;
937 /* don't keep the directory handle open on removable media */
938 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
939 sizeof(device_info), FileFsDeviceInformation ) &&
940 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
942 NtClose( handle );
943 handle = 0;
946 if (curdir->Handle) NtClose( curdir->Handle );
947 curdir->Handle = handle;
949 /* append trailing \ if missing */
950 size = newdir.Length / sizeof(WCHAR);
951 ptr = newdir.Buffer;
952 ptr += 4; /* skip \??\ prefix */
953 size -= 4;
954 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
956 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
957 curdir->DosPath.Buffer[size] = 0;
958 curdir->DosPath.Length = size * sizeof(WCHAR);
960 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
962 out:
963 RtlFreeUnicodeString( &newdir );
964 RtlReleasePebLock();
965 return nts;