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
23 #include "wine/port.h"
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
36 #define WIN32_NO_STATUS
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
)
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 '..'? */
75 /* strip off trailing slashes */
76 while (prev
> 1 && path
[prev
- 1] == '/') prev
--;
83 /***********************************************************************
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.
100 const char *path
= *ppath
;
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
);
117 if (!stat( buffer
, &st
) && S_ISDIR( st
.st_mode
))
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
));
129 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
130 return STATUS_SUCCESS
;
134 if (len
<= 1) break; /* reached root */
135 len
= remove_last_componentA( buffer
, len
);
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
)
154 /* find start of the last path component */
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 '..'? */
166 /* strip off trailing slashes */
167 while (prev
> 1 && IS_SEPARATOR(path
[prev
- 1])) prev
--;
174 /***********************************************************************
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
;
191 const WCHAR
*path
= *ppath
;
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
);
207 for (p
= buffer
; *p
; p
++) if (*p
== '\\') *p
= '/';
211 if (!stat( buffer
, &st
) && S_ISDIR( st
.st_mode
))
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
));
222 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
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
);
234 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
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
; /* "//." */
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
[3] = {'A','U','X'};
273 static const WCHAR comW
[3] = {'C','O','M'};
274 static const WCHAR conW
[3] = {'C','O','N'};
275 static const WCHAR lptW
[3] = {'L','P','T'};
276 static const WCHAR nulW
[3] = {'N','U','L'};
277 static const WCHAR prnW
[3] = {'P','R','N'};
279 const WCHAR
*start
, *end
, *p
;
281 switch(RtlDetermineDosPathNameType_U( dos_name
))
287 if (!strcmpiW( dos_name
, consoleW
))
288 return MAKELONG( sizeof(conW
), 4 * sizeof(WCHAR
) ); /* 4 is length of \\.\ prefix */
290 case ABSOLUTE_DRIVE_PATH
:
291 case RELATIVE_DRIVE_PATH
:
292 start
= dos_name
+ 2; /* skip drive letter */
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;
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)
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
) );
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 */
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
335 * file_part:will point (in ntpath) to the file part in the path
336 * cd: directory reference (optional)
339 * + fill the cd structure
341 BOOLEAN WINAPI
RtlDosPathNameToNtPathName_U(PCWSTR dos_path
,
342 PUNICODE_STRING ntpath
,
346 static const WCHAR LongFileNamePfxW
[4] = {'\\','\\','?','\\'};
348 WCHAR local
[MAX_PATH
];
351 TRACE("(%s,%p,%p,%p)\n",
352 debugstr_w(dos_path
), ntpath
, file_part
, 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 \??\ */
372 if ((ptr
= strrchrW( ntpath
->Buffer
, '\\' )) && ptr
[1]) *file_part
= ptr
+ 1;
373 else *file_part
= NULL
;
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
);
387 ntpath
->MaximumLength
= sz
+ (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR
);
388 ntpath
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, ntpath
->MaximumLength
);
391 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
395 strcpyW(ntpath
->Buffer
, NTDosPrefixW
);
396 switch (RtlDetermineDosPathNameType_U(ptr
))
398 case UNC_PATH
: /* \\foo */
400 strcatW(ntpath
->Buffer
, UncPfxW
);
402 case DEVICE_PATH
: /* \\.\foo */
410 strcatW(ntpath
->Buffer
, ptr
+ offset
);
411 ntpath
->Length
= strlenW(ntpath
->Buffer
) * sizeof(WCHAR
);
413 if (file_part
&& *file_part
)
414 *file_part
= ntpath
->Buffer
+ ntpath
->Length
/ sizeof(WCHAR
) - strlenW(*file_part
);
416 /* FIXME: cd filling */
418 if (ptr
!= local
) RtlFreeHeap(GetProcessHeap(), 0, ptr
);
422 /******************************************************************
425 * Searches a file of name 'name' into a ';' separated list of paths
427 * Doesn't seem to search elsewhere than the paths list
428 * Stores the result in buffer (file_part will point to the position
429 * of the file name in the buffer)
431 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
433 ULONG WINAPI
RtlDosSearchPath_U(LPCWSTR paths
, LPCWSTR search
, LPCWSTR ext
,
434 ULONG buffer_size
, LPWSTR buffer
,
437 DOS_PATHNAME_TYPE type
= RtlDetermineDosPathNameType_U(search
);
440 if (type
== RELATIVE_PATH
)
442 ULONG allocated
= 0, needed
, filelen
;
445 filelen
= 1 /* for \ */ + strlenW(search
) + 1 /* \0 */;
447 /* Windows only checks for '.' without worrying about path components */
448 if (strchrW( search
, '.' )) ext
= NULL
;
449 if (ext
!= NULL
) filelen
+= strlenW(ext
);
455 for (needed
= 0, ptr
= paths
; *ptr
!= 0 && *ptr
++ != ';'; needed
++);
456 if (needed
+ filelen
> allocated
)
458 if (!name
) name
= RtlAllocateHeap(GetProcessHeap(), 0,
459 (needed
+ filelen
) * sizeof(WCHAR
));
462 WCHAR
*newname
= RtlReAllocateHeap(GetProcessHeap(), 0, name
,
463 (needed
+ filelen
) * sizeof(WCHAR
));
464 if (!newname
) RtlFreeHeap(GetProcessHeap(), 0, name
);
468 allocated
= needed
+ filelen
;
470 memmove(name
, paths
, needed
* sizeof(WCHAR
));
471 /* append '\\' if none is present */
472 if (needed
> 0 && name
[needed
- 1] != '\\') name
[needed
++] = '\\';
473 strcpyW(&name
[needed
], search
);
474 if (ext
) strcatW(&name
[needed
], ext
);
475 if (RtlDoesFileExists_U(name
))
477 len
= RtlGetFullPathName_U(name
, buffer_size
, buffer
, file_part
);
482 RtlFreeHeap(GetProcessHeap(), 0, name
);
484 else if (RtlDoesFileExists_U(search
))
486 len
= RtlGetFullPathName_U(search
, buffer_size
, buffer
, file_part
);
493 /******************************************************************
496 * Helper for RtlGetFullPathName_U.
497 * Get rid of . and .. components in the path.
499 static inline void collapse_path( WCHAR
*path
, UINT mark
)
503 /* convert every / into a \ */
504 for (p
= path
; *p
; p
++) if (*p
== '/') *p
= '\\';
506 /* collapse duplicate backslashes */
507 next
= path
+ max( 1, mark
);
508 for (p
= next
; *p
; p
++) if (*p
!= '\\' || next
[-1] != '\\') *next
++ = *p
;
518 case '\\': /* .\ component */
520 memmove( p
, next
, (strlenW(next
) + 1) * sizeof(WCHAR
) );
522 case 0: /* final . */
523 if (p
> path
+ mark
) p
--;
527 if (p
[2] == '\\') /* ..\ component */
533 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
535 memmove( p
, next
, (strlenW(next
) + 1) * sizeof(WCHAR
) );
538 else if (!p
[2]) /* final .. */
543 while (p
> path
+ mark
&& p
[-1] != '\\') p
--;
544 if (p
> path
+ mark
) p
--;
552 /* skip to the next component */
553 while (*p
&& *p
!= '\\') p
++;
556 /* remove last dot in previous dir name */
557 if (p
> path
+ mark
&& p
[-1] == '.') memmove( p
-1, p
, (strlenW(p
) + 1) * sizeof(WCHAR
) );
562 /* remove trailing spaces and dots (yes, Windows really does that, don't ask) */
563 while (p
> path
+ mark
&& (p
[-1] == ' ' || p
[-1] == '.')) p
--;
568 /******************************************************************
571 * Skip the \\share\dir\ part of a file name. Helper for RtlGetFullPathName_U.
573 static const WCHAR
*skip_unc_prefix( const WCHAR
*ptr
)
576 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* share name */
577 while (IS_SEPARATOR(*ptr
)) ptr
++;
578 while (*ptr
&& !IS_SEPARATOR(*ptr
)) ptr
++; /* dir name */
579 while (IS_SEPARATOR(*ptr
)) ptr
++;
584 /******************************************************************
585 * get_full_path_helper
587 * Helper for RtlGetFullPathName_U
588 * Note: name and buffer are allowed to point to the same memory spot
590 static ULONG
get_full_path_helper(LPCWSTR name
, LPWSTR buffer
, ULONG size
)
592 ULONG reqsize
= 0, mark
= 0, dep
= 0, deplen
;
593 LPWSTR ins_str
= NULL
;
595 const UNICODE_STRING
* cd
;
598 /* return error if name only consists of spaces */
599 for (ptr
= name
; *ptr
; ptr
++) if (*ptr
!= ' ') break;
604 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
605 cd
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
607 cd
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
609 switch (RtlDetermineDosPathNameType_U(name
))
611 case UNC_PATH
: /* \\foo */
612 ptr
= skip_unc_prefix( name
);
616 case DEVICE_PATH
: /* \\.\foo */
620 case ABSOLUTE_DRIVE_PATH
: /* c:\foo */
621 reqsize
= sizeof(WCHAR
);
622 tmp
[0] = toupperW(name
[0]);
628 case RELATIVE_DRIVE_PATH
: /* c:foo */
630 if (toupperW(name
[0]) != toupperW(cd
->Buffer
[0]) || cd
->Buffer
[1] != ':')
632 UNICODE_STRING var
, val
;
638 var
.Length
= 3 * sizeof(WCHAR
);
639 var
.MaximumLength
= 4 * sizeof(WCHAR
);
642 val
.MaximumLength
= size
;
643 val
.Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, size
);
645 switch (RtlQueryEnvironmentVariable_U(NULL
, &var
, &val
))
648 /* FIXME: Win2k seems to check that the environment variable actually points
649 * to an existing directory. If not, root of the drive is used
650 * (this seems also to be the only spot in RtlGetFullPathName that the
651 * existence of a part of a path is checked)
654 case STATUS_BUFFER_TOO_SMALL
:
655 reqsize
= val
.Length
+ sizeof(WCHAR
); /* append trailing '\\' */
656 val
.Buffer
[val
.Length
/ sizeof(WCHAR
)] = '\\';
657 ins_str
= val
.Buffer
;
659 case STATUS_VARIABLE_NOT_FOUND
:
660 reqsize
= 3 * sizeof(WCHAR
);
667 ERR("Unsupported status code\n");
675 case RELATIVE_PATH
: /* foo */
676 reqsize
= cd
->Length
;
677 ins_str
= cd
->Buffer
;
678 if (cd
->Buffer
[1] != ':')
680 ptr
= skip_unc_prefix( cd
->Buffer
);
681 mark
= ptr
- cd
->Buffer
;
686 case ABSOLUTE_PATH
: /* \xxx */
687 if (name
[0] == '/') /* may be a Unix path */
689 const WCHAR
*ptr
= name
;
690 int drive
= find_drive_rootW( &ptr
);
693 reqsize
= 3 * sizeof(WCHAR
);
694 tmp
[0] = 'A' + drive
;
703 if (cd
->Buffer
[1] == ':')
705 reqsize
= 2 * sizeof(WCHAR
);
706 tmp
[0] = cd
->Buffer
[0];
713 ptr
= skip_unc_prefix( cd
->Buffer
);
714 reqsize
= (ptr
- cd
->Buffer
) * sizeof(WCHAR
);
715 mark
= reqsize
/ sizeof(WCHAR
);
716 ins_str
= cd
->Buffer
;
720 case UNC_DOT_PATH
: /* \\. */
721 reqsize
= 4 * sizeof(WCHAR
);
736 deplen
= strlenW(name
+ dep
) * sizeof(WCHAR
);
737 if (reqsize
+ deplen
+ sizeof(WCHAR
) > size
)
739 /* not enough space, return need size (including terminating '\0') */
740 reqsize
+= deplen
+ sizeof(WCHAR
);
744 memmove(buffer
+ reqsize
/ sizeof(WCHAR
), name
+ dep
, deplen
+ sizeof(WCHAR
));
745 if (reqsize
) memcpy(buffer
, ins_str
, reqsize
);
748 if (ins_str
!= tmp
&& ins_str
!= cd
->Buffer
)
749 RtlFreeHeap(GetProcessHeap(), 0, ins_str
);
751 collapse_path( buffer
, mark
);
752 reqsize
= strlenW(buffer
) * sizeof(WCHAR
);
759 /******************************************************************
760 * RtlGetFullPathName_U (NTDLL.@)
762 * Returns the number of bytes written to buffer (not including the
763 * terminating NULL) if the function succeeds, or the required number of bytes
764 * (including the terminating NULL) if the buffer is too small.
766 * file_part will point to the filename part inside buffer (except if we use
767 * DOS device name, in which case file_in_buf is NULL)
770 DWORD WINAPI
RtlGetFullPathName_U(const WCHAR
* name
, ULONG size
, WCHAR
* buffer
,
777 TRACE("(%s %u %p %p)\n", debugstr_w(name
), size
, buffer
, file_part
);
779 if (!name
|| !*name
) return 0;
781 if (file_part
) *file_part
= NULL
;
783 /* check for DOS device name */
784 dosdev
= RtlIsDosDeviceName_U(name
);
787 DWORD offset
= HIWORD(dosdev
) / sizeof(WCHAR
); /* get it in WCHARs, not bytes */
788 DWORD sz
= LOWORD(dosdev
); /* in bytes */
790 if (8 + sz
+ 2 > size
) return sz
+ 10;
791 strcpyW(buffer
, DeviceRootW
);
792 memmove(buffer
+ 4, name
+ offset
, sz
);
793 buffer
[4 + sz
/ sizeof(WCHAR
)] = '\0';
794 /* file_part isn't set in this case */
798 reqsize
= get_full_path_helper(name
, buffer
, size
);
799 if (!reqsize
) return 0;
802 LPWSTR tmp
= RtlAllocateHeap(GetProcessHeap(), 0, reqsize
);
803 reqsize
= get_full_path_helper(name
, tmp
, reqsize
);
804 if (reqsize
+ sizeof(WCHAR
) > size
) /* it may have worked the second time */
806 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
807 return reqsize
+ sizeof(WCHAR
);
809 memcpy( buffer
, tmp
, reqsize
+ sizeof(WCHAR
) );
810 RtlFreeHeap(GetProcessHeap(), 0, tmp
);
814 if (file_part
&& (ptr
= strrchrW(buffer
, '\\')) != NULL
&& ptr
>= buffer
+ 2 && *++ptr
)
819 /*************************************************************************
820 * RtlGetLongestNtPathLength [NTDLL.@]
822 * Get the longest allowed path length
828 * The longest allowed path length (277 characters under Win2k).
830 DWORD WINAPI
RtlGetLongestNtPathLength(void)
832 return MAX_NT_PATH_LENGTH
;
835 /******************************************************************
836 * RtlIsNameLegalDOS8Dot3 (NTDLL.@)
838 * Returns TRUE iff unicode is a valid DOS (8+3) name.
839 * If the name is valid, oem gets filled with the corresponding OEM string
840 * spaces is set to TRUE if unicode contains spaces
842 BOOLEAN WINAPI
RtlIsNameLegalDOS8Dot3( const UNICODE_STRING
*unicode
,
843 OEM_STRING
*oem
, BOOLEAN
*spaces
)
845 static const char illegal
[] = "*?<>|\"+=,;[]:/\\\345";
850 BOOLEAN got_space
= FALSE
;
854 oem_str
.Length
= sizeof(buffer
);
855 oem_str
.MaximumLength
= sizeof(buffer
);
856 oem_str
.Buffer
= buffer
;
859 if (RtlUpcaseUnicodeStringToCountedOemString( oem
, unicode
, FALSE
) != STATUS_SUCCESS
)
862 if (oem
->Length
> 12) return FALSE
;
864 /* a starting . is invalid, except for . and .. */
865 if (oem
->Buffer
[0] == '.')
867 if (oem
->Length
!= 1 && (oem
->Length
!= 2 || oem
->Buffer
[1] != '.')) return FALSE
;
868 if (spaces
) *spaces
= FALSE
;
872 for (i
= 0; i
< oem
->Length
; i
++)
874 switch (oem
->Buffer
[i
])
877 /* leading/trailing spaces not allowed */
878 if (!i
|| i
== oem
->Length
-1 || oem
->Buffer
[i
+1] == '.') return FALSE
;
882 if (dot
!= -1) return FALSE
;
886 if (strchr(illegal
, oem
->Buffer
[i
])) return FALSE
;
890 /* check file part is shorter than 8, extension shorter than 3
891 * dot cannot be last in string
895 if (oem
->Length
> 8) return FALSE
;
899 if (dot
> 8 || (oem
->Length
- dot
> 4) || dot
== oem
->Length
- 1) return FALSE
;
901 if (spaces
) *spaces
= got_space
;
905 /******************************************************************
906 * RtlGetCurrentDirectory_U (NTDLL.@)
909 NTSTATUS WINAPI
RtlGetCurrentDirectory_U(ULONG buflen
, LPWSTR buf
)
914 TRACE("(%u %p)\n", buflen
, buf
);
918 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
919 us
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
.DosPath
;
921 us
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
.DosPath
;
923 len
= us
->Length
/ sizeof(WCHAR
);
924 if (us
->Buffer
[len
- 1] == '\\' && us
->Buffer
[len
- 2] != ':')
927 if (buflen
/ sizeof(WCHAR
) > len
)
929 memcpy(buf
, us
->Buffer
, len
* sizeof(WCHAR
));
939 return len
* sizeof(WCHAR
);
942 /******************************************************************
943 * RtlSetCurrentDirectory_U (NTDLL.@)
946 NTSTATUS WINAPI
RtlSetCurrentDirectory_U(const UNICODE_STRING
* dir
)
948 FILE_FS_DEVICE_INFORMATION device_info
;
949 OBJECT_ATTRIBUTES attr
;
950 UNICODE_STRING newdir
;
958 newdir
.Buffer
= NULL
;
962 if (NtCurrentTeb()->Tib
.SubSystemTib
) /* FIXME: hack */
963 curdir
= &((WIN16_SUBSYSTEM_TIB
*)NtCurrentTeb()->Tib
.SubSystemTib
)->curdir
;
965 curdir
= &NtCurrentTeb()->Peb
->ProcessParameters
->CurrentDirectory
;
967 if (!RtlDosPathNameToNtPathName_U( dir
->Buffer
, &newdir
, NULL
, NULL
))
969 nts
= STATUS_OBJECT_NAME_INVALID
;
973 attr
.Length
= sizeof(attr
);
974 attr
.RootDirectory
= 0;
975 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
976 attr
.ObjectName
= &newdir
;
977 attr
.SecurityDescriptor
= NULL
;
978 attr
.SecurityQualityOfService
= NULL
;
980 nts
= NtOpenFile( &handle
, 0, &attr
, &io
, 0, FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
981 if (nts
!= STATUS_SUCCESS
) goto out
;
983 /* don't keep the directory handle open on removable media */
984 if (!NtQueryVolumeInformationFile( handle
, &io
, &device_info
,
985 sizeof(device_info
), FileFsDeviceInformation
) &&
986 (device_info
.Characteristics
& FILE_REMOVABLE_MEDIA
))
992 if (curdir
->Handle
) NtClose( curdir
->Handle
);
993 curdir
->Handle
= handle
;
995 /* append trailing \ if missing */
996 size
= newdir
.Length
/ sizeof(WCHAR
);
998 ptr
+= 4; /* skip \??\ prefix */
1000 if (size
&& ptr
[size
- 1] != '\\') ptr
[size
++] = '\\';
1002 memcpy( curdir
->DosPath
.Buffer
, ptr
, size
* sizeof(WCHAR
));
1003 curdir
->DosPath
.Buffer
[size
] = 0;
1004 curdir
->DosPath
.Length
= size
* sizeof(WCHAR
);
1006 TRACE( "curdir now %s %p\n", debugstr_w(curdir
->DosPath
.Buffer
), curdir
->Handle
);
1009 RtlFreeUnicodeString( &newdir
);
1010 RtlReleasePebLock();
1015 /******************************************************************
1016 * wine_unix_to_nt_file_name (NTDLL.@) Not a Windows API
1018 NTSTATUS CDECL
wine_unix_to_nt_file_name( const ANSI_STRING
*name
, UNICODE_STRING
*nt
)
1020 static const WCHAR prefixW
[] = {'\\','?','?','\\','A',':','\\'};
1021 unsigned int lenW
, lenA
= name
->Length
;
1022 const char *path
= name
->Buffer
;
1028 if (!lenA
|| path
[0] != '/')
1033 if ((status
= DIR_get_unix_cwd( &cwd
)) != STATUS_SUCCESS
) return status
;
1035 size
= strlen(cwd
) + lenA
+ 1;
1036 if (!(newcwd
= RtlReAllocateHeap( GetProcessHeap(), 0, cwd
, size
)))
1038 status
= STATUS_NO_MEMORY
;
1042 end
= cwd
+ strlen(cwd
);
1043 if (end
> cwd
&& end
[-1] != '/') *end
++ = '/';
1044 memcpy( end
, path
, lenA
);
1048 status
= find_drive_rootA( &path
, lenA
, &drive
);
1049 lenA
-= (path
- cwd
);
1054 status
= find_drive_rootA( &path
, lenA
, &drive
);
1055 lenA
-= (path
- name
->Buffer
);
1058 if (status
!= STATUS_SUCCESS
) goto done
;
1059 while (lenA
&& path
[0] == '/') { lenA
--; path
++; }
1061 lenW
= ntdll_umbstowcs( 0, path
, lenA
, NULL
, 0 );
1062 if (!(nt
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
1063 (lenW
+ 1) * sizeof(WCHAR
) + sizeof(prefixW
) )))
1065 status
= STATUS_NO_MEMORY
;
1069 memcpy( nt
->Buffer
, prefixW
, sizeof(prefixW
) );
1070 nt
->Buffer
[4] += drive
;
1071 ntdll_umbstowcs( 0, path
, lenA
, nt
->Buffer
+ sizeof(prefixW
)/sizeof(WCHAR
), lenW
);
1072 lenW
+= sizeof(prefixW
)/sizeof(WCHAR
);
1073 nt
->Buffer
[lenW
] = 0;
1074 nt
->Length
= lenW
* sizeof(WCHAR
);
1075 nt
->MaximumLength
= nt
->Length
+ sizeof(WCHAR
);
1076 for (p
= nt
->Buffer
+ sizeof(prefixW
)/sizeof(WCHAR
); *p
; p
++) if (*p
== '/') *p
= '\\';
1079 RtlFreeHeap( GetProcessHeap(), 0, cwd
);