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