user32: Support forcing the DPI awareness through the image file execution options.
[wine.git] / dlls / ntdll / path.c
blob3062787ca701cf656a78749877dde13b3bd0ab32
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;
328 /**************************************************************************
329 * RtlDosPathNameToNtPathName_U_WithStatus [NTDLL.@]
331 * dos_path: a DOS path name (fully qualified or not)
332 * ntpath: pointer to a UNICODE_STRING to hold the converted
333 * path name
334 * file_part:will point (in ntpath) to the file part in the path
335 * cd: directory reference (optional)
337 * FIXME:
338 * + fill the cd structure
340 NTSTATUS WINAPI RtlDosPathNameToNtPathName_U_WithStatus(const WCHAR *dos_path, UNICODE_STRING *ntpath,
341 WCHAR **file_part, CURDIR *cd)
343 static const WCHAR LongFileNamePfxW[] = {'\\','\\','?','\\'};
344 ULONG sz, offset;
345 WCHAR local[MAX_PATH];
346 LPWSTR ptr;
348 TRACE("(%s,%p,%p,%p)\n", debugstr_w(dos_path), ntpath, file_part, cd);
350 if (cd)
352 FIXME("Unsupported parameter\n");
353 memset(cd, 0, sizeof(*cd));
356 if (!dos_path || !*dos_path)
357 return STATUS_OBJECT_NAME_INVALID;
359 if (!strncmpW(dos_path, LongFileNamePfxW, 4))
361 ntpath->Length = strlenW(dos_path) * sizeof(WCHAR);
362 ntpath->MaximumLength = ntpath->Length + sizeof(WCHAR);
363 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
364 if (!ntpath->Buffer) return STATUS_NO_MEMORY;
365 memcpy( ntpath->Buffer, dos_path, ntpath->MaximumLength );
366 ntpath->Buffer[1] = '?'; /* change \\?\ to \??\ */
367 if (file_part)
369 if ((ptr = strrchrW( ntpath->Buffer, '\\' )) && ptr[1]) *file_part = ptr + 1;
370 else *file_part = NULL;
372 return STATUS_SUCCESS;
375 ptr = local;
376 sz = RtlGetFullPathName_U(dos_path, sizeof(local), ptr, file_part);
377 if (sz == 0) return STATUS_OBJECT_NAME_INVALID;
379 if (sz > sizeof(local))
381 if (!(ptr = RtlAllocateHeap(GetProcessHeap(), 0, sz))) return STATUS_NO_MEMORY;
382 sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
384 sz += (1 /* NUL */ + 4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
385 if (sz > MAXWORD)
387 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
388 return STATUS_OBJECT_NAME_INVALID;
391 ntpath->MaximumLength = sz;
392 ntpath->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, ntpath->MaximumLength);
393 if (!ntpath->Buffer)
395 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
396 return STATUS_NO_MEMORY;
399 strcpyW(ntpath->Buffer, NTDosPrefixW);
400 switch (RtlDetermineDosPathNameType_U(ptr))
402 case UNC_PATH: /* \\foo */
403 offset = 2;
404 strcatW(ntpath->Buffer, UncPfxW);
405 break;
406 case DEVICE_PATH: /* \\.\foo */
407 offset = 4;
408 break;
409 default:
410 offset = 0;
411 break;
414 strcatW(ntpath->Buffer, ptr + offset);
415 ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);
417 if (file_part && *file_part)
418 *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
420 /* FIXME: cd filling */
422 if (ptr != local) RtlFreeHeap(GetProcessHeap(), 0, ptr);
423 return STATUS_SUCCESS;
426 /**************************************************************************
427 * RtlDosPathNameToNtPathName_U [NTDLL.@]
429 * See RtlDosPathNameToNtPathName_U_WithStatus
431 BOOLEAN WINAPI RtlDosPathNameToNtPathName_U(PCWSTR dos_path,
432 PUNICODE_STRING ntpath,
433 PWSTR* file_part,
434 CURDIR* cd)
436 return RtlDosPathNameToNtPathName_U_WithStatus(dos_path, ntpath, file_part, cd) == STATUS_SUCCESS;
439 /******************************************************************
440 * RtlDosSearchPath_U
442 * Searches a file of name 'name' into a ';' separated list of paths
443 * (stored in paths)
444 * Doesn't seem to search elsewhere than the paths list
445 * Stores the result in buffer (file_part will point to the position
446 * of the file name in the buffer)
447 * FIXME:
448 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
450 ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext,
451 ULONG buffer_size, LPWSTR buffer,
452 LPWSTR* file_part)
454 DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
455 ULONG len = 0;
457 if (type == RELATIVE_PATH)
459 ULONG allocated = 0, needed, filelen;
460 WCHAR *name = NULL;
462 filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;
464 /* Windows only checks for '.' without worrying about path components */
465 if (strchrW( search, '.' )) ext = NULL;
466 if (ext != NULL) filelen += strlenW(ext);
468 while (*paths)
470 LPCWSTR ptr;
472 for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
473 if (needed + filelen > allocated)
475 if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
476 (needed + filelen) * sizeof(WCHAR));
477 else
479 WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
480 (needed + filelen) * sizeof(WCHAR));
481 if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
482 name = newname;
484 if (!name) return 0;
485 allocated = needed + filelen;
487 memmove(name, paths, needed * sizeof(WCHAR));
488 /* append '\\' if none is present */
489 if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
490 strcpyW(&name[needed], search);
491 if (ext) strcatW(&name[needed], ext);
492 if (RtlDoesFileExists_U(name))
494 len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
495 break;
497 paths = ptr;
499 RtlFreeHeap(GetProcessHeap(), 0, name);
501 else if (RtlDoesFileExists_U(search))
503 len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
506 return len;
510 /******************************************************************
511 * collapse_path
513 * Helper for RtlGetFullPathName_U.
514 * Get rid of . and .. components in the path.
516 static inline void collapse_path( WCHAR *path, UINT mark )
518 WCHAR *p, *next;
520 /* convert every / into a \ */
521 for (p = path; *p; p++) if (*p == '/') *p = '\\';
523 /* collapse duplicate backslashes */
524 next = path + max( 1, mark );
525 for (p = next; *p; p++) if (*p != '\\' || next[-1] != '\\') *next++ = *p;
526 *next = 0;
528 p = path + mark;
529 while (*p)
531 if (*p == '.')
533 switch(p[1])
535 case '\\': /* .\ component */
536 next = p + 2;
537 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
538 continue;
539 case 0: /* final . */
540 if (p > path + mark) p--;
541 *p = 0;
542 continue;
543 case '.':
544 if (p[2] == '\\') /* ..\ component */
546 next = p + 3;
547 if (p > path + mark)
549 p--;
550 while (p > path + mark && p[-1] != '\\') p--;
552 memmove( p, next, (strlenW(next) + 1) * sizeof(WCHAR) );
553 continue;
555 else if (!p[2]) /* final .. */
557 if (p > path + mark)
559 p--;
560 while (p > path + mark && p[-1] != '\\') p--;
561 if (p > path + mark) p--;
563 *p = 0;
564 continue;
566 break;
569 /* skip to the next component */
570 while (*p && *p != '\\') p++;
571 if (*p == '\\')
573 /* remove last dot in previous dir name */
574 if (p > path + mark && p[-1] == '.') memmove( p-1, p, (strlenW(p) + 1) * sizeof(WCHAR) );
575 else p++;
579 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
580 while (p > path + mark && (p[-1] == ' ' || p[-1] == '.')) p--;
581 *p = 0;
585 /******************************************************************
586 * skip_unc_prefix
588 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
590 static const WCHAR *skip_unc_prefix( const WCHAR *ptr )
592 ptr += 2;
593 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* share name */
594 while (IS_SEPARATOR(*ptr)) ptr++;
595 while (*ptr && !IS_SEPARATOR(*ptr)) ptr++; /* dir name */
596 while (IS_SEPARATOR(*ptr)) ptr++;
597 return ptr;
601 /******************************************************************
602 * get_full_path_helper
604 * Helper for RtlGetFullPathName_U
605 * Note: name and buffer are allowed to point to the same memory spot
607 static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
609 ULONG reqsize = 0, mark = 0, dep = 0, deplen;
610 LPWSTR ins_str = NULL;
611 LPCWSTR ptr;
612 const UNICODE_STRING* cd;
613 WCHAR tmp[4];
615 /* return error if name only consists of spaces */
616 for (ptr = name; *ptr; ptr++) if (*ptr != ' ') break;
617 if (!*ptr) return 0;
619 RtlAcquirePebLock();
621 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
622 cd = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
623 else
624 cd = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
626 switch (RtlDetermineDosPathNameType_U(name))
628 case UNC_PATH: /* \\foo */
629 ptr = skip_unc_prefix( name );
630 mark = (ptr - name);
631 break;
633 case DEVICE_PATH: /* \\.\foo */
634 mark = 4;
635 break;
637 case ABSOLUTE_DRIVE_PATH: /* c:\foo */
638 reqsize = sizeof(WCHAR);
639 tmp[0] = name[0];
640 ins_str = tmp;
641 dep = 1;
642 mark = 3;
643 break;
645 case RELATIVE_DRIVE_PATH: /* c:foo */
646 dep = 2;
647 if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
649 UNICODE_STRING var, val;
651 tmp[0] = '=';
652 tmp[1] = name[0];
653 tmp[2] = ':';
654 tmp[3] = '\0';
655 var.Length = 3 * sizeof(WCHAR);
656 var.MaximumLength = 4 * sizeof(WCHAR);
657 var.Buffer = tmp;
658 val.Length = 0;
659 val.MaximumLength = size;
660 val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
662 switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
664 case STATUS_SUCCESS:
665 /* FIXME: Win2k seems to check that the environment variable actually points
666 * to an existing directory. If not, root of the drive is used
667 * (this seems also to be the only spot in RtlGetFullPathName that the
668 * existence of a part of a path is checked)
670 /* fall through */
671 case STATUS_BUFFER_TOO_SMALL:
672 reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
673 val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
674 ins_str = val.Buffer;
675 break;
676 case STATUS_VARIABLE_NOT_FOUND:
677 reqsize = 3 * sizeof(WCHAR);
678 tmp[0] = name[0];
679 tmp[1] = ':';
680 tmp[2] = '\\';
681 ins_str = tmp;
682 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
683 break;
684 default:
685 ERR("Unsupported status code\n");
686 RtlFreeHeap(GetProcessHeap(), 0, val.Buffer);
687 break;
689 mark = 3;
690 break;
692 /* fall through */
694 case RELATIVE_PATH: /* foo */
695 reqsize = cd->Length;
696 ins_str = cd->Buffer;
697 if (cd->Buffer[1] != ':')
699 ptr = skip_unc_prefix( cd->Buffer );
700 mark = ptr - cd->Buffer;
702 else mark = 3;
703 break;
705 case ABSOLUTE_PATH: /* \xxx */
706 if (name[0] == '/') /* may be a Unix path */
708 const WCHAR *ptr = name;
709 int drive = find_drive_rootW( &ptr );
710 if (drive != -1)
712 reqsize = 3 * sizeof(WCHAR);
713 tmp[0] = 'A' + drive;
714 tmp[1] = ':';
715 tmp[2] = '\\';
716 ins_str = tmp;
717 mark = 3;
718 dep = ptr - name;
719 break;
722 if (cd->Buffer[1] == ':')
724 reqsize = 2 * sizeof(WCHAR);
725 tmp[0] = cd->Buffer[0];
726 tmp[1] = ':';
727 ins_str = tmp;
728 mark = 3;
730 else
732 ptr = skip_unc_prefix( cd->Buffer );
733 reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
734 mark = reqsize / sizeof(WCHAR);
735 ins_str = cd->Buffer;
737 break;
739 case UNC_DOT_PATH: /* \\. */
740 reqsize = 4 * sizeof(WCHAR);
741 dep = 3;
742 tmp[0] = '\\';
743 tmp[1] = '\\';
744 tmp[2] = '.';
745 tmp[3] = '\\';
746 ins_str = tmp;
747 mark = 4;
748 break;
750 case INVALID_PATH:
751 goto done;
754 /* enough space ? */
755 deplen = strlenW(name + dep) * sizeof(WCHAR);
756 if (reqsize + deplen + sizeof(WCHAR) > size)
758 /* not enough space, return need size (including terminating '\0') */
759 reqsize += deplen + sizeof(WCHAR);
760 goto done;
763 memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
764 if (reqsize) memcpy(buffer, ins_str, reqsize);
766 if (ins_str != tmp && ins_str != cd->Buffer)
767 RtlFreeHeap(GetProcessHeap(), 0, ins_str);
769 collapse_path( buffer, mark );
770 reqsize = strlenW(buffer) * sizeof(WCHAR);
772 done:
773 RtlReleasePebLock();
774 return reqsize;
777 /******************************************************************
778 * RtlGetFullPathName_U (NTDLL.@)
780 * Returns the number of bytes written to buffer (not including the
781 * terminating NULL) if the function succeeds, or the required number of bytes
782 * (including the terminating NULL) if the buffer is too small.
784 * file_part will point to the filename part inside buffer (except if we use
785 * DOS device name, in which case file_in_buf is NULL)
788 DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
789 WCHAR** file_part)
791 WCHAR* ptr;
792 DWORD dosdev;
793 DWORD reqsize;
795 TRACE("(%s %u %p %p)\n", debugstr_w(name), size, buffer, file_part);
797 if (!name || !*name) return 0;
799 if (file_part) *file_part = NULL;
801 /* check for DOS device name */
802 dosdev = RtlIsDosDeviceName_U(name);
803 if (dosdev)
805 DWORD offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
806 DWORD sz = LOWORD(dosdev); /* in bytes */
808 if (8 + sz + 2 > size) return sz + 10;
809 strcpyW(buffer, DeviceRootW);
810 memmove(buffer + 4, name + offset, sz);
811 buffer[4 + sz / sizeof(WCHAR)] = '\0';
812 /* file_part isn't set in this case */
813 return sz + 8;
816 reqsize = get_full_path_helper(name, buffer, size);
817 if (!reqsize) return 0;
818 if (reqsize > size)
820 LPWSTR tmp = RtlAllocateHeap(GetProcessHeap(), 0, reqsize);
821 reqsize = get_full_path_helper(name, tmp, reqsize);
822 if (reqsize + sizeof(WCHAR) > size) /* it may have worked the second time */
824 RtlFreeHeap(GetProcessHeap(), 0, tmp);
825 return reqsize + sizeof(WCHAR);
827 memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
828 RtlFreeHeap(GetProcessHeap(), 0, tmp);
831 /* find file part */
832 if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
833 *file_part = ptr;
834 return reqsize;
837 /*************************************************************************
838 * RtlGetLongestNtPathLength [NTDLL.@]
840 * Get the longest allowed path length
842 * PARAMS
843 * None.
845 * RETURNS
846 * The longest allowed path length (277 characters under Win2k).
848 DWORD WINAPI RtlGetLongestNtPathLength(void)
850 return MAX_NT_PATH_LENGTH;
853 /******************************************************************
854 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
856 * Returns TRUE iff unicode is a valid DOS (8+3) name.
857 * If the name is valid, oem gets filled with the corresponding OEM string
858 * spaces is set to TRUE if unicode contains spaces
860 BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
861 OEM_STRING *oem, BOOLEAN *spaces )
863 static const char illegal[] = "*?<>|\"+=,;[]:/\\\345";
864 int dot = -1;
865 int i;
866 char buffer[12];
867 OEM_STRING oem_str;
868 BOOLEAN got_space = FALSE;
870 if (!oem)
872 oem_str.Length = sizeof(buffer);
873 oem_str.MaximumLength = sizeof(buffer);
874 oem_str.Buffer = buffer;
875 oem = &oem_str;
877 if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
878 return FALSE;
880 if (oem->Length > 12) return FALSE;
882 /* a starting . is invalid, except for . and .. */
883 if (oem->Length > 0 && oem->Buffer[0] == '.')
885 if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
886 if (spaces) *spaces = FALSE;
887 return TRUE;
890 for (i = 0; i < oem->Length; i++)
892 switch (oem->Buffer[i])
894 case ' ':
895 /* leading/trailing spaces not allowed */
896 if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
897 got_space = TRUE;
898 break;
899 case '.':
900 if (dot != -1) return FALSE;
901 dot = i;
902 break;
903 default:
904 if (strchr(illegal, oem->Buffer[i])) return FALSE;
905 break;
908 /* check file part is shorter than 8, extension shorter than 3
909 * dot cannot be last in string
911 if (dot == -1)
913 if (oem->Length > 8) return FALSE;
915 else
917 if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
919 if (spaces) *spaces = got_space;
920 return TRUE;
923 /******************************************************************
924 * RtlGetCurrentDirectory_U (NTDLL.@)
927 ULONG WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
929 UNICODE_STRING* us;
930 ULONG len;
932 TRACE("(%u %p)\n", buflen, buf);
934 RtlAcquirePebLock();
936 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
937 us = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir.DosPath;
938 else
939 us = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory.DosPath;
941 len = us->Length / sizeof(WCHAR);
942 if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
943 len--;
945 if (buflen / sizeof(WCHAR) > len)
947 memcpy(buf, us->Buffer, len * sizeof(WCHAR));
948 buf[len] = '\0';
950 else
952 len++;
955 RtlReleasePebLock();
957 return len * sizeof(WCHAR);
960 /******************************************************************
961 * RtlSetCurrentDirectory_U (NTDLL.@)
964 NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
966 FILE_FS_DEVICE_INFORMATION device_info;
967 OBJECT_ATTRIBUTES attr;
968 UNICODE_STRING newdir;
969 IO_STATUS_BLOCK io;
970 CURDIR *curdir;
971 HANDLE handle;
972 NTSTATUS nts;
973 ULONG size;
974 PWSTR ptr;
976 newdir.Buffer = NULL;
978 RtlAcquirePebLock();
980 if (NtCurrentTeb()->Tib.SubSystemTib) /* FIXME: hack */
981 curdir = &((WIN16_SUBSYSTEM_TIB *)NtCurrentTeb()->Tib.SubSystemTib)->curdir;
982 else
983 curdir = &NtCurrentTeb()->Peb->ProcessParameters->CurrentDirectory;
985 if (!RtlDosPathNameToNtPathName_U( dir->Buffer, &newdir, NULL, NULL ))
987 nts = STATUS_OBJECT_NAME_INVALID;
988 goto out;
991 attr.Length = sizeof(attr);
992 attr.RootDirectory = 0;
993 attr.Attributes = OBJ_CASE_INSENSITIVE;
994 attr.ObjectName = &newdir;
995 attr.SecurityDescriptor = NULL;
996 attr.SecurityQualityOfService = NULL;
998 nts = NtOpenFile( &handle, FILE_TRAVERSE | SYNCHRONIZE, &attr, &io, FILE_SHARE_READ | FILE_SHARE_WRITE,
999 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1000 if (nts != STATUS_SUCCESS) goto out;
1002 /* don't keep the directory handle open on removable media */
1003 if (!NtQueryVolumeInformationFile( handle, &io, &device_info,
1004 sizeof(device_info), FileFsDeviceInformation ) &&
1005 (device_info.Characteristics & FILE_REMOVABLE_MEDIA))
1007 NtClose( handle );
1008 handle = 0;
1011 if (curdir->Handle) NtClose( curdir->Handle );
1012 curdir->Handle = handle;
1014 /* append trailing \ if missing */
1015 size = newdir.Length / sizeof(WCHAR);
1016 ptr = newdir.Buffer;
1017 ptr += 4; /* skip \??\ prefix */
1018 size -= 4;
1019 if (size && ptr[size - 1] != '\\') ptr[size++] = '\\';
1021 memcpy( curdir->DosPath.Buffer, ptr, size * sizeof(WCHAR));
1022 curdir->DosPath.Buffer[size] = 0;
1023 curdir->DosPath.Length = size * sizeof(WCHAR);
1025 TRACE( "curdir now %s %p\n", debugstr_w(curdir->DosPath.Buffer), curdir->Handle );
1027 out:
1028 RtlFreeUnicodeString( &newdir );
1029 RtlReleasePebLock();
1030 return nts;
1034 /******************************************************************
1035 * wine_unix_to_nt_file_name (NTDLL.@) Not a Windows API
1037 NTSTATUS CDECL wine_unix_to_nt_file_name( const ANSI_STRING *name, UNICODE_STRING *nt )
1039 static const WCHAR prefixW[] = {'\\','?','?','\\','A',':','\\'};
1040 static const WCHAR unix_prefixW[] = {'\\','?','?','\\','u','n','i','x'};
1041 unsigned int lenW, lenA = name->Length;
1042 const char *path = name->Buffer;
1043 char *cwd;
1044 WCHAR *p;
1045 NTSTATUS status;
1046 int drive;
1048 if (!lenA || path[0] != '/')
1050 char *newcwd, *end;
1051 size_t size;
1053 if ((status = DIR_get_unix_cwd( &cwd )) != STATUS_SUCCESS) return status;
1055 size = strlen(cwd) + lenA + 1;
1056 if (!(newcwd = RtlReAllocateHeap( GetProcessHeap(), 0, cwd, size )))
1058 status = STATUS_NO_MEMORY;
1059 goto done;
1061 cwd = newcwd;
1062 end = cwd + strlen(cwd);
1063 if (end > cwd && end[-1] != '/') *end++ = '/';
1064 memcpy( end, path, lenA );
1065 lenA += end - cwd;
1066 path = cwd;
1068 status = find_drive_rootA( &path, lenA, &drive );
1069 lenA -= (path - cwd);
1071 else
1073 cwd = NULL;
1074 status = find_drive_rootA( &path, lenA, &drive );
1075 lenA -= (path - name->Buffer);
1078 if (status != STATUS_SUCCESS)
1080 if (status == STATUS_OBJECT_PATH_NOT_FOUND)
1082 lenW = ntdll_umbstowcs( 0, path, lenA, NULL, 0 );
1083 nt->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1084 (lenW + 1) * sizeof(WCHAR) + sizeof(unix_prefixW) );
1085 if (nt->Buffer == NULL)
1087 status = STATUS_NO_MEMORY;
1088 goto done;
1090 memcpy( nt->Buffer, unix_prefixW, sizeof(unix_prefixW) );
1091 ntdll_umbstowcs( 0, path, lenA, nt->Buffer + sizeof(unix_prefixW)/sizeof(WCHAR), lenW );
1092 lenW += sizeof(unix_prefixW)/sizeof(WCHAR);
1093 nt->Buffer[lenW] = 0;
1094 nt->Length = lenW * sizeof(WCHAR);
1095 nt->MaximumLength = nt->Length + sizeof(WCHAR);
1096 for (p = nt->Buffer + sizeof(unix_prefixW)/sizeof(WCHAR); *p; p++) if (*p == '/') *p = '\\';
1097 status = STATUS_SUCCESS;
1099 goto done;
1101 while (lenA && path[0] == '/') { lenA--; path++; }
1103 lenW = ntdll_umbstowcs( 0, path, lenA, NULL, 0 );
1104 if (!(nt->Buffer = RtlAllocateHeap( GetProcessHeap(), 0,
1105 (lenW + 1) * sizeof(WCHAR) + sizeof(prefixW) )))
1107 status = STATUS_NO_MEMORY;
1108 goto done;
1111 memcpy( nt->Buffer, prefixW, sizeof(prefixW) );
1112 nt->Buffer[4] += drive;
1113 ntdll_umbstowcs( 0, path, lenA, nt->Buffer + sizeof(prefixW)/sizeof(WCHAR), lenW );
1114 lenW += sizeof(prefixW)/sizeof(WCHAR);
1115 nt->Buffer[lenW] = 0;
1116 nt->Length = lenW * sizeof(WCHAR);
1117 nt->MaximumLength = nt->Length + sizeof(WCHAR);
1118 for (p = nt->Buffer + sizeof(prefixW)/sizeof(WCHAR); *p; p++) if (*p == '/') *p = '\\';
1120 done:
1121 RtlFreeHeap( GetProcessHeap(), 0, cwd );
1122 return status;