Added winebrowser app that launches a Unix browser.
[wine/testsucceed.git] / dlls / shlwapi / path.c
blob388d3b9b0da96f9bc125cf61717e9b48f88289ec
1 /*
2 * Path Functions
4 * Copyright 1999, 2000 Juergen Schmied
5 * Copyright 2001, 2002 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include "wine/unicode.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #define NO_SHLWAPI_STREAM
36 #include "shlwapi.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41 /* Get a function pointer from a DLL handle */
42 #define GET_FUNC(func, module, name, fail) \
43 do { \
44 if (!func) { \
45 if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
46 func = (fn##func)GetProcAddress(SHLWAPI_h##module, name); \
47 if (!func) return fail; \
48 } \
49 } while (0)
51 /* DLL handles for late bound calls */
52 extern HMODULE SHLWAPI_hshell32;
54 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
55 typedef BOOL (WINAPI *fnpIsNetDrive)(int);
56 static fnpIsNetDrive pIsNetDrive;
58 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR,LPWSTR,DWORD);
60 /*************************************************************************
61 * PathAppendA [SHLWAPI.@]
63 * Append one path to another.
65 * PARAMS
66 * lpszPath [I/O] Initial part of path, and destination for output
67 * lpszAppend [I] Path to append
69 * RETURNS
70 * Success: TRUE. lpszPath contains the newly created path.
71 * Failure: FALSE, if either path is NULL, or PathCombineA() fails.
73 * NOTES
74 * lpszAppend must contain at least one backslash ('\') if not NULL.
75 * Because PathCombineA() is used to join the paths, the resulting
76 * path is also canonicalized.
78 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
80 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
82 if (lpszPath && lpszAppend)
84 if (!PathIsUNCA(lpszAppend))
85 while (*lpszAppend == '\\')
86 lpszAppend++;
87 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
88 return TRUE;
90 return FALSE;
93 /*************************************************************************
94 * PathAppendW [SHLWAPI.@]
96 * See PathAppendA.
98 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
100 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
102 if (lpszPath && lpszAppend)
104 if (!PathIsUNCW(lpszAppend))
105 while (*lpszAppend == '\\')
106 lpszAppend++;
107 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
108 return TRUE;
110 return FALSE;
113 /*************************************************************************
114 * PathCombineA [SHLWAPI.@]
116 * Combine two paths together.
118 * PARAMS
119 * lpszDest [O] Destination for combined path
120 * lpszDir [I] Directory path
121 * lpszFile [I] File path
123 * RETURNS
124 * Success: The output path
125 * Failure: NULL, if inputs are invalid.
127 * NOTES
128 * lpszDest should be at least MAX_PATH in size, and may point to the same
129 * memory location as lpszDir. The combined path is canonicalised.
131 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
133 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
135 if (!lpszDest || (!lpszDir && !lpszFile))
136 return NULL; /* Invalid parameters */
137 else
139 WCHAR szDest[MAX_PATH];
140 WCHAR szDir[MAX_PATH];
141 WCHAR szFile[MAX_PATH];
142 if (lpszDir)
143 MultiByteToWideChar(0,0,lpszDir,-1,szDir,MAX_PATH);
144 if (lpszFile)
145 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
146 PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL);
147 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
149 return lpszDest;
152 /*************************************************************************
153 * PathCombineW [SHLWAPI.@]
155 * See PathCombineA.
157 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
159 WCHAR szTemp[MAX_PATH];
160 BOOL bUseBoth = FALSE, bStrip = FALSE;
162 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
164 if (!lpszDest || (!lpszDir && !lpszFile))
165 return lpszDest; /* Invalid parameters */
167 if (!lpszFile || !*lpszFile)
169 /* Use dir only */
170 strncpyW(szTemp, lpszDir, MAX_PATH);
172 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
174 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
176 /* Use file only */
177 strncpyW(szTemp, lpszFile, MAX_PATH);
179 else
181 bUseBoth = TRUE;
182 bStrip = TRUE;
185 else
186 bUseBoth = TRUE;
188 if (bUseBoth)
190 strncpyW(szTemp, lpszDir, MAX_PATH);
191 if (bStrip)
193 PathStripToRootW(szTemp);
194 lpszFile++; /* Skip '\' */
196 if (!PathAddBackslashW(szTemp))
197 return NULL;
198 if (strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
199 return NULL;
200 strcatW(szTemp, lpszFile);
203 PathCanonicalizeW(lpszDest, szTemp);
204 return lpszDest;
207 /*************************************************************************
208 * PathAddBackslashA [SHLWAPI.@]
210 * Append a backslash ('\') to a path if one doesn't exist.
212 * PARAMS
213 * lpszPath [I/O] The path to append a backslash to.
215 * RETURNS
216 * Success: The position of the last backslash in the path.
217 * Failure: NULL, if lpszPath is NULL or the path is too large.
219 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
221 size_t iLen;
223 TRACE("(%s)\n",debugstr_a(lpszPath));
225 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
226 return NULL;
228 if (iLen)
230 lpszPath += iLen;
231 if (lpszPath[-1] != '\\')
233 *lpszPath++ = '\\';
234 *lpszPath = '\0';
237 return lpszPath;
240 /*************************************************************************
241 * PathAddBackslashW [SHLWAPI.@]
243 * See PathAddBackslashA.
245 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
247 size_t iLen;
249 TRACE("(%s)\n",debugstr_w(lpszPath));
251 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
252 return NULL;
254 if (iLen)
256 lpszPath += iLen;
257 if (lpszPath[-1] != '\\')
259 *lpszPath++ = '\\';
260 *lpszPath = '\0';
263 return lpszPath;
266 /*************************************************************************
267 * PathBuildRootA [SHLWAPI.@]
269 * Create a root drive string (e.g. "A:\") from a drive number.
271 * PARAMS
272 * lpszPath [O] Destination for the drive string
274 * RETURNS
275 * lpszPath
277 * NOTES
278 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
280 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
282 TRACE("(%p,%d)\n", lpszPath, drive);
284 if (lpszPath && drive >= 0 && drive < 26)
286 lpszPath[0] = 'A' + drive;
287 lpszPath[1] = ':';
288 lpszPath[2] = '\\';
289 lpszPath[3] = '\0';
291 return lpszPath;
294 /*************************************************************************
295 * PathBuildRootW [SHLWAPI.@]
297 * See PathBuildRootA.
299 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
301 TRACE("(%p,%d)\n", lpszPath, drive);
303 if (lpszPath && drive >= 0 && drive < 26)
305 lpszPath[0] = 'A' + drive;
306 lpszPath[1] = ':';
307 lpszPath[2] = '\\';
308 lpszPath[3] = '\0';
310 return lpszPath;
313 /*************************************************************************
314 * PathFindFileNameA [SHLWAPI.@]
316 * Locate the start of the file name in a path
318 * PARAMS
319 * lpszPath [I] Path to search
321 * RETURNS
322 * A pointer to the first character of the file name
324 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
326 LPCSTR lastSlash = lpszPath;
328 TRACE("(%s)\n",debugstr_a(lpszPath));
330 while (lpszPath && *lpszPath)
332 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
333 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
334 lastSlash = lpszPath + 1;
335 lpszPath = CharNextA(lpszPath);
337 return (LPSTR)lastSlash;
340 /*************************************************************************
341 * PathFindFileNameW [SHLWAPI.@]
343 * See PathFindFileNameA.
345 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
347 LPCWSTR lastSlash = lpszPath;
349 TRACE("(%s)\n",debugstr_w(lpszPath));
351 while (lpszPath && *lpszPath)
353 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
354 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
355 lastSlash = lpszPath + 1;
356 lpszPath = CharNextW(lpszPath);
358 return (LPWSTR)lastSlash;
361 /*************************************************************************
362 * PathFindExtensionA [SHLWAPI.@]
364 * Locate the start of the file extension in a path
366 * PARAMS
367 * lpszPath [I] The path to search
369 * RETURNS
370 * A pointer to the first character of the extension, the end of
371 * the string if the path has no extension, or NULL If lpszPath is NULL
373 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
375 LPCSTR lastpoint = NULL;
377 TRACE("(%s)\n", debugstr_a(lpszPath));
379 if (lpszPath)
381 while (*lpszPath)
383 if (*lpszPath == '\\' || *lpszPath==' ')
384 lastpoint = NULL;
385 else if (*lpszPath == '.')
386 lastpoint = lpszPath;
387 lpszPath = CharNextA(lpszPath);
390 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
393 /*************************************************************************
394 * PathFindExtensionW [SHLWAPI.@]
396 * See PathFindExtensionA.
398 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
400 LPCWSTR lastpoint = NULL;
402 TRACE("(%s)\n", debugstr_w(lpszPath));
404 if (lpszPath)
406 while (*lpszPath)
408 if (*lpszPath == '\\' || *lpszPath==' ')
409 lastpoint = NULL;
410 else if (*lpszPath == '.')
411 lastpoint = lpszPath;
412 lpszPath = CharNextW(lpszPath);
415 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
418 /*************************************************************************
419 * PathGetArgsA [SHLWAPI.@]
421 * Find the next argument in a string delimited by spaces.
423 * PARAMS
424 * lpszPath [I] The string to search for arguments in
426 * RETURNS
427 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
429 * NOTES
430 * Spaces in quoted strings are ignored as delimiters.
432 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
434 BOOL bSeenQuote = FALSE;
436 TRACE("(%s)\n",debugstr_a(lpszPath));
438 if (lpszPath)
440 while (*lpszPath)
442 if ((*lpszPath==' ') && !bSeenQuote)
443 return (LPSTR)lpszPath + 1;
444 if (*lpszPath == '"')
445 bSeenQuote = !bSeenQuote;
446 lpszPath = CharNextA(lpszPath);
449 return (LPSTR)lpszPath;
452 /*************************************************************************
453 * PathGetArgsW [SHLWAPI.@]
455 * See PathGetArgsA.
457 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
459 BOOL bSeenQuote = FALSE;
461 TRACE("(%s)\n",debugstr_w(lpszPath));
463 if (lpszPath)
465 while (*lpszPath)
467 if ((*lpszPath==' ') && !bSeenQuote)
468 return (LPWSTR)lpszPath + 1;
469 if (*lpszPath == '"')
470 bSeenQuote = !bSeenQuote;
471 lpszPath = CharNextW(lpszPath);
474 return (LPWSTR)lpszPath;
477 /*************************************************************************
478 * PathGetDriveNumberA [SHLWAPI.@]
480 * Return the drive number from a path
482 * PARAMS
483 * lpszPath [I] Path to get the drive number from
485 * RETURNS
486 * Success: The drive number corresponding to the drive in the path
487 * Failure: -1, if lpszPath contains no valid drive
489 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
491 TRACE ("(%s)\n",debugstr_a(lpszPath));
493 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
494 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
495 return tolower(*lpszPath) - 'a';
496 return -1;
499 /*************************************************************************
500 * PathGetDriveNumberW [SHLWAPI.@]
502 * See PathGetDriveNumberA.
504 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
506 TRACE ("(%s)\n",debugstr_w(lpszPath));
508 if (lpszPath && lpszPath[1] == ':' &&
509 tolowerW(*lpszPath) >= 'a' && tolowerW(*lpszPath) <= 'z')
510 return tolowerW(*lpszPath) - 'a';
511 return -1;
514 /*************************************************************************
515 * PathRemoveFileSpecA [SHLWAPI.@]
517 * Remove the file specification from a path.
519 * PARAMS
520 * lpszPath [I/O] Path to remove the file spec from
522 * RETURNS
523 * TRUE If the path was valid and modified
524 * FALSE Otherwise
526 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
528 LPSTR lpszFileSpec = lpszPath;
529 BOOL bModified = FALSE;
531 TRACE("(%s)\n",debugstr_a(lpszPath));
533 if(lpszPath)
535 /* Skip directory or UNC path */
536 if (*lpszPath == '\\')
537 lpszFileSpec = ++lpszPath;
538 if (*lpszPath == '\\')
539 lpszFileSpec = ++lpszPath;
541 while (*lpszPath)
543 if(*lpszPath == '\\')
544 lpszFileSpec = lpszPath; /* Skip dir */
545 else if(*lpszPath == ':')
547 lpszFileSpec = ++lpszPath; /* Skip drive */
548 if (*lpszPath == '\\')
549 lpszFileSpec++;
551 if (!(lpszPath = CharNextA(lpszPath)))
552 break;
555 if (*lpszFileSpec)
557 *lpszFileSpec = '\0';
558 bModified = TRUE;
561 return bModified;
564 /*************************************************************************
565 * PathRemoveFileSpecW [SHLWAPI.@]
567 * See PathRemoveFileSpecA.
569 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
571 LPWSTR lpszFileSpec = lpszPath;
572 BOOL bModified = FALSE;
574 TRACE("(%s)\n",debugstr_w(lpszPath));
576 if(lpszPath)
578 /* Skip directory or UNC path */
579 if (*lpszPath == '\\')
580 lpszFileSpec = ++lpszPath;
581 if (*lpszPath == '\\')
582 lpszFileSpec = ++lpszPath;
584 while (*lpszPath)
586 if(*lpszPath == '\\')
587 lpszFileSpec = lpszPath; /* Skip dir */
588 else if(*lpszPath == ':')
590 lpszFileSpec = ++lpszPath; /* Skip drive */
591 if (*lpszPath == '\\')
592 lpszFileSpec++;
594 if (!(lpszPath = CharNextW(lpszPath)))
595 break;
598 if (*lpszFileSpec)
600 *lpszFileSpec = '\0';
601 bModified = TRUE;
604 return bModified;
607 /*************************************************************************
608 * PathStripPathA [SHLWAPI.@]
610 * Remove the initial path from the beginning of a filename
612 * PARAMS
613 * lpszPath [I/O] Path to remove the initial path from
615 * RETURNS
616 * Nothing.
618 void WINAPI PathStripPathA(LPSTR lpszPath)
620 TRACE("(%s)\n", debugstr_a(lpszPath));
622 if (lpszPath)
624 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
625 if(lpszFileName)
626 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
630 /*************************************************************************
631 * PathStripPathW [SHLWAPI.@]
633 * See PathStripPathA.
635 void WINAPI PathStripPathW(LPWSTR lpszPath)
637 LPWSTR lpszFileName;
639 TRACE("(%s)\n", debugstr_w(lpszPath));
640 lpszFileName = PathFindFileNameW(lpszPath);
641 if(lpszFileName)
642 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
645 /*************************************************************************
646 * PathStripToRootA [SHLWAPI.@]
648 * Reduce a path to its root.
650 * PARAMS
651 * lpszPath [I/O] the path to reduce
653 * RETURNS
654 * Success: TRUE if the stripped path is a root path
655 * Failure: FALSE if the path cannot be stripped or is NULL
657 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
659 TRACE("(%s)\n", debugstr_a(lpszPath));
661 if (!lpszPath)
662 return FALSE;
663 while(!PathIsRootA(lpszPath))
664 if (!PathRemoveFileSpecA(lpszPath))
665 return FALSE;
666 return TRUE;
669 /*************************************************************************
670 * PathStripToRootW [SHLWAPI.@]
672 * See PathStripToRootA.
674 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
676 TRACE("(%s)\n", debugstr_w(lpszPath));
678 if (!lpszPath)
679 return FALSE;
680 while(!PathIsRootW(lpszPath))
681 if (!PathRemoveFileSpecW(lpszPath))
682 return FALSE;
683 return TRUE;
686 /*************************************************************************
687 * PathRemoveArgsA [SHLWAPI.@]
689 * Strip space separated arguments from a path.
691 * PARAMS
692 * lpszPath [I/O] Path to remove arguments from
694 * RETURNS
695 * Nothing.
697 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
699 TRACE("(%s)\n",debugstr_a(lpszPath));
701 if(lpszPath)
703 LPSTR lpszArgs = PathGetArgsA(lpszPath);
704 if (*lpszArgs)
705 lpszArgs[-1] = '\0';
706 else
708 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
709 if(*lpszLastChar == ' ')
710 *lpszLastChar = '\0';
715 /*************************************************************************
716 * PathRemoveArgsW [SHLWAPI.@]
718 * See PathRemoveArgsA.
720 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
722 TRACE("(%s)\n",debugstr_w(lpszPath));
724 if(lpszPath)
726 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
727 if (*lpszArgs)
728 lpszArgs[-1] = '\0';
729 else
731 LPWSTR lpszLastChar = CharPrevW(lpszPath, lpszArgs);
732 if(*lpszLastChar == ' ')
733 *lpszLastChar = '\0';
738 /*************************************************************************
739 * PathRemoveExtensionA [SHLWAPI.@]
741 * Remove the file extension from a path
743 * PARAMS
744 * lpszPath [I/O] Path to remove the extension from
746 * RETURNS
747 * Nothing.
749 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
751 TRACE("(%s)\n", debugstr_a(lpszPath));
753 if (lpszPath)
755 lpszPath = PathFindExtensionA(lpszPath);
756 *lpszPath = '\0';
760 /*************************************************************************
761 * PathRemoveExtensionW [SHLWAPI.@]
763 * See PathRemoveExtensionA.
765 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
767 TRACE("(%s)\n", debugstr_w(lpszPath));
769 if (lpszPath)
771 lpszPath = PathFindExtensionW(lpszPath);
772 *lpszPath = '\0';
776 /*************************************************************************
777 * PathRemoveBackslashA [SHLWAPI.@]
779 * Remove a trailing backslash from a path.
781 * PARAMS
782 * lpszPath [I/O] Path to remove backslash from
784 * RETURNS
785 * Success: A pointer to the end of the path
786 * Failure: NULL, if lpszPath is NULL
788 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
790 LPSTR szTemp = NULL;
792 TRACE("(%s)\n", debugstr_a(lpszPath));
794 if(lpszPath)
796 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
797 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
798 *szTemp = '\0';
800 return szTemp;
803 /*************************************************************************
804 * PathRemoveBackslashW [SHLWAPI.@]
806 * See PathRemoveBackslashA.
808 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
810 LPWSTR szTemp = NULL;
812 TRACE("(%s)\n", debugstr_w(lpszPath));
814 if(lpszPath)
816 szTemp = CharPrevW(lpszPath, lpszPath + strlenW(lpszPath));
817 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
818 *szTemp = '\0';
820 return szTemp;
823 /*************************************************************************
824 * PathRemoveBlanksA [SHLWAPI.@]
826 * Remove Spaces from the start and end of a path.
828 * PARAMS
829 * lpszPath [I/O] Path to strip blanks from
831 * RETURNS
832 * Nothing.
834 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
836 TRACE("(%s)\n", debugstr_a(lpszPath));
838 if(lpszPath && *lpszPath)
840 LPSTR start = lpszPath;
842 while (*lpszPath == ' ')
843 lpszPath = CharNextA(lpszPath);
845 while(*lpszPath)
846 *start++ = *lpszPath++;
848 if (start != lpszPath)
849 while (start[-1] == ' ')
850 start--;
851 *start = '\0';
855 /*************************************************************************
856 * PathRemoveBlanksW [SHLWAPI.@]
858 * See PathRemoveBlanksA.
860 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
862 TRACE("(%s)\n", debugstr_w(lpszPath));
864 if(lpszPath && *lpszPath)
866 LPWSTR start = lpszPath;
868 while (*lpszPath == ' ')
869 lpszPath++;
871 while(*lpszPath)
872 *start++ = *lpszPath++;
874 if (start != lpszPath)
875 while (start[-1] == ' ')
876 start--;
877 *start = '\0';
881 /*************************************************************************
882 * PathQuoteSpacesA [SHLWAPI.@]
884 * Surround a path containg spaces in quotes.
886 * PARAMS
887 * lpszPath [I/O] Path to quote
889 * RETURNS
890 * Nothing.
892 * NOTES
893 * The path is not changed if it is invalid or has no spaces.
895 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
897 TRACE("(%s)\n", debugstr_a(lpszPath));
899 if(lpszPath && StrChrA(lpszPath,' '))
901 size_t iLen = strlen(lpszPath) + 1;
903 if (iLen + 2 < MAX_PATH)
905 memmove(lpszPath + 1, lpszPath, iLen);
906 lpszPath[0] = '"';
907 lpszPath[iLen] = '"';
908 lpszPath[iLen + 1] = '\0';
913 /*************************************************************************
914 * PathQuoteSpacesW [SHLWAPI.@]
916 * See PathQuoteSpacesA.
918 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
920 TRACE("(%s)\n", debugstr_w(lpszPath));
922 if(lpszPath && StrChrW(lpszPath,' '))
924 int iLen = strlenW(lpszPath) + 1;
926 if (iLen + 2 < MAX_PATH)
928 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
929 lpszPath[0] = '"';
930 lpszPath[iLen] = '"';
931 lpszPath[iLen + 1] = '\0';
936 /*************************************************************************
937 * PathUnquoteSpacesA [SHLWAPI.@]
939 * Remove quotes ("") from around a path, if present.
941 * PARAMS
942 * lpszPath [I/O] Path to strip quotes from
944 * RETURNS
945 * Nothing
947 * NOTES
948 * If the path contains a single quote only, an empty string will result.
949 * Otherwise quotes are only removed if they appear at the start and end
950 * of the path.
952 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
954 TRACE("(%s)\n", debugstr_a(lpszPath));
956 if (lpszPath && *lpszPath == '"')
958 DWORD dwLen = strlen(lpszPath) - 1;
960 if (lpszPath[dwLen] == '"')
962 lpszPath[dwLen] = '\0';
963 for (; *lpszPath; lpszPath++)
964 *lpszPath = lpszPath[1];
969 /*************************************************************************
970 * PathUnquoteSpacesW [SHLWAPI.@]
972 * See PathUnquoteSpacesA.
974 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
976 TRACE("(%s)\n", debugstr_w(lpszPath));
978 if (lpszPath && *lpszPath == '"')
980 DWORD dwLen = strlenW(lpszPath) - 1;
982 if (lpszPath[dwLen] == '"')
984 lpszPath[dwLen] = '\0';
985 for (; *lpszPath; lpszPath++)
986 *lpszPath = lpszPath[1];
991 /*************************************************************************
992 * PathParseIconLocationA [SHLWAPI.@]
994 * Parse the location of an icon from a path.
996 * PARAMS
997 * lpszPath [I/O] The path to parse the icon location from.
999 * RETURNS
1000 * Success: The number of the icon
1001 * Failure: 0 if the path does not contain an icon location or is NULL
1003 * NOTES
1004 * The path has surrounding quotes and spaces removed regardless
1005 * of whether the call succeeds or not.
1007 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1009 int iRet = 0;
1010 LPSTR lpszComma;
1012 TRACE("(%s)\n", debugstr_a(lpszPath));
1014 if (lpszPath)
1016 if ((lpszComma = strchr(lpszPath, ',')))
1018 *lpszComma++ = '\0';
1019 iRet = StrToIntA(lpszComma);
1021 PathUnquoteSpacesA(lpszPath);
1022 PathRemoveBlanksA(lpszPath);
1024 return iRet;
1027 /*************************************************************************
1028 * PathParseIconLocationW [SHLWAPI.@]
1030 * See PathParseIconLocationA.
1032 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1034 int iRet = 0;
1035 LPWSTR lpszComma;
1037 TRACE("(%s)\n", debugstr_w(lpszPath));
1039 if (lpszPath)
1041 if ((lpszComma = StrChrW(lpszPath, ',')))
1043 *lpszComma++ = '\0';
1044 iRet = StrToIntW(lpszComma);
1046 PathUnquoteSpacesW(lpszPath);
1047 PathRemoveBlanksW(lpszPath);
1049 return iRet;
1052 /*************************************************************************
1053 * @ [SHLWAPI.4]
1055 * Unicode version of PathFileExistsDefExtA.
1057 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1059 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
1060 { '.', 'c', 'o', 'm', 0},
1061 { '.', 'e', 'x', 'e', 0},
1062 { '.', 'b', 'a', 't', 0},
1063 { '.', 'l', 'n', 'k', 0},
1064 { '.', 'c', 'm', 'd', 0},
1065 { 0, 0, 0, 0, 0} };
1067 TRACE("(%s,%ld)\n", debugstr_w(lpszPath), dwWhich);
1069 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1070 return FALSE;
1072 if (dwWhich)
1074 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1075 if (!*szExt || dwWhich & 0x40)
1077 size_t iChoose = 0;
1078 int iLen = lstrlenW(lpszPath);
1079 if (iLen > (MAX_PATH - 5))
1080 return FALSE;
1081 while (dwWhich & 0x1 && iChoose < sizeof(pszExts))
1083 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1084 if (PathFileExistsW(lpszPath))
1085 return TRUE;
1086 iChoose++;
1087 dwWhich >>= 1;
1089 *(lpszPath + iLen) = (WCHAR)'\0';
1090 return FALSE;
1093 return PathFileExistsW(lpszPath);
1096 /*************************************************************************
1097 * @ [SHLWAPI.3]
1099 * Determine if a file exists locally and is of an executable type.
1101 * PARAMS
1102 * lpszPath [I/O] File to search for
1103 * dwWhich [I] Type of executable to search for
1105 * RETURNS
1106 * TRUE If the file was found. lpszPath contains the file name.
1107 * FALSE Otherwise.
1109 * NOTES
1110 * lpszPath is modified in place and must be at least MAX_PATH in length.
1111 * If the function returns FALSE, the path is modified to its orginal state.
1112 * If the given path contains an extension or dwWhich is 0, executable
1113 * extensions are not checked.
1115 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1116 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1117 * their own developers exclusive use. Monopoly, anyone?
1119 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1121 BOOL bRet = FALSE;
1123 TRACE("(%s,%ld)\n", debugstr_a(lpszPath), dwWhich);
1125 if (lpszPath)
1127 WCHAR szPath[MAX_PATH];
1128 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1129 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1130 if (bRet)
1131 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1133 return bRet;
1136 /*************************************************************************
1137 * SHLWAPI_PathFindInOtherDirs
1139 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1141 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1143 static WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1144 static WCHAR szPath[] = { 'P','A','T','H','\0'};
1145 DWORD dwLenPATH;
1146 LPCWSTR lpszCurr;
1147 WCHAR *lpszPATH;
1148 WCHAR buff[MAX_PATH];
1150 TRACE("(%s,%08lx)\n", debugstr_w(lpszFile), dwWhich);
1152 /* Try system directories */
1153 GetSystemDirectoryW(buff, MAX_PATH);
1154 if (!PathAppendW(buff, lpszFile))
1155 return FALSE;
1156 if (PathFileExistsDefExtW(buff, dwWhich))
1158 strcpyW(lpszFile, buff);
1159 return TRUE;
1161 GetWindowsDirectoryW(buff, MAX_PATH);
1162 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1163 return FALSE;
1164 if (PathFileExistsDefExtW(buff, dwWhich))
1166 strcpyW(lpszFile, buff);
1167 return TRUE;
1169 GetWindowsDirectoryW(buff, MAX_PATH);
1170 if (!PathAppendW(buff, lpszFile))
1171 return FALSE;
1172 if (PathFileExistsDefExtW(buff, dwWhich))
1174 strcpyW(lpszFile, buff);
1175 return TRUE;
1177 /* Try dirs listed in %PATH% */
1178 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1180 if (!dwLenPATH || !(lpszPATH = malloc((dwLenPATH + 1) * sizeof (WCHAR))))
1181 return FALSE;
1183 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1184 lpszCurr = lpszPATH;
1185 while (lpszCurr)
1187 LPCWSTR lpszEnd = lpszCurr;
1188 LPWSTR pBuff = buff;
1190 while (*lpszEnd == ' ')
1191 lpszEnd++;
1192 while (*lpszEnd && *lpszEnd != ';')
1193 *pBuff++ = *lpszEnd++;
1194 *pBuff = '\0';
1196 if (*lpszEnd)
1197 lpszCurr = lpszEnd + 1;
1198 else
1199 lpszCurr = NULL; /* Last Path, terminate after this */
1201 if (!PathAppendW(buff, lpszFile))
1203 free(lpszPATH);
1204 return FALSE;
1206 if (PathFileExistsDefExtW(buff, dwWhich))
1208 strcpyW(lpszFile, buff);
1209 free(lpszPATH);
1210 return TRUE;
1213 free(lpszPATH);
1214 return FALSE;
1217 /*************************************************************************
1218 * @ [SHLWAPI.5]
1220 * Search a range of paths for a specific type of executable.
1222 * PARAMS
1223 * lpszFile [I/O] File to search for
1224 * lppszOtherDirs [I] Other directories to look in
1225 * dwWhich [I] Type of executable to search for
1227 * RETURNS
1228 * Success: TRUE. The path to the executable is stored in lpszFile.
1229 * Failure: FALSE. The path to the executable is unchanged.
1231 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1233 WCHAR szFile[MAX_PATH];
1234 WCHAR buff[MAX_PATH];
1236 TRACE("(%s,%p,%08lx)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1238 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1239 return FALSE;
1241 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
1243 /* Search provided directories first */
1244 if (lppszOtherDirs && *lppszOtherDirs)
1246 WCHAR szOther[MAX_PATH];
1247 LPCSTR *lpszOtherPath = lppszOtherDirs;
1249 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1251 MultiByteToWideChar(0,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1252 PathCombineW(buff, szOther, szFile);
1253 if (PathFileExistsDefExtW(buff, dwWhich))
1255 WideCharToMultiByte(0,0,buff,-1,lpszFile,MAX_PATH,0,0);
1256 return TRUE;
1258 lpszOtherPath++;
1261 /* Not found, try system and path dirs */
1262 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1264 WideCharToMultiByte(0,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1265 return TRUE;
1267 return FALSE;
1270 /*************************************************************************
1271 * @ [SHLWAPI.6]
1273 * Unicode version of PathFindOnPathExA.
1275 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1277 WCHAR buff[MAX_PATH];
1279 TRACE("(%s,%p,%08lx)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1281 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1282 return FALSE;
1284 /* Search provided directories first */
1285 if (lppszOtherDirs && *lppszOtherDirs)
1287 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1288 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1290 PathCombineW(buff, *lpszOtherPath, lpszFile);
1291 if (PathFileExistsDefExtW(buff, dwWhich))
1293 strcpyW(lpszFile, buff);
1294 return TRUE;
1296 lpszOtherPath++;
1299 /* Not found, try system and path dirs */
1300 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1303 /*************************************************************************
1304 * PathFindOnPathA [SHLWAPI.@]
1306 * Search a range of paths for an executable.
1308 * PARAMS
1309 * lpszFile [I/O] File to search for
1310 * lppszOtherDirs [I] Other directories to look in
1312 * RETURNS
1313 * Success: TRUE. The path to the executable is stored in lpszFile.
1314 * Failure: FALSE. The path to the executable is unchanged.
1316 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1318 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1319 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1322 /*************************************************************************
1323 * PathFindOnPathW [SHLWAPI.@]
1325 * See PathFindOnPathA.
1327 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1329 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1330 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1333 /*************************************************************************
1334 * PathCompactPathExA [SHLWAPI.@]
1336 * Compact a path into a given number of characters.
1338 * PARAMS
1339 * lpszDest [O] Destination for compacted path
1340 * lpszPath [I] Source path
1341 * cchMax [I] Maximum size of compacted path
1342 * dwFlags [I] Reserved
1344 * RETURNS
1345 * Success: TRUE. The compacted path is written to lpszDest.
1346 * Failure: FALSE. lpszPath is undefined.
1348 * NOTES
1349 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1351 * The Win32 version of this function contains a bug: When cchMax == 7,
1352 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1353 * implementation.
1355 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1356 * because Win32 will insert a "\" in lpszDest, even if one is
1357 * not present in the original path.
1359 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1360 UINT cchMax, DWORD dwFlags)
1362 BOOL bRet = FALSE;
1364 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1366 if (lpszPath && lpszDest)
1368 WCHAR szPath[MAX_PATH];
1369 WCHAR szDest[MAX_PATH];
1371 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1372 szDest[0] = '\0';
1373 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1374 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1376 return bRet;
1379 /*************************************************************************
1380 * PathCompactPathExW [SHLWAPI.@]
1382 * See PathCompactPathExA.
1384 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1385 UINT cchMax, DWORD dwFlags)
1387 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1388 LPCWSTR lpszFile;
1389 DWORD dwLen, dwFileLen = 0;
1391 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1393 if (!lpszPath)
1394 return FALSE;
1396 if (!lpszDest)
1398 WARN("Invalid lpszDest would crash under Win32!\n");
1399 return FALSE;
1402 *lpszDest = '\0';
1404 if (cchMax < 2)
1405 return TRUE;
1407 dwLen = strlenW(lpszPath) + 1;
1409 if (dwLen < cchMax)
1411 /* Don't need to compact */
1412 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1413 return TRUE;
1416 /* Path must be compacted to fit into lpszDest */
1417 lpszFile = PathFindFileNameW(lpszPath);
1418 dwFileLen = lpszPath + dwLen - lpszFile;
1420 if (dwFileLen == dwLen)
1422 /* No root in psth */
1423 if (cchMax <= 4)
1425 while (--cchMax > 0) /* No room left for anything but ellipses */
1426 *lpszDest++ = '.';
1427 *lpszDest = '\0';
1428 return TRUE;
1430 /* Compact the file name with ellipses at the end */
1431 cchMax -= 4;
1432 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1433 strcpyW(lpszDest + cchMax, szEllipses);
1434 return TRUE;
1436 /* We have a root in the path */
1437 lpszFile--; /* Start compacted filename with the path separator */
1438 dwFileLen++;
1440 if (dwFileLen + 3 > cchMax)
1442 /* Compact the file name */
1443 if (cchMax <= 4)
1445 while (--cchMax > 0) /* No room left for anything but ellipses */
1446 *lpszDest++ = '.';
1447 *lpszDest = '\0';
1448 return TRUE;
1450 strcpyW(lpszDest, szEllipses);
1451 lpszDest += 3;
1452 cchMax -= 4;
1453 *lpszDest++ = *lpszFile++;
1454 if (cchMax <= 4)
1456 while (--cchMax > 0) /* No room left for anything but ellipses */
1457 *lpszDest++ = '.';
1458 *lpszDest = '\0';
1459 return TRUE;
1461 cchMax -= 4;
1462 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1463 strcpyW(lpszDest + cchMax, szEllipses);
1464 return TRUE;
1467 /* Only the root needs to be Compacted */
1468 dwLen = cchMax - dwFileLen - 3;
1469 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1470 strcpyW(lpszDest + dwLen, szEllipses);
1471 strcpyW(lpszDest + dwLen + 3, lpszFile);
1472 return TRUE;
1475 /*************************************************************************
1476 * PathIsRelativeA [SHLWAPI.@]
1478 * Determine if a path is a relative path.
1480 * PARAMS
1481 * lpszPath [I] Path to check
1483 * RETURNS
1484 * TRUE: The path is relative, or is invalid.
1485 * FALSE: The path is not relative.
1487 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1489 TRACE("(%s)\n",debugstr_a(lpszPath));
1491 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1492 return TRUE;
1493 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1494 return FALSE;
1495 return TRUE;
1498 /*************************************************************************
1499 * PathIsRelativeW [SHLWAPI.@]
1501 * See PathIsRelativeA.
1503 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1505 TRACE("(%s)\n",debugstr_w(lpszPath));
1507 if (!lpszPath || !*lpszPath)
1508 return TRUE;
1509 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1510 return FALSE;
1511 return TRUE;
1514 /*************************************************************************
1515 * PathIsRootA [SHLWAPI.@]
1517 * Determine if a path is a root path.
1519 * PARAMS
1520 * lpszPath [I] Path to check
1522 * RETURNS
1523 * TRUE If lpszPath is valid and a root path,
1524 * FALSE Otherwise
1526 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1528 TRACE("(%s)\n", debugstr_a(lpszPath));
1530 if (lpszPath && *lpszPath)
1532 if (*lpszPath == '\\')
1534 if (!lpszPath[1])
1535 return TRUE; /* \ */
1536 else if (lpszPath[1]=='\\')
1538 BOOL bSeenSlash = FALSE;
1539 lpszPath += 2;
1541 /* Check for UNC root path */
1542 while (*lpszPath)
1544 if (*lpszPath == '\\')
1546 if (bSeenSlash)
1547 return FALSE;
1548 bSeenSlash = TRUE;
1550 lpszPath = CharNextA(lpszPath);
1552 return TRUE;
1555 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1556 return TRUE; /* X:\ */
1558 return FALSE;
1561 /*************************************************************************
1562 * PathIsRootW [SHLWAPI.@]
1564 * See PathIsRootA.
1566 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1568 TRACE("(%s)\n", debugstr_w(lpszPath));
1570 if (lpszPath && *lpszPath)
1572 if (*lpszPath == '\\')
1574 if (!lpszPath[1])
1575 return TRUE; /* \ */
1576 else if (lpszPath[1]=='\\')
1578 BOOL bSeenSlash = FALSE;
1579 lpszPath += 2;
1581 /* Check for UNC root path */
1582 while (*lpszPath)
1584 if (*lpszPath == '\\')
1586 if (bSeenSlash)
1587 return FALSE;
1588 bSeenSlash = TRUE;
1590 lpszPath = CharNextW(lpszPath);
1592 return TRUE;
1595 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1596 return TRUE; /* X:\ */
1598 return FALSE;
1601 /*************************************************************************
1602 * PathIsDirectoryA [SHLWAPI.@]
1604 * Determine if a path is a valid directory
1606 * PARAMS
1607 * lpszPath [I] Path to check.
1609 * RETURNS
1610 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1611 * FALSE if lpszPath is invalid or not a directory.
1613 * NOTES
1614 * Although this function is prototyped as returning a BOOL, it returns
1615 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1617 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1618 *| ...
1620 * will always fail.
1622 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1624 DWORD dwAttr;
1626 TRACE("(%s)\n", debugstr_a(lpszPath));
1628 if (!lpszPath || PathIsUNCServerA(lpszPath))
1629 return FALSE;
1631 if (PathIsUNCServerShareA(lpszPath))
1633 FIXME("UNC Server Share not yet supported - FAILING\n");
1634 return FALSE;
1637 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1638 return FALSE;
1639 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1642 /*************************************************************************
1643 * PathIsDirectoryW [SHLWAPI.@]
1645 * See PathIsDirectoryA.
1647 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1649 DWORD dwAttr;
1651 TRACE("(%s)\n", debugstr_w(lpszPath));
1653 if (!lpszPath || PathIsUNCServerW(lpszPath))
1654 return FALSE;
1656 if (PathIsUNCServerShareW(lpszPath))
1658 FIXME("UNC Server Share not yet supported - FAILING\n");
1659 return FALSE;
1662 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1663 return FALSE;
1664 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1667 /*************************************************************************
1668 * PathFileExistsA [SHLWAPI.@]
1670 * Determine if a file exists.
1672 * PARAMS
1673 * lpszPath [I] Path to check
1675 * RETURNS
1676 * TRUE If the file exists and is readable
1677 * FALSE Otherwise
1679 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1681 UINT iPrevErrMode;
1682 DWORD dwAttr;
1684 TRACE("(%s)\n",debugstr_a(lpszPath));
1686 if (!lpszPath)
1687 return FALSE;
1689 iPrevErrMode = SetErrorMode(1);
1690 dwAttr = GetFileAttributesA(lpszPath);
1691 SetErrorMode(iPrevErrMode);
1692 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1695 /*************************************************************************
1696 * PathFileExistsW [SHLWAPI.@]
1698 * See PathFileExistsA.
1700 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1702 UINT iPrevErrMode;
1703 DWORD dwAttr;
1705 TRACE("(%s)\n",debugstr_w(lpszPath));
1707 if (!lpszPath)
1708 return FALSE;
1710 iPrevErrMode = SetErrorMode(1);
1711 dwAttr = GetFileAttributesW(lpszPath);
1712 SetErrorMode(iPrevErrMode);
1713 return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1716 /*************************************************************************
1717 * PathMatchSingleMaskA [internal]
1719 static BOOL WINAPI PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1721 while (*name && *mask && *mask!=';')
1723 if (*mask == '*')
1727 if (PathMatchSingleMaskA(name,mask+1))
1728 return TRUE; /* try substrings */
1729 } while (*name++);
1730 return FALSE;
1733 if (toupper(*mask) != toupper(*name) && *mask != '?')
1734 return FALSE;
1736 name = CharNextA(name);
1737 mask = CharNextA(mask);
1740 if (!*name)
1742 while (*mask == '*')
1743 mask++;
1744 if (!*mask || *mask == ';')
1745 return TRUE;
1747 return FALSE;
1750 /*************************************************************************
1751 * PathMatchSingleMaskW [internal]
1753 static BOOL WINAPI PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1755 while (*name && *mask && *mask != ';')
1757 if (*mask == '*')
1761 if (PathMatchSingleMaskW(name,mask+1))
1762 return TRUE; /* try substrings */
1763 } while (*name++);
1764 return FALSE;
1767 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1768 return FALSE;
1770 name = CharNextW(name);
1771 mask = CharNextW(mask);
1773 if (!*name)
1775 while (*mask == '*')
1776 mask++;
1777 if (!*mask || *mask == ';')
1778 return TRUE;
1780 return FALSE;
1783 /*************************************************************************
1784 * PathMatchSpecA [SHLWAPI.@]
1786 * Determine if a path matches one or more search masks.
1788 * PARAMS
1789 * lpszPath [I] Path to check
1790 * lpszMask [I] Search mask(s)
1792 * RETURNS
1793 * TRUE If lpszPath is valid and is matched
1794 * FALSE Otherwise
1796 * NOTES
1797 * Multiple search masks may be given if they are separated by ";". The
1798 * pattern "*.*" is treated specially in that it matches all paths (for
1799 * backwards compatibility with DOS).
1801 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1803 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1805 if (!lstrcmpA(lpszMask, "*.*"))
1806 return TRUE; /* Matches every path */
1808 while (*lpszMask)
1810 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1811 return TRUE; /* Matches the current mask */
1813 while (*lpszMask && *lpszMask != ';')
1814 lpszMask = CharNextA(lpszMask);
1816 if (*lpszMask == ';')
1818 lpszMask++;
1819 while (*lpszMask == ' ')
1820 lpszMask++; /* masks may be separated by "; " */
1823 return FALSE;
1826 /*************************************************************************
1827 * PathMatchSpecW [SHLWAPI.@]
1829 * See PathMatchSpecA.
1831 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1833 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1835 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1837 if (!lstrcmpW(lpszMask, szStarDotStar))
1838 return TRUE; /* Matches every path */
1840 while (*lpszMask)
1842 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1843 return TRUE; /* Matches the current path */
1845 while (*lpszMask && *lpszMask != ';')
1846 lpszMask++;
1848 if (*lpszMask == ';')
1850 lpszMask++;
1851 while (*lpszMask == ' ')
1852 lpszMask++; /* Masks may be separated by "; " */
1855 return FALSE;
1858 /*************************************************************************
1859 * PathIsSameRootA [SHLWAPI.@]
1861 * Determine if two paths share the same root.
1863 * PARAMS
1864 * lpszPath1 [I] Source path
1865 * lpszPath2 [I] Path to compare with
1867 * RETURNS
1868 * TRUE If both paths are valid and share the same root.
1869 * FALSE If either path is invalid or the paths do not share the same root.
1871 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1873 LPCSTR lpszStart;
1874 int dwLen;
1876 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1878 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1879 return FALSE;
1881 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1882 if (lpszStart - lpszPath1 > dwLen)
1883 return FALSE; /* Paths not common up to length of the root */
1884 return TRUE;
1887 /*************************************************************************
1888 * PathIsSameRootW [SHLWAPI.@]
1890 * See PathIsSameRootA.
1892 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1894 LPCWSTR lpszStart;
1895 int dwLen;
1897 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1899 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1900 return FALSE;
1902 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1903 if (lpszStart - lpszPath1 > dwLen)
1904 return FALSE; /* Paths not common up to length of the root */
1905 return TRUE;
1908 /*************************************************************************
1909 * PathIsContentTypeA [SHLWAPI.@]
1911 * Determine if a file is of a given registered content type.
1913 * PARAMS
1914 * lpszPath [I] File to check
1915 * lpszContentType [I] Content type to check for
1917 * RETURNS
1918 * TRUE If lpszPath is a given registered content type,
1919 * FALSE Otherwise.
1921 * NOTES
1922 * This function looks up the registered content type for lpszPath. If
1923 * a content type is registered, it is compared (case insensitively) to
1924 * lpszContentType. Only if this matches does the function succeed.
1926 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1928 LPCSTR szExt;
1929 DWORD dwDummy;
1930 char szBuff[MAX_PATH];
1932 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
1934 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
1935 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
1936 REG_NONE, szBuff, &dwDummy) &&
1937 !strcasecmp(lpszContentType, szBuff))
1939 return TRUE;
1941 return FALSE;
1944 /*************************************************************************
1945 * PathIsContentTypeW [SHLWAPI.@]
1947 * See PathIsContentTypeA.
1949 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
1951 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
1952 LPCWSTR szExt;
1953 DWORD dwDummy;
1954 WCHAR szBuff[MAX_PATH];
1956 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
1958 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
1959 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
1960 REG_NONE, szBuff, &dwDummy) &&
1961 !strcmpiW(lpszContentType, szBuff))
1963 return TRUE;
1965 return FALSE;
1968 /*************************************************************************
1969 * PathIsFileSpecA [SHLWAPI.@]
1971 * Determine if a path is a file specification.
1973 * PARAMS
1974 * lpszPath [I] Path to chack
1976 * RETURNS
1977 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
1978 * FALSE Otherwise.
1980 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
1982 TRACE("(%s)\n", debugstr_a(lpszPath));
1984 if (!lpszPath)
1985 return FALSE;
1987 while (*lpszPath)
1989 if (*lpszPath == '\\' || *lpszPath == ':')
1990 return FALSE;
1991 lpszPath = CharNextA(lpszPath);
1993 return TRUE;
1996 /*************************************************************************
1997 * PathIsFileSpecW [SHLWAPI.@]
1999 * See PathIsFileSpecA.
2001 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2003 TRACE("(%s)\n", debugstr_w(lpszPath));
2005 if (!lpszPath)
2006 return FALSE;
2008 while (*lpszPath)
2010 if (*lpszPath == '\\' || *lpszPath == ':')
2011 return FALSE;
2012 lpszPath = CharNextW(lpszPath);
2014 return TRUE;
2017 /*************************************************************************
2018 * PathIsPrefixA [SHLWAPI.@]
2020 * Determine if a path is a prefix of another.
2022 * PARAMS
2023 * lpszPrefix [I] Prefix
2024 * lpszPath [I] Path to check
2026 * RETURNS
2027 * TRUE If lpszPath has lpszPrefix as its prefix,
2028 * FALSE If either path is NULL or lpszPrefix is not a prefix
2030 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2032 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2034 if (lpszPrefix && lpszPath &&
2035 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2036 return TRUE;
2037 return FALSE;
2040 /*************************************************************************
2041 * PathIsPrefixW [SHLWAPI.@]
2043 * See PathIsPrefixA.
2045 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2047 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2049 if (lpszPrefix && lpszPath &&
2050 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2051 return TRUE;
2052 return FALSE;
2055 /*************************************************************************
2056 * PathIsSystemFolderA [SHLWAPI.@]
2058 * Determine if a path or file attributes are a system folder.
2060 * PARAMS
2061 * lpszPath [I] Path to check.
2062 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2064 * RETURNS
2065 * TRUE If lpszPath or dwAttrib are a system folder.
2066 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2068 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2070 TRACE("(%s,0x%08lx)\n", debugstr_a(lpszPath), dwAttrib);
2072 if (lpszPath && *lpszPath)
2073 dwAttrib = GetFileAttributesA(lpszPath);
2075 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2076 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2077 return FALSE;
2078 return TRUE;
2081 /*************************************************************************
2082 * PathIsSystemFolderW [SHLWAPI.@]
2084 * See PathIsSystemFolderA.
2086 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2088 TRACE("(%s,0x%08lx)\n", debugstr_w(lpszPath), dwAttrib);
2090 if (lpszPath && *lpszPath)
2091 dwAttrib = GetFileAttributesW(lpszPath);
2093 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2094 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2095 return FALSE;
2096 return TRUE;
2099 /*************************************************************************
2100 * PathIsUNCA [SHLWAPI.@]
2102 * Determine if a path is in UNC format.
2104 * PARAMS
2105 * lpszPath [I] Path to check
2107 * RETURNS
2108 * TRUE: The path is UNC.
2109 * FALSE: The path is not UNC or is NULL.
2111 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2113 TRACE("(%s)\n",debugstr_a(lpszPath));
2115 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2116 return TRUE;
2117 return FALSE;
2120 /*************************************************************************
2121 * PathIsUNCW [SHLWAPI.@]
2123 * See PathIsUNCA.
2125 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2127 TRACE("(%s)\n",debugstr_w(lpszPath));
2129 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2130 return TRUE;
2131 return FALSE;
2134 /*************************************************************************
2135 * PathIsUNCServerA [SHLWAPI.@]
2137 * Determine if a path is a UNC server name ("\\SHARENAME").
2139 * PARAMS
2140 * lpszPath [I] Path to check.
2142 * RETURNS
2143 * TRUE If lpszPath is a valid UNC server name.
2144 * FALSE Otherwise.
2146 * NOTES
2147 * This routine is bug compatible with Win32: Server names with a
2148 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2149 * Fixing this bug may break other shlwapi functions!
2151 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2153 TRACE("(%s)\n", debugstr_a(lpszPath));
2155 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2157 while (*lpszPath)
2159 if (*lpszPath == '\\')
2160 return FALSE;
2161 lpszPath = CharNextA(lpszPath);
2163 return TRUE;
2165 return FALSE;
2168 /*************************************************************************
2169 * PathIsUNCServerW [SHLWAPI.@]
2171 * See PathIsUNCServerA.
2173 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2175 TRACE("(%s)\n", debugstr_w(lpszPath));
2177 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2179 while (*lpszPath)
2181 if (*lpszPath == '\\')
2182 return FALSE;
2183 lpszPath = CharNextW(lpszPath);
2185 return TRUE;
2187 return FALSE;
2190 /*************************************************************************
2191 * PathIsUNCServerShareA [SHLWAPI.@]
2193 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2195 * PARAMS
2196 * lpszPath [I] Path to check.
2198 * RETURNS
2199 * TRUE If lpszPath is a valid UNC server share.
2200 * FALSE Otherwise.
2202 * NOTES
2203 * This routine is bug compatible with Win32: Server shares with a
2204 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2205 * Fixing this bug may break other shlwapi functions!
2207 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2209 TRACE("(%s)\n", debugstr_a(lpszPath));
2211 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2213 BOOL bSeenSlash = FALSE;
2214 while (*lpszPath)
2216 if (*lpszPath == '\\')
2218 if (bSeenSlash)
2219 return FALSE;
2220 bSeenSlash = TRUE;
2222 lpszPath = CharNextA(lpszPath);
2224 return bSeenSlash;
2226 return FALSE;
2229 /*************************************************************************
2230 * PathIsUNCServerShareW [SHLWAPI.@]
2232 * See PathIsUNCServerShareA.
2234 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2236 TRACE("(%s)\n", debugstr_w(lpszPath));
2238 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2240 BOOL bSeenSlash = FALSE;
2241 while (*lpszPath)
2243 if (*lpszPath == '\\')
2245 if (bSeenSlash)
2246 return FALSE;
2247 bSeenSlash = TRUE;
2249 lpszPath = CharNextW(lpszPath);
2251 return bSeenSlash;
2253 return FALSE;
2256 /*************************************************************************
2257 * PathCanonicalizeA [SHLWAPI.@]
2259 * Convert a path to its canonical form.
2261 * PARAMS
2262 * lpszBuf [O] Output path
2263 * lpszPath [I] Path to cnonicalize
2265 * RETURNS
2266 * Success: TRUE. lpszBuf contains the output path,
2267 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2269 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2271 BOOL bRet = FALSE;
2273 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2275 if (lpszBuf)
2276 *lpszBuf = '\0';
2278 if (!lpszBuf || !lpszPath)
2279 SetLastError(ERROR_INVALID_PARAMETER);
2280 else
2282 WCHAR szPath[MAX_PATH];
2283 WCHAR szBuff[MAX_PATH];
2284 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2285 bRet = PathCanonicalizeW(szBuff, szPath);
2286 WideCharToMultiByte(0,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2288 return bRet;
2292 /*************************************************************************
2293 * PathCanonicalizeW [SHLWAPI.@]
2295 * See PathCanonicalizeA.
2297 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2299 LPWSTR lpszDst = lpszBuf;
2300 LPCWSTR lpszSrc = lpszPath;
2302 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2304 if (lpszBuf)
2305 *lpszDst = '\0';
2307 if (!lpszBuf || !lpszPath)
2309 SetLastError(ERROR_INVALID_PARAMETER);
2310 return FALSE;
2313 if (!*lpszPath)
2315 *lpszBuf++ = '\\';
2316 *lpszBuf = '\0';
2317 return TRUE;
2320 /* Copy path root */
2321 if (*lpszSrc == '\\')
2323 *lpszDst++ = *lpszSrc++;
2325 else if (*lpszSrc && lpszSrc[1] == ':')
2327 /* X:\ */
2328 *lpszDst++ = *lpszSrc++;
2329 *lpszDst++ = *lpszSrc++;
2330 if (*lpszSrc == '\\')
2331 *lpszDst++ = *lpszSrc++;
2334 /* Canonicalize the rest of the path */
2335 while (*lpszSrc)
2337 if (*lpszSrc == '.')
2339 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2341 lpszSrc += 2; /* Skip .\ */
2343 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2345 /* \.. backs up a directory, over the root if it has no \ following X:.
2346 * .. is ignored if it would remove a UNC server name or inital \\
2348 if (lpszDst != lpszBuf)
2350 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2351 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2352 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2354 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2356 lpszDst -= 2;
2357 while (lpszDst > lpszBuf && *lpszDst != '\\')
2358 lpszDst--;
2359 if (*lpszDst == '\\')
2360 lpszDst++; /* Reset to last '\' */
2361 else
2362 lpszDst = lpszBuf; /* Start path again from new root */
2364 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2365 lpszDst -= 2;
2367 while (lpszDst > lpszBuf && *lpszDst != '\\')
2368 lpszDst--;
2369 if (lpszDst == lpszBuf)
2371 *lpszDst++ = '\\';
2372 lpszSrc++;
2375 lpszSrc += 2; /* Skip .. in src path */
2377 else
2378 *lpszDst++ = *lpszSrc++;
2380 else
2381 *lpszDst++ = *lpszSrc++;
2383 /* Append \ to naked drive specs */
2384 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2385 *lpszDst++ = '\\';
2386 *lpszDst++ = '\0';
2387 return TRUE;
2390 /*************************************************************************
2391 * PathFindNextComponentA [SHLWAPI.@]
2393 * Find the next component in a path.
2395 * PARAMS
2396 * lpszPath [I] Path to find next component in
2398 * RETURNS
2399 * Success: A pointer to the next component, or the end of the string.
2400 * Failure: NULL, If lpszPath is invalid
2402 * NOTES
2403 * A 'component' is either a backslash character (\) or UNC marker (\\).
2404 * Because of this, relative paths (e.g "c:foo") are regarded as having
2405 * only one component.
2407 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2409 LPSTR lpszSlash;
2411 TRACE("(%s)\n", debugstr_a(lpszPath));
2413 if(!lpszPath || !*lpszPath)
2414 return NULL;
2416 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2418 if (lpszSlash[1] == '\\')
2419 lpszSlash++;
2420 return lpszSlash + 1;
2422 return (LPSTR)lpszPath + strlen(lpszPath);
2425 /*************************************************************************
2426 * PathFindNextComponentW [SHLWAPI.@]
2428 * See PathFindNextComponentA.
2430 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2432 LPWSTR lpszSlash;
2434 TRACE("(%s)\n", debugstr_w(lpszPath));
2436 if(!lpszPath || !*lpszPath)
2437 return NULL;
2439 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2441 if (lpszSlash[1] == '\\')
2442 lpszSlash++;
2443 return lpszSlash + 1;
2445 return (LPWSTR)lpszPath + strlenW(lpszPath);
2448 /*************************************************************************
2449 * PathAddExtensionA [SHLWAPI.@]
2451 * Add a file extension to a path
2453 * PARAMS
2454 * lpszPath [I/O] Path to add extension to
2455 * lpszExtension [I] Extension to add to lpszPath
2457 * RETURNS
2458 * TRUE If the path was modified,
2459 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2460 * extension allready, or the new path length is too big.
2462 * FIXME
2463 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2464 * does not do this, so the behaviour was removed.
2466 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2468 size_t dwLen;
2470 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2472 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2473 return FALSE;
2475 dwLen = strlen(lpszPath);
2477 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2478 return FALSE;
2480 strcpy(lpszPath + dwLen, lpszExtension);
2481 return TRUE;
2484 /*************************************************************************
2485 * PathAddExtensionW [SHLWAPI.@]
2487 * See PathAddExtensionA.
2489 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2491 size_t dwLen;
2493 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2495 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2496 return FALSE;
2498 dwLen = strlenW(lpszPath);
2500 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2501 return FALSE;
2503 strcpyW(lpszPath + dwLen, lpszExtension);
2504 return TRUE;
2507 /*************************************************************************
2508 * PathMakePrettyA [SHLWAPI.@]
2510 * Convert an uppercase DOS filename into lowercase.
2512 * PARAMS
2513 * lpszPath [I/O] Path to convert.
2515 * RETURNS
2516 * TRUE If the path was an uppercase DOS path and was converted,
2517 * FALSE Otherwise.
2519 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2521 LPSTR pszIter = lpszPath;
2523 TRACE("(%s)\n", debugstr_a(lpszPath));
2525 if (!pszIter || !*pszIter)
2526 return FALSE;
2528 while (*pszIter)
2530 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2531 return FALSE; /* Not DOS path */
2532 pszIter++;
2534 pszIter = lpszPath + 1;
2535 while (*pszIter)
2537 *pszIter = tolower(*pszIter);
2538 pszIter++;
2540 return TRUE;
2543 /*************************************************************************
2544 * PathMakePrettyW [SHLWAPI.@]
2546 * See PathMakePrettyA.
2548 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2550 LPWSTR pszIter = lpszPath;
2552 TRACE("(%s)\n", debugstr_w(lpszPath));
2554 if (!pszIter || !*pszIter)
2555 return FALSE;
2557 while (*pszIter)
2559 if (islowerW(*pszIter))
2560 return FALSE; /* Not DOS path */
2561 pszIter++;
2563 pszIter = lpszPath + 1;
2564 while (*pszIter)
2566 *pszIter = tolowerW(*pszIter);
2567 pszIter++;
2569 return TRUE;
2572 /*************************************************************************
2573 * PathCommonPrefixA [SHLWAPI.@]
2575 * Determine the length of the common prefix between two paths.
2577 * PARAMS
2578 * lpszFile1 [I] First path for comparison
2579 * lpszFile2 [I] Second path for comparison
2580 * achPath [O] Destination for common prefix string
2582 * RETURNS
2583 * The length of the common prefix. This is 0 if there is no common
2584 * prefix between the paths or if any parameters are invalid. If the prefix
2585 * is non-zero and achPath is not NULL, achPath is filled with the common
2586 * part of the prefix and NUL terminated.
2588 * NOTES
2589 * A common prefix of 2 is always returned as 3. It is thus possible for
2590 * the length returned to be invalid (i.e. Longer than one or both of the
2591 * strings given as parameters). This Win32 behaviour has been implemented
2592 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2593 * To work around this when using this function, always check that the byte
2594 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2596 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2598 size_t iLen = 0;
2599 LPCSTR lpszIter1 = lpszFile1;
2600 LPCSTR lpszIter2 = lpszFile2;
2602 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2604 if (achPath)
2605 *achPath = '\0';
2607 if (!lpszFile1 || !lpszFile2)
2608 return 0;
2610 /* Handle roots first */
2611 if (PathIsUNCA(lpszFile1))
2613 if (!PathIsUNCA(lpszFile2))
2614 return 0;
2615 lpszIter1 += 2;
2616 lpszIter2 += 2;
2618 else if (PathIsUNCA(lpszFile2))
2619 return 0; /* Know already lpszFile1 is not UNC */
2623 /* Update len */
2624 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2625 (!*lpszIter2 || *lpszIter2 == '\\'))
2626 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2628 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2629 break; /* Strings differ at this point */
2631 lpszIter1++;
2632 lpszIter2++;
2633 } while (1);
2635 if (iLen == 2)
2636 iLen++; /* Feature/Bug compatible with Win32 */
2638 if (iLen && achPath)
2640 memcpy(achPath,lpszFile1,iLen);
2641 achPath[iLen] = '\0';
2643 return iLen;
2646 /*************************************************************************
2647 * PathCommonPrefixW [SHLWAPI.@]
2649 * See PathCommonPrefixA.
2651 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2653 size_t iLen = 0;
2654 LPCWSTR lpszIter1 = lpszFile1;
2655 LPCWSTR lpszIter2 = lpszFile2;
2657 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2659 if (achPath)
2660 *achPath = '\0';
2662 if (!lpszFile1 || !lpszFile2)
2663 return 0;
2665 /* Handle roots first */
2666 if (PathIsUNCW(lpszFile1))
2668 if (!PathIsUNCW(lpszFile2))
2669 return 0;
2670 lpszIter1 += 2;
2671 lpszIter2 += 2;
2673 else if (PathIsUNCW(lpszFile2))
2674 return 0; /* Know already lpszFile1 is not UNC */
2678 /* Update len */
2679 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2680 (!*lpszIter2 || *lpszIter2 == '\\'))
2681 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2683 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2684 break; /* Strings differ at this point */
2686 lpszIter1++;
2687 lpszIter2++;
2688 } while (1);
2690 if (iLen == 2)
2691 iLen++; /* Feature/Bug compatible with Win32 */
2693 if (iLen && achPath)
2695 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2696 achPath[iLen] = '\0';
2698 return iLen;
2701 /*************************************************************************
2702 * PathCompactPathA [SHLWAPI.@]
2704 * Make a path fit into a given width when printed to a DC.
2706 * PARAMS
2707 * hDc [I] Destination DC
2708 * lpszPath [I/O] Path to be printed to hDc
2709 * dx [I] Desired width
2711 * RETURNS
2712 * TRUE If the path was modified.
2713 * FALSE Otherwise.
2715 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2717 BOOL bRet = FALSE;
2719 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2721 if (lpszPath)
2723 WCHAR szPath[MAX_PATH];
2724 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2725 bRet = PathCompactPathW(hDC, szPath, dx);
2726 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2728 return bRet;
2731 /*************************************************************************
2732 * PathCompactPathW [SHLWAPI.@]
2734 * See PathCompactPathA.
2736 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2738 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2739 BOOL bRet = TRUE;
2740 HDC hdc = 0;
2741 WCHAR buff[MAX_PATH];
2742 SIZE size;
2743 DWORD dwLen;
2745 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2747 if (!lpszPath)
2748 return bRet;
2750 if (!hDC)
2751 hdc = hDC = GetDC(0);
2753 /* Get the length of the whole path */
2754 dwLen = strlenW(lpszPath);
2755 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2757 if ((UINT)size.cx > dx)
2759 /* Path too big, must reduce it */
2760 LPWSTR sFile;
2761 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2763 sFile = PathFindFileNameW(lpszPath);
2764 if (sFile != lpszPath)
2765 sFile = CharPrevW(lpszPath, sFile);
2767 /* Get the size of ellipses */
2768 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2769 dwEllipsesLen = size.cx;
2770 /* Get the size of the file name */
2771 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2772 dwPathLen = size.cx;
2774 if (sFile != lpszPath)
2776 LPWSTR sPath = sFile;
2777 BOOL bEllipses = FALSE;
2779 /* The path includes a file name. Include as much of the path prior to
2780 * the file name as possible, allowing for the ellipses, e.g:
2781 * c:\some very long path\filename ==> c:\some v...\filename
2783 strncpyW(buff, sFile, MAX_PATH);
2787 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2789 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2790 dwTotalLen += size.cx;
2791 if (dwTotalLen <= dx)
2792 break;
2793 sPath = CharPrevW(lpszPath, sPath);
2794 if (!bEllipses)
2796 bEllipses = TRUE;
2797 sPath = CharPrevW(lpszPath, sPath);
2798 sPath = CharPrevW(lpszPath, sPath);
2800 } while (sPath > lpszPath);
2802 if (sPath > lpszPath)
2804 if (bEllipses)
2806 strcpyW(sPath, szEllipses);
2807 strcpyW(sPath+3, buff);
2809 bRet = TRUE;
2810 goto end;
2812 strcpyW(lpszPath, szEllipses);
2813 strcpyW(lpszPath+3, buff);
2814 bRet = FALSE;
2815 goto end;
2818 /* Trim the path by adding ellipses to the end, e.g:
2819 * A very long file name.txt ==> A very...
2821 dwLen = strlenW(lpszPath);
2823 if (dwLen > MAX_PATH - 3)
2824 dwLen = MAX_PATH - 3;
2825 strncpyW(buff, sFile, dwLen);
2827 do {
2828 dwLen--;
2829 GetTextExtentPointW(hDC, buff, dwLen, &size);
2830 } while (dwLen && size.cx + dwEllipsesLen > dx);
2832 if (!dwLen)
2834 DWORD dwWritten = 0;
2836 dwEllipsesLen /= 3; /* Size of a single '.' */
2838 /* Write as much of the Ellipses string as possible */
2839 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2841 *lpszPath++ = '.';
2842 dwWritten += dwEllipsesLen;
2843 dwLen++;
2845 *lpszPath = '\0';
2846 bRet = FALSE;
2848 else
2850 strcpyW(buff + dwLen, szEllipses);
2851 strcpyW(lpszPath, buff);
2855 end:
2856 if (hdc)
2857 ReleaseDC(0, hdc);
2859 return bRet;
2862 /*************************************************************************
2863 * PathGetCharTypeA [SHLWAPI.@]
2865 * Categorise a character from a file path.
2867 * PARAMS
2868 * ch [I] Character to get the type of
2870 * RETURNS
2871 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2873 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2875 return PathGetCharTypeW(ch);
2878 /*************************************************************************
2879 * PathGetCharTypeW [SHLWAPI.@]
2881 * See PathGetCharTypeA.
2883 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2885 UINT flags = 0;
2887 TRACE("(%d)\n", ch);
2889 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2890 ch == '"' || ch == '|' || ch == '/')
2891 flags = GCT_INVALID; /* Invalid */
2892 else if (ch == '*' || ch=='?')
2893 flags = GCT_WILD; /* Wildchars */
2894 else if ((ch == '\\') || (ch == ':'))
2895 return GCT_SEPARATOR; /* Path separators */
2896 else
2898 if (ch < 126)
2900 if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2901 ch == '.' || ch == '@' || ch == '^' ||
2902 ch == '\'' || ch == 130 || ch == '`')
2903 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2905 else
2906 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2907 flags |= GCT_LFNCHAR; /* Valid for long file names */
2909 return flags;
2912 /*************************************************************************
2913 * SHLWAPI_UseSystemForSystemFolders
2915 * Internal helper for PathMakeSystemFolderW.
2917 static BOOL WINAPI SHLWAPI_UseSystemForSystemFolders()
2919 static BOOL bCheckedReg = FALSE;
2920 static BOOL bUseSystemForSystemFolders = FALSE;
2922 if (!bCheckedReg)
2924 bCheckedReg = TRUE;
2926 /* Key tells Win what file attributes to use on system folders */
2927 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2928 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2929 "UseSystemForSystemFolders", 0, 0, 0))
2930 bUseSystemForSystemFolders = TRUE;
2932 return bUseSystemForSystemFolders;
2935 /*************************************************************************
2936 * PathMakeSystemFolderA [SHLWAPI.@]
2938 * Set system folder attribute for a path.
2940 * PARAMS
2941 * lpszPath [I] The path to turn into a system folder
2943 * RETURNS
2944 * TRUE If the path was changed to/already was a system folder
2945 * FALSE If the path is invalid or SetFileAttributesA() fails
2947 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
2949 BOOL bRet = FALSE;
2951 TRACE("(%s)\n", debugstr_a(lpszPath));
2953 if (lpszPath && *lpszPath)
2955 WCHAR szPath[MAX_PATH];
2956 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2957 bRet = PathMakeSystemFolderW(szPath);
2959 return bRet;
2962 /*************************************************************************
2963 * PathMakeSystemFolderW [SHLWAPI.@]
2965 * See PathMakeSystemFolderA.
2967 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
2969 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
2970 WCHAR buff[MAX_PATH];
2972 TRACE("(%s)\n", debugstr_w(lpszPath));
2974 if (!lpszPath || !*lpszPath)
2975 return FALSE;
2977 /* If the directory is already a system directory, dont do anything */
2978 GetSystemDirectoryW(buff, MAX_PATH);
2979 if (!strcmpW(buff, lpszPath))
2980 return TRUE;
2982 GetWindowsDirectoryW(buff, MAX_PATH);
2983 if (!strcmpW(buff, lpszPath))
2984 return TRUE;
2986 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
2987 if (SHLWAPI_UseSystemForSystemFolders())
2988 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
2990 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
2991 return FALSE;
2993 /* Change file attributes to system attributes */
2994 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
2995 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
2998 /*************************************************************************
2999 * PathRenameExtensionA [SHLWAPI.@]
3001 * Swap the file extension in a path with another extension.
3003 * PARAMS
3004 * lpszPath [I/O] Path to swap the extension in
3005 * lpszExt [I] The new extension
3007 * RETURNS
3008 * TRUE if lpszPath was modified,
3009 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3011 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3013 LPSTR lpszExtension;
3015 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3017 lpszExtension = PathFindExtensionA(lpszPath);
3019 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3020 return FALSE;
3022 strcpy(lpszExtension, lpszExt);
3023 return TRUE;
3026 /*************************************************************************
3027 * PathRenameExtensionW [SHLWAPI.@]
3029 * See PathRenameExtensionA.
3031 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3033 LPWSTR lpszExtension;
3035 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3037 lpszExtension = PathFindExtensionW(lpszPath);
3039 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3040 return FALSE;
3042 strcpyW(lpszExtension, lpszExt);
3043 return TRUE;
3046 /*************************************************************************
3047 * PathSearchAndQualifyA [SHLWAPI.@]
3049 * Determine if a given path is correct and fully qualified.
3051 * PARAMS
3052 * lpszPath [I] Path to check
3053 * lpszBuf [O] Output for correct path
3054 * cchBuf [I] Size of lpszBuf
3056 * RETURNS
3057 * Unknown.
3059 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3061 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3062 return FALSE;
3065 /*************************************************************************
3066 * PathSearchAndQualifyW [SHLWAPI.@]
3068 * See PathSearchAndQualifyA.
3070 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3072 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3073 return FALSE;
3076 /*************************************************************************
3077 * PathSkipRootA [SHLWAPI.@]
3079 * Return the portion of a path following the drive letter or mount point.
3081 * PARAMS
3082 * lpszPath [I] The path to skip on
3084 * RETURNS
3085 * Success: A pointer to the next character after the root.
3086 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3088 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3090 TRACE("(%s)\n", debugstr_a(lpszPath));
3092 if (!lpszPath || !*lpszPath)
3093 return NULL;
3095 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3097 /* Network share: skip share server and mount point */
3098 lpszPath += 2;
3099 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3100 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3101 lpszPath++;
3102 return (LPSTR)lpszPath;
3105 if (IsDBCSLeadByte(*lpszPath))
3106 return NULL;
3108 /* Check x:\ */
3109 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3110 return (LPSTR)lpszPath + 3;
3111 return NULL;
3114 /*************************************************************************
3115 * PathSkipRootW [SHLWAPI.@]
3117 * See PathSkipRootA.
3119 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3121 TRACE("(%s)\n", debugstr_w(lpszPath));
3123 if (!lpszPath || !*lpszPath)
3124 return NULL;
3126 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3128 /* Network share: skip share server and mount point */
3129 lpszPath += 2;
3130 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3131 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3132 lpszPath++;
3133 return (LPWSTR)lpszPath;
3136 /* Check x:\ */
3137 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3138 return (LPWSTR)lpszPath + 3;
3139 return NULL;
3142 /*************************************************************************
3143 * PathCreateFromUrlA [SHLWAPI.@]
3145 * Create a path from a URL
3147 * PARAMS
3148 * lpszUrl [I] URL to convert into a path
3149 * lpszPath [O] Output buffer for the resulting Path
3150 * pcchPath [I] Length of lpszPath
3151 * dwFlags [I] Flags controlling the conversion
3153 * RETURNS
3154 * Success: S_OK. lpszPath contains the URL in path format,
3155 * Failure: An HRESULT error code such as E_INVALIDARG.
3157 HRESULT WINAPI PathCreateFromUrlA(LPCSTR lpszUrl, LPSTR lpszPath,
3158 LPDWORD pcchPath, DWORD dwFlags)
3160 LPSTR pszPathPart;
3161 TRACE("(%s,%p,%p,0x%08lx)\n", debugstr_a(lpszUrl), lpszPath, pcchPath, dwFlags);
3163 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3164 return E_INVALIDARG;
3166 pszPathPart = StrChrA(lpszUrl, ':');
3167 if ((((pszPathPart - lpszUrl) == 1) && isalpha(*lpszUrl)) ||
3168 !lstrcmpA(lpszUrl, "file:"))
3170 return UrlUnescapeA(pszPathPart, lpszPath, pcchPath, dwFlags);
3172 /* extracts thing prior to : in pszURL and checks against:
3173 * https
3174 * shell
3175 * local
3176 * about - if match returns E_INVALIDARG
3179 return E_INVALIDARG;
3182 /*************************************************************************
3183 * PathCreateFromUrlW [SHLWAPI.@]
3185 * See PathCreateFromUrlA.
3187 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR lpszUrl, LPWSTR lpszPath,
3188 LPDWORD pcchPath, DWORD dwFlags)
3190 static const WCHAR stemp[] = { 'f','i','l','e',':','/','/',0 };
3191 LPWSTR pwszPathPart;
3192 HRESULT hr;
3194 TRACE("(%s,%p,%p,0x%08lx)\n", debugstr_w(lpszUrl), lpszPath, pcchPath, dwFlags);
3196 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3197 return E_INVALIDARG;
3199 /* Path of the form file://... */
3200 if (!strncmpW(lpszUrl, stemp, 7))
3202 lpszUrl += 7;
3204 /* Path of the form file:... */
3205 else if (!strncmpW(lpszUrl, stemp, 5))
3207 lpszUrl += 5;
3210 /* Ensure that path is of the form c:... or c|... */
3211 if (lpszUrl[1] != ':' && lpszUrl[1] != '|' && isalphaW(*lpszUrl))
3212 return E_INVALIDARG;
3214 hr = UrlUnescapeW(lpszUrl, lpszPath, pcchPath, dwFlags);
3215 if (lpszPath[1] == '|')
3216 lpszPath[1] = ':';
3218 for (pwszPathPart = lpszPath; *pwszPathPart; pwszPathPart++)
3219 if (*pwszPathPart == '/')
3220 *pwszPathPart = '\\';
3222 TRACE("Returning %s\n",debugstr_w(lpszPath));
3224 return hr;
3227 /*************************************************************************
3228 * PathRelativePathToA [SHLWAPI.@]
3230 * Create a relative path from one path to another.
3232 * PARAMS
3233 * lpszPath [O] Destination for relative path
3234 * lpszFrom [I] Source path
3235 * dwAttrFrom [I] File attribute of source path
3236 * lpszTo [I] Destination path
3237 * dwAttrTo [I] File attributes of destination path
3239 * RETURNS
3240 * TRUE If a relative path can be formed. lpszPath contains the new path
3241 * FALSE If the paths are not relavtive or any parameters are invalid
3243 * NOTES
3244 * lpszTo should be at least MAX_PATH in length.
3246 * Calling this function with relative paths for lpszFrom or lpszTo may
3247 * give erroneous results.
3249 * The Win32 version of this function contains a bug where the lpszTo string
3250 * may be referenced 1 byte beyond the end of the string. As a result random
3251 * garbage may be written to the output path, depending on what lies beyond
3252 * the last byte of the string. This bug occurs because of the behaviour of
3253 * PathCommonPrefix() (see notes for that function), and no workaround seems
3254 * possible with Win32.
3256 * This bug has been fixed here, so for example the relative path from "\\"
3257 * to "\\" is correctly determined as "." in this implementation.
3259 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3260 LPCSTR lpszTo, DWORD dwAttrTo)
3262 BOOL bRet = FALSE;
3264 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_a(lpszFrom),
3265 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3267 if(lpszPath && lpszFrom && lpszTo)
3269 WCHAR szPath[MAX_PATH];
3270 WCHAR szFrom[MAX_PATH];
3271 WCHAR szTo[MAX_PATH];
3272 MultiByteToWideChar(0,0,lpszFrom,-1,szFrom,MAX_PATH);
3273 MultiByteToWideChar(0,0,lpszTo,-1,szTo,MAX_PATH);
3274 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3275 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3277 return bRet;
3280 /*************************************************************************
3281 * PathRelativePathToW [SHLWAPI.@]
3283 * See PathRelativePathToA.
3285 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3286 LPCWSTR lpszTo, DWORD dwAttrTo)
3288 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3289 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3290 WCHAR szFrom[MAX_PATH];
3291 WCHAR szTo[MAX_PATH];
3292 DWORD dwLen;
3294 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_w(lpszFrom),
3295 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3297 if(!lpszPath || !lpszFrom || !lpszTo)
3298 return FALSE;
3300 *lpszPath = '\0';
3301 strncpyW(szFrom, lpszFrom, MAX_PATH);
3302 strncpyW(szTo, lpszTo, MAX_PATH);
3304 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3305 PathRemoveFileSpecW(szFrom);
3306 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3307 PathRemoveFileSpecW(szTo);
3309 /* Paths can only be relative if they have a common root */
3310 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3311 return FALSE;
3313 /* Strip off lpszFrom components to the root, by adding "..\" */
3314 lpszFrom = szFrom + dwLen;
3315 if (!*lpszFrom)
3317 lpszPath[0] = '.';
3318 lpszPath[1] = '\0';
3320 if (*lpszFrom == '\\')
3321 lpszFrom++;
3323 while (*lpszFrom)
3325 lpszFrom = PathFindNextComponentW(lpszFrom);
3326 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3329 /* From the root add the components of lpszTo */
3330 lpszTo += dwLen;
3331 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3332 * this function.
3334 if (*lpszTo && lpszTo[-1])
3336 if (*lpszTo != '\\')
3337 lpszTo--;
3338 dwLen = strlenW(lpszPath);
3339 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3341 *lpszPath = '\0';
3342 return FALSE;
3344 strcpyW(lpszPath + dwLen, lpszTo);
3346 return TRUE;
3349 /*************************************************************************
3350 * PathUnmakeSystemFolderA [SHLWAPI.@]
3352 * Remove the system folder attributes from a path.
3354 * PARAMS
3355 * lpszPath [I] The path to remove attributes from
3357 * RETURNS
3358 * Success: TRUE.
3359 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3360 * SetFileAttributesA() fails.
3362 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3364 DWORD dwAttr;
3366 TRACE("(%s)\n", debugstr_a(lpszPath));
3368 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3369 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3370 return FALSE;
3372 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3373 return SetFileAttributesA(lpszPath, dwAttr);
3376 /*************************************************************************
3377 * PathUnmakeSystemFolderW [SHLWAPI.@]
3379 * See PathUnmakeSystemFolderA.
3381 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3383 DWORD dwAttr;
3385 TRACE("(%s)\n", debugstr_w(lpszPath));
3387 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3388 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3389 return FALSE;
3391 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3392 return SetFileAttributesW(lpszPath, dwAttr);
3396 /*************************************************************************
3397 * PathSetDlgItemPathA [SHLWAPI.@]
3399 * Set the text of a dialog item to a path, shrinking the path to fit
3400 * if it is too big for the item.
3402 * PARAMS
3403 * hDlg [I] Dialog handle
3404 * id [I] ID of item in the dialog
3405 * lpszPath [I] Path to set as the items text
3407 * RETURNS
3408 * Nothing.
3410 * NOTES
3411 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3412 * window text is erased).
3414 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3416 WCHAR szPath[MAX_PATH];
3418 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3420 if (lpszPath)
3421 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3422 else
3423 szPath[0] = '\0';
3424 PathSetDlgItemPathW(hDlg, id, szPath);
3427 /*************************************************************************
3428 * PathSetDlgItemPathW [SHLWAPI.@]
3430 * See PathSetDlgItemPathA.
3432 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3434 WCHAR path[MAX_PATH + 1];
3435 HWND hwItem;
3436 RECT rect;
3437 HDC hdc;
3438 HGDIOBJ hPrevObj;
3440 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3442 if (!(hwItem = GetDlgItem(hDlg, id)))
3443 return;
3445 if (lpszPath)
3446 strncpyW(path, lpszPath, sizeof(path));
3447 else
3448 path[0] = '\0';
3450 GetClientRect(hwItem, &rect);
3451 hdc = GetDC(hDlg);
3452 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3454 if (hPrevObj)
3456 PathCompactPathW(hdc, path, rect.right);
3457 SelectObject(hdc, hPrevObj);
3460 ReleaseDC(hDlg, hdc);
3461 SetWindowTextW(hwItem, path);
3464 /*************************************************************************
3465 * PathIsNetworkPathA [SHLWAPI.@]
3467 * Determine if the given path is a network path.
3469 * PARAMS
3470 * lpszPath [I] Path to check
3472 * RETURNS
3473 * TRUE If lpszPath is a UNC share or mapped network drive, or
3474 * FALSE If lpszPath is a local drive or cannot be determined
3476 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3478 int dwDriveNum;
3480 TRACE("(%s)\n",debugstr_a(lpszPath));
3482 if (!lpszPath)
3483 return FALSE;
3484 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3485 return TRUE;
3486 dwDriveNum = PathGetDriveNumberA(lpszPath);
3487 if (dwDriveNum == -1)
3488 return FALSE;
3489 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3490 return pIsNetDrive(dwDriveNum);
3493 /*************************************************************************
3494 * PathIsNetworkPathW [SHLWAPI.@]
3496 * See PathIsNetworkPathA.
3498 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3500 int dwDriveNum;
3502 TRACE("(%s)\n", debugstr_w(lpszPath));
3504 if (!lpszPath)
3505 return FALSE;
3506 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3507 return TRUE;
3508 dwDriveNum = PathGetDriveNumberW(lpszPath);
3509 if (dwDriveNum == -1)
3510 return FALSE;
3511 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3512 return pIsNetDrive(dwDriveNum);
3515 /*************************************************************************
3516 * PathIsLFNFileSpecA [SHLWAPI.@]
3518 * Determine if the given path is a long file name
3520 * PARAMS
3521 * lpszPath [I] Path to check
3523 * RETURNS
3524 * TRUE If path is a long file name,
3525 * FALSE If path is a valid DOS short file name
3527 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3529 DWORD dwNameLen = 0, dwExtLen = 0;
3531 TRACE("(%s)\n",debugstr_a(lpszPath));
3533 if (!lpszPath)
3534 return FALSE;
3536 while (*lpszPath)
3538 if (*lpszPath == ' ')
3539 return TRUE; /* DOS names cannot have spaces */
3540 if (*lpszPath == '.')
3542 if (dwExtLen)
3543 return TRUE; /* DOS names have only one dot */
3544 dwExtLen = 1;
3546 else if (dwExtLen)
3548 dwExtLen++;
3549 if (dwExtLen > 4)
3550 return TRUE; /* DOS extensions are <= 3 chars*/
3552 else
3554 dwNameLen++;
3555 if (dwNameLen > 8)
3556 return TRUE; /* DOS names are <= 8 chars */
3558 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3560 return FALSE; /* Valid DOS path */
3563 /*************************************************************************
3564 * PathIsLFNFileSpecW [SHLWAPI.@]
3566 * See PathIsLFNFileSpecA.
3568 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3570 DWORD dwNameLen = 0, dwExtLen = 0;
3572 TRACE("(%s)\n",debugstr_w(lpszPath));
3574 if (!lpszPath)
3575 return FALSE;
3577 while (*lpszPath)
3579 if (*lpszPath == ' ')
3580 return TRUE; /* DOS names cannot have spaces */
3581 if (*lpszPath == '.')
3583 if (dwExtLen)
3584 return TRUE; /* DOS names have only one dot */
3585 dwExtLen = 1;
3587 else if (dwExtLen)
3589 dwExtLen++;
3590 if (dwExtLen > 4)
3591 return TRUE; /* DOS extensions are <= 3 chars*/
3593 else
3595 dwNameLen++;
3596 if (dwNameLen > 8)
3597 return TRUE; /* DOS names are <= 8 chars */
3599 lpszPath++;
3601 return FALSE; /* Valid DOS path */
3604 /*************************************************************************
3605 * PathIsDirectoryEmptyA [SHLWAPI.@]
3607 * Determine if a given directory is empty.
3609 * PARAMS
3610 * lpszPath [I] Directory to check
3612 * RETURNS
3613 * TRUE If the directory exists and contains no files,
3614 * FALSE Otherwise
3616 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3618 BOOL bRet = FALSE;
3620 TRACE("(%s)\n",debugstr_a(lpszPath));
3622 if (lpszPath)
3624 WCHAR szPath[MAX_PATH];
3625 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3626 bRet = PathIsDirectoryEmptyW(szPath);
3628 return bRet;
3631 /*************************************************************************
3632 * PathIsDirectoryEmptyW [SHLWAPI.@]
3634 * See PathIsDirectoryEmptyA.
3636 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3638 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3639 WCHAR szSearch[MAX_PATH];
3640 DWORD dwLen;
3641 HANDLE hfind;
3642 BOOL retVal = FALSE;
3643 WIN32_FIND_DATAW find_data;
3645 TRACE("(%s)\n",debugstr_w(lpszPath));
3647 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3648 return FALSE;
3650 strncpyW(szSearch, lpszPath, MAX_PATH);
3651 PathAddBackslashW(szSearch);
3652 dwLen = strlenW(szSearch);
3653 if (dwLen > MAX_PATH - 4)
3654 return FALSE;
3656 strcpyW(szSearch + dwLen, szAllFiles);
3657 hfind = FindFirstFileW(szSearch, &find_data);
3659 if (hfind != INVALID_HANDLE_VALUE &&
3660 find_data.cFileName[0] == '.' &&
3661 find_data.cFileName[1] == '.')
3663 /* The only directory entry should be the parent */
3664 if (!FindNextFileW(hfind, &find_data))
3665 retVal = TRUE;
3666 FindClose(hfind);
3668 return retVal;
3672 /*************************************************************************
3673 * PathFindSuffixArrayA [SHLWAPI.@]
3675 * Find a suffix string in an array of suffix strings
3677 * PARAMS
3678 * lpszSuffix [I] Suffix string to search for
3679 * lppszArray [I] Array of suffix strings to search
3680 * dwCount [I] Number of elements in lppszArray
3682 * RETURNS
3683 * Success: The index of the position of lpszSuffix in lppszArray
3684 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3686 * NOTES
3687 * The search is case sensitive.
3688 * The match is made against the end of the suffix string, so for example:
3689 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3691 int WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3693 size_t dwLen;
3694 int dwRet = 0;
3696 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3698 if (lpszSuffix && lppszArray && dwCount > 0)
3700 dwLen = strlen(lpszSuffix);
3702 while (dwRet < dwCount)
3704 size_t dwCompareLen = strlen(*lppszArray);
3705 if (dwCompareLen < dwLen)
3707 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3708 return dwRet; /* Found */
3710 dwRet++;
3711 lppszArray++;
3714 return 0;
3717 /*************************************************************************
3718 * PathFindSuffixArrayW [SHLWAPI.@]
3720 * See PathFindSuffixArrayA.
3722 int WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3724 size_t dwLen;
3725 int dwRet = 0;
3727 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3729 if (lpszSuffix && lppszArray && dwCount > 0)
3731 dwLen = strlenW(lpszSuffix);
3733 while (dwRet < dwCount)
3735 size_t dwCompareLen = strlenW(*lppszArray);
3736 if (dwCompareLen < dwLen)
3738 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3739 return dwRet; /* Found */
3741 dwRet++;
3742 lppszArray++;
3745 return 0;
3748 /*************************************************************************
3749 * PathUndecorateA [SHLWAPI.@]
3751 * Undecorate a file path
3753 * PARAMS
3754 * lpszPath [I/O] Path to remove any decoration from
3756 * RETURNS
3757 * Nothing
3759 * NOTES
3760 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3762 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3764 TRACE("(%s)\n",debugstr_a(lpszPath));
3766 if (lpszPath)
3768 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3769 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3771 LPSTR lpszSkip = lpszExt - 2;
3772 if (*lpszSkip == '[')
3773 lpszSkip++; /* [] (no number) */
3774 else
3775 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3776 lpszSkip--;
3777 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3779 /* remove the [n] */
3780 lpszSkip--;
3781 while (*lpszExt)
3782 *lpszSkip++ = *lpszExt++;
3783 *lpszSkip = '\0';
3789 /*************************************************************************
3790 * PathUndecorateW [SHLWAPI.@]
3792 * See PathUndecorateA.
3794 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3796 TRACE("(%s)\n",debugstr_w(lpszPath));
3798 if (lpszPath)
3800 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3801 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3803 LPWSTR lpszSkip = lpszExt - 2;
3804 if (*lpszSkip == '[')
3805 lpszSkip++; /* [] (no number) */
3806 else
3807 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3808 lpszSkip--;
3809 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3811 /* remove the [n] */
3812 lpszSkip--;
3813 while (*lpszExt)
3814 *lpszSkip++ = *lpszExt++;
3815 *lpszSkip = '\0';
3821 /*************************************************************************
3822 * @ [SHLWAPI.440]
3824 * Find localised or default web content in "%WINDOWS%\web\".
3826 * PARAMS
3827 * lpszFile [I] File name containing content to look for
3828 * lpszPath [O] Buffer to contain the full path to the file
3829 * dwPathLen [I] Length of lpszPath
3831 * RETURNS
3832 * Success: S_OK. lpszPath contains the full path to the content.
3833 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
3835 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
3837 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
3838 HRESULT hRet;
3840 TRACE("(%s,%p,%ld)\n", lpszFile, lpszPath, dwPathLen);
3842 MultiByteToWideChar(0, 0, lpszFile, -1, szFile, MAX_PATH);
3843 szPath[0] = '\0';
3844 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
3845 WideCharToMultiByte(0, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
3846 return hRet;
3849 /*************************************************************************
3850 * @ [SHLWAPI.441]
3852 * Unicode version of SHGetWebFolderFilePathA.
3854 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
3856 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
3857 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
3858 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
3859 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
3860 DWORD dwLen, dwFileLen;
3861 LANGID lidSystem, lidUser;
3863 TRACE("(%s,%p,%ld)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
3865 /* Get base directory for web content */
3866 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
3867 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
3868 dwLen--;
3870 dwFileLen = strlenW(lpszFile);
3872 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
3873 return E_FAIL; /* lpszPath too short */
3875 strcpyW(lpszPath+dwLen, szWeb);
3876 dwLen += szWebLen;
3877 dwPathLen = dwPathLen - dwLen; /* Remaining space */
3879 lidSystem = GetSystemDefaultUILanguage();
3880 lidUser = GetUserDefaultUILanguage();
3882 if (lidSystem != lidUser)
3884 if (dwFileLen + szWebMuiLen < dwPathLen)
3886 /* Use localised content in the users UI language if present */
3887 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
3888 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
3889 if (PathFileExistsW(lpszPath))
3890 return S_OK;
3894 /* Fall back to OS default installed content */
3895 strcpyW(lpszPath + dwLen, lpszFile);
3896 if (PathFileExistsW(lpszPath))
3897 return S_OK;
3898 return E_FAIL;