shellpath.c 74.8 KB
Newer Older
1 2 3
/*
 * Path Functions
 *
4
 * Copyright 1998, 1999, 2000 Juergen Schmied
5
 * Copyright 2004 Juan Lang
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 *
 * NOTES:
 *
 * Many of these functions are in SHLWAPI.DLL also
24 25
 *
 */
26 27 28 29

#include "config.h"
#include "wine/port.h"

30
#include <stdio.h>
31
#include <stdarg.h>
32 33
#include <string.h>
#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 40
#include "wingdi.h"
#include "winuser.h"
41 42

#include "shlobj.h"
43
#include "shresdef.h"
44
#include "shell32_main.h"
45
#include "undocshell.h"
Juan Lang's avatar
Juan Lang committed
46
#include "pidl.h"
47
#include "wine/unicode.h"
48
#include "shlwapi.h"
49
#include "xdg.h"
50
#include "sddl.h"
51

52
WINE_DEFAULT_DEBUG_CHANNEL(shell);
53

54
/*
Juergen Schmied's avatar
Juergen Schmied committed
55
	########## Combining and Constructing paths ##########
56
*/
Alexandre Julliard's avatar
Alexandre Julliard committed
57

58
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
59
 * PathAppend		[SHELL32.36]
60
 */
Juergen Schmied's avatar
Juergen Schmied committed
61
BOOL WINAPI PathAppendAW(
62 63 64
	LPVOID lpszPath1,
	LPCVOID lpszPath2)
{
65
	if (SHELL_OsIsUnicode())
66 67 68 69
	  return PathAppendW(lpszPath1, lpszPath2);
	return PathAppendA(lpszPath1, lpszPath2);
}

70
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
71
 * PathCombine	 [SHELL32.37]
72
 */
73 74 75
LPVOID WINAPI PathCombineAW(
	LPVOID szDest,
	LPCVOID lpszDir,
76
	LPCVOID lpszFile)
77
{
78
	if (SHELL_OsIsUnicode())
79 80
	  return PathCombineW( szDest, lpszDir, lpszFile );
	return PathCombineA( szDest, lpszDir, lpszFile );
81 82
}

83
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
84
 * PathAddBackslash		[SHELL32.32]
85 86 87
 */
LPVOID WINAPI PathAddBackslashAW(LPVOID lpszPath)
{
88
	if(SHELL_OsIsUnicode())
89 90 91 92 93
	  return PathAddBackslashW(lpszPath);
	return PathAddBackslashA(lpszPath);
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
94
 * PathBuildRoot		[SHELL32.30]
95 96 97
 */
LPVOID WINAPI PathBuildRootAW(LPVOID lpszPath, int drive)
{
98
	if(SHELL_OsIsUnicode())
99 100 101 102 103 104 105 106 107
	  return PathBuildRootW(lpszPath, drive);
	return PathBuildRootA(lpszPath, drive);
}

/*
	Extracting Component Parts
*/

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
108
 * PathFindFileName	[SHELL32.34]
109 110 111
 */
LPVOID WINAPI PathFindFileNameAW(LPCVOID lpszPath)
{
112
	if(SHELL_OsIsUnicode())
113 114 115 116
	  return PathFindFileNameW(lpszPath);
	return PathFindFileNameA(lpszPath);
}

117
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
118
 * PathFindExtension		[SHELL32.31]
119
 */
120
LPVOID WINAPI PathFindExtensionAW(LPCVOID lpszPath)
121
{
122
	if (SHELL_OsIsUnicode())
123 124 125 126 127
	  return PathFindExtensionW(lpszPath);
	return PathFindExtensionA(lpszPath);

}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*************************************************************************
 * PathGetExtensionA		[internal]
 *
 * NOTES
 *  exported by ordinal
 *  return value points to the first char after the dot
 */
static LPSTR PathGetExtensionA(LPCSTR lpszPath)
{
	TRACE("(%s)\n",lpszPath);

	lpszPath = PathFindExtensionA(lpszPath);
	return (LPSTR)(*lpszPath?(lpszPath+1):lpszPath);
}

/*************************************************************************
 * PathGetExtensionW		[internal]
 */
static LPWSTR PathGetExtensionW(LPCWSTR lpszPath)
{
	TRACE("(%s)\n",debugstr_w(lpszPath));

	lpszPath = PathFindExtensionW(lpszPath);
	return (LPWSTR)(*lpszPath?(lpszPath+1):lpszPath);
}

154
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
155
 * PathGetExtension		[SHELL32.158]
156
 */
157
LPVOID WINAPI PathGetExtensionAW(LPCVOID lpszPath,DWORD void1, DWORD void2)
158
{
159
	if (SHELL_OsIsUnicode())
160 161
	  return PathGetExtensionW(lpszPath);
	return PathGetExtensionA(lpszPath);
162
}
163 164

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
165
 * PathGetArgs	[SHELL32.52]
166
 */
167
LPVOID WINAPI PathGetArgsAW(LPVOID lpszPath)
168
{
169
	if (SHELL_OsIsUnicode())
170 171
	  return PathGetArgsW(lpszPath);
	return PathGetArgsA(lpszPath);
172 173
}

174
/*************************************************************************
175
 * PathGetDriveNumber	[SHELL32.57]
176
 */
177
int WINAPI PathGetDriveNumberAW(LPVOID lpszPath)
178
{
179
	if (SHELL_OsIsUnicode())
180 181
	  return PathGetDriveNumberW(lpszPath);
	return PathGetDriveNumberA(lpszPath);
182
}
183

184
/*************************************************************************
185
 * PathRemoveFileSpec [SHELL32.35]
186
 */
187
BOOL WINAPI PathRemoveFileSpecAW(LPVOID lpszPath)
188
{
189
	if (SHELL_OsIsUnicode())
190 191
	  return PathRemoveFileSpecW(lpszPath);
	return PathRemoveFileSpecA(lpszPath);
192
}
193 194

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
195
 * PathStripPath	[SHELL32.38]
196
 */
197
void WINAPI PathStripPathAW(LPVOID lpszPath)
198
{
199
	if (SHELL_OsIsUnicode())
200 201 202
            PathStripPathW(lpszPath);
        else
            PathStripPathA(lpszPath);
203
}
204 205

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
206
 * PathStripToRoot	[SHELL32.50]
207
 */
208
BOOL WINAPI PathStripToRootAW(LPVOID lpszPath)
209
{
210
	if (SHELL_OsIsUnicode())
211 212
	  return PathStripToRootW(lpszPath);
	return PathStripToRootA(lpszPath);
213
}
214

215
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
216
 * PathRemoveArgs	[SHELL32.251]
217
 */
218
void WINAPI PathRemoveArgsAW(LPVOID lpszPath)
219
{
220
	if (SHELL_OsIsUnicode())
221 222 223
            PathRemoveArgsW(lpszPath);
        else
            PathRemoveArgsA(lpszPath);
224
}
225 226

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
227
 * PathRemoveExtension	[SHELL32.250]
228
 */
229
void WINAPI PathRemoveExtensionAW(LPVOID lpszPath)
230
{
231
	if (SHELL_OsIsUnicode())
232 233 234
            PathRemoveExtensionW(lpszPath);
        else
            PathRemoveExtensionA(lpszPath);
235
}
236 237


238 239 240
/*
	Path Manipulations
*/
241 242

/*************************************************************************
243
 * PathGetShortPathA [internal]
244
 */
245
static void PathGetShortPathA(LPSTR pszPath)
246
{
247 248 249 250 251 252 253 254
	CHAR path[MAX_PATH];

	TRACE("%s\n", pszPath);

	if (GetShortPathNameA(pszPath, path, MAX_PATH))
	{
	  lstrcpyA(pszPath, path);
	}
255 256 257
}

/*************************************************************************
258
 * PathGetShortPathW [internal]
259
 */
260
static void PathGetShortPathW(LPWSTR pszPath)
261
{
262 263 264 265 266 267 268 269
	WCHAR path[MAX_PATH];

	TRACE("%s\n", debugstr_w(pszPath));

	if (GetShortPathNameW(pszPath, path, MAX_PATH))
	{
	  lstrcpyW(pszPath, path);
	}
270
}
271 272

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
273
 * PathGetShortPath [SHELL32.92]
274
 */
275
VOID WINAPI PathGetShortPathAW(LPVOID pszPath)
276
{
277
	if(SHELL_OsIsUnicode())
278 279
	  PathGetShortPathW(pszPath);
	PathGetShortPathA(pszPath);
280
}
281 282

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
283
 * PathRemoveBlanks [SHELL32.33]
284
 */
285
void WINAPI PathRemoveBlanksAW(LPVOID str)
Juergen Schmied's avatar
Juergen Schmied committed
286
{
287
	if(SHELL_OsIsUnicode())
288 289 290
            PathRemoveBlanksW(str);
        else
            PathRemoveBlanksA(str);
291 292
}

Juergen Schmied's avatar
Juergen Schmied committed
293
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
294
 * PathQuoteSpaces [SHELL32.55]
Juergen Schmied's avatar
Juergen Schmied committed
295
 */
296
VOID WINAPI PathQuoteSpacesAW (LPVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
297
{
298
	if(SHELL_OsIsUnicode())
299 300 301
            PathQuoteSpacesW(lpszPath);
        else
            PathQuoteSpacesA(lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
302 303 304
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
305
 * PathUnquoteSpaces [SHELL32.56]
Juergen Schmied's avatar
Juergen Schmied committed
306
 */
307
VOID WINAPI PathUnquoteSpacesAW(LPVOID str)
Juergen Schmied's avatar
Juergen Schmied committed
308
{
309
	if(SHELL_OsIsUnicode())
310 311 312
	  PathUnquoteSpacesW(str);
	else
	  PathUnquoteSpacesA(str);
Juergen Schmied's avatar
Juergen Schmied committed
313 314 315
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
316
 * PathParseIconLocation	[SHELL32.249]
Juergen Schmied's avatar
Juergen Schmied committed
317
 */
318
int WINAPI PathParseIconLocationAW (LPVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
319
{
320
	if(SHELL_OsIsUnicode())
321 322
	  return PathParseIconLocationW(lpszPath);
	return PathParseIconLocationA(lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
323 324
}

325 326 327
/*
	########## Path Testing ##########
*/
Juergen Schmied's avatar
Juergen Schmied committed
328
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
329
 * PathIsUNC		[SHELL32.39]
Juergen Schmied's avatar
Juergen Schmied committed
330
 */
331
BOOL WINAPI PathIsUNCAW (LPCVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
332
{
333
	if (SHELL_OsIsUnicode())
334
	  return PathIsUNCW( lpszPath );
335
	return PathIsUNCA( lpszPath );
Juergen Schmied's avatar
Juergen Schmied committed
336 337 338
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
339
 *  PathIsRelative	[SHELL32.40]
Juergen Schmied's avatar
Juergen Schmied committed
340
 */
341
BOOL WINAPI PathIsRelativeAW (LPCVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
342
{
343
	if (SHELL_OsIsUnicode())
344
	  return PathIsRelativeW( lpszPath );
345
	return PathIsRelativeA( lpszPath );
Juergen Schmied's avatar
Juergen Schmied committed
346 347 348
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
349
 * PathIsRoot		[SHELL32.29]
Juergen Schmied's avatar
Juergen Schmied committed
350
 */
351
BOOL WINAPI PathIsRootAW(LPCVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
352
{
353
	if (SHELL_OsIsUnicode())
354 355
	  return PathIsRootW(lpszPath);
	return PathIsRootA(lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
356 357
}

358 359 360 361 362 363
/*************************************************************************
 *  PathIsExeA		[internal]
 */
static BOOL PathIsExeA (LPCSTR lpszPath)
{
	LPCSTR lpszExtension = PathGetExtensionA(lpszPath);
364 365 366
        int i;
        static const char * const lpszExtensions[] =
            {"exe", "com", "pif", "cmd", "bat", "scf", "scr", NULL };
367

368 369 370
	TRACE("path=%s\n",lpszPath);

	for(i=0; lpszExtensions[i]; i++)
371
	  if (!lstrcmpiA(lpszExtension,lpszExtensions[i])) return TRUE;
372

373 374 375 376 377 378 379 380 381
	return FALSE;
}

/*************************************************************************
 *  PathIsExeW		[internal]
 */
static BOOL PathIsExeW (LPCWSTR lpszPath)
{
	LPCWSTR lpszExtension = PathGetExtensionW(lpszPath);
382 383 384 385 386
        int i;
        static const WCHAR lpszExtensions[][4] =
            {{'e','x','e','\0'}, {'c','o','m','\0'}, {'p','i','f','\0'},
             {'c','m','d','\0'}, {'b','a','t','\0'}, {'s','c','f','\0'},
             {'s','c','r','\0'}, {'\0'} };
387

388 389
	TRACE("path=%s\n",debugstr_w(lpszPath));

390
	for(i=0; lpszExtensions[i][0]; i++)
391
	  if (!strcmpiW(lpszExtension,lpszExtensions[i])) return TRUE;
392

393 394 395
	return FALSE;
}

Juergen Schmied's avatar
Juergen Schmied committed
396
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
397
 *  PathIsExe		[SHELL32.43]
Juergen Schmied's avatar
Juergen Schmied committed
398
 */
399
BOOL WINAPI PathIsExeAW (LPCVOID path)
Juergen Schmied's avatar
Juergen Schmied committed
400
{
401
	if (SHELL_OsIsUnicode())
402 403
	  return PathIsExeW (path);
	return PathIsExeA(path);
Juergen Schmied's avatar
Juergen Schmied committed
404 405 406
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
407
 * PathIsDirectory	[SHELL32.159]
Juergen Schmied's avatar
Juergen Schmied committed
408
 */
409
BOOL WINAPI PathIsDirectoryAW (LPCVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
410
{
411
	if (SHELL_OsIsUnicode())
412 413
	  return PathIsDirectoryW (lpszPath);
	return PathIsDirectoryA (lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
414 415 416
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
417
 * PathFileExists	[SHELL32.45]
418
 */
419
BOOL WINAPI PathFileExistsAW (LPCVOID lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
420
{
421
	if (SHELL_OsIsUnicode())
422 423
	  return PathFileExistsW (lpszPath);
	return PathFileExistsA (lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
424 425 426
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
427
 * PathMatchSpec	[SHELL32.46]
Juergen Schmied's avatar
Juergen Schmied committed
428
 */
429
BOOL WINAPI PathMatchSpecAW(LPVOID name, LPVOID mask)
Juergen Schmied's avatar
Juergen Schmied committed
430
{
431
	if (SHELL_OsIsUnicode())
432 433
	  return PathMatchSpecW( name, mask );
	return PathMatchSpecA( name, mask );
Juergen Schmied's avatar
Juergen Schmied committed
434 435 436
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
437
 * PathIsSameRoot	[SHELL32.650]
Juergen Schmied's avatar
Juergen Schmied committed
438
 */
439
BOOL WINAPI PathIsSameRootAW(LPCVOID lpszPath1, LPCVOID lpszPath2)
Juergen Schmied's avatar
Juergen Schmied committed
440
{
441
	if (SHELL_OsIsUnicode())
442 443
	  return PathIsSameRootW(lpszPath1, lpszPath2);
	return PathIsSameRootA(lpszPath1, lpszPath2);
Juergen Schmied's avatar
Juergen Schmied committed
444 445 446
}

/*************************************************************************
447
 * IsLFNDriveA		[SHELL32.41]
Juergen Schmied's avatar
Juergen Schmied committed
448
 */
449
BOOL WINAPI IsLFNDriveA(LPCSTR lpszPath)
Juergen Schmied's avatar
Juergen Schmied committed
450
{
451 452
    DWORD	fnlen;

453 454 455 456 457 458 459 460 461 462 463 464 465
    if (!GetVolumeInformationA(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
	return FALSE;
    return fnlen > 12;
}

/*************************************************************************
 * IsLFNDriveW		[SHELL32.42]
 */
BOOL WINAPI IsLFNDriveW(LPCWSTR lpszPath)
{
    DWORD	fnlen;

    if (!GetVolumeInformationW(lpszPath, NULL, 0, NULL, &fnlen, NULL, NULL, 0))
Juergen Schmied's avatar
Juergen Schmied committed
466
	return FALSE;
467
    return fnlen > 12;
Juergen Schmied's avatar
Juergen Schmied committed
468 469
}

470 471 472 473 474 475 476 477 478 479
/*************************************************************************
 * IsLFNDrive		[SHELL32.119]
 */
BOOL WINAPI IsLFNDriveAW(LPCVOID lpszPath)
{
	if (SHELL_OsIsUnicode())
	  return IsLFNDriveW(lpszPath);
	return IsLFNDriveA(lpszPath);
}

480
/*
Juergen Schmied's avatar
Juergen Schmied committed
481
	########## Creating Something Unique ##########
482
*/
483
/*************************************************************************
484
 * PathMakeUniqueNameA	[internal]
485
 */
486
static BOOL PathMakeUniqueNameA(
487
	LPSTR lpszBuffer,
488
	DWORD dwBuffSize,
489 490 491 492
	LPCSTR lpszShortName,
	LPCSTR lpszLongName,
	LPCSTR lpszPathName)
{
493
	FIXME("%p %u %s %s %s stub\n",
494 495 496
	 lpszBuffer, dwBuffSize, debugstr_a(lpszShortName),
	 debugstr_a(lpszLongName), debugstr_a(lpszPathName));
	return TRUE;
497 498
}

499
/*************************************************************************
500
 * PathMakeUniqueNameW	[internal]
501
 */
502
static BOOL PathMakeUniqueNameW(
503
	LPWSTR lpszBuffer,
504
	DWORD dwBuffSize,
505 506 507 508
	LPCWSTR lpszShortName,
	LPCWSTR lpszLongName,
	LPCWSTR lpszPathName)
{
509
	FIXME("%p %u %s %s %s stub\n",
510 511 512
	 lpszBuffer, dwBuffSize, debugstr_w(lpszShortName),
	 debugstr_w(lpszLongName), debugstr_w(lpszPathName));
	return TRUE;
513 514 515
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
516
 * PathMakeUniqueName	[SHELL32.47]
517
 */
518 519
BOOL WINAPI PathMakeUniqueNameAW(
	LPVOID lpszBuffer,
520
	DWORD dwBuffSize,
521 522 523
	LPCVOID lpszShortName,
	LPCVOID lpszLongName,
	LPCVOID lpszPathName)
524
{
525
	if (SHELL_OsIsUnicode())
526 527
	  return PathMakeUniqueNameW(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
	return PathMakeUniqueNameA(lpszBuffer,dwBuffSize, lpszShortName,lpszLongName,lpszPathName);
528 529
}

530
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
531
 * PathYetAnotherMakeUniqueName [SHELL32.75]
532
 *
533 534 535
 * NOTES
 *     exported by ordinal
 */
536 537 538 539 540
BOOL WINAPI PathYetAnotherMakeUniqueName(
	LPWSTR lpszBuffer,
	LPCWSTR lpszPathName,
	LPCWSTR lpszShortName,
	LPCWSTR lpszLongName)
541
{
542 543
    FIXME("(%p, %s, %s ,%s):stub.\n",
          lpszBuffer, debugstr_w(lpszPathName), debugstr_w(lpszShortName), debugstr_w(lpszLongName));
544 545 546 547
    return TRUE;
}


548
/*
Juergen Schmied's avatar
Juergen Schmied committed
549
	########## cleaning and resolving paths ##########
550
 */
551 552

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
553
 * PathFindOnPath	[SHELL32.145]
554
 */
555
BOOL WINAPI PathFindOnPathAW(LPVOID sFile, LPCVOID *sOtherDirs)
556
{
557
	if (SHELL_OsIsUnicode())
558 559
	  return PathFindOnPathW(sFile, (LPCWSTR *)sOtherDirs);
	return PathFindOnPathA(sFile, (LPCSTR *)sOtherDirs);
560 561
}

562
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
563
 * PathCleanupSpec	[SHELL32.171]
564 565
 *
 * lpszFile is changed in place.
566
 */
567
int WINAPI PathCleanupSpec( LPCWSTR lpszPathW, LPWSTR lpszFileW )
568
{
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    int i = 0;
    DWORD rc = 0;
    int length = 0;

    if (SHELL_OsIsUnicode())
    {
        LPWSTR p = lpszFileW;

        TRACE("Cleanup %s\n",debugstr_w(lpszFileW));

        if (lpszPathW)
            length = strlenW(lpszPathW);

        while (*p)
        {
            int gct = PathGetCharTypeW(*p);
            if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
            {
                lpszFileW[i]='-';
                rc |= PCS_REPLACEDCHAR;
            }
            else
                lpszFileW[i]=*p;
            i++;
            p++;
            if (length + i == MAX_PATH)
            {
                rc |= PCS_FATAL | PCS_PATHTOOLONG;
                break;
            }
        }
        lpszFileW[i]=0;
    }
    else
    {
        LPSTR lpszFileA = (LPSTR)lpszFileW;
605
        LPCSTR lpszPathA = (LPCSTR)lpszPathW;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
        LPSTR p = lpszFileA;

        TRACE("Cleanup %s\n",debugstr_a(lpszFileA));

        if (lpszPathA)
            length = strlen(lpszPathA);

        while (*p)
        {
            int gct = PathGetCharTypeA(*p);
            if (gct == GCT_INVALID || gct == GCT_WILD || gct == GCT_SEPARATOR)
            {
                lpszFileA[i]='-';
                rc |= PCS_REPLACEDCHAR;
            }
            else
                lpszFileA[i]=*p;
            i++;
            p++;
            if (length + i == MAX_PATH)
            {
                rc |= PCS_FATAL | PCS_PATHTOOLONG;
                break;
            }
        }
        lpszFileA[i]=0;
    }
    return rc;
634 635
}

636
/*************************************************************************
637
 * PathQualifyA		[SHELL32]
638
 */
639
static BOOL PathQualifyA(LPCSTR pszPath)
640 641
{
	FIXME("%s\n",pszPath);
642 643 644 645
	return 0;
}

/*************************************************************************
646 647
 * PathQualifyW		[SHELL32]
 */
648
static BOOL PathQualifyW(LPCWSTR pszPath)
649 650 651 652 653 654
{
	FIXME("%s\n",debugstr_w(pszPath));
	return 0;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
655
 * PathQualify	[SHELL32.49]
656
 */
657
BOOL WINAPI PathQualifyAW(LPCVOID pszPath)
658
{
659
	if (SHELL_OsIsUnicode())
660 661 662 663
	  return PathQualifyW(pszPath);
	return PathQualifyA(pszPath);
}

664
static BOOL PathResolveA(
665
	LPSTR lpszPath,
666
	LPCSTR *alpszPaths,
667 668
	DWORD dwFlags)
{
669
	FIXME("(%s,%p,0x%08x),stub!\n",
670 671 672 673
	  lpszPath, *alpszPaths, dwFlags);
	return 0;
}

674
static BOOL PathResolveW(
675
	LPWSTR lpszPath,
676
	LPCWSTR *alpszPaths,
677 678
	DWORD dwFlags)
{
679
	FIXME("(%s,%p,0x%08x),stub!\n",
680
	  debugstr_w(lpszPath), debugstr_w(*alpszPaths), dwFlags);
681 682 683 684
	return 0;
}

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
685
 * PathResolve [SHELL32.51]
686 687 688
 */
BOOL WINAPI PathResolveAW(
	LPVOID lpszPath,
689
	LPCVOID *alpszPaths,
690 691
	DWORD dwFlags)
{
692
	if (SHELL_OsIsUnicode())
693 694 695 696 697
	  return PathResolveW(lpszPath, (LPCWSTR*)alpszPaths, dwFlags);
	return PathResolveA(lpszPath, (LPCSTR*)alpszPaths, dwFlags);
}

/*************************************************************************
698
*	PathProcessCommandA
699
*/
700
static LONG PathProcessCommandA (
701 702 703 704
	LPCSTR lpszPath,
	LPSTR lpszBuff,
	DWORD dwBuffSize,
	DWORD dwFlags)
Juergen Schmied's avatar
Juergen Schmied committed
705
{
706
	FIXME("%s %p 0x%04x 0x%04x stub\n",
707
	lpszPath, lpszBuff, dwBuffSize, dwFlags);
708 709 710
	if(!lpszPath) return -1;
	if(lpszBuff) strcpy(lpszBuff, lpszPath);
	return strlen(lpszPath);
Juergen Schmied's avatar
Juergen Schmied committed
711 712
}

713 714 715
/*************************************************************************
*	PathProcessCommandW
*/
716
static LONG PathProcessCommandW (
717 718 719 720
	LPCWSTR lpszPath,
	LPWSTR lpszBuff,
	DWORD dwBuffSize,
	DWORD dwFlags)
Juergen Schmied's avatar
Juergen Schmied committed
721
{
722
	FIXME("(%s, %p, 0x%04x, 0x%04x) stub\n",
723
	debugstr_w(lpszPath), lpszBuff, dwBuffSize, dwFlags);
724 725 726
	if(!lpszPath) return -1;
	if(lpszBuff) strcpyW(lpszBuff, lpszPath);
	return strlenW(lpszPath);
727
}
728

729
/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
730
*	PathProcessCommand (SHELL32.653)
731
*/
732
LONG WINAPI PathProcessCommandAW (
733 734 735 736
	LPCVOID lpszPath,
	LPVOID lpszBuff,
	DWORD dwBuffSize,
	DWORD dwFlags)
Juergen Schmied's avatar
Juergen Schmied committed
737
{
738
	if (SHELL_OsIsUnicode())
739 740 741 742 743
	  return PathProcessCommandW(lpszPath, lpszBuff, dwBuffSize, dwFlags);
	return PathProcessCommandA(lpszPath, lpszBuff, dwBuffSize, dwFlags);
}

/*
Juergen Schmied's avatar
Juergen Schmied committed
744
	########## special ##########
745 746 747
*/

/*************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
748
 * PathSetDlgItemPath (SHELL32.48)
749
 */
750
VOID WINAPI PathSetDlgItemPathAW(HWND hDlg, int id, LPCVOID pszPath)
751 752 753 754 755
{
	if (SHELL_OsIsUnicode())
            PathSetDlgItemPathW(hDlg, id, pszPath);
        else
            PathSetDlgItemPathA(hDlg, id, pszPath);
Juergen Schmied's avatar
Juergen Schmied committed
756 757
}

758
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'};
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
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'};
static const WCHAR AppDataW[] = {'A','p','p','D','a','t','a','\0'};
static const WCHAR CacheW[] = {'C','a','c','h','e','\0'};
static const WCHAR CD_BurningW[] = {'C','D',' ','B','u','r','n','i','n','g','\0'};
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'};
static const WCHAR Common_AppDataW[] = {'C','o','m','m','o','n',' ','A','p','p','D','a','t','a','\0'};
static const WCHAR Common_DesktopW[] = {'C','o','m','m','o','n',' ','D','e','s','k','t','o','p','\0'};
static const WCHAR Common_DocumentsW[] = {'C','o','m','m','o','n',' ','D','o','c','u','m','e','n','t','s','\0'};
static const WCHAR CommonFilesDirW[] = {'C','o','m','m','o','n','F','i','l','e','s','D','i','r','\0'};
static const WCHAR CommonMusicW[] = {'C','o','m','m','o','n','M','u','s','i','c','\0'};
static const WCHAR CommonPicturesW[] = {'C','o','m','m','o','n','P','i','c','t','u','r','e','s','\0'};
static const WCHAR Common_ProgramsW[] = {'C','o','m','m','o','n',' ','P','r','o','g','r','a','m','s','\0'};
static const WCHAR Common_StartUpW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t','U','p','\0'};
static const WCHAR Common_Start_MenuW[] = {'C','o','m','m','o','n',' ','S','t','a','r','t',' ','M','e','n','u','\0'};
static const WCHAR Common_TemplatesW[] = {'C','o','m','m','o','n',' ','T','e','m','p','l','a','t','e','s','\0'};
static const WCHAR CommonVideoW[] = {'C','o','m','m','o','n','V','i','d','e','o','\0'};
static const WCHAR CookiesW[] = {'C','o','o','k','i','e','s','\0'};
static const WCHAR DesktopW[] = {'D','e','s','k','t','o','p','\0'};
static const WCHAR FavoritesW[] = {'F','a','v','o','r','i','t','e','s','\0'};
static const WCHAR FontsW[] = {'F','o','n','t','s','\0'};
static const WCHAR HistoryW[] = {'H','i','s','t','o','r','y','\0'};
static const WCHAR Local_AppDataW[] = {'L','o','c','a','l',' ','A','p','p','D','a','t','a','\0'};
static const WCHAR My_MusicW[] = {'M','y',' ','M','u','s','i','c','\0'};
static const WCHAR My_PicturesW[] = {'M','y',' ','P','i','c','t','u','r','e','s','\0'};
783
static const WCHAR My_VideoW[] = {'M','y',' ','V','i','d','e','o','s','\0'};
784 785 786 787
static const WCHAR NetHoodW[] = {'N','e','t','H','o','o','d','\0'};
static const WCHAR PersonalW[] = {'P','e','r','s','o','n','a','l','\0'};
static const WCHAR PrintHoodW[] = {'P','r','i','n','t','H','o','o','d','\0'};
static const WCHAR ProgramFilesDirW[] = {'P','r','o','g','r','a','m','F','i','l','e','s','D','i','r','\0'};
788
static const WCHAR ProgramsW[] = {'P','r','o','g','r','a','m','s','\0'};
789 790 791 792 793 794
static const WCHAR RecentW[] = {'R','e','c','e','n','t','\0'};
static const WCHAR ResourcesW[] = {'R','e','s','o','u','r','c','e','s','\0'};
static const WCHAR SendToW[] = {'S','e','n','d','T','o','\0'};
static const WCHAR StartUpW[] = {'S','t','a','r','t','U','p','\0'};
static const WCHAR Start_MenuW[] = {'S','t','a','r','t',' ','M','e','n','u','\0'};
static const WCHAR TemplatesW[] = {'T','e','m','p','l','a','t','e','s','\0'};
795 796 797 798 799 800 801 802 803
static const WCHAR DefaultW[] = {'.','D','e','f','a','u','l','t','\0'};
static const WCHAR AllUsersProfileW[] = {'%','A','L','L','U','S','E','R','S','P','R','O','F','I','L','E','%','\0'};
static const WCHAR UserProfileW[] = {'%','U','S','E','R','P','R','O','F','I','L','E','%','\0'};
static const WCHAR SystemDriveW[] = {'%','S','y','s','t','e','m','D','r','i','v','e','%','\0'};
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};
static const WCHAR ProfilesDirectoryW[] = {'P','r','o','f','i','l','e','s','D','i','r','e','c','t','o','r','y',0};
static const WCHAR AllUsersProfileValueW[] = {'A','l','l','U','s','e','r','s','P','r','o','f','i','l','e','\0'};
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'};
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'};
804 805
static const WCHAR szDefaultProfileDirW[] = {'u','s','e','r','s',0};
static const WCHAR AllUsersW[] = {'P','u','b','l','i','c',0};
806 807 808 809 810 811 812 813 814

typedef enum _CSIDL_Type {
    CSIDL_Type_User,
    CSIDL_Type_AllUsers,
    CSIDL_Type_CurrVer,
    CSIDL_Type_Disallowed,
    CSIDL_Type_NonExistent,
    CSIDL_Type_WindowsPath,
    CSIDL_Type_SystemPath,
815
    CSIDL_Type_SystemX86Path,
816
} CSIDL_Type;
817

818 819
typedef struct
{
820 821 822
    CSIDL_Type type;
    LPCWSTR    szValueName;
    LPCWSTR    szDefaultPath; /* fallback string or resource ID */
823 824 825 826
} CSIDL_DATA;

static const CSIDL_DATA CSIDL_Data[] =
{
827 828 829 830
    { /* 0x00 - CSIDL_DESKTOP */
        CSIDL_Type_User,
        DesktopW,
        MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
831
    },
832 833 834 835
    { /* 0x01 - CSIDL_INTERNET */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
836
    },
837 838 839 840
    { /* 0x02 - CSIDL_PROGRAMS */
        CSIDL_Type_User,
        ProgramsW,
        MAKEINTRESOURCEW(IDS_PROGRAMS)
841
    },
842 843 844 845
    { /* 0x03 - CSIDL_CONTROLS (.CPL files) */
        CSIDL_Type_SystemPath,
        NULL,
        NULL
846
    },
847 848 849 850
    { /* 0x04 - CSIDL_PRINTERS */
        CSIDL_Type_SystemPath,
        NULL,
        NULL
851
    },
852 853 854 855
    { /* 0x05 - CSIDL_PERSONAL */
        CSIDL_Type_User,
        PersonalW,
        MAKEINTRESOURCEW(IDS_PERSONAL)
856
    },
857 858 859 860
    { /* 0x06 - CSIDL_FAVORITES */
        CSIDL_Type_User,
        FavoritesW,
        MAKEINTRESOURCEW(IDS_FAVORITES)
861
    },
862 863 864 865
    { /* 0x07 - CSIDL_STARTUP */
        CSIDL_Type_User,
        StartUpW,
        MAKEINTRESOURCEW(IDS_STARTUP)
866
    },
867 868 869 870
    { /* 0x08 - CSIDL_RECENT */
        CSIDL_Type_User,
        RecentW,
        MAKEINTRESOURCEW(IDS_RECENT)
871
    },
872 873 874 875
    { /* 0x09 - CSIDL_SENDTO */
        CSIDL_Type_User,
        SendToW,
        MAKEINTRESOURCEW(IDS_SENDTO)
876
    },
877 878 879 880
    { /* 0x0a - CSIDL_BITBUCKET - Recycle Bin */
        CSIDL_Type_Disallowed,
        NULL,
        NULL,
881
    },
882 883 884 885
    { /* 0x0b - CSIDL_STARTMENU */
        CSIDL_Type_User,
        Start_MenuW,
        MAKEINTRESOURCEW(IDS_STARTMENU)
886
    },
887 888 889 890
    { /* 0x0c - CSIDL_MYDOCUMENTS */
        CSIDL_Type_Disallowed, /* matches WinXP--can't get its path */
        NULL,
        NULL
891
    },
892 893 894 895
    { /* 0x0d - CSIDL_MYMUSIC */
        CSIDL_Type_User,
        My_MusicW,
        MAKEINTRESOURCEW(IDS_MYMUSIC)
896
    },
897 898 899 900
    { /* 0x0e - CSIDL_MYVIDEO */
        CSIDL_Type_User,
        My_VideoW,
        MAKEINTRESOURCEW(IDS_MYVIDEO)
901
    },
902 903 904 905
    { /* 0x0f - unassigned */
        CSIDL_Type_Disallowed,
        NULL,
        NULL,
906
    },
907 908 909 910
    { /* 0x10 - CSIDL_DESKTOPDIRECTORY */
        CSIDL_Type_User,
        DesktopW,
        MAKEINTRESOURCEW(IDS_DESKTOPDIRECTORY)
911
    },
912 913 914 915
    { /* 0x11 - CSIDL_DRIVES */
        CSIDL_Type_Disallowed,
        NULL,
        NULL,
916
    },
917 918 919 920
    { /* 0x12 - CSIDL_NETWORK */
        CSIDL_Type_Disallowed,
        NULL,
        NULL,
921
    },
922 923 924 925
    { /* 0x13 - CSIDL_NETHOOD */
        CSIDL_Type_User,
        NetHoodW,
        MAKEINTRESOURCEW(IDS_NETHOOD)
926
    },
927 928
    { /* 0x14 - CSIDL_FONTS */
        CSIDL_Type_WindowsPath,
929
        FontsW,
930
        FontsW
931
    },
932 933 934 935
    { /* 0x15 - CSIDL_TEMPLATES */
        CSIDL_Type_User,
        TemplatesW,
        MAKEINTRESOURCEW(IDS_TEMPLATES)
936
    },
937 938 939 940
    { /* 0x16 - CSIDL_COMMON_STARTMENU */
        CSIDL_Type_AllUsers,
        Common_Start_MenuW,
        MAKEINTRESOURCEW(IDS_STARTMENU)
941
    },
942 943 944 945
    { /* 0x17 - CSIDL_COMMON_PROGRAMS */
        CSIDL_Type_AllUsers,
        Common_ProgramsW,
        MAKEINTRESOURCEW(IDS_PROGRAMS)
946
    },
947 948
    { /* 0x18 - CSIDL_COMMON_STARTUP */
        CSIDL_Type_AllUsers,
949
        Common_StartUpW,
950
        MAKEINTRESOURCEW(IDS_STARTUP)
951
    },
952 953 954 955
    { /* 0x19 - CSIDL_COMMON_DESKTOPDIRECTORY */
        CSIDL_Type_AllUsers,
        Common_DesktopW,
        MAKEINTRESOURCEW(IDS_DESKTOP)
956
    },
957 958 959 960
    { /* 0x1a - CSIDL_APPDATA */
        CSIDL_Type_User,
        AppDataW,
        MAKEINTRESOURCEW(IDS_APPDATA)
961
    },
962 963 964 965
    { /* 0x1b - CSIDL_PRINTHOOD */
        CSIDL_Type_User,
        PrintHoodW,
        MAKEINTRESOURCEW(IDS_PRINTHOOD)
966
    },
967 968 969 970
    { /* 0x1c - CSIDL_LOCAL_APPDATA */
        CSIDL_Type_User,
        Local_AppDataW,
        MAKEINTRESOURCEW(IDS_LOCAL_APPDATA)
971
    },
972 973 974 975
    { /* 0x1d - CSIDL_ALTSTARTUP */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
976
    },
977 978 979 980
    { /* 0x1e - CSIDL_COMMON_ALTSTARTUP */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
981
    },
982 983 984 985
    { /* 0x1f - CSIDL_COMMON_FAVORITES */
        CSIDL_Type_AllUsers,
        FavoritesW,
        MAKEINTRESOURCEW(IDS_FAVORITES)
986
    },
987 988 989 990
    { /* 0x20 - CSIDL_INTERNET_CACHE */
        CSIDL_Type_User,
        CacheW,
        MAKEINTRESOURCEW(IDS_INTERNET_CACHE)
991
    },
992 993 994 995
    { /* 0x21 - CSIDL_COOKIES */
        CSIDL_Type_User,
        CookiesW,
        MAKEINTRESOURCEW(IDS_COOKIES)
996
    },
997 998 999 1000
    { /* 0x22 - CSIDL_HISTORY */
        CSIDL_Type_User,
        HistoryW,
        MAKEINTRESOURCEW(IDS_HISTORY)
1001
    },
1002 1003 1004 1005
    { /* 0x23 - CSIDL_COMMON_APPDATA */
        CSIDL_Type_AllUsers,
        Common_AppDataW,
        MAKEINTRESOURCEW(IDS_APPDATA)
1006
    },
1007 1008 1009 1010
    { /* 0x24 - CSIDL_WINDOWS */
        CSIDL_Type_WindowsPath,
        NULL,
        NULL
1011
    },
1012 1013 1014 1015
    { /* 0x25 - CSIDL_SYSTEM */
        CSIDL_Type_SystemPath,
        NULL,
        NULL
1016
    },
1017 1018 1019 1020
    { /* 0x26 - CSIDL_PROGRAM_FILES */
        CSIDL_Type_CurrVer,
        ProgramFilesDirW,
        MAKEINTRESOURCEW(IDS_PROGRAM_FILES)
1021
    },
1022 1023 1024 1025
    { /* 0x27 - CSIDL_MYPICTURES */
        CSIDL_Type_User,
        My_PicturesW,
        MAKEINTRESOURCEW(IDS_MYPICTURES)
1026
    },
1027 1028 1029 1030
    { /* 0x28 - CSIDL_PROFILE */
        CSIDL_Type_User,
        NULL,
        NULL
1031
    },
1032
    { /* 0x29 - CSIDL_SYSTEMX86 */
1033
        CSIDL_Type_SystemX86Path,
1034 1035
        NULL,
        NULL
1036
    },
1037 1038 1039 1040
    { /* 0x2a - CSIDL_PROGRAM_FILESX86 */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
1041
    },
1042 1043 1044 1045
    { /* 0x2b - CSIDL_PROGRAM_FILES_COMMON */
        CSIDL_Type_CurrVer,
        CommonFilesDirW,
        MAKEINTRESOURCEW(IDS_PROGRAM_FILES_COMMON)
1046
    },
1047 1048 1049 1050
    { /* 0x2c - CSIDL_PROGRAM_FILES_COMMONX86 */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
1051
    },
1052 1053 1054 1055
    { /* 0x2d - CSIDL_COMMON_TEMPLATES */
        CSIDL_Type_AllUsers,
        Common_TemplatesW,
        MAKEINTRESOURCEW(IDS_TEMPLATES)
1056
    },
1057 1058 1059 1060
    { /* 0x2e - CSIDL_COMMON_DOCUMENTS */
        CSIDL_Type_AllUsers,
        Common_DocumentsW,
        MAKEINTRESOURCEW(IDS_COMMON_DOCUMENTS)
1061
    },
1062 1063 1064 1065
    { /* 0x2f - CSIDL_COMMON_ADMINTOOLS */
        CSIDL_Type_AllUsers,
        Common_Administrative_ToolsW,
        MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1066
    },
1067 1068 1069 1070
    { /* 0x30 - CSIDL_ADMINTOOLS */
        CSIDL_Type_User,
        Administrative_ToolsW,
        MAKEINTRESOURCEW(IDS_ADMINTOOLS)
1071
    },
1072 1073 1074 1075
    { /* 0x31 - CSIDL_CONNECTIONS */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
1076
    },
1077 1078 1079 1080
    { /* 0x32 - unassigned */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
1081
    },
1082 1083 1084 1085
    { /* 0x33 - unassigned */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
1086
    },
1087 1088 1089 1090
    { /* 0x34 - unassigned */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
1091
    },
1092 1093 1094 1095
    { /* 0x35 - CSIDL_COMMON_MUSIC */
        CSIDL_Type_AllUsers,
        CommonMusicW,
        MAKEINTRESOURCEW(IDS_COMMON_MUSIC)
1096
    },
1097 1098 1099 1100
    { /* 0x36 - CSIDL_COMMON_PICTURES */
        CSIDL_Type_AllUsers,
        CommonPicturesW,
        MAKEINTRESOURCEW(IDS_COMMON_PICTURES)
1101
    },
1102 1103 1104 1105
    { /* 0x37 - CSIDL_COMMON_VIDEO */
        CSIDL_Type_AllUsers,
        CommonVideoW,
        MAKEINTRESOURCEW(IDS_COMMON_VIDEO)
1106
    },
1107 1108 1109 1110
    { /* 0x38 - CSIDL_RESOURCES */
        CSIDL_Type_WindowsPath,
        NULL,
        ResourcesW
1111
    },
1112 1113 1114 1115
    { /* 0x39 - CSIDL_RESOURCES_LOCALIZED */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
1116
    },
1117 1118 1119 1120
    { /* 0x3a - CSIDL_COMMON_OEM_LINKS */
        CSIDL_Type_NonExistent,
        NULL,
        NULL
1121
    },
1122 1123 1124 1125
    { /* 0x3b - CSIDL_CDBURN_AREA */
        CSIDL_Type_User,
        CD_BurningW,
        MAKEINTRESOURCEW(IDS_CDBURN_AREA)
1126
    },
1127 1128 1129 1130
    { /* 0x3c unassigned */
        CSIDL_Type_Disallowed,
        NULL,
        NULL
1131
    },
1132 1133 1134 1135
    { /* 0x3d - CSIDL_COMPUTERSNEARME */
        CSIDL_Type_Disallowed, /* FIXME */
        NULL,
        NULL
1136
    },
1137 1138 1139 1140
    { /* 0x3e - CSIDL_PROFILES */
        CSIDL_Type_Disallowed, /* oddly, this matches WinXP */
        NULL,
        NULL
1141 1142 1143
    }
};

1144
static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest);
1145

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
/* Gets the value named value from the registry key
 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
 * (or from rootKey\userPrefix\... if userPrefix is not NULL) into path, which
 * is assumed to be MAX_PATH WCHARs in length.
 * If it exists, expands the value and writes the expanded value to
 * rootKey\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
 * Returns successful error code if the value was retrieved from the registry,
 * and a failure otherwise.
 */
static HRESULT _SHGetUserShellFolderPath(HKEY rootKey, LPCWSTR userPrefix,
 LPCWSTR value, LPWSTR path)
1157
{
1158 1159 1160
    HRESULT hr;
    WCHAR shellFolderPath[MAX_PATH], userShellFolderPath[MAX_PATH];
    LPCWSTR pShellFolderPath, pUserShellFolderPath;
1161
    DWORD dwType, dwPathLen = MAX_PATH;
1162
    HKEY userShellFolderKey, shellFolderKey;
1163

1164 1165
    TRACE("%p,%s,%s,%p\n",rootKey, debugstr_w(userPrefix), debugstr_w(value),
     path);
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    if (userPrefix)
    {
        strcpyW(shellFolderPath, userPrefix);
        PathAddBackslashW(shellFolderPath);
        strcatW(shellFolderPath, szSHFolders);
        pShellFolderPath = shellFolderPath;
        strcpyW(userShellFolderPath, userPrefix);
        PathAddBackslashW(userShellFolderPath);
        strcatW(userShellFolderPath, szSHUserFolders);
        pUserShellFolderPath = userShellFolderPath;
    }
    else
    {
        pUserShellFolderPath = szSHUserFolders;
        pShellFolderPath = szSHFolders;
    }
Juan Lang's avatar
Juan Lang committed
1183

1184
    if (RegCreateKeyW(rootKey, pShellFolderPath, &shellFolderKey))
1185 1186 1187 1188
    {
        TRACE("Failed to create %s\n", debugstr_w(pShellFolderPath));
        return E_FAIL;
    }
1189
    if (RegCreateKeyW(rootKey, pUserShellFolderPath, &userShellFolderKey))
1190 1191 1192 1193 1194 1195
    {
        TRACE("Failed to create %s\n",
         debugstr_w(pUserShellFolderPath));
        RegCloseKey(shellFolderKey);
        return E_FAIL;
    }
1196

1197 1198 1199
    if (!RegQueryValueExW(userShellFolderKey, value, NULL, &dwType,
     (LPBYTE)path, &dwPathLen) && (dwType == REG_EXPAND_SZ || dwType == REG_SZ))
    {
1200 1201
        LONG ret;

1202
        path[dwPathLen / sizeof(WCHAR)] = '\0';
1203
        if (dwType == REG_EXPAND_SZ && path[0] == '%')
1204 1205
        {
            WCHAR szTemp[MAX_PATH];
1206

1207
            _SHExpandEnvironmentStrings(path, szTemp);
1208
            lstrcpynW(path, szTemp, MAX_PATH);
1209
        }
1210
        ret = RegSetValueExW(shellFolderKey, value, 0, REG_SZ, (LPBYTE)path,
1211
         (strlenW(path) + 1) * sizeof(WCHAR));
1212 1213 1214 1215
        if (ret != ERROR_SUCCESS)
            hr = HRESULT_FROM_WIN32(ret);
        else
            hr = S_OK;
1216 1217 1218 1219 1220
    }
    else
        hr = E_FAIL;
    RegCloseKey(shellFolderKey);
    RegCloseKey(userShellFolderKey);
1221
    TRACE("returning 0x%08x\n", hr);
1222 1223
    return hr;
}
1224

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
/* Gets a 'semi-expanded' default value of the CSIDL with index folder into
 * pszPath, based on the entries in CSIDL_Data.  By semi-expanded, I mean:
 * - The entry's szDefaultPath may be either a string value or an integer
 *   resource identifier.  In the latter case, the string value of the resource
 *   is written.
 * - Depending on the entry's type, the path may begin with an (unexpanded)
 *   environment variable name.  The caller is responsible for expanding
 *   environment strings if so desired.
 *   The types that are prepended with environment variables are:
 *   CSIDL_Type_User:     %USERPROFILE%
 *   CSIDL_Type_AllUsers: %ALLUSERSPROFILE%
 *   CSIDL_Type_CurrVer:  %SystemDrive%
 *   (Others might make sense too, but as yet are unneeded.)
 */
static HRESULT _SHGetDefaultValue(BYTE folder, LPWSTR pszPath)
{
    HRESULT hr;
    WCHAR resourcePath[MAX_PATH];
1243
    LPCWSTR pDefaultPath = NULL;
Juergen Schmied's avatar
Juergen Schmied committed
1244

1245
    TRACE("0x%02x,%p\n", folder, pszPath);
Juergen Schmied's avatar
Juergen Schmied committed
1246

1247 1248 1249 1250
    if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
        return E_INVALIDARG;
    if (!pszPath)
        return E_INVALIDARG;
Juergen Schmied's avatar
Juergen Schmied committed
1251

1252 1253 1254 1255
    if (CSIDL_Data[folder].szDefaultPath &&
     IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath))
    {
        if (LoadStringW(shell32_hInstance,
Kevin Koltzau's avatar
Kevin Koltzau committed
1256
         LOWORD(CSIDL_Data[folder].szDefaultPath), resourcePath, MAX_PATH))
1257 1258 1259 1260 1261 1262
        {
            hr = S_OK;
            pDefaultPath = resourcePath;
        }
        else
        {
1263 1264
            FIXME("(%d,%s), LoadString failed, missing translation?\n", folder,
             debugstr_w(pszPath));
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
            hr = E_FAIL;
        }
    }
    else
    {
        hr = S_OK;
        pDefaultPath = CSIDL_Data[folder].szDefaultPath;
    }
    if (SUCCEEDED(hr))
    {
        switch (CSIDL_Data[folder].type)
        {
            case CSIDL_Type_User:
                strcpyW(pszPath, UserProfileW);
                break;
            case CSIDL_Type_AllUsers:
                strcpyW(pszPath, AllUsersProfileW);
                break;
            case CSIDL_Type_CurrVer:
                strcpyW(pszPath, SystemDriveW);
                break;
            default:
                ; /* no corresponding env. var, do nothing */
        }
        if (pDefaultPath)
        {
            PathAddBackslashW(pszPath);
            strcatW(pszPath, pDefaultPath);
        }
    }
1295
    TRACE("returning 0x%08x\n", hr);
1296 1297
    return hr;
}
1298

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
/* Gets the (unexpanded) value of the folder with index folder into pszPath.
 * The folder's type is assumed to be CSIDL_Type_CurrVer.  Its default value
 * can be overridden in the HKLM\\szCurrentVersion key.
 * If dwFlags has SHGFP_TYPE_DEFAULT set or if the value isn't overridden in
 * the registry, uses _SHGetDefaultValue to get the value.
 */
static HRESULT _SHGetCurrentVersionPath(DWORD dwFlags, BYTE folder,
 LPWSTR pszPath)
{
    HRESULT hr;

1310
    TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

    if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
        return E_INVALIDARG;
    if (CSIDL_Data[folder].type != CSIDL_Type_CurrVer)
        return E_INVALIDARG;
    if (!pszPath)
        return E_INVALIDARG;

    if (dwFlags & SHGFP_TYPE_DEFAULT)
        hr = _SHGetDefaultValue(folder, pszPath);
    else
    {
        HKEY hKey;

1325
        if (RegCreateKeyW(HKEY_LOCAL_MACHINE, szCurrentVersion, &hKey))
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
            hr = E_FAIL;
        else
        {
            DWORD dwType, dwPathLen = MAX_PATH * sizeof(WCHAR);

            if (RegQueryValueExW(hKey, CSIDL_Data[folder].szValueName, NULL,
             &dwType, (LPBYTE)pszPath, &dwPathLen) ||
             (dwType != REG_SZ && dwType != REG_EXPAND_SZ))
            {
                hr = _SHGetDefaultValue(folder, pszPath);
                dwType = REG_EXPAND_SZ;
                RegSetValueExW(hKey, CSIDL_Data[folder].szValueName, 0, dwType,
                 (LPBYTE)pszPath, (strlenW(pszPath)+1)*sizeof(WCHAR));
            }
            else
            {
                pszPath[dwPathLen / sizeof(WCHAR)] = '\0';
                hr = S_OK;
            }
            RegCloseKey(hKey);
        }
    }
1348
    TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1349 1350 1351
    return hr;
}

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
static LPWSTR _GetUserSidStringFromToken(HANDLE Token)
{
    char InfoBuffer[64];
    PTOKEN_USER UserInfo;
    DWORD InfoSize;
    LPWSTR SidStr;

    UserInfo = (PTOKEN_USER) InfoBuffer;
    if (! GetTokenInformation(Token, TokenUser, InfoBuffer, sizeof(InfoBuffer),
                              &InfoSize))
    {
        if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
            return NULL;
        UserInfo = HeapAlloc(GetProcessHeap(), 0, InfoSize);
        if (UserInfo == NULL)
            return NULL;
        if (! GetTokenInformation(Token, TokenUser, UserInfo, InfoSize,
                                  &InfoSize))
        {
            HeapFree(GetProcessHeap(), 0, UserInfo);
            return NULL;
        }
    }

    if (! ConvertSidToStringSidW(UserInfo->User.Sid, &SidStr))
        SidStr = NULL;

    if (UserInfo != (PTOKEN_USER) InfoBuffer)
        HeapFree(GetProcessHeap(), 0, UserInfo);

    return SidStr;
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
/* Gets the user's path (unexpanded) for the CSIDL with index folder:
 * If SHGFP_TYPE_DEFAULT is set, calls _SHGetDefaultValue for it.  Otherwise
 * calls _SHGetUserShellFolderPath for it.  Where it looks depends on hToken:
 * - if hToken is -1, looks in HKEY_USERS\.Default
 * - otherwise looks first in HKEY_CURRENT_USER, followed by HKEY_LOCAL_MACHINE
 *   if HKEY_CURRENT_USER doesn't contain any entries.  If both fail, finally
 *   calls _SHGetDefaultValue for it.
 */
static HRESULT _SHGetUserProfilePath(HANDLE hToken, DWORD dwFlags, BYTE folder,
 LPWSTR pszPath)
{
    HRESULT hr;

1398
    TRACE("%p,0x%08x,0x%02x,%p\n", hToken, dwFlags, folder, pszPath);
1399 1400 1401 1402 1403 1404 1405 1406 1407

    if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
        return E_INVALIDARG;
    if (CSIDL_Data[folder].type != CSIDL_Type_User)
        return E_INVALIDARG;
    if (!pszPath)
        return E_INVALIDARG;

    if (dwFlags & SHGFP_TYPE_DEFAULT)
1408 1409 1410 1411 1412 1413
    {
        if (hToken != NULL && hToken != (HANDLE)-1)
        {
            FIXME("unsupported for user other than current or default\n");
            return E_FAIL;
        }
1414
        hr = _SHGetDefaultValue(folder, pszPath);
1415
    }
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
    else
    {
        LPCWSTR userPrefix = NULL;
        HKEY hRootKey;

        if (hToken == (HANDLE)-1)
        {
            hRootKey = HKEY_USERS;
            userPrefix = DefaultW;
        }
1426
        else if (hToken == NULL)
1427
            hRootKey = HKEY_CURRENT_USER;
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
        else
        {
            hRootKey = HKEY_USERS;
            userPrefix = _GetUserSidStringFromToken(hToken);
            if (userPrefix == NULL)
            {
                hr = E_FAIL;
                goto error;
            }
        }
1438 1439 1440 1441 1442 1443 1444
        hr = _SHGetUserShellFolderPath(hRootKey, userPrefix,
         CSIDL_Data[folder].szValueName, pszPath);
        if (FAILED(hr) && hRootKey != HKEY_LOCAL_MACHINE)
            hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
             CSIDL_Data[folder].szValueName, pszPath);
        if (FAILED(hr))
            hr = _SHGetDefaultValue(folder, pszPath);
1445 1446
        if (userPrefix != NULL && userPrefix != DefaultW)
            LocalFree((HLOCAL) userPrefix);
1447
    }
1448
error:
1449
    TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
    return hr;
}

/* Gets the (unexpanded) path for the CSIDL with index folder.  If dwFlags has
 * SHGFP_TYPE_DEFAULT set, calls _SHGetDefaultValue.  Otherwise calls
 * _SHGetUserShellFolderPath for it, looking only in HKEY_LOCAL_MACHINE.
 * If this fails, falls back to _SHGetDefaultValue.
 */
static HRESULT _SHGetAllUsersProfilePath(DWORD dwFlags, BYTE folder,
 LPWSTR pszPath)
{
    HRESULT hr;

1463
    TRACE("0x%08x,0x%02x,%p\n", dwFlags, folder, pszPath);
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480

    if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
        return E_INVALIDARG;
    if (CSIDL_Data[folder].type != CSIDL_Type_AllUsers)
        return E_INVALIDARG;
    if (!pszPath)
        return E_INVALIDARG;

    if (dwFlags & SHGFP_TYPE_DEFAULT)
        hr = _SHGetDefaultValue(folder, pszPath);
    else
    {
        hr = _SHGetUserShellFolderPath(HKEY_LOCAL_MACHINE, NULL,
         CSIDL_Data[folder].szValueName, pszPath);
        if (FAILED(hr))
            hr = _SHGetDefaultValue(folder, pszPath);
    }
1481
    TRACE("returning 0x%08x (output path is %s)\n", hr, debugstr_w(pszPath));
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
    return hr;
}

static HRESULT _SHOpenProfilesKey(PHKEY pKey)
{
    LONG lRet;
    DWORD disp;

    lRet = RegCreateKeyExW(HKEY_LOCAL_MACHINE, ProfileListW, 0, NULL, 0,
     KEY_ALL_ACCESS, NULL, pKey, &disp);
    return HRESULT_FROM_WIN32(lRet);
}

/* Reads the value named szValueName from the key profilesKey (assumed to be
 * opened by _SHOpenProfilesKey) into szValue, which is assumed to be MAX_PATH
 * WCHARs in length.  If it doesn't exist, returns szDefault (and saves
 * szDefault to the registry).
 */
static HRESULT _SHGetProfilesValue(HKEY profilesKey, LPCWSTR szValueName,
 LPWSTR szValue, LPCWSTR szDefault)
{
    HRESULT hr;
    DWORD type, dwPathLen = MAX_PATH * sizeof(WCHAR);
    LONG lRet;

    TRACE("%p,%s,%p,%s\n", profilesKey, debugstr_w(szValueName), szValue,
     debugstr_w(szDefault));
    lRet = RegQueryValueExW(profilesKey, szValueName, NULL, &type,
     (LPBYTE)szValue, &dwPathLen);
    if (!lRet && (type == REG_SZ || type == REG_EXPAND_SZ) && dwPathLen
     && *szValue)
    {
        dwPathLen /= sizeof(WCHAR);
        szValue[dwPathLen] = '\0';
        hr = S_OK;
    }
    else
    {
        /* Missing or invalid value, set a default */
1521
        lstrcpynW(szValue, szDefault, MAX_PATH);
1522
        TRACE("Setting missing value %s to %s\n", debugstr_w(szValueName),
1523
                                                  debugstr_w(szValue));
1524
        lRet = RegSetValueExW(profilesKey, szValueName, 0, REG_EXPAND_SZ,
1525 1526
                              (LPBYTE)szValue,
                              (strlenW(szValue) + 1) * sizeof(WCHAR));
1527 1528 1529 1530 1531
        if (lRet)
            hr = HRESULT_FROM_WIN32(lRet);
        else
            hr = S_OK;
    }
1532
    TRACE("returning 0x%08x (output value is %s)\n", hr, debugstr_w(szValue));
1533 1534 1535
    return hr;
}

1536 1537 1538 1539 1540 1541
/* Attempts to expand environment variables from szSrc into szDest, which is
 * assumed to be MAX_PATH characters in length.  Before referring to the
 * environment, handles a few variables directly, because the environment
 * variables may not be set when this is called (as during Wine's installation
 * when default values are being written to the registry).
 * The directly handled environment variables, and their source, are:
1542 1543 1544
 * - ALLUSERSPROFILE, USERPROFILE: reads from the registry
 * - SystemDrive: uses GetSystemDirectoryW and uses the drive portion of its
 *   path
1545 1546
 * If one of the directly handled environment variables is expanded, only
 * expands a single variable, and only in the beginning of szSrc.
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
 */
static HRESULT _SHExpandEnvironmentStrings(LPCWSTR szSrc, LPWSTR szDest)
{
    HRESULT hr;
    WCHAR szTemp[MAX_PATH], szProfilesPrefix[MAX_PATH] = { 0 };
    HKEY key = NULL;

    TRACE("%s, %p\n", debugstr_w(szSrc), szDest);

    if (!szSrc || !szDest) return E_INVALIDARG;

1558 1559 1560 1561 1562 1563 1564
    /* short-circuit if there's nothing to expand */
    if (szSrc[0] != '%')
    {
        strcpyW(szDest, szSrc);
        hr = S_OK;
        goto end;
    }
1565 1566 1567 1568
    /* Get the profile prefix, we'll probably be needing it */
    hr = _SHOpenProfilesKey(&key);
    if (SUCCEEDED(hr))
    {
1569 1570 1571 1572 1573 1574
        WCHAR def_val[MAX_PATH];

        /* get the system drive */
        GetSystemDirectoryW(def_val, MAX_PATH);
        if (def_val[1] == ':') strcpyW( def_val + 3, szDefaultProfileDirW );
        else FIXME("non-drive system paths unsupported\n");
1575

1576
        hr = _SHGetProfilesValue(key, ProfilesDirectoryW, szProfilesPrefix, def_val );
1577 1578 1579 1580 1581 1582 1583 1584 1585
    }

    *szDest = 0;
    strcpyW(szTemp, szSrc);
    while (SUCCEEDED(hr) && szTemp[0] == '%')
    {
        if (!strncmpiW(szTemp, AllUsersProfileW, strlenW(AllUsersProfileW)))
        {
            WCHAR szAllUsers[MAX_PATH];
1586

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
            strcpyW(szDest, szProfilesPrefix);
            hr = _SHGetProfilesValue(key, AllUsersProfileValueW,
             szAllUsers, AllUsersW);
            PathAppendW(szDest, szAllUsers);
            PathAppendW(szDest, szTemp + strlenW(AllUsersProfileW));
        }
        else if (!strncmpiW(szTemp, UserProfileW, strlenW(UserProfileW)))
        {
            WCHAR userName[MAX_PATH];
            DWORD userLen = MAX_PATH;

            strcpyW(szDest, szProfilesPrefix);
            GetUserNameW(userName, &userLen);
            PathAppendW(szDest, userName);
            PathAppendW(szDest, szTemp + strlenW(UserProfileW));
        }
        else if (!strncmpiW(szTemp, SystemDriveW, strlenW(SystemDriveW)))
        {
            GetSystemDirectoryW(szDest, MAX_PATH);
            if (szDest[1] != ':')
            {
                FIXME("non-drive system paths unsupported\n");
                hr = E_FAIL;
            }
            else
            {
                strcpyW(szDest + 3, szTemp + strlenW(SystemDriveW) + 1);
                hr = S_OK;
            }
        }
        else
        {
1619 1620 1621 1622 1623 1624 1625 1626
            DWORD ret = ExpandEnvironmentStringsW(szSrc, szDest, MAX_PATH);

            if (ret > MAX_PATH)
                hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
            else if (ret == 0)
                hr = HRESULT_FROM_WIN32(GetLastError());
            else
                hr = S_OK;
1627 1628 1629 1630 1631 1632 1633 1634 1635
        }
        if (SUCCEEDED(hr) && szDest[0] == '%')
            strcpyW(szTemp, szDest);
        else
        {
            /* terminate loop */
            szTemp[0] = '\0';
        }
    }
1636
end:
1637 1638
    if (key)
        RegCloseKey(key);
1639
    TRACE("returning 0x%08x (input was %s, output is %s)\n", hr,
1640 1641 1642 1643 1644 1645 1646
     debugstr_w(szSrc), debugstr_w(szDest));
    return hr;
}

/*************************************************************************
 * SHGetFolderPathW			[SHELL32.@]
 *
1647 1648 1649 1650 1651 1652
 * Convert nFolder to path.  
 *
 * RETURNS
 *  Success: S_OK
 *  Failure: standard HRESULT error codes.
 *
1653
 * NOTES
1654
 * Most values can be overridden in either
1655 1656 1657 1658 1659 1660 1661
 * HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
 * or in the same location in HKLM.
 * The "Shell Folders" registry key was used in NT4 and earlier systems.
 * Beginning with Windows 2000, the "User Shell Folders" key is used, so
 * changes made to it are made to the former key too.  This synchronization is
 * done on-demand: not until someone requests the value of one of these paths
 * (by calling one of the SHGet functions) is the value synchronized.
1662
 * Furthermore, the HKCU paths take precedence over the HKLM paths.
1663
 */
1664
HRESULT WINAPI SHGetFolderPathW(
1665
	HWND hwndOwner,    /* [I] owner window */
1666
	int nFolder,       /* [I] CSIDL identifying the folder */
1667 1668 1669
	HANDLE hToken,     /* [I] access token */
	DWORD dwFlags,     /* [I] which path to return */
	LPWSTR pszPath)    /* [O] converted path */
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
{
    HRESULT hr =  SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, NULL, pszPath);
    if(HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
        hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
    return hr;
}

HRESULT WINAPI SHGetFolderPathAndSubDirA(
	HWND hwndOwner,    /* [I] owner window */
	int nFolder,       /* [I] CSIDL identifying the folder */
	HANDLE hToken,     /* [I] access token */
	DWORD dwFlags,     /* [I] which path to return */
	LPCSTR pszSubPath, /* [I] sub directory of the specified folder */
	LPSTR pszPath)     /* [O] converted path */
{
    int length;
    HRESULT hr = S_OK;
    LPWSTR pszSubPathW = NULL;
    LPWSTR pszPathW = NULL;
    TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));

    if(pszPath) {
        pszPathW = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
        if(!pszPathW) {
            hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
            goto cleanup;
        }
    }
    TRACE("%08x,%08x,%s\n",nFolder, dwFlags, debugstr_w(pszSubPathW));

    /* SHGetFolderPathAndSubDirW does not distinguish if pszSubPath isn't
     * set (null), or an empty string.therefore call it without the parameter set
     * if pszSubPath is an empty string
     */
    if (pszSubPath && pszSubPath[0]) {
        length = MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, NULL, 0);
        pszSubPathW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR));
        if(!pszSubPathW) {
            hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
            goto cleanup;
        }
        MultiByteToWideChar(CP_ACP, 0, pszSubPath, -1, pszSubPathW, length);
    }

    hr = SHGetFolderPathAndSubDirW(hwndOwner, nFolder, hToken, dwFlags, pszSubPathW, pszPathW);

    if (SUCCEEDED(hr) && pszPath)
        WideCharToMultiByte(CP_ACP, 0, pszPathW, -1, pszPath, MAX_PATH, NULL, NULL);

cleanup:
    HeapFree(GetProcessHeap(), 0, pszPathW);
    HeapFree(GetProcessHeap(), 0, pszSubPathW);
    return hr;
}

1725 1726 1727
/*************************************************************************
 * SHGetFolderPathAndSubDirW		[SHELL32.@]
 */
1728 1729 1730 1731 1732 1733 1734
HRESULT WINAPI SHGetFolderPathAndSubDirW(
	HWND hwndOwner,    /* [I] owner window */
	int nFolder,       /* [I] CSIDL identifying the folder */
	HANDLE hToken,     /* [I] access token */
	DWORD dwFlags,     /* [I] which path to return */
	LPCWSTR pszSubPath,/* [I] sub directory of the specified folder */
	LPWSTR pszPath)    /* [O] converted path */
1735 1736 1737 1738 1739
{
    HRESULT    hr;
    WCHAR      szBuildPath[MAX_PATH], szTemp[MAX_PATH];
    DWORD      folder = nFolder & CSIDL_FOLDER_MASK;
    CSIDL_Type type;
1740 1741
    int        ret;
    
1742
    TRACE("%p,%p,nFolder=0x%04x,%s\n", hwndOwner,pszPath,nFolder,debugstr_w(pszSubPath));
1743 1744 1745 1746 1747 1748

    /* Windows always NULL-terminates the resulting path regardless of success
     * or failure, so do so first
     */
    if (pszPath)
        *pszPath = '\0';
1749

1750 1751
    if (folder >= sizeof(CSIDL_Data) / sizeof(CSIDL_Data[0]))
        return E_INVALIDARG;
1752 1753
    if ((SHGFP_TYPE_CURRENT != dwFlags) && (SHGFP_TYPE_DEFAULT != dwFlags))
        return E_INVALIDARG;
1754
    szTemp[0] = 0;
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
    type = CSIDL_Data[folder].type;
    switch (type)
    {
        case CSIDL_Type_Disallowed:
            hr = E_INVALIDARG;
            break;
        case CSIDL_Type_NonExistent:
            hr = S_FALSE;
            break;
        case CSIDL_Type_WindowsPath:
            GetWindowsDirectoryW(szTemp, MAX_PATH);
            if (CSIDL_Data[folder].szDefaultPath &&
             !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
             *CSIDL_Data[folder].szDefaultPath)
            {
                PathAddBackslashW(szTemp);
                strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
            }
            hr = S_OK;
            break;
        case CSIDL_Type_SystemPath:
            GetSystemDirectoryW(szTemp, MAX_PATH);
            if (CSIDL_Data[folder].szDefaultPath &&
             !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
             *CSIDL_Data[folder].szDefaultPath)
            {
                PathAddBackslashW(szTemp);
                strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
            }
            hr = S_OK;
            break;
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
        case CSIDL_Type_SystemX86Path:
            if (!GetSystemWow64DirectoryW(szTemp, MAX_PATH)) GetSystemDirectoryW(szTemp, MAX_PATH);
            if (CSIDL_Data[folder].szDefaultPath &&
             !IS_INTRESOURCE(CSIDL_Data[folder].szDefaultPath) &&
             *CSIDL_Data[folder].szDefaultPath)
            {
                PathAddBackslashW(szTemp);
                strcatW(szTemp, CSIDL_Data[folder].szDefaultPath);
            }
            hr = S_OK;
            break;
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
        case CSIDL_Type_CurrVer:
            hr = _SHGetCurrentVersionPath(dwFlags, folder, szTemp);
            break;
        case CSIDL_Type_User:
            hr = _SHGetUserProfilePath(hToken, dwFlags, folder, szTemp);
            break;
        case CSIDL_Type_AllUsers:
            hr = _SHGetAllUsersProfilePath(dwFlags, folder, szTemp);
            break;
        default:
            FIXME("bogus type %d, please fix\n", type);
            hr = E_INVALIDARG;
1809
            break;
1810 1811 1812 1813 1814 1815 1816
    }

    /* Expand environment strings if necessary */
    if (*szTemp == '%')
        hr = _SHExpandEnvironmentStrings(szTemp, szBuildPath);
    else
        strcpyW(szBuildPath, szTemp);
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

    if (FAILED(hr)) goto end;

    if(pszSubPath) {
        /* make sure the new path does not exceed th bufferlength
         * rememebr to backslash and the termination */
        if(MAX_PATH < (lstrlenW(szBuildPath) + lstrlenW(pszSubPath) + 2)) {
            hr = HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
            goto end;
        }
        PathAppendW(szBuildPath, pszSubPath);
        PathRemoveBackslashW(szBuildPath);
    }
1830
    /* Copy the path if it's available before we might return */
1831
    if (SUCCEEDED(hr) && pszPath)
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
        strcpyW(pszPath, szBuildPath);

    /* if we don't care about existing directories we are ready */
    if(nFolder & CSIDL_FLAG_DONT_VERIFY) goto end;

    if (PathFileExistsW(szBuildPath)) goto end;

    /* not existing but we are not allowed to create it.  The return value
     * is verified against shell32 version 6.0.
     */
    if (!(nFolder & CSIDL_FLAG_CREATE))
    {
1844
        hr = HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
1845 1846 1847 1848
        goto end;
    }

    /* create directory/directories */
1849 1850
    ret = SHCreateDirectoryExW(hwndOwner, szBuildPath, NULL);
    if (ret && ret != ERROR_ALREADY_EXISTS)
1851
    {
1852
        ERR("Failed to create directory %s.\n", debugstr_w(szBuildPath));
1853 1854
        hr = E_FAIL;
        goto end;
1855 1856
    }

1857
    TRACE("Created missing system directory %s\n", debugstr_w(szBuildPath));
1858
end:
1859
    TRACE("returning 0x%08x (final path is %s)\n", hr, debugstr_w(szBuildPath));
1860
    return hr;
1861
}
1862 1863

/*************************************************************************
1864
 * SHGetFolderPathA			[SHELL32.@]
1865 1866
 *
 * See SHGetFolderPathW.
1867
 */
1868
HRESULT WINAPI SHGetFolderPathA(
1869
	HWND hwndOwner,
1870
	int nFolder,
1871 1872 1873
	HANDLE hToken,
	DWORD dwFlags,
	LPSTR pszPath)
1874
{
Juan Lang's avatar
Juan Lang committed
1875 1876
    WCHAR szTemp[MAX_PATH];
    HRESULT hr;
1877

1878
    TRACE("%p,%p,nFolder=0x%04x\n",hwndOwner,pszPath,nFolder);
1879

1880 1881 1882 1883
    if (pszPath)
        *pszPath = '\0';
    hr = SHGetFolderPathW(hwndOwner, nFolder, hToken, dwFlags, szTemp);
    if (SUCCEEDED(hr) && pszPath)
Juan Lang's avatar
Juan Lang committed
1884 1885
        WideCharToMultiByte(CP_ACP, 0, szTemp, -1, pszPath, MAX_PATH, NULL,
         NULL);
1886

1887 1888 1889 1890
    return hr;
}

/* For each folder in folders, if its value has not been set in the registry,
1891
 * calls _SHGetUserProfilePath or _SHGetAllUsersProfilePath (depending on the
1892
 * folder's type) to get the unexpanded value first.
1893 1894 1895 1896
 * Writes the unexpanded value to User Shell Folders, and queries it with
 * SHGetFolderPathW to force the creation of the directory if it doesn't
 * already exist.  SHGetFolderPathW also returns the expanded value, which
 * this then writes to Shell Folders.
1897 1898
 */
static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken,
1899 1900
 LPCWSTR szUserShellFolderPath, LPCWSTR szShellFolderPath, const UINT folders[],
 UINT foldersLen)
1901 1902 1903 1904
{
    UINT i;
    WCHAR path[MAX_PATH];
    HRESULT hr = S_OK;
1905
    HKEY hUserKey = NULL, hKey = NULL;
1906
    DWORD dwType, dwPathLen;
1907 1908 1909 1910 1911
    LONG ret;

    TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken,
     debugstr_w(szUserShellFolderPath), folders, foldersLen);

1912
    ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey);
1913 1914
    if (ret)
        hr = HRESULT_FROM_WIN32(ret);
1915 1916
    else
    {
1917
        ret = RegCreateKeyW(hRootKey, szShellFolderPath, &hKey);
1918 1919 1920
        if (ret)
            hr = HRESULT_FROM_WIN32(ret);
    }
1921 1922 1923
    for (i = 0; SUCCEEDED(hr) && i < foldersLen; i++)
    {
        dwPathLen = MAX_PATH * sizeof(WCHAR);
1924
        if (RegQueryValueExW(hUserKey, CSIDL_Data[folders[i]].szValueName, NULL,
1925 1926 1927 1928 1929 1930 1931 1932 1933
         &dwType, (LPBYTE)path, &dwPathLen) || (dwType != REG_SZ &&
         dwType != REG_EXPAND_SZ))
        {
            *path = '\0';
            if (CSIDL_Data[folders[i]].type == CSIDL_Type_User)
                _SHGetUserProfilePath(hToken, SHGFP_TYPE_DEFAULT, folders[i],
                 path);
            else if (CSIDL_Data[folders[i]].type == CSIDL_Type_AllUsers)
                _SHGetAllUsersProfilePath(SHGFP_TYPE_DEFAULT, folders[i], path);
1934
            else if (CSIDL_Data[folders[i]].type == CSIDL_Type_WindowsPath)
1935
            {
1936
                GetWindowsDirectoryW(path, MAX_PATH);
1937 1938 1939 1940 1941 1942 1943
                if (CSIDL_Data[folders[i]].szDefaultPath &&
                    !IS_INTRESOURCE(CSIDL_Data[folders[i]].szDefaultPath))
                {
                    PathAddBackslashW(path);
                    strcatW(path, CSIDL_Data[folders[i]].szDefaultPath);
                }
            }
1944 1945 1946 1947
            else
                hr = E_FAIL;
            if (*path)
            {
1948 1949 1950
                ret = RegSetValueExW(hUserKey,
                 CSIDL_Data[folders[i]].szValueName, 0, REG_EXPAND_SZ,
                 (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
1951 1952 1953
                if (ret)
                    hr = HRESULT_FROM_WIN32(ret);
                else
1954
                {
1955
                    hr = SHGetFolderPathW(NULL, folders[i] | CSIDL_FLAG_CREATE,
1956 1957 1958 1959 1960 1961 1962
                     hToken, SHGFP_TYPE_DEFAULT, path);
                    ret = RegSetValueExW(hKey,
                     CSIDL_Data[folders[i]].szValueName, 0, REG_SZ,
                     (LPBYTE)path, (strlenW(path) + 1) * sizeof(WCHAR));
                    if (ret)
                        hr = HRESULT_FROM_WIN32(ret);
                }
1963 1964 1965
            }
        }
    }
1966 1967
    if (hUserKey)
        RegCloseKey(hUserKey);
1968 1969 1970
    if (hKey)
        RegCloseKey(hKey);

1971
    TRACE("returning 0x%08x\n", hr);
1972 1973 1974 1975 1976 1977 1978 1979 1980
    return hr;
}

static HRESULT _SHRegisterUserShellFolders(BOOL bDefault)
{
    static const UINT folders[] = {
     CSIDL_PROGRAMS,
     CSIDL_PERSONAL,
     CSIDL_FAVORITES,
1981
     CSIDL_APPDATA,
1982 1983 1984 1985
     CSIDL_STARTUP,
     CSIDL_RECENT,
     CSIDL_SENDTO,
     CSIDL_STARTMENU,
1986 1987
     CSIDL_MYMUSIC,
     CSIDL_MYVIDEO,
1988 1989 1990 1991
     CSIDL_DESKTOPDIRECTORY,
     CSIDL_NETHOOD,
     CSIDL_TEMPLATES,
     CSIDL_PRINTHOOD,
1992 1993
     CSIDL_LOCAL_APPDATA,
     CSIDL_INTERNET_CACHE,
1994 1995
     CSIDL_COOKIES,
     CSIDL_HISTORY,
1996 1997
     CSIDL_MYPICTURES,
     CSIDL_FONTS
1998
    };
1999 2000
    WCHAR userShellFolderPath[MAX_PATH], shellFolderPath[MAX_PATH];
    LPCWSTR pUserShellFolderPath, pShellFolderPath;
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
    HRESULT hr = S_OK;
    HKEY hRootKey;
    HANDLE hToken;

    TRACE("%s\n", bDefault ? "TRUE" : "FALSE");
    if (bDefault)
    {
        hToken = (HANDLE)-1;
        hRootKey = HKEY_USERS;
        strcpyW(userShellFolderPath, DefaultW);
        PathAddBackslashW(userShellFolderPath);
        strcatW(userShellFolderPath, szSHUserFolders);
        pUserShellFolderPath = userShellFolderPath;
2014 2015 2016 2017
        strcpyW(shellFolderPath, DefaultW);
        PathAddBackslashW(shellFolderPath);
        strcatW(shellFolderPath, szSHFolders);
        pShellFolderPath = shellFolderPath;
2018 2019 2020 2021 2022 2023
    }
    else
    {
        hToken = NULL;
        hRootKey = HKEY_CURRENT_USER;
        pUserShellFolderPath = szSHUserFolders;
2024
        pShellFolderPath = szSHFolders;
2025 2026 2027
    }

    hr = _SHRegisterFolders(hRootKey, hToken, pUserShellFolderPath,
2028
     pShellFolderPath, folders, sizeof(folders) / sizeof(folders[0]));
2029
    TRACE("returning 0x%08x\n", hr);
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
    return hr;
}

static HRESULT _SHRegisterCommonShellFolders(void)
{
    static const UINT folders[] = {
     CSIDL_COMMON_STARTMENU,
     CSIDL_COMMON_PROGRAMS,
     CSIDL_COMMON_STARTUP,
     CSIDL_COMMON_DESKTOPDIRECTORY,
     CSIDL_COMMON_FAVORITES,
     CSIDL_COMMON_APPDATA,
     CSIDL_COMMON_TEMPLATES,
     CSIDL_COMMON_DOCUMENTS,
    };
    HRESULT hr;

    TRACE("\n");
    hr = _SHRegisterFolders(HKEY_LOCAL_MACHINE, NULL, szSHUserFolders,
2049
     szSHFolders, folders, sizeof(folders) / sizeof(folders[0]));
2050
    TRACE("returning 0x%08x\n", hr);
2051 2052
    return hr;
}
Juan Lang's avatar
Juan Lang committed
2053

2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
/******************************************************************************
 * _SHAppendToUnixPath  [Internal]
 *
 * Helper function for _SHCreateSymbolicLinks. Appends pwszSubPath (or the 
 * corresponding resource, if IS_INTRESOURCE) to the unix base path 'szBasePath' 
 * and replaces backslashes with slashes.
 *
 * PARAMS
 *  szBasePath  [IO] The unix base path, which will be appended to (CP_UNXICP).
 *  pwszSubPath [I]  Sub-path or resource id (use MAKEINTRESOURCEW).
 *
 * RETURNS
 *  Success: TRUE,
 *  Failure: FALSE
 */
static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
    WCHAR wszSubPath[MAX_PATH];
    int cLen = strlen(szBasePath);
    char *pBackslash;

    if (IS_INTRESOURCE(pwszSubPath)) {
        if (!LoadStringW(shell32_hInstance, LOWORD(pwszSubPath), wszSubPath, MAX_PATH)) {
            /* Fall back to hard coded defaults. */
            switch (LOWORD(pwszSubPath)) {
                case IDS_PERSONAL:
                    lstrcpyW(wszSubPath, PersonalW);
                    break;
                case IDS_MYMUSIC:
                    lstrcpyW(wszSubPath, My_MusicW);
                    break;
                case IDS_MYPICTURES:
                    lstrcpyW(wszSubPath, My_PicturesW);
                    break;
                case IDS_MYVIDEO:
                    lstrcpyW(wszSubPath, My_VideoW);
                    break;
                default:
                    ERR("LoadString(%d) failed!\n", LOWORD(pwszSubPath));
                    return FALSE;
            }
        }
    } else {
        lstrcpyW(wszSubPath, pwszSubPath);
    }
 
    if (szBasePath[cLen-1] != '/') szBasePath[cLen++] = '/';
 
    if (!WideCharToMultiByte(CP_UNIXCP, 0, wszSubPath, -1, szBasePath + cLen,
                             FILENAME_MAX - cLen, NULL, NULL))
    {
        return FALSE;
    }
 
    pBackslash = szBasePath + cLen;
    while ((pBackslash = strchr(pBackslash, '\\'))) *pBackslash = '/';
 
    return TRUE;
}

/******************************************************************************
 * _SHCreateSymbolicLinks  [Internal]
 * 
 * Sets up symbol links for various shell folders to point into the users home
 * directory. We do an educated guess about what the user would probably want:
 * - If there is a 'My Documents' directory in $HOME, the user probably wants
 *   wine's 'My Documents' to point there. Furthermore, we imply that the user
 *   is a Windows lover and has no problem with wine creating 'My Pictures',
 *   'My Music' and 'My Video' subfolders under '$HOME/My Documents', if those
 *   do not already exits. We put appropriate symbolic links in place for those,
 *   too.
 * - If there is no 'My Documents' directory in $HOME, we let 'My Documents'
 *   point directly to $HOME. We assume the user to be a unix hacker who does not
 *   want wine to create anything anywhere besides the .wine directory. So, if
 *   there already is a 'My Music' directory in $HOME, we symlink the 'My Music'
2128 2129 2130
 *   shell folder to it. But if not, then we check XDG_MUSIC_DIR - "well known"
 *   directory, and try to link to that. If that fails, then we symlink to
 *   $HOME directly. The same holds fo 'My Pictures' and 'My Video'.
2131 2132 2133
 * - The Desktop shell folder is symlinked to XDG_DESKTOP_DIR. If that does not
 *   exist, then we try '$HOME/Desktop'. If that does not exist, then we leave
 *   it alone.
2134 2135
 * ('My Music',... above in fact means LoadString(IDS_MYMUSIC))
 */
2136
static void _SHCreateSymbolicLinks(void)
2137 2138 2139
{
    UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEO, IDS_MYMUSIC }, i;
    int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
2140 2141
    static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DESKTOP" };
    static const unsigned int num = sizeof(xdg_dirs) / sizeof(xdg_dirs[0]);
2142 2143 2144 2145 2146 2147 2148
    WCHAR wszTempPath[MAX_PATH];
    char szPersonalTarget[FILENAME_MAX], *pszPersonal;
    char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
    char szDesktopTarget[FILENAME_MAX], *pszDesktop;
    struct stat statFolder;
    const char *pszHome;
    HRESULT hr;
2149
    char ** xdg_results;
2150
    char * xdg_desktop_dir;
2151 2152 2153 2154 2155 2156 2157 2158

    /* Create all necessary profile sub-dirs up to 'My Documents' and get the unix path. */
    hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
                          SHGFP_TYPE_DEFAULT, wszTempPath);
    if (FAILED(hr)) return;
    pszPersonal = wine_get_unix_file_name(wszTempPath);
    if (!pszPersonal) return;

2159 2160
    hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
    if (FAILED(hr)) xdg_results = NULL;
2161

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
    pszHome = getenv("HOME");
    if (pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)) {
        strcpy(szPersonalTarget, pszHome);
        if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
            !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
        {
            /* '$HOME/My Documents' exists. Create 'My Pictures', 'My Videos' and 
             * 'My Music' subfolders or fail silently if they already exist. */
            for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
                strcpy(szMyStuffTarget, szPersonalTarget);
                if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2173
                    mkdir(szMyStuffTarget, 0777);
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
            }
        } 
        else
        {
            /* '$HOME/My Documents' doesn't exists, but '$HOME' does. */ 
            strcpy(szPersonalTarget, pszHome);
        }

        /* Replace 'My Documents' directory with a symlink of fail silently if not empty. */
        rmdir(pszPersonal);
        symlink(szPersonalTarget, pszPersonal);
    }
    else
    {
        /* '$HOME' doesn't exist. Create 'My Pictures', 'My Videos' and 'My Music' subdirs
         * in '%USERPROFILE%\\My Documents' or fail silently if they already exist. */
        strcpy(szPersonalTarget, pszPersonal);
        for (i = 0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
            strcpy(szMyStuffTarget, szPersonalTarget);
            if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])))
2194
                mkdir(szMyStuffTarget, 0777);
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
        }
    }

    /* Create symbolic links for 'My Pictures', 'My Video' and 'My Music'. */
    for (i=0; i < sizeof(aidsMyStuff)/sizeof(aidsMyStuff[0]); i++) {
        /* Create the current 'My Whatever' folder and get it's unix path. */
        hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
                              SHGFP_TYPE_DEFAULT, wszTempPath);
        if (FAILED(hr)) continue;
        pszMyStuff = wine_get_unix_file_name(wszTempPath);
        if (!pszMyStuff) continue;
        
        strcpy(szMyStuffTarget, szPersonalTarget);
        if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
            !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
        {
            /* If there's a 'My Whatever' directory where 'My Documents' links to, link to it. */
            rmdir(pszMyStuff);
            symlink(szMyStuffTarget, pszMyStuff);
        } 
        else
        {
            rmdir(pszMyStuff);
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
            if (xdg_results && xdg_results[i])
            {
                /* the folder specified by XDG_XXX_DIR exists, link to it. */
                symlink(xdg_results[i], pszMyStuff);
            }
            else
            {
                /* Else link to where 'My Documents' itself links to. */
                symlink(szPersonalTarget, pszMyStuff);
            }
2228 2229 2230 2231 2232
        }
        HeapFree(GetProcessHeap(), 0, pszMyStuff);
    }

    /* Last but not least, the Desktop folder */
2233 2234 2235 2236 2237 2238
    if (pszHome)
        strcpy(szDesktopTarget, pszHome);
    else
        strcpy(szDesktopTarget, pszPersonal);
    HeapFree(GetProcessHeap(), 0, pszPersonal);

2239 2240 2241 2242
    xdg_desktop_dir = xdg_results ? xdg_results[num - 1] : NULL;
    if (xdg_desktop_dir ||
        (_SHAppendToUnixPath(szDesktopTarget, DesktopW) &&
        !stat(szDesktopTarget, &statFolder) && S_ISDIR(statFolder.st_mode)))
2243 2244 2245 2246 2247 2248
    {
        hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
                              SHGFP_TYPE_DEFAULT, wszTempPath);
        if (SUCCEEDED(hr) && (pszDesktop = wine_get_unix_file_name(wszTempPath))) 
        {
            rmdir(pszDesktop);
2249 2250 2251 2252
            if (xdg_desktop_dir)
                symlink(xdg_desktop_dir, pszDesktop);
            else
                symlink(szDesktopTarget, pszDesktop);
2253 2254 2255
            HeapFree(GetProcessHeap(), 0, pszDesktop);
        }
    }
2256 2257 2258 2259 2260 2261 2262 2263

    /* Free resources allocated by XDG_UserDirLookup() */
    if (xdg_results)
    {
        for (i = 0; i < num; i++)
            HeapFree(GetProcessHeap(), 0, xdg_results[i]);
        HeapFree(GetProcessHeap(), 0, xdg_results);
    }
2264 2265
}

2266 2267 2268 2269 2270
/* Register the default values in the registry, as some apps seem to depend
 * on their presence.  The set registered was taken from Windows XP.
 */
HRESULT SHELL_RegisterShellFolders(void)
{
2271
    HRESULT hr;
2272

2273 2274 2275 2276 2277 2278 2279
    /* Set up '$HOME' targeted symlinks for 'My Documents', 'My Pictures',
     * 'My Video', 'My Music' and 'Desktop' in advance, so that the
     * _SHRegister*ShellFolders() functions will find everything nice and clean
     * and thus will not attempt to create them in the profile directory. */
    _SHCreateSymbolicLinks();
    
    hr = _SHRegisterUserShellFolders(TRUE);
2280 2281 2282 2283
    if (SUCCEEDED(hr))
        hr = _SHRegisterUserShellFolders(FALSE);
    if (SUCCEEDED(hr))
        hr = _SHRegisterCommonShellFolders();
Juan Lang's avatar
Juan Lang committed
2284
    return hr;
2285
}
2286 2287

/*************************************************************************
2288
 * SHGetSpecialFolderPathA [SHELL32.@]
2289
 */
2290
BOOL WINAPI SHGetSpecialFolderPathA (
2291
	HWND hwndOwner,
2292
	LPSTR szPath,
2293
	int nFolder,
2294 2295
	BOOL bCreate)
{
2296 2297
	return (SHGetFolderPathA(
		hwndOwner,
2298
		nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2299 2300 2301
		NULL,
		0,
		szPath)) == S_OK ? TRUE : FALSE;
2302
}
Jim Aston's avatar
Jim Aston committed
2303

Juergen Schmied's avatar
Juergen Schmied committed
2304
/*************************************************************************
2305
 * SHGetSpecialFolderPathW
Juergen Schmied's avatar
Juergen Schmied committed
2306
 */
2307
BOOL WINAPI SHGetSpecialFolderPathW (
Juergen Schmied's avatar
Juergen Schmied committed
2308
	HWND hwndOwner,
2309
	LPWSTR szPath,
2310
	int nFolder,
2311
	BOOL bCreate)
Juergen Schmied's avatar
Juergen Schmied committed
2312
{
2313
	return (SHGetFolderPathW(
Juergen Schmied's avatar
Juergen Schmied committed
2314
		hwndOwner,
2315
		nFolder + (bCreate ? CSIDL_FLAG_CREATE : 0),
2316 2317 2318
		NULL,
		0,
		szPath)) == S_OK ? TRUE : FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
2319 2320 2321
}

/*************************************************************************
2322
 * SHGetSpecialFolderPath (SHELL32.175)
Juergen Schmied's avatar
Juergen Schmied committed
2323
 */
2324
BOOL WINAPI SHGetSpecialFolderPathAW (
Juergen Schmied's avatar
Juergen Schmied committed
2325
	HWND hwndOwner,
2326
	LPVOID szPath,
2327
	int nFolder,
2328 2329
	BOOL bCreate)

Juergen Schmied's avatar
Juergen Schmied committed
2330
{
2331
	if (SHELL_OsIsUnicode())
2332 2333
	  return SHGetSpecialFolderPathW (hwndOwner, szPath, nFolder, bCreate);
	return SHGetSpecialFolderPathA (hwndOwner, szPath, nFolder, bCreate);
Juergen Schmied's avatar
Juergen Schmied committed
2334
}
Juan Lang's avatar
Juan Lang committed
2335 2336

/*************************************************************************
2337
 * SHGetFolderLocation [SHELL32.@]
Juan Lang's avatar
Juan Lang committed
2338
 *
2339
 * Gets the folder locations from the registry and creates a pidl.
Juan Lang's avatar
Juan Lang committed
2340 2341
 *
 * PARAMS
2342 2343 2344 2345 2346 2347
 *   hwndOwner  [I]
 *   nFolder    [I] CSIDL_xxxxx
 *   hToken     [I] token representing user, or NULL for current user, or -1 for
 *                  default user
 *   dwReserved [I] must be zero
 *   ppidl      [O] PIDL of a special folder
Juan Lang's avatar
Juan Lang committed
2348
 *
2349 2350 2351 2352
 * RETURNS
 *  Success: S_OK
 *  Failure: Standard OLE-defined error result, S_FALSE or E_INVALIDARG
 *
Juan Lang's avatar
Juan Lang committed
2353
 * NOTES
2354 2355 2356
 *  Creates missing reg keys and directories.
 *  Mostly forwards to SHGetFolderPathW, but a few values of nFolder return
 *  virtual folders that are handled here.
Juan Lang's avatar
Juan Lang committed
2357
 */
2358
HRESULT WINAPI SHGetFolderLocation(
Juan Lang's avatar
Juan Lang committed
2359
	HWND hwndOwner,
2360 2361 2362 2363
	int nFolder,
	HANDLE hToken,
	DWORD dwReserved,
	LPITEMIDLIST *ppidl)
Juan Lang's avatar
Juan Lang committed
2364 2365 2366
{
    HRESULT hr = E_INVALIDARG;

2367
    TRACE("%p 0x%08x %p 0x%08x %p\n",
2368 2369
     hwndOwner, nFolder, hToken, dwReserved, ppidl);
    
Juan Lang's avatar
Juan Lang committed
2370 2371
    if (!ppidl)
        return E_INVALIDARG;
2372 2373
    if (dwReserved)
        return E_INVALIDARG;
Juan Lang's avatar
Juan Lang committed
2374

2375
    /* The virtual folders' locations are not user-dependent */
Juan Lang's avatar
Juan Lang committed
2376 2377 2378 2379 2380 2381 2382
    *ppidl = NULL;
    switch (nFolder)
    {
        case CSIDL_DESKTOP:
            *ppidl = _ILCreateDesktop();
            break;

2383 2384 2385 2386
        case CSIDL_PERSONAL:
            *ppidl = _ILCreateMyDocuments();
            break;

Juan Lang's avatar
Juan Lang committed
2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
        case CSIDL_INTERNET:
            *ppidl = _ILCreateIExplore();
            break;

        case CSIDL_CONTROLS:
            *ppidl = _ILCreateControlPanel();
            break;

        case CSIDL_PRINTERS:
            *ppidl = _ILCreatePrinters();
            break;

        case CSIDL_BITBUCKET:
            *ppidl = _ILCreateBitBucket();
            break;

        case CSIDL_DRIVES:
            *ppidl = _ILCreateMyComputer();
            break;

        case CSIDL_NETWORK:
            *ppidl = _ILCreateNetwork();
            break;

        default:
        {
            WCHAR szPath[MAX_PATH];

2415 2416 2417
            hr = SHGetFolderPathW(hwndOwner, nFolder, hToken,
             SHGFP_TYPE_CURRENT, szPath);
            if (SUCCEEDED(hr))
Juan Lang's avatar
Juan Lang committed
2418 2419 2420 2421 2422 2423
            {
                DWORD attributes=0;

                TRACE("Value=%s\n", debugstr_w(szPath));
                hr = SHILCreateFromPathW(szPath, ppidl, &attributes);
            }
2424 2425 2426
            else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
            {
                /* unlike SHGetFolderPath, SHGetFolderLocation in shell32
2427
                 * version 6.0 returns E_FAIL for nonexistent paths
2428 2429 2430
                 */
                hr = E_FAIL;
            }
Juan Lang's avatar
Juan Lang committed
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
        }
    }
    if(*ppidl)
        hr = NOERROR;

    TRACE("-- (new pidl %p)\n",*ppidl);
    return hr;
}

/*************************************************************************
2441
 * SHGetSpecialFolderLocation		[SHELL32.@]
Juan Lang's avatar
Juan Lang committed
2442 2443
 *
 * NOTES
2444 2445
 *   In NT5, SHGetSpecialFolderLocation needs the <winntdir>/Recent
 *   directory.
Juan Lang's avatar
Juan Lang committed
2446
 */
2447 2448 2449 2450
HRESULT WINAPI SHGetSpecialFolderLocation(
	HWND hwndOwner,
	INT nFolder,
	LPITEMIDLIST * ppidl)
Juan Lang's avatar
Juan Lang committed
2451
{
2452 2453 2454
    HRESULT hr = E_INVALIDARG;

    TRACE("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
Juan Lang's avatar
Juan Lang committed
2455 2456 2457 2458

    if (!ppidl)
        return E_INVALIDARG;

2459
    hr = SHGetFolderLocation(hwndOwner, nFolder, NULL, 0, ppidl);
Juan Lang's avatar
Juan Lang committed
2460 2461
    return hr;
}