winhttp: Get rid of send_request_t.
[wine.git] / dlls / kernel32 / path.c
blob41f0d34aa4e683c8969853cfc3cef3fb52a7f6e6
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 );
1396 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1398 SetLastError( RtlNtStatusToDosError(status) );
1399 RtlFreeUnicodeString( &nt_name );
1400 goto error;
1403 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1404 RtlFreeUnicodeString( &nt_name );
1405 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1407 SetLastError( RtlNtStatusToDosError(status) );
1408 goto error;
1411 /* now perform the rename */
1413 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1415 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1417 NtClose( source_handle );
1418 RtlFreeAnsiString( &source_unix );
1419 RtlFreeAnsiString( &dest_unix );
1420 if (!CopyFileExW( source, dest, fnProgress,
1421 param, NULL, COPY_FILE_FAIL_IF_EXISTS ))
1422 return FALSE;
1423 return DeleteFileW( source );
1425 FILE_SetDosError();
1426 /* if we created the destination, remove it */
1427 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1428 goto error;
1431 /* fixup executable permissions */
1433 if (is_executable( source ) != is_executable( dest ))
1435 struct stat fstat;
1436 if (stat( dest_unix.Buffer, &fstat ) != -1)
1438 if (is_executable( dest ))
1439 /* set executable bit where read bit is set */
1440 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1441 else
1442 fstat.st_mode &= ~0111;
1443 chmod( dest_unix.Buffer, fstat.st_mode );
1447 NtClose( source_handle );
1448 RtlFreeAnsiString( &source_unix );
1449 RtlFreeAnsiString( &dest_unix );
1450 return TRUE;
1452 error:
1453 if (source_handle) NtClose( source_handle );
1454 if (dest_handle) NtClose( dest_handle );
1455 RtlFreeAnsiString( &source_unix );
1456 RtlFreeAnsiString( &dest_unix );
1457 return FALSE;
1460 /**************************************************************************
1461 * MoveFileWithProgressA (KERNEL32.@)
1463 BOOL WINAPI MoveFileWithProgressA( LPCSTR source, LPCSTR dest,
1464 LPPROGRESS_ROUTINE fnProgress,
1465 LPVOID param, DWORD flag )
1467 WCHAR *sourceW, *destW;
1468 BOOL ret;
1470 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1471 if (dest)
1473 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1475 else
1476 destW = NULL;
1478 ret = MoveFileWithProgressW( sourceW, destW, fnProgress, param, flag );
1479 HeapFree( GetProcessHeap(), 0, destW );
1480 return ret;
1483 /**************************************************************************
1484 * MoveFileExW (KERNEL32.@)
1486 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
1488 return MoveFileWithProgressW( source, dest, NULL, NULL, flag );
1491 /**************************************************************************
1492 * MoveFileExA (KERNEL32.@)
1494 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1496 return MoveFileWithProgressA( source, dest, NULL, NULL, flag );
1500 /**************************************************************************
1501 * MoveFileW (KERNEL32.@)
1503 * Move file or directory
1505 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1507 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1511 /**************************************************************************
1512 * MoveFileA (KERNEL32.@)
1514 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1516 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1520 /*************************************************************************
1521 * CreateHardLinkW (KERNEL32.@)
1523 BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName,
1524 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1526 NTSTATUS status;
1527 UNICODE_STRING ntDest, ntSource;
1528 ANSI_STRING unixDest, unixSource;
1529 BOOL ret = FALSE;
1531 TRACE("(%s, %s, %p)\n", debugstr_w(lpFileName),
1532 debugstr_w(lpExistingFileName), lpSecurityAttributes);
1534 ntDest.Buffer = ntSource.Buffer = NULL;
1535 if (!RtlDosPathNameToNtPathName_U( lpFileName, &ntDest, NULL, NULL ) ||
1536 !RtlDosPathNameToNtPathName_U( lpExistingFileName, &ntSource, NULL, NULL ))
1538 SetLastError( ERROR_PATH_NOT_FOUND );
1539 goto err;
1542 unixSource.Buffer = unixDest.Buffer = NULL;
1543 status = wine_nt_to_unix_file_name( &ntSource, &unixSource, FILE_OPEN, FALSE );
1544 if (!status)
1546 status = wine_nt_to_unix_file_name( &ntDest, &unixDest, FILE_CREATE, FALSE );
1547 if (!status) /* destination must not exist */
1549 status = STATUS_OBJECT_NAME_EXISTS;
1550 } else if (status == STATUS_NO_SUCH_FILE)
1552 status = STATUS_SUCCESS;
1556 if (status)
1557 SetLastError( RtlNtStatusToDosError(status) );
1558 else if (!link( unixSource.Buffer, unixDest.Buffer ))
1560 TRACE("Hardlinked '%s' to '%s'\n", debugstr_a( unixDest.Buffer ),
1561 debugstr_a( unixSource.Buffer ));
1562 ret = TRUE;
1564 else
1565 FILE_SetDosError();
1567 RtlFreeAnsiString( &unixSource );
1568 RtlFreeAnsiString( &unixDest );
1570 err:
1571 RtlFreeUnicodeString( &ntSource );
1572 RtlFreeUnicodeString( &ntDest );
1573 return ret;
1577 /*************************************************************************
1578 * CreateHardLinkA (KERNEL32.@)
1580 BOOL WINAPI CreateHardLinkA(LPCSTR lpFileName, LPCSTR lpExistingFileName,
1581 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
1583 WCHAR *sourceW, *destW;
1584 BOOL res;
1586 if (!(sourceW = FILE_name_AtoW( lpExistingFileName, TRUE )))
1588 return FALSE;
1590 if (!(destW = FILE_name_AtoW( lpFileName, TRUE )))
1592 HeapFree( GetProcessHeap(), 0, sourceW );
1593 return FALSE;
1596 res = CreateHardLinkW( destW, sourceW, lpSecurityAttributes );
1598 HeapFree( GetProcessHeap(), 0, sourceW );
1599 HeapFree( GetProcessHeap(), 0, destW );
1601 return res;
1605 /***********************************************************************
1606 * CreateDirectoryW (KERNEL32.@)
1607 * RETURNS:
1608 * TRUE : success
1609 * FALSE : failure
1610 * ERROR_DISK_FULL: on full disk
1611 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1612 * ERROR_ACCESS_DENIED: on permission problems
1613 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1615 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1617 OBJECT_ATTRIBUTES attr;
1618 UNICODE_STRING nt_name;
1619 IO_STATUS_BLOCK io;
1620 NTSTATUS status;
1621 HANDLE handle;
1622 BOOL ret = FALSE;
1624 TRACE( "%s\n", debugstr_w(path) );
1626 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1628 SetLastError( ERROR_PATH_NOT_FOUND );
1629 return FALSE;
1631 attr.Length = sizeof(attr);
1632 attr.RootDirectory = 0;
1633 attr.Attributes = OBJ_CASE_INSENSITIVE;
1634 attr.ObjectName = &nt_name;
1635 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1636 attr.SecurityQualityOfService = NULL;
1638 status = NtCreateFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr, &io, NULL,
1639 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1640 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1642 if (status == STATUS_SUCCESS)
1644 NtClose( handle );
1645 ret = TRUE;
1647 else SetLastError( RtlNtStatusToDosError(status) );
1649 RtlFreeUnicodeString( &nt_name );
1650 return ret;
1654 /***********************************************************************
1655 * CreateDirectoryA (KERNEL32.@)
1657 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1659 WCHAR *pathW;
1661 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1662 return CreateDirectoryW( pathW, sa );
1666 /***********************************************************************
1667 * CreateDirectoryExA (KERNEL32.@)
1669 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1671 WCHAR *pathW, *templateW = NULL;
1672 BOOL ret;
1674 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1675 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
1677 ret = CreateDirectoryExW( templateW, pathW, sa );
1678 HeapFree( GetProcessHeap(), 0, templateW );
1679 return ret;
1683 /***********************************************************************
1684 * CreateDirectoryExW (KERNEL32.@)
1686 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1688 return CreateDirectoryW( path, sa );
1692 /***********************************************************************
1693 * RemoveDirectoryW (KERNEL32.@)
1695 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1697 OBJECT_ATTRIBUTES attr;
1698 UNICODE_STRING nt_name;
1699 ANSI_STRING unix_name;
1700 IO_STATUS_BLOCK io;
1701 NTSTATUS status;
1702 HANDLE handle;
1703 BOOL ret = FALSE;
1705 TRACE( "%s\n", debugstr_w(path) );
1707 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1709 SetLastError( ERROR_PATH_NOT_FOUND );
1710 return FALSE;
1712 attr.Length = sizeof(attr);
1713 attr.RootDirectory = 0;
1714 attr.Attributes = OBJ_CASE_INSENSITIVE;
1715 attr.ObjectName = &nt_name;
1716 attr.SecurityDescriptor = NULL;
1717 attr.SecurityQualityOfService = NULL;
1719 status = NtOpenFile( &handle, DELETE | SYNCHRONIZE, &attr, &io,
1720 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1721 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1722 if (status != STATUS_SUCCESS)
1724 SetLastError( RtlNtStatusToDosError(status) );
1725 RtlFreeUnicodeString( &nt_name );
1726 return FALSE;
1729 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1730 RtlFreeUnicodeString( &nt_name );
1731 if (status != STATUS_SUCCESS)
1733 SetLastError( RtlNtStatusToDosError(status) );
1734 NtClose( handle );
1735 return FALSE;
1738 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1739 RtlFreeAnsiString( &unix_name );
1740 NtClose( handle );
1741 return ret;
1745 /***********************************************************************
1746 * RemoveDirectoryA (KERNEL32.@)
1748 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1750 WCHAR *pathW;
1752 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1753 return RemoveDirectoryW( pathW );
1757 /***********************************************************************
1758 * GetCurrentDirectoryW (KERNEL32.@)
1760 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1762 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1766 /***********************************************************************
1767 * GetCurrentDirectoryA (KERNEL32.@)
1769 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1771 WCHAR bufferW[MAX_PATH];
1772 DWORD ret;
1774 if (buflen && buf && ((ULONG_PTR)buf >> 16) == 0)
1776 /* Win9x catches access violations here, returning zero.
1777 * This behaviour resulted in some people not noticing
1778 * that they got the argument order wrong. So let's be
1779 * nice and fail gracefully if buf is invalid and looks
1780 * more like a buflen. */
1781 SetLastError(ERROR_INVALID_PARAMETER);
1782 return 0;
1785 ret = RtlGetCurrentDirectory_U( sizeof(bufferW), bufferW );
1786 if (!ret) return 0;
1787 if (ret > sizeof(bufferW))
1789 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1790 return 0;
1792 return copy_filename_WtoA( bufferW, buf, buflen );
1796 /***********************************************************************
1797 * SetCurrentDirectoryW (KERNEL32.@)
1799 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1801 UNICODE_STRING dirW;
1802 NTSTATUS status;
1804 RtlInitUnicodeString( &dirW, dir );
1805 status = RtlSetCurrentDirectory_U( &dirW );
1806 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1807 return !status;
1811 /***********************************************************************
1812 * SetCurrentDirectoryA (KERNEL32.@)
1814 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1816 WCHAR *dirW;
1817 UNICODE_STRING strW;
1818 NTSTATUS status;
1820 if (!(dirW = FILE_name_AtoW( dir, FALSE ))) return FALSE;
1821 RtlInitUnicodeString( &strW, dirW );
1822 status = RtlSetCurrentDirectory_U( &strW );
1823 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
1824 return !status;
1828 /***********************************************************************
1829 * GetWindowsDirectoryW (KERNEL32.@)
1831 * See comment for GetWindowsDirectoryA.
1833 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
1835 UINT len = strlenW( DIR_Windows ) + 1;
1836 if (path && count >= len)
1838 strcpyW( path, DIR_Windows );
1839 len--;
1841 return len;
1845 /***********************************************************************
1846 * GetWindowsDirectoryA (KERNEL32.@)
1848 * Return value:
1849 * If buffer is large enough to hold full path and terminating '\0' character
1850 * function copies path to buffer and returns length of the path without '\0'.
1851 * Otherwise function returns required size including '\0' character and
1852 * does not touch the buffer.
1854 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
1856 return copy_filename_WtoA( DIR_Windows, path, count );
1860 /***********************************************************************
1861 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1863 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
1865 return GetWindowsDirectoryA( path, count );
1869 /***********************************************************************
1870 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1872 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
1874 return GetWindowsDirectoryW( path, count );
1878 /***********************************************************************
1879 * GetSystemDirectoryW (KERNEL32.@)
1881 * See comment for GetWindowsDirectoryA.
1883 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
1885 UINT len = strlenW( DIR_System ) + 1;
1886 if (path && count >= len)
1888 strcpyW( path, DIR_System );
1889 len--;
1891 return len;
1895 /***********************************************************************
1896 * GetSystemDirectoryA (KERNEL32.@)
1898 * See comment for GetWindowsDirectoryA.
1900 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
1902 return copy_filename_WtoA( DIR_System, path, count );
1906 /***********************************************************************
1907 * GetSystemWow64DirectoryW (KERNEL32.@)
1909 * As seen on MSDN
1910 * - On Win32 we should return ERROR_CALL_NOT_IMPLEMENTED
1911 * - On Win64 we should return the SysWow64 (system64) directory
1913 UINT WINAPI GetSystemWow64DirectoryW( LPWSTR path, UINT count )
1915 UINT len;
1917 if (!DIR_SysWow64)
1919 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1920 return 0;
1922 len = strlenW( DIR_SysWow64 ) + 1;
1923 if (path && count >= len)
1925 strcpyW( path, DIR_SysWow64 );
1926 len--;
1928 return len;
1932 /***********************************************************************
1933 * GetSystemWow64DirectoryA (KERNEL32.@)
1935 * See comment for GetWindowsWow64DirectoryW.
1937 UINT WINAPI GetSystemWow64DirectoryA( LPSTR path, UINT count )
1939 if (!DIR_SysWow64)
1941 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1942 return 0;
1944 return copy_filename_WtoA( DIR_SysWow64, path, count );
1948 /***********************************************************************
1949 * Wow64EnableWow64FsRedirection (KERNEL32.@)
1951 BOOLEAN WINAPI Wow64EnableWow64FsRedirection( BOOLEAN enable )
1953 NTSTATUS status = RtlWow64EnableFsRedirection( enable );
1954 if (status) SetLastError( RtlNtStatusToDosError(status) );
1955 return !status;
1959 /***********************************************************************
1960 * Wow64DisableWow64FsRedirection (KERNEL32.@)
1962 BOOL WINAPI Wow64DisableWow64FsRedirection( PVOID *old_value )
1964 NTSTATUS status = RtlWow64EnableFsRedirectionEx( TRUE, (ULONG *)old_value );
1965 if (status) SetLastError( RtlNtStatusToDosError(status) );
1966 return !status;
1970 /***********************************************************************
1971 * Wow64RevertWow64FsRedirection (KERNEL32.@)
1973 BOOL WINAPI Wow64RevertWow64FsRedirection( PVOID old_value )
1975 NTSTATUS status = RtlWow64EnableFsRedirection( !old_value );
1976 if (status) SetLastError( RtlNtStatusToDosError(status) );
1977 return !status;
1981 /***********************************************************************
1982 * NeedCurrentDirectoryForExePathW (KERNEL32.@)
1984 BOOL WINAPI NeedCurrentDirectoryForExePathW( LPCWSTR name )
1986 static const WCHAR env_name[] = {'N','o','D','e','f','a','u','l','t',
1987 'C','u','r','r','e','n','t',
1988 'D','i','r','e','c','t','o','r','y',
1989 'I','n','E','x','e','P','a','t','h',0};
1990 WCHAR env_val;
1992 /* MSDN mentions some 'registry location'. We do not use registry. */
1993 FIXME("(%s): partial stub\n", debugstr_w(name));
1995 if (strchrW(name, '\\'))
1996 return TRUE;
1998 /* Check the existence of the variable, not value */
1999 if (!GetEnvironmentVariableW( env_name, &env_val, 1 ))
2000 return TRUE;
2002 return FALSE;
2006 /***********************************************************************
2007 * NeedCurrentDirectoryForExePathA (KERNEL32.@)
2009 BOOL WINAPI NeedCurrentDirectoryForExePathA( LPCSTR name )
2011 WCHAR *nameW;
2013 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return TRUE;
2014 return NeedCurrentDirectoryForExePathW( nameW );
2018 /***********************************************************************
2019 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
2021 * Return the full Unix file name for a given path.
2022 * Returned buffer must be freed by caller.
2024 char * CDECL wine_get_unix_file_name( LPCWSTR dosW )
2026 UNICODE_STRING nt_name;
2027 ANSI_STRING unix_name;
2028 NTSTATUS status;
2030 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
2031 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
2032 RtlFreeUnicodeString( &nt_name );
2033 if (status && status != STATUS_NO_SUCH_FILE)
2035 SetLastError( RtlNtStatusToDosError( status ) );
2036 return NULL;
2038 return unix_name.Buffer;
2042 /***********************************************************************
2043 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
2045 * Return the full DOS file name for a given Unix path.
2046 * Returned buffer must be freed by caller.
2048 WCHAR * CDECL wine_get_dos_file_name( LPCSTR str )
2050 UNICODE_STRING nt_name;
2051 ANSI_STRING unix_name;
2052 NTSTATUS status;
2053 DWORD len;
2055 RtlInitAnsiString( &unix_name, str );
2056 status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
2057 if (status)
2059 SetLastError( RtlNtStatusToDosError( status ) );
2060 return NULL;
2062 if (nt_name.Buffer[5] == ':')
2064 /* get rid of the \??\ prefix */
2065 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
2066 len = nt_name.Length - 4 * sizeof(WCHAR);
2067 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
2068 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
2070 else
2071 nt_name.Buffer[1] = '\\';
2072 return nt_name.Buffer;
2075 /*************************************************************************
2076 * CreateSymbolicLinkW (KERNEL32.@)
2078 BOOLEAN WINAPI CreateSymbolicLinkW(LPCWSTR link, LPCWSTR target, DWORD flags)
2080 FIXME("(%s %s %d): stub\n", debugstr_w(link), debugstr_w(target), flags);
2081 return TRUE;
2084 /*************************************************************************
2085 * CreateSymbolicLinkA (KERNEL32.@)
2087 BOOLEAN WINAPI CreateSymbolicLinkA(LPCSTR link, LPCSTR target, DWORD flags)
2089 FIXME("(%s %s %d): stub\n", debugstr_a(link), debugstr_a(target), flags);
2090 return TRUE;
2093 /*************************************************************************
2094 * CreateHardLinkTransactedA (KERNEL32.@)
2096 BOOL WINAPI CreateHardLinkTransactedA(LPCSTR link, LPCSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2098 FIXME("(%s %s %p %p): stub\n", debugstr_a(link), debugstr_a(target), sa, transaction);
2099 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2100 return FALSE;
2103 /*************************************************************************
2104 * CreateHardLinkTransactedW (KERNEL32.@)
2106 BOOL WINAPI CreateHardLinkTransactedW(LPCWSTR link, LPCWSTR target, LPSECURITY_ATTRIBUTES sa, HANDLE transaction)
2108 FIXME("(%s %s %p %p): stub\n", debugstr_w(link), debugstr_w(target), sa, transaction);
2109 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2110 return FALSE;
2113 /*************************************************************************
2114 * CheckNameLegalDOS8Dot3A (KERNEL32.@)
2116 BOOL WINAPI CheckNameLegalDOS8Dot3A(const char *name, char *oemname, DWORD oemname_len,
2117 BOOL *contains_spaces, BOOL *is_legal)
2119 WCHAR *nameW;
2121 TRACE("(%s %p %u %p %p)\n", name, oemname,
2122 oemname_len, contains_spaces, is_legal);
2124 if (!name || !is_legal)
2125 return FALSE;
2127 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return FALSE;
2129 return CheckNameLegalDOS8Dot3W( nameW, oemname, oemname_len, contains_spaces, is_legal );
2132 /*************************************************************************
2133 * CheckNameLegalDOS8Dot3W (KERNEL32.@)
2135 BOOL WINAPI CheckNameLegalDOS8Dot3W(const WCHAR *name, char *oemname, DWORD oemname_len,
2136 BOOL *contains_spaces_ret, BOOL *is_legal)
2138 OEM_STRING oem_str;
2139 UNICODE_STRING nameW;
2140 BOOLEAN contains_spaces;
2142 TRACE("(%s %p %u %p %p)\n", wine_dbgstr_w(name), oemname,
2143 oemname_len, contains_spaces_ret, is_legal);
2145 if (!name || !is_legal)
2146 return FALSE;
2148 RtlInitUnicodeString( &nameW, name );
2150 if (oemname) {
2151 oem_str.Length = oemname_len;
2152 oem_str.MaximumLength = oemname_len;
2153 oem_str.Buffer = oemname;
2156 *is_legal = RtlIsNameLegalDOS8Dot3( &nameW, oemname ? &oem_str : NULL, &contains_spaces );
2157 if (contains_spaces_ret) *contains_spaces_ret = contains_spaces;
2159 return TRUE;
2162 /*************************************************************************
2163 * SetSearchPathMode (KERNEL32.@)
2165 BOOL WINAPI SetSearchPathMode( DWORD flags )
2167 int val;
2169 switch (flags)
2171 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE:
2172 val = 1;
2173 break;
2174 case BASE_SEARCH_PATH_DISABLE_SAFE_SEARCHMODE:
2175 val = 0;
2176 break;
2177 case BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE | BASE_SEARCH_PATH_PERMANENT:
2178 InterlockedExchange( &path_safe_mode, 2 );
2179 return TRUE;
2180 default:
2181 SetLastError( ERROR_INVALID_PARAMETER );
2182 return FALSE;
2185 for (;;)
2187 int prev = path_safe_mode;
2188 if (prev == 2) break; /* permanently set */
2189 if (InterlockedCompareExchange( &path_safe_mode, val, prev ) == prev) return TRUE;
2191 SetLastError( ERROR_ACCESS_DENIED );
2192 return FALSE;