summaryrefslogtreecommitdiffstats
path: root/private/os2/os2ss/sbcnfg.c
blob: f93a567d957f933080d9bcb580c4d4362004c715 (plain) (blame)
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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    sbcnfg.c

Abstract:

    This module contains the code necessary to initialize the
    OS/2 SS entries in the registry.  Since the SS modifies the
    system environment, we need a privileged process to do this,
    and so we run it in os2ss.exe.  This initialization takes
    place the 1st time that os2ss.exe is run after system setup,
    or whenever the CONFIG.SYS entry in the registry disappears.

Author:

    Ofer Porat (oferp) 16-Mar-1993

Environment:

    User Mode only

Revision History:

    Code was originally in server\srvcnfg.c.

--*/

//
// Only compiled for PMNT
//

#ifdef PMNT

#include <wchar.h>
#include "os2srv.h"

#define PATHLIST_MAX        1024        // max length of pathlists such as Os2LibPath (in characters)
#define MAX_CONSYS_SIZE     16384       // max size of config.sys buffers (in bytes)
#define DOS_DEV_LEN         12          // length of "\\DosDevices\\"

static WCHAR Os2SoftwareDirectory[]  = L"\\REGISTRY\\MACHINE\\SOFTWARE\\Microsoft";
static WCHAR Os2ProductDirectory[]   = L"OS/2 Subsystem for NT";
static WCHAR Os2VersionDirectory[]   = L"1.0";
static WCHAR Os2IniName[]            = L"os2.ini";
static WCHAR Os2ConfigSysName[]      = L"config.sys";
static WCHAR Os2Class[]              = L"OS2SS";
static CHAR  Os2ConfigSysDefaultValue[] =
//
// The '\a' in the following strings will be replaced by the SystemDirectory Value
//
"NTREM Here is a summary of what is allowed to appear in this registry entry:\0"
"NTREM   Comments starting with REM will be visible to the user when s/he opens\0"
"NTREM     c:\\config.sys.\0"
"NTREM   Comments starting with NTREM are only visible by direct access to the\0"
"NTREM     registry.\0"
"NTREM   The following OS/2 configuration commands are significant:\0"
"NTREM     COUNTRY=\0"
"NTREM     CODEPAGE=\0"
"NTREM     DEVINFO=KBD,\0"
"NTREM   Any other commands apart from the exceptions listed below will be\0"
"NTREM   visible to an OS/2 program that opens c:\\config.sys, however they are\0"
"NTREM   not used internally by the NT OS/2 SubSystem.\0"
"NTREM   Exceptions:\0"
"NTREM     The following commands are completely ignored.  Their true values\0"
"NTREM     appear in the system environment and should be modified using the\0"
"NTREM     Control Panel System applet.  Note that LIBPATH is called Os2LibPath\0"
"NTREM     in the NT system environment.\0"
"SET PATH=<ignored>\0"
"LIBPATH=<ignored>\0"
"NTREM     In addition, any \"SET=\" commands (except COMSPEC) will be\0"
"NTREM     completely ignored.  You should set OS/2 environment variables just\0"
"NTREM     like any other Windows NT environment variables by using the Control\0"
"NTREM     Panel System applet.\0"
"NTREM   If you have an OS/2 editor available, it is highly recommended that you\0"
"NTREM   modify NT OS/2 config.sys coniguration by editing c:\\config.sys with\0"
"NTREM   this editor.  This is the documented way to make such modification, and\0"
"NTREM   is therefore less error-prone.\0"
"NTREM   Now comes the actual text.\0"
"REM\0"
"REM This is a fake OS/2 config.sys file used by the NT OS/2 SubSystem.\0"
"REM The following information resides in the Registry and NOT in a disk file.\0"
"REM OS/2 Apps that access c:\\config.sys actually manipulate this information.\0"
"REM\0"
"PROTSHELL=c:\\os2\\pmshell.exe c:\\os2\\os2.ini c:\\os2\\os2sys.ini \a\\cmd.exe\0"
"SET COMSPEC=\a\\cmd.exe\0"
;

static WCHAR Os2ConfigSysKeyName[] =
L"\\REGISTRY\\MACHINE\\SOFTWARE\\Microsoft\\OS/2 Subsystem for NT\\1.0\\config.sys";
static WCHAR Os2EnvironmentDirectory[] =
L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
static HANDLE Os2EnvironmentKeyHandle = NULL;

static WCHAR Os2OriginalCanonicalConfigSys[] = L"\\DosDevices\\C:\\CONFIG.SYS";

static BOOLEAN Os2LibPathFound = FALSE;
static WCHAR Os2LibPathValueName[] = L"Os2LibPath";
static UNICODE_STRING Os2LibPathValueData_U;

static PWSTR pOs2ConfigSys = NULL;
static ULONG Os2SizeOfConfigSys = 0;
static PWSTR pOs2UpperCaseConfigSys = NULL;

static WCHAR Os2SystemDirectory[DOS_MAX_PATH_LENGTH];


VOID
Os2InitMBString(
    PANSI_STRING DestinationString,
    PCSZ         SourceString
    )
/*++

Routine Description:

    This routine init an ASCII character string (buffer and length)

Arguments:

    DestinationString - pointer to ansi string to put the result in

    SourceString - pointer to ansi null terminated string to read from

Return Value:


Note:

    Od2ProcessCodePage is used as the code page for the mapping.

    Od2CurrentCodePageIsOem is check to see if RtlOem NLS routines
        can be used.
--*/

{

    // BUGBUG: add support if Code Page is diff from OEMCP

    RtlInitAnsiString(
            DestinationString,
            SourceString);

    return ;
}


NTSTATUS
Os2MBStringToUnicodeString(
    PUNICODE_STRING DestinationString,
    PANSI_STRING    SourceString,
    BOOLEAN         AllocateDestinationString
    )
/*++

Routine Description:

    This routine map a multibyte character string to its unicode character
    counterpart.

Arguments:

    DestinationString - pointer to unicode string to get the mapping result

    SourceString - pointer to ansi string to read string to map from

    AllocateDestinationString - flag indicating if need to allocate space
            for destination string

Return Value:


Note:

    Od2ProcessCodePage is used as the code page for the mapping.

    Od2CurrentCodePageIsOem is check to see if RtlOem NLS routines
        can be used.

--*/

{
    NTSTATUS    Status;

    // BUGBUG: add support if Code Page is diff from OEMCP (use Od2CurrentCodePageIsOem)

    Status = RtlOemStringToUnicodeString(
                DestinationString,
                (POEM_STRING)SourceString,
                AllocateDestinationString
                );

    return(Status);
}


BOOLEAN
Os2InitOriginalConfigSysProcessing(
    VOID
    )

/*++

Routine Description:

    This function checks if there is an OS/2 config.sys configuration file on the
    disk.  If so, it opens it and reads it in.  The file is converted to UNICODE,
    and an upper case copy of it is made.

Arguments:

    None.

Return Value:

    TRUE if there's an OS/2 config.sys, and all the operations needed to prepare
    it have succeeded.  FALSE otherwise.

Notes:
    On success Sets global variables as follows:
        pOs2ConfigSys - a null-terminated UNICODE copy of the OS/2 config.sys.
        pOs2UpperCaseConfigSys - an upper case copy of pOs2ConfigSys.
        Os2SizeOfConfigSys - the number of characters in the above strings.

--*/

{
    UNICODE_STRING CanonicalConfigDotSys_U;
    UNICODE_STRING Tmp_U;
    ANSI_STRING Tmp_MB;
    OBJECT_ATTRIBUTES Obja;
    FILE_STANDARD_INFORMATION FileStandardInfo;
    NTSTATUS Status;
    IO_STATUS_BLOCK IoStatus;
    HANDLE ConfigSysFileHandle;
    PSZ pTempOs2ConfigSys;

    // Try opening OS/2's config.sys file

    RtlInitUnicodeString(&CanonicalConfigDotSys_U, Os2OriginalCanonicalConfigSys);

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

    Status = NtOpenFile(&ConfigSysFileHandle,
                        FILE_GENERIC_READ,
                        &Obja,
                        &IoStatus,
                        FILE_SHARE_READ,
                        FILE_SYNCHRONOUS_IO_NONALERT
                       );
    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - NtOpenFile-1 %lx\n", Status));
        }
#endif
        return (FALSE);
    }

    // Get the file length

    Status = NtQueryInformationFile(ConfigSysFileHandle,
                                    &IoStatus,
                                    &FileStandardInfo,
                                    sizeof(FileStandardInfo),
                                    FileStandardInformation
                                   );
    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - NtQueryInformationFile %lx\n", Status));
        }
#endif
        NtClose(ConfigSysFileHandle);
        return (FALSE);
    }

    Os2SizeOfConfigSys = FileStandardInfo.EndOfFile.LowPart;

    // Allocate space for reading it in

    // the + 1 in following parameter is for inserting the NUL character

    pTempOs2ConfigSys = (PSZ) RtlAllocateHeap(Os2Heap, 0, Os2SizeOfConfigSys + 1);

    if (pTempOs2ConfigSys == NULL) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - RtlAllocateHeap pTempOs2ConfigSys\n"));
        }
#endif
        Os2SizeOfConfigSys = 0;
        NtClose(ConfigSysFileHandle);
        return (FALSE);
    }

    pOs2ConfigSys = (PWSTR) RtlAllocateHeap(Os2Heap, 0, (Os2SizeOfConfigSys + 1) * sizeof(WCHAR));

    if (pOs2ConfigSys == NULL) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - RtlAllocateHeap pOs2ConfigSys\n"));
        }
#endif
        Os2SizeOfConfigSys = 0;
        RtlFreeHeap(Os2Heap, 0, pTempOs2ConfigSys);
        NtClose(ConfigSysFileHandle);
        return (FALSE);
    }

    // Read it in

    Status = NtReadFile(ConfigSysFileHandle,
                         NULL,
                         NULL,
                         NULL,
                         &IoStatus,
                         (PVOID)pTempOs2ConfigSys,
                         Os2SizeOfConfigSys,
                         NULL,
                         NULL
                        );
    NtClose(ConfigSysFileHandle);
    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - NtReadFile %lx\n", Status));
        }
#endif
        Os2SizeOfConfigSys = 0;
        RtlFreeHeap(Os2Heap, 0, pTempOs2ConfigSys);
        RtlFreeHeap(Os2Heap, 0, pOs2ConfigSys);
        pOs2ConfigSys = NULL;
        return (FALSE);
    }

    pTempOs2ConfigSys[Os2SizeOfConfigSys] = '\0';

    // Convert to UNICODE

    Os2InitMBString(&Tmp_MB, pTempOs2ConfigSys);

    Tmp_U.Buffer = pOs2ConfigSys;
    Tmp_U.MaximumLength = (USHORT) ((Os2SizeOfConfigSys + 1) * sizeof(WCHAR));

    Os2MBStringToUnicodeString(&Tmp_U, &Tmp_MB ,FALSE);

    Os2SizeOfConfigSys = Tmp_U.Length / sizeof(WCHAR);

    pOs2ConfigSys[Os2SizeOfConfigSys] = UNICODE_NULL;

    RtlFreeHeap(Os2Heap, 0, pTempOs2ConfigSys);

    // Prepare the upper case copy

    pOs2UpperCaseConfigSys = (PWSTR) RtlAllocateHeap(Os2Heap, 0, (Os2SizeOfConfigSys + 1) * sizeof(WCHAR));
    if (pOs2UpperCaseConfigSys == NULL) {
#if DBG
        IF_OS2_DEBUG( INIT )
        {
            KdPrint(("Os2InitOriginalConfigSysProcessing): FAILED - RtlAllocateHeap pOs2UpperConfigSys\n"));
        }
#endif
        Os2SizeOfConfigSys = 0;
        RtlFreeHeap(Os2Heap, 0, pOs2ConfigSys);
        pOs2ConfigSys = NULL;
        return (FALSE);
    }

    wcscpy(pOs2UpperCaseConfigSys, pOs2ConfigSys);
    Or2UnicodeStrupr(pOs2UpperCaseConfigSys);

    //
    // Verify that the CONFIG.SYS file is really an OS/2 file and not a
    // DOS file.
    // Look for certain strings that MUST appear in an OS/2 CONFIG.SYS
    // file and don't appear in DOS CONFIG.SYS files
    //

    if ((wcsstr(pOs2UpperCaseConfigSys, L"LIBPATH") == NULL) ||
        (wcsstr(pOs2UpperCaseConfigSys, L"PROTSHELL") == NULL) ||
        (wcsstr(pOs2UpperCaseConfigSys, L"PROTECTONLY") == NULL)
       ) {
        Os2SizeOfConfigSys = 0;
        RtlFreeHeap(Os2Heap, 0, pOs2ConfigSys);
        RtlFreeHeap(Os2Heap, 0, pOs2UpperCaseConfigSys);
        pOs2UpperCaseConfigSys = pOs2ConfigSys = NULL;
        return (FALSE);
    }

    return (TRUE);
}


VOID
Os2SetDirectiveProcessingDispatchFunction(
    IN ULONG DispatchTableIndex,
    IN PVOID UserParameter,
    IN PWSTR Name,
    IN ULONG NameLen,
    IN PWSTR Value,
    IN ULONG ValueLen
    )

/*++

Routine Description:

    This is a Dispatch Routine that is used to process SET directives in OS/2's config.sys
    file.  The directives are entered into the system environment.

Arguments:

    Standard arguments passed to a Dispatch Function, see the description of
    Or2IterateEnvironment in ssrtl\consys.c

    UserParameter - points to a pointer which indicates the current position in the
        config.sys registry entry we're building.

Return Value:

    None.

--*/

{

    UNICODE_STRING  VarName_U;      // for setting up variable name
    UNICODE_STRING  VarValue_U;     // for setting up variable value
    PWSTR           Dest = *(PWSTR *) UserParameter;
    NTSTATUS        Status;
    ULONG           ResultLength;
    WCHAR           wch;
    KEY_VALUE_PARTIAL_INFORMATION KeyValueInfo;

    // Set up variable name

    VarName_U.Buffer = Value;
    VarName_U.Length = 0;

    while ((ValueLen > 0) && (*Value != L'=')) {
        Value++;
        VarName_U.Length += sizeof(WCHAR);
        ValueLen--;
    }

    if (ValueLen == 0 ||            // End of line reached without finding '='
        VarName_U.Length == 0) {    // Empty name
        return;
    }

    VarName_U.MaximumLength = VarName_U.Length;

    // Following SET directives are ignored

    if (Or2UnicodeEqualCI(VarName_U.Buffer, L"COMSPEC=", 8) ||
        Or2UnicodeEqualCI(VarName_U.Buffer, L"PATH=", 5) ||
        Or2UnicodeEqualCI(VarName_U.Buffer, L"VIDEO_DEVICES=", 14) ||
        Or2UnicodeEqualCI(VarName_U.Buffer, L"VIO_IBMVGA=", 11) ||
        Or2UnicodeEqualCI(VarName_U.Buffer, L"VIO_VGA=", 8) ||
        Or2UnicodeEqualCI(VarName_U.Buffer, L"PROMPT=", 7)
       ) {
        return;
    }

    // Here, we have a valid name, followed by '='
    Value++;    // Skip the '='
    ValueLen--;

    // Set up variable value

    VarValue_U.Buffer = Value;
    VarValue_U.Length = (USHORT) (ValueLen * sizeof(WCHAR));  // what's left of the line
    VarValue_U.MaximumLength = VarValue_U.Length;

    //
    // Update the information in the registry with the info
    // in the file
    //

#if 0
    // not in effect anymore
    //
    // The KEYS variable is handled in a special way.
    // It's put in the registry config.sys in order to prevent possible
    // conflict.
    //

    if (Or2UnicodeEqualCI(VarName_U.Buffer, L"KEYS=", 5)) {
        RtlMoveMemory(Dest, L"SET ", 8);
        Dest += 4;
        RtlMoveMemory(Dest, VarName_U.Buffer, VarName_U.Length);
        Dest += VarName_U.Length / sizeof(WCHAR);
        *Dest++ = L'=';
        RtlMoveMemory(Dest, VarValue_U.Buffer, VarValue_U.Length);
        Dest += VarValue_U.Length / sizeof(WCHAR);
        *Dest++ = UNICODE_NULL;
        *(PWSTR *) UserParameter = Dest;
        return;
    }
#endif

    //
    // Otherwise, it's stored in the system environment
    //

    //
    // If Os2EnvironmentKeyHandle is NULL, we don't have
    // write access to the system environment, and we skip
    // setting the variable.
    //

    if (Os2EnvironmentKeyHandle == NULL) {
        return;
    }

    //
    // If a value key of the same name already exists,
    // don't replace it. This is done in order to prevent
    // the OS/2 SS from overriding possible NT definitions
    //

    Status = NtQueryValueKey(Os2EnvironmentKeyHandle,
                             &VarName_U,
                             KeyValuePartialInformation,
                             &KeyValueInfo,
                             sizeof(KeyValueInfo),
                             &ResultLength
                            );

    if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {

        //
        // Set the system wide variable to the value specified
        // in the original OS/2 config.sys
        //

        wch = Value[ValueLen];
        Value[ValueLen] = UNICODE_NULL;

        Status = NtSetValueKey(Os2EnvironmentKeyHandle,
                               &VarName_U,
                               (ULONG)0,
                               REG_EXPAND_SZ,
                               VarValue_U.Buffer,
                               VarValue_U.Length + sizeof(WCHAR)
                              );

        Value[ValueLen] = wch;

        if (!NT_SUCCESS(Status))
        {
#if DBG
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("Os2SetDirectiveProcessingDispatchFunction: Unable to NtSetValueKey() system env, rc = %X\n",
                           Status));
            }
#endif
            return;
        }

    } else {
#if DBG
        if (Status != STATUS_BUFFER_OVERFLOW) {
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("Os2SetDirectiveProcessingDispatchFunction: Unable to NtQueryValueKey() system env, rc = %X\n",
                           Status));
            }
        }
#endif
        return;
    }
}


VOID
Os2CommaProcessingDispatchFunction(
    IN ULONG DispatchTableIndex,
    IN PVOID UserParameter,
    IN PWSTR Name,
    IN ULONG NameLen,
    IN PWSTR Value,
    IN ULONG ValueLen
    )

/*++

Routine Description:

    This is a Dispatch Routine that is used to process certain directives in OS/2's config.sys
    file.  The directives are copied into the config.sys registry entry we're building.
    Some directives are truncated after a certain number of commas.

Arguments:

    Standard arguments passed to a Dispatch Function, see the description of
    Or2IterateEnvironment in ssrtl\consys.c

    UserParameter - points to a pointer which indicates the current position in the
        config.sys registry entry we're building.

Return Value:

    None.

--*/

{
//
// Lines passed to this dispatch function need to be copied to the output as are
// except they should be truncated after a certain number of commas.  The
// following table lists the number of commas.  0 means no truncation.
//
    ULONG ItemTable[] = { 1,     // COUNTRY
                          0,     // CODEPAGE
                          2      // DEVINFO (only with KBD)
                        };
    PWSTR Dest = *(PWSTR *) UserParameter;
    ULONG CommaCtr;

    if (DispatchTableIndex== 2 && !Or2UnicodeEqualCI(Value, L"KBD,", 4)) {
        return;
    }

    // First, copy the Name

    RtlMoveMemory(Dest, Name, NameLen * sizeof(WCHAR));
    Dest += NameLen;
    *Dest++ = L'=';

    // Now, copy the value for the right number of commas

    CommaCtr = 0;

    while (ValueLen > 0) {
        if (ItemTable[DispatchTableIndex] != 0 && *Value == L',') {
            CommaCtr++;
            if (CommaCtr == ItemTable[DispatchTableIndex]) {
                break;
            }
        }

        *Dest++ = *Value++;
        ValueLen--;
    }
    *Dest++ = UNICODE_NULL;
    *(PWSTR *) UserParameter = Dest;
}


VOID
Os2ProcessOriginalConfigSys(
    IN OUT PWSTR *DestPtr
    )

/*++

Routine Description:

    This function processes OS/2's config.sys file after it has been properly initialized
    by Os2InitOriginalConfigSysProcessing.

Arguments:

    DestPtr - points to a pointer which indicates the current position in the
        config.sys registry entry we're building.

Return Value:

    None.

--*/

{
    static ENVIRONMENT_DISPATCH_TABLE_ENTRY DispatchTable[] =
        {
            { L"COUNTRY", L"=", Os2CommaProcessingDispatchFunction, NULL },
            { L"CODEPAGE", L"=", Os2CommaProcessingDispatchFunction, NULL },
            { L"DEVINFO", L"=", Os2CommaProcessingDispatchFunction, NULL },
            { L"LIBPATH", L"=", Or2FillInSearchRecordDispatchFunction, NULL },
            { L"SET", L" \t", Os2SetDirectiveProcessingDispatchFunction, NULL }
        };
    ENVIRONMENT_SEARCH_RECORD LibPathRecord;
    ULONG i;
    PWSTR p;
    WCHAR ch;

    //
    // Most of the job is done by Or2IterateEnvironment which processes the file
    // according to a dispatch table.
    //
    // LibPath needs to be handled a little differently.  We need to process only
    // the *last* occurence of LIBPATH= in the file (this is the same as OS/2).
    // Therefore we only record the position of the LIBPATH= statments as we run
    // into them.  After we're finished we'll have the position of the last one,
    // and we can process that line.
    //

    for (i = 0; i < 5; i++) {
        DispatchTable[i].UserParameter = (PVOID) DestPtr;
    }

    DispatchTable[3].UserParameter = (PVOID)&LibPathRecord;
    LibPathRecord.DispatchTableIndex = (ULONG)-1;

    Or2IterateEnvironment(pOs2ConfigSys,
                          DispatchTable,
                          5,
                          CRLF_DELIM);


    if (Os2LibPathFound && LibPathRecord.DispatchTableIndex != (ULONG)-1) {

        // handle LIBPATH if there was one

        // get a pointer to the upper case version, so we can append it

        p = pOs2UpperCaseConfigSys + (LibPathRecord.Value - pOs2ConfigSys);

        ch = p[LibPathRecord.ValueLen];
        p[LibPathRecord.ValueLen] = UNICODE_NULL;

        Or2AppendPathToPath(Os2Heap,
                            p,
                            &Os2LibPathValueData_U,
                            TRUE);
        p[LibPathRecord.ValueLen] = ch;
    }
}


VOID
Os2TerminateOriginalConfigSysProcessing(
    VOID
    )

/*++

Routine Description:

    Cleans up processing of OS/2's config.sys.  Releases the storage used to store the
    file.

Arguments:

    None.

Return Value:

    None.

--*/

{
    RtlFreeHeap(Os2Heap, 0, pOs2ConfigSys);
    RtlFreeHeap(Os2Heap, 0, pOs2UpperCaseConfigSys);
    pOs2UpperCaseConfigSys = pOs2ConfigSys = NULL;
}


NTSTATUS
Os2SbBuildSD(
    PSECURITY_DESCRIPTOR SecurityDescriptor,
    PACL *pDacl
    )

/*++

Routine Description:

    Builds a security descriptor for creating our registry keys.

    Administrators -- all access
    everyone -- read access

Arguments:

    SecurityDescriptor -- supplies a pointer to a preallocated SD that will be built
    pDacl -- returns a pointer to a dacl that should be released from Os2Heap after
             we're finished using the security descriptor.

Return Value:

    NT error code.

--*/

{
    PACL Dacl;
    PACE_HEADER Pace;
    PSID AdminAliasSid;
    ULONG DaclSize;
    SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
    SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
    PSID WorldSid;
    NTSTATUS Status;

    //
    // Create the SIDs for local admin and World.
    //

    Status = RtlAllocateAndInitializeSid(
                 &NtAuthority,
                 2,
                 SECURITY_BUILTIN_DOMAIN_RID,
                 DOMAIN_ALIAS_RID_ADMINS,
                 0, 0, 0, 0, 0, 0,
                 &AdminAliasSid
                 );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlAllocateAndInitializeSid(Admin), Status = %lx\n", Status));
        }
#endif
        return (Status);
    }

    Status = RtlAllocateAndInitializeSid(
                 &WorldSidAuthority,
                 1,
                 SECURITY_WORLD_RID,
                 0, 0, 0, 0, 0, 0, 0,
                 &WorldSid
                 );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlAllocateAndInitializeSid(World), Status = %lx\n", Status));
        }
#endif
        return (Status);
    }

    Status = RtlCreateSecurityDescriptor( SecurityDescriptor,
                                          SECURITY_DESCRIPTOR_REVISION );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlCreateSecurityDescriptor, Status = %lx\n", Status));
        }
#endif
        return (Status);
    }


    //
    // Compute the size of the buffer needed for the
    // DACL.
    //

    DaclSize = sizeof( ACL ) +
               2 * (sizeof( ACCESS_ALLOWED_ACE ) - sizeof( ULONG ))
               +
               RtlLengthSid( AdminAliasSid ) +
               RtlLengthSid( WorldSid );

    Dacl = (PACL) RtlAllocateHeap(Os2Heap, 0, DaclSize);

    if (Dacl == NULL) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlAllocateHeap failed\n"));
        }
#endif
        return(STATUS_NO_MEMORY);
    }

    //
    // Build the ACL
    //

    Status = RtlCreateAcl ( Dacl, DaclSize, ACL_REVISION2 );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlCreateAcl, Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    Status = RtlAddAccessAllowedAce (
                 Dacl,
                 ACL_REVISION2,
                 GENERIC_ALL,
                 AdminAliasSid
                 );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlAddAccessAllowedAce(Admin), Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    Status = RtlAddAccessAllowedAce (
                 Dacl,
                 ACL_REVISION2,
                 GENERIC_READ,
                 WorldSid
                 );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlAddAccessAllowedAce(World), Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    Status = RtlGetAce(
                Dacl,
                0L,
                &Pace
                );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlGetAce(Admin), Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    Pace->AceFlags |= CONTAINER_INHERIT_ACE;

    Status = RtlGetAce(
                Dacl,
                1L,
                &Pace
                );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlGetAce(World), Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    Pace->AceFlags |= CONTAINER_INHERIT_ACE;

    //
    // Add the ACL to the security descriptor
    //

    Status = RtlSetDaclSecurityDescriptor(
                 SecurityDescriptor,
                 TRUE,
                 Dacl,              // put (PACL) NULL to allow everyone all access
                 FALSE );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbBuildSD: RtlSetDaclSecurityDescriptor, Status = %lx\n", Status));
        }
#endif
        RtlFreeHeap(Os2Heap, 0, Dacl);
        return (Status);
    }

    //
    // These have been copied into the security descriptor, so
    // we can free them.
    //

    RtlFreeSid( AdminAliasSid );

    RtlFreeSid( WorldSid );

    *pDacl = Dacl;

    return(STATUS_SUCCESS);
}


NTSTATUS
Os2SbInitializeRegistryKeys(
    OUT PHANDLE phConfigSysKeyHandle,
    OUT PHANDLE phEnvironmentKeyHandle
    )

/*++

Routine Description:

    This routine creates the OS/2 subsystem key hierarchy in the registry.  It also opens
    the config.sys key, and opens the system environment key.

Arguments:

    phConfigSysKeyHandle - Returns a READ/WRITE handle to the config.sys key in the registry.

    phEnvironmentKeyHandle - Returns a READ/WRITE handle to the system environment key.
        If this key can't be opened due to access denied, NULL is returned and the
        return value will be STATUS_SUCCESS.

    Note --- If the return value is not a success return value, none of the above return
        variables can be considered to be valid.

Return Value:

    The value is an NTSTATUS type that is returned when some failure occurs.  It may
    indicate any of several errors that occur during the APIs called in this function.
    The return value should be tested with NT_SUCCESS().

--*/

{
    OBJECT_ATTRIBUTES Obja;
    UNICODE_STRING Class_U;
    UNICODE_STRING SoftwareDirectory_U;
    UNICODE_STRING ProductDirectory_U;
    UNICODE_STRING VersionDirectory_U;
    UNICODE_STRING Os2IniName_U;
    UNICODE_STRING ConfigSysName_U;
    UNICODE_STRING EnvRegDir_U;
    HANDLE SoftwareKeyHandle;
    HANDLE ProductKeyHandle;
    HANDLE VersionKeyHandle;
    HANDLE Os2IniKeyHandle;
    HANDLE ConfigSysKeyHandle;
    HANDLE EnvironmentKeyHandle;
    ULONG  Disposition;
    NTSTATUS Status;
    SECURITY_DESCRIPTOR localSecurityDescriptor;
    PSECURITY_DESCRIPTOR securityDescriptor;
    PACL Dacl = NULL;


    *phConfigSysKeyHandle = NULL;
    *phEnvironmentKeyHandle = NULL;

    // We start off by creating/opening the registry key hierarchy for our subsystem

    securityDescriptor = &localSecurityDescriptor;

    Status = Os2SbBuildSD(securityDescriptor,
                          &Dacl);

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Os2SbBuildSD failed, Status = %lx\n", Status));
        }
#endif
        return (Status);
    }

    RtlInitUnicodeString(&Class_U, Os2Class);

    RtlInitUnicodeString(&SoftwareDirectory_U, Os2SoftwareDirectory);
    InitializeObjectAttributes(&Obja,
                               &SoftwareDirectory_U,
                               OBJ_CASE_INSENSITIVE,
                               NULL,
                               NULL);

    Status = NtOpenKey(&SoftwareKeyHandle,
                       KEY_CREATE_SUB_KEY,
                       &Obja
                      );
    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Can't open software key, rc = %lx\n", Status));
        }
#endif
        if (Dacl != NULL) {
            RtlFreeHeap(Os2Heap, 0, Dacl);
        }
        return (Status);
    }

    RtlInitUnicodeString(&ProductDirectory_U, Os2ProductDirectory);
    InitializeObjectAttributes(&Obja,
                               &ProductDirectory_U,
                               OBJ_CASE_INSENSITIVE,
                               SoftwareKeyHandle,
                               securityDescriptor);

    Status = NtCreateKey(&ProductKeyHandle,
                         KEY_CREATE_SUB_KEY,
                         &Obja,
                         0,
                         &Class_U,
                         REG_OPTION_NON_VOLATILE,
                         &Disposition
                        );
    NtClose(SoftwareKeyHandle);
    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Can't create product key, rc = %lx\n", Status));
        }
#endif
        if (Dacl != NULL) {
            RtlFreeHeap(Os2Heap, 0, Dacl);
        }
        return (Status);
    }

    RtlInitUnicodeString(&VersionDirectory_U, Os2VersionDirectory);
    InitializeObjectAttributes(&Obja,
                               &VersionDirectory_U,
                               OBJ_CASE_INSENSITIVE,
                               ProductKeyHandle,
                               securityDescriptor);

    Status = NtCreateKey(&VersionKeyHandle,
                         KEY_CREATE_SUB_KEY,
                         &Obja,
                         0,
                         &Class_U,
                         REG_OPTION_NON_VOLATILE,
                         &Disposition
                        );
    NtClose(ProductKeyHandle);
    if (!NT_SUCCESS(Status))
    {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Can't create version key, rc = %lx\n", Status));
        }
#endif
        if (Dacl != NULL) {
            RtlFreeHeap(Os2Heap, 0, Dacl);
        }
        return (Status);
    }

    RtlInitUnicodeString(&Os2IniName_U, Os2IniName);
    InitializeObjectAttributes(&Obja,
                               &Os2IniName_U,
                               OBJ_CASE_INSENSITIVE,
                               VersionKeyHandle,
                               securityDescriptor);

    Status = NtCreateKey(&Os2IniKeyHandle,
                         KEY_CREATE_SUB_KEY,
                         &Obja,
                         0,
                         &Class_U,
                         REG_OPTION_NON_VOLATILE,
                         &Disposition
                        );
    if (!NT_SUCCESS(Status))
    {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Can't create os2ini key, rc = %lx\n", Status));
        }
#endif
        NtClose(VersionKeyHandle);
        if (Dacl != NULL) {
            RtlFreeHeap(Os2Heap, 0, Dacl);
        }
        return (Status);
    }
    NtClose(Os2IniKeyHandle);

    RtlInitUnicodeString(&ConfigSysName_U, Os2ConfigSysName);
    InitializeObjectAttributes(&Obja,
                               &ConfigSysName_U,
                               OBJ_CASE_INSENSITIVE,
                               VersionKeyHandle,
                               securityDescriptor);

    Status = NtCreateKey(&ConfigSysKeyHandle,
                         KEY_READ | KEY_WRITE,
                         &Obja,
                         0,
                         &Class_U,
                         REG_OPTION_NON_VOLATILE,
                         &Disposition
                        );

    if (Dacl != NULL) {
        RtlFreeHeap(Os2Heap, 0, Dacl);
    }
    NtClose(VersionKeyHandle);

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Can't create/open config.sys key, rc = %lx\n",
                      Status));
        }
#endif
        return(Status);
    }

    // Open the environment.

    RtlInitUnicodeString(&EnvRegDir_U, Os2EnvironmentDirectory);
    InitializeObjectAttributes(&Obja,
                               &EnvRegDir_U,
                               OBJ_CASE_INSENSITIVE,
                               NULL,
                               NULL);

    Status = NtOpenKey(&EnvironmentKeyHandle,
                       KEY_READ | KEY_WRITE,
                       &Obja
                      );

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistryKeys: Unable to NtOpenKey() the system environment, rc = %lx\n",
                      Status));
        }
#endif
        if (Status != STATUS_ACCESS_DENIED) {
            NtClose(ConfigSysKeyHandle);
            return(Status);
        }

        //
        // on denied access, return with no error so we can at least create the
        // config.sys value entry
        // The caller will know this occured because the environment handle is null.
        //

    } else {
        *phEnvironmentKeyHandle = EnvironmentKeyHandle;
    }

    *phConfigSysKeyHandle = ConfigSysKeyHandle;
    return (STATUS_SUCCESS);
}


NTSTATUS
Os2SbInitializeRegistry(
    VOID
    )

/*++

Routine Description:

    This function is responsible for initializing the entire Registry component of the
    Subsystem.  It generates the key hierarchy in the registry.

    In the generation of the key hierarchy, it also generates a config.sys entry.
    The information in this entry is taken from the following sources:

      -- some default strings we put in (see Os2ConfigSysDefaultValue).
      -- Information from OS/2's config.sys file is added as follows:

        > SET commands are put in the system environment.
          Some SET commands are ignored (see Os2SetDirectiveProcessingDispatchFunction)
        > LIBPATH commands are merged to the Os2LibPath variable in the
          system environment.  A terminating semicolon is added if there
          is only one path in the path list.
        > SET PATH= is ignored, and the system path remains the same.

Arguments:

    None.

Return Value:

    The value is an NTSTATUS type that is returned when some failure occurs.  It may
    indicate any of several errors that occur during the APIs called in this function.
    The return value should be tested with NT_SUCCESS().  If an unsuccessful value
    is returned, it means the registry component was not properly initialized.

--*/

{
    UNICODE_STRING Os2LibPathValueName_U;
    HANDLE ConfigSysKeyHandle;
    NTSTATUS Status;
    UNICODE_STRING ConfigSysName_U;
    PWCHAR pInfo;
    PUCHAR Src;
    PWCHAR Src1;
    PWCHAR Dest;
    WCHAR ch, ch1;

    // Create the key hierarchy

    Status = Os2SbInitializeRegistryKeys(&ConfigSysKeyHandle,
                                       &Os2EnvironmentKeyHandle);

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("OS2SS - Os2SbInitializeRegistry: failed Os2SbInitializeRegistryKeys, rc = %lx\n", Status));
        }
#endif
        return(Status);
    }

    //
    // This is the 1st time the subsystem is running, create the config.sys entry
    // and migrate information from os/2's config.sys file.
    //

    Os2LibPathFound = FALSE;
    Os2LibPathValueData_U.Buffer = NULL;

    if (Os2EnvironmentKeyHandle == NULL) {          // we have no write access to the environment
        goto Os2NoEnvAccess;                        // skip over the Os2LibPath stuff
    }

    // Get Os2LibPath from sys env so we can update it.

    if (!Or2GetEnvPath(&Os2LibPathValueData_U,
                       Os2Heap,
                       PATHLIST_MAX,
                       Os2EnvironmentKeyHandle,
                       Os2LibPathValueName,
                       FALSE)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistry: Unable to fetch Os2LibPath from sys env\n"));
        }
#endif
    } else {

        // make sure there's at lease one semicolon

        Or2CheckSemicolon(&Os2LibPathValueData_U);

        Os2LibPathFound = TRUE;
    }

    if (Os2LibPathFound && Os2LibPathValueData_U.Length == 0) {     // it's empty (or nonexistent)
                                                                    // set up a default
        UNICODE_STRING tmp;

        RtlInitUnicodeString(&tmp, Os2SystemDirectory);

        RtlCopyUnicodeString(&Os2LibPathValueData_U, &tmp);

        RtlAppendUnicodeToString(&Os2LibPathValueData_U, L"\\os2\\dll;");

        Os2LibPathValueData_U.Buffer[Os2LibPathValueData_U.Length/sizeof(WCHAR)] = UNICODE_NULL;
    }

Os2NoEnvAccess:

    // Now set up the initial regisry config.sys entry.

    // Allocate a buffer to build the config.sys entry in.  (it's a multi-string)

    pInfo = (PWCHAR) RtlAllocateHeap(Os2Heap, 0, MAX_CONSYS_SIZE);
    if (pInfo == NULL)
    {
        if (Os2LibPathValueData_U.Buffer != NULL) {
            RtlFreeHeap(Os2Heap, 0, Os2LibPathValueData_U.Buffer);
            Os2LibPathValueData_U.Buffer = NULL;
            Os2LibPathFound = FALSE;
        }
        if (Os2EnvironmentKeyHandle != NULL) {
            NtClose(Os2EnvironmentKeyHandle);
            Os2EnvironmentKeyHandle = NULL;
        }
        NtClose(ConfigSysKeyHandle);

        return (STATUS_BUFFER_TOO_SMALL);
    }

    // Initially, copy our default value into the entry.

    Src = (PUCHAR) Os2ConfigSysDefaultValue;
    Dest = pInfo;
    do
    {
        ch = (WCHAR) *Src++;
        if (ch == L'\a')
        {
            Src1 = Os2SystemDirectory;
            ch1 = *Src1++;
            while (ch1 != UNICODE_NULL)
            {
                *Dest++ = ch1;
                ch1 = *Src1++;
            }
        }
        else
        {
            *Dest++ = ch;
        }
    } while (!((ch == UNICODE_NULL) && (*Src == '\0')));


    // check if the an OS/2 CONFIG.SYS file already exists on
    // the drive. Its name will be C:\CONFIG.SYS
    // If it exists, process it for additional information
    // The system env will also be updated

    if (Os2InitOriginalConfigSysProcessing()) {

        if (Os2EnvironmentKeyHandle == NULL) {          // we have no write access to the environment

            //
            // We couldn't open a write key to the sys env for some reason, so we
            // won't be able to migrate SET variables to NT from config.sys.
            // Perhaps a popup should be generated to notify the user of this at
            // this point.
            // Anyway, we go on and only write the config.sys entry, skipping
            // variable migration.
            //
#if DBG
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("Os2SbInitializeRegistry: Initializing registry without writing system env\n"));
            }
#endif
        }

        Os2ProcessOriginalConfigSys(&Dest);
        Os2TerminateOriginalConfigSysProcessing();
    } else {

        //
        // add a default "COUNTRY=" line
        //

        RtlMoveMemory(Dest, L"COUNTRY=", 16);
        Dest += 8;
#if 0
        Dest += swprintf(Dest, L"%.3hu", LANGIDFROMLCID(NtCurrentTeb()->CurrentLocale));
#else
        RtlMoveMemory(Dest, L"001", 6);
        Dest += 3;
#endif
        *Dest++ = UNICODE_NULL;

    }

    // Write the new Os2LibPath

    if (Os2LibPathFound) {

        RtlInitUnicodeString(&Os2LibPathValueName_U, Os2LibPathValueName);
        Status = NtSetValueKey(Os2EnvironmentKeyHandle,
                               &Os2LibPathValueName_U,
                               (ULONG)0,
                               REG_EXPAND_SZ,
                               Os2LibPathValueData_U.Buffer,
                               Os2LibPathValueData_U.Length + sizeof(WCHAR)
                              );

#if DBG
        if (!NT_SUCCESS(Status))
        {
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("Os2SbInitializeRegistry: Unable to NtSetValueKey() system environment, rc = %X\n",
                          Status));
            }
        }
#endif

        Os2LibPathFound = FALSE;
        RtlFreeHeap(Os2Heap, 0, Os2LibPathValueData_U.Buffer);
        Os2LibPathValueData_U.Buffer = NULL;
    }

    if (Os2EnvironmentKeyHandle != NULL) {
        NtClose(Os2EnvironmentKeyHandle);
        Os2EnvironmentKeyHandle = NULL;
    }

    // set the REG_MULTI_SZ terminating NUL

    *Dest++ = UNICODE_NULL;

    // Finally, write the config.sys multi-string entry into the registry.

    RtlInitUnicodeString(&ConfigSysName_U, Os2ConfigSysName);
    Status = NtSetValueKey(ConfigSysKeyHandle,
                           &ConfigSysName_U,
                           (ULONG)0,
                           REG_MULTI_SZ,
                           (PVOID)pInfo,
                           (PBYTE)Dest - (PBYTE)pInfo
                          );
    RtlFreeHeap(Os2Heap, 0, pInfo);
    if (!NT_SUCCESS(Status))
    {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("Os2SbInitializeRegistry: Unable to NtSetValueKey() registry config.sys, rc = %X\n",
                      Status));
        }
#endif
        NtClose(ConfigSysKeyHandle);
        return (Status);
    }

    NtClose(ConfigSysKeyHandle);
    return (STATUS_SUCCESS);
}


VOID
Os2SbProbeForInitialSetup(
    VOID
    )
{
    OBJECT_ATTRIBUTES Obja;
    UNICODE_STRING ConfigSysKeyName_U;
    UNICODE_STRING ConfigSysName_U;
    PUNICODE_STRING SysDir_U;
    HANDLE ConfigSysKeyHandle;
    ULONG ResultLength;
    USHORT Counter;
    KEY_VALUE_PARTIAL_INFORMATION ValuePartialInformation;
    NTSTATUS Status;

#if DBG
    Os2Debug |= OS2_DEBUG_INIT;
#endif

    RtlInitUnicodeString(&ConfigSysKeyName_U, Os2ConfigSysKeyName);
    InitializeObjectAttributes(&Obja,
                               &ConfigSysKeyName_U,
                               OBJ_CASE_INSENSITIVE,
                               NULL,
                               NULL);

    Status = NtOpenKey(&ConfigSysKeyHandle,
                       KEY_READ,
                       &Obja
                      );

    if (NT_SUCCESS(Status)) {

        RtlInitUnicodeString(&ConfigSysName_U, Os2ConfigSysName);
        Status = NtQueryValueKey(ConfigSysKeyHandle,
                                 &ConfigSysName_U,
                                 KeyValuePartialInformation,
                                 &ValuePartialInformation,
                                 0,
                                 &ResultLength
                                );

        NtClose(ConfigSysKeyHandle);

        //
        // The 2 expected status results are:
        //
        // STATUS_OBJECT_NAME_NOT_FOUND - config.sys not yet defined
        // STATUS_BUFFER_TOO_SMALL      - config.sys already defined
        //

        if (Status == STATUS_BUFFER_TOO_SMALL) {
            return;
        }

        if (Status != STATUS_OBJECT_NAME_NOT_FOUND) {
#if DBG
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("OS2SS: Can't Read CONFIG.SYS registry value, Status = %lx\n", Status));
            }
#endif
            return;
        }

    } else {
        if (Status != STATUS_OBJECT_PATH_NOT_FOUND &&
            Status != STATUS_OBJECT_NAME_NOT_FOUND &&
            Status != STATUS_OBJECT_PATH_INVALID) {
#if DBG
            IF_OS2_DEBUG( INIT ) {
                KdPrint(("OS2SS: Can't Read CONFIG.SYS registry key, Status = %lx\n", Status));
            }
#endif
            return;
        }
    }


    //
    // We need to install the registry stuff
    //

    //
    // Create a heap to use for dynamic memory allocation.
    //

    Os2Heap = RtlCreateHeap( HEAP_GROWABLE,
                             NULL,
                             0x10000L,  // Initial size of heap is 64K
                             0x1000L,   // Commit an initial page
                             NULL,
                             NULL       // Reserved
                           );
    if (Os2Heap == NULL) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("OS2SS: Error at RtlCreateHeap of Os2Heap\n"));
        }
#endif
        return;
    }

    //
    // Figure out the system directory by cutting it out of our ImagePathName
    // (should be something like \DosDevices\%SystemRoot%\system32\os2ss.exe)
    //

    SysDir_U = (PUNICODE_STRING) &NtCurrentPeb()->ProcessParameters->ImagePathName;
    for (Counter = (SysDir_U->Length / sizeof(WCHAR)) - 1; SysDir_U->Buffer[Counter] != L'\\'; Counter--) {
    }

    Counter -= DOS_DEV_LEN;

    RtlMoveMemory(Os2SystemDirectory,
                  SysDir_U->Buffer + DOS_DEV_LEN,
                  Counter * sizeof(WCHAR));

    Os2SystemDirectory[Counter] = UNICODE_NULL;

#if 0
#if DBG
    IF_OS2_DEBUG( INIT ) {
        KdPrint(("\nOS2SS: System Directory is "));

        for (Counter = 0; Os2SystemDirectory[Counter] != UNICODE_NULL; Counter++) {
            KdPrint(("%c", (CHAR) Os2SystemDirectory[Counter]));
        }

        KdPrint(("|\n"));
    }
#endif
#endif

    Status = Os2SbInitializeRegistry();

    if (!NT_SUCCESS(Status)) {
#if DBG
        IF_OS2_DEBUG( INIT ) {
            KdPrint(("OS2SS: Os2SbInitializeRegistry() failed, Status = %lx\n", Status));
        }
#endif
    }

    RtlDestroyHeap(Os2Heap);
}

#endif      // PMNT