shlwapi: UrlCanonicalize will canonize address in format "file://localhost/c:/" corre...
[wine] / dlls / shlwapi / path.c
1 /*
2  * Path Functions
3  *
4  * Copyright 1999, 2000 Juergen Schmied
5  * Copyright 2001, 2002 Jon Griffiths
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "wine/unicode.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #define NO_SHLWAPI_STREAM
37 #include "shlwapi.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41
42 /* Get a function pointer from a DLL handle */
43 #define GET_FUNC(func, module, name, fail) \
44   do { \
45     if (!func) { \
46       if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
47       func = (fn##func)GetProcAddress(SHLWAPI_h##module, name); \
48       if (!func) return fail; \
49     } \
50   } while (0)
51
52 /* DLL handles for late bound calls */
53 static HMODULE SHLWAPI_hshell32;
54
55 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
56 typedef BOOL (WINAPI *fnpIsNetDrive)(int);
57 static  fnpIsNetDrive pIsNetDrive;
58
59 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR,LPWSTR,DWORD);
60
61 /*************************************************************************
62  * PathAppendA    [SHLWAPI.@]
63  *
64  * Append one path to another.
65  *
66  * PARAMS
67  *  lpszPath   [I/O] Initial part of path, and destination for output
68  *  lpszAppend [I]   Path to append
69  *
70  * RETURNS
71  *  Success: TRUE. lpszPath contains the newly created path.
72  *  Failure: FALSE, if either path is NULL, or PathCombineA() fails.
73  *
74  * NOTES
75  *  lpszAppend must contain at least one backslash ('\') if not NULL.
76  *  Because PathCombineA() is used to join the paths, the resulting
77  *  path is also canonicalized.
78  */
79 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
80 {
81   TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
82
83   if (lpszPath && lpszAppend)
84   {
85     if (!PathIsUNCA(lpszAppend))
86       while (*lpszAppend == '\\')
87         lpszAppend++;
88     if (PathCombineA(lpszPath, lpszPath, lpszAppend))
89       return TRUE;
90   }
91   return FALSE;
92 }
93
94 /*************************************************************************
95  * PathAppendW    [SHLWAPI.@]
96  *
97  * See PathAppendA.
98  */
99 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
100 {
101   TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
102
103   if (lpszPath && lpszAppend)
104   {
105     if (!PathIsUNCW(lpszAppend))
106       while (*lpszAppend == '\\')
107         lpszAppend++;
108     if (PathCombineW(lpszPath, lpszPath, lpszAppend))
109       return TRUE;
110   }
111   return FALSE;
112 }
113
114 /*************************************************************************
115  * PathCombineA         [SHLWAPI.@]
116  *
117  * Combine two paths together.
118  *
119  * PARAMS
120  *  lpszDest [O] Destination for combined path
121  *  lpszDir  [I] Directory path
122  *  lpszFile [I] File path
123  *
124  * RETURNS
125  *  Success: The output path
126  *  Failure: NULL, if inputs are invalid.
127  *
128  * NOTES
129  *  lpszDest should be at least MAX_PATH in size, and may point to the same
130  *  memory location as lpszDir. The combined path is canonicalised.
131  */
132 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
133 {
134   WCHAR szDest[MAX_PATH];
135   WCHAR szDir[MAX_PATH];
136   WCHAR szFile[MAX_PATH];
137   TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
138
139   /* Invalid parameters */
140   if (!lpszDest)
141     return NULL;
142   if (!lpszDir && !lpszFile)
143   {
144     lpszDest[0] = 0;
145     return NULL;
146   }
147
148   if (lpszDir)
149     MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH);
150   if (lpszFile)
151     MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
152
153   if (PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL))
154     if (WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0))
155       return lpszDest;
156
157   lpszDest[0] = 0;
158   return NULL;
159 }
160
161 /*************************************************************************
162  * PathCombineW          [SHLWAPI.@]
163  *
164  * See PathCombineA.
165  */
166 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
167 {
168   WCHAR szTemp[MAX_PATH];
169   BOOL bUseBoth = FALSE, bStrip = FALSE;
170
171   TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
172
173   /* Invalid parameters */
174   if (!lpszDest)
175     return NULL;
176   if (!lpszDir && !lpszFile)
177   {
178     lpszDest[0] = 0;
179     return NULL;
180   }
181
182   if ((!lpszFile || !*lpszFile) && lpszDir)
183   {
184     /* Use dir only */
185     lstrcpynW(szTemp, lpszDir, MAX_PATH);
186   }
187   else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
188   {
189     if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
190     {
191       /* Use file only */
192       lstrcpynW(szTemp, lpszFile, MAX_PATH);
193     }
194     else
195     {
196       bUseBoth = TRUE;
197       bStrip = TRUE;
198     }
199   }
200   else
201     bUseBoth = TRUE;
202
203   if (bUseBoth)
204   {
205     lstrcpynW(szTemp, lpszDir, MAX_PATH);
206     if (bStrip)
207     {
208       PathStripToRootW(szTemp);
209       lpszFile++; /* Skip '\' */
210     }
211     if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
212     {
213       lpszDest[0] = 0;
214       return NULL;
215     }
216     strcatW(szTemp, lpszFile);
217   }
218
219   PathCanonicalizeW(lpszDest, szTemp);
220   return lpszDest;
221 }
222
223 /*************************************************************************
224  * PathAddBackslashA    [SHLWAPI.@]
225  *
226  * Append a backslash ('\') to a path if one doesn't exist.
227  *
228  * PARAMS
229  *  lpszPath [I/O] The path to append a backslash to.
230  *
231  * RETURNS
232  *  Success: The position of the last backslash in the path.
233  *  Failure: NULL, if lpszPath is NULL or the path is too large.
234  */
235 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
236 {
237   size_t iLen;
238
239   TRACE("(%s)\n",debugstr_a(lpszPath));
240
241   if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
242     return NULL;
243
244   if (iLen)
245   {
246     lpszPath += iLen;
247     if (lpszPath[-1] != '\\')
248     {
249      *lpszPath++ = '\\';
250      *lpszPath = '\0';
251     }
252   }
253   return lpszPath;
254 }
255
256 /*************************************************************************
257  * PathAddBackslashW  [SHLWAPI.@]
258  *
259  * See PathAddBackslashA.
260  */
261 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
262 {
263   size_t iLen;
264
265   TRACE("(%s)\n",debugstr_w(lpszPath));
266
267   if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
268     return NULL;
269
270   if (iLen)
271   {
272     lpszPath += iLen;
273     if (lpszPath[-1] != '\\')
274     {
275       *lpszPath++ = '\\';
276       *lpszPath = '\0';
277     }
278   }
279   return lpszPath;
280 }
281
282 /*************************************************************************
283  * PathBuildRootA    [SHLWAPI.@]
284  *
285  * Create a root drive string (e.g. "A:\") from a drive number.
286  *
287  * PARAMS
288  *  lpszPath [O] Destination for the drive string
289  *
290  * RETURNS
291  *  lpszPath
292  *
293  * NOTES
294  *  If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
295  */
296 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
297 {
298   TRACE("(%p,%d)\n", lpszPath, drive);
299
300   if (lpszPath && drive >= 0 && drive < 26)
301   {
302     lpszPath[0] = 'A' + drive;
303     lpszPath[1] = ':';
304     lpszPath[2] = '\\';
305     lpszPath[3] = '\0';
306   }
307   return lpszPath;
308 }
309
310 /*************************************************************************
311  * PathBuildRootW    [SHLWAPI.@]
312  *
313  * See PathBuildRootA.
314  */
315 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
316 {
317   TRACE("(%p,%d)\n", lpszPath, drive);
318
319   if (lpszPath && drive >= 0 && drive < 26)
320   {
321     lpszPath[0] = 'A' + drive;
322     lpszPath[1] = ':';
323     lpszPath[2] = '\\';
324     lpszPath[3] = '\0';
325   }
326   return lpszPath;
327 }
328
329 /*************************************************************************
330  * PathFindFileNameA  [SHLWAPI.@]
331  *
332  * Locate the start of the file name in a path
333  *
334  * PARAMS
335  *  lpszPath [I] Path to search
336  *
337  * RETURNS
338  *  A pointer to the first character of the file name
339  */
340 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
341 {
342   LPCSTR lastSlash = lpszPath;
343
344   TRACE("(%s)\n",debugstr_a(lpszPath));
345
346   while (lpszPath && *lpszPath)
347   {
348     if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
349         lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
350       lastSlash = lpszPath + 1;
351     lpszPath = CharNextA(lpszPath);
352   }
353   return (LPSTR)lastSlash;
354 }
355
356 /*************************************************************************
357  * PathFindFileNameW  [SHLWAPI.@]
358  *
359  * See PathFindFileNameA.
360  */
361 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
362 {
363   LPCWSTR lastSlash = lpszPath;
364
365   TRACE("(%s)\n",debugstr_w(lpszPath));
366
367   while (lpszPath && *lpszPath)
368   {
369     if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
370         lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
371       lastSlash = lpszPath + 1;
372     lpszPath++;
373   }
374   return (LPWSTR)lastSlash;
375 }
376
377 /*************************************************************************
378  * PathFindExtensionA  [SHLWAPI.@]
379  *
380  * Locate the start of the file extension in a path
381  *
382  * PARAMS
383  *  lpszPath [I] The path to search
384  *
385  * RETURNS
386  *  A pointer to the first character of the extension, the end of
387  *  the string if the path has no extension, or NULL If lpszPath is NULL
388  */
389 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
390 {
391   LPCSTR lastpoint = NULL;
392
393   TRACE("(%s)\n", debugstr_a(lpszPath));
394
395   if (lpszPath)
396   {
397     while (*lpszPath)
398     {
399       if (*lpszPath == '\\' || *lpszPath==' ')
400         lastpoint = NULL;
401       else if (*lpszPath == '.')
402         lastpoint = lpszPath;
403       lpszPath = CharNextA(lpszPath);
404     }
405   }
406   return (LPSTR)(lastpoint ? lastpoint : lpszPath);
407 }
408
409 /*************************************************************************
410  * PathFindExtensionW  [SHLWAPI.@]
411  *
412  * See PathFindExtensionA.
413  */
414 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
415 {
416   LPCWSTR lastpoint = NULL;
417
418   TRACE("(%s)\n", debugstr_w(lpszPath));
419
420   if (lpszPath)
421   {
422     while (*lpszPath)
423     {
424       if (*lpszPath == '\\' || *lpszPath==' ')
425         lastpoint = NULL;
426       else if (*lpszPath == '.')
427         lastpoint = lpszPath;
428       lpszPath++;
429     }
430   }
431   return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
432 }
433
434 /*************************************************************************
435  * PathGetArgsA    [SHLWAPI.@]
436  *
437  * Find the next argument in a string delimited by spaces.
438  *
439  * PARAMS
440  *  lpszPath [I] The string to search for arguments in
441  *
442  * RETURNS
443  *  The start of the next argument in lpszPath, or NULL if lpszPath is NULL
444  *
445  * NOTES
446  *  Spaces in quoted strings are ignored as delimiters.
447  */
448 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
449 {
450   BOOL bSeenQuote = FALSE;
451
452   TRACE("(%s)\n",debugstr_a(lpszPath));
453
454   if (lpszPath)
455   {
456     while (*lpszPath)
457     {
458       if ((*lpszPath==' ') && !bSeenQuote)
459         return (LPSTR)lpszPath + 1;
460       if (*lpszPath == '"')
461         bSeenQuote = !bSeenQuote;
462       lpszPath = CharNextA(lpszPath);
463     }
464   }
465   return (LPSTR)lpszPath;
466 }
467
468 /*************************************************************************
469  * PathGetArgsW    [SHLWAPI.@]
470  *
471  * See PathGetArgsA.
472  */
473 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
474 {
475   BOOL bSeenQuote = FALSE;
476
477   TRACE("(%s)\n",debugstr_w(lpszPath));
478
479   if (lpszPath)
480   {
481     while (*lpszPath)
482     {
483       if ((*lpszPath==' ') && !bSeenQuote)
484         return (LPWSTR)lpszPath + 1;
485       if (*lpszPath == '"')
486         bSeenQuote = !bSeenQuote;
487       lpszPath++;
488     }
489   }
490   return (LPWSTR)lpszPath;
491 }
492
493 /*************************************************************************
494  * PathGetDriveNumberA  [SHLWAPI.@]
495  *
496  * Return the drive number from a path
497  *
498  * PARAMS
499  *  lpszPath [I] Path to get the drive number from
500  *
501  * RETURNS
502  *  Success: The drive number corresponding to the drive in the path
503  *  Failure: -1, if lpszPath contains no valid drive
504  */
505 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
506 {
507   TRACE ("(%s)\n",debugstr_a(lpszPath));
508
509   if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
510       tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
511     return tolower(*lpszPath) - 'a';
512   return -1;
513 }
514
515 /*************************************************************************
516  * PathGetDriveNumberW  [SHLWAPI.@]
517  *
518  * See PathGetDriveNumberA.
519  */
520 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
521 {
522   TRACE ("(%s)\n",debugstr_w(lpszPath));
523
524   if (lpszPath)
525   {
526       WCHAR tl = tolowerW(lpszPath[0]);
527       if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
528           return tl - 'a';
529   }
530   return -1;
531 }
532
533 /*************************************************************************
534  * PathRemoveFileSpecA  [SHLWAPI.@]
535  *
536  * Remove the file specification from a path.
537  *
538  * PARAMS
539  *  lpszPath [I/O] Path to remove the file spec from
540  *
541  * RETURNS
542  *  TRUE  If the path was valid and modified
543  *  FALSE Otherwise
544  */
545 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
546 {
547   LPSTR lpszFileSpec = lpszPath;
548   BOOL bModified = FALSE;
549
550   TRACE("(%s)\n",debugstr_a(lpszPath));
551
552   if(lpszPath)
553   {
554     /* Skip directory or UNC path */
555     if (*lpszPath == '\\')
556       lpszFileSpec = ++lpszPath;
557     if (*lpszPath == '\\')
558       lpszFileSpec = ++lpszPath;
559
560     while (*lpszPath)
561     {
562       if(*lpszPath == '\\')
563         lpszFileSpec = lpszPath; /* Skip dir */
564       else if(*lpszPath == ':')
565       {
566         lpszFileSpec = ++lpszPath; /* Skip drive */
567         if (*lpszPath == '\\')
568           lpszFileSpec++;
569       }
570       if (!(lpszPath = CharNextA(lpszPath)))
571         break;
572     }
573
574     if (*lpszFileSpec)
575     {
576       *lpszFileSpec = '\0';
577       bModified = TRUE;
578     }
579   }
580   return bModified;
581 }
582
583 /*************************************************************************
584  * PathRemoveFileSpecW  [SHLWAPI.@]
585  *
586  * See PathRemoveFileSpecA.
587  */
588 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
589 {
590   LPWSTR lpszFileSpec = lpszPath;
591   BOOL bModified = FALSE;
592
593   TRACE("(%s)\n",debugstr_w(lpszPath));
594
595   if(lpszPath)
596   {
597     /* Skip directory or UNC path */
598     if (*lpszPath == '\\')
599       lpszFileSpec = ++lpszPath;
600     if (*lpszPath == '\\')
601       lpszFileSpec = ++lpszPath;
602
603     while (*lpszPath)
604     {
605       if(*lpszPath == '\\')
606         lpszFileSpec = lpszPath; /* Skip dir */
607       else if(*lpszPath == ':')
608       {
609         lpszFileSpec = ++lpszPath; /* Skip drive */
610         if (*lpszPath == '\\')
611           lpszFileSpec++;
612       }
613       lpszPath++;
614     }
615
616     if (*lpszFileSpec)
617     {
618       *lpszFileSpec = '\0';
619       bModified = TRUE;
620     }
621   }
622   return bModified;
623 }
624
625 /*************************************************************************
626  * PathStripPathA       [SHLWAPI.@]
627  *
628  * Remove the initial path from the beginning of a filename
629  *
630  * PARAMS
631  *  lpszPath [I/O] Path to remove the initial path from
632  *
633  * RETURNS
634  *  Nothing.
635  */
636 void WINAPI PathStripPathA(LPSTR lpszPath)
637 {
638   TRACE("(%s)\n", debugstr_a(lpszPath));
639
640   if (lpszPath)
641   {
642     LPSTR lpszFileName = PathFindFileNameA(lpszPath);
643     if(lpszFileName)
644       RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
645   }
646 }
647
648 /*************************************************************************
649  * PathStripPathW       [SHLWAPI.@]
650  *
651  * See PathStripPathA.
652  */
653 void WINAPI PathStripPathW(LPWSTR lpszPath)
654 {
655   LPWSTR lpszFileName;
656
657   TRACE("(%s)\n", debugstr_w(lpszPath));
658   lpszFileName = PathFindFileNameW(lpszPath);
659   if(lpszFileName)
660     RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
661 }
662
663 /*************************************************************************
664  * PathStripToRootA     [SHLWAPI.@]
665  *
666  * Reduce a path to its root.
667  *
668  * PARAMS
669  *  lpszPath [I/O] the path to reduce
670  *
671  * RETURNS
672  *  Success: TRUE if the stripped path is a root path
673  *  Failure: FALSE if the path cannot be stripped or is NULL
674  */
675 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
676 {
677   TRACE("(%s)\n", debugstr_a(lpszPath));
678
679   if (!lpszPath)
680     return FALSE;
681   while(!PathIsRootA(lpszPath))
682     if (!PathRemoveFileSpecA(lpszPath))
683       return FALSE;
684   return TRUE;
685 }
686
687 /*************************************************************************
688  * PathStripToRootW     [SHLWAPI.@]
689  *
690  * See PathStripToRootA.
691  */
692 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
693 {
694   TRACE("(%s)\n", debugstr_w(lpszPath));
695
696   if (!lpszPath)
697     return FALSE;
698   while(!PathIsRootW(lpszPath))
699     if (!PathRemoveFileSpecW(lpszPath))
700       return FALSE;
701   return TRUE;
702 }
703
704 /*************************************************************************
705  * PathRemoveArgsA      [SHLWAPI.@]
706  *
707  * Strip space separated arguments from a path.
708  *
709  * PARAMS
710  *  lpszPath [I/O] Path to remove arguments from
711  *
712  * RETURNS
713  *  Nothing.
714  */
715 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
716 {
717   TRACE("(%s)\n",debugstr_a(lpszPath));
718
719   if(lpszPath)
720   {
721     LPSTR lpszArgs = PathGetArgsA(lpszPath);
722     if (*lpszArgs)
723       lpszArgs[-1] = '\0';
724     else
725     {
726       LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
727       if(*lpszLastChar == ' ')
728         *lpszLastChar = '\0';
729     }
730   }
731 }
732
733 /*************************************************************************
734  * PathRemoveArgsW      [SHLWAPI.@]
735  *
736  * See PathRemoveArgsA.
737  */
738 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
739 {
740   TRACE("(%s)\n",debugstr_w(lpszPath));
741
742   if(lpszPath)
743   {
744     LPWSTR lpszArgs = PathGetArgsW(lpszPath);
745     if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
746       lpszArgs[-1] = '\0';
747   }
748 }
749
750 /*************************************************************************
751  * PathRemoveExtensionA         [SHLWAPI.@]
752  *
753  * Remove the file extension from a path
754  *
755  * PARAMS
756  *  lpszPath [I/O] Path to remove the extension from
757  *
758  * RETURNS
759  *  Nothing.
760  */
761 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
762 {
763   TRACE("(%s)\n", debugstr_a(lpszPath));
764
765   if (lpszPath)
766   {
767     lpszPath = PathFindExtensionA(lpszPath);
768     *lpszPath = '\0';
769   }
770 }
771
772 /*************************************************************************
773  * PathRemoveExtensionW         [SHLWAPI.@]
774  *
775  * See PathRemoveExtensionA.
776 */
777 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
778 {
779   TRACE("(%s)\n", debugstr_w(lpszPath));
780
781   if (lpszPath)
782   {
783     lpszPath = PathFindExtensionW(lpszPath);
784     *lpszPath = '\0';
785   }
786 }
787
788 /*************************************************************************
789  * PathRemoveBackslashA [SHLWAPI.@]
790  *
791  * Remove a trailing backslash from a path.
792  *
793  * PARAMS
794  *  lpszPath [I/O] Path to remove backslash from
795  *
796  * RETURNS
797  *  Success: A pointer to the end of the path
798  *  Failure: NULL, if lpszPath is NULL
799  */
800 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
801 {
802   LPSTR szTemp = NULL;
803
804   TRACE("(%s)\n", debugstr_a(lpszPath));
805
806   if(lpszPath)
807   {
808     szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
809     if (!PathIsRootA(lpszPath) && *szTemp == '\\')
810       *szTemp = '\0';
811   }
812   return szTemp;
813 }
814
815 /*************************************************************************
816  * PathRemoveBackslashW [SHLWAPI.@]
817  *
818  * See PathRemoveBackslashA.
819  */
820 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
821 {
822   LPWSTR szTemp = NULL;
823
824   TRACE("(%s)\n", debugstr_w(lpszPath));
825
826   if(lpszPath)
827   {
828     szTemp = lpszPath + strlenW(lpszPath);
829     if (szTemp > lpszPath) szTemp--;
830     if (!PathIsRootW(lpszPath) && *szTemp == '\\')
831       *szTemp = '\0';
832   }
833   return szTemp;
834 }
835
836 /*************************************************************************
837  * PathRemoveBlanksA [SHLWAPI.@]
838  *
839  * Remove Spaces from the start and end of a path.
840  *
841  * PARAMS
842  *  lpszPath [I/O] Path to strip blanks from
843  *
844  * RETURNS
845  *  Nothing.
846  */
847 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
848 {
849   TRACE("(%s)\n", debugstr_a(lpszPath));
850
851   if(lpszPath && *lpszPath)
852   {
853     LPSTR start = lpszPath;
854
855     while (*lpszPath == ' ')
856       lpszPath = CharNextA(lpszPath);
857
858     while(*lpszPath)
859       *start++ = *lpszPath++;
860
861     if (start != lpszPath)
862       while (start[-1] == ' ')
863         start--;
864     *start = '\0';
865   }
866 }
867
868 /*************************************************************************
869  * PathRemoveBlanksW [SHLWAPI.@]
870  *
871  * See PathRemoveBlanksA.
872  */
873 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
874 {
875   TRACE("(%s)\n", debugstr_w(lpszPath));
876
877   if(lpszPath && *lpszPath)
878   {
879     LPWSTR start = lpszPath;
880
881     while (*lpszPath == ' ')
882       lpszPath++;
883
884     while(*lpszPath)
885       *start++ = *lpszPath++;
886
887     if (start != lpszPath)
888       while (start[-1] == ' ')
889         start--;
890     *start = '\0';
891   }
892 }
893
894 /*************************************************************************
895  * PathQuoteSpacesA [SHLWAPI.@]
896  *
897  * Surround a path containing spaces in quotes.
898  *
899  * PARAMS
900  *  lpszPath [I/O] Path to quote
901  *
902  * RETURNS
903  *  Nothing.
904  *
905  * NOTES
906  *  The path is not changed if it is invalid or has no spaces.
907  */
908 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
909 {
910   TRACE("(%s)\n", debugstr_a(lpszPath));
911
912   if(lpszPath && StrChrA(lpszPath,' '))
913   {
914     size_t iLen = strlen(lpszPath) + 1;
915
916     if (iLen + 2 < MAX_PATH)
917     {
918       memmove(lpszPath + 1, lpszPath, iLen);
919       lpszPath[0] = '"';
920       lpszPath[iLen] = '"';
921       lpszPath[iLen + 1] = '\0';
922     }
923   }
924 }
925
926 /*************************************************************************
927  * PathQuoteSpacesW [SHLWAPI.@]
928  *
929  * See PathQuoteSpacesA.
930  */
931 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
932 {
933   TRACE("(%s)\n", debugstr_w(lpszPath));
934
935   if(lpszPath && StrChrW(lpszPath,' '))
936   {
937     int iLen = strlenW(lpszPath) + 1;
938
939     if (iLen + 2 < MAX_PATH)
940     {
941       memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
942       lpszPath[0] = '"';
943       lpszPath[iLen] = '"';
944       lpszPath[iLen + 1] = '\0';
945     }
946   }
947 }
948
949 /*************************************************************************
950  * PathUnquoteSpacesA [SHLWAPI.@]
951  *
952  * Remove quotes ("") from around a path, if present.
953  *
954  * PARAMS
955  *  lpszPath [I/O] Path to strip quotes from
956  *
957  * RETURNS
958  *  Nothing
959  *
960  * NOTES
961  *  If the path contains a single quote only, an empty string will result.
962  *  Otherwise quotes are only removed if they appear at the start and end
963  *  of the path.
964  */
965 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
966 {
967   TRACE("(%s)\n", debugstr_a(lpszPath));
968
969   if (lpszPath && *lpszPath == '"')
970   {
971     DWORD dwLen = strlen(lpszPath) - 1;
972
973     if (lpszPath[dwLen] == '"')
974     {
975       lpszPath[dwLen] = '\0';
976       for (; *lpszPath; lpszPath++)
977         *lpszPath = lpszPath[1];
978     }
979   }
980 }
981
982 /*************************************************************************
983  * PathUnquoteSpacesW [SHLWAPI.@]
984  *
985  * See PathUnquoteSpacesA.
986  */
987 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
988 {
989   TRACE("(%s)\n", debugstr_w(lpszPath));
990
991   if (lpszPath && *lpszPath == '"')
992   {
993     DWORD dwLen = strlenW(lpszPath) - 1;
994
995     if (lpszPath[dwLen] == '"')
996     {
997       lpszPath[dwLen] = '\0';
998       for (; *lpszPath; lpszPath++)
999         *lpszPath = lpszPath[1];
1000     }
1001   }
1002 }
1003
1004 /*************************************************************************
1005  * PathParseIconLocationA  [SHLWAPI.@]
1006  *
1007  * Parse the location of an icon from a path.
1008  *
1009  * PARAMS
1010  *  lpszPath [I/O] The path to parse the icon location from.
1011  *
1012  * RETURNS
1013  *  Success: The number of the icon
1014  *  Failure: 0 if the path does not contain an icon location or is NULL
1015  *
1016  * NOTES
1017  *  The path has surrounding quotes and spaces removed regardless
1018  *  of whether the call succeeds or not.
1019  */
1020 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1021 {
1022   int iRet = 0;
1023   LPSTR lpszComma;
1024
1025   TRACE("(%s)\n", debugstr_a(lpszPath));
1026
1027   if (lpszPath)
1028   {
1029     if ((lpszComma = strchr(lpszPath, ',')))
1030     {
1031       *lpszComma++ = '\0';
1032       iRet = StrToIntA(lpszComma);
1033     }
1034     PathUnquoteSpacesA(lpszPath);
1035     PathRemoveBlanksA(lpszPath);
1036   }
1037   return iRet;
1038 }
1039
1040 /*************************************************************************
1041  * PathParseIconLocationW  [SHLWAPI.@]
1042  *
1043  * See PathParseIconLocationA.
1044  */
1045 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1046 {
1047   int iRet = 0;
1048   LPWSTR lpszComma;
1049
1050   TRACE("(%s)\n", debugstr_w(lpszPath));
1051
1052   if (lpszPath)
1053   {
1054     if ((lpszComma = StrChrW(lpszPath, ',')))
1055     {
1056       *lpszComma++ = '\0';
1057       iRet = StrToIntW(lpszComma);
1058     }
1059     PathUnquoteSpacesW(lpszPath);
1060     PathRemoveBlanksW(lpszPath);
1061   }
1062   return iRet;
1063 }
1064
1065 /*************************************************************************
1066  * @    [SHLWAPI.4]
1067  *
1068  * Unicode version of PathFileExistsDefExtA.
1069  */
1070 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1071 {
1072   static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
1073                                        { '.', 'c', 'o', 'm', 0},
1074                                        { '.', 'e', 'x', 'e', 0},
1075                                        { '.', 'b', 'a', 't', 0},
1076                                        { '.', 'l', 'n', 'k', 0},
1077                                        { '.', 'c', 'm', 'd', 0},
1078                                        { 0, 0, 0, 0, 0} };
1079
1080   TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1081
1082   if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1083     return FALSE;
1084
1085   if (dwWhich)
1086   {
1087     LPCWSTR szExt = PathFindExtensionW(lpszPath);
1088     if (!*szExt || dwWhich & 0x40)
1089     {
1090       size_t iChoose = 0;
1091       int iLen = lstrlenW(lpszPath);
1092       if (iLen > (MAX_PATH - 5))
1093         return FALSE;
1094       while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1095       {
1096         lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1097         if (PathFileExistsW(lpszPath))
1098           return TRUE;
1099         iChoose++;
1100         dwWhich >>= 1;
1101       }
1102       *(lpszPath + iLen) = (WCHAR)'\0';
1103       return FALSE;
1104     }
1105   }
1106   return PathFileExistsW(lpszPath);
1107 }
1108
1109 /*************************************************************************
1110  * @    [SHLWAPI.3]
1111  *
1112  * Determine if a file exists locally and is of an executable type.
1113  *
1114  * PARAMS
1115  *  lpszPath       [I/O] File to search for
1116  *  dwWhich        [I]   Type of executable to search for
1117  *
1118  * RETURNS
1119  *  TRUE  If the file was found. lpszPath contains the file name.
1120  *  FALSE Otherwise.
1121  *
1122  * NOTES
1123  *  lpszPath is modified in place and must be at least MAX_PATH in length.
1124  *  If the function returns FALSE, the path is modified to its original state.
1125  *  If the given path contains an extension or dwWhich is 0, executable
1126  *  extensions are not checked.
1127  *
1128  *  Ordinals 3-6 are a classic case of MS exposing limited functionality to
1129  *  users (here through PathFindOnPathA()) and keeping advanced functionality for
1130  *  their own developers exclusive use. Monopoly, anyone?
1131  */
1132 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1133 {
1134   BOOL bRet = FALSE;
1135
1136   TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1137
1138   if (lpszPath)
1139   {
1140     WCHAR szPath[MAX_PATH];
1141     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1142     bRet = PathFileExistsDefExtW(szPath, dwWhich);
1143     if (bRet)
1144       WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1145   }
1146   return bRet;
1147 }
1148
1149 /*************************************************************************
1150  * SHLWAPI_PathFindInOtherDirs
1151  *
1152  * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1153  */
1154 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1155 {
1156   static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1157   static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1158   DWORD dwLenPATH;
1159   LPCWSTR lpszCurr;
1160   WCHAR *lpszPATH;
1161   WCHAR buff[MAX_PATH];
1162
1163   TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1164
1165   /* Try system directories */
1166   GetSystemDirectoryW(buff, MAX_PATH);
1167   if (!PathAppendW(buff, lpszFile))
1168      return FALSE;
1169   if (PathFileExistsDefExtW(buff, dwWhich))
1170   {
1171     strcpyW(lpszFile, buff);
1172     return TRUE;
1173   }
1174   GetWindowsDirectoryW(buff, MAX_PATH);
1175   if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1176     return FALSE;
1177   if (PathFileExistsDefExtW(buff, dwWhich))
1178   {
1179     strcpyW(lpszFile, buff);
1180     return TRUE;
1181   }
1182   GetWindowsDirectoryW(buff, MAX_PATH);
1183   if (!PathAppendW(buff, lpszFile))
1184     return FALSE;
1185   if (PathFileExistsDefExtW(buff, dwWhich))
1186   {
1187     strcpyW(lpszFile, buff);
1188     return TRUE;
1189   }
1190   /* Try dirs listed in %PATH% */
1191   dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1192
1193   if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1194     return FALSE;
1195
1196   GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1197   lpszCurr = lpszPATH;
1198   while (lpszCurr)
1199   {
1200     LPCWSTR lpszEnd = lpszCurr;
1201     LPWSTR pBuff = buff;
1202
1203     while (*lpszEnd == ' ')
1204       lpszEnd++;
1205     while (*lpszEnd && *lpszEnd != ';')
1206       *pBuff++ = *lpszEnd++;
1207     *pBuff = '\0';
1208
1209     if (*lpszEnd)
1210       lpszCurr = lpszEnd + 1;
1211     else
1212       lpszCurr = NULL; /* Last Path, terminate after this */
1213
1214     if (!PathAppendW(buff, lpszFile))
1215     {
1216       HeapFree(GetProcessHeap(), 0, lpszPATH);
1217       return FALSE;
1218     }
1219     if (PathFileExistsDefExtW(buff, dwWhich))
1220     {
1221       strcpyW(lpszFile, buff);
1222       HeapFree(GetProcessHeap(), 0, lpszPATH);
1223       return TRUE;
1224     }
1225   }
1226   HeapFree(GetProcessHeap(), 0, lpszPATH);
1227   return FALSE;
1228 }
1229
1230 /*************************************************************************
1231  * @    [SHLWAPI.5]
1232  *
1233  * Search a range of paths for a specific type of executable.
1234  *
1235  * PARAMS
1236  *  lpszFile       [I/O] File to search for
1237  *  lppszOtherDirs [I]   Other directories to look in
1238  *  dwWhich        [I]   Type of executable to search for
1239  *
1240  * RETURNS
1241  *  Success: TRUE. The path to the executable is stored in lpszFile.
1242  *  Failure: FALSE. The path to the executable is unchanged.
1243  */
1244 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1245 {
1246   WCHAR szFile[MAX_PATH];
1247   WCHAR buff[MAX_PATH];
1248
1249   TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1250
1251   if (!lpszFile || !PathIsFileSpecA(lpszFile))
1252     return FALSE;
1253
1254   MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1255
1256   /* Search provided directories first */
1257   if (lppszOtherDirs && *lppszOtherDirs)
1258   {
1259     WCHAR szOther[MAX_PATH];
1260     LPCSTR *lpszOtherPath = lppszOtherDirs;
1261
1262     while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1263     {
1264       MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1265       PathCombineW(buff, szOther, szFile);
1266       if (PathFileExistsDefExtW(buff, dwWhich))
1267       {
1268         WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1269         return TRUE;
1270       }
1271       lpszOtherPath++;
1272     }
1273   }
1274   /* Not found, try system and path dirs */
1275   if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1276   {
1277     WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1278     return TRUE;
1279   }
1280   return FALSE;
1281 }
1282
1283 /*************************************************************************
1284  * @    [SHLWAPI.6]
1285  *
1286  * Unicode version of PathFindOnPathExA.
1287  */
1288 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1289 {
1290   WCHAR buff[MAX_PATH];
1291
1292   TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1293
1294   if (!lpszFile || !PathIsFileSpecW(lpszFile))
1295     return FALSE;
1296
1297   /* Search provided directories first */
1298   if (lppszOtherDirs && *lppszOtherDirs)
1299   {
1300     LPCWSTR *lpszOtherPath = lppszOtherDirs;
1301     while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1302     {
1303       PathCombineW(buff, *lpszOtherPath, lpszFile);
1304       if (PathFileExistsDefExtW(buff, dwWhich))
1305       {
1306         strcpyW(lpszFile, buff);
1307         return TRUE;
1308       }
1309       lpszOtherPath++;
1310     }
1311   }
1312   /* Not found, try system and path dirs */
1313   return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1314 }
1315
1316 /*************************************************************************
1317  * PathFindOnPathA      [SHLWAPI.@]
1318  *
1319  * Search a range of paths for an executable.
1320  *
1321  * PARAMS
1322  *  lpszFile       [I/O] File to search for
1323  *  lppszOtherDirs [I]   Other directories to look in
1324  *
1325  * RETURNS
1326  *  Success: TRUE. The path to the executable is stored in lpszFile.
1327  *  Failure: FALSE. The path to the executable is unchanged.
1328  */
1329 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1330 {
1331   TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1332   return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1333  }
1334
1335 /*************************************************************************
1336  * PathFindOnPathW      [SHLWAPI.@]
1337  *
1338  * See PathFindOnPathA.
1339  */
1340 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1341 {
1342   TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1343   return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1344 }
1345
1346 /*************************************************************************
1347  * PathCompactPathExA   [SHLWAPI.@]
1348  *
1349  * Compact a path into a given number of characters.
1350  *
1351  * PARAMS
1352  *  lpszDest [O] Destination for compacted path
1353  *  lpszPath [I] Source path
1354  *  cchMax   [I] Maximum size of compacted path
1355  *  dwFlags  [I] Reserved
1356  *
1357  * RETURNS
1358  *  Success: TRUE. The compacted path is written to lpszDest.
1359  *  Failure: FALSE. lpszPath is undefined.
1360  *
1361  * NOTES
1362  *  If cchMax is given as 0, lpszDest will still be NUL terminated.
1363  *
1364  *  The Win32 version of this function contains a bug: When cchMax == 7,
1365  *  8 bytes will be written to lpszDest. This bug is fixed in the Wine
1366  *  implementation.
1367  *
1368  *  Some relative paths will be different when cchMax == 5 or 6. This occurs
1369  *  because Win32 will insert a "\" in lpszDest, even if one is
1370  *  not present in the original path.
1371  */
1372 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1373                                UINT cchMax, DWORD dwFlags)
1374 {
1375   BOOL bRet = FALSE;
1376
1377   TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1378
1379   if (lpszPath && lpszDest)
1380   {
1381     WCHAR szPath[MAX_PATH];
1382     WCHAR szDest[MAX_PATH];
1383
1384     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1385     szDest[0] = '\0';
1386     bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1387     WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1388   }
1389   return bRet;
1390 }
1391
1392 /*************************************************************************
1393  * PathCompactPathExW   [SHLWAPI.@]
1394  *
1395  * See PathCompactPathExA.
1396  */
1397 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1398                                UINT cchMax, DWORD dwFlags)
1399 {
1400   static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1401   LPCWSTR lpszFile;
1402   DWORD dwLen, dwFileLen = 0;
1403
1404   TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1405
1406   if (!lpszPath)
1407     return FALSE;
1408
1409   if (!lpszDest)
1410   {
1411     WARN("Invalid lpszDest would crash under Win32!\n");
1412     return FALSE;
1413   }
1414
1415   *lpszDest = '\0';
1416
1417   if (cchMax < 2)
1418     return TRUE;
1419
1420   dwLen = strlenW(lpszPath) + 1;
1421
1422   if (dwLen < cchMax)
1423   {
1424     /* Don't need to compact */
1425     memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1426     return TRUE;
1427   }
1428
1429   /* Path must be compacted to fit into lpszDest */
1430   lpszFile = PathFindFileNameW(lpszPath);
1431   dwFileLen = lpszPath + dwLen - lpszFile;
1432
1433   if (dwFileLen == dwLen)
1434   {
1435     /* No root in psth */
1436     if (cchMax <= 4)
1437     {
1438       while (--cchMax > 0) /* No room left for anything but ellipses */
1439         *lpszDest++ = '.';
1440       *lpszDest = '\0';
1441       return TRUE;
1442     }
1443     /* Compact the file name with ellipses at the end */
1444     cchMax -= 4;
1445     memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1446     strcpyW(lpszDest + cchMax, szEllipses);
1447     return TRUE;
1448   }
1449   /* We have a root in the path */
1450   lpszFile--; /* Start compacted filename with the path separator */
1451   dwFileLen++;
1452
1453   if (dwFileLen + 3 > cchMax)
1454   {
1455     /* Compact the file name */
1456     if (cchMax <= 4)
1457     {
1458       while (--cchMax > 0) /* No room left for anything but ellipses */
1459         *lpszDest++ = '.';
1460       *lpszDest = '\0';
1461       return TRUE;
1462     }
1463     strcpyW(lpszDest, szEllipses);
1464     lpszDest += 3;
1465     cchMax -= 4;
1466     *lpszDest++ = *lpszFile++;
1467     if (cchMax <= 4)
1468     {
1469       while (--cchMax > 0) /* No room left for anything but ellipses */
1470         *lpszDest++ = '.';
1471       *lpszDest = '\0';
1472       return TRUE;
1473     }
1474     cchMax -= 4;
1475     memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1476     strcpyW(lpszDest + cchMax, szEllipses);
1477     return TRUE;
1478   }
1479
1480   /* Only the root needs to be Compacted */
1481   dwLen = cchMax - dwFileLen - 3;
1482   memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1483   strcpyW(lpszDest + dwLen, szEllipses);
1484   strcpyW(lpszDest + dwLen + 3, lpszFile);
1485   return TRUE;
1486 }
1487
1488 /*************************************************************************
1489  * PathIsRelativeA      [SHLWAPI.@]
1490  *
1491  * Determine if a path is a relative path.
1492  *
1493  * PARAMS
1494  *  lpszPath [I] Path to check
1495  *
1496  * RETURNS
1497  *  TRUE:  The path is relative, or is invalid.
1498  *  FALSE: The path is not relative.
1499  */
1500 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1501 {
1502   TRACE("(%s)\n",debugstr_a(lpszPath));
1503
1504   if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1505     return TRUE;
1506   if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1507     return FALSE;
1508   return TRUE;
1509 }
1510
1511 /*************************************************************************
1512  *  PathIsRelativeW     [SHLWAPI.@]
1513  *
1514  * See PathIsRelativeA.
1515  */
1516 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1517 {
1518   TRACE("(%s)\n",debugstr_w(lpszPath));
1519
1520   if (!lpszPath || !*lpszPath)
1521     return TRUE;
1522   if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1523     return FALSE;
1524   return TRUE;
1525 }
1526
1527 /*************************************************************************
1528  * PathIsRootA          [SHLWAPI.@]
1529  *
1530  * Determine if a path is a root path.
1531  *
1532  * PARAMS
1533  *  lpszPath [I] Path to check
1534  *
1535  * RETURNS
1536  *  TRUE  If lpszPath is valid and a root path,
1537  *  FALSE Otherwise
1538  */
1539 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1540 {
1541   TRACE("(%s)\n", debugstr_a(lpszPath));
1542
1543   if (lpszPath && *lpszPath)
1544   {
1545     if (*lpszPath == '\\')
1546     {
1547       if (!lpszPath[1])
1548         return TRUE; /* \ */
1549       else if (lpszPath[1]=='\\')
1550       {
1551         BOOL bSeenSlash = FALSE;
1552         lpszPath += 2;
1553
1554         /* Check for UNC root path */
1555         while (*lpszPath)
1556         {
1557           if (*lpszPath == '\\')
1558           {
1559             if (bSeenSlash)
1560               return FALSE;
1561             bSeenSlash = TRUE;
1562           }
1563           lpszPath = CharNextA(lpszPath);
1564         }
1565         return TRUE;
1566       }
1567     }
1568     else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1569       return TRUE; /* X:\ */
1570   }
1571   return FALSE;
1572 }
1573
1574 /*************************************************************************
1575  * PathIsRootW          [SHLWAPI.@]
1576  *
1577  * See PathIsRootA.
1578  */
1579 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1580 {
1581   TRACE("(%s)\n", debugstr_w(lpszPath));
1582
1583   if (lpszPath && *lpszPath)
1584   {
1585     if (*lpszPath == '\\')
1586     {
1587       if (!lpszPath[1])
1588         return TRUE; /* \ */
1589       else if (lpszPath[1]=='\\')
1590       {
1591         BOOL bSeenSlash = FALSE;
1592         lpszPath += 2;
1593
1594         /* Check for UNC root path */
1595         while (*lpszPath)
1596         {
1597           if (*lpszPath == '\\')
1598           {
1599             if (bSeenSlash)
1600               return FALSE;
1601             bSeenSlash = TRUE;
1602           }
1603           lpszPath++;
1604         }
1605         return TRUE;
1606       }
1607     }
1608     else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1609       return TRUE; /* X:\ */
1610   }
1611   return FALSE;
1612 }
1613
1614 /*************************************************************************
1615  * PathIsDirectoryA     [SHLWAPI.@]
1616  *
1617  * Determine if a path is a valid directory
1618  *
1619  * PARAMS
1620  *  lpszPath [I] Path to check.
1621  *
1622  * RETURNS
1623  *  FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1624  *  FALSE if lpszPath is invalid or not a directory.
1625  *
1626  * NOTES
1627  *  Although this function is prototyped as returning a BOOL, it returns
1628  *  FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1629  *
1630  *|  if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1631  *|    ...
1632  *
1633  *  will always fail.
1634  */
1635 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1636 {
1637   DWORD dwAttr;
1638
1639   TRACE("(%s)\n", debugstr_a(lpszPath));
1640
1641   if (!lpszPath || PathIsUNCServerA(lpszPath))
1642     return FALSE;
1643
1644   if (PathIsUNCServerShareA(lpszPath))
1645   {
1646     FIXME("UNC Server Share not yet supported - FAILING\n");
1647     return FALSE;
1648   }
1649
1650   if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1651     return FALSE;
1652   return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1653 }
1654
1655 /*************************************************************************
1656  * PathIsDirectoryW     [SHLWAPI.@]
1657  *
1658  * See PathIsDirectoryA.
1659  */
1660 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1661 {
1662   DWORD dwAttr;
1663
1664   TRACE("(%s)\n", debugstr_w(lpszPath));
1665
1666   if (!lpszPath || PathIsUNCServerW(lpszPath))
1667     return FALSE;
1668
1669   if (PathIsUNCServerShareW(lpszPath))
1670   {
1671     FIXME("UNC Server Share not yet supported - FAILING\n");
1672     return FALSE;
1673   }
1674
1675   if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1676     return FALSE;
1677   return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1678 }
1679
1680 /*************************************************************************
1681  * PathFileExistsA      [SHLWAPI.@]
1682  *
1683  * Determine if a file exists.
1684  *
1685  * PARAMS
1686  *  lpszPath [I] Path to check
1687  *
1688  * RETURNS
1689  *  TRUE  If the file exists and is readable
1690  *  FALSE Otherwise
1691  */
1692 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1693 {
1694   UINT iPrevErrMode;
1695   DWORD dwAttr;
1696
1697   TRACE("(%s)\n",debugstr_a(lpszPath));
1698
1699   if (!lpszPath)
1700     return FALSE;
1701
1702   /* Prevent a dialog box if path is on a disk that has been ejected. */
1703   iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1704   dwAttr = GetFileAttributesA(lpszPath);
1705   SetErrorMode(iPrevErrMode);
1706   return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1707 }
1708
1709 /*************************************************************************
1710  * PathFileExistsW      [SHLWAPI.@]
1711  *
1712  * See PathFileExistsA.
1713  */
1714 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1715 {
1716   UINT iPrevErrMode;
1717   DWORD dwAttr;
1718
1719   TRACE("(%s)\n",debugstr_w(lpszPath));
1720
1721   if (!lpszPath)
1722     return FALSE;
1723
1724   iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1725   dwAttr = GetFileAttributesW(lpszPath);
1726   SetErrorMode(iPrevErrMode);
1727   return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1728 }
1729
1730 /*************************************************************************
1731  * PathFileExistsAndAttributesA [SHLWAPI.445]
1732  *
1733  * Determine if a file exists.
1734  *
1735  * PARAMS
1736  *  lpszPath [I] Path to check
1737  *  dwAttr   [O] attributes of file
1738  *
1739  * RETURNS
1740  *  TRUE  If the file exists and is readable
1741  *  FALSE Otherwise
1742  */
1743 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1744 {
1745   UINT iPrevErrMode;
1746   DWORD dwVal = 0;
1747
1748   TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1749
1750   if (dwAttr)
1751     *dwAttr = INVALID_FILE_ATTRIBUTES;
1752
1753   if (!lpszPath)
1754     return FALSE;
1755
1756   iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1757   dwVal = GetFileAttributesA(lpszPath);
1758   SetErrorMode(iPrevErrMode);
1759   if (dwAttr)
1760     *dwAttr = dwVal;
1761   return (dwVal != INVALID_FILE_ATTRIBUTES);
1762 }
1763
1764 /*************************************************************************
1765  * PathFileExistsAndAttributesW [SHLWAPI.446]
1766  *
1767  * See PathFileExistsA.
1768  */
1769 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1770 {
1771   UINT iPrevErrMode;
1772   DWORD dwVal;
1773
1774   TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1775
1776   if (!lpszPath)
1777     return FALSE;
1778
1779   iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1780   dwVal = GetFileAttributesW(lpszPath);
1781   SetErrorMode(iPrevErrMode);
1782   if (dwAttr)
1783     *dwAttr = dwVal;
1784   return (dwVal != INVALID_FILE_ATTRIBUTES);
1785 }
1786
1787 /*************************************************************************
1788  * PathMatchSingleMaskA [internal]
1789  */
1790 static BOOL WINAPI PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1791 {
1792   while (*name && *mask && *mask!=';')
1793   {
1794     if (*mask == '*')
1795     {
1796       do
1797       {
1798         if (PathMatchSingleMaskA(name,mask+1))
1799           return TRUE;  /* try substrings */
1800       } while (*name++);
1801       return FALSE;
1802     }
1803
1804     if (toupper(*mask) != toupper(*name) && *mask != '?')
1805       return FALSE;
1806
1807     name = CharNextA(name);
1808     mask = CharNextA(mask);
1809   }
1810
1811   if (!*name)
1812   {
1813     while (*mask == '*')
1814       mask++;
1815     if (!*mask || *mask == ';')
1816       return TRUE;
1817   }
1818   return FALSE;
1819 }
1820
1821 /*************************************************************************
1822  * PathMatchSingleMaskW [internal]
1823  */
1824 static BOOL WINAPI PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1825 {
1826   while (*name && *mask && *mask != ';')
1827   {
1828     if (*mask == '*')
1829     {
1830       do
1831       {
1832         if (PathMatchSingleMaskW(name,mask+1))
1833           return TRUE;  /* try substrings */
1834       } while (*name++);
1835       return FALSE;
1836     }
1837
1838     if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1839       return FALSE;
1840
1841     name++;
1842     mask++;
1843   }
1844   if (!*name)
1845   {
1846     while (*mask == '*')
1847       mask++;
1848     if (!*mask || *mask == ';')
1849       return TRUE;
1850   }
1851   return FALSE;
1852 }
1853
1854 /*************************************************************************
1855  * PathMatchSpecA       [SHLWAPI.@]
1856  *
1857  * Determine if a path matches one or more search masks.
1858  *
1859  * PARAMS
1860  *  lpszPath [I] Path to check
1861  *  lpszMask [I] Search mask(s)
1862  *
1863  * RETURNS
1864  *  TRUE  If lpszPath is valid and is matched
1865  *  FALSE Otherwise
1866  *
1867  * NOTES
1868  *  Multiple search masks may be given if they are separated by ";". The
1869  *  pattern "*.*" is treated specially in that it matches all paths (for
1870  *  backwards compatibility with DOS).
1871  */
1872 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1873 {
1874   TRACE("(%s,%s)\n", lpszPath, lpszMask);
1875
1876   if (!lstrcmpA(lpszMask, "*.*"))
1877     return TRUE; /* Matches every path */
1878
1879   while (*lpszMask)
1880   {
1881     while (*lpszMask == ' ')
1882       lpszMask++; /* Eat leading spaces */
1883
1884     if (PathMatchSingleMaskA(lpszPath, lpszMask))
1885       return TRUE; /* Matches the current mask */
1886
1887     while (*lpszMask && *lpszMask != ';')
1888       lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1889
1890     if (*lpszMask == ';')
1891       lpszMask++;
1892   }
1893   return FALSE;
1894 }
1895
1896 /*************************************************************************
1897  * PathMatchSpecW       [SHLWAPI.@]
1898  *
1899  * See PathMatchSpecA.
1900  */
1901 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1902 {
1903   static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1904
1905   TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1906
1907   if (!lstrcmpW(lpszMask, szStarDotStar))
1908     return TRUE; /* Matches every path */
1909
1910   while (*lpszMask)
1911   {
1912     while (*lpszMask == ' ')
1913       lpszMask++; /* Eat leading spaces */
1914
1915     if (PathMatchSingleMaskW(lpszPath, lpszMask))
1916       return TRUE; /* Matches the current path */
1917
1918     while (*lpszMask && *lpszMask != ';')
1919       lpszMask++; /* masks separated by ';' */
1920
1921     if (*lpszMask == ';')
1922       lpszMask++;
1923   }
1924   return FALSE;
1925 }
1926
1927 /*************************************************************************
1928  * PathIsSameRootA      [SHLWAPI.@]
1929  *
1930  * Determine if two paths share the same root.
1931  *
1932  * PARAMS
1933  *  lpszPath1 [I] Source path
1934  *  lpszPath2 [I] Path to compare with
1935  *
1936  * RETURNS
1937  *  TRUE  If both paths are valid and share the same root.
1938  *  FALSE If either path is invalid or the paths do not share the same root.
1939  */
1940 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1941 {
1942   LPCSTR lpszStart;
1943   int dwLen;
1944
1945   TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1946
1947   if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1948     return FALSE;
1949
1950   dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1951   if (lpszStart - lpszPath1 > dwLen)
1952     return FALSE; /* Paths not common up to length of the root */
1953   return TRUE;
1954 }
1955
1956 /*************************************************************************
1957  * PathIsSameRootW      [SHLWAPI.@]
1958  *
1959  * See PathIsSameRootA.
1960  */
1961 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1962 {
1963   LPCWSTR lpszStart;
1964   int dwLen;
1965
1966   TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1967
1968   if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1969     return FALSE;
1970
1971   dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1972   if (lpszStart - lpszPath1 > dwLen)
1973     return FALSE; /* Paths not common up to length of the root */
1974   return TRUE;
1975 }
1976
1977 /*************************************************************************
1978  * PathIsContentTypeA   [SHLWAPI.@]
1979  *
1980  * Determine if a file is of a given registered content type.
1981  *
1982  * PARAMS
1983  *  lpszPath        [I] File to check
1984  *  lpszContentType [I] Content type to check for
1985  *
1986  * RETURNS
1987  *  TRUE  If lpszPath is a given registered content type,
1988  *  FALSE Otherwise.
1989  *
1990  * NOTES
1991  *  This function looks up the registered content type for lpszPath. If
1992  *  a content type is registered, it is compared (case insensitively) to
1993  *  lpszContentType. Only if this matches does the function succeed.
1994  */
1995 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1996 {
1997   LPCSTR szExt;
1998   DWORD dwDummy;
1999   char szBuff[MAX_PATH];
2000
2001   TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2002
2003   if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2004       !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2005                    REG_NONE, szBuff, &dwDummy) &&
2006       !strcasecmp(lpszContentType, szBuff))
2007   {
2008     return TRUE;
2009   }
2010   return FALSE;
2011 }
2012
2013 /*************************************************************************
2014  * PathIsContentTypeW   [SHLWAPI.@]
2015  *
2016  * See PathIsContentTypeA.
2017  */
2018 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2019 {
2020   static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2021   LPCWSTR szExt;
2022   DWORD dwDummy;
2023   WCHAR szBuff[MAX_PATH];
2024
2025   TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2026
2027   if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2028       !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2029                    REG_NONE, szBuff, &dwDummy) &&
2030       !strcmpiW(lpszContentType, szBuff))
2031   {
2032     return TRUE;
2033   }
2034   return FALSE;
2035 }
2036
2037 /*************************************************************************
2038  * PathIsFileSpecA   [SHLWAPI.@]
2039  *
2040  * Determine if a path is a file specification.
2041  *
2042  * PARAMS
2043  *  lpszPath [I] Path to check
2044  *
2045  * RETURNS
2046  *  TRUE  If lpszPath is a file specification (i.e. Contains no directories).
2047  *  FALSE Otherwise.
2048  */
2049 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2050 {
2051   TRACE("(%s)\n", debugstr_a(lpszPath));
2052
2053   if (!lpszPath)
2054     return FALSE;
2055
2056   while (*lpszPath)
2057   {
2058     if (*lpszPath == '\\' || *lpszPath == ':')
2059       return FALSE;
2060     lpszPath = CharNextA(lpszPath);
2061   }
2062   return TRUE;
2063 }
2064
2065 /*************************************************************************
2066  * PathIsFileSpecW   [SHLWAPI.@]
2067  *
2068  * See PathIsFileSpecA.
2069  */
2070 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2071 {
2072   TRACE("(%s)\n", debugstr_w(lpszPath));
2073
2074   if (!lpszPath)
2075     return FALSE;
2076
2077   while (*lpszPath)
2078   {
2079     if (*lpszPath == '\\' || *lpszPath == ':')
2080       return FALSE;
2081     lpszPath++;
2082   }
2083   return TRUE;
2084 }
2085
2086 /*************************************************************************
2087  * PathIsPrefixA   [SHLWAPI.@]
2088  *
2089  * Determine if a path is a prefix of another.
2090  *
2091  * PARAMS
2092  *  lpszPrefix [I] Prefix
2093  *  lpszPath   [I] Path to check
2094  *
2095  * RETURNS
2096  *  TRUE  If lpszPath has lpszPrefix as its prefix,
2097  *  FALSE If either path is NULL or lpszPrefix is not a prefix
2098  */
2099 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2100 {
2101   TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2102
2103   if (lpszPrefix && lpszPath &&
2104       PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2105     return TRUE;
2106   return FALSE;
2107 }
2108
2109 /*************************************************************************
2110  *  PathIsPrefixW   [SHLWAPI.@]
2111  *
2112  *  See PathIsPrefixA.
2113  */
2114 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2115 {
2116   TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2117
2118   if (lpszPrefix && lpszPath &&
2119       PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2120     return TRUE;
2121   return FALSE;
2122 }
2123
2124 /*************************************************************************
2125  * PathIsSystemFolderA   [SHLWAPI.@]
2126  *
2127  * Determine if a path or file attributes are a system folder.
2128  *
2129  * PARAMS
2130  *  lpszPath  [I] Path to check.
2131  *  dwAttrib  [I] Attributes to check, if lpszPath is NULL.
2132  *
2133  * RETURNS
2134  *  TRUE   If lpszPath or dwAttrib are a system folder.
2135  *  FALSE  If GetFileAttributesA() fails or neither parameter is a system folder.
2136  */
2137 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2138 {
2139   TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2140
2141   if (lpszPath && *lpszPath)
2142     dwAttrib = GetFileAttributesA(lpszPath);
2143
2144   if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2145       !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2146     return FALSE;
2147   return TRUE;
2148 }
2149
2150 /*************************************************************************
2151  * PathIsSystemFolderW   [SHLWAPI.@]
2152  *
2153  * See PathIsSystemFolderA.
2154  */
2155 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2156 {
2157   TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2158
2159   if (lpszPath && *lpszPath)
2160     dwAttrib = GetFileAttributesW(lpszPath);
2161
2162   if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2163       !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2164     return FALSE;
2165   return TRUE;
2166 }
2167
2168 /*************************************************************************
2169  * PathIsUNCA           [SHLWAPI.@]
2170  *
2171  * Determine if a path is in UNC format.
2172  *
2173  * PARAMS
2174  *  lpszPath [I] Path to check
2175  *
2176  * RETURNS
2177  *  TRUE: The path is UNC.
2178  *  FALSE: The path is not UNC or is NULL.
2179  */
2180 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2181 {
2182   TRACE("(%s)\n",debugstr_a(lpszPath));
2183
2184   if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2185     return TRUE;
2186   return FALSE;
2187 }
2188
2189 /*************************************************************************
2190  * PathIsUNCW           [SHLWAPI.@]
2191  *
2192  * See PathIsUNCA.
2193  */
2194 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2195 {
2196   TRACE("(%s)\n",debugstr_w(lpszPath));
2197
2198   if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2199     return TRUE;
2200   return FALSE;
2201 }
2202
2203 /*************************************************************************
2204  * PathIsUNCServerA   [SHLWAPI.@]
2205  *
2206  * Determine if a path is a UNC server name ("\\SHARENAME").
2207  *
2208  * PARAMS
2209  *  lpszPath  [I] Path to check.
2210  *
2211  * RETURNS
2212  *  TRUE   If lpszPath is a valid UNC server name.
2213  *  FALSE  Otherwise.
2214  *
2215  * NOTES
2216  *  This routine is bug compatible with Win32: Server names with a
2217  *  trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2218  *  Fixing this bug may break other shlwapi functions!
2219  */
2220 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2221 {
2222   TRACE("(%s)\n", debugstr_a(lpszPath));
2223
2224   if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2225   {
2226     while (*lpszPath)
2227     {
2228       if (*lpszPath == '\\')
2229         return FALSE;
2230       lpszPath = CharNextA(lpszPath);
2231     }
2232     return TRUE;
2233   }
2234   return FALSE;
2235 }
2236
2237 /*************************************************************************
2238  * PathIsUNCServerW   [SHLWAPI.@]
2239  *
2240  * See PathIsUNCServerA.
2241  */
2242 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2243 {
2244   TRACE("(%s)\n", debugstr_w(lpszPath));
2245
2246   if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2247   {
2248       return !strchrW( lpszPath + 2, '\\' );
2249   }
2250   return FALSE;
2251 }
2252
2253 /*************************************************************************
2254  * PathIsUNCServerShareA   [SHLWAPI.@]
2255  *
2256  * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2257  *
2258  * PARAMS
2259  *  lpszPath  [I] Path to check.
2260  *
2261  * RETURNS
2262  *  TRUE   If lpszPath is a valid UNC server share.
2263  *  FALSE  Otherwise.
2264  *
2265  * NOTES
2266  *  This routine is bug compatible with Win32: Server shares with a
2267  *  trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2268  *  Fixing this bug may break other shlwapi functions!
2269  */
2270 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2271 {
2272   TRACE("(%s)\n", debugstr_a(lpszPath));
2273
2274   if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2275   {
2276     BOOL bSeenSlash = FALSE;
2277     while (*lpszPath)
2278     {
2279       if (*lpszPath == '\\')
2280       {
2281         if (bSeenSlash)
2282           return FALSE;
2283         bSeenSlash = TRUE;
2284       }
2285       lpszPath = CharNextA(lpszPath);
2286     }
2287     return bSeenSlash;
2288   }
2289   return FALSE;
2290 }
2291
2292 /*************************************************************************
2293  * PathIsUNCServerShareW   [SHLWAPI.@]
2294  *
2295  * See PathIsUNCServerShareA.
2296  */
2297 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2298 {
2299   TRACE("(%s)\n", debugstr_w(lpszPath));
2300
2301   if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2302   {
2303     BOOL bSeenSlash = FALSE;
2304     while (*lpszPath)
2305     {
2306       if (*lpszPath == '\\')
2307       {
2308         if (bSeenSlash)
2309           return FALSE;
2310         bSeenSlash = TRUE;
2311       }
2312       lpszPath++;
2313     }
2314     return bSeenSlash;
2315   }
2316   return FALSE;
2317 }
2318
2319 /*************************************************************************
2320  * PathCanonicalizeA   [SHLWAPI.@]
2321  *
2322  * Convert a path to its canonical form.
2323  *
2324  * PARAMS
2325  *  lpszBuf  [O] Output path
2326  *  lpszPath [I] Path to canonicalize
2327  *
2328  * RETURNS
2329  *  Success: TRUE.  lpszBuf contains the output path,
2330  *  Failure: FALSE, If input path is invalid. lpszBuf is undefined
2331  */
2332 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2333 {
2334   BOOL bRet = FALSE;
2335
2336   TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2337
2338   if (lpszBuf)
2339     *lpszBuf = '\0';
2340
2341   if (!lpszBuf || !lpszPath)
2342     SetLastError(ERROR_INVALID_PARAMETER);
2343   else
2344   {
2345     WCHAR szPath[MAX_PATH];
2346     WCHAR szBuff[MAX_PATH];
2347     int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2348
2349     if (!ret) {
2350         WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
2351         return FALSE;
2352     }
2353     bRet = PathCanonicalizeW(szBuff, szPath);
2354     WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2355   }
2356   return bRet;
2357 }
2358
2359
2360 /*************************************************************************
2361  * PathCanonicalizeW   [SHLWAPI.@]
2362  *
2363  * See PathCanonicalizeA.
2364  */
2365 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2366 {
2367   LPWSTR lpszDst = lpszBuf;
2368   LPCWSTR lpszSrc = lpszPath;
2369
2370   TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2371
2372   if (lpszBuf)
2373     *lpszDst = '\0';
2374
2375   if (!lpszBuf || !lpszPath)
2376   {
2377     SetLastError(ERROR_INVALID_PARAMETER);
2378     return FALSE;
2379   }
2380
2381   if (!*lpszPath)
2382   {
2383     *lpszBuf++ = '\\';
2384     *lpszBuf = '\0';
2385     return TRUE;
2386   }
2387
2388   /* Copy path root */
2389   if (*lpszSrc == '\\')
2390   {
2391     *lpszDst++ = *lpszSrc++;
2392   }
2393   else if (*lpszSrc && lpszSrc[1] == ':')
2394   {
2395     /* X:\ */
2396     *lpszDst++ = *lpszSrc++;
2397     *lpszDst++ = *lpszSrc++;
2398     if (*lpszSrc == '\\')
2399       *lpszDst++ = *lpszSrc++;
2400   }
2401
2402   /* Canonicalize the rest of the path */
2403   while (*lpszSrc)
2404   {
2405     if (*lpszSrc == '.')
2406     {
2407       if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2408       {
2409         lpszSrc += 2; /* Skip .\ */
2410       }
2411       else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2412       {
2413         /* \.. backs up a directory, over the root if it has no \ following X:.
2414          * .. is ignored if it would remove a UNC server name or inital \\
2415          */
2416         if (lpszDst != lpszBuf)
2417         {
2418           *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2419           if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2420              (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2421           {
2422             if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2423             {
2424               lpszDst -= 2;
2425               while (lpszDst > lpszBuf && *lpszDst != '\\')
2426                 lpszDst--;
2427               if (*lpszDst == '\\')
2428                 lpszDst++; /* Reset to last '\' */
2429               else
2430                 lpszDst = lpszBuf; /* Start path again from new root */
2431             }
2432             else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2433               lpszDst -= 2;
2434           }
2435           while (lpszDst > lpszBuf && *lpszDst != '\\')
2436             lpszDst--;
2437           if (lpszDst == lpszBuf)
2438           {
2439             *lpszDst++ = '\\';
2440             lpszSrc++;
2441           }
2442         }
2443         lpszSrc += 2; /* Skip .. in src path */
2444       }
2445       else
2446         *lpszDst++ = *lpszSrc++;
2447     }
2448     else
2449       *lpszDst++ = *lpszSrc++;
2450   }
2451   /* Append \ to naked drive specs */
2452   if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2453     *lpszDst++ = '\\';
2454   *lpszDst++ = '\0';
2455   return TRUE;
2456 }
2457
2458 /*************************************************************************
2459  * PathFindNextComponentA   [SHLWAPI.@]
2460  *
2461  * Find the next component in a path.
2462  *
2463  * PARAMS
2464  *   lpszPath [I] Path to find next component in
2465  *
2466  * RETURNS
2467  *  Success: A pointer to the next component, or the end of the string.
2468  *  Failure: NULL, If lpszPath is invalid
2469  *
2470  * NOTES
2471  *  A 'component' is either a backslash character (\) or UNC marker (\\).
2472  *  Because of this, relative paths (e.g "c:foo") are regarded as having
2473  *  only one component.
2474  */
2475 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2476 {
2477   LPSTR lpszSlash;
2478
2479   TRACE("(%s)\n", debugstr_a(lpszPath));
2480
2481   if(!lpszPath || !*lpszPath)
2482     return NULL;
2483
2484   if ((lpszSlash = StrChrA(lpszPath, '\\')))
2485   {
2486     if (lpszSlash[1] == '\\')
2487       lpszSlash++;
2488     return lpszSlash + 1;
2489   }
2490   return (LPSTR)lpszPath + strlen(lpszPath);
2491 }
2492
2493 /*************************************************************************
2494  * PathFindNextComponentW   [SHLWAPI.@]
2495  *
2496  * See PathFindNextComponentA.
2497  */
2498 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2499 {
2500   LPWSTR lpszSlash;
2501
2502   TRACE("(%s)\n", debugstr_w(lpszPath));
2503
2504   if(!lpszPath || !*lpszPath)
2505     return NULL;
2506
2507   if ((lpszSlash = StrChrW(lpszPath, '\\')))
2508   {
2509     if (lpszSlash[1] == '\\')
2510       lpszSlash++;
2511     return lpszSlash + 1;
2512   }
2513   return (LPWSTR)lpszPath + strlenW(lpszPath);
2514 }
2515
2516 /*************************************************************************
2517  * PathAddExtensionA   [SHLWAPI.@]
2518  *
2519  * Add a file extension to a path
2520  *
2521  * PARAMS
2522  *  lpszPath      [I/O] Path to add extension to
2523  *  lpszExtension [I]   Extension to add to lpszPath
2524  *
2525  * RETURNS
2526  *  TRUE  If the path was modified,
2527  *  FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2528  *        extension already, or the new path length is too big.
2529  *
2530  * FIXME
2531  *  What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2532  *  does not do this, so the behaviour was removed.
2533  */
2534 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2535 {
2536   size_t dwLen;
2537
2538   TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2539
2540   if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2541     return FALSE;
2542
2543   dwLen = strlen(lpszPath);
2544
2545   if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2546     return FALSE;
2547
2548   strcpy(lpszPath + dwLen, lpszExtension);
2549   return TRUE;
2550 }
2551
2552 /*************************************************************************
2553  * PathAddExtensionW   [SHLWAPI.@]
2554  *
2555  * See PathAddExtensionA.
2556  */
2557 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2558 {
2559   size_t dwLen;
2560
2561   TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2562
2563   if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2564     return FALSE;
2565
2566   dwLen = strlenW(lpszPath);
2567
2568   if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2569     return FALSE;
2570
2571   strcpyW(lpszPath + dwLen, lpszExtension);
2572   return TRUE;
2573 }
2574
2575 /*************************************************************************
2576  * PathMakePrettyA   [SHLWAPI.@]
2577  *
2578  * Convert an uppercase DOS filename into lowercase.
2579  *
2580  * PARAMS
2581  *  lpszPath [I/O] Path to convert.
2582  *
2583  * RETURNS
2584  *  TRUE  If the path was an uppercase DOS path and was converted,
2585  *  FALSE Otherwise.
2586  */
2587 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2588 {
2589   LPSTR pszIter = lpszPath;
2590
2591   TRACE("(%s)\n", debugstr_a(lpszPath));
2592
2593   if (!pszIter)
2594     return FALSE;
2595
2596   if (*pszIter)
2597   {
2598     do
2599     {
2600       if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2601         return FALSE; /* Not DOS path */
2602       pszIter++;
2603     } while (*pszIter);
2604     pszIter = lpszPath + 1;
2605     while (*pszIter)
2606     {
2607       *pszIter = tolower(*pszIter);
2608       pszIter++;
2609     }
2610   }
2611   return TRUE;
2612 }
2613
2614 /*************************************************************************
2615  * PathMakePrettyW   [SHLWAPI.@]
2616  *
2617  * See PathMakePrettyA.
2618  */
2619 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2620 {
2621   LPWSTR pszIter = lpszPath;
2622
2623   TRACE("(%s)\n", debugstr_w(lpszPath));
2624
2625   if (!pszIter)
2626     return FALSE;
2627
2628   if (*pszIter)
2629   {
2630     do
2631     {
2632       if (islowerW(*pszIter))
2633         return FALSE; /* Not DOS path */
2634       pszIter++;
2635     } while (*pszIter);
2636     pszIter = lpszPath + 1;
2637     while (*pszIter)
2638     {
2639       *pszIter = tolowerW(*pszIter);
2640       pszIter++;
2641     }
2642   }
2643   return TRUE;
2644 }
2645
2646 /*************************************************************************
2647  * PathCommonPrefixA   [SHLWAPI.@]
2648  *
2649  * Determine the length of the common prefix between two paths.
2650  *
2651  * PARAMS
2652  *  lpszFile1 [I] First path for comparison
2653  *  lpszFile2 [I] Second path for comparison
2654  *  achPath   [O] Destination for common prefix string
2655  *
2656  * RETURNS
2657  *  The length of the common prefix. This is 0 if there is no common
2658  *  prefix between the paths or if any parameters are invalid. If the prefix
2659  *  is non-zero and achPath is not NULL, achPath is filled with the common
2660  *  part of the prefix and NUL terminated.
2661  *
2662  * NOTES
2663  *  A common prefix of 2 is always returned as 3. It is thus possible for
2664  *  the length returned to be invalid (i.e. Longer than one or both of the
2665  *  strings given as parameters). This Win32 behaviour has been implemented
2666  *  here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2667  *  To work around this when using this function, always check that the byte
2668  *  at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2669  */
2670 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2671 {
2672   size_t iLen = 0;
2673   LPCSTR lpszIter1 = lpszFile1;
2674   LPCSTR lpszIter2 = lpszFile2;
2675
2676   TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2677
2678   if (achPath)
2679     *achPath = '\0';
2680
2681   if (!lpszFile1 || !lpszFile2)
2682     return 0;
2683
2684   /* Handle roots first */
2685   if (PathIsUNCA(lpszFile1))
2686   {
2687     if (!PathIsUNCA(lpszFile2))
2688       return 0;
2689     lpszIter1 += 2;
2690     lpszIter2 += 2;
2691   }
2692   else if (PathIsUNCA(lpszFile2))
2693       return 0; /* Know already lpszFile1 is not UNC */
2694
2695   do
2696   {
2697     /* Update len */
2698     if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2699         (!*lpszIter2 || *lpszIter2 == '\\'))
2700       iLen = lpszIter1 - lpszFile1; /* Common to this point */
2701
2702     if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2703       break; /* Strings differ at this point */
2704
2705     lpszIter1++;
2706     lpszIter2++;
2707   } while (1);
2708
2709   if (iLen == 2)
2710     iLen++; /* Feature/Bug compatible with Win32 */
2711
2712   if (iLen && achPath)
2713   {
2714     memcpy(achPath,lpszFile1,iLen);
2715     achPath[iLen] = '\0';
2716   }
2717   return iLen;
2718 }
2719
2720 /*************************************************************************
2721  * PathCommonPrefixW   [SHLWAPI.@]
2722  *
2723  * See PathCommonPrefixA.
2724  */
2725 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2726 {
2727   size_t iLen = 0;
2728   LPCWSTR lpszIter1 = lpszFile1;
2729   LPCWSTR lpszIter2 = lpszFile2;
2730
2731   TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2732
2733   if (achPath)
2734     *achPath = '\0';
2735
2736   if (!lpszFile1 || !lpszFile2)
2737     return 0;
2738
2739   /* Handle roots first */
2740   if (PathIsUNCW(lpszFile1))
2741   {
2742     if (!PathIsUNCW(lpszFile2))
2743       return 0;
2744     lpszIter1 += 2;
2745     lpszIter2 += 2;
2746   }
2747   else if (PathIsUNCW(lpszFile2))
2748       return 0; /* Know already lpszFile1 is not UNC */
2749
2750   do
2751   {
2752     /* Update len */
2753     if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2754         (!*lpszIter2 || *lpszIter2 == '\\'))
2755       iLen = lpszIter1 - lpszFile1; /* Common to this point */
2756
2757     if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2758       break; /* Strings differ at this point */
2759
2760     lpszIter1++;
2761     lpszIter2++;
2762   } while (1);
2763
2764   if (iLen == 2)
2765     iLen++; /* Feature/Bug compatible with Win32 */
2766
2767   if (iLen && achPath)
2768   {
2769     memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2770     achPath[iLen] = '\0';
2771   }
2772   return iLen;
2773 }
2774
2775 /*************************************************************************
2776  * PathCompactPathA   [SHLWAPI.@]
2777  *
2778  * Make a path fit into a given width when printed to a DC.
2779  *
2780  * PARAMS
2781  *  hDc      [I]   Destination DC
2782  *  lpszPath [I/O] Path to be printed to hDc
2783  *  dx       [I]   Desired width
2784  *
2785  * RETURNS
2786  *  TRUE  If the path was modified/went well.
2787  *  FALSE Otherwise.
2788  */
2789 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2790 {
2791   BOOL bRet = FALSE;
2792
2793   TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2794
2795   if (lpszPath)
2796   {
2797     WCHAR szPath[MAX_PATH];
2798     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2799     bRet = PathCompactPathW(hDC, szPath, dx);
2800     WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2801   }
2802   return bRet;
2803 }
2804
2805 /*************************************************************************
2806  * PathCompactPathW   [SHLWAPI.@]
2807  *
2808  * See PathCompactPathA.
2809  */
2810 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2811 {
2812   static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2813   BOOL bRet = TRUE;
2814   HDC hdc = 0;
2815   WCHAR buff[MAX_PATH];
2816   SIZE size;
2817   DWORD dwLen;
2818
2819   TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2820
2821   if (!lpszPath)
2822     return FALSE;
2823
2824   if (!hDC)
2825     hdc = hDC = GetDC(0);
2826
2827   /* Get the length of the whole path */
2828   dwLen = strlenW(lpszPath);
2829   GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2830
2831   if ((UINT)size.cx > dx)
2832   {
2833     /* Path too big, must reduce it */
2834     LPWSTR sFile;
2835     DWORD dwEllipsesLen = 0, dwPathLen = 0;
2836
2837     sFile = PathFindFileNameW(lpszPath);
2838     if (sFile != lpszPath) sFile--;
2839
2840     /* Get the size of ellipses */
2841     GetTextExtentPointW(hDC, szEllipses, 3, &size);
2842     dwEllipsesLen = size.cx;
2843     /* Get the size of the file name */
2844     GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2845     dwPathLen = size.cx;
2846
2847     if (sFile != lpszPath)
2848     {
2849       LPWSTR sPath = sFile;
2850       BOOL bEllipses = FALSE;
2851
2852       /* The path includes a file name. Include as much of the path prior to
2853        * the file name as possible, allowing for the ellipses, e.g:
2854        * c:\some very long path\filename ==> c:\some v...\filename
2855        */
2856       lstrcpynW(buff, sFile, MAX_PATH);
2857
2858       do
2859       {
2860         DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2861
2862         GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2863         dwTotalLen += size.cx;
2864         if (dwTotalLen <= dx)
2865           break;
2866         sPath--;
2867         if (!bEllipses)
2868         {
2869           bEllipses = TRUE;
2870           sPath -= 2;
2871         }
2872       } while (sPath > lpszPath);
2873
2874       if (sPath > lpszPath)
2875       {
2876         if (bEllipses)
2877         {
2878           strcpyW(sPath, szEllipses);
2879           strcpyW(sPath+3, buff);
2880         }
2881         bRet = TRUE;
2882         goto end;
2883       }
2884       strcpyW(lpszPath, szEllipses);
2885       strcpyW(lpszPath+3, buff);
2886       bRet = FALSE;
2887       goto end;
2888     }
2889
2890     /* Trim the path by adding ellipses to the end, e.g:
2891      * A very long file name.txt ==> A very...
2892      */
2893     dwLen = strlenW(lpszPath);
2894
2895     if (dwLen > MAX_PATH - 3)
2896       dwLen =  MAX_PATH - 3;
2897     lstrcpynW(buff, sFile, dwLen);
2898
2899     do {
2900       dwLen--;
2901       GetTextExtentPointW(hDC, buff, dwLen, &size);
2902     } while (dwLen && size.cx + dwEllipsesLen > dx);
2903
2904    if (!dwLen)
2905    {
2906      DWORD dwWritten = 0;
2907
2908      dwEllipsesLen /= 3; /* Size of a single '.' */
2909
2910      /* Write as much of the Ellipses string as possible */
2911      while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2912      {
2913        *lpszPath++ = '.';
2914        dwWritten += dwEllipsesLen;
2915        dwLen++;
2916      }
2917      *lpszPath = '\0';
2918      bRet = FALSE;
2919    }
2920    else
2921    {
2922      strcpyW(buff + dwLen, szEllipses);
2923      strcpyW(lpszPath, buff);
2924     }
2925   }
2926
2927 end:
2928   if (hdc)
2929     ReleaseDC(0, hdc);
2930
2931   return bRet;
2932 }
2933
2934 /*************************************************************************
2935  * PathGetCharTypeA   [SHLWAPI.@]
2936  *
2937  * Categorise a character from a file path.
2938  *
2939  * PARAMS
2940  *  ch [I] Character to get the type of
2941  *
2942  * RETURNS
2943  *  A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2944  */
2945 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2946 {
2947   return PathGetCharTypeW(ch);
2948 }
2949
2950 /*************************************************************************
2951  * PathGetCharTypeW   [SHLWAPI.@]
2952  *
2953  * See PathGetCharTypeA.
2954  */
2955 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2956 {
2957   UINT flags = 0;
2958
2959   TRACE("(%d)\n", ch);
2960
2961   if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2962       ch == '"' || ch == '|' || ch == '/')
2963     flags = GCT_INVALID; /* Invalid */
2964   else if (ch == '*' || ch=='?')
2965     flags = GCT_WILD; /* Wildchars */
2966   else if ((ch == '\\') || (ch == ':'))
2967     return GCT_SEPARATOR; /* Path separators */
2968   else
2969   {
2970      if (ch < 126)
2971      {
2972        if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2973             ch == '.' || ch == '@' || ch == '^' ||
2974             ch == '\'' || ch == 130 || ch == '`')
2975          flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2976      }
2977      else
2978        flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2979      flags |= GCT_LFNCHAR; /* Valid for long file names */
2980   }
2981   return flags;
2982 }
2983
2984 /*************************************************************************
2985  * SHLWAPI_UseSystemForSystemFolders
2986  *
2987  * Internal helper for PathMakeSystemFolderW.
2988  */
2989 static BOOL WINAPI SHLWAPI_UseSystemForSystemFolders(void)
2990 {
2991   static BOOL bCheckedReg = FALSE;
2992   static BOOL bUseSystemForSystemFolders = FALSE;
2993
2994   if (!bCheckedReg)
2995   {
2996     bCheckedReg = TRUE;
2997
2998     /* Key tells Win what file attributes to use on system folders */
2999     if (SHGetValueA(HKEY_LOCAL_MACHINE,
3000         "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
3001         "UseSystemForSystemFolders", 0, 0, 0))
3002       bUseSystemForSystemFolders = TRUE;
3003   }
3004   return bUseSystemForSystemFolders;
3005 }
3006
3007 /*************************************************************************
3008  * PathMakeSystemFolderA   [SHLWAPI.@]
3009  *
3010  * Set system folder attribute for a path.
3011  *
3012  * PARAMS
3013  *  lpszPath [I] The path to turn into a system folder
3014  *
3015  * RETURNS
3016  *  TRUE  If the path was changed to/already was a system folder
3017  *  FALSE If the path is invalid or SetFileAttributesA() fails
3018  */
3019 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3020 {
3021   BOOL bRet = FALSE;
3022
3023   TRACE("(%s)\n", debugstr_a(lpszPath));
3024
3025   if (lpszPath && *lpszPath)
3026   {
3027     WCHAR szPath[MAX_PATH];
3028     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3029     bRet = PathMakeSystemFolderW(szPath);
3030   }
3031   return bRet;
3032 }
3033
3034 /*************************************************************************
3035  * PathMakeSystemFolderW   [SHLWAPI.@]
3036  *
3037  * See PathMakeSystemFolderA.
3038  */
3039 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3040 {
3041   DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3042   WCHAR buff[MAX_PATH];
3043
3044   TRACE("(%s)\n", debugstr_w(lpszPath));
3045
3046   if (!lpszPath || !*lpszPath)
3047     return FALSE;
3048
3049   /* If the directory is already a system directory, don't do anything */
3050   GetSystemDirectoryW(buff, MAX_PATH);
3051   if (!strcmpW(buff, lpszPath))
3052     return TRUE;
3053
3054   GetWindowsDirectoryW(buff, MAX_PATH);
3055   if (!strcmpW(buff, lpszPath))
3056     return TRUE;
3057
3058   /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3059   if (SHLWAPI_UseSystemForSystemFolders())
3060     dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3061
3062   if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3063     return FALSE;
3064
3065   /* Change file attributes to system attributes */
3066   dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3067   return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3068 }
3069
3070 /*************************************************************************
3071  * PathRenameExtensionA   [SHLWAPI.@]
3072  *
3073  * Swap the file extension in a path with another extension.
3074  *
3075  * PARAMS
3076  *  lpszPath [I/O] Path to swap the extension in
3077  *  lpszExt  [I]   The new extension
3078  *
3079  * RETURNS
3080  *  TRUE  if lpszPath was modified,
3081  *  FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3082  */
3083 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3084 {
3085   LPSTR lpszExtension;
3086
3087   TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3088
3089   lpszExtension = PathFindExtensionA(lpszPath);
3090
3091   if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3092     return FALSE;
3093
3094   strcpy(lpszExtension, lpszExt);
3095   return TRUE;
3096 }
3097
3098 /*************************************************************************
3099  * PathRenameExtensionW   [SHLWAPI.@]
3100  *
3101  * See PathRenameExtensionA.
3102  */
3103 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3104 {
3105   LPWSTR lpszExtension;
3106
3107   TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3108
3109   lpszExtension = PathFindExtensionW(lpszPath);
3110
3111   if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3112     return FALSE;
3113
3114   strcpyW(lpszExtension, lpszExt);
3115   return TRUE;
3116 }
3117
3118 /*************************************************************************
3119  * PathSearchAndQualifyA   [SHLWAPI.@]
3120  *
3121  * Determine if a given path is correct and fully qualified.
3122  *
3123  * PARAMS
3124  *  lpszPath [I] Path to check
3125  *  lpszBuf  [O] Output for correct path
3126  *  cchBuf   [I] Size of lpszBuf
3127  *
3128  * RETURNS
3129  *  Unknown.
3130  */
3131 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3132 {
3133     TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3134
3135     if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3136         return TRUE;
3137     return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3138 }
3139
3140 /*************************************************************************
3141  * PathSearchAndQualifyW   [SHLWAPI.@]
3142  *
3143  * See PathSearchAndQualifyA.
3144  */
3145 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3146 {
3147     TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3148
3149     if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3150         return TRUE;
3151     return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3152 }
3153
3154 /*************************************************************************
3155  * PathSkipRootA   [SHLWAPI.@]
3156  *
3157  * Return the portion of a path following the drive letter or mount point.
3158  *
3159  * PARAMS
3160  *  lpszPath [I] The path to skip on
3161  *
3162  * RETURNS
3163  *  Success: A pointer to the next character after the root.
3164  *  Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3165  */
3166 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3167 {
3168   TRACE("(%s)\n", debugstr_a(lpszPath));
3169
3170   if (!lpszPath || !*lpszPath)
3171     return NULL;
3172
3173   if (*lpszPath == '\\' && lpszPath[1] == '\\')
3174   {
3175     /* Network share: skip share server and mount point */
3176     lpszPath += 2;
3177     if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3178         (lpszPath = StrChrA(lpszPath + 1, '\\')))
3179       lpszPath++;
3180     return (LPSTR)lpszPath;
3181   }
3182
3183   if (IsDBCSLeadByte(*lpszPath))
3184     return NULL;
3185
3186   /* Check x:\ */
3187   if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3188     return (LPSTR)lpszPath + 3;
3189   return NULL;
3190 }
3191
3192 /*************************************************************************
3193  * PathSkipRootW   [SHLWAPI.@]
3194  *
3195  * See PathSkipRootA.
3196  */
3197 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3198 {
3199   TRACE("(%s)\n", debugstr_w(lpszPath));
3200
3201   if (!lpszPath || !*lpszPath)
3202     return NULL;
3203
3204   if (*lpszPath == '\\' && lpszPath[1] == '\\')
3205   {
3206     /* Network share: skip share server and mount point */
3207     lpszPath += 2;
3208     if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3209         (lpszPath = StrChrW(lpszPath + 1, '\\')))
3210      lpszPath++;
3211     return (LPWSTR)lpszPath;
3212   }
3213
3214   /* Check x:\ */
3215   if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3216     return (LPWSTR)lpszPath + 3;
3217   return NULL;
3218 }
3219
3220 /*************************************************************************
3221  * PathCreateFromUrlA   [SHLWAPI.@]
3222  *
3223  * See PathCreateFromUrlW
3224  */
3225 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3226                                   LPDWORD pcchPath, DWORD dwReserved)
3227 {
3228     WCHAR bufW[MAX_PATH];
3229     WCHAR *pathW = bufW;
3230     UNICODE_STRING urlW;
3231     HRESULT ret;
3232     DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3233
3234     if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3235         return E_INVALIDARG;
3236     if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3237         pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3238         ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3239     }
3240     if(ret == S_OK) {
3241         RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3242         if(*pcchPath > lenA) {
3243             RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3244             pszPath[lenA] = 0;
3245             *pcchPath = lenA;
3246         } else {
3247             *pcchPath = lenA + 1;
3248             ret = E_POINTER;
3249         }
3250     }
3251     if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3252     RtlFreeUnicodeString(&urlW);
3253     return ret;
3254 }
3255
3256 /*************************************************************************
3257  * PathCreateFromUrlW   [SHLWAPI.@]
3258  *
3259  * Create a path from a URL
3260  *
3261  * PARAMS
3262  *  lpszUrl  [I] URL to convert into a path
3263  *  lpszPath [O] Output buffer for the resulting Path
3264  *  pcchPath [I] Length of lpszPath
3265  *  dwFlags  [I] Flags controlling the conversion
3266  *
3267  * RETURNS
3268  *  Success: S_OK. lpszPath contains the URL in path format,
3269  *  Failure: An HRESULT error code such as E_INVALIDARG.
3270  */
3271 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3272                                   LPDWORD pcchPath, DWORD dwReserved)
3273 {
3274     static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3275     HRESULT hr;
3276     DWORD nslashes = 0;
3277     WCHAR *ptr;
3278
3279     TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3280
3281     if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3282         return E_INVALIDARG;
3283
3284
3285     if (strncmpW(pszUrl, file_colon, 5))
3286         return E_INVALIDARG;
3287     pszUrl += 5;
3288
3289     while(*pszUrl == '/' || *pszUrl == '\\') {
3290         nslashes++;
3291         pszUrl++;
3292     }
3293
3294     if(isalphaW(*pszUrl) && (pszUrl[1] == ':' || pszUrl[1] == '|') && (pszUrl[2] == '/' || pszUrl[2] == '\\'))
3295         nslashes = 0;
3296
3297     switch(nslashes) {
3298     case 2:
3299         pszUrl -= 2;
3300         break;
3301     case 0:
3302         break;
3303     default:
3304         pszUrl -= 1;
3305         break;
3306     }
3307
3308     hr = UrlUnescapeW((LPWSTR)pszUrl, pszPath, pcchPath, 0);
3309     if(hr != S_OK) return hr;
3310
3311     for(ptr = pszPath; *ptr; ptr++)
3312         if(*ptr == '/') *ptr = '\\';
3313
3314     while(*pszPath == '\\')
3315         pszPath++;
3316  
3317     if(isalphaW(*pszPath) && pszPath[1] == '|' && pszPath[2] == '\\') /* c|\ -> c:\ */
3318         pszPath[1] = ':';
3319
3320     if(nslashes == 2 && (ptr = strchrW(pszPath, '\\'))) { /* \\host\c:\ -> \\hostc:\ */
3321         ptr++;
3322         if(isalphaW(*ptr) && (ptr[1] == ':' || ptr[1] == '|') && ptr[2] == '\\') {
3323             memmove(ptr - 1, ptr, (strlenW(ptr) + 1) * sizeof(WCHAR));
3324             (*pcchPath)--;
3325         }
3326     }
3327
3328     TRACE("Returning %s\n",debugstr_w(pszPath));
3329
3330     return hr;
3331 }
3332
3333 /*************************************************************************
3334  * PathRelativePathToA   [SHLWAPI.@]
3335  *
3336  * Create a relative path from one path to another.
3337  *
3338  * PARAMS
3339  *  lpszPath   [O] Destination for relative path
3340  *  lpszFrom   [I] Source path
3341  *  dwAttrFrom [I] File attribute of source path
3342  *  lpszTo     [I] Destination path
3343  *  dwAttrTo   [I] File attributes of destination path
3344  *
3345  * RETURNS
3346  *  TRUE  If a relative path can be formed. lpszPath contains the new path
3347  *  FALSE If the paths are not relative or any parameters are invalid
3348  *
3349  * NOTES
3350  *  lpszTo should be at least MAX_PATH in length.
3351  *
3352  *  Calling this function with relative paths for lpszFrom or lpszTo may
3353  *  give erroneous results.
3354  *
3355  *  The Win32 version of this function contains a bug where the lpszTo string
3356  *  may be referenced 1 byte beyond the end of the string. As a result random
3357  *  garbage may be written to the output path, depending on what lies beyond
3358  *  the last byte of the string. This bug occurs because of the behaviour of
3359  *  PathCommonPrefix() (see notes for that function), and no workaround seems
3360  *  possible with Win32.
3361  *
3362  *  This bug has been fixed here, so for example the relative path from "\\"
3363  *  to "\\" is correctly determined as "." in this implementation.
3364  */
3365 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3366                                 LPCSTR lpszTo, DWORD dwAttrTo)
3367 {
3368   BOOL bRet = FALSE;
3369
3370   TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3371         dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3372
3373   if(lpszPath && lpszFrom && lpszTo)
3374   {
3375     WCHAR szPath[MAX_PATH];
3376     WCHAR szFrom[MAX_PATH];
3377     WCHAR szTo[MAX_PATH];
3378     MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3379     MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3380     bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3381     WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3382   }
3383   return bRet;
3384 }
3385
3386 /*************************************************************************
3387  * PathRelativePathToW   [SHLWAPI.@]
3388  *
3389  * See PathRelativePathToA.
3390  */
3391 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3392                                 LPCWSTR lpszTo, DWORD dwAttrTo)
3393 {
3394   static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3395   static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3396   WCHAR szFrom[MAX_PATH];
3397   WCHAR szTo[MAX_PATH];
3398   DWORD dwLen;
3399
3400   TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3401         dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3402
3403   if(!lpszPath || !lpszFrom || !lpszTo)
3404     return FALSE;
3405
3406   *lpszPath = '\0';
3407   lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3408   lstrcpynW(szTo, lpszTo, MAX_PATH);
3409
3410   if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3411     PathRemoveFileSpecW(szFrom);
3412   if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3413     PathRemoveFileSpecW(szTo);
3414
3415   /* Paths can only be relative if they have a common root */
3416   if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3417     return FALSE;
3418
3419   /* Strip off lpszFrom components to the root, by adding "..\" */
3420   lpszFrom = szFrom + dwLen;
3421   if (!*lpszFrom)
3422   {
3423     lpszPath[0] = '.';
3424     lpszPath[1] = '\0';
3425   }
3426   if (*lpszFrom == '\\')
3427     lpszFrom++;
3428
3429   while (*lpszFrom)
3430   {
3431     lpszFrom = PathFindNextComponentW(lpszFrom);
3432     strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3433   }
3434
3435   /* From the root add the components of lpszTo */
3436   lpszTo += dwLen;
3437   /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3438    * this function.
3439    */
3440   if (*lpszTo && lpszTo[-1])
3441   {
3442     if (*lpszTo != '\\')
3443       lpszTo--;
3444     dwLen = strlenW(lpszPath);
3445     if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3446     {
3447       *lpszPath = '\0';
3448       return FALSE;
3449     }
3450     strcpyW(lpszPath + dwLen, lpszTo);
3451   }
3452   return TRUE;
3453 }
3454
3455 /*************************************************************************
3456  * PathUnmakeSystemFolderA   [SHLWAPI.@]
3457  *
3458  * Remove the system folder attributes from a path.
3459  *
3460  * PARAMS
3461  *  lpszPath [I] The path to remove attributes from
3462  *
3463  * RETURNS
3464  *  Success: TRUE.
3465  *  Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3466  *           SetFileAttributesA() fails.
3467  */
3468 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3469 {
3470   DWORD dwAttr;
3471
3472   TRACE("(%s)\n", debugstr_a(lpszPath));
3473
3474   if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3475       !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3476     return FALSE;
3477
3478   dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3479   return SetFileAttributesA(lpszPath, dwAttr);
3480 }
3481
3482 /*************************************************************************
3483  * PathUnmakeSystemFolderW   [SHLWAPI.@]
3484  *
3485  * See PathUnmakeSystemFolderA.
3486  */
3487 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3488 {
3489   DWORD dwAttr;
3490
3491   TRACE("(%s)\n", debugstr_w(lpszPath));
3492
3493   if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3494     !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3495     return FALSE;
3496
3497   dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3498   return SetFileAttributesW(lpszPath, dwAttr);
3499 }
3500
3501
3502 /*************************************************************************
3503  * PathSetDlgItemPathA   [SHLWAPI.@]
3504  *
3505  * Set the text of a dialog item to a path, shrinking the path to fit
3506  * if it is too big for the item.
3507  *
3508  * PARAMS
3509  *  hDlg     [I] Dialog handle
3510  *  id       [I] ID of item in the dialog
3511  *  lpszPath [I] Path to set as the items text
3512  *
3513  * RETURNS
3514  *  Nothing.
3515  *
3516  * NOTES
3517  *  If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3518  *  window text is erased).
3519  */
3520 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3521 {
3522   WCHAR szPath[MAX_PATH];
3523
3524   TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3525
3526   if (lpszPath)
3527     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3528   else
3529     szPath[0] = '\0';
3530   PathSetDlgItemPathW(hDlg, id, szPath);
3531 }
3532
3533 /*************************************************************************
3534  * PathSetDlgItemPathW   [SHLWAPI.@]
3535  *
3536  * See PathSetDlgItemPathA.
3537  */
3538 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3539 {
3540   WCHAR path[MAX_PATH + 1];
3541   HWND hwItem;
3542   RECT rect;
3543   HDC hdc;
3544   HGDIOBJ hPrevObj;
3545
3546   TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3547
3548   if (!(hwItem = GetDlgItem(hDlg, id)))
3549     return;
3550
3551   if (lpszPath)
3552     lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3553   else
3554     path[0] = '\0';
3555
3556   GetClientRect(hwItem, &rect);
3557   hdc = GetDC(hDlg);
3558   hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3559
3560   if (hPrevObj)
3561   {
3562     PathCompactPathW(hdc, path, rect.right);
3563     SelectObject(hdc, hPrevObj);
3564   }
3565
3566   ReleaseDC(hDlg, hdc);
3567   SetWindowTextW(hwItem, path);
3568 }
3569
3570 /*************************************************************************
3571  * PathIsNetworkPathA [SHLWAPI.@]
3572  *
3573  * Determine if the given path is a network path.
3574  *
3575  * PARAMS
3576  *  lpszPath [I] Path to check
3577  *
3578  * RETURNS
3579  *  TRUE  If lpszPath is a UNC share or mapped network drive, or
3580  *  FALSE If lpszPath is a local drive or cannot be determined
3581  */
3582 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3583 {
3584   int dwDriveNum;
3585
3586   TRACE("(%s)\n",debugstr_a(lpszPath));
3587
3588   if (!lpszPath)
3589     return FALSE;
3590   if (*lpszPath == '\\' && lpszPath[1] == '\\')
3591     return TRUE;
3592   dwDriveNum = PathGetDriveNumberA(lpszPath);
3593   if (dwDriveNum == -1)
3594     return FALSE;
3595   GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3596   return pIsNetDrive(dwDriveNum);
3597 }
3598
3599 /*************************************************************************
3600  * PathIsNetworkPathW [SHLWAPI.@]
3601  *
3602  * See PathIsNetworkPathA.
3603  */
3604 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3605 {
3606   int dwDriveNum;
3607
3608   TRACE("(%s)\n", debugstr_w(lpszPath));
3609
3610   if (!lpszPath)
3611     return FALSE;
3612   if (*lpszPath == '\\' && lpszPath[1] == '\\')
3613     return TRUE;
3614   dwDriveNum = PathGetDriveNumberW(lpszPath);
3615   if (dwDriveNum == -1)
3616     return FALSE;
3617   GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3618   return pIsNetDrive(dwDriveNum);
3619 }
3620
3621 /*************************************************************************
3622  * PathIsLFNFileSpecA [SHLWAPI.@]
3623  *
3624  * Determine if the given path is a long file name
3625  *
3626  * PARAMS
3627  *  lpszPath [I] Path to check
3628  *
3629  * RETURNS
3630  *  TRUE  If path is a long file name,
3631  *  FALSE If path is a valid DOS short file name
3632  */
3633 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3634 {
3635   DWORD dwNameLen = 0, dwExtLen = 0;
3636
3637   TRACE("(%s)\n",debugstr_a(lpszPath));
3638
3639   if (!lpszPath)
3640     return FALSE;
3641
3642   while (*lpszPath)
3643   {
3644     if (*lpszPath == ' ')
3645       return TRUE; /* DOS names cannot have spaces */
3646     if (*lpszPath == '.')
3647     {
3648       if (dwExtLen)
3649         return TRUE; /* DOS names have only one dot */
3650       dwExtLen = 1;
3651     }
3652     else if (dwExtLen)
3653     {
3654       dwExtLen++;
3655       if (dwExtLen > 4)
3656         return TRUE; /* DOS extensions are <= 3 chars*/
3657     }
3658     else
3659     {
3660       dwNameLen++;
3661       if (dwNameLen > 8)
3662         return TRUE; /* DOS names are <= 8 chars */
3663     }
3664     lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3665   }
3666   return FALSE; /* Valid DOS path */
3667 }
3668
3669 /*************************************************************************
3670  * PathIsLFNFileSpecW [SHLWAPI.@]
3671  *
3672  * See PathIsLFNFileSpecA.
3673  */
3674 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3675 {
3676   DWORD dwNameLen = 0, dwExtLen = 0;
3677
3678   TRACE("(%s)\n",debugstr_w(lpszPath));
3679
3680   if (!lpszPath)
3681     return FALSE;
3682
3683   while (*lpszPath)
3684   {
3685     if (*lpszPath == ' ')
3686       return TRUE; /* DOS names cannot have spaces */
3687     if (*lpszPath == '.')
3688     {
3689       if (dwExtLen)
3690         return TRUE; /* DOS names have only one dot */
3691       dwExtLen = 1;
3692     }
3693     else if (dwExtLen)
3694     {
3695       dwExtLen++;
3696       if (dwExtLen > 4)
3697         return TRUE; /* DOS extensions are <= 3 chars*/
3698     }
3699     else
3700     {
3701       dwNameLen++;
3702       if (dwNameLen > 8)
3703         return TRUE; /* DOS names are <= 8 chars */
3704     }
3705     lpszPath++;
3706   }
3707   return FALSE; /* Valid DOS path */
3708 }
3709
3710 /*************************************************************************
3711  * PathIsDirectoryEmptyA [SHLWAPI.@]
3712  *
3713  * Determine if a given directory is empty.
3714  *
3715  * PARAMS
3716  *  lpszPath [I] Directory to check
3717  *
3718  * RETURNS
3719  *  TRUE  If the directory exists and contains no files,
3720  *  FALSE Otherwise
3721  */
3722 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3723 {
3724   BOOL bRet = FALSE;
3725
3726   TRACE("(%s)\n",debugstr_a(lpszPath));
3727
3728   if (lpszPath)
3729   {
3730     WCHAR szPath[MAX_PATH];
3731     MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3732     bRet = PathIsDirectoryEmptyW(szPath);
3733   }
3734   return bRet;
3735 }
3736
3737 /*************************************************************************
3738  * PathIsDirectoryEmptyW [SHLWAPI.@]
3739  *
3740  * See PathIsDirectoryEmptyA.
3741  */
3742 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3743 {
3744   static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3745   WCHAR szSearch[MAX_PATH];
3746   DWORD dwLen;
3747   HANDLE hfind;
3748   BOOL retVal = FALSE;
3749   WIN32_FIND_DATAW find_data;
3750
3751   TRACE("(%s)\n",debugstr_w(lpszPath));
3752
3753   if (!lpszPath || !PathIsDirectoryW(lpszPath))
3754       return FALSE;
3755
3756   lstrcpynW(szSearch, lpszPath, MAX_PATH);
3757   PathAddBackslashW(szSearch);
3758   dwLen = strlenW(szSearch);
3759   if (dwLen > MAX_PATH - 4)
3760     return FALSE;
3761
3762   strcpyW(szSearch + dwLen, szAllFiles);
3763   hfind = FindFirstFileW(szSearch, &find_data);
3764
3765   if (hfind != INVALID_HANDLE_VALUE &&
3766       find_data.cFileName[0] == '.' &&
3767       find_data.cFileName[1] == '.')
3768   {
3769     /* The only directory entry should be the parent */
3770     if (!FindNextFileW(hfind, &find_data))
3771       retVal = TRUE;
3772     FindClose(hfind);
3773   }
3774   return retVal;
3775 }
3776
3777
3778 /*************************************************************************
3779  * PathFindSuffixArrayA [SHLWAPI.@]
3780  *
3781  * Find a suffix string in an array of suffix strings
3782  *
3783  * PARAMS
3784  *  lpszSuffix [I] Suffix string to search for
3785  *  lppszArray [I] Array of suffix strings to search
3786  *  dwCount    [I] Number of elements in lppszArray
3787  *
3788  * RETURNS
3789  *  Success: The index of the position of lpszSuffix in lppszArray
3790  *  Failure: 0, if any parameters are invalid or lpszSuffix is not found
3791  *
3792  * NOTES
3793  *  The search is case sensitive.
3794  *  The match is made against the end of the suffix string, so for example:
3795  *  lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3796  */
3797 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3798 {
3799   size_t dwLen;
3800   int dwRet = 0;
3801
3802   TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3803
3804   if (lpszSuffix && lppszArray && dwCount > 0)
3805   {
3806     dwLen = strlen(lpszSuffix);
3807
3808     while (dwRet < dwCount)
3809     {
3810       size_t dwCompareLen = strlen(*lppszArray);
3811       if (dwCompareLen < dwLen)
3812       {
3813         if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3814           return *lppszArray; /* Found */
3815       }
3816       dwRet++;
3817       lppszArray++;
3818     }
3819   }
3820   return NULL;
3821 }
3822
3823 /*************************************************************************
3824  * PathFindSuffixArrayW [SHLWAPI.@]
3825  *
3826  * See PathFindSuffixArrayA.
3827  */
3828 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3829 {
3830   size_t dwLen;
3831   int dwRet = 0;
3832
3833   TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3834
3835   if (lpszSuffix && lppszArray && dwCount > 0)
3836   {
3837     dwLen = strlenW(lpszSuffix);
3838
3839     while (dwRet < dwCount)
3840     {
3841       size_t dwCompareLen = strlenW(*lppszArray);
3842       if (dwCompareLen < dwLen)
3843       {
3844         if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3845           return *lppszArray; /* Found */
3846       }
3847       dwRet++;
3848       lppszArray++;
3849     }
3850   }
3851   return NULL;
3852 }
3853
3854 /*************************************************************************
3855  * PathUndecorateA [SHLWAPI.@]
3856  *
3857  * Undecorate a file path
3858  *
3859  * PARAMS
3860  *  lpszPath [I/O] Path to remove any decoration from
3861  *
3862  * RETURNS
3863  *  Nothing
3864  *
3865  * NOTES
3866  *  A decorations form is "path[n].ext" where "n" is an optional decimal number.
3867  */
3868 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3869 {
3870   TRACE("(%s)\n",debugstr_a(lpszPath));
3871
3872   if (lpszPath)
3873   {
3874     LPSTR lpszExt = PathFindExtensionA(lpszPath);
3875     if (lpszExt > lpszPath && lpszExt[-1] == ']')
3876     {
3877       LPSTR lpszSkip = lpszExt - 2;
3878       if (*lpszSkip == '[')
3879         lpszSkip++;  /* [] (no number) */
3880       else
3881         while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3882           lpszSkip--;
3883       if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3884       {
3885         /* remove the [n] */
3886         lpszSkip--;
3887         while (*lpszExt)
3888           *lpszSkip++ = *lpszExt++;
3889         *lpszSkip = '\0';
3890       }
3891     }
3892   }
3893 }
3894
3895 /*************************************************************************
3896  * PathUndecorateW [SHLWAPI.@]
3897  *
3898  * See PathUndecorateA.
3899  */
3900 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3901 {
3902   TRACE("(%s)\n",debugstr_w(lpszPath));
3903
3904   if (lpszPath)
3905   {
3906     LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3907     if (lpszExt > lpszPath && lpszExt[-1] == ']')
3908     {
3909       LPWSTR lpszSkip = lpszExt - 2;
3910       if (*lpszSkip == '[')
3911         lpszSkip++; /* [] (no number) */
3912       else
3913         while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3914           lpszSkip--;
3915       if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3916       {
3917         /* remove the [n] */
3918         lpszSkip--;
3919         while (*lpszExt)
3920           *lpszSkip++ = *lpszExt++;
3921         *lpszSkip = '\0';
3922       }
3923     }
3924   }
3925 }
3926
3927 /*************************************************************************
3928  * PathUnExpandEnvStringsA [SHLWAPI.@]
3929  *
3930  * Substitute folder names in a path with their corresponding environment
3931  * strings.
3932  *
3933  * PARAMS
3934  *  pszPath  [I] Buffer containing the path to unexpand.
3935  *  pszBuf   [O] Buffer to receive the unexpanded path.
3936  *  cchBuf   [I] Size of pszBuf in characters.
3937  *
3938  * RETURNS
3939  *  Success: TRUE
3940  *  Failure: FALSE
3941  */
3942 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR pszPath, LPSTR pszBuf, UINT cchBuf)
3943 {
3944     FIXME("(%s,%s,0x%08x)\n", debugstr_a(pszPath), debugstr_a(pszBuf), cchBuf);
3945     return FALSE;
3946 }
3947
3948 /*************************************************************************
3949  * PathUnExpandEnvStringsW [SHLWAPI.@]
3950  *
3951  * Unicode version of PathUnExpandEnvStringsA.
3952  */
3953 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR pszPath, LPWSTR pszBuf, UINT cchBuf)
3954 {
3955     FIXME("(%s,%s,0x%08x)\n", debugstr_w(pszPath), debugstr_w(pszBuf), cchBuf);
3956     return FALSE;
3957 }
3958
3959 /*************************************************************************
3960  * @     [SHLWAPI.440]
3961  *
3962  * Find localised or default web content in "%WINDOWS%\web\".
3963  *
3964  * PARAMS
3965  *  lpszFile  [I] File name containing content to look for
3966  *  lpszPath  [O] Buffer to contain the full path to the file
3967  *  dwPathLen [I] Length of lpszPath
3968  *
3969  * RETURNS
3970  *  Success: S_OK. lpszPath contains the full path to the content.
3971  *  Failure: E_FAIL. The content does not exist or lpszPath is too short.
3972  */
3973 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
3974 {
3975   WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
3976   HRESULT hRet;
3977
3978   TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
3979
3980   MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
3981   szPath[0] = '\0';
3982   hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
3983   WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
3984   return hRet;
3985 }
3986
3987 /*************************************************************************
3988  * @     [SHLWAPI.441]
3989  *
3990  * Unicode version of SHGetWebFolderFilePathA.
3991  */
3992 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
3993 {
3994   static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
3995   static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
3996 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
3997 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
3998   DWORD dwLen, dwFileLen;
3999   LANGID lidSystem, lidUser;
4000
4001   TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
4002
4003   /* Get base directory for web content */
4004   dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4005   if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4006     dwLen--;
4007
4008   dwFileLen = strlenW(lpszFile);
4009
4010   if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4011     return E_FAIL; /* lpszPath too short */
4012
4013   strcpyW(lpszPath+dwLen, szWeb);
4014   dwLen += szWebLen;
4015   dwPathLen = dwPathLen - dwLen; /* Remaining space */
4016
4017   lidSystem = GetSystemDefaultUILanguage();
4018   lidUser = GetUserDefaultUILanguage();
4019
4020   if (lidSystem != lidUser)
4021   {
4022     if (dwFileLen + szWebMuiLen < dwPathLen)
4023     {
4024       /* Use localised content in the users UI language if present */
4025       wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4026       strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4027       if (PathFileExistsW(lpszPath))
4028         return S_OK;
4029     }
4030   }
4031
4032   /* Fall back to OS default installed content */
4033   strcpyW(lpszPath + dwLen, lpszFile);
4034   if (PathFileExistsW(lpszPath))
4035     return S_OK;
4036   return E_FAIL;
4037 }
4038
4039 #define PATH_CHAR_CLASS_LETTER      0x00000001
4040 #define PATH_CHAR_CLASS_ASTERIX     0x00000002
4041 #define PATH_CHAR_CLASS_DOT         0x00000004
4042 #define PATH_CHAR_CLASS_BACKSLASH   0x00000008
4043 #define PATH_CHAR_CLASS_COLON       0x00000010
4044 #define PATH_CHAR_CLASS_SEMICOLON   0x00000020
4045 #define PATH_CHAR_CLASS_COMMA       0x00000040
4046 #define PATH_CHAR_CLASS_SPACE       0x00000080
4047 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4048 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4049
4050 #define PATH_CHAR_CLASS_INVALID     0x00000000
4051 #define PATH_CHAR_CLASS_ANY         0xffffffff
4052
4053 static const DWORD SHELL_charclass[] =
4054 {
4055     /* 0x00 */  PATH_CHAR_CLASS_INVALID,      /* 0x01 */  PATH_CHAR_CLASS_INVALID,
4056     /* 0x02 */  PATH_CHAR_CLASS_INVALID,      /* 0x03 */  PATH_CHAR_CLASS_INVALID,
4057     /* 0x04 */  PATH_CHAR_CLASS_INVALID,      /* 0x05 */  PATH_CHAR_CLASS_INVALID,
4058     /* 0x06 */  PATH_CHAR_CLASS_INVALID,      /* 0x07 */  PATH_CHAR_CLASS_INVALID,
4059     /* 0x08 */  PATH_CHAR_CLASS_INVALID,      /* 0x09 */  PATH_CHAR_CLASS_INVALID,
4060     /* 0x0a */  PATH_CHAR_CLASS_INVALID,      /* 0x0b */  PATH_CHAR_CLASS_INVALID,
4061     /* 0x0c */  PATH_CHAR_CLASS_INVALID,      /* 0x0d */  PATH_CHAR_CLASS_INVALID,
4062     /* 0x0e */  PATH_CHAR_CLASS_INVALID,      /* 0x0f */  PATH_CHAR_CLASS_INVALID,
4063     /* 0x10 */  PATH_CHAR_CLASS_INVALID,      /* 0x11 */  PATH_CHAR_CLASS_INVALID,
4064     /* 0x12 */  PATH_CHAR_CLASS_INVALID,      /* 0x13 */  PATH_CHAR_CLASS_INVALID,
4065     /* 0x14 */  PATH_CHAR_CLASS_INVALID,      /* 0x15 */  PATH_CHAR_CLASS_INVALID,
4066     /* 0x16 */  PATH_CHAR_CLASS_INVALID,      /* 0x17 */  PATH_CHAR_CLASS_INVALID,
4067     /* 0x18 */  PATH_CHAR_CLASS_INVALID,      /* 0x19 */  PATH_CHAR_CLASS_INVALID,
4068     /* 0x1a */  PATH_CHAR_CLASS_INVALID,      /* 0x1b */  PATH_CHAR_CLASS_INVALID,
4069     /* 0x1c */  PATH_CHAR_CLASS_INVALID,      /* 0x1d */  PATH_CHAR_CLASS_INVALID,
4070     /* 0x1e */  PATH_CHAR_CLASS_INVALID,      /* 0x1f */  PATH_CHAR_CLASS_INVALID,
4071     /* ' '  */  PATH_CHAR_CLASS_SPACE,        /* '!'  */  PATH_CHAR_CLASS_OTHER_VALID,
4072     /* '"'  */  PATH_CHAR_CLASS_DOUBLEQUOTE,  /* '#'  */  PATH_CHAR_CLASS_OTHER_VALID,
4073     /* '$'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '%'  */  PATH_CHAR_CLASS_OTHER_VALID,
4074     /* '&'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '\'' */  PATH_CHAR_CLASS_OTHER_VALID,
4075     /* '('  */  PATH_CHAR_CLASS_OTHER_VALID,  /* ')'  */  PATH_CHAR_CLASS_OTHER_VALID,
4076     /* '*'  */  PATH_CHAR_CLASS_ASTERIX,      /* '+'  */  PATH_CHAR_CLASS_OTHER_VALID,
4077     /* ','  */  PATH_CHAR_CLASS_COMMA,        /* '-'  */  PATH_CHAR_CLASS_OTHER_VALID,
4078     /* '.'  */  PATH_CHAR_CLASS_DOT,          /* '/'  */  PATH_CHAR_CLASS_INVALID,
4079     /* '0'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '1'  */  PATH_CHAR_CLASS_OTHER_VALID,
4080     /* '2'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '3'  */  PATH_CHAR_CLASS_OTHER_VALID,
4081     /* '4'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '5'  */  PATH_CHAR_CLASS_OTHER_VALID,
4082     /* '6'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '7'  */  PATH_CHAR_CLASS_OTHER_VALID,
4083     /* '8'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '9'  */  PATH_CHAR_CLASS_OTHER_VALID,
4084     /* ':'  */  PATH_CHAR_CLASS_COLON,        /* ';'  */  PATH_CHAR_CLASS_SEMICOLON,
4085     /* '<'  */  PATH_CHAR_CLASS_INVALID,      /* '='  */  PATH_CHAR_CLASS_OTHER_VALID,
4086     /* '>'  */  PATH_CHAR_CLASS_INVALID,      /* '?'  */  PATH_CHAR_CLASS_LETTER,
4087     /* '@'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* 'A'  */  PATH_CHAR_CLASS_ANY,
4088     /* 'B'  */  PATH_CHAR_CLASS_ANY,          /* 'C'  */  PATH_CHAR_CLASS_ANY,
4089     /* 'D'  */  PATH_CHAR_CLASS_ANY,          /* 'E'  */  PATH_CHAR_CLASS_ANY,
4090     /* 'F'  */  PATH_CHAR_CLASS_ANY,          /* 'G'  */  PATH_CHAR_CLASS_ANY,
4091     /* 'H'  */  PATH_CHAR_CLASS_ANY,          /* 'I'  */  PATH_CHAR_CLASS_ANY,
4092     /* 'J'  */  PATH_CHAR_CLASS_ANY,          /* 'K'  */  PATH_CHAR_CLASS_ANY,
4093     /* 'L'  */  PATH_CHAR_CLASS_ANY,          /* 'M'  */  PATH_CHAR_CLASS_ANY,
4094     /* 'N'  */  PATH_CHAR_CLASS_ANY,          /* 'O'  */  PATH_CHAR_CLASS_ANY,
4095     /* 'P'  */  PATH_CHAR_CLASS_ANY,          /* 'Q'  */  PATH_CHAR_CLASS_ANY,
4096     /* 'R'  */  PATH_CHAR_CLASS_ANY,          /* 'S'  */  PATH_CHAR_CLASS_ANY,
4097     /* 'T'  */  PATH_CHAR_CLASS_ANY,          /* 'U'  */  PATH_CHAR_CLASS_ANY,
4098     /* 'V'  */  PATH_CHAR_CLASS_ANY,          /* 'W'  */  PATH_CHAR_CLASS_ANY,
4099     /* 'X'  */  PATH_CHAR_CLASS_ANY,          /* 'Y'  */  PATH_CHAR_CLASS_ANY,
4100     /* 'Z'  */  PATH_CHAR_CLASS_ANY,          /* '['  */  PATH_CHAR_CLASS_OTHER_VALID,
4101     /* '\\' */  PATH_CHAR_CLASS_BACKSLASH,    /* ']'  */  PATH_CHAR_CLASS_OTHER_VALID,
4102     /* '^'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '_'  */  PATH_CHAR_CLASS_OTHER_VALID,
4103     /* '`'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* 'a'  */  PATH_CHAR_CLASS_ANY,
4104     /* 'b'  */  PATH_CHAR_CLASS_ANY,          /* 'c'  */  PATH_CHAR_CLASS_ANY,
4105     /* 'd'  */  PATH_CHAR_CLASS_ANY,          /* 'e'  */  PATH_CHAR_CLASS_ANY,
4106     /* 'f'  */  PATH_CHAR_CLASS_ANY,          /* 'g'  */  PATH_CHAR_CLASS_ANY,
4107     /* 'h'  */  PATH_CHAR_CLASS_ANY,          /* 'i'  */  PATH_CHAR_CLASS_ANY,
4108     /* 'j'  */  PATH_CHAR_CLASS_ANY,          /* 'k'  */  PATH_CHAR_CLASS_ANY,
4109     /* 'l'  */  PATH_CHAR_CLASS_ANY,          /* 'm'  */  PATH_CHAR_CLASS_ANY,
4110     /* 'n'  */  PATH_CHAR_CLASS_ANY,          /* 'o'  */  PATH_CHAR_CLASS_ANY,
4111     /* 'p'  */  PATH_CHAR_CLASS_ANY,          /* 'q'  */  PATH_CHAR_CLASS_ANY,
4112     /* 'r'  */  PATH_CHAR_CLASS_ANY,          /* 's'  */  PATH_CHAR_CLASS_ANY,
4113     /* 't'  */  PATH_CHAR_CLASS_ANY,          /* 'u'  */  PATH_CHAR_CLASS_ANY,
4114     /* 'v'  */  PATH_CHAR_CLASS_ANY,          /* 'w'  */  PATH_CHAR_CLASS_ANY,
4115     /* 'x'  */  PATH_CHAR_CLASS_ANY,          /* 'y'  */  PATH_CHAR_CLASS_ANY,
4116     /* 'z'  */  PATH_CHAR_CLASS_ANY,          /* '{'  */  PATH_CHAR_CLASS_OTHER_VALID,
4117     /* '|'  */  PATH_CHAR_CLASS_INVALID,      /* '}'  */  PATH_CHAR_CLASS_OTHER_VALID,
4118     /* '~'  */  PATH_CHAR_CLASS_OTHER_VALID
4119 };
4120
4121 /*************************************************************************
4122  * @     [SHLWAPI.455]
4123  *
4124  * Check if an ASCII char is of a certain class
4125  */
4126 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4127 {
4128     if ((unsigned)c > 0x7e)
4129         return class & PATH_CHAR_CLASS_OTHER_VALID;
4130
4131     return class & SHELL_charclass[(unsigned)c];
4132 }
4133
4134 /*************************************************************************
4135  * @     [SHLWAPI.456]
4136  *
4137  * Check if a Unicode char is of a certain class
4138  */
4139 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4140 {
4141     if (c > 0x7e)
4142         return class & PATH_CHAR_CLASS_OTHER_VALID;
4143
4144     return class & SHELL_charclass[c];
4145 }