jscript: Removed unused do_*_tag_format arguments.
[wine/multimedia.git] / dlls / ntdll / path.c
blobd22434c89f97069d8a0dd9f270c5b707f12b0dac
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 "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <sys/types.h>
27 #include <errno.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winioctl.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41 #include "wine/library.h"
42 #include "ntdll_misc.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(file);
46 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
47 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
48 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
50 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
52 /***********************************************************************
53 * remove_last_componentA
55 * Remove the last component of the path. Helper for find_drive_rootA.
57 static inline unsigned int remove_last_componentA( const char *path, unsigned int len )
59 int level = 0;
61 while (level < 1)
63 /* find start of the last path component */
64 unsigned int prev = len;
65 if (prev <= 1) break; /* reached root */
66 while (prev > 1 && path[prev - 1] != '/') prev--;
67 /* does removing it take us up a level? */
68 if (len - prev != 1 || path[prev] != '.') /* not '.' */
70 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
71 level--;
72 else
73 level++;
75 /* strip off trailing slashes */
76 while (prev > 1 && path[prev - 1] == '/') prev--;
77 len = prev;
79 return len;
83 /***********************************************************************
84 * find_drive_rootA
86 * Find a drive for which the root matches the beginning of the given path.
87 * This can be used to translate a Unix path into a drive + DOS path.
88 * Return value is the drive, or -1 on error. On success, ppath is modified
89 * to point to the beginning of the DOS path.
91 static NTSTATUS find_drive_rootA( LPCSTR *ppath, unsigned int len, int *drive_ret )
93 /* Starting with the full path, check if the device and inode match any of
94 * the wine 'drives'. If not then remove the last path component and try
95 * again. If the last component was a '..' then skip a normal component
96 * since it's a directory that's ascended back out of.
98 int drive;
99 char *buffer;
100 const char *path = *ppath;
101 struct stat st;
102 struct drive_info info[MAX_DOS_DRIVES];
104 /* get device and inode of all drives */
105 if (!DIR_get_drives_info( info )) return STATUS_OBJECT_PATH_NOT_FOUND;
107 /* strip off trailing slashes */
108 while (len > 1 && path[len - 1] == '/') len--;
110 /* make a copy of the path */
111 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, len + 1 ))) return STATUS_NO_MEMORY;
112 memcpy( buffer, path, len );
113 buffer[len] = 0;
115 for (;;)
117 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
119 /* Find the drive */
120 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
122 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
124 if (len == 1) len = 0; /* preserve root slash in returned path */
125 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
126 debugstr_a(path), 'A' + drive, debugstr_a(buffer), debugstr_a(path + len));
127 *ppath += len;
128 *drive_ret = drive;
129 RtlFreeHeap( GetProcessHeap(), 0, buffer );
130 return STATUS_SUCCESS;
134 if (len <= 1) break; /* reached root */
135 len = remove_last_componentA( buffer, len );
136 buffer[len] = 0;
138 RtlFreeHeap( GetProcessHeap(), 0, buffer );
139 return STATUS_OBJECT_PATH_NOT_FOUND;
143 /***********************************************************************
144 * remove_last_componentW
146 * Remove the last component of the path. Helper for find_drive_rootW.
148 static inline int remove_last_componentW( const WCHAR *path, int len )
150 int level = 0;
152 while (level < 1)
154 /* find start of the last path component */
155 int prev = len;
156 if (prev <= 1) break; /* reached root */
157 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
158 /* does removing it take us up a level? */
159 if (len - prev != 1 || path[prev] != '.') /* not '.' */
161 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
162 level--;
163 else
164 level++;
166 /* strip off trailing slashes */
167 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
168 len = prev;
170 return len;
174 /***********************************************************************
175 * find_drive_rootW
177 * Find a drive for which the root matches the beginning of the given path.
178 * This can be used to translate a Unix path into a drive + DOS path.
179 * Return value is the drive, or -1 on error. On success, ppath is modified
180 * to point to the beginning of the DOS path.
182 static int find_drive_rootW( LPCWSTR *ppath )
184 /* Starting with the full path, check if the device and inode match any of
185 * the wine 'drives'. If not then remove the last path component and try
186 * again. If the last component was a '..' then skip a normal component
187 * since it's a directory that's ascended back out of.
189 int drive, lenA, lenW;
190 char *buffer, *p;
191 const WCHAR *path = *ppath;
192 struct stat st;
193 struct drive_info info[MAX_DOS_DRIVES];
195 /* get device and inode of all drives */
196 if (!DIR_get_drives_info( info )) return -1;
198 /* strip off trailing slashes */
199 lenW = strlenW(path);
200 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
202 /* convert path to Unix encoding */
203 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
204 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
205 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
206 buffer[lenA] = 0;
207 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
209 for (;;)
211 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
213 /* Find the drive */
214 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
216 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
218 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
219 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
220 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
221 *ppath += lenW;
222 RtlFreeHeap( GetProcessHeap(), 0, buffer );
223 return drive;
227 if (lenW <= 1) break; /* reached root */
228 lenW = remove_last_componentW( path, lenW );
230 /* we only need the new length, buffer already contains the converted string */
231 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
232 buffer[lenA] = 0;
234 RtlFreeHeap( GetProcessHeap(), 0, buffer );
235 return -1;
239 /***********************************************************************
240 * RtlDetermineDosPathNameType_U (NTDLL.@)
242 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
244 if (IS_SEPARATOR(path[0]))
246 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
247 if (path[2] != '.') return UNC_PATH; /* "//foo" */
248 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
249 if (path[3]) return UNC_PATH; /* "//.foo" */
250 return UNC_DOT_PATH; /* "//." */
252 else
254 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
255 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
256 return RELATIVE_DRIVE_PATH; /* "c:foo" */
260 /***********************************************************************
261 * RtlIsDosDeviceName_U (NTDLL.@)
263 * Check if the given DOS path contains a DOS device name.
265 * Returns the length of the device name in the low word and its
266 * position in the high word (both in bytes, not WCHARs), or 0 if no
267 * device name is found.
269 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
271 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
272 static const WCHAR auxW[] = {'A','U','X'};
273 static const WCHAR comW[] = {'C','O','M'};
274 static const WCHAR conW[] = {'C','O','N'};
275 static const WCHAR lptW[] = {'L','P','T'};
276 static const WCHAR nulW[] = {'N','U','L'};
277 static const WCHAR prnW[] = {'P','R','N'};
279 const WCHAR *start, *end, *p;
281 switch(RtlDetermineDosPathNameType_U( dos_name ))
283 case INVALID_PATH:
284 case UNC_PATH:
285 return 0;
286 case DEVICE_PATH:
287 if (!strcmpiW( dos_name, consoleW ))
288 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
289 return 0;
290 case ABSOLUTE_DRIVE_PATH:
291 case RELATIVE_DRIVE_PATH:
292 start = dos_name + 2; /* skip drive letter */
293 break;
294 default:
295 start = dos_name;
296 break;
299 /* find start of file name */
300 for (p = start; *p; p++) if (IS_SEPARATOR(*p)) start = p + 1;
302 /* truncate at extension and ':' */
303 for (end = start; *end; end++) if (*end == '.' || *end == ':') break;
304 end--;
306 /* remove trailing spaces */
307 while (end >= start && *end == ' ') end--;
309 /* now we have a potential device name between start and end, check it */
310 switch(end - start + 1)
312 case 3:
313 if (strncmpiW( start, auxW, 3 ) &&
314 strncmpiW( start, conW, 3 ) &&
315 strncmpiW( start, nulW, 3 ) &&
316 strncmpiW( start, prnW, 3 )) break;
317 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
318 case 4:
319 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
320 if (*end <= '0' || *end > '9') break;
321 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
322 default: /* can't match anything */
323 break;
325 return 0;
329 /**************************************************************************
330 * RtlDosPathNameToNtPathName_U [NTDLL.@]
332 * dos_path: a DOS path name (fully qualified or not)
333 * ntpath: pointer to a UNICODE_STRING to hold the converted
334 * path name
335 * file_part:will point (in ntpath) to the file part in the path
336 * cd: directory reference (optional)
338 * FIXME:
339 * + fill the cd structure
341 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
342 PUNICODE_STRING ntpath,
343 PWSTR* file_part,
344 CURDIR* cd)
346 static const WCHAR LongFileNamePfxW[] = {'\\','\\','?','\\'};
347 ULONG sz, offset;
348 WCHAR local[MAX_PATH];
349 LPWSTR ptr;
351 TRACE("(%s,%p,%p,%p)\n",
352 debugstr_w(dos_path), ntpath, file_part, cd);
354 if (cd)
356 FIXME("Unsupported parameter\n");
357 memset(cd, 0, sizeof(*cd));
360 if (!dos_path || !*dos_path) return FALSE;
362 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
364 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
365 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
366 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
367 if (!ntpath->Buffer) return FALSE;
368 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
369 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
370 if (file_part)
372 if ((ptr = strrchrW( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
373 else *file_part = NULL;
375 return TRUE;
378 ptr = local;
379 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
380 if (sz == 0) return FALSE;
381 if (sz > sizeof(local))
383 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return FALSE;
384 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
386 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
387 if (sz > MAXWORD)
389 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
390 return FALSE;
393 ntpath->MaximumLength = sz;
394 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
395 if (!ntpath->Buffer)
397 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
398 return FALSE;
401 strcpyW(ntpath->Buffer, NTDosPrefixW);
402 switch (RtlDetermineDosPathNameType_U(ptr))
404 case UNC_PATH: /* \\foo */
405 offset = 2;
406 strcatW(ntpath->Buffer, UncPfxW);
407 break;
408 case DEVICE_PATH: /* \\.\foo */
409 offset = 4;
410 break;
411 default:
412 offset = 0;
413 break;
416 strcatW(ntpath->Buffer, ptr + offset);
417 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
419 if (file_part && *file_part)
420 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
422 /* FIXME: cd filling */
424 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
425 return TRUE;
428 /******************************************************************
429 * RtlDosSearchPath_U
431 * Searches a file of name 'name' into a ';' separated list of paths
432 * (stored in paths)
433 * Doesn't seem to search elsewhere than the paths list
434 * Stores the result in buffer (file_part will point to the position
435 * of the file name in the buffer)
436 * FIXME:
437 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
439 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
440 ULONG buffer_size, LPWSTR buffer,
441 LPWSTR* file_part)
443 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
444 ULONG len = 0;
446 if (type == RELATIVE_PATH)
448 ULONG allocated = 0, needed, filelen;
449 WCHAR *name = NULL;
451 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
453 /* Windows only checks for '.' without worrying about path components */
454 if (strchrW( search, '.' )) ext = NULL;
455 if (ext != NULL) filelen += strlenW(ext);
457 while (*paths)
459 LPCWSTR ptr;
461 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
462 if (needed + filelen > allocated)
464 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
465 (needed + filelen) * sizeof(WCHAR));
466 else
468 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
469 (needed + filelen) * sizeof(WCHAR));
470 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
471 name = newname;
473 if (!name) return 0;
474 allocated = needed + filelen;
476 memmove(name, paths, needed * sizeof(WCHAR));
477 /* append '\\' if none is present */
478 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
479 strcpyW(&name[needed], search);
480 if (ext) strcatW(&name[needed], ext);
481 if (RtlDoesFileExists_U(name))
483 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
484 break;
486 paths = ptr;
488 RtlFreeHeap(GetProcessHeap(), 0, name);
490 else if (RtlDoesFileExists_U(search))
492 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
495 return len;
499 /******************************************************************
500 * collapse_path
502 * Helper for RtlGetFullPathName_U.
503 * Get rid of . and .. components in the path.
505 static inline void collapse_path( WCHAR *path, UINT mark )
507 WCHAR *p, *next;
509 /* convert every / into a \ */
510 for (p = path; *p; p++) if (*p == '/') *p = '\\';
512 /* collapse duplicate backslashes */
513 next = path + max( 1, mark );
514 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
515 *next = 0;
517 p = path + mark;
518 while (*p)
520 if (*p == '.')
522 switch(p[1])
524 case '\\': /* .\ component */
525 next = p + 2;
526 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
527 continue;
528 case 0: /* final . */
529 if (p > path + mark) p--;
530 *p = 0;
531 continue;
532 case '.':
533 if (p[2] == '\\') /* ..\ component */
535 next = p + 3;
536 if (p > path + mark)
538 p--;
539 while (p > path + mark && p[-1] != '\\') p--;
541 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
542 continue;
544 else if (!p[2]) /* final .. */
546 if (p > path + mark)
548 p--;
549 while (p > path + mark && p[-1] != '\\') p--;
550 if (p > path + mark) p--;
552 *p = 0;
553 continue;
555 break;
558 /* skip to the next component */
559 while (*p && *p != '\\') p++;
560 if (*p == '\\')
562 /* remove last dot in previous dir name */
563 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (strlenW(p) + 1) * sizeof(WCHAR) );
564 else p++;
568 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
569 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
570 *p = 0;
574 /******************************************************************
575 * skip_unc_prefix
577 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
579 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
581 ptr += 2;
582 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
583 while (IS_SEPARATOR(*ptr)) ptr++;
584 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
585 while (IS_SEPARATOR(*ptr)) ptr++;
586 return ptr;
590 /******************************************************************
591 * get_full_path_helper
593 * Helper for RtlGetFullPathName_U
594 * Note: name and buffer are allowed to point to the same memory spot
596 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
598 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
599 LPWSTR ins_str = NULL;
600 LPCWSTR ptr;
601 const UNICODE_STRING* cd;
602 WCHAR tmp[4];
604 /* return error if name only consists of spaces */
605 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
606 if (!*ptr) return 0;
608 RtlAcquirePebLock();
610 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
611 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
612 else
613 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
615 switch (RtlDetermineDosPathNameType_U(name))
617 case UNC_PATH: /* \\foo */
618 ptr = skip_unc_prefix( name );
619 mark = (ptr - name);
620 break;
622 case DEVICE_PATH: /* \\.\foo */
623 mark = 4;
624 break;
626 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
627 reqsize = sizeof(WCHAR);
628 tmp[0] = toupperW(name[0]);
629 ins_str = tmp;
630 dep = 1;
631 mark = 3;
632 break;
634 case RELATIVE_DRIVE_PATH: /* c:foo */
635 dep = 2;
636 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
638 UNICODE_STRING var, val;
640 tmp[0] = '=';
641 tmp[1] = name[0];
642 tmp[2] = ':';
643 tmp[3] = '\0';
644 var.Length = 3 * sizeof(WCHAR);
645 var.MaximumLength = 4 * sizeof(WCHAR);
646 var.Buffer = tmp;
647 val.Length = 0;
648 val.MaximumLength = size;
649 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
651 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
653 case STATUS_SUCCESS:
654 /* FIXME: Win2k seems to check that the environment variable actually points
655 * to an existing directory. If not, root of the drive is used
656 * (this seems also to be the only spot in RtlGetFullPathName that the
657 * existence of a part of a path is checked)
659 /* fall thru */
660 case STATUS_BUFFER_TOO_SMALL:
661 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
662 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
663 ins_str = val.Buffer;
664 break;
665 case STATUS_VARIABLE_NOT_FOUND:
666 reqsize = 3 * sizeof(WCHAR);
667 tmp[0] = name[0];
668 tmp[1] = ':';
669 tmp[2] = '\\';
670 ins_str = tmp;
671 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
672 break;
673 default:
674 ERR("Unsupported status code\n");
675 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
676 break;
678 mark = 3;
679 break;
681 /* fall through */
683 case RELATIVE_PATH: /* foo */
684 reqsize = cd->Length;
685 ins_str = cd->Buffer;
686 if (cd->Buffer[1] != ':')
688 ptr = skip_unc_prefix( cd->Buffer );
689 mark = ptr - cd->Buffer;
691 else mark = 3;
692 break;
694 case ABSOLUTE_PATH: /* \xxx */
695 if (name[0] == '/') /* may be a Unix path */
697 const WCHAR *ptr = name;
698 int drive = find_drive_rootW( &ptr );
699 if (drive != -1)
701 reqsize = 3 * sizeof(WCHAR);
702 tmp[0] = 'A' + drive;
703 tmp[1] = ':';
704 tmp[2] = '\\';
705 ins_str = tmp;
706 mark = 3;
707 dep = ptr - name;
708 break;
711 if (cd->Buffer[1] == ':')
713 reqsize = 2 * sizeof(WCHAR);
714 tmp[0] = cd->Buffer[0];
715 tmp[1] = ':';
716 ins_str = tmp;
717 mark = 3;
719 else
721 ptr = skip_unc_prefix( cd->Buffer );
722 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
723 mark = reqsize / sizeof(WCHAR);
724 ins_str = cd->Buffer;
726 break;
728 case UNC_DOT_PATH: /* \\. */
729 reqsize = 4 * sizeof(WCHAR);
730 dep = 3;
731 tmp[0] = '\\';
732 tmp[1] = '\\';
733 tmp[2] = '.';
734 tmp[3] = '\\';
735 ins_str = tmp;
736 mark = 4;
737 break;
739 case INVALID_PATH:
740 goto done;
743 /* enough space ? */
744 deplen = strlenW(name + dep) * sizeof(WCHAR);
745 if (reqsize + deplen + sizeof(WCHAR) > size)
747 /* not enough space, return need size (including terminating '\0') */
748 reqsize += deplen + sizeof(WCHAR);
749 goto done;
752 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
753 if (reqsize) memcpy(buffer, ins_str, reqsize);
755 if (ins_str != tmp && ins_str != cd->Buffer)
756 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
758 collapse_path( buffer, mark );
759 reqsize = strlenW(buffer) * sizeof(WCHAR);
761 done:
762 RtlReleasePebLock();
763 return reqsize;
766 /******************************************************************
767 * RtlGetFullPathName_U (NTDLL.@)
769 * Returns the number of bytes written to buffer (not including the
770 * terminating NULL) if the function succeeds, or the required number of bytes
771 * (including the terminating NULL) if the buffer is too small.
773 * file_part will point to the filename part inside buffer (except if we use
774 * DOS device name, in which case file_in_buf is NULL)
777 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
778 WCHAR** file_part)
780 WCHAR* ptr;
781 DWORD dosdev;
782 DWORD reqsize;
784 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
786 if (!name || !*name) return 0;
788 if (file_part) *file_part = NULL;
790 /* check for DOS device name */
791 dosdev = RtlIsDosDeviceName_U(name);
792 if (dosdev)
794 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
795 DWORD sz = LOWORD(dosdev); /* in bytes */
797 if (8 + sz + 2 > size) return sz + 10;
798 strcpyW(buffer, DeviceRootW);
799 memmove(buffer + 4, name + offset, sz);
800 buffer[4 + sz / sizeof(WCHAR)] = '\0';
801 /* file_part isn't set in this case */
802 return sz + 8;
805 reqsize = get_full_path_helper(name, buffer, size);
806 if (!reqsize) return 0;
807 if (reqsize > size)
809 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
810 reqsize = get_full_path_helper(name, tmp, reqsize);
811 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
813 RtlFreeHeap(GetProcessHeap(), 0, tmp);
814 return reqsize + sizeof(WCHAR);
816 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
817 RtlFreeHeap(GetProcessHeap(), 0, tmp);
820 /* find file part */
821 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
822 *file_part = ptr;
823 return reqsize;
826 /*************************************************************************
827 * RtlGetLongestNtPathLength [NTDLL.@]
829 * Get the longest allowed path length
831 * PARAMS
832 * None.
834 * RETURNS
835 * The longest allowed path length (277 characters under Win2k).
837 DWORD WINAPI RtlGetLongestNtPathLength(void)
839 return MAX_NT_PATH_LENGTH;
842 /******************************************************************
843 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
845 * Returns TRUE iff unicode is a valid DOS (8+3) name.
846 * If the name is valid, oem gets filled with the corresponding OEM string
847 * spaces is set to TRUE if unicode contains spaces
849 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
850 OEM_STRING *oem, BOOLEAN *spaces )
852 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
853 int dot = -1;
854 int i;
855 char buffer[12];
856 OEM_STRING oem_str;
857 BOOLEAN got_space = FALSE;
859 if (!oem)
861 oem_str.Length = sizeof(buffer);
862 oem_str.MaximumLength = sizeof(buffer);
863 oem_str.Buffer = buffer;
864 oem = &oem_str;
866 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
867 return FALSE;
869 if (oem->Length > 12) return FALSE;
871 /* a starting . is invalid, except for . and .. */
872 if (oem->Buffer[0] == '.')
874 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
875 if (spaces) *spaces = FALSE;
876 return TRUE;
879 for (i = 0; i < oem->Length; i++)
881 switch (oem->Buffer[i])
883 case ' ':
884 /* leading/trailing spaces not allowed */
885 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
886 got_space = TRUE;
887 break;
888 case '.':
889 if (dot != -1) return FALSE;
890 dot = i;
891 break;
892 default:
893 if (strchr(illegal, oem->Buffer[i])) return FALSE;
894 break;
897 /* check file part is shorter than 8, extension shorter than 3
898 * dot cannot be last in string
900 if (dot == -1)
902 if (oem->Length > 8) return FALSE;
904 else
906 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
908 if (spaces) *spaces = got_space;
909 return TRUE;
912 /******************************************************************
913 * RtlGetCurrentDirectory_U (NTDLL.@)
916 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
918 UNICODE_STRING* us;
919 ULONG len;
921 TRACE("(%u %p)\n", buflen, buf);
923 RtlAcquirePebLock();
925 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
926 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
927 else
928 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
930 len = us->Length / sizeof(WCHAR);
931 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
932 len--;
934 if (buflen / sizeof(WCHAR) > len)
936 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
937 buf[len] = '\0';
939 else
941 len++;
944 RtlReleasePebLock();
946 return len * sizeof(WCHAR);
949 /******************************************************************
950 * RtlSetCurrentDirectory_U (NTDLL.@)
953 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
955 FILE_FS_DEVICE_INFORMATION device_info;
956 OBJECT_ATTRIBUTES attr;
957 UNICODE_STRING newdir;
958 IO_STATUS_BLOCK io;
959 CURDIR *curdir;
960 HANDLE handle;
961 NTSTATUS nts;
962 ULONG size;
963 PWSTR ptr;
965 newdir.Buffer = NULL;
967 RtlAcquirePebLock();
969 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
970 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
971 else
972 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
974 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
976 nts = STATUS_OBJECT_NAME_INVALID;
977 goto out;
980 attr.Length = sizeof(attr);
981 attr.RootDirectory = 0;
982 attr.Attributes = OBJ_CASE_INSENSITIVE;
983 attr.ObjectName = &newdir;
984 attr.SecurityDescriptor = NULL;
985 attr.SecurityQualityOfService = NULL;
987 nts = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
988 if (nts != STATUS_SUCCESS) goto out;
990 /* don't keep the directory handle open on removable media */
991 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
992 sizeof(device_info), FileFsDeviceInformation ) &&
993 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
995 NtClose( handle );
996 handle = 0;
999 if (curdir->Handle) NtClose( curdir->Handle );
1000 curdir->Handle = handle;
1002 /* append trailing \ if missing */
1003 size = newdir.Length / sizeof(WCHAR);
1004 ptr = newdir.Buffer;
1005 ptr += 4; /* skip \??\ prefix */
1006 size -= 4;
1007 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
1009 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
1010 curdir->DosPath.Buffer[size] = 0;
1011 curdir->DosPath.Length = size * sizeof(WCHAR);
1013 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
1015 out:
1016 RtlFreeUnicodeString( &newdir );
1017 RtlReleasePebLock();
1018 return nts;
1022 /******************************************************************
1023 * wine_unix_to_nt_file_name (NTDLL.@) Not a Windows API
1025 NTSTATUS CDECL wine_unix_to_nt_file_name( const ANSI_STRING *name, UNICODE_STRING *nt )
1027 static const WCHAR prefixW[] = {'\\','?','?','\\','A',':','\\'};
1028 static const WCHAR unix_prefixW[] = {'\\','?','?','\\','u','n','i','x'};
1029 unsigned int lenW, lenA = name->Length;
1030 const char *path = name->Buffer;
1031 char *cwd;
1032 WCHAR *p;
1033 NTSTATUS status;
1034 int drive;
1036 if (!lenA || path[0] != '/')
1038 char *newcwd, *end;
1039 size_t size;
1041 if ((status = DIR_get_unix_cwd( &cwd )) != STATUS_SUCCESS) return status;
1043 size = strlen(cwd) + lenA + 1;
1044 if (!(newcwd = RtlReAllocateHeap( GetProcessHeap(), 0, cwd, size )))
1046 status = STATUS_NO_MEMORY;
1047 goto done;
1049 cwd = newcwd;
1050 end = cwd + strlen(cwd);
1051 if (end > cwd && end[-1] != '/') *end++ = '/';
1052 memcpy( end, path, lenA );
1053 lenA += end - cwd;
1054 path = cwd;
1056 status = find_drive_rootA( &path, lenA, &drive );
1057 lenA -= (path - cwd);
1059 else
1061 cwd = NULL;
1062 status = find_drive_rootA( &path, lenA, &drive );
1063 lenA -= (path - name->Buffer);
1066 if (status != STATUS_SUCCESS)
1068 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
1070 lenW = ntdll_umbstowcs( 0, path, lenA, NULL, 0 );
1071 nt->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1072 (lenW + 1) * sizeof(WCHAR) + sizeof(unix_prefixW) );
1073 if (nt->Buffer == NULL)
1075 status = STATUS_NO_MEMORY;
1076 goto done;
1078 memcpy( nt->Buffer, unix_prefixW, sizeof(unix_prefixW) );
1079 ntdll_umbstowcs( 0, path, lenA, nt->Buffer + sizeof(unix_prefixW)/sizeof(WCHAR), lenW );
1080 lenW += sizeof(unix_prefixW)/sizeof(WCHAR);
1081 nt->Buffer[lenW] = 0;
1082 nt->Length = lenW * sizeof(WCHAR);
1083 nt->MaximumLength = nt->Length + sizeof(WCHAR);
1084 for (p = nt->Buffer + sizeof(unix_prefixW)/sizeof(WCHAR); *p; p++) if (*p == '/') *p = '\\';
1085 status = STATUS_SUCCESS;
1087 goto done;
1089 while (lenA && path[0] == '/') { lenA--; path++; }
1091 lenW = ntdll_umbstowcs( 0, path, lenA, NULL, 0 );
1092 if (!(nt->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1093 (lenW + 1) * sizeof(WCHAR) + sizeof(prefixW) )))
1095 status = STATUS_NO_MEMORY;
1096 goto done;
1099 memcpy( nt->Buffer, prefixW, sizeof(prefixW) );
1100 nt->Buffer[4] += drive;
1101 ntdll_umbstowcs( 0, path, lenA, nt->Buffer + sizeof(prefixW)/sizeof(WCHAR), lenW );
1102 lenW += sizeof(prefixW)/sizeof(WCHAR);
1103 nt->Buffer[lenW] = 0;
1104 nt->Length = lenW * sizeof(WCHAR);
1105 nt->MaximumLength = nt->Length + sizeof(WCHAR);
1106 for (p = nt->Buffer + sizeof(prefixW)/sizeof(WCHAR); *p; p++) if (*p == '/') *p = '\\';
1108 done:
1109 RtlFreeHeap( GetProcessHeap(), 0, cwd );
1110 return status;