2 * File handling functions
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
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 /***********************************************************************
47 * GetFullPathNameW (KERNEL32.@)
49 * if the path closed with '\', *lastpart is 0
51 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
54 return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
57 /***********************************************************************
58 * GetFullPathNameA (KERNEL32.@)
60 * if the path closed with '\', *lastpart is 0
62 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
66 WCHAR bufferW[MAX_PATH];
71 SetLastError(ERROR_INVALID_PARAMETER);
75 if (!RtlCreateUnicodeStringFromAsciiz(&nameW, name))
77 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
81 retW = GetFullPathNameW( nameW.Buffer, MAX_PATH, bufferW, NULL);
85 else if (retW > MAX_PATH)
87 SetLastError(ERROR_FILENAME_EXCED_RANGE);
92 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
93 if (ret && ret <= len)
95 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, len, NULL, NULL);
96 ret--; /* length without 0 */
100 LPSTR p = buffer + strlen(buffer) - 1;
104 while ((p > buffer + 2) && (*p != '\\')) p--;
107 else *lastpart = NULL;
112 RtlFreeUnicodeString(&nameW);
117 /***********************************************************************
118 * GetLongPathNameW (KERNEL32.@)
121 * observed (Win2000):
122 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
123 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
125 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
127 WCHAR tmplongpath[MAX_PATHNAME_LEN];
129 DWORD sp = 0, lp = 0;
131 BOOL unixabsolute = (shortpath[0] == '/');
132 WIN32_FIND_DATAW wfd;
137 SetLastError(ERROR_INVALID_PARAMETER);
142 SetLastError(ERROR_PATH_NOT_FOUND);
146 TRACE("%s,%p,%ld\n", debugstr_w(shortpath), longpath, longlen);
148 if (shortpath[0] == '\\' && shortpath[1] == '\\')
150 ERR("UNC pathname %s\n", debugstr_w(shortpath));
151 lstrcpynW( longpath, shortpath, longlen );
152 return strlenW(longpath);
155 /* check for drive letter */
156 if (!unixabsolute && shortpath[1] == ':' )
158 tmplongpath[0] = shortpath[0];
159 tmplongpath[1] = ':';
163 while (shortpath[sp])
165 /* check for path delimiters and reproduce them */
166 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
168 if (!lp || tmplongpath[lp-1] != '\\')
170 /* strip double "\\" */
171 tmplongpath[lp++] = '\\';
173 tmplongpath[lp] = 0; /* terminate string */
179 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
181 tmplongpath[lp++] = *p++;
182 tmplongpath[lp++] = *p++;
184 for (; *p && *p != '/' && *p != '\\'; p++);
185 tmplen = p - (shortpath + sp);
186 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
187 /* Check if the file exists and use the existing file name */
188 goit = FindFirstFileW(tmplongpath, &wfd);
189 if (goit == INVALID_HANDLE_VALUE)
191 TRACE("not found %s!\n", debugstr_w(tmplongpath));
192 SetLastError ( ERROR_FILE_NOT_FOUND );
196 strcpyW(tmplongpath + lp, wfd.cFileName);
197 lp += strlenW(tmplongpath + lp);
200 tmplen = strlenW(shortpath) - 1;
201 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
202 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
203 tmplongpath[lp++] = shortpath[tmplen];
206 tmplen = strlenW(tmplongpath) + 1;
207 if (tmplen <= longlen)
209 strcpyW(longpath, tmplongpath);
210 TRACE("returning %s\n", debugstr_w(longpath));
211 tmplen--; /* length without 0 */
217 /***********************************************************************
218 * GetLongPathNameA (KERNEL32.@)
220 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
222 UNICODE_STRING shortpathW;
223 WCHAR longpathW[MAX_PATH];
228 SetLastError(ERROR_INVALID_PARAMETER);
232 TRACE("%s\n", debugstr_a(shortpath));
234 if (!RtlCreateUnicodeStringFromAsciiz(&shortpathW, shortpath))
236 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
240 retW = GetLongPathNameW(shortpathW.Buffer, longpathW, MAX_PATH);
244 else if (retW > MAX_PATH)
246 SetLastError(ERROR_FILENAME_EXCED_RANGE);
251 ret = WideCharToMultiByte(CP_ACP, 0, longpathW, -1, NULL, 0, NULL, NULL);
254 WideCharToMultiByte(CP_ACP, 0, longpathW, -1, longpath, longlen, NULL, NULL);
255 ret--; /* length without 0 */
259 RtlFreeUnicodeString(&shortpathW);
264 /***********************************************************************
265 * GetShortPathNameW (KERNEL32.@)
269 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
270 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
272 * more observations ( with NT 3.51 (WinDD) ):
273 * longpath <= 8.3 -> just copy longpath to shortpath
275 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
276 * b) file does exist -> set the short filename.
277 * - trailing slashes are reproduced in the short name, even if the
278 * file is not a directory
279 * - the absolute/relative path of the short name is reproduced like found
281 * - longpath and shortpath may have the same address
284 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
286 WCHAR tmpshortpath[MAX_PATHNAME_LEN];
288 DWORD sp = 0, lp = 0;
290 BOOL unixabsolute = (longpath[0] == '/');
291 WIN32_FIND_DATAW wfd;
294 WCHAR ustr_buf[8+1+3+1];
296 TRACE("%s\n", debugstr_w(longpath));
300 SetLastError(ERROR_INVALID_PARAMETER);
305 SetLastError(ERROR_BAD_PATHNAME);
309 /* check for drive letter */
310 if (!unixabsolute && longpath[1] == ':' )
312 tmpshortpath[0] = longpath[0];
313 tmpshortpath[1] = ':';
317 ustr.Buffer = ustr_buf;
319 ustr.MaximumLength = sizeof(ustr_buf);
323 /* check for path delimiters and reproduce them */
324 if (longpath[lp] == '\\' || longpath[lp] == '/')
326 if (!sp || tmpshortpath[sp-1] != '\\')
328 /* strip double "\\" */
329 tmpshortpath[sp] = '\\';
332 tmpshortpath[sp] = 0; /* terminate string */
337 for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
338 tmplen = p - (longpath + lp);
339 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
340 /* Check, if the current element is a valid dos name */
341 if (tmplen <= 8+1+3+1)
344 memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
345 ustr_buf[tmplen] = '\0';
346 ustr.Length = tmplen * sizeof(WCHAR);
347 if (RtlIsNameLegalDOS8Dot3(&ustr, NULL, &spaces) && !spaces)
355 /* Check if the file exists and use the existing short file name */
356 goit = FindFirstFileW(tmpshortpath, &wfd);
357 if (goit == INVALID_HANDLE_VALUE) goto notfound;
359 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
360 sp += strlenW(tmpshortpath + sp);
363 tmpshortpath[sp] = 0;
365 tmplen = strlenW(tmpshortpath) + 1;
366 if (tmplen <= shortlen)
368 strcpyW(shortpath, tmpshortpath);
369 TRACE("returning %s\n", debugstr_w(shortpath));
370 tmplen--; /* length without 0 */
376 TRACE("not found!\n" );
377 SetLastError ( ERROR_FILE_NOT_FOUND );
381 /***********************************************************************
382 * GetShortPathNameA (KERNEL32.@)
384 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
386 UNICODE_STRING longpathW;
387 WCHAR shortpathW[MAX_PATH];
392 SetLastError(ERROR_INVALID_PARAMETER);
396 TRACE("%s\n", debugstr_a(longpath));
398 if (!RtlCreateUnicodeStringFromAsciiz(&longpathW, longpath))
400 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
404 retW = GetShortPathNameW(longpathW.Buffer, shortpathW, MAX_PATH);
408 else if (retW > MAX_PATH)
410 SetLastError(ERROR_FILENAME_EXCED_RANGE);
415 ret = WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, NULL, 0, NULL, NULL);
418 WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, shortpath, shortlen, NULL, NULL);
419 ret--; /* length without 0 */
423 RtlFreeUnicodeString(&longpathW);
428 /***********************************************************************
429 * GetTempPathA (KERNEL32.@)
431 UINT WINAPI GetTempPathA( UINT count, LPSTR path )
433 WCHAR pathW[MAX_PATH];
436 ret = GetTempPathW(MAX_PATH, pathW);
443 SetLastError(ERROR_FILENAME_EXCED_RANGE);
447 ret = WideCharToMultiByte(CP_ACP, 0, pathW, -1, NULL, 0, NULL, NULL);
450 WideCharToMultiByte(CP_ACP, 0, pathW, -1, path, count, NULL, NULL);
451 ret--; /* length without 0 */
457 /***********************************************************************
458 * GetTempPathW (KERNEL32.@)
460 UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
462 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
463 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
464 WCHAR tmp_path[MAX_PATH];
467 TRACE("%u,%p\n", count, path);
469 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )))
470 if (!(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )))
471 if (!(ret = GetCurrentDirectoryW( MAX_PATH, tmp_path )))
476 SetLastError(ERROR_FILENAME_EXCED_RANGE);
480 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
483 if (ret > MAX_PATH - 2)
485 SetLastError(ERROR_FILENAME_EXCED_RANGE);
489 if (tmp_path[ret-1] != '\\')
491 tmp_path[ret++] = '\\';
492 tmp_path[ret] = '\0';
495 ret++; /* add space for terminating 0 */
499 lstrcpynW(path, tmp_path, count);
501 ret--; /* return length without 0 */
503 path[0] = 0; /* avoid returning ambiguous "X:" */
506 TRACE("returning %u, %s\n", ret, debugstr_w(path));
511 /***********************************************************************
514 * Check if the file name contains a path; helper for SearchPathW.
515 * A relative path is not considered a path unless it starts with ./ or ../
517 inline static BOOL contains_pathW (LPCWSTR name)
519 if (RtlDetermineDosPathNameType_U( name ) != RELATIVE_PATH) return TRUE;
520 if (name[0] != '.') return FALSE;
521 if (name[1] == '/' || name[1] == '\\') return TRUE;
522 return (name[1] == '.' && (name[2] == '/' || name[2] == '\\'));
526 /***********************************************************************
527 * SearchPathW [KERNEL32.@]
529 * Searches for a specified file in the search path.
532 * path [I] Path to search
533 * name [I] Filename to search for.
534 * ext [I] File extension to append to file name. The first
535 * character must be a period. This parameter is
536 * specified only if the filename given does not
537 * contain an extension.
538 * buflen [I] size of buffer, in characters
539 * buffer [O] buffer for found filename
540 * lastpart [O] address of pointer to last used character in
541 * buffer (the final '\')
544 * Success: length of string copied into buffer, not including
545 * terminating null character. If the filename found is
546 * longer than the length of the buffer, the length of the
547 * filename is returned.
551 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
554 DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext, DWORD buflen,
555 LPWSTR buffer, LPWSTR *lastpart )
559 /* If the name contains an explicit path, ignore the path */
561 if (contains_pathW(name))
563 /* try first without extension */
564 if (RtlDoesFileExists_U( name ))
565 return GetFullPathNameW( name, buflen, buffer, lastpart );
569 LPCWSTR p = strrchrW( name, '.' );
570 if (p && !strchrW( p, '/' ) && !strchrW( p, '\\' ))
571 ext = NULL; /* Ignore the specified extension */
574 /* Allocate a buffer for the file name and extension */
578 DWORD len = strlenW(name) + strlenW(ext);
580 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
582 SetLastError( ERROR_OUTOFMEMORY );
585 strcpyW( tmp, name );
587 if (RtlDoesFileExists_U( tmp ))
588 ret = GetFullPathNameW( tmp, buflen, buffer, lastpart );
589 HeapFree( GetProcessHeap(), 0, tmp );
592 else if (path && path[0]) /* search in the specified path */
594 ret = RtlDosSearchPath_U( path, name, ext, buflen * sizeof(WCHAR),
595 buffer, lastpart ) / sizeof(WCHAR);
597 else /* search in the default path */
599 WCHAR *dll_path = MODULE_get_dll_load_path( NULL );
603 ret = RtlDosSearchPath_U( dll_path, name, ext, buflen * sizeof(WCHAR),
604 buffer, lastpart ) / sizeof(WCHAR);
605 HeapFree( GetProcessHeap(), 0, dll_path );
609 SetLastError( ERROR_OUTOFMEMORY );
614 if (!ret) SetLastError( ERROR_FILE_NOT_FOUND );
615 else TRACE( "found %s\n", debugstr_w(buffer) );
620 /***********************************************************************
621 * SearchPathA (KERNEL32.@)
623 DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext,
624 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
626 UNICODE_STRING pathW, nameW, extW;
627 WCHAR bufferW[MAX_PATH];
630 if (path) RtlCreateUnicodeStringFromAsciiz(&pathW, path);
631 else pathW.Buffer = NULL;
632 if (name) RtlCreateUnicodeStringFromAsciiz(&nameW, name);
633 else nameW.Buffer = NULL;
634 if (ext) RtlCreateUnicodeStringFromAsciiz(&extW, ext);
635 else extW.Buffer = NULL;
637 retW = SearchPathW(pathW.Buffer, nameW.Buffer, extW.Buffer, MAX_PATH, bufferW, NULL);
641 else if (retW > MAX_PATH)
643 SetLastError(ERROR_FILENAME_EXCED_RANGE);
648 ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
651 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buflen, NULL, NULL);
652 ret--; /* length without 0 */
653 if (lastpart) *lastpart = strrchr(buffer, '\\') + 1;
657 RtlFreeUnicodeString(&pathW);
658 RtlFreeUnicodeString(&nameW);
659 RtlFreeUnicodeString(&extW);