summaryrefslogtreecommitdiff
path: root/src/od.c
blob: 856a837d7ab62c7243347f09ef9189fe8f6c6d6d (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
/* od -- dump files in octal and other formats
   Copyright (C) 92, 95, 1996 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Written by Jim Meyering.  */

/* AIX requires this to be the first thing in the file.  */
#include <config.h>

#ifdef __GNUC__
#define alloca __builtin_alloca
#else /* not __GNUC__ */
#if HAVE_ALLOCA_H
#include <alloca.h>
#else /* not HAVE_ALLOCA_H */
#ifdef _AIX
 #pragma alloca
#else /* not _AIX */
#ifdef _WIN32
#include <malloc.h>
#include <io.h>
#else
char *alloca ();
#endif /* not _WIN32 */
#endif /* not _AIX */
#endif /* not HAVE_ALLOCA_H */
#endif /* not __GNUC__ */

#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include "xstrtoul.h"
#include "error.h"

#if defined(__GNUC__) || defined(STDC_HEADERS)
#include <float.h>
#endif

#ifdef HAVE_LONG_DOUBLE
typedef long double LONG_DOUBLE;
#else
typedef double LONG_DOUBLE;
#endif

#if HAVE_LIMITS_H
# include <limits.h>
#endif
#ifndef SCHAR_MAX
# define SCHAR_MAX 127
#endif
#ifndef SCHAR_MIN
# define SCHAR_MIN (-128)
#endif
#ifndef SHRT_MAX
# define SHRT_MAX 32767
#endif
#ifndef SHRT_MIN
# define SHRT_MIN (-32768)
#endif
#ifndef ULONG_MAX
# define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
#endif

#if HAVE_VALUES_H
# include <values.h>
#endif

#ifndef BITSPERBYTE
# define BITSPERBYTE 8
#endif

#ifndef OFF_T_MAX
# define OFF_T_MAX ((off_t)(~((off_t)1 << (sizeof (off_t) * BITSPERBYTE - 1))))
#endif

#define STREQ(a,b) (strcmp((a), (b)) == 0)

#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif

#if defined (_WIN32) && defined (_O_BINARY)
# define WINDOWS_SETFILEMODE_BINARY(IN_STREAM, FILENAME)		\
    do									\
      { /* Turn off DOS text file modes, "rb" doesn't work on stdin.  */\
	if (_setmode (_fileno ((IN_STREAM)), _O_BINARY) == -1)		\
          {								\
	    error (0, errno, "%s", (FILENAME));				\
	    err = 1;							\
	    continue;							\
	  }								\
      }									\
    while (0)
#else
# define WINDOWS_SETFILEMODE_BINARY(IN_STREAM, FILENAME) /* empty */
#endif

/* The default number of input bytes per output line.  */
#define DEFAULT_BYTES_PER_BLOCK 16

/* The number of decimal digits of precision in a float.  */
#ifndef FLT_DIG
# define FLT_DIG 7
#endif

/* The number of decimal digits of precision in a double.  */
#ifndef DBL_DIG
# define DBL_DIG 15
#endif

/* The number of decimal digits of precision in a long double.  */
#ifndef LDBL_DIG
# define LDBL_DIG DBL_DIG
#endif

char *xmalloc ();
char *xrealloc ();

enum size_spec
  {
    NO_SIZE,
    CHAR,
    SHORT,
    INT,
    LONG,
    FLOAT_SINGLE,
    FLOAT_DOUBLE,
    FLOAT_LONG_DOUBLE
  };

enum output_format
  {
    SIGNED_DECIMAL,
    UNSIGNED_DECIMAL,
    OCTAL,
    HEXADECIMAL,
    FLOATING_POINT,
    NAMED_CHARACTER,
    CHARACTER
  };

/* Each output format specification (from POSIX `-t spec' or from
   old-style options) is represented by one of these structures.  */
struct tspec
  {
    enum output_format fmt;
    enum size_spec size;
    void (*print_function) ();
    char *fmt_string;
  };

/* The name this program was run with.  */
char *program_name;

/* Convert the number of 8-bit bytes of a binary representation to
   the number of characters (digits + sign if the type is signed)
   required to represent the same quantity in the specified base/type.
   For example, a 32-bit (4-byte) quantity may require a field width
   as wide as the following for these types:
   11	unsigned octal
   11	signed decimal
   10	unsigned decimal
   8	unsigned hexadecimal  */

static const unsigned int bytes_to_oct_digits[] =
{0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};

static const unsigned int bytes_to_signed_dec_digits[] =
{1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};

static const unsigned int bytes_to_unsigned_dec_digits[] =
{0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};

static const unsigned int bytes_to_hex_digits[] =
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};

/* Convert enum size_spec to the size of the named type.  */
static const int width_bytes[] =
{
  -1,
  sizeof (char),
  sizeof (short int),
  sizeof (int),
  sizeof (long int),
  sizeof (float),
  sizeof (double),
  sizeof (LONG_DOUBLE)
};

/* Names for some non-printing characters.  */
static const char *const charname[33] =
{
  "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
  "bs", "ht", "nl", "vt", "ff", "cr", "so", "si",
  "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
  "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
  "sp"
};

/* A printf control string for printing a file offset.  */
static const char *output_address_fmt_string;

/* FIXME: make this the number of octal digits in an unsigned long.  */
#define MAX_ADDRESS_LENGTH 13

/* Space for a normal address, a space, a pseudo address, parentheses
   around the pseudo address, and a trailing zero byte. */
static char address_fmt_buffer[2 * MAX_ADDRESS_LENGTH + 4];
static char address_pad[MAX_ADDRESS_LENGTH + 1];

static unsigned long int string_min;
static unsigned long int flag_dump_strings;

/* Non-zero if we should recognize the pre-POSIX non-option arguments
   that specified at most one file and optional arguments specifying
   offset and pseudo-start address.  */
static int traditional;

/* Non-zero if an old-style `pseudo-address' was specified.  */
static long int flag_pseudo_start;

/* The difference between the old-style pseudo starting address and
   the number of bytes to skip.  */
static long int pseudo_offset;

/* Function to format an address and optionally an additional parenthesized
   pseudo-address; it returns the formatted string.  */
static const char *(*format_address) __P ((long unsigned int));

/* The number of input bytes to skip before formatting and writing.  */
static off_t n_bytes_to_skip = 0;

/* When nonzero, MAX_BYTES_TO_FORMAT is the maximum number of bytes
   to be read and formatted.  Otherwise all input is formatted.  */
static int limit_bytes_to_format = 0;

/* The maximum number of bytes that will be formatted.  This
   value is used only when LIMIT_BYTES_TO_FORMAT is nonzero.  */
static off_t max_bytes_to_format;

/* When nonzero and two or more consecutive blocks are equal, format
   only the first block and output an asterisk alone on the following
   line to indicate that identical blocks have been elided.  */
static int abbreviate_duplicate_blocks = 1;

/* An array of specs describing how to format each input block.  */
static struct tspec *spec;

/* The number of format specs.  */
static unsigned int n_specs;

/* The allocated length of SPEC.  */
static unsigned int n_specs_allocated;

/* The number of input bytes formatted per output line.  It must be
   a multiple of the least common multiple of the sizes associated with
   the specified output types.  It should be as large as possible, but
   no larger than 16 -- unless specified with the -w option.  */
static unsigned int bytes_per_block;

/* Human-readable representation of *file_list (for error messages).
   It differs from *file_list only when *file_list is "-".  */
static char const *input_filename;

/* A NULL-terminated list of the file-arguments from the command line.
   If no file-arguments were specified, this variable is initialized
   to { "-", NULL }.  */
static char const *const *file_list;

/* The input stream associated with the current file.  */
static FILE *in_stream;

/* If nonzero, at least one of the files we read was standard input.  */
static int have_read_stdin;

#define LONGEST_INTEGRAL_TYPE long int

#define MAX_INTEGRAL_TYPE_SIZE sizeof(LONGEST_INTEGRAL_TYPE)
static enum size_spec integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1];

#define MAX_FP_TYPE_SIZE sizeof(LONG_DOUBLE)
static enum size_spec fp_type_size[MAX_FP_TYPE_SIZE + 1];

/* If nonzero, display usage information and exit.  */
static int show_help;

/* If nonzero, print the version on standard output then exit.  */
static int show_version;

static struct option const long_options[] =
{
  /* POSIX options.  */
  {"skip-bytes", required_argument, NULL, 'j'},
  {"address-radix", required_argument, NULL, 'A'},
  {"read-bytes", required_argument, NULL, 'N'},
  {"format", required_argument, NULL, 't'},
  {"output-duplicates", no_argument, NULL, 'v'},

  /* non-POSIX options.  */
  {"strings", optional_argument, NULL, 's'},
  {"traditional", no_argument, NULL, 'B'},
  {"width", optional_argument, NULL, 'w'},
  {"help", no_argument, &show_help, 1},
  {"version", no_argument, &show_version, 1},
  {NULL, 0, NULL, 0}
};

static void
usage (int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
	     program_name);
  else
    {
      printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
  or:  %s --traditional [FILE] [[+]OFFSET [[+]LABEL]]\n\
"),
	      program_name, program_name);
      printf (_("\
Write an unambiguous representation, octal bytes by default, of FILE\n\
to standard output.  With no FILE, or when FILE is -, read standard input.\n\
\n\
  -A, --address-radix=RADIX   decide how file offsets are printed\n\
  -N, --read-bytes=BYTES      limit dump to BYTES input bytes per file\n\
  -j, --skip-bytes=BYTES      skip BYTES input bytes first on each file\n\
  -s, --strings[=BYTES]       output strings of at least BYTES graphic chars\n\
  -t, --format=TYPE           select output format or formats\n\
  -v, --output-duplicates     do not use * to mark line suppression\n\
  -w, --width[=BYTES]         output BYTES bytes per output line\n\
      --traditional           accept arguments in pre-POSIX form\n\
      --help                  display this help and exit\n\
      --version               output version information and exit\n\
\n\
Pre-POSIX format specifications may be intermixed, they accumulate:\n\
  -a   same as -t a,  select named characters\n\
  -b   same as -t oC, select octal bytes\n\
  -c   same as -t c,  select ASCII characters or backslash escapes\n\
  -d   same as -t u2, select unsigned decimal shorts\n\
  -f   same as -t fF, select floats\n\
  -h   same as -t x2, select hexadecimal shorts\n\
  -i   same as -t d2, select decimal shorts\n\
  -l   same as -t d4, select decimal longs\n\
  -o   same as -t o2, select octal shorts\n\
  -x   same as -t x2, select hexadecimal shorts\n\
"));
      printf (_("\
\n\
For older syntax (second call format), OFFSET means -j OFFSET.  LABEL\n\
is the pseudo-address at first byte printed, incremented when dump is\n\
progressing.  For OFFSET and LABEL, a 0x or 0X prefix indicates\n\
hexadecimal, suffixes maybe . for octal and b multiply by 512.\n\
\n\
TYPE is made up of one or more of these specifications:\n\
\n\
  a          named character\n\
  c          ASCII character or backslash escape\n\
  d[SIZE]    signed decimal, SIZE bytes per integer\n\
  f[SIZE]    floating point, SIZE bytes per integer\n\
  o[SIZE]    octal, SIZE bytes per integer\n\
  u[SIZE]    unsigned decimal, SIZE bytes per integer\n\
  x[SIZE]    hexadecimal, SIZE bytes per integer\n\
\n\
SIZE is a number.  For TYPE in doux, SIZE may also be C for\n\
sizeof(char), S for sizeof(short), I for sizeof(int) or L for\n\
sizeof(long).  If TYPE is f, SIZE may also be F for sizeof(float), D\n\
for sizeof(double) or L for sizeof(long double).\n\
\n\
RADIX is d for decimal, o for octal, x for hexadecimal or n for none.\n\
BYTES is hexadecimal with 0x or 0X prefix, it is multiplied by 512\n\
with b suffix, by 1024 with k and by 1048576 with m.  -s without a\n\
number implies 3.  -w without a number implies 32.  By default, od\n\
uses -A o -t d2 -w 16.\n\
"));
    }
  exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

/* Compute the greatest common denominator of U and V
   using Euclid's algorithm.  */

static unsigned int
gcd (unsigned int u, unsigned int v)
{
  unsigned int t;
  while (v != 0)
    {
      t = u % v;
      u = v;
      v = t;
    }
  return u;
}

/* Compute the least common multiple of U and V.  */

static unsigned int
lcm (unsigned int u, unsigned int v)
{
  unsigned int t = gcd (u, v);
  if (t == 0)
    return 0;
  return u * v / t;
}

static void
print_s_char (long unsigned int n_bytes, const char *block,
	      const char *fmt_string)
{
  int i;
  for (i = n_bytes; i > 0; i--)
    {
      int tmp = (unsigned) *(const unsigned char *) block;
      if (tmp > SCHAR_MAX)
	tmp -= SCHAR_MAX - SCHAR_MIN + 1;
      assert (tmp <= SCHAR_MAX);
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned char);
    }
}

static void
print_char (long unsigned int n_bytes, const char *block,
	    const char *fmt_string)
{
  int i;
  for (i = n_bytes; i > 0; i--)
    {
      unsigned int tmp = *(const unsigned char *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned char);
    }
}

static void
print_s_short (long unsigned int n_bytes, const char *block,
	       const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (unsigned short); i > 0; i--)
    {
      int tmp = (unsigned) *(const unsigned short *) block;
      if (tmp > SHRT_MAX)
	tmp -= SHRT_MAX - SHRT_MIN + 1;
      assert (tmp <= SHRT_MAX);
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned short);
    }
}
static void
print_short (long unsigned int n_bytes, const char *block,
	     const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (unsigned short); i > 0; i--)
    {
      unsigned int tmp = *(const unsigned short *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned short);
    }
}

static void
print_int (long unsigned int n_bytes, const char *block,
	   const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (unsigned int); i > 0; i--)
    {
      unsigned int tmp = *(const unsigned int *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned int);
    }
}

static void
print_long (long unsigned int n_bytes, const char *block,
	    const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (unsigned long); i > 0; i--)
    {
      unsigned long tmp = *(const unsigned long *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned long);
    }
}

static void
print_float (long unsigned int n_bytes, const char *block,
	     const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (float); i > 0; i--)
    {
      float tmp = *(const float *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (float);
    }
}

static void
print_double (long unsigned int n_bytes, const char *block,
	      const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (double); i > 0; i--)
    {
      double tmp = *(const double *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (double);
    }
}

#ifdef HAVE_LONG_DOUBLE
static void
print_long_double (long unsigned int n_bytes, const char *block,
		   const char *fmt_string)
{
  int i;
  for (i = n_bytes / sizeof (LONG_DOUBLE); i > 0; i--)
    {
      LONG_DOUBLE tmp = *(const LONG_DOUBLE *) block;
      printf (fmt_string, tmp, (i == 1 ? '\n' : ' '));
      block += sizeof (LONG_DOUBLE);
    }
}

#endif

static void
print_named_ascii (long unsigned int n_bytes, const char *block,
		   const char *unused_fmt_string)
{
  int i;
  for (i = n_bytes; i > 0; i--)
    {
      unsigned int c = *(const unsigned char *) block;
      unsigned int masked_c = (0x7f & c);
      const char *s;
      char buf[5];

      if (masked_c == 127)
	s = "del";
      else if (masked_c <= 040)
	s = charname[masked_c];
      else
	{
	  sprintf (buf, "  %c", masked_c);
	  s = buf;
	}

      printf ("%3s%c", s, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned char);
    }
}

static void
print_ascii (long unsigned int n_bytes, const char *block,
	     const char *unused_fmt_string)
{
  int i;
  for (i = n_bytes; i > 0; i--)
    {
      unsigned int c = *(const unsigned char *) block;
      const char *s;
      char buf[5];

      switch (c)
	{
	case '\0':
	  s = " \\0";
	  break;

	case '\007':
	  s = " \\a";
	  break;

	case '\b':
	  s = " \\b";
	  break;

	case '\f':
	  s = " \\f";
	  break;

	case '\n':
	  s = " \\n";
	  break;

	case '\r':
	  s = " \\r";
	  break;

	case '\t':
	  s = " \\t";
	  break;

	case '\v':
	  s = " \\v";
	  break;

	default:
	  sprintf (buf, (ISPRINT (c) ? "  %c" : "%03o"), c);
	  s = (const char *) buf;
	}

      printf ("%3s%c", s, (i == 1 ? '\n' : ' '));
      block += sizeof (unsigned char);
    }
}

/* Convert a null-terminated (possibly zero-length) string S to an
   unsigned long integer value.  If S points to a non-digit set *P to S,
   *VAL to 0, and return 0.  Otherwise, accumulate the integer value of
   the string of digits.  If the string of digits represents a value
   larger than ULONG_MAX, don't modify *VAL or *P and return nonzero.
   Otherwise, advance *P to the first non-digit after S, set *VAL to
   the result of the conversion and return zero.  */

static int
simple_strtoul (const char *s, const char **p, long unsigned int *val)
{
  unsigned long int sum;

  sum = 0;
  while (ISDIGIT (*s))
    {
      unsigned int c = *s++ - '0';
      if (sum > (ULONG_MAX - c) / 10)
	return 1;
      sum = sum * 10 + c;
    }
  *p = s;
  *val = sum;
  return 0;
}

/* If S points to a single valid POSIX-style od format string, put
   a description of that format in *TSPEC, make *NEXT point at the
   character following the just-decoded format (if *NEXT is non-NULL),
   and return zero.  If S is not valid, don't modify *NEXT or *TSPEC,
   give a diagnostic, and return nonzero.  For example, if S were
   "d4afL" *NEXT would be set to "afL" and *TSPEC would be
     {
       fmt = SIGNED_DECIMAL;
       size = INT or LONG; (whichever integral_type_size[4] resolves to)
       print_function = print_int; (assuming size == INT)
       fmt_string = "%011d%c";
      }
   S_ORIG is solely for reporting errors.  It should be the full format
   string argument.
   */

static int
decode_one_format (const char *s_orig, const char *s, const char **next,
		   struct tspec *tspec)
{
  enum size_spec size_spec;
  unsigned long int size;
  enum output_format fmt;
  const char *pre_fmt_string;
  char *fmt_string;
  void (*print_function) ();
  const char *p;
  unsigned int c;

  assert (tspec != NULL);

  switch (*s)
    {
    case 'd':
    case 'o':
    case 'u':
    case 'x':
      c = *s;
      ++s;
      switch (*s)
	{
	case 'C':
	  ++s;
	  size = sizeof (char);
	  break;

	case 'S':
	  ++s;
	  size = sizeof (short);
	  break;

	case 'I':
	  ++s;
	  size = sizeof (int);
	  break;

	case 'L':
	  ++s;
	  size = sizeof (long int);
	  break;

	default:
	  if (simple_strtoul (s, &p, &size) != 0)
	    {
	      /* The integer at P in S would overflow an unsigned long.
		 A digit string that long is sufficiently odd looking
		 that the following diagnostic is sufficient.  */
	      error (0, 0, _("invalid type string `%s'"), s_orig);
	      return 1;
	    }
	  if (p == s)
	    size = sizeof (int);
	  else
	    {
	      if (size > MAX_INTEGRAL_TYPE_SIZE
		  || integral_type_size[size] == NO_SIZE)
		{
		  error (0, 0, _("invalid type string `%s';\n\
this system doesn't provide a %lu-byte integral type"), s_orig, size);
		  return 1;
		}
	      s = p;
	    }
	  break;
	}

#define FMT_BYTES_ALLOCATED 9
      fmt_string = xmalloc (FMT_BYTES_ALLOCATED);

      size_spec = integral_type_size[size];

      switch (c)
	{
	case 'd':
	  fmt = SIGNED_DECIMAL;
	  sprintf (fmt_string, "%%%u%sd%%c",
		   bytes_to_signed_dec_digits[size],
		   (size_spec == LONG ? "l" : ""));
	  break;

	case 'o':
	  fmt = OCTAL;
	  sprintf (fmt_string, "%%0%u%so%%c",
		   bytes_to_oct_digits[size],
		   (size_spec == LONG ? "l" : ""));
	  break;

	case 'u':
	  fmt = UNSIGNED_DECIMAL;
	  sprintf (fmt_string, "%%%u%su%%c",
		   bytes_to_unsigned_dec_digits[size],
		   (size_spec == LONG ? "l" : ""));
	  break;

	case 'x':
	  fmt = HEXADECIMAL;
	  sprintf (fmt_string, "%%0%u%sx%%c",
		   bytes_to_hex_digits[size],
		   (size_spec == LONG ? "l" : ""));
	  break;

	default:
	  abort ();
	}

      assert (strlen (fmt_string) < FMT_BYTES_ALLOCATED);

      switch (size_spec)
	{
	case CHAR:
	  print_function = (fmt == SIGNED_DECIMAL
			    ? print_s_char
			    : print_char);
	  break;

	case SHORT:
	  print_function = (fmt == SIGNED_DECIMAL
			    ? print_s_short
			    : print_short);
	  break;

	case INT:
	  print_function = print_int;
	  break;

	case LONG:
	  print_function = print_long;
	  break;

	default:
	  abort ();
	}
      break;

    case 'f':
      fmt = FLOATING_POINT;
      ++s;
      switch (*s)
	{
	case 'F':
	  ++s;
	  size = sizeof (float);
	  break;

	case 'D':
	  ++s;
	  size = sizeof (double);
	  break;

	case 'L':
	  ++s;
	  size = sizeof (LONG_DOUBLE);
	  break;

	default:
	  if (simple_strtoul (s, &p, &size) != 0)
	    {
	      /* The integer at P in S would overflow an unsigned long.
		 A digit string that long is sufficiently odd looking
		 that the following diagnostic is sufficient.  */
	      error (0, 0, _("invalid type string `%s'"), s_orig);
	      return 1;
	    }
	  if (p == s)
	    size = sizeof (double);
	  else
	    {
	      if (size > MAX_FP_TYPE_SIZE
		  || fp_type_size[size] == NO_SIZE)
		{
		  error (0, 0, _("invalid type string `%s';\n\
this system doesn't provide a %lu-byte floating point type"), s_orig, size);
		  return 1;
		}
	      s = p;
	    }
	  break;
	}
      size_spec = fp_type_size[size];

      switch (size_spec)
	{
	case FLOAT_SINGLE:
	  print_function = print_float;
	  /* Don't use %#e; not all systems support it.  */
	  pre_fmt_string = "%%%d.%de%%c";
	  fmt_string = xmalloc (strlen (pre_fmt_string));
	  sprintf (fmt_string, pre_fmt_string,
		   FLT_DIG + 8, FLT_DIG);
	  break;

	case FLOAT_DOUBLE:
	  print_function = print_double;
	  pre_fmt_string = "%%%d.%de%%c";
	  fmt_string = xmalloc (strlen (pre_fmt_string));
	  sprintf (fmt_string, pre_fmt_string,
		   DBL_DIG + 8, DBL_DIG);
	  break;

#ifdef HAVE_LONG_DOUBLE
	case FLOAT_LONG_DOUBLE:
	  print_function = print_long_double;
	  pre_fmt_string = "%%%d.%dLe%%c";
	  fmt_string = xmalloc (strlen (pre_fmt_string));
	  sprintf (fmt_string, pre_fmt_string,
		   LDBL_DIG + 8, LDBL_DIG);
	  break;
#endif

	default:
	  abort ();
	}
      break;

    case 'a':
      ++s;
      fmt = NAMED_CHARACTER;
      size_spec = CHAR;
      fmt_string = NULL;
      print_function = print_named_ascii;
      break;

    case 'c':
      ++s;
      fmt = CHARACTER;
      size_spec = CHAR;
      fmt_string = NULL;
      print_function = print_ascii;
      break;

    default:
      error (0, 0, _("invalid character `%c' in type string `%s'"),
	     *s, s_orig);
      return 1;
    }

  tspec->size = size_spec;
  tspec->fmt = fmt;
  tspec->print_function = print_function;
  tspec->fmt_string = fmt_string;

  if (next != NULL)
    *next = s;

  return 0;
}

/* Decode the POSIX-style od format string S.  Append the decoded
   representation to the global array SPEC, reallocating SPEC if
   necessary.  Return zero if S is valid, nonzero otherwise.  */

static int
decode_format_string (const char *s)
{
  const char *s_orig = s;
  assert (s != NULL);

  while (*s != '\0')
    {
      struct tspec tspec;
      const char *next;

      if (decode_one_format (s_orig, s, &next, &tspec))
	return 1;

      assert (s != next);
      s = next;

      if (n_specs >= n_specs_allocated)
	{
	  n_specs_allocated = 1 + (3 * n_specs_allocated) / 2;
	  spec = (struct tspec *) xrealloc (spec, (n_specs_allocated
						   * sizeof (struct tspec)));
	}

      memcpy ((char *) &spec[n_specs], (char *) &tspec,
	      sizeof (struct tspec));
      ++n_specs;
    }

  return 0;
}

/* Given a list of one or more input filenames FILE_LIST, set the global
   file pointer IN_STREAM to position N_SKIP in the concatenation of
   those files.  If any file operation fails or if there are fewer than
   N_SKIP bytes in the combined input, give an error message and return
   nonzero.  When possible, use seek- rather than read operations to
   advance IN_STREAM.  A file name of "-" is interpreted as standard
   input.  */

static int
skip (off_t n_skip)
{
  int err;

  err = 0;
  for ( /* empty */ ; *file_list != NULL; ++file_list)
    {
      struct stat file_stats;
      int j;

      if (STREQ (*file_list, "-"))
	{
	  input_filename = _("standard input");
	  in_stream = stdin;
	  have_read_stdin = 1;
	}
      else
	{
	  input_filename = *file_list;
	  in_stream = fopen (input_filename, "r");
	  if (in_stream == NULL)
	    {
	      error (0, errno, "%s", input_filename);
	      err = 1;
	      continue;
	    }
	}
      WINDOWS_SETFILEMODE_BINARY (in_stream, input_filename);

      if (n_skip == 0)
	break;

      /* First try using fseek.  For large offsets, this extra work is
	 worthwhile.  If the offset is below some threshold it may be
	 more efficient to move the pointer by reading.  There are two
	 issues when trying to use fseek:
	   - the file must be seekable.
	   - before seeking to the specified position, make sure
	     that the new position is in the current file.
	     Try to do that by getting file's size using fstat().
	     But that will work only for regular files and dirs.  */

      if (fstat (fileno (in_stream), &file_stats))
	{
	  error (0, errno, "%s", input_filename);
	  err = 1;
	  continue;
	}

      /* The st_size field is valid only for regular files and
	 directories.  FIXME: is the preceding true?
	 If the number of bytes left to skip is at least as large as
	 the size of the current file, we can decrement
	 n_skip and go on to the next file.  */
      if (S_ISREG (file_stats.st_mode) || S_ISDIR (file_stats.st_mode))
	{
	  if (n_skip >= file_stats.st_size)
	    {
	      n_skip -= file_stats.st_size;
	      if (in_stream != stdin && fclose (in_stream) == EOF)
		{
		  error (0, errno, "%s", input_filename);
		  err = 1;
		}
	      continue;
	    }
	  else
	    {
	      /* fseek may work on some streams for which lseek doesn't.
		 But fseek's offset argument is restricted to the range
		 of type `long'.  So if N_SKIP is too large or if fseek
		 fails, try lseek.  */
	      if ((n_skip <= LONG_MAX
		   && fseek (in_stream, (long) n_skip, SEEK_SET) == 0)
		  || lseek (fileno (in_stream), n_skip, SEEK_SET) >= 0)
		{
		  n_skip = 0;
		  break;
		}
	    }
	}

      /* Seek didn't work or wasn't attempted;  position the file pointer
	 by reading.  */

      for (j = n_skip / BUFSIZ; j >= 0; j--)
	{
	  char buf[BUFSIZ];
	  size_t n_bytes_to_read = (j > 0
				    ? BUFSIZ
				    : n_skip % BUFSIZ);
	  size_t n_bytes_read;
	  n_bytes_read = fread (buf, 1, n_bytes_to_read, in_stream);
	  n_skip -= n_bytes_read;
	  if (n_bytes_read != n_bytes_to_read)
	    break;
	}

      if (n_skip == 0)
	break;
    }

  if (n_skip != 0)
    error (EXIT_FAILURE, 0, _("cannot skip past end of combined input"));

  return err;
}

static const char *
format_address_none (long unsigned int address)
{
  return "";
}

static const char *
format_address_std (long unsigned int address)
{
  const char *address_string;

  sprintf (address_fmt_buffer, output_address_fmt_string, address);
  address_string = address_fmt_buffer;
  return address_string;
}

static const char *
format_address_label (long unsigned int address)
{
  const char *address_string;
  assert (output_address_fmt_string != NULL);

  sprintf (address_fmt_buffer, output_address_fmt_string,
	   address, address + pseudo_offset);
  address_string = address_fmt_buffer;
  return address_string;
}

/* Write N_BYTES bytes from CURR_BLOCK to standard output once for each
   of the N_SPEC format specs.  CURRENT_OFFSET is the byte address of
   CURR_BLOCK in the concatenation of input files, and it is printed
   (optionally) only before the output line associated with the first
   format spec.  When duplicate blocks are being abbreviated, the output
   for a sequence of identical input blocks is the output for the first
   block followed by an asterisk alone on a line.  It is valid to compare
   the blocks PREV_BLOCK and CURR_BLOCK only when N_BYTES == BYTES_PER_BLOCK.
   That condition may be false only for the last input block -- and then
   only when it has not been padded to length BYTES_PER_BLOCK.  */

static void
write_block (long unsigned int current_offset, long unsigned int n_bytes,
	     const char *prev_block, const char *curr_block)
{
  static int first = 1;
  static int prev_pair_equal = 0;

#define EQUAL_BLOCKS(b1, b2) (memcmp ((b1), (b2), bytes_per_block) == 0)

  if (abbreviate_duplicate_blocks
      && !first && n_bytes == bytes_per_block
      && EQUAL_BLOCKS (prev_block, curr_block))
    {
      if (prev_pair_equal)
	{
	  /* The two preceding blocks were equal, and the current
	     block is the same as the last one, so print nothing.  */
	}
      else
	{
	  printf ("*\n");
	  prev_pair_equal = 1;
	}
    }
  else
    {
      unsigned int i;

      prev_pair_equal = 0;
      for (i = 0; i < n_specs; i++)
	{
	  printf ("%s ", (i == 0
			  ? format_address (current_offset)
			  : address_pad));
	  (*spec[i].print_function) (n_bytes, curr_block, spec[i].fmt_string);
	}
    }
  first = 0;
}

/* Test whether there have been errors on in_stream, and close it if
   it is not standard input.  Return nonzero if there has been an error
   on in_stream or stdout; return zero otherwise.  This function will
   report more than one error only if both a read and a write error
   have occurred.  */

static int
check_and_close (void)
{
  int err;

  err = 0;
  if (ferror (in_stream))
    {
      error (0, errno, "%s", input_filename);
      if (in_stream != stdin)
	fclose (in_stream);
      err = 1;
    }
  else if (in_stream != stdin && fclose (in_stream) == EOF)
    {
      error (0, errno, "%s", input_filename);
      err = 1;
    }

  if (ferror (stdout))
    {
      error (0, errno, _("standard output"));
      err = 1;
    }

  return err;
}

/* Read a single byte into *C from the concatenation of the input files
   named in the global array FILE_LIST.  On the first call to this
   function, the global variable IN_STREAM is expected to be an open
   stream associated with the input file *FILE_LIST.  If IN_STREAM is
   at end-of-file, close it and update the global variables IN_STREAM,
   FILE_LIST, and INPUT_FILENAME so they correspond to the next file in
   the list.  Then try to read a byte from the newly opened file.
   Repeat if necessary until *FILE_LIST is NULL.  When EOF is reached
   for the last file in FILE_LIST, set *C to EOF and return.  Subsequent
   calls do likewise.  The return value is nonzero if any errors
   occured, zero otherwise.  */

static int
read_char (int *c)
{
  int err;

  if (*file_list == NULL)
    {
      *c = EOF;
      return 0;
    }

  err = 0;
  while (1)
    {
      *c = fgetc (in_stream);

      if (*c != EOF)
	return err;

      err |= check_and_close ();

      do
	{
	  ++file_list;
	  if (*file_list == NULL)
	    return err;

	  if (STREQ (*file_list, "-"))
	    {
	      input_filename = _("standard input");
	      in_stream = stdin;
	      have_read_stdin = 1;
	    }
	  else
	    {
	      input_filename = *file_list;
	      in_stream = fopen (input_filename, "r");
	      if (in_stream == NULL)
		{
		  error (0, errno, "%s", input_filename);
		  err = 1;
		}
	    }
	  WINDOWS_SETFILEMODE_BINARY (in_stream, input_filename);
	}
      while (in_stream == NULL);
    }
}

/* Read N bytes into BLOCK from the concatenation of the input files
   named in the global array FILE_LIST.  On the first call to this
   function, the global variable IN_STREAM is expected to be an open
   stream associated with the input file *FILE_LIST.  On subsequent
   calls, if *FILE_LIST is NULL, don't modify BLOCK and return zero.
   If all N bytes cannot be read from IN_STREAM, close IN_STREAM and
   update the global variables IN_STREAM, FILE_LIST, and INPUT_FILENAME.
   Then try to read the remaining bytes from the newly opened file.
   Repeat if necessary until *FILE_LIST is NULL.  Set *N_BYTES_IN_BUFFER
   to the number of bytes read.  If an error occurs, it will be detected
   through ferror when the stream is about to be closed.  If there is an
   error, give a message but continue reading as usual and return nonzero.
   Otherwise return zero.  */

static int
read_block (size_t n, char *block, size_t *n_bytes_in_buffer)
{
  int err;

  assert (n > 0 && n <= bytes_per_block);

  *n_bytes_in_buffer = 0;

  if (n == 0)
    return 0;

  if (*file_list == NULL)
    return 0;			/* EOF.  */

  err = 0;
  while (1)
    {
      size_t n_needed;
      size_t n_read;

      n_needed = n - *n_bytes_in_buffer;
      n_read = fread (block + *n_bytes_in_buffer, 1, n_needed, in_stream);

      *n_bytes_in_buffer += n_read;

      if (n_read == n_needed)
	return err;

      err |= check_and_close ();

      do
	{
	  ++file_list;
	  if (*file_list == NULL)
	    return err;

	  if (STREQ (*file_list, "-"))
	    {
	      input_filename = _("standard input");
	      in_stream = stdin;
	      have_read_stdin = 1;
	    }
	  else
	    {
	      input_filename = *file_list;
	      in_stream = fopen (input_filename, "r");
	      if (in_stream == NULL)
		{
		  error (0, errno, "%s", input_filename);
		  err = 1;
		}
	    }
	  WINDOWS_SETFILEMODE_BINARY (in_stream, input_filename);
	}
      while (in_stream == NULL);
    }
}

/* Return the least common multiple of the sizes associated
   with the format specs.  */

static int
get_lcm (void)
{
  unsigned int i;
  int l_c_m = 1;

  for (i = 0; i < n_specs; i++)
    l_c_m = lcm (l_c_m, width_bytes[(int) spec[i].size]);
  return l_c_m;
}

/* If S is a valid pre-POSIX offset specification with an optional leading '+'
   return the offset it denotes.  Otherwise, return -1.  */

off_t
parse_old_offset (const char *s)
{
  int radix;
  off_t offset;
  enum strtol_error s_err;
  long unsigned int tmp;

  if (*s == '\0')
    return -1;

  /* Skip over any leading '+'. */
  if (s[0] == '+')
    ++s;

  /* Determine the radix we'll use to interpret S.  If there is a `.',
     it's decimal, otherwise, if the string begins with `0X'or `0x',
     it's hexadecimal, else octal.  */
  if (strchr (s, '.') != NULL)
    radix = 10;
  else
    {
      if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
	radix = 16;
      else
	radix = 8;
    }

  s_err = xstrtoul (s, NULL, radix, &tmp, "Bb");
  if (s_err != LONGINT_OK)
    {
      STRTOL_FAIL_WARN (s, _("old-style offset"), s_err);
      return -1;
    }
  offset = tmp;
  return offset;
}

/* Read a chunk of size BYTES_PER_BLOCK from the input files, write the
   formatted block to standard output, and repeat until the specified
   maximum number of bytes has been read or until all input has been
   processed.  If the last block read is smaller than BYTES_PER_BLOCK
   and its size is not a multiple of the size associated with a format
   spec, extend the input block with zero bytes until its length is a
   multiple of all format spec sizes.  Write the final block.  Finally,
   write on a line by itself the offset of the byte after the last byte
   read.  Accumulate return values from calls to read_block and
   check_and_close, and if any was nonzero, return nonzero.
   Otherwise, return zero.  */

static int
dump (void)
{
  char *block[2];
  off_t current_offset;
  off_t end_offset;
  int idx;
  int err;
  size_t n_bytes_read;

#ifdef lint  /* Suppress `used before initialized' warning.  */
  end_offset = 0;
#endif

  block[0] = (char *) alloca (bytes_per_block);
  block[1] = (char *) alloca (bytes_per_block);

  current_offset = n_bytes_to_skip;

  idx = 0;
  err = 0;
  if (limit_bytes_to_format)
    {
      end_offset = n_bytes_to_skip + max_bytes_to_format;

      while (1)
	{
	  size_t n_needed;
	  if (current_offset >= end_offset)
	    {
	      n_bytes_read = 0;
	      break;
	    }
	  n_needed = MIN (end_offset - current_offset,
			  (off_t) bytes_per_block);
	  err |= read_block (n_needed, block[idx], &n_bytes_read);
	  if (n_bytes_read < bytes_per_block)
	    break;
	  assert (n_bytes_read == bytes_per_block);
	  write_block (current_offset, n_bytes_read,
		       block[!idx], block[idx]);
	  current_offset += n_bytes_read;
	  idx = !idx;
	}
    }
  else
    {
      while (1)
	{
	  err |= read_block (bytes_per_block, block[idx], &n_bytes_read);
	  if (n_bytes_read < bytes_per_block)
	    break;
	  assert (n_bytes_read == bytes_per_block);
	  write_block (current_offset, n_bytes_read,
		       block[!idx], block[idx]);
	  current_offset += n_bytes_read;
	  idx = !idx;
	}
    }

  if (n_bytes_read > 0)
    {
      int l_c_m;
      size_t bytes_to_write;

      l_c_m = get_lcm ();

      /* Make bytes_to_write the smallest multiple of l_c_m that
	 is at least as large as n_bytes_read.  */
      bytes_to_write = l_c_m * (int) ((n_bytes_read + l_c_m - 1) / l_c_m);

      memset (block[idx] + n_bytes_read, 0, bytes_to_write - n_bytes_read);
      write_block (current_offset, bytes_to_write,
		   block[!idx], block[idx]);
      current_offset += n_bytes_read;
    }

  if (output_address_fmt_string != NULL)
    printf ("%s\n", format_address (current_offset));

  if (limit_bytes_to_format && current_offset > end_offset)
    err |= check_and_close ();

  return err;
}

/* STRINGS mode.  Find each "string constant" in the input.
   A string constant is a run of at least `string_min' ASCII
   graphic (or formatting) characters terminated by a null.
   Based on a function written by Richard Stallman for a
   pre-POSIX version of od.  Return nonzero if an error
   occurs.  Otherwise, return zero.  */

static int
dump_strings (void)
{
  size_t bufsize = MAX (100, string_min);
  char *buf = xmalloc (bufsize);
  off_t address = n_bytes_to_skip;
  int err;

  err = 0;
  while (1)
    {
      unsigned int i;
      int c;

      /* See if the next `string_min' chars are all printing chars.  */
    tryline:

      if (limit_bytes_to_format
	  && address >= (n_bytes_to_skip + max_bytes_to_format - string_min))
	break;

      for (i = 0; i < string_min; i++)
	{
	  err |= read_char (&c);
	  address++;
	  if (c < 0)
	    {
	      free (buf);
	      return err;
	    }
	  if (!ISPRINT (c))
	    /* Found a non-printing.  Try again starting with next char.  */
	    goto tryline;
	  buf[i] = c;
	}

      /* We found a run of `string_min' printable characters.
	 Now see if it is terminated with a null byte.  */
      while (!limit_bytes_to_format
	     || address < n_bytes_to_skip + max_bytes_to_format)
	{
	  if (i == bufsize)
	    {
	      bufsize = 1 + 3 * bufsize / 2;
	      buf = xrealloc (buf, bufsize);
	    }
	  err |= read_char (&c);
	  address++;
	  if (c < 0)
	    {
	      free (buf);
	      return err;
	    }
	  if (c == '\0')
	    break;		/* It is; print this string.  */
	  if (!ISPRINT (c))
	    goto tryline;	/* It isn't; give up on this string.  */
	  buf[i++] = c;		/* String continues; store it all.  */
	}

      /* If we get here, the string is all printable and null-terminated,
	 so print it.  It is all in `buf' and `i' is its length.  */
      buf[i] = 0;
      if (output_address_fmt_string != NULL)
	{
	  printf ("%s ", format_address (address - i - 1));
	}
      for (i = 0; (c = buf[i]); i++)
	{
	  switch (c)
	    {
	    case '\007':
	      fputs ("\\a", stdout);
	      break;

	    case '\b':
	      fputs ("\\b", stdout);
	      break;

	    case '\f':
	      fputs ("\\f", stdout);
	      break;

	    case '\n':
	      fputs ("\\n", stdout);
	      break;

	    case '\r':
	      fputs ("\\r", stdout);
	      break;

	    case '\t':
	      fputs ("\\t", stdout);
	      break;

	    case '\v':
	      fputs ("\\v", stdout);
	      break;

	    default:
	      putc (c, stdout);
	    }
	}
      putchar ('\n');
    }

  /* We reach this point only if we search through
     (max_bytes_to_format - string_min) bytes before reachine EOF.  */

  free (buf);

  err |= check_and_close ();
  return err;
}

int
main (int argc, char **argv)
{
  int c;
  int n_files;
  unsigned int i;
  unsigned int l_c_m;
  unsigned int address_pad_len;
  unsigned long int desired_width;
  int width_specified = 0;
  int n_failed_decodes = 0;
  int err;

  /* The old-style `pseudo starting address' to be printed in parentheses
     after any true address.  */
  long int pseudo_start;

#ifdef lint  /* Suppress `used before initialized' warning.  */
  pseudo_start = 0;
#endif

  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  err = 0;

  for (i = 0; i <= MAX_INTEGRAL_TYPE_SIZE; i++)
    integral_type_size[i] = NO_SIZE;

  integral_type_size[sizeof (char)] = CHAR;
  integral_type_size[sizeof (short int)] = SHORT;
  integral_type_size[sizeof (int)] = INT;
  integral_type_size[sizeof (long int)] = LONG;

  for (i = 0; i <= MAX_FP_TYPE_SIZE; i++)
    fp_type_size[i] = NO_SIZE;

  fp_type_size[sizeof (float)] = FLOAT_SINGLE;
  /* The array entry for `double' is filled in after that for LONG_DOUBLE
     so that if `long double' is the same type or if long double isn't
     supported FLOAT_LONG_DOUBLE will never be used.  */
  fp_type_size[sizeof (LONG_DOUBLE)] = FLOAT_LONG_DOUBLE;
  fp_type_size[sizeof (double)] = FLOAT_DOUBLE;

  n_specs = 0;
  n_specs_allocated = 5;
  spec = (struct tspec *) xmalloc (n_specs_allocated * sizeof (struct tspec));

  output_address_fmt_string = "%07o";
  format_address = format_address_std;
  address_pad_len = 7;
  flag_dump_strings = 0;

  while ((c = getopt_long (argc, argv, "abcdfhilos::xw::A:j:N:t:v",
			   long_options, (int *) 0))
	 != EOF)
    {
      unsigned long int tmp;
      enum strtol_error s_err;

      switch (c)
	{
	case 0:
	  break;

	case 'A':
	  switch (optarg[0])
	    {
	    case 'd':
	      output_address_fmt_string = "%07d";
	      format_address = format_address_std;
	      address_pad_len = 7;
	      break;
	    case 'o':
	      output_address_fmt_string = "%07o";
	      format_address = format_address_std;
	      address_pad_len = 7;
	      break;
	    case 'x':
	      output_address_fmt_string = "%06x";
	      format_address = format_address_std;
	      address_pad_len = 6;
	      break;
	    case 'n':
	      output_address_fmt_string = NULL;
	      format_address = format_address_none;
	      address_pad_len = 0;
	      break;
	    default:
	      error (EXIT_FAILURE, 0,
		     _("invalid output address radix `%c'; \
it must be one character from [doxn]"),
		     optarg[0]);
	      break;
	    }
	  break;

	case 'j':
	  s_err = xstrtoul (optarg, NULL, 0, &tmp, "bkm");
	  n_bytes_to_skip = tmp;
	  if (s_err != LONGINT_OK)
	    STRTOL_FATAL_ERROR (optarg, _("skip argument"), s_err);
	  break;

	case 'N':
	  limit_bytes_to_format = 1;

	  /* FIXME: if off_t is long long and that's an 8-byte type,
	     use xstrtouq here.  */
	  s_err = xstrtoul (optarg, NULL, 0, &tmp, "bkm");
	  max_bytes_to_format = tmp;
	  if (s_err != LONGINT_OK)
	    STRTOL_FATAL_ERROR (optarg, _("limit argument"), s_err);

	  if (tmp > OFF_T_MAX)
	    error (EXIT_FAILURE, 0,
		   _("specified number of bytes `%s' is larger than \
the maximum\nrepresentable value of type off_t"), optarg);
	  break;

	case 's':
	  if (optarg == NULL)
	    string_min = 3;
	  else
	    {
	      s_err = xstrtoul (optarg, NULL, 0, &string_min, "bkm");
	      if (s_err != LONGINT_OK)
		STRTOL_FATAL_ERROR (optarg, _("minimum string length"), s_err);
	    }
	  ++flag_dump_strings;
	  break;

	case 't':
	  if (decode_format_string (optarg))
	    ++n_failed_decodes;
	  break;

	case 'v':
	  abbreviate_duplicate_blocks = 0;
	  break;

	case 'B':
	  traditional = 1;
	  break;

	  /* The next several cases map the old, pre-POSIX format
	     specification options to the corresponding POSIX format
	     specs.  GNU od accepts any combination of old- and
	     new-style options.  Format specification options accumulate.  */

#define CASE_OLD_ARG(old_char,new_string)		\
	case old_char:					\
	  {						\
	    if (decode_format_string (new_string))	\
	      ++n_failed_decodes;			\
	  }						\
	  break

	  CASE_OLD_ARG ('a', "a");
	  CASE_OLD_ARG ('b', "oC");
	  CASE_OLD_ARG ('c', "c");
	  CASE_OLD_ARG ('d', "u2");
	  CASE_OLD_ARG ('f', "fF");
	  CASE_OLD_ARG ('h', "x2");
	  CASE_OLD_ARG ('i', "d2");
	  CASE_OLD_ARG ('l', "d4");
	  CASE_OLD_ARG ('o', "o2");
	  CASE_OLD_ARG ('x', "x2");

#undef CASE_OLD_ARG

	case 'w':
	  width_specified = 1;
	  if (optarg == NULL)
	    {
	      desired_width = 32;
	    }
	  else
	    {
	      s_err = xstrtoul (optarg, NULL, 10, &desired_width, NULL);
	      if (s_err != LONGINT_OK)
		STRTOL_FATAL_ERROR (optarg, _("width specification"), s_err);
	    }
	  break;

	default:
	  usage (1);
	  break;
	}
    }

  if (n_failed_decodes > 0)
    exit (EXIT_FAILURE);

  if (show_version)
    {
      printf ("od - %s\n", PACKAGE_VERSION);
      exit (EXIT_SUCCESS);
    }

  if (show_help)
    usage (0);

  if (flag_dump_strings && n_specs > 0)
    error (EXIT_FAILURE, 0,
	   _("no type may be specified when dumping strings"));

  n_files = argc - optind;

  /* If the --backward-compatible option is used, there may be from
     0 to 3 remaining command line arguments;  handle each case
     separately.
	od [file] [[+]offset[.][b] [[+]label[.][b]]]
     The offset and pseudo_start have the same syntax.  */

  if (traditional)
    {
      off_t offset;

      if (n_files == 1)
	{
	  if ((offset = parse_old_offset (argv[optind])) >= 0)
	    {
	      n_bytes_to_skip = offset;
	      --n_files;
	      ++argv;
	    }
	}
      else if (n_files == 2)
	{
	  off_t o1, o2;
	  if ((o1 = parse_old_offset (argv[optind])) >= 0
	      && (o2 = parse_old_offset (argv[optind + 1])) >= 0)
	    {
	      n_bytes_to_skip = o1;
	      flag_pseudo_start = 1;
	      pseudo_start = o2;
	      argv += 2;
	      n_files -= 2;
	    }
	  else if ((o2 = parse_old_offset (argv[optind + 1])) >= 0)
	    {
	      n_bytes_to_skip = o2;
	      --n_files;
	      argv[optind + 1] = argv[optind];
	      ++argv;
	    }
	  else
	    {
	      error (0, 0,
		     _("invalid second operand in compatibility mode `%s'"),
		     argv[optind + 1]);
	      usage (1);
	    }
	}
      else if (n_files == 3)
	{
	  off_t o1, o2;
	  if ((o1 = parse_old_offset (argv[optind + 1])) >= 0
	      && (o2 = parse_old_offset (argv[optind + 2])) >= 0)
	    {
	      n_bytes_to_skip = o1;
	      flag_pseudo_start = 1;
	      pseudo_start = o2;
	      argv[optind + 2] = argv[optind];
	      argv += 2;
	      n_files -= 2;
	    }
	  else
	    {
	      error (0, 0,
	    _("in compatibility mode the last 2 arguments must be offsets"));
	      usage (1);
	    }
	}
      else
	{
	  error (0, 0,
	   _("in compatibility mode there may be no more than 3 arguments"));
	  usage (1);
	}

      if (flag_pseudo_start)
	{
	  static char buf[10];

	  if (output_address_fmt_string == NULL)
	    {
	      output_address_fmt_string = "(%07o)";
	      format_address = format_address_std;
	    }
	  else
	    {
	      sprintf (buf, "%s (%s)",
		       output_address_fmt_string,
		       output_address_fmt_string);
	      output_address_fmt_string = buf;
	      format_address = format_address_label;
	    }
	}
    }

  assert (address_pad_len <= MAX_ADDRESS_LENGTH);
  for (i = 0; i < address_pad_len; i++)
    address_pad[i] = ' ';
  address_pad[address_pad_len] = '\0';

  if (n_specs == 0)
    {
      if (decode_one_format ("o2", "o2", NULL, &(spec[0])))
	{
	  /* This happens on Cray systems that don't have a 2-byte
	     integral type.  */
	  exit (EXIT_FAILURE);
	}

      n_specs = 1;
    }

  if (n_files > 0)
    file_list = (char const *const *) &argv[optind];
  else
    {
      /* If no files were listed on the command line, set up the
	 global array FILE_LIST so that it contains the null-terminated
	 list of one name: "-".  */
      static char const *const default_file_list[] = {"-", NULL};

      file_list = default_file_list;
    }

  err |= skip (n_bytes_to_skip);
  if (in_stream == NULL)
    goto cleanup;

  pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);

  /* Compute output block length.  */
  l_c_m = get_lcm ();

  if (width_specified)
    {
      if (desired_width != 0 && desired_width % l_c_m == 0)
	bytes_per_block = desired_width;
      else
	{
	  error (0, 0, _("warning: invalid width %lu; using %d instead"),
		 desired_width, l_c_m);
	  bytes_per_block = l_c_m;
	}
    }
  else
    {
      if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
	bytes_per_block = l_c_m * (int) (DEFAULT_BYTES_PER_BLOCK / l_c_m);
      else
	bytes_per_block = l_c_m;
    }

#ifdef DEBUG
  for (i = 0; i < n_specs; i++)
    {
      printf (_("%d: fmt=\"%s\" width=%d\n"),
	      i, spec[i].fmt_string, width_bytes[spec[i].size]);
    }
#endif

  err |= (flag_dump_strings ? dump_strings () : dump ());

cleanup:;

  if (have_read_stdin && fclose (stdin) == EOF)
    error (EXIT_FAILURE, errno, _("standard input"));

  if (fclose (stdout) == EOF)
    error (EXIT_FAILURE, errno, _("write error"));

  exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}