ntdll: Translate signal to trap when trap code is 0 on ARM.
[wine.git] / dlls / kernel32 / path.c
blobcf1c768970473584d8208437e1cdaca7dc82da4d
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 static const WCHAR wildcardsW[] = {'*','?',0};
50 /* check if a file name is for an executable file (.exe or .com) */
51 static inline BOOL is_executable( const WCHAR *name )
53 static const WCHAR exeW[] = {'.','e','x','e',0};
54 static const WCHAR comW[] = {'.','c','o','m',0};
55 int len = strlenW(name);
57 if (len < 4) return FALSE;
58 return (!strcmpiW( name + len - 4, exeW ) || !strcmpiW( name + len - 4, comW ));
61 /***********************************************************************
62 * copy_filename_WtoA
64 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
66 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
68 UNICODE_STRING strW;
69 DWORD ret;
70 BOOL is_ansi = AreFileApisANSI();
72 RtlInitUnicodeString( &strW, nameW );
74 ret = is_ansi ? RtlUnicodeStringToAnsiSize(&strW) : RtlUnicodeStringToOemSize(&strW);
75 if (buffer && ret <= len)
77 ANSI_STRING str;
79 str.Buffer = buffer;
80 str.MaximumLength = min( len, UNICODE_STRING_MAX_CHARS );
81 if (is_ansi)
82 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
83 else
84 RtlUnicodeStringToOemString( &str, &strW, FALSE );
85 ret = str.Length; /* length without terminating 0 */
87 return ret;
90 /***********************************************************************
91 * add_boot_rename_entry
93 * Adds an entry to the registry that is loaded when windows boots and
94 * checks if there are some files to be removed or renamed/moved.
95 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
96 * non-NULL then the file is moved, otherwise it is deleted. The
97 * entry of the registry key is always appended with two zero
98 * terminated strings. If <fn2> is NULL then the second entry is
99 * simply a single 0-byte. Otherwise the second filename goes
100 * there. The entries are prepended with \??\ before the path and the
101 * second filename gets also a '!' as the first character if
102 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
103 * 0-byte follows to indicate the end of the strings.
104 * i.e.:
105 * \??\D:\test\file1[0]
106 * !\??\D:\test\file1_renamed[0]
107 * \??\D:\Test|delete[0]
108 * [0] <- file is to be deleted, second string empty
109 * \??\D:\test\file2[0]
110 * !\??\D:\test\file2_renamed[0]
111 * [0] <- indicates end of strings
113 * or:
114 * \??\D:\test\file1[0]
115 * !\??\D:\test\file1_renamed[0]
116 * \??\D:\Test|delete[0]
117 * [0] <- file is to be deleted, second string empty
118 * [0] <- indicates end of strings
121 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
123 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
124 'F','i','l','e','R','e','n','a','m','e',
125 'O','p','e','r','a','t','i','o','n','s',0};
126 static const WCHAR SessionW[] = {'\\','R','e','g','i','s','t','r','y','\\',
127 'M','a','c','h','i','n','e','\\',
128 'S','y','s','t','e','m','\\',
129 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
130 'C','o','n','t','r','o','l','\\',
131 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
132 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
134 OBJECT_ATTRIBUTES attr;
135 UNICODE_STRING nameW, source_name, dest_name;
136 KEY_VALUE_PARTIAL_INFORMATION *info;
137 BOOL rc = FALSE;
138 HANDLE Reboot = 0;
139 DWORD len1, len2;
140 DWORD DataSize = 0;
141 BYTE *Buffer = NULL;
142 WCHAR *p;
144 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
146 SetLastError( ERROR_PATH_NOT_FOUND );
147 return FALSE;
149 dest_name.Buffer = NULL;
150 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
152 RtlFreeUnicodeString( &source_name );
153 SetLastError( ERROR_PATH_NOT_FOUND );
154 return FALSE;
157 attr.Length = sizeof(attr);
158 attr.RootDirectory = 0;
159 attr.ObjectName = &nameW;
160 attr.Attributes = 0;
161 attr.SecurityDescriptor = NULL;
162 attr.SecurityQualityOfService = NULL;
163 RtlInitUnicodeString( &nameW, SessionW );
165 if (NtCreateKey( &Reboot, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
167 WARN("Error creating key for reboot management [%s]\n",
168 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
169 RtlFreeUnicodeString( &source_name );
170 RtlFreeUnicodeString( &dest_name );
171 return FALSE;
174 len1 = source_name.Length + sizeof(WCHAR);
175 if (dest)
177 len2 = dest_name.Length + sizeof(WCHAR);
178 if (flags & MOVEFILE_REPLACE_EXISTING)
179 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
181 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
183 RtlInitUnicodeString( &nameW, ValueName );
185 /* First we check if the key exists and if so how many bytes it already contains. */
186 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
187 NULL, 0, &DataSize ) == STATUS_BUFFER_TOO_SMALL)
189 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
190 goto Quit;
191 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
192 Buffer, DataSize, &DataSize )) goto Quit;
193 info = (KEY_VALUE_PARTIAL_INFORMATION *)Buffer;
194 if (info->Type != REG_MULTI_SZ) goto Quit;
195 if (DataSize > sizeof(info)) DataSize -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
197 else
199 DataSize = info_size;
200 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
201 goto Quit;
204 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
205 DataSize += len1;
206 p = (WCHAR *)(Buffer + DataSize);
207 if (dest)
209 if (flags & MOVEFILE_REPLACE_EXISTING)
210 *p++ = '!';
211 memcpy( p, dest_name.Buffer, len2 );
212 DataSize += len2;
214 else
216 *p = 0;
217 DataSize += sizeof(WCHAR);
220 /* add final null */
221 p = (WCHAR *)(Buffer + DataSize);
222 *p = 0;
223 DataSize += sizeof(WCHAR);
225 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
227 Quit:
228 RtlFreeUnicodeString( &source_name );
229 RtlFreeUnicodeString( &dest_name );
230 if (Reboot) NtClose(Reboot);
231 HeapFree( GetProcessHeap(), 0, Buffer );
232 return(rc);
236 /***********************************************************************
237 * GetFullPathNameW (KERNEL32.@)
238 * NOTES
239 * if the path closed with '\', *lastpart is 0
241 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
242 LPWSTR *lastpart )
244 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
247 /***********************************************************************
248 * GetFullPathNameA (KERNEL32.@)
249 * NOTES
250 * if the path closed with '\', *lastpart is 0
252 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
253 LPSTR *lastpart )
255 WCHAR *nameW;
256 WCHAR bufferW[MAX_PATH], *lastpartW = NULL;
257 DWORD ret;
259 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
261 ret = GetFullPathNameW( nameW, MAX_PATH, bufferW, &lastpartW);
263 if (!ret) return 0;
264 if (ret > MAX_PATH)
266 SetLastError(ERROR_FILENAME_EXCED_RANGE);
267 return 0;
269 ret = copy_filename_WtoA( bufferW, buffer, len );
270 if (ret < len && lastpart)
272 if (lastpartW)
273 *lastpart = buffer + FILE_name_WtoA( bufferW, lastpartW - bufferW, NULL, 0 );
274 else
275 *lastpart = NULL;
277 return ret;
281 /***********************************************************************
282 * GetLongPathNameW (KERNEL32.@)
284 * NOTES
285 * observed (Win2000):
286 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
287 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
289 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
291 WCHAR tmplongpath[MAX_PATHNAME_LEN];
292 LPCWSTR p;
293 DWORD sp = 0, lp = 0;
294 DWORD tmplen;
295 BOOL unixabsolute;
296 WIN32_FIND_DATAW wfd;
297 HANDLE goit;
298 BOOL is_legal_8dot3;
300 TRACE("%s,%p,%u\n", debugstr_w(shortpath), longpath, longlen);
302 if (!shortpath)
304 SetLastError(ERROR_INVALID_PARAMETER);
305 return 0;
307 if (!shortpath[0])
309 SetLastError(ERROR_PATH_NOT_FOUND);
310 return 0;
313 if (shortpath[0] == '\\' && shortpath[1] == '\\')
315 FIXME("UNC pathname %s\n", debugstr_w(shortpath));
317 tmplen = strlenW(shortpath);
318 if (tmplen < longlen)
320 if (longpath != shortpath) strcpyW( longpath, shortpath );
321 return tmplen;
323 return tmplen + 1;
326 unixabsolute = (shortpath[0] == '/');
328 /* check for drive letter */
329 if (!unixabsolute && shortpath[1] == ':' )
331 tmplongpath[0] = shortpath[0];
332 tmplongpath[1] = ':';
333 lp = sp = 2;
336 if (strpbrkW(shortpath + sp, wildcardsW))
338 SetLastError(ERROR_INVALID_NAME);
339 return 0;
342 while (shortpath[sp])
344 /* check for path delimiters and reproduce them */
345 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
347 tmplongpath[lp++] = shortpath[sp++];
348 tmplongpath[lp] = 0; /* terminate string */
349 continue;
352 p = shortpath + sp;
353 for (; *p && *p != '/' && *p != '\\'; p++);
354 tmplen = p - (shortpath + sp);
355 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
357 if (tmplongpath[lp] == '.')
359 if (tmplen == 1 || (tmplen == 2 && tmplongpath[lp + 1] == '.'))
361 lp += tmplen;
362 sp += tmplen;
363 continue;
367 /* Check if the file exists */
368 goit = FindFirstFileW(tmplongpath, &wfd);
369 if (goit == INVALID_HANDLE_VALUE)
371 TRACE("not found %s!\n", debugstr_w(tmplongpath));
372 SetLastError ( ERROR_FILE_NOT_FOUND );
373 return 0;
375 FindClose(goit);
377 is_legal_8dot3 = FALSE;
378 CheckNameLegalDOS8Dot3W(tmplongpath + lp, NULL, 0, NULL, &is_legal_8dot3);
379 /* Use the existing file name if it's a short name */
380 if (is_legal_8dot3)
381 strcpyW(tmplongpath + lp, wfd.cFileName);
382 lp += strlenW(tmplongpath + lp);
383 sp += tmplen;
385 tmplen = strlenW(shortpath) - 1;
386 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
387 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
388 tmplongpath[lp++] = shortpath[tmplen];
389 tmplongpath[lp] = 0;
391 tmplen = strlenW(tmplongpath) + 1;
392 if (tmplen <= longlen)
394 strcpyW(longpath, tmplongpath);
395 TRACE("returning %s\n", debugstr_w(longpath));
396 tmplen--; /* length without 0 */
399 return tmplen;
402 /***********************************************************************
403 * GetLongPathNameA (KERNEL32.@)
405 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
407 WCHAR *shortpathW;
408 WCHAR longpathW[MAX_PATH];
409 DWORD ret;
411 TRACE("%s\n", debugstr_a(shortpath));
413 if (!(shortpathW = FILE_name_AtoW( shortpath, FALSE ))) return 0;
415 ret = GetLongPathNameW(shortpathW, longpathW, MAX_PATH);
417 if (!ret) return 0;
418 if (ret > MAX_PATH)
420 SetLastError(ERROR_FILENAME_EXCED_RANGE);
421 return 0;
423 return copy_filename_WtoA( longpathW, longpath, longlen );
427 /***********************************************************************
428 * GetShortPathNameW (KERNEL32.@)
430 * NOTES
431 * observed:
432 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
433 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
435 * more observations ( with NT 3.51 (WinDD) ):
436 * longpath <= 8.3 -> just copy longpath to shortpath
437 * longpath > 8.3 ->
438 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
439 * b) file does exist -> set the short filename.
440 * - trailing slashes are reproduced in the short name, even if the
441 * file is not a directory
442 * - the absolute/relative path of the short name is reproduced like found
443 * in the long name
444 * - longpath and shortpath may have the same address
445 * Peter Ganten, 1999
447 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
449 WCHAR *tmpshortpath;
450 LPCWSTR p;
451 DWORD sp = 0, lp = 0;
452 DWORD tmplen, buf_len;
453 WIN32_FIND_DATAW wfd;
454 HANDLE goit;
456 TRACE("%s,%p,%u\n", debugstr_w(longpath), shortpath, shortlen);
458 if (!longpath)
460 SetLastError(ERROR_INVALID_PARAMETER);
461 return 0;
463 if (!longpath[0])
465 SetLastError(ERROR_BAD_PATHNAME);
466 return 0;
469 /* code below only removes characters from string, never adds, so this is
470 * the largest buffer that tmpshortpath will need to have */
471 buf_len = strlenW(longpath) + 1;
472 tmpshortpath = HeapAlloc(GetProcessHeap(), 0, buf_len * sizeof(WCHAR));
473 if (!tmpshortpath)
475 SetLastError(ERROR_OUTOFMEMORY);
476 return 0;
479 if (longpath[0] == '\\' && longpath[1] == '\\' && longpath[2] == '?' && longpath[3] == '\\')
481 memcpy(tmpshortpath, longpath, 4 * sizeof(WCHAR));
482 sp = lp = 4;
485 if (strpbrkW(longpath + lp, wildcardsW))
487 HeapFree(GetProcessHeap(), 0, tmpshortpath);
488 SetLastError(ERROR_INVALID_NAME);
489 return 0;
492 /* check for drive letter */
493 if (longpath[lp] != '/' && longpath[lp + 1] == ':' )
495 tmpshortpath[sp] = longpath[lp];
496 tmpshortpath[sp + 1] = ':';
497 sp += 2;
498 lp += 2;
501 while (longpath[lp])
503 /* check for path delimiters and reproduce them */
504 if (longpath[lp] == '\\' || longpath[lp] == '/')
506 tmpshortpath[sp++] = longpath[lp++];
507 tmpshortpath[sp] = 0; /* terminate string */
508 continue;
511 p = longpath + lp;
512 for (; *p && *p != '/' && *p != '\\'; p++);
513 tmplen = p - (longpath + lp);
514 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
516 if (tmpshortpath[sp] == '.')
518 if (tmplen == 1 || (tmplen == 2 && tmpshortpath[sp + 1] == '.'))
520 sp += tmplen;
521 lp += tmplen;
522 continue;
526 /* Check if the file exists and use the existing short file name */
527 goit = FindFirstFileW(tmpshortpath, &wfd);
528 if (goit == INVALID_HANDLE_VALUE) goto notfound;
529 FindClose(goit);
531 /* In rare cases (like "a.abcd") short path may be longer than original path.
532 * Make sure we have enough space in temp buffer. */
533 if (wfd.cAlternateFileName[0] && tmplen < strlenW(wfd.cAlternateFileName))
535 WCHAR *new_buf;
536 buf_len += strlenW(wfd.cAlternateFileName) - tmplen;
537 new_buf = HeapReAlloc(GetProcessHeap(), 0, tmpshortpath, buf_len * sizeof(WCHAR));
538 if(!new_buf)
540 HeapFree(GetProcessHeap(), 0, tmpshortpath);
541 SetLastError(ERROR_OUTOFMEMORY);
542 return 0;
544 tmpshortpath = new_buf;
547 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName[0] ? wfd.cAlternateFileName : wfd.cFileName);
548 sp += strlenW(tmpshortpath + sp);
549 lp += tmplen;
551 tmpshortpath[sp] = 0;
553 tmplen = strlenW(tmpshortpath) + 1;
554 if (tmplen <= shortlen)
556 strcpyW(shortpath, tmpshortpath);
557 TRACE("returning %s\n", debugstr_w(shortpath));
558 tmplen--; /* length without 0 */
561 HeapFree(GetProcessHeap(), 0, tmpshortpath);
562 return tmplen;
564 notfound:
565 HeapFree(GetProcessHeap(), 0, tmpshortpath);
566 TRACE("not found!\n" );
567 SetLastError ( ERROR_FILE_NOT_FOUND );
568 return 0;
571 /***********************************************************************
572 * GetShortPathNameA (KERNEL32.@)
574 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
576 WCHAR *longpathW;
577 WCHAR shortpathW[MAX_PATH];
578 DWORD ret;
580 TRACE("%s\n", debugstr_a(longpath));
582 if (!(longpathW = FILE_name_AtoW( longpath, FALSE ))) return 0;
584 ret = GetShortPathNameW(longpathW, shortpathW, MAX_PATH);
586 if (!ret) return 0;
587 if (ret > MAX_PATH)
589 SetLastError(ERROR_FILENAME_EXCED_RANGE);
590 return 0;
592 return copy_filename_WtoA( shortpathW, shortpath, shortlen );
596 /***********************************************************************
597 * GetTempPathA (KERNEL32.@)
599 DWORD WINAPI GetTempPathA( DWORD count, LPSTR path )
601 WCHAR pathW[MAX_PATH];
602 UINT ret;
604 ret = GetTempPathW(MAX_PATH, pathW);
606 if (!ret)
607 return 0;
609 if (ret > MAX_PATH)
611 SetLastError(ERROR_FILENAME_EXCED_RANGE);
612 return 0;
614 return copy_filename_WtoA( pathW, path, count );
618 /***********************************************************************
619 * GetTempPathW (KERNEL32.@)
621 DWORD WINAPI GetTempPathW( DWORD count, LPWSTR path )
623 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
624 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
625 static const WCHAR userprofile[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
626 WCHAR tmp_path[MAX_PATH];
627 UINT ret;
629 TRACE("%u,%p\n", count, path);
631 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )) &&
632 !(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )) &&
633 !(ret = GetEnvironmentVariableW( userprofile, tmp_path, MAX_PATH )) &&
634 !(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
635 return 0;
637 if (ret > MAX_PATH)
639 SetLastError(ERROR_FILENAME_EXCED_RANGE);
640 return 0;
643 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
644 if (!ret) return 0;
646 if (ret > MAX_PATH - 2)
648 SetLastError(ERROR_FILENAME_EXCED_RANGE);
649 return 0;
652 if (tmp_path[ret-1] != '\\')
654 tmp_path[ret++] = '\\';
655 tmp_path[ret] = '\0';
658 ret++; /* add space for terminating 0 */
660 if (count >= ret)
662 lstrcpynW(path, tmp_path, count);
663 /* the remaining buffer must be zeroed up to 32766 bytes in XP or 32767
664 * bytes after it, we will assume the > XP behavior for now */
665 memset(path + ret, 0, (min(count, 32767) - ret) * sizeof(WCHAR));
666 ret--; /* return length without 0 */
668 else if (count)
670 /* the buffer must be cleared if contents will not fit */
671 memset(path, 0, count * sizeof(WCHAR));
674 TRACE("returning %u, %s\n", ret, debugstr_w(path));
675 return ret;
679 /***********************************************************************
680 * GetTempFileNameA (KERNEL32.@)
682 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
684 WCHAR *pathW, *prefixW = NULL;
685 WCHAR bufferW[MAX_PATH];
686 UINT ret;
688 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return 0;
689 if (prefix && !(prefixW = FILE_name_AtoW( prefix, TRUE ))) return 0;
691 ret = GetTempFileNameW(pathW, prefixW, unique, bufferW);
692 if (ret) FILE_name_WtoA( bufferW, -1, buffer, MAX_PATH );
694 HeapFree( GetProcessHeap(), 0, prefixW );
695 return ret;
698 /***********************************************************************
699 * GetTempFileNameW (KERNEL32.@)
701 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
703 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
705 int i;
706 LPWSTR p;
707 DWORD attr;
709 if ( !path || !buffer )
711 SetLastError( ERROR_INVALID_PARAMETER );
712 return 0;
715 /* ensure that the provided directory exists */
716 attr = GetFileAttributesW(path);
717 if (attr == INVALID_FILE_ATTRIBUTES || !(attr & FILE_ATTRIBUTE_DIRECTORY))
719 TRACE("path not found %s\n", debugstr_w(path));
720 SetLastError( ERROR_DIRECTORY );
721 return 0;
724 strcpyW( buffer, path );
725 p = buffer + strlenW(buffer);
727 /* add a \, if there isn't one */
728 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
730 if (prefix)
731 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
733 unique &= 0xffff;
735 if (unique) sprintfW( p, formatW, unique );
736 else
738 /* get a "random" unique number and try to create the file */
739 HANDLE handle;
740 UINT num = GetTickCount() & 0xffff;
741 static UINT last;
743 /* avoid using the same name twice in a short interval */
744 if (last - num < 10) num = last + 1;
745 if (!num) num = 1;
746 unique = num;
749 sprintfW( p, formatW, unique );
750 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
751 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
752 if (handle != INVALID_HANDLE_VALUE)
753 { /* We created it */
754 TRACE("created %s\n", debugstr_w(buffer) );
755 CloseHandle( handle );
756 last = unique;
757 break;
759 if (GetLastError() != ERROR_FILE_EXISTS &&
760 GetLastError() != ERROR_SHARING_VIOLATION)
761 break; /* No need to go on */
762 if (!(++unique & 0xffff)) unique = 1;
763 } while (unique != num);
766 TRACE("returning %s\n", debugstr_w(buffer) );
767 return unique;
771 /***********************************************************************
772 * get_path_safe_mode
774 static BOOL get_path_safe_mode(void)
776 static const WCHAR keyW[] = {'\\','R','e','g','i','s','t','r','y','\\',
777 'M','a','c','h','i','n','e','\\',
778 'S','y','s','t','e','m','\\',
779 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
780 'C','o','n','t','r','o','l','\\',
781 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
782 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};
784 if (path_safe_mode == -1)
786 char buffer[offsetof(KEY_VALUE_PARTIAL_INFORMATION, Data[sizeof(DWORD)])];
787 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
788 OBJECT_ATTRIBUTES attr;
789 UNICODE_STRING nameW;
790 HANDLE hkey;
791 DWORD size = sizeof(buffer);
792 BOOL mode = FALSE;
794 attr.Length = sizeof(attr);
795 attr.RootDirectory = 0;
796 attr.ObjectName = &nameW;
797 attr.Attributes = 0;
798 attr.SecurityDescriptor = NULL;
799 attr.SecurityQualityOfService = NULL;
801 RtlInitUnicodeString( &nameW, keyW );
802 if (!NtOpenKey( &hkey, KEY_READ, &attr ))
804 RtlInitUnicodeString( &nameW, valueW );
805 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, buffer, size, &size ) &&
806 info->Type == REG_DWORD && info->DataLength == sizeof(DWORD))
807 mode = !!*(DWORD *)info->Data;
808 NtClose( hkey );
810 InterlockedCompareExchange( &path_safe_mode, mode, -1 );
812 return path_safe_mode != 0;
816 /***********************************************************************
817 * contains_pathW
819 * Check if the file name contains a path; helper for SearchPathW.
820 * A relative path is not considered a path unless it starts with ./ or ../
822 static inline BOOL contains_pathW (LPCWSTR name)
824 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
825 if (name[0] != '.') return FALSE;
826 if (name[1] == '/' || name[1] == '\\') return TRUE;
827 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
830 /***********************************************************************
831 * find_actctx_dllpath
833 * Find the path (if any) of the dll from the activation context.
834 * Returned path doesn't include a name.
836 static NTSTATUS find_actctx_dllpath(const WCHAR *libname, WCHAR **path)
838 static const WCHAR winsxsW[] = {'\\','w','i','n','s','x','s','\\'};
839 static const WCHAR dotManifestW[] = {'.','m','a','n','i','f','e','s','t',0};
841 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *info;
842 ACTCTX_SECTION_KEYED_DATA data;
843 UNICODE_STRING nameW;
844 NTSTATUS status;
845 SIZE_T needed, size = 1024;
846 WCHAR *p;
848 RtlInitUnicodeString( &nameW, libname );
849 data.cbSize = sizeof(data);
850 status = RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, NULL,
851 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION,
852 &nameW, &data );
853 if (status != STATUS_SUCCESS) return status;
855 for (;;)
857 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
859 status = STATUS_NO_MEMORY;
860 goto done;
862 status = RtlQueryInformationActivationContext( 0, data.hActCtx, &data.ulAssemblyRosterIndex,
863 AssemblyDetailedInformationInActivationContext,
864 info, size, &needed );
865 if (status == STATUS_SUCCESS) break;
866 if (status != STATUS_BUFFER_TOO_SMALL) goto done;
867 HeapFree( GetProcessHeap(), 0, info );
868 size = needed;
869 /* restart with larger buffer */
872 if (!info->lpAssemblyManifestPath || !info->lpAssemblyDirectoryName)
874 status = STATUS_SXS_KEY_NOT_FOUND;
875 goto done;
878 if ((p = strrchrW( info->lpAssemblyManifestPath, '\\' )))
880 DWORD dirlen = info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
882 p++;
883 if (strncmpiW( p, info->lpAssemblyDirectoryName, dirlen ) || strcmpiW( p + dirlen, dotManifestW ))
885 /* manifest name does not match directory name, so it's not a global
886 * windows/winsxs manifest; use the manifest directory name instead */
887 dirlen = p - info->lpAssemblyManifestPath;
888 needed = (dirlen + 1) * sizeof(WCHAR);
889 if (!(*path = p = HeapAlloc( GetProcessHeap(), 0, needed )))
891 status = STATUS_NO_MEMORY;
892 goto done;
894 memcpy( p, info->lpAssemblyManifestPath, dirlen * sizeof(WCHAR) );
895 *(p + dirlen) = 0;
896 goto done;
900 needed = (strlenW( DIR_Windows ) * sizeof(WCHAR) +
901 sizeof(winsxsW) + info->ulAssemblyDirectoryNameLength + 2*sizeof(WCHAR));
903 if (!(*path = p = HeapAlloc( GetProcessHeap(), 0, needed )))
905 status = STATUS_NO_MEMORY;
906 goto done;
908 strcpyW( p, DIR_Windows );
909 p += strlenW(p);
910 memcpy( p, winsxsW, sizeof(winsxsW) );
911 p += ARRAY_SIZE( winsxsW );
912 memcpy( p, info->lpAssemblyDirectoryName, info->ulAssemblyDirectoryNameLength );
913 p += info->ulAssemblyDirectoryNameLength / sizeof(WCHAR);
914 *p++ = '\\';
915 *p = 0;
916 done:
917 HeapFree( GetProcessHeap(), 0, info );
918 RtlReleaseActivationContext( data.hActCtx );
919 return status;
922 /***********************************************************************
923 * SearchPathW [KERNEL32.@]
925 * Searches for a specified file in the search path.
927 * PARAMS
928 * path [I] Path to search (NULL means default)
929 * name [I] Filename to search for.
930 * ext [I] File extension to append to file name. The first
931 * character must be a period. This parameter is
932 * specified only if the filename given does not
933 * contain an extension.
934 * buflen [I] size of buffer, in characters
935 * buffer [O] buffer for found filename
936 * lastpart [O] address of pointer to last used character in
937 * buffer (the final '\')
939 * RETURNS
940 * Success: length of string copied into buffer, not including
941 * terminating null character. If the filename found is
942 * longer than the length of the buffer, the length of the
943 * filename is returned.
944 * Failure: Zero
946 * NOTES
947 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
948 * (tested on NT 4.0)
950 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
951 LPWSTR buffer, LPWSTR *lastpart )
953 DWORD ret = 0;
955 if (!name || !name[0])
957 SetLastError(ERROR_INVALID_PARAMETER);
958 return 0;
961 /* If the name contains an explicit path, ignore the path */
963 if (contains_pathW(name))
965 /* try first without extension */
966 if (RtlDoesFileExists_U( name ))
967 return GetFullPathNameW( name, buflen, buffer, lastpart );
969 if (ext)
971 LPCWSTR p = strrchrW( name, '.' );
972 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
973 ext = NULL; /* Ignore the specified extension */
976 /* Allocate a buffer for the file name and extension */
977 if (ext)
979 LPWSTR tmp;
980 DWORD len = strlenW(name) + strlenW(ext);
982 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
984 SetLastError( ERROR_OUTOFMEMORY );
985 return 0;
987 strcpyW( tmp, name );
988 strcatW( tmp, ext );
989 if (RtlDoesFileExists_U( tmp ))
990 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
991 HeapFree( GetProcessHeap(), 0, tmp );
994 else if (path && path[0]) /* search in the specified path */
996 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
997 buffer, lastpart ) / sizeof(WCHAR);
999 else /* search in active context and default path */
1001 WCHAR *dll_path = NULL, *search = NULL;
1002 DWORD req_len, name_len;
1004 req_len = name_len = strlenW(name);
1006 if (strchrW( name, '.' )) ext = NULL;
1007 if (ext)
1009 DWORD ext_len = strlenW(ext);
1011 req_len += ext_len;
1012 name_len += ext_len;
1014 search = HeapAlloc( GetProcessHeap(), 0, (name_len + ext_len + 1) * sizeof(WCHAR) );
1015 if (!search)
1017 SetLastError( ERROR_OUTOFMEMORY );
1018 return 0;
1020 strcpyW( search, name );
1021 strcatW( search, ext );
1022 name = search;
1024 /* now that we have combined name we don't need extension any more */
1027 /* When file is found with activation context no attempt is made
1028 to check if it's really exist, path is returned only basing on context info. */
1029 if (find_actctx_dllpath( name, &dll_path ) == STATUS_SUCCESS)
1031 DWORD path_len;
1033 path_len = strlenW(dll_path);
1034 req_len += path_len;
1036 if (lastpart) *lastpart = NULL;
1038 /* count null termination char too */
1039 if (req_len + 1 <= buflen)
1041 memcpy( buffer, dll_path, path_len * sizeof(WCHAR) );
1042 memcpy( &buffer[path_len], name, name_len * sizeof(WCHAR) );
1043 buffer[req_len] = 0;
1044 if (lastpart) *lastpart = buffer + path_len;
1045 ret = req_len;
1047 else
1048 ret = req_len + 1;
1050 HeapFree( GetProcessHeap(), 0, dll_path );
1051 HeapFree( GetProcessHeap(), 0, search );
1053 else
1055 if ((dll_path = MODULE_get_dll_load_path( NULL, get_path_safe_mode() )))
1057 ret = RtlDosSearchPath_U( dll_path, name, NULL, buflen * sizeof(WCHAR),
1058 buffer, lastpart ) / sizeof(WCHAR);
1059 HeapFree( GetProcessHeap(), 0, dll_path );
1060 HeapFree( GetProcessHeap(), 0, search );
1062 else
1064 SetLastError( ERROR_OUTOFMEMORY );
1065 return 0;
1070 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
1071 else TRACE( "found %s\n", debugstr_w(buffer) );
1072 return ret;
1076 /***********************************************************************
1077 * SearchPathA (KERNEL32.@)
1079 * See SearchPathW.
1081 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
1082 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
1084 WCHAR *pathW = NULL, *nameW, *extW = NULL;
1085 WCHAR bufferW[MAX_PATH];
1086 DWORD ret;
1088 if (!name)
1090 SetLastError(ERROR_INVALID_PARAMETER);
1091 return 0;
1094 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
1095 if (path && !(pathW = FILE_name_AtoW( path, TRUE ))) return 0;
1097 if (ext && !(extW = FILE_name_AtoW( ext, TRUE )))
1099 HeapFree( GetProcessHeap(), 0, pathW );
1100 return 0;
1103 ret = SearchPathW(pathW, nameW, extW, MAX_PATH, bufferW, NULL);
1105 HeapFree( GetProcessHeap(), 0, pathW );
1106 HeapFree( GetProcessHeap(), 0, extW );
1108 if (!ret) return 0;
1109 if (ret > MAX_PATH)
1111 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1112 return 0;
1114 ret = copy_filename_WtoA( bufferW, buffer, buflen );
1115 if (buflen > ret && lastpart)
1116 *lastpart = strrchr(buffer, '\\') + 1;
1117 return ret;
1120 static BOOL is_same_file(HANDLE h1, HANDLE h2)
1122 int fd1;
1123 BOOL ret = FALSE;
1124 if (wine_server_handle_to_fd(h1, 0, &fd1, NULL) == STATUS_SUCCESS)
1126 int fd2;
1127 if (wine_server_handle_to_fd(h2, 0, &fd2, NULL) == STATUS_SUCCESS)
1129 struct stat stat1, stat2;
1130 if (fstat(fd1, &stat1) == 0 && fstat(fd2, &stat2) == 0)
1131 ret = (stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino);
1132 wine_server_release_fd(h2, fd2);
1134 wine_server_release_fd(h1, fd1);
1136 return ret;
1139 /**************************************************************************
1140 * CopyFileW (KERNEL32.@)
1142 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
1144 return CopyFileExW( source, dest, NULL, NULL, NULL,
1145 fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0 );
1149 /**************************************************************************
1150 * CopyFileA (KERNEL32.@)
1152 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
1154 WCHAR *sourceW, *destW;
1155 BOOL ret;
1157 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1158 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1160 ret = CopyFileW( sourceW, destW, fail_if_exists );
1162 HeapFree( GetProcessHeap(), 0, destW );
1163 return ret;
1167 /**************************************************************************
1168 * CopyFileExW (KERNEL32.@)
1170 BOOL WINAPI CopyFileExW(LPCWSTR source, LPCWSTR dest,
1171 LPPROGRESS_ROUTINE progress, LPVOID param,
1172 LPBOOL cancel_ptr, DWORD flags)
1174 static const int buffer_size = 65536;
1175 HANDLE h1, h2;
1176 BY_HANDLE_FILE_INFORMATION info;
1177 DWORD count;
1178 BOOL ret = FALSE;
1179 char *buffer;
1181 if (!source || !dest)
1183 SetLastError(ERROR_INVALID_PARAMETER);
1184 return FALSE;
1186 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
1188 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1189 return FALSE;
1192 TRACE("%s -> %s, %x\n", debugstr_w(source), debugstr_w(dest), flags);
1194 if ((h1 = CreateFileW(source, GENERIC_READ,
1195 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1196 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
1198 WARN("Unable to open source %s\n", debugstr_w(source));
1199 HeapFree( GetProcessHeap(), 0, buffer );
1200 return FALSE;
1203 if (!GetFileInformationByHandle( h1, &info ))
1205 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
1206 HeapFree( GetProcessHeap(), 0, buffer );
1207 CloseHandle( h1 );
1208 return FALSE;
1211 if (!(flags & COPY_FILE_FAIL_IF_EXISTS))
1213 BOOL same_file = FALSE;
1214 h2 = CreateFileW( dest, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1215 OPEN_EXISTING, 0, 0);
1216 if (h2 != INVALID_HANDLE_VALUE)
1218 same_file = is_same_file( h1, h2 );
1219 CloseHandle( h2 );
1221 if (same_file)
1223 HeapFree( GetProcessHeap(), 0, buffer );
1224 CloseHandle( h1 );
1225 SetLastError( ERROR_SHARING_VIOLATION );
1226 return FALSE;
1230 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1231 (flags & COPY_FILE_FAIL_IF_EXISTS) ? CREATE_NEW : CREATE_ALWAYS,
1232 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
1234 WARN("Unable to open dest %s\n", debugstr_w(dest));
1235 HeapFree( GetProcessHeap(), 0, buffer );
1236 CloseHandle( h1 );
1237 return FALSE;
1240 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
1242 char *p = buffer;
1243 while (count != 0)
1245 DWORD res;
1246 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
1247 p += res;
1248 count -= res;
1251 ret = TRUE;
1252 done:
1253 /* Maintain the timestamp of source file to destination file */
1254 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
1255 HeapFree( GetProcessHeap(), 0, buffer );
1256 CloseHandle( h1 );
1257 CloseHandle( h2 );
1258 return ret;
1262 /**************************************************************************
1263 * CopyFileExA (KERNEL32.@)
1265 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
1266 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
1267 LPBOOL cancelFlagPointer, DWORD copyFlags)
1269 WCHAR *sourceW, *destW;
1270 BOOL ret;
1272 /* can't use the TEB buffer since we may have a callback routine */
1273 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
1274 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
1276 HeapFree( GetProcessHeap(), 0, sourceW );
1277 return FALSE;
1279 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
1280 cancelFlagPointer, copyFlags);
1281 HeapFree( GetProcessHeap(), 0, sourceW );
1282 HeapFree( GetProcessHeap(), 0, destW );
1283 return ret;
1286 /**************************************************************************
1287 * MoveFileTransactedA (KERNEL32.@)
1289 BOOL WINAPI MoveFileTransactedA(const char *source, const char *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
1291 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_a(source), debugstr_a(dest), progress, data, flags, handle);
1292 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1293 return FALSE;
1296 /**************************************************************************
1297 * MoveFileTransactedW (KERNEL32.@)
1299 BOOL WINAPI MoveFileTransactedW(const WCHAR *source, const WCHAR *dest, LPPROGRESS_ROUTINE progress, void *data, DWORD flags, HANDLE handle)
1301 FIXME("(%s, %s, %p, %p, %d, %p)\n", debugstr_w(source), debugstr_w(dest), progress, data, flags, handle);
1302 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1303 return FALSE;
1306 /**************************************************************************
1307 * MoveFileWithProgressW (KERNEL32.@)
1309 BOOL WINAPI MoveFileWithProgressW( LPCWSTR source, LPCWSTR dest,
1310 LPPROGRESS_ROUTINE fnProgress,
1311 LPVOID param, DWORD flag )
1313 FILE_BASIC_INFORMATION info;
1314 UNICODE_STRING nt_name;
1315 OBJECT_ATTRIBUTES attr;
1316 IO_STATUS_BLOCK io;
1317 NTSTATUS status;
1318 HANDLE source_handle = 0, dest_handle = 0;
1319 ANSI_STRING source_unix, dest_unix;
1320 DWORD options;
1322 TRACE("(%s,%s,%p,%p,%04x)\n",
1323 debugstr_w(source), debugstr_w(dest), fnProgress, param, flag );
1325 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
1326 return add_boot_rename_entry( source, dest, flag );
1328 if (!dest)
1329 return DeleteFileW( source );
1331 /* check if we are allowed to rename the source */
1333 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
1335 SetLastError( ERROR_PATH_NOT_FOUND );
1336 return FALSE;
1338 source_unix.Buffer = NULL;
1339 dest_unix.Buffer = NULL;
1340 attr.Length = sizeof(attr);
1341 attr.RootDirectory = 0;
1342 attr.Attributes = OBJ_CASE_INSENSITIVE;
1343 attr.ObjectName = &nt_name;
1344 attr.SecurityDescriptor = NULL;
1345 attr.SecurityQualityOfService = NULL;
1347 status = NtOpenFile( &source_handle, DELETE | SYNCHRONIZE, &attr, &io,
1348 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SYNCHRONOUS_IO_NONALERT );
1349 if (status == STATUS_SUCCESS)
1350 status = wine_nt_to_unix_file_name( &nt_name, &source_unix, FILE_OPEN, FALSE );
1351 RtlFreeUnicodeString( &nt_name );
1352 if (status != STATUS_SUCCESS)
1354 SetLastError( RtlNtStatusToDosError(status) );
1355 goto error;
1357 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
1358 if (status != STATUS_SUCCESS)
1360 SetLastError( RtlNtStatusToDosError(status) );
1361 goto error;
1364 /* we must have write access to the destination, and it must */
1365 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1367 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
1369 SetLastError( ERROR_PATH_NOT_FOUND );
1370 goto error;
1372 options = FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT;
1373 if (flag & MOVEFILE_WRITE_THROUGH)
1374 options |= FILE_WRITE_THROUGH;
1375 status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &attr, &io,
1376 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, options );
1377 if (status == STATUS_SUCCESS) /* destination exists */
1379 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1381 if (!is_same_file( source_handle, dest_handle ))
1383 SetLastError( ERROR_ALREADY_EXISTS );
1384 RtlFreeUnicodeString( &nt_name );
1385 goto error;
1388 else if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) /* cannot replace directory */
1390 SetLastError( ERROR_ACCESS_DENIED );
1391 goto error;
1394 NtClose( dest_handle );
1395 dest_handle = NULL;
1397 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1399 SetLastError( RtlNtStatusToDosError(status) );
1400 RtlFreeUnicodeString( &nt_name );
1401 goto error;
1404 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1405 RtlFreeUnicodeString( &nt_name );
1406 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1408 SetLastError( RtlNtStatusToDosError(status) );
1409 goto error;
1412 /* now perform the rename */
1414 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1416 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1418 NtClose( source_handle );
1419 RtlFreeAnsiString( &source_unix );
1420 RtlFreeAnsiString( &dest_unix );
1421 if (!CopyFileExW( source, dest, fnProgress, param, NULL,
1422 flag & MOVEFILE_REPLACE_EXISTING ?
1423 0 : COPY_FILE_FAIL_IF_EXISTS ))
1424 return FALSE;
1425 return DeleteFileW( source );
1427 FILE_SetDosError();
1428 /* if we created the destination, remove it */
1429 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1430 goto error;
1433 /* fixup executable permissions */
1435 if (is_executable( source ) != is_executable( dest ))
1437 struct stat fstat;
1438 if (stat( dest_unix.Buffer, &fstat ) != -1)
1440 if (is_executable( dest ))
1441 /* set executable bit where read bit is set */
1442 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1443 else
1444 fstat.st_mode &= ~0111;
1445 chmod( dest_unix.Buffer, fstat.st_mode );
1449 NtClose( source_handle );
1450 RtlFreeAnsiString( &source_unix );
1451 RtlFreeAnsiString( &dest_unix );
1452 return TRUE;
1454 error:
1455 if (source_handle) NtClose( source_handle );
1456 if (dest_handle) NtClose( dest_handle );
1457 RtlFreeAnsiString( &source_unix );
1458 RtlFreeAnsiString( &dest_unix );
1459 return FALSE;
1462 /**************************************************************************
1463 * MoveFileWithProgressA (KERNEL32.@)
1465 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
1466 LPPROGRESS_ROUTINE fnProgress,
1467 LPVOID param, DWORD flag )
1469 WCHAR *sourceW, *destW;
1470 BOOL ret;
1472 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1473 if (dest)
1475 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1477 else
1478 destW = NULL;
1480 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
1481 HeapFree( GetProcessHeap(), 0, destW );
1482 return ret;
1485 /**************************************************************************
1486 * MoveFileExW (KERNEL32.@)
1488 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
1490 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
1493 /**************************************************************************
1494 * MoveFileExA (KERNEL32.@)
1496 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1498 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
1502 /**************************************************************************
1503 * MoveFileW (KERNEL32.@)
1505 * Move file or directory
1507 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1509 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1513 /**************************************************************************
1514 * MoveFileA (KERNEL32.@)
1516 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1518 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1522 /*************************************************************************
1523 * CreateHardLinkW (KERNEL32.@)
1525 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
1526 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1528 NTSTATUS status;
1529 UNICODE_STRING ntDest, ntSource;
1530 ANSI_STRING unixDest, unixSource;
1531 BOOL ret = FALSE;
1533 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
1534 debugstr_w(lpExistingFileName), lpSecurityAttributes);
1536 ntDest.Buffer = ntSource.Buffer = NULL;
1537 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
1538 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
1540 SetLastError( ERROR_PATH_NOT_FOUND );
1541 goto err;
1544 unixSource.Buffer = unixDest.Buffer = NULL;
1545 status = wine_nt_to_unix_file_name( &ntSource, &unixSource, FILE_OPEN, FALSE );
1546 if (!status)
1548 status = wine_nt_to_unix_file_name( &ntDest, &unixDest, FILE_CREATE, FALSE );
1549 if (!status) /* destination must not exist */
1551 status = STATUS_OBJECT_NAME_EXISTS;
1552 } else if (status == STATUS_NO_SUCH_FILE)
1554 status = STATUS_SUCCESS;
1558 if (status)
1559 SetLastError( RtlNtStatusToDosError(status) );
1560 else if (!link( unixSource.Buffer, unixDest.Buffer ))
1562 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest.Buffer ),
1563 debugstr_a( unixSource.Buffer ));
1564 ret = TRUE;
1566 else
1567 FILE_SetDosError();
1569 RtlFreeAnsiString( &unixSource );
1570 RtlFreeAnsiString( &unixDest );
1572 err:
1573 RtlFreeUnicodeString( &ntSource );
1574 RtlFreeUnicodeString( &ntDest );
1575 return ret;
1579 /*************************************************************************
1580 * CreateHardLinkA (KERNEL32.@)
1582 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
1583 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1585 WCHAR *sourceW, *destW;
1586 BOOL res;
1588 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
1590 return FALSE;
1592 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
1594 HeapFree( GetProcessHeap(), 0, sourceW );
1595 return FALSE;
1598 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
1600 HeapFree( GetProcessHeap(), 0, sourceW );
1601 HeapFree( GetProcessHeap(), 0, destW );
1603 return res;
1607 /***********************************************************************
1608 * CreateDirectoryW (KERNEL32.@)
1609 * RETURNS:
1610 * TRUE : success
1611 * FALSE : failure
1612 * ERROR_DISK_FULL: on full disk
1613 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1614 * ERROR_ACCESS_DENIED: on permission problems
1615 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1617 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1619 OBJECT_ATTRIBUTES attr;
1620 UNICODE_STRING nt_name;
1621 IO_STATUS_BLOCK io;
1622 NTSTATUS status;
1623 HANDLE handle;
1624 BOOL ret = FALSE;
1626 TRACE( "%s\n", debugstr_w(path) );
1628 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1630 SetLastError( ERROR_PATH_NOT_FOUND );
1631 return FALSE;
1633 attr.Length = sizeof(attr);
1634 attr.RootDirectory = 0;
1635 attr.Attributes = OBJ_CASE_INSENSITIVE;
1636 attr.ObjectName = &nt_name;
1637 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1638 attr.SecurityQualityOfService = NULL;
1640 status = NtCreateFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, NULL,
1641 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1642 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1644 if (status == STATUS_SUCCESS)
1646 NtClose( handle );
1647 ret = TRUE;
1649 else SetLastError( RtlNtStatusToDosError(status) );
1651 RtlFreeUnicodeString( &nt_name );
1652 return ret;
1656 /***********************************************************************
1657 * CreateDirectoryA (KERNEL32.@)
1659 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1661 WCHAR *pathW;
1663 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1664 return CreateDirectoryW( pathW, sa );
1668 /***********************************************************************
1669 * CreateDirectoryExA (KERNEL32.@)
1671 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1673 WCHAR *pathW, *templateW = NULL;
1674 BOOL ret;
1676 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1677 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
1679 ret = CreateDirectoryExW( templateW, pathW, sa );
1680 HeapFree( GetProcessHeap(), 0, templateW );
1681 return ret;
1685 /***********************************************************************
1686 * CreateDirectoryExW (KERNEL32.@)
1688 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1690 return CreateDirectoryW( path, sa );
1694 /***********************************************************************
1695 * RemoveDirectoryW (KERNEL32.@)
1697 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1699 OBJECT_ATTRIBUTES attr;
1700 UNICODE_STRING nt_name;
1701 ANSI_STRING unix_name;
1702 IO_STATUS_BLOCK io;
1703 NTSTATUS status;
1704 HANDLE handle;
1705 BOOL ret = FALSE;
1707 TRACE( "%s\n", debugstr_w(path) );
1709 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1711 SetLastError( ERROR_PATH_NOT_FOUND );
1712 return FALSE;
1714 attr.Length = sizeof(attr);
1715 attr.RootDirectory = 0;
1716 attr.Attributes = OBJ_CASE_INSENSITIVE;
1717 attr.ObjectName = &nt_name;
1718 attr.SecurityDescriptor = NULL;
1719 attr.SecurityQualityOfService = NULL;
1721 status = NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
1722 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1723 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1724 if (status != STATUS_SUCCESS)
1726 SetLastError( RtlNtStatusToDosError(status) );
1727 RtlFreeUnicodeString( &nt_name );
1728 return FALSE;
1731 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1732 RtlFreeUnicodeString( &nt_name );
1733 if (status != STATUS_SUCCESS)
1735 SetLastError( RtlNtStatusToDosError(status) );
1736 NtClose( handle );
1737 return FALSE;
1740 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1741 RtlFreeAnsiString( &unix_name );
1742 NtClose( handle );
1743 return ret;
1747 /***********************************************************************
1748 * RemoveDirectoryA (KERNEL32.@)
1750 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1752 WCHAR *pathW;
1754 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1755 return RemoveDirectoryW( pathW );
1759 /***********************************************************************
1760 * GetCurrentDirectoryW (KERNEL32.@)
1762 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1764 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1768 /***********************************************************************
1769 * GetCurrentDirectoryA (KERNEL32.@)
1771 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1773 WCHAR bufferW[MAX_PATH];
1774 DWORD ret;
1776 if (buflen && buf && ((ULONG_PTR)buf >> 16) == 0)
1778 /* Win9x catches access violations here, returning zero.
1779 * This behaviour resulted in some people not noticing
1780 * that they got the argument order wrong. So let's be
1781 * nice and fail gracefully if buf is invalid and looks
1782 * more like a buflen. */
1783 SetLastError(ERROR_INVALID_PARAMETER);
1784 return 0;
1787 ret = RtlGetCurrentDirectory_U( sizeof(bufferW), bufferW );
1788 if (!ret) return 0;
1789 if (ret > sizeof(bufferW))
1791 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1792 return 0;
1794 return copy_filename_WtoA( bufferW, buf, buflen );
1798 /***********************************************************************
1799 * SetCurrentDirectoryW (KERNEL32.@)
1801 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1803 UNICODE_STRING dirW;
1804 NTSTATUS status;
1806 RtlInitUnicodeString( &dirW, dir );
1807 status = RtlSetCurrentDirectory_U( &dirW );
1808 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1809 return !status;
1813 /***********************************************************************
1814 * SetCurrentDirectoryA (KERNEL32.@)
1816 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1818 WCHAR *dirW;
1819 UNICODE_STRING strW;
1820 NTSTATUS status;
1822 if (!(dirW = FILE_name_AtoW( dir, FALSE ))) return FALSE;
1823 RtlInitUnicodeString( &strW, dirW );
1824 status = RtlSetCurrentDirectory_U( &strW );
1825 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1826 return !status;
1830 /***********************************************************************
1831 * GetWindowsDirectoryW (KERNEL32.@)
1833 * See comment for GetWindowsDirectoryA.
1835 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
1837 UINT len = strlenW( DIR_Windows ) + 1;
1838 if (path && count >= len)
1840 strcpyW( path, DIR_Windows );
1841 len--;
1843 return len;
1847 /***********************************************************************
1848 * GetWindowsDirectoryA (KERNEL32.@)
1850 * Return value:
1851 * If buffer is large enough to hold full path and terminating '\0' character
1852 * function copies path to buffer and returns length of the path without '\0'.
1853 * Otherwise function returns required size including '\0' character and
1854 * does not touch the buffer.
1856 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
1858 return copy_filename_WtoA( DIR_Windows, path, count );
1862 /***********************************************************************
1863 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1865 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
1867 return GetWindowsDirectoryA( path, count );
1871 /***********************************************************************
1872 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1874 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
1876 return GetWindowsDirectoryW( path, count );
1880 /***********************************************************************
1881 * GetSystemDirectoryW (KERNEL32.@)
1883 * See comment for GetWindowsDirectoryA.
1885 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
1887 UINT len = strlenW( DIR_System ) + 1;
1888 if (path && count >= len)
1890 strcpyW( path, DIR_System );
1891 len--;
1893 return len;
1897 /***********************************************************************
1898 * GetSystemDirectoryA (KERNEL32.@)
1900 * See comment for GetWindowsDirectoryA.
1902 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
1904 return copy_filename_WtoA( DIR_System, path, count );
1908 /***********************************************************************
1909 * GetSystemWow64DirectoryW (KERNEL32.@)
1911 * As seen on MSDN
1912 * - On Win32 we should return ERROR_CALL_NOT_IMPLEMENTED
1913 * - On Win64 we should return the SysWow64 (system64) directory
1915 UINT WINAPI GetSystemWow64DirectoryW( LPWSTR path, UINT count )
1917 UINT len;
1919 if (!DIR_SysWow64)
1921 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1922 return 0;
1924 len = strlenW( DIR_SysWow64 ) + 1;
1925 if (path && count >= len)
1927 strcpyW( path, DIR_SysWow64 );
1928 len--;
1930 return len;
1934 /***********************************************************************
1935 * GetSystemWow64DirectoryA (KERNEL32.@)
1937 * See comment for GetWindowsWow64DirectoryW.
1939 UINT WINAPI GetSystemWow64DirectoryA( LPSTR path, UINT count )
1941 if (!DIR_SysWow64)
1943 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1944 return 0;
1946 return copy_filename_WtoA( DIR_SysWow64, path, count );
1950 /***********************************************************************
1951 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1953 BOOLEAN WINAPI Wow64EnableWow64FsRedirection( BOOLEAN enable )
1955 NTSTATUS status = RtlWow64EnableFsRedirection( enable );
1956 if (status) SetLastError( RtlNtStatusToDosError(status) );
1957 return !status;
1961 /***********************************************************************
1962 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1964 BOOL WINAPI Wow64DisableWow64FsRedirection( PVOID *old_value )
1966 NTSTATUS status = RtlWow64EnableFsRedirectionEx( TRUE, (ULONG *)old_value );
1967 if (status) SetLastError( RtlNtStatusToDosError(status) );
1968 return !status;
1972 /***********************************************************************
1973 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1975 BOOL WINAPI Wow64RevertWow64FsRedirection( PVOID old_value )
1977 NTSTATUS status = RtlWow64EnableFsRedirection( !old_value );
1978 if (status) SetLastError( RtlNtStatusToDosError(status) );
1979 return !status;
1983 /***********************************************************************
1984 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1986 BOOL WINAPI NeedCurrentDirectoryForExePathW( LPCWSTR name )
1988 static const WCHAR env_name[] = {'N','o','D','e','f','a','u','l','t',
1989 'C','u','r','r','e','n','t',
1990 'D','i','r','e','c','t','o','r','y',
1991 'I','n','E','x','e','P','a','t','h',0};
1992 WCHAR env_val;
1994 /* MSDN mentions some 'registry location'. We do not use registry. */
1995 FIXME("(%s): partial stub\n", debugstr_w(name));
1997 if (strchrW(name, '\\'))
1998 return TRUE;
2000 /* Check the existence of the variable, not value */
2001 if (!GetEnvironmentVariableW( env_name, &env_val, 1 ))
2002 return TRUE;
2004 return FALSE;
2008 /***********************************************************************
2009 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
2011 BOOL WINAPI NeedCurrentDirectoryForExePathA( LPCSTR name )
2013 WCHAR *nameW;
2015 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return TRUE;
2016 return NeedCurrentDirectoryForExePathW( nameW );
2020 /***********************************************************************
2021 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
2023 * Return the full Unix file name for a given path.
2024 * Returned buffer must be freed by caller.
2026 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
2028 UNICODE_STRING nt_name;
2029 ANSI_STRING unix_name;
2030 NTSTATUS status;
2032 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
2033 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
2034 RtlFreeUnicodeString( &nt_name );
2035 if (status && status != STATUS_NO_SUCH_FILE)
2037 SetLastError( RtlNtStatusToDosError( status ) );
2038 return NULL;
2040 return unix_name.Buffer;
2044 /***********************************************************************
2045 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
2047 * Return the full DOS file name for a given Unix path.
2048 * Returned buffer must be freed by caller.
2050 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
2052 UNICODE_STRING nt_name;
2053 ANSI_STRING unix_name;
2054 NTSTATUS status;
2055 DWORD len;
2057 RtlInitAnsiString( &unix_name, str );
2058 status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
2059 if (status)
2061 SetLastError( RtlNtStatusToDosError( status ) );
2062 return NULL;
2064 if (nt_name.Buffer[5] == ':')
2066 /* get rid of the \??\ prefix */
2067 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
2068 len = nt_name.Length - 4 * sizeof(WCHAR);
2069 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
2070 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
2072 else
2073 nt_name.Buffer[1] = '\\';
2074 return nt_name.Buffer;
2077 /*************************************************************************
2078 * CreateSymbolicLinkW (KERNEL32.@)
2080 BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags)
2082 FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags);
2083 return TRUE;
2086 /*************************************************************************
2087 * CreateSymbolicLinkA (KERNEL32.@)
2089 BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags)
2091 FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags);
2092 return TRUE;
2095 /*************************************************************************
2096 * CreateHardLinkTransactedA (KERNEL32.@)
2098 BOOL WINAPI CreateHardLinkTransactedA(LPCSTR link, LPCSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2100 FIXME("(%s %s %p %p): stub\n", debugstr_a(link), debugstr_a(target), sa, transaction);
2101 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2102 return FALSE;
2105 /*************************************************************************
2106 * CreateHardLinkTransactedW (KERNEL32.@)
2108 BOOL WINAPI CreateHardLinkTransactedW(LPCWSTR link, LPCWSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2110 FIXME("(%s %s %p %p): stub\n", debugstr_w(link), debugstr_w(target), sa, transaction);
2111 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2112 return FALSE;
2115 /*************************************************************************
2116 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
2118 BOOL WINAPI CheckNameLegalDOS8Dot3A(const char *name, char *oemname, DWORD oemname_len,
2119 BOOL *contains_spaces, BOOL *is_legal)
2121 WCHAR *nameW;
2123 TRACE("(%s %p %u %p %p)\n", name, oemname,
2124 oemname_len, contains_spaces, is_legal);
2126 if (!name || !is_legal)
2127 return FALSE;
2129 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2131 return CheckNameLegalDOS8Dot3W( nameW, oemname, oemname_len, contains_spaces, is_legal );
2134 /*************************************************************************
2135 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
2137 BOOL WINAPI CheckNameLegalDOS8Dot3W(const WCHAR *name, char *oemname, DWORD oemname_len,
2138 BOOL *contains_spaces_ret, BOOL *is_legal)
2140 OEM_STRING oem_str;
2141 UNICODE_STRING nameW;
2142 BOOLEAN contains_spaces;
2144 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name), oemname,
2145 oemname_len, contains_spaces_ret, is_legal);
2147 if (!name || !is_legal)
2148 return FALSE;
2150 RtlInitUnicodeString( &nameW, name );
2152 if (oemname) {
2153 oem_str.Length = oemname_len;
2154 oem_str.MaximumLength = oemname_len;
2155 oem_str.Buffer = oemname;
2158 *is_legal = RtlIsNameLegalDOS8Dot3( &nameW, oemname ? &oem_str : NULL, &contains_spaces );
2159 if (contains_spaces_ret) *contains_spaces_ret = contains_spaces;
2161 return TRUE;
2164 /*************************************************************************
2165 * SetSearchPathMode (KERNEL32.@)
2167 BOOL WINAPI SetSearchPathMode( DWORD flags )
2169 int val;
2171 switch (flags)
2173 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE:
2174 val = 1;
2175 break;
2176 case BASE_SEARCH_PATH_DISABLE_SAFE_SEARCHMODE:
2177 val = 0;
2178 break;
2179 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT:
2180 InterlockedExchange( &path_safe_mode, 2 );
2181 return TRUE;
2182 default:
2183 SetLastError( ERROR_INVALID_PARAMETER );
2184 return FALSE;
2187 for (;;)
2189 int prev = path_safe_mode;
2190 if (prev == 2) break; /* permanently set */
2191 if (InterlockedCompareExchange( &path_safe_mode, val, prev ) == prev) return TRUE;
2193 SetLastError( ERROR_ACCESS_DENIED );
2194 return FALSE;