path.c 23.2 KB
Newer Older
1 2 3
/*
 * Ntdll path functions
 *
4 5
 * Copyright 2002, 2003 Alexandre Julliard
 * Copyright 2003 Eric Pouech
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

24 25 26 27 28
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winreg.h"
29 30
#include "winternl.h"
#include "wine/unicode.h"
31
#include "wine/debug.h"
32
#include "ntdll_misc.h"
33 34

WINE_DEFAULT_DEBUG_CHANNEL(file);
35

36 37 38 39
static const WCHAR DeviceRootW[] = {'\\','\\','.','\\',0};
static const WCHAR NTDosPrefixW[] = {'\\','?','?','\\',0};
static const WCHAR UncPfxW[] = {'U','N','C','\\',0};

40 41 42 43 44
/* FIXME: hack! */
HANDLE (WINAPI *pCreateFileW)( LPCWSTR filename, DWORD access, DWORD sharing,
                               LPSECURITY_ATTRIBUTES sa, DWORD creation,
                               DWORD attributes, HANDLE template );

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#define IS_SEPARATOR(ch)  ((ch) == '\\' || (ch) == '/')

/***********************************************************************
 *             RtlDetermineDosPathNameType_U   (NTDLL.@)
 */
DOS_PATHNAME_TYPE WINAPI RtlDetermineDosPathNameType_U( PCWSTR path )
{
    if (IS_SEPARATOR(path[0]))
    {
        if (!IS_SEPARATOR(path[1])) return ABSOLUTE_PATH;       /* "/foo" */
        if (path[2] != '.') return UNC_PATH;                    /* "//foo" */
        if (IS_SEPARATOR(path[3])) return DEVICE_PATH;          /* "//./foo" */
        if (path[3]) return UNC_PATH;                           /* "//.foo" */
        return UNC_DOT_PATH;                                    /* "//." */
    }
    else
    {
        if (!path[0] || path[1] != ':') return RELATIVE_PATH;   /* "foo" */
        if (IS_SEPARATOR(path[2])) return ABSOLUTE_DRIVE_PATH;  /* "c:/foo" */
        return RELATIVE_DRIVE_PATH;                             /* "c:foo" */
    }
}

Eric Pouech's avatar
Eric Pouech committed
68 69 70
/******************************************************************
 *		RtlDoesFileExists_U
 *
71
 * FIXME: should not use CreateFileW
Eric Pouech's avatar
Eric Pouech committed
72 73
 */
BOOLEAN WINAPI RtlDoesFileExists_U(LPCWSTR file_name)
74
{
75 76
    HANDLE handle = pCreateFileW( file_name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
                                  NULL, OPEN_EXISTING, 0, 0 );
77 78
    if (handle == INVALID_HANDLE_VALUE) return FALSE;
    NtClose( handle );
Eric Pouech's avatar
Eric Pouech committed
79 80 81
    return TRUE;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/***********************************************************************
 *             RtlIsDosDeviceName_U   (NTDLL.@)
 *
 * Check if the given DOS path contains a DOS device name.
 *
 * Returns the length of the device name in the low word and its
 * position in the high word (both in bytes, not WCHARs), or 0 if no
 * device name is found.
 */
ULONG WINAPI RtlIsDosDeviceName_U( PCWSTR dos_name )
{
    static const WCHAR consoleW[] = {'\\','\\','.','\\','C','O','N',0};
    static const WCHAR auxW[3] = {'A','U','X'};
    static const WCHAR comW[3] = {'C','O','M'};
    static const WCHAR conW[3] = {'C','O','N'};
    static const WCHAR lptW[3] = {'L','P','T'};
    static const WCHAR nulW[3] = {'N','U','L'};
    static const WCHAR prnW[3] = {'P','R','N'};

    const WCHAR *start, *end, *p;

    switch(RtlDetermineDosPathNameType_U( dos_name ))
    {
    case INVALID_PATH:
    case UNC_PATH:
        return 0;
    case DEVICE_PATH:
        if (!strcmpiW( dos_name, consoleW ))
            return MAKELONG( sizeof(conW), 4 * sizeof(WCHAR) );  /* 4 is length of \\.\ prefix */
        return 0;
    default:
        break;
    }

    end = dos_name + strlenW(dos_name) - 1;
    if (end >= dos_name && *end == ':') end--;  /* remove trailing ':' */

    /* find start of file name */
    for (start = end; start >= dos_name; start--)
    {
        if (IS_SEPARATOR(start[0])) break;
        /* check for ':' but ignore if before extension (for things like NUL:.txt) */
        if (start[0] == ':' && start[1] != '.') break;
    }
    start++;

    /* remove extension */
    if ((p = strchrW( start, '.' )))
    {
        end = p - 1;
        if (end >= dos_name && *end == ':') end--;  /* remove trailing ':' before extension */
    }
    else
    {
        /* no extension, remove trailing spaces */
        while (end >= dos_name && *end == ' ') end--;
    }

    /* now we have a potential device name between start and end, check it */
    switch(end - start + 1)
    {
    case 3:
        if (strncmpiW( start, auxW, 3 ) &&
            strncmpiW( start, conW, 3 ) &&
            strncmpiW( start, nulW, 3 ) &&
            strncmpiW( start, prnW, 3 )) break;
        return MAKELONG( 3 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
    case 4:
        if (strncmpiW( start, comW, 3 ) && strncmpiW( start, lptW, 3 )) break;
        if (*end <= '0' || *end > '9') break;
        return MAKELONG( 4 * sizeof(WCHAR), (start - dos_name) * sizeof(WCHAR) );
    default:  /* can't match anything */
        break;
    }
    return 0;
}
158 159


160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/**************************************************************************
 *                 RtlDosPathNameToNtPathName_U		[NTDLL.@]
 *
 * dos_path: a DOS path name (fully qualified or not)
 * ntpath:   pointer to a UNICODE_STRING to hold the converted
 *           path name
 * file_part:will point (in ntpath) to the file part in the path
 * cd:       directory reference (optional)
 *
 * FIXME:
 *      + fill the cd structure
 */
BOOLEAN  WINAPI RtlDosPathNameToNtPathName_U(PWSTR dos_path,
                                             PUNICODE_STRING ntpath,
                                             PWSTR* file_part,
                                             CURDIR* cd)
{
    static const WCHAR LongFileNamePfxW[4] = {'\\','\\','?','\\'};
    ULONG sz, ptr_sz, offset;
    WCHAR local[MAX_PATH];
    LPWSTR ptr;

    TRACE("(%s,%p,%p,%p)\n",
          debugstr_w(dos_path), ntpath, file_part, cd);

    if (cd)
    {
        FIXME("Unsupported parameter\n");
        memset(cd, 0, sizeof(*cd));
    }

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

    if (!memcmp(dos_path, LongFileNamePfxW, sizeof(LongFileNamePfxW)))
    {
        dos_path += sizeof(LongFileNamePfxW) / sizeof(WCHAR);
        ptr = NULL;
        ptr_sz = 0;
    }
    else
    {
        ptr = local;
        ptr_sz = sizeof(local);
    }
    sz = RtlGetFullPathName_U(dos_path, ptr_sz, ptr, file_part);
    if (sz == 0) return FALSE;
    if (sz > ptr_sz)
    {
        ptr = RtlAllocateHeap(ntdll_get_process_heap(), 0, sz);
        sz = RtlGetFullPathName_U(dos_path, sz, ptr, file_part);
    }

    ntpath->MaximumLength = sz + (4 /* unc\ */ + 4 /* \??\ */) * sizeof(WCHAR);
    ntpath->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, ntpath->MaximumLength);
    if (!ntpath->Buffer)
    {
        if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
        return FALSE;
    }

    strcpyW(ntpath->Buffer, NTDosPrefixW);
    offset = 0;
    switch (RtlDetermineDosPathNameType_U(ptr))
    {
    case UNC_PATH: /* \\foo */
        if (ptr[2] != '?')
        {
            offset = 2;
            strcatW(ntpath->Buffer, UncPfxW);
        }
        break;
    case DEVICE_PATH: /* \\.\foo */
        offset = 4;
        break;
    default: break; /* needed to keep gcc quiet */
    }

    strcatW(ntpath->Buffer, ptr + offset);
    ntpath->Length = strlenW(ntpath->Buffer) * sizeof(WCHAR);

    if (file_part && *file_part)
241
        *file_part = ntpath->Buffer + ntpath->Length / sizeof(WCHAR) - strlenW(*file_part);
242 243 244 245 246 247 248

    /* FIXME: cd filling */

    if (ptr != local) RtlFreeHeap(ntdll_get_process_heap(), 0, ptr);
    return TRUE;
}

Eric Pouech's avatar
Eric Pouech committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/******************************************************************
 *		RtlDosSearchPath_U
 *
 * Searchs a file of name 'name' into a ';' separated list of paths
 * (stored in paths)
 * Doesn't seem to search elsewhere than the paths list
 * Stores the result in buffer (file_part will point to the position
 * of the file name in the buffer)
 * FIXME:
 * - how long shall the paths be ??? (MAX_PATH or larger with \\?\ constructs ???)
 */
ULONG WINAPI RtlDosSearchPath_U(LPCWSTR paths, LPCWSTR search, LPCWSTR ext, 
                                ULONG buffer_size, LPWSTR buffer, 
                                LPWSTR* file_part)
{
    DOS_PATHNAME_TYPE type = RtlDetermineDosPathNameType_U(search);
    ULONG len = 0;

    if (type == RELATIVE_PATH)
    {
        ULONG allocated = 0, needed, filelen;
270
        WCHAR *name = NULL;
Eric Pouech's avatar
Eric Pouech committed
271 272 273

        filelen = 1 /* for \ */ + strlenW(search) + 1 /* \0 */;

274 275
        /* Windows only checks for '.' without worrying about path components */
        if (strchrW( search, '.' )) ext = NULL;
Eric Pouech's avatar
Eric Pouech committed
276 277 278 279 280 281 282 283 284
        if (ext != NULL) filelen += strlenW(ext);

        while (*paths)
        {
            LPCWSTR ptr;

            for (needed = 0, ptr = paths; *ptr != 0 && *ptr++ != ';'; needed++);
            if (needed + filelen > allocated)
            {
285 286 287 288 289 290 291 292 293
                if (!name) name = RtlAllocateHeap(GetProcessHeap(), 0,
                                                  (needed + filelen) * sizeof(WCHAR));
                else
                {
                    WCHAR *newname = RtlReAllocateHeap(GetProcessHeap(), 0, name,
                                                       (needed + filelen) * sizeof(WCHAR));
                    if (!newname) RtlFreeHeap(GetProcessHeap(), 0, name);
                    name = newname;
                }
Eric Pouech's avatar
Eric Pouech committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
                if (!name) return 0;
                allocated = needed + filelen;
            }
            memmove(name, paths, needed * sizeof(WCHAR));
            /* append '\\' if none is present */
            if (needed > 0 && name[needed - 1] != '\\') name[needed++] = '\\';
            strcpyW(&name[needed], search);
            if (ext) strcatW(&name[needed], ext);
            if (RtlDoesFileExists_U(name))
            {
                len = RtlGetFullPathName_U(name, buffer_size, buffer, file_part);
                break;
            }
            paths = ptr;
        }
        RtlFreeHeap(ntdll_get_process_heap(), 0, name);
    }
    else if (RtlDoesFileExists_U(search))
    {
        len = RtlGetFullPathName_U(search, buffer_size, buffer, file_part);
    }

    return len;
}

319 320 321 322
/******************************************************************
 *		get_full_path_helper
 *
 * Helper for RtlGetFullPathName_U
323
 * Note: name and buffer are allowed to point to the same memory spot
324 325 326
 */
static ULONG get_full_path_helper(LPCWSTR name, LPWSTR buffer, ULONG size)
{
327 328 329 330 331 332
    ULONG                       reqsize = 0, mark = 0, dep = 0, deplen;
    DOS_PATHNAME_TYPE           type;
    LPWSTR                      ptr, ins_str = NULL;
    const UNICODE_STRING*       cd;
    WCHAR                       tmp[4];
    
333
    RtlAcquirePebLock();
334

335 336 337 338 339 340 341 342 343
    cd = &ntdll_get_process_pmts()->CurrentDirectoryName;

    switch (type = RtlDetermineDosPathNameType_U(name))
    {
    case UNC_PATH:              /* \\foo   */
    case DEVICE_PATH:           /* \\.\foo */
        break;

    case ABSOLUTE_DRIVE_PATH:   /* c:\foo  */
344 345 346 347
        reqsize = sizeof(WCHAR);
        tmp[0] = toupperW(name[0]);
        ins_str = tmp;
        dep = 1;
348 349 350
        break;

    case RELATIVE_DRIVE_PATH:   /* c:foo   */
351
        dep = 2;
352 353 354 355
        if (toupperW(name[0]) != toupperW(cd->Buffer[0]) || cd->Buffer[1] != ':')
        {
            UNICODE_STRING      var, val;

356 357 358 359 360 361 362
            tmp[0] = '=';
            tmp[1] = name[0];
            tmp[2] = ':';
            tmp[3] = '\0';
            var.Length = 3 * sizeof(WCHAR);
            var.MaximumLength = 4 * sizeof(WCHAR);
            var.Buffer = tmp;
363 364
            val.Length = 0;
            val.MaximumLength = size;
365
            val.Buffer = RtlAllocateHeap(GetProcessHeap(), 0, size);
366 367 368 369 370 371 372 373 374 375 376

            switch (RtlQueryEnvironmentVariable_U(NULL, &var, &val))
            {
            case STATUS_SUCCESS:
                /* FIXME: Win2k seems to check that the environment variable actually points 
                 * to an existing directory. If not, root of the drive is used
                 * (this seems also to be the only spot in RtlGetFullPathName that the 
                 * existence of a part of a path is checked)
                 */
                /* fall thru */
            case STATUS_BUFFER_TOO_SMALL:
377 378 379
                reqsize = val.Length + sizeof(WCHAR); /* append trailing '\\' */
                val.Buffer[val.Length / sizeof(WCHAR)] = '\\';
                ins_str = val.Buffer;
380 381
                break;
            case STATUS_VARIABLE_NOT_FOUND:
382 383 384 385 386
                reqsize = 3 * sizeof(WCHAR);
                tmp[0] = name[0];
                tmp[1] = ':';
                tmp[2] = '\\';
                ins_str = tmp;
387 388 389 390 391 392 393 394 395 396
                break;
            default:
                ERR("Unsupported status code\n");
                break;
            }
            break;
        }
        /* fall through */

    case RELATIVE_PATH:         /* foo     */
397 398
        reqsize = cd->Length;
        ins_str = cd->Buffer;
399 400 401 402 403 404 405
        if (cd->Buffer[1] != ':')
        {
            ptr = strchrW(cd->Buffer + 2, '\\');
            if (ptr) ptr = strchrW(ptr + 1, '\\');
            if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
            mark = ptr - cd->Buffer;
        }
406 407 408 409 410
        break;

    case ABSOLUTE_PATH:         /* \xxx    */
        if (cd->Buffer[1] == ':')
        {
411 412 413 414
            reqsize = 2 * sizeof(WCHAR);
            tmp[0] = cd->Buffer[0];
            tmp[1] = ':';
            ins_str = tmp;
415 416 417 418 419 420
        }
        else
        {
            ptr = strchrW(cd->Buffer + 2, '\\');
            if (ptr) ptr = strchrW(ptr + 1, '\\');
            if (!ptr) ptr = cd->Buffer + strlenW(cd->Buffer);
421 422 423
            reqsize = (ptr - cd->Buffer) * sizeof(WCHAR);
            mark = reqsize / sizeof(WCHAR);
            ins_str = cd->Buffer;
424 425 426 427
        }
        break;

    case UNC_DOT_PATH:         /* \\.     */
428 429 430 431 432 433 434
        reqsize = 4 * sizeof(WCHAR);
        dep = 3;
        tmp[0] = '\\';
        tmp[1] = '\\';
        tmp[2] = '.';
        tmp[3] = '\\';
        ins_str = tmp;
435 436 437 438 439 440
        break;

    case INVALID_PATH:
        goto done;
    }

441 442 443 444 445 446 447 448
    /* enough space ? */
    deplen = strlenW(name + dep) * sizeof(WCHAR);
    if (reqsize + deplen + sizeof(WCHAR) > size)
    {
        /* not enough space, return need size (including terminating '\0') */
        reqsize += deplen + sizeof(WCHAR);
        goto done;
    }
449

450 451 452
    memmove(buffer + reqsize / sizeof(WCHAR), name + dep, deplen + sizeof(WCHAR));
    if (reqsize) memcpy(buffer, ins_str, reqsize);
    reqsize += deplen;
453

454 455
    if (ins_str && ins_str != tmp && ins_str != cd->Buffer)
        RtlFreeHeap(GetProcessHeap(), 0, ins_str);
456

457 458
    /* convert every / into a \ */
    for (ptr = buffer; *ptr; ptr++) if (*ptr == '/') *ptr = '\\';
459 460 461 462 463 464

    /* mark is non NULL for UNC names, so start path collapsing after server & share name
     * otherwise, it's a fully qualified DOS name, so start after the drive designation
     */
    for (ptr = buffer + (mark ? mark : 2); ptr < buffer + reqsize / sizeof(WCHAR); )
    {
465 466
        LPWSTR  prev, p = strchrW(ptr, '\\');

467 468 469 470 471 472 473 474 475 476 477
        if (!p) break;

        p++;
        if (p[0] == '.')
        {
            switch (p[1])
            {
            case '.':
                switch (p[2])
                {
                case '\\':
478 479 480 481 482 483 484
                    prev = p - 2;
                    while (prev >= buffer + mark && *prev != '\\') prev--;
                    /* either collapse \foo\.. into \ or \.. into \ */
                    if (prev < buffer + mark) prev = p - 1;
                    reqsize -= (p + 2 - prev) * sizeof(WCHAR);
                    memmove(prev, p + 2, reqsize + sizeof(WCHAR) - (prev - buffer) * sizeof(WCHAR));
                    p = prev;
485 486 487 488 489 490 491 492 493
                    break;
                case '\0':
                    reqsize -= 2 * sizeof(WCHAR);
                    *p = 0;
                    break;
                }
                break;
            case '\\':
                reqsize -= 2 * sizeof(WCHAR);
494
                memmove(p, p + 2, reqsize + sizeof(WCHAR) - (p - buffer) * sizeof(WCHAR));
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                break;
            }
        }
        ptr = p;
    }

done:
    RtlReleasePebLock();
    return reqsize;
}

/******************************************************************
 *		RtlGetFullPathName_U  (NTDLL.@)
 *
 * Returns the number of bytes written to buffer (not including the
 * terminating NULL) if the function succeeds, or the required number of bytes
511
 * (including the terminating NULL) if the buffer is too small.
512 513 514 515 516 517 518 519
 *
 * file_part will point to the filename part inside buffer (except if we use
 * DOS device name, in which case file_in_buf is NULL)
 *
 */
DWORD WINAPI RtlGetFullPathName_U(const WCHAR* name, ULONG size, WCHAR* buffer,
                                  WCHAR** file_part)
{
520
    WCHAR*      ptr;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    DWORD       dosdev;
    DWORD       reqsize;

    TRACE("(%s %lu %p %p)\n", debugstr_w(name), size, buffer, file_part);

    if (!name || !*name) return 0;

    if (file_part) *file_part = NULL;

    /* check for DOS device name */
    dosdev = RtlIsDosDeviceName_U(name);
    if (dosdev)
    {
        DWORD   offset = HIWORD(dosdev) / sizeof(WCHAR); /* get it in WCHARs, not bytes */
        DWORD   sz = LOWORD(dosdev); /* in bytes */

        if (8 + sz + 2 > size) return sz + 10;
        strcpyW(buffer, DeviceRootW);
        memmove(buffer + 4, name + offset, sz);
        buffer[4 + sz / sizeof(WCHAR)] = '\0';
        /* file_part isn't set in this case */
        return sz + 8;
    }

    reqsize = get_full_path_helper(name, buffer, size);
    if (reqsize > size)
    {
548 549 550 551 552 553 554 555
        LPWSTR tmp = RtlAllocateHeap(ntdll_get_process_heap(), 0, reqsize);
        reqsize = get_full_path_helper(name, tmp, reqsize);
        if (reqsize > size)  /* it may have worked the second time */
        {
            RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
            return reqsize + sizeof(WCHAR);
        }
        memcpy( buffer, tmp, reqsize + sizeof(WCHAR) );
556 557
        RtlFreeHeap(ntdll_get_process_heap(), 0, tmp);
    }
558 559 560 561

    /* find file part */
    if (file_part && (ptr = strrchrW(buffer, '\\')) != NULL && ptr >= buffer + 2 && *++ptr)
        *file_part = ptr;
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    return reqsize;
}

/*************************************************************************
 * RtlGetLongestNtPathLength    [NTDLL.@]
 *
 * Get the longest allowed path length
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  The longest allowed path length (277 characters under Win2k).
 */
DWORD WINAPI RtlGetLongestNtPathLength(void)
{
    return 277;
}

581 582 583 584 585 586 587 588 589 590
/******************************************************************
 *             RtlIsNameLegalDOS8Dot3   (NTDLL.@)
 *
 * Returns TRUE iff unicode is a valid DOS (8+3) name.
 * If the name is valid, oem gets filled with the corresponding OEM string
 * spaces is set to TRUE if unicode contains spaces
 */
BOOLEAN WINAPI RtlIsNameLegalDOS8Dot3( const UNICODE_STRING *unicode,
                                       OEM_STRING *oem, BOOLEAN *spaces )
{
591
    static const char* illegal = "*?<>|\"+=,;[]:/\\\345";
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    int dot = -1;
    unsigned int i;
    char buffer[12];
    OEM_STRING oem_str;
    BOOLEAN got_space = FALSE;

    if (!oem)
    {
        oem_str.Length = sizeof(buffer);
        oem_str.MaximumLength = sizeof(buffer);
        oem_str.Buffer = buffer;
        oem = &oem_str;
    }
    if (RtlUpcaseUnicodeStringToCountedOemString( oem, unicode, FALSE ) != STATUS_SUCCESS)
        return FALSE;

    if (oem->Length > 12) return FALSE;

    /* a starting . is invalid, except for . and .. */
    if (oem->Buffer[0] == '.')
    {
        if (oem->Length != 1 && (oem->Length != 2 || oem->Buffer[1] != '.')) return FALSE;
        if (spaces) *spaces = FALSE;
        return TRUE;
    }

    for (i = 0; i < oem->Length; i++)
    {
        switch (oem->Buffer[i])
        {
        case ' ':
            /* leading/trailing spaces not allowed */
            if (!i || i == oem->Length-1 || oem->Buffer[i+1] == '.') return FALSE;
            got_space = TRUE;
            break;
        case '.':
            if (dot != -1) return FALSE;
            dot = i;
            break;
        default:
632
            if (strchr(illegal, oem->Buffer[i])) return FALSE;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
            break;
        }
    }
    /* check file part is shorter than 8, extension shorter than 3
     * dot cannot be last in string
     */
    if (dot == -1)
    {
        if (oem->Length > 8) return FALSE;
    }
    else
    {
        if (dot > 8 || (oem->Length - dot > 4) || dot == oem->Length - 1) return FALSE;
    }
    if (spaces) *spaces = got_space;
    return TRUE;
}
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766

/******************************************************************
 *		RtlGetCurrentDirectory_U (NTDLL.@)
 *
 */
NTSTATUS WINAPI RtlGetCurrentDirectory_U(ULONG buflen, LPWSTR buf)
{
    UNICODE_STRING*     us;
    ULONG               len;

    TRACE("(%lu %p)\n", buflen, buf);

    RtlAcquirePebLock();

    us = &ntdll_get_process_pmts()->CurrentDirectoryName;
    len = us->Length / sizeof(WCHAR);
    if (us->Buffer[len - 1] == '\\' && us->Buffer[len - 2] != ':')
        len--;

    if (buflen / sizeof(WCHAR) > len)
    {
        memcpy(buf, us->Buffer, len * sizeof(WCHAR));
        buf[len] = '\0';
    }
    else
    {
        len++;
    }

    RtlReleasePebLock();

    return len * sizeof(WCHAR);
}

/******************************************************************
 *		RtlSetCurrentDirectory_U (NTDLL.@)
 *
 */
NTSTATUS WINAPI RtlSetCurrentDirectory_U(const UNICODE_STRING* dir)
{
    UNICODE_STRING*     curdir;
    NTSTATUS            nts = STATUS_SUCCESS;
    ULONG               size;
    PWSTR               buf = NULL;

    TRACE("(%s)\n", debugstr_w(dir->Buffer));

    RtlAcquirePebLock();

    curdir = &ntdll_get_process_pmts()->CurrentDirectoryName;
    size = curdir->MaximumLength;

    buf = RtlAllocateHeap(ntdll_get_process_heap(), 0, size);
    if (buf == NULL)
    {
        nts = STATUS_NO_MEMORY;
        goto out;
    }

    size = RtlGetFullPathName_U(dir->Buffer, size, buf, 0);
    if (!size)
    {
        nts = STATUS_OBJECT_NAME_INVALID;
        goto out;
    }

    switch (RtlDetermineDosPathNameType_U(buf))
    {
    case ABSOLUTE_DRIVE_PATH:
    case UNC_PATH:
        break;
    default:
        FIXME("Don't support those cases yes\n");
        nts = STATUS_NOT_IMPLEMENTED;
        goto out;
    }

    /* FIXME: should check that the directory actually exists,
     * and fill CurrentDirectoryHandle accordingly 
     */

    /* append trailing \ if missing */
    if (buf[size / sizeof(WCHAR) - 1] != '\\')
    {
        buf[size / sizeof(WCHAR)] = '\\';
        buf[size / sizeof(WCHAR) + 1] = '\0';
        size += sizeof(WCHAR);
    }

    memmove(curdir->Buffer, buf, size + sizeof(WCHAR));
    curdir->Length = size;

#if 0
    if (curdir->Buffer[1] == ':')
    {
        UNICODE_STRING  env;
        WCHAR           var[4];

        var[0] = '=';
        var[1] = curdir->Buffer[0];
        var[2] = ':';
        var[3] = 0;
        env.Length = 3 * sizeof(WCHAR);
        env.MaximumLength = 4 * sizeof(WCHAR);
        env.Buffer = var;

        RtlSetEnvironmentVariable(NULL, &env, curdir);
    }
#endif

 out:
    if (buf) RtlFreeHeap(ntdll_get_process_heap(), 0, buf);

    RtlReleasePebLock();

    return nts;
}