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 "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(file);
43 #define MAX_PATHNAME_LEN 1024
46 /***********************************************************************
47 * GetLongPathNameW (KERNEL32.@)
51 * shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
52 * shortpath="": LastError=ERROR_PATH_NOT_FOUND, ret=0
54 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
56 WCHAR tmplongpath[MAX_PATHNAME_LEN];
60 BOOL unixabsolute = (shortpath[0] == '/');
66 SetLastError(ERROR_INVALID_PARAMETER);
71 SetLastError(ERROR_PATH_NOT_FOUND);
75 TRACE("%s,%p,%ld\n", debugstr_w(shortpath), longpath, longlen);
77 if (shortpath[0] == '\\' && shortpath[1] == '\\')
79 ERR("UNC pathname %s\n", debugstr_w(shortpath));
80 lstrcpynW( longpath, shortpath, longlen );
81 return strlenW(longpath);
84 /* check for drive letter */
85 if (!unixabsolute && shortpath[1] == ':' )
87 tmplongpath[0] = shortpath[0];
94 /* check for path delimiters and reproduce them */
95 if (shortpath[sp] == '\\' || shortpath[sp] == '/')
97 if (!lp || tmplongpath[lp-1] != '\\')
99 /* strip double "\\" */
100 tmplongpath[lp++] = '\\';
102 tmplongpath[lp] = 0; /* terminate string */
108 if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
110 tmplongpath[lp++] = *p++;
111 tmplongpath[lp++] = *p++;
113 for (; *p && *p != '/' && *p != '\\'; p++);
114 tmplen = p - (shortpath + sp);
115 lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
116 /* Check if the file exists and use the existing file name */
117 goit = FindFirstFileW(tmplongpath, &wfd);
118 if (goit == INVALID_HANDLE_VALUE)
120 TRACE("not found %s!\n", debugstr_w(tmplongpath));
121 SetLastError ( ERROR_FILE_NOT_FOUND );
125 strcpyW(tmplongpath + lp, wfd.cFileName);
126 lp += strlenW(tmplongpath + lp);
129 tmplen = strlenW(shortpath) - 1;
130 if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
131 (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
132 tmplongpath[lp++] = shortpath[tmplen];
135 tmplen = strlenW(tmplongpath) + 1;
136 if (tmplen <= longlen)
138 strcpyW(longpath, tmplongpath);
139 TRACE("returning %s\n", debugstr_w(longpath));
140 tmplen--; /* length without 0 */
146 /***********************************************************************
147 * GetLongPathNameA (KERNEL32.@)
149 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
151 UNICODE_STRING shortpathW;
152 WCHAR longpathW[MAX_PATH];
157 SetLastError(ERROR_INVALID_PARAMETER);
161 TRACE("%s\n", debugstr_a(shortpath));
163 if (!RtlCreateUnicodeStringFromAsciiz(&shortpathW, shortpath))
165 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
169 retW = GetLongPathNameW(shortpathW.Buffer, longpathW, MAX_PATH);
173 else if (retW > MAX_PATH)
175 SetLastError(ERROR_FILENAME_EXCED_RANGE);
180 ret = WideCharToMultiByte(CP_ACP, 0, longpathW, -1, NULL, 0, NULL, NULL);
183 WideCharToMultiByte(CP_ACP, 0, longpathW, -1, longpath, longlen, NULL, NULL);
184 ret--; /* length without 0 */
188 RtlFreeUnicodeString(&shortpathW);
193 /***********************************************************************
194 * GetShortPathNameW (KERNEL32.@)
198 * longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
199 * longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
201 * more observations ( with NT 3.51 (WinDD) ):
202 * longpath <= 8.3 -> just copy longpath to shortpath
204 * a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
205 * b) file does exist -> set the short filename.
206 * - trailing slashes are reproduced in the short name, even if the
207 * file is not a directory
208 * - the absolute/relative path of the short name is reproduced like found
210 * - longpath and shortpath may have the same address
213 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
215 WCHAR tmpshortpath[MAX_PATHNAME_LEN];
217 DWORD sp = 0, lp = 0;
219 BOOL unixabsolute = (longpath[0] == '/');
220 WIN32_FIND_DATAW wfd;
223 WCHAR ustr_buf[8+1+3+1];
225 TRACE("%s\n", debugstr_w(longpath));
229 SetLastError(ERROR_INVALID_PARAMETER);
234 SetLastError(ERROR_BAD_PATHNAME);
238 /* check for drive letter */
239 if (!unixabsolute && longpath[1] == ':' )
241 tmpshortpath[0] = longpath[0];
242 tmpshortpath[1] = ':';
246 ustr.Buffer = ustr_buf;
248 ustr.MaximumLength = sizeof(ustr_buf);
252 /* check for path delimiters and reproduce them */
253 if (longpath[lp] == '\\' || longpath[lp] == '/')
255 if (!sp || tmpshortpath[sp-1] != '\\')
257 /* strip double "\\" */
258 tmpshortpath[sp] = '\\';
261 tmpshortpath[sp] = 0; /* terminate string */
266 for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
267 tmplen = p - (longpath + lp);
268 lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
269 if (tmplen <= 8+1+3+1)
271 memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
272 ustr_buf[tmplen] = '\0';
273 ustr.Length = tmplen * sizeof(WCHAR);
274 /* Check, if the current element is a valid dos name */
275 if (!RtlIsNameLegalDOS8Dot3(&ustr, NULL, NULL))
282 /* Check if the file exists and use the existing file name */
283 goit = FindFirstFileW(tmpshortpath, &wfd);
284 if (goit == INVALID_HANDLE_VALUE) goto notfound;
286 strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
287 sp += strlenW(tmpshortpath + sp);
290 tmpshortpath[sp] = 0;
292 tmplen = strlenW(tmpshortpath) + 1;
293 if (tmplen <= shortlen)
295 strcpyW(shortpath, tmpshortpath);
296 TRACE("returning %s\n", debugstr_w(shortpath));
297 tmplen--; /* length without 0 */
303 TRACE("not found!\n" );
304 SetLastError ( ERROR_FILE_NOT_FOUND );
308 /***********************************************************************
309 * GetShortPathNameA (KERNEL32.@)
311 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
313 UNICODE_STRING longpathW;
314 WCHAR shortpathW[MAX_PATH];
319 SetLastError(ERROR_INVALID_PARAMETER);
323 TRACE("%s\n", debugstr_a(longpath));
325 if (!RtlCreateUnicodeStringFromAsciiz(&longpathW, longpath))
327 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
331 retW = GetShortPathNameW(longpathW.Buffer, shortpathW, MAX_PATH);
335 else if (retW > MAX_PATH)
337 SetLastError(ERROR_FILENAME_EXCED_RANGE);
342 ret = WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, NULL, 0, NULL, NULL);
345 WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, shortpath, shortlen, NULL, NULL);
346 ret--; /* length without 0 */
350 RtlFreeUnicodeString(&longpathW);