Release 1.7.13.
[wine/multimedia.git] / dlls / shlwapi / path.c
blob5f70bcc8e79a1b5a9472117e995b75f3b8aeda35
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 static inline WCHAR* heap_strdupAtoW(LPCSTR str)
63 WCHAR *ret = NULL;
65 if (str)
67 DWORD len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
68 ret = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
69 if (ret)
70 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
73 return ret;
76 /*************************************************************************
77 * PathAppendA [SHLWAPI.@]
79 * Append one path to another.
81 * PARAMS
82 * lpszPath [I/O] Initial part of path, and destination for output
83 * lpszAppend [I] Path to append
85 * RETURNS
86 * Success: TRUE. lpszPath contains the newly created path.
87 * Failure: FALSE, if either path is NULL, or PathCombineA() fails.
89 * NOTES
90 * lpszAppend must contain at least one backslash ('\') if not NULL.
91 * Because PathCombineA() is used to join the paths, the resulting
92 * path is also canonicalized.
94 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
96 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
98 if (lpszPath && lpszAppend)
100 if (!PathIsUNCA(lpszAppend))
101 while (*lpszAppend == '\\')
102 lpszAppend++;
103 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
104 return TRUE;
106 return FALSE;
109 /*************************************************************************
110 * PathAppendW [SHLWAPI.@]
112 * See PathAppendA.
114 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
116 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
118 if (lpszPath && lpszAppend)
120 if (!PathIsUNCW(lpszAppend))
121 while (*lpszAppend == '\\')
122 lpszAppend++;
123 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
124 return TRUE;
126 return FALSE;
129 /*************************************************************************
130 * PathCombineA [SHLWAPI.@]
132 * Combine two paths together.
134 * PARAMS
135 * lpszDest [O] Destination for combined path
136 * lpszDir [I] Directory path
137 * lpszFile [I] File path
139 * RETURNS
140 * Success: The output path
141 * Failure: NULL, if inputs are invalid.
143 * NOTES
144 * lpszDest should be at least MAX_PATH in size, and may point to the same
145 * memory location as lpszDir. The combined path is canonicalised.
147 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
149 WCHAR szDest[MAX_PATH];
150 WCHAR szDir[MAX_PATH];
151 WCHAR szFile[MAX_PATH];
152 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
154 /* Invalid parameters */
155 if (!lpszDest)
156 return NULL;
157 if (!lpszDir && !lpszFile)
159 lpszDest[0] = 0;
160 return NULL;
163 if (lpszDir)
164 MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH);
165 if (lpszFile)
166 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
168 if (PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL))
169 if (WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0))
170 return lpszDest;
172 lpszDest[0] = 0;
173 return NULL;
176 /*************************************************************************
177 * PathCombineW [SHLWAPI.@]
179 * See PathCombineA.
181 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
183 WCHAR szTemp[MAX_PATH];
184 BOOL bUseBoth = FALSE, bStrip = FALSE;
186 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
188 /* Invalid parameters */
189 if (!lpszDest)
190 return NULL;
191 if (!lpszDir && !lpszFile)
193 lpszDest[0] = 0;
194 return NULL;
197 if ((!lpszFile || !*lpszFile) && lpszDir)
199 /* Use dir only */
200 lstrcpynW(szTemp, lpszDir, MAX_PATH);
202 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
204 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
206 /* Use file only */
207 lstrcpynW(szTemp, lpszFile, MAX_PATH);
209 else
211 bUseBoth = TRUE;
212 bStrip = TRUE;
215 else
216 bUseBoth = TRUE;
218 if (bUseBoth)
220 lstrcpynW(szTemp, lpszDir, MAX_PATH);
221 if (bStrip)
223 PathStripToRootW(szTemp);
224 lpszFile++; /* Skip '\' */
226 if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
228 lpszDest[0] = 0;
229 return NULL;
231 strcatW(szTemp, lpszFile);
234 PathCanonicalizeW(lpszDest, szTemp);
235 return lpszDest;
238 /*************************************************************************
239 * PathAddBackslashA [SHLWAPI.@]
241 * Append a backslash ('\') to a path if one doesn't exist.
243 * PARAMS
244 * lpszPath [I/O] The path to append a backslash to.
246 * RETURNS
247 * Success: The position of the last backslash in the path.
248 * Failure: NULL, if lpszPath is NULL or the path is too large.
250 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
252 size_t iLen;
253 LPSTR prev = lpszPath;
255 TRACE("(%s)\n",debugstr_a(lpszPath));
257 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
258 return NULL;
260 if (iLen)
262 do {
263 lpszPath = CharNextA(prev);
264 if (*lpszPath)
265 prev = lpszPath;
266 } while (*lpszPath);
267 if (*prev != '\\')
269 *lpszPath++ = '\\';
270 *lpszPath = '\0';
273 return lpszPath;
276 /*************************************************************************
277 * PathAddBackslashW [SHLWAPI.@]
279 * See PathAddBackslashA.
281 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
283 size_t iLen;
285 TRACE("(%s)\n",debugstr_w(lpszPath));
287 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
288 return NULL;
290 if (iLen)
292 lpszPath += iLen;
293 if (lpszPath[-1] != '\\')
295 *lpszPath++ = '\\';
296 *lpszPath = '\0';
299 return lpszPath;
302 /*************************************************************************
303 * PathBuildRootA [SHLWAPI.@]
305 * Create a root drive string (e.g. "A:\") from a drive number.
307 * PARAMS
308 * lpszPath [O] Destination for the drive string
310 * RETURNS
311 * lpszPath
313 * NOTES
314 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
316 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
318 TRACE("(%p,%d)\n", lpszPath, drive);
320 if (lpszPath && drive >= 0 && drive < 26)
322 lpszPath[0] = 'A' + drive;
323 lpszPath[1] = ':';
324 lpszPath[2] = '\\';
325 lpszPath[3] = '\0';
327 return lpszPath;
330 /*************************************************************************
331 * PathBuildRootW [SHLWAPI.@]
333 * See PathBuildRootA.
335 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
337 TRACE("(%p,%d)\n", lpszPath, drive);
339 if (lpszPath && drive >= 0 && drive < 26)
341 lpszPath[0] = 'A' + drive;
342 lpszPath[1] = ':';
343 lpszPath[2] = '\\';
344 lpszPath[3] = '\0';
346 return lpszPath;
349 /*************************************************************************
350 * PathFindFileNameA [SHLWAPI.@]
352 * Locate the start of the file name in a path
354 * PARAMS
355 * lpszPath [I] Path to search
357 * RETURNS
358 * A pointer to the first character of the file name
360 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
362 LPCSTR lastSlash = lpszPath;
364 TRACE("(%s)\n",debugstr_a(lpszPath));
366 while (lpszPath && *lpszPath)
368 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
369 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
370 lastSlash = lpszPath + 1;
371 lpszPath = CharNextA(lpszPath);
373 return (LPSTR)lastSlash;
376 /*************************************************************************
377 * PathFindFileNameW [SHLWAPI.@]
379 * See PathFindFileNameA.
381 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
383 LPCWSTR lastSlash = lpszPath;
385 TRACE("(%s)\n",debugstr_w(lpszPath));
387 while (lpszPath && *lpszPath)
389 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
390 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
391 lastSlash = lpszPath + 1;
392 lpszPath++;
394 return (LPWSTR)lastSlash;
397 /*************************************************************************
398 * PathFindExtensionA [SHLWAPI.@]
400 * Locate the start of the file extension in a path
402 * PARAMS
403 * lpszPath [I] The path to search
405 * RETURNS
406 * A pointer to the first character of the extension, the end of
407 * the string if the path has no extension, or NULL If lpszPath is NULL
409 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
411 LPCSTR lastpoint = NULL;
413 TRACE("(%s)\n", debugstr_a(lpszPath));
415 if (lpszPath)
417 while (*lpszPath)
419 if (*lpszPath == '\\' || *lpszPath==' ')
420 lastpoint = NULL;
421 else if (*lpszPath == '.')
422 lastpoint = lpszPath;
423 lpszPath = CharNextA(lpszPath);
426 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
429 /*************************************************************************
430 * PathFindExtensionW [SHLWAPI.@]
432 * See PathFindExtensionA.
434 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
436 LPCWSTR lastpoint = NULL;
438 TRACE("(%s)\n", debugstr_w(lpszPath));
440 if (lpszPath)
442 while (*lpszPath)
444 if (*lpszPath == '\\' || *lpszPath==' ')
445 lastpoint = NULL;
446 else if (*lpszPath == '.')
447 lastpoint = lpszPath;
448 lpszPath++;
451 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
454 /*************************************************************************
455 * PathGetArgsA [SHLWAPI.@]
457 * Find the next argument in a string delimited by spaces.
459 * PARAMS
460 * lpszPath [I] The string to search for arguments in
462 * RETURNS
463 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
465 * NOTES
466 * Spaces in quoted strings are ignored as delimiters.
468 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
470 BOOL bSeenQuote = FALSE;
472 TRACE("(%s)\n",debugstr_a(lpszPath));
474 if (lpszPath)
476 while (*lpszPath)
478 if ((*lpszPath==' ') && !bSeenQuote)
479 return (LPSTR)lpszPath + 1;
480 if (*lpszPath == '"')
481 bSeenQuote = !bSeenQuote;
482 lpszPath = CharNextA(lpszPath);
485 return (LPSTR)lpszPath;
488 /*************************************************************************
489 * PathGetArgsW [SHLWAPI.@]
491 * See PathGetArgsA.
493 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
495 BOOL bSeenQuote = FALSE;
497 TRACE("(%s)\n",debugstr_w(lpszPath));
499 if (lpszPath)
501 while (*lpszPath)
503 if ((*lpszPath==' ') && !bSeenQuote)
504 return (LPWSTR)lpszPath + 1;
505 if (*lpszPath == '"')
506 bSeenQuote = !bSeenQuote;
507 lpszPath++;
510 return (LPWSTR)lpszPath;
513 /*************************************************************************
514 * PathGetDriveNumberA [SHLWAPI.@]
516 * Return the drive number from a path
518 * PARAMS
519 * lpszPath [I] Path to get the drive number from
521 * RETURNS
522 * Success: The drive number corresponding to the drive in the path
523 * Failure: -1, if lpszPath contains no valid drive
525 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
527 TRACE ("(%s)\n",debugstr_a(lpszPath));
529 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
530 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
531 return tolower(*lpszPath) - 'a';
532 return -1;
535 /*************************************************************************
536 * PathGetDriveNumberW [SHLWAPI.@]
538 * See PathGetDriveNumberA.
540 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
542 TRACE ("(%s)\n",debugstr_w(lpszPath));
544 if (lpszPath)
546 WCHAR tl = tolowerW(lpszPath[0]);
547 if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
548 return tl - 'a';
550 return -1;
553 /*************************************************************************
554 * PathRemoveFileSpecA [SHLWAPI.@]
556 * Remove the file specification from a path.
558 * PARAMS
559 * lpszPath [I/O] Path to remove the file spec from
561 * RETURNS
562 * TRUE If the path was valid and modified
563 * FALSE Otherwise
565 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
567 LPSTR lpszFileSpec = lpszPath;
568 BOOL bModified = FALSE;
570 TRACE("(%s)\n",debugstr_a(lpszPath));
572 if(lpszPath)
574 /* Skip directory or UNC path */
575 if (*lpszPath == '\\')
576 lpszFileSpec = ++lpszPath;
577 if (*lpszPath == '\\')
578 lpszFileSpec = ++lpszPath;
580 while (*lpszPath)
582 if(*lpszPath == '\\')
583 lpszFileSpec = lpszPath; /* Skip dir */
584 else if(*lpszPath == ':')
586 lpszFileSpec = ++lpszPath; /* Skip drive */
587 if (*lpszPath == '\\')
588 lpszFileSpec++;
590 if (!(lpszPath = CharNextA(lpszPath)))
591 break;
594 if (*lpszFileSpec)
596 *lpszFileSpec = '\0';
597 bModified = TRUE;
600 return bModified;
603 /*************************************************************************
604 * PathRemoveFileSpecW [SHLWAPI.@]
606 * See PathRemoveFileSpecA.
608 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
610 LPWSTR lpszFileSpec = lpszPath;
611 BOOL bModified = FALSE;
613 TRACE("(%s)\n",debugstr_w(lpszPath));
615 if(lpszPath)
617 /* Skip directory or UNC path */
618 if (*lpszPath == '\\')
619 lpszFileSpec = ++lpszPath;
620 if (*lpszPath == '\\')
621 lpszFileSpec = ++lpszPath;
623 while (*lpszPath)
625 if(*lpszPath == '\\')
626 lpszFileSpec = lpszPath; /* Skip dir */
627 else if(*lpszPath == ':')
629 lpszFileSpec = ++lpszPath; /* Skip drive */
630 if (*lpszPath == '\\')
631 lpszFileSpec++;
633 lpszPath++;
636 if (*lpszFileSpec)
638 *lpszFileSpec = '\0';
639 bModified = TRUE;
642 return bModified;
645 /*************************************************************************
646 * PathStripPathA [SHLWAPI.@]
648 * Remove the initial path from the beginning of a filename
650 * PARAMS
651 * lpszPath [I/O] Path to remove the initial path from
653 * RETURNS
654 * Nothing.
656 void WINAPI PathStripPathA(LPSTR lpszPath)
658 TRACE("(%s)\n", debugstr_a(lpszPath));
660 if (lpszPath)
662 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
663 if(lpszFileName)
664 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
668 /*************************************************************************
669 * PathStripPathW [SHLWAPI.@]
671 * See PathStripPathA.
673 void WINAPI PathStripPathW(LPWSTR lpszPath)
675 LPWSTR lpszFileName;
677 TRACE("(%s)\n", debugstr_w(lpszPath));
678 lpszFileName = PathFindFileNameW(lpszPath);
679 if(lpszFileName)
680 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
683 /*************************************************************************
684 * PathStripToRootA [SHLWAPI.@]
686 * Reduce a path to its root.
688 * PARAMS
689 * lpszPath [I/O] the path to reduce
691 * RETURNS
692 * Success: TRUE if the stripped path is a root path
693 * Failure: FALSE if the path cannot be stripped or is NULL
695 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
697 TRACE("(%s)\n", debugstr_a(lpszPath));
699 if (!lpszPath)
700 return FALSE;
701 while(!PathIsRootA(lpszPath))
702 if (!PathRemoveFileSpecA(lpszPath))
703 return FALSE;
704 return TRUE;
707 /*************************************************************************
708 * PathStripToRootW [SHLWAPI.@]
710 * See PathStripToRootA.
712 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
714 TRACE("(%s)\n", debugstr_w(lpszPath));
716 if (!lpszPath)
717 return FALSE;
718 while(!PathIsRootW(lpszPath))
719 if (!PathRemoveFileSpecW(lpszPath))
720 return FALSE;
721 return TRUE;
724 /*************************************************************************
725 * PathRemoveArgsA [SHLWAPI.@]
727 * Strip space separated arguments from a path.
729 * PARAMS
730 * lpszPath [I/O] Path to remove arguments from
732 * RETURNS
733 * Nothing.
735 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
737 TRACE("(%s)\n",debugstr_a(lpszPath));
739 if(lpszPath)
741 LPSTR lpszArgs = PathGetArgsA(lpszPath);
742 if (*lpszArgs)
743 lpszArgs[-1] = '\0';
744 else
746 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
747 if(*lpszLastChar == ' ')
748 *lpszLastChar = '\0';
753 /*************************************************************************
754 * PathRemoveArgsW [SHLWAPI.@]
756 * See PathRemoveArgsA.
758 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
760 TRACE("(%s)\n",debugstr_w(lpszPath));
762 if(lpszPath)
764 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
765 if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
766 lpszArgs[-1] = '\0';
770 /*************************************************************************
771 * PathRemoveExtensionA [SHLWAPI.@]
773 * Remove the file extension from a path
775 * PARAMS
776 * lpszPath [I/O] Path to remove the extension from
778 * NOTES
779 * The NUL terminator must be written only if extension exists
780 * and if the pointed character is not already NUL.
782 * RETURNS
783 * Nothing.
785 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
787 TRACE("(%s)\n", debugstr_a(lpszPath));
789 if (lpszPath)
791 lpszPath = PathFindExtensionA(lpszPath);
792 if (lpszPath && *lpszPath != '\0')
793 *lpszPath = '\0';
797 /*************************************************************************
798 * PathRemoveExtensionW [SHLWAPI.@]
800 * See PathRemoveExtensionA.
802 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
804 TRACE("(%s)\n", debugstr_w(lpszPath));
806 if (lpszPath)
808 lpszPath = PathFindExtensionW(lpszPath);
809 if (lpszPath && *lpszPath != '\0')
810 *lpszPath = '\0';
814 /*************************************************************************
815 * PathRemoveBackslashA [SHLWAPI.@]
817 * Remove a trailing backslash from a path.
819 * PARAMS
820 * lpszPath [I/O] Path to remove backslash from
822 * RETURNS
823 * Success: A pointer to the end of the path
824 * Failure: NULL, if lpszPath is NULL
826 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
828 LPSTR szTemp = NULL;
830 TRACE("(%s)\n", debugstr_a(lpszPath));
832 if(lpszPath)
834 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
835 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
836 *szTemp = '\0';
838 return szTemp;
841 /*************************************************************************
842 * PathRemoveBackslashW [SHLWAPI.@]
844 * See PathRemoveBackslashA.
846 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
848 LPWSTR szTemp = NULL;
850 TRACE("(%s)\n", debugstr_w(lpszPath));
852 if(lpszPath)
854 szTemp = lpszPath + strlenW(lpszPath);
855 if (szTemp > lpszPath) szTemp--;
856 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
857 *szTemp = '\0';
859 return szTemp;
862 /*************************************************************************
863 * PathRemoveBlanksA [SHLWAPI.@]
865 * Remove Spaces from the start and end of a path.
867 * PARAMS
868 * lpszPath [I/O] Path to strip blanks from
870 * RETURNS
871 * Nothing.
873 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
875 TRACE("(%s)\n", debugstr_a(lpszPath));
877 if(lpszPath && *lpszPath)
879 LPSTR start = lpszPath;
881 while (*lpszPath == ' ')
882 lpszPath = CharNextA(lpszPath);
884 while(*lpszPath)
885 *start++ = *lpszPath++;
887 if (start != lpszPath)
888 while (start[-1] == ' ')
889 start--;
890 *start = '\0';
894 /*************************************************************************
895 * PathRemoveBlanksW [SHLWAPI.@]
897 * See PathRemoveBlanksA.
899 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
901 TRACE("(%s)\n", debugstr_w(lpszPath));
903 if(lpszPath && *lpszPath)
905 LPWSTR start = lpszPath;
907 while (*lpszPath == ' ')
908 lpszPath++;
910 while(*lpszPath)
911 *start++ = *lpszPath++;
913 if (start != lpszPath)
914 while (start[-1] == ' ')
915 start--;
916 *start = '\0';
920 /*************************************************************************
921 * PathQuoteSpacesA [SHLWAPI.@]
923 * Surround a path containing spaces in quotes.
925 * PARAMS
926 * lpszPath [I/O] Path to quote
928 * RETURNS
929 * Nothing.
931 * NOTES
932 * The path is not changed if it is invalid or has no spaces.
934 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
936 TRACE("(%s)\n", debugstr_a(lpszPath));
938 if(lpszPath && StrChrA(lpszPath,' '))
940 size_t iLen = strlen(lpszPath) + 1;
942 if (iLen + 2 < MAX_PATH)
944 memmove(lpszPath + 1, lpszPath, iLen);
945 lpszPath[0] = '"';
946 lpszPath[iLen] = '"';
947 lpszPath[iLen + 1] = '\0';
952 /*************************************************************************
953 * PathQuoteSpacesW [SHLWAPI.@]
955 * See PathQuoteSpacesA.
957 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
959 TRACE("(%s)\n", debugstr_w(lpszPath));
961 if(lpszPath && StrChrW(lpszPath,' '))
963 int iLen = strlenW(lpszPath) + 1;
965 if (iLen + 2 < MAX_PATH)
967 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
968 lpszPath[0] = '"';
969 lpszPath[iLen] = '"';
970 lpszPath[iLen + 1] = '\0';
975 /*************************************************************************
976 * PathUnquoteSpacesA [SHLWAPI.@]
978 * Remove quotes ("") from around a path, if present.
980 * PARAMS
981 * lpszPath [I/O] Path to strip quotes from
983 * RETURNS
984 * Nothing
986 * NOTES
987 * If the path contains a single quote only, an empty string will result.
988 * Otherwise quotes are only removed if they appear at the start and end
989 * of the path.
991 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
993 TRACE("(%s)\n", debugstr_a(lpszPath));
995 if (lpszPath && *lpszPath == '"')
997 DWORD dwLen = strlen(lpszPath) - 1;
999 if (lpszPath[dwLen] == '"')
1001 lpszPath[dwLen] = '\0';
1002 for (; *lpszPath; lpszPath++)
1003 *lpszPath = lpszPath[1];
1008 /*************************************************************************
1009 * PathUnquoteSpacesW [SHLWAPI.@]
1011 * See PathUnquoteSpacesA.
1013 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
1015 TRACE("(%s)\n", debugstr_w(lpszPath));
1017 if (lpszPath && *lpszPath == '"')
1019 DWORD dwLen = strlenW(lpszPath) - 1;
1021 if (lpszPath[dwLen] == '"')
1023 lpszPath[dwLen] = '\0';
1024 for (; *lpszPath; lpszPath++)
1025 *lpszPath = lpszPath[1];
1030 /*************************************************************************
1031 * PathParseIconLocationA [SHLWAPI.@]
1033 * Parse the location of an icon from a path.
1035 * PARAMS
1036 * lpszPath [I/O] The path to parse the icon location from.
1038 * RETURNS
1039 * Success: The number of the icon
1040 * Failure: 0 if the path does not contain an icon location or is NULL
1042 * NOTES
1043 * The path has surrounding quotes and spaces removed regardless
1044 * of whether the call succeeds or not.
1046 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1048 int iRet = 0;
1049 LPSTR lpszComma;
1051 TRACE("(%s)\n", debugstr_a(lpszPath));
1053 if (lpszPath)
1055 if ((lpszComma = strchr(lpszPath, ',')))
1057 *lpszComma++ = '\0';
1058 iRet = StrToIntA(lpszComma);
1060 PathUnquoteSpacesA(lpszPath);
1061 PathRemoveBlanksA(lpszPath);
1063 return iRet;
1066 /*************************************************************************
1067 * PathParseIconLocationW [SHLWAPI.@]
1069 * See PathParseIconLocationA.
1071 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1073 int iRet = 0;
1074 LPWSTR lpszComma;
1076 TRACE("(%s)\n", debugstr_w(lpszPath));
1078 if (lpszPath)
1080 if ((lpszComma = StrChrW(lpszPath, ',')))
1082 *lpszComma++ = '\0';
1083 iRet = StrToIntW(lpszComma);
1085 PathUnquoteSpacesW(lpszPath);
1086 PathRemoveBlanksW(lpszPath);
1088 return iRet;
1091 /*************************************************************************
1092 * @ [SHLWAPI.4]
1094 * Unicode version of PathFileExistsDefExtA.
1096 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1098 static const WCHAR pszExts[][5] = { { '.', 'p', 'i', 'f', 0},
1099 { '.', 'c', 'o', 'm', 0},
1100 { '.', 'e', 'x', 'e', 0},
1101 { '.', 'b', 'a', 't', 0},
1102 { '.', 'l', 'n', 'k', 0},
1103 { '.', 'c', 'm', 'd', 0},
1104 { 0, 0, 0, 0, 0} };
1106 TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1108 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1109 return FALSE;
1111 if (dwWhich)
1113 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1114 if (!*szExt || dwWhich & 0x40)
1116 size_t iChoose = 0;
1117 int iLen = lstrlenW(lpszPath);
1118 if (iLen > (MAX_PATH - 5))
1119 return FALSE;
1120 while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1122 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1123 if (PathFileExistsW(lpszPath))
1124 return TRUE;
1125 iChoose++;
1126 dwWhich >>= 1;
1128 *(lpszPath + iLen) = (WCHAR)'\0';
1129 return FALSE;
1132 return PathFileExistsW(lpszPath);
1135 /*************************************************************************
1136 * @ [SHLWAPI.3]
1138 * Determine if a file exists locally and is of an executable type.
1140 * PARAMS
1141 * lpszPath [I/O] File to search for
1142 * dwWhich [I] Type of executable to search for
1144 * RETURNS
1145 * TRUE If the file was found. lpszPath contains the file name.
1146 * FALSE Otherwise.
1148 * NOTES
1149 * lpszPath is modified in place and must be at least MAX_PATH in length.
1150 * If the function returns FALSE, the path is modified to its original state.
1151 * If the given path contains an extension or dwWhich is 0, executable
1152 * extensions are not checked.
1154 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1155 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1156 * their own developers exclusive use. Monopoly, anyone?
1158 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1160 BOOL bRet = FALSE;
1162 TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1164 if (lpszPath)
1166 WCHAR szPath[MAX_PATH];
1167 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1168 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1169 if (bRet)
1170 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1172 return bRet;
1175 /*************************************************************************
1176 * SHLWAPI_PathFindInOtherDirs
1178 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1180 static BOOL SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1182 static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1183 static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1184 DWORD dwLenPATH;
1185 LPCWSTR lpszCurr;
1186 WCHAR *lpszPATH;
1187 WCHAR buff[MAX_PATH];
1189 TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1191 /* Try system directories */
1192 GetSystemDirectoryW(buff, MAX_PATH);
1193 if (!PathAppendW(buff, lpszFile))
1194 return FALSE;
1195 if (PathFileExistsDefExtW(buff, dwWhich))
1197 strcpyW(lpszFile, buff);
1198 return TRUE;
1200 GetWindowsDirectoryW(buff, MAX_PATH);
1201 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1202 return FALSE;
1203 if (PathFileExistsDefExtW(buff, dwWhich))
1205 strcpyW(lpszFile, buff);
1206 return TRUE;
1208 GetWindowsDirectoryW(buff, MAX_PATH);
1209 if (!PathAppendW(buff, lpszFile))
1210 return FALSE;
1211 if (PathFileExistsDefExtW(buff, dwWhich))
1213 strcpyW(lpszFile, buff);
1214 return TRUE;
1216 /* Try dirs listed in %PATH% */
1217 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1219 if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1220 return FALSE;
1222 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1223 lpszCurr = lpszPATH;
1224 while (lpszCurr)
1226 LPCWSTR lpszEnd = lpszCurr;
1227 LPWSTR pBuff = buff;
1229 while (*lpszEnd == ' ')
1230 lpszEnd++;
1231 while (*lpszEnd && *lpszEnd != ';')
1232 *pBuff++ = *lpszEnd++;
1233 *pBuff = '\0';
1235 if (*lpszEnd)
1236 lpszCurr = lpszEnd + 1;
1237 else
1238 lpszCurr = NULL; /* Last Path, terminate after this */
1240 if (!PathAppendW(buff, lpszFile))
1242 HeapFree(GetProcessHeap(), 0, lpszPATH);
1243 return FALSE;
1245 if (PathFileExistsDefExtW(buff, dwWhich))
1247 strcpyW(lpszFile, buff);
1248 HeapFree(GetProcessHeap(), 0, lpszPATH);
1249 return TRUE;
1252 HeapFree(GetProcessHeap(), 0, lpszPATH);
1253 return FALSE;
1256 /*************************************************************************
1257 * @ [SHLWAPI.5]
1259 * Search a range of paths for a specific type of executable.
1261 * PARAMS
1262 * lpszFile [I/O] File to search for
1263 * lppszOtherDirs [I] Other directories to look in
1264 * dwWhich [I] Type of executable to search for
1266 * RETURNS
1267 * Success: TRUE. The path to the executable is stored in lpszFile.
1268 * Failure: FALSE. The path to the executable is unchanged.
1270 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1272 WCHAR szFile[MAX_PATH];
1273 WCHAR buff[MAX_PATH];
1275 TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1277 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1278 return FALSE;
1280 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1282 /* Search provided directories first */
1283 if (lppszOtherDirs && *lppszOtherDirs)
1285 WCHAR szOther[MAX_PATH];
1286 LPCSTR *lpszOtherPath = lppszOtherDirs;
1288 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1290 MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1291 PathCombineW(buff, szOther, szFile);
1292 if (PathFileExistsDefExtW(buff, dwWhich))
1294 WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1295 return TRUE;
1297 lpszOtherPath++;
1300 /* Not found, try system and path dirs */
1301 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1303 WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1304 return TRUE;
1306 return FALSE;
1309 /*************************************************************************
1310 * @ [SHLWAPI.6]
1312 * Unicode version of PathFindOnPathExA.
1314 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1316 WCHAR buff[MAX_PATH];
1318 TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1320 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1321 return FALSE;
1323 /* Search provided directories first */
1324 if (lppszOtherDirs && *lppszOtherDirs)
1326 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1327 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1329 PathCombineW(buff, *lpszOtherPath, lpszFile);
1330 if (PathFileExistsDefExtW(buff, dwWhich))
1332 strcpyW(lpszFile, buff);
1333 return TRUE;
1335 lpszOtherPath++;
1338 /* Not found, try system and path dirs */
1339 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1342 /*************************************************************************
1343 * PathFindOnPathA [SHLWAPI.@]
1345 * Search a range of paths for an executable.
1347 * PARAMS
1348 * lpszFile [I/O] File to search for
1349 * lppszOtherDirs [I] Other directories to look in
1351 * RETURNS
1352 * Success: TRUE. The path to the executable is stored in lpszFile.
1353 * Failure: FALSE. The path to the executable is unchanged.
1355 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1357 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1358 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1361 /*************************************************************************
1362 * PathFindOnPathW [SHLWAPI.@]
1364 * See PathFindOnPathA.
1366 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1368 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1369 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1372 /*************************************************************************
1373 * PathCompactPathExA [SHLWAPI.@]
1375 * Compact a path into a given number of characters.
1377 * PARAMS
1378 * lpszDest [O] Destination for compacted path
1379 * lpszPath [I] Source path
1380 * cchMax [I] Maximum size of compacted path
1381 * dwFlags [I] Reserved
1383 * RETURNS
1384 * Success: TRUE. The compacted path is written to lpszDest.
1385 * Failure: FALSE. lpszPath is undefined.
1387 * NOTES
1388 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1390 * The Win32 version of this function contains a bug: When cchMax == 7,
1391 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1392 * implementation.
1394 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1395 * because Win32 will insert a "\" in lpszDest, even if one is
1396 * not present in the original path.
1398 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1399 UINT cchMax, DWORD dwFlags)
1401 BOOL bRet = FALSE;
1403 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1405 if (lpszPath && lpszDest)
1407 WCHAR szPath[MAX_PATH];
1408 WCHAR szDest[MAX_PATH];
1410 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1411 szDest[0] = '\0';
1412 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1413 WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1415 return bRet;
1418 /*************************************************************************
1419 * PathCompactPathExW [SHLWAPI.@]
1421 * See PathCompactPathExA.
1423 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1424 UINT cchMax, DWORD dwFlags)
1426 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1427 LPCWSTR lpszFile;
1428 DWORD dwLen, dwFileLen = 0;
1430 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1432 if (!lpszPath)
1433 return FALSE;
1435 if (!lpszDest)
1437 WARN("Invalid lpszDest would crash under Win32!\n");
1438 return FALSE;
1441 *lpszDest = '\0';
1443 if (cchMax < 2)
1444 return TRUE;
1446 dwLen = strlenW(lpszPath) + 1;
1448 if (dwLen < cchMax)
1450 /* Don't need to compact */
1451 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1452 return TRUE;
1455 /* Path must be compacted to fit into lpszDest */
1456 lpszFile = PathFindFileNameW(lpszPath);
1457 dwFileLen = lpszPath + dwLen - lpszFile;
1459 if (dwFileLen == dwLen)
1461 /* No root in psth */
1462 if (cchMax <= 4)
1464 while (--cchMax > 0) /* No room left for anything but ellipses */
1465 *lpszDest++ = '.';
1466 *lpszDest = '\0';
1467 return TRUE;
1469 /* Compact the file name with ellipses at the end */
1470 cchMax -= 4;
1471 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1472 strcpyW(lpszDest + cchMax, szEllipses);
1473 return TRUE;
1475 /* We have a root in the path */
1476 lpszFile--; /* Start compacted filename with the path separator */
1477 dwFileLen++;
1479 if (dwFileLen + 3 > cchMax)
1481 /* Compact the file name */
1482 if (cchMax <= 4)
1484 while (--cchMax > 0) /* No room left for anything but ellipses */
1485 *lpszDest++ = '.';
1486 *lpszDest = '\0';
1487 return TRUE;
1489 strcpyW(lpszDest, szEllipses);
1490 lpszDest += 3;
1491 cchMax -= 4;
1492 *lpszDest++ = *lpszFile++;
1493 if (cchMax <= 4)
1495 while (--cchMax > 0) /* No room left for anything but ellipses */
1496 *lpszDest++ = '.';
1497 *lpszDest = '\0';
1498 return TRUE;
1500 cchMax -= 4;
1501 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1502 strcpyW(lpszDest + cchMax, szEllipses);
1503 return TRUE;
1506 /* Only the root needs to be Compacted */
1507 dwLen = cchMax - dwFileLen - 3;
1508 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1509 strcpyW(lpszDest + dwLen, szEllipses);
1510 strcpyW(lpszDest + dwLen + 3, lpszFile);
1511 return TRUE;
1514 /*************************************************************************
1515 * PathIsRelativeA [SHLWAPI.@]
1517 * Determine if a path is a relative path.
1519 * PARAMS
1520 * lpszPath [I] Path to check
1522 * RETURNS
1523 * TRUE: The path is relative, or is invalid.
1524 * FALSE: The path is not relative.
1526 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1528 TRACE("(%s)\n",debugstr_a(lpszPath));
1530 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1531 return TRUE;
1532 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1533 return FALSE;
1534 return TRUE;
1537 /*************************************************************************
1538 * PathIsRelativeW [SHLWAPI.@]
1540 * See PathIsRelativeA.
1542 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1544 TRACE("(%s)\n",debugstr_w(lpszPath));
1546 if (!lpszPath || !*lpszPath)
1547 return TRUE;
1548 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1549 return FALSE;
1550 return TRUE;
1553 /*************************************************************************
1554 * PathIsRootA [SHLWAPI.@]
1556 * Determine if a path is a root path.
1558 * PARAMS
1559 * lpszPath [I] Path to check
1561 * RETURNS
1562 * TRUE If lpszPath is valid and a root path,
1563 * FALSE Otherwise
1565 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1567 TRACE("(%s)\n", debugstr_a(lpszPath));
1569 if (lpszPath && *lpszPath)
1571 if (*lpszPath == '\\')
1573 if (!lpszPath[1])
1574 return TRUE; /* \ */
1575 else if (lpszPath[1]=='\\')
1577 BOOL bSeenSlash = FALSE;
1578 lpszPath += 2;
1580 /* Check for UNC root path */
1581 while (*lpszPath)
1583 if (*lpszPath == '\\')
1585 if (bSeenSlash)
1586 return FALSE;
1587 bSeenSlash = TRUE;
1589 lpszPath = CharNextA(lpszPath);
1591 return TRUE;
1594 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1595 return TRUE; /* X:\ */
1597 return FALSE;
1600 /*************************************************************************
1601 * PathIsRootW [SHLWAPI.@]
1603 * See PathIsRootA.
1605 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1607 TRACE("(%s)\n", debugstr_w(lpszPath));
1609 if (lpszPath && *lpszPath)
1611 if (*lpszPath == '\\')
1613 if (!lpszPath[1])
1614 return TRUE; /* \ */
1615 else if (lpszPath[1]=='\\')
1617 BOOL bSeenSlash = FALSE;
1618 lpszPath += 2;
1620 /* Check for UNC root path */
1621 while (*lpszPath)
1623 if (*lpszPath == '\\')
1625 if (bSeenSlash)
1626 return FALSE;
1627 bSeenSlash = TRUE;
1629 lpszPath++;
1631 return TRUE;
1634 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1635 return TRUE; /* X:\ */
1637 return FALSE;
1640 /*************************************************************************
1641 * PathIsDirectoryA [SHLWAPI.@]
1643 * Determine if a path is a valid directory
1645 * PARAMS
1646 * lpszPath [I] Path to check.
1648 * RETURNS
1649 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1650 * FALSE if lpszPath is invalid or not a directory.
1652 * NOTES
1653 * Although this function is prototyped as returning a BOOL, it returns
1654 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1656 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1657 *| ...
1659 * will always fail.
1661 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1663 DWORD dwAttr;
1665 TRACE("(%s)\n", debugstr_a(lpszPath));
1667 if (!lpszPath || PathIsUNCServerA(lpszPath))
1668 return FALSE;
1670 if (PathIsUNCServerShareA(lpszPath))
1672 FIXME("UNC Server Share not yet supported - FAILING\n");
1673 return FALSE;
1676 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1677 return FALSE;
1678 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1681 /*************************************************************************
1682 * PathIsDirectoryW [SHLWAPI.@]
1684 * See PathIsDirectoryA.
1686 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1688 DWORD dwAttr;
1690 TRACE("(%s)\n", debugstr_w(lpszPath));
1692 if (!lpszPath || PathIsUNCServerW(lpszPath))
1693 return FALSE;
1695 if (PathIsUNCServerShareW(lpszPath))
1697 FIXME("UNC Server Share not yet supported - FAILING\n");
1698 return FALSE;
1701 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1702 return FALSE;
1703 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1706 /*************************************************************************
1707 * PathFileExistsA [SHLWAPI.@]
1709 * Determine if a file exists.
1711 * PARAMS
1712 * lpszPath [I] Path to check
1714 * RETURNS
1715 * TRUE If the file exists and is readable
1716 * FALSE Otherwise
1718 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1720 UINT iPrevErrMode;
1721 DWORD dwAttr;
1723 TRACE("(%s)\n",debugstr_a(lpszPath));
1725 if (!lpszPath)
1726 return FALSE;
1728 /* Prevent a dialog box if path is on a disk that has been ejected. */
1729 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1730 dwAttr = GetFileAttributesA(lpszPath);
1731 SetErrorMode(iPrevErrMode);
1732 return dwAttr != INVALID_FILE_ATTRIBUTES;
1735 /*************************************************************************
1736 * PathFileExistsW [SHLWAPI.@]
1738 * See PathFileExistsA.
1740 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1742 UINT iPrevErrMode;
1743 DWORD dwAttr;
1745 TRACE("(%s)\n",debugstr_w(lpszPath));
1747 if (!lpszPath)
1748 return FALSE;
1750 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1751 dwAttr = GetFileAttributesW(lpszPath);
1752 SetErrorMode(iPrevErrMode);
1753 return dwAttr != INVALID_FILE_ATTRIBUTES;
1756 /*************************************************************************
1757 * PathFileExistsAndAttributesA [SHLWAPI.445]
1759 * Determine if a file exists.
1761 * PARAMS
1762 * lpszPath [I] Path to check
1763 * dwAttr [O] attributes of file
1765 * RETURNS
1766 * TRUE If the file exists and is readable
1767 * FALSE Otherwise
1769 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1771 UINT iPrevErrMode;
1772 DWORD dwVal = 0;
1774 TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1776 if (dwAttr)
1777 *dwAttr = INVALID_FILE_ATTRIBUTES;
1779 if (!lpszPath)
1780 return FALSE;
1782 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1783 dwVal = GetFileAttributesA(lpszPath);
1784 SetErrorMode(iPrevErrMode);
1785 if (dwAttr)
1786 *dwAttr = dwVal;
1787 return (dwVal != INVALID_FILE_ATTRIBUTES);
1790 /*************************************************************************
1791 * PathFileExistsAndAttributesW [SHLWAPI.446]
1793 * See PathFileExistsA.
1795 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1797 UINT iPrevErrMode;
1798 DWORD dwVal;
1800 TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1802 if (!lpszPath)
1803 return FALSE;
1805 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1806 dwVal = GetFileAttributesW(lpszPath);
1807 SetErrorMode(iPrevErrMode);
1808 if (dwAttr)
1809 *dwAttr = dwVal;
1810 return (dwVal != INVALID_FILE_ATTRIBUTES);
1813 /*************************************************************************
1814 * PathMatchSingleMaskA [internal]
1816 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1818 while (*name && *mask && *mask!=';')
1820 if (*mask == '*')
1824 if (PathMatchSingleMaskA(name,mask+1))
1825 return TRUE; /* try substrings */
1826 } while (*name++);
1827 return FALSE;
1830 if (toupper(*mask) != toupper(*name) && *mask != '?')
1831 return FALSE;
1833 name = CharNextA(name);
1834 mask = CharNextA(mask);
1837 if (!*name)
1839 while (*mask == '*')
1840 mask++;
1841 if (!*mask || *mask == ';')
1842 return TRUE;
1844 return FALSE;
1847 /*************************************************************************
1848 * PathMatchSingleMaskW [internal]
1850 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1852 while (*name && *mask && *mask != ';')
1854 if (*mask == '*')
1858 if (PathMatchSingleMaskW(name,mask+1))
1859 return TRUE; /* try substrings */
1860 } while (*name++);
1861 return FALSE;
1864 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1865 return FALSE;
1867 name++;
1868 mask++;
1870 if (!*name)
1872 while (*mask == '*')
1873 mask++;
1874 if (!*mask || *mask == ';')
1875 return TRUE;
1877 return FALSE;
1880 /*************************************************************************
1881 * PathMatchSpecA [SHLWAPI.@]
1883 * Determine if a path matches one or more search masks.
1885 * PARAMS
1886 * lpszPath [I] Path to check
1887 * lpszMask [I] Search mask(s)
1889 * RETURNS
1890 * TRUE If lpszPath is valid and is matched
1891 * FALSE Otherwise
1893 * NOTES
1894 * Multiple search masks may be given if they are separated by ";". The
1895 * pattern "*.*" is treated specially in that it matches all paths (for
1896 * backwards compatibility with DOS).
1898 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1900 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1902 if (!lstrcmpA(lpszMask, "*.*"))
1903 return TRUE; /* Matches every path */
1905 while (*lpszMask)
1907 while (*lpszMask == ' ')
1908 lpszMask++; /* Eat leading spaces */
1910 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1911 return TRUE; /* Matches the current mask */
1913 while (*lpszMask && *lpszMask != ';')
1914 lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1916 if (*lpszMask == ';')
1917 lpszMask++;
1919 return FALSE;
1922 /*************************************************************************
1923 * PathMatchSpecW [SHLWAPI.@]
1925 * See PathMatchSpecA.
1927 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1929 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1931 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1933 if (!lstrcmpW(lpszMask, szStarDotStar))
1934 return TRUE; /* Matches every path */
1936 while (*lpszMask)
1938 while (*lpszMask == ' ')
1939 lpszMask++; /* Eat leading spaces */
1941 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1942 return TRUE; /* Matches the current path */
1944 while (*lpszMask && *lpszMask != ';')
1945 lpszMask++; /* masks separated by ';' */
1947 if (*lpszMask == ';')
1948 lpszMask++;
1950 return FALSE;
1953 /*************************************************************************
1954 * PathIsSameRootA [SHLWAPI.@]
1956 * Determine if two paths share the same root.
1958 * PARAMS
1959 * lpszPath1 [I] Source path
1960 * lpszPath2 [I] Path to compare with
1962 * RETURNS
1963 * TRUE If both paths are valid and share the same root.
1964 * FALSE If either path is invalid or the paths do not share the same root.
1966 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1968 LPCSTR lpszStart;
1969 int dwLen;
1971 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1973 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1974 return FALSE;
1976 dwLen = PathCommonPrefixA(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 * PathIsSameRootW [SHLWAPI.@]
1985 * See PathIsSameRootA.
1987 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1989 LPCWSTR lpszStart;
1990 int dwLen;
1992 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1994 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1995 return FALSE;
1997 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1998 if (lpszStart - lpszPath1 > dwLen)
1999 return FALSE; /* Paths not common up to length of the root */
2000 return TRUE;
2003 /*************************************************************************
2004 * PathIsContentTypeA [SHLWAPI.@]
2006 * Determine if a file is of a given registered content type.
2008 * PARAMS
2009 * lpszPath [I] File to check
2010 * lpszContentType [I] Content type to check for
2012 * RETURNS
2013 * TRUE If lpszPath is a given registered content type,
2014 * FALSE Otherwise.
2016 * NOTES
2017 * This function looks up the registered content type for lpszPath. If
2018 * a content type is registered, it is compared (case insensitively) to
2019 * lpszContentType. Only if this matches does the function succeed.
2021 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
2023 LPCSTR szExt;
2024 DWORD dwDummy;
2025 char szBuff[MAX_PATH];
2027 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2029 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2030 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2031 REG_NONE, szBuff, &dwDummy) &&
2032 !strcasecmp(lpszContentType, szBuff))
2034 return TRUE;
2036 return FALSE;
2039 /*************************************************************************
2040 * PathIsContentTypeW [SHLWAPI.@]
2042 * See PathIsContentTypeA.
2044 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2046 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2047 LPCWSTR szExt;
2048 DWORD dwDummy;
2049 WCHAR szBuff[MAX_PATH];
2051 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2053 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2054 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2055 REG_NONE, szBuff, &dwDummy) &&
2056 !strcmpiW(lpszContentType, szBuff))
2058 return TRUE;
2060 return FALSE;
2063 /*************************************************************************
2064 * PathIsFileSpecA [SHLWAPI.@]
2066 * Determine if a path is a file specification.
2068 * PARAMS
2069 * lpszPath [I] Path to check
2071 * RETURNS
2072 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
2073 * FALSE Otherwise.
2075 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2077 TRACE("(%s)\n", debugstr_a(lpszPath));
2079 if (!lpszPath)
2080 return FALSE;
2082 while (*lpszPath)
2084 if (*lpszPath == '\\' || *lpszPath == ':')
2085 return FALSE;
2086 lpszPath = CharNextA(lpszPath);
2088 return TRUE;
2091 /*************************************************************************
2092 * PathIsFileSpecW [SHLWAPI.@]
2094 * See PathIsFileSpecA.
2096 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2098 TRACE("(%s)\n", debugstr_w(lpszPath));
2100 if (!lpszPath)
2101 return FALSE;
2103 while (*lpszPath)
2105 if (*lpszPath == '\\' || *lpszPath == ':')
2106 return FALSE;
2107 lpszPath++;
2109 return TRUE;
2112 /*************************************************************************
2113 * PathIsPrefixA [SHLWAPI.@]
2115 * Determine if a path is a prefix of another.
2117 * PARAMS
2118 * lpszPrefix [I] Prefix
2119 * lpszPath [I] Path to check
2121 * RETURNS
2122 * TRUE If lpszPath has lpszPrefix as its prefix,
2123 * FALSE If either path is NULL or lpszPrefix is not a prefix
2125 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2127 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2129 if (lpszPrefix && lpszPath &&
2130 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2131 return TRUE;
2132 return FALSE;
2135 /*************************************************************************
2136 * PathIsPrefixW [SHLWAPI.@]
2138 * See PathIsPrefixA.
2140 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2142 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2144 if (lpszPrefix && lpszPath &&
2145 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2146 return TRUE;
2147 return FALSE;
2150 /*************************************************************************
2151 * PathIsSystemFolderA [SHLWAPI.@]
2153 * Determine if a path or file attributes are a system folder.
2155 * PARAMS
2156 * lpszPath [I] Path to check.
2157 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2159 * RETURNS
2160 * TRUE If lpszPath or dwAttrib are a system folder.
2161 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2163 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2165 TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2167 if (lpszPath && *lpszPath)
2168 dwAttrib = GetFileAttributesA(lpszPath);
2170 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2171 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2172 return FALSE;
2173 return TRUE;
2176 /*************************************************************************
2177 * PathIsSystemFolderW [SHLWAPI.@]
2179 * See PathIsSystemFolderA.
2181 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2183 TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2185 if (lpszPath && *lpszPath)
2186 dwAttrib = GetFileAttributesW(lpszPath);
2188 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2189 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2190 return FALSE;
2191 return TRUE;
2194 /*************************************************************************
2195 * PathIsUNCA [SHLWAPI.@]
2197 * Determine if a path is in UNC format.
2199 * PARAMS
2200 * lpszPath [I] Path to check
2202 * RETURNS
2203 * TRUE: The path is UNC.
2204 * FALSE: The path is not UNC or is NULL.
2206 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2208 TRACE("(%s)\n",debugstr_a(lpszPath));
2210 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2211 return TRUE;
2212 return FALSE;
2215 /*************************************************************************
2216 * PathIsUNCW [SHLWAPI.@]
2218 * See PathIsUNCA.
2220 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2222 TRACE("(%s)\n",debugstr_w(lpszPath));
2224 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2225 return TRUE;
2226 return FALSE;
2229 /*************************************************************************
2230 * PathIsUNCServerA [SHLWAPI.@]
2232 * Determine if a path is a UNC server name ("\\SHARENAME").
2234 * PARAMS
2235 * lpszPath [I] Path to check.
2237 * RETURNS
2238 * TRUE If lpszPath is a valid UNC server name.
2239 * FALSE Otherwise.
2241 * NOTES
2242 * This routine is bug compatible with Win32: Server names with a
2243 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2244 * Fixing this bug may break other shlwapi functions!
2246 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2248 TRACE("(%s)\n", debugstr_a(lpszPath));
2250 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2252 while (*lpszPath)
2254 if (*lpszPath == '\\')
2255 return FALSE;
2256 lpszPath = CharNextA(lpszPath);
2258 return TRUE;
2260 return FALSE;
2263 /*************************************************************************
2264 * PathIsUNCServerW [SHLWAPI.@]
2266 * See PathIsUNCServerA.
2268 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2270 TRACE("(%s)\n", debugstr_w(lpszPath));
2272 if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2274 return !strchrW( lpszPath + 2, '\\' );
2276 return FALSE;
2279 /*************************************************************************
2280 * PathIsUNCServerShareA [SHLWAPI.@]
2282 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2284 * PARAMS
2285 * lpszPath [I] Path to check.
2287 * RETURNS
2288 * TRUE If lpszPath is a valid UNC server share.
2289 * FALSE Otherwise.
2291 * NOTES
2292 * This routine is bug compatible with Win32: Server shares with a
2293 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2294 * Fixing this bug may break other shlwapi functions!
2296 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2298 TRACE("(%s)\n", debugstr_a(lpszPath));
2300 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2302 BOOL bSeenSlash = FALSE;
2303 while (*lpszPath)
2305 if (*lpszPath == '\\')
2307 if (bSeenSlash)
2308 return FALSE;
2309 bSeenSlash = TRUE;
2311 lpszPath = CharNextA(lpszPath);
2313 return bSeenSlash;
2315 return FALSE;
2318 /*************************************************************************
2319 * PathIsUNCServerShareW [SHLWAPI.@]
2321 * See PathIsUNCServerShareA.
2323 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2325 TRACE("(%s)\n", debugstr_w(lpszPath));
2327 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2329 BOOL bSeenSlash = FALSE;
2330 while (*lpszPath)
2332 if (*lpszPath == '\\')
2334 if (bSeenSlash)
2335 return FALSE;
2336 bSeenSlash = TRUE;
2338 lpszPath++;
2340 return bSeenSlash;
2342 return FALSE;
2345 /*************************************************************************
2346 * PathCanonicalizeA [SHLWAPI.@]
2348 * Convert a path to its canonical form.
2350 * PARAMS
2351 * lpszBuf [O] Output path
2352 * lpszPath [I] Path to canonicalize
2354 * RETURNS
2355 * Success: TRUE. lpszBuf contains the output path,
2356 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2358 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2360 BOOL bRet = FALSE;
2362 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2364 if (lpszBuf)
2365 *lpszBuf = '\0';
2367 if (!lpszBuf || !lpszPath)
2368 SetLastError(ERROR_INVALID_PARAMETER);
2369 else
2371 WCHAR szPath[MAX_PATH];
2372 WCHAR szBuff[MAX_PATH];
2373 int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2375 if (!ret) {
2376 WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
2377 return FALSE;
2379 bRet = PathCanonicalizeW(szBuff, szPath);
2380 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2382 return bRet;
2386 /*************************************************************************
2387 * PathCanonicalizeW [SHLWAPI.@]
2389 * See PathCanonicalizeA.
2391 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2393 LPWSTR lpszDst = lpszBuf;
2394 LPCWSTR lpszSrc = lpszPath;
2396 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2398 if (lpszBuf)
2399 *lpszDst = '\0';
2401 if (!lpszBuf || !lpszPath)
2403 SetLastError(ERROR_INVALID_PARAMETER);
2404 return FALSE;
2407 if (!*lpszPath)
2409 *lpszBuf++ = '\\';
2410 *lpszBuf = '\0';
2411 return TRUE;
2414 /* Copy path root */
2415 if (*lpszSrc == '\\')
2417 *lpszDst++ = *lpszSrc++;
2419 else if (*lpszSrc && lpszSrc[1] == ':')
2421 /* X:\ */
2422 *lpszDst++ = *lpszSrc++;
2423 *lpszDst++ = *lpszSrc++;
2424 if (*lpszSrc == '\\')
2425 *lpszDst++ = *lpszSrc++;
2428 /* Canonicalize the rest of the path */
2429 while (*lpszSrc)
2431 if (*lpszSrc == '.')
2433 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2435 lpszSrc += 2; /* Skip .\ */
2437 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2439 /* \.. backs up a directory, over the root if it has no \ following X:.
2440 * .. is ignored if it would remove a UNC server name or initial \\
2442 if (lpszDst != lpszBuf)
2444 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2445 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2446 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2448 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2450 lpszDst -= 2;
2451 while (lpszDst > lpszBuf && *lpszDst != '\\')
2452 lpszDst--;
2453 if (*lpszDst == '\\')
2454 lpszDst++; /* Reset to last '\' */
2455 else
2456 lpszDst = lpszBuf; /* Start path again from new root */
2458 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2459 lpszDst -= 2;
2461 while (lpszDst > lpszBuf && *lpszDst != '\\')
2462 lpszDst--;
2463 if (lpszDst == lpszBuf)
2465 *lpszDst++ = '\\';
2466 lpszSrc++;
2469 lpszSrc += 2; /* Skip .. in src path */
2471 else
2472 *lpszDst++ = *lpszSrc++;
2474 else
2475 *lpszDst++ = *lpszSrc++;
2477 /* Append \ to naked drive specs */
2478 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2479 *lpszDst++ = '\\';
2480 *lpszDst++ = '\0';
2481 return TRUE;
2484 /*************************************************************************
2485 * PathFindNextComponentA [SHLWAPI.@]
2487 * Find the next component in a path.
2489 * PARAMS
2490 * lpszPath [I] Path to find next component in
2492 * RETURNS
2493 * Success: A pointer to the next component, or the end of the string.
2494 * Failure: NULL, If lpszPath is invalid
2496 * NOTES
2497 * A 'component' is either a backslash character (\) or UNC marker (\\).
2498 * Because of this, relative paths (e.g "c:foo") are regarded as having
2499 * only one component.
2501 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2503 LPSTR lpszSlash;
2505 TRACE("(%s)\n", debugstr_a(lpszPath));
2507 if(!lpszPath || !*lpszPath)
2508 return NULL;
2510 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2512 if (lpszSlash[1] == '\\')
2513 lpszSlash++;
2514 return lpszSlash + 1;
2516 return (LPSTR)lpszPath + strlen(lpszPath);
2519 /*************************************************************************
2520 * PathFindNextComponentW [SHLWAPI.@]
2522 * See PathFindNextComponentA.
2524 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2526 LPWSTR lpszSlash;
2528 TRACE("(%s)\n", debugstr_w(lpszPath));
2530 if(!lpszPath || !*lpszPath)
2531 return NULL;
2533 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2535 if (lpszSlash[1] == '\\')
2536 lpszSlash++;
2537 return lpszSlash + 1;
2539 return (LPWSTR)lpszPath + strlenW(lpszPath);
2542 /*************************************************************************
2543 * PathAddExtensionA [SHLWAPI.@]
2545 * Add a file extension to a path
2547 * PARAMS
2548 * lpszPath [I/O] Path to add extension to
2549 * lpszExtension [I] Extension to add to lpszPath
2551 * RETURNS
2552 * TRUE If the path was modified,
2553 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2554 * extension already, or the new path length is too big.
2556 * FIXME
2557 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2558 * does not do this, so the behaviour was removed.
2560 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2562 size_t dwLen;
2564 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2566 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2567 return FALSE;
2569 dwLen = strlen(lpszPath);
2571 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2572 return FALSE;
2574 strcpy(lpszPath + dwLen, lpszExtension);
2575 return TRUE;
2578 /*************************************************************************
2579 * PathAddExtensionW [SHLWAPI.@]
2581 * See PathAddExtensionA.
2583 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2585 size_t dwLen;
2587 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2589 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2590 return FALSE;
2592 dwLen = strlenW(lpszPath);
2594 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2595 return FALSE;
2597 strcpyW(lpszPath + dwLen, lpszExtension);
2598 return TRUE;
2601 /*************************************************************************
2602 * PathMakePrettyA [SHLWAPI.@]
2604 * Convert an uppercase DOS filename into lowercase.
2606 * PARAMS
2607 * lpszPath [I/O] Path to convert.
2609 * RETURNS
2610 * TRUE If the path was an uppercase DOS path and was converted,
2611 * FALSE Otherwise.
2613 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2615 LPSTR pszIter = lpszPath;
2617 TRACE("(%s)\n", debugstr_a(lpszPath));
2619 if (!pszIter)
2620 return FALSE;
2622 if (*pszIter)
2626 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2627 return FALSE; /* Not DOS path */
2628 pszIter++;
2629 } while (*pszIter);
2630 pszIter = lpszPath + 1;
2631 while (*pszIter)
2633 *pszIter = tolower(*pszIter);
2634 pszIter++;
2637 return TRUE;
2640 /*************************************************************************
2641 * PathMakePrettyW [SHLWAPI.@]
2643 * See PathMakePrettyA.
2645 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2647 LPWSTR pszIter = lpszPath;
2649 TRACE("(%s)\n", debugstr_w(lpszPath));
2651 if (!pszIter)
2652 return FALSE;
2654 if (*pszIter)
2658 if (islowerW(*pszIter))
2659 return FALSE; /* Not DOS path */
2660 pszIter++;
2661 } while (*pszIter);
2662 pszIter = lpszPath + 1;
2663 while (*pszIter)
2665 *pszIter = tolowerW(*pszIter);
2666 pszIter++;
2669 return TRUE;
2672 /*************************************************************************
2673 * PathCommonPrefixA [SHLWAPI.@]
2675 * Determine the length of the common prefix between two paths.
2677 * PARAMS
2678 * lpszFile1 [I] First path for comparison
2679 * lpszFile2 [I] Second path for comparison
2680 * achPath [O] Destination for common prefix string
2682 * RETURNS
2683 * The length of the common prefix. This is 0 if there is no common
2684 * prefix between the paths or if any parameters are invalid. If the prefix
2685 * is non-zero and achPath is not NULL, achPath is filled with the common
2686 * part of the prefix and NUL terminated.
2688 * NOTES
2689 * A common prefix of 2 is always returned as 3. It is thus possible for
2690 * the length returned to be invalid (i.e. Longer than one or both of the
2691 * strings given as parameters). This Win32 behaviour has been implemented
2692 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2693 * To work around this when using this function, always check that the byte
2694 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2696 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2698 size_t iLen = 0;
2699 LPCSTR lpszIter1 = lpszFile1;
2700 LPCSTR lpszIter2 = lpszFile2;
2702 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2704 if (achPath)
2705 *achPath = '\0';
2707 if (!lpszFile1 || !lpszFile2)
2708 return 0;
2710 /* Handle roots first */
2711 if (PathIsUNCA(lpszFile1))
2713 if (!PathIsUNCA(lpszFile2))
2714 return 0;
2715 lpszIter1 += 2;
2716 lpszIter2 += 2;
2718 else if (PathIsUNCA(lpszFile2))
2719 return 0; /* Know already lpszFile1 is not UNC */
2723 /* Update len */
2724 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2725 (!*lpszIter2 || *lpszIter2 == '\\'))
2726 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2728 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2729 break; /* Strings differ at this point */
2731 lpszIter1++;
2732 lpszIter2++;
2733 } while (1);
2735 if (iLen == 2)
2736 iLen++; /* Feature/Bug compatible with Win32 */
2738 if (iLen && achPath)
2740 memcpy(achPath,lpszFile1,iLen);
2741 achPath[iLen] = '\0';
2743 return iLen;
2746 /*************************************************************************
2747 * PathCommonPrefixW [SHLWAPI.@]
2749 * See PathCommonPrefixA.
2751 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2753 size_t iLen = 0;
2754 LPCWSTR lpszIter1 = lpszFile1;
2755 LPCWSTR lpszIter2 = lpszFile2;
2757 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2759 if (achPath)
2760 *achPath = '\0';
2762 if (!lpszFile1 || !lpszFile2)
2763 return 0;
2765 /* Handle roots first */
2766 if (PathIsUNCW(lpszFile1))
2768 if (!PathIsUNCW(lpszFile2))
2769 return 0;
2770 lpszIter1 += 2;
2771 lpszIter2 += 2;
2773 else if (PathIsUNCW(lpszFile2))
2774 return 0; /* Know already lpszFile1 is not UNC */
2778 /* Update len */
2779 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2780 (!*lpszIter2 || *lpszIter2 == '\\'))
2781 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2783 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2784 break; /* Strings differ at this point */
2786 lpszIter1++;
2787 lpszIter2++;
2788 } while (1);
2790 if (iLen == 2)
2791 iLen++; /* Feature/Bug compatible with Win32 */
2793 if (iLen && achPath)
2795 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2796 achPath[iLen] = '\0';
2798 return iLen;
2801 /*************************************************************************
2802 * PathCompactPathA [SHLWAPI.@]
2804 * Make a path fit into a given width when printed to a DC.
2806 * PARAMS
2807 * hDc [I] Destination DC
2808 * lpszPath [I/O] Path to be printed to hDc
2809 * dx [I] Desired width
2811 * RETURNS
2812 * TRUE If the path was modified/went well.
2813 * FALSE Otherwise.
2815 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2817 BOOL bRet = FALSE;
2819 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2821 if (lpszPath)
2823 WCHAR szPath[MAX_PATH];
2824 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2825 bRet = PathCompactPathW(hDC, szPath, dx);
2826 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2828 return bRet;
2831 /*************************************************************************
2832 * PathCompactPathW [SHLWAPI.@]
2834 * See PathCompactPathA.
2836 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2838 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2839 BOOL bRet = TRUE;
2840 HDC hdc = 0;
2841 WCHAR buff[MAX_PATH];
2842 SIZE size;
2843 DWORD dwLen;
2845 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2847 if (!lpszPath)
2848 return FALSE;
2850 if (!hDC)
2851 hdc = hDC = GetDC(0);
2853 /* Get the length of the whole path */
2854 dwLen = strlenW(lpszPath);
2855 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2857 if ((UINT)size.cx > dx)
2859 /* Path too big, must reduce it */
2860 LPWSTR sFile;
2861 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2863 sFile = PathFindFileNameW(lpszPath);
2864 if (sFile != lpszPath) sFile--;
2866 /* Get the size of ellipses */
2867 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2868 dwEllipsesLen = size.cx;
2869 /* Get the size of the file name */
2870 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2871 dwPathLen = size.cx;
2873 if (sFile != lpszPath)
2875 LPWSTR sPath = sFile;
2876 BOOL bEllipses = FALSE;
2878 /* The path includes a file name. Include as much of the path prior to
2879 * the file name as possible, allowing for the ellipses, e.g:
2880 * c:\some very long path\filename ==> c:\some v...\filename
2882 lstrcpynW(buff, sFile, MAX_PATH);
2886 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2888 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2889 dwTotalLen += size.cx;
2890 if (dwTotalLen <= dx)
2891 break;
2892 sPath--;
2893 if (!bEllipses)
2895 bEllipses = TRUE;
2896 sPath -= 2;
2898 } while (sPath > lpszPath);
2900 if (sPath > lpszPath)
2902 if (bEllipses)
2904 strcpyW(sPath, szEllipses);
2905 strcpyW(sPath+3, buff);
2907 bRet = TRUE;
2908 goto end;
2910 strcpyW(lpszPath, szEllipses);
2911 strcpyW(lpszPath+3, buff);
2912 bRet = FALSE;
2913 goto end;
2916 /* Trim the path by adding ellipses to the end, e.g:
2917 * A very long file name.txt ==> A very...
2919 dwLen = strlenW(lpszPath);
2921 if (dwLen > MAX_PATH - 3)
2922 dwLen = MAX_PATH - 3;
2923 lstrcpynW(buff, sFile, dwLen);
2925 do {
2926 dwLen--;
2927 GetTextExtentPointW(hDC, buff, dwLen, &size);
2928 } while (dwLen && size.cx + dwEllipsesLen > dx);
2930 if (!dwLen)
2932 DWORD dwWritten = 0;
2934 dwEllipsesLen /= 3; /* Size of a single '.' */
2936 /* Write as much of the Ellipses string as possible */
2937 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2939 *lpszPath++ = '.';
2940 dwWritten += dwEllipsesLen;
2941 dwLen++;
2943 *lpszPath = '\0';
2944 bRet = FALSE;
2946 else
2948 strcpyW(buff + dwLen, szEllipses);
2949 strcpyW(lpszPath, buff);
2953 end:
2954 if (hdc)
2955 ReleaseDC(0, hdc);
2957 return bRet;
2960 /*************************************************************************
2961 * PathGetCharTypeA [SHLWAPI.@]
2963 * Categorise a character from a file path.
2965 * PARAMS
2966 * ch [I] Character to get the type of
2968 * RETURNS
2969 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2971 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2973 return PathGetCharTypeW(ch);
2976 /*************************************************************************
2977 * PathGetCharTypeW [SHLWAPI.@]
2979 * See PathGetCharTypeA.
2981 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2983 UINT flags = 0;
2985 TRACE("(%d)\n", ch);
2987 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2988 ch == '"' || ch == '|' || ch == '/')
2989 flags = GCT_INVALID; /* Invalid */
2990 else if (ch == '*' || ch=='?')
2991 flags = GCT_WILD; /* Wildchars */
2992 else if ((ch == '\\') || (ch == ':'))
2993 return GCT_SEPARATOR; /* Path separators */
2994 else
2996 if (ch < 126)
2998 if (((ch & 0x1) && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2999 ch == '.' || ch == '@' || ch == '^' ||
3000 ch == '\'' || ch == 130 || ch == '`')
3001 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
3003 else
3004 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
3005 flags |= GCT_LFNCHAR; /* Valid for long file names */
3007 return flags;
3010 /*************************************************************************
3011 * SHLWAPI_UseSystemForSystemFolders
3013 * Internal helper for PathMakeSystemFolderW.
3015 static BOOL SHLWAPI_UseSystemForSystemFolders(void)
3017 static BOOL bCheckedReg = FALSE;
3018 static BOOL bUseSystemForSystemFolders = FALSE;
3020 if (!bCheckedReg)
3022 bCheckedReg = TRUE;
3024 /* Key tells Win what file attributes to use on system folders */
3025 if (SHGetValueA(HKEY_LOCAL_MACHINE,
3026 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
3027 "UseSystemForSystemFolders", 0, 0, 0))
3028 bUseSystemForSystemFolders = TRUE;
3030 return bUseSystemForSystemFolders;
3033 /*************************************************************************
3034 * PathMakeSystemFolderA [SHLWAPI.@]
3036 * Set system folder attribute for a path.
3038 * PARAMS
3039 * lpszPath [I] The path to turn into a system folder
3041 * RETURNS
3042 * TRUE If the path was changed to/already was a system folder
3043 * FALSE If the path is invalid or SetFileAttributesA() fails
3045 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3047 BOOL bRet = FALSE;
3049 TRACE("(%s)\n", debugstr_a(lpszPath));
3051 if (lpszPath && *lpszPath)
3053 WCHAR szPath[MAX_PATH];
3054 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3055 bRet = PathMakeSystemFolderW(szPath);
3057 return bRet;
3060 /*************************************************************************
3061 * PathMakeSystemFolderW [SHLWAPI.@]
3063 * See PathMakeSystemFolderA.
3065 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3067 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3068 WCHAR buff[MAX_PATH];
3070 TRACE("(%s)\n", debugstr_w(lpszPath));
3072 if (!lpszPath || !*lpszPath)
3073 return FALSE;
3075 /* If the directory is already a system directory, don't do anything */
3076 GetSystemDirectoryW(buff, MAX_PATH);
3077 if (!strcmpW(buff, lpszPath))
3078 return TRUE;
3080 GetWindowsDirectoryW(buff, MAX_PATH);
3081 if (!strcmpW(buff, lpszPath))
3082 return TRUE;
3084 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3085 if (SHLWAPI_UseSystemForSystemFolders())
3086 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3088 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3089 return FALSE;
3091 /* Change file attributes to system attributes */
3092 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3093 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3096 /*************************************************************************
3097 * PathRenameExtensionA [SHLWAPI.@]
3099 * Swap the file extension in a path with another extension.
3101 * PARAMS
3102 * lpszPath [I/O] Path to swap the extension in
3103 * lpszExt [I] The new extension
3105 * RETURNS
3106 * TRUE if lpszPath was modified,
3107 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3109 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3111 LPSTR lpszExtension;
3113 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3115 lpszExtension = PathFindExtensionA(lpszPath);
3117 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3118 return FALSE;
3120 strcpy(lpszExtension, lpszExt);
3121 return TRUE;
3124 /*************************************************************************
3125 * PathRenameExtensionW [SHLWAPI.@]
3127 * See PathRenameExtensionA.
3129 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3131 LPWSTR lpszExtension;
3133 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3135 lpszExtension = PathFindExtensionW(lpszPath);
3137 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3138 return FALSE;
3140 strcpyW(lpszExtension, lpszExt);
3141 return TRUE;
3144 /*************************************************************************
3145 * PathSearchAndQualifyA [SHLWAPI.@]
3147 * Determine if a given path is correct and fully qualified.
3149 * PARAMS
3150 * lpszPath [I] Path to check
3151 * lpszBuf [O] Output for correct path
3152 * cchBuf [I] Size of lpszBuf
3154 * RETURNS
3155 * Unknown.
3157 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3159 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3161 if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3162 return TRUE;
3163 return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3166 /*************************************************************************
3167 * PathSearchAndQualifyW [SHLWAPI.@]
3169 * See PathSearchAndQualifyA.
3171 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3173 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3175 if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3176 return TRUE;
3177 return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3180 /*************************************************************************
3181 * PathSkipRootA [SHLWAPI.@]
3183 * Return the portion of a path following the drive letter or mount point.
3185 * PARAMS
3186 * lpszPath [I] The path to skip on
3188 * RETURNS
3189 * Success: A pointer to the next character after the root.
3190 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3192 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3194 TRACE("(%s)\n", debugstr_a(lpszPath));
3196 if (!lpszPath || !*lpszPath)
3197 return NULL;
3199 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3201 /* Network share: skip share server and mount point */
3202 lpszPath += 2;
3203 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3204 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3205 lpszPath++;
3206 return (LPSTR)lpszPath;
3209 if (IsDBCSLeadByte(*lpszPath))
3210 return NULL;
3212 /* Check x:\ */
3213 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3214 return (LPSTR)lpszPath + 3;
3215 return NULL;
3218 /*************************************************************************
3219 * PathSkipRootW [SHLWAPI.@]
3221 * See PathSkipRootA.
3223 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3225 TRACE("(%s)\n", debugstr_w(lpszPath));
3227 if (!lpszPath || !*lpszPath)
3228 return NULL;
3230 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3232 /* Network share: skip share server and mount point */
3233 lpszPath += 2;
3234 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3235 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3236 lpszPath++;
3237 return (LPWSTR)lpszPath;
3240 /* Check x:\ */
3241 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3242 return (LPWSTR)lpszPath + 3;
3243 return NULL;
3246 /*************************************************************************
3247 * PathCreateFromUrlA [SHLWAPI.@]
3249 * See PathCreateFromUrlW
3251 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3252 LPDWORD pcchPath, DWORD dwReserved)
3254 WCHAR bufW[MAX_PATH];
3255 WCHAR *pathW = bufW;
3256 UNICODE_STRING urlW;
3257 HRESULT ret;
3258 DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3260 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3261 return E_INVALIDARG;
3263 if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3264 return E_INVALIDARG;
3265 if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3266 pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3267 ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3269 if(ret == S_OK) {
3270 RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3271 if(*pcchPath > lenA) {
3272 RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3273 pszPath[lenA] = 0;
3274 *pcchPath = lenA;
3275 } else {
3276 *pcchPath = lenA + 1;
3277 ret = E_POINTER;
3280 if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3281 RtlFreeUnicodeString(&urlW);
3282 return ret;
3285 /*************************************************************************
3286 * PathCreateFromUrlW [SHLWAPI.@]
3288 * Create a path from a URL
3290 * PARAMS
3291 * lpszUrl [I] URL to convert into a path
3292 * lpszPath [O] Output buffer for the resulting Path
3293 * pcchPath [I] Length of lpszPath
3294 * dwFlags [I] Flags controlling the conversion
3296 * RETURNS
3297 * Success: S_OK. lpszPath contains the URL in path format,
3298 * Failure: An HRESULT error code such as E_INVALIDARG.
3300 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3301 LPDWORD pcchPath, DWORD dwReserved)
3303 static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3304 static const WCHAR localhost[] = { 'l','o','c','a','l','h','o','s','t',0 };
3305 DWORD nslashes, unescape, len;
3306 const WCHAR *src;
3307 WCHAR *tpath, *dst;
3308 HRESULT ret;
3310 TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3312 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3313 return E_INVALIDARG;
3315 if (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pszUrl, 5,
3316 file_colon, 5) != CSTR_EQUAL)
3317 return E_INVALIDARG;
3318 pszUrl += 5;
3319 ret = S_OK;
3321 src = pszUrl;
3322 nslashes = 0;
3323 while (*src == '/' || *src == '\\') {
3324 nslashes++;
3325 src++;
3328 /* We need a temporary buffer so we can compute what size to ask for.
3329 * We know that the final string won't be longer than the current pszUrl
3330 * plus at most two backslashes. All the other transformations make it
3331 * shorter.
3333 len = 2 + lstrlenW(pszUrl) + 1;
3334 if (*pcchPath < len)
3335 tpath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3336 else
3337 tpath = pszPath;
3339 len = 0;
3340 dst = tpath;
3341 unescape = 1;
3342 switch (nslashes)
3344 case 0:
3345 /* 'file:' + escaped DOS path */
3346 break;
3347 case 1:
3348 /* 'file:/' + escaped DOS path */
3349 /* fall through */
3350 case 3:
3351 /* 'file:///' (implied localhost) + escaped DOS path */
3352 if (!isalphaW(*src) || (src[1] != ':' && src[1] != '|'))
3353 src -= 1;
3354 break;
3355 case 2:
3356 if (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, src, 9,
3357 localhost, 9) == CSTR_EQUAL &&
3358 (src[9] == '/' || src[9] == '\\'))
3360 /* 'file://localhost/' + escaped DOS path */
3361 src += 10;
3363 else if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3365 /* 'file://' + unescaped DOS path */
3366 unescape = 0;
3368 else
3370 /* 'file://hostname:port/path' (where path is escaped)
3371 * or 'file:' + escaped UNC path (\\server\share\path)
3372 * The second form is clearly specific to Windows and it might
3373 * even be doing a network lookup to try to figure it out.
3375 while (*src && *src != '/' && *src != '\\')
3376 src++;
3377 len = src - pszUrl;
3378 StrCpyNW(dst, pszUrl, len + 1);
3379 dst += len;
3380 if (isalphaW(src[1]) && (src[2] == ':' || src[2] == '|'))
3382 /* 'Forget' to add a trailing '/', just like Windows */
3383 src++;
3386 break;
3387 case 4:
3388 /* 'file://' + unescaped UNC path (\\server\share\path) */
3389 unescape = 0;
3390 if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3391 break;
3392 /* fall through */
3393 default:
3394 /* 'file:/...' + escaped UNC path (\\server\share\path) */
3395 src -= 2;
3398 /* Copy the remainder of the path */
3399 len += lstrlenW(src);
3400 StrCpyW(dst, src);
3402 /* First do the Windows-specific path conversions */
3403 for (dst = tpath; *dst; dst++)
3404 if (*dst == '/') *dst = '\\';
3405 if (isalphaW(*tpath) && tpath[1] == '|')
3406 tpath[1] = ':'; /* c| -> c: */
3408 /* And only then unescape the path (i.e. escaped slashes are left as is) */
3409 if (unescape)
3411 ret = UrlUnescapeW(tpath, NULL, &len, URL_UNESCAPE_INPLACE);
3412 if (ret == S_OK)
3414 /* When working in-place UrlUnescapeW() does not set len */
3415 len = lstrlenW(tpath);
3419 if (*pcchPath < len + 1)
3421 ret = E_POINTER;
3422 *pcchPath = len + 1;
3424 else
3426 *pcchPath = len;
3427 if (tpath != pszPath)
3428 StrCpyW(pszPath, tpath);
3430 if (tpath != pszPath)
3431 HeapFree(GetProcessHeap(), 0, tpath);
3433 TRACE("Returning (%u) %s\n", *pcchPath, debugstr_w(pszPath));
3434 return ret;
3437 /*************************************************************************
3438 * PathCreateFromUrlAlloc [SHLWAPI.@]
3440 HRESULT WINAPI PathCreateFromUrlAlloc(LPCWSTR pszUrl, LPWSTR *pszPath,
3441 DWORD dwReserved)
3443 WCHAR pathW[MAX_PATH];
3444 DWORD size;
3445 HRESULT hr;
3447 size = MAX_PATH;
3448 hr = PathCreateFromUrlW(pszUrl, pathW, &size, dwReserved);
3449 if (SUCCEEDED(hr))
3451 /* Yes, this is supposed to crash if pszPath is NULL */
3452 *pszPath = StrDupW(pathW);
3454 return hr;
3457 /*************************************************************************
3458 * PathRelativePathToA [SHLWAPI.@]
3460 * Create a relative path from one path to another.
3462 * PARAMS
3463 * lpszPath [O] Destination for relative path
3464 * lpszFrom [I] Source path
3465 * dwAttrFrom [I] File attribute of source path
3466 * lpszTo [I] Destination path
3467 * dwAttrTo [I] File attributes of destination path
3469 * RETURNS
3470 * TRUE If a relative path can be formed. lpszPath contains the new path
3471 * FALSE If the paths are not relative or any parameters are invalid
3473 * NOTES
3474 * lpszTo should be at least MAX_PATH in length.
3476 * Calling this function with relative paths for lpszFrom or lpszTo may
3477 * give erroneous results.
3479 * The Win32 version of this function contains a bug where the lpszTo string
3480 * may be referenced 1 byte beyond the end of the string. As a result random
3481 * garbage may be written to the output path, depending on what lies beyond
3482 * the last byte of the string. This bug occurs because of the behaviour of
3483 * PathCommonPrefix() (see notes for that function), and no workaround seems
3484 * possible with Win32.
3486 * This bug has been fixed here, so for example the relative path from "\\"
3487 * to "\\" is correctly determined as "." in this implementation.
3489 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3490 LPCSTR lpszTo, DWORD dwAttrTo)
3492 BOOL bRet = FALSE;
3494 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3495 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3497 if(lpszPath && lpszFrom && lpszTo)
3499 WCHAR szPath[MAX_PATH];
3500 WCHAR szFrom[MAX_PATH];
3501 WCHAR szTo[MAX_PATH];
3502 MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3503 MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3504 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3505 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3507 return bRet;
3510 /*************************************************************************
3511 * PathRelativePathToW [SHLWAPI.@]
3513 * See PathRelativePathToA.
3515 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3516 LPCWSTR lpszTo, DWORD dwAttrTo)
3518 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3519 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3520 WCHAR szFrom[MAX_PATH];
3521 WCHAR szTo[MAX_PATH];
3522 DWORD dwLen;
3524 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3525 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3527 if(!lpszPath || !lpszFrom || !lpszTo)
3528 return FALSE;
3530 *lpszPath = '\0';
3531 lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3532 lstrcpynW(szTo, lpszTo, MAX_PATH);
3534 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3535 PathRemoveFileSpecW(szFrom);
3536 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3537 PathRemoveFileSpecW(szTo);
3539 /* Paths can only be relative if they have a common root */
3540 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3541 return FALSE;
3543 /* Strip off lpszFrom components to the root, by adding "..\" */
3544 lpszFrom = szFrom + dwLen;
3545 if (!*lpszFrom)
3547 lpszPath[0] = '.';
3548 lpszPath[1] = '\0';
3550 if (*lpszFrom == '\\')
3551 lpszFrom++;
3553 while (*lpszFrom)
3555 lpszFrom = PathFindNextComponentW(lpszFrom);
3556 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3559 /* From the root add the components of lpszTo */
3560 lpszTo += dwLen;
3561 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3562 * this function.
3564 if (*lpszTo && lpszTo[-1])
3566 if (*lpszTo != '\\')
3567 lpszTo--;
3568 dwLen = strlenW(lpszPath);
3569 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3571 *lpszPath = '\0';
3572 return FALSE;
3574 strcpyW(lpszPath + dwLen, lpszTo);
3576 return TRUE;
3579 /*************************************************************************
3580 * PathUnmakeSystemFolderA [SHLWAPI.@]
3582 * Remove the system folder attributes from a path.
3584 * PARAMS
3585 * lpszPath [I] The path to remove attributes from
3587 * RETURNS
3588 * Success: TRUE.
3589 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3590 * SetFileAttributesA() fails.
3592 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3594 DWORD dwAttr;
3596 TRACE("(%s)\n", debugstr_a(lpszPath));
3598 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3599 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3600 return FALSE;
3602 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3603 return SetFileAttributesA(lpszPath, dwAttr);
3606 /*************************************************************************
3607 * PathUnmakeSystemFolderW [SHLWAPI.@]
3609 * See PathUnmakeSystemFolderA.
3611 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3613 DWORD dwAttr;
3615 TRACE("(%s)\n", debugstr_w(lpszPath));
3617 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3618 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3619 return FALSE;
3621 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3622 return SetFileAttributesW(lpszPath, dwAttr);
3626 /*************************************************************************
3627 * PathSetDlgItemPathA [SHLWAPI.@]
3629 * Set the text of a dialog item to a path, shrinking the path to fit
3630 * if it is too big for the item.
3632 * PARAMS
3633 * hDlg [I] Dialog handle
3634 * id [I] ID of item in the dialog
3635 * lpszPath [I] Path to set as the items text
3637 * RETURNS
3638 * Nothing.
3640 * NOTES
3641 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3642 * window text is erased).
3644 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3646 WCHAR szPath[MAX_PATH];
3648 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3650 if (lpszPath)
3651 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3652 else
3653 szPath[0] = '\0';
3654 PathSetDlgItemPathW(hDlg, id, szPath);
3657 /*************************************************************************
3658 * PathSetDlgItemPathW [SHLWAPI.@]
3660 * See PathSetDlgItemPathA.
3662 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3664 WCHAR path[MAX_PATH + 1];
3665 HWND hwItem;
3666 RECT rect;
3667 HDC hdc;
3668 HGDIOBJ hPrevObj;
3670 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3672 if (!(hwItem = GetDlgItem(hDlg, id)))
3673 return;
3675 if (lpszPath)
3676 lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3677 else
3678 path[0] = '\0';
3680 GetClientRect(hwItem, &rect);
3681 hdc = GetDC(hDlg);
3682 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3684 if (hPrevObj)
3686 PathCompactPathW(hdc, path, rect.right);
3687 SelectObject(hdc, hPrevObj);
3690 ReleaseDC(hDlg, hdc);
3691 SetWindowTextW(hwItem, path);
3694 /*************************************************************************
3695 * PathIsNetworkPathA [SHLWAPI.@]
3697 * Determine if the given path is a network path.
3699 * PARAMS
3700 * lpszPath [I] Path to check
3702 * RETURNS
3703 * TRUE If lpszPath is a UNC share or mapped network drive, or
3704 * FALSE If lpszPath is a local drive or cannot be determined
3706 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3708 int dwDriveNum;
3710 TRACE("(%s)\n",debugstr_a(lpszPath));
3712 if (!lpszPath)
3713 return FALSE;
3714 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3715 return TRUE;
3716 dwDriveNum = PathGetDriveNumberA(lpszPath);
3717 if (dwDriveNum == -1)
3718 return FALSE;
3719 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3720 return pIsNetDrive(dwDriveNum);
3723 /*************************************************************************
3724 * PathIsNetworkPathW [SHLWAPI.@]
3726 * See PathIsNetworkPathA.
3728 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3730 int dwDriveNum;
3732 TRACE("(%s)\n", debugstr_w(lpszPath));
3734 if (!lpszPath)
3735 return FALSE;
3736 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3737 return TRUE;
3738 dwDriveNum = PathGetDriveNumberW(lpszPath);
3739 if (dwDriveNum == -1)
3740 return FALSE;
3741 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3742 return pIsNetDrive(dwDriveNum);
3745 /*************************************************************************
3746 * PathIsLFNFileSpecA [SHLWAPI.@]
3748 * Determine if the given path is a long file name
3750 * PARAMS
3751 * lpszPath [I] Path to check
3753 * RETURNS
3754 * TRUE If path is a long file name,
3755 * FALSE If path is a valid DOS short file name
3757 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3759 DWORD dwNameLen = 0, dwExtLen = 0;
3761 TRACE("(%s)\n",debugstr_a(lpszPath));
3763 if (!lpszPath)
3764 return FALSE;
3766 while (*lpszPath)
3768 if (*lpszPath == ' ')
3769 return TRUE; /* DOS names cannot have spaces */
3770 if (*lpszPath == '.')
3772 if (dwExtLen)
3773 return TRUE; /* DOS names have only one dot */
3774 dwExtLen = 1;
3776 else if (dwExtLen)
3778 dwExtLen++;
3779 if (dwExtLen > 4)
3780 return TRUE; /* DOS extensions are <= 3 chars*/
3782 else
3784 dwNameLen++;
3785 if (dwNameLen > 8)
3786 return TRUE; /* DOS names are <= 8 chars */
3788 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3790 return FALSE; /* Valid DOS path */
3793 /*************************************************************************
3794 * PathIsLFNFileSpecW [SHLWAPI.@]
3796 * See PathIsLFNFileSpecA.
3798 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3800 DWORD dwNameLen = 0, dwExtLen = 0;
3802 TRACE("(%s)\n",debugstr_w(lpszPath));
3804 if (!lpszPath)
3805 return FALSE;
3807 while (*lpszPath)
3809 if (*lpszPath == ' ')
3810 return TRUE; /* DOS names cannot have spaces */
3811 if (*lpszPath == '.')
3813 if (dwExtLen)
3814 return TRUE; /* DOS names have only one dot */
3815 dwExtLen = 1;
3817 else if (dwExtLen)
3819 dwExtLen++;
3820 if (dwExtLen > 4)
3821 return TRUE; /* DOS extensions are <= 3 chars*/
3823 else
3825 dwNameLen++;
3826 if (dwNameLen > 8)
3827 return TRUE; /* DOS names are <= 8 chars */
3829 lpszPath++;
3831 return FALSE; /* Valid DOS path */
3834 /*************************************************************************
3835 * PathIsDirectoryEmptyA [SHLWAPI.@]
3837 * Determine if a given directory is empty.
3839 * PARAMS
3840 * lpszPath [I] Directory to check
3842 * RETURNS
3843 * TRUE If the directory exists and contains no files,
3844 * FALSE Otherwise
3846 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3848 BOOL bRet = FALSE;
3850 TRACE("(%s)\n",debugstr_a(lpszPath));
3852 if (lpszPath)
3854 WCHAR szPath[MAX_PATH];
3855 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3856 bRet = PathIsDirectoryEmptyW(szPath);
3858 return bRet;
3861 /*************************************************************************
3862 * PathIsDirectoryEmptyW [SHLWAPI.@]
3864 * See PathIsDirectoryEmptyA.
3866 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3868 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3869 WCHAR szSearch[MAX_PATH];
3870 DWORD dwLen;
3871 HANDLE hfind;
3872 BOOL retVal = FALSE;
3873 WIN32_FIND_DATAW find_data;
3875 TRACE("(%s)\n",debugstr_w(lpszPath));
3877 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3878 return FALSE;
3880 lstrcpynW(szSearch, lpszPath, MAX_PATH);
3881 PathAddBackslashW(szSearch);
3882 dwLen = strlenW(szSearch);
3883 if (dwLen > MAX_PATH - 4)
3884 return FALSE;
3886 strcpyW(szSearch + dwLen, szAllFiles);
3887 hfind = FindFirstFileW(szSearch, &find_data);
3889 if (hfind != INVALID_HANDLE_VALUE &&
3890 find_data.cFileName[0] == '.' &&
3891 find_data.cFileName[1] == '.')
3893 /* The only directory entry should be the parent */
3894 if (!FindNextFileW(hfind, &find_data))
3895 retVal = TRUE;
3896 FindClose(hfind);
3898 return retVal;
3902 /*************************************************************************
3903 * PathFindSuffixArrayA [SHLWAPI.@]
3905 * Find a suffix string in an array of suffix strings
3907 * PARAMS
3908 * lpszSuffix [I] Suffix string to search for
3909 * lppszArray [I] Array of suffix strings to search
3910 * dwCount [I] Number of elements in lppszArray
3912 * RETURNS
3913 * Success: The index of the position of lpszSuffix in lppszArray
3914 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3916 * NOTES
3917 * The search is case sensitive.
3918 * The match is made against the end of the suffix string, so for example:
3919 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3921 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3923 size_t dwLen;
3924 int dwRet = 0;
3926 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3928 if (lpszSuffix && lppszArray && dwCount > 0)
3930 dwLen = strlen(lpszSuffix);
3932 while (dwRet < dwCount)
3934 size_t dwCompareLen = strlen(*lppszArray);
3935 if (dwCompareLen < dwLen)
3937 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3938 return *lppszArray; /* Found */
3940 dwRet++;
3941 lppszArray++;
3944 return NULL;
3947 /*************************************************************************
3948 * PathFindSuffixArrayW [SHLWAPI.@]
3950 * See PathFindSuffixArrayA.
3952 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3954 size_t dwLen;
3955 int dwRet = 0;
3957 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3959 if (lpszSuffix && lppszArray && dwCount > 0)
3961 dwLen = strlenW(lpszSuffix);
3963 while (dwRet < dwCount)
3965 size_t dwCompareLen = strlenW(*lppszArray);
3966 if (dwCompareLen < dwLen)
3968 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3969 return *lppszArray; /* Found */
3971 dwRet++;
3972 lppszArray++;
3975 return NULL;
3978 /*************************************************************************
3979 * PathUndecorateA [SHLWAPI.@]
3981 * Undecorate a file path
3983 * PARAMS
3984 * lpszPath [I/O] Path to remove any decoration from
3986 * RETURNS
3987 * Nothing
3989 * NOTES
3990 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3992 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3994 TRACE("(%s)\n",debugstr_a(lpszPath));
3996 if (lpszPath)
3998 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3999 if (lpszExt > lpszPath && lpszExt[-1] == ']')
4001 LPSTR lpszSkip = lpszExt - 2;
4002 if (*lpszSkip == '[')
4003 lpszSkip++; /* [] (no number) */
4004 else
4005 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
4006 lpszSkip--;
4007 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
4009 /* remove the [n] */
4010 lpszSkip--;
4011 while (*lpszExt)
4012 *lpszSkip++ = *lpszExt++;
4013 *lpszSkip = '\0';
4019 /*************************************************************************
4020 * PathUndecorateW [SHLWAPI.@]
4022 * See PathUndecorateA.
4024 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
4026 TRACE("(%s)\n",debugstr_w(lpszPath));
4028 if (lpszPath)
4030 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
4031 if (lpszExt > lpszPath && lpszExt[-1] == ']')
4033 LPWSTR lpszSkip = lpszExt - 2;
4034 if (*lpszSkip == '[')
4035 lpszSkip++; /* [] (no number) */
4036 else
4037 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
4038 lpszSkip--;
4039 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
4041 /* remove the [n] */
4042 lpszSkip--;
4043 while (*lpszExt)
4044 *lpszSkip++ = *lpszExt++;
4045 *lpszSkip = '\0';
4051 /*************************************************************************
4052 * PathUnExpandEnvStringsA [SHLWAPI.@]
4054 * Substitute folder names in a path with their corresponding environment
4055 * strings.
4057 * PARAMS
4058 * path [I] Buffer containing the path to unexpand.
4059 * buffer [O] Buffer to receive the unexpanded path.
4060 * buf_len [I] Size of pszBuf in characters.
4062 * RETURNS
4063 * Success: TRUE
4064 * Failure: FALSE
4066 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR path, LPSTR buffer, UINT buf_len)
4068 WCHAR bufferW[MAX_PATH], *pathW;
4069 DWORD len;
4070 BOOL ret;
4072 TRACE("(%s, %p, %d)\n", debugstr_a(path), buffer, buf_len);
4074 pathW = heap_strdupAtoW(path);
4075 if (!pathW) return FALSE;
4077 ret = PathUnExpandEnvStringsW(pathW, bufferW, MAX_PATH);
4078 HeapFree(GetProcessHeap(), 0, pathW);
4079 if (!ret) return FALSE;
4081 len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
4082 if (buf_len < len + 1) return FALSE;
4084 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buf_len, NULL, NULL);
4085 return TRUE;
4088 static const WCHAR allusersprofileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%',0};
4089 static const WCHAR appdataW[] = {'%','A','P','P','D','A','T','A','%',0};
4090 static const WCHAR computernameW[] = {'%','C','O','M','P','U','T','E','R','N','A','M','E','%',0};
4091 static const WCHAR programfilesW[] = {'%','P','r','o','g','r','a','m','F','i','l','e','s','%',0};
4092 static const WCHAR systemrootW[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
4093 static const WCHAR systemdriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%',0};
4094 static const WCHAR userprofileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%',0};
4096 struct envvars_map
4098 const WCHAR *var;
4099 UINT varlen;
4100 WCHAR path[MAX_PATH];
4101 DWORD len;
4104 static void init_envvars_map(struct envvars_map *map)
4106 while (map->var)
4108 map->len = ExpandEnvironmentStringsW(map->var, map->path, sizeof(map->path)/sizeof(WCHAR));
4109 /* exclude null from length */
4110 if (map->len) map->len--;
4111 map++;
4115 /*************************************************************************
4116 * PathUnExpandEnvStringsW [SHLWAPI.@]
4118 * Unicode version of PathUnExpandEnvStringsA.
4120 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR path, LPWSTR buffer, UINT buf_len)
4122 static struct envvars_map null_var = {NULL, 0, {0}, 0};
4123 struct envvars_map *match = &null_var, *cur;
4124 struct envvars_map envvars[] = {
4125 { allusersprofileW, sizeof(allusersprofileW)/sizeof(WCHAR) },
4126 { appdataW, sizeof(appdataW)/sizeof(WCHAR) },
4127 { computernameW, sizeof(computernameW)/sizeof(WCHAR) },
4128 { programfilesW, sizeof(programfilesW)/sizeof(WCHAR) },
4129 { systemrootW, sizeof(systemrootW)/sizeof(WCHAR) },
4130 { systemdriveW, sizeof(systemdriveW)/sizeof(WCHAR) },
4131 { userprofileW, sizeof(userprofileW)/sizeof(WCHAR) },
4132 { NULL }
4134 DWORD pathlen;
4135 UINT needed;
4137 TRACE("(%s, %p, %d)\n", debugstr_w(path), buffer, buf_len);
4139 pathlen = strlenW(path);
4140 init_envvars_map(envvars);
4141 cur = envvars;
4142 while (cur->var)
4144 /* path can't contain expanded value or value wasn't retrieved */
4145 if (cur->len == 0 || cur->len > pathlen || strncmpiW(cur->path, path, cur->len))
4147 cur++;
4148 continue;
4151 if (cur->len > match->len)
4152 match = cur;
4153 cur++;
4156 /* 'varlen' includes NULL termination char */
4157 needed = match->varlen + pathlen - match->len;
4158 if (match->len == 0 || needed > buf_len) return FALSE;
4160 strcpyW(buffer, match->var);
4161 strcatW(buffer, &path[match->len]);
4162 TRACE("ret %s\n", debugstr_w(buffer));
4164 return TRUE;
4167 /*************************************************************************
4168 * @ [SHLWAPI.440]
4170 * Find localised or default web content in "%WINDOWS%\web\".
4172 * PARAMS
4173 * lpszFile [I] File name containing content to look for
4174 * lpszPath [O] Buffer to contain the full path to the file
4175 * dwPathLen [I] Length of lpszPath
4177 * RETURNS
4178 * Success: S_OK. lpszPath contains the full path to the content.
4179 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
4181 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
4183 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
4184 HRESULT hRet;
4186 TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
4188 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
4189 szPath[0] = '\0';
4190 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
4191 WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
4192 return hRet;
4195 /*************************************************************************
4196 * @ [SHLWAPI.441]
4198 * Unicode version of SHGetWebFolderFilePathA.
4200 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
4202 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
4203 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
4204 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
4205 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
4206 DWORD dwLen, dwFileLen;
4207 LANGID lidSystem, lidUser;
4209 TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
4211 /* Get base directory for web content */
4212 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4213 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4214 dwLen--;
4216 dwFileLen = strlenW(lpszFile);
4218 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4219 return E_FAIL; /* lpszPath too short */
4221 strcpyW(lpszPath+dwLen, szWeb);
4222 dwLen += szWebLen;
4223 dwPathLen = dwPathLen - dwLen; /* Remaining space */
4225 lidSystem = GetSystemDefaultUILanguage();
4226 lidUser = GetUserDefaultUILanguage();
4228 if (lidSystem != lidUser)
4230 if (dwFileLen + szWebMuiLen < dwPathLen)
4232 /* Use localised content in the users UI language if present */
4233 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4234 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4235 if (PathFileExistsW(lpszPath))
4236 return S_OK;
4240 /* Fall back to OS default installed content */
4241 strcpyW(lpszPath + dwLen, lpszFile);
4242 if (PathFileExistsW(lpszPath))
4243 return S_OK;
4244 return E_FAIL;
4247 #define PATH_CHAR_CLASS_LETTER 0x00000001
4248 #define PATH_CHAR_CLASS_ASTERIX 0x00000002
4249 #define PATH_CHAR_CLASS_DOT 0x00000004
4250 #define PATH_CHAR_CLASS_BACKSLASH 0x00000008
4251 #define PATH_CHAR_CLASS_COLON 0x00000010
4252 #define PATH_CHAR_CLASS_SEMICOLON 0x00000020
4253 #define PATH_CHAR_CLASS_COMMA 0x00000040
4254 #define PATH_CHAR_CLASS_SPACE 0x00000080
4255 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4256 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4258 #define PATH_CHAR_CLASS_INVALID 0x00000000
4259 #define PATH_CHAR_CLASS_ANY 0xffffffff
4261 static const DWORD SHELL_charclass[] =
4263 /* 0x00 */ PATH_CHAR_CLASS_INVALID, /* 0x01 */ PATH_CHAR_CLASS_INVALID,
4264 /* 0x02 */ PATH_CHAR_CLASS_INVALID, /* 0x03 */ PATH_CHAR_CLASS_INVALID,
4265 /* 0x04 */ PATH_CHAR_CLASS_INVALID, /* 0x05 */ PATH_CHAR_CLASS_INVALID,
4266 /* 0x06 */ PATH_CHAR_CLASS_INVALID, /* 0x07 */ PATH_CHAR_CLASS_INVALID,
4267 /* 0x08 */ PATH_CHAR_CLASS_INVALID, /* 0x09 */ PATH_CHAR_CLASS_INVALID,
4268 /* 0x0a */ PATH_CHAR_CLASS_INVALID, /* 0x0b */ PATH_CHAR_CLASS_INVALID,
4269 /* 0x0c */ PATH_CHAR_CLASS_INVALID, /* 0x0d */ PATH_CHAR_CLASS_INVALID,
4270 /* 0x0e */ PATH_CHAR_CLASS_INVALID, /* 0x0f */ PATH_CHAR_CLASS_INVALID,
4271 /* 0x10 */ PATH_CHAR_CLASS_INVALID, /* 0x11 */ PATH_CHAR_CLASS_INVALID,
4272 /* 0x12 */ PATH_CHAR_CLASS_INVALID, /* 0x13 */ PATH_CHAR_CLASS_INVALID,
4273 /* 0x14 */ PATH_CHAR_CLASS_INVALID, /* 0x15 */ PATH_CHAR_CLASS_INVALID,
4274 /* 0x16 */ PATH_CHAR_CLASS_INVALID, /* 0x17 */ PATH_CHAR_CLASS_INVALID,
4275 /* 0x18 */ PATH_CHAR_CLASS_INVALID, /* 0x19 */ PATH_CHAR_CLASS_INVALID,
4276 /* 0x1a */ PATH_CHAR_CLASS_INVALID, /* 0x1b */ PATH_CHAR_CLASS_INVALID,
4277 /* 0x1c */ PATH_CHAR_CLASS_INVALID, /* 0x1d */ PATH_CHAR_CLASS_INVALID,
4278 /* 0x1e */ PATH_CHAR_CLASS_INVALID, /* 0x1f */ PATH_CHAR_CLASS_INVALID,
4279 /* ' ' */ PATH_CHAR_CLASS_SPACE, /* '!' */ PATH_CHAR_CLASS_OTHER_VALID,
4280 /* '"' */ PATH_CHAR_CLASS_DOUBLEQUOTE, /* '#' */ PATH_CHAR_CLASS_OTHER_VALID,
4281 /* '$' */ PATH_CHAR_CLASS_OTHER_VALID, /* '%' */ PATH_CHAR_CLASS_OTHER_VALID,
4282 /* '&' */ PATH_CHAR_CLASS_OTHER_VALID, /* '\'' */ PATH_CHAR_CLASS_OTHER_VALID,
4283 /* '(' */ PATH_CHAR_CLASS_OTHER_VALID, /* ')' */ PATH_CHAR_CLASS_OTHER_VALID,
4284 /* '*' */ PATH_CHAR_CLASS_ASTERIX, /* '+' */ PATH_CHAR_CLASS_OTHER_VALID,
4285 /* ',' */ PATH_CHAR_CLASS_COMMA, /* '-' */ PATH_CHAR_CLASS_OTHER_VALID,
4286 /* '.' */ PATH_CHAR_CLASS_DOT, /* '/' */ PATH_CHAR_CLASS_INVALID,
4287 /* '0' */ PATH_CHAR_CLASS_OTHER_VALID, /* '1' */ PATH_CHAR_CLASS_OTHER_VALID,
4288 /* '2' */ PATH_CHAR_CLASS_OTHER_VALID, /* '3' */ PATH_CHAR_CLASS_OTHER_VALID,
4289 /* '4' */ PATH_CHAR_CLASS_OTHER_VALID, /* '5' */ PATH_CHAR_CLASS_OTHER_VALID,
4290 /* '6' */ PATH_CHAR_CLASS_OTHER_VALID, /* '7' */ PATH_CHAR_CLASS_OTHER_VALID,
4291 /* '8' */ PATH_CHAR_CLASS_OTHER_VALID, /* '9' */ PATH_CHAR_CLASS_OTHER_VALID,
4292 /* ':' */ PATH_CHAR_CLASS_COLON, /* ';' */ PATH_CHAR_CLASS_SEMICOLON,
4293 /* '<' */ PATH_CHAR_CLASS_INVALID, /* '=' */ PATH_CHAR_CLASS_OTHER_VALID,
4294 /* '>' */ PATH_CHAR_CLASS_INVALID, /* '?' */ PATH_CHAR_CLASS_LETTER,
4295 /* '@' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'A' */ PATH_CHAR_CLASS_ANY,
4296 /* 'B' */ PATH_CHAR_CLASS_ANY, /* 'C' */ PATH_CHAR_CLASS_ANY,
4297 /* 'D' */ PATH_CHAR_CLASS_ANY, /* 'E' */ PATH_CHAR_CLASS_ANY,
4298 /* 'F' */ PATH_CHAR_CLASS_ANY, /* 'G' */ PATH_CHAR_CLASS_ANY,
4299 /* 'H' */ PATH_CHAR_CLASS_ANY, /* 'I' */ PATH_CHAR_CLASS_ANY,
4300 /* 'J' */ PATH_CHAR_CLASS_ANY, /* 'K' */ PATH_CHAR_CLASS_ANY,
4301 /* 'L' */ PATH_CHAR_CLASS_ANY, /* 'M' */ PATH_CHAR_CLASS_ANY,
4302 /* 'N' */ PATH_CHAR_CLASS_ANY, /* 'O' */ PATH_CHAR_CLASS_ANY,
4303 /* 'P' */ PATH_CHAR_CLASS_ANY, /* 'Q' */ PATH_CHAR_CLASS_ANY,
4304 /* 'R' */ PATH_CHAR_CLASS_ANY, /* 'S' */ PATH_CHAR_CLASS_ANY,
4305 /* 'T' */ PATH_CHAR_CLASS_ANY, /* 'U' */ PATH_CHAR_CLASS_ANY,
4306 /* 'V' */ PATH_CHAR_CLASS_ANY, /* 'W' */ PATH_CHAR_CLASS_ANY,
4307 /* 'X' */ PATH_CHAR_CLASS_ANY, /* 'Y' */ PATH_CHAR_CLASS_ANY,
4308 /* 'Z' */ PATH_CHAR_CLASS_ANY, /* '[' */ PATH_CHAR_CLASS_OTHER_VALID,
4309 /* '\\' */ PATH_CHAR_CLASS_BACKSLASH, /* ']' */ PATH_CHAR_CLASS_OTHER_VALID,
4310 /* '^' */ PATH_CHAR_CLASS_OTHER_VALID, /* '_' */ PATH_CHAR_CLASS_OTHER_VALID,
4311 /* '`' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'a' */ PATH_CHAR_CLASS_ANY,
4312 /* 'b' */ PATH_CHAR_CLASS_ANY, /* 'c' */ PATH_CHAR_CLASS_ANY,
4313 /* 'd' */ PATH_CHAR_CLASS_ANY, /* 'e' */ PATH_CHAR_CLASS_ANY,
4314 /* 'f' */ PATH_CHAR_CLASS_ANY, /* 'g' */ PATH_CHAR_CLASS_ANY,
4315 /* 'h' */ PATH_CHAR_CLASS_ANY, /* 'i' */ PATH_CHAR_CLASS_ANY,
4316 /* 'j' */ PATH_CHAR_CLASS_ANY, /* 'k' */ PATH_CHAR_CLASS_ANY,
4317 /* 'l' */ PATH_CHAR_CLASS_ANY, /* 'm' */ PATH_CHAR_CLASS_ANY,
4318 /* 'n' */ PATH_CHAR_CLASS_ANY, /* 'o' */ PATH_CHAR_CLASS_ANY,
4319 /* 'p' */ PATH_CHAR_CLASS_ANY, /* 'q' */ PATH_CHAR_CLASS_ANY,
4320 /* 'r' */ PATH_CHAR_CLASS_ANY, /* 's' */ PATH_CHAR_CLASS_ANY,
4321 /* 't' */ PATH_CHAR_CLASS_ANY, /* 'u' */ PATH_CHAR_CLASS_ANY,
4322 /* 'v' */ PATH_CHAR_CLASS_ANY, /* 'w' */ PATH_CHAR_CLASS_ANY,
4323 /* 'x' */ PATH_CHAR_CLASS_ANY, /* 'y' */ PATH_CHAR_CLASS_ANY,
4324 /* 'z' */ PATH_CHAR_CLASS_ANY, /* '{' */ PATH_CHAR_CLASS_OTHER_VALID,
4325 /* '|' */ PATH_CHAR_CLASS_INVALID, /* '}' */ PATH_CHAR_CLASS_OTHER_VALID,
4326 /* '~' */ PATH_CHAR_CLASS_OTHER_VALID
4329 /*************************************************************************
4330 * @ [SHLWAPI.455]
4332 * Check if an ASCII char is of a certain class
4334 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4336 if ((unsigned)c > 0x7e)
4337 return class & PATH_CHAR_CLASS_OTHER_VALID;
4339 return class & SHELL_charclass[(unsigned)c];
4342 /*************************************************************************
4343 * @ [SHLWAPI.456]
4345 * Check if a Unicode char is of a certain class
4347 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4349 if (c > 0x7e)
4350 return class & PATH_CHAR_CLASS_OTHER_VALID;
4352 return class & SHELL_charclass[c];