dxdiagn: Remove unneeded casts.
[wine.git] / dlls / shlwapi / path.c
blobb2005c1e787f2ec608cc45b5352e0d06941c1175
1 /*
2 * Path Functions
4 * Copyright 1999, 2000 Juergen Schmied
5 * Copyright 2001, 2002 Jon Griffiths
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include "wine/unicode.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #define NO_SHLWAPI_STREAM
37 #include "shlwapi.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42 /* Get a function pointer from a DLL handle */
43 #define GET_FUNC(func, module, name, fail) \
44 do { \
45 if (!func) { \
46 if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
47 func = (fn##func)GetProcAddress(SHLWAPI_h##module, name); \
48 if (!func) return fail; \
49 } \
50 } while (0)
52 /* DLL handles for late bound calls */
53 static HMODULE SHLWAPI_hshell32;
55 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
56 typedef BOOL (WINAPI *fnpIsNetDrive)(int);
57 static fnpIsNetDrive pIsNetDrive;
59 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR,LPWSTR,DWORD);
61 /*************************************************************************
62 * PathAppendA [SHLWAPI.@]
64 * Append one path to another.
66 * PARAMS
67 * lpszPath [I/O] Initial part of path, and destination for output
68 * lpszAppend [I] Path to append
70 * RETURNS
71 * Success: TRUE. lpszPath contains the newly created path.
72 * Failure: FALSE, if either path is NULL, or PathCombineA() fails.
74 * NOTES
75 * lpszAppend must contain at least one backslash ('\') if not NULL.
76 * Because PathCombineA() is used to join the paths, the resulting
77 * path is also canonicalized.
79 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
81 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
83 if (lpszPath && lpszAppend)
85 if (!PathIsUNCA(lpszAppend))
86 while (*lpszAppend == '\\')
87 lpszAppend++;
88 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
89 return TRUE;
91 return FALSE;
94 /*************************************************************************
95 * PathAppendW [SHLWAPI.@]
97 * See PathAppendA.
99 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
101 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
103 if (lpszPath && lpszAppend)
105 if (!PathIsUNCW(lpszAppend))
106 while (*lpszAppend == '\\')
107 lpszAppend++;
108 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
109 return TRUE;
111 return FALSE;
114 /*************************************************************************
115 * PathCombineA [SHLWAPI.@]
117 * Combine two paths together.
119 * PARAMS
120 * lpszDest [O] Destination for combined path
121 * lpszDir [I] Directory path
122 * lpszFile [I] File path
124 * RETURNS
125 * Success: The output path
126 * Failure: NULL, if inputs are invalid.
128 * NOTES
129 * lpszDest should be at least MAX_PATH in size, and may point to the same
130 * memory location as lpszDir. The combined path is canonicalised.
132 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
134 WCHAR szDest[MAX_PATH];
135 WCHAR szDir[MAX_PATH];
136 WCHAR szFile[MAX_PATH];
137 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
139 /* Invalid parameters */
140 if (!lpszDest)
141 return NULL;
142 if (!lpszDir && !lpszFile)
144 lpszDest[0] = 0;
145 return NULL;
148 if (lpszDir)
149 MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH);
150 if (lpszFile)
151 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
153 if (PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL))
154 if (WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0))
155 return lpszDest;
157 lpszDest[0] = 0;
158 return NULL;
161 /*************************************************************************
162 * PathCombineW [SHLWAPI.@]
164 * See PathCombineA.
166 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
168 WCHAR szTemp[MAX_PATH];
169 BOOL bUseBoth = FALSE, bStrip = FALSE;
171 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
173 /* Invalid parameters */
174 if (!lpszDest)
175 return NULL;
176 if (!lpszDir && !lpszFile)
178 lpszDest[0] = 0;
179 return NULL;
182 if ((!lpszFile || !*lpszFile) && lpszDir)
184 /* Use dir only */
185 lstrcpynW(szTemp, lpszDir, MAX_PATH);
187 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
189 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
191 /* Use file only */
192 lstrcpynW(szTemp, lpszFile, MAX_PATH);
194 else
196 bUseBoth = TRUE;
197 bStrip = TRUE;
200 else
201 bUseBoth = TRUE;
203 if (bUseBoth)
205 lstrcpynW(szTemp, lpszDir, MAX_PATH);
206 if (bStrip)
208 PathStripToRootW(szTemp);
209 lpszFile++; /* Skip '\' */
211 if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
213 lpszDest[0] = 0;
214 return NULL;
216 strcatW(szTemp, lpszFile);
219 PathCanonicalizeW(lpszDest, szTemp);
220 return lpszDest;
223 /*************************************************************************
224 * PathAddBackslashA [SHLWAPI.@]
226 * Append a backslash ('\') to a path if one doesn't exist.
228 * PARAMS
229 * lpszPath [I/O] The path to append a backslash to.
231 * RETURNS
232 * Success: The position of the last backslash in the path.
233 * Failure: NULL, if lpszPath is NULL or the path is too large.
235 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
237 size_t iLen;
239 TRACE("(%s)\n",debugstr_a(lpszPath));
241 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
242 return NULL;
244 if (iLen)
246 lpszPath += iLen;
247 if (lpszPath[-1] != '\\')
249 *lpszPath++ = '\\';
250 *lpszPath = '\0';
253 return lpszPath;
256 /*************************************************************************
257 * PathAddBackslashW [SHLWAPI.@]
259 * See PathAddBackslashA.
261 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
263 size_t iLen;
265 TRACE("(%s)\n",debugstr_w(lpszPath));
267 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
268 return NULL;
270 if (iLen)
272 lpszPath += iLen;
273 if (lpszPath[-1] != '\\')
275 *lpszPath++ = '\\';
276 *lpszPath = '\0';
279 return lpszPath;
282 /*************************************************************************
283 * PathBuildRootA [SHLWAPI.@]
285 * Create a root drive string (e.g. "A:\") from a drive number.
287 * PARAMS
288 * lpszPath [O] Destination for the drive string
290 * RETURNS
291 * lpszPath
293 * NOTES
294 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
296 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
298 TRACE("(%p,%d)\n", lpszPath, drive);
300 if (lpszPath && drive >= 0 && drive < 26)
302 lpszPath[0] = 'A' + drive;
303 lpszPath[1] = ':';
304 lpszPath[2] = '\\';
305 lpszPath[3] = '\0';
307 return lpszPath;
310 /*************************************************************************
311 * PathBuildRootW [SHLWAPI.@]
313 * See PathBuildRootA.
315 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
317 TRACE("(%p,%d)\n", lpszPath, drive);
319 if (lpszPath && drive >= 0 && drive < 26)
321 lpszPath[0] = 'A' + drive;
322 lpszPath[1] = ':';
323 lpszPath[2] = '\\';
324 lpszPath[3] = '\0';
326 return lpszPath;
329 /*************************************************************************
330 * PathFindFileNameA [SHLWAPI.@]
332 * Locate the start of the file name in a path
334 * PARAMS
335 * lpszPath [I] Path to search
337 * RETURNS
338 * A pointer to the first character of the file name
340 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
342 LPCSTR lastSlash = lpszPath;
344 TRACE("(%s)\n",debugstr_a(lpszPath));
346 while (lpszPath && *lpszPath)
348 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
349 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
350 lastSlash = lpszPath + 1;
351 lpszPath = CharNextA(lpszPath);
353 return (LPSTR)lastSlash;
356 /*************************************************************************
357 * PathFindFileNameW [SHLWAPI.@]
359 * See PathFindFileNameA.
361 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
363 LPCWSTR lastSlash = lpszPath;
365 TRACE("(%s)\n",debugstr_w(lpszPath));
367 while (lpszPath && *lpszPath)
369 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
370 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
371 lastSlash = lpszPath + 1;
372 lpszPath++;
374 return (LPWSTR)lastSlash;
377 /*************************************************************************
378 * PathFindExtensionA [SHLWAPI.@]
380 * Locate the start of the file extension in a path
382 * PARAMS
383 * lpszPath [I] The path to search
385 * RETURNS
386 * A pointer to the first character of the extension, the end of
387 * the string if the path has no extension, or NULL If lpszPath is NULL
389 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
391 LPCSTR lastpoint = NULL;
393 TRACE("(%s)\n", debugstr_a(lpszPath));
395 if (lpszPath)
397 while (*lpszPath)
399 if (*lpszPath == '\\' || *lpszPath==' ')
400 lastpoint = NULL;
401 else if (*lpszPath == '.')
402 lastpoint = lpszPath;
403 lpszPath = CharNextA(lpszPath);
406 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
409 /*************************************************************************
410 * PathFindExtensionW [SHLWAPI.@]
412 * See PathFindExtensionA.
414 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
416 LPCWSTR lastpoint = NULL;
418 TRACE("(%s)\n", debugstr_w(lpszPath));
420 if (lpszPath)
422 while (*lpszPath)
424 if (*lpszPath == '\\' || *lpszPath==' ')
425 lastpoint = NULL;
426 else if (*lpszPath == '.')
427 lastpoint = lpszPath;
428 lpszPath++;
431 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
434 /*************************************************************************
435 * PathGetArgsA [SHLWAPI.@]
437 * Find the next argument in a string delimited by spaces.
439 * PARAMS
440 * lpszPath [I] The string to search for arguments in
442 * RETURNS
443 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
445 * NOTES
446 * Spaces in quoted strings are ignored as delimiters.
448 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
450 BOOL bSeenQuote = FALSE;
452 TRACE("(%s)\n",debugstr_a(lpszPath));
454 if (lpszPath)
456 while (*lpszPath)
458 if ((*lpszPath==' ') && !bSeenQuote)
459 return (LPSTR)lpszPath + 1;
460 if (*lpszPath == '"')
461 bSeenQuote = !bSeenQuote;
462 lpszPath = CharNextA(lpszPath);
465 return (LPSTR)lpszPath;
468 /*************************************************************************
469 * PathGetArgsW [SHLWAPI.@]
471 * See PathGetArgsA.
473 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
475 BOOL bSeenQuote = FALSE;
477 TRACE("(%s)\n",debugstr_w(lpszPath));
479 if (lpszPath)
481 while (*lpszPath)
483 if ((*lpszPath==' ') && !bSeenQuote)
484 return (LPWSTR)lpszPath + 1;
485 if (*lpszPath == '"')
486 bSeenQuote = !bSeenQuote;
487 lpszPath++;
490 return (LPWSTR)lpszPath;
493 /*************************************************************************
494 * PathGetDriveNumberA [SHLWAPI.@]
496 * Return the drive number from a path
498 * PARAMS
499 * lpszPath [I] Path to get the drive number from
501 * RETURNS
502 * Success: The drive number corresponding to the drive in the path
503 * Failure: -1, if lpszPath contains no valid drive
505 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
507 TRACE ("(%s)\n",debugstr_a(lpszPath));
509 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
510 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
511 return tolower(*lpszPath) - 'a';
512 return -1;
515 /*************************************************************************
516 * PathGetDriveNumberW [SHLWAPI.@]
518 * See PathGetDriveNumberA.
520 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
522 TRACE ("(%s)\n",debugstr_w(lpszPath));
524 if (lpszPath)
526 WCHAR tl = tolowerW(lpszPath[0]);
527 if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
528 return tl - 'a';
530 return -1;
533 /*************************************************************************
534 * PathRemoveFileSpecA [SHLWAPI.@]
536 * Remove the file specification from a path.
538 * PARAMS
539 * lpszPath [I/O] Path to remove the file spec from
541 * RETURNS
542 * TRUE If the path was valid and modified
543 * FALSE Otherwise
545 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
547 LPSTR lpszFileSpec = lpszPath;
548 BOOL bModified = FALSE;
550 TRACE("(%s)\n",debugstr_a(lpszPath));
552 if(lpszPath)
554 /* Skip directory or UNC path */
555 if (*lpszPath == '\\')
556 lpszFileSpec = ++lpszPath;
557 if (*lpszPath == '\\')
558 lpszFileSpec = ++lpszPath;
560 while (*lpszPath)
562 if(*lpszPath == '\\')
563 lpszFileSpec = lpszPath; /* Skip dir */
564 else if(*lpszPath == ':')
566 lpszFileSpec = ++lpszPath; /* Skip drive */
567 if (*lpszPath == '\\')
568 lpszFileSpec++;
570 if (!(lpszPath = CharNextA(lpszPath)))
571 break;
574 if (*lpszFileSpec)
576 *lpszFileSpec = '\0';
577 bModified = TRUE;
580 return bModified;
583 /*************************************************************************
584 * PathRemoveFileSpecW [SHLWAPI.@]
586 * See PathRemoveFileSpecA.
588 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
590 LPWSTR lpszFileSpec = lpszPath;
591 BOOL bModified = FALSE;
593 TRACE("(%s)\n",debugstr_w(lpszPath));
595 if(lpszPath)
597 /* Skip directory or UNC path */
598 if (*lpszPath == '\\')
599 lpszFileSpec = ++lpszPath;
600 if (*lpszPath == '\\')
601 lpszFileSpec = ++lpszPath;
603 while (*lpszPath)
605 if(*lpszPath == '\\')
606 lpszFileSpec = lpszPath; /* Skip dir */
607 else if(*lpszPath == ':')
609 lpszFileSpec = ++lpszPath; /* Skip drive */
610 if (*lpszPath == '\\')
611 lpszFileSpec++;
613 lpszPath++;
616 if (*lpszFileSpec)
618 *lpszFileSpec = '\0';
619 bModified = TRUE;
622 return bModified;
625 /*************************************************************************
626 * PathStripPathA [SHLWAPI.@]
628 * Remove the initial path from the beginning of a filename
630 * PARAMS
631 * lpszPath [I/O] Path to remove the initial path from
633 * RETURNS
634 * Nothing.
636 void WINAPI PathStripPathA(LPSTR lpszPath)
638 TRACE("(%s)\n", debugstr_a(lpszPath));
640 if (lpszPath)
642 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
643 if(lpszFileName)
644 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
648 /*************************************************************************
649 * PathStripPathW [SHLWAPI.@]
651 * See PathStripPathA.
653 void WINAPI PathStripPathW(LPWSTR lpszPath)
655 LPWSTR lpszFileName;
657 TRACE("(%s)\n", debugstr_w(lpszPath));
658 lpszFileName = PathFindFileNameW(lpszPath);
659 if(lpszFileName)
660 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
663 /*************************************************************************
664 * PathStripToRootA [SHLWAPI.@]
666 * Reduce a path to its root.
668 * PARAMS
669 * lpszPath [I/O] the path to reduce
671 * RETURNS
672 * Success: TRUE if the stripped path is a root path
673 * Failure: FALSE if the path cannot be stripped or is NULL
675 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
677 TRACE("(%s)\n", debugstr_a(lpszPath));
679 if (!lpszPath)
680 return FALSE;
681 while(!PathIsRootA(lpszPath))
682 if (!PathRemoveFileSpecA(lpszPath))
683 return FALSE;
684 return TRUE;
687 /*************************************************************************
688 * PathStripToRootW [SHLWAPI.@]
690 * See PathStripToRootA.
692 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
694 TRACE("(%s)\n", debugstr_w(lpszPath));
696 if (!lpszPath)
697 return FALSE;
698 while(!PathIsRootW(lpszPath))
699 if (!PathRemoveFileSpecW(lpszPath))
700 return FALSE;
701 return TRUE;
704 /*************************************************************************
705 * PathRemoveArgsA [SHLWAPI.@]
707 * Strip space separated arguments from a path.
709 * PARAMS
710 * lpszPath [I/O] Path to remove arguments from
712 * RETURNS
713 * Nothing.
715 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
717 TRACE("(%s)\n",debugstr_a(lpszPath));
719 if(lpszPath)
721 LPSTR lpszArgs = PathGetArgsA(lpszPath);
722 if (*lpszArgs)
723 lpszArgs[-1] = '\0';
724 else
726 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
727 if(*lpszLastChar == ' ')
728 *lpszLastChar = '\0';
733 /*************************************************************************
734 * PathRemoveArgsW [SHLWAPI.@]
736 * See PathRemoveArgsA.
738 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
740 TRACE("(%s)\n",debugstr_w(lpszPath));
742 if(lpszPath)
744 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
745 if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
746 lpszArgs[-1] = '\0';
750 /*************************************************************************
751 * PathRemoveExtensionA [SHLWAPI.@]
753 * Remove the file extension from a path
755 * PARAMS
756 * lpszPath [I/O] Path to remove the extension from
758 * RETURNS
759 * Nothing.
761 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
763 TRACE("(%s)\n", debugstr_a(lpszPath));
765 if (lpszPath)
767 lpszPath = PathFindExtensionA(lpszPath);
768 *lpszPath = '\0';
772 /*************************************************************************
773 * PathRemoveExtensionW [SHLWAPI.@]
775 * See PathRemoveExtensionA.
777 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
779 TRACE("(%s)\n", debugstr_w(lpszPath));
781 if (lpszPath)
783 lpszPath = PathFindExtensionW(lpszPath);
784 *lpszPath = '\0';
788 /*************************************************************************
789 * PathRemoveBackslashA [SHLWAPI.@]
791 * Remove a trailing backslash from a path.
793 * PARAMS
794 * lpszPath [I/O] Path to remove backslash from
796 * RETURNS
797 * Success: A pointer to the end of the path
798 * Failure: NULL, if lpszPath is NULL
800 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
802 LPSTR szTemp = NULL;
804 TRACE("(%s)\n", debugstr_a(lpszPath));
806 if(lpszPath)
808 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
809 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
810 *szTemp = '\0';
812 return szTemp;
815 /*************************************************************************
816 * PathRemoveBackslashW [SHLWAPI.@]
818 * See PathRemoveBackslashA.
820 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
822 LPWSTR szTemp = NULL;
824 TRACE("(%s)\n", debugstr_w(lpszPath));
826 if(lpszPath)
828 szTemp = lpszPath + strlenW(lpszPath);
829 if (szTemp > lpszPath) szTemp--;
830 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
831 *szTemp = '\0';
833 return szTemp;
836 /*************************************************************************
837 * PathRemoveBlanksA [SHLWAPI.@]
839 * Remove Spaces from the start and end of a path.
841 * PARAMS
842 * lpszPath [I/O] Path to strip blanks from
844 * RETURNS
845 * Nothing.
847 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
849 TRACE("(%s)\n", debugstr_a(lpszPath));
851 if(lpszPath && *lpszPath)
853 LPSTR start = lpszPath;
855 while (*lpszPath == ' ')
856 lpszPath = CharNextA(lpszPath);
858 while(*lpszPath)
859 *start++ = *lpszPath++;
861 if (start != lpszPath)
862 while (start[-1] == ' ')
863 start--;
864 *start = '\0';
868 /*************************************************************************
869 * PathRemoveBlanksW [SHLWAPI.@]
871 * See PathRemoveBlanksA.
873 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
875 TRACE("(%s)\n", debugstr_w(lpszPath));
877 if(lpszPath && *lpszPath)
879 LPWSTR start = lpszPath;
881 while (*lpszPath == ' ')
882 lpszPath++;
884 while(*lpszPath)
885 *start++ = *lpszPath++;
887 if (start != lpszPath)
888 while (start[-1] == ' ')
889 start--;
890 *start = '\0';
894 /*************************************************************************
895 * PathQuoteSpacesA [SHLWAPI.@]
897 * Surround a path containg spaces in quotes.
899 * PARAMS
900 * lpszPath [I/O] Path to quote
902 * RETURNS
903 * Nothing.
905 * NOTES
906 * The path is not changed if it is invalid or has no spaces.
908 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
910 TRACE("(%s)\n", debugstr_a(lpszPath));
912 if(lpszPath && StrChrA(lpszPath,' '))
914 size_t iLen = strlen(lpszPath) + 1;
916 if (iLen + 2 < MAX_PATH)
918 memmove(lpszPath + 1, lpszPath, iLen);
919 lpszPath[0] = '"';
920 lpszPath[iLen] = '"';
921 lpszPath[iLen + 1] = '\0';
926 /*************************************************************************
927 * PathQuoteSpacesW [SHLWAPI.@]
929 * See PathQuoteSpacesA.
931 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
933 TRACE("(%s)\n", debugstr_w(lpszPath));
935 if(lpszPath && StrChrW(lpszPath,' '))
937 int iLen = strlenW(lpszPath) + 1;
939 if (iLen + 2 < MAX_PATH)
941 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
942 lpszPath[0] = '"';
943 lpszPath[iLen] = '"';
944 lpszPath[iLen + 1] = '\0';
949 /*************************************************************************
950 * PathUnquoteSpacesA [SHLWAPI.@]
952 * Remove quotes ("") from around a path, if present.
954 * PARAMS
955 * lpszPath [I/O] Path to strip quotes from
957 * RETURNS
958 * Nothing
960 * NOTES
961 * If the path contains a single quote only, an empty string will result.
962 * Otherwise quotes are only removed if they appear at the start and end
963 * of the path.
965 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
967 TRACE("(%s)\n", debugstr_a(lpszPath));
969 if (lpszPath && *lpszPath == '"')
971 DWORD dwLen = strlen(lpszPath) - 1;
973 if (lpszPath[dwLen] == '"')
975 lpszPath[dwLen] = '\0';
976 for (; *lpszPath; lpszPath++)
977 *lpszPath = lpszPath[1];
982 /*************************************************************************
983 * PathUnquoteSpacesW [SHLWAPI.@]
985 * See PathUnquoteSpacesA.
987 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
989 TRACE("(%s)\n", debugstr_w(lpszPath));
991 if (lpszPath && *lpszPath == '"')
993 DWORD dwLen = strlenW(lpszPath) - 1;
995 if (lpszPath[dwLen] == '"')
997 lpszPath[dwLen] = '\0';
998 for (; *lpszPath; lpszPath++)
999 *lpszPath = lpszPath[1];
1004 /*************************************************************************
1005 * PathParseIconLocationA [SHLWAPI.@]
1007 * Parse the location of an icon from a path.
1009 * PARAMS
1010 * lpszPath [I/O] The path to parse the icon location from.
1012 * RETURNS
1013 * Success: The number of the icon
1014 * Failure: 0 if the path does not contain an icon location or is NULL
1016 * NOTES
1017 * The path has surrounding quotes and spaces removed regardless
1018 * of whether the call succeeds or not.
1020 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1022 int iRet = 0;
1023 LPSTR lpszComma;
1025 TRACE("(%s)\n", debugstr_a(lpszPath));
1027 if (lpszPath)
1029 if ((lpszComma = strchr(lpszPath, ',')))
1031 *lpszComma++ = '\0';
1032 iRet = StrToIntA(lpszComma);
1034 PathUnquoteSpacesA(lpszPath);
1035 PathRemoveBlanksA(lpszPath);
1037 return iRet;
1040 /*************************************************************************
1041 * PathParseIconLocationW [SHLWAPI.@]
1043 * See PathParseIconLocationA.
1045 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1047 int iRet = 0;
1048 LPWSTR lpszComma;
1050 TRACE("(%s)\n", debugstr_w(lpszPath));
1052 if (lpszPath)
1054 if ((lpszComma = StrChrW(lpszPath, ',')))
1056 *lpszComma++ = '\0';
1057 iRet = StrToIntW(lpszComma);
1059 PathUnquoteSpacesW(lpszPath);
1060 PathRemoveBlanksW(lpszPath);
1062 return iRet;
1065 /*************************************************************************
1066 * @ [SHLWAPI.4]
1068 * Unicode version of PathFileExistsDefExtA.
1070 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1072 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
1073 { '.', 'c', 'o', 'm', 0},
1074 { '.', 'e', 'x', 'e', 0},
1075 { '.', 'b', 'a', 't', 0},
1076 { '.', 'l', 'n', 'k', 0},
1077 { '.', 'c', 'm', 'd', 0},
1078 { 0, 0, 0, 0, 0} };
1080 TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1082 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1083 return FALSE;
1085 if (dwWhich)
1087 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1088 if (!*szExt || dwWhich & 0x40)
1090 size_t iChoose = 0;
1091 int iLen = lstrlenW(lpszPath);
1092 if (iLen > (MAX_PATH - 5))
1093 return FALSE;
1094 while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1096 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1097 if (PathFileExistsW(lpszPath))
1098 return TRUE;
1099 iChoose++;
1100 dwWhich >>= 1;
1102 *(lpszPath + iLen) = (WCHAR)'\0';
1103 return FALSE;
1106 return PathFileExistsW(lpszPath);
1109 /*************************************************************************
1110 * @ [SHLWAPI.3]
1112 * Determine if a file exists locally and is of an executable type.
1114 * PARAMS
1115 * lpszPath [I/O] File to search for
1116 * dwWhich [I] Type of executable to search for
1118 * RETURNS
1119 * TRUE If the file was found. lpszPath contains the file name.
1120 * FALSE Otherwise.
1122 * NOTES
1123 * lpszPath is modified in place and must be at least MAX_PATH in length.
1124 * If the function returns FALSE, the path is modified to its original state.
1125 * If the given path contains an extension or dwWhich is 0, executable
1126 * extensions are not checked.
1128 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1129 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1130 * their own developers exclusive use. Monopoly, anyone?
1132 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1134 BOOL bRet = FALSE;
1136 TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1138 if (lpszPath)
1140 WCHAR szPath[MAX_PATH];
1141 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1142 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1143 if (bRet)
1144 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1146 return bRet;
1149 /*************************************************************************
1150 * SHLWAPI_PathFindInOtherDirs
1152 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1154 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1156 static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1157 static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1158 DWORD dwLenPATH;
1159 LPCWSTR lpszCurr;
1160 WCHAR *lpszPATH;
1161 WCHAR buff[MAX_PATH];
1163 TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1165 /* Try system directories */
1166 GetSystemDirectoryW(buff, MAX_PATH);
1167 if (!PathAppendW(buff, lpszFile))
1168 return FALSE;
1169 if (PathFileExistsDefExtW(buff, dwWhich))
1171 strcpyW(lpszFile, buff);
1172 return TRUE;
1174 GetWindowsDirectoryW(buff, MAX_PATH);
1175 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1176 return FALSE;
1177 if (PathFileExistsDefExtW(buff, dwWhich))
1179 strcpyW(lpszFile, buff);
1180 return TRUE;
1182 GetWindowsDirectoryW(buff, MAX_PATH);
1183 if (!PathAppendW(buff, lpszFile))
1184 return FALSE;
1185 if (PathFileExistsDefExtW(buff, dwWhich))
1187 strcpyW(lpszFile, buff);
1188 return TRUE;
1190 /* Try dirs listed in %PATH% */
1191 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1193 if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1194 return FALSE;
1196 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1197 lpszCurr = lpszPATH;
1198 while (lpszCurr)
1200 LPCWSTR lpszEnd = lpszCurr;
1201 LPWSTR pBuff = buff;
1203 while (*lpszEnd == ' ')
1204 lpszEnd++;
1205 while (*lpszEnd && *lpszEnd != ';')
1206 *pBuff++ = *lpszEnd++;
1207 *pBuff = '\0';
1209 if (*lpszEnd)
1210 lpszCurr = lpszEnd + 1;
1211 else
1212 lpszCurr = NULL; /* Last Path, terminate after this */
1214 if (!PathAppendW(buff, lpszFile))
1216 HeapFree(GetProcessHeap(), 0, lpszPATH);
1217 return FALSE;
1219 if (PathFileExistsDefExtW(buff, dwWhich))
1221 strcpyW(lpszFile, buff);
1222 HeapFree(GetProcessHeap(), 0, lpszPATH);
1223 return TRUE;
1226 HeapFree(GetProcessHeap(), 0, lpszPATH);
1227 return FALSE;
1230 /*************************************************************************
1231 * @ [SHLWAPI.5]
1233 * Search a range of paths for a specific type of executable.
1235 * PARAMS
1236 * lpszFile [I/O] File to search for
1237 * lppszOtherDirs [I] Other directories to look in
1238 * dwWhich [I] Type of executable to search for
1240 * RETURNS
1241 * Success: TRUE. The path to the executable is stored in lpszFile.
1242 * Failure: FALSE. The path to the executable is unchanged.
1244 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1246 WCHAR szFile[MAX_PATH];
1247 WCHAR buff[MAX_PATH];
1249 TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1251 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1252 return FALSE;
1254 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1256 /* Search provided directories first */
1257 if (lppszOtherDirs && *lppszOtherDirs)
1259 WCHAR szOther[MAX_PATH];
1260 LPCSTR *lpszOtherPath = lppszOtherDirs;
1262 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1264 MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1265 PathCombineW(buff, szOther, szFile);
1266 if (PathFileExistsDefExtW(buff, dwWhich))
1268 WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1269 return TRUE;
1271 lpszOtherPath++;
1274 /* Not found, try system and path dirs */
1275 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1277 WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1278 return TRUE;
1280 return FALSE;
1283 /*************************************************************************
1284 * @ [SHLWAPI.6]
1286 * Unicode version of PathFindOnPathExA.
1288 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1290 WCHAR buff[MAX_PATH];
1292 TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1294 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1295 return FALSE;
1297 /* Search provided directories first */
1298 if (lppszOtherDirs && *lppszOtherDirs)
1300 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1301 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1303 PathCombineW(buff, *lpszOtherPath, lpszFile);
1304 if (PathFileExistsDefExtW(buff, dwWhich))
1306 strcpyW(lpszFile, buff);
1307 return TRUE;
1309 lpszOtherPath++;
1312 /* Not found, try system and path dirs */
1313 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1316 /*************************************************************************
1317 * PathFindOnPathA [SHLWAPI.@]
1319 * Search a range of paths for an executable.
1321 * PARAMS
1322 * lpszFile [I/O] File to search for
1323 * lppszOtherDirs [I] Other directories to look in
1325 * RETURNS
1326 * Success: TRUE. The path to the executable is stored in lpszFile.
1327 * Failure: FALSE. The path to the executable is unchanged.
1329 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1331 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1332 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1335 /*************************************************************************
1336 * PathFindOnPathW [SHLWAPI.@]
1338 * See PathFindOnPathA.
1340 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1342 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1343 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1346 /*************************************************************************
1347 * PathCompactPathExA [SHLWAPI.@]
1349 * Compact a path into a given number of characters.
1351 * PARAMS
1352 * lpszDest [O] Destination for compacted path
1353 * lpszPath [I] Source path
1354 * cchMax [I] Maximum size of compacted path
1355 * dwFlags [I] Reserved
1357 * RETURNS
1358 * Success: TRUE. The compacted path is written to lpszDest.
1359 * Failure: FALSE. lpszPath is undefined.
1361 * NOTES
1362 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1364 * The Win32 version of this function contains a bug: When cchMax == 7,
1365 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1366 * implementation.
1368 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1369 * because Win32 will insert a "\" in lpszDest, even if one is
1370 * not present in the original path.
1372 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1373 UINT cchMax, DWORD dwFlags)
1375 BOOL bRet = FALSE;
1377 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1379 if (lpszPath && lpszDest)
1381 WCHAR szPath[MAX_PATH];
1382 WCHAR szDest[MAX_PATH];
1384 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1385 szDest[0] = '\0';
1386 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1387 WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1389 return bRet;
1392 /*************************************************************************
1393 * PathCompactPathExW [SHLWAPI.@]
1395 * See PathCompactPathExA.
1397 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1398 UINT cchMax, DWORD dwFlags)
1400 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1401 LPCWSTR lpszFile;
1402 DWORD dwLen, dwFileLen = 0;
1404 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1406 if (!lpszPath)
1407 return FALSE;
1409 if (!lpszDest)
1411 WARN("Invalid lpszDest would crash under Win32!\n");
1412 return FALSE;
1415 *lpszDest = '\0';
1417 if (cchMax < 2)
1418 return TRUE;
1420 dwLen = strlenW(lpszPath) + 1;
1422 if (dwLen < cchMax)
1424 /* Don't need to compact */
1425 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1426 return TRUE;
1429 /* Path must be compacted to fit into lpszDest */
1430 lpszFile = PathFindFileNameW(lpszPath);
1431 dwFileLen = lpszPath + dwLen - lpszFile;
1433 if (dwFileLen == dwLen)
1435 /* No root in psth */
1436 if (cchMax <= 4)
1438 while (--cchMax > 0) /* No room left for anything but ellipses */
1439 *lpszDest++ = '.';
1440 *lpszDest = '\0';
1441 return TRUE;
1443 /* Compact the file name with ellipses at the end */
1444 cchMax -= 4;
1445 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1446 strcpyW(lpszDest + cchMax, szEllipses);
1447 return TRUE;
1449 /* We have a root in the path */
1450 lpszFile--; /* Start compacted filename with the path separator */
1451 dwFileLen++;
1453 if (dwFileLen + 3 > cchMax)
1455 /* Compact the file name */
1456 if (cchMax <= 4)
1458 while (--cchMax > 0) /* No room left for anything but ellipses */
1459 *lpszDest++ = '.';
1460 *lpszDest = '\0';
1461 return TRUE;
1463 strcpyW(lpszDest, szEllipses);
1464 lpszDest += 3;
1465 cchMax -= 4;
1466 *lpszDest++ = *lpszFile++;
1467 if (cchMax <= 4)
1469 while (--cchMax > 0) /* No room left for anything but ellipses */
1470 *lpszDest++ = '.';
1471 *lpszDest = '\0';
1472 return TRUE;
1474 cchMax -= 4;
1475 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1476 strcpyW(lpszDest + cchMax, szEllipses);
1477 return TRUE;
1480 /* Only the root needs to be Compacted */
1481 dwLen = cchMax - dwFileLen - 3;
1482 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1483 strcpyW(lpszDest + dwLen, szEllipses);
1484 strcpyW(lpszDest + dwLen + 3, lpszFile);
1485 return TRUE;
1488 /*************************************************************************
1489 * PathIsRelativeA [SHLWAPI.@]
1491 * Determine if a path is a relative path.
1493 * PARAMS
1494 * lpszPath [I] Path to check
1496 * RETURNS
1497 * TRUE: The path is relative, or is invalid.
1498 * FALSE: The path is not relative.
1500 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1502 TRACE("(%s)\n",debugstr_a(lpszPath));
1504 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1505 return TRUE;
1506 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1507 return FALSE;
1508 return TRUE;
1511 /*************************************************************************
1512 * PathIsRelativeW [SHLWAPI.@]
1514 * See PathIsRelativeA.
1516 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1518 TRACE("(%s)\n",debugstr_w(lpszPath));
1520 if (!lpszPath || !*lpszPath)
1521 return TRUE;
1522 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1523 return FALSE;
1524 return TRUE;
1527 /*************************************************************************
1528 * PathIsRootA [SHLWAPI.@]
1530 * Determine if a path is a root path.
1532 * PARAMS
1533 * lpszPath [I] Path to check
1535 * RETURNS
1536 * TRUE If lpszPath is valid and a root path,
1537 * FALSE Otherwise
1539 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1541 TRACE("(%s)\n", debugstr_a(lpszPath));
1543 if (lpszPath && *lpszPath)
1545 if (*lpszPath == '\\')
1547 if (!lpszPath[1])
1548 return TRUE; /* \ */
1549 else if (lpszPath[1]=='\\')
1551 BOOL bSeenSlash = FALSE;
1552 lpszPath += 2;
1554 /* Check for UNC root path */
1555 while (*lpszPath)
1557 if (*lpszPath == '\\')
1559 if (bSeenSlash)
1560 return FALSE;
1561 bSeenSlash = TRUE;
1563 lpszPath = CharNextA(lpszPath);
1565 return TRUE;
1568 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1569 return TRUE; /* X:\ */
1571 return FALSE;
1574 /*************************************************************************
1575 * PathIsRootW [SHLWAPI.@]
1577 * See PathIsRootA.
1579 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1581 TRACE("(%s)\n", debugstr_w(lpszPath));
1583 if (lpszPath && *lpszPath)
1585 if (*lpszPath == '\\')
1587 if (!lpszPath[1])
1588 return TRUE; /* \ */
1589 else if (lpszPath[1]=='\\')
1591 BOOL bSeenSlash = FALSE;
1592 lpszPath += 2;
1594 /* Check for UNC root path */
1595 while (*lpszPath)
1597 if (*lpszPath == '\\')
1599 if (bSeenSlash)
1600 return FALSE;
1601 bSeenSlash = TRUE;
1603 lpszPath++;
1605 return TRUE;
1608 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1609 return TRUE; /* X:\ */
1611 return FALSE;
1614 /*************************************************************************
1615 * PathIsDirectoryA [SHLWAPI.@]
1617 * Determine if a path is a valid directory
1619 * PARAMS
1620 * lpszPath [I] Path to check.
1622 * RETURNS
1623 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1624 * FALSE if lpszPath is invalid or not a directory.
1626 * NOTES
1627 * Although this function is prototyped as returning a BOOL, it returns
1628 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1630 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1631 *| ...
1633 * will always fail.
1635 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1637 DWORD dwAttr;
1639 TRACE("(%s)\n", debugstr_a(lpszPath));
1641 if (!lpszPath || PathIsUNCServerA(lpszPath))
1642 return FALSE;
1644 if (PathIsUNCServerShareA(lpszPath))
1646 FIXME("UNC Server Share not yet supported - FAILING\n");
1647 return FALSE;
1650 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1651 return FALSE;
1652 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1655 /*************************************************************************
1656 * PathIsDirectoryW [SHLWAPI.@]
1658 * See PathIsDirectoryA.
1660 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1662 DWORD dwAttr;
1664 TRACE("(%s)\n", debugstr_w(lpszPath));
1666 if (!lpszPath || PathIsUNCServerW(lpszPath))
1667 return FALSE;
1669 if (PathIsUNCServerShareW(lpszPath))
1671 FIXME("UNC Server Share not yet supported - FAILING\n");
1672 return FALSE;
1675 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1676 return FALSE;
1677 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1680 /*************************************************************************
1681 * PathFileExistsA [SHLWAPI.@]
1683 * Determine if a file exists.
1685 * PARAMS
1686 * lpszPath [I] Path to check
1688 * RETURNS
1689 * TRUE If the file exists and is readable
1690 * FALSE Otherwise
1692 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1694 UINT iPrevErrMode;
1695 DWORD dwAttr;
1697 TRACE("(%s)\n",debugstr_a(lpszPath));
1699 if (!lpszPath)
1700 return FALSE;
1702 /* Prevent a dialog box if path is on a disk that has been ejected. */
1703 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1704 dwAttr = GetFileAttributesA(lpszPath);
1705 SetErrorMode(iPrevErrMode);
1706 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1709 /*************************************************************************
1710 * PathFileExistsW [SHLWAPI.@]
1712 * See PathFileExistsA.
1714 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1716 UINT iPrevErrMode;
1717 DWORD dwAttr;
1719 TRACE("(%s)\n",debugstr_w(lpszPath));
1721 if (!lpszPath)
1722 return FALSE;
1724 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1725 dwAttr = GetFileAttributesW(lpszPath);
1726 SetErrorMode(iPrevErrMode);
1727 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1730 /*************************************************************************
1731 * PathFileExistsAndAttributesA [SHLWAPI.445]
1733 * Determine if a file exists.
1735 * PARAMS
1736 * lpszPath [I] Path to check
1737 * dwAttr [O] attributes of file
1739 * RETURNS
1740 * TRUE If the file exists and is readable
1741 * FALSE Otherwise
1743 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1745 UINT iPrevErrMode;
1746 DWORD dwVal = 0;
1748 TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1750 if (dwAttr)
1751 *dwAttr = INVALID_FILE_ATTRIBUTES;
1753 if (!lpszPath)
1754 return FALSE;
1756 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1757 dwVal = GetFileAttributesA(lpszPath);
1758 SetErrorMode(iPrevErrMode);
1759 if (dwAttr)
1760 *dwAttr = dwVal;
1761 return (dwVal != INVALID_FILE_ATTRIBUTES);
1764 /*************************************************************************
1765 * PathFileExistsAndAttributesW [SHLWAPI.446]
1767 * See PathFileExistsA.
1769 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1771 UINT iPrevErrMode;
1772 DWORD dwVal;
1774 TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1776 if (!lpszPath)
1777 return FALSE;
1779 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1780 dwVal = GetFileAttributesW(lpszPath);
1781 SetErrorMode(iPrevErrMode);
1782 if (dwAttr)
1783 *dwAttr = dwVal;
1784 return (dwVal != INVALID_FILE_ATTRIBUTES);
1787 /*************************************************************************
1788 * PathMatchSingleMaskA [internal]
1790 static BOOL WINAPI PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1792 while (*name && *mask && *mask!=';')
1794 if (*mask == '*')
1798 if (PathMatchSingleMaskA(name,mask+1))
1799 return TRUE; /* try substrings */
1800 } while (*name++);
1801 return FALSE;
1804 if (toupper(*mask) != toupper(*name) && *mask != '?')
1805 return FALSE;
1807 name = CharNextA(name);
1808 mask = CharNextA(mask);
1811 if (!*name)
1813 while (*mask == '*')
1814 mask++;
1815 if (!*mask || *mask == ';')
1816 return TRUE;
1818 return FALSE;
1821 /*************************************************************************
1822 * PathMatchSingleMaskW [internal]
1824 static BOOL WINAPI PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1826 while (*name && *mask && *mask != ';')
1828 if (*mask == '*')
1832 if (PathMatchSingleMaskW(name,mask+1))
1833 return TRUE; /* try substrings */
1834 } while (*name++);
1835 return FALSE;
1838 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1839 return FALSE;
1841 name++;
1842 mask++;
1844 if (!*name)
1846 while (*mask == '*')
1847 mask++;
1848 if (!*mask || *mask == ';')
1849 return TRUE;
1851 return FALSE;
1854 /*************************************************************************
1855 * PathMatchSpecA [SHLWAPI.@]
1857 * Determine if a path matches one or more search masks.
1859 * PARAMS
1860 * lpszPath [I] Path to check
1861 * lpszMask [I] Search mask(s)
1863 * RETURNS
1864 * TRUE If lpszPath is valid and is matched
1865 * FALSE Otherwise
1867 * NOTES
1868 * Multiple search masks may be given if they are separated by ";". The
1869 * pattern "*.*" is treated specially in that it matches all paths (for
1870 * backwards compatibility with DOS).
1872 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1874 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1876 if (!lstrcmpA(lpszMask, "*.*"))
1877 return TRUE; /* Matches every path */
1879 while (*lpszMask)
1881 while (*lpszMask == ' ')
1882 lpszMask++; /* Eat leading spaces */
1884 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1885 return TRUE; /* Matches the current mask */
1887 while (*lpszMask && *lpszMask != ';')
1888 lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1890 if (*lpszMask == ';')
1891 lpszMask++;
1893 return FALSE;
1896 /*************************************************************************
1897 * PathMatchSpecW [SHLWAPI.@]
1899 * See PathMatchSpecA.
1901 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1903 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1905 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1907 if (!lstrcmpW(lpszMask, szStarDotStar))
1908 return TRUE; /* Matches every path */
1910 while (*lpszMask)
1912 while (*lpszMask == ' ')
1913 lpszMask++; /* Eat leading spaces */
1915 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1916 return TRUE; /* Matches the current path */
1918 while (*lpszMask && *lpszMask != ';')
1919 lpszMask++; /* masks separated by ';' */
1921 if (*lpszMask == ';')
1922 lpszMask++;
1924 return FALSE;
1927 /*************************************************************************
1928 * PathIsSameRootA [SHLWAPI.@]
1930 * Determine if two paths share the same root.
1932 * PARAMS
1933 * lpszPath1 [I] Source path
1934 * lpszPath2 [I] Path to compare with
1936 * RETURNS
1937 * TRUE If both paths are valid and share the same root.
1938 * FALSE If either path is invalid or the paths do not share the same root.
1940 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1942 LPCSTR lpszStart;
1943 int dwLen;
1945 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1947 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1948 return FALSE;
1950 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1951 if (lpszStart - lpszPath1 > dwLen)
1952 return FALSE; /* Paths not common up to length of the root */
1953 return TRUE;
1956 /*************************************************************************
1957 * PathIsSameRootW [SHLWAPI.@]
1959 * See PathIsSameRootA.
1961 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1963 LPCWSTR lpszStart;
1964 int dwLen;
1966 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1968 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1969 return FALSE;
1971 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1972 if (lpszStart - lpszPath1 > dwLen)
1973 return FALSE; /* Paths not common up to length of the root */
1974 return TRUE;
1977 /*************************************************************************
1978 * PathIsContentTypeA [SHLWAPI.@]
1980 * Determine if a file is of a given registered content type.
1982 * PARAMS
1983 * lpszPath [I] File to check
1984 * lpszContentType [I] Content type to check for
1986 * RETURNS
1987 * TRUE If lpszPath is a given registered content type,
1988 * FALSE Otherwise.
1990 * NOTES
1991 * This function looks up the registered content type for lpszPath. If
1992 * a content type is registered, it is compared (case insensitively) to
1993 * lpszContentType. Only if this matches does the function succeed.
1995 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1997 LPCSTR szExt;
1998 DWORD dwDummy;
1999 char szBuff[MAX_PATH];
2001 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2003 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2004 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2005 REG_NONE, szBuff, &dwDummy) &&
2006 !strcasecmp(lpszContentType, szBuff))
2008 return TRUE;
2010 return FALSE;
2013 /*************************************************************************
2014 * PathIsContentTypeW [SHLWAPI.@]
2016 * See PathIsContentTypeA.
2018 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2020 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2021 LPCWSTR szExt;
2022 DWORD dwDummy;
2023 WCHAR szBuff[MAX_PATH];
2025 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2027 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2028 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2029 REG_NONE, szBuff, &dwDummy) &&
2030 !strcmpiW(lpszContentType, szBuff))
2032 return TRUE;
2034 return FALSE;
2037 /*************************************************************************
2038 * PathIsFileSpecA [SHLWAPI.@]
2040 * Determine if a path is a file specification.
2042 * PARAMS
2043 * lpszPath [I] Path to chack
2045 * RETURNS
2046 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
2047 * FALSE Otherwise.
2049 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2051 TRACE("(%s)\n", debugstr_a(lpszPath));
2053 if (!lpszPath)
2054 return FALSE;
2056 while (*lpszPath)
2058 if (*lpszPath == '\\' || *lpszPath == ':')
2059 return FALSE;
2060 lpszPath = CharNextA(lpszPath);
2062 return TRUE;
2065 /*************************************************************************
2066 * PathIsFileSpecW [SHLWAPI.@]
2068 * See PathIsFileSpecA.
2070 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2072 TRACE("(%s)\n", debugstr_w(lpszPath));
2074 if (!lpszPath)
2075 return FALSE;
2077 while (*lpszPath)
2079 if (*lpszPath == '\\' || *lpszPath == ':')
2080 return FALSE;
2081 lpszPath++;
2083 return TRUE;
2086 /*************************************************************************
2087 * PathIsPrefixA [SHLWAPI.@]
2089 * Determine if a path is a prefix of another.
2091 * PARAMS
2092 * lpszPrefix [I] Prefix
2093 * lpszPath [I] Path to check
2095 * RETURNS
2096 * TRUE If lpszPath has lpszPrefix as its prefix,
2097 * FALSE If either path is NULL or lpszPrefix is not a prefix
2099 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2101 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2103 if (lpszPrefix && lpszPath &&
2104 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2105 return TRUE;
2106 return FALSE;
2109 /*************************************************************************
2110 * PathIsPrefixW [SHLWAPI.@]
2112 * See PathIsPrefixA.
2114 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2116 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2118 if (lpszPrefix && lpszPath &&
2119 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2120 return TRUE;
2121 return FALSE;
2124 /*************************************************************************
2125 * PathIsSystemFolderA [SHLWAPI.@]
2127 * Determine if a path or file attributes are a system folder.
2129 * PARAMS
2130 * lpszPath [I] Path to check.
2131 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2133 * RETURNS
2134 * TRUE If lpszPath or dwAttrib are a system folder.
2135 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2137 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2139 TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2141 if (lpszPath && *lpszPath)
2142 dwAttrib = GetFileAttributesA(lpszPath);
2144 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2145 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2146 return FALSE;
2147 return TRUE;
2150 /*************************************************************************
2151 * PathIsSystemFolderW [SHLWAPI.@]
2153 * See PathIsSystemFolderA.
2155 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2157 TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2159 if (lpszPath && *lpszPath)
2160 dwAttrib = GetFileAttributesW(lpszPath);
2162 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2163 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2164 return FALSE;
2165 return TRUE;
2168 /*************************************************************************
2169 * PathIsUNCA [SHLWAPI.@]
2171 * Determine if a path is in UNC format.
2173 * PARAMS
2174 * lpszPath [I] Path to check
2176 * RETURNS
2177 * TRUE: The path is UNC.
2178 * FALSE: The path is not UNC or is NULL.
2180 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2182 TRACE("(%s)\n",debugstr_a(lpszPath));
2184 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2185 return TRUE;
2186 return FALSE;
2189 /*************************************************************************
2190 * PathIsUNCW [SHLWAPI.@]
2192 * See PathIsUNCA.
2194 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2196 TRACE("(%s)\n",debugstr_w(lpszPath));
2198 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2199 return TRUE;
2200 return FALSE;
2203 /*************************************************************************
2204 * PathIsUNCServerA [SHLWAPI.@]
2206 * Determine if a path is a UNC server name ("\\SHARENAME").
2208 * PARAMS
2209 * lpszPath [I] Path to check.
2211 * RETURNS
2212 * TRUE If lpszPath is a valid UNC server name.
2213 * FALSE Otherwise.
2215 * NOTES
2216 * This routine is bug compatible with Win32: Server names with a
2217 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2218 * Fixing this bug may break other shlwapi functions!
2220 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2222 TRACE("(%s)\n", debugstr_a(lpszPath));
2224 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2226 while (*lpszPath)
2228 if (*lpszPath == '\\')
2229 return FALSE;
2230 lpszPath = CharNextA(lpszPath);
2232 return TRUE;
2234 return FALSE;
2237 /*************************************************************************
2238 * PathIsUNCServerW [SHLWAPI.@]
2240 * See PathIsUNCServerA.
2242 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2244 TRACE("(%s)\n", debugstr_w(lpszPath));
2246 if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2248 return !strchrW( lpszPath + 2, '\\' );
2250 return FALSE;
2253 /*************************************************************************
2254 * PathIsUNCServerShareA [SHLWAPI.@]
2256 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2258 * PARAMS
2259 * lpszPath [I] Path to check.
2261 * RETURNS
2262 * TRUE If lpszPath is a valid UNC server share.
2263 * FALSE Otherwise.
2265 * NOTES
2266 * This routine is bug compatible with Win32: Server shares with a
2267 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2268 * Fixing this bug may break other shlwapi functions!
2270 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2272 TRACE("(%s)\n", debugstr_a(lpszPath));
2274 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2276 BOOL bSeenSlash = FALSE;
2277 while (*lpszPath)
2279 if (*lpszPath == '\\')
2281 if (bSeenSlash)
2282 return FALSE;
2283 bSeenSlash = TRUE;
2285 lpszPath = CharNextA(lpszPath);
2287 return bSeenSlash;
2289 return FALSE;
2292 /*************************************************************************
2293 * PathIsUNCServerShareW [SHLWAPI.@]
2295 * See PathIsUNCServerShareA.
2297 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2299 TRACE("(%s)\n", debugstr_w(lpszPath));
2301 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2303 BOOL bSeenSlash = FALSE;
2304 while (*lpszPath)
2306 if (*lpszPath == '\\')
2308 if (bSeenSlash)
2309 return FALSE;
2310 bSeenSlash = TRUE;
2312 lpszPath++;
2314 return bSeenSlash;
2316 return FALSE;
2319 /*************************************************************************
2320 * PathCanonicalizeA [SHLWAPI.@]
2322 * Convert a path to its canonical form.
2324 * PARAMS
2325 * lpszBuf [O] Output path
2326 * lpszPath [I] Path to cnonicalize
2328 * RETURNS
2329 * Success: TRUE. lpszBuf contains the output path,
2330 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2332 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2334 BOOL bRet = FALSE;
2336 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2338 if (lpszBuf)
2339 *lpszBuf = '\0';
2341 if (!lpszBuf || !lpszPath)
2342 SetLastError(ERROR_INVALID_PARAMETER);
2343 else
2345 WCHAR szPath[MAX_PATH];
2346 WCHAR szBuff[MAX_PATH];
2347 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2348 bRet = PathCanonicalizeW(szBuff, szPath);
2349 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2351 return bRet;
2355 /*************************************************************************
2356 * PathCanonicalizeW [SHLWAPI.@]
2358 * See PathCanonicalizeA.
2360 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2362 LPWSTR lpszDst = lpszBuf;
2363 LPCWSTR lpszSrc = lpszPath;
2365 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2367 if (lpszBuf)
2368 *lpszDst = '\0';
2370 if (!lpszBuf || !lpszPath)
2372 SetLastError(ERROR_INVALID_PARAMETER);
2373 return FALSE;
2376 if (!*lpszPath)
2378 *lpszBuf++ = '\\';
2379 *lpszBuf = '\0';
2380 return TRUE;
2383 /* Copy path root */
2384 if (*lpszSrc == '\\')
2386 *lpszDst++ = *lpszSrc++;
2388 else if (*lpszSrc && lpszSrc[1] == ':')
2390 /* X:\ */
2391 *lpszDst++ = *lpszSrc++;
2392 *lpszDst++ = *lpszSrc++;
2393 if (*lpszSrc == '\\')
2394 *lpszDst++ = *lpszSrc++;
2397 /* Canonicalize the rest of the path */
2398 while (*lpszSrc)
2400 if (*lpszSrc == '.')
2402 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2404 lpszSrc += 2; /* Skip .\ */
2406 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2408 /* \.. backs up a directory, over the root if it has no \ following X:.
2409 * .. is ignored if it would remove a UNC server name or inital \\
2411 if (lpszDst != lpszBuf)
2413 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2414 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2415 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2417 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2419 lpszDst -= 2;
2420 while (lpszDst > lpszBuf && *lpszDst != '\\')
2421 lpszDst--;
2422 if (*lpszDst == '\\')
2423 lpszDst++; /* Reset to last '\' */
2424 else
2425 lpszDst = lpszBuf; /* Start path again from new root */
2427 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2428 lpszDst -= 2;
2430 while (lpszDst > lpszBuf && *lpszDst != '\\')
2431 lpszDst--;
2432 if (lpszDst == lpszBuf)
2434 *lpszDst++ = '\\';
2435 lpszSrc++;
2438 lpszSrc += 2; /* Skip .. in src path */
2440 else
2441 *lpszDst++ = *lpszSrc++;
2443 else
2444 *lpszDst++ = *lpszSrc++;
2446 /* Append \ to naked drive specs */
2447 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2448 *lpszDst++ = '\\';
2449 *lpszDst++ = '\0';
2450 return TRUE;
2453 /*************************************************************************
2454 * PathFindNextComponentA [SHLWAPI.@]
2456 * Find the next component in a path.
2458 * PARAMS
2459 * lpszPath [I] Path to find next component in
2461 * RETURNS
2462 * Success: A pointer to the next component, or the end of the string.
2463 * Failure: NULL, If lpszPath is invalid
2465 * NOTES
2466 * A 'component' is either a backslash character (\) or UNC marker (\\).
2467 * Because of this, relative paths (e.g "c:foo") are regarded as having
2468 * only one component.
2470 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2472 LPSTR lpszSlash;
2474 TRACE("(%s)\n", debugstr_a(lpszPath));
2476 if(!lpszPath || !*lpszPath)
2477 return NULL;
2479 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2481 if (lpszSlash[1] == '\\')
2482 lpszSlash++;
2483 return lpszSlash + 1;
2485 return (LPSTR)lpszPath + strlen(lpszPath);
2488 /*************************************************************************
2489 * PathFindNextComponentW [SHLWAPI.@]
2491 * See PathFindNextComponentA.
2493 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2495 LPWSTR lpszSlash;
2497 TRACE("(%s)\n", debugstr_w(lpszPath));
2499 if(!lpszPath || !*lpszPath)
2500 return NULL;
2502 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2504 if (lpszSlash[1] == '\\')
2505 lpszSlash++;
2506 return lpszSlash + 1;
2508 return (LPWSTR)lpszPath + strlenW(lpszPath);
2511 /*************************************************************************
2512 * PathAddExtensionA [SHLWAPI.@]
2514 * Add a file extension to a path
2516 * PARAMS
2517 * lpszPath [I/O] Path to add extension to
2518 * lpszExtension [I] Extension to add to lpszPath
2520 * RETURNS
2521 * TRUE If the path was modified,
2522 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2523 * extension already, or the new path length is too big.
2525 * FIXME
2526 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2527 * does not do this, so the behaviour was removed.
2529 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2531 size_t dwLen;
2533 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2535 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2536 return FALSE;
2538 dwLen = strlen(lpszPath);
2540 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2541 return FALSE;
2543 strcpy(lpszPath + dwLen, lpszExtension);
2544 return TRUE;
2547 /*************************************************************************
2548 * PathAddExtensionW [SHLWAPI.@]
2550 * See PathAddExtensionA.
2552 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2554 size_t dwLen;
2556 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2558 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2559 return FALSE;
2561 dwLen = strlenW(lpszPath);
2563 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2564 return FALSE;
2566 strcpyW(lpszPath + dwLen, lpszExtension);
2567 return TRUE;
2570 /*************************************************************************
2571 * PathMakePrettyA [SHLWAPI.@]
2573 * Convert an uppercase DOS filename into lowercase.
2575 * PARAMS
2576 * lpszPath [I/O] Path to convert.
2578 * RETURNS
2579 * TRUE If the path was an uppercase DOS path and was converted,
2580 * FALSE Otherwise.
2582 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2584 LPSTR pszIter = lpszPath;
2586 TRACE("(%s)\n", debugstr_a(lpszPath));
2588 if (!pszIter)
2589 return FALSE;
2591 if (*pszIter)
2595 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2596 return FALSE; /* Not DOS path */
2597 pszIter++;
2598 } while (*pszIter);
2599 pszIter = lpszPath + 1;
2600 while (*pszIter)
2602 *pszIter = tolower(*pszIter);
2603 pszIter++;
2606 return TRUE;
2609 /*************************************************************************
2610 * PathMakePrettyW [SHLWAPI.@]
2612 * See PathMakePrettyA.
2614 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2616 LPWSTR pszIter = lpszPath;
2618 TRACE("(%s)\n", debugstr_w(lpszPath));
2620 if (!pszIter)
2621 return FALSE;
2623 if (*pszIter)
2627 if (islowerW(*pszIter))
2628 return FALSE; /* Not DOS path */
2629 pszIter++;
2630 } while (*pszIter);
2631 pszIter = lpszPath + 1;
2632 while (*pszIter)
2634 *pszIter = tolowerW(*pszIter);
2635 pszIter++;
2638 return TRUE;
2641 /*************************************************************************
2642 * PathCommonPrefixA [SHLWAPI.@]
2644 * Determine the length of the common prefix between two paths.
2646 * PARAMS
2647 * lpszFile1 [I] First path for comparison
2648 * lpszFile2 [I] Second path for comparison
2649 * achPath [O] Destination for common prefix string
2651 * RETURNS
2652 * The length of the common prefix. This is 0 if there is no common
2653 * prefix between the paths or if any parameters are invalid. If the prefix
2654 * is non-zero and achPath is not NULL, achPath is filled with the common
2655 * part of the prefix and NUL terminated.
2657 * NOTES
2658 * A common prefix of 2 is always returned as 3. It is thus possible for
2659 * the length returned to be invalid (i.e. Longer than one or both of the
2660 * strings given as parameters). This Win32 behaviour has been implemented
2661 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2662 * To work around this when using this function, always check that the byte
2663 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2665 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2667 size_t iLen = 0;
2668 LPCSTR lpszIter1 = lpszFile1;
2669 LPCSTR lpszIter2 = lpszFile2;
2671 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2673 if (achPath)
2674 *achPath = '\0';
2676 if (!lpszFile1 || !lpszFile2)
2677 return 0;
2679 /* Handle roots first */
2680 if (PathIsUNCA(lpszFile1))
2682 if (!PathIsUNCA(lpszFile2))
2683 return 0;
2684 lpszIter1 += 2;
2685 lpszIter2 += 2;
2687 else if (PathIsUNCA(lpszFile2))
2688 return 0; /* Know already lpszFile1 is not UNC */
2692 /* Update len */
2693 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2694 (!*lpszIter2 || *lpszIter2 == '\\'))
2695 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2697 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2698 break; /* Strings differ at this point */
2700 lpszIter1++;
2701 lpszIter2++;
2702 } while (1);
2704 if (iLen == 2)
2705 iLen++; /* Feature/Bug compatible with Win32 */
2707 if (iLen && achPath)
2709 memcpy(achPath,lpszFile1,iLen);
2710 achPath[iLen] = '\0';
2712 return iLen;
2715 /*************************************************************************
2716 * PathCommonPrefixW [SHLWAPI.@]
2718 * See PathCommonPrefixA.
2720 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2722 size_t iLen = 0;
2723 LPCWSTR lpszIter1 = lpszFile1;
2724 LPCWSTR lpszIter2 = lpszFile2;
2726 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2728 if (achPath)
2729 *achPath = '\0';
2731 if (!lpszFile1 || !lpszFile2)
2732 return 0;
2734 /* Handle roots first */
2735 if (PathIsUNCW(lpszFile1))
2737 if (!PathIsUNCW(lpszFile2))
2738 return 0;
2739 lpszIter1 += 2;
2740 lpszIter2 += 2;
2742 else if (PathIsUNCW(lpszFile2))
2743 return 0; /* Know already lpszFile1 is not UNC */
2747 /* Update len */
2748 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2749 (!*lpszIter2 || *lpszIter2 == '\\'))
2750 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2752 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2753 break; /* Strings differ at this point */
2755 lpszIter1++;
2756 lpszIter2++;
2757 } while (1);
2759 if (iLen == 2)
2760 iLen++; /* Feature/Bug compatible with Win32 */
2762 if (iLen && achPath)
2764 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2765 achPath[iLen] = '\0';
2767 return iLen;
2770 /*************************************************************************
2771 * PathCompactPathA [SHLWAPI.@]
2773 * Make a path fit into a given width when printed to a DC.
2775 * PARAMS
2776 * hDc [I] Destination DC
2777 * lpszPath [I/O] Path to be printed to hDc
2778 * dx [I] Desired width
2780 * RETURNS
2781 * TRUE If the path was modified/went well.
2782 * FALSE Otherwise.
2784 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2786 BOOL bRet = FALSE;
2788 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2790 if (lpszPath)
2792 WCHAR szPath[MAX_PATH];
2793 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2794 bRet = PathCompactPathW(hDC, szPath, dx);
2795 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2797 return bRet;
2800 /*************************************************************************
2801 * PathCompactPathW [SHLWAPI.@]
2803 * See PathCompactPathA.
2805 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2807 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2808 BOOL bRet = TRUE;
2809 HDC hdc = 0;
2810 WCHAR buff[MAX_PATH];
2811 SIZE size;
2812 DWORD dwLen;
2814 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2816 if (!lpszPath)
2817 return FALSE;
2819 if (!hDC)
2820 hdc = hDC = GetDC(0);
2822 /* Get the length of the whole path */
2823 dwLen = strlenW(lpszPath);
2824 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2826 if ((UINT)size.cx > dx)
2828 /* Path too big, must reduce it */
2829 LPWSTR sFile;
2830 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2832 sFile = PathFindFileNameW(lpszPath);
2833 if (sFile != lpszPath) sFile--;
2835 /* Get the size of ellipses */
2836 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2837 dwEllipsesLen = size.cx;
2838 /* Get the size of the file name */
2839 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2840 dwPathLen = size.cx;
2842 if (sFile != lpszPath)
2844 LPWSTR sPath = sFile;
2845 BOOL bEllipses = FALSE;
2847 /* The path includes a file name. Include as much of the path prior to
2848 * the file name as possible, allowing for the ellipses, e.g:
2849 * c:\some very long path\filename ==> c:\some v...\filename
2851 lstrcpynW(buff, sFile, MAX_PATH);
2855 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2857 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2858 dwTotalLen += size.cx;
2859 if (dwTotalLen <= dx)
2860 break;
2861 sPath--;
2862 if (!bEllipses)
2864 bEllipses = TRUE;
2865 sPath -= 2;
2867 } while (sPath > lpszPath);
2869 if (sPath > lpszPath)
2871 if (bEllipses)
2873 strcpyW(sPath, szEllipses);
2874 strcpyW(sPath+3, buff);
2876 bRet = TRUE;
2877 goto end;
2879 strcpyW(lpszPath, szEllipses);
2880 strcpyW(lpszPath+3, buff);
2881 bRet = FALSE;
2882 goto end;
2885 /* Trim the path by adding ellipses to the end, e.g:
2886 * A very long file name.txt ==> A very...
2888 dwLen = strlenW(lpszPath);
2890 if (dwLen > MAX_PATH - 3)
2891 dwLen = MAX_PATH - 3;
2892 lstrcpynW(buff, sFile, dwLen);
2894 do {
2895 dwLen--;
2896 GetTextExtentPointW(hDC, buff, dwLen, &size);
2897 } while (dwLen && size.cx + dwEllipsesLen > dx);
2899 if (!dwLen)
2901 DWORD dwWritten = 0;
2903 dwEllipsesLen /= 3; /* Size of a single '.' */
2905 /* Write as much of the Ellipses string as possible */
2906 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2908 *lpszPath++ = '.';
2909 dwWritten += dwEllipsesLen;
2910 dwLen++;
2912 *lpszPath = '\0';
2913 bRet = FALSE;
2915 else
2917 strcpyW(buff + dwLen, szEllipses);
2918 strcpyW(lpszPath, buff);
2922 end:
2923 if (hdc)
2924 ReleaseDC(0, hdc);
2926 return bRet;
2929 /*************************************************************************
2930 * PathGetCharTypeA [SHLWAPI.@]
2932 * Categorise a character from a file path.
2934 * PARAMS
2935 * ch [I] Character to get the type of
2937 * RETURNS
2938 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2940 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2942 return PathGetCharTypeW(ch);
2945 /*************************************************************************
2946 * PathGetCharTypeW [SHLWAPI.@]
2948 * See PathGetCharTypeA.
2950 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2952 UINT flags = 0;
2954 TRACE("(%d)\n", ch);
2956 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2957 ch == '"' || ch == '|' || ch == '/')
2958 flags = GCT_INVALID; /* Invalid */
2959 else if (ch == '*' || ch=='?')
2960 flags = GCT_WILD; /* Wildchars */
2961 else if ((ch == '\\') || (ch == ':'))
2962 return GCT_SEPARATOR; /* Path separators */
2963 else
2965 if (ch < 126)
2967 if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2968 ch == '.' || ch == '@' || ch == '^' ||
2969 ch == '\'' || ch == 130 || ch == '`')
2970 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2972 else
2973 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2974 flags |= GCT_LFNCHAR; /* Valid for long file names */
2976 return flags;
2979 /*************************************************************************
2980 * SHLWAPI_UseSystemForSystemFolders
2982 * Internal helper for PathMakeSystemFolderW.
2984 static BOOL WINAPI SHLWAPI_UseSystemForSystemFolders(void)
2986 static BOOL bCheckedReg = FALSE;
2987 static BOOL bUseSystemForSystemFolders = FALSE;
2989 if (!bCheckedReg)
2991 bCheckedReg = TRUE;
2993 /* Key tells Win what file attributes to use on system folders */
2994 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2995 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2996 "UseSystemForSystemFolders", 0, 0, 0))
2997 bUseSystemForSystemFolders = TRUE;
2999 return bUseSystemForSystemFolders;
3002 /*************************************************************************
3003 * PathMakeSystemFolderA [SHLWAPI.@]
3005 * Set system folder attribute for a path.
3007 * PARAMS
3008 * lpszPath [I] The path to turn into a system folder
3010 * RETURNS
3011 * TRUE If the path was changed to/already was a system folder
3012 * FALSE If the path is invalid or SetFileAttributesA() fails
3014 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3016 BOOL bRet = FALSE;
3018 TRACE("(%s)\n", debugstr_a(lpszPath));
3020 if (lpszPath && *lpszPath)
3022 WCHAR szPath[MAX_PATH];
3023 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3024 bRet = PathMakeSystemFolderW(szPath);
3026 return bRet;
3029 /*************************************************************************
3030 * PathMakeSystemFolderW [SHLWAPI.@]
3032 * See PathMakeSystemFolderA.
3034 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3036 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3037 WCHAR buff[MAX_PATH];
3039 TRACE("(%s)\n", debugstr_w(lpszPath));
3041 if (!lpszPath || !*lpszPath)
3042 return FALSE;
3044 /* If the directory is already a system directory, don't do anything */
3045 GetSystemDirectoryW(buff, MAX_PATH);
3046 if (!strcmpW(buff, lpszPath))
3047 return TRUE;
3049 GetWindowsDirectoryW(buff, MAX_PATH);
3050 if (!strcmpW(buff, lpszPath))
3051 return TRUE;
3053 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3054 if (SHLWAPI_UseSystemForSystemFolders())
3055 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3057 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3058 return FALSE;
3060 /* Change file attributes to system attributes */
3061 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3062 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3065 /*************************************************************************
3066 * PathRenameExtensionA [SHLWAPI.@]
3068 * Swap the file extension in a path with another extension.
3070 * PARAMS
3071 * lpszPath [I/O] Path to swap the extension in
3072 * lpszExt [I] The new extension
3074 * RETURNS
3075 * TRUE if lpszPath was modified,
3076 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3078 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3080 LPSTR lpszExtension;
3082 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3084 lpszExtension = PathFindExtensionA(lpszPath);
3086 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3087 return FALSE;
3089 strcpy(lpszExtension, lpszExt);
3090 return TRUE;
3093 /*************************************************************************
3094 * PathRenameExtensionW [SHLWAPI.@]
3096 * See PathRenameExtensionA.
3098 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3100 LPWSTR lpszExtension;
3102 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3104 lpszExtension = PathFindExtensionW(lpszPath);
3106 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3107 return FALSE;
3109 strcpyW(lpszExtension, lpszExt);
3110 return TRUE;
3113 /*************************************************************************
3114 * PathSearchAndQualifyA [SHLWAPI.@]
3116 * Determine if a given path is correct and fully qualified.
3118 * PARAMS
3119 * lpszPath [I] Path to check
3120 * lpszBuf [O] Output for correct path
3121 * cchBuf [I] Size of lpszBuf
3123 * RETURNS
3124 * Unknown.
3126 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3128 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3130 if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3131 return TRUE;
3132 return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3135 /*************************************************************************
3136 * PathSearchAndQualifyW [SHLWAPI.@]
3138 * See PathSearchAndQualifyA.
3140 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3142 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3144 if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3145 return TRUE;
3146 return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3149 /*************************************************************************
3150 * PathSkipRootA [SHLWAPI.@]
3152 * Return the portion of a path following the drive letter or mount point.
3154 * PARAMS
3155 * lpszPath [I] The path to skip on
3157 * RETURNS
3158 * Success: A pointer to the next character after the root.
3159 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3161 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3163 TRACE("(%s)\n", debugstr_a(lpszPath));
3165 if (!lpszPath || !*lpszPath)
3166 return NULL;
3168 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3170 /* Network share: skip share server and mount point */
3171 lpszPath += 2;
3172 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3173 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3174 lpszPath++;
3175 return (LPSTR)lpszPath;
3178 if (IsDBCSLeadByte(*lpszPath))
3179 return NULL;
3181 /* Check x:\ */
3182 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3183 return (LPSTR)lpszPath + 3;
3184 return NULL;
3187 /*************************************************************************
3188 * PathSkipRootW [SHLWAPI.@]
3190 * See PathSkipRootA.
3192 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3194 TRACE("(%s)\n", debugstr_w(lpszPath));
3196 if (!lpszPath || !*lpszPath)
3197 return NULL;
3199 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3201 /* Network share: skip share server and mount point */
3202 lpszPath += 2;
3203 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3204 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3205 lpszPath++;
3206 return (LPWSTR)lpszPath;
3209 /* Check x:\ */
3210 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3211 return (LPWSTR)lpszPath + 3;
3212 return NULL;
3215 /*************************************************************************
3216 * PathCreateFromUrlA [SHLWAPI.@]
3218 * See PathCreateFromUrlW
3220 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3221 LPDWORD pcchPath, DWORD dwReserved)
3223 WCHAR bufW[MAX_PATH];
3224 WCHAR *pathW = bufW;
3225 UNICODE_STRING urlW;
3226 HRESULT ret;
3227 DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3229 if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3230 return E_INVALIDARG;
3231 if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3232 pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3233 ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3235 if(ret == S_OK) {
3236 RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3237 if(*pcchPath > lenA) {
3238 RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3239 pszPath[lenA] = 0;
3240 *pcchPath = lenA;
3241 } else {
3242 *pcchPath = lenA + 1;
3243 ret = E_POINTER;
3246 if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3247 RtlFreeUnicodeString(&urlW);
3248 return ret;
3251 /*************************************************************************
3252 * PathCreateFromUrlW [SHLWAPI.@]
3254 * Create a path from a URL
3256 * PARAMS
3257 * lpszUrl [I] URL to convert into a path
3258 * lpszPath [O] Output buffer for the resulting Path
3259 * pcchPath [I] Length of lpszPath
3260 * dwFlags [I] Flags controlling the conversion
3262 * RETURNS
3263 * Success: S_OK. lpszPath contains the URL in path format,
3264 * Failure: An HRESULT error code such as E_INVALIDARG.
3266 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3267 LPDWORD pcchPath, DWORD dwReserved)
3269 static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3270 HRESULT hr;
3271 DWORD nslashes = 0;
3272 WCHAR *ptr;
3274 TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3276 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3277 return E_INVALIDARG;
3280 if (strncmpW(pszUrl, file_colon, 5))
3281 return E_INVALIDARG;
3282 pszUrl += 5;
3284 while(*pszUrl == '/' || *pszUrl == '\\') {
3285 nslashes++;
3286 pszUrl++;
3289 if(isalphaW(*pszUrl) && (pszUrl[1] == ':' || pszUrl[1] == '|') && (pszUrl[2] == '/' || pszUrl[2] == '\\'))
3290 nslashes = 0;
3292 switch(nslashes) {
3293 case 2:
3294 pszUrl -= 2;
3295 break;
3296 case 0:
3297 break;
3298 default:
3299 pszUrl -= 1;
3300 break;
3303 hr = UrlUnescapeW((LPWSTR)pszUrl, pszPath, pcchPath, 0);
3304 if(hr != S_OK) return hr;
3306 for(ptr = pszPath; *ptr; ptr++)
3307 if(*ptr == '/') *ptr = '\\';
3309 while(*pszPath == '\\')
3310 pszPath++;
3312 if(isalphaW(*pszPath) && pszPath[1] == '|' && pszPath[2] == '\\') /* c|\ -> c:\ */
3313 pszPath[1] = ':';
3315 if(nslashes == 2 && (ptr = strchrW(pszPath, '\\'))) { /* \\host\c:\ -> \\hostc:\ */
3316 ptr++;
3317 if(isalphaW(*ptr) && (ptr[1] == ':' || ptr[1] == '|') && ptr[2] == '\\') {
3318 memmove(ptr - 1, ptr, (strlenW(ptr) + 1) * sizeof(WCHAR));
3319 (*pcchPath)--;
3323 TRACE("Returning %s\n",debugstr_w(pszPath));
3325 return hr;
3328 /*************************************************************************
3329 * PathRelativePathToA [SHLWAPI.@]
3331 * Create a relative path from one path to another.
3333 * PARAMS
3334 * lpszPath [O] Destination for relative path
3335 * lpszFrom [I] Source path
3336 * dwAttrFrom [I] File attribute of source path
3337 * lpszTo [I] Destination path
3338 * dwAttrTo [I] File attributes of destination path
3340 * RETURNS
3341 * TRUE If a relative path can be formed. lpszPath contains the new path
3342 * FALSE If the paths are not relavtive or any parameters are invalid
3344 * NOTES
3345 * lpszTo should be at least MAX_PATH in length.
3347 * Calling this function with relative paths for lpszFrom or lpszTo may
3348 * give erroneous results.
3350 * The Win32 version of this function contains a bug where the lpszTo string
3351 * may be referenced 1 byte beyond the end of the string. As a result random
3352 * garbage may be written to the output path, depending on what lies beyond
3353 * the last byte of the string. This bug occurs because of the behaviour of
3354 * PathCommonPrefix() (see notes for that function), and no workaround seems
3355 * possible with Win32.
3357 * This bug has been fixed here, so for example the relative path from "\\"
3358 * to "\\" is correctly determined as "." in this implementation.
3360 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3361 LPCSTR lpszTo, DWORD dwAttrTo)
3363 BOOL bRet = FALSE;
3365 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3366 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3368 if(lpszPath && lpszFrom && lpszTo)
3370 WCHAR szPath[MAX_PATH];
3371 WCHAR szFrom[MAX_PATH];
3372 WCHAR szTo[MAX_PATH];
3373 MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3374 MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3375 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3376 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3378 return bRet;
3381 /*************************************************************************
3382 * PathRelativePathToW [SHLWAPI.@]
3384 * See PathRelativePathToA.
3386 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3387 LPCWSTR lpszTo, DWORD dwAttrTo)
3389 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3390 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3391 WCHAR szFrom[MAX_PATH];
3392 WCHAR szTo[MAX_PATH];
3393 DWORD dwLen;
3395 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3396 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3398 if(!lpszPath || !lpszFrom || !lpszTo)
3399 return FALSE;
3401 *lpszPath = '\0';
3402 lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3403 lstrcpynW(szTo, lpszTo, MAX_PATH);
3405 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3406 PathRemoveFileSpecW(szFrom);
3407 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3408 PathRemoveFileSpecW(szTo);
3410 /* Paths can only be relative if they have a common root */
3411 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3412 return FALSE;
3414 /* Strip off lpszFrom components to the root, by adding "..\" */
3415 lpszFrom = szFrom + dwLen;
3416 if (!*lpszFrom)
3418 lpszPath[0] = '.';
3419 lpszPath[1] = '\0';
3421 if (*lpszFrom == '\\')
3422 lpszFrom++;
3424 while (*lpszFrom)
3426 lpszFrom = PathFindNextComponentW(lpszFrom);
3427 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3430 /* From the root add the components of lpszTo */
3431 lpszTo += dwLen;
3432 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3433 * this function.
3435 if (*lpszTo && lpszTo[-1])
3437 if (*lpszTo != '\\')
3438 lpszTo--;
3439 dwLen = strlenW(lpszPath);
3440 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3442 *lpszPath = '\0';
3443 return FALSE;
3445 strcpyW(lpszPath + dwLen, lpszTo);
3447 return TRUE;
3450 /*************************************************************************
3451 * PathUnmakeSystemFolderA [SHLWAPI.@]
3453 * Remove the system folder attributes from a path.
3455 * PARAMS
3456 * lpszPath [I] The path to remove attributes from
3458 * RETURNS
3459 * Success: TRUE.
3460 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3461 * SetFileAttributesA() fails.
3463 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3465 DWORD dwAttr;
3467 TRACE("(%s)\n", debugstr_a(lpszPath));
3469 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3470 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3471 return FALSE;
3473 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3474 return SetFileAttributesA(lpszPath, dwAttr);
3477 /*************************************************************************
3478 * PathUnmakeSystemFolderW [SHLWAPI.@]
3480 * See PathUnmakeSystemFolderA.
3482 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3484 DWORD dwAttr;
3486 TRACE("(%s)\n", debugstr_w(lpszPath));
3488 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3489 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3490 return FALSE;
3492 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3493 return SetFileAttributesW(lpszPath, dwAttr);
3497 /*************************************************************************
3498 * PathSetDlgItemPathA [SHLWAPI.@]
3500 * Set the text of a dialog item to a path, shrinking the path to fit
3501 * if it is too big for the item.
3503 * PARAMS
3504 * hDlg [I] Dialog handle
3505 * id [I] ID of item in the dialog
3506 * lpszPath [I] Path to set as the items text
3508 * RETURNS
3509 * Nothing.
3511 * NOTES
3512 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3513 * window text is erased).
3515 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3517 WCHAR szPath[MAX_PATH];
3519 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3521 if (lpszPath)
3522 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3523 else
3524 szPath[0] = '\0';
3525 PathSetDlgItemPathW(hDlg, id, szPath);
3528 /*************************************************************************
3529 * PathSetDlgItemPathW [SHLWAPI.@]
3531 * See PathSetDlgItemPathA.
3533 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3535 WCHAR path[MAX_PATH + 1];
3536 HWND hwItem;
3537 RECT rect;
3538 HDC hdc;
3539 HGDIOBJ hPrevObj;
3541 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3543 if (!(hwItem = GetDlgItem(hDlg, id)))
3544 return;
3546 if (lpszPath)
3547 lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3548 else
3549 path[0] = '\0';
3551 GetClientRect(hwItem, &rect);
3552 hdc = GetDC(hDlg);
3553 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3555 if (hPrevObj)
3557 PathCompactPathW(hdc, path, rect.right);
3558 SelectObject(hdc, hPrevObj);
3561 ReleaseDC(hDlg, hdc);
3562 SetWindowTextW(hwItem, path);
3565 /*************************************************************************
3566 * PathIsNetworkPathA [SHLWAPI.@]
3568 * Determine if the given path is a network path.
3570 * PARAMS
3571 * lpszPath [I] Path to check
3573 * RETURNS
3574 * TRUE If lpszPath is a UNC share or mapped network drive, or
3575 * FALSE If lpszPath is a local drive or cannot be determined
3577 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3579 int dwDriveNum;
3581 TRACE("(%s)\n",debugstr_a(lpszPath));
3583 if (!lpszPath)
3584 return FALSE;
3585 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3586 return TRUE;
3587 dwDriveNum = PathGetDriveNumberA(lpszPath);
3588 if (dwDriveNum == -1)
3589 return FALSE;
3590 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3591 return pIsNetDrive(dwDriveNum);
3594 /*************************************************************************
3595 * PathIsNetworkPathW [SHLWAPI.@]
3597 * See PathIsNetworkPathA.
3599 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3601 int dwDriveNum;
3603 TRACE("(%s)\n", debugstr_w(lpszPath));
3605 if (!lpszPath)
3606 return FALSE;
3607 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3608 return TRUE;
3609 dwDriveNum = PathGetDriveNumberW(lpszPath);
3610 if (dwDriveNum == -1)
3611 return FALSE;
3612 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3613 return pIsNetDrive(dwDriveNum);
3616 /*************************************************************************
3617 * PathIsLFNFileSpecA [SHLWAPI.@]
3619 * Determine if the given path is a long file name
3621 * PARAMS
3622 * lpszPath [I] Path to check
3624 * RETURNS
3625 * TRUE If path is a long file name,
3626 * FALSE If path is a valid DOS short file name
3628 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3630 DWORD dwNameLen = 0, dwExtLen = 0;
3632 TRACE("(%s)\n",debugstr_a(lpszPath));
3634 if (!lpszPath)
3635 return FALSE;
3637 while (*lpszPath)
3639 if (*lpszPath == ' ')
3640 return TRUE; /* DOS names cannot have spaces */
3641 if (*lpszPath == '.')
3643 if (dwExtLen)
3644 return TRUE; /* DOS names have only one dot */
3645 dwExtLen = 1;
3647 else if (dwExtLen)
3649 dwExtLen++;
3650 if (dwExtLen > 4)
3651 return TRUE; /* DOS extensions are <= 3 chars*/
3653 else
3655 dwNameLen++;
3656 if (dwNameLen > 8)
3657 return TRUE; /* DOS names are <= 8 chars */
3659 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3661 return FALSE; /* Valid DOS path */
3664 /*************************************************************************
3665 * PathIsLFNFileSpecW [SHLWAPI.@]
3667 * See PathIsLFNFileSpecA.
3669 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3671 DWORD dwNameLen = 0, dwExtLen = 0;
3673 TRACE("(%s)\n",debugstr_w(lpszPath));
3675 if (!lpszPath)
3676 return FALSE;
3678 while (*lpszPath)
3680 if (*lpszPath == ' ')
3681 return TRUE; /* DOS names cannot have spaces */
3682 if (*lpszPath == '.')
3684 if (dwExtLen)
3685 return TRUE; /* DOS names have only one dot */
3686 dwExtLen = 1;
3688 else if (dwExtLen)
3690 dwExtLen++;
3691 if (dwExtLen > 4)
3692 return TRUE; /* DOS extensions are <= 3 chars*/
3694 else
3696 dwNameLen++;
3697 if (dwNameLen > 8)
3698 return TRUE; /* DOS names are <= 8 chars */
3700 lpszPath++;
3702 return FALSE; /* Valid DOS path */
3705 /*************************************************************************
3706 * PathIsDirectoryEmptyA [SHLWAPI.@]
3708 * Determine if a given directory is empty.
3710 * PARAMS
3711 * lpszPath [I] Directory to check
3713 * RETURNS
3714 * TRUE If the directory exists and contains no files,
3715 * FALSE Otherwise
3717 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3719 BOOL bRet = FALSE;
3721 TRACE("(%s)\n",debugstr_a(lpszPath));
3723 if (lpszPath)
3725 WCHAR szPath[MAX_PATH];
3726 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3727 bRet = PathIsDirectoryEmptyW(szPath);
3729 return bRet;
3732 /*************************************************************************
3733 * PathIsDirectoryEmptyW [SHLWAPI.@]
3735 * See PathIsDirectoryEmptyA.
3737 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3739 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3740 WCHAR szSearch[MAX_PATH];
3741 DWORD dwLen;
3742 HANDLE hfind;
3743 BOOL retVal = FALSE;
3744 WIN32_FIND_DATAW find_data;
3746 TRACE("(%s)\n",debugstr_w(lpszPath));
3748 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3749 return FALSE;
3751 lstrcpynW(szSearch, lpszPath, MAX_PATH);
3752 PathAddBackslashW(szSearch);
3753 dwLen = strlenW(szSearch);
3754 if (dwLen > MAX_PATH - 4)
3755 return FALSE;
3757 strcpyW(szSearch + dwLen, szAllFiles);
3758 hfind = FindFirstFileW(szSearch, &find_data);
3760 if (hfind != INVALID_HANDLE_VALUE &&
3761 find_data.cFileName[0] == '.' &&
3762 find_data.cFileName[1] == '.')
3764 /* The only directory entry should be the parent */
3765 if (!FindNextFileW(hfind, &find_data))
3766 retVal = TRUE;
3767 FindClose(hfind);
3769 return retVal;
3773 /*************************************************************************
3774 * PathFindSuffixArrayA [SHLWAPI.@]
3776 * Find a suffix string in an array of suffix strings
3778 * PARAMS
3779 * lpszSuffix [I] Suffix string to search for
3780 * lppszArray [I] Array of suffix strings to search
3781 * dwCount [I] Number of elements in lppszArray
3783 * RETURNS
3784 * Success: The index of the position of lpszSuffix in lppszArray
3785 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3787 * NOTES
3788 * The search is case sensitive.
3789 * The match is made against the end of the suffix string, so for example:
3790 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3792 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3794 size_t dwLen;
3795 int dwRet = 0;
3797 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3799 if (lpszSuffix && lppszArray && dwCount > 0)
3801 dwLen = strlen(lpszSuffix);
3803 while (dwRet < dwCount)
3805 size_t dwCompareLen = strlen(*lppszArray);
3806 if (dwCompareLen < dwLen)
3808 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3809 return *lppszArray; /* Found */
3811 dwRet++;
3812 lppszArray++;
3815 return NULL;
3818 /*************************************************************************
3819 * PathFindSuffixArrayW [SHLWAPI.@]
3821 * See PathFindSuffixArrayA.
3823 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3825 size_t dwLen;
3826 int dwRet = 0;
3828 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3830 if (lpszSuffix && lppszArray && dwCount > 0)
3832 dwLen = strlenW(lpszSuffix);
3834 while (dwRet < dwCount)
3836 size_t dwCompareLen = strlenW(*lppszArray);
3837 if (dwCompareLen < dwLen)
3839 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3840 return *lppszArray; /* Found */
3842 dwRet++;
3843 lppszArray++;
3846 return NULL;
3849 /*************************************************************************
3850 * PathUndecorateA [SHLWAPI.@]
3852 * Undecorate a file path
3854 * PARAMS
3855 * lpszPath [I/O] Path to remove any decoration from
3857 * RETURNS
3858 * Nothing
3860 * NOTES
3861 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3863 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3865 TRACE("(%s)\n",debugstr_a(lpszPath));
3867 if (lpszPath)
3869 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3870 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3872 LPSTR lpszSkip = lpszExt - 2;
3873 if (*lpszSkip == '[')
3874 lpszSkip++; /* [] (no number) */
3875 else
3876 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3877 lpszSkip--;
3878 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3880 /* remove the [n] */
3881 lpszSkip--;
3882 while (*lpszExt)
3883 *lpszSkip++ = *lpszExt++;
3884 *lpszSkip = '\0';
3890 /*************************************************************************
3891 * PathUndecorateW [SHLWAPI.@]
3893 * See PathUndecorateA.
3895 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3897 TRACE("(%s)\n",debugstr_w(lpszPath));
3899 if (lpszPath)
3901 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3902 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3904 LPWSTR lpszSkip = lpszExt - 2;
3905 if (*lpszSkip == '[')
3906 lpszSkip++; /* [] (no number) */
3907 else
3908 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3909 lpszSkip--;
3910 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3912 /* remove the [n] */
3913 lpszSkip--;
3914 while (*lpszExt)
3915 *lpszSkip++ = *lpszExt++;
3916 *lpszSkip = '\0';
3922 /*************************************************************************
3923 * PathUnExpandEnvStringsA [SHLWAPI.@]
3925 * Substitute folder names in a path with their corresponding environment
3926 * strings.
3928 * PARAMS
3929 * pszPath [I] Buffer containing the path to unexpand.
3930 * pszBuf [O] Buffer to receive the unexpanded path.
3931 * cchBuf [I] Size of pszBuf in characters.
3933 * RETURNS
3934 * Success: TRUE
3935 * Failure: FALSE
3937 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR pszPath, LPSTR pszBuf, UINT cchBuf)
3939 FIXME("(%s,%s,0x%08x)\n", debugstr_a(pszPath), debugstr_a(pszBuf), cchBuf);
3940 return FALSE;
3943 /*************************************************************************
3944 * PathUnExpandEnvStringsW [SHLWAPI.@]
3946 * Unicode version of PathUnExpandEnvStringsA.
3948 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR pszPath, LPWSTR pszBuf, UINT cchBuf)
3950 FIXME("(%s,%s,0x%08x)\n", debugstr_w(pszPath), debugstr_w(pszBuf), cchBuf);
3951 return FALSE;
3954 /*************************************************************************
3955 * @ [SHLWAPI.440]
3957 * Find localised or default web content in "%WINDOWS%\web\".
3959 * PARAMS
3960 * lpszFile [I] File name containing content to look for
3961 * lpszPath [O] Buffer to contain the full path to the file
3962 * dwPathLen [I] Length of lpszPath
3964 * RETURNS
3965 * Success: S_OK. lpszPath contains the full path to the content.
3966 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
3968 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
3970 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
3971 HRESULT hRet;
3973 TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
3975 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
3976 szPath[0] = '\0';
3977 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
3978 WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
3979 return hRet;
3982 /*************************************************************************
3983 * @ [SHLWAPI.441]
3985 * Unicode version of SHGetWebFolderFilePathA.
3987 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
3989 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
3990 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
3991 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
3992 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
3993 DWORD dwLen, dwFileLen;
3994 LANGID lidSystem, lidUser;
3996 TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
3998 /* Get base directory for web content */
3999 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4000 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4001 dwLen--;
4003 dwFileLen = strlenW(lpszFile);
4005 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4006 return E_FAIL; /* lpszPath too short */
4008 strcpyW(lpszPath+dwLen, szWeb);
4009 dwLen += szWebLen;
4010 dwPathLen = dwPathLen - dwLen; /* Remaining space */
4012 lidSystem = GetSystemDefaultUILanguage();
4013 lidUser = GetUserDefaultUILanguage();
4015 if (lidSystem != lidUser)
4017 if (dwFileLen + szWebMuiLen < dwPathLen)
4019 /* Use localised content in the users UI language if present */
4020 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4021 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4022 if (PathFileExistsW(lpszPath))
4023 return S_OK;
4027 /* Fall back to OS default installed content */
4028 strcpyW(lpszPath + dwLen, lpszFile);
4029 if (PathFileExistsW(lpszPath))
4030 return S_OK;
4031 return E_FAIL;
4034 #define PATH_CHAR_CLASS_LETTER 0x00000001
4035 #define PATH_CHAR_CLASS_ASTERIX 0x00000002
4036 #define PATH_CHAR_CLASS_DOT 0x00000004
4037 #define PATH_CHAR_CLASS_BACKSLASH 0x00000008
4038 #define PATH_CHAR_CLASS_COLON 0x00000010
4039 #define PATH_CHAR_CLASS_SEMICOLON 0x00000020
4040 #define PATH_CHAR_CLASS_COMMA 0x00000040
4041 #define PATH_CHAR_CLASS_SPACE 0x00000080
4042 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4043 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4045 #define PATH_CHAR_CLASS_INVALID 0x00000000
4046 #define PATH_CHAR_CLASS_ANY 0xffffffff
4048 static const DWORD SHELL_charclass[] =
4050 /* 0x00 */ PATH_CHAR_CLASS_INVALID, /* 0x01 */ PATH_CHAR_CLASS_INVALID,
4051 /* 0x02 */ PATH_CHAR_CLASS_INVALID, /* 0x03 */ PATH_CHAR_CLASS_INVALID,
4052 /* 0x04 */ PATH_CHAR_CLASS_INVALID, /* 0x05 */ PATH_CHAR_CLASS_INVALID,
4053 /* 0x06 */ PATH_CHAR_CLASS_INVALID, /* 0x07 */ PATH_CHAR_CLASS_INVALID,
4054 /* 0x08 */ PATH_CHAR_CLASS_INVALID, /* 0x09 */ PATH_CHAR_CLASS_INVALID,
4055 /* 0x0a */ PATH_CHAR_CLASS_INVALID, /* 0x0b */ PATH_CHAR_CLASS_INVALID,
4056 /* 0x0c */ PATH_CHAR_CLASS_INVALID, /* 0x0d */ PATH_CHAR_CLASS_INVALID,
4057 /* 0x0e */ PATH_CHAR_CLASS_INVALID, /* 0x0f */ PATH_CHAR_CLASS_INVALID,
4058 /* 0x10 */ PATH_CHAR_CLASS_INVALID, /* 0x11 */ PATH_CHAR_CLASS_INVALID,
4059 /* 0x12 */ PATH_CHAR_CLASS_INVALID, /* 0x13 */ PATH_CHAR_CLASS_INVALID,
4060 /* 0x14 */ PATH_CHAR_CLASS_INVALID, /* 0x15 */ PATH_CHAR_CLASS_INVALID,
4061 /* 0x16 */ PATH_CHAR_CLASS_INVALID, /* 0x17 */ PATH_CHAR_CLASS_INVALID,
4062 /* 0x18 */ PATH_CHAR_CLASS_INVALID, /* 0x19 */ PATH_CHAR_CLASS_INVALID,
4063 /* 0x1a */ PATH_CHAR_CLASS_INVALID, /* 0x1b */ PATH_CHAR_CLASS_INVALID,
4064 /* 0x1c */ PATH_CHAR_CLASS_INVALID, /* 0x1d */ PATH_CHAR_CLASS_INVALID,
4065 /* 0x1e */ PATH_CHAR_CLASS_INVALID, /* 0x1f */ PATH_CHAR_CLASS_INVALID,
4066 /* ' ' */ PATH_CHAR_CLASS_SPACE, /* '!' */ PATH_CHAR_CLASS_OTHER_VALID,
4067 /* '"' */ PATH_CHAR_CLASS_DOUBLEQUOTE, /* '#' */ PATH_CHAR_CLASS_OTHER_VALID,
4068 /* '$' */ PATH_CHAR_CLASS_OTHER_VALID, /* '%' */ PATH_CHAR_CLASS_OTHER_VALID,
4069 /* '&' */ PATH_CHAR_CLASS_OTHER_VALID, /* '\'' */ PATH_CHAR_CLASS_OTHER_VALID,
4070 /* '(' */ PATH_CHAR_CLASS_OTHER_VALID, /* ')' */ PATH_CHAR_CLASS_OTHER_VALID,
4071 /* '*' */ PATH_CHAR_CLASS_ASTERIX, /* '+' */ PATH_CHAR_CLASS_OTHER_VALID,
4072 /* ',' */ PATH_CHAR_CLASS_COMMA, /* '-' */ PATH_CHAR_CLASS_OTHER_VALID,
4073 /* '.' */ PATH_CHAR_CLASS_DOT, /* '/' */ PATH_CHAR_CLASS_INVALID,
4074 /* '0' */ PATH_CHAR_CLASS_OTHER_VALID, /* '1' */ PATH_CHAR_CLASS_OTHER_VALID,
4075 /* '2' */ PATH_CHAR_CLASS_OTHER_VALID, /* '3' */ PATH_CHAR_CLASS_OTHER_VALID,
4076 /* '4' */ PATH_CHAR_CLASS_OTHER_VALID, /* '5' */ PATH_CHAR_CLASS_OTHER_VALID,
4077 /* '6' */ PATH_CHAR_CLASS_OTHER_VALID, /* '7' */ PATH_CHAR_CLASS_OTHER_VALID,
4078 /* '8' */ PATH_CHAR_CLASS_OTHER_VALID, /* '9' */ PATH_CHAR_CLASS_OTHER_VALID,
4079 /* ':' */ PATH_CHAR_CLASS_COLON, /* ';' */ PATH_CHAR_CLASS_SEMICOLON,
4080 /* '<' */ PATH_CHAR_CLASS_INVALID, /* '=' */ PATH_CHAR_CLASS_OTHER_VALID,
4081 /* '>' */ PATH_CHAR_CLASS_INVALID, /* '?' */ PATH_CHAR_CLASS_LETTER,
4082 /* '@' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'A' */ PATH_CHAR_CLASS_ANY,
4083 /* 'B' */ PATH_CHAR_CLASS_ANY, /* 'C' */ PATH_CHAR_CLASS_ANY,
4084 /* 'D' */ PATH_CHAR_CLASS_ANY, /* 'E' */ PATH_CHAR_CLASS_ANY,
4085 /* 'F' */ PATH_CHAR_CLASS_ANY, /* 'G' */ PATH_CHAR_CLASS_ANY,
4086 /* 'H' */ PATH_CHAR_CLASS_ANY, /* 'I' */ PATH_CHAR_CLASS_ANY,
4087 /* 'J' */ PATH_CHAR_CLASS_ANY, /* 'K' */ PATH_CHAR_CLASS_ANY,
4088 /* 'L' */ PATH_CHAR_CLASS_ANY, /* 'M' */ PATH_CHAR_CLASS_ANY,
4089 /* 'N' */ PATH_CHAR_CLASS_ANY, /* 'O' */ PATH_CHAR_CLASS_ANY,
4090 /* 'P' */ PATH_CHAR_CLASS_ANY, /* 'Q' */ PATH_CHAR_CLASS_ANY,
4091 /* 'R' */ PATH_CHAR_CLASS_ANY, /* 'S' */ PATH_CHAR_CLASS_ANY,
4092 /* 'T' */ PATH_CHAR_CLASS_ANY, /* 'U' */ PATH_CHAR_CLASS_ANY,
4093 /* 'V' */ PATH_CHAR_CLASS_ANY, /* 'W' */ PATH_CHAR_CLASS_ANY,
4094 /* 'X' */ PATH_CHAR_CLASS_ANY, /* 'Y' */ PATH_CHAR_CLASS_ANY,
4095 /* 'Z' */ PATH_CHAR_CLASS_ANY, /* '[' */ PATH_CHAR_CLASS_OTHER_VALID,
4096 /* '\\' */ PATH_CHAR_CLASS_BACKSLASH, /* ']' */ PATH_CHAR_CLASS_OTHER_VALID,
4097 /* '^' */ PATH_CHAR_CLASS_OTHER_VALID, /* '_' */ PATH_CHAR_CLASS_OTHER_VALID,
4098 /* '`' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'a' */ PATH_CHAR_CLASS_ANY,
4099 /* 'b' */ PATH_CHAR_CLASS_ANY, /* 'c' */ PATH_CHAR_CLASS_ANY,
4100 /* 'd' */ PATH_CHAR_CLASS_ANY, /* 'e' */ PATH_CHAR_CLASS_ANY,
4101 /* 'f' */ PATH_CHAR_CLASS_ANY, /* 'g' */ PATH_CHAR_CLASS_ANY,
4102 /* 'h' */ PATH_CHAR_CLASS_ANY, /* 'i' */ PATH_CHAR_CLASS_ANY,
4103 /* 'j' */ PATH_CHAR_CLASS_ANY, /* 'k' */ PATH_CHAR_CLASS_ANY,
4104 /* 'l' */ PATH_CHAR_CLASS_ANY, /* 'm' */ PATH_CHAR_CLASS_ANY,
4105 /* 'n' */ PATH_CHAR_CLASS_ANY, /* 'o' */ PATH_CHAR_CLASS_ANY,
4106 /* 'p' */ PATH_CHAR_CLASS_ANY, /* 'q' */ PATH_CHAR_CLASS_ANY,
4107 /* 'r' */ PATH_CHAR_CLASS_ANY, /* 's' */ PATH_CHAR_CLASS_ANY,
4108 /* 't' */ PATH_CHAR_CLASS_ANY, /* 'u' */ PATH_CHAR_CLASS_ANY,
4109 /* 'v' */ PATH_CHAR_CLASS_ANY, /* 'w' */ PATH_CHAR_CLASS_ANY,
4110 /* 'x' */ PATH_CHAR_CLASS_ANY, /* 'y' */ PATH_CHAR_CLASS_ANY,
4111 /* 'z' */ PATH_CHAR_CLASS_ANY, /* '{' */ PATH_CHAR_CLASS_OTHER_VALID,
4112 /* '|' */ PATH_CHAR_CLASS_INVALID, /* '}' */ PATH_CHAR_CLASS_OTHER_VALID,
4113 /* '~' */ PATH_CHAR_CLASS_OTHER_VALID
4116 /*************************************************************************
4117 * @ [SHLWAPI.455]
4119 * Check if an ASCII char is of a certain class
4121 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4123 if ((unsigned)c > 0x7e)
4124 return class & PATH_CHAR_CLASS_OTHER_VALID;
4126 return class & SHELL_charclass[(unsigned)c];
4129 /*************************************************************************
4130 * @ [SHLWAPI.456]
4132 * Check if a Unicode char is of a certain class
4134 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4136 if (c > 0x7e)
4137 return class & PATH_CHAR_CLASS_OTHER_VALID;
4139 return class & SHELL_charclass[c];