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