Create this branch to work on loading of different Kernel-Debugger DLL providers...
[reactos.git] / dll / win32 / shlwapi / path.c
1 /*
2 * Path Functions
3 *
4 * Copyright 1999, 2000 Juergen Schmied
5 * Copyright 2001, 2002 Jon Griffiths
6 *
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.
11 *
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.
16 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #define WIN32_NO_STATUS
23 #define _INC_WINDOWS
24 #define COM_NO_WINDOWS_H
25
26 #include <config.h>
27 //#include "wine/port.h"
28
29 //#include <stdarg.h>
30 //#include <string.h>
31 //#include <stdlib.h>
32
33 #include <wine/unicode.h>
34 #include <windef.h>
35 #include <winbase.h>
36 #include <wingdi.h>
37 #include <winuser.h>
38 #include <winreg.h>
39 #include <winternl.h>
40 #define NO_SHLWAPI_STREAM
41 #include <shlwapi.h>
42 #include <wine/debug.h>
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 /* Get a function pointer from a DLL handle */
47 #define GET_FUNC(func, module, name, fail) \
48 do { \
49 if (!func) { \
50 if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
51 func = (fn##func)GetProcAddress(SHLWAPI_h##module, name); \
52 if (!func) return fail; \
53 } \
54 } while (0)
55
56 /* DLL handles for late bound calls */
57 static HMODULE SHLWAPI_hshell32;
58
59 /* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
60 typedef BOOL (WINAPI *fnpIsNetDrive)(int);
61 static fnpIsNetDrive pIsNetDrive;
62
63 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR,LPWSTR,DWORD);
64
65 /*************************************************************************
66 * PathAppendA [SHLWAPI.@]
67 *
68 * Append one path to another.
69 *
70 * PARAMS
71 * lpszPath [I/O] Initial part of path, and destination for output
72 * lpszAppend [I] Path to append
73 *
74 * RETURNS
75 * Success: TRUE. lpszPath contains the newly created path.
76 * Failure: FALSE, if either path is NULL, or PathCombineA() fails.
77 *
78 * NOTES
79 * lpszAppend must contain at least one backslash ('\') if not NULL.
80 * Because PathCombineA() is used to join the paths, the resulting
81 * path is also canonicalized.
82 */
83 BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
84 {
85 TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));
86
87 if (lpszPath && lpszAppend)
88 {
89 if (!PathIsUNCA(lpszAppend))
90 while (*lpszAppend == '\\')
91 lpszAppend++;
92 if (PathCombineA(lpszPath, lpszPath, lpszAppend))
93 return TRUE;
94 }
95 return FALSE;
96 }
97
98 /*************************************************************************
99 * PathAppendW [SHLWAPI.@]
100 *
101 * See PathAppendA.
102 */
103 BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
104 {
105 TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));
106
107 if (lpszPath && lpszAppend)
108 {
109 if (!PathIsUNCW(lpszAppend))
110 while (*lpszAppend == '\\')
111 lpszAppend++;
112 if (PathCombineW(lpszPath, lpszPath, lpszAppend))
113 return TRUE;
114 }
115 return FALSE;
116 }
117
118 /*************************************************************************
119 * PathCombineA [SHLWAPI.@]
120 *
121 * Combine two paths together.
122 *
123 * PARAMS
124 * lpszDest [O] Destination for combined path
125 * lpszDir [I] Directory path
126 * lpszFile [I] File path
127 *
128 * RETURNS
129 * Success: The output path
130 * Failure: NULL, if inputs are invalid.
131 *
132 * NOTES
133 * lpszDest should be at least MAX_PATH in size, and may point to the same
134 * memory location as lpszDir. The combined path is canonicalised.
135 */
136 LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
137 {
138 WCHAR szDest[MAX_PATH];
139 WCHAR szDir[MAX_PATH];
140 WCHAR szFile[MAX_PATH];
141 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
142
143 /* Invalid parameters */
144 if (!lpszDest)
145 return NULL;
146 if (!lpszDir && !lpszFile)
147 {
148 lpszDest[0] = 0;
149 return NULL;
150 }
151
152 if (lpszDir)
153 MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH);
154 if (lpszFile)
155 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
156
157 if (PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL))
158 if (WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0))
159 return lpszDest;
160
161 lpszDest[0] = 0;
162 return NULL;
163 }
164
165 /*************************************************************************
166 * PathCombineW [SHLWAPI.@]
167 *
168 * See PathCombineA.
169 */
170 LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
171 {
172 WCHAR szTemp[MAX_PATH];
173 BOOL bUseBoth = FALSE, bStrip = FALSE;
174
175 TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));
176
177 /* Invalid parameters */
178 if (!lpszDest)
179 return NULL;
180 if (!lpszDir && !lpszFile)
181 {
182 lpszDest[0] = 0;
183 return NULL;
184 }
185
186 if ((!lpszFile || !*lpszFile) && lpszDir)
187 {
188 /* Use dir only */
189 lstrcpynW(szTemp, lpszDir, MAX_PATH);
190 }
191 else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
192 {
193 if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
194 {
195 /* Use file only */
196 lstrcpynW(szTemp, lpszFile, MAX_PATH);
197 }
198 else
199 {
200 bUseBoth = TRUE;
201 bStrip = TRUE;
202 }
203 }
204 else
205 bUseBoth = TRUE;
206
207 if (bUseBoth)
208 {
209 lstrcpynW(szTemp, lpszDir, MAX_PATH);
210 if (bStrip)
211 {
212 PathStripToRootW(szTemp);
213 lpszFile++; /* Skip '\' */
214 }
215 if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
216 {
217 lpszDest[0] = 0;
218 return NULL;
219 }
220 strcatW(szTemp, lpszFile);
221 }
222
223 PathCanonicalizeW(lpszDest, szTemp);
224 return lpszDest;
225 }
226
227 /*************************************************************************
228 * PathAddBackslashA [SHLWAPI.@]
229 *
230 * Append a backslash ('\') to a path if one doesn't exist.
231 *
232 * PARAMS
233 * lpszPath [I/O] The path to append a backslash to.
234 *
235 * RETURNS
236 * Success: The position of the last backslash in the path.
237 * Failure: NULL, if lpszPath is NULL or the path is too large.
238 */
239 LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
240 {
241 size_t iLen;
242 LPSTR prev = lpszPath;
243
244 TRACE("(%s)\n",debugstr_a(lpszPath));
245
246 if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
247 return NULL;
248
249 if (iLen)
250 {
251 do {
252 lpszPath = CharNextA(prev);
253 if (*lpszPath)
254 prev = lpszPath;
255 } while (*lpszPath);
256 if (*prev != '\\')
257 {
258 *lpszPath++ = '\\';
259 *lpszPath = '\0';
260 }
261 }
262 return lpszPath;
263 }
264
265 /*************************************************************************
266 * PathAddBackslashW [SHLWAPI.@]
267 *
268 * See PathAddBackslashA.
269 */
270 LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
271 {
272 size_t iLen;
273
274 TRACE("(%s)\n",debugstr_w(lpszPath));
275
276 if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
277 return NULL;
278
279 if (iLen)
280 {
281 lpszPath += iLen;
282 if (lpszPath[-1] != '\\')
283 {
284 *lpszPath++ = '\\';
285 *lpszPath = '\0';
286 }
287 }
288 return lpszPath;
289 }
290
291 /*************************************************************************
292 * PathBuildRootA [SHLWAPI.@]
293 *
294 * Create a root drive string (e.g. "A:\") from a drive number.
295 *
296 * PARAMS
297 * lpszPath [O] Destination for the drive string
298 *
299 * RETURNS
300 * lpszPath
301 *
302 * NOTES
303 * If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
304 */
305 LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
306 {
307 TRACE("(%p,%d)\n", lpszPath, drive);
308
309 if (lpszPath && drive >= 0 && drive < 26)
310 {
311 lpszPath[0] = 'A' + drive;
312 lpszPath[1] = ':';
313 lpszPath[2] = '\\';
314 lpszPath[3] = '\0';
315 }
316 return lpszPath;
317 }
318
319 /*************************************************************************
320 * PathBuildRootW [SHLWAPI.@]
321 *
322 * See PathBuildRootA.
323 */
324 LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
325 {
326 TRACE("(%p,%d)\n", lpszPath, drive);
327
328 if (lpszPath && drive >= 0 && drive < 26)
329 {
330 lpszPath[0] = 'A' + drive;
331 lpszPath[1] = ':';
332 lpszPath[2] = '\\';
333 lpszPath[3] = '\0';
334 }
335 return lpszPath;
336 }
337
338 /*************************************************************************
339 * PathFindFileNameA [SHLWAPI.@]
340 *
341 * Locate the start of the file name in a path
342 *
343 * PARAMS
344 * lpszPath [I] Path to search
345 *
346 * RETURNS
347 * A pointer to the first character of the file name
348 */
349 LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
350 {
351 LPCSTR lastSlash = lpszPath;
352
353 TRACE("(%s)\n",debugstr_a(lpszPath));
354
355 while (lpszPath && *lpszPath)
356 {
357 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
358 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
359 lastSlash = lpszPath + 1;
360 lpszPath = CharNextA(lpszPath);
361 }
362 return (LPSTR)lastSlash;
363 }
364
365 /*************************************************************************
366 * PathFindFileNameW [SHLWAPI.@]
367 *
368 * See PathFindFileNameA.
369 */
370 LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
371 {
372 LPCWSTR lastSlash = lpszPath;
373
374 TRACE("(%s)\n",debugstr_w(lpszPath));
375
376 while (lpszPath && *lpszPath)
377 {
378 if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
379 lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
380 lastSlash = lpszPath + 1;
381 lpszPath++;
382 }
383 return (LPWSTR)lastSlash;
384 }
385
386 /*************************************************************************
387 * PathFindExtensionA [SHLWAPI.@]
388 *
389 * Locate the start of the file extension in a path
390 *
391 * PARAMS
392 * lpszPath [I] The path to search
393 *
394 * RETURNS
395 * A pointer to the first character of the extension, the end of
396 * the string if the path has no extension, or NULL If lpszPath is NULL
397 */
398 LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
399 {
400 LPCSTR lastpoint = NULL;
401
402 TRACE("(%s)\n", debugstr_a(lpszPath));
403
404 if (lpszPath)
405 {
406 while (*lpszPath)
407 {
408 if (*lpszPath == '\\' || *lpszPath==' ')
409 lastpoint = NULL;
410 else if (*lpszPath == '.')
411 lastpoint = lpszPath;
412 lpszPath = CharNextA(lpszPath);
413 }
414 }
415 return (LPSTR)(lastpoint ? lastpoint : lpszPath);
416 }
417
418 /*************************************************************************
419 * PathFindExtensionW [SHLWAPI.@]
420 *
421 * See PathFindExtensionA.
422 */
423 LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
424 {
425 LPCWSTR lastpoint = NULL;
426
427 TRACE("(%s)\n", debugstr_w(lpszPath));
428
429 if (lpszPath)
430 {
431 while (*lpszPath)
432 {
433 if (*lpszPath == '\\' || *lpszPath==' ')
434 lastpoint = NULL;
435 else if (*lpszPath == '.')
436 lastpoint = lpszPath;
437 lpszPath++;
438 }
439 }
440 return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
441 }
442
443 /*************************************************************************
444 * PathGetArgsA [SHLWAPI.@]
445 *
446 * Find the next argument in a string delimited by spaces.
447 *
448 * PARAMS
449 * lpszPath [I] The string to search for arguments in
450 *
451 * RETURNS
452 * The start of the next argument in lpszPath, or NULL if lpszPath is NULL
453 *
454 * NOTES
455 * Spaces in quoted strings are ignored as delimiters.
456 */
457 LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
458 {
459 BOOL bSeenQuote = FALSE;
460
461 TRACE("(%s)\n",debugstr_a(lpszPath));
462
463 if (lpszPath)
464 {
465 while (*lpszPath)
466 {
467 if ((*lpszPath==' ') && !bSeenQuote)
468 return (LPSTR)lpszPath + 1;
469 if (*lpszPath == '"')
470 bSeenQuote = !bSeenQuote;
471 lpszPath = CharNextA(lpszPath);
472 }
473 }
474 return (LPSTR)lpszPath;
475 }
476
477 /*************************************************************************
478 * PathGetArgsW [SHLWAPI.@]
479 *
480 * See PathGetArgsA.
481 */
482 LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
483 {
484 BOOL bSeenQuote = FALSE;
485
486 TRACE("(%s)\n",debugstr_w(lpszPath));
487
488 if (lpszPath)
489 {
490 while (*lpszPath)
491 {
492 if ((*lpszPath==' ') && !bSeenQuote)
493 return (LPWSTR)lpszPath + 1;
494 if (*lpszPath == '"')
495 bSeenQuote = !bSeenQuote;
496 lpszPath++;
497 }
498 }
499 return (LPWSTR)lpszPath;
500 }
501
502 /*************************************************************************
503 * PathGetDriveNumberA [SHLWAPI.@]
504 *
505 * Return the drive number from a path
506 *
507 * PARAMS
508 * lpszPath [I] Path to get the drive number from
509 *
510 * RETURNS
511 * Success: The drive number corresponding to the drive in the path
512 * Failure: -1, if lpszPath contains no valid drive
513 */
514 int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
515 {
516 TRACE ("(%s)\n",debugstr_a(lpszPath));
517
518 if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
519 tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
520 return tolower(*lpszPath) - 'a';
521 return -1;
522 }
523
524 /*************************************************************************
525 * PathGetDriveNumberW [SHLWAPI.@]
526 *
527 * See PathGetDriveNumberA.
528 */
529 int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
530 {
531 TRACE ("(%s)\n",debugstr_w(lpszPath));
532
533 if (lpszPath)
534 {
535 WCHAR tl = tolowerW(lpszPath[0]);
536 if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
537 return tl - 'a';
538 }
539 return -1;
540 }
541
542 /*************************************************************************
543 * PathRemoveFileSpecA [SHLWAPI.@]
544 *
545 * Remove the file specification from a path.
546 *
547 * PARAMS
548 * lpszPath [I/O] Path to remove the file spec from
549 *
550 * RETURNS
551 * TRUE If the path was valid and modified
552 * FALSE Otherwise
553 */
554 BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
555 {
556 LPSTR lpszFileSpec = lpszPath;
557 BOOL bModified = FALSE;
558
559 TRACE("(%s)\n",debugstr_a(lpszPath));
560
561 if(lpszPath)
562 {
563 /* Skip directory or UNC path */
564 if (*lpszPath == '\\')
565 lpszFileSpec = ++lpszPath;
566 if (*lpszPath == '\\')
567 lpszFileSpec = ++lpszPath;
568
569 while (*lpszPath)
570 {
571 if(*lpszPath == '\\')
572 lpszFileSpec = lpszPath; /* Skip dir */
573 else if(*lpszPath == ':')
574 {
575 lpszFileSpec = ++lpszPath; /* Skip drive */
576 if (*lpszPath == '\\')
577 lpszFileSpec++;
578 }
579 if (!(lpszPath = CharNextA(lpszPath)))
580 break;
581 }
582
583 if (*lpszFileSpec)
584 {
585 *lpszFileSpec = '\0';
586 bModified = TRUE;
587 }
588 }
589 return bModified;
590 }
591
592 /*************************************************************************
593 * PathRemoveFileSpecW [SHLWAPI.@]
594 *
595 * See PathRemoveFileSpecA.
596 */
597 BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
598 {
599 LPWSTR lpszFileSpec = lpszPath;
600 BOOL bModified = FALSE;
601
602 TRACE("(%s)\n",debugstr_w(lpszPath));
603
604 if(lpszPath)
605 {
606 /* Skip directory or UNC path */
607 if (*lpszPath == '\\')
608 lpszFileSpec = ++lpszPath;
609 if (*lpszPath == '\\')
610 lpszFileSpec = ++lpszPath;
611
612 while (*lpszPath)
613 {
614 if(*lpszPath == '\\')
615 lpszFileSpec = lpszPath; /* Skip dir */
616 else if(*lpszPath == ':')
617 {
618 lpszFileSpec = ++lpszPath; /* Skip drive */
619 if (*lpszPath == '\\')
620 lpszFileSpec++;
621 }
622 lpszPath++;
623 }
624
625 if (*lpszFileSpec)
626 {
627 *lpszFileSpec = '\0';
628 bModified = TRUE;
629 }
630 }
631 return bModified;
632 }
633
634 /*************************************************************************
635 * PathStripPathA [SHLWAPI.@]
636 *
637 * Remove the initial path from the beginning of a filename
638 *
639 * PARAMS
640 * lpszPath [I/O] Path to remove the initial path from
641 *
642 * RETURNS
643 * Nothing.
644 */
645 void WINAPI PathStripPathA(LPSTR lpszPath)
646 {
647 TRACE("(%s)\n", debugstr_a(lpszPath));
648
649 if (lpszPath)
650 {
651 LPSTR lpszFileName = PathFindFileNameA(lpszPath);
652 if(lpszFileName)
653 RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
654 }
655 }
656
657 /*************************************************************************
658 * PathStripPathW [SHLWAPI.@]
659 *
660 * See PathStripPathA.
661 */
662 void WINAPI PathStripPathW(LPWSTR lpszPath)
663 {
664 LPWSTR lpszFileName;
665
666 TRACE("(%s)\n", debugstr_w(lpszPath));
667 lpszFileName = PathFindFileNameW(lpszPath);
668 if(lpszFileName)
669 RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
670 }
671
672 /*************************************************************************
673 * PathStripToRootA [SHLWAPI.@]
674 *
675 * Reduce a path to its root.
676 *
677 * PARAMS
678 * lpszPath [I/O] the path to reduce
679 *
680 * RETURNS
681 * Success: TRUE if the stripped path is a root path
682 * Failure: FALSE if the path cannot be stripped or is NULL
683 */
684 BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
685 {
686 TRACE("(%s)\n", debugstr_a(lpszPath));
687
688 if (!lpszPath)
689 return FALSE;
690 while(!PathIsRootA(lpszPath))
691 if (!PathRemoveFileSpecA(lpszPath))
692 return FALSE;
693 return TRUE;
694 }
695
696 /*************************************************************************
697 * PathStripToRootW [SHLWAPI.@]
698 *
699 * See PathStripToRootA.
700 */
701 BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
702 {
703 TRACE("(%s)\n", debugstr_w(lpszPath));
704
705 if (!lpszPath)
706 return FALSE;
707 while(!PathIsRootW(lpszPath))
708 if (!PathRemoveFileSpecW(lpszPath))
709 return FALSE;
710 return TRUE;
711 }
712
713 /*************************************************************************
714 * PathRemoveArgsA [SHLWAPI.@]
715 *
716 * Strip space separated arguments from a path.
717 *
718 * PARAMS
719 * lpszPath [I/O] Path to remove arguments from
720 *
721 * RETURNS
722 * Nothing.
723 */
724 void WINAPI PathRemoveArgsA(LPSTR lpszPath)
725 {
726 TRACE("(%s)\n",debugstr_a(lpszPath));
727
728 if(lpszPath)
729 {
730 LPSTR lpszArgs = PathGetArgsA(lpszPath);
731 if (*lpszArgs)
732 lpszArgs[-1] = '\0';
733 else
734 {
735 LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
736 if(*lpszLastChar == ' ')
737 *lpszLastChar = '\0';
738 }
739 }
740 }
741
742 /*************************************************************************
743 * PathRemoveArgsW [SHLWAPI.@]
744 *
745 * See PathRemoveArgsA.
746 */
747 void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
748 {
749 TRACE("(%s)\n",debugstr_w(lpszPath));
750
751 if(lpszPath)
752 {
753 LPWSTR lpszArgs = PathGetArgsW(lpszPath);
754 if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
755 lpszArgs[-1] = '\0';
756 }
757 }
758
759 /*************************************************************************
760 * PathRemoveExtensionA [SHLWAPI.@]
761 *
762 * Remove the file extension from a path
763 *
764 * PARAMS
765 * lpszPath [I/O] Path to remove the extension from
766 *
767 * NOTES
768 * The NUL terminator must be written only if extension exists
769 * and if the pointed character is not already NUL.
770 *
771 * RETURNS
772 * Nothing.
773 */
774 void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
775 {
776 TRACE("(%s)\n", debugstr_a(lpszPath));
777
778 if (lpszPath)
779 {
780 lpszPath = PathFindExtensionA(lpszPath);
781 if (lpszPath && *lpszPath != '\0')
782 *lpszPath = '\0';
783 }
784 }
785
786 /*************************************************************************
787 * PathRemoveExtensionW [SHLWAPI.@]
788 *
789 * See PathRemoveExtensionA.
790 */
791 void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
792 {
793 TRACE("(%s)\n", debugstr_w(lpszPath));
794
795 if (lpszPath)
796 {
797 lpszPath = PathFindExtensionW(lpszPath);
798 if (lpszPath && *lpszPath != '\0')
799 *lpszPath = '\0';
800 }
801 }
802
803 /*************************************************************************
804 * PathRemoveBackslashA [SHLWAPI.@]
805 *
806 * Remove a trailing backslash from a path.
807 *
808 * PARAMS
809 * lpszPath [I/O] Path to remove backslash from
810 *
811 * RETURNS
812 * Success: A pointer to the end of the path
813 * Failure: NULL, if lpszPath is NULL
814 */
815 LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
816 {
817 LPSTR szTemp = NULL;
818
819 TRACE("(%s)\n", debugstr_a(lpszPath));
820
821 if(lpszPath)
822 {
823 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
824 if (!PathIsRootA(lpszPath) && *szTemp == '\\')
825 *szTemp = '\0';
826 }
827 return szTemp;
828 }
829
830 /*************************************************************************
831 * PathRemoveBackslashW [SHLWAPI.@]
832 *
833 * See PathRemoveBackslashA.
834 */
835 LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
836 {
837 LPWSTR szTemp = NULL;
838
839 TRACE("(%s)\n", debugstr_w(lpszPath));
840
841 if(lpszPath)
842 {
843 szTemp = lpszPath + strlenW(lpszPath);
844 if (szTemp > lpszPath) szTemp--;
845 if (!PathIsRootW(lpszPath) && *szTemp == '\\')
846 *szTemp = '\0';
847 }
848 return szTemp;
849 }
850
851 /*************************************************************************
852 * PathRemoveBlanksA [SHLWAPI.@]
853 *
854 * Remove Spaces from the start and end of a path.
855 *
856 * PARAMS
857 * lpszPath [I/O] Path to strip blanks from
858 *
859 * RETURNS
860 * Nothing.
861 */
862 VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
863 {
864 TRACE("(%s)\n", debugstr_a(lpszPath));
865
866 if(lpszPath && *lpszPath)
867 {
868 LPSTR start = lpszPath;
869
870 while (*lpszPath == ' ')
871 lpszPath = CharNextA(lpszPath);
872
873 while(*lpszPath)
874 *start++ = *lpszPath++;
875
876 if (start != lpszPath)
877 while (start[-1] == ' ')
878 start--;
879 *start = '\0';
880 }
881 }
882
883 /*************************************************************************
884 * PathRemoveBlanksW [SHLWAPI.@]
885 *
886 * See PathRemoveBlanksA.
887 */
888 VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
889 {
890 TRACE("(%s)\n", debugstr_w(lpszPath));
891
892 if(lpszPath && *lpszPath)
893 {
894 LPWSTR start = lpszPath;
895
896 while (*lpszPath == ' ')
897 lpszPath++;
898
899 while(*lpszPath)
900 *start++ = *lpszPath++;
901
902 if (start != lpszPath)
903 while (start[-1] == ' ')
904 start--;
905 *start = '\0';
906 }
907 }
908
909 /*************************************************************************
910 * PathQuoteSpacesA [SHLWAPI.@]
911 *
912 * Surround a path containing spaces in quotes.
913 *
914 * PARAMS
915 * lpszPath [I/O] Path to quote
916 *
917 * RETURNS
918 * Nothing.
919 *
920 * NOTES
921 * The path is not changed if it is invalid or has no spaces.
922 */
923 VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
924 {
925 TRACE("(%s)\n", debugstr_a(lpszPath));
926
927 if(lpszPath && StrChrA(lpszPath,' '))
928 {
929 size_t iLen = strlen(lpszPath) + 1;
930
931 if (iLen + 2 < MAX_PATH)
932 {
933 memmove(lpszPath + 1, lpszPath, iLen);
934 lpszPath[0] = '"';
935 lpszPath[iLen] = '"';
936 lpszPath[iLen + 1] = '\0';
937 }
938 }
939 }
940
941 /*************************************************************************
942 * PathQuoteSpacesW [SHLWAPI.@]
943 *
944 * See PathQuoteSpacesA.
945 */
946 VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
947 {
948 TRACE("(%s)\n", debugstr_w(lpszPath));
949
950 if(lpszPath && StrChrW(lpszPath,' '))
951 {
952 int iLen = strlenW(lpszPath) + 1;
953
954 if (iLen + 2 < MAX_PATH)
955 {
956 memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
957 lpszPath[0] = '"';
958 lpszPath[iLen] = '"';
959 lpszPath[iLen + 1] = '\0';
960 }
961 }
962 }
963
964 /*************************************************************************
965 * PathUnquoteSpacesA [SHLWAPI.@]
966 *
967 * Remove quotes ("") from around a path, if present.
968 *
969 * PARAMS
970 * lpszPath [I/O] Path to strip quotes from
971 *
972 * RETURNS
973 * Nothing
974 *
975 * NOTES
976 * If the path contains a single quote only, an empty string will result.
977 * Otherwise quotes are only removed if they appear at the start and end
978 * of the path.
979 */
980 VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
981 {
982 TRACE("(%s)\n", debugstr_a(lpszPath));
983
984 if (lpszPath && *lpszPath == '"')
985 {
986 DWORD dwLen = strlen(lpszPath) - 1;
987
988 if (lpszPath[dwLen] == '"')
989 {
990 lpszPath[dwLen] = '\0';
991 for (; *lpszPath; lpszPath++)
992 *lpszPath = lpszPath[1];
993 }
994 }
995 }
996
997 /*************************************************************************
998 * PathUnquoteSpacesW [SHLWAPI.@]
999 *
1000 * See PathUnquoteSpacesA.
1001 */
1002 VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
1003 {
1004 TRACE("(%s)\n", debugstr_w(lpszPath));
1005
1006 if (lpszPath && *lpszPath == '"')
1007 {
1008 DWORD dwLen = strlenW(lpszPath) - 1;
1009
1010 if (lpszPath[dwLen] == '"')
1011 {
1012 lpszPath[dwLen] = '\0';
1013 for (; *lpszPath; lpszPath++)
1014 *lpszPath = lpszPath[1];
1015 }
1016 }
1017 }
1018
1019 /*************************************************************************
1020 * PathParseIconLocationA [SHLWAPI.@]
1021 *
1022 * Parse the location of an icon from a path.
1023 *
1024 * PARAMS
1025 * lpszPath [I/O] The path to parse the icon location from.
1026 *
1027 * RETURNS
1028 * Success: The number of the icon
1029 * Failure: 0 if the path does not contain an icon location or is NULL
1030 *
1031 * NOTES
1032 * The path has surrounding quotes and spaces removed regardless
1033 * of whether the call succeeds or not.
1034 */
1035 int WINAPI PathParseIconLocationA(LPSTR lpszPath)
1036 {
1037 int iRet = 0;
1038 LPSTR lpszComma;
1039
1040 TRACE("(%s)\n", debugstr_a(lpszPath));
1041
1042 if (lpszPath)
1043 {
1044 if ((lpszComma = strchr(lpszPath, ',')))
1045 {
1046 *lpszComma++ = '\0';
1047 iRet = StrToIntA(lpszComma);
1048 }
1049 PathUnquoteSpacesA(lpszPath);
1050 PathRemoveBlanksA(lpszPath);
1051 }
1052 return iRet;
1053 }
1054
1055 /*************************************************************************
1056 * PathParseIconLocationW [SHLWAPI.@]
1057 *
1058 * See PathParseIconLocationA.
1059 */
1060 int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
1061 {
1062 int iRet = 0;
1063 LPWSTR lpszComma;
1064
1065 TRACE("(%s)\n", debugstr_w(lpszPath));
1066
1067 if (lpszPath)
1068 {
1069 if ((lpszComma = StrChrW(lpszPath, ',')))
1070 {
1071 *lpszComma++ = '\0';
1072 iRet = StrToIntW(lpszComma);
1073 }
1074 PathUnquoteSpacesW(lpszPath);
1075 PathRemoveBlanksW(lpszPath);
1076 }
1077 return iRet;
1078 }
1079
1080 /*************************************************************************
1081 * @ [SHLWAPI.4]
1082 *
1083 * Unicode version of PathFileExistsDefExtA.
1084 */
1085 BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1086 {
1087 static const WCHAR pszExts[][5] = { { '.', 'p', 'i', 'f', 0},
1088 { '.', 'c', 'o', 'm', 0},
1089 { '.', 'e', 'x', 'e', 0},
1090 { '.', 'b', 'a', 't', 0},
1091 { '.', 'l', 'n', 'k', 0},
1092 { '.', 'c', 'm', 'd', 0},
1093 { 0, 0, 0, 0, 0} };
1094
1095 TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1096
1097 if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
1098 return FALSE;
1099
1100 if (dwWhich)
1101 {
1102 LPCWSTR szExt = PathFindExtensionW(lpszPath);
1103 if (!*szExt || dwWhich & 0x40)
1104 {
1105 size_t iChoose = 0;
1106 int iLen = lstrlenW(lpszPath);
1107 if (iLen > (MAX_PATH - 5))
1108 return FALSE;
1109 while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1110 {
1111 lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
1112 if (PathFileExistsW(lpszPath))
1113 return TRUE;
1114 iChoose++;
1115 dwWhich >>= 1;
1116 }
1117 *(lpszPath + iLen) = (WCHAR)'\0';
1118 return FALSE;
1119 }
1120 }
1121 return PathFileExistsW(lpszPath);
1122 }
1123
1124 /*************************************************************************
1125 * @ [SHLWAPI.3]
1126 *
1127 * Determine if a file exists locally and is of an executable type.
1128 *
1129 * PARAMS
1130 * lpszPath [I/O] File to search for
1131 * dwWhich [I] Type of executable to search for
1132 *
1133 * RETURNS
1134 * TRUE If the file was found. lpszPath contains the file name.
1135 * FALSE Otherwise.
1136 *
1137 * NOTES
1138 * lpszPath is modified in place and must be at least MAX_PATH in length.
1139 * If the function returns FALSE, the path is modified to its original state.
1140 * If the given path contains an extension or dwWhich is 0, executable
1141 * extensions are not checked.
1142 *
1143 * Ordinals 3-6 are a classic case of MS exposing limited functionality to
1144 * users (here through PathFindOnPathA()) and keeping advanced functionality for
1145 * their own developers exclusive use. Monopoly, anyone?
1146 */
1147 BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1148 {
1149 BOOL bRet = FALSE;
1150
1151 TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1152
1153 if (lpszPath)
1154 {
1155 WCHAR szPath[MAX_PATH];
1156 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1157 bRet = PathFileExistsDefExtW(szPath, dwWhich);
1158 if (bRet)
1159 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1160 }
1161 return bRet;
1162 }
1163
1164 /*************************************************************************
1165 * SHLWAPI_PathFindInOtherDirs
1166 *
1167 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
1168 */
1169 static BOOL SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1170 {
1171 static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
1172 static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1173 DWORD dwLenPATH;
1174 LPCWSTR lpszCurr;
1175 WCHAR *lpszPATH;
1176 WCHAR buff[MAX_PATH];
1177
1178 TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1179
1180 /* Try system directories */
1181 GetSystemDirectoryW(buff, MAX_PATH);
1182 if (!PathAppendW(buff, lpszFile))
1183 return FALSE;
1184 if (PathFileExistsDefExtW(buff, dwWhich))
1185 {
1186 strcpyW(lpszFile, buff);
1187 return TRUE;
1188 }
1189 GetWindowsDirectoryW(buff, MAX_PATH);
1190 if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
1191 return FALSE;
1192 if (PathFileExistsDefExtW(buff, dwWhich))
1193 {
1194 strcpyW(lpszFile, buff);
1195 return TRUE;
1196 }
1197 GetWindowsDirectoryW(buff, MAX_PATH);
1198 if (!PathAppendW(buff, lpszFile))
1199 return FALSE;
1200 if (PathFileExistsDefExtW(buff, dwWhich))
1201 {
1202 strcpyW(lpszFile, buff);
1203 return TRUE;
1204 }
1205 /* Try dirs listed in %PATH% */
1206 dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);
1207
1208 if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1209 return FALSE;
1210
1211 GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
1212 lpszCurr = lpszPATH;
1213 while (lpszCurr)
1214 {
1215 LPCWSTR lpszEnd = lpszCurr;
1216 LPWSTR pBuff = buff;
1217
1218 while (*lpszEnd == ' ')
1219 lpszEnd++;
1220 while (*lpszEnd && *lpszEnd != ';')
1221 *pBuff++ = *lpszEnd++;
1222 *pBuff = '\0';
1223
1224 if (*lpszEnd)
1225 lpszCurr = lpszEnd + 1;
1226 else
1227 lpszCurr = NULL; /* Last Path, terminate after this */
1228
1229 if (!PathAppendW(buff, lpszFile))
1230 {
1231 HeapFree(GetProcessHeap(), 0, lpszPATH);
1232 return FALSE;
1233 }
1234 if (PathFileExistsDefExtW(buff, dwWhich))
1235 {
1236 strcpyW(lpszFile, buff);
1237 HeapFree(GetProcessHeap(), 0, lpszPATH);
1238 return TRUE;
1239 }
1240 }
1241 HeapFree(GetProcessHeap(), 0, lpszPATH);
1242 return FALSE;
1243 }
1244
1245 /*************************************************************************
1246 * @ [SHLWAPI.5]
1247 *
1248 * Search a range of paths for a specific type of executable.
1249 *
1250 * PARAMS
1251 * lpszFile [I/O] File to search for
1252 * lppszOtherDirs [I] Other directories to look in
1253 * dwWhich [I] Type of executable to search for
1254 *
1255 * RETURNS
1256 * Success: TRUE. The path to the executable is stored in lpszFile.
1257 * Failure: FALSE. The path to the executable is unchanged.
1258 */
1259 BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1260 {
1261 WCHAR szFile[MAX_PATH];
1262 WCHAR buff[MAX_PATH];
1263
1264 TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1265
1266 if (!lpszFile || !PathIsFileSpecA(lpszFile))
1267 return FALSE;
1268
1269 MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1270
1271 /* Search provided directories first */
1272 if (lppszOtherDirs && *lppszOtherDirs)
1273 {
1274 WCHAR szOther[MAX_PATH];
1275 LPCSTR *lpszOtherPath = lppszOtherDirs;
1276
1277 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1278 {
1279 MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1280 PathCombineW(buff, szOther, szFile);
1281 if (PathFileExistsDefExtW(buff, dwWhich))
1282 {
1283 WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1284 return TRUE;
1285 }
1286 lpszOtherPath++;
1287 }
1288 }
1289 /* Not found, try system and path dirs */
1290 if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
1291 {
1292 WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1293 return TRUE;
1294 }
1295 return FALSE;
1296 }
1297
1298 /*************************************************************************
1299 * @ [SHLWAPI.6]
1300 *
1301 * Unicode version of PathFindOnPathExA.
1302 */
1303 BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1304 {
1305 WCHAR buff[MAX_PATH];
1306
1307 TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1308
1309 if (!lpszFile || !PathIsFileSpecW(lpszFile))
1310 return FALSE;
1311
1312 /* Search provided directories first */
1313 if (lppszOtherDirs && *lppszOtherDirs)
1314 {
1315 LPCWSTR *lpszOtherPath = lppszOtherDirs;
1316 while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
1317 {
1318 PathCombineW(buff, *lpszOtherPath, lpszFile);
1319 if (PathFileExistsDefExtW(buff, dwWhich))
1320 {
1321 strcpyW(lpszFile, buff);
1322 return TRUE;
1323 }
1324 lpszOtherPath++;
1325 }
1326 }
1327 /* Not found, try system and path dirs */
1328 return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1329 }
1330
1331 /*************************************************************************
1332 * PathFindOnPathA [SHLWAPI.@]
1333 *
1334 * Search a range of paths for an executable.
1335 *
1336 * PARAMS
1337 * lpszFile [I/O] File to search for
1338 * lppszOtherDirs [I] Other directories to look in
1339 *
1340 * RETURNS
1341 * Success: TRUE. The path to the executable is stored in lpszFile.
1342 * Failure: FALSE. The path to the executable is unchanged.
1343 */
1344 BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1345 {
1346 TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1347 return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1348 }
1349
1350 /*************************************************************************
1351 * PathFindOnPathW [SHLWAPI.@]
1352 *
1353 * See PathFindOnPathA.
1354 */
1355 BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1356 {
1357 TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1358 return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1359 }
1360
1361 /*************************************************************************
1362 * PathCompactPathExA [SHLWAPI.@]
1363 *
1364 * Compact a path into a given number of characters.
1365 *
1366 * PARAMS
1367 * lpszDest [O] Destination for compacted path
1368 * lpszPath [I] Source path
1369 * cchMax [I] Maximum size of compacted path
1370 * dwFlags [I] Reserved
1371 *
1372 * RETURNS
1373 * Success: TRUE. The compacted path is written to lpszDest.
1374 * Failure: FALSE. lpszPath is undefined.
1375 *
1376 * NOTES
1377 * If cchMax is given as 0, lpszDest will still be NUL terminated.
1378 *
1379 * The Win32 version of this function contains a bug: When cchMax == 7,
1380 * 8 bytes will be written to lpszDest. This bug is fixed in the Wine
1381 * implementation.
1382 *
1383 * Some relative paths will be different when cchMax == 5 or 6. This occurs
1384 * because Win32 will insert a "\" in lpszDest, even if one is
1385 * not present in the original path.
1386 */
1387 BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
1388 UINT cchMax, DWORD dwFlags)
1389 {
1390 BOOL bRet = FALSE;
1391
1392 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1393
1394 if (lpszPath && lpszDest)
1395 {
1396 WCHAR szPath[MAX_PATH];
1397 WCHAR szDest[MAX_PATH];
1398
1399 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1400 szDest[0] = '\0';
1401 bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1402 WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1403 }
1404 return bRet;
1405 }
1406
1407 /*************************************************************************
1408 * PathCompactPathExW [SHLWAPI.@]
1409 *
1410 * See PathCompactPathExA.
1411 */
1412 BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
1413 UINT cchMax, DWORD dwFlags)
1414 {
1415 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
1416 LPCWSTR lpszFile;
1417 DWORD dwLen, dwFileLen = 0;
1418
1419 TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1420
1421 if (!lpszPath)
1422 return FALSE;
1423
1424 if (!lpszDest)
1425 {
1426 WARN("Invalid lpszDest would crash under Win32!\n");
1427 return FALSE;
1428 }
1429
1430 *lpszDest = '\0';
1431
1432 if (cchMax < 2)
1433 return TRUE;
1434
1435 dwLen = strlenW(lpszPath) + 1;
1436
1437 if (dwLen < cchMax)
1438 {
1439 /* Don't need to compact */
1440 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1441 return TRUE;
1442 }
1443
1444 /* Path must be compacted to fit into lpszDest */
1445 lpszFile = PathFindFileNameW(lpszPath);
1446 dwFileLen = lpszPath + dwLen - lpszFile;
1447
1448 if (dwFileLen == dwLen)
1449 {
1450 /* No root in psth */
1451 if (cchMax <= 4)
1452 {
1453 while (--cchMax > 0) /* No room left for anything but ellipses */
1454 *lpszDest++ = '.';
1455 *lpszDest = '\0';
1456 return TRUE;
1457 }
1458 /* Compact the file name with ellipses at the end */
1459 cchMax -= 4;
1460 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1461 strcpyW(lpszDest + cchMax, szEllipses);
1462 return TRUE;
1463 }
1464 /* We have a root in the path */
1465 lpszFile--; /* Start compacted filename with the path separator */
1466 dwFileLen++;
1467
1468 if (dwFileLen + 3 > cchMax)
1469 {
1470 /* Compact the file name */
1471 if (cchMax <= 4)
1472 {
1473 while (--cchMax > 0) /* No room left for anything but ellipses */
1474 *lpszDest++ = '.';
1475 *lpszDest = '\0';
1476 return TRUE;
1477 }
1478 strcpyW(lpszDest, szEllipses);
1479 lpszDest += 3;
1480 cchMax -= 4;
1481 *lpszDest++ = *lpszFile++;
1482 if (cchMax <= 4)
1483 {
1484 while (--cchMax > 0) /* No room left for anything but ellipses */
1485 *lpszDest++ = '.';
1486 *lpszDest = '\0';
1487 return TRUE;
1488 }
1489 cchMax -= 4;
1490 memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
1491 strcpyW(lpszDest + cchMax, szEllipses);
1492 return TRUE;
1493 }
1494
1495 /* Only the root needs to be Compacted */
1496 dwLen = cchMax - dwFileLen - 3;
1497 memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
1498 strcpyW(lpszDest + dwLen, szEllipses);
1499 strcpyW(lpszDest + dwLen + 3, lpszFile);
1500 return TRUE;
1501 }
1502
1503 /*************************************************************************
1504 * PathIsRelativeA [SHLWAPI.@]
1505 *
1506 * Determine if a path is a relative path.
1507 *
1508 * PARAMS
1509 * lpszPath [I] Path to check
1510 *
1511 * RETURNS
1512 * TRUE: The path is relative, or is invalid.
1513 * FALSE: The path is not relative.
1514 */
1515 BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
1516 {
1517 TRACE("(%s)\n",debugstr_a(lpszPath));
1518
1519 if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
1520 return TRUE;
1521 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1522 return FALSE;
1523 return TRUE;
1524 }
1525
1526 /*************************************************************************
1527 * PathIsRelativeW [SHLWAPI.@]
1528 *
1529 * See PathIsRelativeA.
1530 */
1531 BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
1532 {
1533 TRACE("(%s)\n",debugstr_w(lpszPath));
1534
1535 if (!lpszPath || !*lpszPath)
1536 return TRUE;
1537 if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
1538 return FALSE;
1539 return TRUE;
1540 }
1541
1542 /*************************************************************************
1543 * PathIsRootA [SHLWAPI.@]
1544 *
1545 * Determine if a path is a root path.
1546 *
1547 * PARAMS
1548 * lpszPath [I] Path to check
1549 *
1550 * RETURNS
1551 * TRUE If lpszPath is valid and a root path,
1552 * FALSE Otherwise
1553 */
1554 BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
1555 {
1556 TRACE("(%s)\n", debugstr_a(lpszPath));
1557
1558 if (lpszPath && *lpszPath)
1559 {
1560 if (*lpszPath == '\\')
1561 {
1562 if (!lpszPath[1])
1563 return TRUE; /* \ */
1564 else if (lpszPath[1]=='\\')
1565 {
1566 BOOL bSeenSlash = FALSE;
1567 lpszPath += 2;
1568
1569 /* Check for UNC root path */
1570 while (*lpszPath)
1571 {
1572 if (*lpszPath == '\\')
1573 {
1574 if (bSeenSlash)
1575 return FALSE;
1576 bSeenSlash = TRUE;
1577 }
1578 lpszPath = CharNextA(lpszPath);
1579 }
1580 return TRUE;
1581 }
1582 }
1583 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1584 return TRUE; /* X:\ */
1585 }
1586 return FALSE;
1587 }
1588
1589 /*************************************************************************
1590 * PathIsRootW [SHLWAPI.@]
1591 *
1592 * See PathIsRootA.
1593 */
1594 BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
1595 {
1596 TRACE("(%s)\n", debugstr_w(lpszPath));
1597
1598 if (lpszPath && *lpszPath)
1599 {
1600 if (*lpszPath == '\\')
1601 {
1602 if (!lpszPath[1])
1603 return TRUE; /* \ */
1604 else if (lpszPath[1]=='\\')
1605 {
1606 BOOL bSeenSlash = FALSE;
1607 lpszPath += 2;
1608
1609 /* Check for UNC root path */
1610 while (*lpszPath)
1611 {
1612 if (*lpszPath == '\\')
1613 {
1614 if (bSeenSlash)
1615 return FALSE;
1616 bSeenSlash = TRUE;
1617 }
1618 lpszPath++;
1619 }
1620 return TRUE;
1621 }
1622 }
1623 else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
1624 return TRUE; /* X:\ */
1625 }
1626 return FALSE;
1627 }
1628
1629 /*************************************************************************
1630 * PathIsDirectoryA [SHLWAPI.@]
1631 *
1632 * Determine if a path is a valid directory
1633 *
1634 * PARAMS
1635 * lpszPath [I] Path to check.
1636 *
1637 * RETURNS
1638 * FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
1639 * FALSE if lpszPath is invalid or not a directory.
1640 *
1641 * NOTES
1642 * Although this function is prototyped as returning a BOOL, it returns
1643 * FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
1644 *
1645 *| if (PathIsDirectoryA("c:\\windows\\") == TRUE)
1646 *| ...
1647 *
1648 * will always fail.
1649 */
1650 BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
1651 {
1652 DWORD dwAttr;
1653
1654 TRACE("(%s)\n", debugstr_a(lpszPath));
1655
1656 if (!lpszPath || PathIsUNCServerA(lpszPath))
1657 return FALSE;
1658
1659 if (PathIsUNCServerShareA(lpszPath))
1660 {
1661 FIXME("UNC Server Share not yet supported - FAILING\n");
1662 return FALSE;
1663 }
1664
1665 if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1666 return FALSE;
1667 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1668 }
1669
1670 /*************************************************************************
1671 * PathIsDirectoryW [SHLWAPI.@]
1672 *
1673 * See PathIsDirectoryA.
1674 */
1675 BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
1676 {
1677 DWORD dwAttr;
1678
1679 TRACE("(%s)\n", debugstr_w(lpszPath));
1680
1681 if (!lpszPath || PathIsUNCServerW(lpszPath))
1682 return FALSE;
1683
1684 if (PathIsUNCServerShareW(lpszPath))
1685 {
1686 FIXME("UNC Server Share not yet supported - FAILING\n");
1687 return FALSE;
1688 }
1689
1690 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1691 return FALSE;
1692 return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1693 }
1694
1695 /*************************************************************************
1696 * PathFileExistsA [SHLWAPI.@]
1697 *
1698 * Determine if a file exists.
1699 *
1700 * PARAMS
1701 * lpszPath [I] Path to check
1702 *
1703 * RETURNS
1704 * TRUE If the file exists and is readable
1705 * FALSE Otherwise
1706 */
1707 BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1708 {
1709 UINT iPrevErrMode;
1710 DWORD dwAttr;
1711
1712 TRACE("(%s)\n",debugstr_a(lpszPath));
1713
1714 if (!lpszPath)
1715 return FALSE;
1716
1717 /* Prevent a dialog box if path is on a disk that has been ejected. */
1718 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1719 dwAttr = GetFileAttributesA(lpszPath);
1720 SetErrorMode(iPrevErrMode);
1721 return dwAttr != INVALID_FILE_ATTRIBUTES;
1722 }
1723
1724 /*************************************************************************
1725 * PathFileExistsW [SHLWAPI.@]
1726 *
1727 * See PathFileExistsA.
1728 */
1729 BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1730 {
1731 UINT iPrevErrMode;
1732 DWORD dwAttr;
1733
1734 TRACE("(%s)\n",debugstr_w(lpszPath));
1735
1736 if (!lpszPath)
1737 return FALSE;
1738
1739 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1740 dwAttr = GetFileAttributesW(lpszPath);
1741 SetErrorMode(iPrevErrMode);
1742 return dwAttr != INVALID_FILE_ATTRIBUTES;
1743 }
1744
1745 /*************************************************************************
1746 * PathFileExistsAndAttributesA [SHLWAPI.445]
1747 *
1748 * Determine if a file exists.
1749 *
1750 * PARAMS
1751 * lpszPath [I] Path to check
1752 * dwAttr [O] attributes of file
1753 *
1754 * RETURNS
1755 * TRUE If the file exists and is readable
1756 * FALSE Otherwise
1757 */
1758 BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
1759 {
1760 UINT iPrevErrMode;
1761 DWORD dwVal = 0;
1762
1763 TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);
1764
1765 if (dwAttr)
1766 *dwAttr = INVALID_FILE_ATTRIBUTES;
1767
1768 if (!lpszPath)
1769 return FALSE;
1770
1771 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1772 dwVal = GetFileAttributesA(lpszPath);
1773 SetErrorMode(iPrevErrMode);
1774 if (dwAttr)
1775 *dwAttr = dwVal;
1776 return (dwVal != INVALID_FILE_ATTRIBUTES);
1777 }
1778
1779 /*************************************************************************
1780 * PathFileExistsAndAttributesW [SHLWAPI.446]
1781 *
1782 * See PathFileExistsA.
1783 */
1784 BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
1785 {
1786 UINT iPrevErrMode;
1787 DWORD dwVal;
1788
1789 TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);
1790
1791 if (!lpszPath)
1792 return FALSE;
1793
1794 iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1795 dwVal = GetFileAttributesW(lpszPath);
1796 SetErrorMode(iPrevErrMode);
1797 if (dwAttr)
1798 *dwAttr = dwVal;
1799 return (dwVal != INVALID_FILE_ATTRIBUTES);
1800 }
1801
1802 /*************************************************************************
1803 * PathMatchSingleMaskA [internal]
1804 */
1805 static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
1806 {
1807 while (*name && *mask && *mask!=';')
1808 {
1809 if (*mask == '*')
1810 {
1811 do
1812 {
1813 if (PathMatchSingleMaskA(name,mask+1))
1814 return TRUE; /* try substrings */
1815 } while (*name++);
1816 return FALSE;
1817 }
1818
1819 if (toupper(*mask) != toupper(*name) && *mask != '?')
1820 return FALSE;
1821
1822 name = CharNextA(name);
1823 mask = CharNextA(mask);
1824 }
1825
1826 if (!*name)
1827 {
1828 while (*mask == '*')
1829 mask++;
1830 if (!*mask || *mask == ';')
1831 return TRUE;
1832 }
1833 return FALSE;
1834 }
1835
1836 /*************************************************************************
1837 * PathMatchSingleMaskW [internal]
1838 */
1839 static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
1840 {
1841 while (*name && *mask && *mask != ';')
1842 {
1843 if (*mask == '*')
1844 {
1845 do
1846 {
1847 if (PathMatchSingleMaskW(name,mask+1))
1848 return TRUE; /* try substrings */
1849 } while (*name++);
1850 return FALSE;
1851 }
1852
1853 if (toupperW(*mask) != toupperW(*name) && *mask != '?')
1854 return FALSE;
1855
1856 name++;
1857 mask++;
1858 }
1859 if (!*name)
1860 {
1861 while (*mask == '*')
1862 mask++;
1863 if (!*mask || *mask == ';')
1864 return TRUE;
1865 }
1866 return FALSE;
1867 }
1868
1869 /*************************************************************************
1870 * PathMatchSpecA [SHLWAPI.@]
1871 *
1872 * Determine if a path matches one or more search masks.
1873 *
1874 * PARAMS
1875 * lpszPath [I] Path to check
1876 * lpszMask [I] Search mask(s)
1877 *
1878 * RETURNS
1879 * TRUE If lpszPath is valid and is matched
1880 * FALSE Otherwise
1881 *
1882 * NOTES
1883 * Multiple search masks may be given if they are separated by ";". The
1884 * pattern "*.*" is treated specially in that it matches all paths (for
1885 * backwards compatibility with DOS).
1886 */
1887 BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1888 {
1889 TRACE("(%s,%s)\n", lpszPath, lpszMask);
1890
1891 if (!lstrcmpA(lpszMask, "*.*"))
1892 return TRUE; /* Matches every path */
1893
1894 while (*lpszMask)
1895 {
1896 while (*lpszMask == ' ')
1897 lpszMask++; /* Eat leading spaces */
1898
1899 if (PathMatchSingleMaskA(lpszPath, lpszMask))
1900 return TRUE; /* Matches the current mask */
1901
1902 while (*lpszMask && *lpszMask != ';')
1903 lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1904
1905 if (*lpszMask == ';')
1906 lpszMask++;
1907 }
1908 return FALSE;
1909 }
1910
1911 /*************************************************************************
1912 * PathMatchSpecW [SHLWAPI.@]
1913 *
1914 * See PathMatchSpecA.
1915 */
1916 BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1917 {
1918 static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };
1919
1920 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));
1921
1922 if (!lstrcmpW(lpszMask, szStarDotStar))
1923 return TRUE; /* Matches every path */
1924
1925 while (*lpszMask)
1926 {
1927 while (*lpszMask == ' ')
1928 lpszMask++; /* Eat leading spaces */
1929
1930 if (PathMatchSingleMaskW(lpszPath, lpszMask))
1931 return TRUE; /* Matches the current path */
1932
1933 while (*lpszMask && *lpszMask != ';')
1934 lpszMask++; /* masks separated by ';' */
1935
1936 if (*lpszMask == ';')
1937 lpszMask++;
1938 }
1939 return FALSE;
1940 }
1941
1942 /*************************************************************************
1943 * PathIsSameRootA [SHLWAPI.@]
1944 *
1945 * Determine if two paths share the same root.
1946 *
1947 * PARAMS
1948 * lpszPath1 [I] Source path
1949 * lpszPath2 [I] Path to compare with
1950 *
1951 * RETURNS
1952 * TRUE If both paths are valid and share the same root.
1953 * FALSE If either path is invalid or the paths do not share the same root.
1954 */
1955 BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
1956 {
1957 LPCSTR lpszStart;
1958 int dwLen;
1959
1960 TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1961
1962 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
1963 return FALSE;
1964
1965 dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
1966 if (lpszStart - lpszPath1 > dwLen)
1967 return FALSE; /* Paths not common up to length of the root */
1968 return TRUE;
1969 }
1970
1971 /*************************************************************************
1972 * PathIsSameRootW [SHLWAPI.@]
1973 *
1974 * See PathIsSameRootA.
1975 */
1976 BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
1977 {
1978 LPCWSTR lpszStart;
1979 int dwLen;
1980
1981 TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1982
1983 if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
1984 return FALSE;
1985
1986 dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
1987 if (lpszStart - lpszPath1 > dwLen)
1988 return FALSE; /* Paths not common up to length of the root */
1989 return TRUE;
1990 }
1991
1992 /*************************************************************************
1993 * PathIsContentTypeA [SHLWAPI.@]
1994 *
1995 * Determine if a file is of a given registered content type.
1996 *
1997 * PARAMS
1998 * lpszPath [I] File to check
1999 * lpszContentType [I] Content type to check for
2000 *
2001 * RETURNS
2002 * TRUE If lpszPath is a given registered content type,
2003 * FALSE Otherwise.
2004 *
2005 * NOTES
2006 * This function looks up the registered content type for lpszPath. If
2007 * a content type is registered, it is compared (case insensitively) to
2008 * lpszContentType. Only if this matches does the function succeed.
2009 */
2010 BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
2011 {
2012 LPCSTR szExt;
2013 DWORD dwDummy;
2014 char szBuff[MAX_PATH];
2015
2016 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));
2017
2018 if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
2019 !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
2020 REG_NONE, szBuff, &dwDummy) &&
2021 !strcasecmp(lpszContentType, szBuff))
2022 {
2023 return TRUE;
2024 }
2025 return FALSE;
2026 }
2027
2028 /*************************************************************************
2029 * PathIsContentTypeW [SHLWAPI.@]
2030 *
2031 * See PathIsContentTypeA.
2032 */
2033 BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2034 {
2035 static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
2036 LPCWSTR szExt;
2037 DWORD dwDummy;
2038 WCHAR szBuff[MAX_PATH];
2039
2040 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));
2041
2042 if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
2043 !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
2044 REG_NONE, szBuff, &dwDummy) &&
2045 !strcmpiW(lpszContentType, szBuff))
2046 {
2047 return TRUE;
2048 }
2049 return FALSE;
2050 }
2051
2052 /*************************************************************************
2053 * PathIsFileSpecA [SHLWAPI.@]
2054 *
2055 * Determine if a path is a file specification.
2056 *
2057 * PARAMS
2058 * lpszPath [I] Path to check
2059 *
2060 * RETURNS
2061 * TRUE If lpszPath is a file specification (i.e. Contains no directories).
2062 * FALSE Otherwise.
2063 */
2064 BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2065 {
2066 TRACE("(%s)\n", debugstr_a(lpszPath));
2067
2068 if (!lpszPath)
2069 return FALSE;
2070
2071 while (*lpszPath)
2072 {
2073 if (*lpszPath == '\\' || *lpszPath == ':')
2074 return FALSE;
2075 lpszPath = CharNextA(lpszPath);
2076 }
2077 return TRUE;
2078 }
2079
2080 /*************************************************************************
2081 * PathIsFileSpecW [SHLWAPI.@]
2082 *
2083 * See PathIsFileSpecA.
2084 */
2085 BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2086 {
2087 TRACE("(%s)\n", debugstr_w(lpszPath));
2088
2089 if (!lpszPath)
2090 return FALSE;
2091
2092 while (*lpszPath)
2093 {
2094 if (*lpszPath == '\\' || *lpszPath == ':')
2095 return FALSE;
2096 lpszPath++;
2097 }
2098 return TRUE;
2099 }
2100
2101 /*************************************************************************
2102 * PathIsPrefixA [SHLWAPI.@]
2103 *
2104 * Determine if a path is a prefix of another.
2105 *
2106 * PARAMS
2107 * lpszPrefix [I] Prefix
2108 * lpszPath [I] Path to check
2109 *
2110 * RETURNS
2111 * TRUE If lpszPath has lpszPrefix as its prefix,
2112 * FALSE If either path is NULL or lpszPrefix is not a prefix
2113 */
2114 BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2115 {
2116 TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));
2117
2118 if (lpszPrefix && lpszPath &&
2119 PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2120 return TRUE;
2121 return FALSE;
2122 }
2123
2124 /*************************************************************************
2125 * PathIsPrefixW [SHLWAPI.@]
2126 *
2127 * See PathIsPrefixA.
2128 */
2129 BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2130 {
2131 TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));
2132
2133 if (lpszPrefix && lpszPath &&
2134 PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2135 return TRUE;
2136 return FALSE;
2137 }
2138
2139 /*************************************************************************
2140 * PathIsSystemFolderA [SHLWAPI.@]
2141 *
2142 * Determine if a path or file attributes are a system folder.
2143 *
2144 * PARAMS
2145 * lpszPath [I] Path to check.
2146 * dwAttrib [I] Attributes to check, if lpszPath is NULL.
2147 *
2148 * RETURNS
2149 * TRUE If lpszPath or dwAttrib are a system folder.
2150 * FALSE If GetFileAttributesA() fails or neither parameter is a system folder.
2151 */
2152 BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2153 {
2154 TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2155
2156 if (lpszPath && *lpszPath)
2157 dwAttrib = GetFileAttributesA(lpszPath);
2158
2159 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2160 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2161 return FALSE;
2162 return TRUE;
2163 }
2164
2165 /*************************************************************************
2166 * PathIsSystemFolderW [SHLWAPI.@]
2167 *
2168 * See PathIsSystemFolderA.
2169 */
2170 BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2171 {
2172 TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2173
2174 if (lpszPath && *lpszPath)
2175 dwAttrib = GetFileAttributesW(lpszPath);
2176
2177 if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2178 !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
2179 return FALSE;
2180 return TRUE;
2181 }
2182
2183 /*************************************************************************
2184 * PathIsUNCA [SHLWAPI.@]
2185 *
2186 * Determine if a path is in UNC format.
2187 *
2188 * PARAMS
2189 * lpszPath [I] Path to check
2190 *
2191 * RETURNS
2192 * TRUE: The path is UNC.
2193 * FALSE: The path is not UNC or is NULL.
2194 */
2195 BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2196 {
2197 TRACE("(%s)\n",debugstr_a(lpszPath));
2198
2199 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\') && (lpszPath[2]!='?'))
2200 return TRUE;
2201 return FALSE;
2202 }
2203
2204 /*************************************************************************
2205 * PathIsUNCW [SHLWAPI.@]
2206 *
2207 * See PathIsUNCA.
2208 */
2209 BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2210 {
2211 TRACE("(%s)\n",debugstr_w(lpszPath));
2212
2213 if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\') && (lpszPath[2]!='?'))
2214 return TRUE;
2215 return FALSE;
2216 }
2217
2218 /*************************************************************************
2219 * PathIsUNCServerA [SHLWAPI.@]
2220 *
2221 * Determine if a path is a UNC server name ("\\SHARENAME").
2222 *
2223 * PARAMS
2224 * lpszPath [I] Path to check.
2225 *
2226 * RETURNS
2227 * TRUE If lpszPath is a valid UNC server name.
2228 * FALSE Otherwise.
2229 *
2230 * NOTES
2231 * This routine is bug compatible with Win32: Server names with a
2232 * trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
2233 * Fixing this bug may break other shlwapi functions!
2234 */
2235 BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2236 {
2237 TRACE("(%s)\n", debugstr_a(lpszPath));
2238
2239 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2240 {
2241 while (*lpszPath)
2242 {
2243 if (*lpszPath == '\\')
2244 return FALSE;
2245 lpszPath = CharNextA(lpszPath);
2246 }
2247 return TRUE;
2248 }
2249 return FALSE;
2250 }
2251
2252 /*************************************************************************
2253 * PathIsUNCServerW [SHLWAPI.@]
2254 *
2255 * See PathIsUNCServerA.
2256 */
2257 BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2258 {
2259 TRACE("(%s)\n", debugstr_w(lpszPath));
2260
2261 if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2262 {
2263 return !strchrW( lpszPath + 2, '\\' );
2264 }
2265 return FALSE;
2266 }
2267
2268 /*************************************************************************
2269 * PathIsUNCServerShareA [SHLWAPI.@]
2270 *
2271 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
2272 *
2273 * PARAMS
2274 * lpszPath [I] Path to check.
2275 *
2276 * RETURNS
2277 * TRUE If lpszPath is a valid UNC server share.
2278 * FALSE Otherwise.
2279 *
2280 * NOTES
2281 * This routine is bug compatible with Win32: Server shares with a
2282 * trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
2283 * Fixing this bug may break other shlwapi functions!
2284 */
2285 BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
2286 {
2287 TRACE("(%s)\n", debugstr_a(lpszPath));
2288
2289 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2290 {
2291 BOOL bSeenSlash = FALSE;
2292 while (*lpszPath)
2293 {
2294 if (*lpszPath == '\\')
2295 {
2296 if (bSeenSlash)
2297 return FALSE;
2298 bSeenSlash = TRUE;
2299 }
2300 lpszPath = CharNextA(lpszPath);
2301 }
2302 return bSeenSlash;
2303 }
2304 return FALSE;
2305 }
2306
2307 /*************************************************************************
2308 * PathIsUNCServerShareW [SHLWAPI.@]
2309 *
2310 * See PathIsUNCServerShareA.
2311 */
2312 BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2313 {
2314 TRACE("(%s)\n", debugstr_w(lpszPath));
2315
2316 if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
2317 {
2318 BOOL bSeenSlash = FALSE;
2319 while (*lpszPath)
2320 {
2321 if (*lpszPath == '\\')
2322 {
2323 if (bSeenSlash)
2324 return FALSE;
2325 bSeenSlash = TRUE;
2326 }
2327 lpszPath++;
2328 }
2329 return bSeenSlash;
2330 }
2331 return FALSE;
2332 }
2333
2334 /*************************************************************************
2335 * PathCanonicalizeA [SHLWAPI.@]
2336 *
2337 * Convert a path to its canonical form.
2338 *
2339 * PARAMS
2340 * lpszBuf [O] Output path
2341 * lpszPath [I] Path to canonicalize
2342 *
2343 * RETURNS
2344 * Success: TRUE. lpszBuf contains the output path,
2345 * Failure: FALSE, If input path is invalid. lpszBuf is undefined
2346 */
2347 BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2348 {
2349 BOOL bRet = FALSE;
2350
2351 TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2352
2353 if (lpszBuf)
2354 *lpszBuf = '\0';
2355
2356 if (!lpszBuf || !lpszPath)
2357 SetLastError(ERROR_INVALID_PARAMETER);
2358 else
2359 {
2360 WCHAR szPath[MAX_PATH];
2361 WCHAR szBuff[MAX_PATH];
2362 int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2363
2364 if (!ret) {
2365 WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
2366 return FALSE;
2367 }
2368 bRet = PathCanonicalizeW(szBuff, szPath);
2369 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2370 }
2371 return bRet;
2372 }
2373
2374
2375 /*************************************************************************
2376 * PathCanonicalizeW [SHLWAPI.@]
2377 *
2378 * See PathCanonicalizeA.
2379 */
2380 BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
2381 {
2382 LPWSTR lpszDst = lpszBuf;
2383 LPCWSTR lpszSrc = lpszPath;
2384
2385 TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));
2386
2387 if (lpszBuf)
2388 *lpszDst = '\0';
2389
2390 if (!lpszBuf || !lpszPath)
2391 {
2392 SetLastError(ERROR_INVALID_PARAMETER);
2393 return FALSE;
2394 }
2395
2396 if (!*lpszPath)
2397 {
2398 *lpszBuf++ = '\\';
2399 *lpszBuf = '\0';
2400 return TRUE;
2401 }
2402
2403 /* Copy path root */
2404 if (*lpszSrc == '\\')
2405 {
2406 *lpszDst++ = *lpszSrc++;
2407 }
2408 else if (*lpszSrc && lpszSrc[1] == ':')
2409 {
2410 /* X:\ */
2411 *lpszDst++ = *lpszSrc++;
2412 *lpszDst++ = *lpszSrc++;
2413 if (*lpszSrc == '\\')
2414 *lpszDst++ = *lpszSrc++;
2415 }
2416
2417 /* Canonicalize the rest of the path */
2418 while (*lpszSrc)
2419 {
2420 if (*lpszSrc == '.')
2421 {
2422 if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
2423 {
2424 lpszSrc += 2; /* Skip .\ */
2425 }
2426 else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
2427 {
2428 /* \.. backs up a directory, over the root if it has no \ following X:.
2429 * .. is ignored if it would remove a UNC server name or initial \\
2430 */
2431 if (lpszDst != lpszBuf)
2432 {
2433 *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
2434 if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
2435 (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
2436 {
2437 if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
2438 {
2439 lpszDst -= 2;
2440 while (lpszDst > lpszBuf && *lpszDst != '\\')
2441 lpszDst--;
2442 if (*lpszDst == '\\')
2443 lpszDst++; /* Reset to last '\' */
2444 else
2445 lpszDst = lpszBuf; /* Start path again from new root */
2446 }
2447 else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
2448 lpszDst -= 2;
2449 }
2450 while (lpszDst > lpszBuf && *lpszDst != '\\')
2451 lpszDst--;
2452 if (lpszDst == lpszBuf)
2453 {
2454 *lpszDst++ = '\\';
2455 lpszSrc++;
2456 }
2457 }
2458 lpszSrc += 2; /* Skip .. in src path */
2459 }
2460 else
2461 *lpszDst++ = *lpszSrc++;
2462 }
2463 else
2464 *lpszDst++ = *lpszSrc++;
2465 }
2466 /* Append \ to naked drive specs */
2467 if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
2468 *lpszDst++ = '\\';
2469 *lpszDst++ = '\0';
2470 return TRUE;
2471 }
2472
2473 /*************************************************************************
2474 * PathFindNextComponentA [SHLWAPI.@]
2475 *
2476 * Find the next component in a path.
2477 *
2478 * PARAMS
2479 * lpszPath [I] Path to find next component in
2480 *
2481 * RETURNS
2482 * Success: A pointer to the next component, or the end of the string.
2483 * Failure: NULL, If lpszPath is invalid
2484 *
2485 * NOTES
2486 * A 'component' is either a backslash character (\) or UNC marker (\\).
2487 * Because of this, relative paths (e.g "c:foo") are regarded as having
2488 * only one component.
2489 */
2490 LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2491 {
2492 LPSTR lpszSlash;
2493
2494 TRACE("(%s)\n", debugstr_a(lpszPath));
2495
2496 if(!lpszPath || !*lpszPath)
2497 return NULL;
2498
2499 if ((lpszSlash = StrChrA(lpszPath, '\\')))
2500 {
2501 if (lpszSlash[1] == '\\')
2502 lpszSlash++;
2503 return lpszSlash + 1;
2504 }
2505 return (LPSTR)lpszPath + strlen(lpszPath);
2506 }
2507
2508 /*************************************************************************
2509 * PathFindNextComponentW [SHLWAPI.@]
2510 *
2511 * See PathFindNextComponentA.
2512 */
2513 LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2514 {
2515 LPWSTR lpszSlash;
2516
2517 TRACE("(%s)\n", debugstr_w(lpszPath));
2518
2519 if(!lpszPath || !*lpszPath)
2520 return NULL;
2521
2522 if ((lpszSlash = StrChrW(lpszPath, '\\')))
2523 {
2524 if (lpszSlash[1] == '\\')
2525 lpszSlash++;
2526 return lpszSlash + 1;
2527 }
2528 return (LPWSTR)lpszPath + strlenW(lpszPath);
2529 }
2530
2531 /*************************************************************************
2532 * PathAddExtensionA [SHLWAPI.@]
2533 *
2534 * Add a file extension to a path
2535 *
2536 * PARAMS
2537 * lpszPath [I/O] Path to add extension to
2538 * lpszExtension [I] Extension to add to lpszPath
2539 *
2540 * RETURNS
2541 * TRUE If the path was modified,
2542 * FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2543 * extension already, or the new path length is too big.
2544 *
2545 * FIXME
2546 * What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2547 * does not do this, so the behaviour was removed.
2548 */
2549 BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2550 {
2551 size_t dwLen;
2552
2553 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));
2554
2555 if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
2556 return FALSE;
2557
2558 dwLen = strlen(lpszPath);
2559
2560 if (dwLen + strlen(lpszExtension) >= MAX_PATH)
2561 return FALSE;
2562
2563 strcpy(lpszPath + dwLen, lpszExtension);
2564 return TRUE;
2565 }
2566
2567 /*************************************************************************
2568 * PathAddExtensionW [SHLWAPI.@]
2569 *
2570 * See PathAddExtensionA.
2571 */
2572 BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2573 {
2574 size_t dwLen;
2575
2576 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2577
2578 if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
2579 return FALSE;
2580
2581 dwLen = strlenW(lpszPath);
2582
2583 if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
2584 return FALSE;
2585
2586 strcpyW(lpszPath + dwLen, lpszExtension);
2587 return TRUE;
2588 }
2589
2590 /*************************************************************************
2591 * PathMakePrettyA [SHLWAPI.@]
2592 *
2593 * Convert an uppercase DOS filename into lowercase.
2594 *
2595 * PARAMS
2596 * lpszPath [I/O] Path to convert.
2597 *
2598 * RETURNS
2599 * TRUE If the path was an uppercase DOS path and was converted,
2600 * FALSE Otherwise.
2601 */
2602 BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2603 {
2604 LPSTR pszIter = lpszPath;
2605
2606 TRACE("(%s)\n", debugstr_a(lpszPath));
2607
2608 if (!pszIter)
2609 return FALSE;
2610
2611 if (*pszIter)
2612 {
2613 do
2614 {
2615 if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
2616 return FALSE; /* Not DOS path */
2617 pszIter++;
2618 } while (*pszIter);
2619 pszIter = lpszPath + 1;
2620 while (*pszIter)
2621 {
2622 *pszIter = tolower(*pszIter);
2623 pszIter++;
2624 }
2625 }
2626 return TRUE;
2627 }
2628
2629 /*************************************************************************
2630 * PathMakePrettyW [SHLWAPI.@]
2631 *
2632 * See PathMakePrettyA.
2633 */
2634 BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2635 {
2636 LPWSTR pszIter = lpszPath;
2637
2638 TRACE("(%s)\n", debugstr_w(lpszPath));
2639
2640 if (!pszIter)
2641 return FALSE;
2642
2643 if (*pszIter)
2644 {
2645 do
2646 {
2647 if (islowerW(*pszIter))
2648 return FALSE; /* Not DOS path */
2649 pszIter++;
2650 } while (*pszIter);
2651 pszIter = lpszPath + 1;
2652 while (*pszIter)
2653 {
2654 *pszIter = tolowerW(*pszIter);
2655 pszIter++;
2656 }
2657 }
2658 return TRUE;
2659 }
2660
2661 /*************************************************************************
2662 * PathCommonPrefixA [SHLWAPI.@]
2663 *
2664 * Determine the length of the common prefix between two paths.
2665 *
2666 * PARAMS
2667 * lpszFile1 [I] First path for comparison
2668 * lpszFile2 [I] Second path for comparison
2669 * achPath [O] Destination for common prefix string
2670 *
2671 * RETURNS
2672 * The length of the common prefix. This is 0 if there is no common
2673 * prefix between the paths or if any parameters are invalid. If the prefix
2674 * is non-zero and achPath is not NULL, achPath is filled with the common
2675 * part of the prefix and NUL terminated.
2676 *
2677 * NOTES
2678 * A common prefix of 2 is always returned as 3. It is thus possible for
2679 * the length returned to be invalid (i.e. Longer than one or both of the
2680 * strings given as parameters). This Win32 behaviour has been implemented
2681 * here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
2682 * To work around this when using this function, always check that the byte
2683 * at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2684 */
2685 int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2686 {
2687 size_t iLen = 0;
2688 LPCSTR lpszIter1 = lpszFile1;
2689 LPCSTR lpszIter2 = lpszFile2;
2690
2691 TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);
2692
2693 if (achPath)
2694 *achPath = '\0';
2695
2696 if (!lpszFile1 || !lpszFile2)
2697 return 0;
2698
2699 /* Handle roots first */
2700 if (PathIsUNCA(lpszFile1))
2701 {
2702 if (!PathIsUNCA(lpszFile2))
2703 return 0;
2704 lpszIter1 += 2;
2705 lpszIter2 += 2;
2706 }
2707 else if (PathIsUNCA(lpszFile2))
2708 return 0; /* Know already lpszFile1 is not UNC */
2709
2710 do
2711 {
2712 /* Update len */
2713 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2714 (!*lpszIter2 || *lpszIter2 == '\\'))
2715 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2716
2717 if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
2718 break; /* Strings differ at this point */
2719
2720 lpszIter1++;
2721 lpszIter2++;
2722 } while (1);
2723
2724 if (iLen == 2)
2725 iLen++; /* Feature/Bug compatible with Win32 */
2726
2727 if (iLen && achPath)
2728 {
2729 memcpy(achPath,lpszFile1,iLen);
2730 achPath[iLen] = '\0';
2731 }
2732 return iLen;
2733 }
2734
2735 /*************************************************************************
2736 * PathCommonPrefixW [SHLWAPI.@]
2737 *
2738 * See PathCommonPrefixA.
2739 */
2740 int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2741 {
2742 size_t iLen = 0;
2743 LPCWSTR lpszIter1 = lpszFile1;
2744 LPCWSTR lpszIter2 = lpszFile2;
2745
2746 TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);
2747
2748 if (achPath)
2749 *achPath = '\0';
2750
2751 if (!lpszFile1 || !lpszFile2)
2752 return 0;
2753
2754 /* Handle roots first */
2755 if (PathIsUNCW(lpszFile1))
2756 {
2757 if (!PathIsUNCW(lpszFile2))
2758 return 0;
2759 lpszIter1 += 2;
2760 lpszIter2 += 2;
2761 }
2762 else if (PathIsUNCW(lpszFile2))
2763 return 0; /* Know already lpszFile1 is not UNC */
2764
2765 do
2766 {
2767 /* Update len */
2768 if ((!*lpszIter1 || *lpszIter1 == '\\') &&
2769 (!*lpszIter2 || *lpszIter2 == '\\'))
2770 iLen = lpszIter1 - lpszFile1; /* Common to this point */
2771
2772 if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
2773 break; /* Strings differ at this point */
2774
2775 lpszIter1++;
2776 lpszIter2++;
2777 } while (1);
2778
2779 if (iLen == 2)
2780 iLen++; /* Feature/Bug compatible with Win32 */
2781
2782 if (iLen && achPath)
2783 {
2784 memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
2785 achPath[iLen] = '\0';
2786 }
2787 return iLen;
2788 }
2789
2790 /*************************************************************************
2791 * PathCompactPathA [SHLWAPI.@]
2792 *
2793 * Make a path fit into a given width when printed to a DC.
2794 *
2795 * PARAMS
2796 * hDc [I] Destination DC
2797 * lpszPath [I/O] Path to be printed to hDc
2798 * dx [I] Desired width
2799 *
2800 * RETURNS
2801 * TRUE If the path was modified/went well.
2802 * FALSE Otherwise.
2803 */
2804 BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2805 {
2806 BOOL bRet = FALSE;
2807
2808 TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2809
2810 if (lpszPath)
2811 {
2812 WCHAR szPath[MAX_PATH];
2813 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2814 bRet = PathCompactPathW(hDC, szPath, dx);
2815 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2816 }
2817 return bRet;
2818 }
2819
2820 /*************************************************************************
2821 * PathCompactPathW [SHLWAPI.@]
2822 *
2823 * See PathCompactPathA.
2824 */
2825 BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
2826 {
2827 static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
2828 BOOL bRet = TRUE;
2829 HDC hdc = 0;
2830 WCHAR buff[MAX_PATH];
2831 SIZE size;
2832 DWORD dwLen;
2833
2834 TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2835
2836 if (!lpszPath)
2837 return FALSE;
2838
2839 if (!hDC)
2840 hdc = hDC = GetDC(0);
2841
2842 /* Get the length of the whole path */
2843 dwLen = strlenW(lpszPath);
2844 GetTextExtentPointW(hDC, lpszPath, dwLen, &size);
2845
2846 if ((UINT)size.cx > dx)
2847 {
2848 /* Path too big, must reduce it */
2849 LPWSTR sFile;
2850 DWORD dwEllipsesLen = 0, dwPathLen = 0;
2851
2852 sFile = PathFindFileNameW(lpszPath);
2853 if (sFile != lpszPath) sFile--;
2854
2855 /* Get the size of ellipses */
2856 GetTextExtentPointW(hDC, szEllipses, 3, &size);
2857 dwEllipsesLen = size.cx;
2858 /* Get the size of the file name */
2859 GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
2860 dwPathLen = size.cx;
2861
2862 if (sFile != lpszPath)
2863 {
2864 LPWSTR sPath = sFile;
2865 BOOL bEllipses = FALSE;
2866
2867 /* The path includes a file name. Include as much of the path prior to
2868 * the file name as possible, allowing for the ellipses, e.g:
2869 * c:\some very long path\filename ==> c:\some v...\filename
2870 */
2871 lstrcpynW(buff, sFile, MAX_PATH);
2872
2873 do
2874 {
2875 DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;
2876
2877 GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
2878 dwTotalLen += size.cx;
2879 if (dwTotalLen <= dx)
2880 break;
2881 sPath--;
2882 if (!bEllipses)
2883 {
2884 bEllipses = TRUE;
2885 sPath -= 2;
2886 }
2887 } while (sPath > lpszPath);
2888
2889 if (sPath > lpszPath)
2890 {
2891 if (bEllipses)
2892 {
2893 strcpyW(sPath, szEllipses);
2894 strcpyW(sPath+3, buff);
2895 }
2896 bRet = TRUE;
2897 goto end;
2898 }
2899 strcpyW(lpszPath, szEllipses);
2900 strcpyW(lpszPath+3, buff);
2901 bRet = FALSE;
2902 goto end;
2903 }
2904
2905 /* Trim the path by adding ellipses to the end, e.g:
2906 * A very long file name.txt ==> A very...
2907 */
2908 dwLen = strlenW(lpszPath);
2909
2910 if (dwLen > MAX_PATH - 3)
2911 dwLen = MAX_PATH - 3;
2912 lstrcpynW(buff, sFile, dwLen);
2913
2914 do {
2915 dwLen--;
2916 GetTextExtentPointW(hDC, buff, dwLen, &size);
2917 } while (dwLen && size.cx + dwEllipsesLen > dx);
2918
2919 if (!dwLen)
2920 {
2921 DWORD dwWritten = 0;
2922
2923 dwEllipsesLen /= 3; /* Size of a single '.' */
2924
2925 /* Write as much of the Ellipses string as possible */
2926 while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
2927 {
2928 *lpszPath++ = '.';
2929 dwWritten += dwEllipsesLen;
2930 dwLen++;
2931 }
2932 *lpszPath = '\0';
2933 bRet = FALSE;
2934 }
2935 else
2936 {
2937 strcpyW(buff + dwLen, szEllipses);
2938 strcpyW(lpszPath, buff);
2939 }
2940 }
2941
2942 end:
2943 if (hdc)
2944 ReleaseDC(0, hdc);
2945
2946 return bRet;
2947 }
2948
2949 /*************************************************************************
2950 * PathGetCharTypeA [SHLWAPI.@]
2951 *
2952 * Categorise a character from a file path.
2953 *
2954 * PARAMS
2955 * ch [I] Character to get the type of
2956 *
2957 * RETURNS
2958 * A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2959 */
2960 UINT WINAPI PathGetCharTypeA(UCHAR ch)
2961 {
2962 return PathGetCharTypeW(ch);
2963 }
2964
2965 /*************************************************************************
2966 * PathGetCharTypeW [SHLWAPI.@]
2967 *
2968 * See PathGetCharTypeA.
2969 */
2970 UINT WINAPI PathGetCharTypeW(WCHAR ch)
2971 {
2972 UINT flags = 0;
2973
2974 TRACE("(%d)\n", ch);
2975
2976 if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2977 ch == '"' || ch == '|' || ch == '/')
2978 flags = GCT_INVALID; /* Invalid */
2979 else if (ch == '*' || ch=='?')
2980 flags = GCT_WILD; /* Wildchars */
2981 else if ((ch == '\\') || (ch == ':'))
2982 return GCT_SEPARATOR; /* Path separators */
2983 else
2984 {
2985 if (ch < 126)
2986 {
2987 if (((ch & 0x1) && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2988 ch == '.' || ch == '@' || ch == '^' ||
2989 ch == '\'' || ch == 130 || ch == '`')
2990 flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
2991 }
2992 else
2993 flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2994 flags |= GCT_LFNCHAR; /* Valid for long file names */
2995 }
2996 return flags;
2997 }
2998
2999 /*************************************************************************
3000 * SHLWAPI_UseSystemForSystemFolders
3001 *
3002 * Internal helper for PathMakeSystemFolderW.
3003 */
3004 static BOOL SHLWAPI_UseSystemForSystemFolders(void)
3005 {
3006 static BOOL bCheckedReg = FALSE;
3007 static BOOL bUseSystemForSystemFolders = FALSE;
3008
3009 if (!bCheckedReg)
3010 {
3011 bCheckedReg = TRUE;
3012
3013 /* Key tells Win what file attributes to use on system folders */
3014 if (SHGetValueA(HKEY_LOCAL_MACHINE,
3015 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
3016 "UseSystemForSystemFolders", 0, 0, 0))
3017 bUseSystemForSystemFolders = TRUE;
3018 }
3019 return bUseSystemForSystemFolders;
3020 }
3021
3022 /*************************************************************************
3023 * PathMakeSystemFolderA [SHLWAPI.@]
3024 *
3025 * Set system folder attribute for a path.
3026 *
3027 * PARAMS
3028 * lpszPath [I] The path to turn into a system folder
3029 *
3030 * RETURNS
3031 * TRUE If the path was changed to/already was a system folder
3032 * FALSE If the path is invalid or SetFileAttributesA() fails
3033 */
3034 BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3035 {
3036 BOOL bRet = FALSE;
3037
3038 TRACE("(%s)\n", debugstr_a(lpszPath));
3039
3040 if (lpszPath && *lpszPath)
3041 {
3042 WCHAR szPath[MAX_PATH];
3043 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3044 bRet = PathMakeSystemFolderW(szPath);
3045 }
3046 return bRet;
3047 }
3048
3049 /*************************************************************************
3050 * PathMakeSystemFolderW [SHLWAPI.@]
3051 *
3052 * See PathMakeSystemFolderA.
3053 */
3054 BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3055 {
3056 DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
3057 WCHAR buff[MAX_PATH];
3058
3059 TRACE("(%s)\n", debugstr_w(lpszPath));
3060
3061 if (!lpszPath || !*lpszPath)
3062 return FALSE;
3063
3064 /* If the directory is already a system directory, don't do anything */
3065 GetSystemDirectoryW(buff, MAX_PATH);
3066 if (!strcmpW(buff, lpszPath))
3067 return TRUE;
3068
3069 GetWindowsDirectoryW(buff, MAX_PATH);
3070 if (!strcmpW(buff, lpszPath))
3071 return TRUE;
3072
3073 /* "UseSystemForSystemFolders" Tells Win what attributes to use */
3074 if (SHLWAPI_UseSystemForSystemFolders())
3075 dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;
3076
3077 if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3078 return FALSE;
3079
3080 /* Change file attributes to system attributes */
3081 dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
3082 return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3083 }
3084
3085 /*************************************************************************
3086 * PathRenameExtensionA [SHLWAPI.@]
3087 *
3088 * Swap the file extension in a path with another extension.
3089 *
3090 * PARAMS
3091 * lpszPath [I/O] Path to swap the extension in
3092 * lpszExt [I] The new extension
3093 *
3094 * RETURNS
3095 * TRUE if lpszPath was modified,
3096 * FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3097 */
3098 BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3099 {
3100 LPSTR lpszExtension;
3101
3102 TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));
3103
3104 lpszExtension = PathFindExtensionA(lpszPath);
3105
3106 if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
3107 return FALSE;
3108
3109 strcpy(lpszExtension, lpszExt);
3110 return TRUE;
3111 }
3112
3113 /*************************************************************************
3114 * PathRenameExtensionW [SHLWAPI.@]
3115 *
3116 * See PathRenameExtensionA.
3117 */
3118 BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3119 {
3120 LPWSTR lpszExtension;
3121
3122 TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));
3123
3124 lpszExtension = PathFindExtensionW(lpszPath);
3125
3126 if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
3127 return FALSE;
3128
3129 strcpyW(lpszExtension, lpszExt);
3130 return TRUE;
3131 }
3132
3133 /*************************************************************************
3134 * PathSearchAndQualifyA [SHLWAPI.@]
3135 *
3136 * Determine if a given path is correct and fully qualified.
3137 *
3138 * PARAMS
3139 * lpszPath [I] Path to check
3140 * lpszBuf [O] Output for correct path
3141 * cchBuf [I] Size of lpszBuf
3142 *
3143 * RETURNS
3144 * Unknown.
3145 */
3146 BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3147 {
3148 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);
3149
3150 if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3151 return TRUE;
3152 return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3153 }
3154
3155 /*************************************************************************
3156 * PathSearchAndQualifyW [SHLWAPI.@]
3157 *
3158 * See PathSearchAndQualifyA.
3159 */
3160 BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3161 {
3162 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);
3163
3164 if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
3165 return TRUE;
3166 return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3167 }
3168
3169 /*************************************************************************
3170 * PathSkipRootA [SHLWAPI.@]
3171 *
3172 * Return the portion of a path following the drive letter or mount point.
3173 *
3174 * PARAMS
3175 * lpszPath [I] The path to skip on
3176 *
3177 * RETURNS
3178 * Success: A pointer to the next character after the root.
3179 * Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3180 */
3181 LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3182 {
3183 TRACE("(%s)\n", debugstr_a(lpszPath));
3184
3185 if (!lpszPath || !*lpszPath)
3186 return NULL;
3187
3188 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3189 {
3190 /* Network share: skip share server and mount point */
3191 lpszPath += 2;
3192 if ((lpszPath = StrChrA(lpszPath, '\\')) &&
3193 (lpszPath = StrChrA(lpszPath + 1, '\\')))
3194 lpszPath++;
3195 return (LPSTR)lpszPath;
3196 }
3197
3198 if (IsDBCSLeadByte(*lpszPath))
3199 return NULL;
3200
3201 /* Check x:\ */
3202 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3203 return (LPSTR)lpszPath + 3;
3204 return NULL;
3205 }
3206
3207 /*************************************************************************
3208 * PathSkipRootW [SHLWAPI.@]
3209 *
3210 * See PathSkipRootA.
3211 */
3212 LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3213 {
3214 TRACE("(%s)\n", debugstr_w(lpszPath));
3215
3216 if (!lpszPath || !*lpszPath)
3217 return NULL;
3218
3219 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3220 {
3221 /* Network share: skip share server and mount point */
3222 lpszPath += 2;
3223 if ((lpszPath = StrChrW(lpszPath, '\\')) &&
3224 (lpszPath = StrChrW(lpszPath + 1, '\\')))
3225 lpszPath++;
3226 return (LPWSTR)lpszPath;
3227 }
3228
3229 /* Check x:\ */
3230 if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
3231 return (LPWSTR)lpszPath + 3;
3232 return NULL;
3233 }
3234
3235 /*************************************************************************
3236 * PathCreateFromUrlA [SHLWAPI.@]
3237 *
3238 * See PathCreateFromUrlW
3239 */
3240 HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
3241 LPDWORD pcchPath, DWORD dwReserved)
3242 {
3243 WCHAR bufW[MAX_PATH];
3244 WCHAR *pathW = bufW;
3245 UNICODE_STRING urlW;
3246 HRESULT ret;
3247 DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;
3248
3249 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3250 return E_INVALIDARG;
3251
3252 if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
3253 return E_INVALIDARG;
3254 if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
3255 pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
3256 ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
3257 }
3258 if(ret == S_OK) {
3259 RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
3260 if(*pcchPath > lenA) {
3261 RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
3262 pszPath[lenA] = 0;
3263 *pcchPath = lenA;
3264 } else {
3265 *pcchPath = lenA + 1;
3266 ret = E_POINTER;
3267 }
3268 }
3269 if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
3270 RtlFreeUnicodeString(&urlW);
3271 return ret;
3272 }
3273
3274 /*************************************************************************
3275 * PathCreateFromUrlW [SHLWAPI.@]
3276 *
3277 * Create a path from a URL
3278 *
3279 * PARAMS
3280 * lpszUrl [I] URL to convert into a path
3281 * lpszPath [O] Output buffer for the resulting Path
3282 * pcchPath [I] Length of lpszPath
3283 * dwFlags [I] Flags controlling the conversion
3284 *
3285 * RETURNS
3286 * Success: S_OK. lpszPath contains the URL in path format,
3287 * Failure: An HRESULT error code such as E_INVALIDARG.
3288 */
3289 HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
3290 LPDWORD pcchPath, DWORD dwReserved)
3291 {
3292 static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
3293 static const WCHAR localhost[] = { 'l','o','c','a','l','h','o','s','t',0 };
3294 DWORD nslashes, unescape, len;
3295 const WCHAR *src;
3296 WCHAR *tpath, *dst;
3297 HRESULT ret;
3298
3299 TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3300
3301 if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
3302 return E_INVALIDARG;
3303
3304 if (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pszUrl, 5,
3305 file_colon, 5) != CSTR_EQUAL)
3306 return E_INVALIDARG;
3307 pszUrl += 5;
3308 ret = S_OK;
3309
3310 src = pszUrl;
3311 nslashes = 0;
3312 while (*src == '/' || *src == '\\') {
3313 nslashes++;
3314 src++;
3315 }
3316
3317 /* We need a temporary buffer so we can compute what size to ask for.
3318 * We know that the final string won't be longer than the current pszUrl
3319 * plus at most two backslashes. All the other transformations make it
3320 * shorter.
3321 */
3322 len = 2 + lstrlenW(pszUrl) + 1;
3323 if (*pcchPath < len)
3324 tpath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3325 else
3326 tpath = pszPath;
3327
3328 len = 0;
3329 dst = tpath;
3330 unescape = 1;
3331 switch (nslashes)
3332 {
3333 case 0:
3334 /* 'file:' + escaped DOS path */
3335 break;
3336 case 1:
3337 /* 'file:/' + escaped DOS path */
3338 /* fall through */
3339 case 3:
3340 /* 'file:///' (implied localhost) + escaped DOS path */
3341 if (!isalphaW(*src) || (src[1] != ':' && src[1] != '|'))
3342 src -= 1;
3343 break;
3344 case 2:
3345 if (CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, src, 9,
3346 localhost, 9) == CSTR_EQUAL &&
3347 (src[9] == '/' || src[9] == '\\'))
3348 {
3349 /* 'file://localhost/' + escaped DOS path */
3350 src += 10;
3351 }
3352 else if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3353 {
3354 /* 'file://' + unescaped DOS path */
3355 unescape = 0;
3356 }
3357 else
3358 {
3359 /* 'file://hostname:port/path' (where path is escaped)
3360 * or 'file:' + escaped UNC path (\\server\share\path)
3361 * The second form is clearly specific to Windows and it might
3362 * even be doing a network lookup to try to figure it out.
3363 */
3364 while (*src && *src != '/' && *src != '\\')
3365 src++;
3366 len = src - pszUrl;
3367 StrCpyNW(dst, pszUrl, len + 1);
3368 dst += len;
3369 if (isalphaW(src[1]) && (src[2] == ':' || src[2] == '|'))
3370 {
3371 /* 'Forget' to add a trailing '/', just like Windows */
3372 src++;
3373 }
3374 }
3375 break;
3376 case 4:
3377 /* 'file://' + unescaped UNC path (\\server\share\path) */
3378 unescape = 0;
3379 if (isalphaW(*src) && (src[1] == ':' || src[1] == '|'))
3380 break;
3381 /* fall through */
3382 default:
3383 /* 'file:/...' + escaped UNC path (\\server\share\path) */
3384 src -= 2;
3385 }
3386
3387 /* Copy the remainder of the path */
3388 len += lstrlenW(src);
3389 StrCpyW(dst, src);
3390
3391 /* First do the Windows-specific path conversions */
3392 for (dst = tpath; *dst; dst++)
3393 if (*dst == '/') *dst = '\\';
3394 if (isalphaW(*tpath) && tpath[1] == '|')
3395 tpath[1] = ':'; /* c| -> c: */
3396
3397 /* And only then unescape the path (i.e. escaped slashes are left as is) */
3398 if (unescape)
3399 {
3400 ret = UrlUnescapeW(tpath, NULL, &len, URL_UNESCAPE_INPLACE);
3401 if (ret == S_OK)
3402 {
3403 /* When working in-place UrlUnescapeW() does not set len */
3404 len = lstrlenW(tpath);
3405 }
3406 }
3407
3408 if (*pcchPath < len + 1)
3409 {
3410 ret = E_POINTER;
3411 *pcchPath = len + 1;
3412 }
3413 else
3414 {
3415 *pcchPath = len;
3416 if (tpath != pszPath)
3417 StrCpyW(pszPath, tpath);
3418 }
3419 if (tpath != pszPath)
3420 HeapFree(GetProcessHeap(), 0, tpath);
3421
3422 TRACE("Returning (%u) %s\n", *pcchPath, debugstr_w(pszPath));
3423 return ret;
3424 }
3425
3426 /*************************************************************************
3427 * PathCreateFromUrlAlloc [SHLWAPI.@]
3428 */
3429 HRESULT WINAPI PathCreateFromUrlAlloc(LPCWSTR pszUrl, LPWSTR *pszPath,
3430 DWORD dwReserved)
3431 {
3432 WCHAR pathW[MAX_PATH];
3433 DWORD size;
3434 HRESULT hr;
3435
3436 size = MAX_PATH;
3437 hr = PathCreateFromUrlW(pszUrl, pathW, &size, dwReserved);
3438 if (SUCCEEDED(hr))
3439 {
3440 /* Yes, this is supposed to crash if pszPath is NULL */
3441 *pszPath = StrDupW(pathW);
3442 }
3443 return hr;
3444 }
3445
3446 /*************************************************************************
3447 * PathRelativePathToA [SHLWAPI.@]
3448 *
3449 * Create a relative path from one path to another.
3450 *
3451 * PARAMS
3452 * lpszPath [O] Destination for relative path
3453 * lpszFrom [I] Source path
3454 * dwAttrFrom [I] File attribute of source path
3455 * lpszTo [I] Destination path
3456 * dwAttrTo [I] File attributes of destination path
3457 *
3458 * RETURNS
3459 * TRUE If a relative path can be formed. lpszPath contains the new path
3460 * FALSE If the paths are not relative or any parameters are invalid
3461 *
3462 * NOTES
3463 * lpszTo should be at least MAX_PATH in length.
3464 *
3465 * Calling this function with relative paths for lpszFrom or lpszTo may
3466 * give erroneous results.
3467 *
3468 * The Win32 version of this function contains a bug where the lpszTo string
3469 * may be referenced 1 byte beyond the end of the string. As a result random
3470 * garbage may be written to the output path, depending on what lies beyond
3471 * the last byte of the string. This bug occurs because of the behaviour of
3472 * PathCommonPrefix() (see notes for that function), and no workaround seems
3473 * possible with Win32.
3474 *
3475 * This bug has been fixed here, so for example the relative path from "\\"
3476 * to "\\" is correctly determined as "." in this implementation.
3477 */
3478 BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
3479 LPCSTR lpszTo, DWORD dwAttrTo)
3480 {
3481 BOOL bRet = FALSE;
3482
3483 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3484 dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);
3485
3486 if(lpszPath && lpszFrom && lpszTo)
3487 {
3488 WCHAR szPath[MAX_PATH];
3489 WCHAR szFrom[MAX_PATH];
3490 WCHAR szTo[MAX_PATH];
3491 MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
3492 MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3493 bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3494 WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3495 }
3496 return bRet;
3497 }
3498
3499 /*************************************************************************
3500 * PathRelativePathToW [SHLWAPI.@]
3501 *
3502 * See PathRelativePathToA.
3503 */
3504 BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
3505 LPCWSTR lpszTo, DWORD dwAttrTo)
3506 {
3507 static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
3508 static const WCHAR szPrevDir[] = { '.', '.', '\0' };
3509 WCHAR szFrom[MAX_PATH];
3510 WCHAR szTo[MAX_PATH];
3511 DWORD dwLen;
3512
3513 TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3514 dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);
3515
3516 if(!lpszPath || !lpszFrom || !lpszTo)
3517 return FALSE;
3518
3519 *lpszPath = '\0';
3520 lstrcpynW(szFrom, lpszFrom, MAX_PATH);
3521 lstrcpynW(szTo, lpszTo, MAX_PATH);
3522
3523 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3524 PathRemoveFileSpecW(szFrom);
3525 if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
3526 PathRemoveFileSpecW(szTo);
3527
3528 /* Paths can only be relative if they have a common root */
3529 if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
3530 return FALSE;
3531
3532 /* Strip off lpszFrom components to the root, by adding "..\" */
3533 lpszFrom = szFrom + dwLen;
3534 if (!*lpszFrom)
3535 {
3536 lpszPath[0] = '.';
3537 lpszPath[1] = '\0';
3538 }
3539 if (*lpszFrom == '\\')
3540 lpszFrom++;
3541
3542 while (*lpszFrom)
3543 {
3544 lpszFrom = PathFindNextComponentW(lpszFrom);
3545 strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
3546 }
3547
3548 /* From the root add the components of lpszTo */
3549 lpszTo += dwLen;
3550 /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
3551 * this function.
3552 */
3553 if (*lpszTo && lpszTo[-1])
3554 {
3555 if (*lpszTo != '\\')
3556 lpszTo--;
3557 dwLen = strlenW(lpszPath);
3558 if (dwLen + strlenW(lpszTo) >= MAX_PATH)
3559 {
3560 *lpszPath = '\0';
3561 return FALSE;
3562 }
3563 strcpyW(lpszPath + dwLen, lpszTo);
3564 }
3565 return TRUE;
3566 }
3567
3568 /*************************************************************************
3569 * PathUnmakeSystemFolderA [SHLWAPI.@]
3570 *
3571 * Remove the system folder attributes from a path.
3572 *
3573 * PARAMS
3574 * lpszPath [I] The path to remove attributes from
3575 *
3576 * RETURNS
3577 * Success: TRUE.
3578 * Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
3579 * SetFileAttributesA() fails.
3580 */
3581 BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3582 {
3583 DWORD dwAttr;
3584
3585 TRACE("(%s)\n", debugstr_a(lpszPath));
3586
3587 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3588 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3589 return FALSE;
3590
3591 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3592 return SetFileAttributesA(lpszPath, dwAttr);
3593 }
3594
3595 /*************************************************************************
3596 * PathUnmakeSystemFolderW [SHLWAPI.@]
3597 *
3598 * See PathUnmakeSystemFolderA.
3599 */
3600 BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3601 {
3602 DWORD dwAttr;
3603
3604 TRACE("(%s)\n", debugstr_w(lpszPath));
3605
3606 if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3607 !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
3608 return FALSE;
3609
3610 dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
3611 return SetFileAttributesW(lpszPath, dwAttr);
3612 }
3613
3614
3615 /*************************************************************************
3616 * PathSetDlgItemPathA [SHLWAPI.@]
3617 *
3618 * Set the text of a dialog item to a path, shrinking the path to fit
3619 * if it is too big for the item.
3620 *
3621 * PARAMS
3622 * hDlg [I] Dialog handle
3623 * id [I] ID of item in the dialog
3624 * lpszPath [I] Path to set as the items text
3625 *
3626 * RETURNS
3627 * Nothing.
3628 *
3629 * NOTES
3630 * If lpszPath is NULL, a blank string ("") is set (i.e. The previous
3631 * window text is erased).
3632 */
3633 VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3634 {
3635 WCHAR szPath[MAX_PATH];
3636
3637 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3638
3639 if (lpszPath)
3640 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3641 else
3642 szPath[0] = '\0';
3643 PathSetDlgItemPathW(hDlg, id, szPath);
3644 }
3645
3646 /*************************************************************************
3647 * PathSetDlgItemPathW [SHLWAPI.@]
3648 *
3649 * See PathSetDlgItemPathA.
3650 */
3651 VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3652 {
3653 WCHAR path[MAX_PATH + 1];
3654 HWND hwItem;
3655 RECT rect;
3656 HDC hdc;
3657 HGDIOBJ hPrevObj;
3658
3659 TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3660
3661 if (!(hwItem = GetDlgItem(hDlg, id)))
3662 return;
3663
3664 if (lpszPath)
3665 lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3666 else
3667 path[0] = '\0';
3668
3669 GetClientRect(hwItem, &rect);
3670 hdc = GetDC(hDlg);
3671 hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));
3672
3673 if (hPrevObj)
3674 {
3675 PathCompactPathW(hdc, path, rect.right);
3676 SelectObject(hdc, hPrevObj);
3677 }
3678
3679 ReleaseDC(hDlg, hdc);
3680 SetWindowTextW(hwItem, path);
3681 }
3682
3683 /*************************************************************************
3684 * PathIsNetworkPathA [SHLWAPI.@]
3685 *
3686 * Determine if the given path is a network path.
3687 *
3688 * PARAMS
3689 * lpszPath [I] Path to check
3690 *
3691 * RETURNS
3692 * TRUE If lpszPath is a UNC share or mapped network drive, or
3693 * FALSE If lpszPath is a local drive or cannot be determined
3694 */
3695 BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
3696 {
3697 int dwDriveNum;
3698
3699 TRACE("(%s)\n",debugstr_a(lpszPath));
3700
3701 if (!lpszPath)
3702 return FALSE;
3703 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3704 return TRUE;
3705 dwDriveNum = PathGetDriveNumberA(lpszPath);
3706 if (dwDriveNum == -1)
3707 return FALSE;
3708 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3709 return pIsNetDrive(dwDriveNum);
3710 }
3711
3712 /*************************************************************************
3713 * PathIsNetworkPathW [SHLWAPI.@]
3714 *
3715 * See PathIsNetworkPathA.
3716 */
3717 BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
3718 {
3719 int dwDriveNum;
3720
3721 TRACE("(%s)\n", debugstr_w(lpszPath));
3722
3723 if (!lpszPath)
3724 return FALSE;
3725 if (*lpszPath == '\\' && lpszPath[1] == '\\')
3726 return TRUE;
3727 dwDriveNum = PathGetDriveNumberW(lpszPath);
3728 if (dwDriveNum == -1)
3729 return FALSE;
3730 GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
3731 return pIsNetDrive(dwDriveNum);
3732 }
3733
3734 /*************************************************************************
3735 * PathIsLFNFileSpecA [SHLWAPI.@]
3736 *
3737 * Determine if the given path is a long file name
3738 *
3739 * PARAMS
3740 * lpszPath [I] Path to check
3741 *
3742 * RETURNS
3743 * TRUE If path is a long file name,
3744 * FALSE If path is a valid DOS short file name
3745 */
3746 BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
3747 {
3748 DWORD dwNameLen = 0, dwExtLen = 0;
3749
3750 TRACE("(%s)\n",debugstr_a(lpszPath));
3751
3752 if (!lpszPath)
3753 return FALSE;
3754
3755 while (*lpszPath)
3756 {
3757 if (*lpszPath == ' ')
3758 return TRUE; /* DOS names cannot have spaces */
3759 if (*lpszPath == '.')
3760 {
3761 if (dwExtLen)
3762 return TRUE; /* DOS names have only one dot */
3763 dwExtLen = 1;
3764 }
3765 else if (dwExtLen)
3766 {
3767 dwExtLen++;
3768 if (dwExtLen > 4)
3769 return TRUE; /* DOS extensions are <= 3 chars*/
3770 }
3771 else
3772 {
3773 dwNameLen++;
3774 if (dwNameLen > 8)
3775 return TRUE; /* DOS names are <= 8 chars */
3776 }
3777 lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
3778 }
3779 return FALSE; /* Valid DOS path */
3780 }
3781
3782 /*************************************************************************
3783 * PathIsLFNFileSpecW [SHLWAPI.@]
3784 *
3785 * See PathIsLFNFileSpecA.
3786 */
3787 BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
3788 {
3789 DWORD dwNameLen = 0, dwExtLen = 0;
3790
3791 TRACE("(%s)\n",debugstr_w(lpszPath));
3792
3793 if (!lpszPath)
3794 return FALSE;
3795
3796 while (*lpszPath)
3797 {
3798 if (*lpszPath == ' ')
3799 return TRUE; /* DOS names cannot have spaces */
3800 if (*lpszPath == '.')
3801 {
3802 if (dwExtLen)
3803 return TRUE; /* DOS names have only one dot */
3804 dwExtLen = 1;
3805 }
3806 else if (dwExtLen)
3807 {
3808 dwExtLen++;
3809 if (dwExtLen > 4)
3810 return TRUE; /* DOS extensions are <= 3 chars*/
3811 }
3812 else
3813 {
3814 dwNameLen++;
3815 if (dwNameLen > 8)
3816 return TRUE; /* DOS names are <= 8 chars */
3817 }
3818 lpszPath++;
3819 }
3820 return FALSE; /* Valid DOS path */
3821 }
3822
3823 /*************************************************************************
3824 * PathIsDirectoryEmptyA [SHLWAPI.@]
3825 *
3826 * Determine if a given directory is empty.
3827 *
3828 * PARAMS
3829 * lpszPath [I] Directory to check
3830 *
3831 * RETURNS
3832 * TRUE If the directory exists and contains no files,
3833 * FALSE Otherwise
3834 */
3835 BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
3836 {
3837 BOOL bRet = FALSE;
3838
3839 TRACE("(%s)\n",debugstr_a(lpszPath));
3840
3841 if (lpszPath)
3842 {
3843 WCHAR szPath[MAX_PATH];
3844 MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3845 bRet = PathIsDirectoryEmptyW(szPath);
3846 }
3847 return bRet;
3848 }
3849
3850 /*************************************************************************
3851 * PathIsDirectoryEmptyW [SHLWAPI.@]
3852 *
3853 * See PathIsDirectoryEmptyA.
3854 */
3855 BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
3856 {
3857 static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
3858 WCHAR szSearch[MAX_PATH];
3859 DWORD dwLen;
3860 HANDLE hfind;
3861 BOOL retVal = FALSE;
3862 WIN32_FIND_DATAW find_data;
3863
3864 TRACE("(%s)\n",debugstr_w(lpszPath));
3865
3866 if (!lpszPath || !PathIsDirectoryW(lpszPath))
3867 return FALSE;
3868
3869 lstrcpynW(szSearch, lpszPath, MAX_PATH);
3870 PathAddBackslashW(szSearch);
3871 dwLen = strlenW(szSearch);
3872 if (dwLen > MAX_PATH - 4)
3873 return FALSE;
3874
3875 strcpyW(szSearch + dwLen, szAllFiles);
3876 hfind = FindFirstFileW(szSearch, &find_data);
3877
3878 if (hfind != INVALID_HANDLE_VALUE &&
3879 find_data.cFileName[0] == '.' &&
3880 find_data.cFileName[1] == '.')
3881 {
3882 /* The only directory entry should be the parent */
3883 if (!FindNextFileW(hfind, &find_data))
3884 retVal = TRUE;
3885 FindClose(hfind);
3886 }
3887 return retVal;
3888 }
3889
3890
3891 /*************************************************************************
3892 * PathFindSuffixArrayA [SHLWAPI.@]
3893 *
3894 * Find a suffix string in an array of suffix strings
3895 *
3896 * PARAMS
3897 * lpszSuffix [I] Suffix string to search for
3898 * lppszArray [I] Array of suffix strings to search
3899 * dwCount [I] Number of elements in lppszArray
3900 *
3901 * RETURNS
3902 * Success: The index of the position of lpszSuffix in lppszArray
3903 * Failure: 0, if any parameters are invalid or lpszSuffix is not found
3904 *
3905 * NOTES
3906 * The search is case sensitive.
3907 * The match is made against the end of the suffix string, so for example:
3908 * lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3909 */
3910 LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3911 {
3912 size_t dwLen;
3913 int dwRet = 0;
3914
3915 TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);
3916
3917 if (lpszSuffix && lppszArray && dwCount > 0)
3918 {
3919 dwLen = strlen(lpszSuffix);
3920
3921 while (dwRet < dwCount)
3922 {
3923 size_t dwCompareLen = strlen(*lppszArray);
3924 if (dwCompareLen < dwLen)
3925 {
3926 if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3927 return *lppszArray; /* Found */
3928 }
3929 dwRet++;
3930 lppszArray++;
3931 }
3932 }
3933 return NULL;
3934 }
3935
3936 /*************************************************************************
3937 * PathFindSuffixArrayW [SHLWAPI.@]
3938 *
3939 * See PathFindSuffixArrayA.
3940 */
3941 LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3942 {
3943 size_t dwLen;
3944 int dwRet = 0;
3945
3946 TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);
3947
3948 if (lpszSuffix && lppszArray && dwCount > 0)
3949 {
3950 dwLen = strlenW(lpszSuffix);
3951
3952 while (dwRet < dwCount)
3953 {
3954 size_t dwCompareLen = strlenW(*lppszArray);
3955 if (dwCompareLen < dwLen)
3956 {
3957 if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3958 return *lppszArray; /* Found */
3959 }
3960 dwRet++;
3961 lppszArray++;
3962 }
3963 }
3964 return NULL;
3965 }
3966
3967 /*************************************************************************
3968 * PathUndecorateA [SHLWAPI.@]
3969 *
3970 * Undecorate a file path
3971 *
3972 * PARAMS
3973 * lpszPath [I/O] Path to remove any decoration from
3974 *
3975 * RETURNS
3976 * Nothing
3977 *
3978 * NOTES
3979 * A decorations form is "path[n].ext" where "n" is an optional decimal number.
3980 */
3981 VOID WINAPI PathUndecorateA(LPSTR lpszPath)
3982 {
3983 TRACE("(%s)\n",debugstr_a(lpszPath));
3984
3985 if (lpszPath)
3986 {
3987 LPSTR lpszExt = PathFindExtensionA(lpszPath);
3988 if (lpszExt > lpszPath && lpszExt[-1] == ']')
3989 {
3990 LPSTR lpszSkip = lpszExt - 2;
3991 if (*lpszSkip == '[')
3992 lpszSkip++; /* [] (no number) */
3993 else
3994 while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
3995 lpszSkip--;
3996 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
3997 {
3998 /* remove the [n] */
3999 lpszSkip--;
4000 while (*lpszExt)
4001 *lpszSkip++ = *lpszExt++;
4002 *lpszSkip = '\0';
4003 }
4004 }
4005 }
4006 }
4007
4008 /*************************************************************************
4009 * PathUndecorateW [SHLWAPI.@]
4010 *
4011 * See PathUndecorateA.
4012 */
4013 VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
4014 {
4015 TRACE("(%s)\n",debugstr_w(lpszPath));
4016
4017 if (lpszPath)
4018 {
4019 LPWSTR lpszExt = PathFindExtensionW(lpszPath);
4020 if (lpszExt > lpszPath && lpszExt[-1] == ']')
4021 {
4022 LPWSTR lpszSkip = lpszExt - 2;
4023 if (*lpszSkip == '[')
4024 lpszSkip++; /* [] (no number) */
4025 else
4026 while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
4027 lpszSkip--;
4028 if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
4029 {
4030 /* remove the [n] */
4031 lpszSkip--;
4032 while (*lpszExt)
4033 *lpszSkip++ = *lpszExt++;
4034 *lpszSkip = '\0';
4035 }
4036 }
4037 }
4038 }
4039
4040 /*************************************************************************
4041 * PathUnExpandEnvStringsA [SHLWAPI.@]
4042 *
4043 * Substitute folder names in a path with their corresponding environment
4044 * strings.
4045 *
4046 * PARAMS
4047 * pszPath [I] Buffer containing the path to unexpand.
4048 * pszBuf [O] Buffer to receive the unexpanded path.
4049 * cchBuf [I] Size of pszBuf in characters.
4050 *
4051 * RETURNS
4052 * Success: TRUE
4053 * Failure: FALSE
4054 */
4055 BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR pszPath, LPSTR pszBuf, UINT cchBuf)
4056 {
4057 FIXME("(%s,%s,0x%08x)\n", debugstr_a(pszPath), debugstr_a(pszBuf), cchBuf);
4058 return FALSE;
4059 }
4060
4061 /*************************************************************************
4062 * PathUnExpandEnvStringsW [SHLWAPI.@]
4063 *
4064 * Unicode version of PathUnExpandEnvStringsA.
4065 */
4066 BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR pszPath, LPWSTR pszBuf, UINT cchBuf)
4067 {
4068 FIXME("(%s,%s,0x%08x)\n", debugstr_w(pszPath), debugstr_w(pszBuf), cchBuf);
4069 return FALSE;
4070 }
4071
4072 /*************************************************************************
4073 * @ [SHLWAPI.440]
4074 *
4075 * Find localised or default web content in "%WINDOWS%\web\".
4076 *
4077 * PARAMS
4078 * lpszFile [I] File name containing content to look for
4079 * lpszPath [O] Buffer to contain the full path to the file
4080 * dwPathLen [I] Length of lpszPath
4081 *
4082 * RETURNS
4083 * Success: S_OK. lpszPath contains the full path to the content.
4084 * Failure: E_FAIL. The content does not exist or lpszPath is too short.
4085 */
4086 HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
4087 {
4088 WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
4089 HRESULT hRet;
4090
4091 TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
4092
4093 MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
4094 szPath[0] = '\0';
4095 hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
4096 WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
4097 return hRet;
4098 }
4099
4100 /*************************************************************************
4101 * @ [SHLWAPI.441]
4102 *
4103 * Unicode version of SHGetWebFolderFilePathA.
4104 */
4105 HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
4106 {
4107 static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
4108 static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
4109 #define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
4110 #define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
4111 DWORD dwLen, dwFileLen;
4112 LANGID lidSystem, lidUser;
4113
4114 TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
4115
4116 /* Get base directory for web content */
4117 dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
4118 if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
4119 dwLen--;
4120
4121 dwFileLen = strlenW(lpszFile);
4122
4123 if (dwLen + dwFileLen + szWebLen >= dwPathLen)
4124 return E_FAIL; /* lpszPath too short */
4125
4126 strcpyW(lpszPath+dwLen, szWeb);
4127 dwLen += szWebLen;
4128 dwPathLen = dwPathLen - dwLen; /* Remaining space */
4129
4130 lidSystem = GetSystemDefaultUILanguage();
4131 lidUser = GetUserDefaultUILanguage();
4132
4133 if (lidSystem != lidUser)
4134 {
4135 if (dwFileLen + szWebMuiLen < dwPathLen)
4136 {
4137 /* Use localised content in the users UI language if present */
4138 wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
4139 strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
4140 if (PathFileExistsW(lpszPath))
4141 return S_OK;
4142 }
4143 }
4144
4145 /* Fall back to OS default installed content */
4146 strcpyW(lpszPath + dwLen, lpszFile);
4147 if (PathFileExistsW(lpszPath))
4148 return S_OK;
4149 return E_FAIL;
4150 }
4151
4152 #define PATH_CHAR_CLASS_LETTER 0x00000001
4153 #define PATH_CHAR_CLASS_ASTERIX 0x00000002
4154 #define PATH_CHAR_CLASS_DOT 0x00000004
4155 #define PATH_CHAR_CLASS_BACKSLASH 0x00000008
4156 #define PATH_CHAR_CLASS_COLON 0x00000010
4157 #define PATH_CHAR_CLASS_SEMICOLON 0x00000020
4158 #define PATH_CHAR_CLASS_COMMA 0x00000040
4159 #define PATH_CHAR_CLASS_SPACE 0x00000080
4160 #define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
4161 #define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200
4162
4163 #define PATH_CHAR_CLASS_INVALID 0x00000000
4164 #define PATH_CHAR_CLASS_ANY 0xffffffff
4165
4166 static const DWORD SHELL_charclass[] =
4167 {
4168 /* 0x00 */ PATH_CHAR_CLASS_INVALID, /* 0x01 */ PATH_CHAR_CLASS_INVALID,
4169 /* 0x02 */ PATH_CHAR_CLASS_INVALID, /* 0x03 */ PATH_CHAR_CLASS_INVALID,
4170 /* 0x04 */ PATH_CHAR_CLASS_INVALID, /* 0x05 */ PATH_CHAR_CLASS_INVALID,
4171 /* 0x06 */ PATH_CHAR_CLASS_INVALID, /* 0x07 */ PATH_CHAR_CLASS_INVALID,
4172 /* 0x08 */ PATH_CHAR_CLASS_INVALID, /* 0x09 */ PATH_CHAR_CLASS_INVALID,
4173 /* 0x0a */ PATH_CHAR_CLASS_INVALID, /* 0x0b */ PATH_CHAR_CLASS_INVALID,
4174 /* 0x0c */ PATH_CHAR_CLASS_INVALID, /* 0x0d */ PATH_CHAR_CLASS_INVALID,
4175 /* 0x0e */ PATH_CHAR_CLASS_INVALID, /* 0x0f */ PATH_CHAR_CLASS_INVALID,
4176 /* 0x10 */ PATH_CHAR_CLASS_INVALID, /* 0x11 */ PATH_CHAR_CLASS_INVALID,
4177 /* 0x12 */ PATH_CHAR_CLASS_INVALID, /* 0x13 */ PATH_CHAR_CLASS_INVALID,
4178 /* 0x14 */ PATH_CHAR_CLASS_INVALID, /* 0x15 */ PATH_CHAR_CLASS_INVALID,
4179 /* 0x16 */ PATH_CHAR_CLASS_INVALID, /* 0x17 */ PATH_CHAR_CLASS_INVALID,
4180 /* 0x18 */ PATH_CHAR_CLASS_INVALID, /* 0x19 */ PATH_CHAR_CLASS_INVALID,
4181 /* 0x1a */ PATH_CHAR_CLASS_INVALID, /* 0x1b */ PATH_CHAR_CLASS_INVALID,
4182 /* 0x1c */ PATH_CHAR_CLASS_INVALID, /* 0x1d */ PATH_CHAR_CLASS_INVALID,
4183 /* 0x1e */ PATH_CHAR_CLASS_INVALID, /* 0x1f */ PATH_CHAR_CLASS_INVALID,
4184 /* ' ' */ PATH_CHAR_CLASS_SPACE, /* '!' */ PATH_CHAR_CLASS_OTHER_VALID,
4185 /* '"' */ PATH_CHAR_CLASS_DOUBLEQUOTE, /* '#' */ PATH_CHAR_CLASS_OTHER_VALID,
4186 /* '$' */ PATH_CHAR_CLASS_OTHER_VALID, /* '%' */ PATH_CHAR_CLASS_OTHER_VALID,
4187 /* '&' */ PATH_CHAR_CLASS_OTHER_VALID, /* '\'' */ PATH_CHAR_CLASS_OTHER_VALID,
4188 /* '(' */ PATH_CHAR_CLASS_OTHER_VALID, /* ')' */ PATH_CHAR_CLASS_OTHER_VALID,
4189 /* '*' */ PATH_CHAR_CLASS_ASTERIX, /* '+' */ PATH_CHAR_CLASS_OTHER_VALID,
4190 /* ',' */ PATH_CHAR_CLASS_COMMA, /* '-' */ PATH_CHAR_CLASS_OTHER_VALID,
4191 /* '.' */ PATH_CHAR_CLASS_DOT, /* '/' */ PATH_CHAR_CLASS_INVALID,
4192 /* '0' */ PATH_CHAR_CLASS_OTHER_VALID, /* '1' */ PATH_CHAR_CLASS_OTHER_VALID,
4193 /* '2' */ PATH_CHAR_CLASS_OTHER_VALID, /* '3' */ PATH_CHAR_CLASS_OTHER_VALID,
4194 /* '4' */ PATH_CHAR_CLASS_OTHER_VALID, /* '5' */ PATH_CHAR_CLASS_OTHER_VALID,
4195 /* '6' */ PATH_CHAR_CLASS_OTHER_VALID, /* '7' */ PATH_CHAR_CLASS_OTHER_VALID,
4196 /* '8' */ PATH_CHAR_CLASS_OTHER_VALID, /* '9' */ PATH_CHAR_CLASS_OTHER_VALID,
4197 /* ':' */ PATH_CHAR_CLASS_COLON, /* ';' */ PATH_CHAR_CLASS_SEMICOLON,
4198 /* '<' */ PATH_CHAR_CLASS_INVALID, /* '=' */ PATH_CHAR_CLASS_OTHER_VALID,
4199 /* '>' */ PATH_CHAR_CLASS_INVALID, /* '?' */ PATH_CHAR_CLASS_LETTER,
4200 /* '@' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'A' */ PATH_CHAR_CLASS_ANY,
4201 /* 'B' */ PATH_CHAR_CLASS_ANY, /* 'C' */ PATH_CHAR_CLASS_ANY,
4202 /* 'D' */ PATH_CHAR_CLASS_ANY, /* 'E' */ PATH_CHAR_CLASS_ANY,
4203 /* 'F' */ PATH_CHAR_CLASS_ANY, /* 'G' */ PATH_CHAR_CLASS_ANY,
4204 /* 'H' */ PATH_CHAR_CLASS_ANY, /* 'I' */ PATH_CHAR_CLASS_ANY,
4205 /* 'J' */ PATH_CHAR_CLASS_ANY, /* 'K' */ PATH_CHAR_CLASS_ANY,
4206 /* 'L' */ PATH_CHAR_CLASS_ANY, /* 'M' */ PATH_CHAR_CLASS_ANY,
4207 /* 'N' */ PATH_CHAR_CLASS_ANY, /* 'O' */ PATH_CHAR_CLASS_ANY,
4208 /* 'P' */ PATH_CHAR_CLASS_ANY, /* 'Q' */ PATH_CHAR_CLASS_ANY,
4209 /* 'R' */ PATH_CHAR_CLASS_ANY, /* 'S' */ PATH_CHAR_CLASS_ANY,
4210 /* 'T' */ PATH_CHAR_CLASS_ANY, /* 'U' */ PATH_CHAR_CLASS_ANY,
4211 /* 'V' */ PATH_CHAR_CLASS_ANY, /* 'W' */ PATH_CHAR_CLASS_ANY,
4212 /* 'X' */ PATH_CHAR_CLASS_ANY, /* 'Y' */ PATH_CHAR_CLASS_ANY,
4213 /* 'Z' */ PATH_CHAR_CLASS_ANY, /* '[' */ PATH_CHAR_CLASS_OTHER_VALID,
4214 /* '\\' */ PATH_CHAR_CLASS_BACKSLASH, /* ']' */ PATH_CHAR_CLASS_OTHER_VALID,
4215 /* '^' */ PATH_CHAR_CLASS_OTHER_VALID, /* '_' */ PATH_CHAR_CLASS_OTHER_VALID,
4216 /* '`' */ PATH_CHAR_CLASS_OTHER_VALID, /* 'a' */ PATH_CHAR_CLASS_ANY,
4217 /* 'b' */ PATH_CHAR_CLASS_ANY, /* 'c' */ PATH_CHAR_CLASS_ANY,
4218 /* 'd' */ PATH_CHAR_CLASS_ANY, /* 'e' */ PATH_CHAR_CLASS_ANY,
4219 /* 'f' */ PATH_CHAR_CLASS_ANY, /* 'g' */ PATH_CHAR_CLASS_ANY,
4220 /* 'h' */ PATH_CHAR_CLASS_ANY, /* 'i' */ PATH_CHAR_CLASS_ANY,
4221 /* 'j' */ PATH_CHAR_CLASS_ANY, /* 'k' */ PATH_CHAR_CLASS_ANY,
4222 /* 'l' */ PATH_CHAR_CLASS_ANY, /* 'm' */ PATH_CHAR_CLASS_ANY,
4223 /* 'n' */ PATH_CHAR_CLASS_ANY, /* 'o' */ PATH_CHAR_CLASS_ANY,
4224 /* 'p' */ PATH_CHAR_CLASS_ANY, /* 'q' */ PATH_CHAR_CLASS_ANY,
4225 /* 'r' */ PATH_CHAR_CLASS_ANY, /* 's' */ PATH_CHAR_CLASS_ANY,
4226 /* 't' */ PATH_CHAR_CLASS_ANY, /* 'u' */ PATH_CHAR_CLASS_ANY,
4227 /* 'v' */ PATH_CHAR_CLASS_ANY, /* 'w' */ PATH_CHAR_CLASS_ANY,
4228 /* 'x' */ PATH_CHAR_CLASS_ANY, /* 'y' */ PATH_CHAR_CLASS_ANY,
4229 /* 'z' */ PATH_CHAR_CLASS_ANY, /* '{' */ PATH_CHAR_CLASS_OTHER_VALID,
4230 /* '|' */ PATH_CHAR_CLASS_INVALID, /* '}' */ PATH_CHAR_CLASS_OTHER_VALID,
4231 /* '~' */ PATH_CHAR_CLASS_OTHER_VALID
4232 };
4233
4234 /*************************************************************************
4235 * @ [SHLWAPI.455]
4236 *
4237 * Check if an ASCII char is of a certain class
4238 */
4239 BOOL WINAPI PathIsValidCharA( char c, DWORD class )
4240 {
4241 if ((unsigned)c > 0x7e)
4242 return class & PATH_CHAR_CLASS_OTHER_VALID;
4243
4244 return class & SHELL_charclass[(unsigned)c];
4245 }
4246
4247 /*************************************************************************
4248 * @ [SHLWAPI.456]
4249 *
4250 * Check if a Unicode char is of a certain class
4251 */
4252 BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
4253 {
4254 if (c > 0x7e)
4255 return class & PATH_CHAR_CLASS_OTHER_VALID;
4256
4257 return class & SHELL_charclass[c];
4258 }