- RtlGetFullPathName_U: rewritten so that the source & destination
[wine] / dlls / kernel / path.c
1 /*
2  * File handling functions
3  *
4  * Copyright 1993 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 2003 Eric Pouech
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "winerror.h"
32 #include "ntstatus.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winternl.h"
37
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(file);
42
43 #define MAX_PATHNAME_LEN        1024
44
45 /***********************************************************************
46  *           GetFullPathNameW   (KERNEL32.@)
47  * NOTES
48  *   if the path closed with '\', *lastpart is 0
49  */
50 DWORD WINAPI GetFullPathNameW( LPCWSTR name, DWORD len, LPWSTR buffer,
51                                LPWSTR *lastpart )
52 {
53     return RtlGetFullPathName_U(name, len * sizeof(WCHAR), buffer, lastpart) / sizeof(WCHAR);
54 }
55
56 /***********************************************************************
57  *           GetFullPathNameA   (KERNEL32.@)
58  * NOTES
59  *   if the path closed with '\', *lastpart is 0
60  */
61 DWORD WINAPI GetFullPathNameA( LPCSTR name, DWORD len, LPSTR buffer,
62                                LPSTR *lastpart )
63 {
64     UNICODE_STRING nameW;
65     WCHAR bufferW[MAX_PATH];
66     DWORD ret, retW;
67
68     if (!name)
69     {
70         SetLastError(ERROR_INVALID_PARAMETER);
71         return 0;
72     }
73
74     if (!RtlCreateUnicodeStringFromAsciiz(&nameW, name))
75     {
76         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
77         return 0;
78     }
79
80     retW = GetFullPathNameW( nameW.Buffer, MAX_PATH, bufferW, NULL);
81
82     if (!retW)
83         ret = 0;
84     else if (retW > MAX_PATH)
85     {
86         SetLastError(ERROR_FILENAME_EXCED_RANGE);
87         ret = 0;
88     }
89     else
90     {
91         ret = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
92         if (ret && ret <= len)
93         {
94             WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, len, NULL, NULL);
95             ret--; /* length without 0 */
96
97             if (lastpart)
98             {
99                 LPSTR p = buffer + strlen(buffer) - 1;
100
101                 if (*p != '\\')
102                 {
103                     while ((p > buffer + 2) && (*p != '\\')) p--;
104                     *lastpart = p + 1;
105                 }
106                 else *lastpart = NULL;
107             }
108         }
109     }
110
111     RtlFreeUnicodeString(&nameW);
112     return ret;
113 }
114
115
116 /***********************************************************************
117  *           GetLongPathNameW   (KERNEL32.@)
118  *
119  * NOTES
120  *  observed (Win2000):
121  *  shortpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
122  *  shortpath="":   LastError=ERROR_PATH_NOT_FOUND, ret=0
123  */
124 DWORD WINAPI GetLongPathNameW( LPCWSTR shortpath, LPWSTR longpath, DWORD longlen )
125 {
126     WCHAR               tmplongpath[MAX_PATHNAME_LEN];
127     LPCWSTR             p;
128     DWORD               sp = 0, lp = 0;
129     DWORD               tmplen;
130     BOOL                unixabsolute = (shortpath[0] == '/');
131     WIN32_FIND_DATAW    wfd;
132     HANDLE              goit;
133
134     if (!shortpath)
135     {
136         SetLastError(ERROR_INVALID_PARAMETER);
137         return 0;
138     }
139     if (!shortpath[0])
140     {
141         SetLastError(ERROR_PATH_NOT_FOUND);
142         return 0;
143     }
144
145     TRACE("%s,%p,%ld\n", debugstr_w(shortpath), longpath, longlen);
146
147     if (shortpath[0] == '\\' && shortpath[1] == '\\')
148     {
149         ERR("UNC pathname %s\n", debugstr_w(shortpath));
150         lstrcpynW( longpath, shortpath, longlen );
151         return strlenW(longpath);
152     }
153
154     /* check for drive letter */
155     if (!unixabsolute && shortpath[1] == ':' )
156     {
157         tmplongpath[0] = shortpath[0];
158         tmplongpath[1] = ':';
159         lp = sp = 2;
160     }
161
162     while (shortpath[sp])
163     {
164         /* check for path delimiters and reproduce them */
165         if (shortpath[sp] == '\\' || shortpath[sp] == '/')
166         {
167             if (!lp || tmplongpath[lp-1] != '\\')
168             {
169                 /* strip double "\\" */
170                 tmplongpath[lp++] = '\\';
171             }
172             tmplongpath[lp] = 0; /* terminate string */
173             sp++;
174             continue;
175         }
176
177         p = shortpath + sp;
178         if (sp == 0 && p[0] == '.' && (p[1] == '/' || p[1] == '\\'))
179         {
180             tmplongpath[lp++] = *p++;
181             tmplongpath[lp++] = *p++;
182         }
183         for (; *p && *p != '/' && *p != '\\'; p++);
184         tmplen = p - (shortpath + sp);
185         lstrcpynW(tmplongpath + lp, shortpath + sp, tmplen + 1);
186         /* Check if the file exists and use the existing file name */
187         goit = FindFirstFileW(tmplongpath, &wfd);
188         if (goit == INVALID_HANDLE_VALUE)
189         {
190             TRACE("not found %s!\n", debugstr_w(tmplongpath));
191             SetLastError ( ERROR_FILE_NOT_FOUND );
192             return 0;
193         }
194         FindClose(goit);
195         strcpyW(tmplongpath + lp, wfd.cFileName);
196         lp += strlenW(tmplongpath + lp);
197         sp += tmplen;
198     }
199     tmplen = strlenW(shortpath) - 1;
200     if ((shortpath[tmplen] == '/' || shortpath[tmplen] == '\\') &&
201         (tmplongpath[lp - 1] != '/' && tmplongpath[lp - 1] != '\\'))
202         tmplongpath[lp++] = shortpath[tmplen];
203     tmplongpath[lp] = 0;
204
205     tmplen = strlenW(tmplongpath) + 1;
206     if (tmplen <= longlen)
207     {
208         strcpyW(longpath, tmplongpath);
209         TRACE("returning %s\n", debugstr_w(longpath));
210         tmplen--; /* length without 0 */
211     }
212
213     return tmplen;
214 }
215
216 /***********************************************************************
217  *           GetLongPathNameA   (KERNEL32.@)
218  */
219 DWORD WINAPI GetLongPathNameA( LPCSTR shortpath, LPSTR longpath, DWORD longlen )
220 {
221     UNICODE_STRING shortpathW;
222     WCHAR longpathW[MAX_PATH];
223     DWORD ret, retW;
224
225     if (!shortpath)
226     {
227         SetLastError(ERROR_INVALID_PARAMETER);
228         return 0;
229     }
230
231     TRACE("%s\n", debugstr_a(shortpath));
232
233     if (!RtlCreateUnicodeStringFromAsciiz(&shortpathW, shortpath))
234     {
235         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
236         return 0;
237     }
238
239     retW = GetLongPathNameW(shortpathW.Buffer, longpathW, MAX_PATH);
240
241     if (!retW)
242         ret = 0;
243     else if (retW > MAX_PATH)
244     {
245         SetLastError(ERROR_FILENAME_EXCED_RANGE);
246         ret = 0;
247     }
248     else
249     {
250         ret = WideCharToMultiByte(CP_ACP, 0, longpathW, -1, NULL, 0, NULL, NULL);
251         if (ret <= longlen)
252         {
253             WideCharToMultiByte(CP_ACP, 0, longpathW, -1, longpath, longlen, NULL, NULL);
254             ret--; /* length without 0 */
255         }
256     }
257
258     RtlFreeUnicodeString(&shortpathW);
259     return ret;
260 }
261
262
263 /***********************************************************************
264  *           GetShortPathNameW   (KERNEL32.@)
265  *
266  * NOTES
267  *  observed:
268  *  longpath=NULL: LastError=ERROR_INVALID_PARAMETER, ret=0
269  *  longpath="" or invalid: LastError=ERROR_BAD_PATHNAME, ret=0
270  *
271  * more observations ( with NT 3.51 (WinDD) ):
272  * longpath <= 8.3 -> just copy longpath to shortpath
273  * longpath > 8.3  ->
274  *             a) file does not exist -> return 0, LastError = ERROR_FILE_NOT_FOUND
275  *             b) file does exist     -> set the short filename.
276  * - trailing slashes are reproduced in the short name, even if the
277  *   file is not a directory
278  * - the absolute/relative path of the short name is reproduced like found
279  *   in the long name
280  * - longpath and shortpath may have the same address
281  * Peter Ganten, 1999
282  */
283 DWORD WINAPI GetShortPathNameW( LPCWSTR longpath, LPWSTR shortpath, DWORD shortlen )
284 {
285     WCHAR               tmpshortpath[MAX_PATHNAME_LEN];
286     LPCWSTR             p;
287     DWORD               sp = 0, lp = 0;
288     DWORD               tmplen;
289     BOOL                unixabsolute = (longpath[0] == '/');
290     WIN32_FIND_DATAW    wfd;
291     HANDLE              goit;
292     UNICODE_STRING      ustr;
293     WCHAR               ustr_buf[8+1+3+1];
294
295     TRACE("%s\n", debugstr_w(longpath));
296
297     if (!longpath)
298     {
299         SetLastError(ERROR_INVALID_PARAMETER);
300         return 0;
301     }
302     if (!longpath[0])
303     {
304         SetLastError(ERROR_BAD_PATHNAME);
305         return 0;
306     }
307
308     /* check for drive letter */
309     if (!unixabsolute && longpath[1] == ':' )
310     {
311         tmpshortpath[0] = longpath[0];
312         tmpshortpath[1] = ':';
313         sp = lp = 2;
314     }
315
316     ustr.Buffer = ustr_buf;
317     ustr.Length = 0;
318     ustr.MaximumLength = sizeof(ustr_buf);
319
320     while (longpath[lp])
321     {
322         /* check for path delimiters and reproduce them */
323         if (longpath[lp] == '\\' || longpath[lp] == '/')
324         {
325             if (!sp || tmpshortpath[sp-1] != '\\')
326             {
327                 /* strip double "\\" */
328                 tmpshortpath[sp] = '\\';
329                 sp++;
330             }
331             tmpshortpath[sp] = 0; /* terminate string */
332             lp++;
333             continue;
334         }
335
336         for (p = longpath + lp; *p && *p != '/' && *p != '\\'; p++);
337         tmplen = p - (longpath + lp);
338         lstrcpynW(tmpshortpath + sp, longpath + lp, tmplen + 1);
339         /* Check, if the current element is a valid dos name */
340         if (tmplen <= 8+1+3+1)
341         {
342             memcpy(ustr_buf, longpath + lp, tmplen * sizeof(WCHAR));
343             ustr_buf[tmplen] = '\0';
344             ustr.Length = tmplen * sizeof(WCHAR);
345             if (RtlIsNameLegalDOS8Dot3(&ustr, NULL, NULL))
346             {
347                 sp += tmplen;
348                 lp += tmplen;
349                 continue;
350             }
351         }
352
353         /* Check if the file exists and use the existing short file name */
354         goit = FindFirstFileW(tmpshortpath, &wfd);
355         if (goit == INVALID_HANDLE_VALUE) goto notfound;
356         FindClose(goit);
357         strcpyW(tmpshortpath + sp, wfd.cAlternateFileName);
358         sp += strlenW(tmpshortpath + sp);
359         lp += tmplen;
360     }
361     tmpshortpath[sp] = 0;
362
363     tmplen = strlenW(tmpshortpath) + 1;
364     if (tmplen <= shortlen)
365     {
366         strcpyW(shortpath, tmpshortpath);
367         TRACE("returning %s\n", debugstr_w(shortpath));
368         tmplen--; /* length without 0 */
369     }
370
371     return tmplen;
372
373  notfound:
374     TRACE("not found!\n" );
375     SetLastError ( ERROR_FILE_NOT_FOUND );
376     return 0;
377 }
378
379 /***********************************************************************
380  *           GetShortPathNameA   (KERNEL32.@)
381  */
382 DWORD WINAPI GetShortPathNameA( LPCSTR longpath, LPSTR shortpath, DWORD shortlen )
383 {
384     UNICODE_STRING longpathW;
385     WCHAR shortpathW[MAX_PATH];
386     DWORD ret, retW;
387
388     if (!longpath)
389     {
390         SetLastError(ERROR_INVALID_PARAMETER);
391         return 0;
392     }
393
394     TRACE("%s\n", debugstr_a(longpath));
395
396     if (!RtlCreateUnicodeStringFromAsciiz(&longpathW, longpath))
397     {
398         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
399         return 0;
400     }
401
402     retW = GetShortPathNameW(longpathW.Buffer, shortpathW, MAX_PATH);
403
404     if (!retW)
405         ret = 0;
406     else if (retW > MAX_PATH)
407     {
408         SetLastError(ERROR_FILENAME_EXCED_RANGE);
409         ret = 0;
410     }
411     else
412     {
413         ret = WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, NULL, 0, NULL, NULL);
414         if (ret <= shortlen)
415         {
416             WideCharToMultiByte(CP_ACP, 0, shortpathW, -1, shortpath, shortlen, NULL, NULL);
417             ret--; /* length without 0 */
418         }
419     }
420
421     RtlFreeUnicodeString(&longpathW);
422     return ret;
423 }