d3dcompiler: Fix HeapAlloc/HeapFree for type members in the reflection parser.
[wine/multimedia.git] / dlls / shlwapi / path.c
blob5e95d5c2d781540d76a0806c9b4a40ae35313052
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;
238 LPSTR prev = lpszPath;
240 TRACE("(%s)\n",debugstr_a(lpszPath));
242 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
243 return NULL;
245 if (iLen)
247 do {
248 lpszPath = CharNextA(prev);
249 if (*lpszPath)
250 prev = lpszPath;
251 } while (*lpszPath);
252 if (*prev != '\\')
254 *lpszPath++ = '\\';
255 *lpszPath = '\0';
258 return lpszPath;
261 /*************************************************************************
262 * PathAddBackslashW [SHLWAPI.@]
264 * See PathAddBackslashA.
266 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
268 size_t iLen;
270 TRACE("(%s)\n",debugstr_w(lpszPath));
272 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
273 return NULL;
275 if (iLen)
277 lpszPath += iLen;
278 if (lpszPath[-1] != '\\')
280 *lpszPath++ = '\\';
281 *lpszPath = '\0';
284 return lpszPath;
287 /*************************************************************************
288 * PathBuildRootA [SHLWAPI.@]
290 * Create a root drive string (e.g. "A:\") from a drive number.
292 * PARAMS
293 * lpszPath [O] Destination for the drive string
295 * RETURNS
296 * lpszPath
298 * NOTES
299 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
301 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
303 TRACE("(%p,%d)\n", lpszPath, drive);
305 if (lpszPath && drive >= 0 && drive < 26)
307 lpszPath[0] = 'A' + drive;
308 lpszPath[1] = ':';
309 lpszPath[2] = '\\';
310 lpszPath[3] = '\0';
312 return lpszPath;
315 /*************************************************************************
316 * PathBuildRootW [SHLWAPI.@]
318 * See PathBuildRootA.
320 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
322 TRACE("(%p,%d)\n", lpszPath, drive);
324 if (lpszPath && drive >= 0 && drive < 26)
326 lpszPath[0] = 'A' + drive;
327 lpszPath[1] = ':';
328 lpszPath[2] = '\\';
329 lpszPath[3] = '\0';
331 return lpszPath;
334 /*************************************************************************
335 * PathFindFileNameA [SHLWAPI.@]
337 * Locate the start of the file name in a path
339 * PARAMS
340 * lpszPath [I] Path to search
342 * RETURNS
343 * A pointer to the first character of the file name
345 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
347 LPCSTR lastSlash = lpszPath;
349 TRACE("(%s)\n",debugstr_a(lpszPath));
351 while (lpszPath && *lpszPath)
353 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
354 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
355 lastSlash = lpszPath + 1;
356 lpszPath = CharNextA(lpszPath);
358 return (LPSTR)lastSlash;
361 /*************************************************************************
362 * PathFindFileNameW [SHLWAPI.@]
364 * See PathFindFileNameA.
366 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
368 LPCWSTR lastSlash = lpszPath;
370 TRACE("(%s)\n",debugstr_w(lpszPath));
372 while (lpszPath && *lpszPath)
374 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
375 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
376 lastSlash = lpszPath + 1;
377 lpszPath++;
379 return (LPWSTR)lastSlash;
382 /*************************************************************************
383 * PathFindExtensionA [SHLWAPI.@]
385 * Locate the start of the file extension in a path
387 * PARAMS
388 * lpszPath [I] The path to search
390 * RETURNS
391 * A pointer to the first character of the extension, the end of
392 * the string if the path has no extension, or NULL If lpszPath is NULL
394 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
396 LPCSTR lastpoint = NULL;
398 TRACE("(%s)\n", debugstr_a(lpszPath));
400 if (lpszPath)
402 while (*lpszPath)
404 if (*lpszPath == '\\' || *lpszPath==' ')
405 lastpoint = NULL;
406 else if (*lpszPath == '.')
407 lastpoint = lpszPath;
408 lpszPath = CharNextA(lpszPath);
411 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
414 /*************************************************************************
415 * PathFindExtensionW [SHLWAPI.@]
417 * See PathFindExtensionA.
419 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
421 LPCWSTR lastpoint = NULL;
423 TRACE("(%s)\n", debugstr_w(lpszPath));
425 if (lpszPath)
427 while (*lpszPath)
429 if (*lpszPath == '\\' || *lpszPath==' ')
430 lastpoint = NULL;
431 else if (*lpszPath == '.')
432 lastpoint = lpszPath;
433 lpszPath++;
436 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
439 /*************************************************************************
440 * PathGetArgsA [SHLWAPI.@]
442 * Find the next argument in a string delimited by spaces.
444 * PARAMS
445 * lpszPath [I] The string to search for arguments in
447 * RETURNS
448 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
450 * NOTES
451 * Spaces in quoted strings are ignored as delimiters.
453 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
455 BOOL bSeenQuote = FALSE;
457 TRACE("(%s)\n",debugstr_a(lpszPath));
459 if (lpszPath)
461 while (*lpszPath)
463 if ((*lpszPath==' ') && !bSeenQuote)
464 return (LPSTR)lpszPath + 1;
465 if (*lpszPath == '"')
466 bSeenQuote = !bSeenQuote;
467 lpszPath = CharNextA(lpszPath);
470 return (LPSTR)lpszPath;
473 /*************************************************************************
474 * PathGetArgsW [SHLWAPI.@]
476 * See PathGetArgsA.
478 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
480 BOOL bSeenQuote = FALSE;
482 TRACE("(%s)\n",debugstr_w(lpszPath));
484 if (lpszPath)
486 while (*lpszPath)
488 if ((*lpszPath==' ') && !bSeenQuote)
489 return (LPWSTR)lpszPath + 1;
490 if (*lpszPath == '"')
491 bSeenQuote = !bSeenQuote;
492 lpszPath++;
495 return (LPWSTR)lpszPath;
498 /*************************************************************************
499 * PathGetDriveNumberA [SHLWAPI.@]
501 * Return the drive number from a path
503 * PARAMS
504 * lpszPath [I] Path to get the drive number from
506 * RETURNS
507 * Success: The drive number corresponding to the drive in the path
508 * Failure: -1, if lpszPath contains no valid drive
510 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
512 TRACE ("(%s)\n",debugstr_a(lpszPath));
514 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
515 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
516 return tolower(*lpszPath) - 'a';
517 return -1;
520 /*************************************************************************
521 * PathGetDriveNumberW [SHLWAPI.@]
523 * See PathGetDriveNumberA.
525 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
527 TRACE ("(%s)\n",debugstr_w(lpszPath));
529 if (lpszPath)
531 WCHAR tl = tolowerW(lpszPath[0]);
532 if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
533 return tl - 'a';
535 return -1;
538 /*************************************************************************
539 * PathRemoveFileSpecA [SHLWAPI.@]
541 * Remove the file specification from a path.
543 * PARAMS
544 * lpszPath [I/O] Path to remove the file spec from
546 * RETURNS
547 * TRUE If the path was valid and modified
548 * FALSE Otherwise
550 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
552 LPSTR lpszFileSpec = lpszPath;
553 BOOL bModified = FALSE;
555 TRACE("(%s)\n",debugstr_a(lpszPath));
557 if(lpszPath)
559 /* Skip directory or UNC path */
560 if (*lpszPath == '\\')
561 lpszFileSpec = ++lpszPath;
562 if (*lpszPath == '\\')
563 lpszFileSpec = ++lpszPath;
565 while (*lpszPath)
567 if(*lpszPath == '\\')
568 lpszFileSpec = lpszPath; /* Skip dir */
569 else if(*lpszPath == ':')
571 lpszFileSpec = ++lpszPath; /* Skip drive */
572 if (*lpszPath == '\\')
573 lpszFileSpec++;
575 if (!(lpszPath = CharNextA(lpszPath)))
576 break;
579 if (*lpszFileSpec)
581 *lpszFileSpec = '\0';
582 bModified = TRUE;
585 return bModified;
588 /*************************************************************************
589 * PathRemoveFileSpecW [SHLWAPI.@]
591 * See PathRemoveFileSpecA.
593 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
595 LPWSTR lpszFileSpec = lpszPath;
596 BOOL bModified = FALSE;
598 TRACE("(%s)\n",debugstr_w(lpszPath));
600 if(lpszPath)
602 /* Skip directory or UNC path */
603 if (*lpszPath == '\\')
604 lpszFileSpec = ++lpszPath;
605 if (*lpszPath == '\\')
606 lpszFileSpec = ++lpszPath;
608 while (*lpszPath)
610 if(*lpszPath == '\\')
611 lpszFileSpec = lpszPath; /* Skip dir */
612 else if(*lpszPath == ':')
614 lpszFileSpec = ++lpszPath; /* Skip drive */
615 if (*lpszPath == '\\')
616 lpszFileSpec++;
618 lpszPath++;
621 if (*lpszFileSpec)
623 *lpszFileSpec = '\0';
624 bModified = TRUE;
627 return bModified;
630 /*************************************************************************
631 * PathStripPathA [SHLWAPI.@]
633 * Remove the initial path from the beginning of a filename
635 * PARAMS
636 * lpszPath [I/O] Path to remove the initial path from
638 * RETURNS
639 * Nothing.
641 void WINAPI PathStripPathA(LPSTR lpszPath)
643 TRACE("(%s)\n", debugstr_a(lpszPath));
645 if (lpszPath)
647 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
648 if(lpszFileName)
649 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
653 /*************************************************************************
654 * PathStripPathW [SHLWAPI.@]
656 * See PathStripPathA.
658 void WINAPI PathStripPathW(LPWSTR lpszPath)
660 LPWSTR lpszFileName;
662 TRACE("(%s)\n", debugstr_w(lpszPath));
663 lpszFileName = PathFindFileNameW(lpszPath);
664 if(lpszFileName)
665 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
668 /*************************************************************************
669 * PathStripToRootA [SHLWAPI.@]
671 * Reduce a path to its root.
673 * PARAMS
674 * lpszPath [I/O] the path to reduce
676 * RETURNS
677 * Success: TRUE if the stripped path is a root path
678 * Failure: FALSE if the path cannot be stripped or is NULL
680 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
682 TRACE("(%s)\n", debugstr_a(lpszPath));
684 if (!lpszPath)
685 return FALSE;
686 while(!PathIsRootA(lpszPath))
687 if (!PathRemoveFileSpecA(lpszPath))
688 return FALSE;
689 return TRUE;
692 /*************************************************************************
693 * PathStripToRootW [SHLWAPI.@]
695 * See PathStripToRootA.
697 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
699 TRACE("(%s)\n", debugstr_w(lpszPath));
701 if (!lpszPath)
702 return FALSE;
703 while(!PathIsRootW(lpszPath))
704 if (!PathRemoveFileSpecW(lpszPath))
705 return FALSE;
706 return TRUE;
709 /*************************************************************************
710 * PathRemoveArgsA [SHLWAPI.@]
712 * Strip space separated arguments from a path.
714 * PARAMS
715 * lpszPath [I/O] Path to remove arguments from
717 * RETURNS
718 * Nothing.
720 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
722 TRACE("(%s)\n",debugstr_a(lpszPath));
724 if(lpszPath)
726 LPSTR lpszArgs = PathGetArgsA(lpszPath);
727 if (*lpszArgs)
728 lpszArgs[-1] = '\0';
729 else
731 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
732 if(*lpszLastChar == ' ')
733 *lpszLastChar = '\0';
738 /*************************************************************************
739 * PathRemoveArgsW [SHLWAPI.@]
741 * See PathRemoveArgsA.
743 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
745 TRACE("(%s)\n",debugstr_w(lpszPath));
747 if(lpszPath)
749 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
750 if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
751 lpszArgs[-1] = '\0';
755 /*************************************************************************
756 * PathRemoveExtensionA [SHLWAPI.@]
758 * Remove the file extension from a path
760 * PARAMS
761 * lpszPath [I/O] Path to remove the extension from
763 * RETURNS
764 * Nothing.
766 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
768 TRACE("(%s)\n", debugstr_a(lpszPath));
770 if (lpszPath)
772 lpszPath = PathFindExtensionA(lpszPath);
773 *lpszPath = '\0';
777 /*************************************************************************
778 * PathRemoveExtensionW [SHLWAPI.@]
780 * See PathRemoveExtensionA.
782 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
784 TRACE("(%s)\n", debugstr_w(lpszPath));
786 if (lpszPath)
788 lpszPath = PathFindExtensionW(lpszPath);
789 *lpszPath = '\0';
793 /*************************************************************************
794 * PathRemoveBackslashA [SHLWAPI.@]
796 * Remove a trailing backslash from a path.
798 * PARAMS
799 * lpszPath [I/O] Path to remove backslash from
801 * RETURNS
802 * Success: A pointer to the end of the path
803 * Failure: NULL, if lpszPath is NULL
805 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
807 LPSTR szTemp = NULL;
809 TRACE("(%s)\n", debugstr_a(lpszPath));
811 if(lpszPath)
813 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
814 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
815 *szTemp = '\0';
817 return szTemp;
820 /*************************************************************************
821 * PathRemoveBackslashW [SHLWAPI.@]
823 * See PathRemoveBackslashA.
825 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
827 LPWSTR szTemp = NULL;
829 TRACE("(%s)\n", debugstr_w(lpszPath));
831 if(lpszPath)
833 szTemp = lpszPath + strlenW(lpszPath);
834 if (szTemp > lpszPath) szTemp--;
835 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
836 *szTemp = '\0';
838 return szTemp;
841 /*************************************************************************
842 * PathRemoveBlanksA [SHLWAPI.@]
844 * Remove Spaces from the start and end of a path.
846 * PARAMS
847 * lpszPath [I/O] Path to strip blanks from
849 * RETURNS
850 * Nothing.
852 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
854 TRACE("(%s)\n", debugstr_a(lpszPath));
856 if(lpszPath && *lpszPath)
858 LPSTR start = lpszPath;
860 while (*lpszPath == ' ')
861 lpszPath = CharNextA(lpszPath);
863 while(*lpszPath)
864 *start++ = *lpszPath++;
866 if (start != lpszPath)
867 while (start[-1] == ' ')
868 start--;
869 *start = '\0';
873 /*************************************************************************
874 * PathRemoveBlanksW [SHLWAPI.@]
876 * See PathRemoveBlanksA.
878 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
880 TRACE("(%s)\n", debugstr_w(lpszPath));
882 if(lpszPath && *lpszPath)
884 LPWSTR start = lpszPath;
886 while (*lpszPath == ' ')
887 lpszPath++;
889 while(*lpszPath)
890 *start++ = *lpszPath++;
892 if (start != lpszPath)
893 while (start[-1] == ' ')
894 start--;
895 *start = '\0';
899 /*************************************************************************
900 * PathQuoteSpacesA [SHLWAPI.@]
902 * Surround a path containing spaces in quotes.
904 * PARAMS
905 * lpszPath [I/O] Path to quote
907 * RETURNS
908 * Nothing.
910 * NOTES
911 * The path is not changed if it is invalid or has no spaces.
913 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
915 TRACE("(%s)\n", debugstr_a(lpszPath));
917 if(lpszPath && StrChrA(lpszPath,' '))
919 size_t iLen = strlen(lpszPath) + 1;
921 if (iLen + 2 < MAX_PATH)
923 memmove(lpszPath + 1, lpszPath, iLen);
924 lpszPath[0] = '"';
925 lpszPath[iLen] = '"';
926 lpszPath[iLen + 1] = '\0';
931 /*************************************************************************
932 * PathQuoteSpacesW [SHLWAPI.@]
934 * See PathQuoteSpacesA.
936 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
938 TRACE("(%s)\n", debugstr_w(lpszPath));
940 if(lpszPath && StrChrW(lpszPath,' '))
942 int iLen = strlenW(lpszPath) + 1;
944 if (iLen + 2 < MAX_PATH)
946 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
947 lpszPath[0] = '"';
948 lpszPath[iLen] = '"';
949 lpszPath[iLen + 1] = '\0';
954 /*************************************************************************
955 * PathUnquoteSpacesA [SHLWAPI.@]
957 * Remove quotes ("") from around a path, if present.
959 * PARAMS
960 * lpszPath [I/O] Path to strip quotes from
962 * RETURNS
963 * Nothing
965 * NOTES
966 * If the path contains a single quote only, an empty string will result.
967 * Otherwise quotes are only removed if they appear at the start and end
968 * of the path.
970 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
972 TRACE("(%s)\n", debugstr_a(lpszPath));
974 if (lpszPath && *lpszPath == '"')
976 DWORD dwLen = strlen(lpszPath) - 1;
978 if (lpszPath[dwLen] == '"')
980 lpszPath[dwLen] = '\0';
981 for (; *lpszPath; lpszPath++)
982 *lpszPath = lpszPath[1];
987 /*************************************************************************
988 * PathUnquoteSpacesW [SHLWAPI.@]
990 * See PathUnquoteSpacesA.
992 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
994 TRACE("(%s)\n", debugstr_w(lpszPath));
996 if (lpszPath && *lpszPath == '"')
998 DWORD dwLen = strlenW(lpszPath) - 1;
1000 if (lpszPath[dwLen] == '"')
1002 lpszPath[dwLen] = '\0';
1003 for (; *lpszPath; lpszPath++)
1004 *lpszPath = lpszPath[1];
1009 /*************************************************************************
1010 * PathParseIconLocationA [SHLWAPI.@]
1012 * Parse the location of an icon from a path.
1014 * PARAMS
1015 * lpszPath [I/O] The path to parse the icon location from.
1017 * RETURNS
1018 * Success: The number of the icon
1019 * Failure: 0 if the path does not contain an icon location or is NULL
1021 * NOTES
1022 * The path has surrounding quotes and spaces removed regardless
1023 * of whether the call succeeds or not.
1025 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1027 int iRet = 0;
1028 LPSTR lpszComma;
1030 TRACE("(%s)\n", debugstr_a(lpszPath));
1032 if (lpszPath)
1034 if ((lpszComma = strchr(lpszPath, ',')))
1036 *lpszComma++ = '\0';
1037 iRet = StrToIntA(lpszComma);
1039 PathUnquoteSpacesA(lpszPath);
1040 PathRemoveBlanksA(lpszPath);
1042 return iRet;
1045 /*************************************************************************
1046 * PathParseIconLocationW [SHLWAPI.@]
1048 * See PathParseIconLocationA.
1050 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1052 int iRet = 0;
1053 LPWSTR lpszComma;
1055 TRACE("(%s)\n", debugstr_w(lpszPath));
1057 if (lpszPath)
1059 if ((lpszComma = StrChrW(lpszPath, ',')))
1061 *lpszComma++ = '\0';
1062 iRet = StrToIntW(lpszComma);
1064 PathUnquoteSpacesW(lpszPath);
1065 PathRemoveBlanksW(lpszPath);
1067 return iRet;
1070 /*************************************************************************
1071 * @ [SHLWAPI.4]
1073 * Unicode version of PathFileExistsDefExtA.
1075 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1077 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
1078 { '.', 'c', 'o', 'm', 0},
1079 { '.', 'e', 'x', 'e', 0},
1080 { '.', 'b', 'a', 't', 0},
1081 { '.', 'l', 'n', 'k', 0},
1082 { '.', 'c', 'm', 'd', 0},
1083 { 0, 0, 0, 0, 0} };
1085 TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1087 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1088 return FALSE;
1090 if (dwWhich)
1092 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1093 if (!*szExt || dwWhich & 0x40)
1095 size_t iChoose = 0;
1096 int iLen = lstrlenW(lpszPath);
1097 if (iLen > (MAX_PATH - 5))
1098 return FALSE;
1099 while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1101 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1102 if (PathFileExistsW(lpszPath))
1103 return TRUE;
1104 iChoose++;
1105 dwWhich >>= 1;
1107 *(lpszPath + iLen) = (WCHAR)'\0';
1108 return FALSE;
1111 return PathFileExistsW(lpszPath);
1114 /*************************************************************************
1115 * @ [SHLWAPI.3]
1117 * Determine if a file exists locally and is of an executable type.
1119 * PARAMS
1120 * lpszPath [I/O] File to search for
1121 * dwWhich [I] Type of executable to search for
1123 * RETURNS
1124 * TRUE If the file was found. lpszPath contains the file name.
1125 * FALSE Otherwise.
1127 * NOTES
1128 * lpszPath is modified in place and must be at least MAX_PATH in length.
1129 * If the function returns FALSE, the path is modified to its original state.
1130 * If the given path contains an extension or dwWhich is 0, executable
1131 * extensions are not checked.
1133 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1134 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1135 * their own developers exclusive use. Monopoly, anyone?
1137 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1139 BOOL bRet = FALSE;
1141 TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1143 if (lpszPath)
1145 WCHAR szPath[MAX_PATH];
1146 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1147 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1148 if (bRet)
1149 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1151 return bRet;
1154 /*************************************************************************
1155 * SHLWAPI_PathFindInOtherDirs
1157 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1159 static BOOL SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1161 static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1162 static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1163 DWORD dwLenPATH;
1164 LPCWSTR lpszCurr;
1165 WCHAR *lpszPATH;
1166 WCHAR buff[MAX_PATH];
1168 TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1170 /* Try system directories */
1171 GetSystemDirectoryW(buff, MAX_PATH);
1172 if (!PathAppendW(buff, lpszFile))
1173 return FALSE;
1174 if (PathFileExistsDefExtW(buff, dwWhich))
1176 strcpyW(lpszFile, buff);
1177 return TRUE;
1179 GetWindowsDirectoryW(buff, MAX_PATH);
1180 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1181 return FALSE;
1182 if (PathFileExistsDefExtW(buff, dwWhich))
1184 strcpyW(lpszFile, buff);
1185 return TRUE;
1187 GetWindowsDirectoryW(buff, MAX_PATH);
1188 if (!PathAppendW(buff, lpszFile))
1189 return FALSE;
1190 if (PathFileExistsDefExtW(buff, dwWhich))
1192 strcpyW(lpszFile, buff);
1193 return TRUE;
1195 /* Try dirs listed in %PATH% */
1196 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1198 if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1199 return FALSE;
1201 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1202 lpszCurr = lpszPATH;
1203 while (lpszCurr)
1205 LPCWSTR lpszEnd = lpszCurr;
1206 LPWSTR pBuff = buff;
1208 while (*lpszEnd == ' ')
1209 lpszEnd++;
1210 while (*lpszEnd && *lpszEnd != ';')
1211 *pBuff++ = *lpszEnd++;
1212 *pBuff = '\0';
1214 if (*lpszEnd)
1215 lpszCurr = lpszEnd + 1;
1216 else
1217 lpszCurr = NULL; /* Last Path, terminate after this */
1219 if (!PathAppendW(buff, lpszFile))
1221 HeapFree(GetProcessHeap(), 0, lpszPATH);
1222 return FALSE;
1224 if (PathFileExistsDefExtW(buff, dwWhich))
1226 strcpyW(lpszFile, buff);
1227 HeapFree(GetProcessHeap(), 0, lpszPATH);
1228 return TRUE;
1231 HeapFree(GetProcessHeap(), 0, lpszPATH);
1232 return FALSE;
1235 /*************************************************************************
1236 * @ [SHLWAPI.5]
1238 * Search a range of paths for a specific type of executable.
1240 * PARAMS
1241 * lpszFile [I/O] File to search for
1242 * lppszOtherDirs [I] Other directories to look in
1243 * dwWhich [I] Type of executable to search for
1245 * RETURNS
1246 * Success: TRUE. The path to the executable is stored in lpszFile.
1247 * Failure: FALSE. The path to the executable is unchanged.
1249 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1251 WCHAR szFile[MAX_PATH];
1252 WCHAR buff[MAX_PATH];
1254 TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1256 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1257 return FALSE;
1259 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1261 /* Search provided directories first */
1262 if (lppszOtherDirs && *lppszOtherDirs)
1264 WCHAR szOther[MAX_PATH];
1265 LPCSTR *lpszOtherPath = lppszOtherDirs;
1267 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1269 MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1270 PathCombineW(buff, szOther, szFile);
1271 if (PathFileExistsDefExtW(buff, dwWhich))
1273 WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1274 return TRUE;
1276 lpszOtherPath++;
1279 /* Not found, try system and path dirs */
1280 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1282 WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1283 return TRUE;
1285 return FALSE;
1288 /*************************************************************************
1289 * @ [SHLWAPI.6]
1291 * Unicode version of PathFindOnPathExA.
1293 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1295 WCHAR buff[MAX_PATH];
1297 TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1299 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1300 return FALSE;
1302 /* Search provided directories first */
1303 if (lppszOtherDirs && *lppszOtherDirs)
1305 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1306 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1308 PathCombineW(buff, *lpszOtherPath, lpszFile);
1309 if (PathFileExistsDefExtW(buff, dwWhich))
1311 strcpyW(lpszFile, buff);
1312 return TRUE;
1314 lpszOtherPath++;
1317 /* Not found, try system and path dirs */
1318 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1321 /*************************************************************************
1322 * PathFindOnPathA [SHLWAPI.@]
1324 * Search a range of paths for an executable.
1326 * PARAMS
1327 * lpszFile [I/O] File to search for
1328 * lppszOtherDirs [I] Other directories to look in
1330 * RETURNS
1331 * Success: TRUE. The path to the executable is stored in lpszFile.
1332 * Failure: FALSE. The path to the executable is unchanged.
1334 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1336 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1337 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1340 /*************************************************************************
1341 * PathFindOnPathW [SHLWAPI.@]
1343 * See PathFindOnPathA.
1345 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1347 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1348 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1351 /*************************************************************************
1352 * PathCompactPathExA [SHLWAPI.@]
1354 * Compact a path into a given number of characters.
1356 * PARAMS
1357 * lpszDest [O] Destination for compacted path
1358 * lpszPath [I] Source path
1359 * cchMax [I] Maximum size of compacted path
1360 * dwFlags [I] Reserved
1362 * RETURNS
1363 * Success: TRUE. The compacted path is written to lpszDest.
1364 * Failure: FALSE. lpszPath is undefined.
1366 * NOTES
1367 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1369 * The Win32 version of this function contains a bug: When cchMax == 7,
1370 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1371 * implementation.
1373 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1374 * because Win32 will insert a "\" in lpszDest, even if one is
1375 * not present in the original path.
1377 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1378 UINT cchMax, DWORD dwFlags)
1380 BOOL bRet = FALSE;
1382 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1384 if (lpszPath && lpszDest)
1386 WCHAR szPath[MAX_PATH];
1387 WCHAR szDest[MAX_PATH];
1389 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1390 szDest[0] = '\0';
1391 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1392 WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1394 return bRet;
1397 /*************************************************************************
1398 * PathCompactPathExW [SHLWAPI.@]
1400 * See PathCompactPathExA.
1402 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1403 UINT cchMax, DWORD dwFlags)
1405 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1406 LPCWSTR lpszFile;
1407 DWORD dwLen, dwFileLen = 0;
1409 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1411 if (!lpszPath)
1412 return FALSE;
1414 if (!lpszDest)
1416 WARN("Invalid lpszDest would crash under Win32!\n");
1417 return FALSE;
1420 *lpszDest = '\0';
1422 if (cchMax < 2)
1423 return TRUE;
1425 dwLen = strlenW(lpszPath) + 1;
1427 if (dwLen < cchMax)
1429 /* Don't need to compact */
1430 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1431 return TRUE;
1434 /* Path must be compacted to fit into lpszDest */
1435 lpszFile = PathFindFileNameW(lpszPath);
1436 dwFileLen = lpszPath + dwLen - lpszFile;
1438 if (dwFileLen == dwLen)
1440 /* No root in psth */
1441 if (cchMax <= 4)
1443 while (--cchMax > 0) /* No room left for anything but ellipses */
1444 *lpszDest++ = '.';
1445 *lpszDest = '\0';
1446 return TRUE;
1448 /* Compact the file name with ellipses at the end */
1449 cchMax -= 4;
1450 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1451 strcpyW(lpszDest + cchMax, szEllipses);
1452 return TRUE;
1454 /* We have a root in the path */
1455 lpszFile--; /* Start compacted filename with the path separator */
1456 dwFileLen++;
1458 if (dwFileLen + 3 > cchMax)
1460 /* Compact the file name */
1461 if (cchMax <= 4)
1463 while (--cchMax > 0) /* No room left for anything but ellipses */
1464 *lpszDest++ = '.';
1465 *lpszDest = '\0';
1466 return TRUE;
1468 strcpyW(lpszDest, szEllipses);
1469 lpszDest += 3;
1470 cchMax -= 4;
1471 *lpszDest++ = *lpszFile++;
1472 if (cchMax <= 4)
1474 while (--cchMax > 0) /* No room left for anything but ellipses */
1475 *lpszDest++ = '.';
1476 *lpszDest = '\0';
1477 return TRUE;
1479 cchMax -= 4;
1480 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1481 strcpyW(lpszDest + cchMax, szEllipses);
1482 return TRUE;
1485 /* Only the root needs to be Compacted */
1486 dwLen = cchMax - dwFileLen - 3;
1487 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1488 strcpyW(lpszDest + dwLen, szEllipses);
1489 strcpyW(lpszDest + dwLen + 3, lpszFile);
1490 return TRUE;
1493 /*************************************************************************
1494 * PathIsRelativeA [SHLWAPI.@]
1496 * Determine if a path is a relative path.
1498 * PARAMS
1499 * lpszPath [I] Path to check
1501 * RETURNS
1502 * TRUE: The path is relative, or is invalid.
1503 * FALSE: The path is not relative.
1505 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1507 TRACE("(%s)\n",debugstr_a(lpszPath));
1509 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1510 return TRUE;
1511 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1512 return FALSE;
1513 return TRUE;
1516 /*************************************************************************
1517 * PathIsRelativeW [SHLWAPI.@]
1519 * See PathIsRelativeA.
1521 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1523 TRACE("(%s)\n",debugstr_w(lpszPath));
1525 if (!lpszPath || !*lpszPath)
1526 return TRUE;
1527 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1528 return FALSE;
1529 return TRUE;
1532 /*************************************************************************
1533 * PathIsRootA [SHLWAPI.@]
1535 * Determine if a path is a root path.
1537 * PARAMS
1538 * lpszPath [I] Path to check
1540 * RETURNS
1541 * TRUE If lpszPath is valid and a root path,
1542 * FALSE Otherwise
1544 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1546 TRACE("(%s)\n", debugstr_a(lpszPath));
1548 if (lpszPath && *lpszPath)
1550 if (*lpszPath == '\\')
1552 if (!lpszPath[1])
1553 return TRUE; /* \ */
1554 else if (lpszPath[1]=='\\')
1556 BOOL bSeenSlash = FALSE;
1557 lpszPath += 2;
1559 /* Check for UNC root path */
1560 while (*lpszPath)
1562 if (*lpszPath == '\\')
1564 if (bSeenSlash)
1565 return FALSE;
1566 bSeenSlash = TRUE;
1568 lpszPath = CharNextA(lpszPath);
1570 return TRUE;
1573 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1574 return TRUE; /* X:\ */
1576 return FALSE;
1579 /*************************************************************************
1580 * PathIsRootW [SHLWAPI.@]
1582 * See PathIsRootA.
1584 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1586 TRACE("(%s)\n", debugstr_w(lpszPath));
1588 if (lpszPath && *lpszPath)
1590 if (*lpszPath == '\\')
1592 if (!lpszPath[1])
1593 return TRUE; /* \ */
1594 else if (lpszPath[1]=='\\')
1596 BOOL bSeenSlash = FALSE;
1597 lpszPath += 2;
1599 /* Check for UNC root path */
1600 while (*lpszPath)
1602 if (*lpszPath == '\\')
1604 if (bSeenSlash)
1605 return FALSE;
1606 bSeenSlash = TRUE;
1608 lpszPath++;
1610 return TRUE;
1613 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1614 return TRUE; /* X:\ */
1616 return FALSE;
1619 /*************************************************************************
1620 * PathIsDirectoryA [SHLWAPI.@]
1622 * Determine if a path is a valid directory
1624 * PARAMS
1625 * lpszPath [I] Path to check.
1627 * RETURNS
1628 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1629 * FALSE if lpszPath is invalid or not a directory.
1631 * NOTES
1632 * Although this function is prototyped as returning a BOOL, it returns
1633 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1635 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1636 *| ...
1638 * will always fail.
1640 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1642 DWORD dwAttr;
1644 TRACE("(%s)\n", debugstr_a(lpszPath));
1646 if (!lpszPath || PathIsUNCServerA(lpszPath))
1647 return FALSE;
1649 if (PathIsUNCServerShareA(lpszPath))
1651 FIXME("UNC Server Share not yet supported - FAILING\n");
1652 return FALSE;
1655 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1656 return FALSE;
1657 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1660 /*************************************************************************
1661 * PathIsDirectoryW [SHLWAPI.@]
1663 * See PathIsDirectoryA.
1665 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1667 DWORD dwAttr;
1669 TRACE("(%s)\n", debugstr_w(lpszPath));
1671 if (!lpszPath || PathIsUNCServerW(lpszPath))
1672 return FALSE;
1674 if (PathIsUNCServerShareW(lpszPath))
1676 FIXME("UNC Server Share not yet supported - FAILING\n");
1677 return FALSE;
1680 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1681 return FALSE;
1682 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1685 /*************************************************************************
1686 * PathFileExistsA [SHLWAPI.@]
1688 * Determine if a file exists.
1690 * PARAMS
1691 * lpszPath [I] Path to check
1693 * RETURNS
1694 * TRUE If the file exists and is readable
1695 * FALSE Otherwise
1697 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1699 UINT iPrevErrMode;
1700 DWORD dwAttr;
1702 TRACE("(%s)\n",debugstr_a(lpszPath));
1704 if (!lpszPath)
1705 return FALSE;
1707 /* Prevent a dialog box if path is on a disk that has been ejected. */
1708 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1709 dwAttr = GetFileAttributesA(lpszPath);
1710 SetErrorMode(iPrevErrMode);
1711 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1714 /*************************************************************************
1715 * PathFileExistsW [SHLWAPI.@]
1717 * See PathFileExistsA.
1719 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1721 UINT iPrevErrMode;
1722 DWORD dwAttr;
1724 TRACE("(%s)\n",debugstr_w(lpszPath));
1726 if (!lpszPath)
1727 return FALSE;
1729 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1730 dwAttr = GetFileAttributesW(lpszPath);
1731 SetErrorMode(iPrevErrMode);
1732 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1735 /*************************************************************************
1736 * PathFileExistsAndAttributesA [SHLWAPI.445]
1738 * Determine if a file exists.
1740 * PARAMS
1741 * lpszPath [I] Path to check
1742 * dwAttr [O] attributes of file
1744 * RETURNS
1745 * TRUE If the file exists and is readable
1746 * FALSE Otherwise
1748 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1750 UINT iPrevErrMode;
1751 DWORD dwVal = 0;
1753 TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1755 if (dwAttr)
1756 *dwAttr = INVALID_FILE_ATTRIBUTES;
1758 if (!lpszPath)
1759 return FALSE;
1761 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1762 dwVal = GetFileAttributesA(lpszPath);
1763 SetErrorMode(iPrevErrMode);
1764 if (dwAttr)
1765 *dwAttr = dwVal;
1766 return (dwVal != INVALID_FILE_ATTRIBUTES);
1769 /*************************************************************************
1770 * PathFileExistsAndAttributesW [SHLWAPI.446]
1772 * See PathFileExistsA.
1774 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1776 UINT iPrevErrMode;
1777 DWORD dwVal;
1779 TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1781 if (!lpszPath)
1782 return FALSE;
1784 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1785 dwVal = GetFileAttributesW(lpszPath);
1786 SetErrorMode(iPrevErrMode);
1787 if (dwAttr)
1788 *dwAttr = dwVal;
1789 return (dwVal != INVALID_FILE_ATTRIBUTES);
1792 /*************************************************************************
1793 * PathMatchSingleMaskA [internal]
1795 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1797 while (*name && *mask && *mask!=';')
1799 if (*mask == '*')
1803 if (PathMatchSingleMaskA(name,mask+1))
1804 return TRUE; /* try substrings */
1805 } while (*name++);
1806 return FALSE;
1809 if (toupper(*mask) != toupper(*name) && *mask != '?')
1810 return FALSE;
1812 name = CharNextA(name);
1813 mask = CharNextA(mask);
1816 if (!*name)
1818 while (*mask == '*')
1819 mask++;
1820 if (!*mask || *mask == ';')
1821 return TRUE;
1823 return FALSE;
1826 /*************************************************************************
1827 * PathMatchSingleMaskW [internal]
1829 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1831 while (*name && *mask && *mask != ';')
1833 if (*mask == '*')
1837 if (PathMatchSingleMaskW(name,mask+1))
1838 return TRUE; /* try substrings */
1839 } while (*name++);
1840 return FALSE;
1843 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1844 return FALSE;
1846 name++;
1847 mask++;
1849 if (!*name)
1851 while (*mask == '*')
1852 mask++;
1853 if (!*mask || *mask == ';')
1854 return TRUE;
1856 return FALSE;
1859 /*************************************************************************
1860 * PathMatchSpecA [SHLWAPI.@]
1862 * Determine if a path matches one or more search masks.
1864 * PARAMS
1865 * lpszPath [I] Path to check
1866 * lpszMask [I] Search mask(s)
1868 * RETURNS
1869 * TRUE If lpszPath is valid and is matched
1870 * FALSE Otherwise
1872 * NOTES
1873 * Multiple search masks may be given if they are separated by ";". The
1874 * pattern "*.*" is treated specially in that it matches all paths (for
1875 * backwards compatibility with DOS).
1877 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1879 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1881 if (!lstrcmpA(lpszMask, "*.*"))
1882 return TRUE; /* Matches every path */
1884 while (*lpszMask)
1886 while (*lpszMask == ' ')
1887 lpszMask++; /* Eat leading spaces */
1889 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1890 return TRUE; /* Matches the current mask */
1892 while (*lpszMask && *lpszMask != ';')
1893 lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1895 if (*lpszMask == ';')
1896 lpszMask++;
1898 return FALSE;
1901 /*************************************************************************
1902 * PathMatchSpecW [SHLWAPI.@]
1904 * See PathMatchSpecA.
1906 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1908 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1910 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1912 if (!lstrcmpW(lpszMask, szStarDotStar))
1913 return TRUE; /* Matches every path */
1915 while (*lpszMask)
1917 while (*lpszMask == ' ')
1918 lpszMask++; /* Eat leading spaces */
1920 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1921 return TRUE; /* Matches the current path */
1923 while (*lpszMask && *lpszMask != ';')
1924 lpszMask++; /* masks separated by ';' */
1926 if (*lpszMask == ';')
1927 lpszMask++;
1929 return FALSE;
1932 /*************************************************************************
1933 * PathIsSameRootA [SHLWAPI.@]
1935 * Determine if two paths share the same root.
1937 * PARAMS
1938 * lpszPath1 [I] Source path
1939 * lpszPath2 [I] Path to compare with
1941 * RETURNS
1942 * TRUE If both paths are valid and share the same root.
1943 * FALSE If either path is invalid or the paths do not share the same root.
1945 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1947 LPCSTR lpszStart;
1948 int dwLen;
1950 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1952 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1953 return FALSE;
1955 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1956 if (lpszStart - lpszPath1 > dwLen)
1957 return FALSE; /* Paths not common up to length of the root */
1958 return TRUE;
1961 /*************************************************************************
1962 * PathIsSameRootW [SHLWAPI.@]
1964 * See PathIsSameRootA.
1966 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1968 LPCWSTR lpszStart;
1969 int dwLen;
1971 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1973 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1974 return FALSE;
1976 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1977 if (lpszStart - lpszPath1 > dwLen)
1978 return FALSE; /* Paths not common up to length of the root */
1979 return TRUE;
1982 /*************************************************************************
1983 * PathIsContentTypeA [SHLWAPI.@]
1985 * Determine if a file is of a given registered content type.
1987 * PARAMS
1988 * lpszPath [I] File to check
1989 * lpszContentType [I] Content type to check for
1991 * RETURNS
1992 * TRUE If lpszPath is a given registered content type,
1993 * FALSE Otherwise.
1995 * NOTES
1996 * This function looks up the registered content type for lpszPath. If
1997 * a content type is registered, it is compared (case insensitively) to
1998 * lpszContentType. Only if this matches does the function succeed.
2000 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
2002 LPCSTR szExt;
2003 DWORD dwDummy;
2004 char szBuff[MAX_PATH];
2006 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2008 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2009 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2010 REG_NONE, szBuff, &dwDummy) &&
2011 !strcasecmp(lpszContentType, szBuff))
2013 return TRUE;
2015 return FALSE;
2018 /*************************************************************************
2019 * PathIsContentTypeW [SHLWAPI.@]
2021 * See PathIsContentTypeA.
2023 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2025 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2026 LPCWSTR szExt;
2027 DWORD dwDummy;
2028 WCHAR szBuff[MAX_PATH];
2030 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2032 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2033 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2034 REG_NONE, szBuff, &dwDummy) &&
2035 !strcmpiW(lpszContentType, szBuff))
2037 return TRUE;
2039 return FALSE;
2042 /*************************************************************************
2043 * PathIsFileSpecA [SHLWAPI.@]
2045 * Determine if a path is a file specification.
2047 * PARAMS
2048 * lpszPath [I] Path to check
2050 * RETURNS
2051 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
2052 * FALSE Otherwise.
2054 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2056 TRACE("(%s)\n", debugstr_a(lpszPath));
2058 if (!lpszPath)
2059 return FALSE;
2061 while (*lpszPath)
2063 if (*lpszPath == '\\' || *lpszPath == ':')
2064 return FALSE;
2065 lpszPath = CharNextA(lpszPath);
2067 return TRUE;
2070 /*************************************************************************
2071 * PathIsFileSpecW [SHLWAPI.@]
2073 * See PathIsFileSpecA.
2075 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2077 TRACE("(%s)\n", debugstr_w(lpszPath));
2079 if (!lpszPath)
2080 return FALSE;
2082 while (*lpszPath)
2084 if (*lpszPath == '\\' || *lpszPath == ':')
2085 return FALSE;
2086 lpszPath++;
2088 return TRUE;
2091 /*************************************************************************
2092 * PathIsPrefixA [SHLWAPI.@]
2094 * Determine if a path is a prefix of another.
2096 * PARAMS
2097 * lpszPrefix [I] Prefix
2098 * lpszPath [I] Path to check
2100 * RETURNS
2101 * TRUE If lpszPath has lpszPrefix as its prefix,
2102 * FALSE If either path is NULL or lpszPrefix is not a prefix
2104 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2106 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2108 if (lpszPrefix && lpszPath &&
2109 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2110 return TRUE;
2111 return FALSE;
2114 /*************************************************************************
2115 * PathIsPrefixW [SHLWAPI.@]
2117 * See PathIsPrefixA.
2119 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2121 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2123 if (lpszPrefix && lpszPath &&
2124 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2125 return TRUE;
2126 return FALSE;
2129 /*************************************************************************
2130 * PathIsSystemFolderA [SHLWAPI.@]
2132 * Determine if a path or file attributes are a system folder.
2134 * PARAMS
2135 * lpszPath [I] Path to check.
2136 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2138 * RETURNS
2139 * TRUE If lpszPath or dwAttrib are a system folder.
2140 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2142 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2144 TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2146 if (lpszPath && *lpszPath)
2147 dwAttrib = GetFileAttributesA(lpszPath);
2149 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2150 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2151 return FALSE;
2152 return TRUE;
2155 /*************************************************************************
2156 * PathIsSystemFolderW [SHLWAPI.@]
2158 * See PathIsSystemFolderA.
2160 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2162 TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2164 if (lpszPath && *lpszPath)
2165 dwAttrib = GetFileAttributesW(lpszPath);
2167 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2168 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2169 return FALSE;
2170 return TRUE;
2173 /*************************************************************************
2174 * PathIsUNCA [SHLWAPI.@]
2176 * Determine if a path is in UNC format.
2178 * PARAMS
2179 * lpszPath [I] Path to check
2181 * RETURNS
2182 * TRUE: The path is UNC.
2183 * FALSE: The path is not UNC or is NULL.
2185 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2187 TRACE("(%s)\n",debugstr_a(lpszPath));
2189 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2190 return TRUE;
2191 return FALSE;
2194 /*************************************************************************
2195 * PathIsUNCW [SHLWAPI.@]
2197 * See PathIsUNCA.
2199 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2201 TRACE("(%s)\n",debugstr_w(lpszPath));
2203 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2204 return TRUE;
2205 return FALSE;
2208 /*************************************************************************
2209 * PathIsUNCServerA [SHLWAPI.@]
2211 * Determine if a path is a UNC server name ("\\SHARENAME").
2213 * PARAMS
2214 * lpszPath [I] Path to check.
2216 * RETURNS
2217 * TRUE If lpszPath is a valid UNC server name.
2218 * FALSE Otherwise.
2220 * NOTES
2221 * This routine is bug compatible with Win32: Server names with a
2222 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2223 * Fixing this bug may break other shlwapi functions!
2225 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2227 TRACE("(%s)\n", debugstr_a(lpszPath));
2229 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2231 while (*lpszPath)
2233 if (*lpszPath == '\\')
2234 return FALSE;
2235 lpszPath = CharNextA(lpszPath);
2237 return TRUE;
2239 return FALSE;
2242 /*************************************************************************
2243 * PathIsUNCServerW [SHLWAPI.@]
2245 * See PathIsUNCServerA.
2247 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2249 TRACE("(%s)\n", debugstr_w(lpszPath));
2251 if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2253 return !strchrW( lpszPath + 2, '\\' );
2255 return FALSE;
2258 /*************************************************************************
2259 * PathIsUNCServerShareA [SHLWAPI.@]
2261 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2263 * PARAMS
2264 * lpszPath [I] Path to check.
2266 * RETURNS
2267 * TRUE If lpszPath is a valid UNC server share.
2268 * FALSE Otherwise.
2270 * NOTES
2271 * This routine is bug compatible with Win32: Server shares with a
2272 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2273 * Fixing this bug may break other shlwapi functions!
2275 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2277 TRACE("(%s)\n", debugstr_a(lpszPath));
2279 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2281 BOOL bSeenSlash = FALSE;
2282 while (*lpszPath)
2284 if (*lpszPath == '\\')
2286 if (bSeenSlash)
2287 return FALSE;
2288 bSeenSlash = TRUE;
2290 lpszPath = CharNextA(lpszPath);
2292 return bSeenSlash;
2294 return FALSE;
2297 /*************************************************************************
2298 * PathIsUNCServerShareW [SHLWAPI.@]
2300 * See PathIsUNCServerShareA.
2302 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2304 TRACE("(%s)\n", debugstr_w(lpszPath));
2306 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2308 BOOL bSeenSlash = FALSE;
2309 while (*lpszPath)
2311 if (*lpszPath == '\\')
2313 if (bSeenSlash)
2314 return FALSE;
2315 bSeenSlash = TRUE;
2317 lpszPath++;
2319 return bSeenSlash;
2321 return FALSE;
2324 /*************************************************************************
2325 * PathCanonicalizeA [SHLWAPI.@]
2327 * Convert a path to its canonical form.
2329 * PARAMS
2330 * lpszBuf [O] Output path
2331 * lpszPath [I] Path to canonicalize
2333 * RETURNS
2334 * Success: TRUE. lpszBuf contains the output path,
2335 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2337 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2339 BOOL bRet = FALSE;
2341 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2343 if (lpszBuf)
2344 *lpszBuf = '\0';
2346 if (!lpszBuf || !lpszPath)
2347 SetLastError(ERROR_INVALID_PARAMETER);
2348 else
2350 WCHAR szPath[MAX_PATH];
2351 WCHAR szBuff[MAX_PATH];
2352 int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2354 if (!ret) {
2355 WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
2356 return FALSE;
2358 bRet = PathCanonicalizeW(szBuff, szPath);
2359 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2361 return bRet;
2365 /*************************************************************************
2366 * PathCanonicalizeW [SHLWAPI.@]
2368 * See PathCanonicalizeA.
2370 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2372 LPWSTR lpszDst = lpszBuf;
2373 LPCWSTR lpszSrc = lpszPath;
2375 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2377 if (lpszBuf)
2378 *lpszDst = '\0';
2380 if (!lpszBuf || !lpszPath)
2382 SetLastError(ERROR_INVALID_PARAMETER);
2383 return FALSE;
2386 if (!*lpszPath)
2388 *lpszBuf++ = '\\';
2389 *lpszBuf = '\0';
2390 return TRUE;
2393 /* Copy path root */
2394 if (*lpszSrc == '\\')
2396 *lpszDst++ = *lpszSrc++;
2398 else if (*lpszSrc && lpszSrc[1] == ':')
2400 /* X:\ */
2401 *lpszDst++ = *lpszSrc++;
2402 *lpszDst++ = *lpszSrc++;
2403 if (*lpszSrc == '\\')
2404 *lpszDst++ = *lpszSrc++;
2407 /* Canonicalize the rest of the path */
2408 while (*lpszSrc)
2410 if (*lpszSrc == '.')
2412 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2414 lpszSrc += 2; /* Skip .\ */
2416 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2418 /* \.. backs up a directory, over the root if it has no \ following X:.
2419 * .. is ignored if it would remove a UNC server name or inital \\
2421 if (lpszDst != lpszBuf)
2423 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2424 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2425 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2427 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2429 lpszDst -= 2;
2430 while (lpszDst > lpszBuf && *lpszDst != '\\')
2431 lpszDst--;
2432 if (*lpszDst == '\\')
2433 lpszDst++; /* Reset to last '\' */
2434 else
2435 lpszDst = lpszBuf; /* Start path again from new root */
2437 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2438 lpszDst -= 2;
2440 while (lpszDst > lpszBuf && *lpszDst != '\\')
2441 lpszDst--;
2442 if (lpszDst == lpszBuf)
2444 *lpszDst++ = '\\';
2445 lpszSrc++;
2448 lpszSrc += 2; /* Skip .. in src path */
2450 else
2451 *lpszDst++ = *lpszSrc++;
2453 else
2454 *lpszDst++ = *lpszSrc++;
2456 /* Append \ to naked drive specs */
2457 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2458 *lpszDst++ = '\\';
2459 *lpszDst++ = '\0';
2460 return TRUE;
2463 /*************************************************************************
2464 * PathFindNextComponentA [SHLWAPI.@]
2466 * Find the next component in a path.
2468 * PARAMS
2469 * lpszPath [I] Path to find next component in
2471 * RETURNS
2472 * Success: A pointer to the next component, or the end of the string.
2473 * Failure: NULL, If lpszPath is invalid
2475 * NOTES
2476 * A 'component' is either a backslash character (\) or UNC marker (\\).
2477 * Because of this, relative paths (e.g "c:foo") are regarded as having
2478 * only one component.
2480 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2482 LPSTR lpszSlash;
2484 TRACE("(%s)\n", debugstr_a(lpszPath));
2486 if(!lpszPath || !*lpszPath)
2487 return NULL;
2489 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2491 if (lpszSlash[1] == '\\')
2492 lpszSlash++;
2493 return lpszSlash + 1;
2495 return (LPSTR)lpszPath + strlen(lpszPath);
2498 /*************************************************************************
2499 * PathFindNextComponentW [SHLWAPI.@]
2501 * See PathFindNextComponentA.
2503 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2505 LPWSTR lpszSlash;
2507 TRACE("(%s)\n", debugstr_w(lpszPath));
2509 if(!lpszPath || !*lpszPath)
2510 return NULL;
2512 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2514 if (lpszSlash[1] == '\\')
2515 lpszSlash++;
2516 return lpszSlash + 1;
2518 return (LPWSTR)lpszPath + strlenW(lpszPath);
2521 /*************************************************************************
2522 * PathAddExtensionA [SHLWAPI.@]
2524 * Add a file extension to a path
2526 * PARAMS
2527 * lpszPath [I/O] Path to add extension to
2528 * lpszExtension [I] Extension to add to lpszPath
2530 * RETURNS
2531 * TRUE If the path was modified,
2532 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2533 * extension already, or the new path length is too big.
2535 * FIXME
2536 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2537 * does not do this, so the behaviour was removed.
2539 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2541 size_t dwLen;
2543 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2545 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2546 return FALSE;
2548 dwLen = strlen(lpszPath);
2550 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2551 return FALSE;
2553 strcpy(lpszPath + dwLen, lpszExtension);
2554 return TRUE;
2557 /*************************************************************************
2558 * PathAddExtensionW [SHLWAPI.@]
2560 * See PathAddExtensionA.
2562 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2564 size_t dwLen;
2566 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2568 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2569 return FALSE;
2571 dwLen = strlenW(lpszPath);
2573 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2574 return FALSE;
2576 strcpyW(lpszPath + dwLen, lpszExtension);
2577 return TRUE;
2580 /*************************************************************************
2581 * PathMakePrettyA [SHLWAPI.@]
2583 * Convert an uppercase DOS filename into lowercase.
2585 * PARAMS
2586 * lpszPath [I/O] Path to convert.
2588 * RETURNS
2589 * TRUE If the path was an uppercase DOS path and was converted,
2590 * FALSE Otherwise.
2592 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2594 LPSTR pszIter = lpszPath;
2596 TRACE("(%s)\n", debugstr_a(lpszPath));
2598 if (!pszIter)
2599 return FALSE;
2601 if (*pszIter)
2605 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2606 return FALSE; /* Not DOS path */
2607 pszIter++;
2608 } while (*pszIter);
2609 pszIter = lpszPath + 1;
2610 while (*pszIter)
2612 *pszIter = tolower(*pszIter);
2613 pszIter++;
2616 return TRUE;
2619 /*************************************************************************
2620 * PathMakePrettyW [SHLWAPI.@]
2622 * See PathMakePrettyA.
2624 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2626 LPWSTR pszIter = lpszPath;
2628 TRACE("(%s)\n", debugstr_w(lpszPath));
2630 if (!pszIter)
2631 return FALSE;
2633 if (*pszIter)
2637 if (islowerW(*pszIter))
2638 return FALSE; /* Not DOS path */
2639 pszIter++;
2640 } while (*pszIter);
2641 pszIter = lpszPath + 1;
2642 while (*pszIter)
2644 *pszIter = tolowerW(*pszIter);
2645 pszIter++;
2648 return TRUE;
2651 /*************************************************************************
2652 * PathCommonPrefixA [SHLWAPI.@]
2654 * Determine the length of the common prefix between two paths.
2656 * PARAMS
2657 * lpszFile1 [I] First path for comparison
2658 * lpszFile2 [I] Second path for comparison
2659 * achPath [O] Destination for common prefix string
2661 * RETURNS
2662 * The length of the common prefix. This is 0 if there is no common
2663 * prefix between the paths or if any parameters are invalid. If the prefix
2664 * is non-zero and achPath is not NULL, achPath is filled with the common
2665 * part of the prefix and NUL terminated.
2667 * NOTES
2668 * A common prefix of 2 is always returned as 3. It is thus possible for
2669 * the length returned to be invalid (i.e. Longer than one or both of the
2670 * strings given as parameters). This Win32 behaviour has been implemented
2671 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2672 * To work around this when using this function, always check that the byte
2673 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2675 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2677 size_t iLen = 0;
2678 LPCSTR lpszIter1 = lpszFile1;
2679 LPCSTR lpszIter2 = lpszFile2;
2681 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2683 if (achPath)
2684 *achPath = '\0';
2686 if (!lpszFile1 || !lpszFile2)
2687 return 0;
2689 /* Handle roots first */
2690 if (PathIsUNCA(lpszFile1))
2692 if (!PathIsUNCA(lpszFile2))
2693 return 0;
2694 lpszIter1 += 2;
2695 lpszIter2 += 2;
2697 else if (PathIsUNCA(lpszFile2))
2698 return 0; /* Know already lpszFile1 is not UNC */
2702 /* Update len */
2703 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2704 (!*lpszIter2 || *lpszIter2 == '\\'))
2705 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2707 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2708 break; /* Strings differ at this point */
2710 lpszIter1++;
2711 lpszIter2++;
2712 } while (1);
2714 if (iLen == 2)
2715 iLen++; /* Feature/Bug compatible with Win32 */
2717 if (iLen && achPath)
2719 memcpy(achPath,lpszFile1,iLen);
2720 achPath[iLen] = '\0';
2722 return iLen;
2725 /*************************************************************************
2726 * PathCommonPrefixW [SHLWAPI.@]
2728 * See PathCommonPrefixA.
2730 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2732 size_t iLen = 0;
2733 LPCWSTR lpszIter1 = lpszFile1;
2734 LPCWSTR lpszIter2 = lpszFile2;
2736 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2738 if (achPath)
2739 *achPath = '\0';
2741 if (!lpszFile1 || !lpszFile2)
2742 return 0;
2744 /* Handle roots first */
2745 if (PathIsUNCW(lpszFile1))
2747 if (!PathIsUNCW(lpszFile2))
2748 return 0;
2749 lpszIter1 += 2;
2750 lpszIter2 += 2;
2752 else if (PathIsUNCW(lpszFile2))
2753 return 0; /* Know already lpszFile1 is not UNC */
2757 /* Update len */
2758 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2759 (!*lpszIter2 || *lpszIter2 == '\\'))
2760 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2762 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2763 break; /* Strings differ at this point */
2765 lpszIter1++;
2766 lpszIter2++;
2767 } while (1);
2769 if (iLen == 2)
2770 iLen++; /* Feature/Bug compatible with Win32 */
2772 if (iLen && achPath)
2774 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2775 achPath[iLen] = '\0';
2777 return iLen;
2780 /*************************************************************************
2781 * PathCompactPathA [SHLWAPI.@]
2783 * Make a path fit into a given width when printed to a DC.
2785 * PARAMS
2786 * hDc [I] Destination DC
2787 * lpszPath [I/O] Path to be printed to hDc
2788 * dx [I] Desired width
2790 * RETURNS
2791 * TRUE If the path was modified/went well.
2792 * FALSE Otherwise.
2794 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2796 BOOL bRet = FALSE;
2798 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2800 if (lpszPath)
2802 WCHAR szPath[MAX_PATH];
2803 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2804 bRet = PathCompactPathW(hDC, szPath, dx);
2805 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2807 return bRet;
2810 /*************************************************************************
2811 * PathCompactPathW [SHLWAPI.@]
2813 * See PathCompactPathA.
2815 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2817 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2818 BOOL bRet = TRUE;
2819 HDC hdc = 0;
2820 WCHAR buff[MAX_PATH];
2821 SIZE size;
2822 DWORD dwLen;
2824 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2826 if (!lpszPath)
2827 return FALSE;
2829 if (!hDC)
2830 hdc = hDC = GetDC(0);
2832 /* Get the length of the whole path */
2833 dwLen = strlenW(lpszPath);
2834 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2836 if ((UINT)size.cx > dx)
2838 /* Path too big, must reduce it */
2839 LPWSTR sFile;
2840 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2842 sFile = PathFindFileNameW(lpszPath);
2843 if (sFile != lpszPath) sFile--;
2845 /* Get the size of ellipses */
2846 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2847 dwEllipsesLen = size.cx;
2848 /* Get the size of the file name */
2849 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2850 dwPathLen = size.cx;
2852 if (sFile != lpszPath)
2854 LPWSTR sPath = sFile;
2855 BOOL bEllipses = FALSE;
2857 /* The path includes a file name. Include as much of the path prior to
2858 * the file name as possible, allowing for the ellipses, e.g:
2859 * c:\some very long path\filename ==> c:\some v...\filename
2861 lstrcpynW(buff, sFile, MAX_PATH);
2865 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2867 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2868 dwTotalLen += size.cx;
2869 if (dwTotalLen <= dx)
2870 break;
2871 sPath--;
2872 if (!bEllipses)
2874 bEllipses = TRUE;
2875 sPath -= 2;
2877 } while (sPath > lpszPath);
2879 if (sPath > lpszPath)
2881 if (bEllipses)
2883 strcpyW(sPath, szEllipses);
2884 strcpyW(sPath+3, buff);
2886 bRet = TRUE;
2887 goto end;
2889 strcpyW(lpszPath, szEllipses);
2890 strcpyW(lpszPath+3, buff);
2891 bRet = FALSE;
2892 goto end;
2895 /* Trim the path by adding ellipses to the end, e.g:
2896 * A very long file name.txt ==> A very...
2898 dwLen = strlenW(lpszPath);
2900 if (dwLen > MAX_PATH - 3)
2901 dwLen = MAX_PATH - 3;
2902 lstrcpynW(buff, sFile, dwLen);
2904 do {
2905 dwLen--;
2906 GetTextExtentPointW(hDC, buff, dwLen, &size);
2907 } while (dwLen && size.cx + dwEllipsesLen > dx);
2909 if (!dwLen)
2911 DWORD dwWritten = 0;
2913 dwEllipsesLen /= 3; /* Size of a single '.' */
2915 /* Write as much of the Ellipses string as possible */
2916 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2918 *lpszPath++ = '.';
2919 dwWritten += dwEllipsesLen;
2920 dwLen++;
2922 *lpszPath = '\0';
2923 bRet = FALSE;
2925 else
2927 strcpyW(buff + dwLen, szEllipses);
2928 strcpyW(lpszPath, buff);
2932 end:
2933 if (hdc)
2934 ReleaseDC(0, hdc);
2936 return bRet;
2939 /*************************************************************************
2940 * PathGetCharTypeA [SHLWAPI.@]
2942 * Categorise a character from a file path.
2944 * PARAMS
2945 * ch [I] Character to get the type of
2947 * RETURNS
2948 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2950 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2952 return PathGetCharTypeW(ch);
2955 /*************************************************************************
2956 * PathGetCharTypeW [SHLWAPI.@]
2958 * See PathGetCharTypeA.
2960 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2962 UINT flags = 0;
2964 TRACE("(%d)\n", ch);
2966 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2967 ch == '"' || ch == '|' || ch == '/')
2968 flags = GCT_INVALID; /* Invalid */
2969 else if (ch == '*' || ch=='?')
2970 flags = GCT_WILD; /* Wildchars */
2971 else if ((ch == '\\') || (ch == ':'))
2972 return GCT_SEPARATOR; /* Path separators */
2973 else
2975 if (ch < 126)
2977 if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2978 ch == '.' || ch == '@' || ch == '^' ||
2979 ch == '\'' || ch == 130 || ch == '`')
2980 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2982 else
2983 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2984 flags |= GCT_LFNCHAR; /* Valid for long file names */
2986 return flags;
2989 /*************************************************************************
2990 * SHLWAPI_UseSystemForSystemFolders
2992 * Internal helper for PathMakeSystemFolderW.
2994 static BOOL SHLWAPI_UseSystemForSystemFolders(void)
2996 static BOOL bCheckedReg = FALSE;
2997 static BOOL bUseSystemForSystemFolders = FALSE;
2999 if (!bCheckedReg)
3001 bCheckedReg = TRUE;
3003 /* Key tells Win what file attributes to use on system folders */
3004 if (SHGetValueA(HKEY_LOCAL_MACHINE,
3005 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
3006 "UseSystemForSystemFolders", 0, 0, 0))
3007 bUseSystemForSystemFolders = TRUE;
3009 return bUseSystemForSystemFolders;
3012 /*************************************************************************
3013 * PathMakeSystemFolderA [SHLWAPI.@]
3015 * Set system folder attribute for a path.
3017 * PARAMS
3018 * lpszPath [I] The path to turn into a system folder
3020 * RETURNS
3021 * TRUE If the path was changed to/already was a system folder
3022 * FALSE If the path is invalid or SetFileAttributesA() fails
3024 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3026 BOOL bRet = FALSE;
3028 TRACE("(%s)\n", debugstr_a(lpszPath));
3030 if (lpszPath && *lpszPath)
3032 WCHAR szPath[MAX_PATH];
3033 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3034 bRet = PathMakeSystemFolderW(szPath);
3036 return bRet;
3039 /*************************************************************************
3040 * PathMakeSystemFolderW [SHLWAPI.@]
3042 * See PathMakeSystemFolderA.
3044 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3046 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3047 WCHAR buff[MAX_PATH];
3049 TRACE("(%s)\n", debugstr_w(lpszPath));
3051 if (!lpszPath || !*lpszPath)
3052 return FALSE;
3054 /* If the directory is already a system directory, don't do anything */
3055 GetSystemDirectoryW(buff, MAX_PATH);
3056 if (!strcmpW(buff, lpszPath))
3057 return TRUE;
3059 GetWindowsDirectoryW(buff, MAX_PATH);
3060 if (!strcmpW(buff, lpszPath))
3061 return TRUE;
3063 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3064 if (SHLWAPI_UseSystemForSystemFolders())
3065 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3067 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3068 return FALSE;
3070 /* Change file attributes to system attributes */
3071 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3072 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3075 /*************************************************************************
3076 * PathRenameExtensionA [SHLWAPI.@]
3078 * Swap the file extension in a path with another extension.
3080 * PARAMS
3081 * lpszPath [I/O] Path to swap the extension in
3082 * lpszExt [I] The new extension
3084 * RETURNS
3085 * TRUE if lpszPath was modified,
3086 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3088 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3090 LPSTR lpszExtension;
3092 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3094 lpszExtension = PathFindExtensionA(lpszPath);
3096 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3097 return FALSE;
3099 strcpy(lpszExtension, lpszExt);
3100 return TRUE;
3103 /*************************************************************************
3104 * PathRenameExtensionW [SHLWAPI.@]
3106 * See PathRenameExtensionA.
3108 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3110 LPWSTR lpszExtension;
3112 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3114 lpszExtension = PathFindExtensionW(lpszPath);
3116 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3117 return FALSE;
3119 strcpyW(lpszExtension, lpszExt);
3120 return TRUE;
3123 /*************************************************************************
3124 * PathSearchAndQualifyA [SHLWAPI.@]
3126 * Determine if a given path is correct and fully qualified.
3128 * PARAMS
3129 * lpszPath [I] Path to check
3130 * lpszBuf [O] Output for correct path
3131 * cchBuf [I] Size of lpszBuf
3133 * RETURNS
3134 * Unknown.
3136 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3138 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3140 if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3141 return TRUE;
3142 return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3145 /*************************************************************************
3146 * PathSearchAndQualifyW [SHLWAPI.@]
3148 * See PathSearchAndQualifyA.
3150 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3152 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3154 if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3155 return TRUE;
3156 return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3159 /*************************************************************************
3160 * PathSkipRootA [SHLWAPI.@]
3162 * Return the portion of a path following the drive letter or mount point.
3164 * PARAMS
3165 * lpszPath [I] The path to skip on
3167 * RETURNS
3168 * Success: A pointer to the next character after the root.
3169 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3171 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3173 TRACE("(%s)\n", debugstr_a(lpszPath));
3175 if (!lpszPath || !*lpszPath)
3176 return NULL;
3178 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3180 /* Network share: skip share server and mount point */
3181 lpszPath += 2;
3182 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3183 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3184 lpszPath++;
3185 return (LPSTR)lpszPath;
3188 if (IsDBCSLeadByte(*lpszPath))
3189 return NULL;
3191 /* Check x:\ */
3192 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3193 return (LPSTR)lpszPath + 3;
3194 return NULL;
3197 /*************************************************************************
3198 * PathSkipRootW [SHLWAPI.@]
3200 * See PathSkipRootA.
3202 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3204 TRACE("(%s)\n", debugstr_w(lpszPath));
3206 if (!lpszPath || !*lpszPath)
3207 return NULL;
3209 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3211 /* Network share: skip share server and mount point */
3212 lpszPath += 2;
3213 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3214 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3215 lpszPath++;
3216 return (LPWSTR)lpszPath;
3219 /* Check x:\ */
3220 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3221 return (LPWSTR)lpszPath + 3;
3222 return NULL;
3225 /*************************************************************************
3226 * PathCreateFromUrlA [SHLWAPI.@]
3228 * See PathCreateFromUrlW
3230 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3231 LPDWORD pcchPath, DWORD dwReserved)
3233 WCHAR bufW[MAX_PATH];
3234 WCHAR *pathW = bufW;
3235 UNICODE_STRING urlW;
3236 HRESULT ret;
3237 DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3239 if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3240 return E_INVALIDARG;
3241 if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3242 pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3243 ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3245 if(ret == S_OK) {
3246 RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3247 if(*pcchPath > lenA) {
3248 RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3249 pszPath[lenA] = 0;
3250 *pcchPath = lenA;
3251 } else {
3252 *pcchPath = lenA + 1;
3253 ret = E_POINTER;
3256 if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3257 RtlFreeUnicodeString(&urlW);
3258 return ret;
3261 /*************************************************************************
3262 * PathCreateFromUrlW [SHLWAPI.@]
3264 * Create a path from a URL
3266 * PARAMS
3267 * lpszUrl [I] URL to convert into a path
3268 * lpszPath [O] Output buffer for the resulting Path
3269 * pcchPath [I] Length of lpszPath
3270 * dwFlags [I] Flags controlling the conversion
3272 * RETURNS
3273 * Success: S_OK. lpszPath contains the URL in path format,
3274 * Failure: An HRESULT error code such as E_INVALIDARG.
3276 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3277 LPDWORD pcchPath, DWORD dwReserved)
3279 static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3280 HRESULT hr;
3281 DWORD nslashes = 0;
3282 WCHAR *ptr;
3284 TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3286 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3287 return E_INVALIDARG;
3290 if (strncmpW(pszUrl, file_colon, 5))
3291 return E_INVALIDARG;
3292 pszUrl += 5;
3294 while(*pszUrl == '/' || *pszUrl == '\\') {
3295 nslashes++;
3296 pszUrl++;
3299 if(isalphaW(*pszUrl) && (pszUrl[1] == ':' || pszUrl[1] == '|') && (pszUrl[2] == '/' || pszUrl[2] == '\\'))
3300 nslashes = 0;
3302 switch(nslashes) {
3303 case 2:
3304 pszUrl -= 2;
3305 break;
3306 case 0:
3307 break;
3308 default:
3309 pszUrl -= 1;
3310 break;
3313 hr = UrlUnescapeW((LPWSTR)pszUrl, pszPath, pcchPath, 0);
3314 if(hr != S_OK) return hr;
3316 for(ptr = pszPath; *ptr; ptr++)
3317 if(*ptr == '/') *ptr = '\\';
3319 while(*pszPath == '\\')
3320 pszPath++;
3322 if(isalphaW(*pszPath) && pszPath[1] == '|' && pszPath[2] == '\\') /* c|\ -> c:\ */
3323 pszPath[1] = ':';
3325 if(nslashes == 2 && (ptr = strchrW(pszPath, '\\'))) { /* \\host\c:\ -> \\hostc:\ */
3326 ptr++;
3327 if(isalphaW(*ptr) && (ptr[1] == ':' || ptr[1] == '|') && ptr[2] == '\\') {
3328 memmove(ptr - 1, ptr, (strlenW(ptr) + 1) * sizeof(WCHAR));
3329 (*pcchPath)--;
3333 TRACE("Returning %s\n",debugstr_w(pszPath));
3335 return hr;
3338 /*************************************************************************
3339 * PathRelativePathToA [SHLWAPI.@]
3341 * Create a relative path from one path to another.
3343 * PARAMS
3344 * lpszPath [O] Destination for relative path
3345 * lpszFrom [I] Source path
3346 * dwAttrFrom [I] File attribute of source path
3347 * lpszTo [I] Destination path
3348 * dwAttrTo [I] File attributes of destination path
3350 * RETURNS
3351 * TRUE If a relative path can be formed. lpszPath contains the new path
3352 * FALSE If the paths are not relative or any parameters are invalid
3354 * NOTES
3355 * lpszTo should be at least MAX_PATH in length.
3357 * Calling this function with relative paths for lpszFrom or lpszTo may
3358 * give erroneous results.
3360 * The Win32 version of this function contains a bug where the lpszTo string
3361 * may be referenced 1 byte beyond the end of the string. As a result random
3362 * garbage may be written to the output path, depending on what lies beyond
3363 * the last byte of the string. This bug occurs because of the behaviour of
3364 * PathCommonPrefix() (see notes for that function), and no workaround seems
3365 * possible with Win32.
3367 * This bug has been fixed here, so for example the relative path from "\\"
3368 * to "\\" is correctly determined as "." in this implementation.
3370 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3371 LPCSTR lpszTo, DWORD dwAttrTo)
3373 BOOL bRet = FALSE;
3375 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3376 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3378 if(lpszPath && lpszFrom && lpszTo)
3380 WCHAR szPath[MAX_PATH];
3381 WCHAR szFrom[MAX_PATH];
3382 WCHAR szTo[MAX_PATH];
3383 MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3384 MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3385 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3386 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3388 return bRet;
3391 /*************************************************************************
3392 * PathRelativePathToW [SHLWAPI.@]
3394 * See PathRelativePathToA.
3396 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3397 LPCWSTR lpszTo, DWORD dwAttrTo)
3399 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3400 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3401 WCHAR szFrom[MAX_PATH];
3402 WCHAR szTo[MAX_PATH];
3403 DWORD dwLen;
3405 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3406 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3408 if(!lpszPath || !lpszFrom || !lpszTo)
3409 return FALSE;
3411 *lpszPath = '\0';
3412 lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3413 lstrcpynW(szTo, lpszTo, MAX_PATH);
3415 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3416 PathRemoveFileSpecW(szFrom);
3417 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3418 PathRemoveFileSpecW(szTo);
3420 /* Paths can only be relative if they have a common root */
3421 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3422 return FALSE;
3424 /* Strip off lpszFrom components to the root, by adding "..\" */
3425 lpszFrom = szFrom + dwLen;
3426 if (!*lpszFrom)
3428 lpszPath[0] = '.';
3429 lpszPath[1] = '\0';
3431 if (*lpszFrom == '\\')
3432 lpszFrom++;
3434 while (*lpszFrom)
3436 lpszFrom = PathFindNextComponentW(lpszFrom);
3437 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3440 /* From the root add the components of lpszTo */
3441 lpszTo += dwLen;
3442 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3443 * this function.
3445 if (*lpszTo && lpszTo[-1])
3447 if (*lpszTo != '\\')
3448 lpszTo--;
3449 dwLen = strlenW(lpszPath);
3450 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3452 *lpszPath = '\0';
3453 return FALSE;
3455 strcpyW(lpszPath + dwLen, lpszTo);
3457 return TRUE;
3460 /*************************************************************************
3461 * PathUnmakeSystemFolderA [SHLWAPI.@]
3463 * Remove the system folder attributes from a path.
3465 * PARAMS
3466 * lpszPath [I] The path to remove attributes from
3468 * RETURNS
3469 * Success: TRUE.
3470 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3471 * SetFileAttributesA() fails.
3473 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3475 DWORD dwAttr;
3477 TRACE("(%s)\n", debugstr_a(lpszPath));
3479 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3480 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3481 return FALSE;
3483 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3484 return SetFileAttributesA(lpszPath, dwAttr);
3487 /*************************************************************************
3488 * PathUnmakeSystemFolderW [SHLWAPI.@]
3490 * See PathUnmakeSystemFolderA.
3492 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3494 DWORD dwAttr;
3496 TRACE("(%s)\n", debugstr_w(lpszPath));
3498 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3499 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3500 return FALSE;
3502 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3503 return SetFileAttributesW(lpszPath, dwAttr);
3507 /*************************************************************************
3508 * PathSetDlgItemPathA [SHLWAPI.@]
3510 * Set the text of a dialog item to a path, shrinking the path to fit
3511 * if it is too big for the item.
3513 * PARAMS
3514 * hDlg [I] Dialog handle
3515 * id [I] ID of item in the dialog
3516 * lpszPath [I] Path to set as the items text
3518 * RETURNS
3519 * Nothing.
3521 * NOTES
3522 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3523 * window text is erased).
3525 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3527 WCHAR szPath[MAX_PATH];
3529 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3531 if (lpszPath)
3532 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3533 else
3534 szPath[0] = '\0';
3535 PathSetDlgItemPathW(hDlg, id, szPath);
3538 /*************************************************************************
3539 * PathSetDlgItemPathW [SHLWAPI.@]
3541 * See PathSetDlgItemPathA.
3543 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3545 WCHAR path[MAX_PATH + 1];
3546 HWND hwItem;
3547 RECT rect;
3548 HDC hdc;
3549 HGDIOBJ hPrevObj;
3551 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3553 if (!(hwItem = GetDlgItem(hDlg, id)))
3554 return;
3556 if (lpszPath)
3557 lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3558 else
3559 path[0] = '\0';
3561 GetClientRect(hwItem, &rect);
3562 hdc = GetDC(hDlg);
3563 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3565 if (hPrevObj)
3567 PathCompactPathW(hdc, path, rect.right);
3568 SelectObject(hdc, hPrevObj);
3571 ReleaseDC(hDlg, hdc);
3572 SetWindowTextW(hwItem, path);
3575 /*************************************************************************
3576 * PathIsNetworkPathA [SHLWAPI.@]
3578 * Determine if the given path is a network path.
3580 * PARAMS
3581 * lpszPath [I] Path to check
3583 * RETURNS
3584 * TRUE If lpszPath is a UNC share or mapped network drive, or
3585 * FALSE If lpszPath is a local drive or cannot be determined
3587 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3589 int dwDriveNum;
3591 TRACE("(%s)\n",debugstr_a(lpszPath));
3593 if (!lpszPath)
3594 return FALSE;
3595 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3596 return TRUE;
3597 dwDriveNum = PathGetDriveNumberA(lpszPath);
3598 if (dwDriveNum == -1)
3599 return FALSE;
3600 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3601 return pIsNetDrive(dwDriveNum);
3604 /*************************************************************************
3605 * PathIsNetworkPathW [SHLWAPI.@]
3607 * See PathIsNetworkPathA.
3609 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3611 int dwDriveNum;
3613 TRACE("(%s)\n", debugstr_w(lpszPath));
3615 if (!lpszPath)
3616 return FALSE;
3617 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3618 return TRUE;
3619 dwDriveNum = PathGetDriveNumberW(lpszPath);
3620 if (dwDriveNum == -1)
3621 return FALSE;
3622 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3623 return pIsNetDrive(dwDriveNum);
3626 /*************************************************************************
3627 * PathIsLFNFileSpecA [SHLWAPI.@]
3629 * Determine if the given path is a long file name
3631 * PARAMS
3632 * lpszPath [I] Path to check
3634 * RETURNS
3635 * TRUE If path is a long file name,
3636 * FALSE If path is a valid DOS short file name
3638 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3640 DWORD dwNameLen = 0, dwExtLen = 0;
3642 TRACE("(%s)\n",debugstr_a(lpszPath));
3644 if (!lpszPath)
3645 return FALSE;
3647 while (*lpszPath)
3649 if (*lpszPath == ' ')
3650 return TRUE; /* DOS names cannot have spaces */
3651 if (*lpszPath == '.')
3653 if (dwExtLen)
3654 return TRUE; /* DOS names have only one dot */
3655 dwExtLen = 1;
3657 else if (dwExtLen)
3659 dwExtLen++;
3660 if (dwExtLen > 4)
3661 return TRUE; /* DOS extensions are <= 3 chars*/
3663 else
3665 dwNameLen++;
3666 if (dwNameLen > 8)
3667 return TRUE; /* DOS names are <= 8 chars */
3669 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3671 return FALSE; /* Valid DOS path */
3674 /*************************************************************************
3675 * PathIsLFNFileSpecW [SHLWAPI.@]
3677 * See PathIsLFNFileSpecA.
3679 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3681 DWORD dwNameLen = 0, dwExtLen = 0;
3683 TRACE("(%s)\n",debugstr_w(lpszPath));
3685 if (!lpszPath)
3686 return FALSE;
3688 while (*lpszPath)
3690 if (*lpszPath == ' ')
3691 return TRUE; /* DOS names cannot have spaces */
3692 if (*lpszPath == '.')
3694 if (dwExtLen)
3695 return TRUE; /* DOS names have only one dot */
3696 dwExtLen = 1;
3698 else if (dwExtLen)
3700 dwExtLen++;
3701 if (dwExtLen > 4)
3702 return TRUE; /* DOS extensions are <= 3 chars*/
3704 else
3706 dwNameLen++;
3707 if (dwNameLen > 8)
3708 return TRUE; /* DOS names are <= 8 chars */
3710 lpszPath++;
3712 return FALSE; /* Valid DOS path */
3715 /*************************************************************************
3716 * PathIsDirectoryEmptyA [SHLWAPI.@]
3718 * Determine if a given directory is empty.
3720 * PARAMS
3721 * lpszPath [I] Directory to check
3723 * RETURNS
3724 * TRUE If the directory exists and contains no files,
3725 * FALSE Otherwise
3727 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3729 BOOL bRet = FALSE;
3731 TRACE("(%s)\n",debugstr_a(lpszPath));
3733 if (lpszPath)
3735 WCHAR szPath[MAX_PATH];
3736 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3737 bRet = PathIsDirectoryEmptyW(szPath);
3739 return bRet;
3742 /*************************************************************************
3743 * PathIsDirectoryEmptyW [SHLWAPI.@]
3745 * See PathIsDirectoryEmptyA.
3747 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3749 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3750 WCHAR szSearch[MAX_PATH];
3751 DWORD dwLen;
3752 HANDLE hfind;
3753 BOOL retVal = FALSE;
3754 WIN32_FIND_DATAW find_data;
3756 TRACE("(%s)\n",debugstr_w(lpszPath));
3758 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3759 return FALSE;
3761 lstrcpynW(szSearch, lpszPath, MAX_PATH);
3762 PathAddBackslashW(szSearch);
3763 dwLen = strlenW(szSearch);
3764 if (dwLen > MAX_PATH - 4)
3765 return FALSE;
3767 strcpyW(szSearch + dwLen, szAllFiles);
3768 hfind = FindFirstFileW(szSearch, &find_data);
3770 if (hfind != INVALID_HANDLE_VALUE &&
3771 find_data.cFileName[0] == '.' &&
3772 find_data.cFileName[1] == '.')
3774 /* The only directory entry should be the parent */
3775 if (!FindNextFileW(hfind, &find_data))
3776 retVal = TRUE;
3777 FindClose(hfind);
3779 return retVal;
3783 /*************************************************************************
3784 * PathFindSuffixArrayA [SHLWAPI.@]
3786 * Find a suffix string in an array of suffix strings
3788 * PARAMS
3789 * lpszSuffix [I] Suffix string to search for
3790 * lppszArray [I] Array of suffix strings to search
3791 * dwCount [I] Number of elements in lppszArray
3793 * RETURNS
3794 * Success: The index of the position of lpszSuffix in lppszArray
3795 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3797 * NOTES
3798 * The search is case sensitive.
3799 * The match is made against the end of the suffix string, so for example:
3800 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3802 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3804 size_t dwLen;
3805 int dwRet = 0;
3807 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3809 if (lpszSuffix && lppszArray && dwCount > 0)
3811 dwLen = strlen(lpszSuffix);
3813 while (dwRet < dwCount)
3815 size_t dwCompareLen = strlen(*lppszArray);
3816 if (dwCompareLen < dwLen)
3818 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3819 return *lppszArray; /* Found */
3821 dwRet++;
3822 lppszArray++;
3825 return NULL;
3828 /*************************************************************************
3829 * PathFindSuffixArrayW [SHLWAPI.@]
3831 * See PathFindSuffixArrayA.
3833 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3835 size_t dwLen;
3836 int dwRet = 0;
3838 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3840 if (lpszSuffix && lppszArray && dwCount > 0)
3842 dwLen = strlenW(lpszSuffix);
3844 while (dwRet < dwCount)
3846 size_t dwCompareLen = strlenW(*lppszArray);
3847 if (dwCompareLen < dwLen)
3849 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3850 return *lppszArray; /* Found */
3852 dwRet++;
3853 lppszArray++;
3856 return NULL;
3859 /*************************************************************************
3860 * PathUndecorateA [SHLWAPI.@]
3862 * Undecorate a file path
3864 * PARAMS
3865 * lpszPath [I/O] Path to remove any decoration from
3867 * RETURNS
3868 * Nothing
3870 * NOTES
3871 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3873 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3875 TRACE("(%s)\n",debugstr_a(lpszPath));
3877 if (lpszPath)
3879 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3880 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3882 LPSTR lpszSkip = lpszExt - 2;
3883 if (*lpszSkip == '[')
3884 lpszSkip++; /* [] (no number) */
3885 else
3886 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3887 lpszSkip--;
3888 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3890 /* remove the [n] */
3891 lpszSkip--;
3892 while (*lpszExt)
3893 *lpszSkip++ = *lpszExt++;
3894 *lpszSkip = '\0';
3900 /*************************************************************************
3901 * PathUndecorateW [SHLWAPI.@]
3903 * See PathUndecorateA.
3905 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3907 TRACE("(%s)\n",debugstr_w(lpszPath));
3909 if (lpszPath)
3911 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3912 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3914 LPWSTR lpszSkip = lpszExt - 2;
3915 if (*lpszSkip == '[')
3916 lpszSkip++; /* [] (no number) */
3917 else
3918 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3919 lpszSkip--;
3920 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3922 /* remove the [n] */
3923 lpszSkip--;
3924 while (*lpszExt)
3925 *lpszSkip++ = *lpszExt++;
3926 *lpszSkip = '\0';
3932 /*************************************************************************
3933 * PathUnExpandEnvStringsA [SHLWAPI.@]
3935 * Substitute folder names in a path with their corresponding environment
3936 * strings.
3938 * PARAMS
3939 * pszPath [I] Buffer containing the path to unexpand.
3940 * pszBuf [O] Buffer to receive the unexpanded path.
3941 * cchBuf [I] Size of pszBuf in characters.
3943 * RETURNS
3944 * Success: TRUE
3945 * Failure: FALSE
3947 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR pszPath, LPSTR pszBuf, UINT cchBuf)
3949 FIXME("(%s,%s,0x%08x)\n", debugstr_a(pszPath), debugstr_a(pszBuf), cchBuf);
3950 return FALSE;
3953 /*************************************************************************
3954 * PathUnExpandEnvStringsW [SHLWAPI.@]
3956 * Unicode version of PathUnExpandEnvStringsA.
3958 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR pszPath, LPWSTR pszBuf, UINT cchBuf)
3960 FIXME("(%s,%s,0x%08x)\n", debugstr_w(pszPath), debugstr_w(pszBuf), cchBuf);
3961 return FALSE;
3964 /*************************************************************************
3965 * @ [SHLWAPI.440]
3967 * Find localised or default web content in "%WINDOWS%\web\".
3969 * PARAMS
3970 * lpszFile [I] File name containing content to look for
3971 * lpszPath [O] Buffer to contain the full path to the file
3972 * dwPathLen [I] Length of lpszPath
3974 * RETURNS
3975 * Success: S_OK. lpszPath contains the full path to the content.
3976 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
3978 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
3980 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
3981 HRESULT hRet;
3983 TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
3985 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
3986 szPath[0] = '\0';
3987 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
3988 WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
3989 return hRet;
3992 /*************************************************************************
3993 * @ [SHLWAPI.441]
3995 * Unicode version of SHGetWebFolderFilePathA.
3997 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
3999 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
4000 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
4001 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
4002 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
4003 DWORD dwLen, dwFileLen;
4004 LANGID lidSystem, lidUser;
4006 TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
4008 /* Get base directory for web content */
4009 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4010 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4011 dwLen--;
4013 dwFileLen = strlenW(lpszFile);
4015 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4016 return E_FAIL; /* lpszPath too short */
4018 strcpyW(lpszPath+dwLen, szWeb);
4019 dwLen += szWebLen;
4020 dwPathLen = dwPathLen - dwLen; /* Remaining space */
4022 lidSystem = GetSystemDefaultUILanguage();
4023 lidUser = GetUserDefaultUILanguage();
4025 if (lidSystem != lidUser)
4027 if (dwFileLen + szWebMuiLen < dwPathLen)
4029 /* Use localised content in the users UI language if present */
4030 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4031 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4032 if (PathFileExistsW(lpszPath))
4033 return S_OK;
4037 /* Fall back to OS default installed content */
4038 strcpyW(lpszPath + dwLen, lpszFile);
4039 if (PathFileExistsW(lpszPath))
4040 return S_OK;
4041 return E_FAIL;
4044 #define PATH_CHAR_CLASS_LETTER 0x00000001
4045 #define PATH_CHAR_CLASS_ASTERIX 0x00000002
4046 #define PATH_CHAR_CLASS_DOT 0x00000004
4047 #define PATH_CHAR_CLASS_BACKSLASH 0x00000008
4048 #define PATH_CHAR_CLASS_COLON 0x00000010
4049 #define PATH_CHAR_CLASS_SEMICOLON 0x00000020
4050 #define PATH_CHAR_CLASS_COMMA 0x00000040
4051 #define PATH_CHAR_CLASS_SPACE 0x00000080
4052 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4053 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4055 #define PATH_CHAR_CLASS_INVALID 0x00000000
4056 #define PATH_CHAR_CLASS_ANY 0xffffffff
4058 static const DWORD SHELL_charclass[] =
4060 /* 0x00 */ PATH_CHAR_CLASS_INVALID, /* 0x01 */ PATH_CHAR_CLASS_INVALID,
4061 /* 0x02 */ PATH_CHAR_CLASS_INVALID, /* 0x03 */ PATH_CHAR_CLASS_INVALID,
4062 /* 0x04 */ PATH_CHAR_CLASS_INVALID, /* 0x05 */ PATH_CHAR_CLASS_INVALID,
4063 /* 0x06 */ PATH_CHAR_CLASS_INVALID, /* 0x07 */ PATH_CHAR_CLASS_INVALID,
4064 /* 0x08 */ PATH_CHAR_CLASS_INVALID, /* 0x09 */ PATH_CHAR_CLASS_INVALID,
4065 /* 0x0a */ PATH_CHAR_CLASS_INVALID, /* 0x0b */ PATH_CHAR_CLASS_INVALID,
4066 /* 0x0c */ PATH_CHAR_CLASS_INVALID, /* 0x0d */ PATH_CHAR_CLASS_INVALID,
4067 /* 0x0e */ PATH_CHAR_CLASS_INVALID, /* 0x0f */ PATH_CHAR_CLASS_INVALID,
4068 /* 0x10 */ PATH_CHAR_CLASS_INVALID, /* 0x11 */ PATH_CHAR_CLASS_INVALID,
4069 /* 0x12 */ PATH_CHAR_CLASS_INVALID, /* 0x13 */ PATH_CHAR_CLASS_INVALID,
4070 /* 0x14 */ PATH_CHAR_CLASS_INVALID, /* 0x15 */ PATH_CHAR_CLASS_INVALID,
4071 /* 0x16 */ PATH_CHAR_CLASS_INVALID, /* 0x17 */ PATH_CHAR_CLASS_INVALID,
4072 /* 0x18 */ PATH_CHAR_CLASS_INVALID, /* 0x19 */ PATH_CHAR_CLASS_INVALID,
4073 /* 0x1a */ PATH_CHAR_CLASS_INVALID, /* 0x1b */ PATH_CHAR_CLASS_INVALID,
4074 /* 0x1c */ PATH_CHAR_CLASS_INVALID, /* 0x1d */ PATH_CHAR_CLASS_INVALID,
4075 /* 0x1e */ PATH_CHAR_CLASS_INVALID, /* 0x1f */ PATH_CHAR_CLASS_INVALID,
4076 /* ' ' */ PATH_CHAR_CLASS_SPACE, /* '!' */ PATH_CHAR_CLASS_OTHER_VALID,
4077 /* '"' */ PATH_CHAR_CLASS_DOUBLEQUOTE, /* '#' */ PATH_CHAR_CLASS_OTHER_VALID,
4078 /* '$' */ PATH_CHAR_CLASS_OTHER_VALID, /* '%' */ PATH_CHAR_CLASS_OTHER_VALID,
4079 /* '&' */ PATH_CHAR_CLASS_OTHER_VALID, /* '\'' */ PATH_CHAR_CLASS_OTHER_VALID,
4080 /* '(' */ PATH_CHAR_CLASS_OTHER_VALID, /* ')' */ PATH_CHAR_CLASS_OTHER_VALID,
4081 /* '*' */ PATH_CHAR_CLASS_ASTERIX, /* '+' */ PATH_CHAR_CLASS_OTHER_VALID,
4082 /* ',' */ PATH_CHAR_CLASS_COMMA, /* '-' */ PATH_CHAR_CLASS_OTHER_VALID,
4083 /* '.' */ PATH_CHAR_CLASS_DOT, /* '/' */ PATH_CHAR_CLASS_INVALID,
4084 /* '0' */ PATH_CHAR_CLASS_OTHER_VALID, /* '1' */ PATH_CHAR_CLASS_OTHER_VALID,
4085 /* '2' */ PATH_CHAR_CLASS_OTHER_VALID, /* '3' */ PATH_CHAR_CLASS_OTHER_VALID,
4086 /* '4' */ PATH_CHAR_CLASS_OTHER_VALID, /* '5' */ PATH_CHAR_CLASS_OTHER_VALID,
4087 /* '6' */ PATH_CHAR_CLASS_OTHER_VALID, /* '7' */ PATH_CHAR_CLASS_OTHER_VALID,
4088 /* '8' */ PATH_CHAR_CLASS_OTHER_VALID, /* '9' */ PATH_CHAR_CLASS_OTHER_VALID,
4089 /* ':' */ PATH_CHAR_CLASS_COLON, /* ';' */ PATH_CHAR_CLASS_SEMICOLON,
4090 /* '<' */ PATH_CHAR_CLASS_INVALID, /* '=' */ PATH_CHAR_CLASS_OTHER_VALID,
4091 /* '>' */ PATH_CHAR_CLASS_INVALID, /* '?' */ PATH_CHAR_CLASS_LETTER,
4092 /* '@' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'A' */ PATH_CHAR_CLASS_ANY,
4093 /* 'B' */ PATH_CHAR_CLASS_ANY, /* 'C' */ PATH_CHAR_CLASS_ANY,
4094 /* 'D' */ PATH_CHAR_CLASS_ANY, /* 'E' */ PATH_CHAR_CLASS_ANY,
4095 /* 'F' */ PATH_CHAR_CLASS_ANY, /* 'G' */ PATH_CHAR_CLASS_ANY,
4096 /* 'H' */ PATH_CHAR_CLASS_ANY, /* 'I' */ PATH_CHAR_CLASS_ANY,
4097 /* 'J' */ PATH_CHAR_CLASS_ANY, /* 'K' */ PATH_CHAR_CLASS_ANY,
4098 /* 'L' */ PATH_CHAR_CLASS_ANY, /* 'M' */ PATH_CHAR_CLASS_ANY,
4099 /* 'N' */ PATH_CHAR_CLASS_ANY, /* 'O' */ PATH_CHAR_CLASS_ANY,
4100 /* 'P' */ PATH_CHAR_CLASS_ANY, /* 'Q' */ PATH_CHAR_CLASS_ANY,
4101 /* 'R' */ PATH_CHAR_CLASS_ANY, /* 'S' */ PATH_CHAR_CLASS_ANY,
4102 /* 'T' */ PATH_CHAR_CLASS_ANY, /* 'U' */ PATH_CHAR_CLASS_ANY,
4103 /* 'V' */ PATH_CHAR_CLASS_ANY, /* 'W' */ PATH_CHAR_CLASS_ANY,
4104 /* 'X' */ PATH_CHAR_CLASS_ANY, /* 'Y' */ PATH_CHAR_CLASS_ANY,
4105 /* 'Z' */ PATH_CHAR_CLASS_ANY, /* '[' */ PATH_CHAR_CLASS_OTHER_VALID,
4106 /* '\\' */ PATH_CHAR_CLASS_BACKSLASH, /* ']' */ PATH_CHAR_CLASS_OTHER_VALID,
4107 /* '^' */ PATH_CHAR_CLASS_OTHER_VALID, /* '_' */ PATH_CHAR_CLASS_OTHER_VALID,
4108 /* '`' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'a' */ PATH_CHAR_CLASS_ANY,
4109 /* 'b' */ PATH_CHAR_CLASS_ANY, /* 'c' */ PATH_CHAR_CLASS_ANY,
4110 /* 'd' */ PATH_CHAR_CLASS_ANY, /* 'e' */ PATH_CHAR_CLASS_ANY,
4111 /* 'f' */ PATH_CHAR_CLASS_ANY, /* 'g' */ PATH_CHAR_CLASS_ANY,
4112 /* 'h' */ PATH_CHAR_CLASS_ANY, /* 'i' */ PATH_CHAR_CLASS_ANY,
4113 /* 'j' */ PATH_CHAR_CLASS_ANY, /* 'k' */ PATH_CHAR_CLASS_ANY,
4114 /* 'l' */ PATH_CHAR_CLASS_ANY, /* 'm' */ PATH_CHAR_CLASS_ANY,
4115 /* 'n' */ PATH_CHAR_CLASS_ANY, /* 'o' */ PATH_CHAR_CLASS_ANY,
4116 /* 'p' */ PATH_CHAR_CLASS_ANY, /* 'q' */ PATH_CHAR_CLASS_ANY,
4117 /* 'r' */ PATH_CHAR_CLASS_ANY, /* 's' */ PATH_CHAR_CLASS_ANY,
4118 /* 't' */ PATH_CHAR_CLASS_ANY, /* 'u' */ PATH_CHAR_CLASS_ANY,
4119 /* 'v' */ PATH_CHAR_CLASS_ANY, /* 'w' */ PATH_CHAR_CLASS_ANY,
4120 /* 'x' */ PATH_CHAR_CLASS_ANY, /* 'y' */ PATH_CHAR_CLASS_ANY,
4121 /* 'z' */ PATH_CHAR_CLASS_ANY, /* '{' */ PATH_CHAR_CLASS_OTHER_VALID,
4122 /* '|' */ PATH_CHAR_CLASS_INVALID, /* '}' */ PATH_CHAR_CLASS_OTHER_VALID,
4123 /* '~' */ PATH_CHAR_CLASS_OTHER_VALID
4126 /*************************************************************************
4127 * @ [SHLWAPI.455]
4129 * Check if an ASCII char is of a certain class
4131 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4133 if ((unsigned)c > 0x7e)
4134 return class & PATH_CHAR_CLASS_OTHER_VALID;
4136 return class & SHELL_charclass[(unsigned)c];
4139 /*************************************************************************
4140 * @ [SHLWAPI.456]
4142 * Check if a Unicode char is of a certain class
4144 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4146 if (c > 0x7e)
4147 return class & PATH_CHAR_CLASS_OTHER_VALID;
4149 return class & SHELL_charclass[c];