summaryrefslogtreecommitdiffstats
path: root/tools/h5ls/h5ls.c
blob: 3230b520bfa28f8c871dfc46622f465f6f7ae4d8 (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Programmer:  Robb Matzke <matzke@llnl.gov>
 *              Monday, March 23, 1998
 */


/*
 * We include the private header file so we can get to the uniform
 * programming environment it declares.  Other than that, h5ls only calls
 * HDF5 API functions (except for H5G_basename())
 */
#include "H5private.h"
#include "h5tools.h"
#include "h5tools_utils.h"

/* Command-line switches */
static int      verbose_g = 0;            /* lots of extra output */
static int      width_g = 80;             /* output width in characters */
static hbool_t  address_g = FALSE;        /* print raw data addresses */
static hbool_t  data_g = FALSE;           /* display dataset values? */
static hbool_t  label_g = FALSE;          /* label compound values? */
static hbool_t  string_g = FALSE;         /* print 1-byte numbers as ASCII? */
static hbool_t  fullname_g = FALSE;       /* print full path names */
static hbool_t  recursive_g = FALSE;      /* recursive descent listing */
static hbool_t  grp_literal_g = FALSE;    /* list group, not contents */
static hbool_t  hexdump_g = FALSE;        /* show data as raw hexadecimal */
static hbool_t  show_errors_g = FALSE;    /* print HDF5 error messages */
static hbool_t  simple_output_g = FALSE;  /* make output more machine-readable */
static hbool_t  show_file_name_g = FALSE; /* show file name for full names */

/* Info to pass to the iteration functions */
typedef struct iter_t {
    const char *container;  /* full name of the container object */
} iter_t;

/* Table containing object id and object name */
static struct {
    int  nalloc;                /* number of slots allocated */
    int  nobjs;                 /* number of objects */
    struct {
        unsigned long id[2];    /* object number */
        char *name;             /* full object name */
    } *obj;
} idtab_g;

/* Information about how to display each type of object */
static struct dispatch_t {
    const char *name;
    hid_t (*open)(hid_t loc, const char *name);
    herr_t (*close)(hid_t obj);
    herr_t (*list1)(hid_t obj);
    herr_t (*list2)(hid_t obj, const char *name);
} dispatch_g[H5G_NTYPES];

#define DISPATCH(TYPE,NAME,OPEN,CLOSE,LIST1,LIST2) {         \
    dispatch_g[TYPE].name = (NAME);           \
    dispatch_g[TYPE].open = (OPEN);           \
    dispatch_g[TYPE].close = (CLOSE);           \
    dispatch_g[TYPE].list1 = (LIST1);           \
    dispatch_g[TYPE].list2 = (LIST2);           \
}

static herr_t list (hid_t group, const char *name, void *cd);
static void display_type(hid_t type, int ind);
static char *fix_name(const char *path, const char *base);

hid_t thefile;
char  *prefix;
const char *progname="h5ls";
int   d_status;


/*-------------------------------------------------------------------------
 * Function: usage
 *
 * Purpose: Prints a usage message on stderr and then returns.
 *
 * Return: void
 *
 * Programmer: Robb Matzke
 *              Thursday, July 16, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
usage (void)
{
    fprintf(stderr, "\
usage: %s [OPTIONS] [OBJECTS...]\n\
   OPTIONS\n\
      -h, -?, --help   Print a usage message and exit\n\
      -a, --address    Print addresses for raw data\n\
      -d, --data       Print the values of datasets\n\
      -e, --errors     Show all HDF5 error reporting\n\
      -f, --full       Print full path names instead of base names\n\
      -g, --group      Show information about a group, not its contents\n\
      -l, --label      Label members of compound datasets\n\
      -r, --recursive  List all groups recursively, avoiding cycles\n\
      -s, --string     Print 1-byte integer datasets as ASCII\n\
      -S, --simple     Use a machine-readable output format\n\
      -wN, --width=N   Set the number of columns of output\n\
      -v, --verbose    Generate more verbose output\n\
      -V, --version    Print version number and exit\n\
      --vfd=DRIVER     Use the specified virtual file driver\n\
      -x, --hexdump    Show raw data in hexadecimal format\n\
\n\
   OBJECTS\n\
      Each object consists of an HDF5 file name optionally followed by a\n\
      slash and an object name within the file (if no object is specified\n\
      within the file then the contents of the root group are displayed).\n\
      The file name may include a printf(3C) integer format such as\n\
      \"%%05d\" to open a file family.\n",
     progname);
}


/*-------------------------------------------------------------------------
 * Function: sym_insert
 *
 * Purpose: Add a symbol to the table.
 *
 * Return: void
 *
 * Programmer: Robb Matzke
 *              Thursday, January 21, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
sym_insert(H5G_stat_t *sb, const char *name)
{
    int  n;

    /* Don't add it if the link count is 1 because such an object can only
     * have one name. */
    if (sb->nlink<2) return;
    
    /* Extend the table */
    if (idtab_g.nobjs>=idtab_g.nalloc) {
 idtab_g.nalloc = MAX(256, 2*idtab_g.nalloc);
 idtab_g.obj = realloc(idtab_g.obj,
         idtab_g.nalloc*sizeof(idtab_g.obj[0]));
    }

    /* Insert the entry */
    n = idtab_g.nobjs++;
    idtab_g.obj[n].id[0] = sb->objno[0];
    idtab_g.obj[n].id[1] = sb->objno[1];
    idtab_g.obj[n].name = malloc(strlen(name)+1);
    strcpy(idtab_g.obj[n].name, name);
}


/*-------------------------------------------------------------------------
 * Function: sym_lookup
 *
 * Purpose: Find another name for the specified object.
 *
 * Return: Success: Ptr to another name.
 *
 *  Failure: NULL
 *
 * Programmer: Robb Matzke
 *              Thursday, January 21, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static char *
sym_lookup(H5G_stat_t *sb)
{
    int  n;
    
    if (sb->nlink<2) return NULL; /*only one name possible*/
    for (n=0; n<idtab_g.nobjs; n++) {
 if (idtab_g.obj[n].id[0]==sb->objno[0] &&
     idtab_g.obj[n].id[1]==sb->objno[1]) {
     return idtab_g.obj[n].name;
 }
    }
    return NULL;
}


/*-------------------------------------------------------------------------
 * Function: display_string
 *
 * Purpose: Print a string value by escaping unusual characters. If
 *  STREAM is null then we only count how large the output would
 *  be.
 *
 * Return: Number of characters printed.
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
display_string(FILE *stream, const char *s, hbool_t escape_spaces)
{
    int  nprint=0;
    
    for (/*void*/; s && *s; s++) {
 switch (*s) {
 case '"':
     if (stream) fprintf(stream, "\\\"");
     nprint += 2;
     break;
 case '\\':
     if (stream) fprintf(stream, "\\\\");
     nprint += 2;
     break;
 case '\b':
     if (stream) fprintf(stream, "\\b");
     nprint += 2;
     break;
 case '\f':
     if (stream) fprintf(stream, "\\f");
     nprint += 2;
     break;
 case '\n':
     if (stream) fprintf(stream, "\\n");
     nprint += 2;
     break;
 case '\r':
     if (stream) fprintf(stream, "\\r");
     nprint += 2;
     break;
 case '\t':
     if (stream) fprintf(stream, "\\t");
     nprint += 2;
     break;
 case ' ':
     if (escape_spaces) {
  if (stream) fprintf(stream, "\\ ");
  nprint += 2;
     } else {
  if (stream) fprintf(stream, " ");
  nprint++;
     }
     break;
 default:
     if (isprint((int)*s)) {
  if (stream) putc(*s, stream);
  nprint++;
     } else {
  if (stream) {
      fprintf(stream, "\\%03o", *((const unsigned char*)s));
  }
  nprint += 4;
     }
     break;
 }
    }
    return nprint;
}


/*-------------------------------------------------------------------------
 * Function: display_native_type
 *
 * Purpose: Prints the name of a native C data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed.
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *   Robb Matzke, 1999-06-11
 *  Added the C9x types, but we still prefer to display the types
 *  from the C language itself (like `int' vs. `int32_t').
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_native_type(hid_t type, int UNUSED ind)
{
    if (H5Tequal(type, H5T_NATIVE_SCHAR)) {
 printf("native signed char");
    } else if (H5Tequal(type, H5T_NATIVE_UCHAR)) {
 printf("native unsigned char");
    } else if (H5Tequal(type, H5T_NATIVE_INT)) {
 printf("native int");
    } else if (H5Tequal(type, H5T_NATIVE_UINT)) {
 printf("native unsigned int");
    } else if (H5Tequal(type, H5T_NATIVE_SHORT)) {
 printf("native short");
    } else if (H5Tequal(type, H5T_NATIVE_USHORT)) {
 printf("native unsigned short");
    } else if (H5Tequal(type, H5T_NATIVE_LONG)) {
 printf("native long");
    } else if (H5Tequal(type, H5T_NATIVE_ULONG)) {
 printf("native unsigned long");
    } else if (H5Tequal(type, H5T_NATIVE_LLONG)) {
 printf("native long long");
    } else if (H5Tequal(type, H5T_NATIVE_ULLONG)) {
 printf("native unsigned long long");
    } else if (H5Tequal(type, H5T_NATIVE_FLOAT)) {
 printf("native float");
    } else if (H5Tequal(type, H5T_NATIVE_DOUBLE)) {
 printf("native double");
    } else if (H5Tequal(type, H5T_NATIVE_LDOUBLE)) {
 printf("native long double");
    } else if (H5Tequal(type, H5T_NATIVE_INT8)) {
 printf("native int8_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT8)) {
 printf("native uint8_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT16)) {
 printf("native int16_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT16)) {
 printf("native uint16_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT32)) {
 printf("native int32_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT32)) {
 printf("native uint32_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT64)) {
 printf("native int64_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT64)) {
 printf("native uint64_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_LEAST8)) {
 printf("native int_least8_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_LEAST8)) {
 printf("native uint_least8_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_LEAST16)) {
 printf("native int_least16_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_LEAST16)) {
 printf("native uint_least16_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_LEAST32)) {
 printf("native int_least32_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_LEAST32)) {
 printf("native uint_least32_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_LEAST64)) {
 printf("native int_least64_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_LEAST64)) {
 printf("native uint_least64_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_FAST8)) {
 printf("native int_fast8_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_FAST8)) {
 printf("native uint_fast8_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_FAST16)) {
 printf("native int_fast16_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_FAST16)) {
 printf("native uint_fast16_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_FAST32)) {
 printf("native int_fast32_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_FAST32)) {
 printf("native uint_fast32_t");
    } else if (H5Tequal(type, H5T_NATIVE_INT_FAST64)) {
 printf("native int_fast64_t");
    } else if (H5Tequal(type, H5T_NATIVE_UINT_FAST64)) {
 printf("native uint_fast64_t");
    } else if (H5Tequal(type, H5T_NATIVE_B8)) {
 printf("native 8-bit field");
    } else if (H5Tequal(type, H5T_NATIVE_B16)) {
 printf("native 16-bit field");
    } else if (H5Tequal(type, H5T_NATIVE_B32)) {
 printf("native 32-bit field");
    } else if (H5Tequal(type, H5T_NATIVE_B64)) {
 printf("native 64-bit field");
    } else if (H5Tequal(type, H5T_NATIVE_HSIZE)) {
 printf("native hsize_t");
    } else if (H5Tequal(type, H5T_NATIVE_HSSIZE)) {
 printf("native hssize_t");
    } else if (H5Tequal(type, H5T_NATIVE_HERR)) {
 printf("native herr_t");
    } else if (H5Tequal(type, H5T_NATIVE_HBOOL)) {
 printf("native hbool_t");
    } else {
 return FALSE;
    }
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_ieee_type
 *
 * Purpose: Print the name of an IEEE floating-point data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_ieee_type(hid_t type, int UNUSED ind)
{
    if (H5Tequal(type, H5T_IEEE_F32BE)) {
 printf("IEEE 32-bit big-endian float");
    } else if (H5Tequal(type, H5T_IEEE_F32LE)) {
 printf("IEEE 32-bit little-endian float");
    } else if (H5Tequal(type, H5T_IEEE_F64BE)) {
 printf("IEEE 64-bit big-endian float");
    } else if (H5Tequal(type, H5T_IEEE_F64LE)) {
 printf("IEEE 64-bit little-endian float");
    } else {
 return FALSE;
    }
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_precision
 *
 * Purpose: Prints information on the next line about precision and
 *  padding if the precision is less than the total data type
 *  size.
 *
 * Return: void
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
display_precision(hid_t type, int ind)
{
    size_t      prec;           /* precision */
    H5T_pad_t   plsb, pmsb;     /* lsb and msb padding */
    const char  *plsb_s=NULL;   /* lsb padding string */
    const char  *pmsb_s=NULL;   /* msb padding string */
    size_t      nbits;          /* number of bits */

    /* If the precision is less than the total size then show the precision
     * and offset on the following line.  Also display the padding
     * information. */
    if (8*H5Tget_size(type)!=(prec=H5Tget_precision(type))) {
 printf("\n%*s(%lu bit%s of precision beginning at bit %lu)",
        ind, "", (unsigned long)prec, 1==prec?"":"s",
        (unsigned long)H5Tget_offset(type));

 H5Tget_pad(type, &plsb, &pmsb);
 if (H5Tget_offset(type)>0) {
     switch (plsb) {
     case H5T_PAD_ZERO:
  plsb_s = "zero";
  break;
     case H5T_PAD_ONE:
  plsb_s = "one";
  break;
     case H5T_PAD_BACKGROUND:
  plsb_s = "bkg";
  break;
     case H5T_PAD_ERROR:
     case H5T_NPAD:
  plsb_s = "unknown";
  break;
     }
 }
 if (H5Tget_offset(type)+prec<8*H5Tget_size(type)) {
     switch (pmsb) {
     case H5T_PAD_ZERO:
  pmsb_s = "zero";
  break;
     case H5T_PAD_ONE:
  pmsb_s = "one";
  break;
     case H5T_PAD_BACKGROUND:
  pmsb_s = "bkg";
  break;
     case H5T_PAD_ERROR:
     case H5T_NPAD:
  pmsb_s = "unknown";
  break;
     }
 }
 if (plsb_s || pmsb_s) {
     printf("\n%*s(", ind, "");
     if (plsb_s) {
  nbits = H5Tget_offset(type);
  printf("%lu %s bit%s at bit 0",
         (unsigned long)nbits, plsb_s, 1==nbits?"":"s");
     }
     if (plsb_s && pmsb_s) printf(", ");
     if (pmsb_s) {
  nbits = 8*H5Tget_size(type)-(H5Tget_offset(type)+prec);
  printf("%lu %s bit%s at bit %lu",
         (unsigned long)nbits, pmsb_s, 1==nbits?"":"s",
         (unsigned long)(8*H5Tget_size(type)-nbits));
     }
     printf(")");
 }
    }
}


/*-------------------------------------------------------------------------
 * Function: display_int_type
 *
 * Purpose: Print the name of an integer data type.  Common information
 *  like number of bits, byte order, and sign scheme appear on
 *  the first line. Additional information might appear in
 *  parentheses on the following lines.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_int_type(hid_t type, int ind)
{
    H5T_order_t order;          /* byte order value */
    const char  *order_s=NULL;  /* byte order string */
    H5T_sign_t  sign;           /* sign scheme value */
    const char  *sign_s=NULL;   /* sign scheme string */
    
    if (H5T_INTEGER!=H5Tget_class(type)) return FALSE;
    
    /* Byte order */
    if (H5Tget_size(type)>1) {
 order = H5Tget_order(type);
 if (H5T_ORDER_LE==order) {
     order_s = " little-endian";
 } else if (H5T_ORDER_BE==order) {
     order_s = " big-endian";
 } else if (H5T_ORDER_VAX==order) {
     order_s = " mixed-endian";
 } else {
     order_s = " unknown-byte-order";
 }
    } else {
 order_s = "";
    }

    /* Sign */
    if ((sign=H5Tget_sign(type))>=0) {
 if (H5T_SGN_NONE==sign) {
     sign_s = " unsigned";
 } else if (H5T_SGN_2==sign) {
     sign_s = "";
 } else {
     sign_s = " unknown-sign";
 }
    } else {
 sign_s = " unknown-sign";
    }
    
    /* Print size, order, and sign on first line, precision and padding
     * information on the subsequent lines */
    printf("%lu-bit%s%s integer",
    (unsigned long)(8*H5Tget_size(type)), order_s, sign_s);
    display_precision(type, ind);
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_float_type
 *
 * Purpose: Print info about a floating point data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_float_type(hid_t type, int ind)
{
    H5T_order_t order;          /* byte order value */
    const char  *order_s=NULL;  /* byte order string */
    size_t      spos;           /* sign bit position */
    size_t      esize, epos;    /* exponent size and position */
    size_t      msize, mpos;    /* significand size and position */
    size_t      ebias;          /* exponent bias */
    H5T_norm_t  norm;           /* significand normalization */
    const char  *norm_s=NULL;   /* normalization string */
    H5T_pad_t   pad;            /* internal padding value */
    const char  *pad_s=NULL;    /* internal padding string */
    
    if (H5T_FLOAT!=H5Tget_class(type)) return FALSE;
    
    /* Byte order */
    if (H5Tget_size(type)>1) {
 order = H5Tget_order(type);
 if (H5T_ORDER_LE==order) {
     order_s = " little-endian";
 } else if (H5T_ORDER_BE==order) {
     order_s = " big-endian";
 } else if (H5T_ORDER_VAX==order) {
     order_s = " mixed-endian";
 } else {
     order_s = " unknown-byte-order";
 }
    } else {
 order_s = "";
    }

    /* Print size and byte order on first line, precision and padding on
     * subsequent lines. */
    printf("%lu-bit%s floating-point",
    (unsigned long)(8*H5Tget_size(type)), order_s);
    display_precision(type, ind);

    /* Print sizes, locations, and other information about each field */
    H5Tget_fields (type, &spos, &epos, &esize, &mpos, &msize);
    ebias = H5Tget_ebias(type);
    norm = H5Tget_norm(type);
    switch (norm) {
    case H5T_NORM_IMPLIED:
 norm_s = ", msb implied";
 break;
    case H5T_NORM_MSBSET:
 norm_s = ", msb always set";
 break;
    case H5T_NORM_NONE:
 norm_s = ", no normalization";
 break;
    case H5T_NORM_ERROR:
 norm_s = ", unknown normalization";
 break;
    }
    printf("\n%*s(significant for %lu bit%s at bit %lu%s)", ind, "", 
    (unsigned long)msize, 1==msize?"":"s", (unsigned long)mpos,
    norm_s);
    printf("\n%*s(exponent for %lu bit%s at bit %lu, bias is 0x%lx)",
    ind, "", (unsigned long)esize, 1==esize?"":"s",
    (unsigned long)epos, (unsigned long)ebias);
    printf("\n%*s(sign bit at %lu)", ind, "", (unsigned long)spos);

    /* Display internal padding */
    if (1+esize+msize<H5Tget_precision(type)) {
 pad = H5Tget_inpad(type);
 switch (pad) {
 case H5T_PAD_ZERO:
     pad_s = "zero";
     break;
 case H5T_PAD_ONE:
     pad_s = "one";
     break;
 case H5T_PAD_BACKGROUND:
     pad_s = "bkg";
     break;
 case H5T_PAD_ERROR:
 case H5T_NPAD:
     pad_s = "unknown";
     break;
 }
 printf("\n%*s(internal padding bits are %s)", ind, "", pad_s);
    }
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_cmpd_type
 *
 * Purpose: Print info about a compound data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_cmpd_type(hid_t type, int ind)
{
    char        *name=NULL;     /* member name */
    size_t      size;           /* total size of type in bytes */
    hid_t       subtype;        /* member data type */
    unsigned    nmembs;         /* number of members */
    int         n;              /* miscellaneous counters */
    unsigned    i;              /* miscellaneous counters */
    
    if (H5T_COMPOUND!=H5Tget_class(type)) return FALSE;
    printf("struct {");
    nmembs=H5Tget_nmembers(type);
    for (i=0; i<nmembs; i++) {

        /* Name and offset */
        name = H5Tget_member_name(type, i);
        printf("\n%*s\"", ind+4, "");
        n = display_string(stdout, name, FALSE);
        printf("\"%*s +%-4lu ", MAX(0, 16-n), "",
               (unsigned long)H5Tget_member_offset(type, i));
        free(name);

        /* Member's type */
        subtype = H5Tget_member_type(type, i);
        display_type(subtype, ind+4);
        H5Tclose(subtype);
    }
    size = H5Tget_size(type);
    printf("\n%*s} %lu byte%s",
    ind, "", (unsigned long)size, 1==size?"":"s");
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_enum_type
 *
 * Purpose: Print info about an enumeration data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Wednesday, December 23, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_enum_type(hid_t type, int ind)
{
    char        **name=NULL;    /* member names */
    unsigned char *value=NULL;  /* value array */
    unsigned    nmembs;         /* number of members */
    int         nchars;         /* number of output characters */
    hid_t       super;          /* enum base integer type */
    hid_t       native=-1;      /* native integer data type */
    size_t      dst_size;       /* destination value type size */
    unsigned    i;              /* miscellaneous counters */
    size_t j;
        
    if (H5T_ENUM!=H5Tget_class(type)) return FALSE;
    nmembs = H5Tget_nmembers(type);
    assert(nmembs>0);
    super = H5Tget_super(type);
    printf("enum ");
    display_type(super, ind+4);
    printf(" {");
    
    /* Determine what data type to use for the native values.  To simplify
     * things we entertain three possibilities:
     *  1. long_long -- the largest native signed integer
     * 2. unsigned long_long -- the largest native unsigned integer
     *     3. raw format */
    if (H5Tget_size(type)<=sizeof(long_long)) {
        dst_size = sizeof(long_long);
        if (H5T_SGN_NONE==H5Tget_sign(type)) {
            native = H5T_NATIVE_ULLONG;
        } else {
            native = H5T_NATIVE_LLONG;
        }
    } else {
        dst_size = H5Tget_size(type);
    }

    /* Get the names and raw values of all members */
    name = calloc(nmembs, sizeof(char*));
    value = calloc(nmembs, MAX(H5Tget_size(type), dst_size));
    for (i=0; i<nmembs; i++) {
        name[i] = H5Tget_member_name(type, i);
        H5Tget_member_value(type, i, value+i*H5Tget_size(type));
    }

    /* Convert values to native data type */
    if (native>0) H5Tconvert(super, native, nmembs, value, NULL, H5P_DEFAULT);

    /* Sort members by increasing value */
    /*not implemented yet*/

    /* Print members */
    for (i=0; i<nmembs; i++) {
        printf("\n%*s", ind+4, "");
        nchars = display_string(stdout, name[i], TRUE);
        printf("%*s = ", MAX(0, 16-nchars), "");

        if (native<0) {
            printf("0x");
            for (j=0; j<dst_size; j++)
                printf("%02x", value[i*dst_size+j]);
        } else if (H5T_SGN_NONE==H5Tget_sign(native)) {
            HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"u",
            *((unsigned long_long*)((void*)(value+i*dst_size))));
        } else {
            HDfprintf(stdout,"%"H5_PRINTF_LL_WIDTH"d",
            *((long_long*)((void*)(value+i*dst_size))));
        }
    }

    /* Release resources */
    for (i=0; i<nmembs; i++) free(name[i]);
    free(name);
    free(value);
    H5Tclose(super);

    if (0==nmembs) printf("\n%*s <empty>", ind+4, "");
    printf("\n%*s}", ind, "");
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_string_type
 *
 * Purpose: Print information about a string data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_string_type(hid_t type, int UNUSED ind)
{
    H5T_str_t  pad;
    const char  *pad_s=NULL;
    H5T_cset_t  cset;
    const char  *cset_s=NULL;
    
    if (H5T_STRING!=H5Tget_class(type)) return FALSE;

    /* Padding */
    pad = H5Tget_strpad(type);
    switch (pad) {
    case H5T_STR_NULLTERM:
 pad_s = "null-terminated";
 break;
    case H5T_STR_NULLPAD:
 pad_s = "null-padded";
 break;
    case H5T_STR_SPACEPAD:
 pad_s = "space-padded";
 break;
    case H5T_STR_RESERVED_3:
    case H5T_STR_RESERVED_4:
    case H5T_STR_RESERVED_5:
    case H5T_STR_RESERVED_6:
    case H5T_STR_RESERVED_7:
    case H5T_STR_RESERVED_8:
    case H5T_STR_RESERVED_9:
    case H5T_STR_RESERVED_10:
    case H5T_STR_RESERVED_11:
    case H5T_STR_RESERVED_12:
    case H5T_STR_RESERVED_13:
    case H5T_STR_RESERVED_14:
    case H5T_STR_RESERVED_15:
    case H5T_STR_ERROR:
 pad_s = "unknown-format";
 break;
    }

    /* Character set */
    cset = H5Tget_cset(type);
    switch (cset) {
    case H5T_CSET_ASCII:
 cset_s = "ASCII";
 break;
    case H5T_CSET_RESERVED_1:
    case H5T_CSET_RESERVED_2:
    case H5T_CSET_RESERVED_3:
    case H5T_CSET_RESERVED_4:
    case H5T_CSET_RESERVED_5:
    case H5T_CSET_RESERVED_6:
    case H5T_CSET_RESERVED_7:
    case H5T_CSET_RESERVED_8:
    case H5T_CSET_RESERVED_9:
    case H5T_CSET_RESERVED_10:
    case H5T_CSET_RESERVED_11:
    case H5T_CSET_RESERVED_12:
    case H5T_CSET_RESERVED_13:
    case H5T_CSET_RESERVED_14:
    case H5T_CSET_RESERVED_15:
    case H5T_CSET_ERROR:
 cset_s = "unknown-character-set";
 break;
    }

    if (H5Tis_variable_str(type)) {
        printf("variable-length");
    } else {
        printf("%lu-byte", (unsigned long)H5Tget_size(type));
    }
    printf(" %s %s string", pad_s, cset_s);
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_reference_type
 *
 * Purpose: Prints information about a reference data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *   Robb Matzke, 1999-06-04
 *  Knows about object and dataset region references.
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_reference_type(hid_t type, int UNUSED ind)
{
    if (H5T_REFERENCE!=H5Tget_class(type)) return FALSE;

    if (H5Tequal(type, H5T_STD_REF_OBJ)) {
 printf("object reference");
    } else if (H5Tequal(type, H5T_STD_REF_DSETREG)) {
 printf("dataset region reference");
    } else {
 printf("%lu-byte unknown reference",
        (unsigned long)H5Tget_size(type));
    }
    
    return TRUE;
}


/*-------------------------------------------------------------------------
 * Function: display_opaque_type
 *
 * Purpose: Prints information about an opaque data type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Robb Matzke
 *              Monday, June  7, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hbool_t
display_opaque_type(hid_t type, int ind)
{
    char *tag;
    size_t size;
    
    if (H5T_OPAQUE!=H5Tget_class(type)) return FALSE;

    size = H5Tget_size(type);
    printf("%lu-byte opaque type", (unsigned long)size);
    if ((tag=H5Tget_tag(type))) {
 printf("\n%*s(tag = \"", ind, "");
 display_string(stdout, tag, FALSE);
 printf("\")");
 free(tag);
    }
    return TRUE;
}

/*-------------------------------------------------------------------------
 * Function:    display_vlen_type
 *
 * Purpose:     Print information about a variable-length type
 *
 * Return:      Success:        TRUE
 *
 *              Failure:        FALSE
 *
 * Programmer:  Robb Matzke
 *              Friday, December  1, 2000
 *
 * Modifications:
 *-------------------------------------------------------------------------
 */
static hbool_t
display_vlen_type(hid_t type, int ind)
{
    hid_t       super;
    
    if (H5T_VLEN!=H5Tget_class(type)) return FALSE;

    printf("variable length of\n%*s", ind+4, "");
    super = H5Tget_super(type);
    display_type(super, ind+4);
    H5Tclose(super);
    return TRUE;
}

/*---------------------------------------------------------------------------
 * Purpose:     Print information about an array type
 *
 * Return:      Success:        TRUE
 *
 *              Failure:        FALSE
 *
 * Programmer:  Robb Matzke
 *              Thursday, January 31, 2002
 *
 * Modifications:
 *---------------------------------------------------------------------------
 */
static hbool_t
display_array_type(hid_t type, int ind)
{
    hid_t       super;
    int         ndims, i, *perm=NULL, identity;
    hsize_t     *dims=NULL;

    if (H5T_ARRAY!=H5Tget_class(type)) return FALSE;
    ndims = H5Tget_array_ndims(type);
    if (ndims) {
        dims = malloc(ndims*sizeof(dims[0]));
        perm = malloc(ndims*sizeof(perm[0]));
        H5Tget_array_dims(type, dims, perm);

        /* Print dimensions */
        for (i=0; i<ndims; i++)
            HDfprintf(stdout, "%s%Hu" , i?",":"[", dims[i]);
        putchar(']');

        /* Print permutation vector if not identity */
        for (i=0, identity=TRUE; identity && i<ndims; i++) {
            if (i!=perm[i]) identity = FALSE;
        }
        if (!identity) {
            fputs(" perm=[", stdout);
            for (i=0; i<ndims; i++)
                HDfprintf(stdout, "%s%d", i?",":"", perm[i]);
            putchar(']');
        }

        free(dims);
        free(perm);
    } else {
        fputs(" [SCALAR]", stdout);
    }
    

    /* Print parent type */
    putchar(' ');
    super = H5Tget_super(type);
    display_type(super, ind+4);
    H5Tclose(super);
    return TRUE;
}
      
/*-------------------------------------------------------------------------
 * Function: display_bitfield_type
 *
 * Purpose: Print information about a bitfield type.
 *
 * Return: Success: TRUE
 *
 *  Failure: FALSE, nothing printed
 *
 * Programmer: Pedro Vicente
 *              Tuesday, May  20, 2003
 *
 * Modifications:
 *              Robb Matzke, LLNL 2003-06-05
 *              Generalized Pedro's original if/then/else.  Also display
 *              precision/offset information.
 *-------------------------------------------------------------------------
 */
static hbool_t
display_bitfield_type(hid_t type, int ind)
{
    H5T_order_t order;          /* byte order value */
    const char  *order_s=NULL;  /* byte order string */

    if (H5T_BITFIELD!=H5Tget_class(type)) return FALSE;
    if (H5Tget_size(type)>1) {
        order = H5Tget_order(type);
        if (H5T_ORDER_LE==order) {
            order_s = " little-endian";
        } else if (H5T_ORDER_BE==order) {
            order_s = " big-endian";
        } else if (H5T_ORDER_VAX==order) {
            order_s = " mixed-endian";
        } else {
            order_s = "unknown-byte-order";
        }
    } else {
        order_s = "";
    }

    printf("%lu-bit%s bitfield",
           (unsigned long)(8*H5Tget_size(type)), order_s);
    display_precision(type, ind);
    return TRUE;
}

/*-------------------------------------------------------------------------
 * Function: display_type
 *
 * Purpose: Prints a data type definition.  The definition is printed
 *  without any leading space or trailing line-feed (although
 *  there might be line-feeds inside the type definition).  The
 *  first line is assumed to have IND characters before it on
 *  the same line (printed by the caller).
 *
 * Return: void
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *   Robb Matzke, 1999-06-11
 *  Prints the OID of shared data types.
 *
 *-------------------------------------------------------------------------
 */
static void
display_type(hid_t type, int ind)
{
    H5T_class_t  data_class = H5Tget_class(type);
    H5G_stat_t  sb;
    
    /* Bad data type */
    if (type<0) {
 printf("<ERROR>");
 return;
    }

    /* Shared? If so then print the type's OID */
    if (H5Tcommitted(type)) {
 if (H5Gget_objinfo(type, ".", FALSE, &sb)>=0) {
     printf("shared-%lu:%lu:%lu:%lu ",
     sb.fileno[1], sb.fileno[0],
     sb.objno[1], sb.objno[0]);
 } else {
     printf("shared ");
 }
    }
    
    /* Print the type */
    if ((!simple_output_g && display_native_type(type, ind)) ||
 display_ieee_type(type, ind) ||
 display_int_type(type, ind) ||
 display_float_type(type, ind) ||
 display_cmpd_type(type, ind) ||
 display_enum_type(type, ind) ||
 display_string_type(type, ind) ||
 display_reference_type(type, ind) ||
 display_vlen_type(type, ind) ||
 display_array_type(type, ind) ||
 display_opaque_type(type, ind) ||
	display_bitfield_type(type, ind)) {
 return;
    }

    /* Unknown type */
    printf("%lu-byte class-%u unknown",
    (unsigned long)H5Tget_size(type),
    (unsigned)data_class);
}


/*-------------------------------------------------------------------------
 * Function: dump_dataset_values
 *
 * Purpose: Prints all values of a dataset.
 *
 * Return: void
 *
 * Programmer: Robb Matzke
 *              Tuesday, July 21, 1998
 *
 * Modifications:
 *  Robb Matzke, 1999-09-27
 *  Understands the simple_output_g switch which causes data to
 *  be displayed in a more machine-readable format.
 *-------------------------------------------------------------------------
 */
static void
dump_dataset_values(hid_t dset)
{
    hid_t  f_type = H5Dget_type(dset);
    size_t  size = H5Tget_size(f_type);
    h5dump_t  info;
    char  string_prefix[64];
    static char         fmt_double[16], fmt_float[16];
 
    /* Set to all default values and then override */
    memset(&info, 0, sizeof info);

    if (simple_output_g) {
 info.idx_fmt = "";
 info.line_ncols = 65535; /*something big*/
 info.line_per_line = 1;
 info.line_multi_new = 0;
 info.line_pre  = "        ";
 info.line_cont = "         ";
 
 info.arr_pre = "";
 info.arr_suf = "";
 info.arr_sep = " ";

 info.cmpd_pre = "";
 info.cmpd_suf = "";
 info.cmpd_sep = " ";

 if (label_g) info.cmpd_name = "%s=";
 
 info.elmt_suf1 = " ";
 info.str_locale = ESCAPE_HTML;
 
    } else {
 info.idx_fmt = "(%s)";
 info.line_ncols = width_g;
 info.line_multi_new = 1;
 if (label_g) info.cmpd_name = "%s=";
 info.line_pre  = "        %s ";
 info.line_cont = "        %s  ";
 info.str_repeat = 8;
    }

    /* Floating point types should display full precision */
    sprintf(fmt_float, "%%1.%dg", FLT_DIG);
    info.fmt_float = fmt_float;
    sprintf(fmt_double, "%%1.%dg", DBL_DIG);
    info.fmt_double = fmt_double;

    info.dset_format =  "DSET-%lu:%lu:%lu:%lu-";
    info.dset_hidefileno = 0;
    
    info.obj_format = "-%lu:%lu:%lu:%lu";
    info.obj_hidefileno = 0;
    
    info.dset_blockformat_pre = "%sBlk%lu: ";
    info.dset_ptformat_pre = "%sPt%lu: ";
    
    info.line_indent = "";
    
    if (hexdump_g) {
        /* Print all data in hexadecimal format if the `-x' or `--hexdump'
         * command line switch was given. */
 info.raw = TRUE;
    } else if (string_g && 1==size && H5T_INTEGER==H5Tget_class(f_type)) {
        /* Print 1-byte integer data as an ASCI character string instead of
         * integers if the `-s' or `--string' command-line option was given. */
 info.ascii = TRUE;
 info.elmt_suf1 = "";
 info.elmt_suf2 = "";
 strcpy(string_prefix, info.line_pre);
 strcat(string_prefix, "\"");
 info.line_pre = string_prefix;
 info.line_suf = "\"";
    }
    
    /* Print all the values. */    
    printf("    Data:\n");
    if (h5tools_dump_dset(stdout, &info, dset, -1, NULL, -1) < 0) {
 printf("        Unable to print data.\n");
    }

    H5Tclose(f_type);
}


/*-------------------------------------------------------------------------
 * Function: list_attr
 *
 * Purpose: Prints information about attributes.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Friday, June  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
list_attr (hid_t obj, const char *attr_name, void UNUSED *op_data)
{
    hid_t attr, space, type, p_type;
    hsize_t size[64], nelmts=1;
    int  ndims, i, n;
    size_t need;
    hsize_t     temp_need;
    void *buf;
    h5dump_t info;
    H5S_class_t space_type;

    printf("    Attribute: ");
    n = display_string(stdout, attr_name, TRUE);
    printf("%*s", MAX(0, 9-n), "");

    if ((attr = H5Aopen_name(obj, attr_name))) {
        space = H5Aget_space(attr);
        type = H5Aget_type(attr);

        /* Data space */
        ndims = H5Sget_simple_extent_dims(space, size, NULL);
        space_type = H5Sget_simple_extent_type(space);
        switch (space_type) {
            case H5S_SCALAR:
                /* scalar dataspace */
                puts(" scalar");
                break;
            case H5S_SIMPLE:
                /* simple dataspace */
                printf(" {");
                for (i=0; i<ndims; i++) {
                    HDfprintf(stdout, "%s%Hu", i?", ":"", size[i]);
                    nelmts *= size[i];
                }
                puts("}");
                break;
            default:
                /* Unknown dataspace type */
                puts(" unknown");
                break;
        }

        /* Data type */
        printf("        Type:      ");
        display_type(type, 15);
        putchar('\n');

        /* Data */
        memset(&info, 0, sizeof info);
        info.line_multi_new = 1;
        if (nelmts<5) {
            info.idx_fmt = "";
            info.line_1st  = "        Data:  ";
            info.line_pre  = "               ";
            info.line_cont = "                ";
            info.str_repeat = 8;
            
        } else {
            printf("        Data:\n");
            info.idx_fmt = "(%s)";
            info.line_pre  = "            %s ";
            info.line_cont = "            %s  ";
            info.str_repeat = 8;
        }
        
        info.line_ncols = width_g;
        if (label_g) info.cmpd_name = "%s=";
        if (string_g && 1==H5Tget_size(type) &&
            H5T_INTEGER==H5Tget_class(type)) {
            info.ascii = TRUE;
            info.elmt_suf1 = "";
            info.elmt_suf2 = "";
            info.idx_fmt  = "(%s)";
            info.line_pre = "            %s \"";
            info.line_suf = "\"";
        }

        /* values of type reference */
        info.obj_format = "-%lu:%lu:%lu:%lu";
        info.obj_hidefileno = 0;
        if (hexdump_g)
           p_type = H5Tcopy(type);
        else
           p_type = h5tools_get_native_type(type);

        if (p_type>=0) {
            temp_need= nelmts * MAX(H5Tget_size(type), H5Tget_size(p_type));
            assert(temp_need==(hsize_t)((size_t)temp_need));
            need = (size_t)temp_need;
            buf = malloc(need);
            assert(buf);
            if (H5Aread(attr, p_type, buf)>=0)
               h5tools_dump_mem(stdout, &info, attr, p_type, space, buf, -1);
            free(buf);
            H5Tclose(p_type); 
        }
            
        H5Sclose(space);
        H5Tclose(type);
        H5Aclose(attr);
    } else {
        putchar('\n');
    }
    
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: dataset_list1
 *
 * Purpose: List information about a dataset which should appear on the
 *  same line as the dataset name.  This information will precede
 *  information which is applicable to all objects which will be
 *  printed by the caller.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Thursday, August 27, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
dataset_list1(hid_t dset)
{
    hsize_t     cur_size[64];   /* current dataset dimensions */
    hsize_t     max_size[64];   /* maximum dataset dimensions */
    hid_t       space;          /* data space                 */
    int         ndims;          /* dimensionality             */
    H5S_class_t space_type;     /* type of dataspace          */
    int   i;

    /* Information that goes on the same row as the name.  The name has
     * already been printed. */
    space = H5Dget_space(dset);
    space_type = H5Sget_simple_extent_type(space);
    ndims = H5Sget_simple_extent_dims(space, cur_size, max_size);
    printf (" {");
    for (i=0; i<ndims; i++) {
 HDfprintf (stdout, "%s%Hu", i?", ":"", cur_size[i]);
 if (max_size[i]==H5S_UNLIMITED) {
     HDfprintf (stdout, "/%s", "Inf");
 } else if (max_size[i]!=cur_size[i] || verbose_g>0) {
     HDfprintf(stdout, "/%Hu", max_size[i]);
 }
    }
    if (space_type==H5S_SCALAR) printf("SCALAR");
    putchar('}');
    H5Sclose (space);

    return 0;
}


/*-------------------------------------------------------------------------
 * Function: dataset_list2
 *
 * Purpose: List information about a dataset which should appear after
 *  information which is general to all objects.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Thursday, August 27, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
dataset_list2(hid_t dset, const char UNUSED *name)
{
    hid_t       dcpl;           /* dataset creation property list */
    hid_t       type;           /* data type of dataset */
    hid_t       space;          /* data space of dataset */
    int         nf;             /* number of filters */
    unsigned    filt_flags;     /* filter flags */
    H5Z_filter_t filt_id;       /* filter identification number */
    unsigned    cd_values[20];  /* filter client data values */
    size_t      cd_nelmts;      /* filter client number of values */
    size_t      cd_num;         /* filter client data counter */
    char        f_name[256];    /* filter/file name */
    char        s[64];          /* temporary string buffer */
    off_t       f_offset;       /* offset in external file */
    hsize_t     f_size;         /* bytes used in external file */
    hsize_t     total, used;    /* total size or offset */
    hsize_t     chsize[64];     /* chunk size in elements */
    int         ndims;          /* dimensionality */
    int         n, max_len;     /* max extern file name length */
    double      utilization;    /* percent utilization of storage */
    int   i;
    
    if (verbose_g>0) {
 dcpl = H5Dget_create_plist(dset);
 space = H5Dget_space(dset);
 type = H5Dget_type(dset);

 /* Print information about chunked storage */
 if (H5D_CHUNKED==H5Pget_layout(dcpl)) {
     ndims = H5Pget_chunk(dcpl, NELMTS(chsize), chsize/*out*/);
     printf("    %-10s {", "Chunks:");
     total = H5Tget_size(type);
     for (i=0; i<ndims; i++) {
  printf("%s%lu", i?", ":"", (unsigned long)(chsize[i]));
  total *= chsize[i];
     }
     printf("} %lu bytes\n", (unsigned long)total);
 }

 /* Print total raw storage size */
 total = H5Sget_simple_extent_npoints(space) * H5Tget_size(type);
 used = H5Dget_storage_size(dset);
 printf("    %-10s ", "Storage:");
 printf("%lu logical byte%s, %lu allocated byte%s",
        (unsigned long)total, 1==total?"":"s",
        (unsigned long)used, 1==used?"":"s");
 if (used>0) {
#ifdef WIN32
    utilization = (hssize_t)total*100.0 /(hssize_t)used;
#else
    utilization = (total*100.0)/used;
#endif
     printf(", %1.2f%% utilization", utilization);
 }
 putchar('\n');
 
 /* Print information about external strorage */
 if ((nf = H5Pget_external_count(dcpl))>0) {
     for (i=0, max_len=0; i<nf; i++) {
  H5Pget_external(dcpl, (unsigned)i, sizeof(f_name), f_name, NULL, NULL);
  n = display_string(NULL, f_name, TRUE);
  max_len = MAX(max_len, n);
     }
     printf("    %-10s %d external file%s\n",
     "Extern:", nf, 1==nf?"":"s");
     printf("        %4s %10s %10s %10s %s\n",
     "ID", "DSet-Addr", "File-Addr", "Bytes", "File");
     printf("        %4s %10s %10s %10s ",
     "----", "----------", "----------", "----------");
     for (i=0; i<max_len; i++) putchar('-');
     putchar('\n');
     for (i=0, total=0; i<nf; i++) {
  if (H5Pget_external(dcpl, (unsigned)i, sizeof(f_name), f_name, &f_offset,
        &f_size)<0) {
      HDfprintf(stdout,
         "        #%03d %10Hu %10s %10s ***ERROR*** %s\n",
         i, total, "", "",
         i+1<nf?"Following addresses are incorrect":"");
  } else if (H5S_UNLIMITED==f_size) {
      HDfprintf(stdout, "        #%03d %10Hu %10Hu %10s ",
         i, total, (hsize_t)f_offset, "INF");
      display_string(stdout, f_name, TRUE);
  } else {
      HDfprintf(stdout, "        #%03d %10Hu %10Hu %10Hu ",
         i, total, (hsize_t)f_offset, f_size);
      display_string(stdout, f_name, TRUE);
  }
  putchar('\n');
  total += f_size;
     }
     printf("        %4s %10s %10s %10s ",
     "----", "----------", "----------", "----------");
     for (i=0; i<max_len; i++) putchar('-');
     putchar('\n');
 }

 /* Print information about raw data filters */
 if ((nf = H5Pget_nfilters(dcpl))>0) {
     for (i=0; i<nf; i++) {
  cd_nelmts = NELMTS(cd_values);
  filt_id = H5Pget_filter(dcpl, (unsigned)i, &filt_flags, &cd_nelmts,
     cd_values, sizeof(f_name), f_name);
  f_name[sizeof(f_name)-1] = '\0';
  sprintf(s, "Filter-%d:", i);
  printf("    %-10s %s-%u %s {", s,
         f_name[0]?f_name:"method",
         (unsigned)filt_id,
         filt_flags & H5Z_FLAG_OPTIONAL?"OPT":"");
  for (cd_num=0; cd_num<cd_nelmts; cd_num++) {
      printf("%s%u", cd_num?", ":"", cd_values[cd_num]);
  }
  printf("}\n");
     }
 }

 /* Print data type */
 printf("    %-10s ", "Type:");
 display_type(type, 15);
 printf("\n");

 /* Print address information */
 if (address_g) H5Ddebug(dset);

 /* Close stuff */
 H5Tclose(type);
 H5Sclose(space);
 H5Pclose(dcpl);
    }

    if (data_g) dump_dataset_values(dset);
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: group_list2
 *
 * Purpose: List information about a group which should appear after
 *  information which is general to all objects.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Thursday, January 21, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
group_list2(hid_t grp, const char *name)
{
    iter_t iter;
    
    if (recursive_g) {
 iter.container = name;
 H5Giterate(grp, ".", NULL, list, &iter);
    }
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: datatype_list2
 *
 * Purpose: List information about a data type which should appear after
 *  information which is general to all objects.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Thursday, November  5, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static herr_t
datatype_list2(hid_t type, const char UNUSED *name)
{
    if (verbose_g>0) {
 printf("    %-10s ", "Type:");
 display_type(type, 15);
 printf("\n");
    }
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: link_open
 *
 * Purpose: This gets called to open a symbolic link.  Since symbolic
 *  links don't correspond to actual objects we simply print the
 *  link information and return failure.
 *
 * Return: Success: 0 - an invalid object but successful return
 *    of this function.
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Thursday, August 27, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static hid_t
link_open(hid_t location, const char *name)
{
    char buf[64];
    
    if (H5Gget_linkval (location, name, sizeof(buf), buf)<0) return -1;
    if (NULL==HDmemchr(buf, 0, sizeof(buf))) {
 strcpy(buf+sizeof(buf)-4, "...");
    }
    fputs(buf, stdout);

    return 0;
}


/*-------------------------------------------------------------------------
 * Function: list
 *
 * Purpose: Prints the group member name.
 *
 * Return: Success: 0
 *
 *  Failure: -1
 *
 * Programmer: Robb Matzke
 *              Monday, March 23, 1998
 *
 * Modifications:
 *              Robb Matzke, LLNL, 2003-06-06
 *              If simple_output_g (set by `--simple') is turned on then
 *              the modification time is printed as UTC instead of the
 *              local timezone.
 *-------------------------------------------------------------------------
 */
static herr_t
list (hid_t group, const char *name, void *_iter)
{
    hid_t obj=-1;
    char buf[512], comment[50], *fullname=NULL, *s=NULL;
    H5G_stat_t sb;
    struct tm *tm;
    herr_t status;
    iter_t *iter = (iter_t*)_iter;
    int  n;
    
    /* Print the object name, either full name or base name */
    fullname = fix_name(iter->container, name);
    if (fullname_g) {
 n = display_string(stdout, fullname, TRUE);
 printf("%*s ", MAX(0, 24-n), "");
    } else {
 n = display_string(stdout, name, TRUE);
 printf("%*s ", MAX(0, 24-n), "");
    }

    /* Get object information */
    H5E_BEGIN_TRY {
 status = H5Gget_objinfo(group, name, FALSE, &sb);
    } H5E_END_TRY;
    if (status<0) {
 puts("**NOT FOUND**");
 return 0;
    } else if (sb.type<0 || sb.type>=H5G_NTYPES) {
 printf("Unknown type(%d)", sb.type);
 sb.type = H5G_UNKNOWN;
    }
    if (sb.type>=0 && dispatch_g[sb.type].name) {
 fputs(dispatch_g[sb.type].name, stdout);
    }

    /* If the object has already been printed then just show the object ID
     * and return. */
    if ((s=sym_lookup(&sb))) {
 printf(", same as ");
 display_string(stdout, s, TRUE);
 printf("\n");
 goto done;
    } else {
 sym_insert(&sb, fullname);
    }
    
    /* Open the object.  Not all objects can be opened.  If this is the case
     * then return right away. */
    if (sb.type>=0 &&
 (NULL==dispatch_g[sb.type].open ||
  (obj=(dispatch_g[sb.type].open)(group, name))<0)) {
 printf(" *ERROR*\n");
 goto done;
    }
    
    /* List the first line of information for the object. */    
    if (sb.type>=0 && dispatch_g[sb.type].list1) {
 (dispatch_g[sb.type].list1)(obj);
    }
    putchar('\n');
    
    /* Show detailed information about the object, beginning with information
     * which is common to all objects. */
    if (verbose_g>0 && H5G_LINK!=sb.type) {
 if (sb.type>=0) H5Aiterate(obj, NULL, list_attr, NULL);
 printf("    %-10s %lu:%lu:%lu:%lu\n", "Location:",
        sb.fileno[1], sb.fileno[0], sb.objno[1], sb.objno[0]);
 printf("    %-10s %u\n", "Links:", sb.nlink);
        if (sb.mtime>0) {
            if (simple_output_g) tm=gmtime(&(sb.mtime));
            else tm=localtime(&(sb.mtime));
            if (tm) {
     strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", tm);
     printf("    %-10s %s\n", "Modified:", buf);
 }
 }
 comment[0] = '\0';
 H5Gget_comment(group, name, sizeof(comment), comment);
 strcpy(comment+sizeof(comment)-4, "...");
 if (comment[0]) {
     printf("    %-10s \"", "Comment:");
     display_string(stdout, comment, FALSE);
     puts("\"");
 }
    }
    if (sb.type>=0 && dispatch_g[sb.type].list2) {
 (dispatch_g[sb.type].list2)(obj, fullname);
    }
    
    /* Close the object. */    
 done:
    if (sb.type>=0 && obj>=0 && dispatch_g[sb.type].close) {
 (dispatch_g[sb.type].close)(obj);
    }
    if (fullname) free(fullname);
    return 0;
}


/*-------------------------------------------------------------------------
 * Function: fix_name
 *
 * Purpose: Returns a malloc'd buffer that contains the PATH and BASE
 *  names separated by a single slash. It also removes duplicate
 *  and trailing slashes.
 *
 * Return: Success: Ptr to fixed name from malloc()
 *
 *  Failure: NULL
 *
 * Programmer: Robb Matzke
 *              Thursday, January 21, 1999
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static char *
fix_name(const char *path, const char *base)
{
    size_t n = (path?strlen(path):0) + (base?strlen(base):0) + 3;
    char *s = malloc(n), prev='\0';
    int  len=0;

    if (path) {
 /* Path, followed by slash */
 for (/*void*/; *path; path++) {
     if ('/'!=*path || '/'!=prev) prev = s[len++] = *path;
 }
 if ('/'!=prev) prev = s[len++] = '/';
    }

    if (base) {
 /* Base name w/o trailing slashes */
 const char *end = base + strlen(base);
 while (end>base && '/'==end[-1]) --end;
 for (/*void*/; base<end; base++) {
     if ('/'!=*base || '/'!=prev) prev = s[len++] = *base;
 }
    }

    s[len] = '\0';
    return s;
}


/*-------------------------------------------------------------------------
 * Function: get_width
 *
 * Purpose: Figure out how wide the screen is.  This is highly
 *  unportable, but the user can always override the width we
 *  detect by giving a command-line option. These code snippets
 *  were borrowed from the GNU less(1).
 *
 * Return: Success: Number of columns.
 *
 *  Failure: Some default number of columms.
 *
 * Programmer: Robb Matzke
 *              Friday, November  6, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int
get_width(void)
{
    int  width = 80;  /*the default   */
    char *s;

    /* Try to get it from the COLUMNS environment variable first since it's
     * value is sometimes wrong. */
    if ((s=getenv("COLUMNS")) && *s && isdigit((int)*s)) {
 width = (int)strtol(s, NULL, 0);
    }

#if defined(H5_HAVE_STRUCT_VIDEOCONFIG) && defined(H5_HAVE__GETVIDEOCONFIG)
    {
 /* Microsoft C */
 struct videoconfig w;
 _getvideoconfig(&w);
 width = w.numtextcols;
    }
#elif defined(H5_HAVE_STRUCT_TEXT_INFO) && defined(H5_HAVE_GETTEXTINFO)
    {
 /* Borland C or DJGPPC */
 struct text_info w;
 gettextinfo(&w);
 width = w.screenwidth;
    }
#elif defined(H5_HAVE_GETCONSOLESCREENBUFFERINFO)
    {
 /* Win32 C */
 CONSOLE_SCREEN_BUFFER_INFO scr;
 GetConsoleScreenBufferInfo(con_out, &scr);
 width = scr.srWindow.Right - scr.srWindow.Left + 1;
    }
#elif defined(H5_HAVE__SCRSIZE)
    {
 /* OS/2 */
 int w[2];
 _scrsize(w);
 width = w[0];
    }
#elif defined(H5_HAVE_TIOCGWINSZ) && defined(H5_HAVE_IOCTL)
#ifndef __PUMAGON__
/* the ioctl() call coredump on TFLOPS.  Turn it off for now. */
    {
 /* Unix with ioctl(TIOCGWINSZ) */
 struct winsize w;
 if (ioctl(2, TIOCGWINSZ, &w)>=0 && w.ws_col>0) {
     width = w.ws_col;
 }
    }
#endif
#elif defined(H5_HAVE_TIOCGETD) && defined(H5_HAVE_IOCTL)
    {
 /* Unix with ioctl(TIOCGETD) */
 struct uwdata w;
 if (ioctl(2, WIOCGETD, &w)>=0 && w.uw_width>0) {
     width = w.uw_width / w.uw_hs;
 }
    }
#endif

    /* Set to at least 1 */
    if (width<1) width = 1;
    return width;
}


/*-------------------------------------------------------------------------
 * Function: leave
 *
 * Purpose: Close HDF5 and MPI and call exit()
 *
 * Return: Does not return
 *
 * Programmer: Quincey Koziol
 *              Saturday, January 31, 2004
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
leave(int ret)
{
    h5tools_close();
    
    exit(ret);
}


/*-------------------------------------------------------------------------
 * Function: main
 *
 * Purpose: Opens a file and lists the specified group
 *
 * Return: Success: 0
 *
 *  Failure: 1
 *
 * Programmer: Robb Matzke
 *              Monday, March 23, 1998
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
int
main (int argc, const char *argv[])
{
    hid_t file=-1, root=-1;
    char *fname=NULL, *oname=NULL, *x;
    const char *s = NULL;
    char *rest, *container=NULL;
    int  argno;
    H5G_stat_t sb;
    iter_t iter;
    static char root_name[] = "/";
    char        drivername[50];
    const char                *preferred_driver=NULL;

    /* Initialize h5tools lib */
    h5tools_init();

    /* Build display table */
    DISPATCH(H5G_DATASET, "Dataset", H5Dopen, H5Dclose,
      dataset_list1, dataset_list2);
    DISPATCH(H5G_GROUP, "Group", H5Gopen, H5Gclose,
      NULL, group_list2);
    DISPATCH(H5G_TYPE, "Type", H5Topen, H5Tclose,
      NULL, datatype_list2);
    DISPATCH(H5G_LINK, "-> ", link_open, NULL,
      NULL, NULL);

#if 0
    /* Name of this program without the path */
    if ((progname=strrchr(argv[0], '/'))) progname++;
    else progname = argv[0];
#endif

    /* Default output width */
    width_g = get_width();

    /* Switches come before non-switch arguments */
    for (argno=1; argno<argc && '-'==argv[argno][0]; argno++) {
        if (!strcmp(argv[argno], "--")) {
            /* Last switch */
            argno++;
            break;
        } else if (!strcmp(argv[argno], "--help")) {
            usage();
            leave(0);
        } else if (!strcmp(argv[argno], "--address")) {
            address_g = TRUE;
        } else if (!strcmp(argv[argno], "--data")) {
            data_g = TRUE;
        } else if (!strcmp(argv[argno], "--errors")) {
            show_errors_g = TRUE;
        } else if (!strcmp(argv[argno], "--full")) {
            fullname_g = TRUE;
        } else if (!strcmp(argv[argno], "--group")) {
            grp_literal_g = TRUE; 
        } else if (!strcmp(argv[argno], "--label")) {
            label_g = TRUE;
        } else if (!strcmp(argv[argno], "--recursive")) {
            recursive_g = TRUE;
            fullname_g = TRUE;
        } else if (!strcmp(argv[argno], "--simple")) {
            simple_output_g = TRUE;
        } else if (!strcmp(argv[argno], "--string")) {
            string_g = TRUE;
        } else if (!strncmp(argv[argno], "--vfd=", 6)) {
            preferred_driver = argv[argno]+6;
        } else if (!strncmp(argv[argno], "--width=", 8)) {
            width_g = (int)strtol(argv[argno]+8, &rest, 0);
            if (width_g<=0 || *rest) {
                usage();
                leave(1);
            }
        } else if (!strcmp(argv[argno], "--width")) {
            if (argno+1>=argc) {
                usage();
                leave(1);
            } else {
                s = argv[++argno];
            }
            width_g = (int)strtol(s, &rest, 0);
            if (width_g<=0 || *rest) {
                usage();
                leave(1);
            }
        } else if (!strcmp(argv[argno], "--verbose")) {
            verbose_g++;
        } else if (!strcmp(argv[argno], "--version")) {
            print_version(progname);
            leave(0);
        } else if (!strcmp(argv[argno], "--hexdump")) {
            hexdump_g = TRUE;
        } else if (!strncmp(argv[argno], "-w", 2)) {
            if (argv[argno][2]) {
                s = argv[argno]+2;
            } else if (argno+1>=argc) {
                usage();
                leave(1);
            } else {
                s = argv[++argno];
            }
            width_g = (int)strtol(s, &rest, 0);
            if (width_g<=0 || *rest) {
                usage();
                leave(1);
            }
        } else if ('-'!=argv[argno][1]) {
            /* Single-letter switches */
            for (s=argv[argno]+1; *s; s++) {
                switch (*s) {
                    case '?':
                    case 'h': /* --help */
                        usage();
                        leave(0);
                    case 'a': /* --address */
                        address_g = TRUE;
                        break;
                    case 'd': /* --data */
                        data_g = TRUE;
                        break;
                    case 'e': /* --errors */
                        show_errors_g = TRUE;
                        break;
                    case 'f': /* --full */
                        fullname_g = TRUE;
                        break;
                    case 'g': /* --group */
                        grp_literal_g = TRUE;
                        break;
                    case 'l': /* --label */
                        label_g = TRUE;
                        break;
                    case 'r': /* --recursive */
                        recursive_g = TRUE;
                        fullname_g = TRUE;
                        break;
                    case 'S': /* --simple */
                        simple_output_g = TRUE;
                        break;
                    case 's': /* --string */
                        string_g = TRUE;
                        break;
                    case 'v': /* --verbose */
                        verbose_g++;
                        break;
                    case 'V': /* --version */
                        print_version(progname);
                        leave(0);
                    case 'x': /* --hexdump */
                        hexdump_g = TRUE;
                        break;
                    default:
                        usage();
                        leave(1);
                }
            }
        } else {
            usage();
            leave(1);
        }
    }

    /* If no arguments remain then print a usage message (instead of doing
     * absolutely nothing ;-) */
    if (argno>=argc) {
        usage();
        leave(1);
    }

    /* Turn off HDF5's automatic error printing unless you're debugging h5ls */
    if (!show_errors_g) H5Eset_auto(NULL, NULL);


    /* Each remaining argument is an hdf5 file followed by an optional slash
     * and object name.
     *
     * Example: ../dir1/foo/bar/baz
     *          \_________/\______/
     *             file       obj
     *
     * The dichotomy is determined by calling H5Fopen() repeatedly until it
     * succeeds. The first call uses the entire name and each subsequent call
     * chops off the last component. If we reach the beginning of the name
     * then there must have been something wrong with the file (perhaps it
     * doesn't exist). */
    show_file_name_g = (argc-argno > 1); /*show file names if more than one*/
    while (argno<argc) {
        fname = HDstrdup(argv[argno++]);
        oname = NULL;
        file = -1;

        while (fname && *fname) {
            file = h5tools_fopen(fname, preferred_driver, drivername, sizeof drivername, argc, argv);

            if (file>=0) {
                if (verbose_g) {
                    printf("Opened \"%s\" with %s driver.\n",
                    fname, drivername);
                }
                break; /*success*/
            }

            /* Shorten the file name; lengthen the object name */
            x = oname;
            oname = strrchr(fname, '/');
            if (x) *x = '/';
            if (!oname) break;
            *oname = '\0';
        }
        if (file<0) {
            fprintf(stderr, "%s: unable to open file\n", argv[argno-1]);
            continue;
        }
        if (oname) oname++;
        if (!oname || !*oname) oname = root_name;

        /* Open the object and display it's information */
        if (H5Gget_objinfo(file, oname, TRUE, &sb)>=0 &&
                H5G_GROUP==sb.type && !grp_literal_g) {
            /* Specified name is a group. List the complete contents of the
             * group. */
            sym_insert(&sb, oname);
            iter.container = container = fix_name(show_file_name_g?fname:"", oname);
            H5Giterate(file, oname, NULL, list, &iter);
            free(container);

        } else if ((root=H5Gopen(file, "/"))<0) {
            leave(1); /*major problem!*/

        } else {
            /* Specified name is a non-group object -- list that object.  The
            * container for the object is everything up to the base name. */
            iter.container = show_file_name_g ? fname : "/";
            list(root, oname, &iter);
            if (H5Gclose(root)<0) leave(1);
        }
        H5Fclose(file);
        free(fname);
    }
    leave(0);
}