programs/wcmd: Rename to programs/cmd.
[wine.git] / dlls / kernel / path.c
blobddd169ca6f7ab399a308448814f5062ea11c0e36
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 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33 #include "winerror.h"
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winternl.h"
40 #include "kernel_private.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(file);
46 #define MAX_PATHNAME_LEN 1024
49 /* check if a file name is for an executable file (.exe or .com) */
50 inline static BOOL is_executable( const WCHAR *name )
52 static const WCHAR exeW[] = {'.','e','x','e',0};
53 static const WCHAR comW[] = {'.','c','o','m',0};
54 int len = strlenW(name);
56 if (len < 4) return FALSE;
57 return (!strcmpiW( name + len - 4, exeW ) || !strcmpiW( name + len - 4, comW ));
60 /***********************************************************************
61 * copy_filename_WtoA
63 * copy a file name back to OEM/Ansi, but only if the buffer is large enough
65 static DWORD copy_filename_WtoA( LPCWSTR nameW, LPSTR buffer, DWORD len )
67 UNICODE_STRING strW;
68 DWORD ret;
69 BOOL is_ansi = AreFileApisANSI();
71 RtlInitUnicodeString( &strW, nameW );
73 ret = is_ansi ? RtlUnicodeStringToAnsiSize(&strW) : RtlUnicodeStringToOemSize(&strW);
74 if (buffer && ret <= len)
76 ANSI_STRING str;
78 str.Buffer = buffer;
79 str.MaximumLength = len;
80 if (is_ansi)
81 RtlUnicodeStringToAnsiString( &str, &strW, FALSE );
82 else
83 RtlUnicodeStringToOemString( &str, &strW, FALSE );
84 ret = str.Length; /* length without terminating 0 */
86 return ret;
89 /***********************************************************************
90 * add_boot_rename_entry
92 * Adds an entry to the registry that is loaded when windows boots and
93 * checks if there are some files to be removed or renamed/moved.
94 * <fn1> has to be valid and <fn2> may be NULL. If both pointers are
95 * non-NULL then the file is moved, otherwise it is deleted. The
96 * entry of the registrykey is always appended with two zero
97 * terminated strings. If <fn2> is NULL then the second entry is
98 * simply a single 0-byte. Otherwise the second filename goes
99 * there. The entries are prepended with \??\ before the path and the
100 * second filename gets also a '!' as the first character if
101 * MOVEFILE_REPLACE_EXISTING is set. After the final string another
102 * 0-byte follows to indicate the end of the strings.
103 * i.e.:
104 * \??\D:\test\file1[0]
105 * !\??\D:\test\file1_renamed[0]
106 * \??\D:\Test|delete[0]
107 * [0] <- file is to be deleted, second string empty
108 * \??\D:\test\file2[0]
109 * !\??\D:\test\file2_renamed[0]
110 * [0] <- indicates end of strings
112 * or:
113 * \??\D:\test\file1[0]
114 * !\??\D:\test\file1_renamed[0]
115 * \??\D:\Test|delete[0]
116 * [0] <- file is to be deleted, second string empty
117 * [0] <- indicates end of strings
120 static BOOL add_boot_rename_entry( LPCWSTR source, LPCWSTR dest, DWORD flags )
122 static const WCHAR ValueName[] = {'P','e','n','d','i','n','g',
123 'F','i','l','e','R','e','n','a','m','e',
124 'O','p','e','r','a','t','i','o','n','s',0};
125 static const WCHAR SessionW[] = {'M','a','c','h','i','n','e','\\',
126 'S','y','s','t','e','m','\\',
127 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
128 'C','o','n','t','r','o','l','\\',
129 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
130 static const int info_size = FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION, Data );
132 OBJECT_ATTRIBUTES attr;
133 UNICODE_STRING nameW, source_name, dest_name;
134 KEY_VALUE_PARTIAL_INFORMATION *info;
135 BOOL rc = FALSE;
136 HANDLE Reboot = 0;
137 DWORD len1, len2;
138 DWORD DataSize = 0;
139 BYTE *Buffer = NULL;
140 WCHAR *p;
142 if (!RtlDosPathNameToNtPathName_U( source, &source_name, NULL, NULL ))
144 SetLastError( ERROR_PATH_NOT_FOUND );
145 return FALSE;
147 dest_name.Buffer = NULL;
148 if (dest && !RtlDosPathNameToNtPathName_U( dest, &dest_name, NULL, NULL ))
150 RtlFreeUnicodeString( &source_name );
151 SetLastError( ERROR_PATH_NOT_FOUND );
152 return FALSE;
155 attr.Length = sizeof(attr);
156 attr.RootDirectory = 0;
157 attr.ObjectName = &nameW;
158 attr.Attributes = 0;
159 attr.SecurityDescriptor = NULL;
160 attr.SecurityQualityOfService = NULL;
161 RtlInitUnicodeString( &nameW, SessionW );
163 if (NtCreateKey( &Reboot, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
165 WARN("Error creating key for reboot managment [%s]\n",
166 "SYSTEM\\CurrentControlSet\\Control\\Session Manager");
167 RtlFreeUnicodeString( &source_name );
168 RtlFreeUnicodeString( &dest_name );
169 return FALSE;
172 len1 = source_name.Length + sizeof(WCHAR);
173 if (dest)
175 len2 = dest_name.Length + sizeof(WCHAR);
176 if (flags & MOVEFILE_REPLACE_EXISTING)
177 len2 += sizeof(WCHAR); /* Plus 1 because of the leading '!' */
179 else len2 = sizeof(WCHAR); /* minimum is the 0 characters for the empty second string */
181 RtlInitUnicodeString( &nameW, ValueName );
183 /* First we check if the key exists and if so how many bytes it already contains. */
184 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
185 NULL, 0, &DataSize ) == STATUS_BUFFER_OVERFLOW)
187 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
188 goto Quit;
189 if (NtQueryValueKey( Reboot, &nameW, KeyValuePartialInformation,
190 Buffer, DataSize, &DataSize )) goto Quit;
191 info = (KEY_VALUE_PARTIAL_INFORMATION *)Buffer;
192 if (info->Type != REG_MULTI_SZ) goto Quit;
193 if (DataSize > sizeof(info)) DataSize -= sizeof(WCHAR); /* remove terminating null (will be added back later) */
195 else
197 DataSize = info_size;
198 if (!(Buffer = HeapAlloc( GetProcessHeap(), 0, DataSize + len1 + len2 + sizeof(WCHAR) )))
199 goto Quit;
202 memcpy( Buffer + DataSize, source_name.Buffer, len1 );
203 DataSize += len1;
204 p = (WCHAR *)(Buffer + DataSize);
205 if (dest)
207 if (flags & MOVEFILE_REPLACE_EXISTING)
208 *p++ = '!';
209 memcpy( p, dest_name.Buffer, len2 );
210 DataSize += len2;
212 else
214 *p = 0;
215 DataSize += sizeof(WCHAR);
218 /* add final null */
219 p = (WCHAR *)(Buffer + DataSize);
220 *p = 0;
221 DataSize += sizeof(WCHAR);
223 rc = !NtSetValueKey(Reboot, &nameW, 0, REG_MULTI_SZ, Buffer + info_size, DataSize - info_size);
225 Quit:
226 RtlFreeUnicodeString( &source_name );
227 RtlFreeUnicodeString( &dest_name );
228 if (Reboot) NtClose(Reboot);
229 HeapFree( GetProcessHeap(), 0, Buffer );
230 return(rc);
234 /***********************************************************************
235 * GetFullPathNameW (KERNEL32.@)
236 * NOTES
237 * if the path closed with '\', *lastpart is 0
239 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
240 LPWSTR *lastpart )
242 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
245 /***********************************************************************
246 * GetFullPathNameA (KERNEL32.@)
247 * NOTES
248 * if the path closed with '\', *lastpart is 0
250 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
251 LPSTR *lastpart )
253 WCHAR *nameW;
254 WCHAR bufferW[MAX_PATH];
255 DWORD ret;
257 if (!(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
259 ret = GetFullPathNameW( nameW, MAX_PATH, bufferW, NULL);
261 if (!ret) return 0;
262 if (ret > MAX_PATH)
264 SetLastError(ERROR_FILENAME_EXCED_RANGE);
265 return 0;
267 ret = copy_filename_WtoA( bufferW, buffer, len );
268 if (ret < len && lastpart)
270 LPSTR p = buffer + strlen(buffer) - 1;
272 if (*p != '\\')
274 while ((p > buffer + 2) && (*p != '\\')) p--;
275 *lastpart = p + 1;
277 else *lastpart = NULL;
279 return ret;
283 /***********************************************************************
284 * GetLongPathNameW (KERNEL32.@)
286 * NOTES
287 * observed (Win2000):
288 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
289 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
291 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
293 WCHAR tmplongpath[MAX_PATHNAME_LEN];
294 LPCWSTR p;
295 DWORD sp = 0, lp = 0;
296 DWORD tmplen;
297 BOOL unixabsolute = (shortpath[0] == '/');
298 WIN32_FIND_DATAW wfd;
299 HANDLE goit;
301 if (!shortpath)
303 SetLastError(ERROR_INVALID_PARAMETER);
304 return 0;
306 if (!shortpath[0])
308 SetLastError(ERROR_PATH_NOT_FOUND);
309 return 0;
312 TRACE("%s,%p,%ld\n", debugstr_w(shortpath), longpath, longlen);
314 if (shortpath[0] == '\\' && shortpath[1] == '\\')
316 ERR("UNC pathname %s\n", debugstr_w(shortpath));
317 lstrcpynW( longpath, shortpath, longlen );
318 return strlenW(longpath);
321 /* check for drive letter */
322 if (!unixabsolute && shortpath[1] == ':' )
324 tmplongpath[0] = shortpath[0];
325 tmplongpath[1] = ':';
326 lp = sp = 2;
329 while (shortpath[sp])
331 /* check for path delimiters and reproduce them */
332 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
334 if (!lp || tmplongpath[lp-1] != '\\')
336 /* strip double "\\" */
337 tmplongpath[lp++] = '\\';
339 tmplongpath[lp] = 0; /* terminate string */
340 sp++;
341 continue;
344 p = shortpath + sp;
345 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
347 tmplongpath[lp++] = *p++;
348 tmplongpath[lp++] = *p++;
350 for (; *p && *p != '/' && *p != '\\'; p++);
351 tmplen = p - (shortpath + sp);
352 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
353 /* Check if the file exists and use the existing file name */
354 goit = FindFirstFileW(tmplongpath, &wfd);
355 if (goit == INVALID_HANDLE_VALUE)
357 TRACE("not found %s!\n", debugstr_w(tmplongpath));
358 SetLastError ( ERROR_FILE_NOT_FOUND );
359 return 0;
361 FindClose(goit);
362 strcpyW(tmplongpath + lp, wfd.cFileName);
363 lp += strlenW(tmplongpath + lp);
364 sp += tmplen;
366 tmplen = strlenW(shortpath) - 1;
367 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
368 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
369 tmplongpath[lp++] = shortpath[tmplen];
370 tmplongpath[lp] = 0;
372 tmplen = strlenW(tmplongpath) + 1;
373 if (tmplen <= longlen)
375 strcpyW(longpath, tmplongpath);
376 TRACE("returning %s\n", debugstr_w(longpath));
377 tmplen--; /* length without 0 */
380 return tmplen;
383 /***********************************************************************
384 * GetLongPathNameA (KERNEL32.@)
386 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
388 WCHAR *shortpathW;
389 WCHAR longpathW[MAX_PATH];
390 DWORD ret;
392 TRACE("%s\n", debugstr_a(shortpath));
394 if (!(shortpathW = FILE_name_AtoW( shortpath, FALSE ))) return 0;
396 ret = GetLongPathNameW(shortpathW, longpathW, MAX_PATH);
398 if (!ret) return 0;
399 if (ret > MAX_PATH)
401 SetLastError(ERROR_FILENAME_EXCED_RANGE);
402 return 0;
404 return copy_filename_WtoA( longpathW, longpath, longlen );
408 /***********************************************************************
409 * GetShortPathNameW (KERNEL32.@)
411 * NOTES
412 * observed:
413 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
414 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
416 * more observations ( with NT 3.51 (WinDD) ):
417 * longpath <= 8.3 -> just copy longpath to shortpath
418 * longpath > 8.3 ->
419 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
420 * b) file does exist -> set the short filename.
421 * - trailing slashes are reproduced in the short name, even if the
422 * file is not a directory
423 * - the absolute/relative path of the short name is reproduced like found
424 * in the long name
425 * - longpath and shortpath may have the same address
426 * Peter Ganten, 1999
428 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
430 WCHAR tmpshortpath[MAX_PATHNAME_LEN];
431 LPCWSTR p;
432 DWORD sp = 0, lp = 0;
433 DWORD tmplen;
434 WIN32_FIND_DATAW wfd;
435 HANDLE goit;
436 UNICODE_STRING ustr;
437 WCHAR ustr_buf[8+1+3+1];
439 TRACE("%s\n", debugstr_w(longpath));
441 if (!longpath)
443 SetLastError(ERROR_INVALID_PARAMETER);
444 return 0;
446 if (!longpath[0])
448 SetLastError(ERROR_BAD_PATHNAME);
449 return 0;
452 /* check for drive letter */
453 if (longpath[0] != '/' && longpath[1] == ':' )
455 tmpshortpath[0] = longpath[0];
456 tmpshortpath[1] = ':';
457 sp = lp = 2;
460 ustr.Buffer = ustr_buf;
461 ustr.Length = 0;
462 ustr.MaximumLength = sizeof(ustr_buf);
464 while (longpath[lp])
466 /* check for path delimiters and reproduce them */
467 if (longpath[lp] == '\\' || longpath[lp] == '/')
469 if (!sp || tmpshortpath[sp-1] != '\\')
471 /* strip double "\\" */
472 tmpshortpath[sp] = '\\';
473 sp++;
475 tmpshortpath[sp] = 0; /* terminate string */
476 lp++;
477 continue;
480 for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
481 tmplen = p - (longpath + lp);
482 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
483 /* Check, if the current element is a valid dos name */
484 if (tmplen <= 8+1+3)
486 BOOLEAN spaces;
487 memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
488 ustr_buf[tmplen] = '\0';
489 ustr.Length = tmplen * sizeof(WCHAR);
490 if (RtlIsNameLegalDOS8Dot3(&ustr, NULL, &spaces) && !spaces)
492 sp += tmplen;
493 lp += tmplen;
494 continue;
498 /* Check if the file exists and use the existing short file name */
499 goit = FindFirstFileW(tmpshortpath, &wfd);
500 if (goit == INVALID_HANDLE_VALUE) goto notfound;
501 FindClose(goit);
502 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
503 sp += strlenW(tmpshortpath + sp);
504 lp += tmplen;
506 tmpshortpath[sp] = 0;
508 tmplen = strlenW(tmpshortpath) + 1;
509 if (tmplen <= shortlen)
511 strcpyW(shortpath, tmpshortpath);
512 TRACE("returning %s\n", debugstr_w(shortpath));
513 tmplen--; /* length without 0 */
516 return tmplen;
518 notfound:
519 TRACE("not found!\n" );
520 SetLastError ( ERROR_FILE_NOT_FOUND );
521 return 0;
524 /***********************************************************************
525 * GetShortPathNameA (KERNEL32.@)
527 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
529 WCHAR *longpathW;
530 WCHAR shortpathW[MAX_PATH];
531 DWORD ret;
533 TRACE("%s\n", debugstr_a(longpath));
535 if (!(longpathW = FILE_name_AtoW( longpath, FALSE ))) return 0;
537 ret = GetShortPathNameW(longpathW, shortpathW, MAX_PATH);
539 if (!ret) return 0;
540 if (ret > MAX_PATH)
542 SetLastError(ERROR_FILENAME_EXCED_RANGE);
543 return 0;
545 return copy_filename_WtoA( shortpathW, shortpath, shortlen );
549 /***********************************************************************
550 * GetTempPathA (KERNEL32.@)
552 DWORD WINAPI GetTempPathA( DWORD count, LPSTR path )
554 WCHAR pathW[MAX_PATH];
555 UINT ret;
557 ret = GetTempPathW(MAX_PATH, pathW);
559 if (!ret)
560 return 0;
562 if (ret > MAX_PATH)
564 SetLastError(ERROR_FILENAME_EXCED_RANGE);
565 return 0;
567 return copy_filename_WtoA( pathW, path, count );
571 /***********************************************************************
572 * GetTempPathW (KERNEL32.@)
574 DWORD WINAPI GetTempPathW( DWORD count, LPWSTR path )
576 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
577 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
578 WCHAR tmp_path[MAX_PATH];
579 UINT ret;
581 TRACE("%lu,%p\n", count, path);
583 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
584 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
585 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
586 return 0;
588 if (ret > MAX_PATH)
590 SetLastError(ERROR_FILENAME_EXCED_RANGE);
591 return 0;
594 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
595 if (!ret) return 0;
597 if (ret > MAX_PATH - 2)
599 SetLastError(ERROR_FILENAME_EXCED_RANGE);
600 return 0;
603 if (tmp_path[ret-1] != '\\')
605 tmp_path[ret++] = '\\';
606 tmp_path[ret] = '\0';
609 ret++; /* add space for terminating 0 */
611 if (count)
613 lstrcpynW(path, tmp_path, count);
614 if (count >= ret)
615 ret--; /* return length without 0 */
616 else if (count < 4)
617 path[0] = 0; /* avoid returning ambiguous "X:" */
620 TRACE("returning %u, %s\n", ret, debugstr_w(path));
621 return ret;
625 /***********************************************************************
626 * GetTempFileNameA (KERNEL32.@)
628 UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique, LPSTR buffer)
630 WCHAR *pathW, *prefixW = NULL;
631 WCHAR bufferW[MAX_PATH];
632 UINT ret;
634 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return 0;
635 if (prefix && !(prefixW = FILE_name_AtoW( prefix, TRUE ))) return 0;
637 ret = GetTempFileNameW(pathW, prefixW, unique, bufferW);
638 if (ret) FILE_name_WtoA( bufferW, -1, buffer, MAX_PATH );
640 HeapFree( GetProcessHeap(), 0, prefixW );
641 return ret;
644 /***********************************************************************
645 * GetTempFileNameW (KERNEL32.@)
647 UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique, LPWSTR buffer )
649 static const WCHAR formatW[] = {'%','x','.','t','m','p',0};
651 int i;
652 LPWSTR p;
654 if ( !path || !buffer )
656 SetLastError( ERROR_INVALID_PARAMETER );
657 return 0;
660 strcpyW( buffer, path );
661 p = buffer + strlenW(buffer);
663 /* add a \, if there isn't one */
664 if ((p == buffer) || (p[-1] != '\\')) *p++ = '\\';
666 if (prefix)
667 for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
669 unique &= 0xffff;
671 if (unique) sprintfW( p, formatW, unique );
672 else
674 /* get a "random" unique number and try to create the file */
675 HANDLE handle;
676 UINT num = GetTickCount() & 0xffff;
678 if (!num) num = 1;
679 unique = num;
682 sprintfW( p, formatW, unique );
683 handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
684 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
685 if (handle != INVALID_HANDLE_VALUE)
686 { /* We created it */
687 TRACE("created %s\n", debugstr_w(buffer) );
688 CloseHandle( handle );
689 break;
691 if (GetLastError() != ERROR_FILE_EXISTS &&
692 GetLastError() != ERROR_SHARING_VIOLATION)
693 break; /* No need to go on */
694 if (!(++unique & 0xffff)) unique = 1;
695 } while (unique != num);
698 TRACE("returning %s\n", debugstr_w(buffer) );
699 return unique;
703 /***********************************************************************
704 * contains_pathW
706 * Check if the file name contains a path; helper for SearchPathW.
707 * A relative path is not considered a path unless it starts with ./ or ../
709 inline static BOOL contains_pathW (LPCWSTR name)
711 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
712 if (name[0] != '.') return FALSE;
713 if (name[1] == '/' || name[1] == '\\') return TRUE;
714 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
718 /***********************************************************************
719 * SearchPathW [KERNEL32.@]
721 * Searches for a specified file in the search path.
723 * PARAMS
724 * path [I] Path to search (NULL means default)
725 * name [I] Filename to search for.
726 * ext [I] File extension to append to file name. The first
727 * character must be a period. This parameter is
728 * specified only if the filename given does not
729 * contain an extension.
730 * buflen [I] size of buffer, in characters
731 * buffer [O] buffer for found filename
732 * lastpart [O] address of pointer to last used character in
733 * buffer (the final '\')
735 * RETURNS
736 * Success: length of string copied into buffer, not including
737 * terminating null character. If the filename found is
738 * longer than the length of the buffer, the length of the
739 * filename is returned.
740 * Failure: Zero
742 * NOTES
743 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
744 * (tested on NT 4.0)
746 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
747 LPWSTR buffer, LPWSTR *lastpart )
749 DWORD ret = 0;
751 /* If the name contains an explicit path, ignore the path */
753 if (contains_pathW(name))
755 /* try first without extension */
756 if (RtlDoesFileExists_U( name ))
757 return GetFullPathNameW( name, buflen, buffer, lastpart );
759 if (ext)
761 LPCWSTR p = strrchrW( name, '.' );
762 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
763 ext = NULL; /* Ignore the specified extension */
766 /* Allocate a buffer for the file name and extension */
767 if (ext)
769 LPWSTR tmp;
770 DWORD len = strlenW(name) + strlenW(ext);
772 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
774 SetLastError( ERROR_OUTOFMEMORY );
775 return 0;
777 strcpyW( tmp, name );
778 strcatW( tmp, ext );
779 if (RtlDoesFileExists_U( tmp ))
780 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
781 HeapFree( GetProcessHeap(), 0, tmp );
784 else if (path && path[0]) /* search in the specified path */
786 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
787 buffer, lastpart ) / sizeof(WCHAR);
789 else /* search in the default path */
791 WCHAR *dll_path = MODULE_get_dll_load_path( NULL );
793 if (dll_path)
795 ret = RtlDosSearchPath_U( dll_path, name, ext, buflen * sizeof(WCHAR),
796 buffer, lastpart ) / sizeof(WCHAR);
797 HeapFree( GetProcessHeap(), 0, dll_path );
799 else
801 SetLastError( ERROR_OUTOFMEMORY );
802 return 0;
806 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
807 else TRACE( "found %s\n", debugstr_w(buffer) );
808 return ret;
812 /***********************************************************************
813 * SearchPathA (KERNEL32.@)
815 * See SearchPathW.
817 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
818 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
820 WCHAR *pathW = NULL, *nameW = NULL, *extW = NULL;
821 WCHAR bufferW[MAX_PATH];
822 DWORD ret;
824 if (!name || !(nameW = FILE_name_AtoW( name, FALSE ))) return 0;
825 if (path && !(pathW = FILE_name_AtoW( path, TRUE ))) return 0;
827 if (ext && !(extW = FILE_name_AtoW( ext, TRUE )))
829 HeapFree( GetProcessHeap(), 0, pathW );
830 return 0;
833 ret = SearchPathW(pathW, nameW, extW, MAX_PATH, bufferW, NULL);
835 HeapFree( GetProcessHeap(), 0, pathW );
836 HeapFree( GetProcessHeap(), 0, extW );
838 if (!ret) return 0;
839 if (ret > MAX_PATH)
841 SetLastError(ERROR_FILENAME_EXCED_RANGE);
842 return 0;
844 ret = copy_filename_WtoA( bufferW, buffer, buflen );
845 if (buflen > ret && lastpart)
846 *lastpart = strrchr(buffer, '\\') + 1;
847 return ret;
851 /**************************************************************************
852 * CopyFileW (KERNEL32.@)
854 BOOL WINAPI CopyFileW( LPCWSTR source, LPCWSTR dest, BOOL fail_if_exists )
856 static const int buffer_size = 65536;
857 HANDLE h1, h2;
858 BY_HANDLE_FILE_INFORMATION info;
859 DWORD count;
860 BOOL ret = FALSE;
861 char *buffer;
863 if (!source || !dest)
865 SetLastError(ERROR_INVALID_PARAMETER);
866 return FALSE;
868 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size )))
870 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
871 return FALSE;
874 TRACE("%s -> %s\n", debugstr_w(source), debugstr_w(dest));
876 if ((h1 = CreateFileW(source, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
877 NULL, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE)
879 WARN("Unable to open source %s\n", debugstr_w(source));
880 return FALSE;
883 if (!GetFileInformationByHandle( h1, &info ))
885 WARN("GetFileInformationByHandle returned error for %s\n", debugstr_w(source));
886 CloseHandle( h1 );
887 return FALSE;
890 if ((h2 = CreateFileW( dest, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
891 fail_if_exists ? CREATE_NEW : CREATE_ALWAYS,
892 info.dwFileAttributes, h1 )) == INVALID_HANDLE_VALUE)
894 WARN("Unable to open dest %s\n", debugstr_w(dest));
895 CloseHandle( h1 );
896 return FALSE;
899 while (ReadFile( h1, buffer, buffer_size, &count, NULL ) && count)
901 char *p = buffer;
902 while (count != 0)
904 DWORD res;
905 if (!WriteFile( h2, p, count, &res, NULL ) || !res) goto done;
906 p += res;
907 count -= res;
910 ret = TRUE;
911 done:
912 /* Maintain the timestamp of source file to destination file */
913 SetFileTime(h2, NULL, NULL, &info.ftLastWriteTime);
914 HeapFree( GetProcessHeap(), 0, buffer );
915 CloseHandle( h1 );
916 CloseHandle( h2 );
917 return ret;
921 /**************************************************************************
922 * CopyFileA (KERNEL32.@)
924 BOOL WINAPI CopyFileA( LPCSTR source, LPCSTR dest, BOOL fail_if_exists)
926 WCHAR *sourceW, *destW;
927 BOOL ret;
929 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
930 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
932 ret = CopyFileW( sourceW, destW, fail_if_exists );
934 HeapFree( GetProcessHeap(), 0, destW );
935 return ret;
939 /**************************************************************************
940 * CopyFileExW (KERNEL32.@)
942 * This implementation ignores most of the extra parameters passed-in into
943 * the "ex" version of the method and calls the CopyFile method.
944 * It will have to be fixed eventually.
946 BOOL WINAPI CopyFileExW(LPCWSTR sourceFilename, LPCWSTR destFilename,
947 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
948 LPBOOL cancelFlagPointer, DWORD copyFlags)
951 * Interpret the only flag that CopyFile can interpret.
953 return CopyFileW(sourceFilename, destFilename, (copyFlags & COPY_FILE_FAIL_IF_EXISTS) != 0);
957 /**************************************************************************
958 * CopyFileExA (KERNEL32.@)
960 BOOL WINAPI CopyFileExA(LPCSTR sourceFilename, LPCSTR destFilename,
961 LPPROGRESS_ROUTINE progressRoutine, LPVOID appData,
962 LPBOOL cancelFlagPointer, DWORD copyFlags)
964 WCHAR *sourceW, *destW;
965 BOOL ret;
967 /* can't use the TEB buffer since we may have a callback routine */
968 if (!(sourceW = FILE_name_AtoW( sourceFilename, TRUE ))) return FALSE;
969 if (!(destW = FILE_name_AtoW( destFilename, TRUE )))
971 HeapFree( GetProcessHeap(), 0, sourceW );
972 return FALSE;
974 ret = CopyFileExW(sourceW, destW, progressRoutine, appData,
975 cancelFlagPointer, copyFlags);
976 HeapFree( GetProcessHeap(), 0, sourceW );
977 HeapFree( GetProcessHeap(), 0, destW );
978 return ret;
982 /**************************************************************************
983 * MoveFileExW (KERNEL32.@)
985 BOOL WINAPI MoveFileExW( LPCWSTR source, LPCWSTR dest, DWORD flag )
987 FILE_BASIC_INFORMATION info;
988 UNICODE_STRING nt_name;
989 OBJECT_ATTRIBUTES attr;
990 IO_STATUS_BLOCK io;
991 NTSTATUS status;
992 HANDLE source_handle = 0, dest_handle;
993 ANSI_STRING source_unix, dest_unix;
995 TRACE("(%s,%s,%04lx)\n", debugstr_w(source), debugstr_w(dest), flag);
997 if (flag & MOVEFILE_DELAY_UNTIL_REBOOT)
998 return add_boot_rename_entry( source, dest, flag );
1000 if (!dest)
1001 return DeleteFileW( source );
1003 /* check if we are allowed to rename the source */
1005 if (!RtlDosPathNameToNtPathName_U( source, &nt_name, NULL, NULL ))
1007 SetLastError( ERROR_PATH_NOT_FOUND );
1008 return FALSE;
1010 source_unix.Buffer = NULL;
1011 dest_unix.Buffer = NULL;
1012 attr.Length = sizeof(attr);
1013 attr.RootDirectory = 0;
1014 attr.Attributes = OBJ_CASE_INSENSITIVE;
1015 attr.ObjectName = &nt_name;
1016 attr.SecurityDescriptor = NULL;
1017 attr.SecurityQualityOfService = NULL;
1019 status = NtOpenFile( &source_handle, 0, &attr, &io, 0, FILE_SYNCHRONOUS_IO_NONALERT );
1020 if (status == STATUS_SUCCESS)
1021 status = wine_nt_to_unix_file_name( &nt_name, &source_unix, FILE_OPEN, FALSE );
1022 RtlFreeUnicodeString( &nt_name );
1023 if (status != STATUS_SUCCESS)
1025 SetLastError( RtlNtStatusToDosError(status) );
1026 goto error;
1028 status = NtQueryInformationFile( source_handle, &io, &info, sizeof(info), FileBasicInformation );
1029 if (status != STATUS_SUCCESS)
1031 SetLastError( RtlNtStatusToDosError(status) );
1032 goto error;
1035 if (info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1037 if (flag & MOVEFILE_REPLACE_EXISTING) /* cannot replace directory */
1039 SetLastError( ERROR_INVALID_PARAMETER );
1040 goto error;
1044 /* we must have write access to the destination, and it must */
1045 /* not exist except if MOVEFILE_REPLACE_EXISTING is set */
1047 if (!RtlDosPathNameToNtPathName_U( dest, &nt_name, NULL, NULL ))
1049 SetLastError( ERROR_PATH_NOT_FOUND );
1050 goto error;
1052 status = NtOpenFile( &dest_handle, GENERIC_READ | GENERIC_WRITE, &attr, &io, 0,
1053 FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1054 if (status == STATUS_SUCCESS)
1056 NtClose( dest_handle );
1057 if (!(flag & MOVEFILE_REPLACE_EXISTING))
1059 SetLastError( ERROR_ALREADY_EXISTS );
1060 RtlFreeUnicodeString( &nt_name );
1061 goto error;
1064 else if (status != STATUS_OBJECT_NAME_NOT_FOUND)
1066 SetLastError( RtlNtStatusToDosError(status) );
1067 RtlFreeUnicodeString( &nt_name );
1068 goto error;
1071 status = wine_nt_to_unix_file_name( &nt_name, &dest_unix, FILE_OPEN_IF, FALSE );
1072 RtlFreeUnicodeString( &nt_name );
1073 if (status != STATUS_SUCCESS && status != STATUS_NO_SUCH_FILE)
1075 SetLastError( RtlNtStatusToDosError(status) );
1076 goto error;
1079 /* now perform the rename */
1081 if (rename( source_unix.Buffer, dest_unix.Buffer ) == -1)
1083 if (errno == EXDEV && (flag & MOVEFILE_COPY_ALLOWED))
1085 NtClose( source_handle );
1086 RtlFreeAnsiString( &source_unix );
1087 RtlFreeAnsiString( &dest_unix );
1088 return (CopyFileW( source, dest, TRUE ) && DeleteFileW( source ));
1090 FILE_SetDosError();
1091 /* if we created the destination, remove it */
1092 if (io.Information == FILE_CREATED) unlink( dest_unix.Buffer );
1093 goto error;
1096 /* fixup executable permissions */
1098 if (is_executable( source ) != is_executable( dest ))
1100 struct stat fstat;
1101 if (stat( dest_unix.Buffer, &fstat ) != -1)
1103 if (is_executable( dest ))
1104 /* set executable bit where read bit is set */
1105 fstat.st_mode |= (fstat.st_mode & 0444) >> 2;
1106 else
1107 fstat.st_mode &= ~0111;
1108 chmod( dest_unix.Buffer, fstat.st_mode );
1112 NtClose( source_handle );
1113 RtlFreeAnsiString( &source_unix );
1114 RtlFreeAnsiString( &dest_unix );
1115 return TRUE;
1117 error:
1118 if (source_handle) NtClose( source_handle );
1119 RtlFreeAnsiString( &source_unix );
1120 RtlFreeAnsiString( &dest_unix );
1121 return FALSE;
1124 /**************************************************************************
1125 * MoveFileExA (KERNEL32.@)
1127 BOOL WINAPI MoveFileExA( LPCSTR source, LPCSTR dest, DWORD flag )
1129 WCHAR *sourceW, *destW;
1130 BOOL ret;
1132 if (!(sourceW = FILE_name_AtoW( source, FALSE ))) return FALSE;
1133 if (dest)
1135 if (!(destW = FILE_name_AtoW( dest, TRUE ))) return FALSE;
1137 else
1138 destW = NULL;
1140 ret = MoveFileExW( sourceW, destW, flag );
1141 HeapFree( GetProcessHeap(), 0, destW );
1142 return ret;
1146 /**************************************************************************
1147 * MoveFileW (KERNEL32.@)
1149 * Move file or directory
1151 BOOL WINAPI MoveFileW( LPCWSTR source, LPCWSTR dest )
1153 return MoveFileExW( source, dest, MOVEFILE_COPY_ALLOWED );
1157 /**************************************************************************
1158 * MoveFileA (KERNEL32.@)
1160 BOOL WINAPI MoveFileA( LPCSTR source, LPCSTR dest )
1162 return MoveFileExA( source, dest, MOVEFILE_COPY_ALLOWED );
1166 /***********************************************************************
1167 * CreateDirectoryW (KERNEL32.@)
1168 * RETURNS:
1169 * TRUE : success
1170 * FALSE : failure
1171 * ERROR_DISK_FULL: on full disk
1172 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
1173 * ERROR_ACCESS_DENIED: on permission problems
1174 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
1176 BOOL WINAPI CreateDirectoryW( LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1178 OBJECT_ATTRIBUTES attr;
1179 UNICODE_STRING nt_name;
1180 IO_STATUS_BLOCK io;
1181 NTSTATUS status;
1182 HANDLE handle;
1183 BOOL ret = FALSE;
1185 TRACE( "%s\n", debugstr_w(path) );
1187 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1189 SetLastError( ERROR_PATH_NOT_FOUND );
1190 return FALSE;
1192 attr.Length = sizeof(attr);
1193 attr.RootDirectory = 0;
1194 attr.Attributes = OBJ_CASE_INSENSITIVE;
1195 attr.ObjectName = &nt_name;
1196 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1197 attr.SecurityQualityOfService = NULL;
1199 status = NtCreateFile( &handle, GENERIC_READ, &attr, &io, NULL,
1200 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
1201 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0 );
1203 if (status == STATUS_SUCCESS)
1205 NtClose( handle );
1206 ret = TRUE;
1208 else SetLastError( RtlNtStatusToDosError(status) );
1210 RtlFreeUnicodeString( &nt_name );
1211 return ret;
1215 /***********************************************************************
1216 * CreateDirectoryA (KERNEL32.@)
1218 BOOL WINAPI CreateDirectoryA( LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1220 WCHAR *pathW;
1222 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1223 return CreateDirectoryW( pathW, sa );
1227 /***********************************************************************
1228 * CreateDirectoryExA (KERNEL32.@)
1230 BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path, LPSECURITY_ATTRIBUTES sa )
1232 WCHAR *pathW, *templateW = NULL;
1233 BOOL ret;
1235 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1236 if (template && !(templateW = FILE_name_AtoW( template, TRUE ))) return FALSE;
1238 ret = CreateDirectoryExW( templateW, pathW, sa );
1239 HeapFree( GetProcessHeap(), 0, templateW );
1240 return ret;
1244 /***********************************************************************
1245 * CreateDirectoryExW (KERNEL32.@)
1247 BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path, LPSECURITY_ATTRIBUTES sa )
1249 return CreateDirectoryW( path, sa );
1253 /***********************************************************************
1254 * RemoveDirectoryW (KERNEL32.@)
1256 BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
1258 OBJECT_ATTRIBUTES attr;
1259 UNICODE_STRING nt_name;
1260 ANSI_STRING unix_name;
1261 IO_STATUS_BLOCK io;
1262 NTSTATUS status;
1263 HANDLE handle;
1264 BOOL ret = FALSE;
1266 TRACE( "%s\n", debugstr_w(path) );
1268 if (!RtlDosPathNameToNtPathName_U( path, &nt_name, NULL, NULL ))
1270 SetLastError( ERROR_PATH_NOT_FOUND );
1271 return FALSE;
1273 attr.Length = sizeof(attr);
1274 attr.RootDirectory = 0;
1275 attr.Attributes = OBJ_CASE_INSENSITIVE;
1276 attr.ObjectName = &nt_name;
1277 attr.SecurityDescriptor = NULL;
1278 attr.SecurityQualityOfService = NULL;
1280 status = NtOpenFile( &handle, DELETE, &attr, &io,
1281 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1282 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT );
1283 if (status == STATUS_SUCCESS)
1284 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN, FALSE );
1285 RtlFreeUnicodeString( &nt_name );
1287 if (status != STATUS_SUCCESS)
1289 SetLastError( RtlNtStatusToDosError(status) );
1290 return FALSE;
1293 if (!(ret = (rmdir( unix_name.Buffer ) != -1))) FILE_SetDosError();
1294 RtlFreeAnsiString( &unix_name );
1295 NtClose( handle );
1296 return ret;
1300 /***********************************************************************
1301 * RemoveDirectoryA (KERNEL32.@)
1303 BOOL WINAPI RemoveDirectoryA( LPCSTR path )
1305 WCHAR *pathW;
1307 if (!(pathW = FILE_name_AtoW( path, FALSE ))) return FALSE;
1308 return RemoveDirectoryW( pathW );
1312 /***********************************************************************
1313 * GetCurrentDirectoryW (KERNEL32.@)
1315 UINT WINAPI GetCurrentDirectoryW( UINT buflen, LPWSTR buf )
1317 return RtlGetCurrentDirectory_U( buflen * sizeof(WCHAR), buf ) / sizeof(WCHAR);
1321 /***********************************************************************
1322 * GetCurrentDirectoryA (KERNEL32.@)
1324 UINT WINAPI GetCurrentDirectoryA( UINT buflen, LPSTR buf )
1326 WCHAR bufferW[MAX_PATH];
1327 DWORD ret;
1329 if (buflen && buf && !HIWORD(buf))
1331 /* Win9x catches access violations here, returning zero.
1332 * This behaviour resulted in some people not noticing
1333 * that they got the argument order wrong. So let's be
1334 * nice and fail gracefully if buf is invalid and looks
1335 * more like a buflen (which is probably MAX_PATH). */
1336 SetLastError(ERROR_INVALID_PARAMETER);
1337 return 0;
1340 ret = GetCurrentDirectoryW(MAX_PATH, bufferW);
1342 if (!ret) return 0;
1343 if (ret > MAX_PATH)
1345 SetLastError(ERROR_FILENAME_EXCED_RANGE);
1346 return 0;
1348 return copy_filename_WtoA( bufferW, buf, buflen );
1352 /***********************************************************************
1353 * SetCurrentDirectoryW (KERNEL32.@)
1355 BOOL WINAPI SetCurrentDirectoryW( LPCWSTR dir )
1357 UNICODE_STRING dirW;
1358 NTSTATUS status;
1360 RtlInitUnicodeString( &dirW, dir );
1361 status = RtlSetCurrentDirectory_U( &dirW );
1362 if (status != STATUS_SUCCESS)
1364 SetLastError( RtlNtStatusToDosError(status) );
1365 return FALSE;
1367 return TRUE;
1371 /***********************************************************************
1372 * SetCurrentDirectoryA (KERNEL32.@)
1374 BOOL WINAPI SetCurrentDirectoryA( LPCSTR dir )
1376 WCHAR *dirW;
1378 if (!(dirW = FILE_name_AtoW( dir, FALSE ))) return FALSE;
1379 return SetCurrentDirectoryW( dirW );
1383 /***********************************************************************
1384 * GetWindowsDirectoryW (KERNEL32.@)
1386 * See comment for GetWindowsDirectoryA.
1388 UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
1390 UINT len = strlenW( DIR_Windows ) + 1;
1391 if (path && count >= len)
1393 strcpyW( path, DIR_Windows );
1394 len--;
1396 return len;
1400 /***********************************************************************
1401 * GetWindowsDirectoryA (KERNEL32.@)
1403 * Return value:
1404 * If buffer is large enough to hold full path and terminating '\0' character
1405 * function copies path to buffer and returns length of the path without '\0'.
1406 * Otherwise function returns required size including '\0' character and
1407 * does not touch the buffer.
1409 UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
1411 return copy_filename_WtoA( DIR_Windows, path, count );
1415 /***********************************************************************
1416 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
1418 UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
1420 return GetWindowsDirectoryA( path, count );
1424 /***********************************************************************
1425 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
1427 UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
1429 return GetWindowsDirectoryW( path, count );
1433 /***********************************************************************
1434 * GetSystemDirectoryW (KERNEL32.@)
1436 * See comment for GetWindowsDirectoryA.
1438 UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
1440 UINT len = strlenW( DIR_System ) + 1;
1441 if (path && count >= len)
1443 strcpyW( path, DIR_System );
1444 len--;
1446 return len;
1450 /***********************************************************************
1451 * GetSystemDirectoryA (KERNEL32.@)
1453 * See comment for GetWindowsDirectoryA.
1455 UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
1457 return copy_filename_WtoA( DIR_System, path, count );
1461 /***********************************************************************
1462 * GetSystemWow64DirectoryW (KERNEL32.@)
1464 * As seen on MSDN
1465 * - On Win32 we should returns ERROR_CALL_NOT_IMPLEMENTED
1466 * - On Win64 we should returns the SysWow64 (system64) directory
1468 UINT WINAPI GetSystemWow64DirectoryW( LPWSTR lpBuffer, UINT uSize )
1470 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1471 return 0;
1475 /***********************************************************************
1476 * GetSystemWow64DirectoryA (KERNEL32.@)
1478 * See comment for GetWindowsWow64DirectoryW.
1480 UINT WINAPI GetSystemWow64DirectoryA( LPSTR lpBuffer, UINT uSize )
1482 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1483 return 0;
1487 /***********************************************************************
1488 * wine_get_unix_file_name (KERNEL32.@) Not a Windows API
1490 * Return the full Unix file name for a given path.
1491 * Returned buffer must be freed by caller.
1493 char *wine_get_unix_file_name( LPCWSTR dosW )
1495 UNICODE_STRING nt_name;
1496 ANSI_STRING unix_name;
1497 NTSTATUS status;
1499 if (!RtlDosPathNameToNtPathName_U( dosW, &nt_name, NULL, NULL )) return NULL;
1500 status = wine_nt_to_unix_file_name( &nt_name, &unix_name, FILE_OPEN_IF, FALSE );
1501 RtlFreeUnicodeString( &nt_name );
1502 if (status && status != STATUS_NO_SUCH_FILE)
1504 SetLastError( RtlNtStatusToDosError( status ) );
1505 return NULL;
1507 return unix_name.Buffer;
1511 /***********************************************************************
1512 * wine_get_dos_file_name (KERNEL32.@) Not a Windows API
1514 * Return the full DOS file name for a given Unix path.
1515 * Returned buffer must be freed by caller.
1517 WCHAR *wine_get_dos_file_name( LPCSTR str )
1519 UNICODE_STRING nt_name;
1520 ANSI_STRING unix_name;
1521 NTSTATUS status;
1522 DWORD len;
1524 RtlInitAnsiString( &unix_name, str );
1525 status = wine_unix_to_nt_file_name( &unix_name, &nt_name );
1526 if (status)
1528 SetLastError( RtlNtStatusToDosError( status ) );
1529 return NULL;
1531 /* get rid of the \??\ prefix */
1532 /* FIXME: should implement RtlNtPathNameToDosPathName and use that instead */
1533 len = nt_name.Length - 4 * sizeof(WCHAR);
1534 memmove( nt_name.Buffer, nt_name.Buffer + 4, len );
1535 nt_name.Buffer[len / sizeof(WCHAR)] = 0;
1536 return nt_name.Buffer;