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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/unicode.h"
32 #define NO_SHLWAPI_STREAM
34 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39 /*************************************************************************
40 * PathAppendA [SHLWAPI.@]
42 * Append one path to another.
45 * lpszPath [O] Initial part of path
46 * lpszAppend [I] Path to append
49 * Success: TRUE. lpszPath contains the newly created path.
50 * Failure: FALSE, if either path is NULL, or PathCombineA fails.
53 * lpszAppend must contain at least one backslash ('\') if not NULL.
54 * Because PathCombineA is used to join the paths, the resulting
55 * path is also canonicalized.
57 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
59 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
61 if (lpszPath && lpszAppend)
63 while (*lpszAppend == '\\')
65 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
71 /*************************************************************************
72 * PathAppendW [SHLWAPI.@]
76 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
78 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
80 if (lpszPath && lpszAppend)
82 while (*lpszAppend == '\\')
84 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
90 /*************************************************************************
91 * PathCombineA [SHLWAPI.@]
93 * Combine two paths together.
96 * lpszDest [O] Destination for combined path
97 * lpszDir [I] Directory path
98 * liszFile [I] File path
101 * Success: The output path
102 * Failure: NULL, if inputs are invalid.
105 * lpszDest should be at least MAX_PATH in size, and may point to the same
106 * memory location as lpszDir. The combined path is canonicalised.
108 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
110 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
112 if (!lpszDest || (!lpszDir && !lpszFile))
113 return NULL; /* Invalid parameters */
116 WCHAR szDest[MAX_PATH];
117 WCHAR szDir[MAX_PATH];
118 WCHAR szFile[MAX_PATH];
120 MultiByteToWideChar(0,0,lpszDir,-1,szDir,MAX_PATH);
122 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
123 PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL);
124 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
129 /*************************************************************************
130 * PathCombineW [SHLWAPI.@]
134 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
136 WCHAR szTemp[MAX_PATH];
137 BOOL bUseBoth = FALSE, bStrip = FALSE;
139 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
141 if (!lpszDest || (!lpszDir && !lpszFile))
142 return lpszDest; /* Invalid parameters */
144 if (!lpszFile || !*lpszFile)
147 strncpyW(szTemp, lpszDir, MAX_PATH);
149 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
151 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
154 strncpyW(szTemp, lpszFile, MAX_PATH);
167 strncpyW(szTemp, lpszDir, MAX_PATH);
170 PathStripToRootW(szTemp);
171 lpszFile++; /* Skip '\' */
173 if (!PathAddBackslashW(szTemp))
175 if (strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
177 strcatW(szTemp, lpszFile);
180 PathCanonicalizeW(lpszDest, szTemp);
184 /*************************************************************************
185 * PathAddBackslashA [SHLWAPI.@]
187 * Append a backslash ('\') to a path if one doesn't exist.
190 * lpszPath [O] The path to append a backslash to.
193 * Success: The position of the last backslash in the path.
194 * Failure: NULL, if lpszPath is NULL or the path is too large.
196 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
200 TRACE("(%s)\n",debugstr_a(lpszPath));
202 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
208 if (lpszPath[-1] != '\\')
217 /*************************************************************************
218 * PathAddBackslashW [SHLWAPI.@]
220 * See PathAddBackslashA.
222 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
226 TRACE("(%s)\n",debugstr_w(lpszPath));
228 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
234 if (lpszPath[-1] != '\\')
243 /*************************************************************************
244 * PathBuildRootA [SHLWAPI.@]
246 * Create a root drive string (e.g. "A:\") from a drive number.
249 * lpszPath [O] Destination for the drive string
255 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
257 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
259 TRACE("(%p,%d)\n", debugstr_a(lpszPath), drive);
261 if (lpszPath && drive >= 0 && drive < 26)
263 lpszPath[0] = 'A' + drive;
271 /*************************************************************************
272 * PathBuildRootW [SHLWAPI.@]
274 * See PathBuildRootA.
276 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
278 TRACE("(%p,%d)\n",debugstr_w(lpszPath), drive);
280 if (lpszPath && drive >= 0 && drive < 26)
282 lpszPath[0] = 'A' + drive;
290 /*************************************************************************
291 * PathFindFileNameA [SHLWAPI.@]
293 * Locate the start of the file name in a path
296 * lpszPath [I] Path to search
299 * A pointer to the first character of the file name
301 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
303 LPCSTR lastSlash = lpszPath;
305 TRACE("(%s)\n",debugstr_a(lpszPath));
307 while (lpszPath && *lpszPath)
309 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
310 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
311 lastSlash = lpszPath + 1;
312 lpszPath = CharNextA(lpszPath);
314 return (LPSTR)lastSlash;
317 /*************************************************************************
318 * PathFindFileNameW [SHLWAPI.@]
320 * See PathFindFileNameA.
322 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
324 LPCWSTR lastSlash = lpszPath;
326 TRACE("(%s)\n",debugstr_w(lpszPath));
328 while (lpszPath && *lpszPath)
330 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
331 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
332 lastSlash = lpszPath + 1;
333 lpszPath = CharNextW(lpszPath);
335 return (LPWSTR)lastSlash;
338 /*************************************************************************
339 * PathFindExtensionA [SHLWAPI.@]
341 * Locate the start of the file extension in a path
344 * lpszPath [I] The path to search
347 * A pointer to the first character of the extension, the end of
348 * the string if the path has no extension, or NULL If lpszPath is NULL
350 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
352 LPCSTR lastpoint = NULL;
354 TRACE("(%s)\n", debugstr_a(lpszPath));
360 if (*lpszPath == '\\' || *lpszPath==' ')
362 else if (*lpszPath == '.')
363 lastpoint = lpszPath;
364 lpszPath = CharNextA(lpszPath);
367 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
370 /*************************************************************************
371 * PathFindExtensionW [SHLWAPI.@]
373 * See PathFindExtensionA.
375 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
377 LPCWSTR lastpoint = NULL;
379 TRACE("(%s)\n", debugstr_w(lpszPath));
385 if (*lpszPath == '\\' || *lpszPath==' ')
387 else if (*lpszPath == '.')
388 lastpoint = lpszPath;
389 lpszPath = CharNextW(lpszPath);
392 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
395 /*************************************************************************
396 * PathGetArgsA [SHLWAPI.@]
398 * Find the next argument in a string delimited by spaces.
401 * lpszPath [I] The string to search for arguments in
404 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
407 * Spaces in quoted strings are ignored as delimiters.
409 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
411 BOOL bSeenQuote = FALSE;
413 TRACE("(%s)\n",debugstr_a(lpszPath));
419 if ((*lpszPath==' ') && !bSeenQuote)
420 return (LPSTR)lpszPath + 1;
421 if (*lpszPath == '"')
422 bSeenQuote = !bSeenQuote;
423 lpszPath = CharNextA(lpszPath);
426 return (LPSTR)lpszPath;
429 /*************************************************************************
430 * PathGetArgsW [SHLWAPI.@]
434 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
436 BOOL bSeenQuote = FALSE;
438 TRACE("(%s)\n",debugstr_w(lpszPath));
444 if ((*lpszPath==' ') && !bSeenQuote)
445 return (LPWSTR)lpszPath + 1;
446 if (*lpszPath == '"')
447 bSeenQuote = !bSeenQuote;
448 lpszPath = CharNextW(lpszPath);
451 return (LPWSTR)lpszPath;
454 /*************************************************************************
455 * PathGetDriveNumberA [SHLWAPI.@]
457 * Return the drive number from a path
460 * lpszPath [I] Path to get the drive number from
463 * Success: The drive number corresponding to the drive in the path
464 * Failure: -1, if lpszPath contains no valid drive
466 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
468 TRACE ("(%s)\n",debugstr_a(lpszPath));
470 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
471 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
472 return tolower(*lpszPath) - 'a';
476 /*************************************************************************
477 * PathGetDriveNumberW [SHLWAPI.@]
479 * See PathGetDriveNumberA.
481 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
483 TRACE ("(%s)\n",debugstr_w(lpszPath));
485 if (lpszPath && lpszPath[1] == ':' &&
486 tolowerW(*lpszPath) >= 'a' && tolowerW(*lpszPath) <= 'z')
487 return tolowerW(*lpszPath) - 'a';
491 /*************************************************************************
492 * PathRemoveFileSpecA [SHLWAPI.@]
494 * Remove the file specification from a path.
497 * lpszPath [O] Path to remove the file spec from
500 * TRUE If the path was valid and modified
503 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
505 LPSTR lpszFileSpec = lpszPath;
506 BOOL bModified = FALSE;
508 TRACE("(%s)\n",debugstr_a(lpszPath));
512 /* Skip directory or UNC path */
513 if (*lpszPath == '\\')
514 lpszFileSpec = ++lpszPath;
515 if (*lpszPath == '\\')
516 lpszFileSpec = ++lpszPath;
520 if(*lpszPath == '\\')
521 lpszFileSpec = lpszPath; /* Skip dir */
522 else if(*lpszPath == ':')
524 lpszFileSpec = ++lpszPath; /* Skip drive */
525 if (*lpszPath == '\\')
528 if (!(lpszPath = CharNextA(lpszPath)))
534 *lpszFileSpec = '\0';
541 /*************************************************************************
542 * PathRemoveFileSpecW [SHLWAPI.@]
544 * See PathRemoveFileSpecA.
546 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
548 LPWSTR lpszFileSpec = lpszPath;
549 BOOL bModified = FALSE;
551 TRACE("(%s)\n",debugstr_w(lpszPath));
555 /* Skip directory or UNC path */
556 if (*lpszPath == '\\')
557 lpszFileSpec = ++lpszPath;
558 if (*lpszPath == '\\')
559 lpszFileSpec = ++lpszPath;
563 if(*lpszPath == '\\')
564 lpszFileSpec = lpszPath; /* Skip dir */
565 else if(*lpszPath == ':')
567 lpszFileSpec = ++lpszPath; /* Skip drive */
568 if (*lpszPath == '\\')
571 if (!(lpszPath = CharNextW(lpszPath)))
577 *lpszFileSpec = '\0';
584 /*************************************************************************
585 * PathStripPathA [SHLWAPI.@]
587 * Remove the initial path from the beginning of a filename
590 * lpszPath [O] Path to remove the initial path from
595 void WINAPI PathStripPathA(LPSTR lpszPath)
597 TRACE("(%s)\n", debugstr_a(lpszPath));
601 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
603 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
607 /*************************************************************************
608 * PathStripPathW [SHLWAPI.@]
610 * See PathStripPathA.
612 void WINAPI PathStripPathW(LPWSTR lpszPath)
616 TRACE("(%s)\n", debugstr_w(lpszPath));
617 lpszFileName = PathFindFileNameW(lpszPath);
619 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
622 /*************************************************************************
623 * PathStripToRootA [SHLWAPI.@]
625 * Reduce a path to its root.
628 * lpszPath [O] the path to reduce
631 * Success: TRUE if the stripped path is a root path
632 * Failure: FALSE if the path cannot be stripped or is NULL
634 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
636 TRACE("(%s)\n", debugstr_a(lpszPath));
640 while(!PathIsRootA(lpszPath))
641 if (!PathRemoveFileSpecA(lpszPath))
646 /*************************************************************************
647 * PathStripToRootW [SHLWAPI.@]
649 * See PathStripToRootA.
651 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
653 TRACE("(%s)\n", debugstr_w(lpszPath));
657 while(!PathIsRootW(lpszPath))
658 if (!PathRemoveFileSpecW(lpszPath))
663 /*************************************************************************
664 * PathRemoveArgsA [SHLWAPI.@]
666 * Strip space seperated arguments from a path.
669 * lpszPath [I] Path to remove arguments from
674 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
676 TRACE("(%s)\n",debugstr_a(lpszPath));
680 LPSTR lpszArgs = PathGetArgsA(lpszPath);
685 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
686 if(*lpszLastChar == ' ')
687 *lpszLastChar = '\0';
692 /*************************************************************************
693 * PathRemoveArgsW [SHLWAPI.@]
695 * See PathRemoveArgsA.
697 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
699 TRACE("(%s)\n",debugstr_w(lpszPath));
703 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
708 LPWSTR lpszLastChar = CharPrevW(lpszPath, lpszArgs);
709 if(*lpszLastChar == ' ')
710 *lpszLastChar = '\0';
715 /*************************************************************************
716 * PathRemoveExtensionA [SHLWAPI.@]
718 * Remove the file extension from a path
721 * lpszPath [O] Path to remove the extension from
726 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
728 TRACE("(%s)\n", debugstr_a(lpszPath));
732 lpszPath = PathFindExtensionA(lpszPath);
737 /*************************************************************************
738 * PathRemoveExtensionW [SHLWAPI.@]
740 * See PathRemoveExtensionA.
742 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
744 TRACE("(%s)\n", debugstr_w(lpszPath));
748 lpszPath = PathFindExtensionW(lpszPath);
753 /*************************************************************************
754 * PathRemoveBackslashA [SHLWAPI.@]
756 * Remove a trailing backslash from a path.
759 * lpszPath [O] Path to remove backslash from
762 * Success: A pointer to the end of the path
763 * Failure: NULL, if lpszPath is NULL
765 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
769 TRACE("(%s)\n", debugstr_a(lpszPath));
773 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
774 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
780 /*************************************************************************
781 * PathRemoveBackslashW [SHLWAPI.@]
783 * See PathRemoveBackslashA.
785 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
787 LPWSTR szTemp = NULL;
789 TRACE("(%s)\n", debugstr_w(lpszPath));
793 szTemp = CharPrevW(lpszPath, lpszPath + strlenW(lpszPath));
794 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
800 /*************************************************************************
801 * PathRemoveBlanksA [SHLWAPI.@]
803 * Remove Spaces from the start and end of a path.
806 * lpszPath [O] Path to strip blanks from
811 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
813 TRACE("(%s)\n", debugstr_a(lpszPath));
815 if(lpszPath && *lpszPath)
817 LPSTR start = lpszPath;
819 while (*lpszPath == ' ')
820 lpszPath = CharNextA(lpszPath);
823 *start++ = *lpszPath++;
825 if (start != lpszPath)
826 while (start[-1] == ' ')
832 /*************************************************************************
833 * PathRemoveBlanksW [SHLWAPI.@]
835 * See PathRemoveBlanksA.
837 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
839 TRACE("(%s)\n", debugstr_w(lpszPath));
841 if(lpszPath && *lpszPath)
843 LPWSTR start = lpszPath;
845 while (*lpszPath == ' ')
849 *start++ = *lpszPath++;
851 if (start != lpszPath)
852 while (start[-1] == ' ')
858 /*************************************************************************
859 * PathQuoteSpacesA [SHLWAPI.@]
861 * Surround a path containg spaces in quotes.
864 * lpszPath [O] Path to quote
870 * The path is not changed if it is invalid or has no spaces.
872 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
874 TRACE("(%s)\n", debugstr_a(lpszPath));
876 if(lpszPath && StrChrA(lpszPath,' '))
878 int iLen = strlen(lpszPath) + 1;
880 if (iLen + 2 < MAX_PATH)
882 memmove(lpszPath + 1, lpszPath, iLen);
884 lpszPath[iLen] = '"';
885 lpszPath[iLen + 1] = '\0';
890 /*************************************************************************
891 * PathQuoteSpacesW [SHLWAPI.@]
893 * See PathQuoteSpacesA.
895 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
897 TRACE("(%s)\n", debugstr_w(lpszPath));
899 if(lpszPath && StrChrW(lpszPath,' '))
901 int iLen = strlenW(lpszPath) + 1;
903 if (iLen + 2 < MAX_PATH)
905 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
907 lpszPath[iLen] = '"';
908 lpszPath[iLen + 1] = '\0';
913 /*************************************************************************
914 * PathUnquoteSpacesA [SHLWAPI.@]
916 * Remove quotes ("") from around a path, if present.
919 * lpszPath [O] Path to strip quotes from
925 * If the path contains a single quote only, an empty string will result.
926 * Otherwise quotes are only removed if they appear at the start and end
929 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
931 TRACE("(%s)\n", debugstr_a(lpszPath));
933 if (lpszPath && *lpszPath == '"')
935 DWORD dwLen = strlen(lpszPath) - 1;
937 if (lpszPath[dwLen] == '"')
939 lpszPath[dwLen] = '\0';
940 for (; *lpszPath; lpszPath++)
941 *lpszPath = lpszPath[1];
946 /*************************************************************************
947 * PathUnquoteSpacesW [SHLWAPI.@]
949 * See PathUnquoteSpacesA.
951 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
953 TRACE("(%s)\n", debugstr_w(lpszPath));
955 if (lpszPath && *lpszPath == '"')
957 DWORD dwLen = strlenW(lpszPath) - 1;
959 if (lpszPath[dwLen] == '"')
961 lpszPath[dwLen] = '\0';
962 for (; *lpszPath; lpszPath++)
963 *lpszPath = lpszPath[1];
968 /*************************************************************************
969 * PathParseIconLocationA [SHLWAPI.@]
971 * Parse the location of an icon from a path.
974 * lpszPath [O] The path to parse the icon location from.
977 * Success: The number of the icon
978 * Failure: 0 if the path does not contain an icon location or is NULL
981 * The path has surrounding quotes and spaces removed regardless
982 * of whether the call succeeds or not.
984 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
989 TRACE("(%s)\n", debugstr_a(lpszPath));
993 if ((lpszComma = strchr(lpszPath, ',')))
996 iRet = StrToIntA(lpszComma);
998 PathUnquoteSpacesA(lpszPath);
999 PathRemoveBlanksA(lpszPath);
1004 /*************************************************************************
1005 * PathParseIconLocationW [SHLWAPI.@]
1007 * See PathParseIconLocationA.
1009 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1014 TRACE("(%s)\n", debugstr_w(lpszPath));
1018 if ((lpszComma = StrChrW(lpszPath, ',')))
1020 *lpszComma++ = '\0';
1021 iRet = StrToIntW(lpszComma);
1023 PathUnquoteSpacesW(lpszPath);
1024 PathRemoveBlanksW(lpszPath);
1029 /*************************************************************************
1030 * SHLWAPI_PathFindLocalExeA
1032 * Internal implementation of SHLWAPI_3.
1034 BOOL WINAPI SHLWAPI_PathFindLocalExeA (LPSTR lpszPath, DWORD dwWhich)
1038 TRACE("(%s,%ld)\n", debugstr_a(lpszPath), dwWhich);
1042 WCHAR szPath[MAX_PATH];
1043 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1044 bRet = SHLWAPI_PathFindLocalExeW(szPath, dwWhich);
1046 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1051 /*************************************************************************
1052 * SHLWAPI_PathFindLocalExeW
1054 * Internal implementation of SHLWAPI_4.
1056 BOOL WINAPI SHLWAPI_PathFindLocalExeW (LPWSTR lpszPath, DWORD dwWhich)
1058 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', '0'},
1059 { '.', 'c', 'o', 'm', '0'},
1060 { '.', 'e', 'x', 'e', '0'},
1061 { '.', 'b', 'a', 't', '0'},
1062 { '.', 'l', 'n', 'k', '0'},
1063 { '.', 'c', 'm', 'd', '0'},
1064 { '0', '0', '0', '0', '0'} };
1066 TRACE("(%s,%ld)\n", debugstr_w(lpszPath), dwWhich);
1068 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1073 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1074 if (!*szExt || dwWhich & 0x40)
1077 int iLen = lstrlenW(lpszPath);
1078 if (iLen > (MAX_PATH - 5))
1080 while (dwWhich & 0x1 && iChoose < sizeof(pszExts))
1082 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1083 if (PathFileExistsW(lpszPath))
1088 *(lpszPath + iLen) = (WCHAR)'\0';
1092 return PathFileExistsW(lpszPath);
1095 /*************************************************************************
1096 * SHLWAPI_PathFindInOtherDirs
1098 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1100 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1102 static WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1103 static WCHAR szPath[] = { 'P','A','T','H','\0'};
1107 WCHAR buff[MAX_PATH];
1109 TRACE("(%s,%08lx)\n", debugstr_w(lpszFile), dwWhich);
1111 /* Try system directories */
1112 GetSystemDirectoryW(buff, MAX_PATH);
1113 if (!PathAppendW(buff, lpszFile))
1115 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1117 strcpyW(lpszFile, buff);
1120 GetWindowsDirectoryW(buff, MAX_PATH);
1121 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1123 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1125 strcpyW(lpszFile, buff);
1128 GetWindowsDirectoryW(buff, MAX_PATH);
1129 if (!PathAppendW(buff, lpszFile))
1131 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1133 strcpyW(lpszFile, buff);
1136 /* Try dirs listed in %PATH% */
1137 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1139 if (!dwLenPATH || !(lpszPATH = malloc((dwLenPATH + 1) * sizeof (WCHAR))))
1142 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1143 lpszCurr = lpszPATH;
1146 LPCWSTR lpszEnd = lpszCurr;
1147 LPWSTR pBuff = buff;
1149 while (*lpszEnd == ' ')
1151 while (*lpszEnd && *lpszEnd != ';')
1152 *pBuff++ = *lpszEnd++;
1156 lpszCurr = lpszEnd + 1;
1158 lpszCurr = NULL; /* Last Path, terminate after this */
1160 if (!PathAppendW(buff, lpszFile))
1162 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1164 strcpyW(lpszFile, buff);
1174 /*************************************************************************
1175 * SHLWAPI_PathFindOnPathExA
1177 * Internal implementation of SHLWAPI_5
1179 BOOL WINAPI SHLWAPI_PathFindOnPathExA(LPSTR lpszFile, LPCSTR *lppszOtherDirs, DWORD dwWhich)
1181 WCHAR szFile[MAX_PATH];
1182 WCHAR buff[MAX_PATH];
1184 TRACE("(%s,%p,%08lx)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1186 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1189 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
1191 /* Search provided directories first */
1192 if (lppszOtherDirs && *lppszOtherDirs)
1194 WCHAR szOther[MAX_PATH];
1195 LPCSTR *lpszOtherPath = lppszOtherDirs;
1197 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1199 MultiByteToWideChar(0,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1200 PathCombineW(buff, szOther, szFile);
1201 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1203 WideCharToMultiByte(0,0,buff,-1,lpszFile,MAX_PATH,0,0);
1209 /* Not found, try system and path dirs */
1210 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1212 WideCharToMultiByte(0,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1218 /*************************************************************************
1219 * SHLWAPI_PathFindOnPathExW
1221 * Internal implementation of SHLWAPI_6.
1223 BOOL WINAPI SHLWAPI_PathFindOnPathExW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs, DWORD dwWhich)
1225 WCHAR buff[MAX_PATH];
1227 TRACE("(%s,%p,%08lx)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1229 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1232 /* Search provided directories first */
1233 if (lppszOtherDirs && *lppszOtherDirs)
1235 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1236 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1238 PathCombineW(buff, *lpszOtherPath, lpszFile);
1239 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1241 strcpyW(lpszFile, buff);
1247 /* Not found, try system and path dirs */
1248 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1251 /*************************************************************************
1252 * PathFindOnPathA [SHLWAPI.@]
1254 * Search a range of paths for an executable.
1257 * lpszFile [O] File to search for
1258 * lppszOtherDirs [I] Other directories to look in
1261 * Success: TRUE. The path to the executable is stored in lpszFile.
1262 * Failure: FALSE. The path to the executable is unchanged.
1264 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1266 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1267 return SHLWAPI_PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1270 /*************************************************************************
1271 * PathFindOnPathW [SHLWAPI.@]
1273 * See PathFindOnPathA.
1275 BOOL WINAPI PathFindOnPathW (LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1277 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1278 return SHLWAPI_PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1281 /*************************************************************************
1282 * PathCompactPathExA [SHLWAPI.@]
1287 * lpszDest [O] Destination for compacted path
1288 * lpszPath [I] Source path
1289 * cchMax [I[ Size of lpszDest
1290 * dwFlags [I] Compaction flags
1295 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1296 UINT cchMax, DWORD dwFlags)
1300 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1302 if (lpszPath && lpszDest)
1304 WCHAR szPath[MAX_PATH];
1305 WCHAR szDest[MAX_PATH];
1307 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1309 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1310 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1315 /*************************************************************************
1316 * PathCompactPathExW [SHLWAPI.@]
1318 * See PathCompactPathExA.
1320 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1321 UINT cchMax, DWORD dwFlags)
1323 FIXME("(%p,%s,%d,0x%08lx)-stub\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1330 WARN("Invalid lpszDest would crash under Win32!\n");
1339 /*************************************************************************
1340 * PathIsRelativeA [SHLWAPI.@]
1342 * Determine if a path is a relative path.
1345 * lpszPath [I] Path to check
1348 * TRUE: The path is relative, or is invalid.
1349 * FALSE: The path is not relative.
1351 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1353 TRACE("(%s)\n",debugstr_a(lpszPath));
1355 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1357 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1362 /*************************************************************************
1363 * PathIsRelativeW [SHLWAPI.@]
1365 * See PathIsRelativeA.
1367 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1369 TRACE("(%s)\n",debugstr_w(lpszPath));
1371 if (!lpszPath || !*lpszPath)
1373 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1378 /*************************************************************************
1379 * PathIsRootA [SHLWAPI.@]
1381 * Determine if a path is a root path.
1384 * lpszPath [I] Path to check
1387 * TRUE If lpszPath is valid and a root path
1390 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1392 TRACE("(%s)\n", debugstr_a(lpszPath));
1394 if (lpszPath && *lpszPath)
1396 if (*lpszPath == '\\')
1399 return TRUE; /* \ */
1400 else if (lpszPath[1]=='\\')
1402 BOOL bSeenSlash = FALSE;
1405 /* Check for UNC root path */
1408 if (*lpszPath == '\\')
1414 lpszPath = CharNextA(lpszPath);
1419 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1420 return TRUE; /* X:\ */
1425 /*************************************************************************
1426 * PathIsRootW [SHLWAPI.@]
1430 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1432 TRACE("(%s)\n", debugstr_w(lpszPath));
1434 if (lpszPath && *lpszPath)
1436 if (*lpszPath == '\\')
1439 return TRUE; /* \ */
1440 else if (lpszPath[1]=='\\')
1442 BOOL bSeenSlash = FALSE;
1445 /* Check for UNC root path */
1448 if (*lpszPath == '\\')
1454 lpszPath = CharNextW(lpszPath);
1459 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1460 return TRUE; /* X:\ */
1465 /*************************************************************************
1466 * PathIsDirectoryA [SHLWAPI.@]
1468 * Determine if a path is a valid directory
1471 * lpszPath [I] Path to check.
1474 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1475 * FALSE if lpszPath is invalid or not a directory.
1478 * Although this function is prototyped as returning a BOOL, it returns
1479 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1481 * if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1486 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1490 TRACE("(%s)\n", debugstr_a(lpszPath));
1492 if (!lpszPath || PathIsUNCServerA(lpszPath))
1495 if (PathIsUNCServerShareA(lpszPath))
1497 FIXME("UNC Server Share not yet supported - FAILING\n");
1501 if ((dwAttr = GetFileAttributesA(lpszPath)) == -1)
1503 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1506 /*************************************************************************
1507 * PathIsDirectoryW [SHLWAPI.@]
1509 * See PathIsDirectoryA.
1511 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1515 TRACE("(%s)\n", debugstr_w(lpszPath));
1517 if (!lpszPath || PathIsUNCServerW(lpszPath))
1520 if (PathIsUNCServerShareW(lpszPath))
1522 FIXME("UNC Server Share not yet supported - FAILING\n");
1526 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1)
1528 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1531 /*************************************************************************
1532 * PathFileExistsA [SHLWAPI.@]
1534 * Determine if a file exists.
1537 * lpszPath [I] Path to check
1540 * TRUE If the file exists and is readable
1543 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1548 TRACE("(%s)\n",debugstr_a(lpszPath));
1553 iPrevErrMode = SetErrorMode(1);
1554 dwAttr = GetFileAttributesA(lpszPath);
1555 SetErrorMode(iPrevErrMode);
1556 return dwAttr == -1 ? FALSE : TRUE;
1559 /*************************************************************************
1560 * PathFileExistsW [SHLWAPI.@]
1562 * See PathFileExistsA
1564 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1569 TRACE("(%s)\n",debugstr_w(lpszPath));
1574 iPrevErrMode = SetErrorMode(1);
1575 dwAttr = GetFileAttributesW(lpszPath);
1576 SetErrorMode(iPrevErrMode);
1577 return dwAttr == -1 ? FALSE : TRUE;
1580 /*************************************************************************
1581 * PathMatchSingleMaskA [internal]
1584 * internal (used by PathMatchSpec)
1586 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1588 while (*name && *mask && *mask!=';')
1594 if (PathMatchSingleMaskA(name,mask+1)) return 1; /* try substrings */
1598 if (toupper(*mask)!=toupper(*name) && *mask!='?') return 0;
1599 name = CharNextA(name);
1600 mask = CharNextA(mask);
1604 while (*mask=='*') mask++;
1605 if (!*mask || *mask==';') return 1;
1610 /*************************************************************************
1611 * PathMatchSingleMaskW [internal]
1613 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1615 while (*name && *mask && *mask!=';')
1621 if (PathMatchSingleMaskW(name,mask+1)) return 1; /* try substrings */
1625 if (toupperW(*mask)!=toupperW(*name) && *mask!='?') return 0;
1626 name = CharNextW(name);
1627 mask = CharNextW(mask);
1631 while (*mask=='*') mask++;
1632 if (!*mask || *mask==';') return 1;
1637 /*************************************************************************
1638 * PathMatchSpecA [SHLWAPI.@]
1640 * Determine if a path matches one or more search masks.
1643 * lpszPath [I] Path to check
1644 * lpszMask [I} Search mask(s)
1647 * TRUE If lpszPath is valid and is matched
1651 * Multiple search masks may be given if they are seperated by ";". The
1652 * pattern "*.*" is treated specially in that it matches all paths (for
1653 * backwards compatability with DOS).
1655 BOOL WINAPI PathMatchSpecA(LPCSTR name, LPCSTR mask)
1657 TRACE("%s %s\n",name,mask);
1659 if (!lstrcmpA( mask, "*.*" )) return 1; /* we don't require a period */
1663 if (PathMatchSingleMaskA(name,mask)) return 1; /* helper function */
1664 while (*mask && *mask!=';') mask = CharNextA(mask);
1668 while (*mask==' ') mask++; /* masks may be separated by "; " */
1674 /*************************************************************************
1675 * PathMatchSpecW [SHLWAPI.@]
1677 * See PathMatchSpecA.
1679 BOOL WINAPI PathMatchSpecW(LPCWSTR name, LPCWSTR mask)
1681 static const WCHAR stemp[] = { '*','.','*',0 };
1682 TRACE("%s %s\n",debugstr_w(name),debugstr_w(mask));
1684 if (!lstrcmpW( mask, stemp )) return 1; /* we don't require a period */
1688 if (PathMatchSingleMaskW(name,mask)) return 1; /* helper function */
1689 while (*mask && *mask!=';') mask = CharNextW(mask);
1693 while (*mask==' ') mask++; /* masks may be separated by "; " */
1699 /*************************************************************************
1700 * PathIsSameRootA [SHLWAPI.@]
1702 * Determine if two paths share the same root.
1705 * lpszPath1 [I] Source path
1706 * lpszPath2 [I] Path to compare with
1709 * TRUE If both paths are valid and share the same root.
1710 * FALSE If either path is invalid or the paths do not share the same root.
1712 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1717 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1719 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1722 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1723 if (lpszStart - lpszPath1 > dwLen)
1724 return FALSE; /* Paths not common up to length of the root */
1728 /*************************************************************************
1729 * PathIsSameRootW [SHLWAPI.@]
1731 * See PathIsSameRootA.
1733 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1738 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1740 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1743 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1744 if (lpszStart - lpszPath1 > dwLen)
1745 return FALSE; /* Paths not common up to length of the root */
1749 /*************************************************************************
1750 * PathIsURLA [SHLWAPI.@]
1752 * Check if the given path is a URL.
1755 * lpszPath [I] Path to check.
1758 * TRUE if lpszPath is a URL.
1759 * FALSE if lpszPath is NULL or not a URL.
1761 BOOL WINAPI PathIsURLA(LPCSTR lpstrPath)
1763 UNKNOWN_SHLWAPI_1 base;
1766 if (!lpstrPath || !*lpstrPath) return FALSE;
1770 res1 = SHLWAPI_1(lpstrPath, &base);
1771 return (base.fcncde) ? TRUE : FALSE;
1774 /*************************************************************************
1775 * PathIsURLW [SHLWAPI.@]
1777 BOOL WINAPI PathIsURLW(LPCWSTR lpstrPath)
1779 UNKNOWN_SHLWAPI_2 base;
1782 if (!lpstrPath || !*lpstrPath) return FALSE;
1786 res1 = SHLWAPI_2(lpstrPath, &base);
1787 return (base.fcncde) ? TRUE : FALSE;
1790 /*************************************************************************
1791 * PathIsContentTypeA [SHLWAPI.@]
1793 * Determine if a file is of a registered content type.
1796 * lpszPath [I] file to chack
1799 * TRUE If lpszPath is a registered content type
1802 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1806 char szBuff[MAX_PATH];
1808 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
1810 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
1811 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
1812 REG_NONE, szBuff, &dwDummy) &&
1813 !strcasecmp(lpszContentType, szBuff))
1820 /*************************************************************************
1821 * PathIsContentTypeW [SHLWAPI.@]
1823 * See PathIsContentTypeA.
1825 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
1827 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
1830 WCHAR szBuff[MAX_PATH];
1832 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
1834 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
1835 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
1836 REG_NONE, szBuff, &dwDummy) &&
1837 !strcmpiW(lpszContentType, szBuff))
1844 /*************************************************************************
1845 * PathIsFileSpecA [SHLWAPI.@]
1847 * Determine if a path is a file specification.
1850 * lpszPath [I] Path to chack
1853 * TRUE If lpszPath is a file spec (contains no directories).
1856 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
1858 TRACE("(%s)\n", debugstr_a(lpszPath));
1865 if (*lpszPath == '\\' || *lpszPath == ':')
1867 lpszPath = CharNextA(lpszPath);
1872 /*************************************************************************
1873 * PathIsFileSpecW [SHLWAPI.@]
1875 * See PathIsFileSpecA.
1877 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
1879 TRACE("(%s)\n", debugstr_w(lpszPath));
1886 if (*lpszPath == '\\' || *lpszPath == ':')
1888 lpszPath = CharNextW(lpszPath);
1893 /*************************************************************************
1894 * PathIsPrefixA [SHLWAPI.@]
1896 * Determine if a path is a prefix of another.
1899 * lpszPrefix [I] Prefix
1900 * lpszPath [i] Path to check
1903 * TRUE If lpszPath has lpszPrefix as its prefix
1904 * FALSE If either path is NULL or lpszPrefix is not a prefix
1906 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
1908 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
1910 if (lpszPrefix && lpszPath &&
1911 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == strlen(lpszPrefix))
1916 /*************************************************************************
1917 * PathIsPrefixW [SHLWAPI.@]
1919 * See PathIsPrefixA.
1921 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
1923 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
1925 if (lpszPrefix && lpszPath &&
1926 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == strlenW(lpszPrefix))
1931 /*************************************************************************
1932 * PathIsSystemFolderA [SHLWAPI.@]
1934 * Determine if a path or file attributes are a system folder.
1937 * lpszPath [I] Path to check.
1938 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
1941 * TRUE If lpszPath or dwAttrib are a system folder.
1942 * FALSE If GetFileAttributesA fails or neither parameter is a system folder.
1944 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
1946 TRACE("(%s,0x%08lx)\n", debugstr_a(lpszPath), dwAttrib);
1948 if (lpszPath && *lpszPath)
1949 dwAttrib = GetFileAttributesA(lpszPath);
1951 if (dwAttrib == -1 || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
1952 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
1957 /*************************************************************************
1958 * PathIsSystemFolderW [SHLWAPI.@]
1960 * See PathIsSystemFolderA.
1962 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
1964 TRACE("(%s,0x%08lx)\n", debugstr_w(lpszPath), dwAttrib);
1966 if (lpszPath && *lpszPath)
1967 dwAttrib = GetFileAttributesW(lpszPath);
1969 if (dwAttrib == -1 || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
1970 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
1975 /*************************************************************************
1976 * PathIsUNCA [SHLWAPI.@]
1978 * Determine if a path is in UNC format.
1981 * lpszPath [I] Path to check
1984 * TRUE: The path is UNC.
1985 * FALSE: The path is not UNC or is NULL.
1987 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
1989 TRACE("(%s)\n",debugstr_a(lpszPath));
1991 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
1996 /*************************************************************************
1997 * PathIsUNCW [SHLWAPI.@]
2001 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2003 TRACE("(%s)\n",debugstr_w(lpszPath));
2005 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2010 /*************************************************************************
2011 * PathIsUNCServerA [SHLWAPI.@]
2013 * Determine if a path is a UNC server name ("\\SHARENAME").
2016 * lpszPath [I] Path to check.
2019 * TRUE If lpszPath is a valid UNC server name.
2023 * This routine is bug compatible with Win32: Server names with a
2024 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2025 * Fixing this bug may break other shlwapi functions!
2027 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2029 TRACE("(%s)\n", debugstr_a(lpszPath));
2031 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2035 if (*lpszPath == '\\')
2037 lpszPath = CharNextA(lpszPath);
2044 /*************************************************************************
2045 * PathIsUNCServerW [SHLWAPI.@]
2047 * See PathIsUNCServerA.
2049 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2051 TRACE("(%s)\n", debugstr_w(lpszPath));
2053 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2057 if (*lpszPath == '\\')
2059 lpszPath = CharNextW(lpszPath);
2066 /*************************************************************************
2067 * PathIsUNCServerShareA [SHLWAPI.@]
2069 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2072 * lpszPath [I] Path to check.
2075 * TRUE If lpszPath is a valid UNC server share.
2079 * This routine is bug compatible with Win32: Server shares with a
2080 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2081 * Fixing this bug may break other shlwapi functions!
2083 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2085 TRACE("(%s)\n", debugstr_a(lpszPath));
2087 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2089 BOOL bSeenSlash = FALSE;
2092 if (*lpszPath == '\\')
2098 lpszPath = CharNextA(lpszPath);
2105 /*************************************************************************
2106 * PathIsUNCServerShareW [SHLWAPI.@]
2108 * See PathIsUNCServerShareA.
2110 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2112 TRACE("(%s)\n", debugstr_w(lpszPath));
2114 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2116 BOOL bSeenSlash = FALSE;
2119 if (*lpszPath == '\\')
2125 lpszPath = CharNextW(lpszPath);
2132 /*************************************************************************
2133 * PathCanonicalizeA [SHLWAPI.@]
2135 * Convert a path to its canonical form.
2138 * lpszBuf [O] Output path
2139 * lpszPath [I] Path to cnonicalize
2142 * Success: TRUE. lpszBuf contains the output path
2143 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2145 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2149 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2154 if (!lpszBuf || !lpszPath)
2155 SetLastError(ERROR_INVALID_PARAMETER);
2158 WCHAR szPath[MAX_PATH];
2159 WCHAR szBuff[MAX_PATH];
2160 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2161 bRet = PathCanonicalizeW(szBuff, szPath);
2162 WideCharToMultiByte(0,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2168 /*************************************************************************
2169 * PathCanonicalizeW [SHLWAPI.@]
2171 * See PathCanonicalizeA.
2173 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2175 LPWSTR lpszDst = lpszBuf;
2176 LPCWSTR lpszSrc = lpszPath;
2178 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2183 if (!lpszBuf || !lpszPath)
2185 SetLastError(ERROR_INVALID_PARAMETER);
2196 /* Copy path root */
2197 if (*lpszSrc == '\\')
2199 *lpszDst++ = *lpszSrc++;
2201 else if (*lpszSrc && lpszSrc[1] == ':')
2204 *lpszDst++ = *lpszSrc++;
2205 *lpszDst++ = *lpszSrc++;
2206 if (*lpszSrc == '\\')
2207 *lpszDst++ = *lpszSrc++;
2210 /* Canonicalize the rest of the path */
2213 if (*lpszSrc == '.')
2215 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2217 lpszSrc += 2; /* Skip .\ */
2219 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2221 /* \.. backs up a directory, over the root if it has no \ following X:.
2222 * .. is ignored if it would remove a UNC server name or inital \\
2224 if (lpszDst != lpszBuf)
2226 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2227 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2228 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2230 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2233 while (lpszDst > lpszBuf && *lpszDst != '\\')
2235 if (*lpszDst == '\\')
2236 lpszDst++; /* Reset to last '\' */
2238 lpszDst = lpszBuf; /* Start path again from new root */
2240 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2243 while (lpszDst > lpszBuf && *lpszDst != '\\')
2245 if (lpszDst == lpszBuf)
2251 lpszSrc += 2; /* Skip .. in src path */
2254 *lpszDst++ = *lpszSrc++;
2257 *lpszDst++ = *lpszSrc++;
2259 /* Append \ to naked drive specs */
2260 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2266 /*************************************************************************
2267 * PathFindNextComponentA [SHLWAPI.@]
2269 * Find the next component in a path.
2272 * lpszPath [I] Path to find next component in
2275 * Success: A pointer to the next component, or the end of the string
2276 * Failure: NULL, If lpszPath is invalid
2279 * A 'component' is either a backslash character (\) or UNC marker (\\).
2280 * Because of this, relative paths (e.g "c:foo") are regarded as having
2281 * only one component.
2283 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2287 TRACE("(%s)\n", debugstr_a(lpszPath));
2289 if(!lpszPath || !*lpszPath)
2292 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2294 if (lpszSlash[1] == '\\')
2296 return lpszSlash + 1;
2298 return (LPSTR)lpszPath + strlen(lpszPath);
2301 /*************************************************************************
2302 * PathFindNextComponentW [SHLWAPI.@]
2304 * See PathFindNextComponentA.
2306 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2310 TRACE("(%s)\n", debugstr_w(lpszPath));
2312 if(!lpszPath || !*lpszPath)
2315 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2317 if (lpszSlash[1] == '\\')
2319 return lpszSlash + 1;
2321 return (LPWSTR)lpszPath + strlenW(lpszPath);
2324 /*************************************************************************
2325 * PathAddExtensionA [SHLWAPI.@]
2327 * Add a file extension to a path
2330 * lpszPath [O] Path to add extension to
2331 * lpszExtension [I} Extension to add to lpszPath
2334 * TRUE If the path was modified
2335 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2336 * extension allready, or the new path length is too big.
2339 * What version of shlwapi.dll adds "exe" if pszExtension is NULL? Win2k
2340 * does not do this, so the behaviour was removed.
2342 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2346 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2348 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2351 dwLen = strlen(lpszPath);
2353 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2356 strcpy(lpszPath + dwLen, lpszExtension);
2360 /*************************************************************************
2361 * PathAddExtensionW [SHLWAPI.@]
2363 * See PathAddExtensionA.
2365 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2369 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2371 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2374 dwLen = strlenW(lpszPath);
2376 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2379 strcpyW(lpszPath + dwLen, lpszExtension);
2383 /*************************************************************************
2384 * PathMakePrettyA [SHLWAPI.@]
2386 * Convert an uppercase DOS filename into lowercase.
2389 * lpszPath [O] Path to convert.
2392 * TRUE If the path was an uppercase DOS path and was converted
2395 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2397 LPSTR pszIter = lpszPath;
2399 TRACE("(%s)\n", debugstr_a(lpszPath));
2401 if (!pszIter || !*pszIter)
2406 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2407 return FALSE; /* Not DOS path */
2410 pszIter = lpszPath + 1;
2413 *pszIter = tolower(*pszIter);
2419 /*************************************************************************
2420 * PathMakePrettyW [SHLWAPI.@]
2422 * See PathMakePrettyA
2424 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2426 LPWSTR pszIter = lpszPath;
2428 TRACE("(%s)\n", debugstr_w(lpszPath));
2430 if (!pszIter || !*pszIter)
2435 if (islowerW(*pszIter))
2436 return FALSE; /* Not DOS path */
2439 pszIter = lpszPath + 1;
2442 *pszIter = tolowerW(*pszIter);
2448 /*************************************************************************
2449 * PathCommonPrefixA [SHLWAPI.@]
2451 * Determine the length of the common prefix between two paths.
2454 * lpszFile1 [I] First path for comparason
2455 * lpszFile2 [I] Second path for comparason
2456 * achPath [O] Destination for common prefix string
2459 * The length of the common prefix. This is 0 if there is no common
2460 * prefix between the paths or if any parameters are invalid. If the prefix
2461 * is non-zero and achPath is not NULL, achPath is filled with the common
2462 * part of the prefix and NUL terminated.
2465 * A common prefix of 2 is always returned as 3. It is thus possible for
2466 * the length returned to be invalid (i.e. Longer than one or both of the
2467 * strings given as parameters). This Win32 behaviour has been implimented
2468 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2469 * To work around this when using this function, always check that the byte
2470 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2472 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2475 LPCSTR lpszIter1 = lpszFile1;
2476 LPCSTR lpszIter2 = lpszFile2;
2478 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2483 if (!lpszFile1 || !lpszFile2)
2486 /* Handle roots first */
2487 if (PathIsUNCA(lpszFile1))
2489 if (!PathIsUNCA(lpszFile2))
2494 else if (PathIsUNCA(lpszFile2))
2495 return 0; /* Know already lpszFile1 is not UNC */
2500 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2501 (!*lpszIter2 || *lpszIter2 == '\\'))
2502 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2504 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2505 break; /* Strings differ at this point */
2512 iLen++; /* Feature/Bug compatable with Win32 */
2514 if (iLen && achPath)
2516 memcpy(achPath,lpszFile1,iLen);
2517 achPath[iLen] = '\0';
2522 /*************************************************************************
2523 * PathCommonPrefixW [SHLWAPI.@]
2525 * See PathCommonPrefixA.
2527 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2530 LPCWSTR lpszIter1 = lpszFile1;
2531 LPCWSTR lpszIter2 = lpszFile2;
2533 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2538 if (!lpszFile1 || !lpszFile2)
2541 /* Handle roots first */
2542 if (PathIsUNCW(lpszFile1))
2544 if (!PathIsUNCW(lpszFile2))
2549 else if (PathIsUNCW(lpszFile2))
2550 return 0; /* Know already lpszFile1 is not UNC */
2555 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2556 (!*lpszIter2 || *lpszIter2 == '\\'))
2557 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2559 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2560 break; /* Strings differ at this point */
2567 iLen++; /* Feature/Bug compatable with Win32 */
2569 if (iLen && achPath)
2571 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2572 achPath[iLen] = '\0';
2577 /*************************************************************************
2578 * PathCompactPathA [SHLWAPI.@]
2580 * Make a path fit into a given width when printed to a DC.
2583 * hDc [I] Destination DC
2584 * lpszPath [O] Path to be printed to hDc
2585 * dx [i] Desired width
2588 * TRUE If the path was modified.
2591 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2595 TRACE("(%08x,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2599 WCHAR szPath[MAX_PATH];
2600 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2601 bRet = PathCompactPathW(hDC, szPath, dx);
2602 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2607 /*************************************************************************
2608 * PathCompactPathW [SHLWAPI.@]
2610 * See PathCompactPathA.
2612 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2614 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2617 WCHAR buff[MAX_PATH];
2621 TRACE("(%08x,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2627 hdc = hDC = GetDC(0);
2629 /* Get the length of the whole path */
2630 dwLen = strlenW(lpszPath);
2631 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2635 /* Path too big, must reduce it */
2637 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2639 sFile = PathFindFileNameW(lpszPath);
2640 if (sFile != lpszPath)
2641 sFile = CharPrevW(lpszPath, sFile);
2643 /* Get the size of ellipses */
2644 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2645 dwEllipsesLen = size.cx;
2646 /* Get the size of the file name */
2647 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2648 dwPathLen = size.cx;
2650 if (sFile != lpszPath)
2652 LPWSTR sPath = sFile;
2653 BOOL bEllipses = FALSE;
2655 /* The path includes a file name. Include as much of the path prior to
2656 * the file name as possible, allowing for the ellipses, e.g:
2657 * c:\some very long path\filename ==> c:\some v...\filename
2659 strncpyW(buff, sFile, MAX_PATH);
2663 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2665 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2666 dwTotalLen += size.cx;
2667 if (dwTotalLen <= dx)
2669 sPath = CharPrevW(lpszPath, sPath);
2673 sPath = CharPrevW(lpszPath, sPath);
2674 sPath = CharPrevW(lpszPath, sPath);
2676 } while (sPath > lpszPath);
2678 if (sPath > lpszPath)
2682 strcpyW(sPath, szEllipses);
2683 strcpyW(sPath+3, buff);
2689 strcpyW(lpszPath, szEllipses);
2690 strcpyW(lpszPath+3, buff);
2694 /* Trim the path by adding ellipses to the end, e.g:
2695 * A very long file name.txt ==> A very...
2697 dwLen = strlenW(lpszPath);
2699 if (dwLen > MAX_PATH - 3)
2700 dwLen = MAX_PATH - 3;
2701 strncpyW(buff, sFile, dwLen);
2705 GetTextExtentPointW(hDC, buff, dwLen, &size);
2706 } while (dwLen && size.cx + dwEllipsesLen > dx);
2710 DWORD dwWritten = 0;
2712 dwEllipsesLen /= 3; /* Size of a single '.' */
2714 /* Write as much of the Ellipses string as possible */
2715 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2718 dwWritten += dwEllipsesLen;
2726 strcpyW(buff + dwLen, szEllipses);
2727 strcpyW(lpszPath, buff);
2737 /*************************************************************************
2738 * PathGetCharTypeA [SHLWAPI.@]
2740 * Categorise a character from a file path.
2743 * ch [I] Character to get the type of
2746 * A set of GCT_ bit flags (from shlwapi.h) indicating the character type.
2748 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2750 return PathGetCharTypeW(ch);
2753 /*************************************************************************
2754 * PathGetCharTypeW [SHLWAPI.@]
2756 * See PathGetCharTypeA.
2758 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2762 TRACE("(%d)\n", ch);
2764 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2765 ch == '"' || ch == '|' || ch == 255)
2766 flags = GCT_INVALID; /* Invalid */
2767 else if (ch == '*' || ch=='?')
2768 flags = GCT_WILD; /* Wildchars */
2769 else if ((ch == '\\') || (ch=='/') || (ch == ':'))
2770 return GCT_SEPARATOR; /* Path separators */
2775 if (!ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2776 ch == '.' || ch == '@' || ch == '^' ||
2777 ch == '\'' || ch == 130 || ch == '`')
2778 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2782 flags |= GCT_SHORTCHAR; /* Bug compatable with win32 */
2783 flags |= GCT_LFNCHAR; /* Valid for long file names */
2788 /*************************************************************************
2789 * SHLWAPI_UseSystemForSystemFolders
2791 * Internal helper for PathMakeSystemFolderW.
2793 static BOOL SHLWAPI_UseSystemForSystemFolders()
2795 static BOOL bCheckedReg = FALSE;
2796 static BOOL bUseSystemForSystemFolders = FALSE;
2802 /* Key tells Win what file attributes to use on system folders */
2803 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2804 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2805 "UseSystemForSystemFolders", 0, 0, 0))
2806 bUseSystemForSystemFolders = TRUE;
2808 return bUseSystemForSystemFolders;
2811 /*************************************************************************
2812 * PathMakeSystemFolderA [SHLWAPI.@]
2814 * Set system folder attribute for a path.
2817 * lpszPath [I] The path to turn into a system folder
2820 * TRUE If the path was changed to/already was a system folder
2821 * FALSE If the path is invalid or SetFileAttributesA fails
2823 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
2827 TRACE("(%s)\n", debugstr_a(lpszPath));
2829 if (lpszPath && *lpszPath)
2831 WCHAR szPath[MAX_PATH];
2832 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2833 bRet = PathMakeSystemFolderW(szPath);
2838 /*************************************************************************
2839 * PathMakeSystemFolderW [SHLWAPI.@]
2841 * See PathMakeSystemFolderA.
2843 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
2845 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
2846 WCHAR buff[MAX_PATH];
2848 TRACE("(%s)\n", debugstr_w(lpszPath));
2850 if (!lpszPath || !*lpszPath)
2853 /* If the directory is already a system directory, dont do anything */
2854 GetSystemDirectoryW(buff, MAX_PATH);
2855 if (!strcmpW(buff, lpszPath))
2858 GetWindowsDirectoryW(buff, MAX_PATH);
2859 if (!strcmpW(buff, lpszPath))
2862 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
2863 if (SHLWAPI_UseSystemForSystemFolders())
2864 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
2866 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1)
2869 /* Change file attributes to system attributes */
2870 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
2871 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
2874 /*************************************************************************
2875 * PathRenameExtensionA [SHLWAPI.@]
2877 * Swap the file extension in a path with another extension.
2880 * pszPath [O] Path to swap the extension in
2881 * pszExt [I] The new extension
2884 * TRUE if pszPath was modified
2885 * FALSE if pszPath or pszExt is NULL, or the new path is too long
2887 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
2889 LPSTR lpszExtension;
2891 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
2893 lpszExtension = PathFindExtensionA(lpszPath);
2895 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
2898 strcpy(lpszExtension, lpszExt);
2902 /*************************************************************************
2903 * PathRenameExtensionW [SHLWAPI.@]
2905 * See PathRenameExtensionA.
2907 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
2909 LPWSTR lpszExtension;
2911 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
2913 lpszExtension = PathFindExtensionW(lpszPath);
2915 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
2918 strcpyW(lpszExtension, lpszExt);
2922 /*************************************************************************
2923 * PathSearchAndQualifyA [SHLWAPI.@]
2930 * cchBuf [I] Size of lpszBuf
2935 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
2937 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
2941 /*************************************************************************
2942 * PathSearchAndQualifyW [SHLWAPI.@]
2944 * See PathSearchAndQualifyA
2946 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
2948 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
2952 /*************************************************************************
2953 * PathSkipRootA [SHLWAPI.@]
2955 * Return the portion of a path following the drive letter or mount point.
2958 * lpszPath [I] The path to skip on
2961 * Success: A pointer to the next character after the root.
2962 * Failure: NULL, if lpszPath is invalid, has no root or is a MB string.
2964 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
2966 TRACE("(%s)\n", debugstr_a(lpszPath));
2968 if (!lpszPath || !*lpszPath)
2971 if (*lpszPath == '\\' && lpszPath[1] == '\\')
2973 /* Network share: skip share server and mount point */
2975 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
2976 (lpszPath = StrChrA(lpszPath + 1, '\\')))
2978 return (LPSTR)lpszPath;
2981 if (IsDBCSLeadByte(*lpszPath))
2985 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
2986 return (LPSTR)lpszPath + 3;
2990 /*************************************************************************
2991 * PathSkipRootW [SHLWAPI.@]
2993 * See PathSkipRootA.
2995 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
2997 TRACE("(%s)\n", debugstr_w(lpszPath));
2999 if (!lpszPath || !*lpszPath)
3002 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3004 /* Network share: skip share server and mount point */
3006 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3007 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3009 return (LPWSTR)lpszPath;
3013 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3014 return (LPWSTR)lpszPath + 3;
3018 /*************************************************************************
3019 * PathCreateFromUrlA [SHLWAPI.@]
3021 * Create a path from a URL
3024 * lpszUrl [I] URL to convert into a path
3025 * lpszPath [O] Output buffer for the resulting Path
3026 * pcchPath [I] Length of lpszPath
3027 * dwFlags [I] Flags controlling the conversion
3030 * Success: S_OK. lpszPath contains the URL in path format
3031 * Failure: An HRESULT error code such as E_INVALIDARG.
3033 HRESULT WINAPI PathCreateFromUrlA(LPCSTR lpszUrl, LPSTR lpszPath,
3034 LPDWORD pcchPath, DWORD dwFlags)
3036 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_a(lpszUrl), lpszPath, pcchPath, dwFlags);
3038 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3039 return E_INVALIDARG;
3041 /* extracts thing prior to : in pszURL and checks against:
3045 * about - if match returns E_INVALIDARG
3051 /*************************************************************************
3052 * PathCreateFromUrlW [SHLWAPI.@]
3054 * See PathCreateFromUrlA.
3056 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR lpszUrl, LPWSTR lpszPath,
3057 LPDWORD pcchPath, DWORD dwFlags)
3059 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_w(lpszUrl), lpszPath, pcchPath, dwFlags);
3061 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3062 return E_INVALIDARG;
3067 /*************************************************************************
3068 * PathRelativePathToA [SHLWAPI.@]
3070 * Create a relative path from one path to another.
3073 * lpszPath [O] Destination for relative path
3074 * lpszFrom [I] Source path
3075 * dwAttrFrom [I] File attribute of source path
3076 * lpszTo [I] Destination path
3077 * dwAttrTo [I] File attributes of destination path
3080 * TRUE If a relative path can be formed. lpszPath contains the new path
3081 * FALSE If the paths are not relavtive or any parameters are invalid
3084 * lpszTo should be at least MAX_PATH in length.
3085 * Calling this function with relative paths for lpszFrom or lpszTo may
3086 * give erroneous results.
3088 * The Win32 version of this function contains a bug where the lpszTo string
3089 * may be referenced 1 byte beyond the end of the string. As a result random
3090 * garbage may be written to the output path, depending on what lies beyond
3091 * the last byte of the string. This bug occurs because of the behaviour of
3092 * PathCommonPrefix (see notes for that function), and no workaround seems
3093 * possible with Win32.
3094 * This bug has been fixed here, so for example the relative path from "\\"
3095 * to "\\" is correctly determined as "." in this implementation.
3097 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3098 LPCSTR lpszTo, DWORD dwAttrTo)
3102 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_a(lpszFrom),
3103 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3105 if(lpszPath && lpszFrom && lpszTo)
3107 WCHAR szPath[MAX_PATH];
3108 WCHAR szFrom[MAX_PATH];
3109 WCHAR szTo[MAX_PATH];
3110 MultiByteToWideChar(0,0,lpszFrom,-1,szFrom,MAX_PATH);
3111 MultiByteToWideChar(0,0,lpszTo,-1,szTo,MAX_PATH);
3112 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3113 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3118 /*************************************************************************
3119 * PathRelativePathToW [SHLWAPI.@]
3121 * See PathRelativePathToA.
3123 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3124 LPCWSTR lpszTo, DWORD dwAttrTo)
3126 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3127 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3128 WCHAR szFrom[MAX_PATH];
3129 WCHAR szTo[MAX_PATH];
3132 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_w(lpszFrom),
3133 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3135 if(!lpszPath || !lpszFrom || !lpszTo)
3139 strncpyW(szFrom, lpszFrom, MAX_PATH);
3140 strncpyW(szTo, lpszTo, MAX_PATH);
3142 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3143 PathRemoveFileSpecW(szFrom);
3144 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3145 PathRemoveFileSpecW(szTo);
3147 /* Paths can only be relative if they have a common root */
3148 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3151 /* Strip off lpszFrom components to the root, by adding "..\" */
3152 lpszFrom = szFrom + dwLen;
3158 if (*lpszFrom == '\\')
3163 lpszFrom = PathFindNextComponentW(lpszFrom);
3164 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3167 /* From the root add the components of lpszTo */
3169 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3172 if (*lpszTo && lpszTo[-1])
3174 if (*lpszTo != '\\')
3176 dwLen = strlenW(lpszPath);
3177 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3182 strcpyW(lpszPath + dwLen, lpszTo);
3187 /*************************************************************************
3188 * PathUnmakeSystemFolderA [SHLWAPI.@]
3190 * Remove the system folder attributes from a path.
3193 * lpszPath [I] The path to remove attributes from
3197 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3198 * SetFileAttributesA fails.
3200 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3204 TRACE("(%s)\n", debugstr_a(lpszPath));
3206 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == -1 ||
3207 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3210 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3211 return SetFileAttributesA(lpszPath, dwAttr);
3214 /*************************************************************************
3215 * PathUnmakeSystemFolderW [SHLWAPI.@]
3217 * See PathUnmakeSystemFolderA.
3219 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3223 TRACE("(%s)\n", debugstr_w(lpszPath));
3225 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == -1 ||
3226 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3229 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3230 return SetFileAttributesW(lpszPath, dwAttr);
3234 /*************************************************************************
3235 * PathSetDlgItemPathA [SHLWAPI.@]
3237 * Set the text of a dialog item to a path, shrinking the path to fit
3238 * if it is too big for the item.
3241 * hDlg [I] Dialog handle
3242 * id [I] ID of item in the dialog
3243 * lpszPath [I] Path to set as the items text
3249 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3250 * window text is erased).
3252 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3254 WCHAR szPath[MAX_PATH];
3256 TRACE("(%8x,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3259 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3262 PathSetDlgItemPathW(hDlg, id, szPath);
3265 /*************************************************************************
3266 * PathSetDlgItemPathW [SHLWAPI.@]
3268 * See PathSetDlgItemPathA.
3270 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3272 WCHAR path[MAX_PATH + 1];
3278 TRACE("(%8x,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3280 if (!(hwItem = GetDlgItem(hDlg, id)))
3284 strncpyW(path, lpszPath, sizeof(path));
3288 GetClientRect(hwItem, &rect);
3290 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3294 PathCompactPathW(hdc, path, rect.right);
3295 SelectObject(hdc, hPrevObj);
3298 ReleaseDC(hDlg, hdc);
3299 SetWindowTextW(hwItem, path);
3302 /*************************************************************************
3303 * PathIsNetworkPathA [SHLWAPI.@]
3305 * Determine if the given path is a network path.
3308 * lpszPath [I] Path to check
3311 * TRUE If path is a UNC share or mapped network drive
3312 * FALSE If path is a local drive or cannot be determined
3314 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3316 static BOOL (WINAPI *pfnFunc)(DWORD);
3319 TRACE("(%s)\n",debugstr_a(lpszPath));
3323 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3325 dwDriveNum = PathGetDriveNumberA(lpszPath);
3326 if (dwDriveNum == -1)
3328 GET_FUNC(shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3329 return pfnFunc(dwDriveNum);
3332 /*************************************************************************
3333 * PathIsNetworkPathW [SHLWAPI.@]
3335 * See PathIsNetworkPathA.
3337 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3339 static BOOL (WINAPI *pfnFunc)(DWORD);
3342 TRACE("(%s)\n", debugstr_w(lpszPath));
3346 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3348 dwDriveNum = PathGetDriveNumberW(lpszPath);
3349 if (dwDriveNum == -1)
3351 GET_FUNC(shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3352 return pfnFunc(dwDriveNum);
3355 /*************************************************************************
3356 * PathIsLFNFileSpecA [SHLWAPI.@]
3358 * Determine if the given path is a long file name
3361 * lpszPath [I] Path to check
3364 * TRUE If path is a long file name
3365 * FALSE If path is a valid DOS 8.3 file name
3367 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3369 DWORD dwNameLen = 0, dwExtLen = 0;
3371 TRACE("(%s)\n",debugstr_a(lpszPath));
3378 if (*lpszPath == ' ')
3379 return TRUE; /* DOS names cannot have spaces */
3380 if (*lpszPath == '.')
3383 return TRUE; /* DOS names have only one dot */
3390 return TRUE; /* DOS extensions are <= 3 chars*/
3396 return TRUE; /* DOS names are <= 8 chars */
3398 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3400 return FALSE; /* Valid DOS path */
3403 /*************************************************************************
3404 * PathIsLFNFileSpecW [SHLWAPI.@]
3406 * See PathIsLFNFileSpecA.
3408 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3410 DWORD dwNameLen = 0, dwExtLen = 0;
3412 TRACE("(%s)\n",debugstr_w(lpszPath));
3419 if (*lpszPath == ' ')
3420 return TRUE; /* DOS names cannot have spaces */
3421 if (*lpszPath == '.')
3424 return TRUE; /* DOS names have only one dot */
3431 return TRUE; /* DOS extensions are <= 3 chars*/
3437 return TRUE; /* DOS names are <= 8 chars */
3441 return FALSE; /* Valid DOS path */
3444 /*************************************************************************
3445 * PathIsDirectoryEmptyA [SHLWAPI.@]
3447 * Determine if a given directory is empty.
3450 * lpszPath [I] Directory to check
3453 * TRUE If the directory exists and contains no files
3456 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3460 TRACE("(%s)\n",debugstr_a(lpszPath));
3464 WCHAR szPath[MAX_PATH];
3465 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3466 bRet = PathIsDirectoryEmptyW(szPath);
3471 /*************************************************************************
3472 * PathIsDirectoryEmptyW [SHLWAPI.@]
3474 * See PathIsDirectoryEmptyA.
3476 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3478 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3479 WCHAR szSearch[MAX_PATH];
3482 BOOL retVal = FALSE;
3483 WIN32_FIND_DATAW find_data;
3485 TRACE("(%s)\n",debugstr_w(lpszPath));
3487 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3490 strncpyW(szSearch, lpszPath, MAX_PATH);
3491 PathAddBackslashW(szSearch);
3492 dwLen = strlenW(szSearch);
3493 if (dwLen > MAX_PATH - 4)
3496 strcpyW(szSearch + dwLen, szAllFiles);
3497 hfind = FindFirstFileW(szSearch, &find_data);
3499 if (hfind != INVALID_HANDLE_VALUE &&
3500 find_data.cFileName[0] == '.' &&
3501 find_data.cFileName[1] == '.')
3503 /* The only directory entry should be the parent */
3504 if (!FindNextFileW(hfind, &find_data))
3512 /*************************************************************************
3513 * PathFindSuffixArrayA [SHLWAPI.@]
3515 * Find a suffix string in an array of suffix strings
3518 * lpszSuffix [I] Suffix string to search for
3519 * lppszArray [I] Array of suffix strings to search
3520 * dwCount [I] Number of elements in lppszArray
3523 * Success The index of the position of lpszSuffix in lppszArray
3524 * Failure 0, if any parameters are invalid or lpszSuffix is not found
3527 * The search is case sensitive.
3528 * The match is made against the end of the suffix string, so for example:
3529 * lpszSuffix=fooBAR matches BAR, but lpszSuffix=fooBARfoo does not.
3531 int WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3536 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3538 if (lpszSuffix && lppszArray && dwCount > 0)
3540 dwLen = strlen(lpszSuffix);
3542 while (dwRet < dwCount)
3544 DWORD dwCompareLen = strlen(*lppszArray);
3545 if (dwCompareLen < dwLen)
3547 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3548 return dwRet; /* Found */
3557 /*************************************************************************
3558 * PathFindSuffixArrayW [SHLWAPI.@]
3560 * See PathFindSuffixArrayA.
3562 int WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3567 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3569 if (lpszSuffix && lppszArray && dwCount > 0)
3571 dwLen = strlenW(lpszSuffix);
3573 while (dwRet < dwCount)
3575 DWORD dwCompareLen = strlenW(*lppszArray);
3576 if (dwCompareLen < dwLen)
3578 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3579 return dwRet; /* Found */
3588 /*************************************************************************
3589 * PathUndecorateA [SHLWAPI.@]
3591 * Undecorate a file path
3594 * lpszPath [O] Path to undecorate
3600 * A decorations form is "path[n].ext" where n is an optional decimal number.
3602 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3604 TRACE("(%s)\n",debugstr_a(lpszPath));
3608 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3609 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3611 LPSTR lpszSkip = lpszExt - 2;
3612 if (*lpszSkip == '[')
3613 lpszSkip++; /* [] (no number) */
3615 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3617 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3619 /* remove the [n] */
3622 *lpszSkip++ = *lpszExt++;
3629 /*************************************************************************
3630 * PathUndecorateW [SHLWAPI.@]
3632 * See PathUndecorateA.
3634 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3636 TRACE("(%s)\n",debugstr_w(lpszPath));
3640 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3641 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3643 LPWSTR lpszSkip = lpszExt - 2;
3644 if (*lpszSkip == '[')
3645 lpszSkip++; /* [] (no number) */
3647 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3649 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3651 /* remove the [n] */
3654 *lpszSkip++ = *lpszExt++;