2 * File handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996, 2004 Alexandre Julliard
6 * Copyright 2003 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
35 #define WIN32_NO_STATUS
40 #include "kernel_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(file
);
46 #define MAX_PATHNAME_LEN 1024
49 /* check if a file name is for an executable file (.exe or .com) */
50 static inline BOOL
is_executable( const WCHAR
*name
)
52 static const WCHAR exeW
[] = {'.','e','x','e',0};
53 static const WCHAR comW
[] = {'.','c','o','m',0};
54 int len
= strlenW(name
);
56 if (len
< 4) return FALSE
;
57 return (!strcmpiW( name
+ len
- 4, exeW
) || !strcmpiW( name
+ len
- 4, comW
));
60 /***********************************************************************
63 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
65 static DWORD
copy_filename_WtoA( LPCWSTR nameW
, LPSTR buffer
, DWORD len
)
69 BOOL is_ansi
= AreFileApisANSI();
71 RtlInitUnicodeString( &strW
, nameW
);
73 ret
= is_ansi
? RtlUnicodeStringToAnsiSize(&strW
) : RtlUnicodeStringToOemSize(&strW
);
74 if (buffer
&& ret
<= len
)
79 str
.MaximumLength
= min( len
, UNICODE_STRING_MAX_CHARS
);
81 RtlUnicodeStringToAnsiString( &str
, &strW
, FALSE
);
83 RtlUnicodeStringToOemString( &str
, &strW
, FALSE
);
84 ret
= str
.Length
; /* length without terminating 0 */
89 /***********************************************************************
90 * add_boot_rename_entry
92 * Adds an entry to the registry that is loaded when windows boots and
93 * checks if there are some files to be removed or renamed/moved.
94 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
95 * non-NULL then the file is moved, otherwise it is deleted. The
96 * entry of the registrykey is always appended with two zero
97 * terminated strings. If <fn2> is NULL then the second entry is
98 * simply a single 0-byte. Otherwise the second filename goes
99 * there. The entries are prepended with \??\ before the path and the
100 * second filename gets also a '!' as the first character if
101 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
102 * 0-byte follows to indicate the end of the strings.
104 * \??\D:\test\file1[0]
105 * !\??\D:\test\file1_renamed[0]
106 * \??\D:\Test|delete[0]
107 * [0] <- file is to be deleted, second string empty
108 * \??\D:\test\file2[0]
109 * !\??\D:\test\file2_renamed[0]
110 * [0] <- indicates end of strings
113 * \??\D:\test\file1[0]
114 * !\??\D:\test\file1_renamed[0]
115 * \??\D:\Test|delete[0]
116 * [0] <- file is to be deleted, second string empty
117 * [0] <- indicates end of strings
120 static BOOL
add_boot_rename_entry( LPCWSTR source
, LPCWSTR dest
, DWORD flags
)
122 static const WCHAR ValueName
[] = {'P','e','n','d','i','n','g',
123 'F','i','l','e','R','e','n','a','m','e',
124 'O','p','e','r','a','t','i','o','n','s',0};
125 static const WCHAR SessionW
[] = {'M','a','c','h','i','n','e','\\',
126 'S','y','s','t','e','m','\\',
127 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
128 'C','o','n','t','r','o','l','\\',
129 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
130 static const int info_size
= FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
132 OBJECT_ATTRIBUTES attr
;
133 UNICODE_STRING nameW
, source_name
, dest_name
;
134 KEY_VALUE_PARTIAL_INFORMATION
*info
;
142 if (!RtlDosPathNameToNtPathName_U( source
, &source_name
, NULL
, NULL
))
144 SetLastError( ERROR_PATH_NOT_FOUND
);
147 dest_name
.Buffer
= NULL
;
148 if (dest
&& !RtlDosPathNameToNtPathName_U( dest
, &dest_name
, NULL
, NULL
))
150 RtlFreeUnicodeString( &source_name
);
151 SetLastError( ERROR_PATH_NOT_FOUND
);
155 attr
.Length
= sizeof(attr
);
156 attr
.RootDirectory
= 0;
157 attr
.ObjectName
= &nameW
;
159 attr
.SecurityDescriptor
= NULL
;
160 attr
.SecurityQualityOfService
= NULL
;
161 RtlInitUnicodeString( &nameW
, SessionW
);
163 if (NtCreateKey( &Reboot
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
) != STATUS_SUCCESS
)
165 WARN("Error creating key for reboot managment [%s]\n",
166 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
167 RtlFreeUnicodeString( &source_name
);
168 RtlFreeUnicodeString( &dest_name
);
172 len1
= source_name
.Length
+ sizeof(WCHAR
);
175 len2
= dest_name
.Length
+ sizeof(WCHAR
);
176 if (flags
& MOVEFILE_REPLACE_EXISTING
)
177 len2
+= sizeof(WCHAR
); /* Plus 1 because of the leading '!' */
179 else len2
= sizeof(WCHAR
); /* minimum is the 0 characters for the empty second string */
181 RtlInitUnicodeString( &nameW
, ValueName
);
183 /* First we check if the key exists and if so how many bytes it already contains. */
184 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
185 NULL
, 0, &DataSize
) == STATUS_BUFFER_TOO_SMALL
)
187 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
189 if (NtQueryValueKey( Reboot
, &nameW
, KeyValuePartialInformation
,
190 Buffer
, DataSize
, &DataSize
)) goto Quit
;
191 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)Buffer
;
192 if (info
->Type
!= REG_MULTI_SZ
) goto Quit
;
193 if (DataSize
> sizeof(info
)) DataSize
-= sizeof(WCHAR
); /* remove terminating null (will be added back later) */
197 DataSize
= info_size
;
198 if (!(Buffer
= HeapAlloc( GetProcessHeap(), 0, DataSize
+ len1
+ len2
+ sizeof(WCHAR
) )))
202 memcpy( Buffer
+ DataSize
, source_name
.Buffer
, len1
);
204 p
= (WCHAR
*)(Buffer
+ DataSize
);
207 if (flags
& MOVEFILE_REPLACE_EXISTING
)
209 memcpy( p
, dest_name
.Buffer
, len2
);
215 DataSize
+= sizeof(WCHAR
);
219 p
= (WCHAR
*)(Buffer
+ DataSize
);
221 DataSize
+= sizeof(WCHAR
);
223 rc
= !NtSetValueKey(Reboot
, &nameW
, 0, REG_MULTI_SZ
, Buffer
+ info_size
, DataSize
- info_size
);
226 RtlFreeUnicodeString( &source_name
);
227 RtlFreeUnicodeString( &dest_name
);
228 if (Reboot
) NtClose(Reboot
);
229 HeapFree( GetProcessHeap(), 0, Buffer
);
234 /***********************************************************************
235 * GetFullPathNameW (KERNEL32.@)
237 * if the path closed with '\', *lastpart is 0
239 DWORD WINAPI
GetFullPathNameW( LPCWSTR name
, DWORD len
, LPWSTR buffer
,
242 return RtlGetFullPathName_U(name
, len
* sizeof(WCHAR
), buffer
, lastpart
) / sizeof(WCHAR
);
245 /***********************************************************************
246 * GetFullPathNameA (KERNEL32.@)
248 * if the path closed with '\', *lastpart is 0
250 DWORD WINAPI
GetFullPathNameA( LPCSTR name
, DWORD len
, LPSTR buffer
,
254 WCHAR bufferW
[MAX_PATH
];
257 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
259 ret
= GetFullPathNameW( nameW
, MAX_PATH
, bufferW
, NULL
);
264 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
267 ret
= copy_filename_WtoA( bufferW
, buffer
, len
);
268 if (ret
< len
&& lastpart
)
270 LPSTR p
= buffer
+ strlen(buffer
) - 1;
274 while ((p
> buffer
+ 2) && (*p
!= '\\')) p
--;
277 else *lastpart
= NULL
;
283 /***********************************************************************
284 * GetLongPathNameW (KERNEL32.@)
287 * observed (Win2000):
288 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
289 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
291 DWORD WINAPI
GetLongPathNameW( LPCWSTR shortpath
, LPWSTR longpath
, DWORD longlen
)
293 WCHAR tmplongpath
[MAX_PATHNAME_LEN
];
295 DWORD sp
= 0, lp
= 0;
298 WIN32_FIND_DATAW wfd
;
303 SetLastError(ERROR_INVALID_PARAMETER
);
308 SetLastError(ERROR_PATH_NOT_FOUND
);
312 TRACE("%s,%p,%d\n", debugstr_w(shortpath
), longpath
, longlen
);
314 if (shortpath
[0] == '\\' && shortpath
[1] == '\\')
316 FIXME("UNC pathname %s\n", debugstr_w(shortpath
));
318 tmplen
= strlenW(shortpath
);
319 if (tmplen
< longlen
)
321 if (longpath
!= shortpath
) strcpyW( longpath
, shortpath
);
327 unixabsolute
= (shortpath
[0] == '/');
329 /* check for drive letter */
330 if (!unixabsolute
&& shortpath
[1] == ':' )
332 tmplongpath
[0] = shortpath
[0];
333 tmplongpath
[1] = ':';
337 while (shortpath
[sp
])
339 /* check for path delimiters and reproduce them */
340 if (shortpath
[sp
] == '\\' || shortpath
[sp
] == '/')
342 if (!lp
|| tmplongpath
[lp
-1] != '\\')
344 /* strip double "\\" */
345 tmplongpath
[lp
++] = '\\';
347 tmplongpath
[lp
] = 0; /* terminate string */
353 if (sp
== 0 && p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\'))
355 tmplongpath
[lp
++] = *p
++;
356 tmplongpath
[lp
++] = *p
++;
358 for (; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
359 tmplen
= p
- (shortpath
+ sp
);
360 lstrcpynW(tmplongpath
+ lp
, shortpath
+ sp
, tmplen
+ 1);
361 /* Check if the file exists and use the existing file name */
362 goit
= FindFirstFileW(tmplongpath
, &wfd
);
363 if (goit
== INVALID_HANDLE_VALUE
)
365 TRACE("not found %s!\n", debugstr_w(tmplongpath
));
366 SetLastError ( ERROR_FILE_NOT_FOUND
);
370 strcpyW(tmplongpath
+ lp
, wfd
.cFileName
);
371 lp
+= strlenW(tmplongpath
+ lp
);
374 tmplen
= strlenW(shortpath
) - 1;
375 if ((shortpath
[tmplen
] == '/' || shortpath
[tmplen
] == '\\') &&
376 (tmplongpath
[lp
- 1] != '/' && tmplongpath
[lp
- 1] != '\\'))
377 tmplongpath
[lp
++] = shortpath
[tmplen
];
380 tmplen
= strlenW(tmplongpath
) + 1;
381 if (tmplen
<= longlen
)
383 strcpyW(longpath
, tmplongpath
);
384 TRACE("returning %s\n", debugstr_w(longpath
));
385 tmplen
--; /* length without 0 */
391 /***********************************************************************
392 * GetLongPathNameA (KERNEL32.@)
394 DWORD WINAPI
GetLongPathNameA( LPCSTR shortpath
, LPSTR longpath
, DWORD longlen
)
397 WCHAR longpathW
[MAX_PATH
];
400 TRACE("%s\n", debugstr_a(shortpath
));
402 if (!(shortpathW
= FILE_name_AtoW( shortpath
, FALSE
))) return 0;
404 ret
= GetLongPathNameW(shortpathW
, longpathW
, MAX_PATH
);
409 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
412 return copy_filename_WtoA( longpathW
, longpath
, longlen
);
416 /***********************************************************************
417 * GetShortPathNameW (KERNEL32.@)
421 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
422 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
424 * more observations ( with NT 3.51 (WinDD) ):
425 * longpath <= 8.3 -> just copy longpath to shortpath
427 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
428 * b) file does exist -> set the short filename.
429 * - trailing slashes are reproduced in the short name, even if the
430 * file is not a directory
431 * - the absolute/relative path of the short name is reproduced like found
433 * - longpath and shortpath may have the same address
436 DWORD WINAPI
GetShortPathNameW( LPCWSTR longpath
, LPWSTR shortpath
, DWORD shortlen
)
438 WCHAR tmpshortpath
[MAX_PATHNAME_LEN
];
440 DWORD sp
= 0, lp
= 0;
442 WIN32_FIND_DATAW wfd
;
445 WCHAR ustr_buf
[8+1+3+1];
447 TRACE("%s\n", debugstr_w(longpath
));
451 SetLastError(ERROR_INVALID_PARAMETER
);
456 SetLastError(ERROR_BAD_PATHNAME
);
460 /* check for drive letter */
461 if (longpath
[0] != '/' && longpath
[1] == ':' )
463 tmpshortpath
[0] = longpath
[0];
464 tmpshortpath
[1] = ':';
468 ustr
.Buffer
= ustr_buf
;
470 ustr
.MaximumLength
= sizeof(ustr_buf
);
474 /* check for path delimiters and reproduce them */
475 if (longpath
[lp
] == '\\' || longpath
[lp
] == '/')
477 if (!sp
|| tmpshortpath
[sp
-1] != '\\')
479 /* strip double "\\" */
480 tmpshortpath
[sp
] = '\\';
483 tmpshortpath
[sp
] = 0; /* terminate string */
488 for (p
= longpath
+ lp
; *p
&& *p
!= '/' && *p
!= '\\'; p
++);
489 tmplen
= p
- (longpath
+ lp
);
490 lstrcpynW(tmpshortpath
+ sp
, longpath
+ lp
, tmplen
+ 1);
491 /* Check, if the current element is a valid dos name */
495 memcpy(ustr_buf
, longpath
+ lp
, tmplen
* sizeof(WCHAR
));
496 ustr_buf
[tmplen
] = '\0';
497 ustr
.Length
= tmplen
* sizeof(WCHAR
);
498 if (RtlIsNameLegalDOS8Dot3(&ustr
, NULL
, &spaces
) && !spaces
)
506 /* Check if the file exists and use the existing short file name */
507 goit
= FindFirstFileW(tmpshortpath
, &wfd
);
508 if (goit
== INVALID_HANDLE_VALUE
) goto notfound
;
510 strcpyW(tmpshortpath
+ sp
, wfd
.cAlternateFileName
);
511 sp
+= strlenW(tmpshortpath
+ sp
);
514 tmpshortpath
[sp
] = 0;
516 tmplen
= strlenW(tmpshortpath
) + 1;
517 if (tmplen
<= shortlen
)
519 strcpyW(shortpath
, tmpshortpath
);
520 TRACE("returning %s\n", debugstr_w(shortpath
));
521 tmplen
--; /* length without 0 */
527 TRACE("not found!\n" );
528 SetLastError ( ERROR_FILE_NOT_FOUND
);
532 /***********************************************************************
533 * GetShortPathNameA (KERNEL32.@)
535 DWORD WINAPI
GetShortPathNameA( LPCSTR longpath
, LPSTR shortpath
, DWORD shortlen
)
538 WCHAR shortpathW
[MAX_PATH
];
541 TRACE("%s\n", debugstr_a(longpath
));
543 if (!(longpathW
= FILE_name_AtoW( longpath
, FALSE
))) return 0;
545 ret
= GetShortPathNameW(longpathW
, shortpathW
, MAX_PATH
);
550 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
553 return copy_filename_WtoA( shortpathW
, shortpath
, shortlen
);
557 /***********************************************************************
558 * GetTempPathA (KERNEL32.@)
560 DWORD WINAPI
GetTempPathA( DWORD count
, LPSTR path
)
562 WCHAR pathW
[MAX_PATH
];
565 ret
= GetTempPathW(MAX_PATH
, pathW
);
572 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
575 return copy_filename_WtoA( pathW
, path
, count
);
579 /***********************************************************************
580 * GetTempPathW (KERNEL32.@)
582 DWORD WINAPI
GetTempPathW( DWORD count
, LPWSTR path
)
584 static const WCHAR tmp
[] = { 'T', 'M', 'P', 0 };
585 static const WCHAR temp
[] = { 'T', 'E', 'M', 'P', 0 };
586 static const WCHAR userprofile
[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
587 WCHAR tmp_path
[MAX_PATH
];
590 TRACE("%u,%p\n", count
, path
);
592 if (!(ret
= GetEnvironmentVariableW( tmp
, tmp_path
, MAX_PATH
)) &&
593 !(ret
= GetEnvironmentVariableW( temp
, tmp_path
, MAX_PATH
)) &&
594 !(ret
= GetEnvironmentVariableW( userprofile
, tmp_path
, MAX_PATH
)) &&
595 !(ret
= GetWindowsDirectoryW( tmp_path
, MAX_PATH
)))
600 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
604 ret
= GetFullPathNameW(tmp_path
, MAX_PATH
, tmp_path
, NULL
);
607 if (ret
> MAX_PATH
- 2)
609 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
613 if (tmp_path
[ret
-1] != '\\')
615 tmp_path
[ret
++] = '\\';
616 tmp_path
[ret
] = '\0';
619 ret
++; /* add space for terminating 0 */
623 lstrcpynW(path
, tmp_path
, count
);
625 ret
--; /* return length without 0 */
627 path
[0] = 0; /* avoid returning ambiguous "X:" */
630 TRACE("returning %u, %s\n", ret
, debugstr_w(path
));
635 /***********************************************************************
636 * GetTempFileNameA (KERNEL32.@)
638 UINT WINAPI
GetTempFileNameA( LPCSTR path
, LPCSTR prefix
, UINT unique
, LPSTR buffer
)
640 WCHAR
*pathW
, *prefixW
= NULL
;
641 WCHAR bufferW
[MAX_PATH
];
644 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return 0;
645 if (prefix
&& !(prefixW
= FILE_name_AtoW( prefix
, TRUE
))) return 0;
647 ret
= GetTempFileNameW(pathW
, prefixW
, unique
, bufferW
);
648 if (ret
) FILE_name_WtoA( bufferW
, -1, buffer
, MAX_PATH
);
650 HeapFree( GetProcessHeap(), 0, prefixW
);
654 /***********************************************************************
655 * GetTempFileNameW (KERNEL32.@)
657 UINT WINAPI
GetTempFileNameW( LPCWSTR path
, LPCWSTR prefix
, UINT unique
, LPWSTR buffer
)
659 static const WCHAR formatW
[] = {'%','x','.','t','m','p',0};
664 if ( !path
|| !buffer
)
666 SetLastError( ERROR_INVALID_PARAMETER
);
670 strcpyW( buffer
, path
);
671 p
= buffer
+ strlenW(buffer
);
673 /* add a \, if there isn't one */
674 if ((p
== buffer
) || (p
[-1] != '\\')) *p
++ = '\\';
677 for (i
= 3; (i
> 0) && (*prefix
); i
--) *p
++ = *prefix
++;
681 if (unique
) sprintfW( p
, formatW
, unique
);
684 /* get a "random" unique number and try to create the file */
686 UINT num
= GetTickCount() & 0xffff;
692 sprintfW( p
, formatW
, unique
);
693 handle
= CreateFileW( buffer
, GENERIC_WRITE
, 0, NULL
,
694 CREATE_NEW
, FILE_ATTRIBUTE_NORMAL
, 0 );
695 if (handle
!= INVALID_HANDLE_VALUE
)
696 { /* We created it */
697 TRACE("created %s\n", debugstr_w(buffer
) );
698 CloseHandle( handle
);
701 if (GetLastError() != ERROR_FILE_EXISTS
&&
702 GetLastError() != ERROR_SHARING_VIOLATION
)
703 break; /* No need to go on */
704 if (!(++unique
& 0xffff)) unique
= 1;
705 } while (unique
!= num
);
708 TRACE("returning %s\n", debugstr_w(buffer
) );
713 /***********************************************************************
716 * Check if the file name contains a path; helper for SearchPathW.
717 * A relative path is not considered a path unless it starts with ./ or ../
719 static inline BOOL
contains_pathW (LPCWSTR name
)
721 if (RtlDetermineDosPathNameType_U( name
) != RELATIVE_PATH
) return TRUE
;
722 if (name
[0] != '.') return FALSE
;
723 if (name
[1] == '/' || name
[1] == '\\') return TRUE
;
724 return (name
[1] == '.' && (name
[2] == '/' || name
[2] == '\\'));
728 /***********************************************************************
729 * SearchPathW [KERNEL32.@]
731 * Searches for a specified file in the search path.
734 * path [I] Path to search (NULL means default)
735 * name [I] Filename to search for.
736 * ext [I] File extension to append to file name. The first
737 * character must be a period. This parameter is
738 * specified only if the filename given does not
739 * contain an extension.
740 * buflen [I] size of buffer, in characters
741 * buffer [O] buffer for found filename
742 * lastpart [O] address of pointer to last used character in
743 * buffer (the final '\')
746 * Success: length of string copied into buffer, not including
747 * terminating null character. If the filename found is
748 * longer than the length of the buffer, the length of the
749 * filename is returned.
753 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
756 DWORD WINAPI
SearchPathW( LPCWSTR path
, LPCWSTR name
, LPCWSTR ext
, DWORD buflen
,
757 LPWSTR buffer
, LPWSTR
*lastpart
)
761 if (!name
|| !name
[0])
763 SetLastError(ERROR_INVALID_PARAMETER
);
767 /* If the name contains an explicit path, ignore the path */
769 if (contains_pathW(name
))
771 /* try first without extension */
772 if (RtlDoesFileExists_U( name
))
773 return GetFullPathNameW( name
, buflen
, buffer
, lastpart
);
777 LPCWSTR p
= strrchrW( name
, '.' );
778 if (p
&& !strchrW( p
, '/' ) && !strchrW( p
, '\\' ))
779 ext
= NULL
; /* Ignore the specified extension */
782 /* Allocate a buffer for the file name and extension */
786 DWORD len
= strlenW(name
) + strlenW(ext
);
788 if (!(tmp
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
790 SetLastError( ERROR_OUTOFMEMORY
);
793 strcpyW( tmp
, name
);
795 if (RtlDoesFileExists_U( tmp
))
796 ret
= GetFullPathNameW( tmp
, buflen
, buffer
, lastpart
);
797 HeapFree( GetProcessHeap(), 0, tmp
);
800 else if (path
&& path
[0]) /* search in the specified path */
802 ret
= RtlDosSearchPath_U( path
, name
, ext
, buflen
* sizeof(WCHAR
),
803 buffer
, lastpart
) / sizeof(WCHAR
);
805 else /* search in the default path */
807 WCHAR
*dll_path
= MODULE_get_dll_load_path( NULL
);
811 ret
= RtlDosSearchPath_U( dll_path
, name
, ext
, buflen
* sizeof(WCHAR
),
812 buffer
, lastpart
) / sizeof(WCHAR
);
813 HeapFree( GetProcessHeap(), 0, dll_path
);
817 SetLastError( ERROR_OUTOFMEMORY
);
822 if (!ret
) SetLastError( ERROR_FILE_NOT_FOUND
);
823 else TRACE( "found %s\n", debugstr_w(buffer
) );
828 /***********************************************************************
829 * SearchPathA (KERNEL32.@)
833 DWORD WINAPI
SearchPathA( LPCSTR path
, LPCSTR name
, LPCSTR ext
,
834 DWORD buflen
, LPSTR buffer
, LPSTR
*lastpart
)
836 WCHAR
*pathW
= NULL
, *nameW
, *extW
= NULL
;
837 WCHAR bufferW
[MAX_PATH
];
842 SetLastError(ERROR_INVALID_PARAMETER
);
846 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return 0;
847 if (path
&& !(pathW
= FILE_name_AtoW( path
, TRUE
))) return 0;
849 if (ext
&& !(extW
= FILE_name_AtoW( ext
, TRUE
)))
851 HeapFree( GetProcessHeap(), 0, pathW
);
855 ret
= SearchPathW(pathW
, nameW
, extW
, MAX_PATH
, bufferW
, NULL
);
857 HeapFree( GetProcessHeap(), 0, pathW
);
858 HeapFree( GetProcessHeap(), 0, extW
);
863 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
866 ret
= copy_filename_WtoA( bufferW
, buffer
, buflen
);
867 if (buflen
> ret
&& lastpart
)
868 *lastpart
= strrchr(buffer
, '\\') + 1;
872 static BOOL
is_same_file(HANDLE h1
, HANDLE h2
)
876 if (wine_server_handle_to_fd(h1
, 0, &fd1
, NULL
) == STATUS_SUCCESS
)
879 if (wine_server_handle_to_fd(h2
, 0, &fd2
, NULL
) == STATUS_SUCCESS
)
881 struct stat stat1
, stat2
;
882 if (fstat(fd1
, &stat1
) == 0 && fstat(fd2
, &stat2
) == 0)
883 ret
= (stat1
.st_dev
== stat2
.st_dev
&& stat1
.st_ino
== stat2
.st_ino
);
884 wine_server_release_fd(h2
, fd2
);
886 wine_server_release_fd(h1
, fd1
);
891 /**************************************************************************
892 * CopyFileW (KERNEL32.@)
894 BOOL WINAPI
CopyFileW( LPCWSTR source
, LPCWSTR dest
, BOOL fail_if_exists
)
896 static const int buffer_size
= 65536;
898 BY_HANDLE_FILE_INFORMATION info
;
903 if (!source
|| !dest
)
905 SetLastError(ERROR_INVALID_PARAMETER
);
908 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, buffer_size
)))
910 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
914 TRACE("%s -> %s\n", debugstr_w(source
), debugstr_w(dest
));
916 if ((h1
= CreateFileW(source
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
917 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
)
919 WARN("Unable to open source %s\n", debugstr_w(source
));
920 HeapFree( GetProcessHeap(), 0, buffer
);
924 if (!GetFileInformationByHandle( h1
, &info
))
926 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source
));
927 HeapFree( GetProcessHeap(), 0, buffer
);
934 BOOL same_file
= FALSE
;
935 h2
= CreateFileW( dest
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
936 OPEN_EXISTING
, 0, 0);
937 if (h2
!= INVALID_HANDLE_VALUE
)
939 same_file
= is_same_file( h1
, h2
);
944 HeapFree( GetProcessHeap(), 0, buffer
);
946 SetLastError( ERROR_SHARING_VIOLATION
);
951 if ((h2
= CreateFileW( dest
, GENERIC_WRITE
, FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
952 fail_if_exists
? CREATE_NEW
: CREATE_ALWAYS
,
953 info
.dwFileAttributes
, h1
)) == INVALID_HANDLE_VALUE
)
955 WARN("Unable to open dest %s\n", debugstr_w(dest
));
956 HeapFree( GetProcessHeap(), 0, buffer
);
961 while (ReadFile( h1
, buffer
, buffer_size
, &count
, NULL
) && count
)
967 if (!WriteFile( h2
, p
, count
, &res
, NULL
) || !res
) goto done
;
974 /* Maintain the timestamp of source file to destination file */
975 SetFileTime(h2
, NULL
, NULL
, &info
.ftLastWriteTime
);
976 HeapFree( GetProcessHeap(), 0, buffer
);
983 /**************************************************************************
984 * CopyFileA (KERNEL32.@)
986 BOOL WINAPI
CopyFileA( LPCSTR source
, LPCSTR dest
, BOOL fail_if_exists
)
988 WCHAR
*sourceW
, *destW
;
991 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
992 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
994 ret
= CopyFileW( sourceW
, destW
, fail_if_exists
);
996 HeapFree( GetProcessHeap(), 0, destW
);
1001 /**************************************************************************
1002 * CopyFileExW (KERNEL32.@)
1004 * This implementation ignores most of the extra parameters passed-in into
1005 * the "ex" version of the method and calls the CopyFile method.
1006 * It will have to be fixed eventually.
1008 BOOL WINAPI
CopyFileExW(LPCWSTR sourceFilename
, LPCWSTR destFilename
,
1009 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
1010 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
1013 * Interpret the only flag that CopyFile can interpret.
1015 return CopyFileW(sourceFilename
, destFilename
, (copyFlags
& COPY_FILE_FAIL_IF_EXISTS
) != 0);
1019 /**************************************************************************
1020 * CopyFileExA (KERNEL32.@)
1022 BOOL WINAPI
CopyFileExA(LPCSTR sourceFilename
, LPCSTR destFilename
,
1023 LPPROGRESS_ROUTINE progressRoutine
, LPVOID appData
,
1024 LPBOOL cancelFlagPointer
, DWORD copyFlags
)
1026 WCHAR
*sourceW
, *destW
;
1029 /* can't use the TEB buffer since we may have a callback routine */
1030 if (!(sourceW
= FILE_name_AtoW( sourceFilename
, TRUE
))) return FALSE
;
1031 if (!(destW
= FILE_name_AtoW( destFilename
, TRUE
)))
1033 HeapFree( GetProcessHeap(), 0, sourceW
);
1036 ret
= CopyFileExW(sourceW
, destW
, progressRoutine
, appData
,
1037 cancelFlagPointer
, copyFlags
);
1038 HeapFree( GetProcessHeap(), 0, sourceW
);
1039 HeapFree( GetProcessHeap(), 0, destW
);
1044 /**************************************************************************
1045 * MoveFileWithProgressW (KERNEL32.@)
1047 BOOL WINAPI
MoveFileWithProgressW( LPCWSTR source
, LPCWSTR dest
,
1048 LPPROGRESS_ROUTINE fnProgress
,
1049 LPVOID param
, DWORD flag
)
1051 FILE_BASIC_INFORMATION info
;
1052 UNICODE_STRING nt_name
;
1053 OBJECT_ATTRIBUTES attr
;
1056 HANDLE source_handle
= 0, dest_handle
;
1057 ANSI_STRING source_unix
, dest_unix
;
1059 TRACE("(%s,%s,%p,%p,%04x)\n",
1060 debugstr_w(source
), debugstr_w(dest
), fnProgress
, param
, flag
);
1062 if (flag
& MOVEFILE_DELAY_UNTIL_REBOOT
)
1063 return add_boot_rename_entry( source
, dest
, flag
);
1066 return DeleteFileW( source
);
1068 if (flag
& MOVEFILE_WRITE_THROUGH
)
1069 FIXME("MOVEFILE_WRITE_THROUGH unimplemented\n");
1071 /* check if we are allowed to rename the source */
1073 if (!RtlDosPathNameToNtPathName_U( source
, &nt_name
, NULL
, NULL
))
1075 SetLastError( ERROR_PATH_NOT_FOUND
);
1078 source_unix
.Buffer
= NULL
;
1079 dest_unix
.Buffer
= NULL
;
1080 attr
.Length
= sizeof(attr
);
1081 attr
.RootDirectory
= 0;
1082 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1083 attr
.ObjectName
= &nt_name
;
1084 attr
.SecurityDescriptor
= NULL
;
1085 attr
.SecurityQualityOfService
= NULL
;
1087 status
= NtOpenFile( &source_handle
, 0, &attr
, &io
, 0, FILE_SYNCHRONOUS_IO_NONALERT
);
1088 if (status
== STATUS_SUCCESS
)
1089 status
= wine_nt_to_unix_file_name( &nt_name
, &source_unix
, FILE_OPEN
, FALSE
);
1090 RtlFreeUnicodeString( &nt_name
);
1091 if (status
!= STATUS_SUCCESS
)
1093 SetLastError( RtlNtStatusToDosError(status
) );
1096 status
= NtQueryInformationFile( source_handle
, &io
, &info
, sizeof(info
), FileBasicInformation
);
1097 if (status
!= STATUS_SUCCESS
)
1099 SetLastError( RtlNtStatusToDosError(status
) );
1103 /* we must have write access to the destination, and it must */
1104 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1106 if (!RtlDosPathNameToNtPathName_U( dest
, &nt_name
, NULL
, NULL
))
1108 SetLastError( ERROR_PATH_NOT_FOUND
);
1111 status
= NtOpenFile( &dest_handle
, GENERIC_READ
| GENERIC_WRITE
, &attr
, &io
, 0,
1112 FILE_NON_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1113 if (status
== STATUS_SUCCESS
) /* destination exists */
1115 NtClose( dest_handle
);
1116 if (!(flag
& MOVEFILE_REPLACE_EXISTING
))
1118 SetLastError( ERROR_ALREADY_EXISTS
);
1119 RtlFreeUnicodeString( &nt_name
);
1122 else if (info
.FileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) /* cannot replace directory */
1124 SetLastError( ERROR_ACCESS_DENIED
);
1128 else if (status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1130 SetLastError( RtlNtStatusToDosError(status
) );
1131 RtlFreeUnicodeString( &nt_name
);
1135 status
= wine_nt_to_unix_file_name( &nt_name
, &dest_unix
, FILE_OPEN_IF
, FALSE
);
1136 RtlFreeUnicodeString( &nt_name
);
1137 if (status
!= STATUS_SUCCESS
&& status
!= STATUS_NO_SUCH_FILE
)
1139 SetLastError( RtlNtStatusToDosError(status
) );
1143 /* now perform the rename */
1145 if (rename( source_unix
.Buffer
, dest_unix
.Buffer
) == -1)
1147 if (errno
== EXDEV
&& (flag
& MOVEFILE_COPY_ALLOWED
))
1149 NtClose( source_handle
);
1150 RtlFreeAnsiString( &source_unix
);
1151 RtlFreeAnsiString( &dest_unix
);
1152 if (!CopyFileExW( source
, dest
, fnProgress
,
1153 param
, NULL
, COPY_FILE_FAIL_IF_EXISTS
))
1155 return DeleteFileW( source
);
1158 /* if we created the destination, remove it */
1159 if (io
.Information
== FILE_CREATED
) unlink( dest_unix
.Buffer
);
1163 /* fixup executable permissions */
1165 if (is_executable( source
) != is_executable( dest
))
1168 if (stat( dest_unix
.Buffer
, &fstat
) != -1)
1170 if (is_executable( dest
))
1171 /* set executable bit where read bit is set */
1172 fstat
.st_mode
|= (fstat
.st_mode
& 0444) >> 2;
1174 fstat
.st_mode
&= ~0111;
1175 chmod( dest_unix
.Buffer
, fstat
.st_mode
);
1179 NtClose( source_handle
);
1180 RtlFreeAnsiString( &source_unix
);
1181 RtlFreeAnsiString( &dest_unix
);
1185 if (source_handle
) NtClose( source_handle
);
1186 RtlFreeAnsiString( &source_unix
);
1187 RtlFreeAnsiString( &dest_unix
);
1191 /**************************************************************************
1192 * MoveFileWithProgressA (KERNEL32.@)
1194 BOOL WINAPI
MoveFileWithProgressA( LPCSTR source
, LPCSTR dest
,
1195 LPPROGRESS_ROUTINE fnProgress
,
1196 LPVOID param
, DWORD flag
)
1198 WCHAR
*sourceW
, *destW
;
1201 if (!(sourceW
= FILE_name_AtoW( source
, FALSE
))) return FALSE
;
1204 if (!(destW
= FILE_name_AtoW( dest
, TRUE
))) return FALSE
;
1209 ret
= MoveFileWithProgressW( sourceW
, destW
, fnProgress
, param
, flag
);
1210 HeapFree( GetProcessHeap(), 0, destW
);
1214 /**************************************************************************
1215 * MoveFileExW (KERNEL32.@)
1217 BOOL WINAPI
MoveFileExW( LPCWSTR source
, LPCWSTR dest
, DWORD flag
)
1219 return MoveFileWithProgressW( source
, dest
, NULL
, NULL
, flag
);
1222 /**************************************************************************
1223 * MoveFileExA (KERNEL32.@)
1225 BOOL WINAPI
MoveFileExA( LPCSTR source
, LPCSTR dest
, DWORD flag
)
1227 return MoveFileWithProgressA( source
, dest
, NULL
, NULL
, flag
);
1231 /**************************************************************************
1232 * MoveFileW (KERNEL32.@)
1234 * Move file or directory
1236 BOOL WINAPI
MoveFileW( LPCWSTR source
, LPCWSTR dest
)
1238 return MoveFileExW( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1242 /**************************************************************************
1243 * MoveFileA (KERNEL32.@)
1245 BOOL WINAPI
MoveFileA( LPCSTR source
, LPCSTR dest
)
1247 return MoveFileExA( source
, dest
, MOVEFILE_COPY_ALLOWED
);
1251 /*************************************************************************
1252 * CreateHardLinkW (KERNEL32.@)
1254 BOOL WINAPI
CreateHardLinkW(LPCWSTR lpFileName
, LPCWSTR lpExistingFileName
,
1255 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1258 UNICODE_STRING ntDest
, ntSource
;
1259 ANSI_STRING unixDest
, unixSource
;
1262 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName
),
1263 debugstr_w(lpExistingFileName
), lpSecurityAttributes
);
1265 ntDest
.Buffer
= ntSource
.Buffer
= NULL
;
1266 if (!RtlDosPathNameToNtPathName_U( lpFileName
, &ntDest
, NULL
, NULL
) ||
1267 !RtlDosPathNameToNtPathName_U( lpExistingFileName
, &ntSource
, NULL
, NULL
))
1269 SetLastError( ERROR_PATH_NOT_FOUND
);
1273 unixSource
.Buffer
= unixDest
.Buffer
= NULL
;
1274 status
= wine_nt_to_unix_file_name( &ntSource
, &unixSource
, FILE_OPEN
, FALSE
);
1277 status
= wine_nt_to_unix_file_name( &ntDest
, &unixDest
, FILE_CREATE
, FALSE
);
1278 if (!status
) /* destination must not exist */
1280 status
= STATUS_OBJECT_NAME_EXISTS
;
1281 } else if (status
== STATUS_NO_SUCH_FILE
)
1283 status
= STATUS_SUCCESS
;
1288 SetLastError( RtlNtStatusToDosError(status
) );
1289 else if (!link( unixSource
.Buffer
, unixDest
.Buffer
))
1291 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest
.Buffer
),
1292 debugstr_a( unixSource
.Buffer
));
1298 RtlFreeAnsiString( &unixSource
);
1299 RtlFreeAnsiString( &unixDest
);
1302 RtlFreeUnicodeString( &ntSource
);
1303 RtlFreeUnicodeString( &ntDest
);
1308 /*************************************************************************
1309 * CreateHardLinkA (KERNEL32.@)
1311 BOOL WINAPI
CreateHardLinkA(LPCSTR lpFileName
, LPCSTR lpExistingFileName
,
1312 LPSECURITY_ATTRIBUTES lpSecurityAttributes
)
1314 WCHAR
*sourceW
, *destW
;
1317 if (!(sourceW
= FILE_name_AtoW( lpExistingFileName
, TRUE
)))
1321 if (!(destW
= FILE_name_AtoW( lpFileName
, TRUE
)))
1323 HeapFree( GetProcessHeap(), 0, sourceW
);
1327 res
= CreateHardLinkW( destW
, sourceW
, lpSecurityAttributes
);
1329 HeapFree( GetProcessHeap(), 0, sourceW
);
1330 HeapFree( GetProcessHeap(), 0, destW
);
1336 /***********************************************************************
1337 * CreateDirectoryW (KERNEL32.@)
1341 * ERROR_DISK_FULL: on full disk
1342 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1343 * ERROR_ACCESS_DENIED: on permission problems
1344 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1346 BOOL WINAPI
CreateDirectoryW( LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1348 OBJECT_ATTRIBUTES attr
;
1349 UNICODE_STRING nt_name
;
1355 TRACE( "%s\n", debugstr_w(path
) );
1357 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1359 SetLastError( ERROR_PATH_NOT_FOUND
);
1362 attr
.Length
= sizeof(attr
);
1363 attr
.RootDirectory
= 0;
1364 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1365 attr
.ObjectName
= &nt_name
;
1366 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1367 attr
.SecurityQualityOfService
= NULL
;
1369 status
= NtCreateFile( &handle
, GENERIC_READ
, &attr
, &io
, NULL
,
1370 FILE_ATTRIBUTE_NORMAL
, FILE_SHARE_READ
, FILE_CREATE
,
1371 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
, NULL
, 0 );
1373 if (status
== STATUS_SUCCESS
)
1378 else SetLastError( RtlNtStatusToDosError(status
) );
1380 RtlFreeUnicodeString( &nt_name
);
1385 /***********************************************************************
1386 * CreateDirectoryA (KERNEL32.@)
1388 BOOL WINAPI
CreateDirectoryA( LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1392 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1393 return CreateDirectoryW( pathW
, sa
);
1397 /***********************************************************************
1398 * CreateDirectoryExA (KERNEL32.@)
1400 BOOL WINAPI
CreateDirectoryExA( LPCSTR
template, LPCSTR path
, LPSECURITY_ATTRIBUTES sa
)
1402 WCHAR
*pathW
, *templateW
= NULL
;
1405 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1406 if (template && !(templateW
= FILE_name_AtoW( template, TRUE
))) return FALSE
;
1408 ret
= CreateDirectoryExW( templateW
, pathW
, sa
);
1409 HeapFree( GetProcessHeap(), 0, templateW
);
1414 /***********************************************************************
1415 * CreateDirectoryExW (KERNEL32.@)
1417 BOOL WINAPI
CreateDirectoryExW( LPCWSTR
template, LPCWSTR path
, LPSECURITY_ATTRIBUTES sa
)
1419 return CreateDirectoryW( path
, sa
);
1423 /***********************************************************************
1424 * RemoveDirectoryW (KERNEL32.@)
1426 BOOL WINAPI
RemoveDirectoryW( LPCWSTR path
)
1428 OBJECT_ATTRIBUTES attr
;
1429 UNICODE_STRING nt_name
;
1430 ANSI_STRING unix_name
;
1436 TRACE( "%s\n", debugstr_w(path
) );
1438 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1440 SetLastError( ERROR_PATH_NOT_FOUND
);
1443 attr
.Length
= sizeof(attr
);
1444 attr
.RootDirectory
= 0;
1445 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1446 attr
.ObjectName
= &nt_name
;
1447 attr
.SecurityDescriptor
= NULL
;
1448 attr
.SecurityQualityOfService
= NULL
;
1450 status
= NtOpenFile( &handle
, DELETE
, &attr
, &io
,
1451 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1452 FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
);
1453 if (status
== STATUS_SUCCESS
)
1454 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
);
1455 RtlFreeUnicodeString( &nt_name
);
1457 if (status
!= STATUS_SUCCESS
)
1459 SetLastError( RtlNtStatusToDosError(status
) );
1463 if (!(ret
= (rmdir( unix_name
.Buffer
) != -1))) FILE_SetDosError();
1464 RtlFreeAnsiString( &unix_name
);
1470 /***********************************************************************
1471 * RemoveDirectoryA (KERNEL32.@)
1473 BOOL WINAPI
RemoveDirectoryA( LPCSTR path
)
1477 if (!(pathW
= FILE_name_AtoW( path
, FALSE
))) return FALSE
;
1478 return RemoveDirectoryW( pathW
);
1482 /***********************************************************************
1483 * GetCurrentDirectoryW (KERNEL32.@)
1485 UINT WINAPI
GetCurrentDirectoryW( UINT buflen
, LPWSTR buf
)
1487 return RtlGetCurrentDirectory_U( buflen
* sizeof(WCHAR
), buf
) / sizeof(WCHAR
);
1491 /***********************************************************************
1492 * GetCurrentDirectoryA (KERNEL32.@)
1494 UINT WINAPI
GetCurrentDirectoryA( UINT buflen
, LPSTR buf
)
1496 WCHAR bufferW
[MAX_PATH
];
1499 if (buflen
&& buf
&& ((ULONG_PTR
)buf
>> 16) == 0)
1501 /* Win9x catches access violations here, returning zero.
1502 * This behaviour resulted in some people not noticing
1503 * that they got the argument order wrong. So let's be
1504 * nice and fail gracefully if buf is invalid and looks
1505 * more like a buflen. */
1506 SetLastError(ERROR_INVALID_PARAMETER
);
1510 ret
= GetCurrentDirectoryW(MAX_PATH
, bufferW
);
1515 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
1518 return copy_filename_WtoA( bufferW
, buf
, buflen
);
1522 /***********************************************************************
1523 * SetCurrentDirectoryW (KERNEL32.@)
1525 BOOL WINAPI
SetCurrentDirectoryW( LPCWSTR dir
)
1527 UNICODE_STRING dirW
;
1530 RtlInitUnicodeString( &dirW
, dir
);
1531 status
= RtlSetCurrentDirectory_U( &dirW
);
1532 if (status
!= STATUS_SUCCESS
)
1534 SetLastError( RtlNtStatusToDosError(status
) );
1541 /***********************************************************************
1542 * SetCurrentDirectoryA (KERNEL32.@)
1544 BOOL WINAPI
SetCurrentDirectoryA( LPCSTR dir
)
1548 if (!(dirW
= FILE_name_AtoW( dir
, FALSE
))) return FALSE
;
1549 return SetCurrentDirectoryW( dirW
);
1553 /***********************************************************************
1554 * GetWindowsDirectoryW (KERNEL32.@)
1556 * See comment for GetWindowsDirectoryA.
1558 UINT WINAPI
GetWindowsDirectoryW( LPWSTR path
, UINT count
)
1560 UINT len
= strlenW( DIR_Windows
) + 1;
1561 if (path
&& count
>= len
)
1563 strcpyW( path
, DIR_Windows
);
1570 /***********************************************************************
1571 * GetWindowsDirectoryA (KERNEL32.@)
1574 * If buffer is large enough to hold full path and terminating '\0' character
1575 * function copies path to buffer and returns length of the path without '\0'.
1576 * Otherwise function returns required size including '\0' character and
1577 * does not touch the buffer.
1579 UINT WINAPI
GetWindowsDirectoryA( LPSTR path
, UINT count
)
1581 return copy_filename_WtoA( DIR_Windows
, path
, count
);
1585 /***********************************************************************
1586 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1588 UINT WINAPI
GetSystemWindowsDirectoryA( LPSTR path
, UINT count
)
1590 return GetWindowsDirectoryA( path
, count
);
1594 /***********************************************************************
1595 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1597 UINT WINAPI
GetSystemWindowsDirectoryW( LPWSTR path
, UINT count
)
1599 return GetWindowsDirectoryW( path
, count
);
1603 /***********************************************************************
1604 * GetSystemDirectoryW (KERNEL32.@)
1606 * See comment for GetWindowsDirectoryA.
1608 UINT WINAPI
GetSystemDirectoryW( LPWSTR path
, UINT count
)
1610 UINT len
= strlenW( DIR_System
) + 1;
1611 if (path
&& count
>= len
)
1613 strcpyW( path
, DIR_System
);
1620 /***********************************************************************
1621 * GetSystemDirectoryA (KERNEL32.@)
1623 * See comment for GetWindowsDirectoryA.
1625 UINT WINAPI
GetSystemDirectoryA( LPSTR path
, UINT count
)
1627 return copy_filename_WtoA( DIR_System
, path
, count
);
1631 /***********************************************************************
1632 * GetSystemWow64DirectoryW (KERNEL32.@)
1635 * - On Win32 we should returns ERROR_CALL_NOT_IMPLEMENTED
1636 * - On Win64 we should returns the SysWow64 (system64) directory
1638 UINT WINAPI
GetSystemWow64DirectoryW( LPWSTR path
, UINT count
)
1644 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1647 len
= strlenW( DIR_SysWow64
) + 1;
1648 if (path
&& count
>= len
)
1650 strcpyW( path
, DIR_SysWow64
);
1657 /***********************************************************************
1658 * GetSystemWow64DirectoryA (KERNEL32.@)
1660 * See comment for GetWindowsWow64DirectoryW.
1662 UINT WINAPI
GetSystemWow64DirectoryA( LPSTR path
, UINT count
)
1666 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);
1669 return copy_filename_WtoA( DIR_SysWow64
, path
, count
);
1673 /***********************************************************************
1674 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1676 BOOLEAN WINAPI
Wow64EnableWow64FsRedirection( BOOLEAN enable
)
1678 NTSTATUS status
= RtlWow64EnableFsRedirection( enable
);
1679 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1684 /***********************************************************************
1685 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1687 BOOL WINAPI
Wow64DisableWow64FsRedirection( PVOID
*old_value
)
1689 NTSTATUS status
= RtlWow64EnableFsRedirectionEx( TRUE
, (ULONG
*)old_value
);
1690 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1695 /***********************************************************************
1696 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1698 BOOL WINAPI
Wow64RevertWow64FsRedirection( PVOID old_value
)
1700 NTSTATUS status
= RtlWow64EnableFsRedirection( !old_value
);
1701 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
1706 /***********************************************************************
1707 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1709 BOOL WINAPI
NeedCurrentDirectoryForExePathW( LPCWSTR name
)
1711 static const WCHAR env_name
[] = {'N','o','D','e','f','a','u','l','t',
1712 'C','u','r','r','e','n','t',
1713 'D','i','r','e','c','t','o','r','y',
1714 'I','n','E','x','e','P','a','t','h',0};
1717 /* MSDN mentions some 'registry location'. We do not use registry. */
1718 FIXME("(%s): partial stub\n", debugstr_w(name
));
1720 if (strchrW(name
, '\\'))
1723 /* Check the existence of the variable, not value */
1724 if (!GetEnvironmentVariableW( env_name
, &env_val
, 1 ))
1731 /***********************************************************************
1732 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
1734 BOOL WINAPI
NeedCurrentDirectoryForExePathA( LPCSTR name
)
1738 if (!(nameW
= FILE_name_AtoW( name
, FALSE
))) return TRUE
;
1739 return NeedCurrentDirectoryForExePathW( nameW
);
1743 /***********************************************************************
1744 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1746 * Return the full Unix file name for a given path.
1747 * Returned buffer must be freed by caller.
1749 char * CDECL
wine_get_unix_file_name( LPCWSTR dosW
)
1751 UNICODE_STRING nt_name
;
1752 ANSI_STRING unix_name
;
1755 if (!RtlDosPathNameToNtPathName_U( dosW
, &nt_name
, NULL
, NULL
)) return NULL
;
1756 status
= wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN_IF
, FALSE
);
1757 RtlFreeUnicodeString( &nt_name
);
1758 if (status
&& status
!= STATUS_NO_SUCH_FILE
)
1760 SetLastError( RtlNtStatusToDosError( status
) );
1763 return unix_name
.Buffer
;
1767 /***********************************************************************
1768 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
1770 * Return the full DOS file name for a given Unix path.
1771 * Returned buffer must be freed by caller.
1773 WCHAR
* CDECL
wine_get_dos_file_name( LPCSTR str
)
1775 UNICODE_STRING nt_name
;
1776 ANSI_STRING unix_name
;
1780 RtlInitAnsiString( &unix_name
, str
);
1781 status
= wine_unix_to_nt_file_name( &unix_name
, &nt_name
);
1784 SetLastError( RtlNtStatusToDosError( status
) );
1787 /* get rid of the \??\ prefix */
1788 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
1789 len
= nt_name
.Length
- 4 * sizeof(WCHAR
);
1790 memmove( nt_name
.Buffer
, nt_name
.Buffer
+ 4, len
);
1791 nt_name
.Buffer
[len
/ sizeof(WCHAR
)] = 0;
1792 return nt_name
.Buffer
;