summaryrefslogblamecommitdiffstats
path: root/private/ntos/dll/curdir.c
blob: 5b7d09ef3e6f0526909db4e1b809398fd32fef7e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
548
549
550
551
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
586
587
588
589
590
591
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
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
1455
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
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
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
1578
1579
1580
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
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
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
1792
1793
1794
1795
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
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
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
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
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





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                             
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    curdir.c

Abstract:

    Current directory support

Author:

    Mark Lucovsky (markl) 10-Oct-1990

Revision History:

--*/

#include "nt.h"
#include "ntrtl.h"
#include "nturtl.h"
#include "string.h"
#include "ctype.h"

#define IS_PATH_SEPARATOR_U(ch) ((ch == (WCHAR)'\\') || (ch == (WCHAR)'/'))
#define IS_DOT_U(s) ( s[0] == (WCHAR)'.' && ( IS_PATH_SEPARATOR_U(s[1]) || s[1] == UNICODE_NULL) )
#define IS_DOT_DOT_U(s) ( s[0] == (WCHAR)'.' && s[1] == (WCHAR)'.' && ( IS_PATH_SEPARATOR_U(s[2]) || s[2] == UNICODE_NULL) )

VOID
RtlpCheckRelativeDrive(
    WCHAR NewDrive
    );

#define NUMBER_OF_DOS_DEVICE_NAMES 7
#define DOS_DEVICE_LPT      0
#define DOS_DEVICE_COM      1
#define DOS_DEVICE_PRN      2
#define DOS_DEVICE_AUX      3
#define DOS_DEVICE_NUL      4
#define DOS_DEVICE_CON      5
#define DOS_DEVICE_SLASHCON 6
UNICODE_STRING RtlpDosDevices[NUMBER_OF_DOS_DEVICE_NAMES];
UNICODE_STRING RtlpSlashSlashDot;
UNICODE_STRING RtlpDosDevicesPrefix;
UNICODE_STRING RtlpDosDevicesUncPrefix;
ULONG RtlpLongestPrefix;

ULONG
RtlpComputeBackupIndex(
    IN PCURDIR CurDir
    )
{
    ULONG BackupIndex;
    PWSTR UncPathPointer;
    ULONG NumberOfPathSeparators;
    RTL_PATH_TYPE CurDirPathType;


    //
    // Get pathType of curdir
    //

    CurDirPathType = RtlDetermineDosPathNameType_U(CurDir->DosPath.Buffer);
    BackupIndex = 3;
    if ( CurDirPathType == RtlPathTypeUncAbsolute ) {

        //
        // We want to scan the supplied path to determine where
        // the "share" ends, and set BackupIndex to that point.
        //

        UncPathPointer = CurDir->DosPath.Buffer + 2;
        NumberOfPathSeparators = 0;
        while (*UncPathPointer) {
            if (IS_PATH_SEPARATOR_U(*UncPathPointer)) {

                NumberOfPathSeparators++;

                if (NumberOfPathSeparators == 2) {
                    break;
                    }
                }

            UncPathPointer++;

            }

        BackupIndex = (UncPathPointer - CurDir->DosPath.Buffer);
        }
    return BackupIndex;
}


ULONG
RtlGetLongestNtPathLength( VOID )
{
    return RtlpLongestPrefix + DOS_MAX_PATH_LENGTH + 1;
}

VOID
RtlpResetDriveEnvironment(
    IN WCHAR DriveLetter
    )
{
    WCHAR EnvVarNameBuffer[4];
    WCHAR EnvVarNameValue[4];
    UNICODE_STRING s1,s2;

    EnvVarNameBuffer[0] = L'=';
    EnvVarNameBuffer[1] = DriveLetter;
    EnvVarNameBuffer[2] = L':';
    EnvVarNameBuffer[3] = L'\0';
    RtlInitUnicodeString(&s1,EnvVarNameBuffer);

    EnvVarNameValue[0] = DriveLetter;
    EnvVarNameValue[1] = L':';
    EnvVarNameValue[2] = L'\\';
    EnvVarNameValue[3] = L'\0';
    RtlInitUnicodeString(&s2,EnvVarNameValue);

    RtlSetEnvironmentVariable(NULL,&s1,&s2);
}


VOID
RtlpCurdirInit()
{

    //
    // Initialize the built in dos device names..
    //

    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_LPT ], L"LPT" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_COM ], L"COM" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_PRN ], L"PRN" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_AUX ], L"AUX" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_NUL ], L"NUL" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_CON ], L"CON" );
    RtlInitUnicodeString( &RtlpDosDevices[ DOS_DEVICE_SLASHCON ], L"\\\\.\\CON" );
    RtlInitUnicodeString( &RtlpSlashSlashDot, L"\\\\.\\" );
    RtlInitUnicodeString( &RtlpDosDevicesPrefix, L"\\??\\" );
    RtlInitUnicodeString( &RtlpDosDevicesUncPrefix, L"\\??\\UNC\\" );
    RtlpLongestPrefix = RtlpDosDevicesUncPrefix.Length;
}

ULONG
RtlGetCurrentDirectory_U(
    ULONG nBufferLength,
    PWSTR lpBuffer
    )

/*++

Routine Description:

    The current directory for a process can be retreived using
    GetCurrentDirectory.

Arguments:

    nBufferLength - Supplies the length in bytes of the buffer that is to
        receive the current directory string.

    lpBuffer - Returns the current directory string for the current
        process.  The string is a null terminated string and specifies
        the absolute path to the current directory.

Return Value:

    The return value is the length of the string copied to lpBuffer, not
    including the terminating null character.  If the return value is
    greater than nBufferLength, the return value is the size of the buffer
    required to hold the pathname.  The return value is zero if the
    function failed.

--*/

{
    PCURDIR CurDir;
    ULONG Length;
    PWSTR  CurDirName;

    CurDir = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);

    RtlAcquirePebLock();
    CurDirName = CurDir->DosPath.Buffer;

    //
    // Make sure user's buffer is big enough to hold the null
    // terminated current directory
    //

    Length = CurDir->DosPath.Length>>1;
    if (CurDirName[Length-2] != (WCHAR)':') {
        if ( nBufferLength < (Length)<<1 ) {
            RtlReleasePebLock();
            return (Length)<<1;
            }
        }
    else {
        if ( nBufferLength <= (Length<<1) ) {
            RtlReleasePebLock();
            return ((Length+1)<<1);
            }
        }

    try {
        RtlMoveMemory(lpBuffer,CurDirName,Length<<1);
        ASSERT(lpBuffer[Length-1] == (WCHAR)'\\');
        if (lpBuffer[Length-2] == (WCHAR)':') {
            lpBuffer[Length] = UNICODE_NULL;
            }
        else {
            lpBuffer[Length-1] = UNICODE_NULL;
            Length--;
            }
        }
    except (EXCEPTION_EXECUTE_HANDLER) {
        RtlReleasePebLock();
        return 0L;
        }
    RtlReleasePebLock();
    return Length<<1;
}

NTSTATUS
RtlSetCurrentDirectory_U(
    PUNICODE_STRING PathName
    )

/*++

Routine Description:

    The current directory for a process is changed using
    SetCurrentDirectory.

    Each process has a single current directory.  A current directory is
    made up of type parts.

        - A disk designator either which is either a drive letter followed
          by a colon, or a UNC servername/sharename "\\servername\sharename".

        - A directory on the disk designator.

    For APIs that manipulate files, the file names may be relative to
    the current directory.  A filename is relative to the entire current
    directory if it does not begin with a disk designator or a path name
    SEPARATOR.  If the file name begins with a path name SEPARATOR, then
    it is relative to the disk designator of the current directory.  If
    a file name begins with a disk designator, than it is a fully
    qualified absolute path name.

    The value of lpPathName supplies the current directory.  The value
    of lpPathName, may be a relative path name as described above, or a
    fully qualified absolute path name.  In either case, the fully
    qualified absolute path name of the specified directory is
    calculated and is stored as the current directory.

Arguments:

    lpPathName - Supplies the pathname of the directory that is to be
        made the current directory.

Return Value:

    NT_SUCCESS - The operation was successful

    !NT_SUCCESS - The operation failed

--*/

{
    PCURDIR CurDir;
    NTSTATUS Status;
    BOOLEAN TranslationStatus;
    PVOID FreeBuffer;
    ULONG DosDirLength;
    ULONG DosDirCharCount;
    UNICODE_STRING DosDir;
    UNICODE_STRING NtFileName;
    HANDLE NewDirectoryHandle;
    OBJECT_ATTRIBUTES Obja;
    IO_STATUS_BLOCK IoStatusBlock;
    FILE_FS_DEVICE_INFORMATION DeviceInfo;

    CurDir = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);


    DosDir.Buffer = NULL;
    FreeBuffer = NULL;
    NewDirectoryHandle = NULL;

    RtlAcquirePebLock();

    NtCurrentPeb()->EnvironmentUpdateCount += 1;

    //
    // Set current directory is called first by the loader.
    // If current directory is not being inherited, then close
    // it !
    //

    if ( ((ULONG)CurDir->Handle & OBJ_HANDLE_TAGBITS) == RTL_USER_PROC_CURDIR_CLOSE ) {
        NtClose(CurDir->Handle);
        CurDir->Handle = NULL;
        }

    try {
        try {

            //
            // Compute the length of the Dos style fully qualified current
            // directory
            //

            DosDirLength = CurDir->DosPath.MaximumLength;
            DosDir.Buffer = RtlAllocateHeap(RtlProcessHeap(), 0,DosDirLength);
            DosDir.Length = 0;
            DosDir.MaximumLength = (USHORT)DosDirLength;

            //
            // Now get the full pathname for the Dos style current
            // directory
            //

            DosDirLength = RtlGetFullPathName_U(
                                PathName->Buffer,
                                DosDirLength,
                                DosDir.Buffer,
                                NULL
                                );
            if ( !DosDirLength ) {
                return STATUS_OBJECT_NAME_INVALID;
                }
            DosDirCharCount = DosDirLength >> 1;


            //
            // Get the Nt filename of the new current directory
            //
            TranslationStatus = RtlDosPathNameToNtPathName_U(
                                    DosDir.Buffer,
                                    &NtFileName,
                                    NULL,
                                    NULL
                                    );

            if ( !TranslationStatus ) {
                return STATUS_OBJECT_NAME_INVALID;
                }
            FreeBuffer = NtFileName.Buffer;

            InitializeObjectAttributes(
                &Obja,
                &NtFileName,
                OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
                NULL,
                NULL
                );

            //
            // If we are inheriting current directory, then
            // avoid the open
            //

            if ( ((ULONG)CurDir->Handle & OBJ_HANDLE_TAGBITS) ==  RTL_USER_PROC_CURDIR_INHERIT ) {
                NewDirectoryHandle = (HANDLE)((ULONG)CurDir->Handle & ~OBJ_HANDLE_TAGBITS);
                CurDir->Handle = NULL;

                //
                // Test to see if this is removable media. If so
                // tag the handle this may fail if the process was
                // created with inherit handles set to false
                //

                Status = NtQueryVolumeInformationFile(
                            NewDirectoryHandle,
                            &IoStatusBlock,
                            &DeviceInfo,
                            sizeof(DeviceInfo),
                            FileFsDeviceInformation
                            );
                if ( !NT_SUCCESS(Status) ) {
                    return RtlSetCurrentDirectory_U(PathName);
                    }
                else {
                    if ( DeviceInfo.Characteristics & FILE_REMOVABLE_MEDIA ) {
                        NewDirectoryHandle =(HANDLE)( (ULONG)NewDirectoryHandle | 1);
                        }
                    }

                }
            else {
                //
                // Open a handle to the current directory. Don't allow
                // deletes of the directory.
                //

                Status = NtOpenFile(
                            &NewDirectoryHandle,
                            SYNCHRONIZE | FILE_TRAVERSE,
                            &Obja,
                            &IoStatusBlock,
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                            FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT
                            );

                if ( !NT_SUCCESS(Status) ) {
                    return Status;
                    }

                //
                // Test to see if this is removable media. If so
                // tag the handle
                //
                Status = NtQueryVolumeInformationFile(
                            NewDirectoryHandle,
                            &IoStatusBlock,
                            &DeviceInfo,
                            sizeof(DeviceInfo),
                            FileFsDeviceInformation
                            );
                if ( !NT_SUCCESS(Status) ) {
                    return Status;
                    }
                else {
                    if ( DeviceInfo.Characteristics & FILE_REMOVABLE_MEDIA ) {
                        NewDirectoryHandle =(HANDLE)( (ULONG)NewDirectoryHandle | 1);
                        }
                    }
                }

            //
            // If there is no trailing '\', than place one
            //

            if ( DosDir.Buffer[DosDirCharCount] != (WCHAR)'\\' &&
                 DosDir.Buffer[DosDirCharCount-1] != (WCHAR)'\\') {
                DosDir.Buffer[DosDirCharCount] = (WCHAR)'\\';
                DosDir.Buffer[DosDirCharCount+1] = UNICODE_NULL;
                DosDir.Length = (USHORT)(DosDirLength + sizeof(UNICODE_NULL));
                }
            else {
                DosDir.Length = (USHORT)DosDirLength;
                }

            //
            // Now we are set to change to the new directory.
            //

            RtlMoveMemory( CurDir->DosPath.Buffer, DosDir.Buffer, DosDir.Length+sizeof(UNICODE_NULL) );
            CurDir->DosPath.Length = DosDir.Length;

            if ( CurDir->Handle ) {
                NtClose(CurDir->Handle);
                }
            CurDir->Handle = NewDirectoryHandle;
            NewDirectoryHandle = NULL;
            }
        finally {
            if ( DosDir.Buffer ) {
                RtlFreeHeap(RtlProcessHeap(), 0, DosDir.Buffer);
                }
            if ( FreeBuffer ) {
                RtlFreeHeap(RtlProcessHeap(), 0, FreeBuffer);
                }
            if ( NewDirectoryHandle ) {
                NtClose(NewDirectoryHandle);
                }
            RtlReleasePebLock();
            }
        }
    except (EXCEPTION_EXECUTE_HANDLER) {
        return STATUS_ACCESS_VIOLATION;
        }
    return STATUS_SUCCESS;
}

RTL_PATH_TYPE
RtlDetermineDosPathNameType_U(
    IN PCWSTR DosFileName
    )

/*++

Routine Description:

    This function examines the Dos format file name and determines the
    type of file name (i.e.  UNC, DriveAbsolute, Current Directory
    rooted, or Relative.

Arguments:

    DosFileName - Supplies the Dos format file name whose type is to be
        determined.

Return Value:

    RtlPathTypeUnknown - The path type can not be determined

    RtlPathTypeUncAbsolute - The path specifies a Unc absolute path
        in the format \\server-name\sharename\rest-of-path

    RtlPathTypeLocalDevice - The path specifies a local device in the format
        \\.\rest-of-path this can be used for any device where the nt and
        Win32 names are the same. For example mailslots.

    RtlPathTypeRootLocalDevice - The path specifies the root of the local
        devices in the format \\.

    RtlPathTypeDriveAbsolute - The path specifies a drive letter absolute
        path in the form drive:\rest-of-path

    RtlPathTypeDriveRelative - The path specifies a drive letter relative
        path in the form drive:rest-of-path

    RtlPathTypeRooted - The path is rooted relative to the current disk
        designator (either Unc disk, or drive). The form is \rest-of-path.

    RtlPathTypeRelative - The path is relative (i.e. not absolute or rooted).

--*/

{

    RTL_PATH_TYPE ReturnValue;

    if ( IS_PATH_SEPARATOR_U(*DosFileName) ) {
        if ( IS_PATH_SEPARATOR_U(*(DosFileName+1)) ) {
            if ( DosFileName[2] == '.' ) {
                if ( IS_PATH_SEPARATOR_U(*(DosFileName+3)) ){
                    ReturnValue = RtlPathTypeLocalDevice;
                    }
                else if ( (*(DosFileName+3)) == UNICODE_NULL ){
                    ReturnValue = RtlPathTypeRootLocalDevice;
                    }
                else {
                    ReturnValue = RtlPathTypeUncAbsolute;
                    }
                }
            else {
                ReturnValue = RtlPathTypeUncAbsolute;
                }
            }
        else {
            ReturnValue = RtlPathTypeRooted;
            }
        }
    else if (*(DosFileName+1)==(WCHAR)':') {
            if (IS_PATH_SEPARATOR_U(*(DosFileName+2))) {
                ReturnValue = RtlPathTypeDriveAbsolute;
                }
            else  {
                ReturnValue = RtlPathTypeDriveRelative;
                }
            }
    else {
        ReturnValue = RtlPathTypeRelative;
        }
    return ReturnValue;
}

ULONG
RtlIsDosDeviceName_Ustr(
    IN PUNICODE_STRING DosFileName
    )

/*++

Routine Description:

    This function examines the Dos format file name and determines if it
    is a Dos device name (e.g. LPT1, etc.).  Valid Dos device names are:

        LPTn
        COMn
        PRN
        AUX
        NUL
        CON

    when n is a digit.  Trailing colon is ignored if present.

Arguments:

    DosFileName - Supplies the Dos format file name that is to be examined.

Return Value:

    0 - Specified Dos file name is not the name of a Dos device.

    > 0 - Specified Dos file name is the name of a Dos device and the
          return value is a ULONG where the high order 16 bits is the
          offset in the input buffer where the dos device name beings
          and the low order 16 bits is the length of the device name
          the length of the name (excluding any optional
          trailing colon).

--*/

{
    UNICODE_STRING UnicodeString;
    USHORT NumberOfCharacters;
    ULONG ReturnLength;
    ULONG ReturnOffset;
    LPWSTR p;
    USHORT ColonBias;
    RTL_PATH_TYPE PathType;
    WCHAR wch;

    ColonBias = 0;

    PathType = RtlDetermineDosPathNameType_U(DosFileName->Buffer);
    switch ( PathType ) {

        case RtlPathTypeLocalDevice:
                //
                // For Unc Absolute, Check for \\.\CON
                // since this really is not a device
                //

                if ( RtlEqualUnicodeString(DosFileName,&RtlpDosDevices[ DOS_DEVICE_SLASHCON ],TRUE) ) {
                    return 0x00080006;
                    }
                //
                // FALLTHRU
                //

        case RtlPathTypeUncAbsolute:
        case RtlPathTypeUnknown:
            return 0;
            break;
            }

    UnicodeString = *DosFileName;
    NumberOfCharacters = DosFileName->Length >> 1;

    if (NumberOfCharacters && DosFileName->Buffer[NumberOfCharacters-1] == (WCHAR)':') {
        UnicodeString.Length -= sizeof(WCHAR);
        NumberOfCharacters--;
        ColonBias = 1;
        }

    wch = UnicodeString.Buffer[NumberOfCharacters-1];
    while ( NumberOfCharacters &&
            (wch == (WCHAR)'.' || wch == (WCHAR)' ') ) {
        UnicodeString.Length -= sizeof(WCHAR);
        NumberOfCharacters--;
        ColonBias++;
        wch = UnicodeString.Buffer[NumberOfCharacters-1];
        }

    ReturnLength = NumberOfCharacters << 1;

    //
    // p points to last character
    //
    ReturnOffset = 0;
    if ( NumberOfCharacters ) {
        p = UnicodeString.Buffer + NumberOfCharacters-1;
        while ( p >= UnicodeString.Buffer ) {
            if ( *p == (WCHAR)'\\' || (*p == (WCHAR)':'&& *(p+1)!=(WCHAR)'.') || *p == (WCHAR)'/') {
                p++;

                wch = *p;

                //
                // check to see if we possibly have a hit on
                // lpt, prn, con, com, aux, or nul
                //

                if ( !
                    (wch == (WCHAR)'l' || wch == (WCHAR)'L' ||
                     wch == (WCHAR)'c' || wch == (WCHAR)'C' ||
                     wch == (WCHAR)'p' || wch == (WCHAR)'P' ||
                     wch == (WCHAR)'a' || wch == (WCHAR)'A' ||
                     wch == (WCHAR)'n' || wch == (WCHAR)'N') ) {
                    return 0;
                    }
                ReturnOffset = (ULONG)((PSZ)p - (PSZ)UnicodeString.Buffer);
                RtlInitUnicodeString(&UnicodeString,p);
                NumberOfCharacters = UnicodeString.Length >> 1;
                NumberOfCharacters -= ColonBias;
                ReturnLength = NumberOfCharacters << 1;
                UnicodeString.Length -= ColonBias*sizeof(WCHAR);
                break;
                }
            p--;
            }

        wch = UnicodeString.Buffer[0];

        //
        // check to see if we possibly have a hit on
        // lpt, prn, con, com, aux, or nul
        //

        if ( !
            (wch == (WCHAR)'l' || wch == (WCHAR)'L' ||
             wch == (WCHAR)'c' || wch == (WCHAR)'C' ||
             wch == (WCHAR)'p' || wch == (WCHAR)'P' ||
             wch == (WCHAR)'a' || wch == (WCHAR)'A' ||
             wch == (WCHAR)'n' || wch == (WCHAR)'N') ) {
            return 0;
            }
        }
    //
    // Now we need to see if we are dealing with a device name that has
    // an extension. If so, we need to limit the search to the file portion
    // only
    //

    p = wcschr(UnicodeString.Buffer,(WCHAR)'.');

    if ( p ) {
        NumberOfCharacters = p - UnicodeString.Buffer;
        UnicodeString.Length = NumberOfCharacters << 1;
        if (UnicodeString.Buffer[NumberOfCharacters-1] == (WCHAR)':') {
            UnicodeString.Length -= sizeof(WCHAR);
            NumberOfCharacters--;
            }
        }

    if ( NumberOfCharacters == 4 && iswdigit(UnicodeString.Buffer[3] ) ) {
        if ( (WCHAR)UnicodeString.Buffer[3] == (WCHAR)'0') {
            ReturnLength = 0;
            }
        else {
            UnicodeString.Length -= sizeof(WCHAR);
            if ( RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_LPT ],TRUE) ||
                 RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_COM ],TRUE) ) {
                ReturnLength = NumberOfCharacters << 1;
                }
            else {
                ReturnLength = 0;
                }
            }
        }
    else {
        if ( NumberOfCharacters != 3 ) {
            ReturnLength = 0;
            }
        else {
            ReturnLength = 0;
            if ( RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_PRN ],TRUE) ) {
                ReturnLength = NumberOfCharacters << 1;
                }
            else
            if ( RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_AUX ],TRUE) ) {
                ReturnLength = NumberOfCharacters << 1;
                }
            else
            if ( RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_NUL ],TRUE) ) {
                ReturnLength = NumberOfCharacters << 1;
                }
            if ( RtlEqualUnicodeString(&UnicodeString,&RtlpDosDevices[ DOS_DEVICE_CON ],TRUE) ) {
                ReturnLength = NumberOfCharacters << 1;
                }
            }
        }
    if ( ReturnLength ) {
        ReturnLength = ReturnLength | (ReturnOffset << 16);
        }
    return ReturnLength;
}

ULONG
RtlIsDosDeviceName_U(
    IN PWSTR DosFileName
    )
{
    UNICODE_STRING UnicodeString;

    RtlInitUnicodeString(&UnicodeString,DosFileName);

    return RtlIsDosDeviceName_Ustr(&UnicodeString);
}

BOOLEAN
RtlpCheckDeviceName(
    PUNICODE_STRING DevName,
    ULONG DeviceNameOffset
    )
{

    BOOLEAN NameInvalid;
    PWSTR DevPath;

    DevPath = RtlAllocateHeap(RtlProcessHeap(), 0,DevName->Length);
    if (!DevPath) {
        return FALSE;
        }

    NameInvalid = TRUE;
    try {

        RtlCopyMemory(DevPath,DevName->Buffer,DevName->Length);
        DevPath[DeviceNameOffset>>1]=(WCHAR)'.';
        DevPath[(DeviceNameOffset>>1)+1]=UNICODE_NULL;

        if (RtlDoesFileExists_U(DevPath) ) {
            NameInvalid = FALSE;
            }
        else {
            NameInvalid = TRUE;
            }

        }
    finally {
        RtlFreeHeap(RtlProcessHeap(), 0, DevPath);
        }
    return NameInvalid;
}

ULONG
RtlGetFullPathName_Ustr(
    PUNICODE_STRING  FileName,
    ULONG nBufferLength,
    PWSTR lpBuffer,
    PWSTR *lpFilePart OPTIONAL,
    PBOOLEAN NameInvalid,
    RTL_PATH_TYPE *InputPathType
    )

/*++

Routine Description:

    This function is used to return a fully qualified pathname
    corresponding to the specified unicode filename.  It does this by
    merging the current drive and directory together with the specified
    file name.  In addition to this, it calculates the address of the
    file name portion of the fully qualified pathname.

Arguments:

    lpFileName - Supplies the unicode file name of the file whose fully
        qualified pathname is to be returned.

    nBufferLength - Supplies the length in bytes of the buffer that is
        to receive the fully qualified path.

    lpBuffer - Returns the fully qualified pathname corresponding to the
        specified file.

    lpFilePart - Optional parameter that if specified, returns the
        address of the last component of the fully qualified pathname.

Return Value:

    The return value is the length of the string copied to lpBuffer, not
    including the terminating unicode null character.  If the return
    value is greater than nBufferLength, the return value is the size of
    the buffer required to hold the pathname.  The return value is zero
    if the function failed.

--*/

{
    ULONG DeviceNameLength;
    ULONG DeviceNameOffset;
    ULONG PrefixSourceLength;
    LONG PathNameLength;
    UCHAR CurDrive, NewDrive;
    WCHAR EnvVarNameBuffer[4];
    UNICODE_STRING EnvVarName;
    PWSTR Source,Dest;
    UNICODE_STRING Prefix;
    PCURDIR CurDir;
    ULONG MaximumLength;
    UNICODE_STRING FullPath;
    ULONG BackupIndex;
    RTL_PATH_TYPE PathType;
    NTSTATUS Status;
    BOOLEAN StripTrailingSlash;
    UNICODE_STRING UnicodeString;
    ULONG NumberOfCharacters;
    PWSTR lpFileName;
    WCHAR wch;
    ULONG i,j;

    if ( ARGUMENT_PRESENT(NameInvalid) ) {
        *NameInvalid = FALSE;
        }

    if ( nBufferLength > MAXUSHORT ) {
        nBufferLength = MAXUSHORT-2;
        }

    *InputPathType = RtlPathTypeUnknown;

    UnicodeString = *FileName;
    lpFileName = UnicodeString.Buffer;

    NumberOfCharacters = UnicodeString.Length >> 1;
    PathNameLength = UnicodeString.Length;

    if ( PathNameLength == 0 || UnicodeString.Buffer[0] == UNICODE_NULL ) {
        return 0;
        }
    else {

        //
        // trim trailing spaces to check for a null name
        //
        DeviceNameLength = PathNameLength;
        wch = UnicodeString.Buffer[(DeviceNameLength>>1) - 1];
        while ( DeviceNameLength && wch == (WCHAR)' ' ) {
            DeviceNameLength -= sizeof(WCHAR);
            wch = UnicodeString.Buffer[(DeviceNameLength>>1) - 1];
            }
        if ( !DeviceNameLength ) {
            return 0;
            }
        }

    if ( lpFileName[NumberOfCharacters-1] == (WCHAR)'\\' ) {
        StripTrailingSlash = FALSE;
        }
    else {
        StripTrailingSlash = TRUE;
        }

    //
    // If pass Dos file name is a Dos device name, then turn it into
    // \\.\devicename and return its length.
    //

    DeviceNameLength = RtlIsDosDeviceName_Ustr(&UnicodeString);
    if ( DeviceNameLength ) {

        if ( ARGUMENT_PRESENT( lpFilePart ) ) {
            *lpFilePart = NULL;
            }

        DeviceNameOffset = DeviceNameLength >> 16;
        DeviceNameLength &= 0x0000ffff;

        if ( ARGUMENT_PRESENT(NameInvalid) && DeviceNameOffset ) {
            if ( RtlpCheckDeviceName(&UnicodeString,DeviceNameOffset) ) {
                *NameInvalid = TRUE;
                return 0;
                }
            }

        PathNameLength = DeviceNameLength + RtlpSlashSlashDot.Length;
        if ( PathNameLength < (LONG)nBufferLength ) {
            RtlMoveMemory(
                lpBuffer,
                RtlpSlashSlashDot.Buffer,
                RtlpSlashSlashDot.Length
                );
            RtlMoveMemory(
                (PVOID)((PUCHAR)lpBuffer+RtlpSlashSlashDot.Length),
                (PSZ)lpFileName+DeviceNameOffset,
                DeviceNameLength
                );

            RtlZeroMemory(
                (PVOID)((PUCHAR)lpBuffer+RtlpSlashSlashDot.Length+DeviceNameLength),
                sizeof(UNICODE_NULL)
                );

            return PathNameLength;
            }
        else {
            return PathNameLength+sizeof(UNICODE_NULL);
            }
        }

    //
    // Setup output string that points to callers buffer.
    //

    FullPath.MaximumLength = (USHORT)nBufferLength;
    FullPath.Length = 0;
    FullPath.Buffer = lpBuffer;
    RtlZeroMemory(lpBuffer,nBufferLength);
    //
    // Get a pointer to the current directory structure.
    //

    CurDir = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);


    //
    // Determine the type of Dos Path Name specified.
    //

    *InputPathType = PathType = RtlDetermineDosPathNameType_U(lpFileName);

    //
    // Determine the prefix and backup index.
    //
    //  Input        Prefix                     Backup Index
    //
    //  \\        -> \\,                            end of \\server\share
    //  \\.\      -> \\.\,                          4
    //  \\.       -> \\.                            3 (\\.)
    //  \         -> Drive: from CurDir.DosPath     3 (Drive:\)
    //  d:        -> Drive:\curdir from environment 3 (Drive:\)
    //  d:\       -> no prefix                      3 (Drive:\)
    //  any       -> CurDir.DosPath                 3 (Drive:\)
    //

    RtlAcquirePebLock();
    try {

        //
        // No prefixes yet.
        //

        Source = lpFileName;
        PrefixSourceLength = 0;
        Prefix.Length = 0;
        Prefix.MaximumLength = 0;
        Prefix.Buffer = NULL;

        switch (PathType) {
            case RtlPathTypeUncAbsolute :
                {
                    PWSTR UncPathPointer;
                    ULONG NumberOfPathSeparators;

                    //
                    // We want to scan the supplied path to determine where
                    // the "share" ends, and set BackupIndex to that point.
                    //

                    UncPathPointer = lpFileName + 2;
                    NumberOfPathSeparators = 0;
                    while (*UncPathPointer) {
                        if (IS_PATH_SEPARATOR_U(*UncPathPointer)) {

                            NumberOfPathSeparators++;

                            if (NumberOfPathSeparators == 2) {
                                break;
                                }
                            }

                        UncPathPointer++;

                        }

                    BackupIndex = (UncPathPointer - lpFileName);

                    //
                    // Unc name. prefix = \\server\share
                    //

                    PrefixSourceLength = BackupIndex << 1;

                    Source += BackupIndex;

                    }
                break;

            case RtlPathTypeLocalDevice :

                //
                // Local device name. prefix = "\\.\"
                //

                PrefixSourceLength = RtlpSlashSlashDot.Length;
                BackupIndex = 4;
                Source += BackupIndex;
                break;

            case RtlPathTypeRootLocalDevice :

                //
                // Local Device root. prefix = "\\.\"
                //


                Prefix = RtlpSlashSlashDot;
                Prefix.Length = (USHORT)(Prefix.Length - (USHORT)(2*sizeof(UNICODE_NULL)));
                PrefixSourceLength = Prefix.Length + sizeof(UNICODE_NULL);
                BackupIndex = 3;
                Source += BackupIndex;
                break;

            case RtlPathTypeDriveAbsolute :

                CurDrive = (UCHAR)RtlUpcaseUnicodeChar( CurDir->DosPath.Buffer[0] );
                NewDrive = (UCHAR)RtlUpcaseUnicodeChar( lpFileName[0] );
                if ( CurDrive == NewDrive ) {

                    //
                    // If we are dealing with removable media,
                    // check to see if the current directory handle is
                    // still good. If it is not ok, then trim current
                    // directory back to the root.
                    //

                    if ( (ULONG)CurDir->Handle & 1 ) {

                        NTSTATUS FsCtlStatus;
                        IO_STATUS_BLOCK IoStatusBlock;
                        WCHAR TrimmedPath[8];
                        UNICODE_STRING str;

                        //
                        // Call Nt to see if the volume that
                        // contains the directory is still mounted.
                        // If it is, then continue. Otherwise, trim
                        // current directory to the root.
                        //

                        FsCtlStatus = NtFsControlFile(
                                        CurDir->Handle,
                                        NULL,
                                        NULL,
                                        NULL,
                                        &IoStatusBlock,
                                        FSCTL_IS_VOLUME_MOUNTED,
                                        NULL,
                                        0,
                                        NULL,
                                        0
                                        );

                        if ( FsCtlStatus == STATUS_WRONG_VOLUME ) {

                            //
                            // Reset current directory to
                            // root of the current drive
                            //

                            NtClose(CurDir->Handle);
                            CurDir->Handle = NULL;

                            FsCtlStatus = RtlSetCurrentDirectory_U(&CurDir->DosPath);
                            if ( !NT_SUCCESS(FsCtlStatus) ) {

                                RtlMoveMemory(TrimmedPath,CurDir->DosPath.Buffer,6);
                                TrimmedPath[3] = UNICODE_NULL;
                                RtlpResetDriveEnvironment(TrimmedPath[0]);
                                RtlInitUnicodeString(&str,TrimmedPath);
                                FsCtlStatus = RtlSetCurrentDirectory_U(&str);
                                ASSERT(NT_SUCCESS(FsCtlStatus));
                                }
                            }
                        }
                    }
                //
                // Dos drive absolute name
                //

                BackupIndex = 3;
                break;

            case RtlPathTypeDriveRelative :

                //
                // Dos drive relative name
                //

                CurDrive = (UCHAR)RtlUpcaseUnicodeChar( CurDir->DosPath.Buffer[0] );
                NewDrive = (UCHAR)RtlUpcaseUnicodeChar( lpFileName[0] );
                if ( CurDrive == NewDrive ) {

                    //
                    // If we are dealing with removable media,
                    // check to see if the current directory handle is
                    // still good. If it is not ok, then trim current
                    // directory back to the root.
                    //

                    if ( (ULONG)CurDir->Handle & 1 ) {

                        NTSTATUS FsCtlStatus;
                        IO_STATUS_BLOCK IoStatusBlock;
                        WCHAR TrimmedPath[8];
                        UNICODE_STRING str;

                        //
                        // Call Nt to see if the volume that
                        // contains the directory is still mounted.
                        // If it is, then continue. Otherwise, trim
                        // current directory to the root.
                        //

                        FsCtlStatus = NtFsControlFile(
                                        CurDir->Handle,
                                        NULL,
                                        NULL,
                                        NULL,
                                        &IoStatusBlock,
                                        FSCTL_IS_VOLUME_MOUNTED,
                                        NULL,
                                        0,
                                        NULL,
                                        0
                                        );

                        if ( FsCtlStatus == STATUS_WRONG_VOLUME ) {

                            //
                            // Reset current directory to
                            // root of the current drive
                            //

                            NtClose(CurDir->Handle);
                            CurDir->Handle = NULL;
                            FsCtlStatus = RtlSetCurrentDirectory_U(&CurDir->DosPath);
                            if ( !NT_SUCCESS(FsCtlStatus) ) {

                                RtlMoveMemory(TrimmedPath,CurDir->DosPath.Buffer,6);
                                TrimmedPath[3] = UNICODE_NULL;
                                RtlpResetDriveEnvironment(TrimmedPath[0]);
                                RtlInitUnicodeString(&str,TrimmedPath);
                                FsCtlStatus = RtlSetCurrentDirectory_U(&str);
                                ASSERT(NT_SUCCESS(FsCtlStatus));
                                }

                            }
                        }

                    Prefix = *(PUNICODE_STRING)&CurDir->DosPath;
                    }

                else {
                    RtlpCheckRelativeDrive((WCHAR)NewDrive);

                    EnvVarNameBuffer[0] = (WCHAR)'=';
                    EnvVarNameBuffer[1] = (WCHAR)NewDrive;
                    EnvVarNameBuffer[2] = (WCHAR)':';
                    EnvVarNameBuffer[3] = UNICODE_NULL;
                    RtlInitUnicodeString(&EnvVarName,EnvVarNameBuffer);

                    Prefix = FullPath;
                    Status = RtlQueryEnvironmentVariable_U( NULL,
                                                            &EnvVarName,
                                                            &Prefix
                                                          );
                    if ( !NT_SUCCESS( Status ) ) {
                        if (Status == STATUS_BUFFER_TOO_SMALL) {
                            return (ULONG)(Prefix.Length) + PathNameLength + 2;
                            }
                        else {
                            //
                            // Otherwise default to root directory of drive
                            //

                            EnvVarNameBuffer[0] = (WCHAR)NewDrive;
                            EnvVarNameBuffer[1] = (WCHAR)':';
                            EnvVarNameBuffer[2] = (WCHAR)'\\';
                            EnvVarNameBuffer[3] = UNICODE_NULL;
                            RtlInitUnicodeString(&Prefix,EnvVarNameBuffer);
                            }
                        }
                    else {

                            {
                            ULONG LastChar;

                            //
                            // Determine
                            // if a backslash needs to be added
                            //

                            LastChar = Prefix.Length >> 1;

                            if (LastChar > 3) {
                                Prefix.Buffer[ LastChar ] = (WCHAR)'\\';
                                Prefix.Length += sizeof(UNICODE_NULL);
                                }
                            }

                        }
                    }

                BackupIndex = 3;
                Source += 2;
                break;

            case RtlPathTypeRooted :
                BackupIndex = RtlpComputeBackupIndex(CurDir);
                if ( BackupIndex != 3 ) {
                    Prefix = CurDir->DosPath;
                    Prefix.Length = (USHORT)(BackupIndex << 1);
                    }
                else {

                    //
                    // Rooted name. Prefix is drive portion of current directory
                    //

                    Prefix = CurDir->DosPath;
                    Prefix.Length = 2*sizeof(UNICODE_NULL);
                    }
                break;

            case RtlPathTypeRelative :


                //
                // If we are dealing with removable media,
                // check to see if the current directory handle is
                // still good. If it is not ok, then trim current
                // directory back to the root.
                //

                if ( (ULONG)CurDir->Handle & 1 ) {

                    NTSTATUS FsCtlStatus;
                    IO_STATUS_BLOCK IoStatusBlock;
                    WCHAR TrimmedPath[8];
                    UNICODE_STRING str;

                    //
                    // Call Nt to see if the volume that
                    // contains the directory is still mounted.
                    // If it is, then continue. Otherwise, trim
                    // current directory to the root.
                    //

                    FsCtlStatus = NtFsControlFile(
                                    CurDir->Handle,
                                    NULL,
                                    NULL,
                                    NULL,
                                    &IoStatusBlock,
                                    FSCTL_IS_VOLUME_MOUNTED,
                                    NULL,
                                    0,
                                    NULL,
                                    0
                                    );

                    if ( FsCtlStatus == STATUS_WRONG_VOLUME ) {

                        //
                        // Reset current directory to
                        // root of the current drive
                        //

                        NtClose(CurDir->Handle);
                        CurDir->Handle = NULL;
                        FsCtlStatus = RtlSetCurrentDirectory_U(&CurDir->DosPath);
                        if ( !NT_SUCCESS(FsCtlStatus) ) {

                            RtlMoveMemory(TrimmedPath,CurDir->DosPath.Buffer,6);
                            TrimmedPath[3] = UNICODE_NULL;
                            RtlpResetDriveEnvironment(TrimmedPath[0]);

                            RtlInitUnicodeString(&str,TrimmedPath);
                            FsCtlStatus = RtlSetCurrentDirectory_U(&str);
                            ASSERT(NT_SUCCESS(FsCtlStatus));
                            }
                        }
                    }

                //
                // Current drive:directory relative name
                //

                Prefix = CurDir->DosPath;
                BackupIndex = RtlpComputeBackupIndex(CurDir);
                break;

            default:
                return 0;

            }

        //
        // Maximum length required is the length of the prefix plus
        // the length of the specified pathname. If the callers buffer
        // is not at least this large, then return an error.
        //

        MaximumLength = PathNameLength + Prefix.Length;

        if ( MaximumLength >= nBufferLength ) {
            if ( (NumberOfCharacters > 1) ||
                 (*lpFileName != (WCHAR)'.') ) {
                if ( PathType == RtlPathTypeRelative && NumberOfCharacters == 1 ) {
                    return (ULONG)Prefix.Length - sizeof(UNICODE_NULL);
                    }
                return MaximumLength+sizeof(UNICODE_NULL);
                }
            else {

                //
                // If we are expanding curdir, then remember the trailing '\'
                //

                if ( NumberOfCharacters == 1 && *lpFileName == (WCHAR)'.' ) {

                    //
                    // We are expanding .
                    //

                    if ( Prefix.Length == 6 ) {
                        if ( nBufferLength <= Prefix.Length ) {
                            return (ULONG)(Prefix.Length+(USHORT)sizeof(UNICODE_NULL));
                            }
                        }
                    else {
                        if ( nBufferLength < Prefix.Length ) {
                            return (ULONG)Prefix.Length;
                            }
                        else {
                            for(i=0,j=0;i<Prefix.Length;i+=sizeof(WCHAR),j++){
                                if ( Prefix.Buffer[j] == (WCHAR)'\\' ||
                                     Prefix.Buffer[j] == (WCHAR)'/' ) {

                                    FullPath.Buffer[j] = (WCHAR)'\\';
                                    }
                                else {
                                    FullPath.Buffer[j] = Prefix.Buffer[j];
                                    }
                                }
                                FullPath.Length = Prefix.Length-(USHORT)sizeof((WCHAR)'\\');
                            goto skipit;
                            }
                        }
                    }
                else {
                    return MaximumLength;
                    }
                }
            }

        if (PrefixSourceLength || Prefix.Buffer != FullPath.Buffer) {
            //
            // Copy the prefix from the source string.
            //

            //RtlMoveMemory(FullPath.Buffer,lpFileName,PrefixSourceLength);

            for(i=0,j=0;i<PrefixSourceLength;i+=sizeof(WCHAR),j++){
                if ( lpFileName[j] == (WCHAR)'\\' ||
                     lpFileName[j] == (WCHAR)'/' ) {

                    FullPath.Buffer[j] = (WCHAR)'\\';
                    }
                else {
                    FullPath.Buffer[j] = lpFileName[j];
                    }
                }

            FullPath.Length = (USHORT)PrefixSourceLength;

            //
            // Append any additional prefix
            //

            for(i=0,j=0;i<Prefix.Length;i+=sizeof(WCHAR),j++){
                if ( Prefix.Buffer[j] == (WCHAR)'\\' ||
                     Prefix.Buffer[j] == (WCHAR)'/' ) {

                    FullPath.Buffer[j+(FullPath.Length>>1)] = (WCHAR)'\\';
                    }
                else {
                    FullPath.Buffer[j+(FullPath.Length>>1)] = Prefix.Buffer[j];
                    }
                }
            FullPath.Length += Prefix.Length;

            }
        else {
            FullPath.Length = Prefix.Length;
            }
skipit:
        Dest =  (PWSTR)((PUCHAR)FullPath.Buffer + FullPath.Length);
        *Dest = UNICODE_NULL;

        while ( *Source ) {
            switch ( *Source ) {

            case (WCHAR)'\\' :
            case (WCHAR)'/' :

                //
                // collapse multiple "\" characters.
                //

                if  ( *(Dest-1) != (WCHAR)'\\' ) {
                    *Dest++ = (WCHAR)'\\';
                    }

                Source++;
                break;

            case '.' :

                //
                // Ignore dot in a leading //./
                // Eat single dots as in /./
                // Double dots back up one level as in /../
                // Any other . is just a filename character
                //
                if ( IS_DOT_U(Source) ) {
                    Source++;
                    if (IS_PATH_SEPARATOR_U(*Source)) {
                        Source++;
                        }
                    break;
                    }
                else if ( IS_DOT_DOT_U(Source) ) {
                    //
                    // backup destination string looking for a '\'
                    //

                    while (*Dest != (WCHAR)'\\') {
                        *Dest = UNICODE_NULL;
                        Dest--;
                        }

                    //
                    // backup to previous component..
                    // \a\b\c\.. to \a\b
                    //

                    do {

                        //
                        // If we bump into root prefix, then
                        // stay at root
                        //

                        if ( Dest ==  FullPath.Buffer + (BackupIndex-1) ) {
                            break;
                            }

                        *Dest = UNICODE_NULL;
                        Dest--;

                        } while (*Dest != (WCHAR)'\\');
                    if ( Dest ==  FullPath.Buffer + (BackupIndex-1) ) {
                        Dest++;
                        }

                    //
                    // Advance source past ..
                    //

                    Source += 2;

                    break;
                    }

                //
                // FALLTHRU
                //

            default:

                //
                // Copy the filename. The copy will stop
                // on "non-portable" characters. Note that
                // null and /,\ will stop the copy. If any
                // charcter other than null or /,\ is encountered,
                // then the pathname is invalid.
                //

                //
                // strip trailing .'s within a component
                //

                while ( *Source && !IS_PATH_SEPARATOR_U(*Source) ) {
                    if ( *Source == (WCHAR)'.' ) {

                        //
                        // nuke .'s that preceed pathname seperators
                        //

                        if ( IS_PATH_SEPARATOR_U(*(Source+1)) ) {
                            Source++;
                            }
                        else {
                            *Dest++ = *Source++;
                            }
                        }
                    else {
                        *Dest++ = *Source++;
                        }
                    }
                }
            }

        *Dest = UNICODE_NULL;

        if ( StripTrailingSlash ) {
            if ( Dest > (FullPath.Buffer + BackupIndex ) && *(Dest-1) == (WCHAR)'\\' ) {
                Dest--;
                *Dest = UNICODE_NULL;
                }
            }
        FullPath.Length = (USHORT)Dest - (USHORT)FullPath.Buffer;

        //
        // strip trailing spaces and dots
        //

        Source = Dest-1;
        while(Source > FullPath.Buffer ) {
            if ( *Source == (WCHAR)' ' || *Source == (WCHAR)'.' ) {
                *Source = UNICODE_NULL;
                Dest--;
                Source--;
                FullPath.Length -= 2;
                }
            else {
                break;
                }
            }

        if ( ARGUMENT_PRESENT( lpFilePart ) ) {

            //
            // Locate the file part...
            //

            Source = Dest-1;
            Dest = NULL;

            while(Source > FullPath.Buffer ) {
                if ( *Source == (WCHAR)'\\' ) {
                    Dest = Source + 1;
                    break;
                    }
                Source--;
                }

            if ( Dest && *Dest ) {
                *lpFilePart = Dest;
                }
            else {
                *lpFilePart = NULL;
                }
            }
        }
    finally {
        RtlReleasePebLock();
        }

    return (ULONG)FullPath.Length;
}

ULONG
RtlGetFullPathName_U(
    PCWSTR lpFileName,
    ULONG nBufferLength,
    PWSTR lpBuffer,
    PWSTR *lpFilePart OPTIONAL
    )

{
    UNICODE_STRING UnicodeString;
    RTL_PATH_TYPE PathType;

    RtlInitUnicodeString(&UnicodeString,lpFileName);

    return RtlGetFullPathName_Ustr(&UnicodeString,nBufferLength,lpBuffer,lpFilePart,NULL,&PathType);
}

BOOLEAN
RtlpWin32NTNameToNtPathName_U(
    IN PUNICODE_STRING DosFileName,
    OUT PUNICODE_STRING NtFileName,
    OUT PWSTR *FilePart OPTIONAL,
    OUT PRTL_RELATIVE_NAME RelativeName OPTIONAL
    )
{

    PWSTR FullNtPathName = NULL;
    PWSTR Source,Dest;

    FullNtPathName = RtlAllocateHeap(
                        RtlProcessHeap(),
                        0,
                        DosFileName->Length-8+sizeof(UNICODE_NULL)+RtlpDosDevicesPrefix.Length
                        );
    if ( !FullNtPathName ) {
        return FALSE;
        }

    //
    // Copy the full Win32/NT path next to the name prefix, skipping over
    // the \\?\ at the front of the path.
    //

    RtlMoveMemory(FullNtPathName,RtlpDosDevicesPrefix.Buffer,RtlpDosDevicesPrefix.Length);
    RtlMoveMemory((PUCHAR)FullNtPathName+RtlpDosDevicesPrefix.Length,
                  DosFileName->Buffer + 4,
                  DosFileName->Length - 8);

    //
    // Null terminate the path name to make strlen below happy.
    //


    NtFileName->Buffer = FullNtPathName;
    NtFileName->Length = (USHORT)(DosFileName->Length-8+RtlpDosDevicesPrefix.Length);
    NtFileName->MaximumLength = NtFileName->Length + sizeof(UNICODE_NULL);
    FullNtPathName[ NtFileName->Length >> 1 ] = UNICODE_NULL;

    //
    // Now we have the passed in path with \DosDevices\ prepended. Blow out the
    // relative name structure (if provided), and possibly compute filepart
    //

    if ( ARGUMENT_PRESENT(RelativeName) ) {

        //
        // If the current directory is a sub-string of the
        // Nt file name, and if a handle exists for the current
        // directory, then return the directory handle and name
        // relative to the directory.
        //

        RelativeName->RelativeName.Length = 0;
        RelativeName->RelativeName.MaximumLength = 0;
        RelativeName->RelativeName.Buffer = 0;
        RelativeName->ContainingDirectory = NULL;
        }

    if ( ARGUMENT_PRESENT( FilePart ) ) {

        //
        // Locate the file part...
        //

        Source = &FullNtPathName[ (NtFileName->Length-1) >> 1 ];
        Dest = NULL;

        while(Source > FullNtPathName ) {
            if ( *Source == (WCHAR)'\\' ) {
                Dest = Source + 1;
                break;
                }
            Source--;
            }

        if ( Dest && *Dest ) {
            *FilePart = Dest;
            }
        else {
            *FilePart = NULL;
            }
        }

    return TRUE;
}

BOOLEAN
RtlDosPathNameToNtPathName_U(
    IN PCWSTR DosFileName,
    OUT PUNICODE_STRING NtFileName,
    OUT PWSTR *FilePart OPTIONAL,
    OUT PRTL_RELATIVE_NAME RelativeName OPTIONAL
    )

/*++

Routine Description:

    A Dos pathname can be translated into an Nt style pathname
    using this function.

    This function is used only within the Base dll to translate Dos
    pathnames to Nt pathnames. Upon successful translation, the
    pointer (NtFileName->Buffer) points to memory from RtlProcessHeap()
    that contains the Nt version of the input dos file name.

Arguments:

    DosFileName - Supplies the unicode Dos style file name that is to be
        translated into an equivalent unicode Nt file name.

    NtFileName - Returns the address of memory in the RtlProcessHeap() that
        contains an NT filename that refers to the specified Dos file
        name.

    FilePart - Optional parameter that if specified, returns the
        trailing file portion of the file name.  A path of \foo\bar\x.x
        returns the address of x.x as the file part.

    RelativeName - An optional parameter, that if specified, returns
        a pathname relative to the current directory of the file. The
        length field of RelativeName->RelativeName is 0 if the relative
        name can not be used.

Return Value:

    TRUE - The path name translation was successful.  Once the caller is
        done with the translated name, the memory pointed to by
        NtFileName.Buffer should be returned to the RtlProcessHeap().

    FALSE - The operation failed.

Note:
    The buffers pointed to by RelativeName, FilePart, and NtFileName must ALL
    point within the same memory address.  If they don't, code that calls
    this routine will fail.

--*/

{

    ULONG BufferLength;
    ULONG DosPathLength;
    PWSTR FullNtPathName = NULL;
    PWSTR FullDosPathName = NULL;
    UNICODE_STRING Prefix;
    UNICODE_STRING UnicodeFilePart;
    UNICODE_STRING FullDosPathString;
    PCURDIR CurDir;
    RTL_PATH_TYPE DosPathType;
    RTL_PATH_TYPE InputDosPathType;
    ULONG DosPathNameOffset;
    ULONG FullDosPathNameLength;
    ULONG LastCharacter;
    UNICODE_STRING UnicodeString;
    BOOLEAN NameInvalid;
    WCHAR StaticDosBuffer[DOS_MAX_PATH_LENGTH + 1];

    //
    // Calculate the size needed for the full pathname. Add in
    // space for the longest Nt prefix
    //

    BufferLength = (DOS_MAX_PATH_LENGTH << 1 ) + sizeof(UNICODE_NULL);
    DosPathLength = (DOS_MAX_PATH_LENGTH << 1 );

    if ( !BufferLength ) {
        return FALSE;
        }


    RtlAcquirePebLock();
    try {


        RtlInitUnicodeString(&UnicodeString,DosFileName);

        //
        // see if this is \\?\ form of name
        //

        if ( UnicodeString.Length > 8 && UnicodeString.Buffer[0] == '\\' &&
             UnicodeString.Buffer[1] == '\\' && UnicodeString.Buffer[2] == '?' &&
             UnicodeString.Buffer[3] == '\\' ) {

            if (RtlpWin32NTNameToNtPathName_U(&UnicodeString,NtFileName,FilePart,RelativeName)) {
                goto finally_exit;
                }
            else {
                return FALSE;
                }

            }

        //
        // The dos name starts just after the longest Nt prefix
        //

        FullDosPathName = &StaticDosBuffer[0];

        BufferLength += RtlpLongestPrefix;

        //
        // Allocate space for the full Nt Name (including DOS name portion)
        //

        FullNtPathName = RtlAllocateHeap(RtlProcessHeap(), 0, BufferLength);

        if ( !FullNtPathName ) {
            return FALSE;
            }

        FullDosPathNameLength = RtlGetFullPathName_Ustr(
                                    &UnicodeString,
                                    DosPathLength,
                                    FullDosPathName,
                                    FilePart,
                                    &NameInvalid,
                                    &InputDosPathType
                                    );

        if ( NameInvalid || !FullDosPathNameLength ||
              FullDosPathNameLength > DosPathLength ) {
            return FALSE;
            }

        //
        // Determine how to format prefix of FullNtPathName base on the
        // the type of Dos path name.  All Nt names begin in the \DosDevices
        // directory.
        //

        Prefix = RtlpDosDevicesPrefix;

        DosPathType = RtlDetermineDosPathNameType_U(FullDosPathName);

        switch (DosPathType) {
            case RtlPathTypeUncAbsolute :

                //
                // Unc name, use \DosDevices\UNC symbolic link to find
                // redirector.  Skip of \\ in source Dos path.
                //

                Prefix = RtlpDosDevicesUncPrefix;
                DosPathNameOffset = 2;
                break;

            case RtlPathTypeLocalDevice :

                //
                // Local device name, so just use \DosDevices prefix and
                // skip \\.\ in source Dos path.
                //

                DosPathNameOffset = 4;
                break;

            case RtlPathTypeRootLocalDevice :

                ASSERT( FALSE );
                break;

            case RtlPathTypeDriveAbsolute :
            case RtlPathTypeDriveRelative :
            case RtlPathTypeRooted :
            case RtlPathTypeRelative :

                //
                // All drive references just use \DosDevices prefix and
                // do not skip any of the characters in the source Dos path.
                //

                DosPathNameOffset = 0;
                break;

            default:
                ASSERT( FALSE );
            }

        //
        // Copy the full DOS path next to the name prefix, skipping over
        // the "\\" at the front of the UNC path or the "\\.\" at the front
        // of a device name.
        //

        RtlMoveMemory(FullNtPathName,Prefix.Buffer,Prefix.Length);
        RtlMoveMemory((PUCHAR)FullNtPathName+Prefix.Length,
                      FullDosPathName + DosPathNameOffset,
                      FullDosPathNameLength - (DosPathNameOffset<<1));

        //
        // Null terminate the path name to make strlen below happy.
        //


        NtFileName->Buffer = FullNtPathName;
        NtFileName->Length = (USHORT)(FullDosPathNameLength-(DosPathNameOffset<<1))+Prefix.Length;
        NtFileName->MaximumLength = (USHORT)BufferLength;
        LastCharacter = NtFileName->Length >> 1;
        FullNtPathName[ LastCharacter ] = UNICODE_NULL;


        //
        // Readjust the file part to point to the appropriate position within
        // the FullNtPathName buffer instead of inside the FullDosPathName
        // buffer
        //


        if ( ARGUMENT_PRESENT(FilePart) ) {
            if (*FilePart) {
                 RtlInitUnicodeString(&UnicodeFilePart,*FilePart);
                *FilePart = &FullNtPathName[ LastCharacter ] - (UnicodeFilePart.Length >> 1);
                }
            }

        if ( ARGUMENT_PRESENT(RelativeName) ) {

            //
            // If the current directory is a sub-string of the
            // Nt file name, and if a handle exists for the current
            // directory, then return the directory handle and name
            // relative to the directory.
            //

            RelativeName->RelativeName.Length = 0;
            RelativeName->RelativeName.MaximumLength = 0;
            RelativeName->RelativeName.Buffer = 0;
            RelativeName->ContainingDirectory = NULL;

            if ( InputDosPathType == RtlPathTypeRelative ) {

                CurDir = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);

                if ( CurDir->Handle ) {

                    //
                    // Now compare curdir to full dos path. If curdir length is
                    // greater than full path. It is not a match. Otherwise,
                    // trim full path length to cur dir length and compare.
                    //

                    RtlInitUnicodeString(&FullDosPathString,FullDosPathName);
                    if ( CurDir->DosPath.Length <= FullDosPathString.Length ) {
                        FullDosPathString.Length = CurDir->DosPath.Length;
                        if ( RtlEqualUnicodeString(
                                (PUNICODE_STRING)&CurDir->DosPath,
                                &FullDosPathString,
                                TRUE
                                ) ) {

                            //
                            // The full dos pathname is a substring of the
                            // current directory.  Compute the start of the
                            // relativename.
                            //

                            RelativeName->RelativeName.Buffer = ((PUCHAR)FullNtPathName + Prefix.Length - (DosPathNameOffset<<1) + (CurDir->DosPath.Length));
                            RelativeName->RelativeName.Length = (USHORT)FullDosPathNameLength - (CurDir->DosPath.Length);
                            if ( *(PWSTR)(RelativeName->RelativeName.Buffer) == (WCHAR)'\\' ) {
                                (PWSTR)(RelativeName->RelativeName.Buffer)++;
                                RelativeName->RelativeName.Length -= 2;
                                }
                            RelativeName->RelativeName.MaximumLength = RelativeName->RelativeName.Length;
                            RelativeName->ContainingDirectory = CurDir->Handle;
                            }
                        }
                    }
                }
            }
finally_exit:;
        }
    finally {

        if ( AbnormalTermination() ) {
            if ( FullNtPathName ) {
                RtlFreeHeap(RtlProcessHeap(), 0, FullNtPathName);
                }

            RtlReleasePebLock();

            return FALSE;
            }

        RtlReleasePebLock();
    }

    return TRUE;
}

ULONG
RtlDosSearchPath_U(
    IN PWSTR lpPath,
    IN PWSTR lpFileName,
    IN PWSTR lpExtension OPTIONAL,
    IN ULONG nBufferLength,
    OUT PWSTR lpBuffer,
    OUT PWSTR *lpFilePart
    )

/*++

Routine Description:

    This function is used to search for a file specifying a search path
    and a filename.  It returns with a fully qualified pathname of the
    found file.

    This function is used to locate a file using the specified path.  If
    the file is found, its fully qualified pathname is returned.  In
    addition to this, it calculates the address of the file name portion
    of the fully qualified pathname.

Arguments:

    lpPath - Supplies the search path to be used when locating the file.

    lpFileName - Supplies the file name of the file to search for.

    lpExtension - An optional parameter, that if specified, supplies an
        extension to be added to the filename when doing the search.
        The extension is only added if the specified filename does not
        end with an extension.

    nBufferLength - Supplies the length in bytes of the buffer that is
        to receive the fully qualified path.

    lpBuffer - Returns the fully qualified pathname corresponding to the
        file that was found.

    lpFilePart - Optional parameter that if specified, returns the
        address of the last component of the fully qualified pathname.

Return Value:

    The return value is the length of the string copied to lpBuffer, not
    including the terminating null character.  If the return value is
    greater than nBufferLength, the return value is the size of the buffer
    required to hold the pathname.  The return value is zero if the
    function failed.


--*/

{

    PWSTR ComputedFileName;
    ULONG ExtensionLength;
    ULONG PathLength;
    ULONG FileLength;
    PWSTR p;
    UNICODE_STRING Scratch;

    //
    // if the file name is not a relative name, then
    // return an if the file does not exist.
    //

    if ( RtlDetermineDosPathNameType_U(lpFileName) != RtlPathTypeRelative ) {
        if (RtlDoesFileExists_U(lpFileName) ) {
            PathLength = RtlGetFullPathName_U(
                           lpFileName,
                           nBufferLength,
                           lpBuffer,
                           lpFilePart
                           );
            return PathLength;
            }
        else {
            return 0;
            }
        }

    //
    // Determine if the file name contains an extension
    //

    ExtensionLength = 1;
    p = lpFileName;
    while (*p) {
        if ( *p == (WCHAR)'.' ) {
            ExtensionLength = 0;
            break;
            }
        p++;
        }

    //
    // If no extension was found, then determine the extension length
    // that should be used to search for the file
    //

    if ( ExtensionLength ) {
        if ( ARGUMENT_PRESENT(lpExtension) ) {
            RtlInitUnicodeString(&Scratch,lpExtension);
            ExtensionLength = Scratch.Length;
            }
        else {
            ExtensionLength = 0;
            }
        }

    //
    // Compute the file name length and the path length;
    //

    RtlInitUnicodeString(&Scratch,lpPath);
    PathLength = Scratch.Length;
    RtlInitUnicodeString(&Scratch,lpFileName);
    FileLength = Scratch.Length;

    ComputedFileName = RtlAllocateHeap(
                            RtlProcessHeap(), 0,
                            PathLength + FileLength + ExtensionLength + 3*sizeof(UNICODE_NULL)
                            );

    if ( !ComputedFileName ) {
        return 0;
        }

    //
    // find ; 's in path and copy path component to computed file name
    //

    do {
        p = ComputedFileName;
        while (*lpPath) {
            if (*lpPath == (WCHAR)';') {
                lpPath++;
                break;
                }
            *p++ = *lpPath++;
            }

        if (p != ComputedFileName &&
            p [ -1 ] != (WCHAR)'\\' ) {
            *p++ = (WCHAR)'\\';
            }
        if (*lpPath == UNICODE_NULL) {
            lpPath = NULL;
            }
        RtlMoveMemory(p,lpFileName,FileLength);
        if ( ExtensionLength ) {
            RtlMoveMemory((PUCHAR)p+FileLength,lpExtension,ExtensionLength+sizeof(UNICODE_NULL));
            }
        else {
            *(PWSTR)((PUCHAR)p+FileLength) = UNICODE_NULL;
            }

        if (RtlDoesFileExists_U(ComputedFileName) ) {
            PathLength = RtlGetFullPathName_U(
                           ComputedFileName,
                           nBufferLength,
                           lpBuffer,
                           lpFilePart
                           );
            RtlFreeHeap(RtlProcessHeap(), 0, ComputedFileName);
            return PathLength;
            }
        }
    while ( lpPath );

    RtlFreeHeap(RtlProcessHeap(), 0, ComputedFileName);
    return 0;
}

BOOLEAN
RtlDoesFileExists_U(
    IN PCWSTR FileName
    )

/*++

Routine Description:

    This function checks to see if the specified unicode filename exists.

Arguments:

    FileName - Supplies the file name of the file to find.

Return Value:

    TRUE - The file was found.

    FALSE - The file was not found.

--*/

{
    NTSTATUS Status;
    OBJECT_ATTRIBUTES Obja;
    UNICODE_STRING NtFileName;
    BOOLEAN ReturnValue;
    RTL_RELATIVE_NAME RelativeName;
    PVOID FreeBuffer;
    FILE_BASIC_INFORMATION BasicInfo;

    ReturnValue = RtlDosPathNameToNtPathName_U(
                    FileName,
                    &NtFileName,
                    NULL,
                    &RelativeName
                    );

    if ( !ReturnValue ) {
        return FALSE;
        }

    FreeBuffer = NtFileName.Buffer;

    if ( RelativeName.RelativeName.Length ) {
        NtFileName = *(PUNICODE_STRING)&RelativeName.RelativeName;
        }
    else {
        RelativeName.ContainingDirectory = NULL;
        }

    InitializeObjectAttributes(
        &Obja,
        &NtFileName,
        OBJ_CASE_INSENSITIVE,
        RelativeName.ContainingDirectory,
        NULL
        );

    //
    // Query the file's attributes.  Note that the file cannot simply be opened
    // to determine whether or not it exists, as the NT LanMan redirector lies
    // on NtOpenFile to a Lan Manager server because it does not actually open
    // the file until an operation is performed on it.
    //

    Status = NtQueryAttributesFile(
                &Obja,
                &BasicInfo
                );
    RtlFreeHeap(RtlProcessHeap(),0,FreeBuffer);

    if ( !NT_SUCCESS(Status) ) {
        if ( Status == STATUS_SHARING_VIOLATION ||
             Status == STATUS_ACCESS_DENIED ) {
            ReturnValue = TRUE;
            }
        else {
            ReturnValue = FALSE;
            }
        }
    else {
        ReturnValue = TRUE;
        }
    return ReturnValue;
}

VOID
RtlpCheckRelativeDrive(
    WCHAR NewDrive
    )

/*++

Routine Description:

    This function is called whenever we are asked to expand a non
    current directory drive relative name ( f:this\is\my\file ).  In
    this case, we validate the environment variable string to make sure
    the current directory at that drive is valid. If not, we trim back to
    the root.

Arguments:

    NewDrive - Supplies the drive to check

Return Value:

    None.

--*/

{

    WCHAR EnvVarValueBuffer[DOS_MAX_PATH_LENGTH+12]; // + sizeof (\DosDevices\)
    WCHAR EnvVarNameBuffer[4];
    UNICODE_STRING EnvVarName;
    UNICODE_STRING EnvValue;
    NTSTATUS Status;
    OBJECT_ATTRIBUTES Obja;
    IO_STATUS_BLOCK IoStatusBlock;
    HANDLE DirHandle;
    ULONG HardErrorValue;
    PTEB Teb;

    EnvVarNameBuffer[0] = (WCHAR)'=';
    EnvVarNameBuffer[1] = (WCHAR)NewDrive;
    EnvVarNameBuffer[2] = (WCHAR)':';
    EnvVarNameBuffer[3] = UNICODE_NULL;
    RtlInitUnicodeString(&EnvVarName,EnvVarNameBuffer);


    //
    // capture the value in a buffer that has space at the front for the dos devices
    // prefix
    //

    EnvValue.Length = 0;
    EnvValue.MaximumLength = DOS_MAX_PATH_LENGTH<<1;
    EnvValue.Buffer = &EnvVarValueBuffer[RtlpDosDevicesPrefix.Length>>1];

    Status = RtlQueryEnvironmentVariable_U( NULL,
                                            &EnvVarName,
                                            &EnvValue
                                          );
    if ( !NT_SUCCESS( Status ) ) {

        //
        // Otherwise default to root directory of drive
        //

        EnvValue.Buffer[0] = (WCHAR)NewDrive;
        EnvValue.Buffer[1] = (WCHAR)':';
        EnvValue.Buffer[2] = (WCHAR)'\\';
        EnvValue.Buffer[3] = UNICODE_NULL;
        EnvValue.Length = 6;
        }

    //
    // Form the NT name for this directory
    //

    EnvValue.Length = EnvValue.Length + RtlpDosDevicesPrefix.Length;
    EnvValue.MaximumLength = sizeof(EnvValue);
    EnvValue.Buffer = EnvVarValueBuffer;
    RtlCopyMemory(EnvVarValueBuffer,RtlpDosDevicesPrefix.Buffer,RtlpDosDevicesPrefix.Length);

    InitializeObjectAttributes(
        &Obja,
        &EnvValue,
        OBJ_CASE_INSENSITIVE,
        NULL,
        NULL
        );


    Teb = NtCurrentTeb();
    HardErrorValue = Teb->HardErrorsAreDisabled;
    Teb->HardErrorsAreDisabled = 1;

    Status = NtOpenFile(
                &DirHandle,
                SYNCHRONIZE,
                &Obja,
                &IoStatusBlock,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT
                );

    Teb->HardErrorsAreDisabled = HardErrorValue;

    //
    // If the open succeeds, then the directory is valid... No need to do anything
    // further. If the open fails, then trim back the environment to the root.
    //

    if ( NT_SUCCESS(Status) ) {
        NtClose(DirHandle);
        return;
        }

    RtlpResetDriveEnvironment(NewDrive);
}