msvcrt: Use the msvcrt version of printf everywhere.
[wine] / dlls / shell32 / shellpath.c
1 /*
2  * Path Functions
3  *
4  * Copyright 1998, 1999, 2000 Juergen Schmied
5  * Copyright 2004 Juan Lang
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * NOTES:
22  *
23  * Many of these functions are in SHLWAPI.DLL also
24  *
25  */
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include <stdarg.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include "wine/debug.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "winreg.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40
41 #include "shlobj.h"
42 #include "shresdef.h"
43 #include "shell32_main.h"
44 #include "undocshell.h"
45 #include "pidl.h"
46 #include "wine/unicode.h"
47 #include "shlwapi.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50
51 /*
52         ########## Combining and Constructing paths ##########
53 */
54
55 /*************************************************************************
56  * PathAppend           [SHELL32.36]
57  */
58 BOOL WINAPI PathAppendAW(
59         LPVOID lpszPath1,
60         LPCVOID lpszPath2)
61 {
62         if (SHELL_OsIsUnicode())
63           return PathAppendW(lpszPath1, lpszPath2);
64         return PathAppendA(lpszPath1, lpszPath2);
65 }
66
67 /*************************************************************************
68  * PathCombine   [SHELL32.37]
69  */
70 LPVOID WINAPI PathCombineAW(
71         LPVOID szDest,
72         LPCVOID lpszDir,
73         LPCVOID lpszFile)
74 {
75         if (SHELL_OsIsUnicode())
76           return PathCombineW( szDest, lpszDir, lpszFile );
77         return PathCombineA( szDest, lpszDir, lpszFile );
78 }
79
80 /*************************************************************************
81  * PathAddBackslash             [SHELL32.32]
82  */
83 LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
84 {
85         if(SHELL_OsIsUnicode())
86           return PathAddBackslashW(lpszPath);
87         return PathAddBackslashA(lpszPath);
88 }
89
90 /*************************************************************************
91  * PathBuildRoot                [SHELL32.30]
92  */
93 LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
94 {
95         if(SHELL_OsIsUnicode())
96           return PathBuildRootW(lpszPath, drive);
97         return PathBuildRootA(lpszPath, drive);
98 }
99
100 /*
101         Extracting Component Parts
102 */
103
104 /*************************************************************************
105  * PathFindFileName     [SHELL32.34]
106  */
107 LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
108 {
109         if(SHELL_OsIsUnicode())
110           return PathFindFileNameW(lpszPath);
111         return PathFindFileNameA(lpszPath);
112 }
113
114 /*************************************************************************
115  * PathFindExtension            [SHELL32.31]
116  */
117 LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
118 {
119         if (SHELL_OsIsUnicode())
120           return PathFindExtensionW(lpszPath);
121         return PathFindExtensionA(lpszPath);
122
123 }
124
125 /*************************************************************************
126  * PathGetExtensionA            [internal]
127  *
128  * NOTES
129  *  exported by ordinal
130  *  return value points to the first char after the dot
131  */
132 static LPSTR PathGetExtensionA(LPCSTR lpszPath)
133 {
134         TRACE("(%s)\n",lpszPath);
135
136         lpszPath = PathFindExtensionA(lpszPath);
137         return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
138 }
139
140 /*************************************************************************
141  * PathGetExtensionW            [internal]
142  */
143 static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
144 {
145         TRACE("(%s)\n",debugstr_w(lpszPath));
146
147         lpszPath = PathFindExtensionW(lpszPath);
148         return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
149 }
150
151 /*************************************************************************
152  * PathGetExtension             [SHELL32.158]
153  */
154 LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
155 {
156         if (SHELL_OsIsUnicode())
157           return PathGetExtensionW(lpszPath);
158         return PathGetExtensionA(lpszPath);
159 }
160
161 /*************************************************************************
162  * PathGetArgs  [SHELL32.52]
163  */
164 LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
165 {
166         if (SHELL_OsIsUnicode())
167           return PathGetArgsW(lpszPath);
168         return PathGetArgsA(lpszPath);
169 }
170
171 /*************************************************************************
172  * PathGetDriveNumber   [SHELL32.57]
173  */
174 int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
175 {
176         if (SHELL_OsIsUnicode())
177           return PathGetDriveNumberW(lpszPath);
178         return PathGetDriveNumberA(lpszPath);
179 }
180
181 /*************************************************************************
182  * PathRemoveFileSpec [SHELL32.35]
183  */
184 BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
185 {
186         if (SHELL_OsIsUnicode())
187           return PathRemoveFileSpecW(lpszPath);
188         return PathRemoveFileSpecA(lpszPath);
189 }
190
191 /*************************************************************************
192  * PathStripPath        [SHELL32.38]
193  */
194 void WINAPI PathStripPathAW(LPVOID lpszPath)
195 {
196         if (SHELL_OsIsUnicode())
197             PathStripPathW(lpszPath);
198         else
199             PathStripPathA(lpszPath);
200 }
201
202 /*************************************************************************
203  * PathStripToRoot      [SHELL32.50]
204  */
205 BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
206 {
207         if (SHELL_OsIsUnicode())
208           return PathStripToRootW(lpszPath);
209         return PathStripToRootA(lpszPath);
210 }
211
212 /*************************************************************************
213  * PathRemoveArgs       [SHELL32.251]
214  */
215 void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
216 {
217         if (SHELL_OsIsUnicode())
218             PathRemoveArgsW(lpszPath);
219         else
220             PathRemoveArgsA(lpszPath);
221 }
222
223 /*************************************************************************
224  * PathRemoveExtension  [SHELL32.250]
225  */
226 void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
227 {
228         if (SHELL_OsIsUnicode())
229             PathRemoveExtensionW(lpszPath);
230         else
231             PathRemoveExtensionA(lpszPath);
232 }
233
234
235 /*
236         Path Manipulations
237 */
238
239 /*************************************************************************
240  * PathGetShortPathA [internal]
241  */
242 static void PathGetShortPathA(LPSTR pszPath)
243 {
244         CHAR path[MAX_PATH];
245
246         TRACE("%s\n", pszPath);
247
248         if (GetShortPathNameA(pszPath, path, MAX_PATH))
249         {
250           lstrcpyA(pszPath, path);
251         }
252 }
253
254 /*************************************************************************
255  * PathGetShortPathW [internal]
256  */
257 static void PathGetShortPathW(LPWSTR pszPath)
258 {
259         WCHAR path[MAX_PATH];
260
261         TRACE("%s\n", debugstr_w(pszPath));
262
263         if (GetShortPathNameW(pszPath, path, MAX_PATH))
264         {
265           lstrcpyW(pszPath, path);
266         }
267 }
268
269 /*************************************************************************
270  * PathGetShortPath [SHELL32.92]
271  */
272 VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
273 {
274         if(SHELL_OsIsUnicode())
275           PathGetShortPathW(pszPath);
276         PathGetShortPathA(pszPath);
277 }
278
279 /*************************************************************************
280  * PathRemoveBlanks [SHELL32.33]
281  */
282 void WINAPI PathRemoveBlanksAW(LPVOID str)
283 {
284         if(SHELL_OsIsUnicode())
285             PathRemoveBlanksW(str);
286         else
287             PathRemoveBlanksA(str);
288 }
289
290 /*************************************************************************
291  * PathQuoteSpaces [SHELL32.55]
292  */
293 VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
294 {
295         if(SHELL_OsIsUnicode())
296             PathQuoteSpacesW(lpszPath);
297         else
298             PathQuoteSpacesA(lpszPath);
299 }
300
301 /*************************************************************************
302  * PathUnquoteSpaces [SHELL32.56]
303  */
304 VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
305 {
306         if(SHELL_OsIsUnicode())
307           PathUnquoteSpacesW(str);
308         else
309           PathUnquoteSpacesA(str);
310 }
311
312 /*************************************************************************
313  * PathParseIconLocation        [SHELL32.249]
314  */
315 int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
316 {
317         if(SHELL_OsIsUnicode())
318           return PathParseIconLocationW(lpszPath);
319         return PathParseIconLocationA(lpszPath);
320 }
321
322 /*
323         ########## Path Testing ##########
324 */
325 /*************************************************************************
326  * PathIsUNC            [SHELL32.39]
327  */
328 BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
329 {
330         if (SHELL_OsIsUnicode())
331           return PathIsUNCW( lpszPath );
332         return PathIsUNCA( lpszPath );
333 }
334
335 /*************************************************************************
336  *  PathIsRelative      [SHELL32.40]
337  */
338 BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
339 {
340         if (SHELL_OsIsUnicode())
341           return PathIsRelativeW( lpszPath );
342         return PathIsRelativeA( lpszPath );
343 }
344
345 /*************************************************************************
346  * PathIsRoot           [SHELL32.29]
347  */
348 BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
349 {
350         if (SHELL_OsIsUnicode())
351           return PathIsRootW(lpszPath);
352         return PathIsRootA(lpszPath);
353 }
354
355 /*************************************************************************
356  *  PathIsExeA          [internal]
357  */
358 static BOOL PathIsExeA (LPCSTR lpszPath)
359 {
360         LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
361         int i;
362         static const char * const lpszExtensions[] =
363             {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
364
365         TRACE("path=%s\n",lpszPath);
366
367         for(i=0; lpszExtensions[i]; i++)
368           if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
369
370         return FALSE;
371 }
372
373 /*************************************************************************
374  *  PathIsExeW          [internal]
375  */
376 static BOOL PathIsExeW (LPCWSTR lpszPath)
377 {
378         LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
379         int i;
380         static const WCHAR lpszExtensions[][4] =
381             {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
382              {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
383              {'s','c','r','\0'}, {'\0'} };
384
385         TRACE("path=%s\n",debugstr_w(lpszPath));
386
387         for(i=0; lpszExtensions[i][0]; i++)
388           if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
389
390         return FALSE;
391 }
392
393 /*************************************************************************
394  *  PathIsExe           [SHELL32.43]
395  */
396 BOOL WINAPI PathIsExeAW (LPCVOID path)
397 {
398         if (SHELL_OsIsUnicode())
399           return PathIsExeW (path);
400         return PathIsExeA(path);
401 }
402
403 /*************************************************************************
404  * PathIsDirectory      [SHELL32.159]
405  */
406 BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
407 {
408         if (SHELL_OsIsUnicode())
409           return PathIsDirectoryW (lpszPath);
410         return PathIsDirectoryA (lpszPath);
411 }
412
413 /*************************************************************************
414  * PathFileExists       [SHELL32.45]
415  */
416 BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
417 {
418         if (SHELL_OsIsUnicode())
419           return PathFileExistsW (lpszPath);
420         return PathFileExistsA (lpszPath);
421 }
422
423 /*************************************************************************
424  * PathMatchSpec        [SHELL32.46]
425  */
426 BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
427 {
428         if (SHELL_OsIsUnicode())
429           return PathMatchSpecW( name, mask );
430         return PathMatchSpecA( name, mask );
431 }
432
433 /*************************************************************************
434  * PathIsSameRoot       [SHELL32.650]
435  */
436 BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
437 {
438         if (SHELL_OsIsUnicode())
439           return PathIsSameRootW(lpszPath1, lpszPath2);
440         return PathIsSameRootA(lpszPath1, lpszPath2);
441 }
442
443 /*************************************************************************
444  * IsLFNDriveA          [SHELL32.41]
445  */
446 BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
447 {
448     DWORD       fnlen;
449
450     if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
451         return FALSE;
452     return fnlen > 12;
453 }
454
455 /*************************************************************************
456  * IsLFNDriveW          [SHELL32.42]
457  */
458 BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
459 {
460     DWORD       fnlen;
461
462     if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
463         return FALSE;
464     return fnlen > 12;
465 }
466
467 /*************************************************************************
468  * IsLFNDrive           [SHELL32.119]
469  */
470 BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
471 {
472         if (SHELL_OsIsUnicode())
473           return IsLFNDriveW(lpszPath);
474         return IsLFNDriveA(lpszPath);
475 }
476
477 /*
478         ########## Creating Something Unique ##########
479 */
480 /*************************************************************************
481  * PathMakeUniqueNameA  [internal]
482  */
483 BOOL WINAPI PathMakeUniqueNameA(
484         LPSTR lpszBuffer,
485         DWORD dwBuffSize,
486         LPCSTR lpszShortName,
487         LPCSTR lpszLongName,
488         LPCSTR lpszPathName)
489 {
490         FIXME("%p %lu %s %s %s stub\n",
491          lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
492          debugstr_a(lpszLongName), debugstr_a(lpszPathName));
493         return TRUE;
494 }
495
496 /*************************************************************************
497  * PathMakeUniqueNameW  [internal]
498  */
499 BOOL WINAPI PathMakeUniqueNameW(
500         LPWSTR lpszBuffer,
501         DWORD dwBuffSize,
502         LPCWSTR lpszShortName,
503         LPCWSTR lpszLongName,
504         LPCWSTR lpszPathName)
505 {
506         FIXME("%p %lu %s %s %s stub\n",
507          lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
508          debugstr_w(lpszLongName), debugstr_w(lpszPathName));
509         return TRUE;
510 }
511
512 /*************************************************************************
513  * PathMakeUniqueName   [SHELL32.47]
514  */
515 BOOL WINAPI PathMakeUniqueNameAW(
516         LPVOID lpszBuffer,
517         DWORD dwBuffSize,
518         LPCVOID lpszShortName,
519         LPCVOID lpszLongName,
520         LPCVOID lpszPathName)
521 {
522         if (SHELL_OsIsUnicode())
523           return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
524         return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
525 }
526
527 /*************************************************************************
528  * PathYetAnotherMakeUniqueName [SHELL32.75]
529  *
530  * NOTES
531  *     exported by ordinal
532  */
533 BOOL WINAPI PathYetAnotherMakeUniqueName(
534         LPWSTR lpszBuffer,
535         LPCWSTR lpszPathName,
536         LPCWSTR lpszShortName,
537         LPCWSTR lpszLongName)
538 {
539     FIXME("(%p, %s, %s ,%s):stub.\n",
540           lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
541     return TRUE;
542 }
543
544
545 /*
546         ########## cleaning and resolving paths ##########
547  */
548
549 /*************************************************************************
550  * PathFindOnPath       [SHELL32.145]
551  */
552 BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID sOtherDirs)
553 {
554         if (SHELL_OsIsUnicode())
555           return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
556         return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
557 }
558
559 /*************************************************************************
560  * PathCleanupSpec      [SHELL32.171]
561  *
562  * lpszFile is changed in place.
563  */
564 int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
565 {
566     int i = 0;
567     DWORD rc = 0;
568     int length = 0;
569
570     if (SHELL_OsIsUnicode())
571     {
572         LPWSTR p = lpszFileW;
573
574         TRACE("Cleanup %s\n",debugstr_w(lpszFileW));
575
576         if (lpszPathW)
577             length = strlenW(lpszPathW);
578
579         while (*p)
580         {
581             int gct = PathGetCharTypeW(*p);
582             if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
583             {
584                 lpszFileW[i]='-';
585                 rc |= PCS_REPLACEDCHAR;
586             }
587             else
588                 lpszFileW[i]=*p;
589             i++;
590             p++;
591             if (length + i == MAX_PATH)
592             {
593                 rc |= PCS_FATAL | PCS_PATHTOOLONG;
594                 break;
595             }
596         }
597         lpszFileW[i]=0;
598     }
599     else
600     {
601         LPSTR lpszFileA = (LPSTR)lpszFileW;
602         LPCSTR lpszPathA = (LPSTR)lpszPathW;
603         LPSTR p = lpszFileA;
604
605         TRACE("Cleanup %s\n",debugstr_a(lpszFileA));
606
607         if (lpszPathA)
608             length = strlen(lpszPathA);
609
610         while (*p)
611         {
612             int gct = PathGetCharTypeA(*p);
613             if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
614             {
615                 lpszFileA[i]='-';
616                 rc |= PCS_REPLACEDCHAR;
617             }
618             else
619                 lpszFileA[i]=*p;
620             i++;
621             p++;
622             if (length + i == MAX_PATH)
623             {
624                 rc |= PCS_FATAL | PCS_PATHTOOLONG;
625                 break;
626             }
627         }
628         lpszFileA[i]=0;
629     }
630     return rc;
631 }
632
633 /*************************************************************************
634  * PathQualifyA         [SHELL32]
635  */
636 BOOL WINAPI PathQualifyA(LPCSTR pszPath)
637 {
638         FIXME("%s\n",pszPath);
639         return 0;
640 }
641
642 /*************************************************************************
643  * PathQualifyW         [SHELL32]
644  */
645 BOOL WINAPI PathQualifyW(LPCWSTR pszPath)
646 {
647         FIXME("%s\n",debugstr_w(pszPath));
648         return 0;
649 }
650
651 /*************************************************************************
652  * PathQualify  [SHELL32.49]
653  */
654 BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
655 {
656         if (SHELL_OsIsUnicode())
657           return PathQualifyW(pszPath);
658         return PathQualifyA(pszPath);
659 }
660
661 /*************************************************************************
662  * PathResolveA [SHELL32.51]
663  */
664 BOOL WINAPI PathResolveA(
665         LPSTR lpszPath,
666         LPCSTR *alpszPaths,
667         DWORD dwFlags)
668 {
669         FIXME("(%s,%p,0x%08lx),stub!\n",
670           lpszPath, *alpszPaths, dwFlags);
671         return 0;
672 }
673
674 /*************************************************************************
675  * PathResolveW [SHELL32]
676  */
677 BOOL WINAPI PathResolveW(
678         LPWSTR lpszPath,
679         LPCWSTR *alpszPaths,
680         DWORD dwFlags)
681 {
682         FIXME("(%s,%p,0x%08lx),stub!\n",
683           debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
684         return 0;
685 }
686
687 /*************************************************************************
688  * PathResolve [SHELL32.51]
689  */
690 BOOL WINAPI PathResolveAW(
691         LPVOID lpszPath,
692         LPCVOID *alpszPaths,
693         DWORD dwFlags)
694 {
695         if (SHELL_OsIsUnicode())
696           return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
697         return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
698 }
699
700 /*************************************************************************
701 *       PathProcessCommandA     [SHELL32.653]
702 */
703 LONG WINAPI PathProcessCommandA (
704         LPCSTR lpszPath,
705         LPSTR lpszBuff,
706         DWORD dwBuffSize,
707         DWORD dwFlags)
708 {
709         FIXME("%s %p 0x%04lx 0x%04lx stub\n",
710         lpszPath, lpszBuff, dwBuffSize, dwFlags);
711         if(!lpszPath) return -1;
712         if(lpszBuff) strcpy(lpszBuff, lpszPath);
713         return strlen(lpszPath);
714 }
715
716 /*************************************************************************
717 *       PathProcessCommandW
718 */
719 LONG WINAPI PathProcessCommandW (
720         LPCWSTR lpszPath,
721         LPWSTR lpszBuff,
722         DWORD dwBuffSize,
723         DWORD dwFlags)
724 {
725         FIXME("(%s, %p, 0x%04lx, 0x%04lx) stub\n",
726         debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
727         if(!lpszPath) return -1;
728         if(lpszBuff) strcpyW(lpszBuff, lpszPath);
729         return strlenW(lpszPath);
730 }
731
732 /*************************************************************************
733 *       PathProcessCommand (SHELL32.653)
734 */
735 LONG WINAPI PathProcessCommandAW (
736         LPCVOID lpszPath,
737         LPVOID lpszBuff,
738         DWORD dwBuffSize,
739         DWORD dwFlags)
740 {
741         if (SHELL_OsIsUnicode())
742           return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
743         return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
744 }
745
746 /*
747         ########## special ##########
748 */
749
750 /*************************************************************************
751  * PathSetDlgItemPath (SHELL32.48)
752  */
753 VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
754 {
755         if (SHELL_OsIsUnicode())
756             PathSetDlgItemPathW(hDlg, id, pszPath);
757         else
758             PathSetDlgItemPathA(hDlg, id, pszPath);
759 }
760
761 static const WCHAR szCurrentVersion[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\0'};
762 static const WCHAR Administrative_ToolsW[] = {'A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
763 static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
764 static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
765 static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
766 static const WCHAR Common_Administrative_ToolsW[] = {'C','o','m','m','o','n',' ','A','d','m','i','n','i','s','t','r','a','t','i','v','e',' ','T','o','o','l','s','\0'};
767 static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
768 static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
769 static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
770 static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
771 static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
772 static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
773 static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
774 static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
775 static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
776 static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
777 static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
778 static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
779 static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
780 static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
781 static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
782 static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
783 static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
784 static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
785 static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
786 static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','\0'};
787 static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
788 static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
789 static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
790 static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
791 static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
792 static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
793 static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
794 static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
795 static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
796 static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
797 static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
798 static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
799 static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
800 static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
801 static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
802 static const WCHAR ProfileListW[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','P','r','o','f','i','l','e','L','i','s','t',0};
803 static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
804 static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
805 static const WCHAR szSHFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
806 static const WCHAR szSHUserFolders[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l','o','r','e','r','\\','U','s','e','r',' ','S','h','e','l','l',' ','F','o','l','d','e','r','s','\0'};
807 /* This defaults to L"Documents and Settings" on Windows 2000/XP, but we're
808  * acting more Windows 9x-like for now.
809  */
810 static const WCHAR szDefaultProfileDirW[] = {'p','r','o','f','i','l','e','s','\0'};
811 static const WCHAR AllUsersW[] = {'A','l','l',' ','U','s','e','r','s','\0'};
812
813 typedef enum _CSIDL_Type {
814     CSIDL_Type_User,
815     CSIDL_Type_AllUsers,
816     CSIDL_Type_CurrVer,
817     CSIDL_Type_Disallowed,
818     CSIDL_Type_NonExistent,
819     CSIDL_Type_WindowsPath,
820     CSIDL_Type_SystemPath,
821 } CSIDL_Type;
822
823 typedef struct
824 {
825     CSIDL_Type type;
826     LPCWSTR    szValueName;
827     LPCWSTR    szDefaultPath; /* fallback string or resource ID */
828 } CSIDL_DATA;
829
830 static const CSIDL_DATA CSIDL_Data[] =
831 {
832     { /* 0x00 - CSIDL_DESKTOP */
833         CSIDL_Type_User,
834         DesktopW,
835         MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
836     },
837     { /* 0x01 - CSIDL_INTERNET */
838         CSIDL_Type_Disallowed,
839         NULL,
840         NULL
841     },
842     { /* 0x02 - CSIDL_PROGRAMS */
843         CSIDL_Type_User,
844         ProgramsW,
845         MAKEINTRESOURCEW(IDS_PROGRAMS)
846     },
847     { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
848         CSIDL_Type_SystemPath,
849         NULL,
850         NULL
851     },
852     { /* 0x04 - CSIDL_PRINTERS */
853         CSIDL_Type_SystemPath,
854         NULL,
855         NULL
856     },
857     { /* 0x05 - CSIDL_PERSONAL */
858         CSIDL_Type_User,
859         PersonalW,
860         MAKEINTRESOURCEW(IDS_PERSONAL)
861     },
862     { /* 0x06 - CSIDL_FAVORITES */
863         CSIDL_Type_User,
864         FavoritesW,
865         MAKEINTRESOURCEW(IDS_FAVORITES)
866     },
867     { /* 0x07 - CSIDL_STARTUP */
868         CSIDL_Type_User,
869         StartUpW,
870         MAKEINTRESOURCEW(IDS_STARTUP)
871     },
872     { /* 0x08 - CSIDL_RECENT */
873         CSIDL_Type_User,
874         RecentW,
875         MAKEINTRESOURCEW(IDS_RECENT)
876     },
877     { /* 0x09 - CSIDL_SENDTO */
878         CSIDL_Type_User,
879         SendToW,
880         MAKEINTRESOURCEW(IDS_SENDTO)
881     },
882     { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
883         CSIDL_Type_Disallowed,
884         NULL,
885         NULL,
886     },
887     { /* 0x0b - CSIDL_STARTMENU */
888         CSIDL_Type_User,
889         Start_MenuW,
890         MAKEINTRESOURCEW(IDS_STARTMENU)
891     },
892     { /* 0x0c - CSIDL_MYDOCUMENTS */
893         CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
894         NULL,
895         NULL
896     },
897     { /* 0x0d - CSIDL_MYMUSIC */
898         CSIDL_Type_User,
899         My_MusicW,
900         MAKEINTRESOURCEW(IDS_MYMUSIC)
901     },
902     { /* 0x0e - CSIDL_MYVIDEO */
903         CSIDL_Type_User,
904         My_VideoW,
905         MAKEINTRESOURCEW(IDS_MYVIDEO)
906     },
907     { /* 0x0f - unassigned */
908         CSIDL_Type_Disallowed,
909         NULL,
910         NULL,
911     },
912     { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
913         CSIDL_Type_User,
914         DesktopW,
915         MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
916     },
917     { /* 0x11 - CSIDL_DRIVES */
918         CSIDL_Type_Disallowed,
919         NULL,
920         NULL,
921     },
922     { /* 0x12 - CSIDL_NETWORK */
923         CSIDL_Type_Disallowed,
924         NULL,
925         NULL,
926     },
927     { /* 0x13 - CSIDL_NETHOOD */
928         CSIDL_Type_User,
929         NetHoodW,
930         MAKEINTRESOURCEW(IDS_NETHOOD)
931     },
932     { /* 0x14 - CSIDL_FONTS */
933         CSIDL_Type_WindowsPath,
934         NULL,
935         FontsW
936     },
937     { /* 0x15 - CSIDL_TEMPLATES */
938         CSIDL_Type_User,
939         TemplatesW,
940         MAKEINTRESOURCEW(IDS_TEMPLATES)
941     },
942     { /* 0x16 - CSIDL_COMMON_STARTMENU */
943         CSIDL_Type_AllUsers,
944         Common_Start_MenuW,
945         MAKEINTRESOURCEW(IDS_STARTMENU)
946     },
947     { /* 0x17 - CSIDL_COMMON_PROGRAMS */
948         CSIDL_Type_AllUsers,
949         Common_ProgramsW,
950         MAKEINTRESOURCEW(IDS_PROGRAMS)
951     },
952     { /* 0x18 - CSIDL_COMMON_STARTUP */
953         CSIDL_Type_AllUsers,
954         Common_StartUpW,
955         MAKEINTRESOURCEW(IDS_STARTUP)
956     },
957     { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
958         CSIDL_Type_AllUsers,
959         Common_DesktopW,
960         MAKEINTRESOURCEW(IDS_DESKTOP)
961     },
962     { /* 0x1a - CSIDL_APPDATA */
963         CSIDL_Type_User,
964         AppDataW,
965         MAKEINTRESOURCEW(IDS_APPDATA)
966     },
967     { /* 0x1b - CSIDL_PRINTHOOD */
968         CSIDL_Type_User,
969         PrintHoodW,
970         MAKEINTRESOURCEW(IDS_PRINTHOOD)
971     },
972     { /* 0x1c - CSIDL_LOCAL_APPDATA */
973         CSIDL_Type_User,
974         Local_AppDataW,
975         MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
976     },
977     { /* 0x1d - CSIDL_ALTSTARTUP */
978         CSIDL_Type_NonExistent,
979         NULL,
980         NULL
981     },
982     { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
983         CSIDL_Type_NonExistent,
984         NULL,
985         NULL
986     },
987     { /* 0x1f - CSIDL_COMMON_FAVORITES */
988         CSIDL_Type_AllUsers,
989         FavoritesW,
990         MAKEINTRESOURCEW(IDS_FAVORITES)
991     },
992     { /* 0x20 - CSIDL_INTERNET_CACHE */
993         CSIDL_Type_User,
994         CacheW,
995         MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
996     },
997     { /* 0x21 - CSIDL_COOKIES */
998         CSIDL_Type_User,
999         CookiesW,
1000         MAKEINTRESOURCEW(IDS_COOKIES)
1001     },
1002     { /* 0x22 - CSIDL_HISTORY */
1003         CSIDL_Type_User,
1004         HistoryW,
1005         MAKEINTRESOURCEW(IDS_HISTORY)
1006     },
1007     { /* 0x23 - CSIDL_COMMON_APPDATA */
1008         CSIDL_Type_AllUsers,
1009         Common_AppDataW,
1010         MAKEINTRESOURCEW(IDS_APPDATA)
1011     },
1012     { /* 0x24 - CSIDL_WINDOWS */
1013         CSIDL_Type_WindowsPath,
1014         NULL,
1015         NULL
1016     },
1017     { /* 0x25 - CSIDL_SYSTEM */
1018         CSIDL_Type_SystemPath,
1019         NULL,
1020         NULL
1021     },
1022     { /* 0x26 - CSIDL_PROGRAM_FILES */
1023         CSIDL_Type_CurrVer,
1024         ProgramFilesDirW,
1025         MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1026     },
1027     { /* 0x27 - CSIDL_MYPICTURES */
1028         CSIDL_Type_User,
1029         My_PicturesW,
1030         MAKEINTRESOURCEW(IDS_MYPICTURES)
1031     },
1032     { /* 0x28 - CSIDL_PROFILE */
1033         CSIDL_Type_User,
1034         NULL,
1035         NULL
1036     },
1037     { /* 0x29 - CSIDL_SYSTEMX86 */
1038         CSIDL_Type_NonExistent,
1039         NULL,
1040         NULL
1041     },
1042     { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
1043         CSIDL_Type_NonExistent,
1044         NULL,
1045         NULL
1046     },
1047     { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
1048         CSIDL_Type_CurrVer,
1049         CommonFilesDirW,
1050         MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1051     },
1052     { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
1053         CSIDL_Type_NonExistent,
1054         NULL,
1055         NULL
1056     },
1057     { /* 0x2d - CSIDL_COMMON_TEMPLATES */
1058         CSIDL_Type_AllUsers,
1059         Common_TemplatesW,
1060         MAKEINTRESOURCEW(IDS_TEMPLATES)
1061     },
1062     { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
1063         CSIDL_Type_AllUsers,
1064         Common_DocumentsW,
1065         MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1066     },
1067     { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
1068         CSIDL_Type_AllUsers,
1069         Common_Administrative_ToolsW,
1070         MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1071     },
1072     { /* 0x30 - CSIDL_ADMINTOOLS */
1073         CSIDL_Type_User,
1074         Administrative_ToolsW,
1075         MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1076     },
1077     { /* 0x31 - CSIDL_CONNECTIONS */
1078         CSIDL_Type_Disallowed,
1079         NULL,
1080         NULL
1081     },
1082     { /* 0x32 - unassigned */
1083         CSIDL_Type_Disallowed,
1084         NULL,
1085         NULL
1086     },
1087     { /* 0x33 - unassigned */
1088         CSIDL_Type_Disallowed,
1089         NULL,
1090         NULL
1091     },
1092     { /* 0x34 - unassigned */
1093         CSIDL_Type_Disallowed,
1094         NULL,
1095         NULL
1096     },
1097     { /* 0x35 - CSIDL_COMMON_MUSIC */
1098         CSIDL_Type_AllUsers,
1099         CommonMusicW,
1100         MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1101     },
1102     { /* 0x36 - CSIDL_COMMON_PICTURES */
1103         CSIDL_Type_AllUsers,
1104         CommonPicturesW,
1105         MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1106     },
1107     { /* 0x37 - CSIDL_COMMON_VIDEO */
1108         CSIDL_Type_AllUsers,
1109         CommonVideoW,
1110         MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1111     },
1112     { /* 0x38 - CSIDL_RESOURCES */
1113         CSIDL_Type_WindowsPath,
1114         NULL,
1115         ResourcesW
1116     },
1117     { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
1118         CSIDL_Type_NonExistent,
1119         NULL,
1120         NULL
1121     },
1122     { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
1123         CSIDL_Type_NonExistent,
1124         NULL,
1125         NULL
1126     },
1127     { /* 0x3b - CSIDL_CDBURN_AREA */
1128         CSIDL_Type_User,
1129         CD_BurningW,
1130         MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1131     },
1132     { /* 0x3c unassigned */
1133         CSIDL_Type_Disallowed,
1134         NULL,
1135         NULL
1136     },
1137     { /* 0x3d - CSIDL_COMPUTERSNEARME */
1138         CSIDL_Type_Disallowed, /* FIXME */
1139         NULL,
1140         NULL
1141     },
1142     { /* 0x3e - CSIDL_PROFILES */
1143         CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
1144         NULL,
1145         NULL
1146     }
1147 };
1148
1149 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1150
1151 /* Gets the value named value from the registry key
1152  * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1153  * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
1154  * is assumed to be MAX_PATH WCHARs in length.
1155  * If it exists, expands the value and writes the expanded value to
1156  * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
1157  * Returns successful error code if the value was retrieved from the registry,
1158  * and a failure otherwise.
1159  */
1160 static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
1161  LPCWSTR value, LPWSTR path)
1162 {
1163     HRESULT hr;
1164     WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
1165     LPCWSTR pShellFolderPath, pUserShellFolderPath;
1166     DWORD dwDisp, dwType, dwPathLen = MAX_PATH;
1167     HKEY userShellFolderKey, shellFolderKey;
1168
1169     TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
1170      path);
1171
1172     if (userPrefix)
1173     {
1174         strcpyW(shellFolderPath, userPrefix);
1175         PathAddBackslashW(shellFolderPath);
1176         strcatW(shellFolderPath, szSHFolders);
1177         pShellFolderPath = shellFolderPath;
1178         strcpyW(userShellFolderPath, userPrefix);
1179         PathAddBackslashW(userShellFolderPath);
1180         strcatW(userShellFolderPath, szSHUserFolders);
1181         pUserShellFolderPath = userShellFolderPath;
1182     }
1183     else
1184     {
1185         pUserShellFolderPath = szSHUserFolders;
1186         pShellFolderPath = szSHFolders;
1187     }
1188
1189     if (RegCreateKeyExW(rootKey, pShellFolderPath, 0, NULL, 0, KEY_ALL_ACCESS,
1190      NULL, &shellFolderKey, &dwDisp))
1191     {
1192         TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
1193         return E_FAIL;
1194     }
1195     if (RegCreateKeyExW(rootKey, pUserShellFolderPath, 0, NULL, 0,
1196      KEY_ALL_ACCESS, NULL, &userShellFolderKey, &dwDisp))
1197     {
1198         TRACE("Failed to create %s\n",
1199          debugstr_w(pUserShellFolderPath));
1200         RegCloseKey(shellFolderKey);
1201         return E_FAIL;
1202     }
1203
1204     if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
1205      (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
1206     {
1207         LONG ret;
1208
1209         path[dwPathLen / sizeof(WCHAR)] = '\0';
1210         if (dwType == REG_EXPAND_SZ && path[0] == '%')
1211         {
1212             WCHAR szTemp[MAX_PATH];
1213
1214             _SHExpandEnvironmentStrings(path, szTemp);
1215             lstrcpynW(path, szTemp, MAX_PATH);
1216         }
1217         ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1218          (strlenW(path) + 1) * sizeof(WCHAR));
1219         if (ret != ERROR_SUCCESS)
1220             hr = HRESULT_FROM_WIN32(ret);
1221         else
1222             hr = S_OK;
1223     }
1224     else
1225         hr = E_FAIL;
1226     RegCloseKey(shellFolderKey);
1227     RegCloseKey(userShellFolderKey);
1228     TRACE("returning 0x%08lx\n", hr);
1229     return hr;
1230 }
1231
1232 /* Helper function for _SHGetDefaultValue 
1233  *
1234  *   handing the directories under $HOME:
1235  *   1) try path under $HOME (such as $HOME/My Documents/My Pictures), if it
1236  *   exists return it.
1237  *   2) if not, but $HOME/My Documents exists return path 1 and have it created
1238  *   3) try $HOME if it exists return it
1239  *   4) normal fallback to C:/windows/Profiles/...
1240  */
1241 static HRESULT expand_home_path(LPWSTR pszPath, LPCWSTR def_path, UINT resource,
1242                                 BOOL create_lastdir)
1243 {
1244     HRESULT hr = E_FAIL;
1245     const char *home = getenv("HOME");
1246
1247     if (home)
1248     {
1249         LPWSTR homeW = wine_get_dos_file_name(home);
1250
1251         if (homeW)
1252         {
1253             WCHAR resourcePath[MAX_PATH];
1254             lstrcpynW(pszPath, homeW, MAX_PATH);
1255
1256             if (LoadStringW(shell32_hInstance, resource, resourcePath, MAX_PATH))
1257                 PathAppendW(pszPath, resourcePath);
1258             else
1259                 PathAppendW(pszPath, def_path);
1260
1261             if (PathIsDirectoryW(pszPath)) hr = S_OK;
1262             else if (create_lastdir)
1263             {
1264                 /* attempt 2, try for My Documents */
1265
1266                 WCHAR* ptr = strrchrW(pszPath, '\\');
1267                 if (ptr)
1268                 {
1269                     *ptr = 0;
1270                     if (PathIsDirectoryW(pszPath))
1271                     {
1272                         *ptr = '\\';
1273                         hr = S_OK;
1274                     }
1275                 }
1276             }
1277
1278             if (hr != S_OK)
1279             {
1280                 /* attempt 3 return HOME */
1281                 lstrcpyW(pszPath,homeW);
1282                 hr = S_OK;
1283             }
1284             HeapFree(GetProcessHeap(), 0, homeW);
1285         }
1286         else
1287             hr = HRESULT_FROM_WIN32(GetLastError());
1288     }
1289     return hr;
1290 }
1291
1292 /* Gets a 'semi-expanded' default value of the CSIDL with index folder into
1293  * pszPath, based on the entries in CSIDL_Data.  By semi-expanded, I mean:
1294  * - The entry's szDefaultPath may be either a string value or an integer
1295  *   resource identifier.  In the latter case, the string value of the resource
1296  *   is written.
1297  * - Depending on the entry's type, the path may begin with an (unexpanded)
1298  *   environment variable name.  The caller is responsible for expanding
1299  *   environment strings if so desired.
1300  *   The types that are prepended with environment variables are:
1301  *   CSIDL_Type_User:     %USERPROFILE%
1302  *   CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
1303  *   CSIDL_Type_CurrVer:  %SystemDrive%
1304  *   (Others might make sense too, but as yet are unneeded.)
1305  */
1306 static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
1307 {
1308     HRESULT hr;
1309     WCHAR resourcePath[MAX_PATH];
1310     LPCWSTR pDefaultPath = NULL;
1311
1312     TRACE("0x%02x,%p\n", folder, pszPath);
1313
1314     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1315         return E_INVALIDARG;
1316     if (!pszPath)
1317         return E_INVALIDARG;
1318
1319     /* Try special cases first */
1320     hr = E_FAIL;
1321     switch (folder)
1322     {
1323         case CSIDL_MYPICTURES:
1324             hr = expand_home_path(pszPath,My_PicturesW,IDS_MYPICTURES,TRUE);
1325             break;
1326         case CSIDL_PERSONAL:
1327             hr = expand_home_path(pszPath,PersonalW,IDS_PERSONAL,FALSE);
1328             break;
1329         case CSIDL_MYMUSIC:
1330             hr = expand_home_path(pszPath,My_MusicW,IDS_MYMUSIC,TRUE);
1331             break;
1332         case CSIDL_MYVIDEO:
1333             hr = expand_home_path(pszPath,My_VideoW,IDS_MYVIDEO,TRUE);
1334             break;
1335         case CSIDL_DESKTOP:
1336         case CSIDL_DESKTOPDIRECTORY:
1337         {
1338             const char *home = getenv("HOME");
1339
1340             /* special case for Desktop, map to $HOME/Desktop if it exists */
1341             if (home)
1342             {
1343                 LPWSTR homeW = wine_get_dos_file_name(home);
1344
1345                 if (homeW)
1346                 {
1347                     lstrcpynW(pszPath, homeW, MAX_PATH);
1348                     if (PathAppendW(pszPath, DesktopW))
1349                     {
1350                         if (PathIsDirectoryW(pszPath))
1351                             hr = S_OK;
1352                     }
1353                     else
1354                         hr = HRESULT_FROM_WIN32(GetLastError());
1355                     HeapFree(GetProcessHeap(), 0, homeW);
1356                 }
1357                 else
1358                     hr = HRESULT_FROM_WIN32(GetLastError());
1359             }
1360             break;
1361         }
1362     }
1363     if (SUCCEEDED(hr))
1364         return hr;
1365
1366     /* Either the folder was unhandled, or a suitable default wasn't found,
1367      * so use one of the resource-based defaults
1368      */
1369     if (CSIDL_Data[folder].szDefaultPath &&
1370      IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
1371     {
1372         if (LoadStringW(shell32_hInstance,
1373          LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1374         {
1375             hr = S_OK;
1376             pDefaultPath = resourcePath;
1377         }
1378         else
1379         {
1380             FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
1381              debugstr_w(pszPath));
1382             hr = E_FAIL;
1383         }
1384     }
1385     else
1386     {
1387         hr = S_OK;
1388         pDefaultPath = CSIDL_Data[folder].szDefaultPath;
1389     }
1390     if (SUCCEEDED(hr))
1391     {
1392         switch (CSIDL_Data[folder].type)
1393         {
1394             case CSIDL_Type_User:
1395                 strcpyW(pszPath, UserProfileW);
1396                 break;
1397             case CSIDL_Type_AllUsers:
1398                 strcpyW(pszPath, AllUsersProfileW);
1399                 break;
1400             case CSIDL_Type_CurrVer:
1401                 strcpyW(pszPath, SystemDriveW);
1402                 break;
1403             default:
1404                 ; /* no corresponding env. var, do nothing */
1405         }
1406         if (pDefaultPath)
1407         {
1408             PathAddBackslashW(pszPath);
1409             strcatW(pszPath, pDefaultPath);
1410         }
1411     }
1412     TRACE("returning 0x%08lx\n", hr);
1413     return hr;
1414 }
1415
1416 /* Gets the (unexpanded) value of the folder with index folder into pszPath.
1417  * The folder's type is assumed to be CSIDL_Type_CurrVer.  Its default value
1418  * can be overridden in the HKLM\\szCurrentVersion key.
1419  * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
1420  * the registry, uses _SHGetDefaultValue to get the value.
1421  */
1422 static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
1423  LPWSTR pszPath)
1424 {
1425     HRESULT hr;
1426
1427     TRACE("0x%08lx,0x%02x,%p\n", dwFlags, folder, pszPath);
1428
1429     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1430         return E_INVALIDARG;
1431     if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
1432         return E_INVALIDARG;
1433     if (!pszPath)
1434         return E_INVALIDARG;
1435
1436     if (dwFlags & SHGFP_TYPE_DEFAULT)
1437         hr = _SHGetDefaultValue(folder, pszPath);
1438     else
1439     {
1440         HKEY hKey;
1441         DWORD dwDisp;
1442
1443         if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szCurrentVersion, 0,
1444          NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp))
1445             hr = E_FAIL;
1446         else
1447         {
1448             DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);
1449
1450             if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
1451              &dwType, (LPBYTE)pszPath, &dwPathLen) ||
1452              (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
1453             {
1454                 hr = _SHGetDefaultValue(folder, pszPath);
1455                 dwType = REG_EXPAND_SZ;
1456                 RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
1457                  (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
1458             }
1459             else
1460             {
1461                 pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
1462                 hr = S_OK;
1463             }
1464             RegCloseKey(hKey);
1465         }
1466     }
1467     TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1468     return hr;
1469 }
1470
1471 /* Gets the user's path (unexpanded) for the CSIDL with index folder:
1472  * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it.  Otherwise
1473  * calls _SHGetUserShellFolderPath for it.  Where it looks depends on hToken:
1474  * - if hToken is -1, looks in HKEY_USERS\.Default
1475  * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
1476  *   if HKEY_CURRENT_USER doesn't contain any entries.  If both fail, finally
1477  *   calls _SHGetDefaultValue for it.
1478  */
1479 static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
1480  LPWSTR pszPath)
1481 {
1482     HRESULT hr;
1483
1484     TRACE("%p,0x%08lx,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1485
1486     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1487         return E_INVALIDARG;
1488     if (CSIDL_Data[folder].type != CSIDL_Type_User)
1489         return E_INVALIDARG;
1490     if (!pszPath)
1491         return E_INVALIDARG;
1492
1493     /* Only the current user and the default user are supported right now
1494      * I'm afraid.
1495      * FIXME: should be able to call GetTokenInformation on the token,
1496      * then call ConvertSidToStringSidW on it to get the user prefix.
1497      * But Wine's registry doesn't store user info by sid, it stores it
1498      * by user name (and I don't know how to convert from a token to a
1499      * user name).
1500      */
1501     if (hToken != NULL && hToken != (HANDLE)-1)
1502     {
1503         FIXME("unsupported for user other than current or default\n");
1504         return E_FAIL;
1505     }
1506
1507     if (dwFlags & SHGFP_TYPE_DEFAULT)
1508         hr = _SHGetDefaultValue(folder, pszPath);
1509     else
1510     {
1511         LPCWSTR userPrefix = NULL;
1512         HKEY hRootKey;
1513
1514         if (hToken == (HANDLE)-1)
1515         {
1516             hRootKey = HKEY_USERS;
1517             userPrefix = DefaultW;
1518         }
1519         else /* hToken == NULL, other values disallowed above */
1520             hRootKey = HKEY_CURRENT_USER;
1521         hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
1522          CSIDL_Data[folder].szValueName, pszPath);
1523         if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
1524             hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1525              CSIDL_Data[folder].szValueName, pszPath);
1526         if (FAILED(hr))
1527             hr = _SHGetDefaultValue(folder, pszPath);
1528     }
1529     TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1530     return hr;
1531 }
1532
1533 /* Gets the (unexpanded) path for the CSIDL with index folder.  If dwFlags has
1534  * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue.  Otherwise calls
1535  * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
1536  * If this fails, falls back to _SHGetDefaultValue.
1537  */
1538 static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
1539  LPWSTR pszPath)
1540 {
1541     HRESULT hr;
1542
1543     TRACE("0x%08lx,0x%02x,%p\n", dwFlags, folder, pszPath);
1544
1545     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1546         return E_INVALIDARG;
1547     if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
1548         return E_INVALIDARG;
1549     if (!pszPath)
1550         return E_INVALIDARG;
1551
1552     if (dwFlags & SHGFP_TYPE_DEFAULT)
1553         hr = _SHGetDefaultValue(folder, pszPath);
1554     else
1555     {
1556         hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
1557          CSIDL_Data[folder].szValueName, pszPath);
1558         if (FAILED(hr))
1559             hr = _SHGetDefaultValue(folder, pszPath);
1560     }
1561     TRACE("returning 0x%08lx (output path is %s)\n", hr, debugstr_w(pszPath));
1562     return hr;
1563 }
1564
1565 static HRESULT _SHOpenProfilesKey(PHKEY pKey)
1566 {
1567     LONG lRet;
1568     DWORD disp;
1569
1570     lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
1571      KEY_ALL_ACCESS, NULL, pKey, &disp);
1572     return HRESULT_FROM_WIN32(lRet);
1573 }
1574
1575 /* Reads the value named szValueName from the key profilesKey (assumed to be
1576  * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
1577  * WCHARs in length.  If it doesn't exist, returns szDefault (and saves
1578  * szDefault to the registry).
1579  */
1580 static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
1581  LPWSTR szValue, LPCWSTR szDefault)
1582 {
1583     HRESULT hr;
1584     DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
1585     LONG lRet;
1586
1587     TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
1588      debugstr_w(szDefault));
1589     lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
1590      (LPBYTE)szValue, &dwPathLen);
1591     if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
1592      && *szValue)
1593     {
1594         dwPathLen /= sizeof(WCHAR);
1595         szValue[dwPathLen] = '\0';
1596         hr = S_OK;
1597     }
1598     else
1599     {
1600         /* Missing or invalid value, set a default */
1601         lstrcpynW(szValue, szDefault, MAX_PATH);
1602         TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1603                                                   debugstr_w(szValue));
1604         lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1605                               (LPBYTE)szValue,
1606                               (strlenW(szValue) + 1) * sizeof(WCHAR));
1607         if (lRet)
1608             hr = HRESULT_FROM_WIN32(lRet);
1609         else
1610             hr = S_OK;
1611     }
1612     TRACE("returning 0x%08lx (output value is %s)\n", hr, debugstr_w(szValue));
1613     return hr;
1614 }
1615
1616 /* Attempts to expand environment variables from szSrc into szDest, which is
1617  * assumed to be MAX_PATH characters in length.  Before referring to the
1618  * environment, handles a few variables directly, because the environment
1619  * variables may not be set when this is called (as during Wine's installation
1620  * when default values are being written to the registry).
1621  * The directly handled environment variables, and their source, are:
1622  * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
1623  * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
1624  *   path
1625  * If one of the directly handled environment variables is expanded, only
1626  * expands a single variable, and only in the beginning of szSrc.
1627  */
1628 static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
1629 {
1630     HRESULT hr;
1631     WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
1632     HKEY key = NULL;
1633
1634     TRACE("%s, %p\n", debugstr_w(szSrc), szDest);
1635
1636     if (!szSrc || !szDest) return E_INVALIDARG;
1637
1638     /* short-circuit if there's nothing to expand */
1639     if (szSrc[0] != '%')
1640     {
1641         strcpyW(szDest, szSrc);
1642         hr = S_OK;
1643         goto end;
1644     }
1645     /* Get the profile prefix, we'll probably be needing it */
1646     hr = _SHOpenProfilesKey(&key);
1647     if (SUCCEEDED(hr))
1648     {
1649         WCHAR szDefaultProfilesPrefix[MAX_PATH];
1650
1651         GetWindowsDirectoryW(szDefaultProfilesPrefix, MAX_PATH);
1652         PathAddBackslashW(szDefaultProfilesPrefix);
1653         PathAppendW(szDefaultProfilesPrefix, szDefaultProfileDirW);
1654         hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix,
1655          szDefaultProfilesPrefix);
1656     }
1657
1658     *szDest = 0;
1659     strcpyW(szTemp, szSrc);
1660     while (SUCCEEDED(hr) && szTemp[0] == '%')
1661     {
1662         if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
1663         {
1664             WCHAR szAllUsers[MAX_PATH];
1665
1666             strcpyW(szDest, szProfilesPrefix);
1667             hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
1668              szAllUsers, AllUsersW);
1669             PathAppendW(szDest, szAllUsers);
1670             PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
1671         }
1672         else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
1673         {
1674             WCHAR userName[MAX_PATH];
1675             DWORD userLen = MAX_PATH;
1676
1677             strcpyW(szDest, szProfilesPrefix);
1678             GetUserNameW(userName, &userLen);
1679             PathAppendW(szDest, userName);
1680             PathAppendW(szDest, szTemp + strlenW(UserProfileW));
1681         }
1682         else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
1683         {
1684             GetSystemDirectoryW(szDest, MAX_PATH);
1685             if (szDest[1] != ':')
1686             {
1687                 FIXME("non-drive system paths unsupported\n");
1688                 hr = E_FAIL;
1689             }
1690             else
1691             {
1692                 strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
1693                 hr = S_OK;
1694             }
1695         }
1696         else
1697         {
1698             DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);
1699
1700             if (ret > MAX_PATH)
1701                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
1702             else if (ret == 0)
1703                 hr = HRESULT_FROM_WIN32(GetLastError());
1704             else
1705                 hr = S_OK;
1706         }
1707         if (SUCCEEDED(hr) && szDest[0] == '%')
1708             strcpyW(szTemp, szDest);
1709         else
1710         {
1711             /* terminate loop */
1712             szTemp[0] = '\0';
1713         }
1714     }
1715 end:
1716     if (key)
1717         RegCloseKey(key);
1718     TRACE("returning 0x%08lx (input was %s, output is %s)\n", hr,
1719      debugstr_w(szSrc), debugstr_w(szDest));
1720     return hr;
1721 }
1722
1723 /*************************************************************************
1724  * SHGetFolderPathW                     [SHELL32.@]
1725  *
1726  * Convert nFolder to path.  
1727  *
1728  * RETURNS
1729  *  Success: S_OK
1730  *  Failure: standard HRESULT error codes.
1731  *
1732  * NOTES
1733  * Most values can be overridden in either
1734  * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
1735  * or in the same location in HKLM.
1736  * The registry usage is explained by the following tech note:
1737  * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36173.asp
1738  * The "Shell Folders" registry key was used in NT4 and earlier systems.
1739  * Beginning with Windows 2000, the "User Shell Folders" key is used, so
1740  * changes made to it are made to the former key too.  This synchronization is
1741  * done on-demand: not until someone requests the value of one of these paths
1742  * (by calling one of the SHGet functions) is the value synchronized.
1743  * Furthermore, as explained here:
1744  * http://www.microsoft.com/windows2000/techinfo/reskit/en-us/default.asp?url=/windows2000/techinfo/reskit/en-us/regentry/36276.asp
1745  * the HKCU paths take precedence over the HKLM paths.
1746  *
1747  */
1748 HRESULT WINAPI SHGetFolderPathW(
1749         HWND hwndOwner,    /* [I] owner window */
1750         int nFolder,       /* [I] CSIDL identifying the folder */
1751         HANDLE hToken,     /* [I] access token */
1752         DWORD dwFlags,     /* [I] which path to return */
1753         LPWSTR pszPath)    /* [O] converted path */
1754 {
1755     HRESULT    hr;
1756     WCHAR      szBuildPath[MAX_PATH], szTemp[MAX_PATH];
1757     DWORD      folder = nFolder & CSIDL_FOLDER_MASK;
1758     CSIDL_Type type;
1759     int        ret;
1760     
1761     TRACE("%p,%p,nFolder=0x%04x\n", hwndOwner,pszPath,nFolder);
1762
1763     /* Windows always NULL-terminates the resulting path regardless of success
1764      * or failure, so do so first
1765      */
1766     if (pszPath)
1767         *pszPath = '\0';
1768     if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
1769         return E_INVALIDARG;
1770     szTemp[0] = 0;
1771     type = CSIDL_Data[folder].type;
1772     switch (type)
1773     {
1774         case CSIDL_Type_Disallowed:
1775             hr = E_INVALIDARG;
1776             break;
1777         case CSIDL_Type_NonExistent:
1778             hr = S_FALSE;
1779             break;
1780         case CSIDL_Type_WindowsPath:
1781             GetWindowsDirectoryW(szTemp, MAX_PATH);
1782             if (CSIDL_Data[folder].szDefaultPath &&
1783              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1784              *CSIDL_Data[folder].szDefaultPath)
1785             {
1786                 PathAddBackslashW(szTemp);
1787                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1788             }
1789             hr = S_OK;
1790             break;
1791         case CSIDL_Type_SystemPath:
1792             GetSystemDirectoryW(szTemp, MAX_PATH);
1793             if (CSIDL_Data[folder].szDefaultPath &&
1794              !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
1795              *CSIDL_Data[folder].szDefaultPath)
1796             {
1797                 PathAddBackslashW(szTemp);
1798                 strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
1799             }
1800             hr = S_OK;
1801             break;
1802         case CSIDL_Type_CurrVer:
1803             hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
1804             break;
1805         case CSIDL_Type_User:
1806             hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
1807             break;
1808         case CSIDL_Type_AllUsers:
1809             hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
1810             break;
1811         default:
1812             FIXME("bogus type %d, please fix\n", type);
1813             hr = E_INVALIDARG;
1814             break;
1815     }
1816
1817     /* Expand environment strings if necessary */
1818     if (*szTemp == '%')
1819         hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
1820     else
1821         strcpyW(szBuildPath, szTemp);
1822     /* Copy the path if it's available before we might return */
1823     if (SUCCEEDED(hr) && pszPath)
1824         strcpyW(pszPath, szBuildPath);
1825
1826     if (FAILED(hr)) goto end;
1827
1828     /* if we don't care about existing directories we are ready */
1829     if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;
1830
1831     if (PathFileExistsW(szBuildPath)) goto end;
1832
1833     /* not existing but we are not allowed to create it.  The return value
1834      * is verified against shell32 version 6.0.
1835      */
1836     if (!(nFolder & CSIDL_FLAG_CREATE))
1837     {
1838         hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1839         goto end;
1840     }
1841
1842     /* create directory/directories */
1843     ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
1844     if (ret && ret != ERROR_ALREADY_EXISTS)
1845     {
1846         ERR("Failed to create directory '%s'.\n", debugstr_w(szBuildPath));
1847         hr = E_FAIL;
1848         goto end;
1849     }
1850
1851     TRACE("Created missing system directory '%s'\n", debugstr_w(szBuildPath));
1852 end:
1853     TRACE("returning 0x%08lx (final path is %s)\n", hr, debugstr_w(szBuildPath));
1854     return hr;
1855 }
1856
1857 /*************************************************************************
1858  * SHGetFolderPathA                     [SHELL32.@]
1859  *
1860  * See SHGetFolderPathW.
1861  */
1862 HRESULT WINAPI SHGetFolderPathA(
1863         HWND hwndOwner,
1864         int nFolder,
1865         HANDLE hToken,
1866         DWORD dwFlags,
1867         LPSTR pszPath)
1868 {
1869     WCHAR szTemp[MAX_PATH];
1870     HRESULT hr;
1871
1872     TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1873
1874     if (pszPath)
1875         *pszPath = '\0';
1876     hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
1877     if (SUCCEEDED(hr) && pszPath)
1878         WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
1879          NULL);
1880
1881     return hr;
1882 }
1883
1884 /* For each folder in folders, if its value has not been set in the registry,
1885  * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1886  * folder's type) to get the unexpanded value first.
1887  * Writes the unexpanded value to User Shell Folders, and queries it with
1888  * SHGetFolderPathW to force the creation of the directory if it doesn't
1889  * already exist.  SHGetFolderPathW also returns the expanded value, which
1890  * this then writes to Shell Folders.
1891  */
1892 static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1893  LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
1894  UINT foldersLen)
1895 {
1896     UINT i;
1897     WCHAR path[MAX_PATH];
1898     HRESULT hr = S_OK;
1899     HKEY hUserKey = NULL, hKey = NULL;
1900     DWORD dwDisp, dwType, dwPathLen;
1901     LONG ret;
1902
1903     TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
1904      debugstr_w(szUserShellFolderPath), folders, foldersLen);
1905
1906     ret = RegCreateKeyExW(hRootKey, szUserShellFolderPath, 0, NULL, 0,
1907      KEY_ALL_ACCESS, NULL, &hUserKey, &dwDisp);
1908     if (ret)
1909         hr = HRESULT_FROM_WIN32(ret);
1910     else
1911     {
1912         ret = RegCreateKeyExW(hRootKey, szShellFolderPath, 0, NULL, 0,
1913          KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
1914         if (ret)
1915             hr = HRESULT_FROM_WIN32(ret);
1916     }
1917     for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
1918     {
1919         dwPathLen = MAX_PATH * sizeof(WCHAR);
1920         if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1921          &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
1922          dwType != REG_EXPAND_SZ))
1923         {
1924             *path = '\0';
1925             if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
1926                 _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
1927                  path);
1928             else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
1929                 _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1930             else
1931                 hr = E_FAIL;
1932             if (*path)
1933             {
1934                 ret = RegSetValueExW(hUserKey,
1935                  CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
1936                  (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1937                 if (ret)
1938                     hr = HRESULT_FROM_WIN32(ret);
1939                 else
1940                 {
1941                     hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1942                      hToken, SHGFP_TYPE_DEFAULT, path);
1943                     ret = RegSetValueExW(hKey,
1944                      CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
1945                      (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1946                     if (ret)
1947                         hr = HRESULT_FROM_WIN32(ret);
1948                 }
1949             }
1950         }
1951     }
1952     if (hUserKey)
1953         RegCloseKey(hUserKey);
1954     if (hKey)
1955         RegCloseKey(hKey);
1956
1957     TRACE("returning 0x%08lx\n", hr);
1958     return hr;
1959 }
1960
1961 static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
1962 {
1963     static const UINT folders[] = {
1964      CSIDL_PROGRAMS,
1965      CSIDL_PERSONAL,
1966      CSIDL_FAVORITES,
1967      CSIDL_APPDATA,
1968      CSIDL_STARTUP,
1969      CSIDL_RECENT,
1970      CSIDL_SENDTO,
1971      CSIDL_STARTMENU,
1972      CSIDL_MYMUSIC,
1973      CSIDL_MYVIDEO,
1974      CSIDL_DESKTOPDIRECTORY,
1975      CSIDL_NETHOOD,
1976      CSIDL_TEMPLATES,
1977      CSIDL_PRINTHOOD,
1978      CSIDL_LOCAL_APPDATA,
1979      CSIDL_INTERNET_CACHE,
1980      CSIDL_COOKIES,
1981      CSIDL_HISTORY,
1982      CSIDL_MYPICTURES
1983     };
1984     WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
1985     LPCWSTR pUserShellFolderPath, pShellFolderPath;
1986     HRESULT hr = S_OK;
1987     HKEY hRootKey;
1988     HANDLE hToken;
1989
1990     TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
1991     if (bDefault)
1992     {
1993         hToken = (HANDLE)-1;
1994         hRootKey = HKEY_USERS;
1995         strcpyW(userShellFolderPath, DefaultW);
1996         PathAddBackslashW(userShellFolderPath);
1997         strcatW(userShellFolderPath, szSHUserFolders);
1998         pUserShellFolderPath = userShellFolderPath;
1999         strcpyW(shellFolderPath, DefaultW);
2000         PathAddBackslashW(shellFolderPath);
2001         strcatW(shellFolderPath, szSHFolders);
2002         pShellFolderPath = shellFolderPath;
2003     }
2004     else
2005     {
2006         hToken = NULL;
2007         hRootKey = HKEY_CURRENT_USER;
2008         pUserShellFolderPath = szSHUserFolders;
2009         pShellFolderPath = szSHFolders;
2010     }
2011
2012     hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2013      pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2014     TRACE("returning 0x%08lx\n", hr);
2015     return hr;
2016 }
2017
2018 static HRESULT _SHRegisterCommonShellFolders(void)
2019 {
2020     static const UINT folders[] = {
2021      CSIDL_COMMON_STARTMENU,
2022      CSIDL_COMMON_PROGRAMS,
2023      CSIDL_COMMON_STARTUP,
2024      CSIDL_COMMON_DESKTOPDIRECTORY,
2025      CSIDL_COMMON_FAVORITES,
2026      CSIDL_COMMON_APPDATA,
2027      CSIDL_COMMON_TEMPLATES,
2028      CSIDL_COMMON_DOCUMENTS,
2029     };
2030     HRESULT hr;
2031
2032     TRACE("\n");
2033     hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2034      szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2035     TRACE("returning 0x%08lx\n", hr);
2036     return hr;
2037 }
2038
2039 /* Register the default values in the registry, as some apps seem to depend
2040  * on their presence.  The set registered was taken from Windows XP.
2041  */
2042 HRESULT SHELL_RegisterShellFolders(void)
2043 {
2044     HRESULT hr = _SHRegisterUserShellFolders(TRUE);
2045
2046     if (SUCCEEDED(hr))
2047         hr = _SHRegisterUserShellFolders(FALSE);
2048     if (SUCCEEDED(hr))
2049         hr = _SHRegisterCommonShellFolders();
2050     return hr;
2051 }
2052
2053 /*************************************************************************
2054  * SHGetSpecialFolderPathA [SHELL32.@]
2055  */
2056 BOOL WINAPI SHGetSpecialFolderPathA (
2057         HWND hwndOwner,
2058         LPSTR szPath,
2059         int nFolder,
2060         BOOL bCreate)
2061 {
2062         return (SHGetFolderPathA(
2063                 hwndOwner,
2064                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2065                 NULL,
2066                 0,
2067                 szPath)) == S_OK ? TRUE : FALSE;
2068 }
2069
2070 /*************************************************************************
2071  * SHGetSpecialFolderPathW
2072  */
2073 BOOL WINAPI SHGetSpecialFolderPathW (
2074         HWND hwndOwner,
2075         LPWSTR szPath,
2076         int nFolder,
2077         BOOL bCreate)
2078 {
2079         return (SHGetFolderPathW(
2080                 hwndOwner,
2081                 nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2082                 NULL,
2083                 0,
2084                 szPath)) == S_OK ? TRUE : FALSE;
2085 }
2086
2087 /*************************************************************************
2088  * SHGetSpecialFolderPath (SHELL32.175)
2089  */
2090 BOOL WINAPI SHGetSpecialFolderPathAW (
2091         HWND hwndOwner,
2092         LPVOID szPath,
2093         int nFolder,
2094         BOOL bCreate)
2095
2096 {
2097         if (SHELL_OsIsUnicode())
2098           return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
2099         return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
2100 }
2101
2102 /*************************************************************************
2103  * SHGetFolderLocation [SHELL32.@]
2104  *
2105  * Gets the folder locations from the registry and creates a pidl.
2106  *
2107  * PARAMS
2108  *   hwndOwner  [I]
2109  *   nFolder    [I] CSIDL_xxxxx
2110  *   hToken     [I] token representing user, or NULL for current user, or -1 for
2111  *                  default user
2112  *   dwReserved [I] must be zero
2113  *   ppidl      [O] PIDL of a special folder
2114  *
2115  * RETURNS
2116  *  Success: S_OK
2117  *  Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
2118  *
2119  * NOTES
2120  *  Creates missing reg keys and directories.
2121  *  Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
2122  *  virtual folders that are handled here.
2123  */
2124 HRESULT WINAPI SHGetFolderLocation(
2125         HWND hwndOwner,
2126         int nFolder,
2127         HANDLE hToken,
2128         DWORD dwReserved,
2129         LPITEMIDLIST *ppidl)
2130 {
2131     HRESULT hr = E_INVALIDARG;
2132
2133     TRACE("%p 0x%08x %p 0x%08lx %p\n",
2134      hwndOwner, nFolder, hToken, dwReserved, ppidl);
2135     
2136     if (!ppidl)
2137         return E_INVALIDARG;
2138     if (dwReserved)
2139         return E_INVALIDARG;
2140
2141     /* The virtual folders' locations are not user-dependent */
2142     *ppidl = NULL;
2143     switch (nFolder)
2144     {
2145         case CSIDL_DESKTOP:
2146             *ppidl = _ILCreateDesktop();
2147             break;
2148
2149         case CSIDL_INTERNET:
2150             *ppidl = _ILCreateIExplore();
2151             break;
2152
2153         case CSIDL_CONTROLS:
2154             *ppidl = _ILCreateControlPanel();
2155             break;
2156
2157         case CSIDL_PRINTERS:
2158             *ppidl = _ILCreatePrinters();
2159             break;
2160
2161         case CSIDL_BITBUCKET:
2162             *ppidl = _ILCreateBitBucket();
2163             break;
2164
2165         case CSIDL_DRIVES:
2166             *ppidl = _ILCreateMyComputer();
2167             break;
2168
2169         case CSIDL_NETWORK:
2170             *ppidl = _ILCreateNetwork();
2171             break;
2172
2173         default:
2174         {
2175             WCHAR szPath[MAX_PATH];
2176
2177             hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
2178              SHGFP_TYPE_CURRENT, szPath);
2179             if (SUCCEEDED(hr))
2180             {
2181                 DWORD attributes=0;
2182
2183                 TRACE("Value=%s\n", debugstr_w(szPath));
2184                 hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
2185             }
2186             else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
2187             {
2188                 /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2189                  * version 6.0 returns E_FAIL for nonexistent paths
2190                  */
2191                 hr = E_FAIL;
2192             }
2193         }
2194     }
2195     if(*ppidl)
2196         hr = NOERROR;
2197
2198     TRACE("-- (new pidl %p)\n",*ppidl);
2199     return hr;
2200 }
2201
2202 /*************************************************************************
2203  * SHGetSpecialFolderLocation           [SHELL32.@]
2204  *
2205  * NOTES
2206  *   In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
2207  *   directory.
2208  */
2209 HRESULT WINAPI SHGetSpecialFolderLocation(
2210         HWND hwndOwner,
2211         INT nFolder,
2212         LPITEMIDLIST * ppidl)
2213 {
2214     HRESULT hr = E_INVALIDARG;
2215
2216     TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
2217
2218     if (!ppidl)
2219         return E_INVALIDARG;
2220
2221     hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
2222     return hr;
2223 }