Release 20050930.
[wine/gsoc-2012-control.git] / dlls / ntdll / path.c
blobbefe15c0a4fc39f13ad905f18a439ee6b5d07a4f
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"
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 "windef.h"
36 #include "winioctl.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39 #include "wine/library.h"
40 #include "thread.h"
41 #include "ntdll_misc.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(file);
45 static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
46 static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
47 static const WCHAR UncPfxW[] = {'U','N','C','\\',0};
49 #define IS_SEPARATOR(ch) ((ch) == '\\' || (ch) == '/')
51 #define MAX_DOS_DRIVES 26
53 struct drive_info
55 dev_t dev;
56 ino_t ino;
59 /***********************************************************************
60 * get_drives_info
62 * Retrieve device/inode number for all the drives. Helper for find_drive_root.
64 static inline int get_drives_info( struct drive_info info[MAX_DOS_DRIVES] )
66 const char *config_dir = wine_get_config_dir();
67 char *buffer, *p;
68 struct stat st;
69 int i, ret;
71 buffer = RtlAllocateHeap( GetProcessHeap(), 0, strlen(config_dir) + sizeof("/dosdevices/a:") );
72 if (!buffer) return 0;
73 strcpy( buffer, config_dir );
74 strcat( buffer, "/dosdevices/a:" );
75 p = buffer + strlen(buffer) - 2;
77 for (i = ret = 0; i < MAX_DOS_DRIVES; i++)
79 *p = 'a' + i;
80 if (!stat( buffer, &st ))
82 info[i].dev = st.st_dev;
83 info[i].ino = st.st_ino;
84 ret++;
86 else
88 info[i].dev = 0;
89 info[i].ino = 0;
92 RtlFreeHeap( GetProcessHeap(), 0, buffer );
93 return ret;
97 /***********************************************************************
98 * remove_last_componentA
100 * Remove the last component of the path. Helper for find_drive_rootA.
102 static inline unsigned int remove_last_componentA( const char *path, unsigned int len )
104 int level = 0;
106 while (level < 1)
108 /* find start of the last path component */
109 unsigned int prev = len;
110 if (prev <= 1) break; /* reached root */
111 while (prev > 1 && path[prev - 1] != '/') prev--;
112 /* does removing it take us up a level? */
113 if (len - prev != 1 || path[prev] != '.') /* not '.' */
115 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
116 level--;
117 else
118 level++;
120 /* strip off trailing slashes */
121 while (prev > 1 && path[prev - 1] == '/') prev--;
122 len = prev;
124 return len;
128 /***********************************************************************
129 * find_drive_rootA
131 * Find a drive for which the root matches the beginning of the given path.
132 * This can be used to translate a Unix path into a drive + DOS path.
133 * Return value is the drive, or -1 on error. On success, ppath is modified
134 * to point to the beginning of the DOS path.
136 static NTSTATUS find_drive_rootA( LPCSTR *ppath, unsigned int len, int *drive_ret )
138 /* Starting with the full path, check if the device and inode match any of
139 * the wine 'drives'. If not then remove the last path component and try
140 * again. If the last component was a '..' then skip a normal component
141 * since it's a directory that's ascended back out of.
143 int drive;
144 char *buffer;
145 const char *path = *ppath;
146 struct stat st;
147 struct drive_info info[MAX_DOS_DRIVES];
149 /* get device and inode of all drives */
150 if (!get_drives_info( info )) return STATUS_OBJECT_PATH_NOT_FOUND;
152 /* strip off trailing slashes */
153 while (len > 1 && path[len - 1] == '/') len--;
155 /* make a copy of the path */
156 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, len + 1 ))) return STATUS_NO_MEMORY;
157 memcpy( buffer, path, len );
158 buffer[len] = 0;
160 for (;;)
162 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
164 /* Find the drive */
165 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
167 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
169 if (len == 1) len = 0; /* preserve root slash in returned path */
170 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
171 debugstr_a(path), 'A' + drive, debugstr_a(buffer), debugstr_a(path + len));
172 *ppath += len;
173 *drive_ret = drive;
174 RtlFreeHeap( GetProcessHeap(), 0, buffer );
175 return STATUS_SUCCESS;
179 if (len <= 1) break; /* reached root */
180 len = remove_last_componentA( buffer, len );
181 buffer[len] = 0;
183 RtlFreeHeap( GetProcessHeap(), 0, buffer );
184 return STATUS_OBJECT_PATH_NOT_FOUND;
188 /***********************************************************************
189 * remove_last_componentW
191 * Remove the last component of the path. Helper for find_drive_rootW.
193 static inline int remove_last_componentW( const WCHAR *path, int len )
195 int level = 0;
197 while (level < 1)
199 /* find start of the last path component */
200 int prev = len;
201 if (prev <= 1) break; /* reached root */
202 while (prev > 1 && !IS_SEPARATOR(path[prev - 1])) prev--;
203 /* does removing it take us up a level? */
204 if (len - prev != 1 || path[prev] != '.') /* not '.' */
206 if (len - prev == 2 && path[prev] == '.' && path[prev+1] == '.') /* is it '..'? */
207 level--;
208 else
209 level++;
211 /* strip off trailing slashes */
212 while (prev > 1 && IS_SEPARATOR(path[prev - 1])) prev--;
213 len = prev;
215 return len;
219 /***********************************************************************
220 * find_drive_rootW
222 * Find a drive for which the root matches the beginning of the given path.
223 * This can be used to translate a Unix path into a drive + DOS path.
224 * Return value is the drive, or -1 on error. On success, ppath is modified
225 * to point to the beginning of the DOS path.
227 static int find_drive_rootW( LPCWSTR *ppath )
229 /* Starting with the full path, check if the device and inode match any of
230 * the wine 'drives'. If not then remove the last path component and try
231 * again. If the last component was a '..' then skip a normal component
232 * since it's a directory that's ascended back out of.
234 int drive, lenA, lenW;
235 char *buffer, *p;
236 const WCHAR *path = *ppath;
237 struct stat st;
238 struct drive_info info[MAX_DOS_DRIVES];
240 /* get device and inode of all drives */
241 if (!get_drives_info( info )) return -1;
243 /* strip off trailing slashes */
244 lenW = strlenW(path);
245 while (lenW > 1 && IS_SEPARATOR(path[lenW - 1])) lenW--;
247 /* convert path to Unix encoding */
248 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
249 if (!(buffer = RtlAllocateHeap( GetProcessHeap(), 0, lenA + 1 ))) return -1;
250 lenA = ntdll_wcstoumbs( 0, path, lenW, buffer, lenA, NULL, NULL );
251 buffer[lenA] = 0;
252 for (p = buffer; *p; p++) if (*p == '\\') *p = '/';
254 for (;;)
256 if (!stat( buffer, &st ) && S_ISDIR( st.st_mode ))
258 /* Find the drive */
259 for (drive = 0; drive < MAX_DOS_DRIVES; drive++)
261 if ((info[drive].dev == st.st_dev) && (info[drive].ino == st.st_ino))
263 if (lenW == 1) lenW = 0; /* preserve root slash in returned path */
264 TRACE( "%s -> drive %c:, root=%s, name=%s\n",
265 debugstr_w(path), 'A' + drive, debugstr_a(buffer), debugstr_w(path + lenW));
266 *ppath += lenW;
267 RtlFreeHeap( GetProcessHeap(), 0, buffer );
268 return drive;
272 if (lenW <= 1) break; /* reached root */
273 lenW = remove_last_componentW( path, lenW );
275 /* we only need the new length, buffer already contains the converted string */
276 lenA = ntdll_wcstoumbs( 0, path, lenW, NULL, 0, NULL, NULL );
277 buffer[lenA] = 0;
279 RtlFreeHeap( GetProcessHeap(), 0, buffer );
280 return -1;
284 /***********************************************************************
285 * RtlDetermineDosPathNameType_U (NTDLL.@)
287 DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
289 if (IS_SEPARATOR(path[0]))
291 if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH; /* "/foo" */
292 if (path[2] != '.') return UNC_PATH; /* "//foo" */
293 if (IS_SEPARATOR(path[3])) return DEVICE_PATH; /* "//./foo" */
294 if (path[3]) return UNC_PATH; /* "//.foo" */
295 return UNC_DOT_PATH; /* "//." */
297 else
299 if (!path[0] || path[1] != ':') return RELATIVE_PATH; /* "foo" */
300 if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH; /* "c:/foo" */
301 return RELATIVE_DRIVE_PATH; /* "c:foo" */
305 /***********************************************************************
306 * RtlIsDosDeviceName_U (NTDLL.@)
308 * Check if the given DOS path contains a DOS device name.
310 * Returns the length of the device name in the low word and its
311 * position in the high word (both in bytes, not WCHARs), or 0 if no
312 * device name is found.
314 ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
316 static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
317 static const WCHAR auxW[3] = {'A','U','X'};
318 static const WCHAR comW[3] = {'C','O','M'};
319 static const WCHAR conW[3] = {'C','O','N'};
320 static const WCHAR lptW[3] = {'L','P','T'};
321 static const WCHAR nulW[3] = {'N','U','L'};
322 static const WCHAR prnW[3] = {'P','R','N'};
324 const WCHAR *start, *end, *p;
326 switch(RtlDetermineDosPathNameType_U( dos_name ))
328 case INVALID_PATH:
329 case UNC_PATH:
330 return 0;
331 case DEVICE_PATH:
332 if (!strcmpiW( dos_name, consoleW ))
333 return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) ); /* 4 is length of \\.\ prefix */
334 return 0;
335 default:
336 break;
339 end = dos_name + strlenW(dos_name) - 1;
340 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' */
342 /* find start of file name */
343 for (start = end; start >= dos_name; start--)
345 if (IS_SEPARATOR(start[0])) break;
346 /* check for ':' but ignore if before extension (for things like NUL:.txt) */
347 if (start[0] == ':' && start[1] != '.') break;
349 start++;
351 /* remove extension */
352 if ((p = strchrW( start, '.' )))
354 end = p - 1;
355 if (end >= dos_name && *end == ':') end--; /* remove trailing ':' before extension */
357 else
359 /* no extension, remove trailing spaces */
360 while (end >= dos_name && *end == ' ') end--;
363 /* now we have a potential device name between start and end, check it */
364 switch(end - start + 1)
366 case 3:
367 if (strncmpiW( start, auxW, 3 ) &&
368 strncmpiW( start, conW, 3 ) &&
369 strncmpiW( start, nulW, 3 ) &&
370 strncmpiW( start, prnW, 3 )) break;
371 return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
372 case 4:
373 if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
374 if (*end <= '0' || *end > '9') break;
375 return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
376 default: /* can't match anything */
377 break;
379 return 0;
383 /**************************************************************************
384 * RtlDosPathNameToNtPathName_U [NTDLL.@]
386 * dos_path: a DOS path name (fully qualified or not)
387 * ntpath: pointer to a UNICODE_STRING to hold the converted
388 * path name
389 * file_part:will point (in ntpath) to the file part in the path
390 * cd: directory reference (optional)
392 * FIXME:
393 * + fill the cd structure
395 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
396 PUNICODE_STRING ntpath,
397 PWSTR* file_part,
398 CURDIR* cd)
400 static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
401 ULONG sz, offset;
402 WCHAR local[MAX_PATH];
403 LPWSTR ptr;
405 TRACE("(%s,%p,%p,%p)\n",
406 debugstr_w(dos_path), ntpath, file_part, cd);
408 if (cd)
410 FIXME("Unsupported parameter\n");
411 memset(cd, 0, sizeof(*cd));
414 if (!dos_path || !*dos_path) return FALSE;
416 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
418 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
419 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
420 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
421 if (!ntpath->Buffer) return FALSE;
422 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
423 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
424 return TRUE;
427 ptr = local;
428 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
429 if (sz == 0) return FALSE;
430 if (sz > sizeof(local))
432 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return FALSE;
433 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
436 ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
437 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
438 if (!ntpath->Buffer)
440 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
441 return FALSE;
444 strcpyW(ntpath->Buffer, NTDosPrefixW);
445 switch (RtlDetermineDosPathNameType_U(ptr))
447 case UNC_PATH: /* \\foo */
448 offset = 2;
449 strcatW(ntpath->Buffer, UncPfxW);
450 break;
451 case DEVICE_PATH: /* \\.\foo */
452 offset = 4;
453 break;
454 default:
455 offset = 0;
456 break;
459 strcatW(ntpath->Buffer, ptr + offset);
460 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
462 if (file_part && *file_part)
463 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
465 /* FIXME: cd filling */
467 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
468 return TRUE;
471 /******************************************************************
472 * RtlDosSearchPath_U
474 * Searchs a file of name 'name' into a ';' separated list of paths
475 * (stored in paths)
476 * Doesn't seem to search elsewhere than the paths list
477 * Stores the result in buffer (file_part will point to the position
478 * of the file name in the buffer)
479 * FIXME:
480 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
482 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
483 ULONG buffer_size, LPWSTR buffer,
484 LPWSTR* file_part)
486 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
487 ULONG len = 0;
489 if (type == RELATIVE_PATH)
491 ULONG allocated = 0, needed, filelen;
492 WCHAR *name = NULL;
494 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
496 /* Windows only checks for '.' without worrying about path components */
497 if (strchrW( search, '.' )) ext = NULL;
498 if (ext != NULL) filelen += strlenW(ext);
500 while (*paths)
502 LPCWSTR ptr;
504 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
505 if (needed + filelen > allocated)
507 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
508 (needed + filelen) * sizeof(WCHAR));
509 else
511 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
512 (needed + filelen) * sizeof(WCHAR));
513 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
514 name = newname;
516 if (!name) return 0;
517 allocated = needed + filelen;
519 memmove(name, paths, needed * sizeof(WCHAR));
520 /* append '\\' if none is present */
521 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
522 strcpyW(&name[needed], search);
523 if (ext) strcatW(&name[needed], ext);
524 if (RtlDoesFileExists_U(name))
526 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
527 break;
529 paths = ptr;
531 RtlFreeHeap(GetProcessHeap(), 0, name);
533 else if (RtlDoesFileExists_U(search))
535 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
538 return len;
542 /******************************************************************
543 * collapse_path
545 * Helper for RtlGetFullPathName_U.
546 * Get rid of . and .. components in the path.
548 static inline void collapse_path( WCHAR *path, UINT mark )
550 WCHAR *p, *next;
552 /* convert every / into a \ */
553 for (p = path; *p; p++) if (*p == '/') *p = '\\';
555 /* collapse duplicate backslashes */
556 next = path + max( 1, mark );
557 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
558 *next = 0;
560 p = path + mark;
561 while (*p)
563 if (*p == '.')
565 switch(p[1])
567 case '\\': /* .\ component */
568 next = p + 2;
569 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
570 continue;
571 case 0: /* final . */
572 if (p > path + mark) p--;
573 *p = 0;
574 continue;
575 case '.':
576 if (p[2] == '\\') /* ..\ component */
578 next = p + 3;
579 if (p > path + mark)
581 p--;
582 while (p > path + mark && p[-1] != '\\') p--;
584 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
585 continue;
587 else if (!p[2]) /* final .. */
589 if (p > path + mark)
591 p--;
592 while (p > path + mark && p[-1] != '\\') p--;
593 if (p > path + mark) p--;
595 *p = 0;
596 continue;
598 break;
601 /* skip to the next component */
602 while (*p && *p != '\\') p++;
603 if (*p == '\\')
605 /* remove last dot in previous dir name */
606 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (strlenW(p) + 1) * sizeof(WCHAR) );
607 else p++;
611 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
612 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
613 *p = 0;
617 /******************************************************************
618 * skip_unc_prefix
620 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
622 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
624 ptr += 2;
625 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
626 while (IS_SEPARATOR(*ptr)) ptr++;
627 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
628 while (IS_SEPARATOR(*ptr)) ptr++;
629 return ptr;
633 /******************************************************************
634 * get_full_path_helper
636 * Helper for RtlGetFullPathName_U
637 * Note: name and buffer are allowed to point to the same memory spot
639 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
641 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
642 DOS_PATHNAME_TYPE type;
643 LPWSTR ins_str = NULL;
644 LPCWSTR ptr;
645 const UNICODE_STRING* cd;
646 WCHAR tmp[4];
648 /* return error if name only consists of spaces */
649 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
650 if (!*ptr) return 0;
652 RtlAcquirePebLock();
654 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
655 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
656 else
657 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
659 switch (type = RtlDetermineDosPathNameType_U(name))
661 case UNC_PATH: /* \\foo */
662 ptr = skip_unc_prefix( name );
663 mark = (ptr - name);
664 break;
666 case DEVICE_PATH: /* \\.\foo */
667 mark = 4;
668 break;
670 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
671 reqsize = sizeof(WCHAR);
672 tmp[0] = toupperW(name[0]);
673 ins_str = tmp;
674 dep = 1;
675 mark = 3;
676 break;
678 case RELATIVE_DRIVE_PATH: /* c:foo */
679 dep = 2;
680 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
682 UNICODE_STRING var, val;
684 tmp[0] = '=';
685 tmp[1] = name[0];
686 tmp[2] = ':';
687 tmp[3] = '\0';
688 var.Length = 3 * sizeof(WCHAR);
689 var.MaximumLength = 4 * sizeof(WCHAR);
690 var.Buffer = tmp;
691 val.Length = 0;
692 val.MaximumLength = size;
693 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
695 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
697 case STATUS_SUCCESS:
698 /* FIXME: Win2k seems to check that the environment variable actually points
699 * to an existing directory. If not, root of the drive is used
700 * (this seems also to be the only spot in RtlGetFullPathName that the
701 * existence of a part of a path is checked)
703 /* fall thru */
704 case STATUS_BUFFER_TOO_SMALL:
705 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
706 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
707 ins_str = val.Buffer;
708 break;
709 case STATUS_VARIABLE_NOT_FOUND:
710 reqsize = 3 * sizeof(WCHAR);
711 tmp[0] = name[0];
712 tmp[1] = ':';
713 tmp[2] = '\\';
714 ins_str = tmp;
715 break;
716 default:
717 ERR("Unsupported status code\n");
718 break;
720 mark = 3;
721 break;
723 /* fall through */
725 case RELATIVE_PATH: /* foo */
726 reqsize = cd->Length;
727 ins_str = cd->Buffer;
728 if (cd->Buffer[1] != ':')
730 ptr = skip_unc_prefix( cd->Buffer );
731 mark = ptr - cd->Buffer;
733 else mark = 3;
734 break;
736 case ABSOLUTE_PATH: /* \xxx */
737 if (name[0] == '/') /* may be a Unix path */
739 const WCHAR *ptr = name;
740 int drive = find_drive_rootW( &ptr );
741 if (drive != -1)
743 reqsize = 3 * sizeof(WCHAR);
744 tmp[0] = 'A' + drive;
745 tmp[1] = ':';
746 tmp[2] = '\\';
747 ins_str = tmp;
748 mark = 3;
749 dep = ptr - name;
750 break;
753 if (cd->Buffer[1] == ':')
755 reqsize = 2 * sizeof(WCHAR);
756 tmp[0] = cd->Buffer[0];
757 tmp[1] = ':';
758 ins_str = tmp;
759 mark = 3;
761 else
763 ptr = skip_unc_prefix( cd->Buffer );
764 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
765 mark = reqsize / sizeof(WCHAR);
766 ins_str = cd->Buffer;
768 break;
770 case UNC_DOT_PATH: /* \\. */
771 reqsize = 4 * sizeof(WCHAR);
772 dep = 3;
773 tmp[0] = '\\';
774 tmp[1] = '\\';
775 tmp[2] = '.';
776 tmp[3] = '\\';
777 ins_str = tmp;
778 mark = 4;
779 break;
781 case INVALID_PATH:
782 goto done;
785 /* enough space ? */
786 deplen = strlenW(name + dep) * sizeof(WCHAR);
787 if (reqsize + deplen + sizeof(WCHAR) > size)
789 /* not enough space, return need size (including terminating '\0') */
790 reqsize += deplen + sizeof(WCHAR);
791 goto done;
794 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
795 if (reqsize) memcpy(buffer, ins_str, reqsize);
796 reqsize += deplen;
798 if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
799 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
801 collapse_path( buffer, mark );
802 reqsize = strlenW(buffer) * sizeof(WCHAR);
804 done:
805 RtlReleasePebLock();
806 return reqsize;
809 /******************************************************************
810 * RtlGetFullPathName_U (NTDLL.@)
812 * Returns the number of bytes written to buffer (not including the
813 * terminating NULL) if the function succeeds, or the required number of bytes
814 * (including the terminating NULL) if the buffer is too small.
816 * file_part will point to the filename part inside buffer (except if we use
817 * DOS device name, in which case file_in_buf is NULL)
820 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
821 WCHAR** file_part)
823 WCHAR* ptr;
824 DWORD dosdev;
825 DWORD reqsize;
827 TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);
829 if (!name || !*name) return 0;
831 if (file_part) *file_part = NULL;
833 /* check for DOS device name */
834 dosdev = RtlIsDosDeviceName_U(name);
835 if (dosdev)
837 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
838 DWORD sz = LOWORD(dosdev); /* in bytes */
840 if (8 + sz + 2 > size) return sz + 10;
841 strcpyW(buffer, DeviceRootW);
842 memmove(buffer + 4, name + offset, sz);
843 buffer[4 + sz / sizeof(WCHAR)] = '\0';
844 /* file_part isn't set in this case */
845 return sz + 8;
848 reqsize = get_full_path_helper(name, buffer, size);
849 if (!reqsize) return 0;
850 if (reqsize > size)
852 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
853 reqsize = get_full_path_helper(name, tmp, reqsize);
854 if (reqsize > size) /* it may have worked the second time */
856 RtlFreeHeap(GetProcessHeap(), 0, tmp);
857 return reqsize + sizeof(WCHAR);
859 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
860 RtlFreeHeap(GetProcessHeap(), 0, tmp);
863 /* find file part */
864 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
865 *file_part = ptr;
866 return reqsize;
869 /*************************************************************************
870 * RtlGetLongestNtPathLength [NTDLL.@]
872 * Get the longest allowed path length
874 * PARAMS
875 * None.
877 * RETURNS
878 * The longest allowed path length (277 characters under Win2k).
880 DWORD WINAPI RtlGetLongestNtPathLength(void)
882 return MAX_NT_PATH_LENGTH;
885 /******************************************************************
886 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
888 * Returns TRUE iff unicode is a valid DOS (8+3) name.
889 * If the name is valid, oem gets filled with the corresponding OEM string
890 * spaces is set to TRUE if unicode contains spaces
892 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
893 OEM_STRING *oem, BOOLEAN *spaces )
895 static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
896 int dot = -1;
897 int i;
898 char buffer[12];
899 OEM_STRING oem_str;
900 BOOLEAN got_space = FALSE;
902 if (!oem)
904 oem_str.Length = sizeof(buffer);
905 oem_str.MaximumLength = sizeof(buffer);
906 oem_str.Buffer = buffer;
907 oem = &oem_str;
909 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
910 return FALSE;
912 if (oem->Length > 12) return FALSE;
914 /* a starting . is invalid, except for . and .. */
915 if (oem->Buffer[0] == '.')
917 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
918 if (spaces) *spaces = FALSE;
919 return TRUE;
922 for (i = 0; i < oem->Length; i++)
924 switch (oem->Buffer[i])
926 case ' ':
927 /* leading/trailing spaces not allowed */
928 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
929 got_space = TRUE;
930 break;
931 case '.':
932 if (dot != -1) return FALSE;
933 dot = i;
934 break;
935 default:
936 if (strchr(illegal, oem->Buffer[i])) return FALSE;
937 break;
940 /* check file part is shorter than 8, extension shorter than 3
941 * dot cannot be last in string
943 if (dot == -1)
945 if (oem->Length > 8) return FALSE;
947 else
949 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
951 if (spaces) *spaces = got_space;
952 return TRUE;
955 /******************************************************************
956 * RtlGetCurrentDirectory_U (NTDLL.@)
959 NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
961 UNICODE_STRING* us;
962 ULONG len;
964 TRACE("(%lu %p)\n", buflen, buf);
966 RtlAcquirePebLock();
968 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
969 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
970 else
971 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
973 len = us->Length / sizeof(WCHAR);
974 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
975 len--;
977 if (buflen / sizeof(WCHAR) > len)
979 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
980 buf[len] = '\0';
982 else
984 len++;
987 RtlReleasePebLock();
989 return len * sizeof(WCHAR);
992 /******************************************************************
993 * RtlSetCurrentDirectory_U (NTDLL.@)
996 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
998 FILE_FS_DEVICE_INFORMATION device_info;
999 OBJECT_ATTRIBUTES attr;
1000 UNICODE_STRING newdir;
1001 IO_STATUS_BLOCK io;
1002 CURDIR *curdir;
1003 HANDLE handle;
1004 NTSTATUS nts;
1005 ULONG size;
1006 PWSTR ptr;
1008 newdir.Buffer = NULL;
1010 RtlAcquirePebLock();
1012 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
1013 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
1014 else
1015 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
1017 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
1019 nts = STATUS_OBJECT_NAME_INVALID;
1020 goto out;
1023 attr.Length = sizeof(attr);
1024 attr.RootDirectory = 0;
1025 attr.Attributes = OBJ_CASE_INSENSITIVE;
1026 attr.ObjectName = &newdir;
1027 attr.SecurityDescriptor = NULL;
1028 attr.SecurityQualityOfService = NULL;
1030 nts = NtOpenFile( &handle, 0, &attr, &io, 0, FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1031 if (nts != STATUS_SUCCESS) goto out;
1033 /* don't keep the directory handle open on removable media */
1034 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
1035 sizeof(device_info), FileFsDeviceInformation ) &&
1036 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
1038 NtClose( handle );
1039 handle = 0;
1042 if (curdir->Handle) NtClose( curdir->Handle );
1043 curdir->Handle = handle;
1045 /* append trailing \ if missing */
1046 size = newdir.Length / sizeof(WCHAR);
1047 ptr = newdir.Buffer;
1048 ptr += 4; /* skip \??\ prefix */
1049 size -= 4;
1050 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
1052 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
1053 curdir->DosPath.Buffer[size] = 0;
1054 curdir->DosPath.Length = size * sizeof(WCHAR);
1056 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
1058 out:
1059 RtlFreeUnicodeString( &newdir );
1060 RtlReleasePebLock();
1061 return nts;
1065 /******************************************************************
1066 * wine_unix_to_nt_file_name (NTDLL.@) Not a Windows API
1068 NTSTATUS wine_unix_to_nt_file_name( const ANSI_STRING *name, UNICODE_STRING *nt )
1070 static const WCHAR prefixW[] = {'\\','?','?','\\','a',':','\\'};
1071 unsigned int lenW, lenA = name->Length;
1072 const char *path = name->Buffer;
1073 char *cwd;
1074 WCHAR *p;
1075 NTSTATUS status;
1076 int drive;
1078 if (!lenA || path[0] != '/')
1080 char *newcwd, *end;
1081 size_t size;
1083 if ((status = DIR_get_unix_cwd( &cwd )) != STATUS_SUCCESS) return status;
1085 size = strlen(cwd) + lenA + 1;
1086 if (!(newcwd = RtlReAllocateHeap( GetProcessHeap(), 0, cwd, size )))
1088 status = STATUS_NO_MEMORY;
1089 goto done;
1091 cwd = newcwd;
1092 end = cwd + strlen(cwd);
1093 if (end > cwd && end[-1] != '/') *end++ = '/';
1094 memcpy( end, path, lenA );
1095 lenA += end - cwd;
1096 path = cwd;
1098 status = find_drive_rootA( &path, lenA, &drive );
1099 lenA -= (path - cwd);
1101 else
1103 cwd = NULL;
1104 status = find_drive_rootA( &path, lenA, &drive );
1105 lenA -= (path - name->Buffer);
1108 if (status != STATUS_SUCCESS) goto done;
1109 while (lenA && path[0] == '/') { lenA--; path++; }
1111 lenW = ntdll_umbstowcs( 0, path, lenA, NULL, 0 );
1112 if (!(nt->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1113 (lenW + 1) * sizeof(WCHAR) + sizeof(prefixW) )))
1115 status = STATUS_NO_MEMORY;
1116 goto done;
1119 memcpy( nt->Buffer, prefixW, sizeof(prefixW) );
1120 nt->Buffer[4] += drive;
1121 ntdll_umbstowcs( 0, path, lenA, nt->Buffer + sizeof(prefixW)/sizeof(WCHAR), lenW );
1122 lenW += sizeof(prefixW)/sizeof(WCHAR);
1123 nt->Buffer[lenW] = 0;
1124 nt->Length = lenW * sizeof(WCHAR);
1125 nt->MaximumLength = nt->Length + sizeof(WCHAR);
1126 for (p = nt->Buffer + sizeof(prefixW)/sizeof(WCHAR); *p; p++) if (*p == '/') *p = '\\';
1128 done:
1129 RtlFreeHeap( GetProcessHeap(), 0, cwd );
1130 return status;