kernel32: Implement safe search mode for SearchPath.
[wine.git] / dlls / kernel32 / path.c
bloba3897431717d39e4f43db2e7aa5ca1538cf3d761
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdarg.h>
31 #include "winerror.h"
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winternl.h"
38 #include "kernel_private.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(file);
44 #define MAX_PATHNAME_LEN 1024
46 static int path_safe_mode = -1; /* path mode set by SetSearchPathMode */
48 /* check if a file name is for an executable file (.exe or .com) */
49 static inline BOOL is_executable( const WCHAR *name )
51 static const WCHAR exeW[] = {'.','e','x','e',0};
52 static const WCHAR comW[] = {'.','c','o','m',0};
53 int len = strlenW(name);
55 if (len < 4) return FALSE;
56 return (!strcmpiW( name + len - 4, exeW ) || !strcmpiW( name + len - 4, comW ));
59 /***********************************************************************
60 * copy_filename_WtoA
62 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
64 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
66 UNICODE_STRING strW;
67 DWORD ret;
68 BOOL is_ansi = AreFileApisANSI();
70 RtlInitUnicodeString( &strW, nameW );
72 ret = is_ansi ? RtlUnicodeStringToAnsiSize(&strW) : RtlUnicodeStringToOemSize(&strW);
73 if (buffer && ret <= len)
75 ANSI_STRING str;
77 str.Buffer = buffer;
78 str.MaximumLength = min( len, UNICODE_STRING_MAX_CHARS );
79 if (is_ansi)
80 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
81 else
82 RtlUnicodeStringToOemString( &str, &strW, FALSE );
83 ret = str.Length; /* length without terminating 0 */
85 return ret;
88 /***********************************************************************
89 * add_boot_rename_entry
91 * Adds an entry to the registry that is loaded when windows boots and
92 * checks if there are some files to be removed or renamed/moved.
93 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
94 * non-NULL then the file is moved, otherwise it is deleted. The
95 * entry of the registry key is always appended with two zero
96 * terminated strings. If <fn2> is NULL then the second entry is
97 * simply a single 0-byte. Otherwise the second filename goes
98 * there. The entries are prepended with \??\ before the path and the
99 * second filename gets also a '!' as the first character if
100 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
101 * 0-byte follows to indicate the end of the strings.
102 * i.e.:
103 * \??\D:\test\file1[0]
104 * !\??\D:\test\file1_renamed[0]
105 * \??\D:\Test|delete[0]
106 * [0] <- file is to be deleted, second string empty
107 * \??\D:\test\file2[0]
108 * !\??\D:\test\file2_renamed[0]
109 * [0] <- indicates end of strings
111 * or:
112 * \??\D:\test\file1[0]
113 * !\??\D:\test\file1_renamed[0]
114 * \??\D:\Test|delete[0]
115 * [0] <- file is to be deleted, second string empty
116 * [0] <- indicates end of strings
119 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
121 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
122 'F','i','l','e','R','e','n','a','m','e',
123 'O','p','e','r','a','t','i','o','n','s',0};
124 static const WCHAR SessionW[] = {'\\','R','e','g','i','s','t','r','y','\\',
125 '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;
135 BOOL rc = FALSE;
136 HANDLE Reboot = 0;
137 DWORD len1, len2;
138 DWORD DataSize = 0;
139 BYTE *Buffer = NULL;
140 WCHAR *p;
142 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
145 return FALSE;
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 );
152 return FALSE;
155 attr.Length = sizeof(attr);
156 attr.RootDirectory = 0;
157 attr.ObjectName = &nameW;
158 attr.Attributes = 0;
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 management [%s]\n",
166 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
167 RtlFreeUnicodeString( &source_name );
168 RtlFreeUnicodeString( &dest_name );
169 return FALSE;
172 len1 = source_name.Length + sizeof(WCHAR);
173 if (dest)
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) )))
188 goto Quit;
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) */
195 else
197 DataSize = info_size;
198 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
199 goto Quit;
202 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
203 DataSize += len1;
204 p = (WCHAR *)(Buffer + DataSize);
205 if (dest)
207 if (flags & MOVEFILE_REPLACE_EXISTING)
208 *p++ = '!';
209 memcpy( p, dest_name.Buffer, len2 );
210 DataSize += len2;
212 else
214 *p = 0;
215 DataSize += sizeof(WCHAR);
218 /* add final null */
219 p = (WCHAR *)(Buffer + DataSize);
220 *p = 0;
221 DataSize += sizeof(WCHAR);
223 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
225 Quit:
226 RtlFreeUnicodeString( &source_name );
227 RtlFreeUnicodeString( &dest_name );
228 if (Reboot) NtClose(Reboot);
229 HeapFree( GetProcessHeap(), 0, Buffer );
230 return(rc);
234 /***********************************************************************
235 * GetFullPathNameW (KERNEL32.@)
236 * NOTES
237 * if the path closed with '\', *lastpart is 0
239 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
240 LPWSTR *lastpart )
242 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
245 /***********************************************************************
246 * GetFullPathNameA (KERNEL32.@)
247 * NOTES
248 * if the path closed with '\', *lastpart is 0
250 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
251 LPSTR *lastpart )
253 WCHAR *nameW;
254 WCHAR bufferW[MAX_PATH], *lastpartW = NULL;
255 DWORD ret;
257 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
259 ret = GetFullPathNameW( nameW, MAX_PATH, bufferW, &lastpartW);
261 if (!ret) return 0;
262 if (ret > MAX_PATH)
264 SetLastError(ERROR_FILENAME_EXCED_RANGE);
265 return 0;
267 ret = copy_filename_WtoA( bufferW, buffer, len );
268 if (ret < len && lastpart)
270 if (lastpartW)
271 *lastpart = buffer + FILE_name_WtoA( bufferW, lastpartW - bufferW, NULL, 0 );
272 else
273 *lastpart = NULL;
275 return ret;
279 /***********************************************************************
280 * GetLongPathNameW (KERNEL32.@)
282 * NOTES
283 * observed (Win2000):
284 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
285 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
287 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
289 WCHAR tmplongpath[MAX_PATHNAME_LEN];
290 LPCWSTR p;
291 DWORD sp = 0, lp = 0;
292 DWORD tmplen;
293 BOOL unixabsolute;
294 WIN32_FIND_DATAW wfd;
295 HANDLE goit;
296 BOOL is_legal_8dot3;
298 if (!shortpath)
300 SetLastError(ERROR_INVALID_PARAMETER);
301 return 0;
303 if (!shortpath[0])
305 SetLastError(ERROR_PATH_NOT_FOUND);
306 return 0;
309 TRACE("%s,%p,%d\n", debugstr_w(shortpath), longpath, longlen);
311 if (shortpath[0] == '\\' && shortpath[1] == '\\')
313 FIXME("UNC pathname %s\n", debugstr_w(shortpath));
315 tmplen = strlenW(shortpath);
316 if (tmplen < longlen)
318 if (longpath != shortpath) strcpyW( longpath, shortpath );
319 return tmplen;
321 return tmplen + 1;
324 unixabsolute = (shortpath[0] == '/');
326 /* check for drive letter */
327 if (!unixabsolute && shortpath[1] == ':' )
329 tmplongpath[0] = shortpath[0];
330 tmplongpath[1] = ':';
331 lp = sp = 2;
334 while (shortpath[sp])
336 /* check for path delimiters and reproduce them */
337 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
339 tmplongpath[lp++] = shortpath[sp++];
340 tmplongpath[lp] = 0; /* terminate string */
341 continue;
344 p = shortpath + sp;
345 for (; *p && *p != '/' && *p != '\\'; p++);
346 tmplen = p - (shortpath + sp);
347 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
349 if (tmplongpath[lp] == '.')
351 if (tmplen == 1 || (tmplen == 2 && tmplongpath[lp + 1] == '.'))
353 lp += tmplen;
354 sp += tmplen;
355 continue;
359 /* Check if the file exists */
360 goit = FindFirstFileW(tmplongpath, &wfd);
361 if (goit == INVALID_HANDLE_VALUE)
363 TRACE("not found %s!\n", debugstr_w(tmplongpath));
364 SetLastError ( ERROR_FILE_NOT_FOUND );
365 return 0;
367 FindClose(goit);
369 is_legal_8dot3 = FALSE;
370 CheckNameLegalDOS8Dot3W(tmplongpath + lp, NULL, 0, NULL, &is_legal_8dot3);
371 /* Use the existing file name if it's a short name */
372 if (is_legal_8dot3)
373 strcpyW(tmplongpath + lp, wfd.cFileName);
374 lp += strlenW(tmplongpath + lp);
375 sp += tmplen;
377 tmplen = strlenW(shortpath) - 1;
378 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
379 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
380 tmplongpath[lp++] = shortpath[tmplen];
381 tmplongpath[lp] = 0;
383 tmplen = strlenW(tmplongpath) + 1;
384 if (tmplen <= longlen)
386 strcpyW(longpath, tmplongpath);
387 TRACE("returning %s\n", debugstr_w(longpath));
388 tmplen--; /* length without 0 */
391 return tmplen;
394 /***********************************************************************
395 * GetLongPathNameA (KERNEL32.@)
397 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
399 WCHAR *shortpathW;
400 WCHAR longpathW[MAX_PATH];
401 DWORD ret;
403 TRACE("%s\n", debugstr_a(shortpath));
405 if (!(shortpathW = FILE_name_AtoW( shortpath, FALSE ))) return 0;
407 ret = GetLongPathNameW(shortpathW, longpathW, MAX_PATH);
409 if (!ret) return 0;
410 if (ret > MAX_PATH)
412 SetLastError(ERROR_FILENAME_EXCED_RANGE);
413 return 0;
415 return copy_filename_WtoA( longpathW, longpath, longlen );
419 /***********************************************************************
420 * GetShortPathNameW (KERNEL32.@)
422 * NOTES
423 * observed:
424 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
425 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
427 * more observations ( with NT 3.51 (WinDD) ):
428 * longpath <= 8.3 -> just copy longpath to shortpath
429 * longpath > 8.3 ->
430 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
431 * b) file does exist -> set the short filename.
432 * - trailing slashes are reproduced in the short name, even if the
433 * file is not a directory
434 * - the absolute/relative path of the short name is reproduced like found
435 * in the long name
436 * - longpath and shortpath may have the same address
437 * Peter Ganten, 1999
439 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
441 WCHAR *tmpshortpath;
442 LPCWSTR p;
443 DWORD sp = 0, lp = 0;
444 DWORD tmplen, buf_len;
445 WIN32_FIND_DATAW wfd;
446 HANDLE goit;
448 TRACE("%s\n", debugstr_w(longpath));
450 if (!longpath)
452 SetLastError(ERROR_INVALID_PARAMETER);
453 return 0;
455 if (!longpath[0])
457 SetLastError(ERROR_BAD_PATHNAME);
458 return 0;
461 /* code below only removes characters from string, never adds, so this is
462 * the largest buffer that tmpshortpath will need to have */
463 buf_len = strlenW(longpath) + 1;
464 tmpshortpath = HeapAlloc(GetProcessHeap(), 0, buf_len * sizeof(WCHAR));
465 if (!tmpshortpath)
467 SetLastError(ERROR_OUTOFMEMORY);
468 return 0;
471 if (longpath[0] == '\\' && longpath[1] == '\\' && longpath[2] == '?' && longpath[3] == '\\')
473 memcpy(tmpshortpath, longpath, 4 * sizeof(WCHAR));
474 sp = lp = 4;
477 /* check for drive letter */
478 if (longpath[lp] != '/' && longpath[lp + 1] == ':' )
480 tmpshortpath[sp] = longpath[lp];
481 tmpshortpath[sp + 1] = ':';
482 sp += 2;
483 lp += 2;
486 while (longpath[lp])
488 /* check for path delimiters and reproduce them */
489 if (longpath[lp] == '\\' || longpath[lp] == '/')
491 tmpshortpath[sp++] = longpath[lp++];
492 tmpshortpath[sp] = 0; /* terminate string */
493 continue;
496 p = longpath + lp;
497 for (; *p && *p != '/' && *p != '\\'; p++);
498 tmplen = p - (longpath + lp);
499 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
501 if (tmpshortpath[sp] == '.')
503 if (tmplen == 1 || (tmplen == 2 && tmpshortpath[sp + 1] == '.'))
505 sp += tmplen;
506 lp += tmplen;
507 continue;
511 /* Check if the file exists and use the existing short file name */
512 goit = FindFirstFileW(tmpshortpath, &wfd);
513 if (goit == INVALID_HANDLE_VALUE) goto notfound;
514 FindClose(goit);
516 /* In rare cases (like "a.abcd") short path may be longer than original path.
517 * Make sure we have enough space in temp buffer. */
518 if (wfd.cAlternateFileName[0] && tmplen < strlenW(wfd.cAlternateFileName))
520 WCHAR *new_buf;
521 buf_len += strlenW(wfd.cAlternateFileName) - tmplen;
522 new_buf = HeapReAlloc(GetProcessHeap(), 0, tmpshortpath, buf_len * sizeof(WCHAR));
523 if(!new_buf)
525 HeapFree(GetProcessHeap(), 0, tmpshortpath);
526 SetLastError(ERROR_OUTOFMEMORY);
527 return 0;
529 tmpshortpath = new_buf;
532 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName[0] ? wfd.cAlternateFileName : wfd.cFileName);
533 sp += strlenW(tmpshortpath + sp);
534 lp += tmplen;
536 tmpshortpath[sp] = 0;
538 tmplen = strlenW(tmpshortpath) + 1;
539 if (tmplen <= shortlen)
541 strcpyW(shortpath, tmpshortpath);
542 TRACE("returning %s\n", debugstr_w(shortpath));
543 tmplen--; /* length without 0 */
546 HeapFree(GetProcessHeap(), 0, tmpshortpath);
547 return tmplen;
549 notfound:
550 HeapFree(GetProcessHeap(), 0, tmpshortpath);
551 TRACE("not found!\n" );
552 SetLastError ( ERROR_FILE_NOT_FOUND );
553 return 0;
556 /***********************************************************************
557 * GetShortPathNameA (KERNEL32.@)
559 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
561 WCHAR *longpathW;
562 WCHAR shortpathW[MAX_PATH];
563 DWORD ret;
565 TRACE("%s\n", debugstr_a(longpath));
567 if (!(longpathW = FILE_name_AtoW( longpath, FALSE ))) return 0;
569 ret = GetShortPathNameW(longpathW, shortpathW, MAX_PATH);
571 if (!ret) return 0;
572 if (ret > MAX_PATH)
574 SetLastError(ERROR_FILENAME_EXCED_RANGE);
575 return 0;
577 return copy_filename_WtoA( shortpathW, shortpath, shortlen );
581 /***********************************************************************
582 * GetTempPathA (KERNEL32.@)
584 DWORD WINAPI GetTempPathA( DWORD count, LPSTR path )
586 WCHAR pathW[MAX_PATH];
587 UINT ret;
589 ret = GetTempPathW(MAX_PATH, pathW);
591 if (!ret)
592 return 0;
594 if (ret > MAX_PATH)
596 SetLastError(ERROR_FILENAME_EXCED_RANGE);
597 return 0;
599 return copy_filename_WtoA( pathW, path, count );
603 /***********************************************************************
604 * GetTempPathW (KERNEL32.@)
606 DWORD WINAPI GetTempPathW( DWORD count, LPWSTR path )
608 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
609 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
610 static const WCHAR userprofile[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
611 WCHAR tmp_path[MAX_PATH];
612 UINT ret;
614 TRACE("%u,%p\n", count, path);
616 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )) &&
617 !(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )) &&
618 !(ret = GetEnvironmentVariableW( userprofile, tmp_path, MAX_PATH )) &&
619 !(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
620 return 0;
622 if (ret > MAX_PATH)
624 SetLastError(ERROR_FILENAME_EXCED_RANGE);
625 return 0;
628 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
629 if (!ret) return 0;
631 if (ret > MAX_PATH - 2)
633 SetLastError(ERROR_FILENAME_EXCED_RANGE);
634 return 0;
637 if (tmp_path[ret-1] != '\\')
639 tmp_path[ret++] = '\\';
640 tmp_path[ret] = '\0';
643 ret++; /* add space for terminating 0 */
645 if (count >= ret)
647 lstrcpynW(path, tmp_path, count);
648 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
649 * bytes after it, we will assume the > XP behavior for now */
650 memset(path + ret, 0, (min(count, 32767) - ret) * sizeof(WCHAR));
651 ret--; /* return length without 0 */
653 else if (count)
655 /* the buffer must be cleared if contents will not fit */
656 memset(path, 0, count * sizeof(WCHAR));
659 TRACE("returning %u, %s\n", ret, debugstr_w(path));
660 return ret;
664 /***********************************************************************
665 * GetTempFileNameA (KERNEL32.@)
667 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
669 WCHAR *pathW, *prefixW = NULL;
670 WCHAR bufferW[MAX_PATH];
671 UINT ret;
673 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return 0;
674 if (prefix && !(prefixW = FILE_name_AtoW( prefix, TRUE ))) return 0;
676 ret = GetTempFileNameW(pathW, prefixW, unique, bufferW);
677 if (ret) FILE_name_WtoA( bufferW, -1, buffer, MAX_PATH );
679 HeapFree( GetProcessHeap(), 0, prefixW );
680 return ret;
683 /***********************************************************************
684 * GetTempFileNameW (KERNEL32.@)
686 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
688 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
690 int i;
691 LPWSTR p;
692 DWORD attr;
694 if ( !path || !buffer )
696 SetLastError( ERROR_INVALID_PARAMETER );
697 return 0;
700 /* ensure that the provided directory exists */
701 attr = GetFileAttributesW(path);
702 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
704 TRACE("path not found %s\n", debugstr_w(path));
705 SetLastError( ERROR_DIRECTORY );
706 return 0;
709 strcpyW( buffer, path );
710 p = buffer + strlenW(buffer);
712 /* add a \, if there isn't one */
713 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
715 if (prefix)
716 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
718 unique &= 0xffff;
720 if (unique) sprintfW( p, formatW, unique );
721 else
723 /* get a "random" unique number and try to create the file */
724 HANDLE handle;
725 UINT num = GetTickCount() & 0xffff;
726 static UINT last;
728 /* avoid using the same name twice in a short interval */
729 if (last - num < 10) num = last + 1;
730 if (!num) num = 1;
731 unique = num;
734 sprintfW( p, formatW, unique );
735 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
736 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
737 if (handle != INVALID_HANDLE_VALUE)
738 { /* We created it */
739 TRACE("created %s\n", debugstr_w(buffer) );
740 CloseHandle( handle );
741 last = unique;
742 break;
744 if (GetLastError() != ERROR_FILE_EXISTS &&
745 GetLastError() != ERROR_SHARING_VIOLATION)
746 break; /* No need to go on */
747 if (!(++unique & 0xffff)) unique = 1;
748 } while (unique != num);
751 TRACE("returning %s\n", debugstr_w(buffer) );
752 return unique;
756 /***********************************************************************
757 * get_path_safe_mode
759 static BOOL get_path_safe_mode(void)
761 static const WCHAR keyW[] = {'\\','R','e','g','i','s','t','r','y','\\',
762 'M','a','c','h','i','n','e','\\',
763 'S','y','s','t','e','m','\\',
764 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
765 'C','o','n','t','r','o','l','\\',
766 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
767 static const WCHAR valueW[] = {'S','a','f','e','P','r','o','c','e','s','s','S','e','a','r','c','h','M','o','d','e',0};
769 if (path_safe_mode == -1)
771 char buffer[offsetof(KEY_VALUE_PARTIAL_INFORMATION, Data[sizeof(DWORD)])];
772 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
773 OBJECT_ATTRIBUTES attr;
774 UNICODE_STRING nameW;
775 HANDLE hkey;
776 DWORD size = sizeof(buffer);
777 BOOL mode = FALSE;
779 attr.Length = sizeof(attr);
780 attr.RootDirectory = 0;
781 attr.ObjectName = &nameW;
782 attr.Attributes = 0;
783 attr.SecurityDescriptor = NULL;
784 attr.SecurityQualityOfService = NULL;
786 RtlInitUnicodeString( &nameW, keyW );
787 if (!NtOpenKey( &hkey, KEY_READ, &attr ))
789 RtlInitUnicodeString( &nameW, valueW );
790 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, buffer, size, &size ) &&
791 info->Type == REG_DWORD && info->DataLength == sizeof(DWORD))
792 mode = !!*(DWORD *)info->Data;
793 NtClose( hkey );
795 InterlockedCompareExchange( &path_safe_mode, mode, -1 );
797 return path_safe_mode != 0;
801 /***********************************************************************
802 * contains_pathW
804 * Check if the file name contains a path; helper for SearchPathW.
805 * A relative path is not considered a path unless it starts with ./ or ../
807 static inline BOOL contains_pathW (LPCWSTR name)
809 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
810 if (name[0] != '.') return FALSE;
811 if (name[1] == '/' || name[1] == '\\') return TRUE;
812 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
815 /***********************************************************************
816 * find_actctx_dllpath
818 * Find the path (if any) of the dll from the activation context.
819 * Returned path doesn't include a name.
821 static NTSTATUS find_actctx_dllpath(const WCHAR *libname, WCHAR **path)
823 static const WCHAR winsxsW[] = {'\\','w','i','n','s','x','s','\\'};
824 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
826 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *info;
827 ACTCTX_SECTION_KEYED_DATA data;
828 UNICODE_STRING nameW;
829 NTSTATUS status;
830 SIZE_T needed, size = 1024;
831 WCHAR *p;
833 RtlInitUnicodeString( &nameW, libname );
834 data.cbSize = sizeof(data);
835 status = RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, NULL,
836 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
837 &nameW, &data );
838 if (status != STATUS_SUCCESS) return status;
840 for (;;)
842 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
844 status = STATUS_NO_MEMORY;
845 goto done;
847 status = RtlQueryInformationActivationContext( 0, data.hActCtx, &data.ulAssemblyRosterIndex,
848 AssemblyDetailedInformationInActivationContext,
849 info, size, &needed );
850 if (status == STATUS_SUCCESS) break;
851 if (status != STATUS_BUFFER_TOO_SMALL) goto done;
852 HeapFree( GetProcessHeap(), 0, info );
853 size = needed;
854 /* restart with larger buffer */
857 if (!info->lpAssemblyManifestPath || !info->lpAssemblyDirectoryName)
859 status = STATUS_SXS_KEY_NOT_FOUND;
860 goto done;
863 if ((p = strrchrW( info->lpAssemblyManifestPath, '\\' )))
865 DWORD dirlen = info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
867 p++;
868 if (strncmpiW( p, info->lpAssemblyDirectoryName, dirlen ) || strcmpiW( p + dirlen, dotManifestW ))
870 /* manifest name does not match directory name, so it's not a global
871 * windows/winsxs manifest; use the manifest directory name instead */
872 dirlen = p - info->lpAssemblyManifestPath;
873 needed = (dirlen + 1) * sizeof(WCHAR);
874 if (!(*path = p = HeapAlloc( GetProcessHeap(), 0, needed )))
876 status = STATUS_NO_MEMORY;
877 goto done;
879 memcpy( p, info->lpAssemblyManifestPath, dirlen * sizeof(WCHAR) );
880 *(p + dirlen) = 0;
881 goto done;
885 needed = (strlenW( DIR_Windows ) * sizeof(WCHAR) +
886 sizeof(winsxsW) + info->ulAssemblyDirectoryNameLength + 2*sizeof(WCHAR));
888 if (!(*path = p = HeapAlloc( GetProcessHeap(), 0, needed )))
890 status = STATUS_NO_MEMORY;
891 goto done;
893 strcpyW( p, DIR_Windows );
894 p += strlenW(p);
895 memcpy( p, winsxsW, sizeof(winsxsW) );
896 p += sizeof(winsxsW) / sizeof(WCHAR);
897 memcpy( p, info->lpAssemblyDirectoryName, info->ulAssemblyDirectoryNameLength );
898 p += info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
899 *p++ = '\\';
900 *p = 0;
901 done:
902 HeapFree( GetProcessHeap(), 0, info );
903 RtlReleaseActivationContext( data.hActCtx );
904 return status;
907 /***********************************************************************
908 * SearchPathW [KERNEL32.@]
910 * Searches for a specified file in the search path.
912 * PARAMS
913 * path [I] Path to search (NULL means default)
914 * name [I] Filename to search for.
915 * ext [I] File extension to append to file name. The first
916 * character must be a period. This parameter is
917 * specified only if the filename given does not
918 * contain an extension.
919 * buflen [I] size of buffer, in characters
920 * buffer [O] buffer for found filename
921 * lastpart [O] address of pointer to last used character in
922 * buffer (the final '\')
924 * RETURNS
925 * Success: length of string copied into buffer, not including
926 * terminating null character. If the filename found is
927 * longer than the length of the buffer, the length of the
928 * filename is returned.
929 * Failure: Zero
931 * NOTES
932 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
933 * (tested on NT 4.0)
935 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
936 LPWSTR buffer, LPWSTR *lastpart )
938 DWORD ret = 0;
940 if (!name || !name[0])
942 SetLastError(ERROR_INVALID_PARAMETER);
943 return 0;
946 /* If the name contains an explicit path, ignore the path */
948 if (contains_pathW(name))
950 /* try first without extension */
951 if (RtlDoesFileExists_U( name ))
952 return GetFullPathNameW( name, buflen, buffer, lastpart );
954 if (ext)
956 LPCWSTR p = strrchrW( name, '.' );
957 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
958 ext = NULL; /* Ignore the specified extension */
961 /* Allocate a buffer for the file name and extension */
962 if (ext)
964 LPWSTR tmp;
965 DWORD len = strlenW(name) + strlenW(ext);
967 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
969 SetLastError( ERROR_OUTOFMEMORY );
970 return 0;
972 strcpyW( tmp, name );
973 strcatW( tmp, ext );
974 if (RtlDoesFileExists_U( tmp ))
975 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
976 HeapFree( GetProcessHeap(), 0, tmp );
979 else if (path && path[0]) /* search in the specified path */
981 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
982 buffer, lastpart ) / sizeof(WCHAR);
984 else /* search in active context and default path */
986 WCHAR *dll_path = NULL, *search = NULL;
987 DWORD req_len, name_len;
989 req_len = name_len = strlenW(name);
991 if (strchrW( name, '.' )) ext = NULL;
992 if (ext)
994 DWORD ext_len = strlenW(ext);
996 req_len += ext_len;
997 name_len += ext_len;
999 search = HeapAlloc( GetProcessHeap(), 0, (name_len + ext_len + 1) * sizeof(WCHAR) );
1000 if (!search)
1002 SetLastError( ERROR_OUTOFMEMORY );
1003 return 0;
1005 strcpyW( search, name );
1006 strcatW( search, ext );
1007 name = search;
1009 /* now that we have combined name we don't need extension any more */
1012 /* When file is found with activation context no attempt is made
1013 to check if it's really exist, path is returned only basing on context info. */
1014 if (find_actctx_dllpath( name, &dll_path ) == STATUS_SUCCESS)
1016 DWORD path_len;
1018 path_len = strlenW(dll_path);
1019 req_len += path_len;
1021 if (lastpart) *lastpart = NULL;
1023 /* count null termination char too */
1024 if (req_len + 1 <= buflen)
1026 memcpy( buffer, dll_path, path_len * sizeof(WCHAR) );
1027 memcpy( &buffer[path_len], name, name_len * sizeof(WCHAR) );
1028 buffer[req_len] = 0;
1029 if (lastpart) *lastpart = buffer + path_len;
1030 ret = req_len;
1032 else
1033 ret = req_len + 1;
1035 HeapFree( GetProcessHeap(), 0, dll_path );
1036 HeapFree( GetProcessHeap(), 0, search );
1038 else
1040 if ((dll_path = MODULE_get_dll_load_path( NULL, get_path_safe_mode() )))
1042 ret = RtlDosSearchPath_U( dll_path, name, NULL, buflen * sizeof(WCHAR),
1043 buffer, lastpart ) / sizeof(WCHAR);
1044 HeapFree( GetProcessHeap(), 0, dll_path );
1045 HeapFree( GetProcessHeap(), 0, search );
1047 else
1049 SetLastError( ERROR_OUTOFMEMORY );
1050 return 0;
1055 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
1056 else TRACE( "found %s\n", debugstr_w(buffer) );
1057 return ret;
1061 /***********************************************************************
1062 * SearchPathA (KERNEL32.@)
1064 * See SearchPathW.
1066 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
1067 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
1069 WCHAR *pathW = NULL, *nameW, *extW = NULL;
1070 WCHAR bufferW[MAX_PATH];
1071 DWORD ret;
1073 if (!name)
1075 SetLastError(ERROR_INVALID_PARAMETER);
1076 return 0;
1079 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
1080 if (path && !(pathW = FILE_name_AtoW( path, TRUE ))) return 0;
1082 if (ext && !(extW = FILE_name_AtoW( ext, TRUE )))
1084 HeapFree( GetProcessHeap(), 0, pathW );
1085 return 0;
1088 ret = SearchPathW(pathW, nameW, extW, MAX_PATH, bufferW, NULL);
1090 HeapFree( GetProcessHeap(), 0, pathW );
1091 HeapFree( GetProcessHeap(), 0, extW );
1093 if (!ret) return 0;
1094 if (ret > MAX_PATH)
1096 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1097 return 0;
1099 ret = copy_filename_WtoA( bufferW, buffer, buflen );
1100 if (buflen > ret && lastpart)
1101 *lastpart = strrchr(buffer, '\\') + 1;
1102 return ret;
1105 static BOOL is_same_file(HANDLE h1, HANDLE h2)
1107 int fd1;
1108 BOOL ret = FALSE;
1109 if (wine_server_handle_to_fd(h1, 0, &fd1, NULL) == STATUS_SUCCESS)
1111 int fd2;
1112 if (wine_server_handle_to_fd(h2, 0, &fd2, NULL) == STATUS_SUCCESS)
1114 struct stat stat1, stat2;
1115 if (fstat(fd1, &stat1) == 0 && fstat(fd2, &stat2) == 0)
1116 ret = (stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino);
1117 wine_server_release_fd(h2, fd2);
1119 wine_server_release_fd(h1, fd1);
1121 return ret;
1124 /**************************************************************************
1125 * CopyFileW (KERNEL32.@)
1127 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
1129 return CopyFileExW( source, dest, NULL, NULL, NULL,
1130 fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0 );
1134 /**************************************************************************
1135 * CopyFileA (KERNEL32.@)
1137 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
1139 WCHAR *sourceW, *destW;
1140 BOOL ret;
1142 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1143 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1145 ret = CopyFileW( sourceW, destW, fail_if_exists );
1147 HeapFree( GetProcessHeap(), 0, destW );
1148 return ret;
1152 /**************************************************************************
1153 * CopyFileExW (KERNEL32.@)
1155 BOOL WINAPI CopyFileExW(LPCWSTR source, LPCWSTR dest,
1156 LPPROGRESS_ROUTINE progress, LPVOID param,
1157 LPBOOL cancel_ptr, DWORD flags)
1159 static const int buffer_size = 65536;
1160 HANDLE h1, h2;
1161 BY_HANDLE_FILE_INFORMATION info;
1162 DWORD count;
1163 BOOL ret = FALSE;
1164 char *buffer;
1166 if (!source || !dest)
1168 SetLastError(ERROR_INVALID_PARAMETER);
1169 return FALSE;
1171 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1173 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1174 return FALSE;
1177 TRACE("%s -> %s, %x\n", debugstr_w(source), debugstr_w(dest), flags);
1179 if ((h1 = CreateFileW(source, GENERIC_READ,
1180 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1181 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
1183 WARN("Unable to open source %s\n", debugstr_w(source));
1184 HeapFree( GetProcessHeap(), 0, buffer );
1185 return FALSE;
1188 if (!GetFileInformationByHandle( h1, &info ))
1190 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
1191 HeapFree( GetProcessHeap(), 0, buffer );
1192 CloseHandle( h1 );
1193 return FALSE;
1196 if (!(flags & COPY_FILE_FAIL_IF_EXISTS))
1198 BOOL same_file = FALSE;
1199 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1200 OPEN_EXISTING, 0, 0);
1201 if (h2 != INVALID_HANDLE_VALUE)
1203 same_file = is_same_file( h1, h2 );
1204 CloseHandle( h2 );
1206 if (same_file)
1208 HeapFree( GetProcessHeap(), 0, buffer );
1209 CloseHandle( h1 );
1210 SetLastError( ERROR_SHARING_VIOLATION );
1211 return FALSE;
1215 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1216 (flags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS,
1217 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
1219 WARN("Unable to open dest %s\n", debugstr_w(dest));
1220 HeapFree( GetProcessHeap(), 0, buffer );
1221 CloseHandle( h1 );
1222 return FALSE;
1225 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
1227 char *p = buffer;
1228 while (count != 0)
1230 DWORD res;
1231 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
1232 p += res;
1233 count -= res;
1236 ret = TRUE;
1237 done:
1238 /* Maintain the timestamp of source file to destination file */
1239 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
1240 HeapFree( GetProcessHeap(), 0, buffer );
1241 CloseHandle( h1 );
1242 CloseHandle( h2 );
1243 return ret;
1247 /**************************************************************************
1248 * CopyFileExA (KERNEL32.@)
1250 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
1251 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1252 LPBOOL cancelFlagPointer, DWORD copyFlags)
1254 WCHAR *sourceW, *destW;
1255 BOOL ret;
1257 /* can't use the TEB buffer since we may have a callback routine */
1258 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
1259 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
1261 HeapFree( GetProcessHeap(), 0, sourceW );
1262 return FALSE;
1264 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
1265 cancelFlagPointer, copyFlags);
1266 HeapFree( GetProcessHeap(), 0, sourceW );
1267 HeapFree( GetProcessHeap(), 0, destW );
1268 return ret;
1272 /**************************************************************************
1273 * MoveFileWithProgressW (KERNEL32.@)
1275 BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
1276 LPPROGRESS_ROUTINE fnProgress,
1277 LPVOID param, DWORD flag )
1279 FILE_BASIC_INFORMATION info;
1280 UNICODE_STRING nt_name;
1281 OBJECT_ATTRIBUTES attr;
1282 IO_STATUS_BLOCK io;
1283 NTSTATUS status;
1284 HANDLE source_handle = 0, dest_handle;
1285 ANSI_STRING source_unix, dest_unix;
1287 TRACE("(%s,%s,%p,%p,%04x)\n",
1288 debugstr_w(source), debugstr_w(dest), fnProgress, param, flag );
1290 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1291 return add_boot_rename_entry( source, dest, flag );
1293 if (!dest)
1294 return DeleteFileW( source );
1296 if (flag & MOVEFILE_WRITE_THROUGH)
1297 FIXME("MOVEFILE_WRITE_THROUGH unimplemented\n");
1299 /* check if we are allowed to rename the source */
1301 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
1303 SetLastError( ERROR_PATH_NOT_FOUND );
1304 return FALSE;
1306 source_unix.Buffer = NULL;
1307 dest_unix.Buffer = NULL;
1308 attr.Length = sizeof(attr);
1309 attr.RootDirectory = 0;
1310 attr.Attributes = OBJ_CASE_INSENSITIVE;
1311 attr.ObjectName = &nt_name;
1312 attr.SecurityDescriptor = NULL;
1313 attr.SecurityQualityOfService = NULL;
1315 status = NtOpenFile( &source_handle, SYNCHRONIZE, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1316 if (status == STATUS_SUCCESS)
1317 status = wine_nt_to_unix_file_name( &nt_name, &source_unix, FILE_OPEN, FALSE );
1318 RtlFreeUnicodeString( &nt_name );
1319 if (status != STATUS_SUCCESS)
1321 SetLastError( RtlNtStatusToDosError(status) );
1322 goto error;
1324 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
1325 if (status != STATUS_SUCCESS)
1327 SetLastError( RtlNtStatusToDosError(status) );
1328 goto error;
1331 /* we must have write access to the destination, and it must */
1332 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1334 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
1336 SetLastError( ERROR_PATH_NOT_FOUND );
1337 goto error;
1339 status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &attr, &io, 0,
1340 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1341 if (status == STATUS_SUCCESS) /* destination exists */
1343 NtClose( dest_handle );
1344 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1346 SetLastError( ERROR_ALREADY_EXISTS );
1347 RtlFreeUnicodeString( &nt_name );
1348 goto error;
1350 else if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* cannot replace directory */
1352 SetLastError( ERROR_ACCESS_DENIED );
1353 goto error;
1356 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1358 SetLastError( RtlNtStatusToDosError(status) );
1359 RtlFreeUnicodeString( &nt_name );
1360 goto error;
1363 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1364 RtlFreeUnicodeString( &nt_name );
1365 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1367 SetLastError( RtlNtStatusToDosError(status) );
1368 goto error;
1371 /* now perform the rename */
1373 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1375 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1377 NtClose( source_handle );
1378 RtlFreeAnsiString( &source_unix );
1379 RtlFreeAnsiString( &dest_unix );
1380 if (!CopyFileExW( source, dest, fnProgress,
1381 param, NULL, COPY_FILE_FAIL_IF_EXISTS ))
1382 return FALSE;
1383 return DeleteFileW( source );
1385 FILE_SetDosError();
1386 /* if we created the destination, remove it */
1387 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1388 goto error;
1391 /* fixup executable permissions */
1393 if (is_executable( source ) != is_executable( dest ))
1395 struct stat fstat;
1396 if (stat( dest_unix.Buffer, &fstat ) != -1)
1398 if (is_executable( dest ))
1399 /* set executable bit where read bit is set */
1400 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1401 else
1402 fstat.st_mode &= ~0111;
1403 chmod( dest_unix.Buffer, fstat.st_mode );
1407 NtClose( source_handle );
1408 RtlFreeAnsiString( &source_unix );
1409 RtlFreeAnsiString( &dest_unix );
1410 return TRUE;
1412 error:
1413 if (source_handle) NtClose( source_handle );
1414 RtlFreeAnsiString( &source_unix );
1415 RtlFreeAnsiString( &dest_unix );
1416 return FALSE;
1419 /**************************************************************************
1420 * MoveFileWithProgressA (KERNEL32.@)
1422 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
1423 LPPROGRESS_ROUTINE fnProgress,
1424 LPVOID param, DWORD flag )
1426 WCHAR *sourceW, *destW;
1427 BOOL ret;
1429 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1430 if (dest)
1432 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1434 else
1435 destW = NULL;
1437 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
1438 HeapFree( GetProcessHeap(), 0, destW );
1439 return ret;
1442 /**************************************************************************
1443 * MoveFileExW (KERNEL32.@)
1445 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
1447 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
1450 /**************************************************************************
1451 * MoveFileExA (KERNEL32.@)
1453 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1455 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
1459 /**************************************************************************
1460 * MoveFileW (KERNEL32.@)
1462 * Move file or directory
1464 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1466 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1470 /**************************************************************************
1471 * MoveFileA (KERNEL32.@)
1473 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1475 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1479 /*************************************************************************
1480 * CreateHardLinkW (KERNEL32.@)
1482 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
1483 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1485 NTSTATUS status;
1486 UNICODE_STRING ntDest, ntSource;
1487 ANSI_STRING unixDest, unixSource;
1488 BOOL ret = FALSE;
1490 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
1491 debugstr_w(lpExistingFileName), lpSecurityAttributes);
1493 ntDest.Buffer = ntSource.Buffer = NULL;
1494 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
1495 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
1497 SetLastError( ERROR_PATH_NOT_FOUND );
1498 goto err;
1501 unixSource.Buffer = unixDest.Buffer = NULL;
1502 status = wine_nt_to_unix_file_name( &ntSource, &unixSource, FILE_OPEN, FALSE );
1503 if (!status)
1505 status = wine_nt_to_unix_file_name( &ntDest, &unixDest, FILE_CREATE, FALSE );
1506 if (!status) /* destination must not exist */
1508 status = STATUS_OBJECT_NAME_EXISTS;
1509 } else if (status == STATUS_NO_SUCH_FILE)
1511 status = STATUS_SUCCESS;
1515 if (status)
1516 SetLastError( RtlNtStatusToDosError(status) );
1517 else if (!link( unixSource.Buffer, unixDest.Buffer ))
1519 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest.Buffer ),
1520 debugstr_a( unixSource.Buffer ));
1521 ret = TRUE;
1523 else
1524 FILE_SetDosError();
1526 RtlFreeAnsiString( &unixSource );
1527 RtlFreeAnsiString( &unixDest );
1529 err:
1530 RtlFreeUnicodeString( &ntSource );
1531 RtlFreeUnicodeString( &ntDest );
1532 return ret;
1536 /*************************************************************************
1537 * CreateHardLinkA (KERNEL32.@)
1539 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
1540 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1542 WCHAR *sourceW, *destW;
1543 BOOL res;
1545 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
1547 return FALSE;
1549 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
1551 HeapFree( GetProcessHeap(), 0, sourceW );
1552 return FALSE;
1555 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
1557 HeapFree( GetProcessHeap(), 0, sourceW );
1558 HeapFree( GetProcessHeap(), 0, destW );
1560 return res;
1564 /***********************************************************************
1565 * CreateDirectoryW (KERNEL32.@)
1566 * RETURNS:
1567 * TRUE : success
1568 * FALSE : failure
1569 * ERROR_DISK_FULL: on full disk
1570 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1571 * ERROR_ACCESS_DENIED: on permission problems
1572 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1574 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1576 OBJECT_ATTRIBUTES attr;
1577 UNICODE_STRING nt_name;
1578 IO_STATUS_BLOCK io;
1579 NTSTATUS status;
1580 HANDLE handle;
1581 BOOL ret = FALSE;
1583 TRACE( "%s\n", debugstr_w(path) );
1585 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1587 SetLastError( ERROR_PATH_NOT_FOUND );
1588 return FALSE;
1590 attr.Length = sizeof(attr);
1591 attr.RootDirectory = 0;
1592 attr.Attributes = OBJ_CASE_INSENSITIVE;
1593 attr.ObjectName = &nt_name;
1594 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1595 attr.SecurityQualityOfService = NULL;
1597 status = NtCreateFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, NULL,
1598 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1599 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1601 if (status == STATUS_SUCCESS)
1603 NtClose( handle );
1604 ret = TRUE;
1606 else SetLastError( RtlNtStatusToDosError(status) );
1608 RtlFreeUnicodeString( &nt_name );
1609 return ret;
1613 /***********************************************************************
1614 * CreateDirectoryA (KERNEL32.@)
1616 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1618 WCHAR *pathW;
1620 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1621 return CreateDirectoryW( pathW, sa );
1625 /***********************************************************************
1626 * CreateDirectoryExA (KERNEL32.@)
1628 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1630 WCHAR *pathW, *templateW = NULL;
1631 BOOL ret;
1633 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1634 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
1636 ret = CreateDirectoryExW( templateW, pathW, sa );
1637 HeapFree( GetProcessHeap(), 0, templateW );
1638 return ret;
1642 /***********************************************************************
1643 * CreateDirectoryExW (KERNEL32.@)
1645 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1647 return CreateDirectoryW( path, sa );
1651 /***********************************************************************
1652 * RemoveDirectoryW (KERNEL32.@)
1654 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1656 OBJECT_ATTRIBUTES attr;
1657 UNICODE_STRING nt_name;
1658 ANSI_STRING unix_name;
1659 IO_STATUS_BLOCK io;
1660 NTSTATUS status;
1661 HANDLE handle;
1662 BOOL ret = FALSE;
1664 TRACE( "%s\n", debugstr_w(path) );
1666 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1668 SetLastError( ERROR_PATH_NOT_FOUND );
1669 return FALSE;
1671 attr.Length = sizeof(attr);
1672 attr.RootDirectory = 0;
1673 attr.Attributes = OBJ_CASE_INSENSITIVE;
1674 attr.ObjectName = &nt_name;
1675 attr.SecurityDescriptor = NULL;
1676 attr.SecurityQualityOfService = NULL;
1678 status = NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
1679 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1680 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1681 if (status != STATUS_SUCCESS)
1683 SetLastError( RtlNtStatusToDosError(status) );
1684 RtlFreeUnicodeString( &nt_name );
1685 return FALSE;
1688 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1689 RtlFreeUnicodeString( &nt_name );
1690 if (status != STATUS_SUCCESS)
1692 SetLastError( RtlNtStatusToDosError(status) );
1693 NtClose( handle );
1694 return FALSE;
1697 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1698 RtlFreeAnsiString( &unix_name );
1699 NtClose( handle );
1700 return ret;
1704 /***********************************************************************
1705 * RemoveDirectoryA (KERNEL32.@)
1707 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1709 WCHAR *pathW;
1711 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1712 return RemoveDirectoryW( pathW );
1716 /***********************************************************************
1717 * GetCurrentDirectoryW (KERNEL32.@)
1719 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1721 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1725 /***********************************************************************
1726 * GetCurrentDirectoryA (KERNEL32.@)
1728 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1730 WCHAR bufferW[MAX_PATH];
1731 DWORD ret;
1733 if (buflen && buf && ((ULONG_PTR)buf >> 16) == 0)
1735 /* Win9x catches access violations here, returning zero.
1736 * This behaviour resulted in some people not noticing
1737 * that they got the argument order wrong. So let's be
1738 * nice and fail gracefully if buf is invalid and looks
1739 * more like a buflen. */
1740 SetLastError(ERROR_INVALID_PARAMETER);
1741 return 0;
1744 ret = RtlGetCurrentDirectory_U( sizeof(bufferW), bufferW );
1745 if (!ret) return 0;
1746 if (ret > sizeof(bufferW))
1748 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1749 return 0;
1751 return copy_filename_WtoA( bufferW, buf, buflen );
1755 /***********************************************************************
1756 * SetCurrentDirectoryW (KERNEL32.@)
1758 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1760 UNICODE_STRING dirW;
1761 NTSTATUS status;
1763 RtlInitUnicodeString( &dirW, dir );
1764 status = RtlSetCurrentDirectory_U( &dirW );
1765 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1766 return !status;
1770 /***********************************************************************
1771 * SetCurrentDirectoryA (KERNEL32.@)
1773 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1775 WCHAR *dirW;
1776 UNICODE_STRING strW;
1777 NTSTATUS status;
1779 if (!(dirW = FILE_name_AtoW( dir, FALSE ))) return FALSE;
1780 RtlInitUnicodeString( &strW, dirW );
1781 status = RtlSetCurrentDirectory_U( &strW );
1782 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1783 return !status;
1787 /***********************************************************************
1788 * GetWindowsDirectoryW (KERNEL32.@)
1790 * See comment for GetWindowsDirectoryA.
1792 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
1794 UINT len = strlenW( DIR_Windows ) + 1;
1795 if (path && count >= len)
1797 strcpyW( path, DIR_Windows );
1798 len--;
1800 return len;
1804 /***********************************************************************
1805 * GetWindowsDirectoryA (KERNEL32.@)
1807 * Return value:
1808 * If buffer is large enough to hold full path and terminating '\0' character
1809 * function copies path to buffer and returns length of the path without '\0'.
1810 * Otherwise function returns required size including '\0' character and
1811 * does not touch the buffer.
1813 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
1815 return copy_filename_WtoA( DIR_Windows, path, count );
1819 /***********************************************************************
1820 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1822 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
1824 return GetWindowsDirectoryA( path, count );
1828 /***********************************************************************
1829 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1831 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
1833 return GetWindowsDirectoryW( path, count );
1837 /***********************************************************************
1838 * GetSystemDirectoryW (KERNEL32.@)
1840 * See comment for GetWindowsDirectoryA.
1842 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
1844 UINT len = strlenW( DIR_System ) + 1;
1845 if (path && count >= len)
1847 strcpyW( path, DIR_System );
1848 len--;
1850 return len;
1854 /***********************************************************************
1855 * GetSystemDirectoryA (KERNEL32.@)
1857 * See comment for GetWindowsDirectoryA.
1859 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
1861 return copy_filename_WtoA( DIR_System, path, count );
1865 /***********************************************************************
1866 * GetSystemWow64DirectoryW (KERNEL32.@)
1868 * As seen on MSDN
1869 * - On Win32 we should return ERROR_CALL_NOT_IMPLEMENTED
1870 * - On Win64 we should return the SysWow64 (system64) directory
1872 UINT WINAPI GetSystemWow64DirectoryW( LPWSTR path, UINT count )
1874 UINT len;
1876 if (!DIR_SysWow64)
1878 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1879 return 0;
1881 len = strlenW( DIR_SysWow64 ) + 1;
1882 if (path && count >= len)
1884 strcpyW( path, DIR_SysWow64 );
1885 len--;
1887 return len;
1891 /***********************************************************************
1892 * GetSystemWow64DirectoryA (KERNEL32.@)
1894 * See comment for GetWindowsWow64DirectoryW.
1896 UINT WINAPI GetSystemWow64DirectoryA( LPSTR path, UINT count )
1898 if (!DIR_SysWow64)
1900 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1901 return 0;
1903 return copy_filename_WtoA( DIR_SysWow64, path, count );
1907 /***********************************************************************
1908 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1910 BOOLEAN WINAPI Wow64EnableWow64FsRedirection( BOOLEAN enable )
1912 NTSTATUS status = RtlWow64EnableFsRedirection( enable );
1913 if (status) SetLastError( RtlNtStatusToDosError(status) );
1914 return !status;
1918 /***********************************************************************
1919 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1921 BOOL WINAPI Wow64DisableWow64FsRedirection( PVOID *old_value )
1923 NTSTATUS status = RtlWow64EnableFsRedirectionEx( TRUE, (ULONG *)old_value );
1924 if (status) SetLastError( RtlNtStatusToDosError(status) );
1925 return !status;
1929 /***********************************************************************
1930 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1932 BOOL WINAPI Wow64RevertWow64FsRedirection( PVOID old_value )
1934 NTSTATUS status = RtlWow64EnableFsRedirection( !old_value );
1935 if (status) SetLastError( RtlNtStatusToDosError(status) );
1936 return !status;
1940 /***********************************************************************
1941 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1943 BOOL WINAPI NeedCurrentDirectoryForExePathW( LPCWSTR name )
1945 static const WCHAR env_name[] = {'N','o','D','e','f','a','u','l','t',
1946 'C','u','r','r','e','n','t',
1947 'D','i','r','e','c','t','o','r','y',
1948 'I','n','E','x','e','P','a','t','h',0};
1949 WCHAR env_val;
1951 /* MSDN mentions some 'registry location'. We do not use registry. */
1952 FIXME("(%s): partial stub\n", debugstr_w(name));
1954 if (strchrW(name, '\\'))
1955 return TRUE;
1957 /* Check the existence of the variable, not value */
1958 if (!GetEnvironmentVariableW( env_name, &env_val, 1 ))
1959 return TRUE;
1961 return FALSE;
1965 /***********************************************************************
1966 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
1968 BOOL WINAPI NeedCurrentDirectoryForExePathA( LPCSTR name )
1970 WCHAR *nameW;
1972 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return TRUE;
1973 return NeedCurrentDirectoryForExePathW( nameW );
1977 /***********************************************************************
1978 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1980 * Return the full Unix file name for a given path.
1981 * Returned buffer must be freed by caller.
1983 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
1985 UNICODE_STRING nt_name;
1986 ANSI_STRING unix_name;
1987 NTSTATUS status;
1989 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
1990 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
1991 RtlFreeUnicodeString( &nt_name );
1992 if (status && status != STATUS_NO_SUCH_FILE)
1994 SetLastError( RtlNtStatusToDosError( status ) );
1995 return NULL;
1997 return unix_name.Buffer;
2001 /***********************************************************************
2002 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
2004 * Return the full DOS file name for a given Unix path.
2005 * Returned buffer must be freed by caller.
2007 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
2009 UNICODE_STRING nt_name;
2010 ANSI_STRING unix_name;
2011 NTSTATUS status;
2012 DWORD len;
2014 RtlInitAnsiString( &unix_name, str );
2015 status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
2016 if (status)
2018 SetLastError( RtlNtStatusToDosError( status ) );
2019 return NULL;
2021 if (nt_name.Buffer[5] == ':')
2023 /* get rid of the \??\ prefix */
2024 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
2025 len = nt_name.Length - 4 * sizeof(WCHAR);
2026 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
2027 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
2029 else
2030 nt_name.Buffer[1] = '\\';
2031 return nt_name.Buffer;
2034 /*************************************************************************
2035 * CreateSymbolicLinkW (KERNEL32.@)
2037 BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags)
2039 FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags);
2040 return TRUE;
2043 /*************************************************************************
2044 * CreateSymbolicLinkA (KERNEL32.@)
2046 BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags)
2048 FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags);
2049 return TRUE;
2052 /*************************************************************************
2053 * CreateHardLinkTransactedA (KERNEL32.@)
2055 BOOL WINAPI CreateHardLinkTransactedA(LPCSTR link, LPCSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2057 FIXME("(%s %s %p %p): stub\n", debugstr_a(link), debugstr_a(target), sa, transaction);
2058 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2059 return FALSE;
2062 /*************************************************************************
2063 * CreateHardLinkTransactedW (KERNEL32.@)
2065 BOOL WINAPI CreateHardLinkTransactedW(LPCWSTR link, LPCWSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2067 FIXME("(%s %s %p %p): stub\n", debugstr_w(link), debugstr_w(target), sa, transaction);
2068 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2069 return FALSE;
2072 /*************************************************************************
2073 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
2075 BOOL WINAPI CheckNameLegalDOS8Dot3A(const char *name, char *oemname, DWORD oemname_len,
2076 BOOL *contains_spaces, BOOL *is_legal)
2078 WCHAR *nameW;
2080 TRACE("(%s %p %u %p %p)\n", name, oemname,
2081 oemname_len, contains_spaces, is_legal);
2083 if (!name || !is_legal)
2084 return FALSE;
2086 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2088 return CheckNameLegalDOS8Dot3W( nameW, oemname, oemname_len, contains_spaces, is_legal );
2091 /*************************************************************************
2092 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
2094 BOOL WINAPI CheckNameLegalDOS8Dot3W(const WCHAR *name, char *oemname, DWORD oemname_len,
2095 BOOL *contains_spaces_ret, BOOL *is_legal)
2097 OEM_STRING oem_str;
2098 UNICODE_STRING nameW;
2099 BOOLEAN contains_spaces;
2101 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name), oemname,
2102 oemname_len, contains_spaces_ret, is_legal);
2104 if (!name || !is_legal)
2105 return FALSE;
2107 RtlInitUnicodeString( &nameW, name );
2109 if (oemname) {
2110 oem_str.Length = oemname_len;
2111 oem_str.MaximumLength = oemname_len;
2112 oem_str.Buffer = oemname;
2115 *is_legal = RtlIsNameLegalDOS8Dot3( &nameW, oemname ? &oem_str : NULL, &contains_spaces );
2116 if (contains_spaces_ret) *contains_spaces_ret = contains_spaces;
2118 return TRUE;
2121 /*************************************************************************
2122 * SetSearchPathMode (KERNEL32.@)
2124 BOOL WINAPI SetSearchPathMode( DWORD flags )
2126 int val;
2128 switch (flags)
2130 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE:
2131 val = 1;
2132 break;
2133 case BASE_SEARCH_PATH_DISABLE_SAFE_SEARCHMODE:
2134 val = 0;
2135 break;
2136 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT:
2137 InterlockedExchange( &path_safe_mode, 2 );
2138 return TRUE;
2139 default:
2140 SetLastError( ERROR_INVALID_PARAMETER );
2141 return FALSE;
2144 for (;;)
2146 int prev = path_safe_mode;
2147 if (prev == 2) break; /* permanently set */
2148 if (InterlockedCompareExchange( &path_safe_mode, val, prev ) == prev) return TRUE;
2150 SetLastError( ERROR_ACCESS_DENIED );
2151 return FALSE;