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 /* function pointers for GET_FUNC macro; these need to be global because of gcc bug */
40 static BOOL (WINAPI *pIsNetDrive)(DWORD);
42 /*************************************************************************
43 * PathAppendA [SHLWAPI.@]
45 * Append one path to another.
48 * lpszPath [O] Initial part of path
49 * lpszAppend [I] Path to append
52 * Success: TRUE. lpszPath contains the newly created path.
53 * Failure: FALSE, if either path is NULL, or PathCombineA fails.
56 * lpszAppend must contain at least one backslash ('\') if not NULL.
57 * Because PathCombineA is used to join the paths, the resulting
58 * path is also canonicalized.
60 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
62 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
64 if (lpszPath && lpszAppend)
66 while (*lpszAppend == '\\')
68 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
74 /*************************************************************************
75 * PathAppendW [SHLWAPI.@]
79 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
81 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
83 if (lpszPath && lpszAppend)
85 while (*lpszAppend == '\\')
87 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
93 /*************************************************************************
94 * PathCombineA [SHLWAPI.@]
96 * Combine two paths together.
99 * lpszDest [O] Destination for combined path
100 * lpszDir [I] Directory path
101 * liszFile [I] File path
104 * Success: The output path
105 * Failure: NULL, if inputs are invalid.
108 * lpszDest should be at least MAX_PATH in size, and may point to the same
109 * memory location as lpszDir. The combined path is canonicalised.
111 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
113 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
115 if (!lpszDest || (!lpszDir && !lpszFile))
116 return NULL; /* Invalid parameters */
119 WCHAR szDest[MAX_PATH];
120 WCHAR szDir[MAX_PATH];
121 WCHAR szFile[MAX_PATH];
123 MultiByteToWideChar(0,0,lpszDir,-1,szDir,MAX_PATH);
125 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
126 PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL);
127 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
132 /*************************************************************************
133 * PathCombineW [SHLWAPI.@]
137 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
139 WCHAR szTemp[MAX_PATH];
140 BOOL bUseBoth = FALSE, bStrip = FALSE;
142 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
144 if (!lpszDest || (!lpszDir && !lpszFile))
145 return lpszDest; /* Invalid parameters */
147 if (!lpszFile || !*lpszFile)
150 strncpyW(szTemp, lpszDir, MAX_PATH);
152 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
154 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
157 strncpyW(szTemp, lpszFile, MAX_PATH);
170 strncpyW(szTemp, lpszDir, MAX_PATH);
173 PathStripToRootW(szTemp);
174 lpszFile++; /* Skip '\' */
176 if (!PathAddBackslashW(szTemp))
178 if (strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
180 strcatW(szTemp, lpszFile);
183 PathCanonicalizeW(lpszDest, szTemp);
187 /*************************************************************************
188 * PathAddBackslashA [SHLWAPI.@]
190 * Append a backslash ('\') to a path if one doesn't exist.
193 * lpszPath [O] The path to append a backslash to.
196 * Success: The position of the last backslash in the path.
197 * Failure: NULL, if lpszPath is NULL or the path is too large.
199 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
203 TRACE("(%s)\n",debugstr_a(lpszPath));
205 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
211 if (lpszPath[-1] != '\\')
220 /*************************************************************************
221 * PathAddBackslashW [SHLWAPI.@]
223 * See PathAddBackslashA.
225 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
229 TRACE("(%s)\n",debugstr_w(lpszPath));
231 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
237 if (lpszPath[-1] != '\\')
246 /*************************************************************************
247 * PathBuildRootA [SHLWAPI.@]
249 * Create a root drive string (e.g. "A:\") from a drive number.
252 * lpszPath [O] Destination for the drive string
258 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
260 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
262 TRACE("(%p,%d)\n", debugstr_a(lpszPath), drive);
264 if (lpszPath && drive >= 0 && drive < 26)
266 lpszPath[0] = 'A' + drive;
274 /*************************************************************************
275 * PathBuildRootW [SHLWAPI.@]
277 * See PathBuildRootA.
279 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
281 TRACE("(%p,%d)\n",debugstr_w(lpszPath), drive);
283 if (lpszPath && drive >= 0 && drive < 26)
285 lpszPath[0] = 'A' + drive;
293 /*************************************************************************
294 * PathFindFileNameA [SHLWAPI.@]
296 * Locate the start of the file name in a path
299 * lpszPath [I] Path to search
302 * A pointer to the first character of the file name
304 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
306 LPCSTR lastSlash = lpszPath;
308 TRACE("(%s)\n",debugstr_a(lpszPath));
310 while (lpszPath && *lpszPath)
312 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
313 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
314 lastSlash = lpszPath + 1;
315 lpszPath = CharNextA(lpszPath);
317 return (LPSTR)lastSlash;
320 /*************************************************************************
321 * PathFindFileNameW [SHLWAPI.@]
323 * See PathFindFileNameA.
325 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
327 LPCWSTR lastSlash = lpszPath;
329 TRACE("(%s)\n",debugstr_w(lpszPath));
331 while (lpszPath && *lpszPath)
333 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
334 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
335 lastSlash = lpszPath + 1;
336 lpszPath = CharNextW(lpszPath);
338 return (LPWSTR)lastSlash;
341 /*************************************************************************
342 * PathFindExtensionA [SHLWAPI.@]
344 * Locate the start of the file extension in a path
347 * lpszPath [I] The path to search
350 * A pointer to the first character of the extension, the end of
351 * the string if the path has no extension, or NULL If lpszPath is NULL
353 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
355 LPCSTR lastpoint = NULL;
357 TRACE("(%s)\n", debugstr_a(lpszPath));
363 if (*lpszPath == '\\' || *lpszPath==' ')
365 else if (*lpszPath == '.')
366 lastpoint = lpszPath;
367 lpszPath = CharNextA(lpszPath);
370 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
373 /*************************************************************************
374 * PathFindExtensionW [SHLWAPI.@]
376 * See PathFindExtensionA.
378 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
380 LPCWSTR lastpoint = NULL;
382 TRACE("(%s)\n", debugstr_w(lpszPath));
388 if (*lpszPath == '\\' || *lpszPath==' ')
390 else if (*lpszPath == '.')
391 lastpoint = lpszPath;
392 lpszPath = CharNextW(lpszPath);
395 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
398 /*************************************************************************
399 * PathGetArgsA [SHLWAPI.@]
401 * Find the next argument in a string delimited by spaces.
404 * lpszPath [I] The string to search for arguments in
407 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
410 * Spaces in quoted strings are ignored as delimiters.
412 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
414 BOOL bSeenQuote = FALSE;
416 TRACE("(%s)\n",debugstr_a(lpszPath));
422 if ((*lpszPath==' ') && !bSeenQuote)
423 return (LPSTR)lpszPath + 1;
424 if (*lpszPath == '"')
425 bSeenQuote = !bSeenQuote;
426 lpszPath = CharNextA(lpszPath);
429 return (LPSTR)lpszPath;
432 /*************************************************************************
433 * PathGetArgsW [SHLWAPI.@]
437 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
439 BOOL bSeenQuote = FALSE;
441 TRACE("(%s)\n",debugstr_w(lpszPath));
447 if ((*lpszPath==' ') && !bSeenQuote)
448 return (LPWSTR)lpszPath + 1;
449 if (*lpszPath == '"')
450 bSeenQuote = !bSeenQuote;
451 lpszPath = CharNextW(lpszPath);
454 return (LPWSTR)lpszPath;
457 /*************************************************************************
458 * PathGetDriveNumberA [SHLWAPI.@]
460 * Return the drive number from a path
463 * lpszPath [I] Path to get the drive number from
466 * Success: The drive number corresponding to the drive in the path
467 * Failure: -1, if lpszPath contains no valid drive
469 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
471 TRACE ("(%s)\n",debugstr_a(lpszPath));
473 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
474 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
475 return tolower(*lpszPath) - 'a';
479 /*************************************************************************
480 * PathGetDriveNumberW [SHLWAPI.@]
482 * See PathGetDriveNumberA.
484 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
486 TRACE ("(%s)\n",debugstr_w(lpszPath));
488 if (lpszPath && lpszPath[1] == ':' &&
489 tolowerW(*lpszPath) >= 'a' && tolowerW(*lpszPath) <= 'z')
490 return tolowerW(*lpszPath) - 'a';
494 /*************************************************************************
495 * PathRemoveFileSpecA [SHLWAPI.@]
497 * Remove the file specification from a path.
500 * lpszPath [O] Path to remove the file spec from
503 * TRUE If the path was valid and modified
506 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
508 LPSTR lpszFileSpec = lpszPath;
509 BOOL bModified = FALSE;
511 TRACE("(%s)\n",debugstr_a(lpszPath));
515 /* Skip directory or UNC path */
516 if (*lpszPath == '\\')
517 lpszFileSpec = ++lpszPath;
518 if (*lpszPath == '\\')
519 lpszFileSpec = ++lpszPath;
523 if(*lpszPath == '\\')
524 lpszFileSpec = lpszPath; /* Skip dir */
525 else if(*lpszPath == ':')
527 lpszFileSpec = ++lpszPath; /* Skip drive */
528 if (*lpszPath == '\\')
531 if (!(lpszPath = CharNextA(lpszPath)))
537 *lpszFileSpec = '\0';
544 /*************************************************************************
545 * PathRemoveFileSpecW [SHLWAPI.@]
547 * See PathRemoveFileSpecA.
549 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
551 LPWSTR lpszFileSpec = lpszPath;
552 BOOL bModified = FALSE;
554 TRACE("(%s)\n",debugstr_w(lpszPath));
558 /* Skip directory or UNC path */
559 if (*lpszPath == '\\')
560 lpszFileSpec = ++lpszPath;
561 if (*lpszPath == '\\')
562 lpszFileSpec = ++lpszPath;
566 if(*lpszPath == '\\')
567 lpszFileSpec = lpszPath; /* Skip dir */
568 else if(*lpszPath == ':')
570 lpszFileSpec = ++lpszPath; /* Skip drive */
571 if (*lpszPath == '\\')
574 if (!(lpszPath = CharNextW(lpszPath)))
580 *lpszFileSpec = '\0';
587 /*************************************************************************
588 * PathStripPathA [SHLWAPI.@]
590 * Remove the initial path from the beginning of a filename
593 * lpszPath [O] Path to remove the initial path from
598 void WINAPI PathStripPathA(LPSTR lpszPath)
600 TRACE("(%s)\n", debugstr_a(lpszPath));
604 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
606 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
610 /*************************************************************************
611 * PathStripPathW [SHLWAPI.@]
613 * See PathStripPathA.
615 void WINAPI PathStripPathW(LPWSTR lpszPath)
619 TRACE("(%s)\n", debugstr_w(lpszPath));
620 lpszFileName = PathFindFileNameW(lpszPath);
622 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
625 /*************************************************************************
626 * PathStripToRootA [SHLWAPI.@]
628 * Reduce a path to its root.
631 * lpszPath [O] the path to reduce
634 * Success: TRUE if the stripped path is a root path
635 * Failure: FALSE if the path cannot be stripped or is NULL
637 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
639 TRACE("(%s)\n", debugstr_a(lpszPath));
643 while(!PathIsRootA(lpszPath))
644 if (!PathRemoveFileSpecA(lpszPath))
649 /*************************************************************************
650 * PathStripToRootW [SHLWAPI.@]
652 * See PathStripToRootA.
654 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
656 TRACE("(%s)\n", debugstr_w(lpszPath));
660 while(!PathIsRootW(lpszPath))
661 if (!PathRemoveFileSpecW(lpszPath))
666 /*************************************************************************
667 * PathRemoveArgsA [SHLWAPI.@]
669 * Strip space seperated arguments from a path.
672 * lpszPath [I] Path to remove arguments from
677 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
679 TRACE("(%s)\n",debugstr_a(lpszPath));
683 LPSTR lpszArgs = PathGetArgsA(lpszPath);
688 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
689 if(*lpszLastChar == ' ')
690 *lpszLastChar = '\0';
695 /*************************************************************************
696 * PathRemoveArgsW [SHLWAPI.@]
698 * See PathRemoveArgsA.
700 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
702 TRACE("(%s)\n",debugstr_w(lpszPath));
706 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
711 LPWSTR lpszLastChar = CharPrevW(lpszPath, lpszArgs);
712 if(*lpszLastChar == ' ')
713 *lpszLastChar = '\0';
718 /*************************************************************************
719 * PathRemoveExtensionA [SHLWAPI.@]
721 * Remove the file extension from a path
724 * lpszPath [O] Path to remove the extension from
729 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
731 TRACE("(%s)\n", debugstr_a(lpszPath));
735 lpszPath = PathFindExtensionA(lpszPath);
740 /*************************************************************************
741 * PathRemoveExtensionW [SHLWAPI.@]
743 * See PathRemoveExtensionA.
745 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
747 TRACE("(%s)\n", debugstr_w(lpszPath));
751 lpszPath = PathFindExtensionW(lpszPath);
756 /*************************************************************************
757 * PathRemoveBackslashA [SHLWAPI.@]
759 * Remove a trailing backslash from a path.
762 * lpszPath [O] Path to remove backslash from
765 * Success: A pointer to the end of the path
766 * Failure: NULL, if lpszPath is NULL
768 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
772 TRACE("(%s)\n", debugstr_a(lpszPath));
776 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
777 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
783 /*************************************************************************
784 * PathRemoveBackslashW [SHLWAPI.@]
786 * See PathRemoveBackslashA.
788 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
790 LPWSTR szTemp = NULL;
792 TRACE("(%s)\n", debugstr_w(lpszPath));
796 szTemp = CharPrevW(lpszPath, lpszPath + strlenW(lpszPath));
797 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
803 /*************************************************************************
804 * PathRemoveBlanksA [SHLWAPI.@]
806 * Remove Spaces from the start and end of a path.
809 * lpszPath [O] Path to strip blanks from
814 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
816 TRACE("(%s)\n", debugstr_a(lpszPath));
818 if(lpszPath && *lpszPath)
820 LPSTR start = lpszPath;
822 while (*lpszPath == ' ')
823 lpszPath = CharNextA(lpszPath);
826 *start++ = *lpszPath++;
828 if (start != lpszPath)
829 while (start[-1] == ' ')
835 /*************************************************************************
836 * PathRemoveBlanksW [SHLWAPI.@]
838 * See PathRemoveBlanksA.
840 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
842 TRACE("(%s)\n", debugstr_w(lpszPath));
844 if(lpszPath && *lpszPath)
846 LPWSTR start = lpszPath;
848 while (*lpszPath == ' ')
852 *start++ = *lpszPath++;
854 if (start != lpszPath)
855 while (start[-1] == ' ')
861 /*************************************************************************
862 * PathQuoteSpacesA [SHLWAPI.@]
864 * Surround a path containg spaces in quotes.
867 * lpszPath [O] Path to quote
873 * The path is not changed if it is invalid or has no spaces.
875 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
877 TRACE("(%s)\n", debugstr_a(lpszPath));
879 if(lpszPath && StrChrA(lpszPath,' '))
881 int iLen = strlen(lpszPath) + 1;
883 if (iLen + 2 < MAX_PATH)
885 memmove(lpszPath + 1, lpszPath, iLen);
887 lpszPath[iLen] = '"';
888 lpszPath[iLen + 1] = '\0';
893 /*************************************************************************
894 * PathQuoteSpacesW [SHLWAPI.@]
896 * See PathQuoteSpacesA.
898 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
900 TRACE("(%s)\n", debugstr_w(lpszPath));
902 if(lpszPath && StrChrW(lpszPath,' '))
904 int iLen = strlenW(lpszPath) + 1;
906 if (iLen + 2 < MAX_PATH)
908 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
910 lpszPath[iLen] = '"';
911 lpszPath[iLen + 1] = '\0';
916 /*************************************************************************
917 * PathUnquoteSpacesA [SHLWAPI.@]
919 * Remove quotes ("") from around a path, if present.
922 * lpszPath [O] Path to strip quotes from
928 * If the path contains a single quote only, an empty string will result.
929 * Otherwise quotes are only removed if they appear at the start and end
932 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
934 TRACE("(%s)\n", debugstr_a(lpszPath));
936 if (lpszPath && *lpszPath == '"')
938 DWORD dwLen = strlen(lpszPath) - 1;
940 if (lpszPath[dwLen] == '"')
942 lpszPath[dwLen] = '\0';
943 for (; *lpszPath; lpszPath++)
944 *lpszPath = lpszPath[1];
949 /*************************************************************************
950 * PathUnquoteSpacesW [SHLWAPI.@]
952 * See PathUnquoteSpacesA.
954 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
956 TRACE("(%s)\n", debugstr_w(lpszPath));
958 if (lpszPath && *lpszPath == '"')
960 DWORD dwLen = strlenW(lpszPath) - 1;
962 if (lpszPath[dwLen] == '"')
964 lpszPath[dwLen] = '\0';
965 for (; *lpszPath; lpszPath++)
966 *lpszPath = lpszPath[1];
971 /*************************************************************************
972 * PathParseIconLocationA [SHLWAPI.@]
974 * Parse the location of an icon from a path.
977 * lpszPath [O] The path to parse the icon location from.
980 * Success: The number of the icon
981 * Failure: 0 if the path does not contain an icon location or is NULL
984 * The path has surrounding quotes and spaces removed regardless
985 * of whether the call succeeds or not.
987 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
992 TRACE("(%s)\n", debugstr_a(lpszPath));
996 if ((lpszComma = strchr(lpszPath, ',')))
999 iRet = StrToIntA(lpszComma);
1001 PathUnquoteSpacesA(lpszPath);
1002 PathRemoveBlanksA(lpszPath);
1007 /*************************************************************************
1008 * PathParseIconLocationW [SHLWAPI.@]
1010 * See PathParseIconLocationA.
1012 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1017 TRACE("(%s)\n", debugstr_w(lpszPath));
1021 if ((lpszComma = StrChrW(lpszPath, ',')))
1023 *lpszComma++ = '\0';
1024 iRet = StrToIntW(lpszComma);
1026 PathUnquoteSpacesW(lpszPath);
1027 PathRemoveBlanksW(lpszPath);
1032 /*************************************************************************
1033 * SHLWAPI_PathFindLocalExeA
1035 * Internal implementation of SHLWAPI_3.
1037 BOOL WINAPI SHLWAPI_PathFindLocalExeA (LPSTR lpszPath, DWORD dwWhich)
1041 TRACE("(%s,%ld)\n", debugstr_a(lpszPath), dwWhich);
1045 WCHAR szPath[MAX_PATH];
1046 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1047 bRet = SHLWAPI_PathFindLocalExeW(szPath, dwWhich);
1049 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1054 /*************************************************************************
1055 * SHLWAPI_PathFindLocalExeW
1057 * Internal implementation of SHLWAPI_4.
1059 BOOL WINAPI SHLWAPI_PathFindLocalExeW (LPWSTR lpszPath, DWORD dwWhich)
1061 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', '0'},
1062 { '.', 'c', 'o', 'm', '0'},
1063 { '.', 'e', 'x', 'e', '0'},
1064 { '.', 'b', 'a', 't', '0'},
1065 { '.', 'l', 'n', 'k', '0'},
1066 { '.', 'c', 'm', 'd', '0'},
1067 { '0', '0', '0', '0', '0'} };
1069 TRACE("(%s,%ld)\n", debugstr_w(lpszPath), dwWhich);
1071 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1076 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1077 if (!*szExt || dwWhich & 0x40)
1080 int iLen = lstrlenW(lpszPath);
1081 if (iLen > (MAX_PATH - 5))
1083 while (dwWhich & 0x1 && iChoose < sizeof(pszExts))
1085 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1086 if (PathFileExistsW(lpszPath))
1091 *(lpszPath + iLen) = (WCHAR)'\0';
1095 return PathFileExistsW(lpszPath);
1098 /*************************************************************************
1099 * SHLWAPI_PathFindInOtherDirs
1101 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1103 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1105 static WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1106 static WCHAR szPath[] = { 'P','A','T','H','\0'};
1110 WCHAR buff[MAX_PATH];
1112 TRACE("(%s,%08lx)\n", debugstr_w(lpszFile), dwWhich);
1114 /* Try system directories */
1115 GetSystemDirectoryW(buff, MAX_PATH);
1116 if (!PathAppendW(buff, lpszFile))
1118 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1120 strcpyW(lpszFile, buff);
1123 GetWindowsDirectoryW(buff, MAX_PATH);
1124 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1126 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1128 strcpyW(lpszFile, buff);
1131 GetWindowsDirectoryW(buff, MAX_PATH);
1132 if (!PathAppendW(buff, lpszFile))
1134 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1136 strcpyW(lpszFile, buff);
1139 /* Try dirs listed in %PATH% */
1140 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1142 if (!dwLenPATH || !(lpszPATH = malloc((dwLenPATH + 1) * sizeof (WCHAR))))
1145 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1146 lpszCurr = lpszPATH;
1149 LPCWSTR lpszEnd = lpszCurr;
1150 LPWSTR pBuff = buff;
1152 while (*lpszEnd == ' ')
1154 while (*lpszEnd && *lpszEnd != ';')
1155 *pBuff++ = *lpszEnd++;
1159 lpszCurr = lpszEnd + 1;
1161 lpszCurr = NULL; /* Last Path, terminate after this */
1163 if (!PathAppendW(buff, lpszFile))
1165 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1167 strcpyW(lpszFile, buff);
1177 /*************************************************************************
1178 * SHLWAPI_PathFindOnPathExA
1180 * Internal implementation of SHLWAPI_5
1182 BOOL WINAPI SHLWAPI_PathFindOnPathExA(LPSTR lpszFile, LPCSTR *lppszOtherDirs, DWORD dwWhich)
1184 WCHAR szFile[MAX_PATH];
1185 WCHAR buff[MAX_PATH];
1187 TRACE("(%s,%p,%08lx)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1189 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1192 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
1194 /* Search provided directories first */
1195 if (lppszOtherDirs && *lppszOtherDirs)
1197 WCHAR szOther[MAX_PATH];
1198 LPCSTR *lpszOtherPath = lppszOtherDirs;
1200 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1202 MultiByteToWideChar(0,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1203 PathCombineW(buff, szOther, szFile);
1204 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1206 WideCharToMultiByte(0,0,buff,-1,lpszFile,MAX_PATH,0,0);
1212 /* Not found, try system and path dirs */
1213 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1215 WideCharToMultiByte(0,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1221 /*************************************************************************
1222 * SHLWAPI_PathFindOnPathExW
1224 * Internal implementation of SHLWAPI_6.
1226 BOOL WINAPI SHLWAPI_PathFindOnPathExW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs, DWORD dwWhich)
1228 WCHAR buff[MAX_PATH];
1230 TRACE("(%s,%p,%08lx)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1232 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1235 /* Search provided directories first */
1236 if (lppszOtherDirs && *lppszOtherDirs)
1238 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1239 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1241 PathCombineW(buff, *lpszOtherPath, lpszFile);
1242 if (SHLWAPI_PathFindLocalExeW(buff, dwWhich))
1244 strcpyW(lpszFile, buff);
1250 /* Not found, try system and path dirs */
1251 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1254 /*************************************************************************
1255 * PathFindOnPathA [SHLWAPI.@]
1257 * Search a range of paths for an executable.
1260 * lpszFile [O] File to search for
1261 * lppszOtherDirs [I] Other directories to look in
1264 * Success: TRUE. The path to the executable is stored in lpszFile.
1265 * Failure: FALSE. The path to the executable is unchanged.
1267 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1269 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1270 return SHLWAPI_PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1273 /*************************************************************************
1274 * PathFindOnPathW [SHLWAPI.@]
1276 * See PathFindOnPathA.
1278 BOOL WINAPI PathFindOnPathW (LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1280 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1281 return SHLWAPI_PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1284 /*************************************************************************
1285 * PathCompactPathExA [SHLWAPI.@]
1290 * lpszDest [O] Destination for compacted path
1291 * lpszPath [I] Source path
1292 * cchMax [I[ Size of lpszDest
1293 * dwFlags [I] Compaction flags
1298 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1299 UINT cchMax, DWORD dwFlags)
1303 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1305 if (lpszPath && lpszDest)
1307 WCHAR szPath[MAX_PATH];
1308 WCHAR szDest[MAX_PATH];
1310 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1312 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1313 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1318 /*************************************************************************
1319 * PathCompactPathExW [SHLWAPI.@]
1321 * See PathCompactPathExA.
1323 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1324 UINT cchMax, DWORD dwFlags)
1326 FIXME("(%p,%s,%d,0x%08lx)-stub\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1333 WARN("Invalid lpszDest would crash under Win32!\n");
1342 /*************************************************************************
1343 * PathIsRelativeA [SHLWAPI.@]
1345 * Determine if a path is a relative path.
1348 * lpszPath [I] Path to check
1351 * TRUE: The path is relative, or is invalid.
1352 * FALSE: The path is not relative.
1354 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1356 TRACE("(%s)\n",debugstr_a(lpszPath));
1358 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1360 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1365 /*************************************************************************
1366 * PathIsRelativeW [SHLWAPI.@]
1368 * See PathIsRelativeA.
1370 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1372 TRACE("(%s)\n",debugstr_w(lpszPath));
1374 if (!lpszPath || !*lpszPath)
1376 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1381 /*************************************************************************
1382 * PathIsRootA [SHLWAPI.@]
1384 * Determine if a path is a root path.
1387 * lpszPath [I] Path to check
1390 * TRUE If lpszPath is valid and a root path
1393 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1395 TRACE("(%s)\n", debugstr_a(lpszPath));
1397 if (lpszPath && *lpszPath)
1399 if (*lpszPath == '\\')
1402 return TRUE; /* \ */
1403 else if (lpszPath[1]=='\\')
1405 BOOL bSeenSlash = FALSE;
1408 /* Check for UNC root path */
1411 if (*lpszPath == '\\')
1417 lpszPath = CharNextA(lpszPath);
1422 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1423 return TRUE; /* X:\ */
1428 /*************************************************************************
1429 * PathIsRootW [SHLWAPI.@]
1433 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1435 TRACE("(%s)\n", debugstr_w(lpszPath));
1437 if (lpszPath && *lpszPath)
1439 if (*lpszPath == '\\')
1442 return TRUE; /* \ */
1443 else if (lpszPath[1]=='\\')
1445 BOOL bSeenSlash = FALSE;
1448 /* Check for UNC root path */
1451 if (*lpszPath == '\\')
1457 lpszPath = CharNextW(lpszPath);
1462 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1463 return TRUE; /* X:\ */
1468 /*************************************************************************
1469 * PathIsDirectoryA [SHLWAPI.@]
1471 * Determine if a path is a valid directory
1474 * lpszPath [I] Path to check.
1477 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1478 * FALSE if lpszPath is invalid or not a directory.
1481 * Although this function is prototyped as returning a BOOL, it returns
1482 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1484 * if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1489 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1493 TRACE("(%s)\n", debugstr_a(lpszPath));
1495 if (!lpszPath || PathIsUNCServerA(lpszPath))
1498 if (PathIsUNCServerShareA(lpszPath))
1500 FIXME("UNC Server Share not yet supported - FAILING\n");
1504 if ((dwAttr = GetFileAttributesA(lpszPath)) == -1)
1506 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1509 /*************************************************************************
1510 * PathIsDirectoryW [SHLWAPI.@]
1512 * See PathIsDirectoryA.
1514 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1518 TRACE("(%s)\n", debugstr_w(lpszPath));
1520 if (!lpszPath || PathIsUNCServerW(lpszPath))
1523 if (PathIsUNCServerShareW(lpszPath))
1525 FIXME("UNC Server Share not yet supported - FAILING\n");
1529 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1)
1531 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1534 /*************************************************************************
1535 * PathFileExistsA [SHLWAPI.@]
1537 * Determine if a file exists.
1540 * lpszPath [I] Path to check
1543 * TRUE If the file exists and is readable
1546 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1551 TRACE("(%s)\n",debugstr_a(lpszPath));
1556 iPrevErrMode = SetErrorMode(1);
1557 dwAttr = GetFileAttributesA(lpszPath);
1558 SetErrorMode(iPrevErrMode);
1559 return dwAttr == -1 ? FALSE : TRUE;
1562 /*************************************************************************
1563 * PathFileExistsW [SHLWAPI.@]
1565 * See PathFileExistsA
1567 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1572 TRACE("(%s)\n",debugstr_w(lpszPath));
1577 iPrevErrMode = SetErrorMode(1);
1578 dwAttr = GetFileAttributesW(lpszPath);
1579 SetErrorMode(iPrevErrMode);
1580 return dwAttr == -1 ? FALSE : TRUE;
1583 /*************************************************************************
1584 * PathMatchSingleMaskA [internal]
1587 * internal (used by PathMatchSpec)
1589 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1591 while (*name && *mask && *mask!=';')
1597 if (PathMatchSingleMaskA(name,mask+1)) return 1; /* try substrings */
1601 if (toupper(*mask)!=toupper(*name) && *mask!='?') return 0;
1602 name = CharNextA(name);
1603 mask = CharNextA(mask);
1607 while (*mask=='*') mask++;
1608 if (!*mask || *mask==';') return 1;
1613 /*************************************************************************
1614 * PathMatchSingleMaskW [internal]
1616 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1618 while (*name && *mask && *mask!=';')
1624 if (PathMatchSingleMaskW(name,mask+1)) return 1; /* try substrings */
1628 if (toupperW(*mask)!=toupperW(*name) && *mask!='?') return 0;
1629 name = CharNextW(name);
1630 mask = CharNextW(mask);
1634 while (*mask=='*') mask++;
1635 if (!*mask || *mask==';') return 1;
1640 /*************************************************************************
1641 * PathMatchSpecA [SHLWAPI.@]
1643 * Determine if a path matches one or more search masks.
1646 * lpszPath [I] Path to check
1647 * lpszMask [I} Search mask(s)
1650 * TRUE If lpszPath is valid and is matched
1654 * Multiple search masks may be given if they are seperated by ";". The
1655 * pattern "*.*" is treated specially in that it matches all paths (for
1656 * backwards compatability with DOS).
1658 BOOL WINAPI PathMatchSpecA(LPCSTR name, LPCSTR mask)
1660 TRACE("%s %s\n",name,mask);
1662 if (!lstrcmpA( mask, "*.*" )) return 1; /* we don't require a period */
1666 if (PathMatchSingleMaskA(name,mask)) return 1; /* helper function */
1667 while (*mask && *mask!=';') mask = CharNextA(mask);
1671 while (*mask==' ') mask++; /* masks may be separated by "; " */
1677 /*************************************************************************
1678 * PathMatchSpecW [SHLWAPI.@]
1680 * See PathMatchSpecA.
1682 BOOL WINAPI PathMatchSpecW(LPCWSTR name, LPCWSTR mask)
1684 static const WCHAR stemp[] = { '*','.','*',0 };
1685 TRACE("%s %s\n",debugstr_w(name),debugstr_w(mask));
1687 if (!lstrcmpW( mask, stemp )) return 1; /* we don't require a period */
1691 if (PathMatchSingleMaskW(name,mask)) return 1; /* helper function */
1692 while (*mask && *mask!=';') mask = CharNextW(mask);
1696 while (*mask==' ') mask++; /* masks may be separated by "; " */
1702 /*************************************************************************
1703 * PathIsSameRootA [SHLWAPI.@]
1705 * Determine if two paths share the same root.
1708 * lpszPath1 [I] Source path
1709 * lpszPath2 [I] Path to compare with
1712 * TRUE If both paths are valid and share the same root.
1713 * FALSE If either path is invalid or the paths do not share the same root.
1715 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1720 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1722 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1725 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1726 if (lpszStart - lpszPath1 > dwLen)
1727 return FALSE; /* Paths not common up to length of the root */
1731 /*************************************************************************
1732 * PathIsSameRootW [SHLWAPI.@]
1734 * See PathIsSameRootA.
1736 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1741 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1743 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1746 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1747 if (lpszStart - lpszPath1 > dwLen)
1748 return FALSE; /* Paths not common up to length of the root */
1752 /*************************************************************************
1753 * PathIsURLA [SHLWAPI.@]
1755 * Check if the given path is a URL.
1758 * lpszPath [I] Path to check.
1761 * TRUE if lpszPath is a URL.
1762 * FALSE if lpszPath is NULL or not a URL.
1764 BOOL WINAPI PathIsURLA(LPCSTR lpstrPath)
1766 UNKNOWN_SHLWAPI_1 base;
1769 if (!lpstrPath || !*lpstrPath) return FALSE;
1773 res1 = SHLWAPI_1(lpstrPath, &base);
1774 return (base.fcncde) ? TRUE : FALSE;
1777 /*************************************************************************
1778 * PathIsURLW [SHLWAPI.@]
1780 BOOL WINAPI PathIsURLW(LPCWSTR lpstrPath)
1782 UNKNOWN_SHLWAPI_2 base;
1785 if (!lpstrPath || !*lpstrPath) return FALSE;
1789 res1 = SHLWAPI_2(lpstrPath, &base);
1790 return (base.fcncde) ? TRUE : FALSE;
1793 /*************************************************************************
1794 * PathIsContentTypeA [SHLWAPI.@]
1796 * Determine if a file is of a registered content type.
1799 * lpszPath [I] file to chack
1802 * TRUE If lpszPath is a registered content type
1805 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1809 char szBuff[MAX_PATH];
1811 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
1813 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
1814 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
1815 REG_NONE, szBuff, &dwDummy) &&
1816 !strcasecmp(lpszContentType, szBuff))
1823 /*************************************************************************
1824 * PathIsContentTypeW [SHLWAPI.@]
1826 * See PathIsContentTypeA.
1828 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
1830 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
1833 WCHAR szBuff[MAX_PATH];
1835 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
1837 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
1838 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
1839 REG_NONE, szBuff, &dwDummy) &&
1840 !strcmpiW(lpszContentType, szBuff))
1847 /*************************************************************************
1848 * PathIsFileSpecA [SHLWAPI.@]
1850 * Determine if a path is a file specification.
1853 * lpszPath [I] Path to chack
1856 * TRUE If lpszPath is a file spec (contains no directories).
1859 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
1861 TRACE("(%s)\n", debugstr_a(lpszPath));
1868 if (*lpszPath == '\\' || *lpszPath == ':')
1870 lpszPath = CharNextA(lpszPath);
1875 /*************************************************************************
1876 * PathIsFileSpecW [SHLWAPI.@]
1878 * See PathIsFileSpecA.
1880 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
1882 TRACE("(%s)\n", debugstr_w(lpszPath));
1889 if (*lpszPath == '\\' || *lpszPath == ':')
1891 lpszPath = CharNextW(lpszPath);
1896 /*************************************************************************
1897 * PathIsPrefixA [SHLWAPI.@]
1899 * Determine if a path is a prefix of another.
1902 * lpszPrefix [I] Prefix
1903 * lpszPath [i] Path to check
1906 * TRUE If lpszPath has lpszPrefix as its prefix
1907 * FALSE If either path is NULL or lpszPrefix is not a prefix
1909 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
1911 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
1913 if (lpszPrefix && lpszPath &&
1914 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == strlen(lpszPrefix))
1919 /*************************************************************************
1920 * PathIsPrefixW [SHLWAPI.@]
1922 * See PathIsPrefixA.
1924 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
1926 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
1928 if (lpszPrefix && lpszPath &&
1929 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == strlenW(lpszPrefix))
1934 /*************************************************************************
1935 * PathIsSystemFolderA [SHLWAPI.@]
1937 * Determine if a path or file attributes are a system folder.
1940 * lpszPath [I] Path to check.
1941 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
1944 * TRUE If lpszPath or dwAttrib are a system folder.
1945 * FALSE If GetFileAttributesA fails or neither parameter is a system folder.
1947 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
1949 TRACE("(%s,0x%08lx)\n", debugstr_a(lpszPath), dwAttrib);
1951 if (lpszPath && *lpszPath)
1952 dwAttrib = GetFileAttributesA(lpszPath);
1954 if (dwAttrib == -1 || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
1955 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
1960 /*************************************************************************
1961 * PathIsSystemFolderW [SHLWAPI.@]
1963 * See PathIsSystemFolderA.
1965 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
1967 TRACE("(%s,0x%08lx)\n", debugstr_w(lpszPath), dwAttrib);
1969 if (lpszPath && *lpszPath)
1970 dwAttrib = GetFileAttributesW(lpszPath);
1972 if (dwAttrib == -1 || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
1973 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
1978 /*************************************************************************
1979 * PathIsUNCA [SHLWAPI.@]
1981 * Determine if a path is in UNC format.
1984 * lpszPath [I] Path to check
1987 * TRUE: The path is UNC.
1988 * FALSE: The path is not UNC or is NULL.
1990 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
1992 TRACE("(%s)\n",debugstr_a(lpszPath));
1994 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
1999 /*************************************************************************
2000 * PathIsUNCW [SHLWAPI.@]
2004 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2006 TRACE("(%s)\n",debugstr_w(lpszPath));
2008 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2013 /*************************************************************************
2014 * PathIsUNCServerA [SHLWAPI.@]
2016 * Determine if a path is a UNC server name ("\\SHARENAME").
2019 * lpszPath [I] Path to check.
2022 * TRUE If lpszPath is a valid UNC server name.
2026 * This routine is bug compatible with Win32: Server names with a
2027 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2028 * Fixing this bug may break other shlwapi functions!
2030 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2032 TRACE("(%s)\n", debugstr_a(lpszPath));
2034 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2038 if (*lpszPath == '\\')
2040 lpszPath = CharNextA(lpszPath);
2047 /*************************************************************************
2048 * PathIsUNCServerW [SHLWAPI.@]
2050 * See PathIsUNCServerA.
2052 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2054 TRACE("(%s)\n", debugstr_w(lpszPath));
2056 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2060 if (*lpszPath == '\\')
2062 lpszPath = CharNextW(lpszPath);
2069 /*************************************************************************
2070 * PathIsUNCServerShareA [SHLWAPI.@]
2072 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2075 * lpszPath [I] Path to check.
2078 * TRUE If lpszPath is a valid UNC server share.
2082 * This routine is bug compatible with Win32: Server shares with a
2083 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2084 * Fixing this bug may break other shlwapi functions!
2086 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2088 TRACE("(%s)\n", debugstr_a(lpszPath));
2090 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2092 BOOL bSeenSlash = FALSE;
2095 if (*lpszPath == '\\')
2101 lpszPath = CharNextA(lpszPath);
2108 /*************************************************************************
2109 * PathIsUNCServerShareW [SHLWAPI.@]
2111 * See PathIsUNCServerShareA.
2113 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2115 TRACE("(%s)\n", debugstr_w(lpszPath));
2117 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2119 BOOL bSeenSlash = FALSE;
2122 if (*lpszPath == '\\')
2128 lpszPath = CharNextW(lpszPath);
2135 /*************************************************************************
2136 * PathCanonicalizeA [SHLWAPI.@]
2138 * Convert a path to its canonical form.
2141 * lpszBuf [O] Output path
2142 * lpszPath [I] Path to cnonicalize
2145 * Success: TRUE. lpszBuf contains the output path
2146 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2148 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2152 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2157 if (!lpszBuf || !lpszPath)
2158 SetLastError(ERROR_INVALID_PARAMETER);
2161 WCHAR szPath[MAX_PATH];
2162 WCHAR szBuff[MAX_PATH];
2163 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2164 bRet = PathCanonicalizeW(szBuff, szPath);
2165 WideCharToMultiByte(0,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2171 /*************************************************************************
2172 * PathCanonicalizeW [SHLWAPI.@]
2174 * See PathCanonicalizeA.
2176 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2178 LPWSTR lpszDst = lpszBuf;
2179 LPCWSTR lpszSrc = lpszPath;
2181 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2186 if (!lpszBuf || !lpszPath)
2188 SetLastError(ERROR_INVALID_PARAMETER);
2199 /* Copy path root */
2200 if (*lpszSrc == '\\')
2202 *lpszDst++ = *lpszSrc++;
2204 else if (*lpszSrc && lpszSrc[1] == ':')
2207 *lpszDst++ = *lpszSrc++;
2208 *lpszDst++ = *lpszSrc++;
2209 if (*lpszSrc == '\\')
2210 *lpszDst++ = *lpszSrc++;
2213 /* Canonicalize the rest of the path */
2216 if (*lpszSrc == '.')
2218 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2220 lpszSrc += 2; /* Skip .\ */
2222 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2224 /* \.. backs up a directory, over the root if it has no \ following X:.
2225 * .. is ignored if it would remove a UNC server name or inital \\
2227 if (lpszDst != lpszBuf)
2229 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2230 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2231 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2233 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2236 while (lpszDst > lpszBuf && *lpszDst != '\\')
2238 if (*lpszDst == '\\')
2239 lpszDst++; /* Reset to last '\' */
2241 lpszDst = lpszBuf; /* Start path again from new root */
2243 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2246 while (lpszDst > lpszBuf && *lpszDst != '\\')
2248 if (lpszDst == lpszBuf)
2254 lpszSrc += 2; /* Skip .. in src path */
2257 *lpszDst++ = *lpszSrc++;
2260 *lpszDst++ = *lpszSrc++;
2262 /* Append \ to naked drive specs */
2263 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2269 /*************************************************************************
2270 * PathFindNextComponentA [SHLWAPI.@]
2272 * Find the next component in a path.
2275 * lpszPath [I] Path to find next component in
2278 * Success: A pointer to the next component, or the end of the string
2279 * Failure: NULL, If lpszPath is invalid
2282 * A 'component' is either a backslash character (\) or UNC marker (\\).
2283 * Because of this, relative paths (e.g "c:foo") are regarded as having
2284 * only one component.
2286 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2290 TRACE("(%s)\n", debugstr_a(lpszPath));
2292 if(!lpszPath || !*lpszPath)
2295 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2297 if (lpszSlash[1] == '\\')
2299 return lpszSlash + 1;
2301 return (LPSTR)lpszPath + strlen(lpszPath);
2304 /*************************************************************************
2305 * PathFindNextComponentW [SHLWAPI.@]
2307 * See PathFindNextComponentA.
2309 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2313 TRACE("(%s)\n", debugstr_w(lpszPath));
2315 if(!lpszPath || !*lpszPath)
2318 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2320 if (lpszSlash[1] == '\\')
2322 return lpszSlash + 1;
2324 return (LPWSTR)lpszPath + strlenW(lpszPath);
2327 /*************************************************************************
2328 * PathAddExtensionA [SHLWAPI.@]
2330 * Add a file extension to a path
2333 * lpszPath [O] Path to add extension to
2334 * lpszExtension [I} Extension to add to lpszPath
2337 * TRUE If the path was modified
2338 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2339 * extension allready, or the new path length is too big.
2342 * What version of shlwapi.dll adds "exe" if pszExtension is NULL? Win2k
2343 * does not do this, so the behaviour was removed.
2345 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2349 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2351 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2354 dwLen = strlen(lpszPath);
2356 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2359 strcpy(lpszPath + dwLen, lpszExtension);
2363 /*************************************************************************
2364 * PathAddExtensionW [SHLWAPI.@]
2366 * See PathAddExtensionA.
2368 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2372 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2374 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2377 dwLen = strlenW(lpszPath);
2379 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2382 strcpyW(lpszPath + dwLen, lpszExtension);
2386 /*************************************************************************
2387 * PathMakePrettyA [SHLWAPI.@]
2389 * Convert an uppercase DOS filename into lowercase.
2392 * lpszPath [O] Path to convert.
2395 * TRUE If the path was an uppercase DOS path and was converted
2398 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2400 LPSTR pszIter = lpszPath;
2402 TRACE("(%s)\n", debugstr_a(lpszPath));
2404 if (!pszIter || !*pszIter)
2409 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2410 return FALSE; /* Not DOS path */
2413 pszIter = lpszPath + 1;
2416 *pszIter = tolower(*pszIter);
2422 /*************************************************************************
2423 * PathMakePrettyW [SHLWAPI.@]
2425 * See PathMakePrettyA
2427 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2429 LPWSTR pszIter = lpszPath;
2431 TRACE("(%s)\n", debugstr_w(lpszPath));
2433 if (!pszIter || !*pszIter)
2438 if (islowerW(*pszIter))
2439 return FALSE; /* Not DOS path */
2442 pszIter = lpszPath + 1;
2445 *pszIter = tolowerW(*pszIter);
2451 /*************************************************************************
2452 * PathCommonPrefixA [SHLWAPI.@]
2454 * Determine the length of the common prefix between two paths.
2457 * lpszFile1 [I] First path for comparason
2458 * lpszFile2 [I] Second path for comparason
2459 * achPath [O] Destination for common prefix string
2462 * The length of the common prefix. This is 0 if there is no common
2463 * prefix between the paths or if any parameters are invalid. If the prefix
2464 * is non-zero and achPath is not NULL, achPath is filled with the common
2465 * part of the prefix and NUL terminated.
2468 * A common prefix of 2 is always returned as 3. It is thus possible for
2469 * the length returned to be invalid (i.e. Longer than one or both of the
2470 * strings given as parameters). This Win32 behaviour has been implimented
2471 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2472 * To work around this when using this function, always check that the byte
2473 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2475 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2478 LPCSTR lpszIter1 = lpszFile1;
2479 LPCSTR lpszIter2 = lpszFile2;
2481 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2486 if (!lpszFile1 || !lpszFile2)
2489 /* Handle roots first */
2490 if (PathIsUNCA(lpszFile1))
2492 if (!PathIsUNCA(lpszFile2))
2497 else if (PathIsUNCA(lpszFile2))
2498 return 0; /* Know already lpszFile1 is not UNC */
2503 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2504 (!*lpszIter2 || *lpszIter2 == '\\'))
2505 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2507 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2508 break; /* Strings differ at this point */
2515 iLen++; /* Feature/Bug compatable with Win32 */
2517 if (iLen && achPath)
2519 memcpy(achPath,lpszFile1,iLen);
2520 achPath[iLen] = '\0';
2525 /*************************************************************************
2526 * PathCommonPrefixW [SHLWAPI.@]
2528 * See PathCommonPrefixA.
2530 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2533 LPCWSTR lpszIter1 = lpszFile1;
2534 LPCWSTR lpszIter2 = lpszFile2;
2536 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2541 if (!lpszFile1 || !lpszFile2)
2544 /* Handle roots first */
2545 if (PathIsUNCW(lpszFile1))
2547 if (!PathIsUNCW(lpszFile2))
2552 else if (PathIsUNCW(lpszFile2))
2553 return 0; /* Know already lpszFile1 is not UNC */
2558 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2559 (!*lpszIter2 || *lpszIter2 == '\\'))
2560 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2562 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2563 break; /* Strings differ at this point */
2570 iLen++; /* Feature/Bug compatable with Win32 */
2572 if (iLen && achPath)
2574 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2575 achPath[iLen] = '\0';
2580 /*************************************************************************
2581 * PathCompactPathA [SHLWAPI.@]
2583 * Make a path fit into a given width when printed to a DC.
2586 * hDc [I] Destination DC
2587 * lpszPath [O] Path to be printed to hDc
2588 * dx [i] Desired width
2591 * TRUE If the path was modified.
2594 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2598 TRACE("(%08x,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2602 WCHAR szPath[MAX_PATH];
2603 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2604 bRet = PathCompactPathW(hDC, szPath, dx);
2605 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2610 /*************************************************************************
2611 * PathCompactPathW [SHLWAPI.@]
2613 * See PathCompactPathA.
2615 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2617 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2620 WCHAR buff[MAX_PATH];
2624 TRACE("(%08x,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2630 hdc = hDC = GetDC(0);
2632 /* Get the length of the whole path */
2633 dwLen = strlenW(lpszPath);
2634 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2638 /* Path too big, must reduce it */
2640 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2642 sFile = PathFindFileNameW(lpszPath);
2643 if (sFile != lpszPath)
2644 sFile = CharPrevW(lpszPath, sFile);
2646 /* Get the size of ellipses */
2647 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2648 dwEllipsesLen = size.cx;
2649 /* Get the size of the file name */
2650 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2651 dwPathLen = size.cx;
2653 if (sFile != lpszPath)
2655 LPWSTR sPath = sFile;
2656 BOOL bEllipses = FALSE;
2658 /* The path includes a file name. Include as much of the path prior to
2659 * the file name as possible, allowing for the ellipses, e.g:
2660 * c:\some very long path\filename ==> c:\some v...\filename
2662 strncpyW(buff, sFile, MAX_PATH);
2666 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2668 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2669 dwTotalLen += size.cx;
2670 if (dwTotalLen <= dx)
2672 sPath = CharPrevW(lpszPath, sPath);
2676 sPath = CharPrevW(lpszPath, sPath);
2677 sPath = CharPrevW(lpszPath, sPath);
2679 } while (sPath > lpszPath);
2681 if (sPath > lpszPath)
2685 strcpyW(sPath, szEllipses);
2686 strcpyW(sPath+3, buff);
2692 strcpyW(lpszPath, szEllipses);
2693 strcpyW(lpszPath+3, buff);
2697 /* Trim the path by adding ellipses to the end, e.g:
2698 * A very long file name.txt ==> A very...
2700 dwLen = strlenW(lpszPath);
2702 if (dwLen > MAX_PATH - 3)
2703 dwLen = MAX_PATH - 3;
2704 strncpyW(buff, sFile, dwLen);
2708 GetTextExtentPointW(hDC, buff, dwLen, &size);
2709 } while (dwLen && size.cx + dwEllipsesLen > dx);
2713 DWORD dwWritten = 0;
2715 dwEllipsesLen /= 3; /* Size of a single '.' */
2717 /* Write as much of the Ellipses string as possible */
2718 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2721 dwWritten += dwEllipsesLen;
2729 strcpyW(buff + dwLen, szEllipses);
2730 strcpyW(lpszPath, buff);
2740 /*************************************************************************
2741 * PathGetCharTypeA [SHLWAPI.@]
2743 * Categorise a character from a file path.
2746 * ch [I] Character to get the type of
2749 * A set of GCT_ bit flags (from shlwapi.h) indicating the character type.
2751 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2753 return PathGetCharTypeW(ch);
2756 /*************************************************************************
2757 * PathGetCharTypeW [SHLWAPI.@]
2759 * See PathGetCharTypeA.
2761 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2765 TRACE("(%d)\n", ch);
2767 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2768 ch == '"' || ch == '|' || ch == 255)
2769 flags = GCT_INVALID; /* Invalid */
2770 else if (ch == '*' || ch=='?')
2771 flags = GCT_WILD; /* Wildchars */
2772 else if ((ch == '\\') || (ch=='/') || (ch == ':'))
2773 return GCT_SEPARATOR; /* Path separators */
2778 if (!ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2779 ch == '.' || ch == '@' || ch == '^' ||
2780 ch == '\'' || ch == 130 || ch == '`')
2781 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2785 flags |= GCT_SHORTCHAR; /* Bug compatable with win32 */
2786 flags |= GCT_LFNCHAR; /* Valid for long file names */
2791 /*************************************************************************
2792 * SHLWAPI_UseSystemForSystemFolders
2794 * Internal helper for PathMakeSystemFolderW.
2796 static BOOL SHLWAPI_UseSystemForSystemFolders()
2798 static BOOL bCheckedReg = FALSE;
2799 static BOOL bUseSystemForSystemFolders = FALSE;
2805 /* Key tells Win what file attributes to use on system folders */
2806 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2807 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2808 "UseSystemForSystemFolders", 0, 0, 0))
2809 bUseSystemForSystemFolders = TRUE;
2811 return bUseSystemForSystemFolders;
2814 /*************************************************************************
2815 * PathMakeSystemFolderA [SHLWAPI.@]
2817 * Set system folder attribute for a path.
2820 * lpszPath [I] The path to turn into a system folder
2823 * TRUE If the path was changed to/already was a system folder
2824 * FALSE If the path is invalid or SetFileAttributesA fails
2826 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
2830 TRACE("(%s)\n", debugstr_a(lpszPath));
2832 if (lpszPath && *lpszPath)
2834 WCHAR szPath[MAX_PATH];
2835 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2836 bRet = PathMakeSystemFolderW(szPath);
2841 /*************************************************************************
2842 * PathMakeSystemFolderW [SHLWAPI.@]
2844 * See PathMakeSystemFolderA.
2846 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
2848 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
2849 WCHAR buff[MAX_PATH];
2851 TRACE("(%s)\n", debugstr_w(lpszPath));
2853 if (!lpszPath || !*lpszPath)
2856 /* If the directory is already a system directory, dont do anything */
2857 GetSystemDirectoryW(buff, MAX_PATH);
2858 if (!strcmpW(buff, lpszPath))
2861 GetWindowsDirectoryW(buff, MAX_PATH);
2862 if (!strcmpW(buff, lpszPath))
2865 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
2866 if (SHLWAPI_UseSystemForSystemFolders())
2867 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
2869 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1)
2872 /* Change file attributes to system attributes */
2873 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
2874 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
2877 /*************************************************************************
2878 * PathRenameExtensionA [SHLWAPI.@]
2880 * Swap the file extension in a path with another extension.
2883 * pszPath [O] Path to swap the extension in
2884 * pszExt [I] The new extension
2887 * TRUE if pszPath was modified
2888 * FALSE if pszPath or pszExt is NULL, or the new path is too long
2890 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
2892 LPSTR lpszExtension;
2894 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
2896 lpszExtension = PathFindExtensionA(lpszPath);
2898 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
2901 strcpy(lpszExtension, lpszExt);
2905 /*************************************************************************
2906 * PathRenameExtensionW [SHLWAPI.@]
2908 * See PathRenameExtensionA.
2910 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
2912 LPWSTR lpszExtension;
2914 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
2916 lpszExtension = PathFindExtensionW(lpszPath);
2918 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
2921 strcpyW(lpszExtension, lpszExt);
2925 /*************************************************************************
2926 * PathSearchAndQualifyA [SHLWAPI.@]
2933 * cchBuf [I] Size of lpszBuf
2938 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
2940 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
2944 /*************************************************************************
2945 * PathSearchAndQualifyW [SHLWAPI.@]
2947 * See PathSearchAndQualifyA
2949 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
2951 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
2955 /*************************************************************************
2956 * PathSkipRootA [SHLWAPI.@]
2958 * Return the portion of a path following the drive letter or mount point.
2961 * lpszPath [I] The path to skip on
2964 * Success: A pointer to the next character after the root.
2965 * Failure: NULL, if lpszPath is invalid, has no root or is a MB string.
2967 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
2969 TRACE("(%s)\n", debugstr_a(lpszPath));
2971 if (!lpszPath || !*lpszPath)
2974 if (*lpszPath == '\\' && lpszPath[1] == '\\')
2976 /* Network share: skip share server and mount point */
2978 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
2979 (lpszPath = StrChrA(lpszPath + 1, '\\')))
2981 return (LPSTR)lpszPath;
2984 if (IsDBCSLeadByte(*lpszPath))
2988 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
2989 return (LPSTR)lpszPath + 3;
2993 /*************************************************************************
2994 * PathSkipRootW [SHLWAPI.@]
2996 * See PathSkipRootA.
2998 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3000 TRACE("(%s)\n", debugstr_w(lpszPath));
3002 if (!lpszPath || !*lpszPath)
3005 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3007 /* Network share: skip share server and mount point */
3009 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3010 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3012 return (LPWSTR)lpszPath;
3016 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3017 return (LPWSTR)lpszPath + 3;
3021 /*************************************************************************
3022 * PathCreateFromUrlA [SHLWAPI.@]
3024 * Create a path from a URL
3027 * lpszUrl [I] URL to convert into a path
3028 * lpszPath [O] Output buffer for the resulting Path
3029 * pcchPath [I] Length of lpszPath
3030 * dwFlags [I] Flags controlling the conversion
3033 * Success: S_OK. lpszPath contains the URL in path format
3034 * Failure: An HRESULT error code such as E_INVALIDARG.
3036 HRESULT WINAPI PathCreateFromUrlA(LPCSTR lpszUrl, LPSTR lpszPath,
3037 LPDWORD pcchPath, DWORD dwFlags)
3039 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_a(lpszUrl), lpszPath, pcchPath, dwFlags);
3041 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3042 return E_INVALIDARG;
3044 /* extracts thing prior to : in pszURL and checks against:
3048 * about - if match returns E_INVALIDARG
3054 /*************************************************************************
3055 * PathCreateFromUrlW [SHLWAPI.@]
3057 * See PathCreateFromUrlA.
3059 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR lpszUrl, LPWSTR lpszPath,
3060 LPDWORD pcchPath, DWORD dwFlags)
3062 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_w(lpszUrl), lpszPath, pcchPath, dwFlags);
3064 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3065 return E_INVALIDARG;
3070 /*************************************************************************
3071 * PathRelativePathToA [SHLWAPI.@]
3073 * Create a relative path from one path to another.
3076 * lpszPath [O] Destination for relative path
3077 * lpszFrom [I] Source path
3078 * dwAttrFrom [I] File attribute of source path
3079 * lpszTo [I] Destination path
3080 * dwAttrTo [I] File attributes of destination path
3083 * TRUE If a relative path can be formed. lpszPath contains the new path
3084 * FALSE If the paths are not relavtive or any parameters are invalid
3087 * lpszTo should be at least MAX_PATH in length.
3088 * Calling this function with relative paths for lpszFrom or lpszTo may
3089 * give erroneous results.
3091 * The Win32 version of this function contains a bug where the lpszTo string
3092 * may be referenced 1 byte beyond the end of the string. As a result random
3093 * garbage may be written to the output path, depending on what lies beyond
3094 * the last byte of the string. This bug occurs because of the behaviour of
3095 * PathCommonPrefix (see notes for that function), and no workaround seems
3096 * possible with Win32.
3097 * This bug has been fixed here, so for example the relative path from "\\"
3098 * to "\\" is correctly determined as "." in this implementation.
3100 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3101 LPCSTR lpszTo, DWORD dwAttrTo)
3105 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_a(lpszFrom),
3106 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3108 if(lpszPath && lpszFrom && lpszTo)
3110 WCHAR szPath[MAX_PATH];
3111 WCHAR szFrom[MAX_PATH];
3112 WCHAR szTo[MAX_PATH];
3113 MultiByteToWideChar(0,0,lpszFrom,-1,szFrom,MAX_PATH);
3114 MultiByteToWideChar(0,0,lpszTo,-1,szTo,MAX_PATH);
3115 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3116 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3121 /*************************************************************************
3122 * PathRelativePathToW [SHLWAPI.@]
3124 * See PathRelativePathToA.
3126 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3127 LPCWSTR lpszTo, DWORD dwAttrTo)
3129 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3130 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3131 WCHAR szFrom[MAX_PATH];
3132 WCHAR szTo[MAX_PATH];
3135 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_w(lpszFrom),
3136 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3138 if(!lpszPath || !lpszFrom || !lpszTo)
3142 strncpyW(szFrom, lpszFrom, MAX_PATH);
3143 strncpyW(szTo, lpszTo, MAX_PATH);
3145 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3146 PathRemoveFileSpecW(szFrom);
3147 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3148 PathRemoveFileSpecW(szTo);
3150 /* Paths can only be relative if they have a common root */
3151 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3154 /* Strip off lpszFrom components to the root, by adding "..\" */
3155 lpszFrom = szFrom + dwLen;
3161 if (*lpszFrom == '\\')
3166 lpszFrom = PathFindNextComponentW(lpszFrom);
3167 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3170 /* From the root add the components of lpszTo */
3172 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3175 if (*lpszTo && lpszTo[-1])
3177 if (*lpszTo != '\\')
3179 dwLen = strlenW(lpszPath);
3180 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3185 strcpyW(lpszPath + dwLen, lpszTo);
3190 /*************************************************************************
3191 * PathUnmakeSystemFolderA [SHLWAPI.@]
3193 * Remove the system folder attributes from a path.
3196 * lpszPath [I] The path to remove attributes from
3200 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3201 * SetFileAttributesA fails.
3203 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3207 TRACE("(%s)\n", debugstr_a(lpszPath));
3209 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == -1 ||
3210 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3213 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3214 return SetFileAttributesA(lpszPath, dwAttr);
3217 /*************************************************************************
3218 * PathUnmakeSystemFolderW [SHLWAPI.@]
3220 * See PathUnmakeSystemFolderA.
3222 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3226 TRACE("(%s)\n", debugstr_w(lpszPath));
3228 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == -1 ||
3229 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3232 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3233 return SetFileAttributesW(lpszPath, dwAttr);
3237 /*************************************************************************
3238 * PathSetDlgItemPathA [SHLWAPI.@]
3240 * Set the text of a dialog item to a path, shrinking the path to fit
3241 * if it is too big for the item.
3244 * hDlg [I] Dialog handle
3245 * id [I] ID of item in the dialog
3246 * lpszPath [I] Path to set as the items text
3252 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3253 * window text is erased).
3255 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3257 WCHAR szPath[MAX_PATH];
3259 TRACE("(%8x,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3262 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3265 PathSetDlgItemPathW(hDlg, id, szPath);
3268 /*************************************************************************
3269 * PathSetDlgItemPathW [SHLWAPI.@]
3271 * See PathSetDlgItemPathA.
3273 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3275 WCHAR path[MAX_PATH + 1];
3281 TRACE("(%8x,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3283 if (!(hwItem = GetDlgItem(hDlg, id)))
3287 strncpyW(path, lpszPath, sizeof(path));
3291 GetClientRect(hwItem, &rect);
3293 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3297 PathCompactPathW(hdc, path, rect.right);
3298 SelectObject(hdc, hPrevObj);
3301 ReleaseDC(hDlg, hdc);
3302 SetWindowTextW(hwItem, path);
3305 /*************************************************************************
3306 * PathIsNetworkPathA [SHLWAPI.@]
3308 * Determine if the given path is a network path.
3311 * lpszPath [I] Path to check
3314 * TRUE If path is a UNC share or mapped network drive
3315 * FALSE If path is a local drive or cannot be determined
3317 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3321 TRACE("(%s)\n",debugstr_a(lpszPath));
3325 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3327 dwDriveNum = PathGetDriveNumberA(lpszPath);
3328 if (dwDriveNum == -1)
3330 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3331 return pIsNetDrive(dwDriveNum);
3334 /*************************************************************************
3335 * PathIsNetworkPathW [SHLWAPI.@]
3337 * See PathIsNetworkPathA.
3339 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3343 TRACE("(%s)\n", debugstr_w(lpszPath));
3347 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3349 dwDriveNum = PathGetDriveNumberW(lpszPath);
3350 if (dwDriveNum == -1)
3352 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3353 return pIsNetDrive(dwDriveNum);
3356 /*************************************************************************
3357 * PathIsLFNFileSpecA [SHLWAPI.@]
3359 * Determine if the given path is a long file name
3362 * lpszPath [I] Path to check
3365 * TRUE If path is a long file name
3366 * FALSE If path is a valid DOS 8.3 file name
3368 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3370 DWORD dwNameLen = 0, dwExtLen = 0;
3372 TRACE("(%s)\n",debugstr_a(lpszPath));
3379 if (*lpszPath == ' ')
3380 return TRUE; /* DOS names cannot have spaces */
3381 if (*lpszPath == '.')
3384 return TRUE; /* DOS names have only one dot */
3391 return TRUE; /* DOS extensions are <= 3 chars*/
3397 return TRUE; /* DOS names are <= 8 chars */
3399 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3401 return FALSE; /* Valid DOS path */
3404 /*************************************************************************
3405 * PathIsLFNFileSpecW [SHLWAPI.@]
3407 * See PathIsLFNFileSpecA.
3409 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3411 DWORD dwNameLen = 0, dwExtLen = 0;
3413 TRACE("(%s)\n",debugstr_w(lpszPath));
3420 if (*lpszPath == ' ')
3421 return TRUE; /* DOS names cannot have spaces */
3422 if (*lpszPath == '.')
3425 return TRUE; /* DOS names have only one dot */
3432 return TRUE; /* DOS extensions are <= 3 chars*/
3438 return TRUE; /* DOS names are <= 8 chars */
3442 return FALSE; /* Valid DOS path */
3445 /*************************************************************************
3446 * PathIsDirectoryEmptyA [SHLWAPI.@]
3448 * Determine if a given directory is empty.
3451 * lpszPath [I] Directory to check
3454 * TRUE If the directory exists and contains no files
3457 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3461 TRACE("(%s)\n",debugstr_a(lpszPath));
3465 WCHAR szPath[MAX_PATH];
3466 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3467 bRet = PathIsDirectoryEmptyW(szPath);
3472 /*************************************************************************
3473 * PathIsDirectoryEmptyW [SHLWAPI.@]
3475 * See PathIsDirectoryEmptyA.
3477 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3479 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3480 WCHAR szSearch[MAX_PATH];
3483 BOOL retVal = FALSE;
3484 WIN32_FIND_DATAW find_data;
3486 TRACE("(%s)\n",debugstr_w(lpszPath));
3488 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3491 strncpyW(szSearch, lpszPath, MAX_PATH);
3492 PathAddBackslashW(szSearch);
3493 dwLen = strlenW(szSearch);
3494 if (dwLen > MAX_PATH - 4)
3497 strcpyW(szSearch + dwLen, szAllFiles);
3498 hfind = FindFirstFileW(szSearch, &find_data);
3500 if (hfind != INVALID_HANDLE_VALUE &&
3501 find_data.cFileName[0] == '.' &&
3502 find_data.cFileName[1] == '.')
3504 /* The only directory entry should be the parent */
3505 if (!FindNextFileW(hfind, &find_data))
3513 /*************************************************************************
3514 * PathFindSuffixArrayA [SHLWAPI.@]
3516 * Find a suffix string in an array of suffix strings
3519 * lpszSuffix [I] Suffix string to search for
3520 * lppszArray [I] Array of suffix strings to search
3521 * dwCount [I] Number of elements in lppszArray
3524 * Success The index of the position of lpszSuffix in lppszArray
3525 * Failure 0, if any parameters are invalid or lpszSuffix is not found
3528 * The search is case sensitive.
3529 * The match is made against the end of the suffix string, so for example:
3530 * lpszSuffix=fooBAR matches BAR, but lpszSuffix=fooBARfoo does not.
3532 int WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3537 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3539 if (lpszSuffix && lppszArray && dwCount > 0)
3541 dwLen = strlen(lpszSuffix);
3543 while (dwRet < dwCount)
3545 DWORD dwCompareLen = strlen(*lppszArray);
3546 if (dwCompareLen < dwLen)
3548 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3549 return dwRet; /* Found */
3558 /*************************************************************************
3559 * PathFindSuffixArrayW [SHLWAPI.@]
3561 * See PathFindSuffixArrayA.
3563 int WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3568 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3570 if (lpszSuffix && lppszArray && dwCount > 0)
3572 dwLen = strlenW(lpszSuffix);
3574 while (dwRet < dwCount)
3576 DWORD dwCompareLen = strlenW(*lppszArray);
3577 if (dwCompareLen < dwLen)
3579 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3580 return dwRet; /* Found */
3589 /*************************************************************************
3590 * PathUndecorateA [SHLWAPI.@]
3592 * Undecorate a file path
3595 * lpszPath [O] Path to undecorate
3601 * A decorations form is "path[n].ext" where n is an optional decimal number.
3603 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3605 TRACE("(%s)\n",debugstr_a(lpszPath));
3609 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3610 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3612 LPSTR lpszSkip = lpszExt - 2;
3613 if (*lpszSkip == '[')
3614 lpszSkip++; /* [] (no number) */
3616 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3618 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3620 /* remove the [n] */
3623 *lpszSkip++ = *lpszExt++;
3630 /*************************************************************************
3631 * PathUndecorateW [SHLWAPI.@]
3633 * See PathUndecorateA.
3635 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3637 TRACE("(%s)\n",debugstr_w(lpszPath));
3641 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3642 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3644 LPWSTR lpszSkip = lpszExt - 2;
3645 if (*lpszSkip == '[')
3646 lpszSkip++; /* [] (no number) */
3648 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3650 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3652 /* remove the [n] */
3655 *lpszSkip++ = *lpszExt++;