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