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