Release 20030408.
[wine/gsoc-2012-control.git] / dlls / shlwapi / path.c
blobcb2f0407653c8b9e1abcc96be1a447395b383779
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 <ctype.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include "winerror.h"
30 #include "wine/unicode.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 if (!(func = (void*)GetProcAddress(SHLWAPI_h##module, name))) return fail; \
47 } \
48 } while (0)
50 /* DLL handles for late bound calls */
51 extern HMODULE SHLWAPI_hshell32;
53 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
54 static BOOL (WINAPI *pIsNetDrive)(DWORD);
56 /*************************************************************************
57 * PathAppendA [SHLWAPI.@]
59 * Append one path to another.
61 * PARAMS
62 * lpszPath [O] Initial part of path
63 * lpszAppend [I] Path to append
65 * RETURNS
66 * Success: TRUE. lpszPath contains the newly created path.
67 * Failure: FALSE, if either path is NULL, or PathCombineA() fails.
69 * NOTES
70 * lpszAppend must contain at least one backslash ('\') if not NULL.
71 * Because PathCombineA() is used to join the paths, the resulting
72 * path is also canonicalized.
74 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
76 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
78 if (lpszPath && lpszAppend)
80 if (!PathIsUNCA(lpszAppend))
81 while (*lpszAppend == '\\')
82 lpszAppend++;
83 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
84 return TRUE;
86 return FALSE;
89 /*************************************************************************
90 * PathAppendW [SHLWAPI.@]
92 * See PathAppendA.
94 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
96 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
98 if (lpszPath && lpszAppend)
100 if (!PathIsUNCW(lpszAppend))
101 while (*lpszAppend == '\\')
102 lpszAppend++;
103 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
104 return TRUE;
106 return FALSE;
109 /*************************************************************************
110 * PathCombineA [SHLWAPI.@]
112 * Combine two paths together.
114 * PARAMS
115 * lpszDest [O] Destination for combined path
116 * lpszDir [I] Directory path
117 * lpszFile [I] File path
119 * RETURNS
120 * Success: The output path
121 * Failure: NULL, if inputs are invalid.
123 * NOTES
124 * lpszDest should be at least MAX_PATH in size, and may point to the same
125 * memory location as lpszDir. The combined path is canonicalised.
127 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
129 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
131 if (!lpszDest || (!lpszDir && !lpszFile))
132 return NULL; /* Invalid parameters */
133 else
135 WCHAR szDest[MAX_PATH];
136 WCHAR szDir[MAX_PATH];
137 WCHAR szFile[MAX_PATH];
138 if (lpszDir)
139 MultiByteToWideChar(0,0,lpszDir,-1,szDir,MAX_PATH);
140 if (lpszFile)
141 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
142 PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL);
143 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
145 return lpszDest;
148 /*************************************************************************
149 * PathCombineW [SHLWAPI.@]
151 * See PathCombineA.
153 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
155 WCHAR szTemp[MAX_PATH];
156 BOOL bUseBoth = FALSE, bStrip = FALSE;
158 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
160 if (!lpszDest || (!lpszDir && !lpszFile))
161 return lpszDest; /* Invalid parameters */
163 if (!lpszFile || !*lpszFile)
165 /* Use dir only */
166 strncpyW(szTemp, lpszDir, MAX_PATH);
168 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
170 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
172 /* Use file only */
173 strncpyW(szTemp, lpszFile, MAX_PATH);
175 else
177 bUseBoth = TRUE;
178 bStrip = TRUE;
181 else
182 bUseBoth = TRUE;
184 if (bUseBoth)
186 strncpyW(szTemp, lpszDir, MAX_PATH);
187 if (bStrip)
189 PathStripToRootW(szTemp);
190 lpszFile++; /* Skip '\' */
192 if (!PathAddBackslashW(szTemp))
193 return NULL;
194 if (strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
195 return NULL;
196 strcatW(szTemp, lpszFile);
199 PathCanonicalizeW(lpszDest, szTemp);
200 return lpszDest;
203 /*************************************************************************
204 * PathAddBackslashA [SHLWAPI.@]
206 * Append a backslash ('\') to a path if one doesn't exist.
208 * PARAMS
209 * lpszPath [O] The path to append a backslash to.
211 * RETURNS
212 * Success: The position of the last backslash in the path.
213 * Failure: NULL, if lpszPath is NULL or the path is too large.
215 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
217 int iLen;
219 TRACE("(%s)\n",debugstr_a(lpszPath));
221 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
222 return NULL;
224 if (iLen)
226 lpszPath += iLen;
227 if (lpszPath[-1] != '\\')
229 *lpszPath++ = '\\';
230 *lpszPath = '\0';
233 return lpszPath;
236 /*************************************************************************
237 * PathAddBackslashW [SHLWAPI.@]
239 * See PathAddBackslashA.
241 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
243 int iLen;
245 TRACE("(%s)\n",debugstr_w(lpszPath));
247 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
248 return NULL;
250 if (iLen)
252 lpszPath += iLen;
253 if (lpszPath[-1] != '\\')
255 *lpszPath++ = '\\';
256 *lpszPath = '\0';
259 return lpszPath;
262 /*************************************************************************
263 * PathBuildRootA [SHLWAPI.@]
265 * Create a root drive string (e.g. "A:\") from a drive number.
267 * PARAMS
268 * lpszPath [O] Destination for the drive string
270 * RETURNS
271 * lpszPath
273 * NOTES
274 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
276 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
278 TRACE("(%p,%d)\n", debugstr_a(lpszPath), drive);
280 if (lpszPath && drive >= 0 && drive < 26)
282 lpszPath[0] = 'A' + drive;
283 lpszPath[1] = ':';
284 lpszPath[2] = '\\';
285 lpszPath[3] = '\0';
287 return lpszPath;
290 /*************************************************************************
291 * PathBuildRootW [SHLWAPI.@]
293 * See PathBuildRootA.
295 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
297 TRACE("(%p,%d)\n",debugstr_w(lpszPath), drive);
299 if (lpszPath && drive >= 0 && drive < 26)
301 lpszPath[0] = 'A' + drive;
302 lpszPath[1] = ':';
303 lpszPath[2] = '\\';
304 lpszPath[3] = '\0';
306 return lpszPath;
309 /*************************************************************************
310 * PathFindFileNameA [SHLWAPI.@]
312 * Locate the start of the file name in a path
314 * PARAMS
315 * lpszPath [I] Path to search
317 * RETURNS
318 * A pointer to the first character of the file name
320 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
322 LPCSTR lastSlash = lpszPath;
324 TRACE("(%s)\n",debugstr_a(lpszPath));
326 while (lpszPath && *lpszPath)
328 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
329 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
330 lastSlash = lpszPath + 1;
331 lpszPath = CharNextA(lpszPath);
333 return (LPSTR)lastSlash;
336 /*************************************************************************
337 * PathFindFileNameW [SHLWAPI.@]
339 * See PathFindFileNameA.
341 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
343 LPCWSTR lastSlash = lpszPath;
345 TRACE("(%s)\n",debugstr_w(lpszPath));
347 while (lpszPath && *lpszPath)
349 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
350 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
351 lastSlash = lpszPath + 1;
352 lpszPath = CharNextW(lpszPath);
354 return (LPWSTR)lastSlash;
357 /*************************************************************************
358 * PathFindExtensionA [SHLWAPI.@]
360 * Locate the start of the file extension in a path
362 * PARAMS
363 * lpszPath [I] The path to search
365 * RETURNS
366 * A pointer to the first character of the extension, the end of
367 * the string if the path has no extension, or NULL If lpszPath is NULL
369 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
371 LPCSTR lastpoint = NULL;
373 TRACE("(%s)\n", debugstr_a(lpszPath));
375 if (lpszPath)
377 while (*lpszPath)
379 if (*lpszPath == '\\' || *lpszPath==' ')
380 lastpoint = NULL;
381 else if (*lpszPath == '.')
382 lastpoint = lpszPath;
383 lpszPath = CharNextA(lpszPath);
386 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
389 /*************************************************************************
390 * PathFindExtensionW [SHLWAPI.@]
392 * See PathFindExtensionA.
394 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
396 LPCWSTR lastpoint = NULL;
398 TRACE("(%s)\n", debugstr_w(lpszPath));
400 if (lpszPath)
402 while (*lpszPath)
404 if (*lpszPath == '\\' || *lpszPath==' ')
405 lastpoint = NULL;
406 else if (*lpszPath == '.')
407 lastpoint = lpszPath;
408 lpszPath = CharNextW(lpszPath);
411 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
414 /*************************************************************************
415 * PathGetArgsA [SHLWAPI.@]
417 * Find the next argument in a string delimited by spaces.
419 * PARAMS
420 * lpszPath [I] The string to search for arguments in
422 * RETURNS
423 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
425 * NOTES
426 * Spaces in quoted strings are ignored as delimiters.
428 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
430 BOOL bSeenQuote = FALSE;
432 TRACE("(%s)\n",debugstr_a(lpszPath));
434 if (lpszPath)
436 while (*lpszPath)
438 if ((*lpszPath==' ') && !bSeenQuote)
439 return (LPSTR)lpszPath + 1;
440 if (*lpszPath == '"')
441 bSeenQuote = !bSeenQuote;
442 lpszPath = CharNextA(lpszPath);
445 return (LPSTR)lpszPath;
448 /*************************************************************************
449 * PathGetArgsW [SHLWAPI.@]
451 * See PathGetArgsA.
453 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
455 BOOL bSeenQuote = FALSE;
457 TRACE("(%s)\n",debugstr_w(lpszPath));
459 if (lpszPath)
461 while (*lpszPath)
463 if ((*lpszPath==' ') && !bSeenQuote)
464 return (LPWSTR)lpszPath + 1;
465 if (*lpszPath == '"')
466 bSeenQuote = !bSeenQuote;
467 lpszPath = CharNextW(lpszPath);
470 return (LPWSTR)lpszPath;
473 /*************************************************************************
474 * PathGetDriveNumberA [SHLWAPI.@]
476 * Return the drive number from a path
478 * PARAMS
479 * lpszPath [I] Path to get the drive number from
481 * RETURNS
482 * Success: The drive number corresponding to the drive in the path
483 * Failure: -1, if lpszPath contains no valid drive
485 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
487 TRACE ("(%s)\n",debugstr_a(lpszPath));
489 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
490 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
491 return tolower(*lpszPath) - 'a';
492 return -1;
495 /*************************************************************************
496 * PathGetDriveNumberW [SHLWAPI.@]
498 * See PathGetDriveNumberA.
500 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
502 TRACE ("(%s)\n",debugstr_w(lpszPath));
504 if (lpszPath && lpszPath[1] == ':' &&
505 tolowerW(*lpszPath) >= 'a' && tolowerW(*lpszPath) <= 'z')
506 return tolowerW(*lpszPath) - 'a';
507 return -1;
510 /*************************************************************************
511 * PathRemoveFileSpecA [SHLWAPI.@]
513 * Remove the file specification from a path.
515 * PARAMS
516 * lpszPath [O] Path to remove the file spec from
518 * RETURNS
519 * TRUE If the path was valid and modified
520 * FALSE Otherwise
522 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
524 LPSTR lpszFileSpec = lpszPath;
525 BOOL bModified = FALSE;
527 TRACE("(%s)\n",debugstr_a(lpszPath));
529 if(lpszPath)
531 /* Skip directory or UNC path */
532 if (*lpszPath == '\\')
533 lpszFileSpec = ++lpszPath;
534 if (*lpszPath == '\\')
535 lpszFileSpec = ++lpszPath;
537 while (*lpszPath)
539 if(*lpszPath == '\\')
540 lpszFileSpec = lpszPath; /* Skip dir */
541 else if(*lpszPath == ':')
543 lpszFileSpec = ++lpszPath; /* Skip drive */
544 if (*lpszPath == '\\')
545 lpszFileSpec++;
547 if (!(lpszPath = CharNextA(lpszPath)))
548 break;
551 if (*lpszFileSpec)
553 *lpszFileSpec = '\0';
554 bModified = TRUE;
557 return bModified;
560 /*************************************************************************
561 * PathRemoveFileSpecW [SHLWAPI.@]
563 * See PathRemoveFileSpecA.
565 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
567 LPWSTR lpszFileSpec = lpszPath;
568 BOOL bModified = FALSE;
570 TRACE("(%s)\n",debugstr_w(lpszPath));
572 if(lpszPath)
574 /* Skip directory or UNC path */
575 if (*lpszPath == '\\')
576 lpszFileSpec = ++lpszPath;
577 if (*lpszPath == '\\')
578 lpszFileSpec = ++lpszPath;
580 while (*lpszPath)
582 if(*lpszPath == '\\')
583 lpszFileSpec = lpszPath; /* Skip dir */
584 else if(*lpszPath == ':')
586 lpszFileSpec = ++lpszPath; /* Skip drive */
587 if (*lpszPath == '\\')
588 lpszFileSpec++;
590 if (!(lpszPath = CharNextW(lpszPath)))
591 break;
594 if (*lpszFileSpec)
596 *lpszFileSpec = '\0';
597 bModified = TRUE;
600 return bModified;
603 /*************************************************************************
604 * PathStripPathA [SHLWAPI.@]
606 * Remove the initial path from the beginning of a filename
608 * PARAMS
609 * lpszPath [O] Path to remove the initial path from
611 * RETURNS
612 * Nothing.
614 void WINAPI PathStripPathA(LPSTR lpszPath)
616 TRACE("(%s)\n", debugstr_a(lpszPath));
618 if (lpszPath)
620 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
621 if(lpszFileName)
622 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
626 /*************************************************************************
627 * PathStripPathW [SHLWAPI.@]
629 * See PathStripPathA.
631 void WINAPI PathStripPathW(LPWSTR lpszPath)
633 LPWSTR lpszFileName;
635 TRACE("(%s)\n", debugstr_w(lpszPath));
636 lpszFileName = PathFindFileNameW(lpszPath);
637 if(lpszFileName)
638 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
641 /*************************************************************************
642 * PathStripToRootA [SHLWAPI.@]
644 * Reduce a path to its root.
646 * PARAMS
647 * lpszPath [O] the path to reduce
649 * RETURNS
650 * Success: TRUE if the stripped path is a root path
651 * Failure: FALSE if the path cannot be stripped or is NULL
653 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
655 TRACE("(%s)\n", debugstr_a(lpszPath));
657 if (!lpszPath)
658 return FALSE;
659 while(!PathIsRootA(lpszPath))
660 if (!PathRemoveFileSpecA(lpszPath))
661 return FALSE;
662 return TRUE;
665 /*************************************************************************
666 * PathStripToRootW [SHLWAPI.@]
668 * See PathStripToRootA.
670 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
672 TRACE("(%s)\n", debugstr_w(lpszPath));
674 if (!lpszPath)
675 return FALSE;
676 while(!PathIsRootW(lpszPath))
677 if (!PathRemoveFileSpecW(lpszPath))
678 return FALSE;
679 return TRUE;
682 /*************************************************************************
683 * PathRemoveArgsA [SHLWAPI.@]
685 * Strip space seperated arguments from a path.
687 * PARAMS
688 * lpszPath [I] Path to remove arguments from
690 * RETURNS
691 * Nothing.
693 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
695 TRACE("(%s)\n",debugstr_a(lpszPath));
697 if(lpszPath)
699 LPSTR lpszArgs = PathGetArgsA(lpszPath);
700 if (*lpszArgs)
701 lpszArgs[-1] = '\0';
702 else
704 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
705 if(*lpszLastChar == ' ')
706 *lpszLastChar = '\0';
711 /*************************************************************************
712 * PathRemoveArgsW [SHLWAPI.@]
714 * See PathRemoveArgsA.
716 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
718 TRACE("(%s)\n",debugstr_w(lpszPath));
720 if(lpszPath)
722 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
723 if (*lpszArgs)
724 lpszArgs[-1] = '\0';
725 else
727 LPWSTR lpszLastChar = CharPrevW(lpszPath, lpszArgs);
728 if(*lpszLastChar == ' ')
729 *lpszLastChar = '\0';
734 /*************************************************************************
735 * PathRemoveExtensionA [SHLWAPI.@]
737 * Remove the file extension from a path
739 * PARAMS
740 * lpszPath [O] Path to remove the extension from
742 * RETURNS
743 * Nothing.
745 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
747 TRACE("(%s)\n", debugstr_a(lpszPath));
749 if (lpszPath)
751 lpszPath = PathFindExtensionA(lpszPath);
752 *lpszPath = '\0';
756 /*************************************************************************
757 * PathRemoveExtensionW [SHLWAPI.@]
759 * See PathRemoveExtensionA.
761 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
763 TRACE("(%s)\n", debugstr_w(lpszPath));
765 if (lpszPath)
767 lpszPath = PathFindExtensionW(lpszPath);
768 *lpszPath = '\0';
772 /*************************************************************************
773 * PathRemoveBackslashA [SHLWAPI.@]
775 * Remove a trailing backslash from a path.
777 * PARAMS
778 * lpszPath [O] Path to remove backslash from
780 * RETURNS
781 * Success: A pointer to the end of the path
782 * Failure: NULL, if lpszPath is NULL
784 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
786 LPSTR szTemp = NULL;
788 TRACE("(%s)\n", debugstr_a(lpszPath));
790 if(lpszPath)
792 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
793 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
794 *szTemp = '\0';
796 return szTemp;
799 /*************************************************************************
800 * PathRemoveBackslashW [SHLWAPI.@]
802 * See PathRemoveBackslashA.
804 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
806 LPWSTR szTemp = NULL;
808 TRACE("(%s)\n", debugstr_w(lpszPath));
810 if(lpszPath)
812 szTemp = CharPrevW(lpszPath, lpszPath + strlenW(lpszPath));
813 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
814 *szTemp = '\0';
816 return szTemp;
819 /*************************************************************************
820 * PathRemoveBlanksA [SHLWAPI.@]
822 * Remove Spaces from the start and end of a path.
824 * PARAMS
825 * lpszPath [O] Path to strip blanks from
827 * RETURNS
828 * Nothing.
830 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
832 TRACE("(%s)\n", debugstr_a(lpszPath));
834 if(lpszPath && *lpszPath)
836 LPSTR start = lpszPath;
838 while (*lpszPath == ' ')
839 lpszPath = CharNextA(lpszPath);
841 while(*lpszPath)
842 *start++ = *lpszPath++;
844 if (start != lpszPath)
845 while (start[-1] == ' ')
846 start--;
847 *start = '\0';
851 /*************************************************************************
852 * PathRemoveBlanksW [SHLWAPI.@]
854 * See PathRemoveBlanksA.
856 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
858 TRACE("(%s)\n", debugstr_w(lpszPath));
860 if(lpszPath && *lpszPath)
862 LPWSTR start = lpszPath;
864 while (*lpszPath == ' ')
865 lpszPath++;
867 while(*lpszPath)
868 *start++ = *lpszPath++;
870 if (start != lpszPath)
871 while (start[-1] == ' ')
872 start--;
873 *start = '\0';
877 /*************************************************************************
878 * PathQuoteSpacesA [SHLWAPI.@]
880 * Surround a path containg spaces in quotes.
882 * PARAMS
883 * lpszPath [O] Path to quote
885 * RETURNS
886 * Nothing.
888 * NOTES
889 * The path is not changed if it is invalid or has no spaces.
891 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
893 TRACE("(%s)\n", debugstr_a(lpszPath));
895 if(lpszPath && StrChrA(lpszPath,' '))
897 int iLen = strlen(lpszPath) + 1;
899 if (iLen + 2 < MAX_PATH)
901 memmove(lpszPath + 1, lpszPath, iLen);
902 lpszPath[0] = '"';
903 lpszPath[iLen] = '"';
904 lpszPath[iLen + 1] = '\0';
909 /*************************************************************************
910 * PathQuoteSpacesW [SHLWAPI.@]
912 * See PathQuoteSpacesA.
914 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
916 TRACE("(%s)\n", debugstr_w(lpszPath));
918 if(lpszPath && StrChrW(lpszPath,' '))
920 int iLen = strlenW(lpszPath) + 1;
922 if (iLen + 2 < MAX_PATH)
924 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
925 lpszPath[0] = '"';
926 lpszPath[iLen] = '"';
927 lpszPath[iLen + 1] = '\0';
932 /*************************************************************************
933 * PathUnquoteSpacesA [SHLWAPI.@]
935 * Remove quotes ("") from around a path, if present.
937 * PARAMS
938 * lpszPath [O] Path to strip quotes from
940 * RETURNS
941 * Nothing
943 * NOTES
944 * If the path contains a single quote only, an empty string will result.
945 * Otherwise quotes are only removed if they appear at the start and end
946 * of the path.
948 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
950 TRACE("(%s)\n", debugstr_a(lpszPath));
952 if (lpszPath && *lpszPath == '"')
954 DWORD dwLen = strlen(lpszPath) - 1;
956 if (lpszPath[dwLen] == '"')
958 lpszPath[dwLen] = '\0';
959 for (; *lpszPath; lpszPath++)
960 *lpszPath = lpszPath[1];
965 /*************************************************************************
966 * PathUnquoteSpacesW [SHLWAPI.@]
968 * See PathUnquoteSpacesA.
970 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
972 TRACE("(%s)\n", debugstr_w(lpszPath));
974 if (lpszPath && *lpszPath == '"')
976 DWORD dwLen = strlenW(lpszPath) - 1;
978 if (lpszPath[dwLen] == '"')
980 lpszPath[dwLen] = '\0';
981 for (; *lpszPath; lpszPath++)
982 *lpszPath = lpszPath[1];
987 /*************************************************************************
988 * PathParseIconLocationA [SHLWAPI.@]
990 * Parse the location of an icon from a path.
992 * PARAMS
993 * lpszPath [O] The path to parse the icon location from.
995 * RETURNS
996 * Success: The number of the icon
997 * Failure: 0 if the path does not contain an icon location or is NULL
999 * NOTES
1000 * The path has surrounding quotes and spaces removed regardless
1001 * of whether the call succeeds or not.
1003 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1005 int iRet = 0;
1006 LPSTR lpszComma;
1008 TRACE("(%s)\n", debugstr_a(lpszPath));
1010 if (lpszPath)
1012 if ((lpszComma = strchr(lpszPath, ',')))
1014 *lpszComma++ = '\0';
1015 iRet = StrToIntA(lpszComma);
1017 PathUnquoteSpacesA(lpszPath);
1018 PathRemoveBlanksA(lpszPath);
1020 return iRet;
1023 /*************************************************************************
1024 * PathParseIconLocationW [SHLWAPI.@]
1026 * See PathParseIconLocationA.
1028 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1030 int iRet = 0;
1031 LPWSTR lpszComma;
1033 TRACE("(%s)\n", debugstr_w(lpszPath));
1035 if (lpszPath)
1037 if ((lpszComma = StrChrW(lpszPath, ',')))
1039 *lpszComma++ = '\0';
1040 iRet = StrToIntW(lpszComma);
1042 PathUnquoteSpacesW(lpszPath);
1043 PathRemoveBlanksW(lpszPath);
1045 return iRet;
1048 /*************************************************************************
1049 * @ [SHLWAPI.4]
1051 * Unicode version of SHLWAPI_3.
1053 BOOL WINAPI SHLWAPI_4(LPWSTR lpszPath,DWORD dwWhich)
1055 static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
1056 { '.', 'c', 'o', 'm', 0},
1057 { '.', 'e', 'x', 'e', 0},
1058 { '.', 'b', 'a', 't', 0},
1059 { '.', 'l', 'n', 'k', 0},
1060 { '.', 'c', 'm', 'd', 0},
1061 { 0, 0, 0, 0, 0} };
1063 TRACE("(%s,%ld)\n", debugstr_w(lpszPath), dwWhich);
1065 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1066 return FALSE;
1068 if (dwWhich)
1070 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1071 if (!*szExt || dwWhich & 0x40)
1073 size_t iChoose = 0;
1074 int iLen = lstrlenW(lpszPath);
1075 if (iLen > (MAX_PATH - 5))
1076 return FALSE;
1077 while (dwWhich & 0x1 && iChoose < sizeof(pszExts))
1079 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1080 if (PathFileExistsW(lpszPath))
1081 return TRUE;
1082 iChoose++;
1083 dwWhich >>= 1;
1085 *(lpszPath + iLen) = (WCHAR)'\0';
1086 return FALSE;
1089 return PathFileExistsW(lpszPath);
1092 /*************************************************************************
1093 * @ [SHLWAPI.3]
1095 * Determine if a file exists locally and is of an executable type.
1097 * PARAMS
1098 * lpszPath [O] File to search for
1099 * dwWhich [I] Type of executable to search for
1101 * RETURNS
1102 * TRUE If the file was found. lpszPath contains the file name.
1103 * FALSE Otherwise.
1105 * NOTES
1106 * lpszPath is modified in place and must be at least MAX_PATH in length.
1107 * If the function returns FALSE, the path is modified to its orginal state.
1108 * If the given path contains an extension or dwWhich is 0, executable
1109 * extensions are not checked.
1111 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1112 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1113 * their own developers exclusive use. Monopoly, anyone?
1115 BOOL WINAPI SHLWAPI_3(LPSTR lpszPath,DWORD dwWhich)
1117 BOOL bRet = FALSE;
1119 TRACE("(%s,%ld)\n", debugstr_a(lpszPath), dwWhich);
1121 if (lpszPath)
1123 WCHAR szPath[MAX_PATH];
1124 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1125 bRet = SHLWAPI_4(szPath, dwWhich);
1126 if (bRet)
1127 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1129 return bRet;
1132 /*************************************************************************
1133 * SHLWAPI_PathFindInOtherDirs
1135 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1137 static BOOL WINAPI SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1139 static WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1140 static WCHAR szPath[] = { 'P','A','T','H','\0'};
1141 DWORD dwLenPATH;
1142 LPCWSTR lpszCurr;
1143 WCHAR *lpszPATH;
1144 WCHAR buff[MAX_PATH];
1146 TRACE("(%s,%08lx)\n", debugstr_w(lpszFile), dwWhich);
1148 /* Try system directories */
1149 GetSystemDirectoryW(buff, MAX_PATH);
1150 if (!PathAppendW(buff, lpszFile))
1151 return FALSE;
1152 if (SHLWAPI_4(buff, dwWhich))
1154 strcpyW(lpszFile, buff);
1155 return TRUE;
1157 GetWindowsDirectoryW(buff, MAX_PATH);
1158 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1159 return FALSE;
1160 if (SHLWAPI_4(buff, dwWhich))
1162 strcpyW(lpszFile, buff);
1163 return TRUE;
1165 GetWindowsDirectoryW(buff, MAX_PATH);
1166 if (!PathAppendW(buff, lpszFile))
1167 return FALSE;
1168 if (SHLWAPI_4(buff, dwWhich))
1170 strcpyW(lpszFile, buff);
1171 return TRUE;
1173 /* Try dirs listed in %PATH% */
1174 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1176 if (!dwLenPATH || !(lpszPATH = malloc((dwLenPATH + 1) * sizeof (WCHAR))))
1177 return FALSE;
1179 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1180 lpszCurr = lpszPATH;
1181 while (lpszCurr)
1183 LPCWSTR lpszEnd = lpszCurr;
1184 LPWSTR pBuff = buff;
1186 while (*lpszEnd == ' ')
1187 lpszEnd++;
1188 while (*lpszEnd && *lpszEnd != ';')
1189 *pBuff++ = *lpszEnd++;
1190 *pBuff = '\0';
1192 if (*lpszEnd)
1193 lpszCurr = lpszEnd + 1;
1194 else
1195 lpszCurr = NULL; /* Last Path, terminate after this */
1197 if (!PathAppendW(buff, lpszFile))
1198 return FALSE;
1199 if (SHLWAPI_4(buff, dwWhich))
1201 strcpyW(lpszFile, buff);
1202 free(lpszPATH);
1203 return TRUE;
1206 free(lpszPATH);
1207 return FALSE;
1210 /*************************************************************************
1211 * @ [SHLWAPI.5]
1213 * Search a range of paths for a specific type of executable.
1215 * PARAMS
1216 * lpszFile [O] File to search for
1217 * lppszOtherDirs [I] Other directories to look in
1218 * dwWhich [I] Type of executable to search for
1220 * RETURNS
1221 * Success: TRUE. The path to the executable is stored in lpszFile.
1222 * Failure: FALSE. The path to the executable is unchanged.
1224 BOOL WINAPI SHLWAPI_5(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1226 WCHAR szFile[MAX_PATH];
1227 WCHAR buff[MAX_PATH];
1229 TRACE("(%s,%p,%08lx)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1231 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1232 return FALSE;
1234 MultiByteToWideChar(0,0,lpszFile,-1,szFile,MAX_PATH);
1236 /* Search provided directories first */
1237 if (lppszOtherDirs && *lppszOtherDirs)
1239 WCHAR szOther[MAX_PATH];
1240 LPCSTR *lpszOtherPath = lppszOtherDirs;
1242 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1244 MultiByteToWideChar(0,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1245 PathCombineW(buff, szOther, szFile);
1246 if (SHLWAPI_4(buff, dwWhich))
1248 WideCharToMultiByte(0,0,buff,-1,lpszFile,MAX_PATH,0,0);
1249 return TRUE;
1251 lpszOtherPath++;
1254 /* Not found, try system and path dirs */
1255 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1257 WideCharToMultiByte(0,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1258 return TRUE;
1260 return FALSE;
1263 /*************************************************************************
1264 * @ [SHLWAPI.6]
1266 * Unicode version of SHLWAPI_5.
1268 BOOL WINAPI SHLWAPI_6(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1270 WCHAR buff[MAX_PATH];
1272 TRACE("(%s,%p,%08lx)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1274 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1275 return FALSE;
1277 /* Search provided directories first */
1278 if (lppszOtherDirs && *lppszOtherDirs)
1280 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1281 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1283 PathCombineW(buff, *lpszOtherPath, lpszFile);
1284 if (SHLWAPI_4(buff, dwWhich))
1286 strcpyW(lpszFile, buff);
1287 return TRUE;
1289 lpszOtherPath++;
1292 /* Not found, try system and path dirs */
1293 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1296 /*************************************************************************
1297 * PathFindOnPathA [SHLWAPI.@]
1299 * Search a range of paths for an executable.
1301 * PARAMS
1302 * lpszFile [O] File to search for
1303 * lppszOtherDirs [I] Other directories to look in
1305 * RETURNS
1306 * Success: TRUE. The path to the executable is stored in lpszFile.
1307 * Failure: FALSE. The path to the executable is unchanged.
1309 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1311 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1312 return SHLWAPI_5(lpszFile, lppszOtherDirs, 0);
1315 /*************************************************************************
1316 * PathFindOnPathW [SHLWAPI.@]
1318 * See PathFindOnPathA.
1320 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1322 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1323 return SHLWAPI_6(lpszFile,lppszOtherDirs, 0);
1326 /*************************************************************************
1327 * PathCompactPathExA [SHLWAPI.@]
1329 * Compact a path into a given number of characters.
1331 * PARAMS
1332 * lpszDest [O] Destination for compacted path
1333 * lpszPath [I] Source path
1334 * cchMax [I] Maximum size of compacted path
1335 * dwFlags [I] Reserved
1337 * RETURNS
1338 * Success: TRUE. The compacted path is written to lpszDest.
1339 * Failure: FALSE. lpszPath is undefined.
1341 * NOTES
1342 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1343 * The Win32 version of this function contains a bug: When cchMax == 7,
1344 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1345 * implementation.
1347 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1348 * because Win32 will insert a "\" in lpszDest, even if one is
1349 * not present in the original path.
1351 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1352 UINT cchMax, DWORD dwFlags)
1354 BOOL bRet = FALSE;
1356 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1358 if (lpszPath && lpszDest)
1360 WCHAR szPath[MAX_PATH];
1361 WCHAR szDest[MAX_PATH];
1363 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1364 szDest[0] = '\0';
1365 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1366 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1368 return bRet;
1371 /*************************************************************************
1372 * PathCompactPathExW [SHLWAPI.@]
1374 * See PathCompactPathExA.
1376 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1377 UINT cchMax, DWORD dwFlags)
1379 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1380 LPCWSTR lpszFile;
1381 DWORD dwLen, dwFileLen = 0;
1383 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1385 if (!lpszPath)
1386 return FALSE;
1388 if (!lpszDest)
1390 WARN("Invalid lpszDest would crash under Win32!\n");
1391 return FALSE;
1394 *lpszDest = '\0';
1396 if (cchMax < 2)
1397 return TRUE;
1399 dwLen = strlenW(lpszPath) + 1;
1401 if (dwLen < cchMax)
1403 /* Don't need to compact */
1404 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1405 return TRUE;
1408 /* Path must be compacted to fit into lpszDest */
1409 lpszFile = PathFindFileNameW(lpszPath);
1410 dwFileLen = lpszPath + dwLen - lpszFile;
1412 if (dwFileLen == dwLen)
1414 /* No root in psth */
1415 if (cchMax <= 4)
1417 while (--cchMax > 0) /* No room left for anything but ellipses */
1418 *lpszDest++ = '.';
1419 *lpszDest = '\0';
1420 return TRUE;
1422 /* Compact the file name with ellipses at the end */
1423 cchMax -= 4;
1424 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1425 strcpyW(lpszDest + cchMax, szEllipses);
1426 return TRUE;
1428 /* We have a root in the path */
1429 lpszFile--; /* Start compacted filename with the path seperator */
1430 dwFileLen++;
1432 if (dwFileLen + 3 > cchMax)
1434 /* Compact the file name */
1435 if (cchMax <= 4)
1437 while (--cchMax > 0) /* No room left for anything but ellipses */
1438 *lpszDest++ = '.';
1439 *lpszDest = '\0';
1440 return TRUE;
1442 strcpyW(lpszDest, szEllipses);
1443 lpszDest += 3;
1444 cchMax -= 4;
1445 *lpszDest++ = *lpszFile++;
1446 if (cchMax <= 4)
1448 while (--cchMax > 0) /* No room left for anything but ellipses */
1449 *lpszDest++ = '.';
1450 *lpszDest = '\0';
1451 return TRUE;
1453 cchMax -= 4;
1454 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1455 strcpyW(lpszDest + cchMax, szEllipses);
1456 return TRUE;
1459 /* Only the root needs to be Compacted */
1460 dwLen = cchMax - dwFileLen - 3;
1461 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1462 strcpyW(lpszDest + dwLen, szEllipses);
1463 strcpyW(lpszDest + dwLen + 3, lpszFile);
1464 return TRUE;
1467 /*************************************************************************
1468 * PathIsRelativeA [SHLWAPI.@]
1470 * Determine if a path is a relative path.
1472 * PARAMS
1473 * lpszPath [I] Path to check
1475 * RETURNS
1476 * TRUE: The path is relative, or is invalid.
1477 * FALSE: The path is not relative.
1479 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1481 TRACE("(%s)\n",debugstr_a(lpszPath));
1483 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1484 return TRUE;
1485 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1486 return FALSE;
1487 return TRUE;
1490 /*************************************************************************
1491 * PathIsRelativeW [SHLWAPI.@]
1493 * See PathIsRelativeA.
1495 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1497 TRACE("(%s)\n",debugstr_w(lpszPath));
1499 if (!lpszPath || !*lpszPath)
1500 return TRUE;
1501 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1502 return FALSE;
1503 return TRUE;
1506 /*************************************************************************
1507 * PathIsRootA [SHLWAPI.@]
1509 * Determine if a path is a root path.
1511 * PARAMS
1512 * lpszPath [I] Path to check
1514 * RETURNS
1515 * TRUE If lpszPath is valid and a root path,
1516 * FALSE Otherwise
1518 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1520 TRACE("(%s)\n", debugstr_a(lpszPath));
1522 if (lpszPath && *lpszPath)
1524 if (*lpszPath == '\\')
1526 if (!lpszPath[1])
1527 return TRUE; /* \ */
1528 else if (lpszPath[1]=='\\')
1530 BOOL bSeenSlash = FALSE;
1531 lpszPath += 2;
1533 /* Check for UNC root path */
1534 while (*lpszPath)
1536 if (*lpszPath == '\\')
1538 if (bSeenSlash)
1539 return FALSE;
1540 bSeenSlash = TRUE;
1542 lpszPath = CharNextA(lpszPath);
1544 return TRUE;
1547 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1548 return TRUE; /* X:\ */
1550 return FALSE;
1553 /*************************************************************************
1554 * PathIsRootW [SHLWAPI.@]
1556 * See PathIsRootA.
1558 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1560 TRACE("(%s)\n", debugstr_w(lpszPath));
1562 if (lpszPath && *lpszPath)
1564 if (*lpszPath == '\\')
1566 if (!lpszPath[1])
1567 return TRUE; /* \ */
1568 else if (lpszPath[1]=='\\')
1570 BOOL bSeenSlash = FALSE;
1571 lpszPath += 2;
1573 /* Check for UNC root path */
1574 while (*lpszPath)
1576 if (*lpszPath == '\\')
1578 if (bSeenSlash)
1579 return FALSE;
1580 bSeenSlash = TRUE;
1582 lpszPath = CharNextW(lpszPath);
1584 return TRUE;
1587 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1588 return TRUE; /* X:\ */
1590 return FALSE;
1593 /*************************************************************************
1594 * PathIsDirectoryA [SHLWAPI.@]
1596 * Determine if a path is a valid directory
1598 * PARAMS
1599 * lpszPath [I] Path to check.
1601 * RETURNS
1602 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1603 * FALSE if lpszPath is invalid or not a directory.
1605 * NOTES
1606 * Although this function is prototyped as returning a BOOL, it returns
1607 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1609 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1610 *| ...
1612 * will always fail.
1614 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1616 DWORD dwAttr;
1618 TRACE("(%s)\n", debugstr_a(lpszPath));
1620 if (!lpszPath || PathIsUNCServerA(lpszPath))
1621 return FALSE;
1623 if (PathIsUNCServerShareA(lpszPath))
1625 FIXME("UNC Server Share not yet supported - FAILING\n");
1626 return FALSE;
1629 if ((dwAttr = GetFileAttributesA(lpszPath)) == -1u)
1630 return FALSE;
1631 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1634 /*************************************************************************
1635 * PathIsDirectoryW [SHLWAPI.@]
1637 * See PathIsDirectoryA.
1639 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1641 DWORD dwAttr;
1643 TRACE("(%s)\n", debugstr_w(lpszPath));
1645 if (!lpszPath || PathIsUNCServerW(lpszPath))
1646 return FALSE;
1648 if (PathIsUNCServerShareW(lpszPath))
1650 FIXME("UNC Server Share not yet supported - FAILING\n");
1651 return FALSE;
1654 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1u)
1655 return FALSE;
1656 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1659 /*************************************************************************
1660 * PathFileExistsA [SHLWAPI.@]
1662 * Determine if a file exists.
1664 * PARAMS
1665 * lpszPath [I] Path to check
1667 * RETURNS
1668 * TRUE If the file exists and is readable
1669 * FALSE Otherwise
1671 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1673 UINT iPrevErrMode;
1674 DWORD dwAttr;
1676 TRACE("(%s)\n",debugstr_a(lpszPath));
1678 if (!lpszPath)
1679 return FALSE;
1681 iPrevErrMode = SetErrorMode(1);
1682 dwAttr = GetFileAttributesA(lpszPath);
1683 SetErrorMode(iPrevErrMode);
1684 return dwAttr == -1u ? FALSE : TRUE;
1687 /*************************************************************************
1688 * PathFileExistsW [SHLWAPI.@]
1690 * See PathFileExistsA
1692 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1694 UINT iPrevErrMode;
1695 DWORD dwAttr;
1697 TRACE("(%s)\n",debugstr_w(lpszPath));
1699 if (!lpszPath)
1700 return FALSE;
1702 iPrevErrMode = SetErrorMode(1);
1703 dwAttr = GetFileAttributesW(lpszPath);
1704 SetErrorMode(iPrevErrMode);
1705 return dwAttr == -1u ? FALSE : TRUE;
1708 /*************************************************************************
1709 * PathMatchSingleMaskA [internal]
1711 * NOTES
1712 * internal (used by PathMatchSpec)
1714 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1716 while (*name && *mask && *mask!=';')
1718 if (*mask=='*')
1722 if (PathMatchSingleMaskA(name,mask+1)) return 1; /* try substrings */
1723 } while (*name++);
1724 return 0;
1726 if (toupper(*mask)!=toupper(*name) && *mask!='?') return 0;
1727 name = CharNextA(name);
1728 mask = CharNextA(mask);
1730 if (!*name)
1732 while (*mask=='*') mask++;
1733 if (!*mask || *mask==';') return 1;
1735 return 0;
1738 /*************************************************************************
1739 * PathMatchSingleMaskW [internal]
1741 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1743 while (*name && *mask && *mask!=';')
1745 if (*mask=='*')
1749 if (PathMatchSingleMaskW(name,mask+1)) return 1; /* try substrings */
1750 } while (*name++);
1751 return 0;
1753 if (toupperW(*mask)!=toupperW(*name) && *mask!='?') return 0;
1754 name = CharNextW(name);
1755 mask = CharNextW(mask);
1757 if (!*name)
1759 while (*mask=='*') mask++;
1760 if (!*mask || *mask==';') return 1;
1762 return 0;
1765 /*************************************************************************
1766 * PathMatchSpecA [SHLWAPI.@]
1768 * Determine if a path matches one or more search masks.
1770 * PARAMS
1771 * lpszPath [I] Path to check
1772 * lpszMask [I] Search mask(s)
1774 * RETURNS
1775 * TRUE If lpszPath is valid and is matched
1776 * FALSE Otherwise
1778 * NOTES
1779 * Multiple search masks may be given if they are seperated by ";". The
1780 * pattern "*.*" is treated specially in that it matches all paths (for
1781 * backwards compatability with DOS).
1783 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1785 TRACE("%s %s\n", lpszPath, lpszMask);
1787 if (!lstrcmpA( lpszMask, "*.*" )) return 1; /* we don't require a period */
1789 while (*lpszMask)
1791 if (PathMatchSingleMaskA(lpszPath,lpszMask)) return 1; /* helper function */
1792 while (*lpszMask && *lpszMask!=';') lpszMask = CharNextA(lpszMask);
1793 if (*lpszMask==';')
1795 lpszMask++;
1796 while (*lpszMask==' ') lpszMask++; /* masks may be separated by "; " */
1799 return 0;
1802 /*************************************************************************
1803 * PathMatchSpecW [SHLWAPI.@]
1805 * See PathMatchSpecA.
1807 BOOL WINAPI PathMatchSpecW(LPCWSTR name, LPCWSTR mask)
1809 static const WCHAR stemp[] = { '*','.','*',0 };
1810 TRACE("%s %s\n",debugstr_w(name),debugstr_w(mask));
1812 if (!lstrcmpW( mask, stemp )) return 1; /* we don't require a period */
1814 while (*mask)
1816 if (PathMatchSingleMaskW(name,mask)) return 1; /* helper function */
1817 while (*mask && *mask!=';') mask = CharNextW(mask);
1818 if (*mask==';')
1820 mask++;
1821 while (*mask==' ') mask++; /* masks may be separated by "; " */
1824 return 0;
1827 /*************************************************************************
1828 * PathIsSameRootA [SHLWAPI.@]
1830 * Determine if two paths share the same root.
1832 * PARAMS
1833 * lpszPath1 [I] Source path
1834 * lpszPath2 [I] Path to compare with
1836 * RETURNS
1837 * TRUE If both paths are valid and share the same root.
1838 * FALSE If either path is invalid or the paths do not share the same root.
1840 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1842 LPCSTR lpszStart;
1843 int dwLen;
1845 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1847 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1848 return FALSE;
1850 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1851 if (lpszStart - lpszPath1 > dwLen)
1852 return FALSE; /* Paths not common up to length of the root */
1853 return TRUE;
1856 /*************************************************************************
1857 * PathIsSameRootW [SHLWAPI.@]
1859 * See PathIsSameRootA.
1861 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1863 LPCWSTR lpszStart;
1864 int dwLen;
1866 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1868 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1869 return FALSE;
1871 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1872 if (lpszStart - lpszPath1 > dwLen)
1873 return FALSE; /* Paths not common up to length of the root */
1874 return TRUE;
1877 /*************************************************************************
1878 * PathIsContentTypeA [SHLWAPI.@]
1880 * Determine if a file is of a given registered content type.
1882 * PARAMS
1883 * lpszPath [I] file to check
1885 * RETURNS
1886 * TRUE If lpszPath is a given registered content type,
1887 * FALSE Otherwise.
1889 * NOTES
1890 * This function looks up the registered content type for lpszPath. If
1891 * a content type is registered, it is compared (case insensitively) to
1892 * lpszContentType. Only if this matches does the function succeed.
1894 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1896 LPCSTR szExt;
1897 DWORD dwDummy;
1898 char szBuff[MAX_PATH];
1900 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
1902 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
1903 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
1904 REG_NONE, szBuff, &dwDummy) &&
1905 !strcasecmp(lpszContentType, szBuff))
1907 return TRUE;
1909 return FALSE;
1912 /*************************************************************************
1913 * PathIsContentTypeW [SHLWAPI.@]
1915 * See PathIsContentTypeA.
1917 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
1919 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
1920 LPCWSTR szExt;
1921 DWORD dwDummy;
1922 WCHAR szBuff[MAX_PATH];
1924 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
1926 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
1927 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
1928 REG_NONE, szBuff, &dwDummy) &&
1929 !strcmpiW(lpszContentType, szBuff))
1931 return TRUE;
1933 return FALSE;
1936 /*************************************************************************
1937 * PathIsFileSpecA [SHLWAPI.@]
1939 * Determine if a path is a file specification.
1941 * PARAMS
1942 * lpszPath [I] Path to chack
1944 * RETURNS
1945 * TRUE If lpszPath is a file spec (contains no directories).
1946 * FALSE Otherwise.
1948 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
1950 TRACE("(%s)\n", debugstr_a(lpszPath));
1952 if (!lpszPath)
1953 return FALSE;
1955 while (*lpszPath)
1957 if (*lpszPath == '\\' || *lpszPath == ':')
1958 return FALSE;
1959 lpszPath = CharNextA(lpszPath);
1961 return TRUE;
1964 /*************************************************************************
1965 * PathIsFileSpecW [SHLWAPI.@]
1967 * See PathIsFileSpecA.
1969 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
1971 TRACE("(%s)\n", debugstr_w(lpszPath));
1973 if (!lpszPath)
1974 return FALSE;
1976 while (*lpszPath)
1978 if (*lpszPath == '\\' || *lpszPath == ':')
1979 return FALSE;
1980 lpszPath = CharNextW(lpszPath);
1982 return TRUE;
1985 /*************************************************************************
1986 * PathIsPrefixA [SHLWAPI.@]
1988 * Determine if a path is a prefix of another.
1990 * PARAMS
1991 * lpszPrefix [I] Prefix
1992 * lpszPath [i] Path to check
1994 * RETURNS
1995 * TRUE If lpszPath has lpszPrefix as its prefix,
1996 * FALSE If either path is NULL or lpszPrefix is not a prefix
1998 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2000 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2002 if (lpszPrefix && lpszPath &&
2003 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2004 return TRUE;
2005 return FALSE;
2008 /*************************************************************************
2009 * PathIsPrefixW [SHLWAPI.@]
2011 * See PathIsPrefixA.
2013 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2015 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2017 if (lpszPrefix && lpszPath &&
2018 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2019 return TRUE;
2020 return FALSE;
2023 /*************************************************************************
2024 * PathIsSystemFolderA [SHLWAPI.@]
2026 * Determine if a path or file attributes are a system folder.
2028 * PARAMS
2029 * lpszPath [I] Path to check.
2030 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2032 * RETURNS
2033 * TRUE If lpszPath or dwAttrib are a system folder.
2034 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2036 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2038 TRACE("(%s,0x%08lx)\n", debugstr_a(lpszPath), dwAttrib);
2040 if (lpszPath && *lpszPath)
2041 dwAttrib = GetFileAttributesA(lpszPath);
2043 if (dwAttrib == -1u || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2044 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2045 return FALSE;
2046 return TRUE;
2049 /*************************************************************************
2050 * PathIsSystemFolderW [SHLWAPI.@]
2052 * See PathIsSystemFolderA.
2054 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2056 TRACE("(%s,0x%08lx)\n", debugstr_w(lpszPath), dwAttrib);
2058 if (lpszPath && *lpszPath)
2059 dwAttrib = GetFileAttributesW(lpszPath);
2061 if (dwAttrib == -1u || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2062 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2063 return FALSE;
2064 return TRUE;
2067 /*************************************************************************
2068 * PathIsUNCA [SHLWAPI.@]
2070 * Determine if a path is in UNC format.
2072 * PARAMS
2073 * lpszPath [I] Path to check
2075 * RETURNS
2076 * TRUE: The path is UNC.
2077 * FALSE: The path is not UNC or is NULL.
2079 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2081 TRACE("(%s)\n",debugstr_a(lpszPath));
2083 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2084 return TRUE;
2085 return FALSE;
2088 /*************************************************************************
2089 * PathIsUNCW [SHLWAPI.@]
2091 * See PathIsUNCA.
2093 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2095 TRACE("(%s)\n",debugstr_w(lpszPath));
2097 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2098 return TRUE;
2099 return FALSE;
2102 /*************************************************************************
2103 * PathIsUNCServerA [SHLWAPI.@]
2105 * Determine if a path is a UNC server name ("\\SHARENAME").
2107 * PARAMS
2108 * lpszPath [I] Path to check.
2110 * RETURNS
2111 * TRUE If lpszPath is a valid UNC server name.
2112 * FALSE Otherwise.
2114 * NOTES
2115 * This routine is bug compatible with Win32: Server names with a
2116 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2117 * Fixing this bug may break other shlwapi functions!
2119 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2121 TRACE("(%s)\n", debugstr_a(lpszPath));
2123 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2125 while (*lpszPath)
2127 if (*lpszPath == '\\')
2128 return FALSE;
2129 lpszPath = CharNextA(lpszPath);
2131 return TRUE;
2133 return FALSE;
2136 /*************************************************************************
2137 * PathIsUNCServerW [SHLWAPI.@]
2139 * See PathIsUNCServerA.
2141 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2143 TRACE("(%s)\n", debugstr_w(lpszPath));
2145 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2147 while (*lpszPath)
2149 if (*lpszPath == '\\')
2150 return FALSE;
2151 lpszPath = CharNextW(lpszPath);
2153 return TRUE;
2155 return FALSE;
2158 /*************************************************************************
2159 * PathIsUNCServerShareA [SHLWAPI.@]
2161 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2163 * PARAMS
2164 * lpszPath [I] Path to check.
2166 * RETURNS
2167 * TRUE If lpszPath is a valid UNC server share.
2168 * FALSE Otherwise.
2170 * NOTES
2171 * This routine is bug compatible with Win32: Server shares with a
2172 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2173 * Fixing this bug may break other shlwapi functions!
2175 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2177 TRACE("(%s)\n", debugstr_a(lpszPath));
2179 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2181 BOOL bSeenSlash = FALSE;
2182 while (*lpszPath)
2184 if (*lpszPath == '\\')
2186 if (bSeenSlash)
2187 return FALSE;
2188 bSeenSlash = TRUE;
2190 lpszPath = CharNextA(lpszPath);
2192 return bSeenSlash;
2194 return FALSE;
2197 /*************************************************************************
2198 * PathIsUNCServerShareW [SHLWAPI.@]
2200 * See PathIsUNCServerShareA.
2202 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2204 TRACE("(%s)\n", debugstr_w(lpszPath));
2206 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2208 BOOL bSeenSlash = FALSE;
2209 while (*lpszPath)
2211 if (*lpszPath == '\\')
2213 if (bSeenSlash)
2214 return FALSE;
2215 bSeenSlash = TRUE;
2217 lpszPath = CharNextW(lpszPath);
2219 return bSeenSlash;
2221 return FALSE;
2224 /*************************************************************************
2225 * PathCanonicalizeA [SHLWAPI.@]
2227 * Convert a path to its canonical form.
2229 * PARAMS
2230 * lpszBuf [O] Output path
2231 * lpszPath [I] Path to cnonicalize
2233 * RETURNS
2234 * Success: TRUE. lpszBuf contains the output path,
2235 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2237 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2239 BOOL bRet = FALSE;
2241 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2243 if (lpszBuf)
2244 *lpszBuf = '\0';
2246 if (!lpszBuf || !lpszPath)
2247 SetLastError(ERROR_INVALID_PARAMETER);
2248 else
2250 WCHAR szPath[MAX_PATH];
2251 WCHAR szBuff[MAX_PATH];
2252 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2253 bRet = PathCanonicalizeW(szBuff, szPath);
2254 WideCharToMultiByte(0,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2256 return bRet;
2260 /*************************************************************************
2261 * PathCanonicalizeW [SHLWAPI.@]
2263 * See PathCanonicalizeA.
2265 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2267 LPWSTR lpszDst = lpszBuf;
2268 LPCWSTR lpszSrc = lpszPath;
2270 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2272 if (lpszBuf)
2273 *lpszDst = '\0';
2275 if (!lpszBuf || !lpszPath)
2277 SetLastError(ERROR_INVALID_PARAMETER);
2278 return FALSE;
2281 if (!*lpszPath)
2283 *lpszBuf++ = '\\';
2284 *lpszBuf = '\0';
2285 return TRUE;
2288 /* Copy path root */
2289 if (*lpszSrc == '\\')
2291 *lpszDst++ = *lpszSrc++;
2293 else if (*lpszSrc && lpszSrc[1] == ':')
2295 /* X:\ */
2296 *lpszDst++ = *lpszSrc++;
2297 *lpszDst++ = *lpszSrc++;
2298 if (*lpszSrc == '\\')
2299 *lpszDst++ = *lpszSrc++;
2302 /* Canonicalize the rest of the path */
2303 while (*lpszSrc)
2305 if (*lpszSrc == '.')
2307 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2309 lpszSrc += 2; /* Skip .\ */
2311 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2313 /* \.. backs up a directory, over the root if it has no \ following X:.
2314 * .. is ignored if it would remove a UNC server name or inital \\
2316 if (lpszDst != lpszBuf)
2318 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2319 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2320 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2322 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2324 lpszDst -= 2;
2325 while (lpszDst > lpszBuf && *lpszDst != '\\')
2326 lpszDst--;
2327 if (*lpszDst == '\\')
2328 lpszDst++; /* Reset to last '\' */
2329 else
2330 lpszDst = lpszBuf; /* Start path again from new root */
2332 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2333 lpszDst -= 2;
2335 while (lpszDst > lpszBuf && *lpszDst != '\\')
2336 lpszDst--;
2337 if (lpszDst == lpszBuf)
2339 *lpszDst++ = '\\';
2340 lpszSrc++;
2343 lpszSrc += 2; /* Skip .. in src path */
2345 else
2346 *lpszDst++ = *lpszSrc++;
2348 else
2349 *lpszDst++ = *lpszSrc++;
2351 /* Append \ to naked drive specs */
2352 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2353 *lpszDst++ = '\\';
2354 *lpszDst++ = '\0';
2355 return TRUE;
2358 /*************************************************************************
2359 * PathFindNextComponentA [SHLWAPI.@]
2361 * Find the next component in a path.
2363 * PARAMS
2364 * lpszPath [I] Path to find next component in
2366 * RETURNS
2367 * Success: A pointer to the next component, or the end of the string,
2368 * Failure: NULL, If lpszPath is invalid
2370 * NOTES
2371 * A 'component' is either a backslash character (\) or UNC marker (\\).
2372 * Because of this, relative paths (e.g "c:foo") are regarded as having
2373 * only one component.
2375 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2377 LPSTR lpszSlash;
2379 TRACE("(%s)\n", debugstr_a(lpszPath));
2381 if(!lpszPath || !*lpszPath)
2382 return NULL;
2384 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2386 if (lpszSlash[1] == '\\')
2387 lpszSlash++;
2388 return lpszSlash + 1;
2390 return (LPSTR)lpszPath + strlen(lpszPath);
2393 /*************************************************************************
2394 * PathFindNextComponentW [SHLWAPI.@]
2396 * See PathFindNextComponentA.
2398 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2400 LPWSTR lpszSlash;
2402 TRACE("(%s)\n", debugstr_w(lpszPath));
2404 if(!lpszPath || !*lpszPath)
2405 return NULL;
2407 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2409 if (lpszSlash[1] == '\\')
2410 lpszSlash++;
2411 return lpszSlash + 1;
2413 return (LPWSTR)lpszPath + strlenW(lpszPath);
2416 /*************************************************************************
2417 * PathAddExtensionA [SHLWAPI.@]
2419 * Add a file extension to a path
2421 * PARAMS
2422 * lpszPath [O] Path to add extension to
2423 * lpszExtension [I] Extension to add to lpszPath
2425 * RETURNS
2426 * TRUE If the path was modified,
2427 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2428 * extension allready, or the new path length is too big.
2430 * FIXME
2431 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2432 * does not do this, so the behaviour was removed.
2434 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2436 DWORD dwLen;
2438 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2440 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2441 return FALSE;
2443 dwLen = strlen(lpszPath);
2445 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2446 return FALSE;
2448 strcpy(lpszPath + dwLen, lpszExtension);
2449 return TRUE;
2452 /*************************************************************************
2453 * PathAddExtensionW [SHLWAPI.@]
2455 * See PathAddExtensionA.
2457 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2459 DWORD dwLen;
2461 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2463 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2464 return FALSE;
2466 dwLen = strlenW(lpszPath);
2468 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2469 return FALSE;
2471 strcpyW(lpszPath + dwLen, lpszExtension);
2472 return TRUE;
2475 /*************************************************************************
2476 * PathMakePrettyA [SHLWAPI.@]
2478 * Convert an uppercase DOS filename into lowercase.
2480 * PARAMS
2481 * lpszPath [O] Path to convert.
2483 * RETURNS
2484 * TRUE If the path was an uppercase DOS path and was converted,
2485 * FALSE Otherwise.
2487 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2489 LPSTR pszIter = lpszPath;
2491 TRACE("(%s)\n", debugstr_a(lpszPath));
2493 if (!pszIter || !*pszIter)
2494 return FALSE;
2496 while (*pszIter)
2498 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2499 return FALSE; /* Not DOS path */
2500 pszIter++;
2502 pszIter = lpszPath + 1;
2503 while (*pszIter)
2505 *pszIter = tolower(*pszIter);
2506 pszIter++;
2508 return TRUE;
2511 /*************************************************************************
2512 * PathMakePrettyW [SHLWAPI.@]
2514 * See PathMakePrettyA
2516 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2518 LPWSTR pszIter = lpszPath;
2520 TRACE("(%s)\n", debugstr_w(lpszPath));
2522 if (!pszIter || !*pszIter)
2523 return FALSE;
2525 while (*pszIter)
2527 if (islowerW(*pszIter))
2528 return FALSE; /* Not DOS path */
2529 pszIter++;
2531 pszIter = lpszPath + 1;
2532 while (*pszIter)
2534 *pszIter = tolowerW(*pszIter);
2535 pszIter++;
2537 return TRUE;
2540 /*************************************************************************
2541 * PathCommonPrefixA [SHLWAPI.@]
2543 * Determine the length of the common prefix between two paths.
2545 * PARAMS
2546 * lpszFile1 [I] First path for comparison
2547 * lpszFile2 [I] Second path for comparison
2548 * achPath [O] Destination for common prefix string
2550 * RETURNS
2551 * The length of the common prefix. This is 0 if there is no common
2552 * prefix between the paths or if any parameters are invalid. If the prefix
2553 * is non-zero and achPath is not NULL, achPath is filled with the common
2554 * part of the prefix and NUL terminated.
2556 * NOTES
2557 * A common prefix of 2 is always returned as 3. It is thus possible for
2558 * the length returned to be invalid (i.e. Longer than one or both of the
2559 * strings given as parameters). This Win32 behaviour has been implimented
2560 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2561 * To work around this when using this function, always check that the byte
2562 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2564 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2566 int iLen = 0;
2567 LPCSTR lpszIter1 = lpszFile1;
2568 LPCSTR lpszIter2 = lpszFile2;
2570 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2572 if (achPath)
2573 *achPath = '\0';
2575 if (!lpszFile1 || !lpszFile2)
2576 return 0;
2578 /* Handle roots first */
2579 if (PathIsUNCA(lpszFile1))
2581 if (!PathIsUNCA(lpszFile2))
2582 return 0;
2583 lpszIter1 += 2;
2584 lpszIter2 += 2;
2586 else if (PathIsUNCA(lpszFile2))
2587 return 0; /* Know already lpszFile1 is not UNC */
2591 /* Update len */
2592 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2593 (!*lpszIter2 || *lpszIter2 == '\\'))
2594 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2596 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2597 break; /* Strings differ at this point */
2599 lpszIter1++;
2600 lpszIter2++;
2601 } while (1);
2603 if (iLen == 2)
2604 iLen++; /* Feature/Bug compatable with Win32 */
2606 if (iLen && achPath)
2608 memcpy(achPath,lpszFile1,iLen);
2609 achPath[iLen] = '\0';
2611 return iLen;
2614 /*************************************************************************
2615 * PathCommonPrefixW [SHLWAPI.@]
2617 * See PathCommonPrefixA.
2619 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2621 int iLen = 0;
2622 LPCWSTR lpszIter1 = lpszFile1;
2623 LPCWSTR lpszIter2 = lpszFile2;
2625 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2627 if (achPath)
2628 *achPath = '\0';
2630 if (!lpszFile1 || !lpszFile2)
2631 return 0;
2633 /* Handle roots first */
2634 if (PathIsUNCW(lpszFile1))
2636 if (!PathIsUNCW(lpszFile2))
2637 return 0;
2638 lpszIter1 += 2;
2639 lpszIter2 += 2;
2641 else if (PathIsUNCW(lpszFile2))
2642 return 0; /* Know already lpszFile1 is not UNC */
2646 /* Update len */
2647 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2648 (!*lpszIter2 || *lpszIter2 == '\\'))
2649 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2651 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2652 break; /* Strings differ at this point */
2654 lpszIter1++;
2655 lpszIter2++;
2656 } while (1);
2658 if (iLen == 2)
2659 iLen++; /* Feature/Bug compatable with Win32 */
2661 if (iLen && achPath)
2663 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2664 achPath[iLen] = '\0';
2666 return iLen;
2669 /*************************************************************************
2670 * PathCompactPathA [SHLWAPI.@]
2672 * Make a path fit into a given width when printed to a DC.
2674 * PARAMS
2675 * hDc [I] Destination DC
2676 * lpszPath [O] Path to be printed to hDc
2677 * dx [i] Desired width
2679 * RETURNS
2680 * TRUE If the path was modified.
2681 * FALSE Otherwise.
2683 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2685 BOOL bRet = FALSE;
2687 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2689 if (lpszPath)
2691 WCHAR szPath[MAX_PATH];
2692 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2693 bRet = PathCompactPathW(hDC, szPath, dx);
2694 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2696 return bRet;
2699 /*************************************************************************
2700 * PathCompactPathW [SHLWAPI.@]
2702 * See PathCompactPathA.
2704 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2706 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2707 BOOL bRet = TRUE;
2708 HDC hdc = 0;
2709 WCHAR buff[MAX_PATH];
2710 SIZE size;
2711 DWORD dwLen;
2713 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2715 if (!lpszPath)
2716 return bRet;
2718 if (!hDC)
2719 hdc = hDC = GetDC(0);
2721 /* Get the length of the whole path */
2722 dwLen = strlenW(lpszPath);
2723 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2725 if ((UINT)size.cx > dx)
2727 /* Path too big, must reduce it */
2728 LPWSTR sFile;
2729 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2731 sFile = PathFindFileNameW(lpszPath);
2732 if (sFile != lpszPath)
2733 sFile = CharPrevW(lpszPath, sFile);
2735 /* Get the size of ellipses */
2736 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2737 dwEllipsesLen = size.cx;
2738 /* Get the size of the file name */
2739 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2740 dwPathLen = size.cx;
2742 if (sFile != lpszPath)
2744 LPWSTR sPath = sFile;
2745 BOOL bEllipses = FALSE;
2747 /* The path includes a file name. Include as much of the path prior to
2748 * the file name as possible, allowing for the ellipses, e.g:
2749 * c:\some very long path\filename ==> c:\some v...\filename
2751 strncpyW(buff, sFile, MAX_PATH);
2755 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2757 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2758 dwTotalLen += size.cx;
2759 if (dwTotalLen <= dx)
2760 break;
2761 sPath = CharPrevW(lpszPath, sPath);
2762 if (!bEllipses)
2764 bEllipses = TRUE;
2765 sPath = CharPrevW(lpszPath, sPath);
2766 sPath = CharPrevW(lpszPath, sPath);
2768 } while (sPath > lpszPath);
2770 if (sPath > lpszPath)
2772 if (bEllipses)
2774 strcpyW(sPath, szEllipses);
2775 strcpyW(sPath+3, buff);
2777 bRet = TRUE;
2778 goto end;
2780 strcpyW(lpszPath, szEllipses);
2781 strcpyW(lpszPath+3, buff);
2782 bRet = FALSE;
2783 goto end;
2786 /* Trim the path by adding ellipses to the end, e.g:
2787 * A very long file name.txt ==> A very...
2789 dwLen = strlenW(lpszPath);
2791 if (dwLen > MAX_PATH - 3)
2792 dwLen = MAX_PATH - 3;
2793 strncpyW(buff, sFile, dwLen);
2795 do {
2796 dwLen--;
2797 GetTextExtentPointW(hDC, buff, dwLen, &size);
2798 } while (dwLen && size.cx + dwEllipsesLen > dx);
2800 if (!dwLen)
2802 DWORD dwWritten = 0;
2804 dwEllipsesLen /= 3; /* Size of a single '.' */
2806 /* Write as much of the Ellipses string as possible */
2807 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2809 *lpszPath++ = '.';
2810 dwWritten += dwEllipsesLen;
2811 dwLen++;
2813 *lpszPath = '\0';
2814 bRet = FALSE;
2816 else
2818 strcpyW(buff + dwLen, szEllipses);
2819 strcpyW(lpszPath, buff);
2823 end:
2824 if (hdc)
2825 ReleaseDC(0, hdc);
2827 return bRet;
2830 /*************************************************************************
2831 * PathGetCharTypeA [SHLWAPI.@]
2833 * Categorise a character from a file path.
2835 * PARAMS
2836 * ch [I] Character to get the type of
2838 * RETURNS
2839 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2841 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2843 return PathGetCharTypeW(ch);
2846 /*************************************************************************
2847 * PathGetCharTypeW [SHLWAPI.@]
2849 * See PathGetCharTypeA.
2851 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2853 UINT flags = 0;
2855 TRACE("(%d)\n", ch);
2857 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2858 ch == '"' || ch == '|' || ch == '/')
2859 flags = GCT_INVALID; /* Invalid */
2860 else if (ch == '*' || ch=='?')
2861 flags = GCT_WILD; /* Wildchars */
2862 else if ((ch == '\\') || (ch == ':'))
2863 return GCT_SEPARATOR; /* Path separators */
2864 else
2866 if (ch < 126)
2868 if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2869 ch == '.' || ch == '@' || ch == '^' ||
2870 ch == '\'' || ch == 130 || ch == '`')
2871 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2873 else
2874 flags |= GCT_SHORTCHAR; /* Bug compatable with win32 */
2875 flags |= GCT_LFNCHAR; /* Valid for long file names */
2877 return flags;
2880 /*************************************************************************
2881 * SHLWAPI_UseSystemForSystemFolders
2883 * Internal helper for PathMakeSystemFolderW.
2885 static BOOL SHLWAPI_UseSystemForSystemFolders()
2887 static BOOL bCheckedReg = FALSE;
2888 static BOOL bUseSystemForSystemFolders = FALSE;
2890 if (!bCheckedReg)
2892 bCheckedReg = TRUE;
2894 /* Key tells Win what file attributes to use on system folders */
2895 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2896 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2897 "UseSystemForSystemFolders", 0, 0, 0))
2898 bUseSystemForSystemFolders = TRUE;
2900 return bUseSystemForSystemFolders;
2903 /*************************************************************************
2904 * PathMakeSystemFolderA [SHLWAPI.@]
2906 * Set system folder attribute for a path.
2908 * PARAMS
2909 * lpszPath [I] The path to turn into a system folder
2911 * RETURNS
2912 * TRUE If the path was changed to/already was a system folder
2913 * FALSE If the path is invalid or SetFileAttributesA() fails
2915 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
2917 BOOL bRet = FALSE;
2919 TRACE("(%s)\n", debugstr_a(lpszPath));
2921 if (lpszPath && *lpszPath)
2923 WCHAR szPath[MAX_PATH];
2924 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2925 bRet = PathMakeSystemFolderW(szPath);
2927 return bRet;
2930 /*************************************************************************
2931 * PathMakeSystemFolderW [SHLWAPI.@]
2933 * See PathMakeSystemFolderA.
2935 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
2937 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
2938 WCHAR buff[MAX_PATH];
2940 TRACE("(%s)\n", debugstr_w(lpszPath));
2942 if (!lpszPath || !*lpszPath)
2943 return FALSE;
2945 /* If the directory is already a system directory, dont do anything */
2946 GetSystemDirectoryW(buff, MAX_PATH);
2947 if (!strcmpW(buff, lpszPath))
2948 return TRUE;
2950 GetWindowsDirectoryW(buff, MAX_PATH);
2951 if (!strcmpW(buff, lpszPath))
2952 return TRUE;
2954 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
2955 if (SHLWAPI_UseSystemForSystemFolders())
2956 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
2958 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1u)
2959 return FALSE;
2961 /* Change file attributes to system attributes */
2962 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
2963 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
2966 /*************************************************************************
2967 * PathRenameExtensionA [SHLWAPI.@]
2969 * Swap the file extension in a path with another extension.
2971 * PARAMS
2972 * lpszPath [O] Path to swap the extension in
2973 * lpszExt [I] The new extension
2975 * RETURNS
2976 * TRUE if lpszPath was modified,
2977 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
2979 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
2981 LPSTR lpszExtension;
2983 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
2985 lpszExtension = PathFindExtensionA(lpszPath);
2987 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
2988 return FALSE;
2990 strcpy(lpszExtension, lpszExt);
2991 return TRUE;
2994 /*************************************************************************
2995 * PathRenameExtensionW [SHLWAPI.@]
2997 * See PathRenameExtensionA.
2999 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3001 LPWSTR lpszExtension;
3003 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3005 lpszExtension = PathFindExtensionW(lpszPath);
3007 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3008 return FALSE;
3010 strcpyW(lpszExtension, lpszExt);
3011 return TRUE;
3014 /*************************************************************************
3015 * PathSearchAndQualifyA [SHLWAPI.@]
3017 * Determine if a given path is correct and fully qualified.
3019 * PARAMS
3020 * lpszPath [I] Path to check
3021 * lpszBuf [O] Output for correct path
3022 * cchBuf [I] Size of lpszBuf
3024 * RETURNS
3025 * Unknown.
3027 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3029 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3030 return FALSE;
3033 /*************************************************************************
3034 * PathSearchAndQualifyW [SHLWAPI.@]
3036 * See PathSearchAndQualifyA
3038 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3040 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3041 return FALSE;
3044 /*************************************************************************
3045 * PathSkipRootA [SHLWAPI.@]
3047 * Return the portion of a path following the drive letter or mount point.
3049 * PARAMS
3050 * lpszPath [I] The path to skip on
3052 * RETURNS
3053 * Success: A pointer to the next character after the root.
3054 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3056 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3058 TRACE("(%s)\n", debugstr_a(lpszPath));
3060 if (!lpszPath || !*lpszPath)
3061 return NULL;
3063 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3065 /* Network share: skip share server and mount point */
3066 lpszPath += 2;
3067 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3068 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3069 lpszPath++;
3070 return (LPSTR)lpszPath;
3073 if (IsDBCSLeadByte(*lpszPath))
3074 return NULL;
3076 /* Check x:\ */
3077 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3078 return (LPSTR)lpszPath + 3;
3079 return NULL;
3082 /*************************************************************************
3083 * PathSkipRootW [SHLWAPI.@]
3085 * See PathSkipRootA.
3087 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3089 TRACE("(%s)\n", debugstr_w(lpszPath));
3091 if (!lpszPath || !*lpszPath)
3092 return NULL;
3094 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3096 /* Network share: skip share server and mount point */
3097 lpszPath += 2;
3098 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3099 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3100 lpszPath++;
3101 return (LPWSTR)lpszPath;
3104 /* Check x:\ */
3105 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3106 return (LPWSTR)lpszPath + 3;
3107 return NULL;
3110 /*************************************************************************
3111 * PathCreateFromUrlA [SHLWAPI.@]
3113 * Create a path from a URL
3115 * PARAMS
3116 * lpszUrl [I] URL to convert into a path
3117 * lpszPath [O] Output buffer for the resulting Path
3118 * pcchPath [I] Length of lpszPath
3119 * dwFlags [I] Flags controlling the conversion
3121 * RETURNS
3122 * Success: S_OK. lpszPath contains the URL in path format,
3123 * Failure: An HRESULT error code such as E_INVALIDARG.
3125 HRESULT WINAPI PathCreateFromUrlA(LPCSTR lpszUrl, LPSTR lpszPath,
3126 LPDWORD pcchPath, DWORD dwFlags)
3128 LPSTR pszPathPart;
3129 TRACE("(%s,%p,%p,0x%08lx)\n", debugstr_a(lpszUrl), lpszPath, pcchPath, dwFlags);
3131 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3132 return E_INVALIDARG;
3134 pszPathPart = StrChrA(lpszUrl, ':');
3135 if ((((pszPathPart - lpszUrl) == 1) && isalpha(*lpszUrl)) ||
3136 !lstrcmpA(lpszUrl, "file:"))
3138 return UrlUnescapeA(pszPathPart, lpszPath, pcchPath, dwFlags);
3140 /* extracts thing prior to : in pszURL and checks against:
3141 * https
3142 * shell
3143 * local
3144 * about - if match returns E_INVALIDARG
3147 return E_INVALIDARG;
3150 /*************************************************************************
3151 * PathCreateFromUrlW [SHLWAPI.@]
3153 * See PathCreateFromUrlA.
3155 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR lpszUrl, LPWSTR lpszPath,
3156 LPDWORD pcchPath, DWORD dwFlags)
3158 static const WCHAR stemp[] = { 'f','i','l','e',':','/','/',0 };
3159 LPWSTR pwszPathPart;
3160 HRESULT hr;
3162 TRACE("(%s,%p,%p,0x%08lx)\n", debugstr_w(lpszUrl), lpszPath, pcchPath, dwFlags);
3164 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3165 return E_INVALIDARG;
3167 /* Path of the form file://... */
3168 if (!strncmpW(lpszUrl, stemp, 7))
3170 lpszUrl += 7;
3172 /* Path of the form file:... */
3173 else if (!strncmpW(lpszUrl, stemp, 5))
3175 lpszUrl += 5;
3178 /* Ensure that path is of the form c:... or c|... */
3179 if (lpszUrl[1] != ':' && lpszUrl[1] != '|' && isalphaW(*lpszUrl))
3180 return E_INVALIDARG;
3182 hr = UrlUnescapeW(lpszUrl, lpszPath, pcchPath, dwFlags);
3183 if (lpszPath[1] == '|')
3184 lpszPath[1] = ':';
3186 for (pwszPathPart = lpszPath; *pwszPathPart; pwszPathPart++)
3187 if (*pwszPathPart == '/')
3188 *pwszPathPart = '\\';
3190 TRACE("Returning %s\n",debugstr_w(lpszPath));
3192 return hr;
3195 /*************************************************************************
3196 * PathRelativePathToA [SHLWAPI.@]
3198 * Create a relative path from one path to another.
3200 * PARAMS
3201 * lpszPath [O] Destination for relative path
3202 * lpszFrom [I] Source path
3203 * dwAttrFrom [I] File attribute of source path
3204 * lpszTo [I] Destination path
3205 * dwAttrTo [I] File attributes of destination path
3207 * RETURNS
3208 * TRUE If a relative path can be formed. lpszPath contains the new path
3209 * FALSE If the paths are not relavtive or any parameters are invalid
3211 * NOTES
3212 * lpszTo should be at least MAX_PATH in length.
3214 * Calling this function with relative paths for lpszFrom or lpszTo may
3215 * give erroneous results.
3217 * The Win32 version of this function contains a bug where the lpszTo string
3218 * may be referenced 1 byte beyond the end of the string. As a result random
3219 * garbage may be written to the output path, depending on what lies beyond
3220 * the last byte of the string. This bug occurs because of the behaviour of
3221 * PathCommonPrefix() (see notes for that function), and no workaround seems
3222 * possible with Win32.
3224 * This bug has been fixed here, so for example the relative path from "\\"
3225 * to "\\" is correctly determined as "." in this implementation.
3227 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3228 LPCSTR lpszTo, DWORD dwAttrTo)
3230 BOOL bRet = FALSE;
3232 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_a(lpszFrom),
3233 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3235 if(lpszPath && lpszFrom && lpszTo)
3237 WCHAR szPath[MAX_PATH];
3238 WCHAR szFrom[MAX_PATH];
3239 WCHAR szTo[MAX_PATH];
3240 MultiByteToWideChar(0,0,lpszFrom,-1,szFrom,MAX_PATH);
3241 MultiByteToWideChar(0,0,lpszTo,-1,szTo,MAX_PATH);
3242 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3243 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3245 return bRet;
3248 /*************************************************************************
3249 * PathRelativePathToW [SHLWAPI.@]
3251 * See PathRelativePathToA.
3253 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3254 LPCWSTR lpszTo, DWORD dwAttrTo)
3256 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3257 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3258 WCHAR szFrom[MAX_PATH];
3259 WCHAR szTo[MAX_PATH];
3260 DWORD dwLen;
3262 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_w(lpszFrom),
3263 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3265 if(!lpszPath || !lpszFrom || !lpszTo)
3266 return FALSE;
3268 *lpszPath = '\0';
3269 strncpyW(szFrom, lpszFrom, MAX_PATH);
3270 strncpyW(szTo, lpszTo, MAX_PATH);
3272 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3273 PathRemoveFileSpecW(szFrom);
3274 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3275 PathRemoveFileSpecW(szTo);
3277 /* Paths can only be relative if they have a common root */
3278 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3279 return FALSE;
3281 /* Strip off lpszFrom components to the root, by adding "..\" */
3282 lpszFrom = szFrom + dwLen;
3283 if (!*lpszFrom)
3285 lpszPath[0] = '.';
3286 lpszPath[1] = '\0';
3288 if (*lpszFrom == '\\')
3289 lpszFrom++;
3291 while (*lpszFrom)
3293 lpszFrom = PathFindNextComponentW(lpszFrom);
3294 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3297 /* From the root add the components of lpszTo */
3298 lpszTo += dwLen;
3299 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3300 * this function.
3302 if (*lpszTo && lpszTo[-1])
3304 if (*lpszTo != '\\')
3305 lpszTo--;
3306 dwLen = strlenW(lpszPath);
3307 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3309 *lpszPath = '\0';
3310 return FALSE;
3312 strcpyW(lpszPath + dwLen, lpszTo);
3314 return TRUE;
3317 /*************************************************************************
3318 * PathUnmakeSystemFolderA [SHLWAPI.@]
3320 * Remove the system folder attributes from a path.
3322 * PARAMS
3323 * lpszPath [I] The path to remove attributes from
3325 * RETURNS
3326 * Success: TRUE.
3327 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3328 * SetFileAttributesA() fails.
3330 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3332 DWORD dwAttr;
3334 TRACE("(%s)\n", debugstr_a(lpszPath));
3336 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == -1u ||
3337 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3338 return FALSE;
3340 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3341 return SetFileAttributesA(lpszPath, dwAttr);
3344 /*************************************************************************
3345 * PathUnmakeSystemFolderW [SHLWAPI.@]
3347 * See PathUnmakeSystemFolderA.
3349 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3351 DWORD dwAttr;
3353 TRACE("(%s)\n", debugstr_w(lpszPath));
3355 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == -1u ||
3356 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3357 return FALSE;
3359 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3360 return SetFileAttributesW(lpszPath, dwAttr);
3364 /*************************************************************************
3365 * PathSetDlgItemPathA [SHLWAPI.@]
3367 * Set the text of a dialog item to a path, shrinking the path to fit
3368 * if it is too big for the item.
3370 * PARAMS
3371 * hDlg [I] Dialog handle
3372 * id [I] ID of item in the dialog
3373 * lpszPath [I] Path to set as the items text
3375 * RETURNS
3376 * Nothing.
3378 * NOTES
3379 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3380 * window text is erased).
3382 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3384 WCHAR szPath[MAX_PATH];
3386 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3388 if (lpszPath)
3389 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3390 else
3391 szPath[0] = '\0';
3392 PathSetDlgItemPathW(hDlg, id, szPath);
3395 /*************************************************************************
3396 * PathSetDlgItemPathW [SHLWAPI.@]
3398 * See PathSetDlgItemPathA.
3400 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3402 WCHAR path[MAX_PATH + 1];
3403 HWND hwItem;
3404 RECT rect;
3405 HDC hdc;
3406 HGDIOBJ hPrevObj;
3408 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3410 if (!(hwItem = GetDlgItem(hDlg, id)))
3411 return;
3413 if (lpszPath)
3414 strncpyW(path, lpszPath, sizeof(path));
3415 else
3416 path[0] = '\0';
3418 GetClientRect(hwItem, &rect);
3419 hdc = GetDC(hDlg);
3420 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3422 if (hPrevObj)
3424 PathCompactPathW(hdc, path, rect.right);
3425 SelectObject(hdc, hPrevObj);
3428 ReleaseDC(hDlg, hdc);
3429 SetWindowTextW(hwItem, path);
3432 /*************************************************************************
3433 * PathIsNetworkPathA [SHLWAPI.@]
3435 * Determine if the given path is a network path.
3437 * PARAMS
3438 * lpszPath [I] Path to check
3440 * RETURNS
3441 * TRUE If lpszPath is a UNC share or mapped network drive, or
3442 * FALSE If lpszPath is a local drive or cannot be determined
3444 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3446 DWORD dwDriveNum;
3448 TRACE("(%s)\n",debugstr_a(lpszPath));
3450 if (!lpszPath)
3451 return FALSE;
3452 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3453 return TRUE;
3454 dwDriveNum = PathGetDriveNumberA(lpszPath);
3455 if (dwDriveNum == -1u)
3456 return FALSE;
3457 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3458 return pIsNetDrive(dwDriveNum);
3461 /*************************************************************************
3462 * PathIsNetworkPathW [SHLWAPI.@]
3464 * See PathIsNetworkPathA.
3466 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3468 DWORD dwDriveNum;
3470 TRACE("(%s)\n", debugstr_w(lpszPath));
3472 if (!lpszPath)
3473 return FALSE;
3474 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3475 return TRUE;
3476 dwDriveNum = PathGetDriveNumberW(lpszPath);
3477 if (dwDriveNum == -1u)
3478 return FALSE;
3479 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3480 return pIsNetDrive(dwDriveNum);
3483 /*************************************************************************
3484 * PathIsLFNFileSpecA [SHLWAPI.@]
3486 * Determine if the given path is a long file name
3488 * PARAMS
3489 * lpszPath [I] Path to check
3491 * RETURNS
3492 * TRUE If path is a long file name,
3493 * FALSE If path is a valid DOS 8.3 file name
3495 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3497 DWORD dwNameLen = 0, dwExtLen = 0;
3499 TRACE("(%s)\n",debugstr_a(lpszPath));
3501 if (!lpszPath)
3502 return FALSE;
3504 while (*lpszPath)
3506 if (*lpszPath == ' ')
3507 return TRUE; /* DOS names cannot have spaces */
3508 if (*lpszPath == '.')
3510 if (dwExtLen)
3511 return TRUE; /* DOS names have only one dot */
3512 dwExtLen = 1;
3514 else if (dwExtLen)
3516 dwExtLen++;
3517 if (dwExtLen > 4)
3518 return TRUE; /* DOS extensions are <= 3 chars*/
3520 else
3522 dwNameLen++;
3523 if (dwNameLen > 8)
3524 return TRUE; /* DOS names are <= 8 chars */
3526 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3528 return FALSE; /* Valid DOS path */
3531 /*************************************************************************
3532 * PathIsLFNFileSpecW [SHLWAPI.@]
3534 * See PathIsLFNFileSpecA.
3536 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3538 DWORD dwNameLen = 0, dwExtLen = 0;
3540 TRACE("(%s)\n",debugstr_w(lpszPath));
3542 if (!lpszPath)
3543 return FALSE;
3545 while (*lpszPath)
3547 if (*lpszPath == ' ')
3548 return TRUE; /* DOS names cannot have spaces */
3549 if (*lpszPath == '.')
3551 if (dwExtLen)
3552 return TRUE; /* DOS names have only one dot */
3553 dwExtLen = 1;
3555 else if (dwExtLen)
3557 dwExtLen++;
3558 if (dwExtLen > 4)
3559 return TRUE; /* DOS extensions are <= 3 chars*/
3561 else
3563 dwNameLen++;
3564 if (dwNameLen > 8)
3565 return TRUE; /* DOS names are <= 8 chars */
3567 lpszPath++;
3569 return FALSE; /* Valid DOS path */
3572 /*************************************************************************
3573 * PathIsDirectoryEmptyA [SHLWAPI.@]
3575 * Determine if a given directory is empty.
3577 * PARAMS
3578 * lpszPath [I] Directory to check
3580 * RETURNS
3581 * TRUE If the directory exists and contains no files,
3582 * FALSE Otherwise
3584 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3586 BOOL bRet = FALSE;
3588 TRACE("(%s)\n",debugstr_a(lpszPath));
3590 if (lpszPath)
3592 WCHAR szPath[MAX_PATH];
3593 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3594 bRet = PathIsDirectoryEmptyW(szPath);
3596 return bRet;
3599 /*************************************************************************
3600 * PathIsDirectoryEmptyW [SHLWAPI.@]
3602 * See PathIsDirectoryEmptyA.
3604 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3606 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3607 WCHAR szSearch[MAX_PATH];
3608 DWORD dwLen;
3609 HANDLE hfind;
3610 BOOL retVal = FALSE;
3611 WIN32_FIND_DATAW find_data;
3613 TRACE("(%s)\n",debugstr_w(lpszPath));
3615 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3616 return FALSE;
3618 strncpyW(szSearch, lpszPath, MAX_PATH);
3619 PathAddBackslashW(szSearch);
3620 dwLen = strlenW(szSearch);
3621 if (dwLen > MAX_PATH - 4)
3622 return FALSE;
3624 strcpyW(szSearch + dwLen, szAllFiles);
3625 hfind = FindFirstFileW(szSearch, &find_data);
3627 if (hfind != INVALID_HANDLE_VALUE &&
3628 find_data.cFileName[0] == '.' &&
3629 find_data.cFileName[1] == '.')
3631 /* The only directory entry should be the parent */
3632 if (!FindNextFileW(hfind, &find_data))
3633 retVal = TRUE;
3634 FindClose(hfind);
3636 return retVal;
3640 /*************************************************************************
3641 * PathFindSuffixArrayA [SHLWAPI.@]
3643 * Find a suffix string in an array of suffix strings
3645 * PARAMS
3646 * lpszSuffix [I] Suffix string to search for
3647 * lppszArray [I] Array of suffix strings to search
3648 * dwCount [I] Number of elements in lppszArray
3650 * RETURNS
3651 * Success: The index of the position of lpszSuffix in lppszArray
3652 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3654 * NOTES
3655 * The search is case sensitive.
3656 * The match is made against the end of the suffix string, so for example:
3657 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3659 int WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3661 DWORD dwLen;
3662 int dwRet = 0;
3664 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3666 if (lpszSuffix && lppszArray && dwCount > 0)
3668 dwLen = strlen(lpszSuffix);
3670 while (dwRet < dwCount)
3672 DWORD dwCompareLen = strlen(*lppszArray);
3673 if (dwCompareLen < dwLen)
3675 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3676 return dwRet; /* Found */
3678 dwRet++;
3679 lppszArray++;
3682 return 0;
3685 /*************************************************************************
3686 * PathFindSuffixArrayW [SHLWAPI.@]
3688 * See PathFindSuffixArrayA.
3690 int WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3692 DWORD dwLen;
3693 int dwRet = 0;
3695 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3697 if (lpszSuffix && lppszArray && dwCount > 0)
3699 dwLen = strlenW(lpszSuffix);
3701 while (dwRet < dwCount)
3703 DWORD dwCompareLen = strlenW(*lppszArray);
3704 if (dwCompareLen < dwLen)
3706 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3707 return dwRet; /* Found */
3709 dwRet++;
3710 lppszArray++;
3713 return 0;
3716 /*************************************************************************
3717 * PathUndecorateA [SHLWAPI.@]
3719 * Undecorate a file path
3721 * PARAMS
3722 * lpszPath [O] Path to undecorate
3724 * RETURNS
3725 * Nothing
3727 * NOTES
3728 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3730 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3732 TRACE("(%s)\n",debugstr_a(lpszPath));
3734 if (lpszPath)
3736 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3737 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3739 LPSTR lpszSkip = lpszExt - 2;
3740 if (*lpszSkip == '[')
3741 lpszSkip++; /* [] (no number) */
3742 else
3743 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3744 lpszSkip--;
3745 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3747 /* remove the [n] */
3748 lpszSkip--;
3749 while (*lpszExt)
3750 *lpszSkip++ = *lpszExt++;
3751 *lpszSkip = '\0';
3757 /*************************************************************************
3758 * PathUndecorateW [SHLWAPI.@]
3760 * See PathUndecorateA.
3762 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3764 TRACE("(%s)\n",debugstr_w(lpszPath));
3766 if (lpszPath)
3768 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3769 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3771 LPWSTR lpszSkip = lpszExt - 2;
3772 if (*lpszSkip == '[')
3773 lpszSkip++; /* [] (no number) */
3774 else
3775 while (lpszSkip > lpszPath && isdigitW(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';