[go: up one dir, main page]

File: fdasd.c

package info (click to toggle)
s390-tools 1.6.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,160 kB
  • ctags: 3,968
  • sloc: ansic: 26,413; asm: 5,072; sh: 4,042; cpp: 1,518; perl: 1,299; makefile: 587
file content (2658 lines) | stat: -rw-r--r-- 66,330 bytes parent folder | download
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
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
/*
 * File...........: s390-tools/fdasd/fdasd.c
 * Author(s)......: Volker Sameske <sameske@de.ibm.com>
 *                  Horst Hummel   <horst.hummel@de.ibm.com>
 *                  Gerhard Tonn   <ton@de.ibm.com>
 * Copyright IBM Corp. 2001,2007
 */

#include <getopt.h>

#include "zt_common.h"
#include "vtoc.h"
#include "fdasd.h"

/* Full tool name */
static const char tool_name[] = "fdasd: zSeries DASD partitioning program";

/* Copyright notice */
static const char copyright_notice[] = "Copyright IBM Corp. 2001, 2006";

/*
 * Print version information.
 */
static void
print_version (void)
{
	printf ("%s version %s.\n", tool_name, RELEASE_STRING);
	printf ("%s.\n", copyright_notice);
}

static int
getpos (fdasd_anchor_t *anc, int dsn)
{
	return anc->partno[dsn];
}

static int
getdsn (fdasd_anchor_t *anc, int pos)
{
	int i;

	for (i=0; i<USABLE_PARTITIONS; i++) {
		if (anc->partno[i] == pos)
			return i;
	}

	return -1;
}

static void
setpos (fdasd_anchor_t *anc, int dsn, int pos)
{
	anc->partno[dsn] = pos;
}


/* 
 * Check for valid volume serial characters (max. 6) - remove invalid.
 * If volser is empty, fill with default volser. 
 */
static void 
fdasd_check_volser(char *volser, int devno)
{
	int from, to;

	for (from=0, to=0; volser[from] && from < VOLSER_LENGTH; from++)
		if ((volser[from] >= 0x23 && 
		     volser[from] <= 0x25) || /* # $ % */
		    (volser[from] >= 0x30 && 
		     volser[from] <= 0x39) || /* 0-9 */
		    (volser[from] >= 0x40 &&
		     volser[from] <= 0x5a) || /* @ A-Z */
		    (volser[from] >= 0x61 &&
		     volser[from] <= 0x7a))   /* a-z */
			volser[to++] = toupper(volser[from]);

	volser[to] = 0x00;
	
	if (volser[0] == 0x00) {
		sprintf(volser, "0X%04x", devno);
	}
}


/*
 * Free memory of fdasd anchor struct.
 */
static void 
fdasd_cleanup (fdasd_anchor_t *anchor) 
{
        partition_info_t *part_info, *next;
        int i;

        if (anchor == NULL) return;

	if (anchor->f4 != NULL) free(anchor->f4);
	if (anchor->f5 != NULL) free(anchor->f5);
	if (anchor->f7 != NULL) free(anchor->f7);
	if (anchor->vlabel != NULL) free(anchor->vlabel);
	
	part_info = anchor->first;
	for (i = 1; i <= USABLE_PARTITIONS && part_info != NULL; i++) {
		next = part_info->next;
		free(part_info);
		part_info = next;
	}
}


/*
 * Exit fdasd.
 */
static void 
fdasd_exit (fdasd_anchor_t *anchor, int rc) 
{
        fdasd_cleanup(anchor);
	exit(rc);
}


/*
 *
 */
static void 
fdasd_error(fdasd_anchor_t *anc, enum fdasd_failure why, char *str)
{
        char err_str[ERROR_STRING_SIZE];

	switch (why) {
	case parser_failed:
		snprintf(err_str, ERROR_STRING_SIZE,
			 "%s parser error\n%s\n", FDASD_ERROR, str);
		break;
        case unable_to_open_disk:
	        snprintf(err_str, ERROR_STRING_SIZE,
			"%s open error\n%s\n", FDASD_ERROR, str);
		break;
        case unable_to_seek_disk:
	        snprintf(err_str, ERROR_STRING_SIZE, 
			"%s seek error\n%s\n", FDASD_ERROR, str);
		break;
        case unable_to_read_disk:
	        snprintf(err_str, ERROR_STRING_SIZE, 
			"%s read error\n%s\n", FDASD_ERROR, str);
		break;
        case read_only_disk:
	        snprintf(err_str, ERROR_STRING_SIZE, 
			"%s write error\n%s\n", FDASD_ERROR, str);
		break;
        case unable_to_ioctl:
	        snprintf(err_str, ERROR_STRING_SIZE,
			"%s IOCTL error\n%s\n", FDASD_ERROR, str);
		break;
        case wrong_disk_type:
                snprintf(err_str, ERROR_STRING_SIZE,
			"%s Unsupported disk type\n%s\n",
                        FDASD_ERROR, str);
                break;           
        case wrong_disk_format:
                snprintf(err_str, ERROR_STRING_SIZE,
			"%s Unsupported disk format\n%s\n",
                        FDASD_ERROR, str);
                break;   
        case disk_in_use:
                snprintf(err_str, ERROR_STRING_SIZE,
			"%s Disk in use\n%s\n", FDASD_ERROR, str);
                break;      
        case config_syntax_error:
                snprintf(err_str, ERROR_STRING_SIZE,
			"%s Config file syntax error\n%s\n",
                        FDASD_ERROR, str);
                break;       
        case vlabel_corrupted:
	        snprintf(err_str, ERROR_STRING_SIZE, 
			"%s Volume label is corrupted.\n%s\n", 
			FDASD_ERROR, str);
		break;
        case dsname_corrupted:
	        snprintf(err_str, ERROR_STRING_SIZE,
			"%s a data set name is corrupted.\n%s\n", 
			FDASD_ERROR, str);
		break;
        case malloc_failed:
	        snprintf(err_str, ERROR_STRING_SIZE, 
			"%s space allocation\n%s\n", FDASD_ERROR, str);
		break;
        case device_verification_failed:
	        snprintf(err_str, ERROR_STRING_SIZE,
			 "%s device verification failed\n%s\n",
			 FDASD_ERROR, str);
		break;
	case volser_not_found:
		snprintf(err_str, ERROR_STRING_SIZE,
			 "%s VOLSER not found on device %s\n",
			 FDASD_ERROR, str);
		break;
	default: 
	        snprintf(err_str, ERROR_STRING_SIZE,
			"%s Fatal error\n%s\n",
			FDASD_ERROR, str);
	}

	fputc('\n', stderr);
	fputs(err_str, stderr);

	fdasd_exit(anc, -1);
}


/*
 * Read line from stdin into global line_buffer
 * and set global line_ptr to first printing character except space.
 */
static int
read_line(void) 
{
	bzero(line_buffer, LINE_LENGTH);
	line_ptr = line_buffer;
        if (!fgets(line_buffer, LINE_LENGTH, stdin))
		return 0;
	while (*line_ptr && !isgraph(*line_ptr))
		line_ptr++;
	return *line_ptr;
}


/*
 * 
 */
static char
read_char(char *mesg) 
{
        fputs(mesg, stdout);
	read_line();

        return *line_ptr;
}


/*
 * Print question string an enforce y/n answer.
 */
static int
yes_no(char *question_str) 
{
	char answer;
	
	while (1) {
		printf("%s (y/n): ", question_str);
		scanf("%c", &answer);
		if (answer == 'y') return 0;
		if (answer == 'n') return 1;
	}
}


/*
 * converts cyl-cyl-head-head-blk to blk
 */
static unsigned long
cchhb2blk (cchhb_t *p) 
{
        return (unsigned long) (p->cc * geo.heads * geo.sectors + 
                                p->hh * geo.sectors +
                                p->b);
}


/*
 *
 */
static char *
fdasd_partition_type (char *str) 
{
	if (strncmp("NATIVE", str, 6) == 0)
		strcpy(str, "Linux native");
	else if (strncmp("NEW   ", str, 6) == 0)
		strcpy(str, "Linux native");
	else if (strncmp("SWAP  ", str, 6) == 0)
		strcpy(str, "Linux swap");
	else
		strcpy(str, "unknown");

	return str;
}


/*
 * prints out the usage text
 */
static void
fdasd_usage (void) 
{
	printf ("\nUsage: fdasd [OPTIONS] [DEVICE]\n"
		"\n"
		"Partition a DASD device either in interactive mode or "
		"automatically.\n"
		"DEVICE is the node of the device (e.g. '/dev/dasda')\n"
		"\n"
		"-h, --help               Print this help, then exit\n"
		"-v, --version            Print version information, "
		                          "then exit\n"
		"-s, --silent             Suppress messages\n"
		"-r, --verbose            Provide more verbose output\n"
		"-a, --auto               Automatically create a partition "
		                          "using the entire disk\n"
		"-k, --keep_volser        Keep current volume serial when "
		                          "performing automatic\n"
		"                         partitioning\n"
		"-l, --label VOLSER       Set the volume serial to VOLSER "
		                          "when performing\n"
		"                         automatic partitioning\n"
		"-c, --config CONFIGFILE  Automatically create partition(s) "
		                          "using information\n"
                "                         found in CONFIGFILE\n"
		"-i, --volser             Print volume serial\n"
		"-p, --table              Print partition table\n");
}


/*
 * prints the menu
 */
static void
fdasd_menu (void) 
{
	printf("Command action\n"
	       "   m   print this menu\n"
	       "   p   print the partition table\n"
	       "   n   add a new partition\n"
	       "   d   delete a partition\n"
	       "   v   change volume serial\n"
	       "   t   change partition type\n"
	       "   r   re-create VTOC and delete all partitions\n"
	       "   u   re-create VTOC re-using existing partition sizes\n"
	       "   s   show mapping (partition number - data set name)\n"
	       "   q   quit without saving changes\n"
	       "   w   write table to disk and exit\n");
}


/*
 * initializes the anchor structure and allocates some
 * memory for the labels
 */
static void
fdasd_initialize_anchor (fdasd_anchor_t *anc) 
{
	partition_info_t *part_info, *prev_part_info;
	volume_label_t *vlabel;
	int i;

	bzero(anc, sizeof(fdasd_anchor_t));

	for (i=0; i<USABLE_PARTITIONS; i++)
		setpos(anc, i, -1);

	anc->f4 = malloc(sizeof(format4_label_t));
	if (anc->f4 == NULL) 
		fdasd_error(anc, malloc_failed,
			    "FMT4 DSCB memory allocation failed.");

	anc->f5 = malloc(sizeof(format5_label_t));
	if (anc->f5 == NULL) 
		fdasd_error(anc, malloc_failed,
			    "FMT5 DSCB memory allocation failed.");

	anc->f7 = malloc(sizeof(format7_label_t));
	if (anc->f7 == NULL) 
		fdasd_error(anc, malloc_failed,
			    "FMT7 DSCB memory allocation failed.");

	bzero(anc->f4, sizeof(format4_label_t));
	bzero(anc->f5, sizeof(format5_label_t));
	bzero(anc->f7, sizeof(format7_label_t));

	vlabel = malloc(sizeof(volume_label_t));
	if (vlabel == NULL) 
		fdasd_error(anc, malloc_failed,
			    "Volume label memory allocation failed.");
	bzero(vlabel, sizeof(volume_label_t));
	anc->vlabel = vlabel;

	for (i=1; i<=USABLE_PARTITIONS; i++) {
	        part_info = malloc(sizeof(partition_info_t));
		if (part_info == NULL) 
			fdasd_error(anc, malloc_failed,
				   "Partition info memory allocation failed.");
		part_info->used       = 0x00;
		part_info->len_trk    = 0;
		part_info->start_trk  = 0;
		part_info->fspace_trk = 0;

		/* add part_info to double pointered list */
		if (i == 1) {
		        anc->first = part_info;
			part_info->prev = NULL;
		} 
		else if (i == USABLE_PARTITIONS) {
		        anc->last = part_info;
		        part_info->next = NULL;
			part_info->prev = prev_part_info;
			prev_part_info->next = part_info;
		} 
		else {
		        part_info->prev = prev_part_info;
		        prev_part_info->next = part_info;
		}

		part_info->f1 = malloc(sizeof(format1_label_t));
		if (part_info->f1 == NULL) 
			fdasd_error(anc, malloc_failed,
				    "FMT1 DSCB memory allocation failed.");
		bzero(part_info->f1, sizeof(format1_label_t));
		
		prev_part_info = part_info;
	}
}


/*
 * parses the command line options
 */
static void
fdasd_parse_options (fdasd_anchor_t *anc, struct fdasd_options *options, 
		     int argc, char *argv[]) 
{
	int opt, index;

	do {
		opt = getopt_long(argc, argv, option_string, 
				  fdasd_long_options, &index);
		switch (opt) {
		case 'v':
			print_version();
			fdasd_exit(anc, 0);
		case 'h':
			fdasd_usage ();
			fdasd_exit(anc, 0);
		case 'l':
			if (options->volser)
				fdasd_error(anc, parser_failed,
					    "Option 'label' specified more "
					    "than once.\n");
			options->volser = optarg;
			break;
		case 'a':
			anc->auto_partition++;
			break;
		case 's':
			anc->silent++;
			break;
		case 'r':
			anc->verbose++;
			break;
		case 'p':
			anc->print_table++;
			break;
		case 'i':
			anc->print_volser++;
			anc->silent++;
			break;
		case 'c':
			if (options->conffile)
				fdasd_error(anc, parser_failed,
					    "Option 'config' specified more"
					    " than once.\n");
			options->conffile = optarg;
			break;
		case 'k':
			anc->keep_volser++;
			break;
		case -1:
			/* End of options string - start of devices list */
			break;
		default:
			fprintf(stderr, "Try 'fdasd --help' for more"
					" information.\n");
			fdasd_exit(anc, 1);
		}
	} while (opt != -1);

	/* save device */
	if (optind >= argc)
		fdasd_error(anc, parser_failed, 
			    "No device speficied.\n");
	if (optind + 1 < argc)
		fdasd_error(anc, parser_failed, 
			    "More than one device speficied.\n");
	options->device = argv[optind]; 	
}


/*
 * parses config file
 */
static int
fdasd_parse_conffile(fdasd_anchor_t *anc, struct fdasd_options *options) 
{
	char buffer[USABLE_PARTITIONS * LINE_LENGTH];
	char err_str[ERROR_STRING_SIZE], *c1, *c2;
	int fd, i=0;

	/* if name of config file was not specified, select the default */
	if (options->conffile == NULL)
		options->conffile = DEFAULT_FDASD_CONF;

	if (!anc->silent)
		printf("parsing config file '%s'...\n", options->conffile);
	fd = open(options->conffile, O_RDONLY);
	if (fd < 0) {
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not open config file '%s' "
			"in read-only mode!\n", options->conffile);
		fdasd_error(anc, unable_to_open_disk, err_str);
	}

	bzero(buffer, sizeof(buffer));
	read(fd, buffer, sizeof(buffer));
	close(fd);


	for (i=0; i<sizeof(buffer); i++) 
		buffer[i] = toupper(buffer[i]);

	c1 = buffer;

	for (i=0; i<USABLE_PARTITIONS; i++) {
		char *token, *stopstring;

		c1 = strchr(c1, '[');
		if (c1 == NULL) {
			if (!anc->silent)
				printf("no config file entry for " \
				       "partition %d found...\n", i+1);
			break;
		}
		c1 += 1;

		c2 = strchr(c1, ']');
		if (c2 == NULL) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"']' missing in config file " \
				"%s\n", options->conffile);
			fdasd_error(anc, config_syntax_error, err_str);
		}
		strcpy(c2, "");

		token = strtok(c1, ",");
		if (strstr(token, "FIRST") != NULL)
			anc->confdata[i].start=FIRST_USABLE_TRK;
		else
			anc->confdata[i].start=strtol(token, &stopstring, 10);

		token = strtok(NULL, ",");
		if (strstr(token, "LAST") != NULL)
			anc->confdata[i].stop = geo.cylinders * geo.heads - 1;
		else
			anc->confdata[i].stop = strtol(token, &stopstring, 10);
		c1 = c2 + 1;
	}

	return 0;
}


/*
 * checks input from config file
 */
static void
fdasd_check_conffile_input (fdasd_anchor_t *anc,
			    struct fdasd_options *options)
{
	partition_info_t *part_info = anc->first;
	int i;

	if (anc->verbose) printf("checking config file data...\n");
	for (i=0; i<USABLE_PARTITIONS; i++) {
		unsigned long start, stop, first_trk, last_trk;
		char err_str[ERROR_STRING_SIZE];
	   
		start = anc->confdata[i].start;
		stop = anc->confdata[i].stop;

		if ((start == 0) || (stop == 0)) break;

		first_trk = FIRST_USABLE_TRK;
		last_trk = geo.cylinders * geo.heads - 1;

		if ((start < first_trk) || (start > last_trk)) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"One of the lower partition limits "
				"(%ld) is not within the range of \n "
				"available tracks on disk (%ld-%ld)!\n", 
				start, first_trk, last_trk);
			fdasd_error(anc, config_syntax_error, err_str);
		}

		if ((stop < first_trk) || (stop > last_trk)) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"One of the upper partition limits "
				"(%ld) is not within the range of \n "
				"available tracks on disk (%ld-%ld)!\n", 
				stop, first_trk, last_trk);
			fdasd_error(anc, config_syntax_error, err_str);
		}

		if (start >= stop) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"Lower partition limit (%ld) is not "
				"less than upper partition \nlimit (%ld) "
				"in config file %s!\n", 
				 start, stop, options->conffile);
			fdasd_error(anc, config_syntax_error, err_str);
		}

		if ((i > 0) && (start <= anc->confdata[i-1].stop)) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"Partitions overlap or are not in "
				"ascending order!\n");
			fdasd_error(anc, config_syntax_error, err_str);
		}

		if ((i < (USABLE_PARTITIONS - 1)) && 
		    (anc->confdata[i+1].start > 0) && 
		    (stop >= anc->confdata[i+1].start)) {
			snprintf(err_str, ERROR_STRING_SIZE,
				"Partitions overlap or are not in "
				"ascending order!\n");
			fdasd_error(anc, config_syntax_error, err_str);
		}

		part_info->used      = 0x01;
		part_info->start_trk = start;
		part_info->end_trk   = stop;
		part_info->len_trk   = stop - start + 1;

		/* update the current free space counter */
		if (i == 0)
			anc->fspace_trk = start - FIRST_USABLE_TRK;
		
		if (i < USABLE_PARTITIONS - 1) {
			if (anc->confdata[i+1].start != 0)
				part_info->fspace_trk = 
					anc->confdata[i+1].start-stop-1; 
			else
				part_info->fspace_trk = last_trk - stop; 
		}
		else if (i == USABLE_PARTITIONS - 1)
			part_info->fspace_trk = last_trk - stop; 

		part_info = part_info->next;
	}
	return;
}


/*
 * Verifies the specified block device.
 */
static void
fdasd_verify_device (fdasd_anchor_t *anc, char *name) 
{
	struct stat dst;
	char err_str[ERROR_STRING_SIZE];

	if ((stat(name, &dst)) < 0 ) {
		snprintf(err_str, ERROR_STRING_SIZE,
			 "Unable to get device status for device '%s'\n",
			 name);
		fdasd_error(anc, device_verification_failed, err_str);
	}

	if (!(S_ISBLK (dst.st_mode))) {
		snprintf(err_str, ERROR_STRING_SIZE,
			 "Device '%s' (%d/%d) is not a block device\n", name,
			 (unsigned short) major(dst.st_rdev),
			 (unsigned short) minor(dst.st_rdev));
		fdasd_error(anc, device_verification_failed, err_str);
	}

	if (minor (dst.st_rdev) & PARTN_MASK) {
		snprintf(err_str, ERROR_STRING_SIZE,
			 "Partition '%s' (%d/%d) detected where device is "
			 "required\n", name,
			 (unsigned short) major(dst.st_rdev),
			 (unsigned short) minor(dst.st_rdev));

		fdasd_error(anc, device_verification_failed, err_str);
	}

	if (anc->verbose)
		printf("Verification successful for '%s' (%d/%d)\n", name,
		       (unsigned short) major(dst.st_rdev),
		       (unsigned short) minor(dst.st_rdev));
}


/*
 * Verifies the specified fdasd command line option
 * combinations.
 *
 * Note: 
 *  - 'version' and 'help' are priority options. 
 *       All other paramters are ignored in that case.
 *  - 'silent' and 'verbose' are allways allowed in any
 *       combination.
 * 
 */
static void 
fdasd_verify_options (fdasd_anchor_t *anc) 
{
	/* Checked option combinations                       */
	/* (inv = invalid / req = required / opt = optional) */
	/*                                                   */
	/*              vols labe keep auto conf tabl        */
	/*              er   l    _vol      if   e           */
	/*                        ser                        */
	/*                                                   */
	/* volser       -    inv  INV  inv  inv  inv         */
	/* label             -    inv  REQ  INV  inv         */
	/* keep_volser            -    REQ  INV  inv         */
	/* auto                        -    inv  inv         */
	/* config                           -    inv         */
	/* table                                 -           */

	if (anc->print_volser && 
	    (options.volser || anc->keep_volser || anc->auto_partition ||
	     options.conffile || anc->print_table)) {
		fdasd_error(anc, parser_failed, 
			    "Option 'volser' cannot be used with other"
			    " options.\n");
	}

	if (options.volser) {
		if (!anc->auto_partition) {
			fdasd_error(anc, parser_failed, 
				    "Option 'auto' required when specifying"
				    " 'label'\n");
		}
		if ((anc->keep_volser || options.conffile || 
		     anc->print_table)) {
			fdasd_error(anc, parser_failed, 
				    "Option 'label' cannot be used with "
				    "'keep_volser', 'config' and 'table'.\n");
		}
	}

	if (anc->keep_volser) {
		if (!anc->auto_partition) {
			fdasd_error(anc, parser_failed, 
				    "Option 'auto' required when specifying"
				    " 'keep_volser'\n");
		}
		if (options.conffile || anc->print_table) {
			fdasd_error(anc, parser_failed, 
				    "Option 'keep_volser' cannot be used"
				    " with 'config' and 'table'.\n");
		}
	}
	if (anc->auto_partition && 
	    (options.conffile || anc->print_table)) {
		fdasd_error(anc, parser_failed, 
			    "Option 'auto' cannot be used with "
			    "'config' and 'table'.\n");
	}
	
	if (options.conffile  && 
	    (anc->print_table)) {
		fdasd_error(anc, parser_failed, 
			    "Option 'config' cannot be used with"
			    " 'table'.\n");
	}
}


/*
 * print mapping: partition number - data set name
 */
static void
fdasd_show_mapping (fdasd_anchor_t *anc) 
{
	char str[20], dev[30], dsname[45], *strp;
        partition_info_t *part_info;
	int i=0, j=0;

        printf("\ndevice .........: %s\n",options.device);
	bzero(str, sizeof(str));
	vtoc_volume_label_get_label(anc->vlabel, str);
        printf("volume label ...: %.4s\n", str);
	bzero(str, sizeof(str));
	vtoc_volume_label_get_volser(anc->vlabel, str);
        printf("volume serial ..: %s\n\n", str);

        strcpy(dev, options.device);
        if (((strp = strstr(dev,DISC)) != NULL) ||
	    ((strp = strstr(dev,DEVICE)) != NULL))
                strcpy(strp, PART);

	printf("WARNING: This mapping may be NOT up-to-date,\n"
	       "         if you have NOT saved your last changes!\n\n");

	for (part_info = anc->first ; part_info != NULL;
	     part_info = part_info->next) {
                i++;
                if (part_info->used != 0x01)
			continue;

		bzero(dsname, sizeof(dsname));
		strncpy(dsname, part_info->f1->DS1DSNAM, 44);
		vtoc_ebcdic_dec(dsname, dsname, 44);
	
		if (getdsn(anc, i-1) < 0)
			sprintf(dsname, "new data set");

		printf("%s%-2d -  %-44s\n", dev, i, dsname);
		j++;
        }

	if (j == 0) printf("No partitions defined.\n");
}


/*
 * prints only the volume serial
 */
static void 
fdasd_print_volser (fdasd_anchor_t *anc)
{
	char volser[VOLSER_LENGTH + 1];

	bzero(volser, VOLSER_LENGTH);
	vtoc_ebcdic_dec(anc->vlabel->volid, volser, VOLSER_LENGTH);
	printf("%6.6s\n", volser);
}


/*
 * print partition table
 */
static void
fdasd_list_partition_table (fdasd_anchor_t *anc) 
{
        partition_info_t *part_info;
        char str[20], dev[30], *strp, *ch;
        int i=0, dev_len = strlen(options.device);

	if (!anc->silent) {
		printf("\nDisk %s: \n"
		       "  cylinders ............: %d\n"
		       "  tracks per cylinder ..: %d\n"
		       "  blocks per track .....: %d\n"
		       "  bytes per block ......: %d\n",
		       options.device, geo.cylinders, geo.heads, 
		       geo.sectors, anc->blksize);

		vtoc_volume_label_get_label(anc->vlabel, str);
		printf("  volume label .........: %s\n", str);

		vtoc_volume_label_get_volser(anc->vlabel, str);

		printf("  volume serial ........: %s\n", str);
		printf("  max partitions .......: %d\n\n", USABLE_PARTITIONS);
	}

        if (dev_len < 20)
                dev_len = 20;

	if (!anc->silent) {
		printf(" ------------------------------- tracks"
		       " -------------------------------\n");
		printf("%*s      start      end   length   Id  System\n",
		       dev_len + 1, "Device");
	}

        strcpy(dev, options.device);
        if (((strp = strstr(dev,DISC)) != NULL) ||
	    ((strp = strstr(dev,DEVICE)) != NULL))
                strcpy(strp, PART);

        for (part_info = anc->first; part_info != NULL;
	     part_info = part_info->next) {
		i++;

                if ((part_info == anc->first) && (anc->fspace_trk > 0)) 
                        printf("%*s   %9ld%9ld%9ld       unused\n",dev_len,"",
                                (unsigned long) FIRST_USABLE_TRK,
                                (unsigned long) FIRST_USABLE_TRK +
                                anc->fspace_trk - 1,
                                anc->fspace_trk);

                if (part_info->used != 0x01)
			continue;

		vtoc_ebcdic_dec(part_info->f1->DS1DSNAM,
				part_info->f1->DS1DSNAM, 44);
		ch = strstr(part_info->f1->DS1DSNAM, "PART");
		if (ch != NULL) {
			strncpy(str, ch + 9, 6);
			str[6] = '\0';
		} else
			strcpy(str, "error");

		vtoc_ebcdic_enc(part_info->f1->DS1DSNAM,
				part_info->f1->DS1DSNAM, 44);

		printf("%*s%-2d %9ld%9ld%9ld   %2x  %6s\n", 
		       dev_len, dev, i, part_info->start_trk,
		       part_info->end_trk, part_info->len_trk, i,
		       fdasd_partition_type(str));

		if (part_info->fspace_trk > 0) 
			printf("%*s   %9ld%9ld%9ld       unused\n",
			       dev_len , "" , part_info->end_trk + 1,
			       part_info->end_trk + part_info->fspace_trk,
			       part_info->fspace_trk);
        }
}

/*
 * get volser from vtoc
 */
static int
fdasd_get_volser(fdasd_anchor_t *anc, char *devname, char *volser)
{
	dasd_information_t dasd_info;
	int blksize;
	int fd;
	volume_label_t vlabel;
	char err_str[ERROR_STRING_SIZE];

	if ((fd = open(devname, O_RDONLY)) == -1) {
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not open device '%s' "
			"in read-only mode!\n", devname);
		fdasd_error(anc, unable_to_open_disk, err_str);
	}     

	if (ioctl(fd, BIODASDINFO, &dasd_info) != 0) {
		close(fd);
		fdasd_error(anc, unable_to_ioctl, 
			    "Could not retrieve disk information.");
	}

	if (ioctl(fd, BLKSSZGET, &blksize) != 0) {
		close(fd);
	        fdasd_error(anc, unable_to_ioctl,
			    "Could not retrieve blocksize information.");
	}

	close(fd);

	if ((strncmp(dasd_info.type, "ECKD", 4) == 0) &&
	    (!dasd_info.FBA_layout)) {
		/* OS/390 and zOS compatible disk layout */
		vtoc_read_volume_label(options.device,
				       dasd_info.label_block * blksize,
				       &vlabel);
		vtoc_volume_label_get_volser(&vlabel, volser);
		return 0;
        }
	else {
		return -1;
	}
}
	

/*
 * call IOCTL to re-read the partition table
 */
static void
fdasd_reread_partition_table (fdasd_anchor_t *anc)
{
	char err_str[ERROR_STRING_SIZE];
	int fd;

	if (!anc->silent) printf("rereading partition table...\n");

	if ((fd = open(options.device, O_RDONLY)) < 0) {
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not open device '%s' "
			"in read-only mode!\n", options.device);
		fdasd_error(anc, unable_to_open_disk, err_str);
	} 

	if (ioctl(fd, BLKRRPART, NULL) != 0) {
		close(fd);
		fdasd_error(anc, unable_to_ioctl, "Error while rereading "
			    "partition table.\nPlease reboot!");
	}
	close(fd);
} 


/*
 * writes all changes to dasd
 */
static void
fdasd_write_vtoc_labels (fdasd_anchor_t *anc) 
{
        partition_info_t *part_info;
	unsigned long blk;
	char dsno[6], volser[VOLSER_LENGTH + 1], s2[45], *c1, *c2, *ch;
	int i=0, k=0;

	if (!anc->silent) printf("writing VTOC...\n");
	if (anc->verbose) printf("DSCBs: ");

	blk = (cchhb2blk(&anc->vlabel->vtoc) - 1) * anc->blksize;
	if (blk <= 0) 
		fdasd_error(anc, vlabel_corrupted, "");

	/* write FMT4 DSCB */
	vtoc_write_label(options.device, blk, NULL, anc->f4, NULL, NULL);
	if (anc->verbose) printf("f4 ");

	/* write FMT5 DSCB */
	blk += anc->blksize;
	vtoc_write_label(options.device, blk, NULL, NULL, anc->f5, NULL);
	if (anc->verbose) printf("f5 ");

	/* write FMT7 DSCB */
	if (anc->big_disk) {
		blk += anc->blksize;
		vtoc_write_label(options.device, blk,  NULL, NULL,
				 NULL,anc->f7);
		if (anc->verbose) printf("f7 ");
	}

	/* loop over all FMT1 DSCBs */
	for (part_info = anc->first; part_info != NULL;
	     part_info = part_info->next) {
		blk += anc->blksize; 
		i++;

		if (part_info->used != 0x01) {
			vtoc_write_label(options.device, blk, 
					 part_info->f1, NULL, NULL, NULL);
			continue;
		}

		strncpy(part_info->f1->DS1DSSN,	anc->vlabel->volid,
			VOLSER_LENGTH);

		ch = part_info->f1->DS1DSNAM;
		vtoc_ebcdic_dec(ch, ch, 44);
		c1 = ch + 7;

		if (getdsn(anc, i-1) > -1) {
			/* re-use the existing data set name */
			c2 = strchr(c1, '.');
			if (c2 != NULL)
				strncpy(s2, c2, 31);
			else
				fdasd_error(anc, dsname_corrupted, "");

			strncpy(volser, anc->vlabel->volid, VOLSER_LENGTH);
			vtoc_ebcdic_dec(volser, volser, VOLSER_LENGTH);
			volser[VOLSER_LENGTH] = ' ';
			strncpy(c1, volser, VOLSER_LENGTH + 1);
			c1 = strchr(ch, ' ');
			strncpy(c1, s2, 31);
		}
		else {
			char *tmp = strstr(ch, "SWAP");

			/* create a new data set name */
			while (getpos(anc, k) > -1)
				k++;

			setpos(anc, k, i-1);
			
			strncpy(ch, "LINUX.V               "
				"                      ", 44);

			strncpy(volser, anc->vlabel->volid, VOLSER_LENGTH);
			vtoc_ebcdic_dec(volser, volser, VOLSER_LENGTH);
			strncpy(c1, volser, VOLSER_LENGTH);
				
			c1 = strchr(ch, ' ');
			strncpy(c1, ".PART", 5);
			c1 += 5;

			sprintf(dsno,"%04d", k+1);
			strncpy(c1, dsno, 4);

			c1 += 4;
			if (tmp)
				strncpy(c1, ".SWAP", 5);
			else
				strncpy(c1, ".NATIVE", 7);
		}
		vtoc_ebcdic_enc(ch, ch, 44);
		if (anc->verbose) printf("f1 ");

		vtoc_write_label(options.device, blk, part_info->f1, NULL,
				 NULL, NULL);
	}
	if (anc->verbose) printf("\n");
}


/*
 * writes all changes to dasd
 */
static void
fdasd_write_labels (fdasd_anchor_t *anc) 
{
        if (anc->vlabel_changed) {
		if (!anc->silent) printf("writing volume label...\n");
	        vtoc_write_volume_label(options.device, anc->label_pos,
					anc->vlabel);
	}

	if (anc->vtoc_changed) 
		fdasd_write_vtoc_labels(anc);

        if ((anc->vtoc_changed)||(anc->vlabel_changed)) 
		fdasd_reread_partition_table(anc);
}


/*
 * re-creates the VTOC and deletes all partitions
 */
static void
fdasd_recreate_vtoc(fdasd_anchor_t *anc)
{
	partition_info_t *part_info = anc->first;
	char str[INPUT_BUF_SIZE];
	int i;

	if (!anc->silent) {
		snprintf(str, INPUT_BUF_SIZE, 
			 "WARNING: All partitions on device '%s' will be "
			 "deleted!\nDo you want to continue?",
			 options.device);

		if (yes_no(str) != 0)
			return;

		printf("creating new VTOC... ");
	}

	vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS,
				geo.cylinders, geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);
	vtoc_set_freespace(anc->f4,anc->f5, anc->f7, '+', anc->verbose,
			   FIRST_USABLE_TRK, geo.cylinders * geo.heads - 1,
			   geo.cylinders, geo.heads);

	while (part_info != NULL) {

		bzero(part_info->f1, sizeof(format1_label_t));

		if (part_info->used == 0x01) {
			part_info->used       = 0x00;
			part_info->start_trk  = 0;
			part_info->end_trk    = 0;
			part_info->len_trk    = 0;
			part_info->fspace_trk = 0;
		}

		part_info = part_info->next;
	}

	anc->used_partitions = 0;
	anc->fspace_trk = geo.cylinders * geo.heads - FIRST_USABLE_TRK;

	for (i=0; i<USABLE_PARTITIONS; i++)
		setpos(anc, i, -1);

	anc->vtoc_changed++;
	if (!anc->silent) printf("ok\n");
}


/*
 * re-create all VTOC labels, but use the partition information
 * from existing VTOC
 */
static void
fdasd_reuse_vtoc(fdasd_anchor_t *anc)
{
	partition_info_t *part_info = anc->first;
	format1_label_t f1;
	format4_label_t f4;
	format5_label_t f5;
	format7_label_t f7;
	char str[INPUT_BUF_SIZE];

	if (!anc->silent) {
		snprintf(str, INPUT_BUF_SIZE, 
			"WARNING: this will re-create your VTOC "
			"entries using the partition\n           "
			"information of your existing VTOC. Continue?");

		if (yes_no(str) != 0)
			return;
	}

	if (!anc->silent) printf("re-creating VTOC... ");

	vtoc_init_format4_label(&f4, USABLE_PARTITIONS,
				geo.cylinders, geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	/* reuse some FMT4 values */
	f4.DS4HPCHR = anc->f4->DS4HPCHR;
	f4.DS4DSREC = anc->f4->DS4DSREC;

	/* re-initialize both free-space labels */
	vtoc_init_format5_label(&f5);
	vtoc_init_format7_label(&f7);

	if (anc->fspace_trk > 0)
		vtoc_set_freespace(&f4, &f5, &f7, '+', anc->verbose,
				   FIRST_USABLE_TRK, 
				   FIRST_USABLE_TRK + anc->fspace_trk - 1,
				   geo.cylinders, geo.heads);

	while (part_info != NULL) {
		if (part_info->used != 0x01) {
			part_info = part_info->next;
			continue;
		}

		vtoc_init_format1_label(anc->vlabel->volid, anc->blksize, 
					&part_info->f1->DS1EXT1, &f1);

		strncpy(f1.DS1DSNAM, part_info->f1->DS1DSNAM, 44);
		strncpy(f1.DS1DSSN,  part_info->f1->DS1DSSN, 6);
		f1.DS1CREDT = part_info->f1->DS1CREDT;

		memcpy(part_info->f1, &f1, sizeof(format1_label_t));

		if (part_info->fspace_trk > 0)
			vtoc_set_freespace(&f4, &f5, &f7, '+', anc->verbose,
					   part_info->end_trk + 1, 
					   part_info->end_trk +
					   part_info->fspace_trk,
					   geo.cylinders, geo.heads);

		part_info = part_info->next;
	}

	/* over-write old labels with new ones */
	memcpy(anc->f4, &f4, sizeof(format4_label_t));
	memcpy(anc->f5, &f5, sizeof(format5_label_t));
	memcpy(anc->f7, &f7, sizeof(format7_label_t));

	if (!anc->silent) printf("ok\n");
	anc->vtoc_changed++;

	return;
}


/*
 * Changes the volume serial (menu option)
 */
static void
fdasd_change_volser (fdasd_anchor_t *anc) 
{
	char volser[VOLSER_LENGTH + 1];

	vtoc_volume_label_get_volser(anc->vlabel, volser);
	printf("Please specify new volume serial (6 characters).\n");
	printf("current     : %-6.6s\nnew [0X%04x]: ", volser, anc->devno);

	read_line();
	fdasd_check_volser(line_ptr, anc->devno);

	printf("\nvolume identifier changed to '%-6s'\n",line_ptr);
	vtoc_volume_label_set_volser(anc->vlabel, line_ptr);

	vtoc_set_cchhb(&anc->vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);
	anc->vlabel_changed++;
	anc->vtoc_changed++;
}


/*
 * changes the partition type
 */
static void
fdasd_change_part_type (fdasd_anchor_t *anc)
{
        int i;
        unsigned int part_id, part_type;
	char str[20], *ch;
        partition_info_t *part_info;

	fdasd_list_partition_table(anc);

	/* ask for partition number */
	printf("\nchange partition type\n");
        while (!isdigit(part_id = read_char("partition id (use 0 to exit): ")))
		printf("Invalid partition id '%c' detected.\n", part_id);

        part_id -= 48;
	printf("\n");
        if (part_id == 0) 
		return;
        if ((part_id < 0) || (part_id > anc->used_partitions)) {
                printf("'%d' is not a valid partition id!\n", part_id);
                return;
        }

	part_info = anc->first;
        for (i=1; i < part_id; i++) 
		part_info = part_info->next;

	/* ask for partition type */
	vtoc_ebcdic_dec(part_info->f1->DS1DSNAM, part_info->f1->DS1DSNAM, 44);
	ch = strstr(part_info->f1->DS1DSNAM, "PART") + 9;
	if (ch != NULL) {
		strncpy(str, ch, 6);
		str[6] = '\0';
	} else
		strcpy(str, "error");

	printf("current partition type is: %s\n\n", fdasd_partition_type(str));
	printf("   1  Linux native\n" \
	       "   2  Linux swap\n\n");
	part_type = 0;
	while ((part_type < 1) || (part_type > 2)) {
        	while (!isdigit(part_type = 
				read_char("new partition type: ")));
        	part_type -= 48;
	}

        switch (part_type) {
        case 1:
		strncpy(str, "NATIVE", 6);
                break;
        case 2:
		strncpy(str, "SWAP  ", 6);
                break;
        default:
                printf("'%d' is not supported!\n", part_type);
        }

	ch = strstr(part_info->f1->DS1DSNAM, "PART") + 9;
	if (ch != NULL)	
		strncpy(ch, str, 6);
	vtoc_ebcdic_enc(part_info->f1->DS1DSNAM, part_info->f1->DS1DSNAM, 44);
        anc->vtoc_changed++;
}



/*
 * initialize the VOL1 volume label
 */
static void
fdasd_init_volume_label (fdasd_anchor_t *anc) 
{
	volume_label_t *vlabel = anc->vlabel;
	char old_volser[VOLSER_LENGTH + 1];

	vtoc_volume_label_init(vlabel);
	vtoc_volume_label_set_key(vlabel, "VOL1");
	vtoc_volume_label_set_label(vlabel, "VOL1");

	if (anc->keep_volser) {
		if(fdasd_get_volser(anc, options.device, old_volser) == 0)
			vtoc_volume_label_set_volser(vlabel, old_volser);
		else {
			fdasd_error(anc, volser_not_found, options.device);
		}
	}
	else {
		if (options.volser == NULL) {
	        	printf("\nPlease specify volume serial (6 characters)"
			       "[0X%04x]: ", 
			       anc->devno);
			read_line();
			fdasd_check_volser(line_ptr, anc->devno);
			vtoc_volume_label_set_volser(vlabel, line_ptr);
		} 
		else {
			fdasd_check_volser(options.volser, anc->devno);
			vtoc_volume_label_set_volser(vlabel, options.volser);
		}

	}

	vtoc_set_cchhb(&vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);
	anc->vlabel_changed++;
}


/*
 * sets some important partition data
 * (like used, start_trk, end_trk, len_trk)
 * by calculating these values with the
 * information provided in the labels
 */
static void
fdasd_update_partition_info (fdasd_anchor_t *anc) 
{
        partition_info_t *prev_part_info = NULL, *part_info = anc->first;
	unsigned int head = geo.heads;
	unsigned long max = geo.cylinders * head - 1;
        int i;

	anc->used_partitions = geo.sectors - 2 - anc->f4->DS4DSREC;

        for (i=1; i<=USABLE_PARTITIONS; i++) {
	        if (part_info->f1->DS1FMTID != 0xf1) {
		        if (i == 1)
				/* there is no partition at all */
				anc->fspace_trk = max - FIRST_USABLE_TRK + 1;
			else
			        /* previous partition was the last one */
			        prev_part_info->fspace_trk = 
					max - prev_part_info->end_trk;
			break;
		}
		
		/* this is a valid format 1 label */
		part_info->used = 0x01;
		part_info->start_trk = part_info->f1->DS1EXT1.llimit.cc * 
			head + part_info->f1->DS1EXT1.llimit.hh;
		part_info->end_trk = part_info->f1->DS1EXT1.ulimit.cc * head +
			part_info->f1->DS1EXT1.ulimit.hh;
		part_info->len_trk = part_info->end_trk -
			part_info->start_trk + 1;
		
		if (i == 1) 
		        /* first partition, there is at least one */
			anc->fspace_trk = 
				part_info->start_trk - FIRST_USABLE_TRK;
		else {
		        if (i == USABLE_PARTITIONS) 
			        /* last possible partition */
			        part_info->fspace_trk = 
					max - part_info->end_trk;

			/* set free space values of previous partition */
		        prev_part_info->fspace_trk = part_info->start_trk - 
				prev_part_info->end_trk - 1;
		}

	        prev_part_info = part_info;
	        part_info = part_info->next;
	}
}

/*
 * reorganizes all FMT1s, move all empty labels to the end 
 */
static void
fdasd_reorganize_FMT1s (fdasd_anchor_t *anc) 
{
	int i, j;
	format1_label_t *f1_label;
	partition_info_t *part_info;

	for (i=1; i<=USABLE_PARTITIONS - 1; i++) {
		part_info = anc->first;
		for (j=1; j<=USABLE_PARTITIONS - i; j++) {
			if (part_info->f1->DS1FMTID < 
			    part_info->next->f1->DS1FMTID) {
				f1_label = part_info->f1;
				part_info->f1 = part_info->next->f1;
				part_info->next->f1 = f1_label;
			}
			part_info = part_info->next;
		}
	}
}


/*
 * we have a invalid FMT4 DSCB and therefore we will re-create the VTOC
 */
static void
fdasd_process_invalid_vtoc(fdasd_anchor_t *anc)
{
	printf(" invalid\ncreating new VTOC...\n");
	vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS,
				geo.cylinders, geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);
	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', anc->verbose,
			   FIRST_USABLE_TRK, geo.cylinders * geo.heads - 1,
			   geo.cylinders, geo.heads);

	anc->vtoc_changed++;
}


/*
 *
 */
static void
fdasd_process_valid_vtoc(fdasd_anchor_t *anc, unsigned long blk)
{
	int f1_counter = 0, f7_counter = 0, f5_counter = 0;
	int i, part_no, f1_size = sizeof(format1_label_t);
	partition_info_t *part_info = anc->first;
	format1_label_t f1_label;
	char part_no_str[5], *part_pos;

	if (!anc->silent) printf(" ok\n");
	blk += anc->blksize;

	if (anc->verbose) printf("VTOC DSCBs          : ");

	for (i = 1; i <= geo.sectors; i++) {
		bzero(&f1_label, f1_size);
		vtoc_read_label(options.device, blk, &f1_label, NULL, NULL,
				NULL);

		switch (f1_label.DS1FMTID) {
		case 0xf1:
			if (anc->verbose) printf("f1 ");
			if(part_info == NULL) break;
			memcpy(part_info->f1, &f1_label, f1_size);

			part_no = -1;
			vtoc_ebcdic_dec(part_info->f1->DS1DSNAM,
					part_info->f1->DS1DSNAM, 44);
			part_pos = strstr(part_info->f1->DS1DSNAM, "PART");
			if (part_pos != NULL) {
				strncpy(part_no_str, part_pos + 4, 4);
				part_no_str[4] = '\0';
				part_no = atoi(part_no_str) - 1;
			}

			vtoc_ebcdic_enc(part_info->f1->DS1DSNAM,
					part_info->f1->DS1DSNAM, 44);

			if ((part_no < 0) || (part_no >= USABLE_PARTITIONS))
				printf("WARNING: partition number (%i) found "
				       "in data set name of an existing "
				       "partition\ndoes not match range of "
				       "possible partition numbers (1-%d)\n\n",
				       part_no + 1, USABLE_PARTITIONS);
			else
				setpos(anc, part_no, f1_counter);

			part_info = part_info->next;
			f1_counter++;
			break;
		case 0xf5:
			if (anc->verbose) printf("f5 ");
			memcpy(anc->f5, &f1_label, f1_size);
			f5_counter++;
			break;
		case 0xf7:
			if (anc->verbose) printf("f7 ");
			if (f7_counter == 0)
				memcpy(anc->f7, &f1_label, f1_size);
			f7_counter++;
			break;
		default: 
			if (f1_label.DS1FMTID > 0)
				printf("'%d' is not supported!\n", 
				       f1_label.DS1FMTID);
		}
		blk += anc->blksize;
	}
		
	if (anc->verbose) printf("\n");

	if ((f5_counter == 0) || (anc->big_disk)) 
		vtoc_init_format5_label(anc->f5);
		
	if (f7_counter == 0) 
		vtoc_init_format7_label(anc->f7);

	fdasd_reorganize_FMT1s(anc);
	fdasd_update_partition_info(anc);
}


/*
 * we have a valid VTOC pointer, let's go and read the VTOC labels
 */
static int
fdasd_valid_vtoc_pointer(fdasd_anchor_t *anc, unsigned long blk)
{
	char str[INPUT_BUF_SIZE];

	/* VOL1 label contains valid VTOC pointer */
	if (!anc->silent)
		printf("reading vtoc ..........:");

	vtoc_read_label(options.device, blk, NULL, anc->f4, NULL, NULL);

	if (anc->f4->DS4IDFMT != 0xf4) { 
		if (anc->print_table) {
			printf("Your VTOC is corrupted!\n");
			return -1;
		}
		fdasd_process_invalid_vtoc(anc);
	}
	else 
		fdasd_process_valid_vtoc(anc, blk);

	/* change volume serial? */
	if (options.volser && !anc->print_table) {
		char volser[VOLSER_LENGTH + 1];

		vtoc_volume_label_get_volser(anc->vlabel, volser);
		volser[VOLSER_LENGTH] = 0x00;

		fdasd_check_volser(options.volser, anc->devno);

		snprintf(str, INPUT_BUF_SIZE,
			"Overwrite existing volume serial '%-6s'\n"
			"with new volume serial '%-6s'?", 
			volser, options.volser);

		if (yes_no(str) != 0)
			return 0;
		
		vtoc_volume_label_set_volser(anc->vlabel, options.volser);
		anc->vlabel_changed++;
		anc->vtoc_changed++;
	}
	return 0;
}


/*
 *
 */
static void
fdasd_invalid_vtoc_pointer(fdasd_anchor_t *anc)
{
	/* VOL1 label doesn't contain valid VTOC pointer */
	if (yes_no("There is no VTOC yet, should I create one?") == 1) {
		if (!anc->silent) printf("exiting...\n");
		fdasd_exit(anc, 0);
	}

	vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS, 
				geo.cylinders, geo.heads, geo.sectors, 
				anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);

	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', anc->verbose,
			   FIRST_USABLE_TRK, geo.cylinders * geo.heads - 1,
			   geo.cylinders, geo.heads);

	vtoc_set_cchhb(&anc->vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);

	anc->vtoc_changed++;
	anc->vlabel_changed++;
}


/*
 * check the dasd for a volume label
 */
static int
fdasd_check_volume (fdasd_anchor_t *anc) 
{
	volume_label_t *vlabel = anc->vlabel;
        unsigned long blk = -1;
	char str[LINE_LENGTH];
	char inp_buf[INPUT_BUF_SIZE];

	if (!anc->silent)
		printf("reading volume label ..:");

        vtoc_read_volume_label(options.device, anc->label_pos, vlabel);

	if (strncmp(vlabel->vollbl, vtoc_ebcdic_enc("VOL1",str,4),4) == 0) {
	        /* found VOL1 volume label */
		if (!anc->silent)
			printf(" VOL1\n");

		blk = (cchhb2blk(&vlabel->vtoc) - 1) * anc->blksize;
		if (blk > 0) {
			int rc;
			rc = fdasd_valid_vtoc_pointer(anc, blk);

			if (anc->print_table && (rc < 0))
				return -1;
		}
		else {
			if (anc->print_table) {
				printf("\nFound invalid VTOC pointer.\n");
				return -1;
			}
			fdasd_invalid_vtoc_pointer(anc);
		}
	}
	else {
	        /* didn't find VOL1 volume label */

		if (anc->print_table) {
			printf("\nCould not find VOL1 volume label.\n");
			return -1;
		}

	        if (strncmp(vlabel->vollbl, 
			    vtoc_ebcdic_enc("LNX1",str,4),4) == 0) {
			if (!anc->silent)
				printf(" LNX1\n");
			strcpy(inp_buf,"Overwrite inapplicable label?");
		}
		else {
			if (!anc->silent)
				printf(" no known label\n");
			strcpy(inp_buf,"Should I create a new one?");
		}
                if ((!anc->print_volser) && (!anc->print_table) && 
		    (yes_no(inp_buf) == 1)) {
		        printf("You need a VOL1 volume label for " \
		               "partitioning\nexiting...\n");
			fdasd_exit(anc, -1);
                }
		fdasd_init_volume_label(anc);

		vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS,
					geo.cylinders, geo.heads, geo.sectors,
					anc->blksize, anc->dev_type);

		vtoc_init_format5_label(anc->f5);
		vtoc_init_format7_label(anc->f7);

		vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', 
				   anc->verbose, FIRST_USABLE_TRK, 
				   geo.cylinders * geo.heads - 1,
				   geo.cylinders, geo.heads);

		anc->vtoc_changed++;
	}

	if (!anc->silent)
		printf("\n");

	return 0;
}


/*
 * check disk access
 */
static void
fdasd_check_disk_access (fdasd_anchor_t *anc)
{
	char err_str[ERROR_STRING_SIZE];
	format1_label_t f1;
	int fd, pos, ro;

        if ((fd = open(options.device, O_RDWR)) == -1) {
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not open device '%s' " \
			"in read-only mode!\n", options.device);
		fdasd_error(anc, unable_to_open_disk, err_str);
	}

	pos = anc->blksize * (2 * geo.heads - 1);
	/* last block in the second track */
        if (lseek(fd, pos, SEEK_SET) == -1) {
	        close(fd);
		snprintf(err_str, ERROR_STRING_SIZE, 
			"Could not seek device '%s'.", options.device);
		fdasd_error(anc, unable_to_seek_disk, err_str);
        }

	if (read(fd, &f1, sizeof(format1_label_t)) != 
	    sizeof(format1_label_t)) {
		close(fd);
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not read from device '%s'.", options.device);
		fdasd_error(anc, unable_to_read_disk, err_str);
	}

        if (lseek(fd, pos, SEEK_SET) == -1) {
	        close(fd);
		snprintf(err_str, ERROR_STRING_SIZE, 
			"Could not seek device '%s'.", options.device);
		fdasd_error(anc, unable_to_seek_disk, err_str);
        }
	
	if (ioctl(fd, BLKROGET, &ro) != 0) {
		snprintf(err_str, ERROR_STRING_SIZE, 
			 "Could not get read-only status for device '%s'.",
			 options.device);
		fdasd_error(anc, unable_to_ioctl, err_str);
	}
	if (ro && !anc->print_volser && !anc->print_table)
		printf("\nWARNING: Device '%s' is a read-only device!\n"
		       "You will not be able to save any changes.\n\n",
		       options.device);

        close(fd);
}     


/*
 * reads dasd geometry data
 */
static void
fdasd_get_geometry (fdasd_anchor_t *anc) 
{
        int fd, blksize = 0;
	dasd_information_t dasd_info;
	char err_str[ERROR_STRING_SIZE];

	if ((fd = open(options.device,O_RDONLY)) < 0) {
		snprintf(err_str, ERROR_STRING_SIZE,
			"Could not open device '%s' "
			"in read-only mode!\n", options.device);
		fdasd_error(anc, unable_to_open_disk, err_str);
	}

	if (ioctl(fd, HDIO_GETGEO, &geo) != 0) {
	        close(fd);
		fdasd_error(anc, unable_to_ioctl, 
			    "Could not retrieve disk geometry information.");
	}

	if (ioctl(fd, BLKSSZGET, &blksize) != 0) {
	        close(fd);
	        fdasd_error(anc, unable_to_ioctl,
			    "Could not retrieve blocksize information.");
	}

	/* get disk type */
	if (ioctl(fd, BIODASDINFO, &dasd_info) != 0) {
	        close(fd);
		fdasd_error(anc, unable_to_ioctl, 
			    "Could not retrieve disk information.");
	}

	close(fd);

	if (strncmp(dasd_info.type, "ECKD", 4) != 0) {
		snprintf(err_str, ERROR_STRING_SIZE, 
			"%s is not an ECKD disk! This disk type "
			"is not supported!", options.device);
		fdasd_error(anc,wrong_disk_type, err_str);
	}

	if (anc->verbose) printf("disk type check     : ok\n");

        if (dasd_info.FBA_layout != 0) {
                snprintf(err_str, ERROR_STRING_SIZE, 
			"%s is not formatted with z/OS compatible "
			"disk layout!", options.device);
                fdasd_error(anc, wrong_disk_format, err_str);
        }      

	if (anc->verbose) printf("disk layout check   : ok\n");

	if (dasd_info.open_count > 1) {
		if (anc->auto_partition) {
			snprintf(err_str, ERROR_STRING_SIZE, 
				"DASD '%s' is in use. Unmount it first!",
				options.device);
			fdasd_error(anc, disk_in_use, err_str);
		} else {	
			printf("\nWARNING: Your DASD '%s' is in use.\n"
			       "         If you proceed, you can heavily "
			       "damage your system.\n         If possible exit"
			       " all applications using this disk\n         "
			       "and/or unmount it.\n\n", options.device);
		}
	}

	if (anc->verbose) printf("usage count check   : ok\n");

	anc->dev_type   = dasd_info.dev_type;
	anc->blksize    = blksize;
	anc->label_pos  = dasd_info.label_block * blksize;
	anc->devno      = dasd_info.devno;
	anc->fspace_trk = geo.cylinders * geo.heads - FIRST_USABLE_TRK;
}


/*
 * asks for partition boundaries
 */
static unsigned long
fdasd_read_int (unsigned long low, unsigned long dflt, unsigned long high, 
		enum offset base, char *mesg, fdasd_anchor_t *anc) 
{
	unsigned long long trk = 0;
	unsigned int use_default = 1;
	char msg_txt[70];

	switch(base) {
	case lower:
	        sprintf(msg_txt, "%s ([%ld]-%ld): ", mesg, low, high);
		break;
	case upper:
	        sprintf(msg_txt, "%s (%ld-[%ld]): ", mesg, low, high);
		break;
	default:
	        sprintf(msg_txt, "%s (%ld-%ld): ", mesg, low, high);
		break;
	}

	while (1) {
	        while (!isdigit(read_char(msg_txt))
		       && (*line_ptr != '-' && 
			   *line_ptr != '+' &&
			   *line_ptr != '\0'))
		        continue;
		if ((*line_ptr == '+' || *line_ptr == '-') &&
			base != lower) {
		        if (*line_ptr == '+')
			        ++line_ptr;
			trk = atoi(line_ptr);
			while (isdigit(*line_ptr)) {
			        line_ptr++;
				use_default = 0;
			}

                        switch (*line_ptr) {
			case 'c':
			case 'C': 
				trk *= geo.heads;
				break;
			case 'k':
			case 'K': 
				trk *= 1024;
				trk /= anc->blksize;
				trk /= geo.sectors;
				break;
			case 'm':
			case 'M': 
				trk *= (1024*1024);
				trk /= anc->blksize;
				trk /= geo.sectors;
				break;
			case 0x0a:
				break;
			default: 
				printf("WARNING: '%c' is not a "
				       "valid appendix and probably "
				       "not what you want!\n", 
				       *line_ptr);
				break;
                        }
			
			trk += (low - 1);
			
		}
		else if (*line_ptr == '\0') {
			switch(base) {
			case lower: trk = low; break;
			case upper: trk = high; break;
			}
		}
		else {
                        if (*line_ptr == '+' || *line_ptr == '-') {
				printf("\nWARNING: '%c' is not valid in \n"
				       "this case and will be ignored!\n",
				       *line_ptr);
                                ++line_ptr;
			}

		        trk = atoi(line_ptr);
			while (isdigit(*line_ptr)) {
			        line_ptr++;
				use_default = 0;
			}

			if (*line_ptr != 0x0a)
				printf("\nWARNING: '%c' is not a valid "
				       "appendix and probably not what "
				       "you want!\n", *line_ptr);
		}
		if (use_default)
		        printf("Using default value %lld\n", trk = dflt);
		else
		        printf("You have selected track %lld\n", trk);

		if (trk >= low && trk <= high)
		        break;
                else
		        printf("Value out of range.\n");
	}
	return trk;
}


/*
 * returns unused partition info pointer if there
 * is a free partition, otherwise NULL
 */
static partition_info_t *
fdasd_get_empty_f1_label (fdasd_anchor_t * anc) 
{
	if (anc->used_partitions < USABLE_PARTITIONS)	  
	        return anc->last;	      
	else
	        return NULL;
}


/*
 * asks for and sets some important partition data
 */
static int 
fdasd_get_partition_data (fdasd_anchor_t *anc, extent_t *part_extent,
			  partition_info_t *part_info) 
{
	unsigned long start, stop, limit;
	unsigned int cc, hh;
	cchh_t llimit,ulimit;
	partition_info_t *part_tmp;
        char mesg[48];
	u_int8_t b1, b2;
	u_int16_t cyl, head;

	start = FIRST_USABLE_TRK;
	if (anc->f4->DS4DEVCT.DS4DEVFG & ALTERNATE_CYLINDERS_USED)
		cyl = anc->f4->DS4DEVCT.DS4DSCYL - 
			(u_int16_t) anc->f4->DS4DEVAC;
	else
		cyl = anc->f4->DS4DEVCT.DS4DSCYL;

	head = anc->f4->DS4DEVCT.DS4DSTRK;
	limit = (head * cyl - 1);

	sprintf(mesg, "First track (1 track = %d KByte)", 
		geo.sectors * anc->blksize / 1024);

	/* find default start value */
	for (part_tmp = anc->first; part_tmp->next != NULL;
	     part_tmp = part_tmp->next) {
		if ((start >= part_tmp->start_trk) && 
		    (start <= part_tmp->end_trk))
			start = part_tmp->end_trk + 1;
	}

	if (start > limit) {
	        printf("Not that easy, no free tracks available.\n");
		return -1;
	}

	/* read start value */
	start = fdasd_read_int(start, start, limit, lower, mesg, anc);

	/* check start value from user */
	for (part_tmp = anc->first; part_tmp->next != NULL;
	     part_tmp = part_tmp->next) {
		if (start >= part_tmp->start_trk &&
		    start <= part_tmp->end_trk) {
			/* start is within another partition */
			start = part_tmp->end_trk + 1;
			if (start > limit) {
				start = FIRST_USABLE_TRK;
				part_tmp = anc->first;
			}

			printf("value within another partition, " \
			       "using %ld instead\n", start);
		}

		if (start < part_tmp->start_trk) {
			limit = part_tmp->start_trk - 1;
			break;
		}

	}

	if (start == limit)
	        stop = start;
	else {
	        sprintf(mesg, "Last track or +size[c|k|M]");
		stop = fdasd_read_int(start, limit, limit, upper, mesg, anc);
	}

	/* update partition info */
	part_info->len_trk    = stop - start + 1;
	part_info->start_trk  = start;
	part_info->end_trk    = stop;

	cc = start / geo.heads;
	hh = start - (cc * geo.heads);
	vtoc_set_cchh(&llimit, cc, hh);

	/* check for cylinder boundary */
	if (hh == 0)  
		b1 = 0x81;
	else
		b1 = 0x01;

	cc = stop / geo.heads;
	hh = stop - cc * geo.heads;
	vtoc_set_cchh(&ulimit, cc, hh);

        /* it is always the 1st extent */
	b2 = 0x00;

	vtoc_set_extent(part_extent, b1, b2, &llimit, &ulimit);

	return 0;
}


/*
 *
 */
static void
fdasd_enqueue_new_partition (fdasd_anchor_t *anc) 
{
        partition_info_t *part_tmp = anc->first, *part_info = anc->last;
	int i, k = 0;

	for (i = 1; i < USABLE_PARTITIONS; i++) {
	        if ((part_tmp->end_trk == 0) || 
		    (part_info->start_trk < part_tmp->start_trk))
		        break;
		else { 
		        part_tmp = part_tmp->next;
			k++;
		}
	}

	if (anc->first == part_tmp) anc->first = part_info;
	
	if (part_info != part_tmp) {
	        anc->last->prev->next = NULL;
		anc->last = anc->last->prev;

	        part_info->next = part_tmp;
		part_info->prev = part_tmp->prev;
		part_tmp->prev = part_info;
		
		if (part_info->prev != NULL)
		        part_info->prev->next = part_info;
	}

	part_info->used       = 0x01;

	for (i=0; i<USABLE_PARTITIONS; i++) {
		int j = getpos(anc, i);
		if (j >= k) setpos(anc, i, j + 1);
	}

	/* update free-space counters */
	if (anc->first == part_info) {
	        /* partition is the first used partition */
	        if (part_info->start_trk == FIRST_USABLE_TRK) {
	               /* partition starts right behind VTOC */
	               part_info->fspace_trk = anc->fspace_trk -
			       part_info->len_trk;
		       anc->fspace_trk = 0;
		}
		else {
	               /* there is some space between VTOC and partition */

	               part_info->fspace_trk = anc->fspace_trk -
			       part_info->len_trk - part_info->start_trk +
			       FIRST_USABLE_TRK;
		       anc->fspace_trk = part_info->start_trk -
			       FIRST_USABLE_TRK;
		}
	}
	else {
	        /* there are partitons in front of the new one */
 	        if (part_info->start_trk == part_info->prev->end_trk + 1) {
		        /* new partition is right behind the previous one */
		        part_info->fspace_trk = part_info->prev->fspace_trk -
				part_info->len_trk;
			part_info->prev->fspace_trk = 0;
		}
		else {
		        /* there is some space between new and prev. part. */
		        part_info->fspace_trk = part_info->prev->fspace_trk - 
				part_info->len_trk - part_info->start_trk +
				part_info->prev->end_trk + 1;
			part_info->prev->fspace_trk = part_info->start_trk - 
				part_info->prev->end_trk - 1;
		}
	}
}


/*
 *
 */
static void
fdasd_dequeue_old_partition (fdasd_anchor_t *anc, partition_info_t *part_info,
			     int k) 
{
	int i;

	if (part_info != anc->first && part_info != anc->last) {
	        /* dequeue any non-special element */
	        part_info->prev->next = part_info->next;
	        part_info->next->prev = part_info->prev;
	}        

	if (part_info == anc->first) {
	        /* dequeue first element */
	        anc->first = part_info->next;
		part_info->next->prev = NULL;
	        anc->fspace_trk += (part_info->len_trk +
				    part_info->fspace_trk);
	} else
		part_info->prev->fspace_trk += (part_info->len_trk +
						part_info->fspace_trk);

	if (part_info != anc->last) {
	        part_info->prev = anc->last;
	        part_info->next = NULL;
		anc->last->next = part_info;
		anc->last       = part_info;
	}

	for (i=0; i<USABLE_PARTITIONS; i++) {
		int j = getpos(anc, i);
		if (j >= k) setpos(anc, i, j - 1);
	}

	part_info->used       = 0x00;
	part_info->len_trk    = 0x0;
	part_info->start_trk  = 0x0;
	part_info->end_trk    = 0x0;
	part_info->fspace_trk = 0x0;
	bzero(part_info->f1, sizeof(format1_label_t));
}


/*
 * adds a new partition to the 'partition table'
 */
static void
fdasd_add_partition (fdasd_anchor_t *anc) 
{
	cchhb_t hf1;
	partition_info_t *part_info;
	extent_t ext;
	unsigned long start, stop;
	int i;

	if ((part_info = fdasd_get_empty_f1_label(anc)) == NULL) {
	        printf("No more free partitions left,\n"
		       "you have to delete one first!");
		return;
	}

	if (fdasd_get_partition_data(anc, &ext, part_info) != 0)
	        return;
	vtoc_init_format1_label(anc->vlabel->volid, anc->blksize, &ext,
				part_info->f1);
	fdasd_enqueue_new_partition(anc);
	anc->used_partitions += 1;

	i = anc->used_partitions + 2;
	if (anc->big_disk) i++;

        vtoc_set_cchhb(&hf1, VTOC_START_CC, VTOC_START_HH, i);
	vtoc_update_format4_label(anc->f4, &hf1, anc->f4->DS4DSREC - 1);

	start = ext.llimit.cc * geo.heads + ext.llimit.hh;
	stop  = ext.ulimit.cc * geo.heads + ext.ulimit.hh;

	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '-', anc->verbose,
			   start, stop, geo.cylinders, geo.heads);

	anc->vtoc_changed++;
}


/*
 * removes a partition from the 'partition table'
 */
static void
fdasd_remove_partition (fdasd_anchor_t *anc) 
{
	cchhb_t hf1;
        int i;
        unsigned int part_id;
	unsigned long start, stop;
	partition_info_t *part_info = anc->first;

	fdasd_list_partition_table(anc);

	while (!isdigit(part_id = read_char("\ndelete partition with id "
					    "(use 0 to exit): ")))
		printf("Invalid partition id '%c' detected.\n", part_id);

	printf("\n");
	part_id -= 48;
	if (part_id == 0) return;
        if ((part_id < 0) || (part_id > anc->used_partitions)) {
                printf("'%d' is not a valid partition id!\n", part_id);
                return;
        }

	printf("deleting partition number '%d'...\n", part_id);	

	setpos(anc, part_id-1, -1);
	for (i=1; i<part_id; i++) part_info=part_info->next;

	start = part_info->f1->DS1EXT1.llimit.cc * geo.heads + 
		part_info->f1->DS1EXT1.llimit.hh;
	stop  = part_info->f1->DS1EXT1.ulimit.cc * geo.heads + 
		part_info->f1->DS1EXT1.ulimit.hh;

	fdasd_dequeue_old_partition (anc, part_info, part_id-1);
	anc->used_partitions -= 1;

	if (anc->used_partitions != 0) {
		i = anc->used_partitions + 2;
		if (anc->big_disk) i++;

        	vtoc_set_cchhb(&hf1, VTOC_START_CC, VTOC_START_HH, i);
	}
	else
		bzero(&hf1, sizeof(struct cchhb));

	vtoc_update_format4_label(anc->f4, &hf1, anc->f4->DS4DSREC + 1);
	vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', anc->verbose,
			   start, stop, geo.cylinders, geo.heads);

	anc->vtoc_changed++;
}


/*
 * writes a standard volume label and a standard VTOC with
 * only one partition to disc. With this function is it 
 * possible to create one partiton in non-interactive mode,
 * which can be used within shell scripts
 */
static void
fdasd_auto_partition(fdasd_anchor_t *anc)
{
	volume_label_t *vlabel = anc->vlabel;
	partition_info_t *part_info = anc->first;
	cchh_t llimit,ulimit;
	cchhb_t hf1;
	extent_t ext;
	char volser[VOLSER_LENGTH + 1];
	u_int16_t cyl, head;
	int i;
	char old_volser[VOLSER_LENGTH + 1];

	if (!anc->silent)
		printf("auto-creating one partition for the whole disk...\n");

	vtoc_volume_label_init(vlabel);
	vtoc_volume_label_set_key(vlabel, "VOL1");
	vtoc_volume_label_set_label(vlabel, "VOL1");

	if (anc->keep_volser) {
		if(fdasd_get_volser(anc, options.device, old_volser) == 0)
			vtoc_volume_label_set_volser(vlabel, old_volser);
		else {
			fdasd_error(anc, volser_not_found, options.device);
		}
	}
	else {
		if (options.volser == NULL) 
			sprintf(volser, "0X%04x", anc->devno);
		else {
			strcpy(volser, options.volser);
			fdasd_check_volser(volser, anc->devno);
		}
		vtoc_volume_label_set_volser(vlabel, volser);

	}

	vtoc_set_cchhb(&vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);

	if (anc->verbose) printf("initializing labels...\n");
	vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS,
				geo.cylinders, geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);

	if (anc->f4->DS4DEVCT.DS4DEVFG & ALTERNATE_CYLINDERS_USED)
		cyl = anc->f4->DS4DEVCT.DS4DSCYL -
			(u_int16_t) anc->f4->DS4DEVAC;
	else
		cyl = anc->f4->DS4DEVCT.DS4DSCYL;

	head = anc->f4->DS4DEVCT.DS4DSTRK;

	part_info->used       = 0x01;
        part_info->fspace_trk = 0;
	part_info->len_trk    = head * cyl - FIRST_USABLE_TRK;
	part_info->start_trk  = FIRST_USABLE_TRK;
	part_info->end_trk    = head * cyl - 1;

	vtoc_set_cchh(&llimit, 0, FIRST_USABLE_TRK);
	vtoc_set_cchh(&ulimit, cyl - 1, head - 1);

	vtoc_set_extent(&ext, 0x01, 0x00, &llimit, &ulimit);

	vtoc_init_format1_label(anc->vlabel->volid, anc->blksize, &ext,
				part_info->f1);
	i = 3;
	if (anc->big_disk) i++;

        vtoc_set_cchhb(&hf1, VTOC_START_CC, VTOC_START_HH, i);
	vtoc_update_format4_label(anc->f4, &hf1, anc->f4->DS4DSREC - 1);

        anc->fspace_trk      = 0;
	anc->used_partitions = 1;

	anc->vlabel_changed++;
	anc->vtoc_changed++;

	fdasd_write_labels(anc);
	fdasd_exit(anc, 0);		    
}


/*
 * does the partitioning regarding to the config file
 */
static void
fdasd_auto_partition_conffile(fdasd_anchor_t *anc)
{
	volume_label_t *vlabel = anc->vlabel;
	partition_info_t *part_info = anc->first;
	char volser[VOLSER_LENGTH + 1];
	int i;
	cchh_t llimit,ulimit;
	unsigned long start, stop;
	extent_t ext;
	cchhb_t hf1;
	char old_volser[VOLSER_LENGTH + 1];

	vtoc_volume_label_init(vlabel);
	vtoc_volume_label_set_key(vlabel, "VOL1");
	vtoc_volume_label_set_label(vlabel, "VOL1");

	if (anc->keep_volser) {
		if(fdasd_get_volser(anc, options.device, old_volser) == 0)
			vtoc_volume_label_set_volser(vlabel, old_volser);
		else {
			fdasd_error(anc, volser_not_found, options.device);
		}
	}
	else {
		if (options.volser == NULL) 
			sprintf(volser, "0X%04x", anc->devno);
		else {
			strcpy(volser, options.volser);
			fdasd_check_volser(volser, anc->devno);
		}
		vtoc_volume_label_set_volser(vlabel, volser);

	}

	vtoc_set_cchhb(&vlabel->vtoc, VTOC_START_CC, VTOC_START_HH, 0x01);

	if (anc->verbose) printf("initializing labels...\n");
	vtoc_init_format4_label(anc->f4, USABLE_PARTITIONS,
				geo.cylinders, geo.heads, geo.sectors,
				anc->blksize, anc->dev_type);

	vtoc_init_format5_label(anc->f5);
	vtoc_init_format7_label(anc->f7);

	if (anc->fspace_trk != 0) {
		start = FIRST_USABLE_TRK;
		stop  = start + anc->fspace_trk - 1;
				
		vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', 
				   anc->verbose, start, stop,
				   geo.cylinders, geo.heads);
	}

	do {
		if (part_info->used != 0x01)
			continue;

		vtoc_set_cchh(&llimit,
			      part_info->start_trk / geo.heads, 
			      part_info->start_trk % geo.heads);
		vtoc_set_cchh(&ulimit, 
			      part_info->end_trk / geo.heads, 
			      part_info->end_trk % geo.heads);

		vtoc_set_extent(&ext, (llimit.hh == 0 ? 0x81 : 0x01), 
				0x00, &llimit, &ulimit);

		vtoc_init_format1_label(vlabel->volid,
					anc->blksize, &ext, part_info->f1);

		anc->used_partitions += 1;

		i = anc->used_partitions + 2;
		if (anc->big_disk) i++;

		vtoc_set_cchhb(&hf1, VTOC_START_CC, VTOC_START_HH, i);

		vtoc_update_format4_label(anc->f4, &hf1,anc->f4->DS4DSREC - 1);

		/* update free space labels */
		if (part_info->fspace_trk != 0) {
			start = part_info->end_trk + 1;
			stop  = start + part_info->fspace_trk -1;
				
			vtoc_set_freespace(anc->f4, anc->f5, anc->f7, '+', 
					   anc->verbose, start, stop,
					   geo.cylinders, geo.heads);
		}
	} while ((part_info = part_info->next) != NULL);

	anc->vlabel_changed++;
	anc->vtoc_changed++;

	fdasd_write_labels(anc);
	fdasd_exit(anc, 0);
}

/*
 * quits fdasd without saving
 */
static void
fdasd_quit(fdasd_anchor_t *anc)
{
	char str[INPUT_BUF_SIZE];

	if ((anc->vtoc_changed)||(anc->vlabel_changed)) {
		snprintf(str, INPUT_BUF_SIZE,
			"All changes will be lost! "
			"Do you really want to quit?");

                if (yes_no(str) == 1)
			return;

		printf("exiting without saving...\n");
	}
	else
		if (!anc->silent) printf("exiting...\n");

	fdasd_exit(anc, 0);
}

/*
 *
 */
int
main(int argc, char *argv[]) 
{
        fdasd_anchor_t anchor;
	int rc=0;

        fdasd_initialize_anchor(&anchor);

        fdasd_parse_options (&anchor, &options, argc, argv);
	fdasd_verify_device (&anchor, options.device);
	fdasd_verify_options (&anchor);
	fdasd_get_geometry(&anchor);
	fdasd_check_disk_access(&anchor);

	if ((geo.cylinders * geo.heads) > BIG_DISK_SIZE) anchor.big_disk++;

	if (anchor.auto_partition)
		fdasd_auto_partition(&anchor);

	if (options.conffile) {
		fdasd_parse_conffile(&anchor, &options);
		fdasd_check_conffile_input(&anchor, &options);
		fdasd_auto_partition_conffile(&anchor);
	}

	/* check dasd for labels and vtoc */
	rc = fdasd_check_volume(&anchor);

	if (anchor.print_table) {
		if (rc == 0)
			fdasd_list_partition_table(&anchor);
		fdasd_quit(&anchor);
	}

	if (anchor.print_volser) {
		fdasd_print_volser(&anchor);
		fdasd_quit(&anchor);
	}

	fdasd_menu();

	while (1) {
	        putchar('\n');
		switch (tolower(read_char("Command (m for help): "))) {
		case 'd':
		        fdasd_remove_partition(&anchor);
			break;
		case 'n':
		        fdasd_add_partition(&anchor);
			break;
                case 'v':
                        fdasd_change_volser(&anchor);
                        break;
                case 't':
                        fdasd_change_part_type(&anchor);
                        break;
		case 'p':
		        fdasd_list_partition_table(&anchor);
			break;
		case 's':
			fdasd_show_mapping(&anchor);
			break;
		case 'u':
			anchor.option_reuse++;
			break;
		case 'r':
			anchor.option_recreate++;
			break;
		case 'm':
		        fdasd_menu();		  
			break;
		case 'q':
			fdasd_quit(&anchor);
			break;
		case 'w':
		        fdasd_write_labels(&anchor);
			fdasd_exit(&anchor, 0);		    
		default:
		        printf("please use one of the following commands:\n");
			fdasd_menu();
		}

		if (anchor.option_reuse) {
			fdasd_reuse_vtoc(&anchor);
			anchor.option_reuse = 0;
		}

		if (anchor.option_recreate) {
			fdasd_recreate_vtoc(&anchor);
			anchor.option_recreate = 0;
		}

	}		

	return -1;
}