Removed obsolete INT_Int31Handler.
[wine/multimedia.git] / dlls / shlwapi / path.c
blobbd50ea54bf69d55138c70add4e4c509397cb4d51
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 * liszFile [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. lpszFile 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 PathFindOnPath) 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 sFile.
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.
1346 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1347 * because Win32 will insert a \ in the compact filename, even if one is
1348 * not present in the original path.
1350 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1351 UINT cchMax, DWORD dwFlags)
1353 BOOL bRet = FALSE;
1355 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1357 if (lpszPath && lpszDest)
1359 WCHAR szPath[MAX_PATH];
1360 WCHAR szDest[MAX_PATH];
1362 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
1363 szDest[0] = '\0';
1364 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1365 WideCharToMultiByte(0,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1367 return bRet;
1370 /*************************************************************************
1371 * PathCompactPathExW [SHLWAPI.@]
1373 * See PathCompactPathExA.
1375 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1376 UINT cchMax, DWORD dwFlags)
1378 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1379 LPCWSTR lpszFile;
1380 DWORD dwLen, dwFileLen = 0;
1382 TRACE("(%p,%s,%d,0x%08lx)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1384 if (!lpszPath)
1385 return FALSE;
1387 if (!lpszDest)
1389 WARN("Invalid lpszDest would crash under Win32!\n");
1390 return FALSE;
1393 *lpszDest = '\0';
1395 if (cchMax < 2)
1396 return TRUE;
1398 dwLen = strlenW(lpszPath) + 1;
1400 if (dwLen < cchMax)
1402 /* Don't need to compact */
1403 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1404 return TRUE;
1407 /* Path must be compacted to fit into lpszDest */
1408 lpszFile = PathFindFileNameW(lpszPath);
1409 dwFileLen = lpszPath + dwLen - lpszFile;
1411 if (dwFileLen == dwLen)
1413 /* No root in psth */
1414 if (cchMax <= 4)
1416 while (--cchMax > 0) /* No room left for anything but ellipses */
1417 *lpszDest++ = '.';
1418 *lpszDest = '\0';
1419 return TRUE;
1421 /* Compact the file name with ellipses at the end */
1422 cchMax -= 4;
1423 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1424 strcpyW(lpszDest + cchMax, szEllipses);
1425 return TRUE;
1427 /* We have a root in the path */
1428 lpszFile--; /* Start compacted filename with the path seperator */
1429 dwFileLen++;
1431 if (dwFileLen + 3 > cchMax)
1433 /* Compact the file name */
1434 if (cchMax <= 4)
1436 while (--cchMax > 0) /* No room left for anything but ellipses */
1437 *lpszDest++ = '.';
1438 *lpszDest = '\0';
1439 return TRUE;
1441 strcpyW(lpszDest, szEllipses);
1442 lpszDest += 3;
1443 cchMax -= 4;
1444 *lpszDest++ = *lpszFile++;
1445 if (cchMax <= 4)
1447 while (--cchMax > 0) /* No room left for anything but ellipses */
1448 *lpszDest++ = '.';
1449 *lpszDest = '\0';
1450 return TRUE;
1452 cchMax -= 4;
1453 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1454 strcpyW(lpszDest + cchMax, szEllipses);
1455 return TRUE;
1458 /* Only the root needs to be Compacted */
1459 dwLen = cchMax - dwFileLen - 3;
1460 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1461 strcpyW(lpszDest + dwLen, szEllipses);
1462 strcpyW(lpszDest + dwLen + 3, lpszFile);
1463 return TRUE;
1466 /*************************************************************************
1467 * PathIsRelativeA [SHLWAPI.@]
1469 * Determine if a path is a relative path.
1471 * PARAMS
1472 * lpszPath [I] Path to check
1474 * RETURNS
1475 * TRUE: The path is relative, or is invalid.
1476 * FALSE: The path is not relative.
1478 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1480 TRACE("(%s)\n",debugstr_a(lpszPath));
1482 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1483 return TRUE;
1484 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1485 return FALSE;
1486 return TRUE;
1489 /*************************************************************************
1490 * PathIsRelativeW [SHLWAPI.@]
1492 * See PathIsRelativeA.
1494 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1496 TRACE("(%s)\n",debugstr_w(lpszPath));
1498 if (!lpszPath || !*lpszPath)
1499 return TRUE;
1500 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1501 return FALSE;
1502 return TRUE;
1505 /*************************************************************************
1506 * PathIsRootA [SHLWAPI.@]
1508 * Determine if a path is a root path.
1510 * PARAMS
1511 * lpszPath [I] Path to check
1513 * RETURNS
1514 * TRUE If lpszPath is valid and a root path
1515 * FALSE Otherwise
1517 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1519 TRACE("(%s)\n", debugstr_a(lpszPath));
1521 if (lpszPath && *lpszPath)
1523 if (*lpszPath == '\\')
1525 if (!lpszPath[1])
1526 return TRUE; /* \ */
1527 else if (lpszPath[1]=='\\')
1529 BOOL bSeenSlash = FALSE;
1530 lpszPath += 2;
1532 /* Check for UNC root path */
1533 while (*lpszPath)
1535 if (*lpszPath == '\\')
1537 if (bSeenSlash)
1538 return FALSE;
1539 bSeenSlash = TRUE;
1541 lpszPath = CharNextA(lpszPath);
1543 return TRUE;
1546 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1547 return TRUE; /* X:\ */
1549 return FALSE;
1552 /*************************************************************************
1553 * PathIsRootW [SHLWAPI.@]
1555 * See PathIsRootA.
1557 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1559 TRACE("(%s)\n", debugstr_w(lpszPath));
1561 if (lpszPath && *lpszPath)
1563 if (*lpszPath == '\\')
1565 if (!lpszPath[1])
1566 return TRUE; /* \ */
1567 else if (lpszPath[1]=='\\')
1569 BOOL bSeenSlash = FALSE;
1570 lpszPath += 2;
1572 /* Check for UNC root path */
1573 while (*lpszPath)
1575 if (*lpszPath == '\\')
1577 if (bSeenSlash)
1578 return FALSE;
1579 bSeenSlash = TRUE;
1581 lpszPath = CharNextW(lpszPath);
1583 return TRUE;
1586 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1587 return TRUE; /* X:\ */
1589 return FALSE;
1592 /*************************************************************************
1593 * PathIsDirectoryA [SHLWAPI.@]
1595 * Determine if a path is a valid directory
1597 * PARAMS
1598 * lpszPath [I] Path to check.
1600 * RETURNS
1601 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1602 * FALSE if lpszPath is invalid or not a directory.
1604 * NOTES
1605 * Although this function is prototyped as returning a BOOL, it returns
1606 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1608 * if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1609 * ...
1611 * will always fail.
1613 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1615 DWORD dwAttr;
1617 TRACE("(%s)\n", debugstr_a(lpszPath));
1619 if (!lpszPath || PathIsUNCServerA(lpszPath))
1620 return FALSE;
1622 if (PathIsUNCServerShareA(lpszPath))
1624 FIXME("UNC Server Share not yet supported - FAILING\n");
1625 return FALSE;
1628 if ((dwAttr = GetFileAttributesA(lpszPath)) == -1u)
1629 return FALSE;
1630 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1633 /*************************************************************************
1634 * PathIsDirectoryW [SHLWAPI.@]
1636 * See PathIsDirectoryA.
1638 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1640 DWORD dwAttr;
1642 TRACE("(%s)\n", debugstr_w(lpszPath));
1644 if (!lpszPath || PathIsUNCServerW(lpszPath))
1645 return FALSE;
1647 if (PathIsUNCServerShareW(lpszPath))
1649 FIXME("UNC Server Share not yet supported - FAILING\n");
1650 return FALSE;
1653 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1u)
1654 return FALSE;
1655 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1658 /*************************************************************************
1659 * PathFileExistsA [SHLWAPI.@]
1661 * Determine if a file exists.
1663 * PARAMS
1664 * lpszPath [I] Path to check
1666 * RETURNS
1667 * TRUE If the file exists and is readable
1668 * FALSE Otherwise
1670 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1672 UINT iPrevErrMode;
1673 DWORD dwAttr;
1675 TRACE("(%s)\n",debugstr_a(lpszPath));
1677 if (!lpszPath)
1678 return FALSE;
1680 iPrevErrMode = SetErrorMode(1);
1681 dwAttr = GetFileAttributesA(lpszPath);
1682 SetErrorMode(iPrevErrMode);
1683 return dwAttr == -1u ? FALSE : TRUE;
1686 /*************************************************************************
1687 * PathFileExistsW [SHLWAPI.@]
1689 * See PathFileExistsA
1691 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1693 UINT iPrevErrMode;
1694 DWORD dwAttr;
1696 TRACE("(%s)\n",debugstr_w(lpszPath));
1698 if (!lpszPath)
1699 return FALSE;
1701 iPrevErrMode = SetErrorMode(1);
1702 dwAttr = GetFileAttributesW(lpszPath);
1703 SetErrorMode(iPrevErrMode);
1704 return dwAttr == -1u ? FALSE : TRUE;
1707 /*************************************************************************
1708 * PathMatchSingleMaskA [internal]
1710 * NOTES
1711 * internal (used by PathMatchSpec)
1713 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1715 while (*name && *mask && *mask!=';')
1717 if (*mask=='*')
1721 if (PathMatchSingleMaskA(name,mask+1)) return 1; /* try substrings */
1722 } while (*name++);
1723 return 0;
1725 if (toupper(*mask)!=toupper(*name) && *mask!='?') return 0;
1726 name = CharNextA(name);
1727 mask = CharNextA(mask);
1729 if (!*name)
1731 while (*mask=='*') mask++;
1732 if (!*mask || *mask==';') return 1;
1734 return 0;
1737 /*************************************************************************
1738 * PathMatchSingleMaskW [internal]
1740 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1742 while (*name && *mask && *mask!=';')
1744 if (*mask=='*')
1748 if (PathMatchSingleMaskW(name,mask+1)) return 1; /* try substrings */
1749 } while (*name++);
1750 return 0;
1752 if (toupperW(*mask)!=toupperW(*name) && *mask!='?') return 0;
1753 name = CharNextW(name);
1754 mask = CharNextW(mask);
1756 if (!*name)
1758 while (*mask=='*') mask++;
1759 if (!*mask || *mask==';') return 1;
1761 return 0;
1764 /*************************************************************************
1765 * PathMatchSpecA [SHLWAPI.@]
1767 * Determine if a path matches one or more search masks.
1769 * PARAMS
1770 * lpszPath [I] Path to check
1771 * lpszMask [I} Search mask(s)
1773 * RETURNS
1774 * TRUE If lpszPath is valid and is matched
1775 * FALSE Otherwise
1777 * NOTES
1778 * Multiple search masks may be given if they are seperated by ";". The
1779 * pattern "*.*" is treated specially in that it matches all paths (for
1780 * backwards compatability with DOS).
1782 BOOL WINAPI PathMatchSpecA(LPCSTR name, LPCSTR mask)
1784 TRACE("%s %s\n",name,mask);
1786 if (!lstrcmpA( mask, "*.*" )) return 1; /* we don't require a period */
1788 while (*mask)
1790 if (PathMatchSingleMaskA(name,mask)) return 1; /* helper function */
1791 while (*mask && *mask!=';') mask = CharNextA(mask);
1792 if (*mask==';')
1794 mask++;
1795 while (*mask==' ') mask++; /* masks may be separated by "; " */
1798 return 0;
1801 /*************************************************************************
1802 * PathMatchSpecW [SHLWAPI.@]
1804 * See PathMatchSpecA.
1806 BOOL WINAPI PathMatchSpecW(LPCWSTR name, LPCWSTR mask)
1808 static const WCHAR stemp[] = { '*','.','*',0 };
1809 TRACE("%s %s\n",debugstr_w(name),debugstr_w(mask));
1811 if (!lstrcmpW( mask, stemp )) return 1; /* we don't require a period */
1813 while (*mask)
1815 if (PathMatchSingleMaskW(name,mask)) return 1; /* helper function */
1816 while (*mask && *mask!=';') mask = CharNextW(mask);
1817 if (*mask==';')
1819 mask++;
1820 while (*mask==' ') mask++; /* masks may be separated by "; " */
1823 return 0;
1826 /*************************************************************************
1827 * PathIsSameRootA [SHLWAPI.@]
1829 * Determine if two paths share the same root.
1831 * PARAMS
1832 * lpszPath1 [I] Source path
1833 * lpszPath2 [I] Path to compare with
1835 * RETURNS
1836 * TRUE If both paths are valid and share the same root.
1837 * FALSE If either path is invalid or the paths do not share the same root.
1839 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1841 LPCSTR lpszStart;
1842 int dwLen;
1844 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1846 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1847 return FALSE;
1849 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1850 if (lpszStart - lpszPath1 > dwLen)
1851 return FALSE; /* Paths not common up to length of the root */
1852 return TRUE;
1855 /*************************************************************************
1856 * PathIsSameRootW [SHLWAPI.@]
1858 * See PathIsSameRootA.
1860 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1862 LPCWSTR lpszStart;
1863 int dwLen;
1865 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1867 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1868 return FALSE;
1870 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1871 if (lpszStart - lpszPath1 > dwLen)
1872 return FALSE; /* Paths not common up to length of the root */
1873 return TRUE;
1876 /*************************************************************************
1877 * PathIsContentTypeA [SHLWAPI.@]
1879 * Determine if a file is of a given registered content type.
1881 * PARAMS
1882 * lpszPath [I] file to check
1884 * RETURNS
1885 * TRUE If lpszPath is a given registered content type
1886 * FALSE Otherwise.
1888 * NOTES
1889 * This function looks up the registered content type for lpszPath. If
1890 * a content type is registered, it is compared (case insensitively) to
1891 * lpszContentType. Only if this matches does the function succeed.
1893 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
1895 LPCSTR szExt;
1896 DWORD dwDummy;
1897 char szBuff[MAX_PATH];
1899 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
1901 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
1902 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
1903 REG_NONE, szBuff, &dwDummy) &&
1904 !strcasecmp(lpszContentType, szBuff))
1906 return TRUE;
1908 return FALSE;
1911 /*************************************************************************
1912 * PathIsContentTypeW [SHLWAPI.@]
1914 * See PathIsContentTypeA.
1916 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
1918 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
1919 LPCWSTR szExt;
1920 DWORD dwDummy;
1921 WCHAR szBuff[MAX_PATH];
1923 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
1925 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
1926 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
1927 REG_NONE, szBuff, &dwDummy) &&
1928 !strcmpiW(lpszContentType, szBuff))
1930 return TRUE;
1932 return FALSE;
1935 /*************************************************************************
1936 * PathIsFileSpecA [SHLWAPI.@]
1938 * Determine if a path is a file specification.
1940 * PARAMS
1941 * lpszPath [I] Path to chack
1943 * RETURNS
1944 * TRUE If lpszPath is a file spec (contains no directories).
1945 * FALSE Otherwise.
1947 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
1949 TRACE("(%s)\n", debugstr_a(lpszPath));
1951 if (!lpszPath)
1952 return FALSE;
1954 while (*lpszPath)
1956 if (*lpszPath == '\\' || *lpszPath == ':')
1957 return FALSE;
1958 lpszPath = CharNextA(lpszPath);
1960 return TRUE;
1963 /*************************************************************************
1964 * PathIsFileSpecW [SHLWAPI.@]
1966 * See PathIsFileSpecA.
1968 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
1970 TRACE("(%s)\n", debugstr_w(lpszPath));
1972 if (!lpszPath)
1973 return FALSE;
1975 while (*lpszPath)
1977 if (*lpszPath == '\\' || *lpszPath == ':')
1978 return FALSE;
1979 lpszPath = CharNextW(lpszPath);
1981 return TRUE;
1984 /*************************************************************************
1985 * PathIsPrefixA [SHLWAPI.@]
1987 * Determine if a path is a prefix of another.
1989 * PARAMS
1990 * lpszPrefix [I] Prefix
1991 * lpszPath [i] Path to check
1993 * RETURNS
1994 * TRUE If lpszPath has lpszPrefix as its prefix
1995 * FALSE If either path is NULL or lpszPrefix is not a prefix
1997 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
1999 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2001 if (lpszPrefix && lpszPath &&
2002 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2003 return TRUE;
2004 return FALSE;
2007 /*************************************************************************
2008 * PathIsPrefixW [SHLWAPI.@]
2010 * See PathIsPrefixA.
2012 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2014 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2016 if (lpszPrefix && lpszPath &&
2017 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2018 return TRUE;
2019 return FALSE;
2022 /*************************************************************************
2023 * PathIsSystemFolderA [SHLWAPI.@]
2025 * Determine if a path or file attributes are a system folder.
2027 * PARAMS
2028 * lpszPath [I] Path to check.
2029 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2031 * RETURNS
2032 * TRUE If lpszPath or dwAttrib are a system folder.
2033 * FALSE If GetFileAttributesA fails or neither parameter is a system folder.
2035 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2037 TRACE("(%s,0x%08lx)\n", debugstr_a(lpszPath), dwAttrib);
2039 if (lpszPath && *lpszPath)
2040 dwAttrib = GetFileAttributesA(lpszPath);
2042 if (dwAttrib == -1u || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2043 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2044 return FALSE;
2045 return TRUE;
2048 /*************************************************************************
2049 * PathIsSystemFolderW [SHLWAPI.@]
2051 * See PathIsSystemFolderA.
2053 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2055 TRACE("(%s,0x%08lx)\n", debugstr_w(lpszPath), dwAttrib);
2057 if (lpszPath && *lpszPath)
2058 dwAttrib = GetFileAttributesW(lpszPath);
2060 if (dwAttrib == -1u || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2061 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2062 return FALSE;
2063 return TRUE;
2066 /*************************************************************************
2067 * PathIsUNCA [SHLWAPI.@]
2069 * Determine if a path is in UNC format.
2071 * PARAMS
2072 * lpszPath [I] Path to check
2074 * RETURNS
2075 * TRUE: The path is UNC.
2076 * FALSE: The path is not UNC or is NULL.
2078 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2080 TRACE("(%s)\n",debugstr_a(lpszPath));
2082 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2083 return TRUE;
2084 return FALSE;
2087 /*************************************************************************
2088 * PathIsUNCW [SHLWAPI.@]
2090 * See PathIsUNCA.
2092 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2094 TRACE("(%s)\n",debugstr_w(lpszPath));
2096 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
2097 return TRUE;
2098 return FALSE;
2101 /*************************************************************************
2102 * PathIsUNCServerA [SHLWAPI.@]
2104 * Determine if a path is a UNC server name ("\\SHARENAME").
2106 * PARAMS
2107 * lpszPath [I] Path to check.
2109 * RETURNS
2110 * TRUE If lpszPath is a valid UNC server name.
2111 * FALSE Otherwise.
2113 * NOTES
2114 * This routine is bug compatible with Win32: Server names with a
2115 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2116 * Fixing this bug may break other shlwapi functions!
2118 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2120 TRACE("(%s)\n", debugstr_a(lpszPath));
2122 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2124 while (*lpszPath)
2126 if (*lpszPath == '\\')
2127 return FALSE;
2128 lpszPath = CharNextA(lpszPath);
2130 return TRUE;
2132 return FALSE;
2135 /*************************************************************************
2136 * PathIsUNCServerW [SHLWAPI.@]
2138 * See PathIsUNCServerA.
2140 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2142 TRACE("(%s)\n", debugstr_w(lpszPath));
2144 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2146 while (*lpszPath)
2148 if (*lpszPath == '\\')
2149 return FALSE;
2150 lpszPath = CharNextW(lpszPath);
2152 return TRUE;
2154 return FALSE;
2157 /*************************************************************************
2158 * PathIsUNCServerShareA [SHLWAPI.@]
2160 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2162 * PARAMS
2163 * lpszPath [I] Path to check.
2165 * RETURNS
2166 * TRUE If lpszPath is a valid UNC server share.
2167 * FALSE Otherwise.
2169 * NOTES
2170 * This routine is bug compatible with Win32: Server shares with a
2171 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2172 * Fixing this bug may break other shlwapi functions!
2174 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2176 TRACE("(%s)\n", debugstr_a(lpszPath));
2178 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2180 BOOL bSeenSlash = FALSE;
2181 while (*lpszPath)
2183 if (*lpszPath == '\\')
2185 if (bSeenSlash)
2186 return FALSE;
2187 bSeenSlash = TRUE;
2189 lpszPath = CharNextA(lpszPath);
2191 return bSeenSlash;
2193 return FALSE;
2196 /*************************************************************************
2197 * PathIsUNCServerShareW [SHLWAPI.@]
2199 * See PathIsUNCServerShareA.
2201 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2203 TRACE("(%s)\n", debugstr_w(lpszPath));
2205 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2207 BOOL bSeenSlash = FALSE;
2208 while (*lpszPath)
2210 if (*lpszPath == '\\')
2212 if (bSeenSlash)
2213 return FALSE;
2214 bSeenSlash = TRUE;
2216 lpszPath = CharNextW(lpszPath);
2218 return bSeenSlash;
2220 return FALSE;
2223 /*************************************************************************
2224 * PathCanonicalizeA [SHLWAPI.@]
2226 * Convert a path to its canonical form.
2228 * PARAMS
2229 * lpszBuf [O] Output path
2230 * lpszPath [I] Path to cnonicalize
2232 * RETURNS
2233 * Success: TRUE. lpszBuf contains the output path
2234 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2236 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2238 BOOL bRet = FALSE;
2240 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2242 if (lpszBuf)
2243 *lpszBuf = '\0';
2245 if (!lpszBuf || !lpszPath)
2246 SetLastError(ERROR_INVALID_PARAMETER);
2247 else
2249 WCHAR szPath[MAX_PATH];
2250 WCHAR szBuff[MAX_PATH];
2251 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2252 bRet = PathCanonicalizeW(szBuff, szPath);
2253 WideCharToMultiByte(0,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2255 return bRet;
2259 /*************************************************************************
2260 * PathCanonicalizeW [SHLWAPI.@]
2262 * See PathCanonicalizeA.
2264 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2266 LPWSTR lpszDst = lpszBuf;
2267 LPCWSTR lpszSrc = lpszPath;
2269 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2271 if (lpszBuf)
2272 *lpszDst = '\0';
2274 if (!lpszBuf || !lpszPath)
2276 SetLastError(ERROR_INVALID_PARAMETER);
2277 return FALSE;
2280 if (!*lpszPath)
2282 *lpszBuf++ = '\\';
2283 *lpszBuf = '\0';
2284 return TRUE;
2287 /* Copy path root */
2288 if (*lpszSrc == '\\')
2290 *lpszDst++ = *lpszSrc++;
2292 else if (*lpszSrc && lpszSrc[1] == ':')
2294 /* X:\ */
2295 *lpszDst++ = *lpszSrc++;
2296 *lpszDst++ = *lpszSrc++;
2297 if (*lpszSrc == '\\')
2298 *lpszDst++ = *lpszSrc++;
2301 /* Canonicalize the rest of the path */
2302 while (*lpszSrc)
2304 if (*lpszSrc == '.')
2306 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2308 lpszSrc += 2; /* Skip .\ */
2310 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2312 /* \.. backs up a directory, over the root if it has no \ following X:.
2313 * .. is ignored if it would remove a UNC server name or inital \\
2315 if (lpszDst != lpszBuf)
2317 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2318 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2319 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2321 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2323 lpszDst -= 2;
2324 while (lpszDst > lpszBuf && *lpszDst != '\\')
2325 lpszDst--;
2326 if (*lpszDst == '\\')
2327 lpszDst++; /* Reset to last '\' */
2328 else
2329 lpszDst = lpszBuf; /* Start path again from new root */
2331 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2332 lpszDst -= 2;
2334 while (lpszDst > lpszBuf && *lpszDst != '\\')
2335 lpszDst--;
2336 if (lpszDst == lpszBuf)
2338 *lpszDst++ = '\\';
2339 lpszSrc++;
2342 lpszSrc += 2; /* Skip .. in src path */
2344 else
2345 *lpszDst++ = *lpszSrc++;
2347 else
2348 *lpszDst++ = *lpszSrc++;
2350 /* Append \ to naked drive specs */
2351 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2352 *lpszDst++ = '\\';
2353 *lpszDst++ = '\0';
2354 return TRUE;
2357 /*************************************************************************
2358 * PathFindNextComponentA [SHLWAPI.@]
2360 * Find the next component in a path.
2362 * PARAMS
2363 * lpszPath [I] Path to find next component in
2365 * RETURNS
2366 * Success: A pointer to the next component, or the end of the string
2367 * Failure: NULL, If lpszPath is invalid
2369 * NOTES
2370 * A 'component' is either a backslash character (\) or UNC marker (\\).
2371 * Because of this, relative paths (e.g "c:foo") are regarded as having
2372 * only one component.
2374 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2376 LPSTR lpszSlash;
2378 TRACE("(%s)\n", debugstr_a(lpszPath));
2380 if(!lpszPath || !*lpszPath)
2381 return NULL;
2383 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2385 if (lpszSlash[1] == '\\')
2386 lpszSlash++;
2387 return lpszSlash + 1;
2389 return (LPSTR)lpszPath + strlen(lpszPath);
2392 /*************************************************************************
2393 * PathFindNextComponentW [SHLWAPI.@]
2395 * See PathFindNextComponentA.
2397 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2399 LPWSTR lpszSlash;
2401 TRACE("(%s)\n", debugstr_w(lpszPath));
2403 if(!lpszPath || !*lpszPath)
2404 return NULL;
2406 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2408 if (lpszSlash[1] == '\\')
2409 lpszSlash++;
2410 return lpszSlash + 1;
2412 return (LPWSTR)lpszPath + strlenW(lpszPath);
2415 /*************************************************************************
2416 * PathAddExtensionA [SHLWAPI.@]
2418 * Add a file extension to a path
2420 * PARAMS
2421 * lpszPath [O] Path to add extension to
2422 * lpszExtension [I} Extension to add to lpszPath
2424 * RETURNS
2425 * TRUE If the path was modified
2426 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2427 * extension allready, or the new path length is too big.
2429 * FIXME
2430 * What version of shlwapi.dll adds "exe" if pszExtension is NULL? Win2k
2431 * does not do this, so the behaviour was removed.
2433 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2435 DWORD dwLen;
2437 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2439 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2440 return FALSE;
2442 dwLen = strlen(lpszPath);
2444 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2445 return FALSE;
2447 strcpy(lpszPath + dwLen, lpszExtension);
2448 return TRUE;
2451 /*************************************************************************
2452 * PathAddExtensionW [SHLWAPI.@]
2454 * See PathAddExtensionA.
2456 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2458 DWORD dwLen;
2460 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2462 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2463 return FALSE;
2465 dwLen = strlenW(lpszPath);
2467 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2468 return FALSE;
2470 strcpyW(lpszPath + dwLen, lpszExtension);
2471 return TRUE;
2474 /*************************************************************************
2475 * PathMakePrettyA [SHLWAPI.@]
2477 * Convert an uppercase DOS filename into lowercase.
2479 * PARAMS
2480 * lpszPath [O] Path to convert.
2482 * RETURNS
2483 * TRUE If the path was an uppercase DOS path and was converted
2484 * FALSE Otherwise.
2486 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2488 LPSTR pszIter = lpszPath;
2490 TRACE("(%s)\n", debugstr_a(lpszPath));
2492 if (!pszIter || !*pszIter)
2493 return FALSE;
2495 while (*pszIter)
2497 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2498 return FALSE; /* Not DOS path */
2499 pszIter++;
2501 pszIter = lpszPath + 1;
2502 while (*pszIter)
2504 *pszIter = tolower(*pszIter);
2505 pszIter++;
2507 return TRUE;
2510 /*************************************************************************
2511 * PathMakePrettyW [SHLWAPI.@]
2513 * See PathMakePrettyA
2515 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2517 LPWSTR pszIter = lpszPath;
2519 TRACE("(%s)\n", debugstr_w(lpszPath));
2521 if (!pszIter || !*pszIter)
2522 return FALSE;
2524 while (*pszIter)
2526 if (islowerW(*pszIter))
2527 return FALSE; /* Not DOS path */
2528 pszIter++;
2530 pszIter = lpszPath + 1;
2531 while (*pszIter)
2533 *pszIter = tolowerW(*pszIter);
2534 pszIter++;
2536 return TRUE;
2539 /*************************************************************************
2540 * PathCommonPrefixA [SHLWAPI.@]
2542 * Determine the length of the common prefix between two paths.
2544 * PARAMS
2545 * lpszFile1 [I] First path for comparason
2546 * lpszFile2 [I] Second path for comparason
2547 * achPath [O] Destination for common prefix string
2549 * RETURNS
2550 * The length of the common prefix. This is 0 if there is no common
2551 * prefix between the paths or if any parameters are invalid. If the prefix
2552 * is non-zero and achPath is not NULL, achPath is filled with the common
2553 * part of the prefix and NUL terminated.
2555 * NOTES
2556 * A common prefix of 2 is always returned as 3. It is thus possible for
2557 * the length returned to be invalid (i.e. Longer than one or both of the
2558 * strings given as parameters). This Win32 behaviour has been implimented
2559 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2560 * To work around this when using this function, always check that the byte
2561 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2563 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2565 int iLen = 0;
2566 LPCSTR lpszIter1 = lpszFile1;
2567 LPCSTR lpszIter2 = lpszFile2;
2569 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2571 if (achPath)
2572 *achPath = '\0';
2574 if (!lpszFile1 || !lpszFile2)
2575 return 0;
2577 /* Handle roots first */
2578 if (PathIsUNCA(lpszFile1))
2580 if (!PathIsUNCA(lpszFile2))
2581 return 0;
2582 lpszIter1 += 2;
2583 lpszIter2 += 2;
2585 else if (PathIsUNCA(lpszFile2))
2586 return 0; /* Know already lpszFile1 is not UNC */
2590 /* Update len */
2591 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2592 (!*lpszIter2 || *lpszIter2 == '\\'))
2593 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2595 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2596 break; /* Strings differ at this point */
2598 lpszIter1++;
2599 lpszIter2++;
2600 } while (1);
2602 if (iLen == 2)
2603 iLen++; /* Feature/Bug compatable with Win32 */
2605 if (iLen && achPath)
2607 memcpy(achPath,lpszFile1,iLen);
2608 achPath[iLen] = '\0';
2610 return iLen;
2613 /*************************************************************************
2614 * PathCommonPrefixW [SHLWAPI.@]
2616 * See PathCommonPrefixA.
2618 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2620 int iLen = 0;
2621 LPCWSTR lpszIter1 = lpszFile1;
2622 LPCWSTR lpszIter2 = lpszFile2;
2624 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2626 if (achPath)
2627 *achPath = '\0';
2629 if (!lpszFile1 || !lpszFile2)
2630 return 0;
2632 /* Handle roots first */
2633 if (PathIsUNCW(lpszFile1))
2635 if (!PathIsUNCW(lpszFile2))
2636 return 0;
2637 lpszIter1 += 2;
2638 lpszIter2 += 2;
2640 else if (PathIsUNCW(lpszFile2))
2641 return 0; /* Know already lpszFile1 is not UNC */
2645 /* Update len */
2646 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2647 (!*lpszIter2 || *lpszIter2 == '\\'))
2648 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2650 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2651 break; /* Strings differ at this point */
2653 lpszIter1++;
2654 lpszIter2++;
2655 } while (1);
2657 if (iLen == 2)
2658 iLen++; /* Feature/Bug compatable with Win32 */
2660 if (iLen && achPath)
2662 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2663 achPath[iLen] = '\0';
2665 return iLen;
2668 /*************************************************************************
2669 * PathCompactPathA [SHLWAPI.@]
2671 * Make a path fit into a given width when printed to a DC.
2673 * PARAMS
2674 * hDc [I] Destination DC
2675 * lpszPath [O] Path to be printed to hDc
2676 * dx [i] Desired width
2678 * RETURNS
2679 * TRUE If the path was modified.
2680 * FALSE Otherwise.
2682 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2684 BOOL bRet = FALSE;
2686 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2688 if (lpszPath)
2690 WCHAR szPath[MAX_PATH];
2691 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2692 bRet = PathCompactPathW(hDC, szPath, dx);
2693 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2695 return bRet;
2698 /*************************************************************************
2699 * PathCompactPathW [SHLWAPI.@]
2701 * See PathCompactPathA.
2703 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2705 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2706 BOOL bRet = TRUE;
2707 HDC hdc = 0;
2708 WCHAR buff[MAX_PATH];
2709 SIZE size;
2710 DWORD dwLen;
2712 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2714 if (!lpszPath)
2715 return bRet;
2717 if (!hDC)
2718 hdc = hDC = GetDC(0);
2720 /* Get the length of the whole path */
2721 dwLen = strlenW(lpszPath);
2722 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2724 if ((UINT)size.cx > dx)
2726 /* Path too big, must reduce it */
2727 LPWSTR sFile;
2728 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2730 sFile = PathFindFileNameW(lpszPath);
2731 if (sFile != lpszPath)
2732 sFile = CharPrevW(lpszPath, sFile);
2734 /* Get the size of ellipses */
2735 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2736 dwEllipsesLen = size.cx;
2737 /* Get the size of the file name */
2738 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2739 dwPathLen = size.cx;
2741 if (sFile != lpszPath)
2743 LPWSTR sPath = sFile;
2744 BOOL bEllipses = FALSE;
2746 /* The path includes a file name. Include as much of the path prior to
2747 * the file name as possible, allowing for the ellipses, e.g:
2748 * c:\some very long path\filename ==> c:\some v...\filename
2750 strncpyW(buff, sFile, MAX_PATH);
2754 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2756 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2757 dwTotalLen += size.cx;
2758 if (dwTotalLen <= dx)
2759 break;
2760 sPath = CharPrevW(lpszPath, sPath);
2761 if (!bEllipses)
2763 bEllipses = TRUE;
2764 sPath = CharPrevW(lpszPath, sPath);
2765 sPath = CharPrevW(lpszPath, sPath);
2767 } while (sPath > lpszPath);
2769 if (sPath > lpszPath)
2771 if (bEllipses)
2773 strcpyW(sPath, szEllipses);
2774 strcpyW(sPath+3, buff);
2776 bRet = TRUE;
2777 goto end;
2779 strcpyW(lpszPath, szEllipses);
2780 strcpyW(lpszPath+3, buff);
2781 bRet = FALSE;
2782 goto end;
2785 /* Trim the path by adding ellipses to the end, e.g:
2786 * A very long file name.txt ==> A very...
2788 dwLen = strlenW(lpszPath);
2790 if (dwLen > MAX_PATH - 3)
2791 dwLen = MAX_PATH - 3;
2792 strncpyW(buff, sFile, dwLen);
2794 do {
2795 dwLen--;
2796 GetTextExtentPointW(hDC, buff, dwLen, &size);
2797 } while (dwLen && size.cx + dwEllipsesLen > dx);
2799 if (!dwLen)
2801 DWORD dwWritten = 0;
2803 dwEllipsesLen /= 3; /* Size of a single '.' */
2805 /* Write as much of the Ellipses string as possible */
2806 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2808 *lpszPath++ = '.';
2809 dwWritten += dwEllipsesLen;
2810 dwLen++;
2812 *lpszPath = '\0';
2813 bRet = FALSE;
2815 else
2817 strcpyW(buff + dwLen, szEllipses);
2818 strcpyW(lpszPath, buff);
2822 end:
2823 if (hdc)
2824 ReleaseDC(0, hdc);
2826 return bRet;
2829 /*************************************************************************
2830 * PathGetCharTypeA [SHLWAPI.@]
2832 * Categorise a character from a file path.
2834 * PARAMS
2835 * ch [I] Character to get the type of
2837 * RETURNS
2838 * A set of GCT_ bit flags (from shlwapi.h) indicating the character type.
2840 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2842 return PathGetCharTypeW(ch);
2845 /*************************************************************************
2846 * PathGetCharTypeW [SHLWAPI.@]
2848 * See PathGetCharTypeA.
2850 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2852 UINT flags = 0;
2854 TRACE("(%d)\n", ch);
2856 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2857 ch == '"' || ch == '|' || ch == '/')
2858 flags = GCT_INVALID; /* Invalid */
2859 else if (ch == '*' || ch=='?')
2860 flags = GCT_WILD; /* Wildchars */
2861 else if ((ch == '\\') || (ch == ':'))
2862 return GCT_SEPARATOR; /* Path separators */
2863 else
2865 if (ch < 126)
2867 if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2868 ch == '.' || ch == '@' || ch == '^' ||
2869 ch == '\'' || ch == 130 || ch == '`')
2870 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2872 else
2873 flags |= GCT_SHORTCHAR; /* Bug compatable with win32 */
2874 flags |= GCT_LFNCHAR; /* Valid for long file names */
2876 return flags;
2879 /*************************************************************************
2880 * SHLWAPI_UseSystemForSystemFolders
2882 * Internal helper for PathMakeSystemFolderW.
2884 static BOOL SHLWAPI_UseSystemForSystemFolders()
2886 static BOOL bCheckedReg = FALSE;
2887 static BOOL bUseSystemForSystemFolders = FALSE;
2889 if (!bCheckedReg)
2891 bCheckedReg = TRUE;
2893 /* Key tells Win what file attributes to use on system folders */
2894 if (SHGetValueA(HKEY_LOCAL_MACHINE,
2895 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
2896 "UseSystemForSystemFolders", 0, 0, 0))
2897 bUseSystemForSystemFolders = TRUE;
2899 return bUseSystemForSystemFolders;
2902 /*************************************************************************
2903 * PathMakeSystemFolderA [SHLWAPI.@]
2905 * Set system folder attribute for a path.
2907 * PARAMS
2908 * lpszPath [I] The path to turn into a system folder
2910 * RETURNS
2911 * TRUE If the path was changed to/already was a system folder
2912 * FALSE If the path is invalid or SetFileAttributesA fails
2914 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
2916 BOOL bRet = FALSE;
2918 TRACE("(%s)\n", debugstr_a(lpszPath));
2920 if (lpszPath && *lpszPath)
2922 WCHAR szPath[MAX_PATH];
2923 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
2924 bRet = PathMakeSystemFolderW(szPath);
2926 return bRet;
2929 /*************************************************************************
2930 * PathMakeSystemFolderW [SHLWAPI.@]
2932 * See PathMakeSystemFolderA.
2934 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
2936 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
2937 WCHAR buff[MAX_PATH];
2939 TRACE("(%s)\n", debugstr_w(lpszPath));
2941 if (!lpszPath || !*lpszPath)
2942 return FALSE;
2944 /* If the directory is already a system directory, dont do anything */
2945 GetSystemDirectoryW(buff, MAX_PATH);
2946 if (!strcmpW(buff, lpszPath))
2947 return TRUE;
2949 GetWindowsDirectoryW(buff, MAX_PATH);
2950 if (!strcmpW(buff, lpszPath))
2951 return TRUE;
2953 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
2954 if (SHLWAPI_UseSystemForSystemFolders())
2955 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
2957 if ((dwAttr = GetFileAttributesW(lpszPath)) == -1u)
2958 return FALSE;
2960 /* Change file attributes to system attributes */
2961 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
2962 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
2965 /*************************************************************************
2966 * PathRenameExtensionA [SHLWAPI.@]
2968 * Swap the file extension in a path with another extension.
2970 * PARAMS
2971 * pszPath [O] Path to swap the extension in
2972 * pszExt [I] The new extension
2974 * RETURNS
2975 * TRUE if pszPath was modified
2976 * FALSE if pszPath or pszExt is NULL, or the new path is too long
2978 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
2980 LPSTR lpszExtension;
2982 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
2984 lpszExtension = PathFindExtensionA(lpszPath);
2986 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
2987 return FALSE;
2989 strcpy(lpszExtension, lpszExt);
2990 return TRUE;
2993 /*************************************************************************
2994 * PathRenameExtensionW [SHLWAPI.@]
2996 * See PathRenameExtensionA.
2998 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3000 LPWSTR lpszExtension;
3002 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3004 lpszExtension = PathFindExtensionW(lpszPath);
3006 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3007 return FALSE;
3009 strcpyW(lpszExtension, lpszExt);
3010 return TRUE;
3013 /*************************************************************************
3014 * PathSearchAndQualifyA [SHLWAPI.@]
3016 * Determine if a given path is correct and fully qualified.
3018 * PARAMS
3019 * lpszPath [I] Path to check
3020 * lpszBuf [O] Output for correct path
3021 * cchBuf [I] Size of lpszBuf
3023 * RETURNS
3024 * Unknown.
3026 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3028 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3029 return FALSE;
3032 /*************************************************************************
3033 * PathSearchAndQualifyW [SHLWAPI.@]
3035 * See PathSearchAndQualifyA
3037 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3039 FIXME("(%s,%p,0x%08x)-stub\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3040 return FALSE;
3043 /*************************************************************************
3044 * PathSkipRootA [SHLWAPI.@]
3046 * Return the portion of a path following the drive letter or mount point.
3048 * PARAMS
3049 * lpszPath [I] The path to skip on
3051 * RETURNS
3052 * Success: A pointer to the next character after the root.
3053 * Failure: NULL, if lpszPath is invalid, has no root or is a MB string.
3055 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3057 TRACE("(%s)\n", debugstr_a(lpszPath));
3059 if (!lpszPath || !*lpszPath)
3060 return NULL;
3062 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3064 /* Network share: skip share server and mount point */
3065 lpszPath += 2;
3066 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3067 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3068 lpszPath++;
3069 return (LPSTR)lpszPath;
3072 if (IsDBCSLeadByte(*lpszPath))
3073 return NULL;
3075 /* Check x:\ */
3076 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3077 return (LPSTR)lpszPath + 3;
3078 return NULL;
3081 /*************************************************************************
3082 * PathSkipRootW [SHLWAPI.@]
3084 * See PathSkipRootA.
3086 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3088 TRACE("(%s)\n", debugstr_w(lpszPath));
3090 if (!lpszPath || !*lpszPath)
3091 return NULL;
3093 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3095 /* Network share: skip share server and mount point */
3096 lpszPath += 2;
3097 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3098 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3099 lpszPath++;
3100 return (LPWSTR)lpszPath;
3103 /* Check x:\ */
3104 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3105 return (LPWSTR)lpszPath + 3;
3106 return NULL;
3109 /*************************************************************************
3110 * PathCreateFromUrlA [SHLWAPI.@]
3112 * Create a path from a URL
3114 * PARAMS
3115 * lpszUrl [I] URL to convert into a path
3116 * lpszPath [O] Output buffer for the resulting Path
3117 * pcchPath [I] Length of lpszPath
3118 * dwFlags [I] Flags controlling the conversion
3120 * RETURNS
3121 * Success: S_OK. lpszPath contains the URL in path format
3122 * Failure: An HRESULT error code such as E_INVALIDARG.
3124 HRESULT WINAPI PathCreateFromUrlA(LPCSTR lpszUrl, LPSTR lpszPath,
3125 LPDWORD pcchPath, DWORD dwFlags)
3127 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_a(lpszUrl), lpszPath, pcchPath, dwFlags);
3129 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3130 return E_INVALIDARG;
3132 /* extracts thing prior to : in pszURL and checks against:
3133 * https
3134 * shell
3135 * local
3136 * about - if match returns E_INVALIDARG
3139 return S_OK;
3142 /*************************************************************************
3143 * PathCreateFromUrlW [SHLWAPI.@]
3145 * See PathCreateFromUrlA.
3147 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR lpszUrl, LPWSTR lpszPath,
3148 LPDWORD pcchPath, DWORD dwFlags)
3150 FIXME("(%s,%p,%p,0x%08lx)-stub\n", debugstr_w(lpszUrl), lpszPath, pcchPath, dwFlags);
3152 if (!lpszUrl || !lpszPath || !pcchPath || !*pcchPath)
3153 return E_INVALIDARG;
3155 return S_OK;
3158 /*************************************************************************
3159 * PathRelativePathToA [SHLWAPI.@]
3161 * Create a relative path from one path to another.
3163 * PARAMS
3164 * lpszPath [O] Destination for relative path
3165 * lpszFrom [I] Source path
3166 * dwAttrFrom [I] File attribute of source path
3167 * lpszTo [I] Destination path
3168 * dwAttrTo [I] File attributes of destination path
3170 * RETURNS
3171 * TRUE If a relative path can be formed. lpszPath contains the new path
3172 * FALSE If the paths are not relavtive or any parameters are invalid
3174 * NOTES
3175 * lpszTo should be at least MAX_PATH in length.
3176 * Calling this function with relative paths for lpszFrom or lpszTo may
3177 * give erroneous results.
3179 * The Win32 version of this function contains a bug where the lpszTo string
3180 * may be referenced 1 byte beyond the end of the string. As a result random
3181 * garbage may be written to the output path, depending on what lies beyond
3182 * the last byte of the string. This bug occurs because of the behaviour of
3183 * PathCommonPrefix (see notes for that function), and no workaround seems
3184 * possible with Win32.
3185 * This bug has been fixed here, so for example the relative path from "\\"
3186 * to "\\" is correctly determined as "." in this implementation.
3188 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3189 LPCSTR lpszTo, DWORD dwAttrTo)
3191 BOOL bRet = FALSE;
3193 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_a(lpszFrom),
3194 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3196 if(lpszPath && lpszFrom && lpszTo)
3198 WCHAR szPath[MAX_PATH];
3199 WCHAR szFrom[MAX_PATH];
3200 WCHAR szTo[MAX_PATH];
3201 MultiByteToWideChar(0,0,lpszFrom,-1,szFrom,MAX_PATH);
3202 MultiByteToWideChar(0,0,lpszTo,-1,szTo,MAX_PATH);
3203 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3204 WideCharToMultiByte(0,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3206 return bRet;
3209 /*************************************************************************
3210 * PathRelativePathToW [SHLWAPI.@]
3212 * See PathRelativePathToA.
3214 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3215 LPCWSTR lpszTo, DWORD dwAttrTo)
3217 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3218 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3219 WCHAR szFrom[MAX_PATH];
3220 WCHAR szTo[MAX_PATH];
3221 DWORD dwLen;
3223 TRACE("(%p,%s,0x%08lx,%s,0x%08lx)\n", lpszPath, debugstr_w(lpszFrom),
3224 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3226 if(!lpszPath || !lpszFrom || !lpszTo)
3227 return FALSE;
3229 *lpszPath = '\0';
3230 strncpyW(szFrom, lpszFrom, MAX_PATH);
3231 strncpyW(szTo, lpszTo, MAX_PATH);
3233 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3234 PathRemoveFileSpecW(szFrom);
3235 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3236 PathRemoveFileSpecW(szTo);
3238 /* Paths can only be relative if they have a common root */
3239 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3240 return FALSE;
3242 /* Strip off lpszFrom components to the root, by adding "..\" */
3243 lpszFrom = szFrom + dwLen;
3244 if (!*lpszFrom)
3246 lpszPath[0] = '.';
3247 lpszPath[1] = '\0';
3249 if (*lpszFrom == '\\')
3250 lpszFrom++;
3252 while (*lpszFrom)
3254 lpszFrom = PathFindNextComponentW(lpszFrom);
3255 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3258 /* From the root add the components of lpszTo */
3259 lpszTo += dwLen;
3260 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3261 * this function.
3263 if (*lpszTo && lpszTo[-1])
3265 if (*lpszTo != '\\')
3266 lpszTo--;
3267 dwLen = strlenW(lpszPath);
3268 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3270 *lpszPath = '\0';
3271 return FALSE;
3273 strcpyW(lpszPath + dwLen, lpszTo);
3275 return TRUE;
3278 /*************************************************************************
3279 * PathUnmakeSystemFolderA [SHLWAPI.@]
3281 * Remove the system folder attributes from a path.
3283 * PARAMS
3284 * lpszPath [I] The path to remove attributes from
3286 * RETURNS
3287 * Success: TRUE.
3288 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3289 * SetFileAttributesA fails.
3291 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3293 DWORD dwAttr;
3295 TRACE("(%s)\n", debugstr_a(lpszPath));
3297 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == -1u ||
3298 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3299 return FALSE;
3301 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3302 return SetFileAttributesA(lpszPath, dwAttr);
3305 /*************************************************************************
3306 * PathUnmakeSystemFolderW [SHLWAPI.@]
3308 * See PathUnmakeSystemFolderA.
3310 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3312 DWORD dwAttr;
3314 TRACE("(%s)\n", debugstr_w(lpszPath));
3316 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == -1u ||
3317 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3318 return FALSE;
3320 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3321 return SetFileAttributesW(lpszPath, dwAttr);
3325 /*************************************************************************
3326 * PathSetDlgItemPathA [SHLWAPI.@]
3328 * Set the text of a dialog item to a path, shrinking the path to fit
3329 * if it is too big for the item.
3331 * PARAMS
3332 * hDlg [I] Dialog handle
3333 * id [I] ID of item in the dialog
3334 * lpszPath [I] Path to set as the items text
3336 * RETURNS
3337 * Nothing.
3339 * NOTES
3340 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3341 * window text is erased).
3343 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3345 WCHAR szPath[MAX_PATH];
3347 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3349 if (lpszPath)
3350 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3351 else
3352 szPath[0] = '\0';
3353 PathSetDlgItemPathW(hDlg, id, szPath);
3356 /*************************************************************************
3357 * PathSetDlgItemPathW [SHLWAPI.@]
3359 * See PathSetDlgItemPathA.
3361 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3363 WCHAR path[MAX_PATH + 1];
3364 HWND hwItem;
3365 RECT rect;
3366 HDC hdc;
3367 HGDIOBJ hPrevObj;
3369 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3371 if (!(hwItem = GetDlgItem(hDlg, id)))
3372 return;
3374 if (lpszPath)
3375 strncpyW(path, lpszPath, sizeof(path));
3376 else
3377 path[0] = '\0';
3379 GetClientRect(hwItem, &rect);
3380 hdc = GetDC(hDlg);
3381 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3383 if (hPrevObj)
3385 PathCompactPathW(hdc, path, rect.right);
3386 SelectObject(hdc, hPrevObj);
3389 ReleaseDC(hDlg, hdc);
3390 SetWindowTextW(hwItem, path);
3393 /*************************************************************************
3394 * PathIsNetworkPathA [SHLWAPI.@]
3396 * Determine if the given path is a network path.
3398 * PARAMS
3399 * lpszPath [I] Path to check
3401 * RETURNS
3402 * TRUE If path is a UNC share or mapped network drive
3403 * FALSE If path is a local drive or cannot be determined
3405 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3407 DWORD dwDriveNum;
3409 TRACE("(%s)\n",debugstr_a(lpszPath));
3411 if (!lpszPath)
3412 return FALSE;
3413 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3414 return TRUE;
3415 dwDriveNum = PathGetDriveNumberA(lpszPath);
3416 if (dwDriveNum == -1u)
3417 return FALSE;
3418 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3419 return pIsNetDrive(dwDriveNum);
3422 /*************************************************************************
3423 * PathIsNetworkPathW [SHLWAPI.@]
3425 * See PathIsNetworkPathA.
3427 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3429 DWORD dwDriveNum;
3431 TRACE("(%s)\n", debugstr_w(lpszPath));
3433 if (!lpszPath)
3434 return FALSE;
3435 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3436 return TRUE;
3437 dwDriveNum = PathGetDriveNumberW(lpszPath);
3438 if (dwDriveNum == -1u)
3439 return FALSE;
3440 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3441 return pIsNetDrive(dwDriveNum);
3444 /*************************************************************************
3445 * PathIsLFNFileSpecA [SHLWAPI.@]
3447 * Determine if the given path is a long file name
3449 * PARAMS
3450 * lpszPath [I] Path to check
3452 * RETURNS
3453 * TRUE If path is a long file name
3454 * FALSE If path is a valid DOS 8.3 file name
3456 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3458 DWORD dwNameLen = 0, dwExtLen = 0;
3460 TRACE("(%s)\n",debugstr_a(lpszPath));
3462 if (!lpszPath)
3463 return FALSE;
3465 while (*lpszPath)
3467 if (*lpszPath == ' ')
3468 return TRUE; /* DOS names cannot have spaces */
3469 if (*lpszPath == '.')
3471 if (dwExtLen)
3472 return TRUE; /* DOS names have only one dot */
3473 dwExtLen = 1;
3475 else if (dwExtLen)
3477 dwExtLen++;
3478 if (dwExtLen > 4)
3479 return TRUE; /* DOS extensions are <= 3 chars*/
3481 else
3483 dwNameLen++;
3484 if (dwNameLen > 8)
3485 return TRUE; /* DOS names are <= 8 chars */
3487 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3489 return FALSE; /* Valid DOS path */
3492 /*************************************************************************
3493 * PathIsLFNFileSpecW [SHLWAPI.@]
3495 * See PathIsLFNFileSpecA.
3497 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3499 DWORD dwNameLen = 0, dwExtLen = 0;
3501 TRACE("(%s)\n",debugstr_w(lpszPath));
3503 if (!lpszPath)
3504 return FALSE;
3506 while (*lpszPath)
3508 if (*lpszPath == ' ')
3509 return TRUE; /* DOS names cannot have spaces */
3510 if (*lpszPath == '.')
3512 if (dwExtLen)
3513 return TRUE; /* DOS names have only one dot */
3514 dwExtLen = 1;
3516 else if (dwExtLen)
3518 dwExtLen++;
3519 if (dwExtLen > 4)
3520 return TRUE; /* DOS extensions are <= 3 chars*/
3522 else
3524 dwNameLen++;
3525 if (dwNameLen > 8)
3526 return TRUE; /* DOS names are <= 8 chars */
3528 lpszPath++;
3530 return FALSE; /* Valid DOS path */
3533 /*************************************************************************
3534 * PathIsDirectoryEmptyA [SHLWAPI.@]
3536 * Determine if a given directory is empty.
3538 * PARAMS
3539 * lpszPath [I] Directory to check
3541 * RETURNS
3542 * TRUE If the directory exists and contains no files
3543 * FALSE Otherwise
3545 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3547 BOOL bRet = FALSE;
3549 TRACE("(%s)\n",debugstr_a(lpszPath));
3551 if (lpszPath)
3553 WCHAR szPath[MAX_PATH];
3554 MultiByteToWideChar(0,0,lpszPath,-1,szPath,MAX_PATH);
3555 bRet = PathIsDirectoryEmptyW(szPath);
3557 return bRet;
3560 /*************************************************************************
3561 * PathIsDirectoryEmptyW [SHLWAPI.@]
3563 * See PathIsDirectoryEmptyA.
3565 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3567 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3568 WCHAR szSearch[MAX_PATH];
3569 DWORD dwLen;
3570 HANDLE hfind;
3571 BOOL retVal = FALSE;
3572 WIN32_FIND_DATAW find_data;
3574 TRACE("(%s)\n",debugstr_w(lpszPath));
3576 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3577 return FALSE;
3579 strncpyW(szSearch, lpszPath, MAX_PATH);
3580 PathAddBackslashW(szSearch);
3581 dwLen = strlenW(szSearch);
3582 if (dwLen > MAX_PATH - 4)
3583 return FALSE;
3585 strcpyW(szSearch + dwLen, szAllFiles);
3586 hfind = FindFirstFileW(szSearch, &find_data);
3588 if (hfind != INVALID_HANDLE_VALUE &&
3589 find_data.cFileName[0] == '.' &&
3590 find_data.cFileName[1] == '.')
3592 /* The only directory entry should be the parent */
3593 if (!FindNextFileW(hfind, &find_data))
3594 retVal = TRUE;
3595 FindClose(hfind);
3597 return retVal;
3601 /*************************************************************************
3602 * PathFindSuffixArrayA [SHLWAPI.@]
3604 * Find a suffix string in an array of suffix strings
3606 * PARAMS
3607 * lpszSuffix [I] Suffix string to search for
3608 * lppszArray [I] Array of suffix strings to search
3609 * dwCount [I] Number of elements in lppszArray
3611 * RETURNS
3612 * Success The index of the position of lpszSuffix in lppszArray
3613 * Failure 0, if any parameters are invalid or lpszSuffix is not found
3615 * NOTES
3616 * The search is case sensitive.
3617 * The match is made against the end of the suffix string, so for example:
3618 * lpszSuffix=fooBAR matches BAR, but lpszSuffix=fooBARfoo does not.
3620 int WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3622 DWORD dwLen;
3623 int dwRet = 0;
3625 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3627 if (lpszSuffix && lppszArray && dwCount > 0)
3629 dwLen = strlen(lpszSuffix);
3631 while (dwRet < dwCount)
3633 DWORD dwCompareLen = strlen(*lppszArray);
3634 if (dwCompareLen < dwLen)
3636 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3637 return dwRet; /* Found */
3639 dwRet++;
3640 lppszArray++;
3643 return 0;
3646 /*************************************************************************
3647 * PathFindSuffixArrayW [SHLWAPI.@]
3649 * See PathFindSuffixArrayA.
3651 int WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3653 DWORD dwLen;
3654 int dwRet = 0;
3656 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3658 if (lpszSuffix && lppszArray && dwCount > 0)
3660 dwLen = strlenW(lpszSuffix);
3662 while (dwRet < dwCount)
3664 DWORD dwCompareLen = strlenW(*lppszArray);
3665 if (dwCompareLen < dwLen)
3667 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3668 return dwRet; /* Found */
3670 dwRet++;
3671 lppszArray++;
3674 return 0;
3677 /*************************************************************************
3678 * PathUndecorateA [SHLWAPI.@]
3680 * Undecorate a file path
3682 * PARAMS
3683 * lpszPath [O] Path to undecorate
3685 * RETURNS
3686 * Nothing
3688 * NOTES
3689 * A decorations form is "path[n].ext" where n is an optional decimal number.
3691 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3693 TRACE("(%s)\n",debugstr_a(lpszPath));
3695 if (lpszPath)
3697 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3698 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3700 LPSTR lpszSkip = lpszExt - 2;
3701 if (*lpszSkip == '[')
3702 lpszSkip++; /* [] (no number) */
3703 else
3704 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3705 lpszSkip--;
3706 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3708 /* remove the [n] */
3709 lpszSkip--;
3710 while (*lpszExt)
3711 *lpszSkip++ = *lpszExt++;
3712 *lpszSkip = '\0';
3718 /*************************************************************************
3719 * PathUndecorateW [SHLWAPI.@]
3721 * See PathUndecorateA.
3723 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
3725 TRACE("(%s)\n",debugstr_w(lpszPath));
3727 if (lpszPath)
3729 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
3730 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3732 LPWSTR lpszSkip = lpszExt - 2;
3733 if (*lpszSkip == '[')
3734 lpszSkip++; /* [] (no number) */
3735 else
3736 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
3737 lpszSkip--;
3738 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3740 /* remove the [n] */
3741 lpszSkip--;
3742 while (*lpszExt)
3743 *lpszSkip++ = *lpszExt++;
3744 *lpszSkip = '\0';