oleaut32/tests: Test more return values.
[wine.git] / dlls / shlwapi / path.c
blob89a3572b1611760f8cc789a15308ca9d5a0a853a
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)
158 goto fail;
160 if (lpszDir)
161 if (!MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH))
162 goto fail;
164 if (lpszFile)
165 if (!MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH))
166 goto fail;
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 fail:
173 lpszDest[0] = 0;
174 return NULL;
177 /*************************************************************************
178 * PathCombineW [SHLWAPI.@]
180 * See PathCombineA.
182 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
184 WCHAR szTemp[MAX_PATH];
185 BOOL bUseBoth = FALSE, bStrip = FALSE;
187 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
189 /* Invalid parameters */
190 if (!lpszDest)
191 return NULL;
192 if (!lpszDir && !lpszFile)
194 lpszDest[0] = 0;
195 return NULL;
198 if ((!lpszFile || !*lpszFile) && lpszDir)
200 /* Use dir only */
201 lstrcpynW(szTemp, lpszDir, MAX_PATH);
203 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
205 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
207 /* Use file only */
208 lstrcpynW(szTemp, lpszFile, MAX_PATH);
210 else
212 bUseBoth = TRUE;
213 bStrip = TRUE;
216 else
217 bUseBoth = TRUE;
219 if (bUseBoth)
221 lstrcpynW(szTemp, lpszDir, MAX_PATH);
222 if (bStrip)
224 PathStripToRootW(szTemp);
225 lpszFile++; /* Skip '\' */
227 if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
229 lpszDest[0] = 0;
230 return NULL;
232 strcatW(szTemp, lpszFile);
235 PathCanonicalizeW(lpszDest, szTemp);
236 return lpszDest;
239 /*************************************************************************
240 * PathAddBackslashA [SHLWAPI.@]
242 * Append a backslash ('\') to a path if one doesn't exist.
244 * PARAMS
245 * lpszPath [I/O] The path to append a backslash to.
247 * RETURNS
248 * Success: The position of the last backslash in the path.
249 * Failure: NULL, if lpszPath is NULL or the path is too large.
251 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
253 size_t iLen;
254 LPSTR prev = lpszPath;
256 TRACE("(%s)\n",debugstr_a(lpszPath));
258 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
259 return NULL;
261 if (iLen)
263 do {
264 lpszPath = CharNextA(prev);
265 if (*lpszPath)
266 prev = lpszPath;
267 } while (*lpszPath);
268 if (*prev != '\\')
270 *lpszPath++ = '\\';
271 *lpszPath = '\0';
274 return lpszPath;
277 /*************************************************************************
278 * PathAddBackslashW [SHLWAPI.@]
280 * See PathAddBackslashA.
282 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
284 size_t iLen;
286 TRACE("(%s)\n",debugstr_w(lpszPath));
288 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
289 return NULL;
291 if (iLen)
293 lpszPath += iLen;
294 if (lpszPath[-1] != '\\')
296 *lpszPath++ = '\\';
297 *lpszPath = '\0';
300 return lpszPath;
303 /*************************************************************************
304 * PathBuildRootA [SHLWAPI.@]
306 * Create a root drive string (e.g. "A:\") from a drive number.
308 * PARAMS
309 * lpszPath [O] Destination for the drive string
311 * RETURNS
312 * lpszPath
314 * NOTES
315 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
317 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
319 TRACE("(%p,%d)\n", lpszPath, drive);
321 if (lpszPath && drive >= 0 && drive < 26)
323 lpszPath[0] = 'A' + drive;
324 lpszPath[1] = ':';
325 lpszPath[2] = '\\';
326 lpszPath[3] = '\0';
328 return lpszPath;
331 /*************************************************************************
332 * PathBuildRootW [SHLWAPI.@]
334 * See PathBuildRootA.
336 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
338 TRACE("(%p,%d)\n", lpszPath, drive);
340 if (lpszPath && drive >= 0 && drive < 26)
342 lpszPath[0] = 'A' + drive;
343 lpszPath[1] = ':';
344 lpszPath[2] = '\\';
345 lpszPath[3] = '\0';
347 return lpszPath;
350 /*************************************************************************
351 * PathFindFileNameA [SHLWAPI.@]
353 * Locate the start of the file name in a path
355 * PARAMS
356 * lpszPath [I] Path to search
358 * RETURNS
359 * A pointer to the first character of the file name
361 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
363 LPCSTR lastSlash = lpszPath;
365 TRACE("(%s)\n",debugstr_a(lpszPath));
367 while (lpszPath && *lpszPath)
369 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
370 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
371 lastSlash = lpszPath + 1;
372 lpszPath = CharNextA(lpszPath);
374 return (LPSTR)lastSlash;
377 /*************************************************************************
378 * PathFindFileNameW [SHLWAPI.@]
380 * See PathFindFileNameA.
382 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
384 LPCWSTR lastSlash = lpszPath;
386 TRACE("(%s)\n",debugstr_w(lpszPath));
388 while (lpszPath && *lpszPath)
390 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
391 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
392 lastSlash = lpszPath + 1;
393 lpszPath++;
395 return (LPWSTR)lastSlash;
398 /*************************************************************************
399 * PathFindExtensionA [SHLWAPI.@]
401 * Locate the start of the file extension in a path
403 * PARAMS
404 * lpszPath [I] The path to search
406 * RETURNS
407 * A pointer to the first character of the extension, the end of
408 * the string if the path has no extension, or NULL If lpszPath is NULL
410 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
412 LPCSTR lastpoint = NULL;
414 TRACE("(%s)\n", debugstr_a(lpszPath));
416 if (lpszPath)
418 while (*lpszPath)
420 if (*lpszPath == '\\' || *lpszPath==' ')
421 lastpoint = NULL;
422 else if (*lpszPath == '.')
423 lastpoint = lpszPath;
424 lpszPath = CharNextA(lpszPath);
427 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
430 /*************************************************************************
431 * PathFindExtensionW [SHLWAPI.@]
433 * See PathFindExtensionA.
435 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
437 LPCWSTR lastpoint = NULL;
439 TRACE("(%s)\n", debugstr_w(lpszPath));
441 if (lpszPath)
443 while (*lpszPath)
445 if (*lpszPath == '\\' || *lpszPath==' ')
446 lastpoint = NULL;
447 else if (*lpszPath == '.')
448 lastpoint = lpszPath;
449 lpszPath++;
452 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
455 /*************************************************************************
456 * PathGetArgsA [SHLWAPI.@]
458 * Find the next argument in a string delimited by spaces.
460 * PARAMS
461 * lpszPath [I] The string to search for arguments in
463 * RETURNS
464 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
466 * NOTES
467 * Spaces in quoted strings are ignored as delimiters.
469 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
471 BOOL bSeenQuote = FALSE;
473 TRACE("(%s)\n",debugstr_a(lpszPath));
475 if (lpszPath)
477 while (*lpszPath)
479 if ((*lpszPath==' ') && !bSeenQuote)
480 return (LPSTR)lpszPath + 1;
481 if (*lpszPath == '"')
482 bSeenQuote = !bSeenQuote;
483 lpszPath = CharNextA(lpszPath);
486 return (LPSTR)lpszPath;
489 /*************************************************************************
490 * PathGetArgsW [SHLWAPI.@]
492 * See PathGetArgsA.
494 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
496 BOOL bSeenQuote = FALSE;
498 TRACE("(%s)\n",debugstr_w(lpszPath));
500 if (lpszPath)
502 while (*lpszPath)
504 if ((*lpszPath==' ') && !bSeenQuote)
505 return (LPWSTR)lpszPath + 1;
506 if (*lpszPath == '"')
507 bSeenQuote = !bSeenQuote;
508 lpszPath++;
511 return (LPWSTR)lpszPath;
514 /*************************************************************************
515 * PathGetDriveNumberA [SHLWAPI.@]
517 * Return the drive number from a path
519 * PARAMS
520 * lpszPath [I] Path to get the drive number from
522 * RETURNS
523 * Success: The drive number corresponding to the drive in the path
524 * Failure: -1, if lpszPath contains no valid drive
526 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
528 TRACE ("(%s)\n",debugstr_a(lpszPath));
530 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
531 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
532 return tolower(*lpszPath) - 'a';
533 return -1;
536 /*************************************************************************
537 * PathGetDriveNumberW [SHLWAPI.@]
539 * See PathGetDriveNumberA.
541 int WINAPI PathGetDriveNumberW(const WCHAR *path)
543 WCHAR drive;
545 static const WCHAR nt_prefixW[] = {'\\','\\','?','\\'};
547 TRACE("(%s)\n", debugstr_w(path));
549 if (!path)
550 return -1;
552 if (!strncmpW(path, nt_prefixW, 4))
553 path += 4;
555 drive = tolowerW(path[0]);
556 if (drive < 'a' || drive > 'z' || path[1] != ':')
557 return -1;
559 return drive - 'a';
562 /*************************************************************************
563 * PathRemoveFileSpecA [SHLWAPI.@]
565 * Remove the file specification from a path.
567 * PARAMS
568 * lpszPath [I/O] Path to remove the file spec from
570 * RETURNS
571 * TRUE If the path was valid and modified
572 * FALSE Otherwise
574 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
576 LPSTR lpszFileSpec = lpszPath;
577 BOOL bModified = FALSE;
579 TRACE("(%s)\n",debugstr_a(lpszPath));
581 if(lpszPath)
583 /* Skip directory or UNC path */
584 if (*lpszPath == '\\')
585 lpszFileSpec = ++lpszPath;
586 if (*lpszPath == '\\')
587 lpszFileSpec = ++lpszPath;
589 while (*lpszPath)
591 if(*lpszPath == '\\')
592 lpszFileSpec = lpszPath; /* Skip dir */
593 else if(*lpszPath == ':')
595 lpszFileSpec = ++lpszPath; /* Skip drive */
596 if (*lpszPath == '\\')
597 lpszFileSpec++;
599 if (!(lpszPath = CharNextA(lpszPath)))
600 break;
603 if (*lpszFileSpec)
605 *lpszFileSpec = '\0';
606 bModified = TRUE;
609 return bModified;
612 /*************************************************************************
613 * PathRemoveFileSpecW [SHLWAPI.@]
615 * See PathRemoveFileSpecA.
617 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
619 LPWSTR lpszFileSpec = lpszPath;
620 BOOL bModified = FALSE;
622 TRACE("(%s)\n",debugstr_w(lpszPath));
624 if(lpszPath)
626 /* Skip directory or UNC path */
627 if (*lpszPath == '\\')
628 lpszFileSpec = ++lpszPath;
629 if (*lpszPath == '\\')
630 lpszFileSpec = ++lpszPath;
632 while (*lpszPath)
634 if(*lpszPath == '\\')
635 lpszFileSpec = lpszPath; /* Skip dir */
636 else if(*lpszPath == ':')
638 lpszFileSpec = ++lpszPath; /* Skip drive */
639 if (*lpszPath == '\\')
640 lpszFileSpec++;
642 lpszPath++;
645 if (*lpszFileSpec)
647 *lpszFileSpec = '\0';
648 bModified = TRUE;
651 return bModified;
654 /*************************************************************************
655 * PathStripPathA [SHLWAPI.@]
657 * Remove the initial path from the beginning of a filename
659 * PARAMS
660 * lpszPath [I/O] Path to remove the initial path from
662 * RETURNS
663 * Nothing.
665 void WINAPI PathStripPathA(LPSTR lpszPath)
667 TRACE("(%s)\n", debugstr_a(lpszPath));
669 if (lpszPath)
671 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
672 if(lpszFileName != lpszPath)
673 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
677 /*************************************************************************
678 * PathStripPathW [SHLWAPI.@]
680 * See PathStripPathA.
682 void WINAPI PathStripPathW(LPWSTR lpszPath)
684 LPWSTR lpszFileName;
686 TRACE("(%s)\n", debugstr_w(lpszPath));
687 lpszFileName = PathFindFileNameW(lpszPath);
688 if(lpszFileName != lpszPath)
689 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
692 /*************************************************************************
693 * PathStripToRootA [SHLWAPI.@]
695 * Reduce a path to its root.
697 * PARAMS
698 * lpszPath [I/O] the path to reduce
700 * RETURNS
701 * Success: TRUE if the stripped path is a root path
702 * Failure: FALSE if the path cannot be stripped or is NULL
704 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
706 TRACE("(%s)\n", debugstr_a(lpszPath));
708 if (!lpszPath)
709 return FALSE;
710 while(!PathIsRootA(lpszPath))
711 if (!PathRemoveFileSpecA(lpszPath))
712 return FALSE;
713 return TRUE;
716 /*************************************************************************
717 * PathStripToRootW [SHLWAPI.@]
719 * See PathStripToRootA.
721 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
723 TRACE("(%s)\n", debugstr_w(lpszPath));
725 if (!lpszPath)
726 return FALSE;
727 while(!PathIsRootW(lpszPath))
728 if (!PathRemoveFileSpecW(lpszPath))
729 return FALSE;
730 return TRUE;
733 /*************************************************************************
734 * PathRemoveArgsA [SHLWAPI.@]
736 * Strip space separated arguments from a path.
738 * PARAMS
739 * lpszPath [I/O] Path to remove arguments from
741 * RETURNS
742 * Nothing.
744 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
746 TRACE("(%s)\n",debugstr_a(lpszPath));
748 if(lpszPath)
750 LPSTR lpszArgs = PathGetArgsA(lpszPath);
751 if (*lpszArgs)
752 lpszArgs[-1] = '\0';
753 else
755 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
756 if(*lpszLastChar == ' ')
757 *lpszLastChar = '\0';
762 /*************************************************************************
763 * PathRemoveArgsW [SHLWAPI.@]
765 * See PathRemoveArgsA.
767 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
769 TRACE("(%s)\n",debugstr_w(lpszPath));
771 if(lpszPath)
773 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
774 if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
775 lpszArgs[-1] = '\0';
779 /*************************************************************************
780 * PathRemoveExtensionA [SHLWAPI.@]
782 * Remove the file extension from a path
784 * PARAMS
785 * lpszPath [I/O] Path to remove the extension from
787 * NOTES
788 * The NUL terminator must be written only if extension exists
789 * and if the pointed character is not already NUL.
791 * RETURNS
792 * Nothing.
794 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
796 TRACE("(%s)\n", debugstr_a(lpszPath));
798 if (lpszPath)
800 lpszPath = PathFindExtensionA(lpszPath);
801 if (lpszPath && *lpszPath != '\0')
802 *lpszPath = '\0';
806 /*************************************************************************
807 * PathRemoveExtensionW [SHLWAPI.@]
809 * See PathRemoveExtensionA.
811 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
813 TRACE("(%s)\n", debugstr_w(lpszPath));
815 if (lpszPath)
817 lpszPath = PathFindExtensionW(lpszPath);
818 if (lpszPath && *lpszPath != '\0')
819 *lpszPath = '\0';
823 /*************************************************************************
824 * PathRemoveBackslashA [SHLWAPI.@]
826 * Remove a trailing backslash from a path.
828 * PARAMS
829 * lpszPath [I/O] Path to remove backslash from
831 * RETURNS
832 * Success: A pointer to the end of the path
833 * Failure: NULL, if lpszPath is NULL
835 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
837 LPSTR szTemp = NULL;
839 TRACE("(%s)\n", debugstr_a(lpszPath));
841 if(lpszPath)
843 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
844 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
845 *szTemp = '\0';
847 return szTemp;
850 /*************************************************************************
851 * PathRemoveBackslashW [SHLWAPI.@]
853 * See PathRemoveBackslashA.
855 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
857 LPWSTR szTemp = NULL;
859 TRACE("(%s)\n", debugstr_w(lpszPath));
861 if(lpszPath)
863 szTemp = lpszPath + strlenW(lpszPath);
864 if (szTemp > lpszPath) szTemp--;
865 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
866 *szTemp = '\0';
868 return szTemp;
871 /*************************************************************************
872 * PathRemoveBlanksA [SHLWAPI.@]
874 * Remove Spaces from the start and end of a path.
876 * PARAMS
877 * lpszPath [I/O] Path to strip blanks from
879 * RETURNS
880 * Nothing.
882 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
884 TRACE("(%s)\n", debugstr_a(lpszPath));
886 if(lpszPath && *lpszPath)
888 LPSTR start = lpszPath;
890 while (*lpszPath == ' ')
891 lpszPath = CharNextA(lpszPath);
893 while(*lpszPath)
894 *start++ = *lpszPath++;
896 if (start != lpszPath)
897 while (start[-1] == ' ')
898 start--;
899 *start = '\0';
903 /*************************************************************************
904 * PathRemoveBlanksW [SHLWAPI.@]
906 * See PathRemoveBlanksA.
908 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
910 TRACE("(%s)\n", debugstr_w(lpszPath));
912 if(lpszPath && *lpszPath)
914 LPWSTR start = lpszPath;
916 while (*lpszPath == ' ')
917 lpszPath++;
919 while(*lpszPath)
920 *start++ = *lpszPath++;
922 if (start != lpszPath)
923 while (start[-1] == ' ')
924 start--;
925 *start = '\0';
929 /*************************************************************************
930 * PathQuoteSpacesA [SHLWAPI.@]
932 * Surround a path containing spaces in quotes.
934 * PARAMS
935 * lpszPath [I/O] Path to quote
937 * RETURNS
938 * Nothing.
940 * NOTES
941 * The path is not changed if it is invalid or has no spaces.
943 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
945 TRACE("(%s)\n", debugstr_a(lpszPath));
947 if(lpszPath && StrChrA(lpszPath,' '))
949 size_t iLen = strlen(lpszPath) + 1;
951 if (iLen + 2 < MAX_PATH)
953 memmove(lpszPath + 1, lpszPath, iLen);
954 lpszPath[0] = '"';
955 lpszPath[iLen] = '"';
956 lpszPath[iLen + 1] = '\0';
961 /*************************************************************************
962 * PathQuoteSpacesW [SHLWAPI.@]
964 * See PathQuoteSpacesA.
966 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
968 TRACE("(%s)\n", debugstr_w(lpszPath));
970 if(lpszPath && StrChrW(lpszPath,' '))
972 int iLen = strlenW(lpszPath) + 1;
974 if (iLen + 2 < MAX_PATH)
976 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
977 lpszPath[0] = '"';
978 lpszPath[iLen] = '"';
979 lpszPath[iLen + 1] = '\0';
984 /*************************************************************************
985 * PathUnquoteSpacesA [SHLWAPI.@]
987 * Remove quotes ("") from around a path, if present.
989 * PARAMS
990 * lpszPath [I/O] Path to strip quotes from
992 * RETURNS
993 * Nothing
995 * NOTES
996 * If the path contains a single quote only, an empty string will result.
997 * Otherwise quotes are only removed if they appear at the start and end
998 * of the path.
1000 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
1002 TRACE("(%s)\n", debugstr_a(lpszPath));
1004 if (lpszPath && *lpszPath == '"')
1006 DWORD dwLen = strlen(lpszPath) - 1;
1008 if (lpszPath[dwLen] == '"')
1010 lpszPath[dwLen] = '\0';
1011 for (; *lpszPath; lpszPath++)
1012 *lpszPath = lpszPath[1];
1017 /*************************************************************************
1018 * PathUnquoteSpacesW [SHLWAPI.@]
1020 * See PathUnquoteSpacesA.
1022 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
1024 TRACE("(%s)\n", debugstr_w(lpszPath));
1026 if (lpszPath && *lpszPath == '"')
1028 DWORD dwLen = strlenW(lpszPath) - 1;
1030 if (lpszPath[dwLen] == '"')
1032 lpszPath[dwLen] = '\0';
1033 for (; *lpszPath; lpszPath++)
1034 *lpszPath = lpszPath[1];
1039 /*************************************************************************
1040 * PathParseIconLocationA [SHLWAPI.@]
1042 * Parse the location of an icon from a path.
1044 * PARAMS
1045 * lpszPath [I/O] The path to parse the icon location from.
1047 * RETURNS
1048 * Success: The number of the icon
1049 * Failure: 0 if the path does not contain an icon location or is NULL
1051 * NOTES
1052 * The path has surrounding quotes and spaces removed regardless
1053 * of whether the call succeeds or not.
1055 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1057 int iRet = 0;
1058 LPSTR lpszComma;
1060 TRACE("(%s)\n", debugstr_a(lpszPath));
1062 if (lpszPath)
1064 if ((lpszComma = strchr(lpszPath, ',')))
1066 *lpszComma++ = '\0';
1067 iRet = StrToIntA(lpszComma);
1069 PathUnquoteSpacesA(lpszPath);
1070 PathRemoveBlanksA(lpszPath);
1072 return iRet;
1075 /*************************************************************************
1076 * PathParseIconLocationW [SHLWAPI.@]
1078 * See PathParseIconLocationA.
1080 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1082 int iRet = 0;
1083 LPWSTR lpszComma;
1085 TRACE("(%s)\n", debugstr_w(lpszPath));
1087 if (lpszPath)
1089 if ((lpszComma = StrChrW(lpszPath, ',')))
1091 *lpszComma++ = '\0';
1092 iRet = StrToIntW(lpszComma);
1094 PathUnquoteSpacesW(lpszPath);
1095 PathRemoveBlanksW(lpszPath);
1097 return iRet;
1100 /*************************************************************************
1101 * @ [SHLWAPI.4]
1103 * Unicode version of PathFileExistsDefExtA.
1105 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1107 static const WCHAR pszExts[][5] = { { '.', 'p', 'i', 'f', 0},
1108 { '.', 'c', 'o', 'm', 0},
1109 { '.', 'e', 'x', 'e', 0},
1110 { '.', 'b', 'a', 't', 0},
1111 { '.', 'l', 'n', 'k', 0},
1112 { '.', 'c', 'm', 'd', 0},
1113 { 0, 0, 0, 0, 0} };
1115 TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1117 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1118 return FALSE;
1120 if (dwWhich)
1122 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1123 if (!*szExt || dwWhich & 0x40)
1125 size_t iChoose = 0;
1126 int iLen = lstrlenW(lpszPath);
1127 if (iLen > (MAX_PATH - 5))
1128 return FALSE;
1129 while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1131 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1132 if (PathFileExistsW(lpszPath))
1133 return TRUE;
1134 iChoose++;
1135 dwWhich >>= 1;
1137 *(lpszPath + iLen) = (WCHAR)'\0';
1138 return FALSE;
1141 return PathFileExistsW(lpszPath);
1144 /*************************************************************************
1145 * @ [SHLWAPI.3]
1147 * Determine if a file exists locally and is of an executable type.
1149 * PARAMS
1150 * lpszPath [I/O] File to search for
1151 * dwWhich [I] Type of executable to search for
1153 * RETURNS
1154 * TRUE If the file was found. lpszPath contains the file name.
1155 * FALSE Otherwise.
1157 * NOTES
1158 * lpszPath is modified in place and must be at least MAX_PATH in length.
1159 * If the function returns FALSE, the path is modified to its original state.
1160 * If the given path contains an extension or dwWhich is 0, executable
1161 * extensions are not checked.
1163 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1164 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1165 * their own developers exclusive use. Monopoly, anyone?
1167 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1169 BOOL bRet = FALSE;
1171 TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1173 if (lpszPath)
1175 WCHAR szPath[MAX_PATH];
1176 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1177 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1178 if (bRet)
1179 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1181 return bRet;
1184 /*************************************************************************
1185 * SHLWAPI_PathFindInOtherDirs
1187 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1189 static BOOL SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1191 static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1192 static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1193 DWORD dwLenPATH;
1194 LPCWSTR lpszCurr;
1195 WCHAR *lpszPATH;
1196 WCHAR buff[MAX_PATH];
1198 TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1200 /* Try system directories */
1201 GetSystemDirectoryW(buff, MAX_PATH);
1202 if (!PathAppendW(buff, lpszFile))
1203 return FALSE;
1204 if (PathFileExistsDefExtW(buff, dwWhich))
1206 strcpyW(lpszFile, buff);
1207 return TRUE;
1209 GetWindowsDirectoryW(buff, MAX_PATH);
1210 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1211 return FALSE;
1212 if (PathFileExistsDefExtW(buff, dwWhich))
1214 strcpyW(lpszFile, buff);
1215 return TRUE;
1217 GetWindowsDirectoryW(buff, MAX_PATH);
1218 if (!PathAppendW(buff, lpszFile))
1219 return FALSE;
1220 if (PathFileExistsDefExtW(buff, dwWhich))
1222 strcpyW(lpszFile, buff);
1223 return TRUE;
1225 /* Try dirs listed in %PATH% */
1226 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1228 if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1229 return FALSE;
1231 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1232 lpszCurr = lpszPATH;
1233 while (lpszCurr)
1235 LPCWSTR lpszEnd = lpszCurr;
1236 LPWSTR pBuff = buff;
1238 while (*lpszEnd == ' ')
1239 lpszEnd++;
1240 while (*lpszEnd && *lpszEnd != ';')
1241 *pBuff++ = *lpszEnd++;
1242 *pBuff = '\0';
1244 if (*lpszEnd)
1245 lpszCurr = lpszEnd + 1;
1246 else
1247 lpszCurr = NULL; /* Last Path, terminate after this */
1249 if (!PathAppendW(buff, lpszFile))
1251 HeapFree(GetProcessHeap(), 0, lpszPATH);
1252 return FALSE;
1254 if (PathFileExistsDefExtW(buff, dwWhich))
1256 strcpyW(lpszFile, buff);
1257 HeapFree(GetProcessHeap(), 0, lpszPATH);
1258 return TRUE;
1261 HeapFree(GetProcessHeap(), 0, lpszPATH);
1262 return FALSE;
1265 /*************************************************************************
1266 * @ [SHLWAPI.5]
1268 * Search a range of paths for a specific type of executable.
1270 * PARAMS
1271 * lpszFile [I/O] File to search for
1272 * lppszOtherDirs [I] Other directories to look in
1273 * dwWhich [I] Type of executable to search for
1275 * RETURNS
1276 * Success: TRUE. The path to the executable is stored in lpszFile.
1277 * Failure: FALSE. The path to the executable is unchanged.
1279 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1281 WCHAR szFile[MAX_PATH];
1282 WCHAR buff[MAX_PATH];
1284 TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1286 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1287 return FALSE;
1289 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1291 /* Search provided directories first */
1292 if (lppszOtherDirs && *lppszOtherDirs)
1294 WCHAR szOther[MAX_PATH];
1295 LPCSTR *lpszOtherPath = lppszOtherDirs;
1297 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1299 MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1300 PathCombineW(buff, szOther, szFile);
1301 if (PathFileExistsDefExtW(buff, dwWhich))
1303 WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1304 return TRUE;
1306 lpszOtherPath++;
1309 /* Not found, try system and path dirs */
1310 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1312 WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1313 return TRUE;
1315 return FALSE;
1318 /*************************************************************************
1319 * @ [SHLWAPI.6]
1321 * Unicode version of PathFindOnPathExA.
1323 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1325 WCHAR buff[MAX_PATH];
1327 TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1329 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1330 return FALSE;
1332 /* Search provided directories first */
1333 if (lppszOtherDirs && *lppszOtherDirs)
1335 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1336 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1338 PathCombineW(buff, *lpszOtherPath, lpszFile);
1339 if (PathFileExistsDefExtW(buff, dwWhich))
1341 strcpyW(lpszFile, buff);
1342 return TRUE;
1344 lpszOtherPath++;
1347 /* Not found, try system and path dirs */
1348 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1351 /*************************************************************************
1352 * PathFindOnPathA [SHLWAPI.@]
1354 * Search a range of paths for an executable.
1356 * PARAMS
1357 * lpszFile [I/O] File to search for
1358 * lppszOtherDirs [I] Other directories to look in
1360 * RETURNS
1361 * Success: TRUE. The path to the executable is stored in lpszFile.
1362 * Failure: FALSE. The path to the executable is unchanged.
1364 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1366 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1367 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1370 /*************************************************************************
1371 * PathFindOnPathW [SHLWAPI.@]
1373 * See PathFindOnPathA.
1375 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1377 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1378 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1381 /*************************************************************************
1382 * PathCompactPathExA [SHLWAPI.@]
1384 * Compact a path into a given number of characters.
1386 * PARAMS
1387 * lpszDest [O] Destination for compacted path
1388 * lpszPath [I] Source path
1389 * cchMax [I] Maximum size of compacted path
1390 * dwFlags [I] Reserved
1392 * RETURNS
1393 * Success: TRUE. The compacted path is written to lpszDest.
1394 * Failure: FALSE. lpszPath is undefined.
1396 * NOTES
1397 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1399 * The Win32 version of this function contains a bug: When cchMax == 7,
1400 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1401 * implementation.
1403 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1404 * because Win32 will insert a "\" in lpszDest, even if one is
1405 * not present in the original path.
1407 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1408 UINT cchMax, DWORD dwFlags)
1410 BOOL bRet = FALSE;
1412 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1414 if (lpszPath && lpszDest)
1416 WCHAR szPath[MAX_PATH];
1417 WCHAR szDest[MAX_PATH];
1419 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1420 szDest[0] = '\0';
1421 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1422 WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1424 return bRet;
1427 /*************************************************************************
1428 * PathCompactPathExW [SHLWAPI.@]
1430 * See PathCompactPathExA.
1432 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1433 UINT cchMax, DWORD dwFlags)
1435 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1436 LPCWSTR lpszFile;
1437 DWORD dwLen, dwFileLen = 0;
1439 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1441 if (!lpszPath)
1442 return FALSE;
1444 if (!lpszDest)
1446 WARN("Invalid lpszDest would crash under Win32!\n");
1447 return FALSE;
1450 *lpszDest = '\0';
1452 if (cchMax < 2)
1453 return TRUE;
1455 dwLen = strlenW(lpszPath) + 1;
1457 if (dwLen < cchMax)
1459 /* Don't need to compact */
1460 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1461 return TRUE;
1464 /* Path must be compacted to fit into lpszDest */
1465 lpszFile = PathFindFileNameW(lpszPath);
1466 dwFileLen = lpszPath + dwLen - lpszFile;
1468 if (dwFileLen == dwLen)
1470 /* No root in psth */
1471 if (cchMax <= 4)
1473 while (--cchMax > 0) /* No room left for anything but ellipses */
1474 *lpszDest++ = '.';
1475 *lpszDest = '\0';
1476 return TRUE;
1478 /* Compact the file name with ellipses at the end */
1479 cchMax -= 4;
1480 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1481 strcpyW(lpszDest + cchMax, szEllipses);
1482 return TRUE;
1484 /* We have a root in the path */
1485 lpszFile--; /* Start compacted filename with the path separator */
1486 dwFileLen++;
1488 if (dwFileLen + 3 > cchMax)
1490 /* Compact the file name */
1491 if (cchMax <= 4)
1493 while (--cchMax > 0) /* No room left for anything but ellipses */
1494 *lpszDest++ = '.';
1495 *lpszDest = '\0';
1496 return TRUE;
1498 strcpyW(lpszDest, szEllipses);
1499 lpszDest += 3;
1500 cchMax -= 4;
1501 *lpszDest++ = *lpszFile++;
1502 if (cchMax <= 4)
1504 while (--cchMax > 0) /* No room left for anything but ellipses */
1505 *lpszDest++ = '.';
1506 *lpszDest = '\0';
1507 return TRUE;
1509 cchMax -= 4;
1510 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1511 strcpyW(lpszDest + cchMax, szEllipses);
1512 return TRUE;
1515 /* Only the root needs to be Compacted */
1516 dwLen = cchMax - dwFileLen - 3;
1517 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1518 strcpyW(lpszDest + dwLen, szEllipses);
1519 strcpyW(lpszDest + dwLen + 3, lpszFile);
1520 return TRUE;
1523 /*************************************************************************
1524 * PathIsRelativeA [SHLWAPI.@]
1526 * Determine if a path is a relative path.
1528 * PARAMS
1529 * lpszPath [I] Path to check
1531 * RETURNS
1532 * TRUE: The path is relative, or is invalid.
1533 * FALSE: The path is not relative.
1535 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1537 TRACE("(%s)\n",debugstr_a(lpszPath));
1539 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1540 return TRUE;
1541 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1542 return FALSE;
1543 return TRUE;
1546 /*************************************************************************
1547 * PathIsRelativeW [SHLWAPI.@]
1549 * See PathIsRelativeA.
1551 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1553 TRACE("(%s)\n",debugstr_w(lpszPath));
1555 if (!lpszPath || !*lpszPath)
1556 return TRUE;
1557 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1558 return FALSE;
1559 return TRUE;
1562 /*************************************************************************
1563 * PathIsRootA [SHLWAPI.@]
1565 * Determine if a path is a root path.
1567 * PARAMS
1568 * lpszPath [I] Path to check
1570 * RETURNS
1571 * TRUE If lpszPath is valid and a root path,
1572 * FALSE Otherwise
1574 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1576 TRACE("(%s)\n", debugstr_a(lpszPath));
1578 if (lpszPath && *lpszPath)
1580 if (*lpszPath == '\\')
1582 if (!lpszPath[1])
1583 return TRUE; /* \ */
1584 else if (lpszPath[1]=='\\')
1586 BOOL bSeenSlash = FALSE;
1587 lpszPath += 2;
1589 /* Check for UNC root path */
1590 while (*lpszPath)
1592 if (*lpszPath == '\\')
1594 if (bSeenSlash)
1595 return FALSE;
1596 bSeenSlash = TRUE;
1598 lpszPath = CharNextA(lpszPath);
1600 return TRUE;
1603 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1604 return TRUE; /* X:\ */
1606 return FALSE;
1609 /*************************************************************************
1610 * PathIsRootW [SHLWAPI.@]
1612 * See PathIsRootA.
1614 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1616 TRACE("(%s)\n", debugstr_w(lpszPath));
1618 if (lpszPath && *lpszPath)
1620 if (*lpszPath == '\\')
1622 if (!lpszPath[1])
1623 return TRUE; /* \ */
1624 else if (lpszPath[1]=='\\')
1626 BOOL bSeenSlash = FALSE;
1627 lpszPath += 2;
1629 /* Check for UNC root path */
1630 while (*lpszPath)
1632 if (*lpszPath == '\\')
1634 if (bSeenSlash)
1635 return FALSE;
1636 bSeenSlash = TRUE;
1638 lpszPath++;
1640 return TRUE;
1643 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1644 return TRUE; /* X:\ */
1646 return FALSE;
1649 /*************************************************************************
1650 * PathIsDirectoryA [SHLWAPI.@]
1652 * Determine if a path is a valid directory
1654 * PARAMS
1655 * lpszPath [I] Path to check.
1657 * RETURNS
1658 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1659 * FALSE if lpszPath is invalid or not a directory.
1661 * NOTES
1662 * Although this function is prototyped as returning a BOOL, it returns
1663 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1665 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1666 *| ...
1668 * will always fail.
1670 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1672 DWORD dwAttr;
1674 TRACE("(%s)\n", debugstr_a(lpszPath));
1676 if (!lpszPath || PathIsUNCServerA(lpszPath))
1677 return FALSE;
1679 if (PathIsUNCServerShareA(lpszPath))
1681 FIXME("UNC Server Share not yet supported - FAILING\n");
1682 return FALSE;
1685 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1686 return FALSE;
1687 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1690 /*************************************************************************
1691 * PathIsDirectoryW [SHLWAPI.@]
1693 * See PathIsDirectoryA.
1695 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1697 DWORD dwAttr;
1699 TRACE("(%s)\n", debugstr_w(lpszPath));
1701 if (!lpszPath || PathIsUNCServerW(lpszPath))
1702 return FALSE;
1704 if (PathIsUNCServerShareW(lpszPath))
1706 FIXME("UNC Server Share not yet supported - FAILING\n");
1707 return FALSE;
1710 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1711 return FALSE;
1712 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1715 /*************************************************************************
1716 * PathFileExistsA [SHLWAPI.@]
1718 * Determine if a file exists.
1720 * PARAMS
1721 * lpszPath [I] Path to check
1723 * RETURNS
1724 * TRUE If the file exists and is readable
1725 * FALSE Otherwise
1727 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1729 UINT iPrevErrMode;
1730 DWORD dwAttr;
1732 TRACE("(%s)\n",debugstr_a(lpszPath));
1734 if (!lpszPath)
1735 return FALSE;
1737 /* Prevent a dialog box if path is on a disk that has been ejected. */
1738 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1739 dwAttr = GetFileAttributesA(lpszPath);
1740 SetErrorMode(iPrevErrMode);
1741 return dwAttr != INVALID_FILE_ATTRIBUTES;
1744 /*************************************************************************
1745 * PathFileExistsW [SHLWAPI.@]
1747 * See PathFileExistsA.
1749 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1751 UINT iPrevErrMode;
1752 DWORD dwAttr;
1754 TRACE("(%s)\n",debugstr_w(lpszPath));
1756 if (!lpszPath)
1757 return FALSE;
1759 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1760 dwAttr = GetFileAttributesW(lpszPath);
1761 SetErrorMode(iPrevErrMode);
1762 return dwAttr != INVALID_FILE_ATTRIBUTES;
1765 /*************************************************************************
1766 * PathFileExistsAndAttributesA [SHLWAPI.445]
1768 * Determine if a file exists.
1770 * PARAMS
1771 * lpszPath [I] Path to check
1772 * dwAttr [O] attributes of file
1774 * RETURNS
1775 * TRUE If the file exists and is readable
1776 * FALSE Otherwise
1778 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1780 UINT iPrevErrMode;
1781 DWORD dwVal = 0;
1783 TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1785 if (dwAttr)
1786 *dwAttr = INVALID_FILE_ATTRIBUTES;
1788 if (!lpszPath)
1789 return FALSE;
1791 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1792 dwVal = GetFileAttributesA(lpszPath);
1793 SetErrorMode(iPrevErrMode);
1794 if (dwAttr)
1795 *dwAttr = dwVal;
1796 return (dwVal != INVALID_FILE_ATTRIBUTES);
1799 /*************************************************************************
1800 * PathFileExistsAndAttributesW [SHLWAPI.446]
1802 * See PathFileExistsA.
1804 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1806 UINT iPrevErrMode;
1807 DWORD dwVal;
1809 TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1811 if (!lpszPath)
1812 return FALSE;
1814 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1815 dwVal = GetFileAttributesW(lpszPath);
1816 SetErrorMode(iPrevErrMode);
1817 if (dwAttr)
1818 *dwAttr = dwVal;
1819 return (dwVal != INVALID_FILE_ATTRIBUTES);
1822 /*************************************************************************
1823 * PathMatchSingleMaskA [internal]
1825 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1827 while (*name && *mask && *mask!=';')
1829 if (*mask == '*')
1833 if (PathMatchSingleMaskA(name,mask+1))
1834 return TRUE; /* try substrings */
1835 } while (*name++);
1836 return FALSE;
1839 if (toupper(*mask) != toupper(*name) && *mask != '?')
1840 return FALSE;
1842 name = CharNextA(name);
1843 mask = CharNextA(mask);
1846 if (!*name)
1848 while (*mask == '*')
1849 mask++;
1850 if (!*mask || *mask == ';')
1851 return TRUE;
1853 return FALSE;
1856 /*************************************************************************
1857 * PathMatchSingleMaskW [internal]
1859 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1861 while (*name && *mask && *mask != ';')
1863 if (*mask == '*')
1867 if (PathMatchSingleMaskW(name,mask+1))
1868 return TRUE; /* try substrings */
1869 } while (*name++);
1870 return FALSE;
1873 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1874 return FALSE;
1876 name++;
1877 mask++;
1879 if (!*name)
1881 while (*mask == '*')
1882 mask++;
1883 if (!*mask || *mask == ';')
1884 return TRUE;
1886 return FALSE;
1889 /*************************************************************************
1890 * PathMatchSpecA [SHLWAPI.@]
1892 * Determine if a path matches one or more search masks.
1894 * PARAMS
1895 * lpszPath [I] Path to check
1896 * lpszMask [I] Search mask(s)
1898 * RETURNS
1899 * TRUE If lpszPath is valid and is matched
1900 * FALSE Otherwise
1902 * NOTES
1903 * Multiple search masks may be given if they are separated by ";". The
1904 * pattern "*.*" is treated specially in that it matches all paths (for
1905 * backwards compatibility with DOS).
1907 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1909 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1911 if (!lstrcmpA(lpszMask, "*.*"))
1912 return TRUE; /* Matches every path */
1914 while (*lpszMask)
1916 while (*lpszMask == ' ')
1917 lpszMask++; /* Eat leading spaces */
1919 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1920 return TRUE; /* Matches the current mask */
1922 while (*lpszMask && *lpszMask != ';')
1923 lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1925 if (*lpszMask == ';')
1926 lpszMask++;
1928 return FALSE;
1931 /*************************************************************************
1932 * PathMatchSpecW [SHLWAPI.@]
1934 * See PathMatchSpecA.
1936 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1938 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1940 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1942 if (!lstrcmpW(lpszMask, szStarDotStar))
1943 return TRUE; /* Matches every path */
1945 while (*lpszMask)
1947 while (*lpszMask == ' ')
1948 lpszMask++; /* Eat leading spaces */
1950 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1951 return TRUE; /* Matches the current path */
1953 while (*lpszMask && *lpszMask != ';')
1954 lpszMask++; /* masks separated by ';' */
1956 if (*lpszMask == ';')
1957 lpszMask++;
1959 return FALSE;
1962 /*************************************************************************
1963 * PathIsSameRootA [SHLWAPI.@]
1965 * Determine if two paths share the same root.
1967 * PARAMS
1968 * lpszPath1 [I] Source path
1969 * lpszPath2 [I] Path to compare with
1971 * RETURNS
1972 * TRUE If both paths are valid and share the same root.
1973 * FALSE If either path is invalid or the paths do not share the same root.
1975 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1977 LPCSTR lpszStart;
1978 int dwLen;
1980 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1982 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1983 return FALSE;
1985 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1986 if (lpszStart - lpszPath1 > dwLen)
1987 return FALSE; /* Paths not common up to length of the root */
1988 return TRUE;
1991 /*************************************************************************
1992 * PathIsSameRootW [SHLWAPI.@]
1994 * See PathIsSameRootA.
1996 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1998 LPCWSTR lpszStart;
1999 int dwLen;
2001 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
2003 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
2004 return FALSE;
2006 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
2007 if (lpszStart - lpszPath1 > dwLen)
2008 return FALSE; /* Paths not common up to length of the root */
2009 return TRUE;
2012 /*************************************************************************
2013 * PathIsContentTypeA [SHLWAPI.@]
2015 * Determine if a file is of a given registered content type.
2017 * PARAMS
2018 * lpszPath [I] File to check
2019 * lpszContentType [I] Content type to check for
2021 * RETURNS
2022 * TRUE If lpszPath is a given registered content type,
2023 * FALSE Otherwise.
2025 * NOTES
2026 * This function looks up the registered content type for lpszPath. If
2027 * a content type is registered, it is compared (case insensitively) to
2028 * lpszContentType. Only if this matches does the function succeed.
2030 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
2032 LPCSTR szExt;
2033 DWORD dwDummy;
2034 char szBuff[MAX_PATH];
2036 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2038 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2039 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2040 REG_NONE, szBuff, &dwDummy) &&
2041 !strcasecmp(lpszContentType, szBuff))
2043 return TRUE;
2045 return FALSE;
2048 /*************************************************************************
2049 * PathIsContentTypeW [SHLWAPI.@]
2051 * See PathIsContentTypeA.
2053 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2055 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2056 LPCWSTR szExt;
2057 DWORD dwDummy;
2058 WCHAR szBuff[MAX_PATH];
2060 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2062 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2063 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2064 REG_NONE, szBuff, &dwDummy) &&
2065 !strcmpiW(lpszContentType, szBuff))
2067 return TRUE;
2069 return FALSE;
2072 /*************************************************************************
2073 * PathIsFileSpecA [SHLWAPI.@]
2075 * Determine if a path is a file specification.
2077 * PARAMS
2078 * lpszPath [I] Path to check
2080 * RETURNS
2081 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
2082 * FALSE Otherwise.
2084 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2086 TRACE("(%s)\n", debugstr_a(lpszPath));
2088 if (!lpszPath)
2089 return FALSE;
2091 while (*lpszPath)
2093 if (*lpszPath == '\\' || *lpszPath == ':')
2094 return FALSE;
2095 lpszPath = CharNextA(lpszPath);
2097 return TRUE;
2100 /*************************************************************************
2101 * PathIsFileSpecW [SHLWAPI.@]
2103 * See PathIsFileSpecA.
2105 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2107 TRACE("(%s)\n", debugstr_w(lpszPath));
2109 if (!lpszPath)
2110 return FALSE;
2112 while (*lpszPath)
2114 if (*lpszPath == '\\' || *lpszPath == ':')
2115 return FALSE;
2116 lpszPath++;
2118 return TRUE;
2121 /*************************************************************************
2122 * PathIsPrefixA [SHLWAPI.@]
2124 * Determine if a path is a prefix of another.
2126 * PARAMS
2127 * lpszPrefix [I] Prefix
2128 * lpszPath [I] Path to check
2130 * RETURNS
2131 * TRUE If lpszPath has lpszPrefix as its prefix,
2132 * FALSE If either path is NULL or lpszPrefix is not a prefix
2134 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2136 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2138 if (lpszPrefix && lpszPath &&
2139 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2140 return TRUE;
2141 return FALSE;
2144 /*************************************************************************
2145 * PathIsPrefixW [SHLWAPI.@]
2147 * See PathIsPrefixA.
2149 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2151 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2153 if (lpszPrefix && lpszPath &&
2154 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2155 return TRUE;
2156 return FALSE;
2159 /*************************************************************************
2160 * PathIsSystemFolderA [SHLWAPI.@]
2162 * Determine if a path or file attributes are a system folder.
2164 * PARAMS
2165 * lpszPath [I] Path to check.
2166 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2168 * RETURNS
2169 * TRUE If lpszPath or dwAttrib are a system folder.
2170 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2172 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2174 TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2176 if (lpszPath && *lpszPath)
2177 dwAttrib = GetFileAttributesA(lpszPath);
2179 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2180 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2181 return FALSE;
2182 return TRUE;
2185 /*************************************************************************
2186 * PathIsSystemFolderW [SHLWAPI.@]
2188 * See PathIsSystemFolderA.
2190 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2192 TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2194 if (lpszPath && *lpszPath)
2195 dwAttrib = GetFileAttributesW(lpszPath);
2197 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2198 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2199 return FALSE;
2200 return TRUE;
2203 /*************************************************************************
2204 * PathIsUNCA [SHLWAPI.@]
2206 * Determine if a path is in UNC format.
2208 * PARAMS
2209 * lpszPath [I] Path to check
2211 * RETURNS
2212 * TRUE: The path is UNC.
2213 * FALSE: The path is not UNC or is NULL.
2215 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2217 TRACE("(%s)\n",debugstr_a(lpszPath));
2219 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2220 return TRUE;
2221 return FALSE;
2224 /*************************************************************************
2225 * PathIsUNCW [SHLWAPI.@]
2227 * See PathIsUNCA.
2229 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2231 TRACE("(%s)\n",debugstr_w(lpszPath));
2233 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2234 return TRUE;
2235 return FALSE;
2238 /*************************************************************************
2239 * PathIsUNCServerA [SHLWAPI.@]
2241 * Determine if a path is a UNC server name ("\\SHARENAME").
2243 * PARAMS
2244 * lpszPath [I] Path to check.
2246 * RETURNS
2247 * TRUE If lpszPath is a valid UNC server name.
2248 * FALSE Otherwise.
2250 * NOTES
2251 * This routine is bug compatible with Win32: Server names with a
2252 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2253 * Fixing this bug may break other shlwapi functions!
2255 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2257 TRACE("(%s)\n", debugstr_a(lpszPath));
2259 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2261 while (*lpszPath)
2263 if (*lpszPath == '\\')
2264 return FALSE;
2265 lpszPath = CharNextA(lpszPath);
2267 return TRUE;
2269 return FALSE;
2272 /*************************************************************************
2273 * PathIsUNCServerW [SHLWAPI.@]
2275 * See PathIsUNCServerA.
2277 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2279 TRACE("(%s)\n", debugstr_w(lpszPath));
2281 if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2283 return !strchrW( lpszPath + 2, '\\' );
2285 return FALSE;
2288 /*************************************************************************
2289 * PathIsUNCServerShareA [SHLWAPI.@]
2291 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2293 * PARAMS
2294 * lpszPath [I] Path to check.
2296 * RETURNS
2297 * TRUE If lpszPath is a valid UNC server share.
2298 * FALSE Otherwise.
2300 * NOTES
2301 * This routine is bug compatible with Win32: Server shares with a
2302 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2303 * Fixing this bug may break other shlwapi functions!
2305 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2307 TRACE("(%s)\n", debugstr_a(lpszPath));
2309 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2311 BOOL bSeenSlash = FALSE;
2312 while (*lpszPath)
2314 if (*lpszPath == '\\')
2316 if (bSeenSlash)
2317 return FALSE;
2318 bSeenSlash = TRUE;
2320 lpszPath = CharNextA(lpszPath);
2322 return bSeenSlash;
2324 return FALSE;
2327 /*************************************************************************
2328 * PathIsUNCServerShareW [SHLWAPI.@]
2330 * See PathIsUNCServerShareA.
2332 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2334 TRACE("(%s)\n", debugstr_w(lpszPath));
2336 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2338 BOOL bSeenSlash = FALSE;
2339 while (*lpszPath)
2341 if (*lpszPath == '\\')
2343 if (bSeenSlash)
2344 return FALSE;
2345 bSeenSlash = TRUE;
2347 lpszPath++;
2349 return bSeenSlash;
2351 return FALSE;
2354 /*************************************************************************
2355 * PathCanonicalizeA [SHLWAPI.@]
2357 * Convert a path to its canonical form.
2359 * PARAMS
2360 * lpszBuf [O] Output path
2361 * lpszPath [I] Path to canonicalize
2363 * RETURNS
2364 * Success: TRUE. lpszBuf contains the output path,
2365 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2367 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2369 BOOL bRet = FALSE;
2371 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2373 if (lpszBuf)
2374 *lpszBuf = '\0';
2376 if (!lpszBuf || !lpszPath)
2377 SetLastError(ERROR_INVALID_PARAMETER);
2378 else
2380 WCHAR szPath[MAX_PATH];
2381 WCHAR szBuff[MAX_PATH];
2382 int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2384 if (!ret) {
2385 WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
2386 return FALSE;
2388 bRet = PathCanonicalizeW(szBuff, szPath);
2389 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2391 return bRet;
2395 /*************************************************************************
2396 * PathCanonicalizeW [SHLWAPI.@]
2398 * See PathCanonicalizeA.
2400 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2402 LPWSTR lpszDst = lpszBuf;
2403 LPCWSTR lpszSrc = lpszPath;
2405 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2407 if (lpszBuf)
2408 *lpszDst = '\0';
2410 if (!lpszBuf || !lpszPath)
2412 SetLastError(ERROR_INVALID_PARAMETER);
2413 return FALSE;
2416 if (!*lpszPath)
2418 *lpszBuf++ = '\\';
2419 *lpszBuf = '\0';
2420 return TRUE;
2423 /* Copy path root */
2424 if (*lpszSrc == '\\')
2426 *lpszDst++ = *lpszSrc++;
2428 else if (*lpszSrc && lpszSrc[1] == ':')
2430 /* X:\ */
2431 *lpszDst++ = *lpszSrc++;
2432 *lpszDst++ = *lpszSrc++;
2433 if (*lpszSrc == '\\')
2434 *lpszDst++ = *lpszSrc++;
2437 /* Canonicalize the rest of the path */
2438 while (*lpszSrc)
2440 if (*lpszSrc == '.')
2442 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2444 lpszSrc += 2; /* Skip .\ */
2446 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2448 /* \.. backs up a directory, over the root if it has no \ following X:.
2449 * .. is ignored if it would remove a UNC server name or initial \\
2451 if (lpszDst != lpszBuf)
2453 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2454 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2455 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2457 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2459 lpszDst -= 2;
2460 while (lpszDst > lpszBuf && *lpszDst != '\\')
2461 lpszDst--;
2462 if (*lpszDst == '\\')
2463 lpszDst++; /* Reset to last '\' */
2464 else
2465 lpszDst = lpszBuf; /* Start path again from new root */
2467 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2468 lpszDst -= 2;
2470 while (lpszDst > lpszBuf && *lpszDst != '\\')
2471 lpszDst--;
2472 if (lpszDst == lpszBuf)
2474 *lpszDst++ = '\\';
2475 lpszSrc++;
2478 lpszSrc += 2; /* Skip .. in src path */
2480 else
2481 *lpszDst++ = *lpszSrc++;
2483 else
2484 *lpszDst++ = *lpszSrc++;
2486 /* Append \ to naked drive specs */
2487 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2488 *lpszDst++ = '\\';
2489 *lpszDst++ = '\0';
2490 return TRUE;
2493 /*************************************************************************
2494 * PathFindNextComponentA [SHLWAPI.@]
2496 * Find the next component in a path.
2498 * PARAMS
2499 * lpszPath [I] Path to find next component in
2501 * RETURNS
2502 * Success: A pointer to the next component, or the end of the string.
2503 * Failure: NULL, If lpszPath is invalid
2505 * NOTES
2506 * A 'component' is either a backslash character (\) or UNC marker (\\).
2507 * Because of this, relative paths (e.g "c:foo") are regarded as having
2508 * only one component.
2510 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2512 LPSTR lpszSlash;
2514 TRACE("(%s)\n", debugstr_a(lpszPath));
2516 if(!lpszPath || !*lpszPath)
2517 return NULL;
2519 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2521 if (lpszSlash[1] == '\\')
2522 lpszSlash++;
2523 return lpszSlash + 1;
2525 return (LPSTR)lpszPath + strlen(lpszPath);
2528 /*************************************************************************
2529 * PathFindNextComponentW [SHLWAPI.@]
2531 * See PathFindNextComponentA.
2533 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2535 LPWSTR lpszSlash;
2537 TRACE("(%s)\n", debugstr_w(lpszPath));
2539 if(!lpszPath || !*lpszPath)
2540 return NULL;
2542 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2544 if (lpszSlash[1] == '\\')
2545 lpszSlash++;
2546 return lpszSlash + 1;
2548 return (LPWSTR)lpszPath + strlenW(lpszPath);
2551 /*************************************************************************
2552 * PathAddExtensionA [SHLWAPI.@]
2554 * Add a file extension to a path
2556 * PARAMS
2557 * lpszPath [I/O] Path to add extension to
2558 * lpszExtension [I] Extension to add to lpszPath
2560 * RETURNS
2561 * TRUE If the path was modified,
2562 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2563 * extension already, or the new path length is too big.
2565 * FIXME
2566 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2567 * does not do this, so the behaviour was removed.
2569 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2571 size_t dwLen;
2573 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2575 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2576 return FALSE;
2578 dwLen = strlen(lpszPath);
2580 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2581 return FALSE;
2583 strcpy(lpszPath + dwLen, lpszExtension);
2584 return TRUE;
2587 /*************************************************************************
2588 * PathAddExtensionW [SHLWAPI.@]
2590 * See PathAddExtensionA.
2592 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2594 size_t dwLen;
2596 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2598 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2599 return FALSE;
2601 dwLen = strlenW(lpszPath);
2603 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2604 return FALSE;
2606 strcpyW(lpszPath + dwLen, lpszExtension);
2607 return TRUE;
2610 /*************************************************************************
2611 * PathMakePrettyA [SHLWAPI.@]
2613 * Convert an uppercase DOS filename into lowercase.
2615 * PARAMS
2616 * lpszPath [I/O] Path to convert.
2618 * RETURNS
2619 * TRUE If the path was an uppercase DOS path and was converted,
2620 * FALSE Otherwise.
2622 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2624 LPSTR pszIter = lpszPath;
2626 TRACE("(%s)\n", debugstr_a(lpszPath));
2628 if (!pszIter)
2629 return FALSE;
2631 if (*pszIter)
2635 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2636 return FALSE; /* Not DOS path */
2637 pszIter++;
2638 } while (*pszIter);
2639 pszIter = lpszPath + 1;
2640 while (*pszIter)
2642 *pszIter = tolower(*pszIter);
2643 pszIter++;
2646 return TRUE;
2649 /*************************************************************************
2650 * PathMakePrettyW [SHLWAPI.@]
2652 * See PathMakePrettyA.
2654 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2656 LPWSTR pszIter = lpszPath;
2658 TRACE("(%s)\n", debugstr_w(lpszPath));
2660 if (!pszIter)
2661 return FALSE;
2663 if (*pszIter)
2667 if (islowerW(*pszIter))
2668 return FALSE; /* Not DOS path */
2669 pszIter++;
2670 } while (*pszIter);
2671 pszIter = lpszPath + 1;
2672 while (*pszIter)
2674 *pszIter = tolowerW(*pszIter);
2675 pszIter++;
2678 return TRUE;
2681 /*************************************************************************
2682 * PathCommonPrefixA [SHLWAPI.@]
2684 * Determine the length of the common prefix between two paths.
2686 * PARAMS
2687 * lpszFile1 [I] First path for comparison
2688 * lpszFile2 [I] Second path for comparison
2689 * achPath [O] Destination for common prefix string
2691 * RETURNS
2692 * The length of the common prefix. This is 0 if there is no common
2693 * prefix between the paths or if any parameters are invalid. If the prefix
2694 * is non-zero and achPath is not NULL, achPath is filled with the common
2695 * part of the prefix and NUL terminated.
2697 * NOTES
2698 * A common prefix of 2 is always returned as 3. It is thus possible for
2699 * the length returned to be invalid (i.e. Longer than one or both of the
2700 * strings given as parameters). This Win32 behaviour has been implemented
2701 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2702 * To work around this when using this function, always check that the byte
2703 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2705 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2707 size_t iLen = 0;
2708 LPCSTR lpszIter1 = lpszFile1;
2709 LPCSTR lpszIter2 = lpszFile2;
2711 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2713 if (achPath)
2714 *achPath = '\0';
2716 if (!lpszFile1 || !lpszFile2)
2717 return 0;
2719 /* Handle roots first */
2720 if (PathIsUNCA(lpszFile1))
2722 if (!PathIsUNCA(lpszFile2))
2723 return 0;
2724 lpszIter1 += 2;
2725 lpszIter2 += 2;
2727 else if (PathIsUNCA(lpszFile2))
2728 return 0; /* Know already lpszFile1 is not UNC */
2732 /* Update len */
2733 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2734 (!*lpszIter2 || *lpszIter2 == '\\'))
2735 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2737 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2738 break; /* Strings differ at this point */
2740 lpszIter1++;
2741 lpszIter2++;
2742 } while (1);
2744 if (iLen == 2)
2745 iLen++; /* Feature/Bug compatible with Win32 */
2747 if (iLen && achPath)
2749 memcpy(achPath,lpszFile1,iLen);
2750 achPath[iLen] = '\0';
2752 return iLen;
2755 /*************************************************************************
2756 * PathCommonPrefixW [SHLWAPI.@]
2758 * See PathCommonPrefixA.
2760 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2762 size_t iLen = 0;
2763 LPCWSTR lpszIter1 = lpszFile1;
2764 LPCWSTR lpszIter2 = lpszFile2;
2766 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2768 if (achPath)
2769 *achPath = '\0';
2771 if (!lpszFile1 || !lpszFile2)
2772 return 0;
2774 /* Handle roots first */
2775 if (PathIsUNCW(lpszFile1))
2777 if (!PathIsUNCW(lpszFile2))
2778 return 0;
2779 lpszIter1 += 2;
2780 lpszIter2 += 2;
2782 else if (PathIsUNCW(lpszFile2))
2783 return 0; /* Know already lpszFile1 is not UNC */
2787 /* Update len */
2788 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2789 (!*lpszIter2 || *lpszIter2 == '\\'))
2790 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2792 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2793 break; /* Strings differ at this point */
2795 lpszIter1++;
2796 lpszIter2++;
2797 } while (1);
2799 if (iLen == 2)
2800 iLen++; /* Feature/Bug compatible with Win32 */
2802 if (iLen && achPath)
2804 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2805 achPath[iLen] = '\0';
2807 return iLen;
2810 /*************************************************************************
2811 * PathCompactPathA [SHLWAPI.@]
2813 * Make a path fit into a given width when printed to a DC.
2815 * PARAMS
2816 * hDc [I] Destination DC
2817 * lpszPath [I/O] Path to be printed to hDc
2818 * dx [I] Desired width
2820 * RETURNS
2821 * TRUE If the path was modified/went well.
2822 * FALSE Otherwise.
2824 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2826 BOOL bRet = FALSE;
2828 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2830 if (lpszPath)
2832 WCHAR szPath[MAX_PATH];
2833 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2834 bRet = PathCompactPathW(hDC, szPath, dx);
2835 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2837 return bRet;
2840 /*************************************************************************
2841 * PathCompactPathW [SHLWAPI.@]
2843 * See PathCompactPathA.
2845 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2847 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2848 BOOL bRet = TRUE;
2849 HDC hdc = 0;
2850 WCHAR buff[MAX_PATH];
2851 SIZE size;
2852 DWORD dwLen;
2854 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2856 if (!lpszPath)
2857 return FALSE;
2859 if (!hDC)
2860 hdc = hDC = GetDC(0);
2862 /* Get the length of the whole path */
2863 dwLen = strlenW(lpszPath);
2864 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2866 if ((UINT)size.cx > dx)
2868 /* Path too big, must reduce it */
2869 LPWSTR sFile;
2870 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2872 sFile = PathFindFileNameW(lpszPath);
2873 if (sFile != lpszPath) sFile--;
2875 /* Get the size of ellipses */
2876 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2877 dwEllipsesLen = size.cx;
2878 /* Get the size of the file name */
2879 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2880 dwPathLen = size.cx;
2882 if (sFile != lpszPath)
2884 LPWSTR sPath = sFile;
2885 BOOL bEllipses = FALSE;
2887 /* The path includes a file name. Include as much of the path prior to
2888 * the file name as possible, allowing for the ellipses, e.g:
2889 * c:\some very long path\filename ==> c:\some v...\filename
2891 lstrcpynW(buff, sFile, MAX_PATH);
2895 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2897 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2898 dwTotalLen += size.cx;
2899 if (dwTotalLen <= dx)
2900 break;
2901 sPath--;
2902 if (!bEllipses)
2904 bEllipses = TRUE;
2905 sPath -= 2;
2907 } while (sPath > lpszPath);
2909 if (sPath > lpszPath)
2911 if (bEllipses)
2913 strcpyW(sPath, szEllipses);
2914 strcpyW(sPath+3, buff);
2916 bRet = TRUE;
2917 goto end;
2919 strcpyW(lpszPath, szEllipses);
2920 strcpyW(lpszPath+3, buff);
2921 bRet = FALSE;
2922 goto end;
2925 /* Trim the path by adding ellipses to the end, e.g:
2926 * A very long file name.txt ==> A very...
2928 dwLen = strlenW(lpszPath);
2930 if (dwLen > MAX_PATH - 3)
2931 dwLen = MAX_PATH - 3;
2932 lstrcpynW(buff, sFile, dwLen);
2934 do {
2935 dwLen--;
2936 GetTextExtentPointW(hDC, buff, dwLen, &size);
2937 } while (dwLen && size.cx + dwEllipsesLen > dx);
2939 if (!dwLen)
2941 DWORD dwWritten = 0;
2943 dwEllipsesLen /= 3; /* Size of a single '.' */
2945 /* Write as much of the Ellipses string as possible */
2946 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2948 *lpszPath++ = '.';
2949 dwWritten += dwEllipsesLen;
2950 dwLen++;
2952 *lpszPath = '\0';
2953 bRet = FALSE;
2955 else
2957 strcpyW(buff + dwLen, szEllipses);
2958 strcpyW(lpszPath, buff);
2962 end:
2963 if (hdc)
2964 ReleaseDC(0, hdc);
2966 return bRet;
2969 /*************************************************************************
2970 * PathGetCharTypeA [SHLWAPI.@]
2972 * Categorise a character from a file path.
2974 * PARAMS
2975 * ch [I] Character to get the type of
2977 * RETURNS
2978 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2980 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2982 return PathGetCharTypeW(ch);
2985 /*************************************************************************
2986 * PathGetCharTypeW [SHLWAPI.@]
2988 * See PathGetCharTypeA.
2990 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2992 UINT flags = 0;
2994 TRACE("(%d)\n", ch);
2996 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2997 ch == '"' || ch == '|' || ch == '/')
2998 flags = GCT_INVALID; /* Invalid */
2999 else if (ch == '*' || ch=='?')
3000 flags = GCT_WILD; /* Wildchars */
3001 else if ((ch == '\\') || (ch == ':'))
3002 return GCT_SEPARATOR; /* Path separators */
3003 else
3005 if (ch < 126)
3007 if (((ch & 0x1) && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
3008 ch == '.' || ch == '@' || ch == '^' ||
3009 ch == '\'' || ch == 130 || ch == '`')
3010 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
3012 else
3013 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
3014 flags |= GCT_LFNCHAR; /* Valid for long file names */
3016 return flags;
3019 /*************************************************************************
3020 * SHLWAPI_UseSystemForSystemFolders
3022 * Internal helper for PathMakeSystemFolderW.
3024 static BOOL SHLWAPI_UseSystemForSystemFolders(void)
3026 static BOOL bCheckedReg = FALSE;
3027 static BOOL bUseSystemForSystemFolders = FALSE;
3029 if (!bCheckedReg)
3031 bCheckedReg = TRUE;
3033 /* Key tells Win what file attributes to use on system folders */
3034 if (SHGetValueA(HKEY_LOCAL_MACHINE,
3035 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
3036 "UseSystemForSystemFolders", 0, 0, 0))
3037 bUseSystemForSystemFolders = TRUE;
3039 return bUseSystemForSystemFolders;
3042 /*************************************************************************
3043 * PathMakeSystemFolderA [SHLWAPI.@]
3045 * Set system folder attribute for a path.
3047 * PARAMS
3048 * lpszPath [I] The path to turn into a system folder
3050 * RETURNS
3051 * TRUE If the path was changed to/already was a system folder
3052 * FALSE If the path is invalid or SetFileAttributesA() fails
3054 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3056 BOOL bRet = FALSE;
3058 TRACE("(%s)\n", debugstr_a(lpszPath));
3060 if (lpszPath && *lpszPath)
3062 WCHAR szPath[MAX_PATH];
3063 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3064 bRet = PathMakeSystemFolderW(szPath);
3066 return bRet;
3069 /*************************************************************************
3070 * PathMakeSystemFolderW [SHLWAPI.@]
3072 * See PathMakeSystemFolderA.
3074 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3076 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3077 WCHAR buff[MAX_PATH];
3079 TRACE("(%s)\n", debugstr_w(lpszPath));
3081 if (!lpszPath || !*lpszPath)
3082 return FALSE;
3084 /* If the directory is already a system directory, don't do anything */
3085 GetSystemDirectoryW(buff, MAX_PATH);
3086 if (!strcmpW(buff, lpszPath))
3087 return TRUE;
3089 GetWindowsDirectoryW(buff, MAX_PATH);
3090 if (!strcmpW(buff, lpszPath))
3091 return TRUE;
3093 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3094 if (SHLWAPI_UseSystemForSystemFolders())
3095 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3097 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3098 return FALSE;
3100 /* Change file attributes to system attributes */
3101 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3102 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3105 /*************************************************************************
3106 * PathRenameExtensionA [SHLWAPI.@]
3108 * Swap the file extension in a path with another extension.
3110 * PARAMS
3111 * lpszPath [I/O] Path to swap the extension in
3112 * lpszExt [I] The new extension
3114 * RETURNS
3115 * TRUE if lpszPath was modified,
3116 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3118 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3120 LPSTR lpszExtension;
3122 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3124 lpszExtension = PathFindExtensionA(lpszPath);
3126 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3127 return FALSE;
3129 strcpy(lpszExtension, lpszExt);
3130 return TRUE;
3133 /*************************************************************************
3134 * PathRenameExtensionW [SHLWAPI.@]
3136 * See PathRenameExtensionA.
3138 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3140 LPWSTR lpszExtension;
3142 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3144 lpszExtension = PathFindExtensionW(lpszPath);
3146 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3147 return FALSE;
3149 strcpyW(lpszExtension, lpszExt);
3150 return TRUE;
3153 /*************************************************************************
3154 * PathSearchAndQualifyA [SHLWAPI.@]
3156 * Determine if a given path is correct and fully qualified.
3158 * PARAMS
3159 * lpszPath [I] Path to check
3160 * lpszBuf [O] Output for correct path
3161 * cchBuf [I] Size of lpszBuf
3163 * RETURNS
3164 * Unknown.
3166 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3168 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3170 if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3171 return TRUE;
3172 return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3175 /*************************************************************************
3176 * PathSearchAndQualifyW [SHLWAPI.@]
3178 * See PathSearchAndQualifyA.
3180 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3182 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3184 if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3185 return TRUE;
3186 return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3189 /*************************************************************************
3190 * PathSkipRootA [SHLWAPI.@]
3192 * Return the portion of a path following the drive letter or mount point.
3194 * PARAMS
3195 * lpszPath [I] The path to skip on
3197 * RETURNS
3198 * Success: A pointer to the next character after the root.
3199 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3201 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3203 TRACE("(%s)\n", debugstr_a(lpszPath));
3205 if (!lpszPath || !*lpszPath)
3206 return NULL;
3208 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3210 /* Network share: skip share server and mount point */
3211 lpszPath += 2;
3212 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3213 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3214 lpszPath++;
3215 return (LPSTR)lpszPath;
3218 if (IsDBCSLeadByte(*lpszPath))
3219 return NULL;
3221 /* Check x:\ */
3222 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3223 return (LPSTR)lpszPath + 3;
3224 return NULL;
3227 /*************************************************************************
3228 * PathSkipRootW [SHLWAPI.@]
3230 * See PathSkipRootA.
3232 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3234 TRACE("(%s)\n", debugstr_w(lpszPath));
3236 if (!lpszPath || !*lpszPath)
3237 return NULL;
3239 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3241 /* Network share: skip share server and mount point */
3242 lpszPath += 2;
3243 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3244 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3245 lpszPath++;
3246 return (LPWSTR)lpszPath;
3249 /* Check x:\ */
3250 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3251 return (LPWSTR)lpszPath + 3;
3252 return NULL;
3255 /*************************************************************************
3256 * PathCreateFromUrlA [SHLWAPI.@]
3258 * See PathCreateFromUrlW
3260 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3261 LPDWORD pcchPath, DWORD dwReserved)
3263 WCHAR bufW[MAX_PATH];
3264 WCHAR *pathW = bufW;
3265 UNICODE_STRING urlW;
3266 HRESULT ret;
3267 DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3269 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3270 return E_INVALIDARG;
3272 if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3273 return E_INVALIDARG;
3274 if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3275 pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3276 ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3278 if(ret == S_OK) {
3279 RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3280 if(*pcchPath > lenA) {
3281 RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3282 pszPath[lenA] = 0;
3283 *pcchPath = lenA;
3284 } else {
3285 *pcchPath = lenA + 1;
3286 ret = E_POINTER;
3289 if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3290 RtlFreeUnicodeString(&urlW);
3291 return ret;
3294 /*************************************************************************
3295 * PathCreateFromUrlW [SHLWAPI.@]
3297 * Create a path from a URL
3299 * PARAMS
3300 * lpszUrl [I] URL to convert into a path
3301 * lpszPath [O] Output buffer for the resulting Path
3302 * pcchPath [I] Length of lpszPath
3303 * dwFlags [I] Flags controlling the conversion
3305 * RETURNS
3306 * Success: S_OK. lpszPath contains the URL in path format,
3307 * Failure: An HRESULT error code such as E_INVALIDARG.
3309 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3310 LPDWORD pcchPath, DWORD dwReserved)
3312 static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3313 static const WCHAR localhost[] = { 'l','o','c','a','l','h','o','s','t',0 };
3314 DWORD nslashes, unescape, len;
3315 const WCHAR *src;
3316 WCHAR *tpath, *dst;
3317 HRESULT ret;
3319 TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3321 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3322 return E_INVALIDARG;
3324 if (lstrlenW(pszUrl) < 5)
3325 return E_INVALIDARG;
3327 if (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pszUrl, 5,
3328 file_colon, 5) != CSTR_EQUAL)
3329 return E_INVALIDARG;
3330 pszUrl += 5;
3331 ret = S_OK;
3333 src = pszUrl;
3334 nslashes = 0;
3335 while (*src == '/' || *src == '\\') {
3336 nslashes++;
3337 src++;
3340 /* We need a temporary buffer so we can compute what size to ask for.
3341 * We know that the final string won't be longer than the current pszUrl
3342 * plus at most two backslashes. All the other transformations make it
3343 * shorter.
3345 len = 2 + lstrlenW(pszUrl) + 1;
3346 if (*pcchPath < len)
3347 tpath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3348 else
3349 tpath = pszPath;
3351 len = 0;
3352 dst = tpath;
3353 unescape = 1;
3354 switch (nslashes)
3356 case 0:
3357 /* 'file:' + escaped DOS path */
3358 break;
3359 case 1:
3360 /* 'file:/' + escaped DOS path */
3361 /* fall through */
3362 case 3:
3363 /* 'file:///' (implied localhost) + escaped DOS path */
3364 if (!isalphaW(*src) || (src[1] != ':' && src[1] != '|'))
3365 src -= 1;
3366 break;
3367 case 2:
3368 if (lstrlenW(src) >= 10 && CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE,
3369 src, 9, localhost, 9) == CSTR_EQUAL && (src[9] == '/' || src[9] == '\\'))
3371 /* 'file://localhost/' + escaped DOS path */
3372 src += 10;
3374 else if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3376 /* 'file://' + unescaped DOS path */
3377 unescape = 0;
3379 else
3381 /* 'file://hostname:port/path' (where path is escaped)
3382 * or 'file:' + escaped UNC path (\\server\share\path)
3383 * The second form is clearly specific to Windows and it might
3384 * even be doing a network lookup to try to figure it out.
3386 while (*src && *src != '/' && *src != '\\')
3387 src++;
3388 len = src - pszUrl;
3389 StrCpyNW(dst, pszUrl, len + 1);
3390 dst += len;
3391 if (*src && isalphaW(src[1]) && (src[2] == ':' || src[2] == '|'))
3393 /* 'Forget' to add a trailing '/', just like Windows */
3394 src++;
3397 break;
3398 case 4:
3399 /* 'file://' + unescaped UNC path (\\server\share\path) */
3400 unescape = 0;
3401 if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3402 break;
3403 /* fall through */
3404 default:
3405 /* 'file:/...' + escaped UNC path (\\server\share\path) */
3406 src -= 2;
3409 /* Copy the remainder of the path */
3410 len += lstrlenW(src);
3411 StrCpyW(dst, src);
3413 /* First do the Windows-specific path conversions */
3414 for (dst = tpath; *dst; dst++)
3415 if (*dst == '/') *dst = '\\';
3416 if (isalphaW(*tpath) && tpath[1] == '|')
3417 tpath[1] = ':'; /* c| -> c: */
3419 /* And only then unescape the path (i.e. escaped slashes are left as is) */
3420 if (unescape)
3422 ret = UrlUnescapeW(tpath, NULL, &len, URL_UNESCAPE_INPLACE);
3423 if (ret == S_OK)
3425 /* When working in-place UrlUnescapeW() does not set len */
3426 len = lstrlenW(tpath);
3430 if (*pcchPath < len + 1)
3432 ret = E_POINTER;
3433 *pcchPath = len + 1;
3435 else
3437 *pcchPath = len;
3438 if (tpath != pszPath)
3439 StrCpyW(pszPath, tpath);
3441 if (tpath != pszPath)
3442 HeapFree(GetProcessHeap(), 0, tpath);
3444 TRACE("Returning (%u) %s\n", *pcchPath, debugstr_w(pszPath));
3445 return ret;
3448 /*************************************************************************
3449 * PathCreateFromUrlAlloc [SHLWAPI.@]
3451 HRESULT WINAPI PathCreateFromUrlAlloc(LPCWSTR pszUrl, LPWSTR *pszPath,
3452 DWORD dwReserved)
3454 WCHAR pathW[MAX_PATH];
3455 DWORD size;
3456 HRESULT hr;
3458 size = MAX_PATH;
3459 hr = PathCreateFromUrlW(pszUrl, pathW, &size, dwReserved);
3460 if (SUCCEEDED(hr))
3462 /* Yes, this is supposed to crash if pszPath is NULL */
3463 *pszPath = StrDupW(pathW);
3465 return hr;
3468 /*************************************************************************
3469 * PathRelativePathToA [SHLWAPI.@]
3471 * Create a relative path from one path to another.
3473 * PARAMS
3474 * lpszPath [O] Destination for relative path
3475 * lpszFrom [I] Source path
3476 * dwAttrFrom [I] File attribute of source path
3477 * lpszTo [I] Destination path
3478 * dwAttrTo [I] File attributes of destination path
3480 * RETURNS
3481 * TRUE If a relative path can be formed. lpszPath contains the new path
3482 * FALSE If the paths are not relative or any parameters are invalid
3484 * NOTES
3485 * lpszTo should be at least MAX_PATH in length.
3487 * Calling this function with relative paths for lpszFrom or lpszTo may
3488 * give erroneous results.
3490 * The Win32 version of this function contains a bug where the lpszTo string
3491 * may be referenced 1 byte beyond the end of the string. As a result random
3492 * garbage may be written to the output path, depending on what lies beyond
3493 * the last byte of the string. This bug occurs because of the behaviour of
3494 * PathCommonPrefix() (see notes for that function), and no workaround seems
3495 * possible with Win32.
3497 * This bug has been fixed here, so for example the relative path from "\\"
3498 * to "\\" is correctly determined as "." in this implementation.
3500 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3501 LPCSTR lpszTo, DWORD dwAttrTo)
3503 BOOL bRet = FALSE;
3505 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3506 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3508 if(lpszPath && lpszFrom && lpszTo)
3510 WCHAR szPath[MAX_PATH];
3511 WCHAR szFrom[MAX_PATH];
3512 WCHAR szTo[MAX_PATH];
3513 MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3514 MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3515 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3516 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3518 return bRet;
3521 /*************************************************************************
3522 * PathRelativePathToW [SHLWAPI.@]
3524 * See PathRelativePathToA.
3526 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3527 LPCWSTR lpszTo, DWORD dwAttrTo)
3529 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3530 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3531 WCHAR szFrom[MAX_PATH];
3532 WCHAR szTo[MAX_PATH];
3533 DWORD dwLen;
3535 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3536 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3538 if(!lpszPath || !lpszFrom || !lpszTo)
3539 return FALSE;
3541 *lpszPath = '\0';
3542 lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3543 lstrcpynW(szTo, lpszTo, MAX_PATH);
3545 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3546 PathRemoveFileSpecW(szFrom);
3547 if(!(dwAttrTo & FILE_ATTRIBUTE_DIRECTORY))
3548 PathRemoveFileSpecW(szTo);
3550 /* Paths can only be relative if they have a common root */
3551 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3552 return FALSE;
3554 /* Strip off lpszFrom components to the root, by adding "..\" */
3555 lpszFrom = szFrom + dwLen;
3556 if (!*lpszFrom)
3558 lpszPath[0] = '.';
3559 lpszPath[1] = '\0';
3561 if (*lpszFrom == '\\')
3562 lpszFrom++;
3564 while (*lpszFrom)
3566 lpszFrom = PathFindNextComponentW(lpszFrom);
3567 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3570 /* From the root add the components of lpszTo */
3571 lpszTo += dwLen;
3572 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3573 * this function.
3575 if (*lpszTo && lpszTo[-1])
3577 if (*lpszTo != '\\')
3578 lpszTo--;
3579 dwLen = strlenW(lpszPath);
3580 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3582 *lpszPath = '\0';
3583 return FALSE;
3585 strcpyW(lpszPath + dwLen, lpszTo);
3587 return TRUE;
3590 /*************************************************************************
3591 * PathUnmakeSystemFolderA [SHLWAPI.@]
3593 * Remove the system folder attributes from a path.
3595 * PARAMS
3596 * lpszPath [I] The path to remove attributes from
3598 * RETURNS
3599 * Success: TRUE.
3600 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3601 * SetFileAttributesA() fails.
3603 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3605 DWORD dwAttr;
3607 TRACE("(%s)\n", debugstr_a(lpszPath));
3609 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3610 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3611 return FALSE;
3613 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3614 return SetFileAttributesA(lpszPath, dwAttr);
3617 /*************************************************************************
3618 * PathUnmakeSystemFolderW [SHLWAPI.@]
3620 * See PathUnmakeSystemFolderA.
3622 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3624 DWORD dwAttr;
3626 TRACE("(%s)\n", debugstr_w(lpszPath));
3628 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3629 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3630 return FALSE;
3632 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3633 return SetFileAttributesW(lpszPath, dwAttr);
3637 /*************************************************************************
3638 * PathSetDlgItemPathA [SHLWAPI.@]
3640 * Set the text of a dialog item to a path, shrinking the path to fit
3641 * if it is too big for the item.
3643 * PARAMS
3644 * hDlg [I] Dialog handle
3645 * id [I] ID of item in the dialog
3646 * lpszPath [I] Path to set as the items text
3648 * RETURNS
3649 * Nothing.
3651 * NOTES
3652 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3653 * window text is erased).
3655 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3657 WCHAR szPath[MAX_PATH];
3659 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3661 if (lpszPath)
3662 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3663 else
3664 szPath[0] = '\0';
3665 PathSetDlgItemPathW(hDlg, id, szPath);
3668 /*************************************************************************
3669 * PathSetDlgItemPathW [SHLWAPI.@]
3671 * See PathSetDlgItemPathA.
3673 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3675 WCHAR path[MAX_PATH + 1];
3676 HWND hwItem;
3677 RECT rect;
3678 HDC hdc;
3679 HGDIOBJ hPrevObj;
3681 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3683 if (!(hwItem = GetDlgItem(hDlg, id)))
3684 return;
3686 if (lpszPath)
3687 lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3688 else
3689 path[0] = '\0';
3691 GetClientRect(hwItem, &rect);
3692 hdc = GetDC(hDlg);
3693 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3695 if (hPrevObj)
3697 PathCompactPathW(hdc, path, rect.right);
3698 SelectObject(hdc, hPrevObj);
3701 ReleaseDC(hDlg, hdc);
3702 SetWindowTextW(hwItem, path);
3705 /*************************************************************************
3706 * PathIsNetworkPathA [SHLWAPI.@]
3708 * Determine if the given path is a network path.
3710 * PARAMS
3711 * lpszPath [I] Path to check
3713 * RETURNS
3714 * TRUE If lpszPath is a UNC share or mapped network drive, or
3715 * FALSE If lpszPath is a local drive or cannot be determined
3717 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3719 int dwDriveNum;
3721 TRACE("(%s)\n",debugstr_a(lpszPath));
3723 if (!lpszPath)
3724 return FALSE;
3725 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3726 return TRUE;
3727 dwDriveNum = PathGetDriveNumberA(lpszPath);
3728 if (dwDriveNum == -1)
3729 return FALSE;
3730 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3731 return pIsNetDrive(dwDriveNum);
3734 /*************************************************************************
3735 * PathIsNetworkPathW [SHLWAPI.@]
3737 * See PathIsNetworkPathA.
3739 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3741 int dwDriveNum;
3743 TRACE("(%s)\n", debugstr_w(lpszPath));
3745 if (!lpszPath)
3746 return FALSE;
3747 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3748 return TRUE;
3749 dwDriveNum = PathGetDriveNumberW(lpszPath);
3750 if (dwDriveNum == -1)
3751 return FALSE;
3752 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3753 return pIsNetDrive(dwDriveNum);
3756 /*************************************************************************
3757 * PathIsLFNFileSpecA [SHLWAPI.@]
3759 * Determine if the given path is a long file name
3761 * PARAMS
3762 * lpszPath [I] Path to check
3764 * RETURNS
3765 * TRUE If path is a long file name,
3766 * FALSE If path is a valid DOS short file name
3768 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3770 DWORD dwNameLen = 0, dwExtLen = 0;
3772 TRACE("(%s)\n",debugstr_a(lpszPath));
3774 if (!lpszPath)
3775 return FALSE;
3777 while (*lpszPath)
3779 if (*lpszPath == ' ')
3780 return TRUE; /* DOS names cannot have spaces */
3781 if (*lpszPath == '.')
3783 if (dwExtLen)
3784 return TRUE; /* DOS names have only one dot */
3785 dwExtLen = 1;
3787 else if (dwExtLen)
3789 dwExtLen++;
3790 if (dwExtLen > 4)
3791 return TRUE; /* DOS extensions are <= 3 chars*/
3793 else
3795 dwNameLen++;
3796 if (dwNameLen > 8)
3797 return TRUE; /* DOS names are <= 8 chars */
3799 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3801 return FALSE; /* Valid DOS path */
3804 /*************************************************************************
3805 * PathIsLFNFileSpecW [SHLWAPI.@]
3807 * See PathIsLFNFileSpecA.
3809 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3811 DWORD dwNameLen = 0, dwExtLen = 0;
3813 TRACE("(%s)\n",debugstr_w(lpszPath));
3815 if (!lpszPath)
3816 return FALSE;
3818 while (*lpszPath)
3820 if (*lpszPath == ' ')
3821 return TRUE; /* DOS names cannot have spaces */
3822 if (*lpszPath == '.')
3824 if (dwExtLen)
3825 return TRUE; /* DOS names have only one dot */
3826 dwExtLen = 1;
3828 else if (dwExtLen)
3830 dwExtLen++;
3831 if (dwExtLen > 4)
3832 return TRUE; /* DOS extensions are <= 3 chars*/
3834 else
3836 dwNameLen++;
3837 if (dwNameLen > 8)
3838 return TRUE; /* DOS names are <= 8 chars */
3840 lpszPath++;
3842 return FALSE; /* Valid DOS path */
3845 /*************************************************************************
3846 * PathIsDirectoryEmptyA [SHLWAPI.@]
3848 * Determine if a given directory is empty.
3850 * PARAMS
3851 * lpszPath [I] Directory to check
3853 * RETURNS
3854 * TRUE If the directory exists and contains no files,
3855 * FALSE Otherwise
3857 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3859 BOOL bRet = FALSE;
3861 TRACE("(%s)\n",debugstr_a(lpszPath));
3863 if (lpszPath)
3865 WCHAR szPath[MAX_PATH];
3866 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3867 bRet = PathIsDirectoryEmptyW(szPath);
3869 return bRet;
3872 /*************************************************************************
3873 * PathIsDirectoryEmptyW [SHLWAPI.@]
3875 * See PathIsDirectoryEmptyA.
3877 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3879 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3880 WCHAR szSearch[MAX_PATH];
3881 DWORD dwLen;
3882 HANDLE hfind;
3883 BOOL retVal = TRUE;
3884 WIN32_FIND_DATAW find_data;
3886 TRACE("(%s)\n",debugstr_w(lpszPath));
3888 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3889 return FALSE;
3891 lstrcpynW(szSearch, lpszPath, MAX_PATH);
3892 PathAddBackslashW(szSearch);
3893 dwLen = strlenW(szSearch);
3894 if (dwLen > MAX_PATH - 4)
3895 return FALSE;
3897 strcpyW(szSearch + dwLen, szAllFiles);
3898 hfind = FindFirstFileW(szSearch, &find_data);
3899 if (hfind == INVALID_HANDLE_VALUE)
3900 return FALSE;
3904 if (find_data.cFileName[0] == '.')
3906 if (find_data.cFileName[1] == '\0') continue;
3907 if (find_data.cFileName[1] == '.' && find_data.cFileName[2] == '\0') continue;
3910 retVal = FALSE;
3911 break;
3913 while (FindNextFileW(hfind, &find_data));
3915 FindClose(hfind);
3916 return retVal;
3920 /*************************************************************************
3921 * PathFindSuffixArrayA [SHLWAPI.@]
3923 * Find a suffix string in an array of suffix strings
3925 * PARAMS
3926 * lpszSuffix [I] Suffix string to search for
3927 * lppszArray [I] Array of suffix strings to search
3928 * dwCount [I] Number of elements in lppszArray
3930 * RETURNS
3931 * Success: The index of the position of lpszSuffix in lppszArray
3932 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3934 * NOTES
3935 * The search is case sensitive.
3936 * The match is made against the end of the suffix string, so for example:
3937 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3939 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3941 size_t dwLen;
3942 int dwRet = 0;
3944 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3946 if (lpszSuffix && lppszArray && dwCount > 0)
3948 dwLen = strlen(lpszSuffix);
3950 while (dwRet < dwCount)
3952 size_t dwCompareLen = strlen(*lppszArray);
3953 if (dwCompareLen < dwLen)
3955 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3956 return *lppszArray; /* Found */
3958 dwRet++;
3959 lppszArray++;
3962 return NULL;
3965 /*************************************************************************
3966 * PathFindSuffixArrayW [SHLWAPI.@]
3968 * See PathFindSuffixArrayA.
3970 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3972 size_t dwLen;
3973 int dwRet = 0;
3975 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3977 if (lpszSuffix && lppszArray && dwCount > 0)
3979 dwLen = strlenW(lpszSuffix);
3981 while (dwRet < dwCount)
3983 size_t dwCompareLen = strlenW(*lppszArray);
3984 if (dwCompareLen < dwLen)
3986 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3987 return *lppszArray; /* Found */
3989 dwRet++;
3990 lppszArray++;
3993 return NULL;
3996 /*************************************************************************
3997 * PathUndecorateA [SHLWAPI.@]
3999 * Undecorate a file path
4001 * PARAMS
4002 * lpszPath [I/O] Path to remove any decoration from
4004 * RETURNS
4005 * Nothing
4007 * NOTES
4008 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
4010 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
4012 TRACE("(%s)\n",debugstr_a(lpszPath));
4014 if (lpszPath)
4016 LPSTR lpszExt = PathFindExtensionA(lpszPath);
4017 if (lpszExt > lpszPath && lpszExt[-1] == ']')
4019 LPSTR lpszSkip = lpszExt - 2;
4020 if (*lpszSkip == '[')
4021 lpszSkip++; /* [] (no number) */
4022 else
4023 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
4024 lpszSkip--;
4025 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
4027 /* remove the [n] */
4028 lpszSkip--;
4029 while (*lpszExt)
4030 *lpszSkip++ = *lpszExt++;
4031 *lpszSkip = '\0';
4037 /*************************************************************************
4038 * PathUndecorateW [SHLWAPI.@]
4040 * See PathUndecorateA.
4042 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
4044 TRACE("(%s)\n",debugstr_w(lpszPath));
4046 if (lpszPath)
4048 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
4049 if (lpszExt > lpszPath && lpszExt[-1] == ']')
4051 LPWSTR lpszSkip = lpszExt - 2;
4052 if (*lpszSkip == '[')
4053 lpszSkip++; /* [] (no number) */
4054 else
4055 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
4056 lpszSkip--;
4057 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
4059 /* remove the [n] */
4060 lpszSkip--;
4061 while (*lpszExt)
4062 *lpszSkip++ = *lpszExt++;
4063 *lpszSkip = '\0';
4069 /*************************************************************************
4070 * PathUnExpandEnvStringsA [SHLWAPI.@]
4072 * Substitute folder names in a path with their corresponding environment
4073 * strings.
4075 * PARAMS
4076 * path [I] Buffer containing the path to unexpand.
4077 * buffer [O] Buffer to receive the unexpanded path.
4078 * buf_len [I] Size of pszBuf in characters.
4080 * RETURNS
4081 * Success: TRUE
4082 * Failure: FALSE
4084 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR path, LPSTR buffer, UINT buf_len)
4086 WCHAR bufferW[MAX_PATH], *pathW;
4087 DWORD len;
4088 BOOL ret;
4090 TRACE("(%s, %p, %d)\n", debugstr_a(path), buffer, buf_len);
4092 pathW = heap_strdupAtoW(path);
4093 if (!pathW) return FALSE;
4095 ret = PathUnExpandEnvStringsW(pathW, bufferW, MAX_PATH);
4096 HeapFree(GetProcessHeap(), 0, pathW);
4097 if (!ret) return FALSE;
4099 len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
4100 if (buf_len < len + 1) return FALSE;
4102 WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, buf_len, NULL, NULL);
4103 return TRUE;
4106 static const WCHAR allusersprofileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%',0};
4107 static const WCHAR appdataW[] = {'%','A','P','P','D','A','T','A','%',0};
4108 static const WCHAR computernameW[] = {'%','C','O','M','P','U','T','E','R','N','A','M','E','%',0};
4109 static const WCHAR programfilesW[] = {'%','P','r','o','g','r','a','m','F','i','l','e','s','%',0};
4110 static const WCHAR systemrootW[] = {'%','S','y','s','t','e','m','R','o','o','t','%',0};
4111 static const WCHAR systemdriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%',0};
4112 static const WCHAR userprofileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%',0};
4114 struct envvars_map
4116 const WCHAR *var;
4117 UINT varlen;
4118 WCHAR path[MAX_PATH];
4119 DWORD len;
4122 static void init_envvars_map(struct envvars_map *map)
4124 while (map->var)
4126 map->len = ExpandEnvironmentStringsW(map->var, map->path, sizeof(map->path)/sizeof(WCHAR));
4127 /* exclude null from length */
4128 if (map->len) map->len--;
4129 map++;
4133 /*************************************************************************
4134 * PathUnExpandEnvStringsW [SHLWAPI.@]
4136 * Unicode version of PathUnExpandEnvStringsA.
4138 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR path, LPWSTR buffer, UINT buf_len)
4140 static struct envvars_map null_var = {NULL, 0, {0}, 0};
4141 struct envvars_map *match = &null_var, *cur;
4142 struct envvars_map envvars[] = {
4143 { allusersprofileW, sizeof(allusersprofileW)/sizeof(WCHAR) },
4144 { appdataW, sizeof(appdataW)/sizeof(WCHAR) },
4145 { computernameW, sizeof(computernameW)/sizeof(WCHAR) },
4146 { programfilesW, sizeof(programfilesW)/sizeof(WCHAR) },
4147 { systemrootW, sizeof(systemrootW)/sizeof(WCHAR) },
4148 { systemdriveW, sizeof(systemdriveW)/sizeof(WCHAR) },
4149 { userprofileW, sizeof(userprofileW)/sizeof(WCHAR) },
4150 { NULL }
4152 DWORD pathlen;
4153 UINT needed;
4155 TRACE("(%s, %p, %d)\n", debugstr_w(path), buffer, buf_len);
4157 pathlen = strlenW(path);
4158 init_envvars_map(envvars);
4159 cur = envvars;
4160 while (cur->var)
4162 /* path can't contain expanded value or value wasn't retrieved */
4163 if (cur->len == 0 || cur->len > pathlen || strncmpiW(cur->path, path, cur->len))
4165 cur++;
4166 continue;
4169 if (cur->len > match->len)
4170 match = cur;
4171 cur++;
4174 /* 'varlen' includes NULL termination char */
4175 needed = match->varlen + pathlen - match->len;
4176 if (match->len == 0 || needed > buf_len) return FALSE;
4178 strcpyW(buffer, match->var);
4179 strcatW(buffer, &path[match->len]);
4180 TRACE("ret %s\n", debugstr_w(buffer));
4182 return TRUE;
4185 /*************************************************************************
4186 * @ [SHLWAPI.440]
4188 * Find localised or default web content in "%WINDOWS%\web\".
4190 * PARAMS
4191 * lpszFile [I] File name containing content to look for
4192 * lpszPath [O] Buffer to contain the full path to the file
4193 * dwPathLen [I] Length of lpszPath
4195 * RETURNS
4196 * Success: S_OK. lpszPath contains the full path to the content.
4197 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
4199 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
4201 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
4202 HRESULT hRet;
4204 TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
4206 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
4207 szPath[0] = '\0';
4208 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
4209 WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
4210 return hRet;
4213 /*************************************************************************
4214 * @ [SHLWAPI.441]
4216 * Unicode version of SHGetWebFolderFilePathA.
4218 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
4220 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
4221 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
4222 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
4223 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
4224 DWORD dwLen, dwFileLen;
4225 LANGID lidSystem, lidUser;
4227 TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
4229 /* Get base directory for web content */
4230 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4231 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4232 dwLen--;
4234 dwFileLen = strlenW(lpszFile);
4236 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4237 return E_FAIL; /* lpszPath too short */
4239 strcpyW(lpszPath+dwLen, szWeb);
4240 dwLen += szWebLen;
4241 dwPathLen = dwPathLen - dwLen; /* Remaining space */
4243 lidSystem = GetSystemDefaultUILanguage();
4244 lidUser = GetUserDefaultUILanguage();
4246 if (lidSystem != lidUser)
4248 if (dwFileLen + szWebMuiLen < dwPathLen)
4250 /* Use localised content in the users UI language if present */
4251 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4252 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4253 if (PathFileExistsW(lpszPath))
4254 return S_OK;
4258 /* Fall back to OS default installed content */
4259 strcpyW(lpszPath + dwLen, lpszFile);
4260 if (PathFileExistsW(lpszPath))
4261 return S_OK;
4262 return E_FAIL;
4265 #define PATH_CHAR_CLASS_LETTER 0x00000001
4266 #define PATH_CHAR_CLASS_ASTERIX 0x00000002
4267 #define PATH_CHAR_CLASS_DOT 0x00000004
4268 #define PATH_CHAR_CLASS_BACKSLASH 0x00000008
4269 #define PATH_CHAR_CLASS_COLON 0x00000010
4270 #define PATH_CHAR_CLASS_SEMICOLON 0x00000020
4271 #define PATH_CHAR_CLASS_COMMA 0x00000040
4272 #define PATH_CHAR_CLASS_SPACE 0x00000080
4273 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4274 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4276 #define PATH_CHAR_CLASS_INVALID 0x00000000
4277 #define PATH_CHAR_CLASS_ANY 0xffffffff
4279 static const DWORD SHELL_charclass[] =
4281 /* 0x00 */ PATH_CHAR_CLASS_INVALID, /* 0x01 */ PATH_CHAR_CLASS_INVALID,
4282 /* 0x02 */ PATH_CHAR_CLASS_INVALID, /* 0x03 */ PATH_CHAR_CLASS_INVALID,
4283 /* 0x04 */ PATH_CHAR_CLASS_INVALID, /* 0x05 */ PATH_CHAR_CLASS_INVALID,
4284 /* 0x06 */ PATH_CHAR_CLASS_INVALID, /* 0x07 */ PATH_CHAR_CLASS_INVALID,
4285 /* 0x08 */ PATH_CHAR_CLASS_INVALID, /* 0x09 */ PATH_CHAR_CLASS_INVALID,
4286 /* 0x0a */ PATH_CHAR_CLASS_INVALID, /* 0x0b */ PATH_CHAR_CLASS_INVALID,
4287 /* 0x0c */ PATH_CHAR_CLASS_INVALID, /* 0x0d */ PATH_CHAR_CLASS_INVALID,
4288 /* 0x0e */ PATH_CHAR_CLASS_INVALID, /* 0x0f */ PATH_CHAR_CLASS_INVALID,
4289 /* 0x10 */ PATH_CHAR_CLASS_INVALID, /* 0x11 */ PATH_CHAR_CLASS_INVALID,
4290 /* 0x12 */ PATH_CHAR_CLASS_INVALID, /* 0x13 */ PATH_CHAR_CLASS_INVALID,
4291 /* 0x14 */ PATH_CHAR_CLASS_INVALID, /* 0x15 */ PATH_CHAR_CLASS_INVALID,
4292 /* 0x16 */ PATH_CHAR_CLASS_INVALID, /* 0x17 */ PATH_CHAR_CLASS_INVALID,
4293 /* 0x18 */ PATH_CHAR_CLASS_INVALID, /* 0x19 */ PATH_CHAR_CLASS_INVALID,
4294 /* 0x1a */ PATH_CHAR_CLASS_INVALID, /* 0x1b */ PATH_CHAR_CLASS_INVALID,
4295 /* 0x1c */ PATH_CHAR_CLASS_INVALID, /* 0x1d */ PATH_CHAR_CLASS_INVALID,
4296 /* 0x1e */ PATH_CHAR_CLASS_INVALID, /* 0x1f */ PATH_CHAR_CLASS_INVALID,
4297 /* ' ' */ PATH_CHAR_CLASS_SPACE, /* '!' */ PATH_CHAR_CLASS_OTHER_VALID,
4298 /* '"' */ PATH_CHAR_CLASS_DOUBLEQUOTE, /* '#' */ PATH_CHAR_CLASS_OTHER_VALID,
4299 /* '$' */ PATH_CHAR_CLASS_OTHER_VALID, /* '%' */ PATH_CHAR_CLASS_OTHER_VALID,
4300 /* '&' */ PATH_CHAR_CLASS_OTHER_VALID, /* '\'' */ PATH_CHAR_CLASS_OTHER_VALID,
4301 /* '(' */ PATH_CHAR_CLASS_OTHER_VALID, /* ')' */ PATH_CHAR_CLASS_OTHER_VALID,
4302 /* '*' */ PATH_CHAR_CLASS_ASTERIX, /* '+' */ PATH_CHAR_CLASS_OTHER_VALID,
4303 /* ',' */ PATH_CHAR_CLASS_COMMA, /* '-' */ PATH_CHAR_CLASS_OTHER_VALID,
4304 /* '.' */ PATH_CHAR_CLASS_DOT, /* '/' */ PATH_CHAR_CLASS_INVALID,
4305 /* '0' */ PATH_CHAR_CLASS_OTHER_VALID, /* '1' */ PATH_CHAR_CLASS_OTHER_VALID,
4306 /* '2' */ PATH_CHAR_CLASS_OTHER_VALID, /* '3' */ PATH_CHAR_CLASS_OTHER_VALID,
4307 /* '4' */ PATH_CHAR_CLASS_OTHER_VALID, /* '5' */ PATH_CHAR_CLASS_OTHER_VALID,
4308 /* '6' */ PATH_CHAR_CLASS_OTHER_VALID, /* '7' */ PATH_CHAR_CLASS_OTHER_VALID,
4309 /* '8' */ PATH_CHAR_CLASS_OTHER_VALID, /* '9' */ PATH_CHAR_CLASS_OTHER_VALID,
4310 /* ':' */ PATH_CHAR_CLASS_COLON, /* ';' */ PATH_CHAR_CLASS_SEMICOLON,
4311 /* '<' */ PATH_CHAR_CLASS_INVALID, /* '=' */ PATH_CHAR_CLASS_OTHER_VALID,
4312 /* '>' */ PATH_CHAR_CLASS_INVALID, /* '?' */ PATH_CHAR_CLASS_LETTER,
4313 /* '@' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'A' */ PATH_CHAR_CLASS_ANY,
4314 /* 'B' */ PATH_CHAR_CLASS_ANY, /* 'C' */ PATH_CHAR_CLASS_ANY,
4315 /* 'D' */ PATH_CHAR_CLASS_ANY, /* 'E' */ PATH_CHAR_CLASS_ANY,
4316 /* 'F' */ PATH_CHAR_CLASS_ANY, /* 'G' */ PATH_CHAR_CLASS_ANY,
4317 /* 'H' */ PATH_CHAR_CLASS_ANY, /* 'I' */ PATH_CHAR_CLASS_ANY,
4318 /* 'J' */ PATH_CHAR_CLASS_ANY, /* 'K' */ PATH_CHAR_CLASS_ANY,
4319 /* 'L' */ PATH_CHAR_CLASS_ANY, /* 'M' */ PATH_CHAR_CLASS_ANY,
4320 /* 'N' */ PATH_CHAR_CLASS_ANY, /* 'O' */ PATH_CHAR_CLASS_ANY,
4321 /* 'P' */ PATH_CHAR_CLASS_ANY, /* 'Q' */ PATH_CHAR_CLASS_ANY,
4322 /* 'R' */ PATH_CHAR_CLASS_ANY, /* 'S' */ PATH_CHAR_CLASS_ANY,
4323 /* 'T' */ PATH_CHAR_CLASS_ANY, /* 'U' */ PATH_CHAR_CLASS_ANY,
4324 /* 'V' */ PATH_CHAR_CLASS_ANY, /* 'W' */ PATH_CHAR_CLASS_ANY,
4325 /* 'X' */ PATH_CHAR_CLASS_ANY, /* 'Y' */ PATH_CHAR_CLASS_ANY,
4326 /* 'Z' */ PATH_CHAR_CLASS_ANY, /* '[' */ PATH_CHAR_CLASS_OTHER_VALID,
4327 /* '\\' */ PATH_CHAR_CLASS_BACKSLASH, /* ']' */ PATH_CHAR_CLASS_OTHER_VALID,
4328 /* '^' */ PATH_CHAR_CLASS_OTHER_VALID, /* '_' */ PATH_CHAR_CLASS_OTHER_VALID,
4329 /* '`' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'a' */ PATH_CHAR_CLASS_ANY,
4330 /* 'b' */ PATH_CHAR_CLASS_ANY, /* 'c' */ PATH_CHAR_CLASS_ANY,
4331 /* 'd' */ PATH_CHAR_CLASS_ANY, /* 'e' */ PATH_CHAR_CLASS_ANY,
4332 /* 'f' */ PATH_CHAR_CLASS_ANY, /* 'g' */ PATH_CHAR_CLASS_ANY,
4333 /* 'h' */ PATH_CHAR_CLASS_ANY, /* 'i' */ PATH_CHAR_CLASS_ANY,
4334 /* 'j' */ PATH_CHAR_CLASS_ANY, /* 'k' */ PATH_CHAR_CLASS_ANY,
4335 /* 'l' */ PATH_CHAR_CLASS_ANY, /* 'm' */ PATH_CHAR_CLASS_ANY,
4336 /* 'n' */ PATH_CHAR_CLASS_ANY, /* 'o' */ PATH_CHAR_CLASS_ANY,
4337 /* 'p' */ PATH_CHAR_CLASS_ANY, /* 'q' */ PATH_CHAR_CLASS_ANY,
4338 /* 'r' */ PATH_CHAR_CLASS_ANY, /* 's' */ PATH_CHAR_CLASS_ANY,
4339 /* 't' */ PATH_CHAR_CLASS_ANY, /* 'u' */ PATH_CHAR_CLASS_ANY,
4340 /* 'v' */ PATH_CHAR_CLASS_ANY, /* 'w' */ PATH_CHAR_CLASS_ANY,
4341 /* 'x' */ PATH_CHAR_CLASS_ANY, /* 'y' */ PATH_CHAR_CLASS_ANY,
4342 /* 'z' */ PATH_CHAR_CLASS_ANY, /* '{' */ PATH_CHAR_CLASS_OTHER_VALID,
4343 /* '|' */ PATH_CHAR_CLASS_INVALID, /* '}' */ PATH_CHAR_CLASS_OTHER_VALID,
4344 /* '~' */ PATH_CHAR_CLASS_OTHER_VALID
4347 /*************************************************************************
4348 * @ [SHLWAPI.455]
4350 * Check if an ASCII char is of a certain class
4352 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4354 if ((unsigned)c > 0x7e)
4355 return class & PATH_CHAR_CLASS_OTHER_VALID;
4357 return class & SHELL_charclass[(unsigned)c];
4360 /*************************************************************************
4361 * @ [SHLWAPI.456]
4363 * Check if a Unicode char is of a certain class
4365 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4367 if (c > 0x7e)
4368 return class & PATH_CHAR_CLASS_OTHER_VALID;
4370 return class & SHELL_charclass[c];