path.c 106 KB
Newer Older
1 2
/*
 * Path Functions
3 4
 *
 * Copyright 1999, 2000 Juergen Schmied
5
 * Copyright 2001, 2002 Jon Griffiths
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 24
#include "config.h"
#include "wine/port.h"

25
#include <stdarg.h>
26
#include <string.h>
27
#include <stdlib.h>
28 29

#include "wine/unicode.h"
30
#include "windef.h"
31 32 33
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
34
#include "winreg.h"
Huw Davies's avatar
Huw Davies committed
35
#include "winternl.h"
36
#define NO_SHLWAPI_STREAM
37
#include "shlwapi.h"
38
#include "wine/debug.h"
39

40
WINE_DEFAULT_DEBUG_CHANNEL(shell);
41

42 43 44 45 46
/* Get a function pointer from a DLL handle */
#define GET_FUNC(func, module, name, fail) \
  do { \
    if (!func) { \
      if (!SHLWAPI_h##module && !(SHLWAPI_h##module = LoadLibraryA(#module ".dll"))) return fail; \
Jon Griffiths's avatar
Jon Griffiths committed
47 48
      func = (fn##func)GetProcAddress(SHLWAPI_h##module, name); \
      if (!func) return fail; \
49 50 51 52
    } \
  } while (0)

/* DLL handles for late bound calls */
53
static HMODULE SHLWAPI_hshell32;
54 55

/* Function pointers for GET_FUNC macro; these need to be global because of gcc bug */
Jon Griffiths's avatar
Jon Griffiths committed
56 57 58
typedef BOOL (WINAPI *fnpIsNetDrive)(int);
static  fnpIsNetDrive pIsNetDrive;

59
HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR,LPWSTR,DWORD);
60

61
/*************************************************************************
62
 * PathAppendA    [SHLWAPI.@]
63
 *
64 65 66
 * Append one path to another.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
67 68
 *  lpszPath   [I/O] Initial part of path, and destination for output
 *  lpszAppend [I]   Path to append
69 70 71
 *
 * RETURNS
 *  Success: TRUE. lpszPath contains the newly created path.
Jon Griffiths's avatar
Jon Griffiths committed
72
 *  Failure: FALSE, if either path is NULL, or PathCombineA() fails.
73 74 75
 *
 * NOTES
 *  lpszAppend must contain at least one backslash ('\') if not NULL.
Jon Griffiths's avatar
Jon Griffiths committed
76
 *  Because PathCombineA() is used to join the paths, the resulting
77
 *  path is also canonicalized.
78
 */
79
BOOL WINAPI PathAppendA (LPSTR lpszPath, LPCSTR lpszAppend)
80
{
81 82 83 84
  TRACE("(%s,%s)\n",debugstr_a(lpszPath), debugstr_a(lpszAppend));

  if (lpszPath && lpszAppend)
  {
85 86 87
    if (!PathIsUNCA(lpszAppend))
      while (*lpszAppend == '\\')
        lpszAppend++;
88 89 90 91
    if (PathCombineA(lpszPath, lpszPath, lpszAppend))
      return TRUE;
  }
  return FALSE;
92 93 94
}

/*************************************************************************
95 96 97
 * PathAppendW    [SHLWAPI.@]
 *
 * See PathAppendA.
98
 */
99
BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
100
{
101 102 103 104
  TRACE("(%s,%s)\n",debugstr_w(lpszPath), debugstr_w(lpszAppend));

  if (lpszPath && lpszAppend)
  {
105 106 107
    if (!PathIsUNCW(lpszAppend))
      while (*lpszAppend == '\\')
        lpszAppend++;
108 109 110 111
    if (PathCombineW(lpszPath, lpszPath, lpszAppend))
      return TRUE;
  }
  return FALSE;
112 113 114 115 116
}

/*************************************************************************
 * PathCombineA		[SHLWAPI.@]
 *
117 118 119 120 121
 * Combine two paths together.
 *
 * PARAMS
 *  lpszDest [O] Destination for combined path
 *  lpszDir  [I] Directory path
Jon Griffiths's avatar
Jon Griffiths committed
122
 *  lpszFile [I] File path
123 124 125 126 127 128 129 130 131 132 133
 *
 * RETURNS
 *  Success: The output path
 *  Failure: NULL, if inputs are invalid.
 *
 * NOTES
 *  lpszDest should be at least MAX_PATH in size, and may point to the same
 *  memory location as lpszDir. The combined path is canonicalised.
 */
LPSTR WINAPI PathCombineA(LPSTR lpszDest, LPCSTR lpszDir, LPCSTR lpszFile)
{
134 135 136
  WCHAR szDest[MAX_PATH];
  WCHAR szDir[MAX_PATH];
  WCHAR szFile[MAX_PATH];
137
  TRACE("(%p,%s,%s)\n", lpszDest, debugstr_a(lpszDir), debugstr_a(lpszFile));
138

139 140 141 142
  /* Invalid parameters */
  if (!lpszDest)
    return NULL;
  if (!lpszDir && !lpszFile)
143
  {
144 145
    lpszDest[0] = 0;
    return NULL;
146
  }
147 148 149 150 151 152 153 154 155 156 157 158

  if (lpszDir)
    MultiByteToWideChar(CP_ACP,0,lpszDir,-1,szDir,MAX_PATH);
  if (lpszFile)
    MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);

  if (PathCombineW(szDest, lpszDir ? szDir : NULL, lpszFile ? szFile : NULL))
    if (WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0))
      return lpszDest;

  lpszDest[0] = 0;
  return NULL;
159 160 161 162
}

/*************************************************************************
 * PathCombineW		 [SHLWAPI.@]
163 164 165 166 167 168 169 170 171 172
 *
 * See PathCombineA.
 */
LPWSTR WINAPI PathCombineW(LPWSTR lpszDest, LPCWSTR lpszDir, LPCWSTR lpszFile)
{
  WCHAR szTemp[MAX_PATH];
  BOOL bUseBoth = FALSE, bStrip = FALSE;

  TRACE("(%p,%s,%s)\n", lpszDest, debugstr_w(lpszDir), debugstr_w(lpszFile));

173 174 175 176 177 178 179 180
  /* Invalid parameters */
  if (!lpszDest)
    return NULL;
  if (!lpszDir && !lpszFile)
  {
    lpszDest[0] = 0;
    return NULL;
  }
181

182
  if ((!lpszFile || !*lpszFile) && lpszDir)
183 184
  {
    /* Use dir only */
185
    lstrcpynW(szTemp, lpszDir, MAX_PATH);
186 187 188 189 190 191
  }
  else if (!lpszDir || !*lpszDir || !PathIsRelativeW(lpszFile))
  {
    if (!lpszDir || !*lpszDir || *lpszFile != '\\' || PathIsUNCW(lpszFile))
    {
      /* Use file only */
192
      lstrcpynW(szTemp, lpszFile, MAX_PATH);
193 194 195 196 197 198 199 200 201 202 203 204
    }
    else
    {
      bUseBoth = TRUE;
      bStrip = TRUE;
    }
  }
  else
    bUseBoth = TRUE;

  if (bUseBoth)
  {
205
    lstrcpynW(szTemp, lpszDir, MAX_PATH);
206 207 208 209 210
    if (bStrip)
    {
      PathStripToRootW(szTemp);
      lpszFile++; /* Skip '\' */
    }
211 212 213
    if (!PathAddBackslashW(szTemp) || strlenW(szTemp) + strlenW(lpszFile) >= MAX_PATH)
    {
      lpszDest[0] = 0;
214
      return NULL;
215
    }
216 217 218 219 220
    strcatW(szTemp, lpszFile);
  }

  PathCanonicalizeW(lpszDest, szTemp);
  return lpszDest;
221 222 223 224 225
}

/*************************************************************************
 * PathAddBackslashA	[SHLWAPI.@]
 *
226 227 228
 * Append a backslash ('\') to a path if one doesn't exist.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
229
 *  lpszPath [I/O] The path to append a backslash to.
230 231 232 233
 *
 * RETURNS
 *  Success: The position of the last backslash in the path.
 *  Failure: NULL, if lpszPath is NULL or the path is too large.
234 235 236
 */
LPSTR WINAPI PathAddBackslashA(LPSTR lpszPath)
{
Jon Griffiths's avatar
Jon Griffiths committed
237
  size_t iLen;
238
  LPSTR prev = lpszPath;
239

240 241 242 243 244 245 246
  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (!lpszPath || (iLen = strlen(lpszPath)) >= MAX_PATH)
    return NULL;

  if (iLen)
  {
247 248 249 250 251 252
    do {
      lpszPath = CharNextA(prev);
      if (*lpszPath)
        prev = lpszPath;
    } while (*lpszPath);
    if (*prev != '\\')
253
    {
254 255
      *lpszPath++ = '\\';
      *lpszPath = '\0';
256 257 258
    }
  }
  return lpszPath;
259 260 261
}

/*************************************************************************
262 263 264
 * PathAddBackslashW  [SHLWAPI.@]
 *
 * See PathAddBackslashA.
265
 */
266
LPWSTR WINAPI PathAddBackslashW( LPWSTR lpszPath )
267
{
Jon Griffiths's avatar
Jon Griffiths committed
268
  size_t iLen;
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  TRACE("(%s)\n",debugstr_w(lpszPath));

  if (!lpszPath || (iLen = strlenW(lpszPath)) >= MAX_PATH)
    return NULL;

  if (iLen)
  {
    lpszPath += iLen;
    if (lpszPath[-1] != '\\')
    {
      *lpszPath++ = '\\';
      *lpszPath = '\0';
    }
  }
  return lpszPath;
285 286 287
}

/*************************************************************************
288 289 290 291 292 293 294 295 296 297 298 299
 * PathBuildRootA    [SHLWAPI.@]
 *
 * Create a root drive string (e.g. "A:\") from a drive number.
 *
 * PARAMS
 *  lpszPath [O] Destination for the drive string
 *
 * RETURNS
 *  lpszPath
 *
 * NOTES
 *  If lpszPath is NULL or drive is invalid, nothing is written to lpszPath.
300
 */
301
LPSTR WINAPI PathBuildRootA(LPSTR lpszPath, int drive)
302
{
Jon Griffiths's avatar
Jon Griffiths committed
303
  TRACE("(%p,%d)\n", lpszPath, drive);
304

305 306 307 308 309 310 311 312
  if (lpszPath && drive >= 0 && drive < 26)
  {
    lpszPath[0] = 'A' + drive;
    lpszPath[1] = ':';
    lpszPath[2] = '\\';
    lpszPath[3] = '\0';
  }
  return lpszPath;
313 314 315
}

/*************************************************************************
316 317 318
 * PathBuildRootW    [SHLWAPI.@]
 *
 * See PathBuildRootA.
319
 */
320
LPWSTR WINAPI PathBuildRootW(LPWSTR lpszPath, int drive)
321
{
Jon Griffiths's avatar
Jon Griffiths committed
322
  TRACE("(%p,%d)\n", lpszPath, drive);
323

324 325 326 327 328 329 330 331 332
  if (lpszPath && drive >= 0 && drive < 26)
  {
    lpszPath[0] = 'A' + drive;
    lpszPath[1] = ':';
    lpszPath[2] = '\\';
    lpszPath[3] = '\0';
  }
  return lpszPath;
}
333 334

/*************************************************************************
335 336 337 338 339 340 341 342 343
 * PathFindFileNameA  [SHLWAPI.@]
 *
 * Locate the start of the file name in a path
 *
 * PARAMS
 *  lpszPath [I] Path to search
 *
 * RETURNS
 *  A pointer to the first character of the file name
344 345 346
 */
LPSTR WINAPI PathFindFileNameA(LPCSTR lpszPath)
{
347
  LPCSTR lastSlash = lpszPath;
348

349
  TRACE("(%s)\n",debugstr_a(lpszPath));
350

351 352 353 354 355 356 357 358
  while (lpszPath && *lpszPath)
  {
    if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
        lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
      lastSlash = lpszPath + 1;
    lpszPath = CharNextA(lpszPath);
  }
  return (LPSTR)lastSlash;
359 360 361
}

/*************************************************************************
362 363 364
 * PathFindFileNameW  [SHLWAPI.@]
 *
 * See PathFindFileNameA.
365 366 367
 */
LPWSTR WINAPI PathFindFileNameW(LPCWSTR lpszPath)
{
368
  LPCWSTR lastSlash = lpszPath;
369

370 371 372 373 374 375 376
  TRACE("(%s)\n",debugstr_w(lpszPath));

  while (lpszPath && *lpszPath)
  {
    if ((*lpszPath == '\\' || *lpszPath == '/' || *lpszPath == ':') &&
        lpszPath[1] && lpszPath[1] != '\\' && lpszPath[1] != '/')
      lastSlash = lpszPath + 1;
377
    lpszPath++;
378 379
  }
  return (LPWSTR)lastSlash;
380 381 382
}

/*************************************************************************
383
 * PathFindExtensionA  [SHLWAPI.@]
384
 *
385 386 387 388 389 390 391 392
 * Locate the start of the file extension in a path
 *
 * PARAMS
 *  lpszPath [I] The path to search
 *
 * RETURNS
 *  A pointer to the first character of the extension, the end of
 *  the string if the path has no extension, or NULL If lpszPath is NULL
393
 */
394
LPSTR WINAPI PathFindExtensionA( LPCSTR lpszPath )
395
{
396
  LPCSTR lastpoint = NULL;
397

398
  TRACE("(%s)\n", debugstr_a(lpszPath));
399

400 401 402 403 404 405 406 407 408 409 410 411
  if (lpszPath)
  {
    while (*lpszPath)
    {
      if (*lpszPath == '\\' || *lpszPath==' ')
        lastpoint = NULL;
      else if (*lpszPath == '.')
        lastpoint = lpszPath;
      lpszPath = CharNextA(lpszPath);
    }
  }
  return (LPSTR)(lastpoint ? lastpoint : lpszPath);
412 413 414
}

/*************************************************************************
415 416 417
 * PathFindExtensionW  [SHLWAPI.@]
 *
 * See PathFindExtensionA.
418
 */
419
LPWSTR WINAPI PathFindExtensionW( LPCWSTR lpszPath )
420
{
421
  LPCWSTR lastpoint = NULL;
422

423
  TRACE("(%s)\n", debugstr_w(lpszPath));
424

425 426 427 428 429 430 431 432
  if (lpszPath)
  {
    while (*lpszPath)
    {
      if (*lpszPath == '\\' || *lpszPath==' ')
        lastpoint = NULL;
      else if (*lpszPath == '.')
        lastpoint = lpszPath;
433
      lpszPath++;
434 435 436
    }
  }
  return (LPWSTR)(lastpoint ? lastpoint : lpszPath);
437 438 439
}

/*************************************************************************
440
 * PathGetArgsA    [SHLWAPI.@]
441
 *
442
 * Find the next argument in a string delimited by spaces.
443
 *
444 445 446 447 448 449 450 451
 * PARAMS
 *  lpszPath [I] The string to search for arguments in
 *
 * RETURNS
 *  The start of the next argument in lpszPath, or NULL if lpszPath is NULL
 *
 * NOTES
 *  Spaces in quoted strings are ignored as delimiters.
452
 */
453
LPSTR WINAPI PathGetArgsA(LPCSTR lpszPath)
454
{
455
  BOOL bSeenQuote = FALSE;
456

457
  TRACE("(%s)\n",debugstr_a(lpszPath));
458

459 460 461 462 463 464 465 466 467 468 469 470
  if (lpszPath)
  {
    while (*lpszPath)
    {
      if ((*lpszPath==' ') && !bSeenQuote)
        return (LPSTR)lpszPath + 1;
      if (*lpszPath == '"')
        bSeenQuote = !bSeenQuote;
      lpszPath = CharNextA(lpszPath);
    }
  }
  return (LPSTR)lpszPath;
471 472 473
}

/*************************************************************************
474 475 476
 * PathGetArgsW    [SHLWAPI.@]
 *
 * See PathGetArgsA.
477
 */
478
LPWSTR WINAPI PathGetArgsW(LPCWSTR lpszPath)
479
{
480
  BOOL bSeenQuote = FALSE;
481

482
  TRACE("(%s)\n",debugstr_w(lpszPath));
483

484 485 486 487 488 489 490 491
  if (lpszPath)
  {
    while (*lpszPath)
    {
      if ((*lpszPath==' ') && !bSeenQuote)
        return (LPWSTR)lpszPath + 1;
      if (*lpszPath == '"')
        bSeenQuote = !bSeenQuote;
492
      lpszPath++;
493 494 495
    }
  }
  return (LPWSTR)lpszPath;
496 497 498 499
}

/*************************************************************************
 * PathGetDriveNumberA	[SHLWAPI.@]
500 501 502 503 504 505 506 507 508
 *
 * Return the drive number from a path
 *
 * PARAMS
 *  lpszPath [I] Path to get the drive number from
 *
 * RETURNS
 *  Success: The drive number corresponding to the drive in the path
 *  Failure: -1, if lpszPath contains no valid drive
509 510 511
 */
int WINAPI PathGetDriveNumberA(LPCSTR lpszPath)
{
512
  TRACE ("(%s)\n",debugstr_a(lpszPath));
513

514 515 516 517
  if (lpszPath && !IsDBCSLeadByte(*lpszPath) && lpszPath[1] == ':' &&
      tolower(*lpszPath) >= 'a' && tolower(*lpszPath) <= 'z')
    return tolower(*lpszPath) - 'a';
  return -1;
518 519 520 521
}

/*************************************************************************
 * PathGetDriveNumberW	[SHLWAPI.@]
522 523
 *
 * See PathGetDriveNumberA.
524 525 526
 */
int WINAPI PathGetDriveNumberW(LPCWSTR lpszPath)
{
527
  TRACE ("(%s)\n",debugstr_w(lpszPath));
528

529 530 531 532 533 534
  if (lpszPath)
  {
      WCHAR tl = tolowerW(lpszPath[0]);
      if (tl >= 'a' && tl <= 'z' && lpszPath[1] == ':')
          return tl - 'a';
  }
535
  return -1;
536 537 538 539
}

/*************************************************************************
 * PathRemoveFileSpecA	[SHLWAPI.@]
540 541 542 543
 *
 * Remove the file specification from a path.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
544
 *  lpszPath [I/O] Path to remove the file spec from
545 546 547 548
 *
 * RETURNS
 *  TRUE  If the path was valid and modified
 *  FALSE Otherwise
549 550 551
 */
BOOL WINAPI PathRemoveFileSpecA(LPSTR lpszPath)
{
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  LPSTR lpszFileSpec = lpszPath;
  BOOL bModified = FALSE;

  TRACE("(%s)\n",debugstr_a(lpszPath));

  if(lpszPath)
  {
    /* Skip directory or UNC path */
    if (*lpszPath == '\\')
      lpszFileSpec = ++lpszPath;
    if (*lpszPath == '\\')
      lpszFileSpec = ++lpszPath;

    while (*lpszPath)
    {
      if(*lpszPath == '\\')
        lpszFileSpec = lpszPath; /* Skip dir */
      else if(*lpszPath == ':')
      {
        lpszFileSpec = ++lpszPath; /* Skip drive */
        if (*lpszPath == '\\')
          lpszFileSpec++;
      }
      if (!(lpszPath = CharNextA(lpszPath)))
        break;
    }

    if (*lpszFileSpec)
    {
      *lpszFileSpec = '\0';
      bModified = TRUE;
    }
  }
  return bModified;
586 587 588 589
}

/*************************************************************************
 * PathRemoveFileSpecW	[SHLWAPI.@]
590 591
 *
 * See PathRemoveFileSpecA.
592 593 594
 */
BOOL WINAPI PathRemoveFileSpecW(LPWSTR lpszPath)
{
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  LPWSTR lpszFileSpec = lpszPath;
  BOOL bModified = FALSE;

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

  if(lpszPath)
  {
    /* Skip directory or UNC path */
    if (*lpszPath == '\\')
      lpszFileSpec = ++lpszPath;
    if (*lpszPath == '\\')
      lpszFileSpec = ++lpszPath;

    while (*lpszPath)
    {
      if(*lpszPath == '\\')
        lpszFileSpec = lpszPath; /* Skip dir */
      else if(*lpszPath == ':')
      {
        lpszFileSpec = ++lpszPath; /* Skip drive */
        if (*lpszPath == '\\')
          lpszFileSpec++;
      }
618
      lpszPath++;
619 620 621 622 623 624 625 626 627
    }

    if (*lpszFileSpec)
    {
      *lpszFileSpec = '\0';
      bModified = TRUE;
    }
  }
  return bModified;
628 629 630
}

/*************************************************************************
631
 * PathStripPathA	[SHLWAPI.@]
632 633 634 635
 *
 * Remove the initial path from the beginning of a filename
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
636
 *  lpszPath [I/O] Path to remove the initial path from
637 638 639
 *
 * RETURNS
 *  Nothing.
640 641 642
 */
void WINAPI PathStripPathA(LPSTR lpszPath)
{
643
  TRACE("(%s)\n", debugstr_a(lpszPath));
644

645 646 647 648 649 650
  if (lpszPath)
  {
    LPSTR lpszFileName = PathFindFileNameA(lpszPath);
    if(lpszFileName)
      RtlMoveMemory(lpszPath, lpszFileName, strlen(lpszFileName)+1);
  }
651 652 653
}

/*************************************************************************
654
 * PathStripPathW	[SHLWAPI.@]
655 656
 *
 * See PathStripPathA.
657 658 659
 */
void WINAPI PathStripPathW(LPWSTR lpszPath)
{
660
  LPWSTR lpszFileName;
661

662 663 664 665
  TRACE("(%s)\n", debugstr_w(lpszPath));
  lpszFileName = PathFindFileNameW(lpszPath);
  if(lpszFileName)
    RtlMoveMemory(lpszPath, lpszFileName, (strlenW(lpszFileName)+1)*sizeof(WCHAR));
666 667 668 669
}

/*************************************************************************
 * PathStripToRootA	[SHLWAPI.@]
670 671 672 673
 *
 * Reduce a path to its root.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
674
 *  lpszPath [I/O] the path to reduce
675 676 677 678
 *
 * RETURNS
 *  Success: TRUE if the stripped path is a root path
 *  Failure: FALSE if the path cannot be stripped or is NULL
679 680 681
 */
BOOL WINAPI PathStripToRootA(LPSTR lpszPath)
{
682
  TRACE("(%s)\n", debugstr_a(lpszPath));
683

684 685 686 687 688 689
  if (!lpszPath)
    return FALSE;
  while(!PathIsRootA(lpszPath))
    if (!PathRemoveFileSpecA(lpszPath))
      return FALSE;
  return TRUE;
690 691 692 693
}

/*************************************************************************
 * PathStripToRootW	[SHLWAPI.@]
694 695
 *
 * See PathStripToRootA.
696 697 698
 */
BOOL WINAPI PathStripToRootW(LPWSTR lpszPath)
{
699
  TRACE("(%s)\n", debugstr_w(lpszPath));
700

701 702 703 704 705 706
  if (!lpszPath)
    return FALSE;
  while(!PathIsRootW(lpszPath))
    if (!PathRemoveFileSpecW(lpszPath))
      return FALSE;
  return TRUE;
707 708 709 710 711
}

/*************************************************************************
 * PathRemoveArgsA	[SHLWAPI.@]
 *
712
 * Strip space separated arguments from a path.
713 714
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
715
 *  lpszPath [I/O] Path to remove arguments from
716 717 718
 *
 * RETURNS
 *  Nothing.
719 720 721
 */
void WINAPI PathRemoveArgsA(LPSTR lpszPath)
{
722 723 724 725 726 727 728 729 730 731 732 733 734 735
  TRACE("(%s)\n",debugstr_a(lpszPath));

  if(lpszPath)
  {
    LPSTR lpszArgs = PathGetArgsA(lpszPath);
    if (*lpszArgs)
      lpszArgs[-1] = '\0';
    else
    {
      LPSTR lpszLastChar = CharPrevA(lpszPath, lpszArgs);
      if(*lpszLastChar == ' ')
        *lpszLastChar = '\0';
    }
  }
736 737 738 739
}

/*************************************************************************
 * PathRemoveArgsW	[SHLWAPI.@]
740 741
 *
 * See PathRemoveArgsA.
742 743 744
 */
void WINAPI PathRemoveArgsW(LPWSTR lpszPath)
{
745
  TRACE("(%s)\n",debugstr_w(lpszPath));
746

747 748 749
  if(lpszPath)
  {
    LPWSTR lpszArgs = PathGetArgsW(lpszPath);
750
    if (*lpszArgs || (lpszArgs > lpszPath && lpszArgs[-1] == ' '))
751 752
      lpszArgs[-1] = '\0';
  }
753 754 755 756
}

/*************************************************************************
 * PathRemoveExtensionA		[SHLWAPI.@]
757 758 759 760
 *
 * Remove the file extension from a path
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
761
 *  lpszPath [I/O] Path to remove the extension from
762 763 764
 *
 * RETURNS
 *  Nothing.
765 766 767
 */
void WINAPI PathRemoveExtensionA(LPSTR lpszPath)
{
768
  TRACE("(%s)\n", debugstr_a(lpszPath));
769

770 771 772 773 774
  if (lpszPath)
  {
    lpszPath = PathFindExtensionA(lpszPath);
    *lpszPath = '\0';
  }
775 776 777 778
}

/*************************************************************************
 * PathRemoveExtensionW		[SHLWAPI.@]
779 780 781
 *
 * See PathRemoveExtensionA.
*/
782 783
void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
{
784
  TRACE("(%s)\n", debugstr_w(lpszPath));
785

786 787 788 789 790
  if (lpszPath)
  {
    lpszPath = PathFindExtensionW(lpszPath);
    *lpszPath = '\0';
  }
791 792 793 794 795
}

/*************************************************************************
 * PathRemoveBackslashA	[SHLWAPI.@]
 *
796
 * Remove a trailing backslash from a path.
797
 *
798
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
799
 *  lpszPath [I/O] Path to remove backslash from
800 801 802 803
 *
 * RETURNS
 *  Success: A pointer to the end of the path
 *  Failure: NULL, if lpszPath is NULL
804 805 806
 */
LPSTR WINAPI PathRemoveBackslashA( LPSTR lpszPath )
{
807 808 809 810 811 812 813 814 815 816 817
  LPSTR szTemp = NULL;

  TRACE("(%s)\n", debugstr_a(lpszPath));

  if(lpszPath)
  {
    szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
    if (!PathIsRootA(lpszPath) && *szTemp == '\\')
      *szTemp = '\0';
  }
  return szTemp;
818 819 820 821
}

/*************************************************************************
 * PathRemoveBackslashW	[SHLWAPI.@]
822 823
 *
 * See PathRemoveBackslashA.
824 825 826
 */
LPWSTR WINAPI PathRemoveBackslashW( LPWSTR lpszPath )
{
827
  LPWSTR szTemp = NULL;
828

829
  TRACE("(%s)\n", debugstr_w(lpszPath));
830

831 832
  if(lpszPath)
  {
833 834
    szTemp = lpszPath + strlenW(lpszPath);
    if (szTemp > lpszPath) szTemp--;
835 836 837 838 839
    if (!PathIsRootW(lpszPath) && *szTemp == '\\')
      *szTemp = '\0';
  }
  return szTemp;
}
840 841 842

/*************************************************************************
 * PathRemoveBlanksA [SHLWAPI.@]
843 844 845 846
 *
 * Remove Spaces from the start and end of a path.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
847
 *  lpszPath [I/O] Path to strip blanks from
848 849 850
 *
 * RETURNS
 *  Nothing.
851
 */
852
VOID WINAPI PathRemoveBlanksA(LPSTR lpszPath)
853
{
854
  TRACE("(%s)\n", debugstr_a(lpszPath));
855

856 857 858
  if(lpszPath && *lpszPath)
  {
    LPSTR start = lpszPath;
859

860 861 862 863 864 865 866 867 868 869 870
    while (*lpszPath == ' ')
      lpszPath = CharNextA(lpszPath);

    while(*lpszPath)
      *start++ = *lpszPath++;

    if (start != lpszPath)
      while (start[-1] == ' ')
        start--;
    *start = '\0';
  }
871 872 873 874
}

/*************************************************************************
 * PathRemoveBlanksW [SHLWAPI.@]
875 876
 *
 * See PathRemoveBlanksA.
877
 */
878
VOID WINAPI PathRemoveBlanksW(LPWSTR lpszPath)
879
{
880
  TRACE("(%s)\n", debugstr_w(lpszPath));
881

882 883 884
  if(lpszPath && *lpszPath)
  {
    LPWSTR start = lpszPath;
885

886 887 888 889 890 891 892 893 894 895 896
    while (*lpszPath == ' ')
      lpszPath++;

    while(*lpszPath)
      *start++ = *lpszPath++;

    if (start != lpszPath)
      while (start[-1] == ' ')
        start--;
    *start = '\0';
  }
897 898 899 900
}

/*************************************************************************
 * PathQuoteSpacesA [SHLWAPI.@]
901
 *
Austin English's avatar
Austin English committed
902
 * Surround a path containing spaces in quotes.
903 904
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
905
 *  lpszPath [I/O] Path to quote
906 907 908 909 910 911
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  The path is not changed if it is invalid or has no spaces.
912
 */
913
VOID WINAPI PathQuoteSpacesA(LPSTR lpszPath)
914
{
915
  TRACE("(%s)\n", debugstr_a(lpszPath));
916

917 918
  if(lpszPath && StrChrA(lpszPath,' '))
  {
Jon Griffiths's avatar
Jon Griffiths committed
919
    size_t iLen = strlen(lpszPath) + 1;
920 921 922 923 924 925 926 927 928

    if (iLen + 2 < MAX_PATH)
    {
      memmove(lpszPath + 1, lpszPath, iLen);
      lpszPath[0] = '"';
      lpszPath[iLen] = '"';
      lpszPath[iLen + 1] = '\0';
    }
  }
929 930 931 932
}

/*************************************************************************
 * PathQuoteSpacesW [SHLWAPI.@]
933 934
 *
 * See PathQuoteSpacesA.
935
 */
936
VOID WINAPI PathQuoteSpacesW(LPWSTR lpszPath)
937
{
938
  TRACE("(%s)\n", debugstr_w(lpszPath));
939

940 941 942 943 944 945 946 947 948 949 950 951
  if(lpszPath && StrChrW(lpszPath,' '))
  {
    int iLen = strlenW(lpszPath) + 1;

    if (iLen + 2 < MAX_PATH)
    {
      memmove(lpszPath + 1, lpszPath, iLen * sizeof(WCHAR));
      lpszPath[0] = '"';
      lpszPath[iLen] = '"';
      lpszPath[iLen + 1] = '\0';
    }
  }
952 953 954 955
}

/*************************************************************************
 * PathUnquoteSpacesA [SHLWAPI.@]
956 957 958 959
 *
 * Remove quotes ("") from around a path, if present.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
960
 *  lpszPath [I/O] Path to strip quotes from
961 962 963 964
 *
 * RETURNS
 *  Nothing
 *
965
 * NOTES
966 967 968
 *  If the path contains a single quote only, an empty string will result.
 *  Otherwise quotes are only removed if they appear at the start and end
 *  of the path.
969
 */
970
VOID WINAPI PathUnquoteSpacesA(LPSTR lpszPath)
971
{
972
  TRACE("(%s)\n", debugstr_a(lpszPath));
973

974 975 976
  if (lpszPath && *lpszPath == '"')
  {
    DWORD dwLen = strlen(lpszPath) - 1;
977

978 979 980 981 982 983 984
    if (lpszPath[dwLen] == '"')
    {
      lpszPath[dwLen] = '\0';
      for (; *lpszPath; lpszPath++)
        *lpszPath = lpszPath[1];
    }
  }
985 986 987 988
}

/*************************************************************************
 * PathUnquoteSpacesW [SHLWAPI.@]
989 990
 *
 * See PathUnquoteSpacesA.
991
 */
992
VOID WINAPI PathUnquoteSpacesW(LPWSTR lpszPath)
993
{
994
  TRACE("(%s)\n", debugstr_w(lpszPath));
995

996 997 998
  if (lpszPath && *lpszPath == '"')
  {
    DWORD dwLen = strlenW(lpszPath) - 1;
999

1000 1001 1002 1003 1004 1005 1006
    if (lpszPath[dwLen] == '"')
    {
      lpszPath[dwLen] = '\0';
      for (; *lpszPath; lpszPath++)
        *lpszPath = lpszPath[1];
    }
  }
1007 1008 1009
}

/*************************************************************************
1010 1011 1012 1013 1014
 * PathParseIconLocationA  [SHLWAPI.@]
 *
 * Parse the location of an icon from a path.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1015
 *  lpszPath [I/O] The path to parse the icon location from.
1016 1017 1018 1019 1020 1021 1022 1023
 *
 * RETURNS
 *  Success: The number of the icon
 *  Failure: 0 if the path does not contain an icon location or is NULL
 *
 * NOTES
 *  The path has surrounding quotes and spaces removed regardless
 *  of whether the call succeeds or not.
1024 1025 1026
 */
int WINAPI PathParseIconLocationA(LPSTR lpszPath)
{
1027 1028
  int iRet = 0;
  LPSTR lpszComma;
1029

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (lpszPath)
  {
    if ((lpszComma = strchr(lpszPath, ',')))
    {
      *lpszComma++ = '\0';
      iRet = StrToIntA(lpszComma);
    }
    PathUnquoteSpacesA(lpszPath);
    PathRemoveBlanksA(lpszPath);
  }
  return iRet;
1043 1044 1045
}

/*************************************************************************
1046 1047 1048
 * PathParseIconLocationW  [SHLWAPI.@]
 *
 * See PathParseIconLocationA.
1049 1050 1051
 */
int WINAPI PathParseIconLocationW(LPWSTR lpszPath)
{
1052 1053
  int iRet = 0;
  LPWSTR lpszComma;
1054

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (lpszPath)
  {
    if ((lpszComma = StrChrW(lpszPath, ',')))
    {
      *lpszComma++ = '\0';
      iRet = StrToIntW(lpszComma);
    }
    PathUnquoteSpacesW(lpszPath);
    PathRemoveBlanksW(lpszPath);
  }
  return iRet;
1068 1069
}

1070
/*************************************************************************
1071
 * @	[SHLWAPI.4]
1072
 *
1073
 * Unicode version of PathFileExistsDefExtA.
1074
 */
1075
BOOL WINAPI PathFileExistsDefExtW(LPWSTR lpszPath,DWORD dwWhich)
1076
{
1077 1078 1079 1080 1081 1082 1083
  static const WCHAR pszExts[7][5] = { { '.', 'p', 'i', 'f', 0},
                                       { '.', 'c', 'o', 'm', 0},
                                       { '.', 'e', 'x', 'e', 0},
                                       { '.', 'b', 'a', 't', 0},
                                       { '.', 'l', 'n', 'k', 0},
                                       { '.', 'c', 'm', 'd', 0},
                                       { 0, 0, 0, 0, 0} };
1084

1085
  TRACE("(%s,%d)\n", debugstr_w(lpszPath), dwWhich);
1086 1087 1088 1089 1090 1091 1092 1093 1094

  if (!lpszPath || PathIsUNCServerW(lpszPath) || PathIsUNCServerShareW(lpszPath))
    return FALSE;

  if (dwWhich)
  {
    LPCWSTR szExt = PathFindExtensionW(lpszPath);
    if (!*szExt || dwWhich & 0x40)
    {
1095
      size_t iChoose = 0;
1096 1097 1098
      int iLen = lstrlenW(lpszPath);
      if (iLen > (MAX_PATH - 5))
        return FALSE;
1099
      while ( (dwWhich & 0x1) && pszExts[iChoose][0] )
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
      {
        lstrcpyW(lpszPath + iLen, pszExts[iChoose]);
        if (PathFileExistsW(lpszPath))
          return TRUE;
        iChoose++;
        dwWhich >>= 1;
      }
      *(lpszPath + iLen) = (WCHAR)'\0';
      return FALSE;
    }
  }
  return PathFileExistsW(lpszPath);
}

1114 1115 1116 1117 1118 1119
/*************************************************************************
 * @	[SHLWAPI.3]
 *
 * Determine if a file exists locally and is of an executable type.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1120 1121
 *  lpszPath       [I/O] File to search for
 *  dwWhich        [I]   Type of executable to search for
1122 1123
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1124
 *  TRUE  If the file was found. lpszPath contains the file name.
1125 1126 1127 1128
 *  FALSE Otherwise.
 *
 * NOTES
 *  lpszPath is modified in place and must be at least MAX_PATH in length.
1129
 *  If the function returns FALSE, the path is modified to its original state.
1130 1131 1132 1133
 *  If the given path contains an extension or dwWhich is 0, executable
 *  extensions are not checked.
 *
 *  Ordinals 3-6 are a classic case of MS exposing limited functionality to
Jon Griffiths's avatar
Jon Griffiths committed
1134
 *  users (here through PathFindOnPathA()) and keeping advanced functionality for
1135 1136
 *  their own developers exclusive use. Monopoly, anyone?
 */
1137
BOOL WINAPI PathFileExistsDefExtA(LPSTR lpszPath,DWORD dwWhich)
1138 1139 1140
{
  BOOL bRet = FALSE;

1141
  TRACE("(%s,%d)\n", debugstr_a(lpszPath), dwWhich);
1142 1143 1144 1145

  if (lpszPath)
  {
    WCHAR szPath[MAX_PATH];
1146
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1147
    bRet = PathFileExistsDefExtW(szPath, dwWhich);
1148
    if (bRet)
1149
      WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
1150 1151 1152 1153
  }
  return bRet;
}

1154 1155 1156 1157 1158
/*************************************************************************
 * SHLWAPI_PathFindInOtherDirs
 *
 * Internal helper for SHLWAPI_PathFindOnPathExA/W.
 */
1159
static BOOL SHLWAPI_PathFindInOtherDirs(LPWSTR lpszFile, DWORD dwWhich)
1160
{
1161 1162
  static const WCHAR szSystem[] = { 'S','y','s','t','e','m','\0'};
  static const WCHAR szPath[] = { 'P','A','T','H','\0'};
1163 1164 1165 1166 1167
  DWORD dwLenPATH;
  LPCWSTR lpszCurr;
  WCHAR *lpszPATH;
  WCHAR buff[MAX_PATH];

1168
  TRACE("(%s,%08x)\n", debugstr_w(lpszFile), dwWhich);
1169 1170 1171 1172 1173

  /* Try system directories */
  GetSystemDirectoryW(buff, MAX_PATH);
  if (!PathAppendW(buff, lpszFile))
     return FALSE;
1174
  if (PathFileExistsDefExtW(buff, dwWhich))
1175 1176 1177 1178 1179 1180 1181
  {
    strcpyW(lpszFile, buff);
    return TRUE;
  }
  GetWindowsDirectoryW(buff, MAX_PATH);
  if (!PathAppendW(buff, szSystem ) || !PathAppendW(buff, lpszFile))
    return FALSE;
1182
  if (PathFileExistsDefExtW(buff, dwWhich))
1183 1184 1185 1186 1187 1188 1189
  {
    strcpyW(lpszFile, buff);
    return TRUE;
  }
  GetWindowsDirectoryW(buff, MAX_PATH);
  if (!PathAppendW(buff, lpszFile))
    return FALSE;
1190
  if (PathFileExistsDefExtW(buff, dwWhich))
1191 1192 1193 1194 1195 1196 1197
  {
    strcpyW(lpszFile, buff);
    return TRUE;
  }
  /* Try dirs listed in %PATH% */
  dwLenPATH = GetEnvironmentVariableW(szPath, buff, MAX_PATH);

1198
  if (!dwLenPATH || !(lpszPATH = HeapAlloc(GetProcessHeap(), 0, (dwLenPATH + 1) * sizeof (WCHAR))))
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
    return FALSE;

  GetEnvironmentVariableW(szPath, lpszPATH, dwLenPATH + 1);
  lpszCurr = lpszPATH;
  while (lpszCurr)
  {
    LPCWSTR lpszEnd = lpszCurr;
    LPWSTR pBuff = buff;

    while (*lpszEnd == ' ')
      lpszEnd++;
    while (*lpszEnd && *lpszEnd != ';')
      *pBuff++ = *lpszEnd++;
    *pBuff = '\0';

    if (*lpszEnd)
      lpszCurr = lpszEnd + 1;
    else
      lpszCurr = NULL; /* Last Path, terminate after this */

    if (!PathAppendW(buff, lpszFile))
Mike McCormack's avatar
Mike McCormack committed
1220
    {
1221
      HeapFree(GetProcessHeap(), 0, lpszPATH);
1222
      return FALSE;
Mike McCormack's avatar
Mike McCormack committed
1223
    }
1224
    if (PathFileExistsDefExtW(buff, dwWhich))
1225 1226
    {
      strcpyW(lpszFile, buff);
1227
      HeapFree(GetProcessHeap(), 0, lpszPATH);
1228 1229 1230
      return TRUE;
    }
  }
1231
  HeapFree(GetProcessHeap(), 0, lpszPATH);
1232 1233 1234 1235
  return FALSE;
}

/*************************************************************************
1236
 * @	[SHLWAPI.5]
1237
 *
1238 1239 1240
 * Search a range of paths for a specific type of executable.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1241 1242 1243
 *  lpszFile       [I/O] File to search for
 *  lppszOtherDirs [I]   Other directories to look in
 *  dwWhich        [I]   Type of executable to search for
1244 1245
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1246
 *  Success: TRUE. The path to the executable is stored in lpszFile.
1247
 *  Failure: FALSE. The path to the executable is unchanged.
1248
 */
1249
BOOL WINAPI PathFindOnPathExA(LPSTR lpszFile,LPCSTR *lppszOtherDirs,DWORD dwWhich)
1250
{
1251 1252 1253
  WCHAR szFile[MAX_PATH];
  WCHAR buff[MAX_PATH];

1254
  TRACE("(%s,%p,%08x)\n", debugstr_a(lpszFile), lppszOtherDirs, dwWhich);
1255 1256 1257 1258

  if (!lpszFile || !PathIsFileSpecA(lpszFile))
    return FALSE;

1259
  MultiByteToWideChar(CP_ACP,0,lpszFile,-1,szFile,MAX_PATH);
1260 1261 1262 1263 1264 1265 1266 1267 1268

  /* Search provided directories first */
  if (lppszOtherDirs && *lppszOtherDirs)
  {
    WCHAR szOther[MAX_PATH];
    LPCSTR *lpszOtherPath = lppszOtherDirs;

    while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
    {
1269
      MultiByteToWideChar(CP_ACP,0,*lpszOtherPath,-1,szOther,MAX_PATH);
1270
      PathCombineW(buff, szOther, szFile);
1271
      if (PathFileExistsDefExtW(buff, dwWhich))
1272
      {
1273
        WideCharToMultiByte(CP_ACP,0,buff,-1,lpszFile,MAX_PATH,0,0);
1274 1275 1276 1277 1278 1279 1280 1281
        return TRUE;
      }
      lpszOtherPath++;
    }
  }
  /* Not found, try system and path dirs */
  if (SHLWAPI_PathFindInOtherDirs(szFile, dwWhich))
  {
1282
    WideCharToMultiByte(CP_ACP,0,szFile,-1,lpszFile,MAX_PATH,0,0);
1283 1284 1285
    return TRUE;
  }
  return FALSE;
1286 1287 1288
}

/*************************************************************************
1289
 * @	[SHLWAPI.6]
1290
 *
1291
 * Unicode version of PathFindOnPathExA.
1292
 */
1293
BOOL WINAPI PathFindOnPathExW(LPWSTR lpszFile,LPCWSTR *lppszOtherDirs,DWORD dwWhich)
1294
{
1295 1296
  WCHAR buff[MAX_PATH];

1297
  TRACE("(%s,%p,%08x)\n", debugstr_w(lpszFile), lppszOtherDirs, dwWhich);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

  if (!lpszFile || !PathIsFileSpecW(lpszFile))
    return FALSE;

  /* Search provided directories first */
  if (lppszOtherDirs && *lppszOtherDirs)
  {
    LPCWSTR *lpszOtherPath = lppszOtherDirs;
    while (lpszOtherPath && *lpszOtherPath && (*lpszOtherPath)[0])
    {
      PathCombineW(buff, *lpszOtherPath, lpszFile);
1309
      if (PathFileExistsDefExtW(buff, dwWhich))
1310 1311 1312 1313 1314 1315 1316 1317 1318
      {
        strcpyW(lpszFile, buff);
        return TRUE;
      }
      lpszOtherPath++;
    }
  }
  /* Not found, try system and path dirs */
  return SHLWAPI_PathFindInOtherDirs(lpszFile, dwWhich);
1319 1320 1321
}

/*************************************************************************
1322 1323 1324 1325 1326
 * PathFindOnPathA	[SHLWAPI.@]
 *
 * Search a range of paths for an executable.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1327 1328
 *  lpszFile       [I/O] File to search for
 *  lppszOtherDirs [I]   Other directories to look in
1329 1330 1331 1332
 *
 * RETURNS
 *  Success: TRUE. The path to the executable is stored in lpszFile.
 *  Failure: FALSE. The path to the executable is unchanged.
1333
 */
1334
BOOL WINAPI PathFindOnPathA(LPSTR lpszFile, LPCSTR *lppszOtherDirs)
1335
{
1336
  TRACE("(%s,%p)\n", debugstr_a(lpszFile), lppszOtherDirs);
1337
  return PathFindOnPathExA(lpszFile, lppszOtherDirs, 0);
1338
 }
1339 1340

/*************************************************************************
1341 1342 1343
 * PathFindOnPathW      [SHLWAPI.@]
 *
 * See PathFindOnPathA.
1344
 */
1345
BOOL WINAPI PathFindOnPathW(LPWSTR lpszFile, LPCWSTR *lppszOtherDirs)
1346
{
1347
  TRACE("(%s,%p)\n", debugstr_w(lpszFile), lppszOtherDirs);
1348
  return PathFindOnPathExW(lpszFile,lppszOtherDirs, 0);
1349 1350 1351
}

/*************************************************************************
1352 1353
 * PathCompactPathExA   [SHLWAPI.@]
 *
1354
 * Compact a path into a given number of characters.
1355 1356 1357 1358
 *
 * PARAMS
 *  lpszDest [O] Destination for compacted path
 *  lpszPath [I] Source path
Jon Griffiths's avatar
Jon Griffiths committed
1359
 *  cchMax   [I] Maximum size of compacted path
1360
 *  dwFlags  [I] Reserved
1361 1362
 *
 * RETURNS
1363 1364 1365 1366 1367
 *  Success: TRUE. The compacted path is written to lpszDest.
 *  Failure: FALSE. lpszPath is undefined.
 *
 * NOTES
 *  If cchMax is given as 0, lpszDest will still be NUL terminated.
Jon Griffiths's avatar
Jon Griffiths committed
1368
 *
1369 1370 1371
 *  The Win32 version of this function contains a bug: When cchMax == 7,
 *  8 bytes will be written to lpszDest. This bug is fixed in the Wine
 *  implementation.
Jon Griffiths's avatar
Jon Griffiths committed
1372
 *
1373
 *  Some relative paths will be different when cchMax == 5 or 6. This occurs
Jon Griffiths's avatar
Jon Griffiths committed
1374
 *  because Win32 will insert a "\" in lpszDest, even if one is
1375
 *  not present in the original path.
1376
 */
1377 1378
BOOL WINAPI PathCompactPathExA(LPSTR lpszDest, LPCSTR lpszPath,
                               UINT cchMax, DWORD dwFlags)
1379
{
1380 1381
  BOOL bRet = FALSE;

1382
  TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_a(lpszPath), cchMax, dwFlags);
1383 1384 1385 1386 1387

  if (lpszPath && lpszDest)
  {
    WCHAR szPath[MAX_PATH];
    WCHAR szDest[MAX_PATH];
1388

1389
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
1390 1391
    szDest[0] = '\0';
    bRet = PathCompactPathExW(szDest, szPath, cchMax, dwFlags);
1392
    WideCharToMultiByte(CP_ACP,0,szDest,-1,lpszDest,MAX_PATH,0,0);
1393 1394
  }
  return bRet;
1395 1396 1397
}

/*************************************************************************
1398 1399 1400
 * PathCompactPathExW   [SHLWAPI.@]
 *
 * See PathCompactPathExA.
1401
 */
1402 1403
BOOL WINAPI PathCompactPathExW(LPWSTR lpszDest, LPCWSTR lpszPath,
                               UINT cchMax, DWORD dwFlags)
1404
{
1405 1406 1407 1408
  static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
  LPCWSTR lpszFile;
  DWORD dwLen, dwFileLen = 0;

1409
  TRACE("(%p,%s,%d,0x%08x)\n", lpszDest, debugstr_w(lpszPath), cchMax, dwFlags);
1410 1411 1412 1413 1414 1415 1416 1417 1418

  if (!lpszPath)
    return FALSE;

  if (!lpszDest)
  {
    WARN("Invalid lpszDest would crash under Win32!\n");
    return FALSE;
  }
1419

1420
  *lpszDest = '\0';
1421

1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
  if (cchMax < 2)
    return TRUE;

  dwLen = strlenW(lpszPath) + 1;

  if (dwLen < cchMax)
  {
    /* Don't need to compact */
    memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
    return TRUE;
  }

  /* Path must be compacted to fit into lpszDest */
  lpszFile = PathFindFileNameW(lpszPath);
  dwFileLen = lpszPath + dwLen - lpszFile;

  if (dwFileLen == dwLen)
  {
    /* No root in psth */
    if (cchMax <= 4)
    {
      while (--cchMax > 0) /* No room left for anything but ellipses */
        *lpszDest++ = '.';
      *lpszDest = '\0';
      return TRUE;
    }
    /* Compact the file name with ellipses at the end */
    cchMax -= 4;
    memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
    strcpyW(lpszDest + cchMax, szEllipses);
    return TRUE;
  }
  /* We have a root in the path */
Francois Gouget's avatar
Francois Gouget committed
1455
  lpszFile--; /* Start compacted filename with the path separator */
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
  dwFileLen++;

  if (dwFileLen + 3 > cchMax)
  {
    /* Compact the file name */
    if (cchMax <= 4)
    {
      while (--cchMax > 0) /* No room left for anything but ellipses */
        *lpszDest++ = '.';
      *lpszDest = '\0';
      return TRUE;
    }
    strcpyW(lpszDest, szEllipses);
    lpszDest += 3;
    cchMax -= 4;
    *lpszDest++ = *lpszFile++;
    if (cchMax <= 4)
    {
      while (--cchMax > 0) /* No room left for anything but ellipses */
        *lpszDest++ = '.';
      *lpszDest = '\0';
      return TRUE;
    }
    cchMax -= 4;
    memcpy(lpszDest, lpszFile, cchMax * sizeof(WCHAR));
    strcpyW(lpszDest + cchMax, szEllipses);
    return TRUE;
  }

  /* Only the root needs to be Compacted */
  dwLen = cchMax - dwFileLen - 3;
  memcpy(lpszDest, lpszPath, dwLen * sizeof(WCHAR));
  strcpyW(lpszDest + dwLen, szEllipses);
  strcpyW(lpszDest + dwLen + 3, lpszFile);
  return TRUE;
1491 1492 1493
}

/*************************************************************************
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
 * PathIsRelativeA	[SHLWAPI.@]
 *
 * Determine if a path is a relative path.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
 *  TRUE:  The path is relative, or is invalid.
 *  FALSE: The path is not relative.
1504 1505 1506
 */
BOOL WINAPI PathIsRelativeA (LPCSTR lpszPath)
{
1507
  TRACE("(%s)\n",debugstr_a(lpszPath));
1508

1509 1510 1511 1512 1513
  if (!lpszPath || !*lpszPath || IsDBCSLeadByte(*lpszPath))
    return TRUE;
  if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
    return FALSE;
  return TRUE;
1514 1515 1516 1517
}

/*************************************************************************
 *  PathIsRelativeW	[SHLWAPI.@]
1518 1519
 *
 * See PathIsRelativeA.
1520 1521 1522
 */
BOOL WINAPI PathIsRelativeW (LPCWSTR lpszPath)
{
1523
  TRACE("(%s)\n",debugstr_w(lpszPath));
1524

1525 1526 1527 1528 1529
  if (!lpszPath || !*lpszPath)
    return TRUE;
  if (*lpszPath == '\\' || (*lpszPath && lpszPath[1] == ':'))
    return FALSE;
  return TRUE;
1530 1531 1532 1533 1534
}

/*************************************************************************
 * PathIsRootA		[SHLWAPI.@]
 *
1535 1536 1537 1538 1539 1540
 * Determine if a path is a root path.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1541
 *  TRUE  If lpszPath is valid and a root path,
1542
 *  FALSE Otherwise
1543 1544 1545
 */
BOOL WINAPI PathIsRootA(LPCSTR lpszPath)
{
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (lpszPath && *lpszPath)
  {
    if (*lpszPath == '\\')
    {
      if (!lpszPath[1])
        return TRUE; /* \ */
      else if (lpszPath[1]=='\\')
      {
        BOOL bSeenSlash = FALSE;
        lpszPath += 2;

        /* Check for UNC root path */
        while (*lpszPath)
        {
          if (*lpszPath == '\\')
          {
            if (bSeenSlash)
              return FALSE;
            bSeenSlash = TRUE;
          }
          lpszPath = CharNextA(lpszPath);
        }
        return TRUE;
      }
    }
    else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
      return TRUE; /* X:\ */
  }
  return FALSE;
}
1578 1579 1580

/*************************************************************************
 * PathIsRootW		[SHLWAPI.@]
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
 *
 * See PathIsRootA.
 */
BOOL WINAPI PathIsRootW(LPCWSTR lpszPath)
{
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (lpszPath && *lpszPath)
  {
    if (*lpszPath == '\\')
    {
      if (!lpszPath[1])
        return TRUE; /* \ */
      else if (lpszPath[1]=='\\')
      {
        BOOL bSeenSlash = FALSE;
        lpszPath += 2;

        /* Check for UNC root path */
        while (*lpszPath)
        {
          if (*lpszPath == '\\')
          {
            if (bSeenSlash)
              return FALSE;
            bSeenSlash = TRUE;
          }
1608
          lpszPath++;
1609 1610 1611 1612 1613 1614 1615 1616
        }
        return TRUE;
      }
    }
    else if (lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
      return TRUE; /* X:\ */
  }
  return FALSE;
1617 1618 1619 1620
}

/*************************************************************************
 * PathIsDirectoryA	[SHLWAPI.@]
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
 *
 * Determine if a path is a valid directory
 *
 * PARAMS
 *  lpszPath [I] Path to check.
 *
 * RETURNS
 *  FILE_ATTRIBUTE_DIRECTORY if lpszPath exists and can be read (See Notes)
 *  FALSE if lpszPath is invalid or not a directory.
 *
 * NOTES
 *  Although this function is prototyped as returning a BOOL, it returns
 *  FILE_ATTRIBUTE_DIRECTORY for success. This means that code such as:
 *
Jon Griffiths's avatar
Jon Griffiths committed
1635 1636
 *|  if (PathIsDirectoryA("c:\\windows\\") == TRUE)
 *|    ...
1637 1638
 *
 *  will always fail.
1639 1640 1641
 */
BOOL WINAPI PathIsDirectoryA(LPCSTR lpszPath)
{
1642
  DWORD dwAttr;
1643

1644
  TRACE("(%s)\n", debugstr_a(lpszPath));
1645

1646 1647 1648
  if (!lpszPath || PathIsUNCServerA(lpszPath))
    return FALSE;

Jon Griffiths's avatar
Jon Griffiths committed
1649 1650 1651 1652 1653
  if (PathIsUNCServerShareA(lpszPath))
  {
    FIXME("UNC Server Share not yet supported - FAILING\n");
    return FALSE;
  }
1654

Jon Griffiths's avatar
Jon Griffiths committed
1655
  if ((dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1656 1657
    return FALSE;
  return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1658 1659 1660 1661
}

/*************************************************************************
 * PathIsDirectoryW	[SHLWAPI.@]
1662 1663
 *
 * See PathIsDirectoryA.
1664 1665 1666
 */
BOOL WINAPI PathIsDirectoryW(LPCWSTR lpszPath)
{
1667
  DWORD dwAttr;
1668

1669 1670 1671 1672 1673
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (!lpszPath || PathIsUNCServerW(lpszPath))
    return FALSE;

Jon Griffiths's avatar
Jon Griffiths committed
1674 1675 1676 1677 1678
  if (PathIsUNCServerShareW(lpszPath))
  {
    FIXME("UNC Server Share not yet supported - FAILING\n");
    return FALSE;
  }
1679

Jon Griffiths's avatar
Jon Griffiths committed
1680
  if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
1681 1682
    return FALSE;
  return dwAttr & FILE_ATTRIBUTE_DIRECTORY;
1683 1684 1685 1686
}

/*************************************************************************
 * PathFileExistsA	[SHLWAPI.@]
1687 1688 1689 1690 1691 1692 1693 1694 1695
 *
 * Determine if a file exists.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
 *  TRUE  If the file exists and is readable
 *  FALSE Otherwise
1696
 */
1697
BOOL WINAPI PathFileExistsA(LPCSTR lpszPath)
1698
{
1699 1700 1701 1702 1703 1704 1705 1706
  UINT iPrevErrMode;
  DWORD dwAttr;

  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (!lpszPath)
    return FALSE;

1707 1708
  /* Prevent a dialog box if path is on a disk that has been ejected. */
  iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1709 1710
  dwAttr = GetFileAttributesA(lpszPath);
  SetErrorMode(iPrevErrMode);
Jon Griffiths's avatar
Jon Griffiths committed
1711
  return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1712 1713 1714 1715
}

/*************************************************************************
 * PathFileExistsW	[SHLWAPI.@]
1716
 *
Jon Griffiths's avatar
Jon Griffiths committed
1717
 * See PathFileExistsA.
1718
 */
1719
BOOL WINAPI PathFileExistsW(LPCWSTR lpszPath)
1720
{
1721 1722 1723 1724 1725 1726 1727 1728
  UINT iPrevErrMode;
  DWORD dwAttr;

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

  if (!lpszPath)
    return FALSE;

1729
  iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1730 1731
  dwAttr = GetFileAttributesW(lpszPath);
  SetErrorMode(iPrevErrMode);
Jon Griffiths's avatar
Jon Griffiths committed
1732
  return dwAttr == INVALID_FILE_ATTRIBUTES ? FALSE : TRUE;
1733 1734
}

1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 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 1786 1787 1788 1789 1790 1791
/*************************************************************************
 * PathFileExistsAndAttributesA	[SHLWAPI.445]
 *
 * Determine if a file exists.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *  dwAttr   [O] attributes of file
 *
 * RETURNS
 *  TRUE  If the file exists and is readable
 *  FALSE Otherwise
 */
BOOL WINAPI PathFileExistsAndAttributesA(LPCSTR lpszPath, DWORD *dwAttr)
{
  UINT iPrevErrMode;
  DWORD dwVal = 0;

  TRACE("(%s %p)\n", debugstr_a(lpszPath), dwAttr);

  if (dwAttr)
    *dwAttr = INVALID_FILE_ATTRIBUTES;

  if (!lpszPath)
    return FALSE;

  iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  dwVal = GetFileAttributesA(lpszPath);
  SetErrorMode(iPrevErrMode);
  if (dwAttr)
    *dwAttr = dwVal;
  return (dwVal != INVALID_FILE_ATTRIBUTES);
}

/*************************************************************************
 * PathFileExistsAndAttributesW	[SHLWAPI.446]
 *
 * See PathFileExistsA.
 */
BOOL WINAPI PathFileExistsAndAttributesW(LPCWSTR lpszPath, DWORD *dwAttr)
{
  UINT iPrevErrMode;
  DWORD dwVal;

  TRACE("(%s %p)\n", debugstr_w(lpszPath), dwAttr);

  if (!lpszPath)
    return FALSE;

  iPrevErrMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  dwVal = GetFileAttributesW(lpszPath);
  SetErrorMode(iPrevErrMode);
  if (dwAttr)
    *dwAttr = dwVal;
  return (dwVal != INVALID_FILE_ATTRIBUTES);
}

1792 1793
/*************************************************************************
 * PathMatchSingleMaskA	[internal]
Jon Griffiths's avatar
Jon Griffiths committed
1794
 */
1795
static BOOL PathMatchSingleMaskA(LPCSTR name, LPCSTR mask)
Jon Griffiths's avatar
Jon Griffiths committed
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
{
  while (*name && *mask && *mask!=';')
  {
    if (*mask == '*')
    {
      do
      {
        if (PathMatchSingleMaskA(name,mask+1))
          return TRUE;  /* try substrings */
      } while (*name++);
      return FALSE;
    }

    if (toupper(*mask) != toupper(*name) && *mask != '?')
      return FALSE;

    name = CharNextA(name);
    mask = CharNextA(mask);
  }

  if (!*name)
  {
    while (*mask == '*')
      mask++;
    if (!*mask || *mask == ';')
      return TRUE;
  }
  return FALSE;
1824 1825 1826 1827 1828
}

/*************************************************************************
 * PathMatchSingleMaskW	[internal]
 */
1829
static BOOL PathMatchSingleMaskW(LPCWSTR name, LPCWSTR mask)
Jon Griffiths's avatar
Jon Griffiths committed
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
{
  while (*name && *mask && *mask != ';')
  {
    if (*mask == '*')
    {
      do
      {
        if (PathMatchSingleMaskW(name,mask+1))
          return TRUE;  /* try substrings */
      } while (*name++);
      return FALSE;
    }

    if (toupperW(*mask) != toupperW(*name) && *mask != '?')
      return FALSE;

1846 1847
    name++;
    mask++;
Jon Griffiths's avatar
Jon Griffiths committed
1848 1849 1850 1851 1852 1853 1854 1855 1856
  }
  if (!*name)
  {
    while (*mask == '*')
      mask++;
    if (!*mask || *mask == ';')
      return TRUE;
  }
  return FALSE;
1857
}
1858

1859 1860
/*************************************************************************
 * PathMatchSpecA	[SHLWAPI.@]
1861 1862 1863 1864 1865
 *
 * Determine if a path matches one or more search masks.
 *
 * PARAMS
 *  lpszPath [I] Path to check
Jon Griffiths's avatar
Jon Griffiths committed
1866
 *  lpszMask [I] Search mask(s)
1867 1868 1869 1870 1871
 *
 * RETURNS
 *  TRUE  If lpszPath is valid and is matched
 *  FALSE Otherwise
 *
1872
 * NOTES
Francois Gouget's avatar
Francois Gouget committed
1873
 *  Multiple search masks may be given if they are separated by ";". The
1874
 *  pattern "*.*" is treated specially in that it matches all paths (for
1875
 *  backwards compatibility with DOS).
1876
 */
Jon Griffiths's avatar
Jon Griffiths committed
1877
BOOL WINAPI PathMatchSpecA(LPCSTR lpszPath, LPCSTR lpszMask)
1878
{
Jon Griffiths's avatar
Jon Griffiths committed
1879 1880 1881 1882 1883 1884 1885
  TRACE("(%s,%s)\n", lpszPath, lpszMask);

  if (!lstrcmpA(lpszMask, "*.*"))
    return TRUE; /* Matches every path */

  while (*lpszMask)
  {
1886 1887 1888
    while (*lpszMask == ' ')
      lpszMask++; /* Eat leading spaces */

Jon Griffiths's avatar
Jon Griffiths committed
1889 1890
    if (PathMatchSingleMaskA(lpszPath, lpszMask))
      return TRUE; /* Matches the current mask */
1891

Jon Griffiths's avatar
Jon Griffiths committed
1892
    while (*lpszMask && *lpszMask != ';')
1893
      lpszMask = CharNextA(lpszMask); /* masks separated by ';' */
1894

Jon Griffiths's avatar
Jon Griffiths committed
1895 1896 1897 1898
    if (*lpszMask == ';')
      lpszMask++;
  }
  return FALSE;
1899 1900 1901 1902
}

/*************************************************************************
 * PathMatchSpecW	[SHLWAPI.@]
1903 1904
 *
 * See PathMatchSpecA.
1905
 */
Jon Griffiths's avatar
Jon Griffiths committed
1906
BOOL WINAPI PathMatchSpecW(LPCWSTR lpszPath, LPCWSTR lpszMask)
1907
{
Jon Griffiths's avatar
Jon Griffiths committed
1908 1909 1910 1911 1912 1913
  static const WCHAR szStarDotStar[] = { '*', '.', '*', '\0' };

  TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszMask));

  if (!lstrcmpW(lpszMask, szStarDotStar))
    return TRUE; /* Matches every path */
1914

Jon Griffiths's avatar
Jon Griffiths committed
1915 1916
  while (*lpszMask)
  {
1917 1918 1919
    while (*lpszMask == ' ')
      lpszMask++; /* Eat leading spaces */

Jon Griffiths's avatar
Jon Griffiths committed
1920 1921 1922 1923
    if (PathMatchSingleMaskW(lpszPath, lpszMask))
      return TRUE; /* Matches the current path */

    while (*lpszMask && *lpszMask != ';')
1924
      lpszMask++; /* masks separated by ';' */
1925

Jon Griffiths's avatar
Jon Griffiths committed
1926 1927 1928 1929
    if (*lpszMask == ';')
      lpszMask++;
  }
  return FALSE;
1930 1931 1932 1933 1934
}

/*************************************************************************
 * PathIsSameRootA	[SHLWAPI.@]
 *
1935 1936 1937 1938 1939 1940 1941 1942 1943
 * Determine if two paths share the same root.
 *
 * PARAMS
 *  lpszPath1 [I] Source path
 *  lpszPath2 [I] Path to compare with
 *
 * RETURNS
 *  TRUE  If both paths are valid and share the same root.
 *  FALSE If either path is invalid or the paths do not share the same root.
1944 1945 1946
 */
BOOL WINAPI PathIsSameRootA(LPCSTR lpszPath1, LPCSTR lpszPath2)
{
1947
  LPCSTR lpszStart;
1948
  int dwLen;
1949

1950
  TRACE("(%s,%s)\n", debugstr_a(lpszPath1), debugstr_a(lpszPath2));
1951

1952 1953 1954 1955 1956 1957 1958
  if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootA(lpszPath1)))
    return FALSE;

  dwLen = PathCommonPrefixA(lpszPath1, lpszPath2, NULL) + 1;
  if (lpszStart - lpszPath1 > dwLen)
    return FALSE; /* Paths not common up to length of the root */
  return TRUE;
1959 1960 1961 1962
}

/*************************************************************************
 * PathIsSameRootW	[SHLWAPI.@]
1963 1964
 *
 * See PathIsSameRootA.
1965 1966 1967
 */
BOOL WINAPI PathIsSameRootW(LPCWSTR lpszPath1, LPCWSTR lpszPath2)
{
1968
  LPCWSTR lpszStart;
1969
  int dwLen;
1970

1971
  TRACE("(%s,%s)\n", debugstr_w(lpszPath1), debugstr_w(lpszPath2));
1972

1973 1974 1975 1976 1977 1978 1979
  if (!lpszPath1 || !lpszPath2 || !(lpszStart = PathSkipRootW(lpszPath1)))
    return FALSE;

  dwLen = PathCommonPrefixW(lpszPath1, lpszPath2, NULL) + 1;
  if (lpszStart - lpszPath1 > dwLen)
    return FALSE; /* Paths not common up to length of the root */
  return TRUE;
1980 1981
}

1982 1983 1984
/*************************************************************************
 * PathIsContentTypeA   [SHLWAPI.@]
 *
1985
 * Determine if a file is of a given registered content type.
1986 1987
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
1988 1989
 *  lpszPath        [I] File to check
 *  lpszContentType [I] Content type to check for
1990 1991
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
1992
 *  TRUE  If lpszPath is a given registered content type,
1993
 *  FALSE Otherwise.
1994 1995 1996 1997 1998
 *
 * NOTES
 *  This function looks up the registered content type for lpszPath. If
 *  a content type is registered, it is compared (case insensitively) to
 *  lpszContentType. Only if this matches does the function succeed.
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
 */
BOOL WINAPI PathIsContentTypeA(LPCSTR lpszPath, LPCSTR lpszContentType)
{
  LPCSTR szExt;
  DWORD dwDummy;
  char szBuff[MAX_PATH];

  TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszContentType));

  if (lpszPath && (szExt = PathFindExtensionA(lpszPath)) && *szExt &&
      !SHGetValueA(HKEY_CLASSES_ROOT, szExt, "Content Type",
                   REG_NONE, szBuff, &dwDummy) &&
      !strcasecmp(lpszContentType, szBuff))
  {
    return TRUE;
  }
  return FALSE;
}
2017 2018

/*************************************************************************
2019 2020 2021
 * PathIsContentTypeW   [SHLWAPI.@]
 *
 * See PathIsContentTypeA.
2022
 */
2023
BOOL WINAPI PathIsContentTypeW(LPCWSTR lpszPath, LPCWSTR lpszContentType)
2024
{
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
  static const WCHAR szContentType[] = { 'C','o','n','t','e','n','t',' ','T','y','p','e','\0' };
  LPCWSTR szExt;
  DWORD dwDummy;
  WCHAR szBuff[MAX_PATH];

  TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszContentType));

  if (lpszPath && (szExt = PathFindExtensionW(lpszPath)) && *szExt &&
      !SHGetValueW(HKEY_CLASSES_ROOT, szExt, szContentType,
                   REG_NONE, szBuff, &dwDummy) &&
      !strcmpiW(lpszContentType, szBuff))
  {
    return TRUE;
  }
  return FALSE;
2040 2041 2042
}

/*************************************************************************
2043 2044 2045 2046 2047
 * PathIsFileSpecA   [SHLWAPI.@]
 *
 * Determine if a path is a file specification.
 *
 * PARAMS
Austin English's avatar
Austin English committed
2048
 *  lpszPath [I] Path to check
2049 2050
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2051
 *  TRUE  If lpszPath is a file specification (i.e. Contains no directories).
2052
 *  FALSE Otherwise.
2053
 */
2054
BOOL WINAPI PathIsFileSpecA(LPCSTR lpszPath)
2055
{
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (!lpszPath)
    return FALSE;

  while (*lpszPath)
  {
    if (*lpszPath == '\\' || *lpszPath == ':')
      return FALSE;
    lpszPath = CharNextA(lpszPath);
  }
  return TRUE;
2068 2069 2070
}

/*************************************************************************
2071 2072 2073
 * PathIsFileSpecW   [SHLWAPI.@]
 *
 * See PathIsFileSpecA.
2074
 */
2075
BOOL WINAPI PathIsFileSpecW(LPCWSTR lpszPath)
2076
{
2077 2078 2079 2080 2081 2082 2083 2084 2085
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (!lpszPath)
    return FALSE;

  while (*lpszPath)
  {
    if (*lpszPath == '\\' || *lpszPath == ':')
      return FALSE;
2086
    lpszPath++;
2087 2088
  }
  return TRUE;
2089 2090 2091
}

/*************************************************************************
2092 2093 2094 2095 2096 2097
 * PathIsPrefixA   [SHLWAPI.@]
 *
 * Determine if a path is a prefix of another.
 *
 * PARAMS
 *  lpszPrefix [I] Prefix
Jon Griffiths's avatar
Jon Griffiths committed
2098
 *  lpszPath   [I] Path to check
2099 2100
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2101
 *  TRUE  If lpszPath has lpszPrefix as its prefix,
2102
 *  FALSE If either path is NULL or lpszPrefix is not a prefix
2103
 */
2104
BOOL WINAPI PathIsPrefixA (LPCSTR lpszPrefix, LPCSTR lpszPath)
2105
{
2106 2107 2108
  TRACE("(%s,%s)\n", debugstr_a(lpszPrefix), debugstr_a(lpszPath));

  if (lpszPrefix && lpszPath &&
2109
      PathCommonPrefixA(lpszPath, lpszPrefix, NULL) == (int)strlen(lpszPrefix))
2110 2111
    return TRUE;
  return FALSE;
2112 2113 2114
}

/*************************************************************************
2115 2116 2117
 *  PathIsPrefixW   [SHLWAPI.@]
 *
 *  See PathIsPrefixA.
2118
 */
2119
BOOL WINAPI PathIsPrefixW(LPCWSTR lpszPrefix, LPCWSTR lpszPath)
2120
{
2121 2122 2123
  TRACE("(%s,%s)\n", debugstr_w(lpszPrefix), debugstr_w(lpszPath));

  if (lpszPrefix && lpszPath &&
2124
      PathCommonPrefixW(lpszPath, lpszPrefix, NULL) == (int)strlenW(lpszPrefix))
2125 2126
    return TRUE;
  return FALSE;
2127 2128 2129
}

/*************************************************************************
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
 * PathIsSystemFolderA   [SHLWAPI.@]
 *
 * Determine if a path or file attributes are a system folder.
 *
 * PARAMS
 *  lpszPath  [I] Path to check.
 *  dwAttrib  [I] Attributes to check, if lpszPath is NULL.
 *
 * RETURNS
 *  TRUE   If lpszPath or dwAttrib are a system folder.
Jon Griffiths's avatar
Jon Griffiths committed
2140
 *  FALSE  If GetFileAttributesA() fails or neither parameter is a system folder.
2141
 */
2142
BOOL WINAPI PathIsSystemFolderA(LPCSTR lpszPath, DWORD dwAttrib)
2143
{
2144
  TRACE("(%s,0x%08x)\n", debugstr_a(lpszPath), dwAttrib);
2145 2146 2147 2148

  if (lpszPath && *lpszPath)
    dwAttrib = GetFileAttributesA(lpszPath);

Jon Griffiths's avatar
Jon Griffiths committed
2149
  if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2150 2151 2152
      !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
    return FALSE;
  return TRUE;
2153 2154 2155
}

/*************************************************************************
2156 2157 2158
 * PathIsSystemFolderW   [SHLWAPI.@]
 *
 * See PathIsSystemFolderA.
2159
 */
2160
BOOL WINAPI PathIsSystemFolderW(LPCWSTR lpszPath, DWORD dwAttrib)
2161
{
2162
  TRACE("(%s,0x%08x)\n", debugstr_w(lpszPath), dwAttrib);
2163 2164 2165 2166

  if (lpszPath && *lpszPath)
    dwAttrib = GetFileAttributesW(lpszPath);

Jon Griffiths's avatar
Jon Griffiths committed
2167
  if (dwAttrib == INVALID_FILE_ATTRIBUTES || !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY) ||
2168 2169 2170
      !(dwAttrib & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY)))
    return FALSE;
  return TRUE;
2171 2172 2173
}

/*************************************************************************
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
 * PathIsUNCA		[SHLWAPI.@]
 *
 * Determine if a path is in UNC format.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
 *  TRUE: The path is UNC.
 *  FALSE: The path is not UNC or is NULL.
2184
 */
2185
BOOL WINAPI PathIsUNCA(LPCSTR lpszPath)
2186
{
2187 2188 2189 2190 2191
  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
    return TRUE;
  return FALSE;
2192 2193 2194
}

/*************************************************************************
2195 2196 2197
 * PathIsUNCW		[SHLWAPI.@]
 *
 * See PathIsUNCA.
2198
 */
2199
BOOL WINAPI PathIsUNCW(LPCWSTR lpszPath)
2200
{
2201 2202 2203 2204 2205
  TRACE("(%s)\n",debugstr_w(lpszPath));

  if (lpszPath && (lpszPath[0]=='\\') && (lpszPath[1]=='\\'))
    return TRUE;
  return FALSE;
2206 2207 2208
}

/*************************************************************************
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
 * PathIsUNCServerA   [SHLWAPI.@]
 *
 * Determine if a path is a UNC server name ("\\SHARENAME").
 *
 * PARAMS
 *  lpszPath  [I] Path to check.
 *
 * RETURNS
 *  TRUE   If lpszPath is a valid UNC server name.
 *  FALSE  Otherwise.
 *
 * NOTES
 *  This routine is bug compatible with Win32: Server names with a
 *  trailing backslash (e.g. "\\FOO\"), return FALSE incorrectly.
 *  Fixing this bug may break other shlwapi functions!
2224
 */
2225
BOOL WINAPI PathIsUNCServerA(LPCSTR lpszPath)
2226
{
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
  {
    while (*lpszPath)
    {
      if (*lpszPath == '\\')
        return FALSE;
      lpszPath = CharNextA(lpszPath);
    }
    return TRUE;
  }
  return FALSE;
2240 2241 2242
}

/*************************************************************************
2243 2244 2245
 * PathIsUNCServerW   [SHLWAPI.@]
 *
 * See PathIsUNCServerA.
2246
 */
2247
BOOL WINAPI PathIsUNCServerW(LPCWSTR lpszPath)
2248
{
2249 2250
  TRACE("(%s)\n", debugstr_w(lpszPath));

2251
  if (lpszPath && lpszPath[0] == '\\' && lpszPath[1] == '\\')
2252
  {
2253
      return !strchrW( lpszPath + 2, '\\' );
2254 2255
  }
  return FALSE;
2256 2257 2258
}

/*************************************************************************
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
 * PathIsUNCServerShareA   [SHLWAPI.@]
 *
 * Determine if a path is a UNC server share ("\\SHARENAME\SHARE").
 *
 * PARAMS
 *  lpszPath  [I] Path to check.
 *
 * RETURNS
 *  TRUE   If lpszPath is a valid UNC server share.
 *  FALSE  Otherwise.
 *
 * NOTES
 *  This routine is bug compatible with Win32: Server shares with a
 *  trailing backslash (e.g. "\\FOO\BAR\"), return FALSE incorrectly.
 *  Fixing this bug may break other shlwapi functions!
 */
BOOL WINAPI PathIsUNCServerShareA(LPCSTR lpszPath)
{
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
  {
    BOOL bSeenSlash = FALSE;
    while (*lpszPath)
    {
      if (*lpszPath == '\\')
      {
        if (bSeenSlash)
          return FALSE;
        bSeenSlash = TRUE;
      }
      lpszPath = CharNextA(lpszPath);
    }
    return bSeenSlash;
  }
  return FALSE;
}

/*************************************************************************
 * PathIsUNCServerShareW   [SHLWAPI.@]
 *
 * See PathIsUNCServerShareA.
2301
 */
2302
BOOL WINAPI PathIsUNCServerShareW(LPCWSTR lpszPath)
2303
{
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (lpszPath && *lpszPath++ == '\\' && *lpszPath++ == '\\')
  {
    BOOL bSeenSlash = FALSE;
    while (*lpszPath)
    {
      if (*lpszPath == '\\')
      {
        if (bSeenSlash)
          return FALSE;
        bSeenSlash = TRUE;
      }
2317
      lpszPath++;
2318 2319 2320 2321
    }
    return bSeenSlash;
  }
  return FALSE;
2322 2323 2324 2325 2326
}

/*************************************************************************
 * PathCanonicalizeA   [SHLWAPI.@]
 *
2327 2328 2329 2330
 * Convert a path to its canonical form.
 *
 * PARAMS
 *  lpszBuf  [O] Output path
Austin English's avatar
Austin English committed
2331
 *  lpszPath [I] Path to canonicalize
2332 2333
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2334
 *  Success: TRUE.  lpszBuf contains the output path,
2335
 *  Failure: FALSE, If input path is invalid. lpszBuf is undefined
2336
 */
2337
BOOL WINAPI PathCanonicalizeA(LPSTR lpszBuf, LPCSTR lpszPath)
2338
{
2339
  BOOL bRet = FALSE;
2340

2341
  TRACE("(%p,%s)\n", lpszBuf, debugstr_a(lpszPath));
2342

2343 2344 2345 2346 2347 2348 2349 2350 2351
  if (lpszBuf)
    *lpszBuf = '\0';

  if (!lpszBuf || !lpszPath)
    SetLastError(ERROR_INVALID_PARAMETER);
  else
  {
    WCHAR szPath[MAX_PATH];
    WCHAR szBuff[MAX_PATH];
2352 2353 2354 2355 2356 2357
    int ret = MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);

    if (!ret) {
	WARN("Failed to convert string to widechar (too long?), LE %d.\n", GetLastError());
	return FALSE;
    }
2358
    bRet = PathCanonicalizeW(szBuff, szPath);
2359
    WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszBuf,MAX_PATH,0,0);
2360 2361
  }
  return bRet;
2362 2363 2364 2365 2366 2367
}


/*************************************************************************
 * PathCanonicalizeW   [SHLWAPI.@]
 *
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 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 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438
 * See PathCanonicalizeA.
 */
BOOL WINAPI PathCanonicalizeW(LPWSTR lpszBuf, LPCWSTR lpszPath)
{
  LPWSTR lpszDst = lpszBuf;
  LPCWSTR lpszSrc = lpszPath;

  TRACE("(%p,%s)\n", lpszBuf, debugstr_w(lpszPath));

  if (lpszBuf)
    *lpszDst = '\0';

  if (!lpszBuf || !lpszPath)
  {
    SetLastError(ERROR_INVALID_PARAMETER);
    return FALSE;
  }

  if (!*lpszPath)
  {
    *lpszBuf++ = '\\';
    *lpszBuf = '\0';
    return TRUE;
  }

  /* Copy path root */
  if (*lpszSrc == '\\')
  {
    *lpszDst++ = *lpszSrc++;
  }
  else if (*lpszSrc && lpszSrc[1] == ':')
  {
    /* X:\ */
    *lpszDst++ = *lpszSrc++;
    *lpszDst++ = *lpszSrc++;
    if (*lpszSrc == '\\')
      *lpszDst++ = *lpszSrc++;
  }

  /* Canonicalize the rest of the path */
  while (*lpszSrc)
  {
    if (*lpszSrc == '.')
    {
      if (lpszSrc[1] == '\\' && (lpszSrc == lpszPath || lpszSrc[-1] == '\\' || lpszSrc[-1] == ':'))
      {
        lpszSrc += 2; /* Skip .\ */
      }
      else if (lpszSrc[1] == '.' && (lpszDst == lpszBuf || lpszDst[-1] == '\\'))
      {
        /* \.. backs up a directory, over the root if it has no \ following X:.
         * .. is ignored if it would remove a UNC server name or inital \\
         */
        if (lpszDst != lpszBuf)
        {
          *lpszDst = '\0'; /* Allow PathIsUNCServerShareA test on lpszBuf */
          if (lpszDst > lpszBuf+1 && lpszDst[-1] == '\\' &&
             (lpszDst[-2] != '\\' || lpszDst > lpszBuf+2))
          {
            if (lpszDst[-2] == ':' && (lpszDst > lpszBuf+3 || lpszDst[-3] == ':'))
            {
              lpszDst -= 2;
              while (lpszDst > lpszBuf && *lpszDst != '\\')
                lpszDst--;
              if (*lpszDst == '\\')
                lpszDst++; /* Reset to last '\' */
              else
                lpszDst = lpszBuf; /* Start path again from new root */
            }
            else if (lpszDst[-2] != ':' && !PathIsUNCServerShareW(lpszBuf))
              lpszDst -= 2;
2439
          }
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
          while (lpszDst > lpszBuf && *lpszDst != '\\')
            lpszDst--;
          if (lpszDst == lpszBuf)
          {
            *lpszDst++ = '\\';
            lpszSrc++;
          }
        }
        lpszSrc += 2; /* Skip .. in src path */
      }
      else
        *lpszDst++ = *lpszSrc++;
    }
    else
      *lpszDst++ = *lpszSrc++;
  }
  /* Append \ to naked drive specs */
  if (lpszDst - lpszBuf == 2 && lpszDst[-1] == ':')
    *lpszDst++ = '\\';
  *lpszDst++ = '\0';
  return TRUE;
2461 2462 2463 2464 2465
}

/*************************************************************************
 * PathFindNextComponentA   [SHLWAPI.@]
 *
2466 2467 2468 2469 2470 2471
 * Find the next component in a path.
 *
 * PARAMS
 *   lpszPath [I] Path to find next component in
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2472
 *  Success: A pointer to the next component, or the end of the string.
2473 2474
 *  Failure: NULL, If lpszPath is invalid
 *
2475
 * NOTES
2476 2477 2478 2479 2480
 *  A 'component' is either a backslash character (\) or UNC marker (\\).
 *  Because of this, relative paths (e.g "c:foo") are regarded as having
 *  only one component.
 */
LPSTR WINAPI PathFindNextComponentA(LPCSTR lpszPath)
2481
{
2482
  LPSTR lpszSlash;
2483

2484
  TRACE("(%s)\n", debugstr_a(lpszPath));
2485

2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
  if(!lpszPath || !*lpszPath)
    return NULL;

  if ((lpszSlash = StrChrA(lpszPath, '\\')))
  {
    if (lpszSlash[1] == '\\')
      lpszSlash++;
    return lpszSlash + 1;
  }
  return (LPSTR)lpszPath + strlen(lpszPath);
2496 2497 2498 2499
}

/*************************************************************************
 * PathFindNextComponentW   [SHLWAPI.@]
2500 2501
 *
 * See PathFindNextComponentA.
2502
 */
2503
LPWSTR WINAPI PathFindNextComponentW(LPCWSTR lpszPath)
2504
{
2505 2506 2507
  LPWSTR lpszSlash;

  TRACE("(%s)\n", debugstr_w(lpszPath));
2508

2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
  if(!lpszPath || !*lpszPath)
    return NULL;

  if ((lpszSlash = StrChrW(lpszPath, '\\')))
  {
    if (lpszSlash[1] == '\\')
      lpszSlash++;
    return lpszSlash + 1;
  }
  return (LPWSTR)lpszPath + strlenW(lpszPath);
2519 2520 2521 2522 2523
}

/*************************************************************************
 * PathAddExtensionA   [SHLWAPI.@]
 *
2524 2525 2526
 * Add a file extension to a path
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
2527 2528
 *  lpszPath      [I/O] Path to add extension to
 *  lpszExtension [I]   Extension to add to lpszPath
2529 2530
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2531
 *  TRUE  If the path was modified,
2532
 *  FALSE If lpszPath or lpszExtension are invalid, lpszPath has an
2533
 *        extension already, or the new path length is too big.
2534 2535
 *
 * FIXME
Jon Griffiths's avatar
Jon Griffiths committed
2536
 *  What version of shlwapi.dll adds "exe" if lpszExtension is NULL? Win2k
2537
 *  does not do this, so the behaviour was removed.
2538
 */
2539
BOOL WINAPI PathAddExtensionA(LPSTR lpszPath, LPCSTR lpszExtension)
2540
{
Jon Griffiths's avatar
Jon Griffiths committed
2541
  size_t dwLen;
2542

2543 2544 2545 2546 2547 2548
  TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExtension));

  if (!lpszPath || !lpszExtension || *(PathFindExtensionA(lpszPath)))
    return FALSE;

  dwLen = strlen(lpszPath);
2549

2550 2551 2552 2553 2554
  if (dwLen + strlen(lpszExtension) >= MAX_PATH)
    return FALSE;

  strcpy(lpszPath + dwLen, lpszExtension);
  return TRUE;
2555 2556 2557
}

/*************************************************************************
2558 2559 2560
 * PathAddExtensionW   [SHLWAPI.@]
 *
 * See PathAddExtensionA.
2561
 */
2562
BOOL WINAPI PathAddExtensionW(LPWSTR lpszPath, LPCWSTR lpszExtension)
2563
{
Jon Griffiths's avatar
Jon Griffiths committed
2564
  size_t dwLen;
2565

2566
  TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExtension));
2567

2568 2569 2570 2571 2572 2573 2574
  if (!lpszPath || !lpszExtension || *(PathFindExtensionW(lpszPath)))
    return FALSE;

  dwLen = strlenW(lpszPath);

  if (dwLen + strlenW(lpszExtension) >= MAX_PATH)
    return FALSE;
2575

2576 2577
  strcpyW(lpszPath + dwLen, lpszExtension);
  return TRUE;
2578 2579 2580
}

/*************************************************************************
2581 2582 2583 2584 2585
 * PathMakePrettyA   [SHLWAPI.@]
 *
 * Convert an uppercase DOS filename into lowercase.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
2586
 *  lpszPath [I/O] Path to convert.
2587 2588
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2589
 *  TRUE  If the path was an uppercase DOS path and was converted,
2590
 *  FALSE Otherwise.
2591
 */
2592
BOOL WINAPI PathMakePrettyA(LPSTR lpszPath)
2593
{
2594 2595 2596 2597
  LPSTR pszIter = lpszPath;

  TRACE("(%s)\n", debugstr_a(lpszPath));

2598
  if (!pszIter)
2599 2600
    return FALSE;

2601
  if (*pszIter)
2602
  {
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
    do
    {
      if (islower(*pszIter) || IsDBCSLeadByte(*pszIter))
        return FALSE; /* Not DOS path */
      pszIter++;
    } while (*pszIter);
    pszIter = lpszPath + 1;
    while (*pszIter)
    {
      *pszIter = tolower(*pszIter);
      pszIter++;
    }
2615 2616
  }
  return TRUE;
2617 2618 2619
}

/*************************************************************************
2620 2621
 * PathMakePrettyW   [SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
2622
 * See PathMakePrettyA.
2623
 */
2624
BOOL WINAPI PathMakePrettyW(LPWSTR lpszPath)
2625
{
2626
  LPWSTR pszIter = lpszPath;
2627

2628 2629
  TRACE("(%s)\n", debugstr_w(lpszPath));

2630
  if (!pszIter)
2631 2632
    return FALSE;

2633
  if (*pszIter)
2634
  {
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
    do
    {
      if (islowerW(*pszIter))
        return FALSE; /* Not DOS path */
      pszIter++;
    } while (*pszIter);
    pszIter = lpszPath + 1;
    while (*pszIter)
    {
      *pszIter = tolowerW(*pszIter);
      pszIter++;
    }
2647 2648
  }
  return TRUE;
2649 2650 2651
}

/*************************************************************************
2652 2653 2654 2655 2656
 * PathCommonPrefixA   [SHLWAPI.@]
 *
 * Determine the length of the common prefix between two paths.
 *
 * PARAMS
2657 2658
 *  lpszFile1 [I] First path for comparison
 *  lpszFile2 [I] Second path for comparison
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
 *  achPath   [O] Destination for common prefix string
 *
 * RETURNS
 *  The length of the common prefix. This is 0 if there is no common
 *  prefix between the paths or if any parameters are invalid. If the prefix
 *  is non-zero and achPath is not NULL, achPath is filled with the common
 *  part of the prefix and NUL terminated.
 *
 * NOTES
 *  A common prefix of 2 is always returned as 3. It is thus possible for
Jon Griffiths's avatar
Jon Griffiths committed
2669
 *  the length returned to be invalid (i.e. Longer than one or both of the
2670
 *  strings given as parameters). This Win32 behaviour has been implemented
2671 2672 2673
 *  here, and cannot be changed (fixed?) without breaking other SHLWAPI calls.
 *  To work around this when using this function, always check that the byte
 *  at [common_prefix_len-1] is not a NUL. If it is, deduct 1 from the prefix.
2674
 */
2675
int WINAPI PathCommonPrefixA(LPCSTR lpszFile1, LPCSTR lpszFile2, LPSTR achPath)
2676
{
Jon Griffiths's avatar
Jon Griffiths committed
2677
  size_t iLen = 0;
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714
  LPCSTR lpszIter1 = lpszFile1;
  LPCSTR lpszIter2 = lpszFile2;

  TRACE("(%s,%s,%p)\n", debugstr_a(lpszFile1), debugstr_a(lpszFile2), achPath);

  if (achPath)
    *achPath = '\0';

  if (!lpszFile1 || !lpszFile2)
    return 0;

  /* Handle roots first */
  if (PathIsUNCA(lpszFile1))
  {
    if (!PathIsUNCA(lpszFile2))
      return 0;
    lpszIter1 += 2;
    lpszIter2 += 2;
  }
  else if (PathIsUNCA(lpszFile2))
      return 0; /* Know already lpszFile1 is not UNC */

  do
  {
    /* Update len */
    if ((!*lpszIter1 || *lpszIter1 == '\\') &&
        (!*lpszIter2 || *lpszIter2 == '\\'))
      iLen = lpszIter1 - lpszFile1; /* Common to this point */

    if (!*lpszIter1 || (tolower(*lpszIter1) != tolower(*lpszIter2)))
      break; /* Strings differ at this point */

    lpszIter1++;
    lpszIter2++;
  } while (1);

  if (iLen == 2)
2715
    iLen++; /* Feature/Bug compatible with Win32 */
2716 2717 2718 2719 2720 2721 2722

  if (iLen && achPath)
  {
    memcpy(achPath,lpszFile1,iLen);
    achPath[iLen] = '\0';
  }
  return iLen;
2723 2724 2725
}

/*************************************************************************
2726 2727 2728
 * PathCommonPrefixW   [SHLWAPI.@]
 *
 * See PathCommonPrefixA.
2729
 */
2730
int WINAPI PathCommonPrefixW(LPCWSTR lpszFile1, LPCWSTR lpszFile2, LPWSTR achPath)
2731
{
Jon Griffiths's avatar
Jon Griffiths committed
2732
  size_t iLen = 0;
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
  LPCWSTR lpszIter1 = lpszFile1;
  LPCWSTR lpszIter2 = lpszFile2;

  TRACE("(%s,%s,%p)\n", debugstr_w(lpszFile1), debugstr_w(lpszFile2), achPath);

  if (achPath)
    *achPath = '\0';

  if (!lpszFile1 || !lpszFile2)
    return 0;

  /* Handle roots first */
  if (PathIsUNCW(lpszFile1))
  {
    if (!PathIsUNCW(lpszFile2))
      return 0;
    lpszIter1 += 2;
    lpszIter2 += 2;
  }
  else if (PathIsUNCW(lpszFile2))
      return 0; /* Know already lpszFile1 is not UNC */

  do
  {
    /* Update len */
    if ((!*lpszIter1 || *lpszIter1 == '\\') &&
        (!*lpszIter2 || *lpszIter2 == '\\'))
      iLen = lpszIter1 - lpszFile1; /* Common to this point */

    if (!*lpszIter1 || (tolowerW(*lpszIter1) != tolowerW(*lpszIter2)))
      break; /* Strings differ at this point */

    lpszIter1++;
    lpszIter2++;
  } while (1);

  if (iLen == 2)
2770
    iLen++; /* Feature/Bug compatible with Win32 */
2771 2772 2773 2774 2775 2776 2777

  if (iLen && achPath)
  {
    memcpy(achPath,lpszFile1,iLen * sizeof(WCHAR));
    achPath[iLen] = '\0';
  }
  return iLen;
2778 2779 2780
}

/*************************************************************************
2781 2782 2783 2784 2785
 * PathCompactPathA   [SHLWAPI.@]
 *
 * Make a path fit into a given width when printed to a DC.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
2786 2787 2788
 *  hDc      [I]   Destination DC
 *  lpszPath [I/O] Path to be printed to hDc
 *  dx       [I]   Desired width
2789 2790
 *
 * RETURNS
Peter Berg Larsen's avatar
Peter Berg Larsen committed
2791
 *  TRUE  If the path was modified/went well.
2792
 *  FALSE Otherwise.
2793
 */
2794
BOOL WINAPI PathCompactPathA(HDC hDC, LPSTR lpszPath, UINT dx)
2795
{
2796 2797
  BOOL bRet = FALSE;

2798
  TRACE("(%p,%s,%d)\n", hDC, debugstr_a(lpszPath), dx);
2799 2800 2801 2802

  if (lpszPath)
  {
    WCHAR szPath[MAX_PATH];
2803
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
2804
    bRet = PathCompactPathW(hDC, szPath, dx);
2805
    WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
2806 2807
  }
  return bRet;
2808 2809 2810
}

/*************************************************************************
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
 * PathCompactPathW   [SHLWAPI.@]
 *
 * See PathCompactPathA.
 */
BOOL WINAPI PathCompactPathW(HDC hDC, LPWSTR lpszPath, UINT dx)
{
  static const WCHAR szEllipses[] = { '.', '.', '.', '\0' };
  BOOL bRet = TRUE;
  HDC hdc = 0;
  WCHAR buff[MAX_PATH];
  SIZE size;
  DWORD dwLen;

2824
  TRACE("(%p,%s,%d)\n", hDC, debugstr_w(lpszPath), dx);
2825 2826

  if (!lpszPath)
Peter Berg Larsen's avatar
Peter Berg Larsen committed
2827
    return FALSE;
2828 2829 2830 2831 2832 2833 2834 2835

  if (!hDC)
    hdc = hDC = GetDC(0);

  /* Get the length of the whole path */
  dwLen = strlenW(lpszPath);
  GetTextExtentPointW(hDC, lpszPath, dwLen, &size);

2836
  if ((UINT)size.cx > dx)
2837 2838 2839 2840 2841 2842
  {
    /* Path too big, must reduce it */
    LPWSTR sFile;
    DWORD dwEllipsesLen = 0, dwPathLen = 0;

    sFile = PathFindFileNameW(lpszPath);
2843
    if (sFile != lpszPath) sFile--;
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860

    /* Get the size of ellipses */
    GetTextExtentPointW(hDC, szEllipses, 3, &size);
    dwEllipsesLen = size.cx;
    /* Get the size of the file name */
    GetTextExtentPointW(hDC, sFile, strlenW(sFile), &size);
    dwPathLen = size.cx;

    if (sFile != lpszPath)
    {
      LPWSTR sPath = sFile;
      BOOL bEllipses = FALSE;

      /* The path includes a file name. Include as much of the path prior to
       * the file name as possible, allowing for the ellipses, e.g:
       * c:\some very long path\filename ==> c:\some v...\filename
       */
2861
      lstrcpynW(buff, sFile, MAX_PATH);
2862 2863 2864 2865 2866 2867 2868 2869 2870

      do
      {
        DWORD dwTotalLen = bEllipses? dwPathLen + dwEllipsesLen : dwPathLen;

        GetTextExtentPointW(hDC, lpszPath, sPath - lpszPath, &size);
        dwTotalLen += size.cx;
        if (dwTotalLen <= dx)
          break;
2871
        sPath--;
2872 2873 2874
        if (!bEllipses)
        {
          bEllipses = TRUE;
2875
          sPath -= 2;
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
        }
      } while (sPath > lpszPath);

      if (sPath > lpszPath)
      {
        if (bEllipses)
        {
          strcpyW(sPath, szEllipses);
          strcpyW(sPath+3, buff);
        }
2886 2887
        bRet = TRUE;
        goto end;
2888 2889 2890
      }
      strcpyW(lpszPath, szEllipses);
      strcpyW(lpszPath+3, buff);
2891 2892
      bRet = FALSE;
      goto end;
2893 2894 2895 2896 2897 2898 2899 2900 2901
    }

    /* Trim the path by adding ellipses to the end, e.g:
     * A very long file name.txt ==> A very...
     */
    dwLen = strlenW(lpszPath);

    if (dwLen > MAX_PATH - 3)
      dwLen =  MAX_PATH - 3;
Peter Berg Larsen's avatar
Peter Berg Larsen committed
2902
    lstrcpynW(buff, sFile, dwLen);
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931

    do {
      dwLen--;
      GetTextExtentPointW(hDC, buff, dwLen, &size);
    } while (dwLen && size.cx + dwEllipsesLen > dx);

   if (!dwLen)
   {
     DWORD dwWritten = 0;

     dwEllipsesLen /= 3; /* Size of a single '.' */

     /* Write as much of the Ellipses string as possible */
     while (dwWritten + dwEllipsesLen < dx && dwLen < 3)
     {
       *lpszPath++ = '.';
       dwWritten += dwEllipsesLen;
       dwLen++;
     }
     *lpszPath = '\0';
     bRet = FALSE;
   }
   else
   {
     strcpyW(buff + dwLen, szEllipses);
     strcpyW(lpszPath, buff);
    }
  }

2932
end:
2933 2934 2935 2936
  if (hdc)
    ReleaseDC(0, hdc);

  return bRet;
2937 2938 2939
}

/*************************************************************************
2940 2941 2942 2943 2944 2945 2946 2947
 * PathGetCharTypeA   [SHLWAPI.@]
 *
 * Categorise a character from a file path.
 *
 * PARAMS
 *  ch [I] Character to get the type of
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
2948
 *  A set of GCT_ bit flags (from "shlwapi.h") indicating the character type.
2949 2950 2951
 */
UINT WINAPI PathGetCharTypeA(UCHAR ch)
{
2952
  return PathGetCharTypeW(ch);
2953 2954 2955
}

/*************************************************************************
2956 2957 2958
 * PathGetCharTypeW   [SHLWAPI.@]
 *
 * See PathGetCharTypeA.
2959 2960 2961
 */
UINT WINAPI PathGetCharTypeW(WCHAR ch)
{
2962 2963 2964 2965 2966
  UINT flags = 0;

  TRACE("(%d)\n", ch);

  if (!ch || ch < ' ' || ch == '<' || ch == '>' ||
2967
      ch == '"' || ch == '|' || ch == '/')
2968 2969 2970
    flags = GCT_INVALID; /* Invalid */
  else if (ch == '*' || ch=='?')
    flags = GCT_WILD; /* Wildchars */
2971
  else if ((ch == '\\') || (ch == ':'))
2972 2973 2974 2975 2976
    return GCT_SEPARATOR; /* Path separators */
  else
  {
     if (ch < 126)
     {
2977
       if ((ch & 0x1 && ch != ';') || !ch || isalnum(ch) || ch == '$' || ch == '&' || ch == '(' ||
2978 2979 2980 2981 2982
            ch == '.' || ch == '@' || ch == '^' ||
            ch == '\'' || ch == 130 || ch == '`')
         flags |= GCT_SHORTCHAR; /* All these are valid for DOS */
     }
     else
2983
       flags |= GCT_SHORTCHAR; /* Bug compatible with win32 */
2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
     flags |= GCT_LFNCHAR; /* Valid for long file names */
  }
  return flags;
}

/*************************************************************************
 * SHLWAPI_UseSystemForSystemFolders
 *
 * Internal helper for PathMakeSystemFolderW.
 */
2994
static BOOL SHLWAPI_UseSystemForSystemFolders(void)
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
{
  static BOOL bCheckedReg = FALSE;
  static BOOL bUseSystemForSystemFolders = FALSE;

  if (!bCheckedReg)
  {
    bCheckedReg = TRUE;

    /* Key tells Win what file attributes to use on system folders */
    if (SHGetValueA(HKEY_LOCAL_MACHINE,
        "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer",
        "UseSystemForSystemFolders", 0, 0, 0))
      bUseSystemForSystemFolders = TRUE;
  }
  return bUseSystemForSystemFolders;
3010 3011 3012
}

/*************************************************************************
3013 3014 3015 3016 3017 3018 3019 3020 3021
 * PathMakeSystemFolderA   [SHLWAPI.@]
 *
 * Set system folder attribute for a path.
 *
 * PARAMS
 *  lpszPath [I] The path to turn into a system folder
 *
 * RETURNS
 *  TRUE  If the path was changed to/already was a system folder
Jon Griffiths's avatar
Jon Griffiths committed
3022
 *  FALSE If the path is invalid or SetFileAttributesA() fails
3023
 */
3024
BOOL WINAPI PathMakeSystemFolderA(LPCSTR lpszPath)
3025
{
3026 3027 3028 3029 3030 3031 3032
  BOOL bRet = FALSE;

  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (lpszPath && *lpszPath)
  {
    WCHAR szPath[MAX_PATH];
3033
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3034 3035 3036
    bRet = PathMakeSystemFolderW(szPath);
  }
  return bRet;
3037 3038 3039
}

/*************************************************************************
3040 3041 3042
 * PathMakeSystemFolderW   [SHLWAPI.@]
 *
 * See PathMakeSystemFolderA.
3043
 */
3044
BOOL WINAPI PathMakeSystemFolderW(LPCWSTR lpszPath)
3045
{
3046 3047 3048 3049 3050 3051 3052 3053
  DWORD dwDefaultAttr = FILE_ATTRIBUTE_READONLY, dwAttr;
  WCHAR buff[MAX_PATH];

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

  if (!lpszPath || !*lpszPath)
    return FALSE;

3054
  /* If the directory is already a system directory, don't do anything */
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
  GetSystemDirectoryW(buff, MAX_PATH);
  if (!strcmpW(buff, lpszPath))
    return TRUE;

  GetWindowsDirectoryW(buff, MAX_PATH);
  if (!strcmpW(buff, lpszPath))
    return TRUE;

  /* "UseSystemForSystemFolders" Tells Win what attributes to use */
  if (SHLWAPI_UseSystemForSystemFolders())
    dwDefaultAttr = FILE_ATTRIBUTE_SYSTEM;

Jon Griffiths's avatar
Jon Griffiths committed
3067
  if ((dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES)
3068 3069 3070 3071 3072
    return FALSE;

  /* Change file attributes to system attributes */
  dwAttr &= ~(FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY);
  return SetFileAttributesW(lpszPath, dwAttr | dwDefaultAttr);
3073 3074 3075
}

/*************************************************************************
3076 3077 3078 3079 3080
 * PathRenameExtensionA   [SHLWAPI.@]
 *
 * Swap the file extension in a path with another extension.
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
3081 3082
 *  lpszPath [I/O] Path to swap the extension in
 *  lpszExt  [I]   The new extension
3083 3084
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3085 3086
 *  TRUE  if lpszPath was modified,
 *  FALSE if lpszPath or lpszExt is NULL, or the new path is too long
3087
 */
3088
BOOL WINAPI PathRenameExtensionA(LPSTR lpszPath, LPCSTR lpszExt)
3089
{
3090 3091 3092 3093 3094
  LPSTR lpszExtension;

  TRACE("(%s,%s)\n", debugstr_a(lpszPath), debugstr_a(lpszExt));

  lpszExtension = PathFindExtensionA(lpszPath);
3095

3096 3097
  if (!lpszExtension || (lpszExtension - lpszPath + strlen(lpszExt) >= MAX_PATH))
    return FALSE;
3098

3099 3100
  strcpy(lpszExtension, lpszExt);
  return TRUE;
3101 3102 3103
}

/*************************************************************************
3104 3105 3106
 * PathRenameExtensionW   [SHLWAPI.@]
 *
 * See PathRenameExtensionA.
3107
 */
3108
BOOL WINAPI PathRenameExtensionW(LPWSTR lpszPath, LPCWSTR lpszExt)
3109
{
3110 3111 3112 3113 3114
  LPWSTR lpszExtension;

  TRACE("(%s,%s)\n", debugstr_w(lpszPath), debugstr_w(lpszExt));

  lpszExtension = PathFindExtensionW(lpszPath);
3115

3116 3117
  if (!lpszExtension || (lpszExtension - lpszPath + strlenW(lpszExt) >= MAX_PATH))
    return FALSE;
3118

3119 3120
  strcpyW(lpszExtension, lpszExt);
  return TRUE;
3121 3122 3123
}

/*************************************************************************
3124 3125
 * PathSearchAndQualifyA   [SHLWAPI.@]
 *
3126
 * Determine if a given path is correct and fully qualified.
3127 3128
 *
 * PARAMS
3129 3130
 *  lpszPath [I] Path to check
 *  lpszBuf  [O] Output for correct path
3131 3132 3133 3134
 *  cchBuf   [I] Size of lpszBuf
 *
 * RETURNS
 *  Unknown.
3135
 */
3136
BOOL WINAPI PathSearchAndQualifyA(LPCSTR lpszPath, LPSTR lpszBuf, UINT cchBuf)
3137
{
3138 3139 3140 3141 3142
    TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszPath), lpszBuf, cchBuf);

    if(SearchPathA(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
        return TRUE;
    return !!GetFullPathNameA(lpszPath, cchBuf, lpszBuf, NULL);
3143 3144 3145
}

/*************************************************************************
3146 3147
 * PathSearchAndQualifyW   [SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
3148
 * See PathSearchAndQualifyA.
3149
 */
3150
BOOL WINAPI PathSearchAndQualifyW(LPCWSTR lpszPath, LPWSTR lpszBuf, UINT cchBuf)
3151
{
3152 3153 3154 3155 3156
    TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszPath), lpszBuf, cchBuf);

    if(SearchPathW(NULL, lpszPath, NULL, cchBuf, lpszBuf, NULL))
        return TRUE;
    return !!GetFullPathNameW(lpszPath, cchBuf, lpszBuf, NULL);
3157 3158 3159
}

/*************************************************************************
3160 3161 3162 3163 3164 3165 3166 3167 3168
 * PathSkipRootA   [SHLWAPI.@]
 *
 * Return the portion of a path following the drive letter or mount point.
 *
 * PARAMS
 *  lpszPath [I] The path to skip on
 *
 * RETURNS
 *  Success: A pointer to the next character after the root.
Jon Griffiths's avatar
Jon Griffiths committed
3169
 *  Failure: NULL, if lpszPath is invalid, has no root or is a multibyte string.
3170
 */
3171
LPSTR WINAPI PathSkipRootA(LPCSTR lpszPath)
3172
{
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
  TRACE("(%s)\n", debugstr_a(lpszPath));

  if (!lpszPath || !*lpszPath)
    return NULL;

  if (*lpszPath == '\\' && lpszPath[1] == '\\')
  {
    /* Network share: skip share server and mount point */
    lpszPath += 2;
    if ((lpszPath = StrChrA(lpszPath, '\\')) &&
        (lpszPath = StrChrA(lpszPath + 1, '\\')))
      lpszPath++;
    return (LPSTR)lpszPath;
  }

  if (IsDBCSLeadByte(*lpszPath))
    return NULL;

  /* Check x:\ */
  if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
    return (LPSTR)lpszPath + 3;
  return NULL;
3195 3196 3197
}

/*************************************************************************
3198 3199 3200
 * PathSkipRootW   [SHLWAPI.@]
 *
 * See PathSkipRootA.
3201
 */
3202
LPWSTR WINAPI PathSkipRootW(LPCWSTR lpszPath)
3203
{
3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
  TRACE("(%s)\n", debugstr_w(lpszPath));

  if (!lpszPath || !*lpszPath)
    return NULL;

  if (*lpszPath == '\\' && lpszPath[1] == '\\')
  {
    /* Network share: skip share server and mount point */
    lpszPath += 2;
    if ((lpszPath = StrChrW(lpszPath, '\\')) &&
        (lpszPath = StrChrW(lpszPath + 1, '\\')))
     lpszPath++;
    return (LPWSTR)lpszPath;
  }

  /* Check x:\ */
  if (lpszPath[0] && lpszPath[1] == ':' && lpszPath[2] == '\\')
    return (LPWSTR)lpszPath + 3;
  return NULL;
3223 3224 3225
}

/*************************************************************************
3226 3227
 * PathCreateFromUrlA   [SHLWAPI.@]
 *
Huw Davies's avatar
Huw Davies committed
3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
 * See PathCreateFromUrlW
 */
HRESULT WINAPI PathCreateFromUrlA(LPCSTR pszUrl, LPSTR pszPath,
                                  LPDWORD pcchPath, DWORD dwReserved)
{
    WCHAR bufW[MAX_PATH];
    WCHAR *pathW = bufW;
    UNICODE_STRING urlW;
    HRESULT ret;
    DWORD lenW = sizeof(bufW)/sizeof(WCHAR), lenA;

    if(!RtlCreateUnicodeStringFromAsciiz(&urlW, pszUrl))
        return E_INVALIDARG;
    if((ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved)) == E_POINTER) {
        pathW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
        ret = PathCreateFromUrlW(urlW.Buffer, pathW, &lenW, dwReserved);
    }
    if(ret == S_OK) {
        RtlUnicodeToMultiByteSize(&lenA, pathW, lenW * sizeof(WCHAR));
        if(*pcchPath > lenA) {
            RtlUnicodeToMultiByteN(pszPath, *pcchPath - 1, &lenA, pathW, lenW * sizeof(WCHAR));
            pszPath[lenA] = 0;
            *pcchPath = lenA;
        } else {
            *pcchPath = lenA + 1;
            ret = E_POINTER;
        }
    }
    if(pathW != bufW) HeapFree(GetProcessHeap(), 0, pathW);
    RtlFreeUnicodeString(&urlW);
    return ret;
}

/*************************************************************************
 * PathCreateFromUrlW   [SHLWAPI.@]
 *
3264 3265 3266 3267 3268 3269 3270 3271 3272
 * Create a path from a URL
 *
 * PARAMS
 *  lpszUrl  [I] URL to convert into a path
 *  lpszPath [O] Output buffer for the resulting Path
 *  pcchPath [I] Length of lpszPath
 *  dwFlags  [I] Flags controlling the conversion
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3273
 *  Success: S_OK. lpszPath contains the URL in path format,
3274
 *  Failure: An HRESULT error code such as E_INVALIDARG.
3275
 */
Huw Davies's avatar
Huw Davies committed
3276 3277
HRESULT WINAPI PathCreateFromUrlW(LPCWSTR pszUrl, LPWSTR pszPath,
                                  LPDWORD pcchPath, DWORD dwReserved)
3278
{
Huw Davies's avatar
Huw Davies committed
3279 3280 3281 3282
    static const WCHAR file_colon[] = { 'f','i','l','e',':',0 };
    HRESULT hr;
    DWORD nslashes = 0;
    WCHAR *ptr;
3283

3284
    TRACE("(%s,%p,%p,0x%08x)\n", debugstr_w(pszUrl), pszPath, pcchPath, dwReserved);
3285

Huw Davies's avatar
Huw Davies committed
3286 3287
    if (!pszUrl || !pszPath || !pcchPath || !*pcchPath)
        return E_INVALIDARG;
3288

3289

Huw Davies's avatar
Huw Davies committed
3290 3291 3292
    if (strncmpW(pszUrl, file_colon, 5))
        return E_INVALIDARG;
    pszUrl += 5;
3293

Huw Davies's avatar
Huw Davies committed
3294 3295 3296 3297
    while(*pszUrl == '/' || *pszUrl == '\\') {
        nslashes++;
        pszUrl++;
    }
3298

Huw Davies's avatar
Huw Davies committed
3299 3300
    if(isalphaW(*pszUrl) && (pszUrl[1] == ':' || pszUrl[1] == '|') && (pszUrl[2] == '/' || pszUrl[2] == '\\'))
        nslashes = 0;
3301

Huw Davies's avatar
Huw Davies committed
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
    switch(nslashes) {
    case 2:
        pszUrl -= 2;
        break;
    case 0:
        break;
    default:
        pszUrl -= 1;
        break;
    }
3312

Huw Davies's avatar
Huw Davies committed
3313 3314
    hr = UrlUnescapeW((LPWSTR)pszUrl, pszPath, pcchPath, 0);
    if(hr != S_OK) return hr;
3315

Huw Davies's avatar
Huw Davies committed
3316 3317 3318 3319 3320 3321 3322 3323
    for(ptr = pszPath; *ptr; ptr++)
        if(*ptr == '/') *ptr = '\\';

    while(*pszPath == '\\')
        pszPath++;
 
    if(isalphaW(*pszPath) && pszPath[1] == '|' && pszPath[2] == '\\') /* c|\ -> c:\ */
        pszPath[1] = ':';
3324

Huw Davies's avatar
Huw Davies committed
3325 3326 3327 3328 3329 3330 3331
    if(nslashes == 2 && (ptr = strchrW(pszPath, '\\'))) { /* \\host\c:\ -> \\hostc:\ */
        ptr++;
        if(isalphaW(*ptr) && (ptr[1] == ':' || ptr[1] == '|') && ptr[2] == '\\') {
            memmove(ptr - 1, ptr, (strlenW(ptr) + 1) * sizeof(WCHAR));
            (*pcchPath)--;
        }
    }
3332

Huw Davies's avatar
Huw Davies committed
3333
    TRACE("Returning %s\n",debugstr_w(pszPath));
3334

Huw Davies's avatar
Huw Davies committed
3335
    return hr;
3336 3337 3338
}

/*************************************************************************
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
 * PathRelativePathToA   [SHLWAPI.@]
 *
 * Create a relative path from one path to another.
 *
 * PARAMS
 *  lpszPath   [O] Destination for relative path
 *  lpszFrom   [I] Source path
 *  dwAttrFrom [I] File attribute of source path
 *  lpszTo     [I] Destination path
 *  dwAttrTo   [I] File attributes of destination path
 *
 * RETURNS
 *  TRUE  If a relative path can be formed. lpszPath contains the new path
Austin English's avatar
Austin English committed
3352
 *  FALSE If the paths are not relative or any parameters are invalid
3353 3354 3355
 *
 * NOTES
 *  lpszTo should be at least MAX_PATH in length.
Jon Griffiths's avatar
Jon Griffiths committed
3356
 *
3357 3358 3359 3360 3361 3362 3363
 *  Calling this function with relative paths for lpszFrom or lpszTo may
 *  give erroneous results.
 *
 *  The Win32 version of this function contains a bug where the lpszTo string
 *  may be referenced 1 byte beyond the end of the string. As a result random
 *  garbage may be written to the output path, depending on what lies beyond
 *  the last byte of the string. This bug occurs because of the behaviour of
Jon Griffiths's avatar
Jon Griffiths committed
3364
 *  PathCommonPrefix() (see notes for that function), and no workaround seems
3365
 *  possible with Win32.
Jon Griffiths's avatar
Jon Griffiths committed
3366
 *
3367 3368 3369 3370 3371 3372 3373 3374
 *  This bug has been fixed here, so for example the relative path from "\\"
 *  to "\\" is correctly determined as "." in this implementation.
 */
BOOL WINAPI PathRelativePathToA(LPSTR lpszPath, LPCSTR lpszFrom, DWORD dwAttrFrom,
                                LPCSTR lpszTo, DWORD dwAttrTo)
{
  BOOL bRet = FALSE;

3375
  TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_a(lpszFrom),
3376 3377 3378 3379 3380 3381 3382
        dwAttrFrom, debugstr_a(lpszTo), dwAttrTo);

  if(lpszPath && lpszFrom && lpszTo)
  {
    WCHAR szPath[MAX_PATH];
    WCHAR szFrom[MAX_PATH];
    WCHAR szTo[MAX_PATH];
3383 3384
    MultiByteToWideChar(CP_ACP,0,lpszFrom,-1,szFrom,MAX_PATH);
    MultiByteToWideChar(CP_ACP,0,lpszTo,-1,szTo,MAX_PATH);
3385
    bRet = PathRelativePathToW(szPath,szFrom,dwAttrFrom,szTo,dwAttrTo);
3386
    WideCharToMultiByte(CP_ACP,0,szPath,-1,lpszPath,MAX_PATH,0,0);
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404
  }
  return bRet;
}

/*************************************************************************
 * PathRelativePathToW   [SHLWAPI.@]
 *
 * See PathRelativePathToA.
 */
BOOL WINAPI PathRelativePathToW(LPWSTR lpszPath, LPCWSTR lpszFrom, DWORD dwAttrFrom,
                                LPCWSTR lpszTo, DWORD dwAttrTo)
{
  static const WCHAR szPrevDirSlash[] = { '.', '.', '\\', '\0' };
  static const WCHAR szPrevDir[] = { '.', '.', '\0' };
  WCHAR szFrom[MAX_PATH];
  WCHAR szTo[MAX_PATH];
  DWORD dwLen;

3405
  TRACE("(%p,%s,0x%08x,%s,0x%08x)\n", lpszPath, debugstr_w(lpszFrom),
3406 3407 3408 3409 3410 3411
        dwAttrFrom, debugstr_w(lpszTo), dwAttrTo);

  if(!lpszPath || !lpszFrom || !lpszTo)
    return FALSE;

  *lpszPath = '\0';
3412 3413
  lstrcpynW(szFrom, lpszFrom, MAX_PATH);
  lstrcpynW(szTo, lpszTo, MAX_PATH);
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470

  if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
    PathRemoveFileSpecW(szFrom);
  if(!(dwAttrFrom & FILE_ATTRIBUTE_DIRECTORY))
    PathRemoveFileSpecW(szTo);

  /* Paths can only be relative if they have a common root */
  if(!(dwLen = PathCommonPrefixW(szFrom, szTo, 0)))
    return FALSE;

  /* Strip off lpszFrom components to the root, by adding "..\" */
  lpszFrom = szFrom + dwLen;
  if (!*lpszFrom)
  {
    lpszPath[0] = '.';
    lpszPath[1] = '\0';
  }
  if (*lpszFrom == '\\')
    lpszFrom++;

  while (*lpszFrom)
  {
    lpszFrom = PathFindNextComponentW(lpszFrom);
    strcatW(lpszPath, *lpszFrom ? szPrevDirSlash : szPrevDir);
  }

  /* From the root add the components of lpszTo */
  lpszTo += dwLen;
  /* We check lpszTo[-1] to avoid skipping end of string. See the notes for
   * this function.
   */
  if (*lpszTo && lpszTo[-1])
  {
    if (*lpszTo != '\\')
      lpszTo--;
    dwLen = strlenW(lpszPath);
    if (dwLen + strlenW(lpszTo) >= MAX_PATH)
    {
      *lpszPath = '\0';
      return FALSE;
    }
    strcpyW(lpszPath + dwLen, lpszTo);
  }
  return TRUE;
}

/*************************************************************************
 * PathUnmakeSystemFolderA   [SHLWAPI.@]
 *
 * Remove the system folder attributes from a path.
 *
 * PARAMS
 *  lpszPath [I] The path to remove attributes from
 *
 * RETURNS
 *  Success: TRUE.
 *  Failure: FALSE, if lpszPath is NULL, empty, not a directory, or calling
Jon Griffiths's avatar
Jon Griffiths committed
3471
 *           SetFileAttributesA() fails.
3472
 */
3473
BOOL WINAPI PathUnmakeSystemFolderA(LPCSTR lpszPath)
3474
{
3475 3476 3477 3478
  DWORD dwAttr;

  TRACE("(%s)\n", debugstr_a(lpszPath));

Jon Griffiths's avatar
Jon Griffiths committed
3479
  if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesA(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3480 3481 3482 3483 3484
      !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
    return FALSE;

  dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
  return SetFileAttributesA(lpszPath, dwAttr);
3485 3486 3487
}

/*************************************************************************
3488 3489 3490
 * PathUnmakeSystemFolderW   [SHLWAPI.@]
 *
 * See PathUnmakeSystemFolderA.
3491
 */
3492
BOOL WINAPI PathUnmakeSystemFolderW(LPCWSTR lpszPath)
3493
{
3494 3495 3496 3497
  DWORD dwAttr;

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

Jon Griffiths's avatar
Jon Griffiths committed
3498
  if (!lpszPath || !*lpszPath || (dwAttr = GetFileAttributesW(lpszPath)) == INVALID_FILE_ATTRIBUTES ||
3499 3500 3501 3502 3503
    !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
    return FALSE;

  dwAttr &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
  return SetFileAttributesW(lpszPath, dwAttr);
3504 3505
}

3506

3507
/*************************************************************************
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523
 * PathSetDlgItemPathA   [SHLWAPI.@]
 *
 * Set the text of a dialog item to a path, shrinking the path to fit
 * if it is too big for the item.
 *
 * PARAMS
 *  hDlg     [I] Dialog handle
 *  id       [I] ID of item in the dialog
 *  lpszPath [I] Path to set as the items text
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  If lpszPath is NULL, a blank string ("") is set (i.e. The previous
 *  window text is erased).
3524
 */
3525
VOID WINAPI PathSetDlgItemPathA(HWND hDlg, int id, LPCSTR lpszPath)
3526
{
3527 3528
  WCHAR szPath[MAX_PATH];

3529
  TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_a(lpszPath));
3530 3531

  if (lpszPath)
3532
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3533 3534 3535
  else
    szPath[0] = '\0';
  PathSetDlgItemPathW(hDlg, id, szPath);
3536 3537 3538
}

/*************************************************************************
3539 3540 3541
 * PathSetDlgItemPathW   [SHLWAPI.@]
 *
 * See PathSetDlgItemPathA.
3542
 */
3543
VOID WINAPI PathSetDlgItemPathW(HWND hDlg, int id, LPCWSTR lpszPath)
3544
{
3545 3546 3547 3548 3549 3550
  WCHAR path[MAX_PATH + 1];
  HWND hwItem;
  RECT rect;
  HDC hdc;
  HGDIOBJ hPrevObj;

3551
  TRACE("(%p,%8x,%s)\n",hDlg, id, debugstr_w(lpszPath));
3552 3553 3554 3555 3556

  if (!(hwItem = GetDlgItem(hDlg, id)))
    return;

  if (lpszPath)
3557
    lstrcpynW(path, lpszPath, sizeof(path) / sizeof(WCHAR));
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
  else
    path[0] = '\0';

  GetClientRect(hwItem, &rect);
  hdc = GetDC(hDlg);
  hPrevObj = SelectObject(hdc, (HGDIOBJ)SendMessageW(hwItem,WM_GETFONT,0,0));

  if (hPrevObj)
  {
    PathCompactPathW(hdc, path, rect.right);
    SelectObject(hdc, hPrevObj);
  }

  ReleaseDC(hDlg, hdc);
  SetWindowTextW(hwItem, path);
3573 3574
}

3575 3576 3577 3578 3579 3580 3581 3582 3583
/*************************************************************************
 * PathIsNetworkPathA [SHLWAPI.@]
 *
 * Determine if the given path is a network path.
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3584 3585
 *  TRUE  If lpszPath is a UNC share or mapped network drive, or
 *  FALSE If lpszPath is a local drive or cannot be determined
3586 3587 3588
 */
BOOL WINAPI PathIsNetworkPathA(LPCSTR lpszPath)
{
Jon Griffiths's avatar
Jon Griffiths committed
3589
  int dwDriveNum;
3590 3591 3592 3593 3594 3595 3596 3597

  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (!lpszPath)
    return FALSE;
  if (*lpszPath == '\\' && lpszPath[1] == '\\')
    return TRUE;
  dwDriveNum = PathGetDriveNumberA(lpszPath);
Jon Griffiths's avatar
Jon Griffiths committed
3598
  if (dwDriveNum == -1)
3599
    return FALSE;
3600 3601
  GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
  return pIsNetDrive(dwDriveNum);
3602
}
3603 3604

/*************************************************************************
3605 3606 3607 3608 3609 3610
 * PathIsNetworkPathW [SHLWAPI.@]
 *
 * See PathIsNetworkPathA.
 */
BOOL WINAPI PathIsNetworkPathW(LPCWSTR lpszPath)
{
Jon Griffiths's avatar
Jon Griffiths committed
3611
  int dwDriveNum;
3612 3613 3614 3615 3616 3617 3618 3619

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

  if (!lpszPath)
    return FALSE;
  if (*lpszPath == '\\' && lpszPath[1] == '\\')
    return TRUE;
  dwDriveNum = PathGetDriveNumberW(lpszPath);
Jon Griffiths's avatar
Jon Griffiths committed
3620
  if (dwDriveNum == -1)
3621
    return FALSE;
3622 3623
  GET_FUNC(pIsNetDrive, shell32, (LPCSTR)66, FALSE); /* ord 66 = shell32.IsNetDrive */
  return pIsNetDrive(dwDriveNum);
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
}

/*************************************************************************
 * PathIsLFNFileSpecA [SHLWAPI.@]
 *
 * Determine if the given path is a long file name
 *
 * PARAMS
 *  lpszPath [I] Path to check
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3635
 *  TRUE  If path is a long file name,
Jon Griffiths's avatar
Jon Griffiths committed
3636
 *  FALSE If path is a valid DOS short file name
3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723
 */
BOOL WINAPI PathIsLFNFileSpecA(LPCSTR lpszPath)
{
  DWORD dwNameLen = 0, dwExtLen = 0;

  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (!lpszPath)
    return FALSE;

  while (*lpszPath)
  {
    if (*lpszPath == ' ')
      return TRUE; /* DOS names cannot have spaces */
    if (*lpszPath == '.')
    {
      if (dwExtLen)
        return TRUE; /* DOS names have only one dot */
      dwExtLen = 1;
    }
    else if (dwExtLen)
    {
      dwExtLen++;
      if (dwExtLen > 4)
        return TRUE; /* DOS extensions are <= 3 chars*/
    }
    else
    {
      dwNameLen++;
      if (dwNameLen > 8)
        return TRUE; /* DOS names are <= 8 chars */
    }
    lpszPath += IsDBCSLeadByte(*lpszPath) ? 2 : 1;
  }
  return FALSE; /* Valid DOS path */
}

/*************************************************************************
 * PathIsLFNFileSpecW [SHLWAPI.@]
 *
 * See PathIsLFNFileSpecA.
 */
BOOL WINAPI PathIsLFNFileSpecW(LPCWSTR lpszPath)
{
  DWORD dwNameLen = 0, dwExtLen = 0;

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

  if (!lpszPath)
    return FALSE;

  while (*lpszPath)
  {
    if (*lpszPath == ' ')
      return TRUE; /* DOS names cannot have spaces */
    if (*lpszPath == '.')
    {
      if (dwExtLen)
        return TRUE; /* DOS names have only one dot */
      dwExtLen = 1;
    }
    else if (dwExtLen)
    {
      dwExtLen++;
      if (dwExtLen > 4)
        return TRUE; /* DOS extensions are <= 3 chars*/
    }
    else
    {
      dwNameLen++;
      if (dwNameLen > 8)
        return TRUE; /* DOS names are <= 8 chars */
    }
    lpszPath++;
  }
  return FALSE; /* Valid DOS path */
}

/*************************************************************************
 * PathIsDirectoryEmptyA [SHLWAPI.@]
 *
 * Determine if a given directory is empty.
 *
 * PARAMS
 *  lpszPath [I] Directory to check
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3724
 *  TRUE  If the directory exists and contains no files,
3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735
 *  FALSE Otherwise
 */
BOOL WINAPI PathIsDirectoryEmptyA(LPCSTR lpszPath)
{
  BOOL bRet = FALSE;

  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (lpszPath)
  {
    WCHAR szPath[MAX_PATH];
3736
    MultiByteToWideChar(CP_ACP,0,lpszPath,-1,szPath,MAX_PATH);
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760
    bRet = PathIsDirectoryEmptyW(szPath);
  }
  return bRet;
}

/*************************************************************************
 * PathIsDirectoryEmptyW [SHLWAPI.@]
 *
 * See PathIsDirectoryEmptyA.
 */
BOOL WINAPI PathIsDirectoryEmptyW(LPCWSTR lpszPath)
{
  static const WCHAR szAllFiles[] = { '*', '.', '*', '\0' };
  WCHAR szSearch[MAX_PATH];
  DWORD dwLen;
  HANDLE hfind;
  BOOL retVal = FALSE;
  WIN32_FIND_DATAW find_data;

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

  if (!lpszPath || !PathIsDirectoryW(lpszPath))
      return FALSE;

3761
  lstrcpynW(szSearch, lpszPath, MAX_PATH);
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793
  PathAddBackslashW(szSearch);
  dwLen = strlenW(szSearch);
  if (dwLen > MAX_PATH - 4)
    return FALSE;

  strcpyW(szSearch + dwLen, szAllFiles);
  hfind = FindFirstFileW(szSearch, &find_data);

  if (hfind != INVALID_HANDLE_VALUE &&
      find_data.cFileName[0] == '.' &&
      find_data.cFileName[1] == '.')
  {
    /* The only directory entry should be the parent */
    if (!FindNextFileW(hfind, &find_data))
      retVal = TRUE;
    FindClose(hfind);
  }
  return retVal;
}


/*************************************************************************
 * PathFindSuffixArrayA [SHLWAPI.@]
 *
 * Find a suffix string in an array of suffix strings
 *
 * PARAMS
 *  lpszSuffix [I] Suffix string to search for
 *  lppszArray [I] Array of suffix strings to search
 *  dwCount    [I] Number of elements in lppszArray
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
3794 3795
 *  Success: The index of the position of lpszSuffix in lppszArray
 *  Failure: 0, if any parameters are invalid or lpszSuffix is not found
3796 3797
 *
 * NOTES
3798 3799
 *  The search is case sensitive.
 *  The match is made against the end of the suffix string, so for example:
Jon Griffiths's avatar
Jon Griffiths committed
3800
 *  lpszSuffix="fooBAR" matches "BAR", but lpszSuffix="fooBARfoo" does not.
3801
 */
3802
LPCSTR WINAPI PathFindSuffixArrayA(LPCSTR lpszSuffix, LPCSTR *lppszArray, int dwCount)
3803
{
Jon Griffiths's avatar
Jon Griffiths committed
3804
  size_t dwLen;
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
  int dwRet = 0;

  TRACE("(%s,%p,%d)\n",debugstr_a(lpszSuffix), lppszArray, dwCount);

  if (lpszSuffix && lppszArray && dwCount > 0)
  {
    dwLen = strlen(lpszSuffix);

    while (dwRet < dwCount)
    {
Jon Griffiths's avatar
Jon Griffiths committed
3815
      size_t dwCompareLen = strlen(*lppszArray);
3816 3817 3818
      if (dwCompareLen < dwLen)
      {
        if (!strcmp(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3819
          return *lppszArray; /* Found */
3820 3821 3822 3823 3824
      }
      dwRet++;
      lppszArray++;
    }
  }
3825
  return NULL;
3826 3827 3828
}

/*************************************************************************
3829 3830 3831
 * PathFindSuffixArrayW [SHLWAPI.@]
 *
 * See PathFindSuffixArrayA.
3832
 */
3833
LPCWSTR WINAPI PathFindSuffixArrayW(LPCWSTR lpszSuffix, LPCWSTR *lppszArray, int dwCount)
3834
{
Jon Griffiths's avatar
Jon Griffiths committed
3835
  size_t dwLen;
3836 3837 3838 3839 3840 3841 3842 3843 3844 3845
  int dwRet = 0;

  TRACE("(%s,%p,%d)\n",debugstr_w(lpszSuffix), lppszArray, dwCount);

  if (lpszSuffix && lppszArray && dwCount > 0)
  {
    dwLen = strlenW(lpszSuffix);

    while (dwRet < dwCount)
    {
Jon Griffiths's avatar
Jon Griffiths committed
3846
      size_t dwCompareLen = strlenW(*lppszArray);
3847 3848 3849
      if (dwCompareLen < dwLen)
      {
        if (!strcmpW(lpszSuffix + dwLen - dwCompareLen, *lppszArray))
3850
          return *lppszArray; /* Found */
3851 3852 3853 3854 3855
      }
      dwRet++;
      lppszArray++;
    }
  }
3856
  return NULL;
3857 3858 3859 3860 3861 3862 3863 3864
}

/*************************************************************************
 * PathUndecorateA [SHLWAPI.@]
 *
 * Undecorate a file path
 *
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
3865
 *  lpszPath [I/O] Path to remove any decoration from
3866 3867 3868 3869 3870
 *
 * RETURNS
 *  Nothing
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
3871
 *  A decorations form is "path[n].ext" where "n" is an optional decimal number.
3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
 */
VOID WINAPI PathUndecorateA(LPSTR lpszPath)
{
  TRACE("(%s)\n",debugstr_a(lpszPath));

  if (lpszPath)
  {
    LPSTR lpszExt = PathFindExtensionA(lpszPath);
    if (lpszExt > lpszPath && lpszExt[-1] == ']')
    {
      LPSTR lpszSkip = lpszExt - 2;
      if (*lpszSkip == '[')
        lpszSkip++;  /* [] (no number) */
      else
        while (lpszSkip > lpszPath && isdigit(lpszSkip[-1]))
          lpszSkip--;
      if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
      {
        /* remove the [n] */
        lpszSkip--;
        while (*lpszExt)
          *lpszSkip++ = *lpszExt++;
        *lpszSkip = '\0';
      }
    }
  }
}

/*************************************************************************
 * PathUndecorateW [SHLWAPI.@]
 *
 * See PathUndecorateA.
 */
VOID WINAPI PathUndecorateW(LPWSTR lpszPath)
{
  TRACE("(%s)\n",debugstr_w(lpszPath));

  if (lpszPath)
  {
    LPWSTR lpszExt = PathFindExtensionW(lpszPath);
    if (lpszExt > lpszPath && lpszExt[-1] == ']')
    {
      LPWSTR lpszSkip = lpszExt - 2;
      if (*lpszSkip == '[')
        lpszSkip++; /* [] (no number) */
      else
        while (lpszSkip > lpszPath && isdigitW(lpszSkip[-1]))
          lpszSkip--;
      if (lpszSkip > lpszPath && lpszSkip[-1] == '[' && lpszSkip[-2] != '\\')
      {
        /* remove the [n] */
        lpszSkip--;
        while (*lpszExt)
          *lpszSkip++ = *lpszExt++;
        *lpszSkip = '\0';
      }
    }
  }
3930
}
Jon Griffiths's avatar
Jon Griffiths committed
3931

3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
/*************************************************************************
 * PathUnExpandEnvStringsA [SHLWAPI.@]
 *
 * Substitute folder names in a path with their corresponding environment
 * strings.
 *
 * PARAMS
 *  pszPath  [I] Buffer containing the path to unexpand.
 *  pszBuf   [O] Buffer to receive the unexpanded path.
 *  cchBuf   [I] Size of pszBuf in characters.
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
BOOL WINAPI PathUnExpandEnvStringsA(LPCSTR pszPath, LPSTR pszBuf, UINT cchBuf)
{
    FIXME("(%s,%s,0x%08x)\n", debugstr_a(pszPath), debugstr_a(pszBuf), cchBuf);
    return FALSE;
}

/*************************************************************************
 * PathUnExpandEnvStringsW [SHLWAPI.@]
 *
 * Unicode version of PathUnExpandEnvStringsA.
 */
BOOL WINAPI PathUnExpandEnvStringsW(LPCWSTR pszPath, LPWSTR pszBuf, UINT cchBuf)
{
    FIXME("(%s,%s,0x%08x)\n", debugstr_w(pszPath), debugstr_w(pszBuf), cchBuf);
    return FALSE;
}

Jon Griffiths's avatar
Jon Griffiths committed
3964
/*************************************************************************
3965
 * @     [SHLWAPI.440]
Jon Griffiths's avatar
Jon Griffiths committed
3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977
 *
 * Find localised or default web content in "%WINDOWS%\web\".
 *
 * PARAMS
 *  lpszFile  [I] File name containing content to look for
 *  lpszPath  [O] Buffer to contain the full path to the file
 *  dwPathLen [I] Length of lpszPath
 *
 * RETURNS
 *  Success: S_OK. lpszPath contains the full path to the content.
 *  Failure: E_FAIL. The content does not exist or lpszPath is too short.
 */
3978
HRESULT WINAPI SHGetWebFolderFilePathA(LPCSTR lpszFile, LPSTR lpszPath, DWORD dwPathLen)
Jon Griffiths's avatar
Jon Griffiths committed
3979 3980 3981 3982
{
  WCHAR szFile[MAX_PATH], szPath[MAX_PATH];
  HRESULT hRet;

3983
  TRACE("(%s,%p,%d)\n", lpszFile, lpszPath, dwPathLen);
Jon Griffiths's avatar
Jon Griffiths committed
3984

3985
  MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, szFile, MAX_PATH);
Jon Griffiths's avatar
Jon Griffiths committed
3986
  szPath[0] = '\0';
3987
  hRet = SHGetWebFolderFilePathW(szFile, szPath, dwPathLen);
3988
  WideCharToMultiByte(CP_ACP, 0, szPath, -1, lpszPath, dwPathLen, 0, 0);
Jon Griffiths's avatar
Jon Griffiths committed
3989 3990 3991 3992
  return hRet;
}

/*************************************************************************
3993
 * @     [SHLWAPI.441]
Jon Griffiths's avatar
Jon Griffiths committed
3994
 *
3995
 * Unicode version of SHGetWebFolderFilePathA.
Jon Griffiths's avatar
Jon Griffiths committed
3996
 */
3997
HRESULT WINAPI SHGetWebFolderFilePathW(LPCWSTR lpszFile, LPWSTR lpszPath, DWORD dwPathLen)
Jon Griffiths's avatar
Jon Griffiths committed
3998 3999 4000 4001 4002 4003 4004 4005
{
  static const WCHAR szWeb[] = {'\\','W','e','b','\\','\0'};
  static const WCHAR szWebMui[] = {'m','u','i','\\','%','0','4','x','\\','\0'};
#define szWebLen (sizeof(szWeb)/sizeof(WCHAR))
#define szWebMuiLen ((sizeof(szWebMui)+1)/sizeof(WCHAR))
  DWORD dwLen, dwFileLen;
  LANGID lidSystem, lidUser;

4006
  TRACE("(%s,%p,%d)\n", debugstr_w(lpszFile), lpszPath, dwPathLen);
Jon Griffiths's avatar
Jon Griffiths committed
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042

  /* Get base directory for web content */
  dwLen = GetSystemWindowsDirectoryW(lpszPath, dwPathLen);
  if (dwLen > 0 && lpszPath[dwLen-1] == '\\')
    dwLen--;

  dwFileLen = strlenW(lpszFile);

  if (dwLen + dwFileLen + szWebLen >= dwPathLen)
    return E_FAIL; /* lpszPath too short */

  strcpyW(lpszPath+dwLen, szWeb);
  dwLen += szWebLen;
  dwPathLen = dwPathLen - dwLen; /* Remaining space */

  lidSystem = GetSystemDefaultUILanguage();
  lidUser = GetUserDefaultUILanguage();

  if (lidSystem != lidUser)
  {
    if (dwFileLen + szWebMuiLen < dwPathLen)
    {
      /* Use localised content in the users UI language if present */
      wsprintfW(lpszPath + dwLen, szWebMui, lidUser);
      strcpyW(lpszPath + dwLen + szWebMuiLen, lpszFile);
      if (PathFileExistsW(lpszPath))
        return S_OK;
    }
  }

  /* Fall back to OS default installed content */
  strcpyW(lpszPath + dwLen, lpszFile);
  if (PathFileExistsW(lpszPath))
    return S_OK;
  return E_FAIL;
}
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150

#define PATH_CHAR_CLASS_LETTER      0x00000001
#define PATH_CHAR_CLASS_ASTERIX     0x00000002
#define PATH_CHAR_CLASS_DOT         0x00000004
#define PATH_CHAR_CLASS_BACKSLASH   0x00000008
#define PATH_CHAR_CLASS_COLON       0x00000010
#define PATH_CHAR_CLASS_SEMICOLON   0x00000020
#define PATH_CHAR_CLASS_COMMA       0x00000040
#define PATH_CHAR_CLASS_SPACE       0x00000080
#define PATH_CHAR_CLASS_OTHER_VALID 0x00000100
#define PATH_CHAR_CLASS_DOUBLEQUOTE 0x00000200

#define PATH_CHAR_CLASS_INVALID     0x00000000
#define PATH_CHAR_CLASS_ANY         0xffffffff

static const DWORD SHELL_charclass[] =
{
    /* 0x00 */  PATH_CHAR_CLASS_INVALID,      /* 0x01 */  PATH_CHAR_CLASS_INVALID,
    /* 0x02 */  PATH_CHAR_CLASS_INVALID,      /* 0x03 */  PATH_CHAR_CLASS_INVALID,
    /* 0x04 */  PATH_CHAR_CLASS_INVALID,      /* 0x05 */  PATH_CHAR_CLASS_INVALID,
    /* 0x06 */  PATH_CHAR_CLASS_INVALID,      /* 0x07 */  PATH_CHAR_CLASS_INVALID,
    /* 0x08 */  PATH_CHAR_CLASS_INVALID,      /* 0x09 */  PATH_CHAR_CLASS_INVALID,
    /* 0x0a */  PATH_CHAR_CLASS_INVALID,      /* 0x0b */  PATH_CHAR_CLASS_INVALID,
    /* 0x0c */  PATH_CHAR_CLASS_INVALID,      /* 0x0d */  PATH_CHAR_CLASS_INVALID,
    /* 0x0e */  PATH_CHAR_CLASS_INVALID,      /* 0x0f */  PATH_CHAR_CLASS_INVALID,
    /* 0x10 */  PATH_CHAR_CLASS_INVALID,      /* 0x11 */  PATH_CHAR_CLASS_INVALID,
    /* 0x12 */  PATH_CHAR_CLASS_INVALID,      /* 0x13 */  PATH_CHAR_CLASS_INVALID,
    /* 0x14 */  PATH_CHAR_CLASS_INVALID,      /* 0x15 */  PATH_CHAR_CLASS_INVALID,
    /* 0x16 */  PATH_CHAR_CLASS_INVALID,      /* 0x17 */  PATH_CHAR_CLASS_INVALID,
    /* 0x18 */  PATH_CHAR_CLASS_INVALID,      /* 0x19 */  PATH_CHAR_CLASS_INVALID,
    /* 0x1a */  PATH_CHAR_CLASS_INVALID,      /* 0x1b */  PATH_CHAR_CLASS_INVALID,
    /* 0x1c */  PATH_CHAR_CLASS_INVALID,      /* 0x1d */  PATH_CHAR_CLASS_INVALID,
    /* 0x1e */  PATH_CHAR_CLASS_INVALID,      /* 0x1f */  PATH_CHAR_CLASS_INVALID,
    /* ' '  */  PATH_CHAR_CLASS_SPACE,        /* '!'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '"'  */  PATH_CHAR_CLASS_DOUBLEQUOTE,  /* '#'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '$'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '%'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '&'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '\'' */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '('  */  PATH_CHAR_CLASS_OTHER_VALID,  /* ')'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '*'  */  PATH_CHAR_CLASS_ASTERIX,      /* '+'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* ','  */  PATH_CHAR_CLASS_COMMA,        /* '-'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '.'  */  PATH_CHAR_CLASS_DOT,          /* '/'  */  PATH_CHAR_CLASS_INVALID,
    /* '0'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '1'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '2'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '3'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '4'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '5'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '6'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '7'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '8'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '9'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* ':'  */  PATH_CHAR_CLASS_COLON,        /* ';'  */  PATH_CHAR_CLASS_SEMICOLON,
    /* '<'  */  PATH_CHAR_CLASS_INVALID,      /* '='  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '>'  */  PATH_CHAR_CLASS_INVALID,      /* '?'  */  PATH_CHAR_CLASS_LETTER,
    /* '@'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* 'A'  */  PATH_CHAR_CLASS_ANY,
    /* 'B'  */  PATH_CHAR_CLASS_ANY,          /* 'C'  */  PATH_CHAR_CLASS_ANY,
    /* 'D'  */  PATH_CHAR_CLASS_ANY,          /* 'E'  */  PATH_CHAR_CLASS_ANY,
    /* 'F'  */  PATH_CHAR_CLASS_ANY,          /* 'G'  */  PATH_CHAR_CLASS_ANY,
    /* 'H'  */  PATH_CHAR_CLASS_ANY,          /* 'I'  */  PATH_CHAR_CLASS_ANY,
    /* 'J'  */  PATH_CHAR_CLASS_ANY,          /* 'K'  */  PATH_CHAR_CLASS_ANY,
    /* 'L'  */  PATH_CHAR_CLASS_ANY,          /* 'M'  */  PATH_CHAR_CLASS_ANY,
    /* 'N'  */  PATH_CHAR_CLASS_ANY,          /* 'O'  */  PATH_CHAR_CLASS_ANY,
    /* 'P'  */  PATH_CHAR_CLASS_ANY,          /* 'Q'  */  PATH_CHAR_CLASS_ANY,
    /* 'R'  */  PATH_CHAR_CLASS_ANY,          /* 'S'  */  PATH_CHAR_CLASS_ANY,
    /* 'T'  */  PATH_CHAR_CLASS_ANY,          /* 'U'  */  PATH_CHAR_CLASS_ANY,
    /* 'V'  */  PATH_CHAR_CLASS_ANY,          /* 'W'  */  PATH_CHAR_CLASS_ANY,
    /* 'X'  */  PATH_CHAR_CLASS_ANY,          /* 'Y'  */  PATH_CHAR_CLASS_ANY,
    /* 'Z'  */  PATH_CHAR_CLASS_ANY,          /* '['  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '\\' */  PATH_CHAR_CLASS_BACKSLASH,    /* ']'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '^'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* '_'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '`'  */  PATH_CHAR_CLASS_OTHER_VALID,  /* 'a'  */  PATH_CHAR_CLASS_ANY,
    /* 'b'  */  PATH_CHAR_CLASS_ANY,          /* 'c'  */  PATH_CHAR_CLASS_ANY,
    /* 'd'  */  PATH_CHAR_CLASS_ANY,          /* 'e'  */  PATH_CHAR_CLASS_ANY,
    /* 'f'  */  PATH_CHAR_CLASS_ANY,          /* 'g'  */  PATH_CHAR_CLASS_ANY,
    /* 'h'  */  PATH_CHAR_CLASS_ANY,          /* 'i'  */  PATH_CHAR_CLASS_ANY,
    /* 'j'  */  PATH_CHAR_CLASS_ANY,          /* 'k'  */  PATH_CHAR_CLASS_ANY,
    /* 'l'  */  PATH_CHAR_CLASS_ANY,          /* 'm'  */  PATH_CHAR_CLASS_ANY,
    /* 'n'  */  PATH_CHAR_CLASS_ANY,          /* 'o'  */  PATH_CHAR_CLASS_ANY,
    /* 'p'  */  PATH_CHAR_CLASS_ANY,          /* 'q'  */  PATH_CHAR_CLASS_ANY,
    /* 'r'  */  PATH_CHAR_CLASS_ANY,          /* 's'  */  PATH_CHAR_CLASS_ANY,
    /* 't'  */  PATH_CHAR_CLASS_ANY,          /* 'u'  */  PATH_CHAR_CLASS_ANY,
    /* 'v'  */  PATH_CHAR_CLASS_ANY,          /* 'w'  */  PATH_CHAR_CLASS_ANY,
    /* 'x'  */  PATH_CHAR_CLASS_ANY,          /* 'y'  */  PATH_CHAR_CLASS_ANY,
    /* 'z'  */  PATH_CHAR_CLASS_ANY,          /* '{'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '|'  */  PATH_CHAR_CLASS_INVALID,      /* '}'  */  PATH_CHAR_CLASS_OTHER_VALID,
    /* '~'  */  PATH_CHAR_CLASS_OTHER_VALID
};

/*************************************************************************
 * @     [SHLWAPI.455]
 *
 * Check if an ASCII char is of a certain class
 */
BOOL WINAPI PathIsValidCharA( char c, DWORD class )
{
    if ((unsigned)c > 0x7e)
        return class & PATH_CHAR_CLASS_OTHER_VALID;

    return class & SHELL_charclass[(unsigned)c];
}

/*************************************************************************
 * @     [SHLWAPI.456]
 *
 * Check if a Unicode char is of a certain class
 */
BOOL WINAPI PathIsValidCharW( WCHAR c, DWORD class )
{
    if (c > 0x7e)
        return class & PATH_CHAR_CLASS_OTHER_VALID;

    return class & SHELL_charclass[c];
}