forked from coreutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
od.c
1914 lines (1628 loc) · 55.8 KB
/
od.c
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
/* od -- dump files in octal and other formats
Copyright (C) 1992, 1995-2011 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
/* Written by Jim Meyering. */
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include "error.h"
#include "ftoastr.h"
#include "quote.h"
#include "xfreopen.h"
#include "xprintf.h"
#include "xstrtol.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "od"
#define AUTHORS proper_name ("Jim Meyering")
/* The default number of input bytes per output line. */
#define DEFAULT_BYTES_PER_BLOCK 16
#if HAVE_UNSIGNED_LONG_LONG_INT
typedef unsigned long long int unsigned_long_long_int;
#else
/* This is just a place-holder to avoid a few `#if' directives.
In this case, the type isn't actually used. */
typedef unsigned long int unsigned_long_long_int;
#endif
enum size_spec
{
NO_SIZE,
CHAR,
SHORT,
INT,
LONG,
LONG_LONG,
/* FIXME: add INTMAX support, too */
FLOAT_SINGLE,
FLOAT_DOUBLE,
FLOAT_LONG_DOUBLE,
N_SIZE_SPECS
};
enum output_format
{
SIGNED_DECIMAL,
UNSIGNED_DECIMAL,
OCTAL,
HEXADECIMAL,
FLOATING_POINT,
NAMED_CHARACTER,
CHARACTER
};
#define MAX_INTEGRAL_TYPE_SIZE sizeof (unsigned_long_long_int)
/* The maximum number of bytes needed for a format string, including
the trailing nul. Each format string expects a variable amount of
padding (guaranteed to be at least 1 plus the field width), then an
element that will be formatted in the field. */
enum
{
FMT_BYTES_ALLOCATED =
(sizeof "%*.99" - 1
+ MAX (sizeof "ld",
MAX (sizeof PRIdMAX,
MAX (sizeof PRIoMAX,
MAX (sizeof PRIuMAX,
sizeof PRIxMAX)))))
};
/* Ensure that our choice for FMT_BYTES_ALLOCATED is reasonable. */
verify (MAX_INTEGRAL_TYPE_SIZE * CHAR_BIT / 3 <= 99);
/* Each output format specification (from `-t spec' or from
old-style options) is represented by one of these structures. */
struct tspec
{
enum output_format fmt;
enum size_spec size; /* Type of input object. */
/* FIELDS is the number of fields per line, BLANK is the number of
fields to leave blank. WIDTH is width of one field, excluding
leading space, and PAD is total pad to divide among FIELDS.
PAD is at least as large as FIELDS. */
void (*print_function) (size_t fields, size_t blank, void const *data,
char const *fmt, int width, int pad);
char fmt_string[FMT_BYTES_ALLOCATED]; /* Of the style "%*d". */
bool hexl_mode_trailer;
int field_width; /* Minimum width of a field, excluding leading space. */
int pad_width; /* Total padding to be divided among fields. */
};
/* 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 unsigned int const bytes_to_oct_digits[] =
{0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};
static unsigned int const bytes_to_signed_dec_digits[] =
{1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};
static unsigned int const bytes_to_unsigned_dec_digits[] =
{0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};
static unsigned int const bytes_to_hex_digits[] =
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};
/* It'll be a while before we see integral types wider than 16 bytes,
but if/when it happens, this check will catch it. Without this check,
a wider type would provoke a buffer overrun. */
verify (MAX_INTEGRAL_TYPE_SIZE < ARRAY_CARDINALITY (bytes_to_hex_digits));
/* Make sure the other arrays have the same length. */
verify (sizeof bytes_to_oct_digits == sizeof bytes_to_signed_dec_digits);
verify (sizeof bytes_to_oct_digits == sizeof bytes_to_unsigned_dec_digits);
verify (sizeof bytes_to_oct_digits == sizeof bytes_to_hex_digits);
/* 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 (unsigned_long_long_int),
sizeof (float),
sizeof (double),
sizeof (long double)
};
/* Ensure that for each member of `enum size_spec' there is an
initializer in the width_bytes array. */
verify (ARRAY_CARDINALITY (width_bytes) == N_SIZE_SPECS);
/* Names for some non-printing characters. */
static char const charname[33][4] =
{
"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"
};
/* Address base (8, 10 or 16). */
static int address_base;
/* The number of octal digits required to represent the largest
address value. */
#define MAX_ADDRESS_LENGTH \
((sizeof (uintmax_t) * CHAR_BIT + CHAR_BIT - 1) / 3)
/* Width of a normal address. */
static int address_pad_len;
/* Minimum length when detecting --strings. */
static size_t string_min;
/* True when in --strings mode. */
static bool flag_dump_strings;
/* True if we should recognize the older non-option arguments
that specified at most one file and optional arguments specifying
offset and pseudo-start address. */
static bool traditional;
/* True if an old-style `pseudo-address' was specified. */
static bool flag_pseudo_start;
/* The difference between the old-style pseudo starting address and
the number of bytes to skip. */
static uintmax_t pseudo_offset;
/* Function that accepts an address and an optional following char,
and prints the address and char to stdout. */
static void (*format_address) (uintmax_t, char);
/* The number of input bytes to skip before formatting and writing. */
static uintmax_t n_bytes_to_skip = 0;
/* When false, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
input is formatted. */
static bool limit_bytes_to_format = false;
/* The maximum number of bytes that will be formatted. */
static uintmax_t max_bytes_to_format;
/* The offset of the first byte after the last byte to be formatted. */
static uintmax_t end_offset;
/* When true 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 bool abbreviate_duplicate_blocks = true;
/* An array of specs describing how to format each input block. */
static struct tspec *spec;
/* The number of format specs. */
static size_t n_specs;
/* The allocated length of SPEC. */
static size_t 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 size_t bytes_per_block;
/* Human-readable representation of *file_list (for error messages).
It differs from file_list[-1] only when file_list[-1] is "-". */
static char const *input_filename;
/* A NULL-terminated list of the file-arguments from the command line. */
static char const *const *file_list;
/* Initializer for file_list if no file-arguments
were specified on the command line. */
static char const *const default_file_list[] = {"-", NULL};
/* The input stream associated with the current file. */
static FILE *in_stream;
/* If true, at least one of the files we read was standard input. */
static bool have_read_stdin;
/* Map the size in bytes to a type identifier. */
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];
static char const short_options[] = "A:aBbcDdeFfHhIij:LlN:OoS:st:vw::Xx";
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
TRADITIONAL_OPTION = CHAR_MAX + 1
};
static struct option const long_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'},
{"strings", optional_argument, NULL, 'S'},
{"traditional", no_argument, NULL, TRADITIONAL_OPTION},
{"width", optional_argument, NULL, 'w'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
or: %s [-abcdfilosx]... [FILE] [[+]OFFSET[.][b]]\n\
or: %s --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]\n\
"),
program_name, program_name, program_name);
fputs (_("\n\
Write an unambiguous representation, octal bytes by default,\n\
of FILE to standard output. With more than one FILE argument,\n\
concatenate them in the listed order to form the input.\n\
With no FILE, or when FILE is -, read standard input.\n\
\n\
"), stdout);
fputs (_("\
All arguments to long options are mandatory for short options.\n\
"), stdout);
fputs (_("\
-A, --address-radix=RADIX decide how file offsets are printed\n\
-j, --skip-bytes=BYTES skip BYTES input bytes first\n\
"), stdout);
fputs (_("\
-N, --read-bytes=BYTES limit dump to BYTES input bytes\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 traditional form\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
Traditional format specifications may be intermixed; they accumulate:\n\
-a same as -t a, select named characters, ignoring high-order bit\n\
-b same as -t o1, select octal bytes\n\
-c same as -t c, select ASCII characters or backslash escapes\n\
-d same as -t u2, select unsigned decimal 2-byte units\n\
"), stdout);
fputs (_("\
-f same as -t fF, select floats\n\
-i same as -t dI, select decimal ints\n\
-l same as -t dL, select decimal longs\n\
-o same as -t o2, select octal 2-byte units\n\
-s same as -t d2, select decimal 2-byte units\n\
-x same as -t x2, select hexadecimal 2-byte units\n\
"), stdout);
fputs (_("\
\n\
If first and second call formats both apply, the second format is assumed\n\
if the last operand begins with + or (if there are 2 operands) a digit.\n\
An OFFSET operand means -j OFFSET. LABEL is the pseudo-address\n\
at first byte printed, incremented when dump is progressing.\n\
For OFFSET and LABEL, a 0x or 0X prefix indicates hexadecimal;\n\
suffixes may be . for octal and b for multiply by 512.\n\
"), stdout);
fputs (_("\
\n\
TYPE is made up of one or more of these specifications:\n\
\n\
a named character, ignoring high-order bit\n\
c ASCII character or backslash escape\n\
"), stdout);
fputs (_("\
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\
"), stdout);
fputs (_("\
\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\
"), stdout);
fputs (_("\
\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, and may have a multiplier suffix:\n\
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
Adding a z suffix to any type displays printable characters at the end of each\
\n\
output line.\n\
"), stdout);
fputs (_("\
Option --string without a number implies 3; option --width without a number\n\
implies 32. By default, od uses -A o -t oS -w16.\n\
"), stdout);
emit_ancillary_info ();
}
exit (status);
}
/* Define the print functions. */
#define PRINT_FIELDS(N, T, FMT_STRING, ACTION) \
static void \
N (size_t fields, size_t blank, void const *block, \
char const *FMT_STRING, int width, int pad) \
{ \
T const *p = block; \
size_t i; \
int pad_remaining = pad; \
for (i = fields; blank < i; i--) \
{ \
int next_pad = pad * (i - 1) / fields; \
int adjusted_width = pad_remaining - next_pad + width; \
T x = *p++; \
ACTION; \
pad_remaining = next_pad; \
} \
}
#define PRINT_TYPE(N, T) \
PRINT_FIELDS (N, T, fmt_string, xprintf (fmt_string, adjusted_width, x))
#define PRINT_FLOATTYPE(N, T, FTOASTR, BUFSIZE) \
PRINT_FIELDS (N, T, fmt_string ATTRIBUTE_UNUSED, \
char buf[BUFSIZE]; \
FTOASTR (buf, sizeof buf, 0, 0, x); \
xprintf ("%*s", adjusted_width, buf))
PRINT_TYPE (print_s_char, signed char)
PRINT_TYPE (print_char, unsigned char)
PRINT_TYPE (print_s_short, short int)
PRINT_TYPE (print_short, unsigned short int)
PRINT_TYPE (print_int, unsigned int)
PRINT_TYPE (print_long, unsigned long int)
PRINT_TYPE (print_long_long, unsigned_long_long_int)
PRINT_FLOATTYPE (print_float, float, ftoastr, FLT_BUFSIZE_BOUND)
PRINT_FLOATTYPE (print_double, double, dtoastr, DBL_BUFSIZE_BOUND)
PRINT_FLOATTYPE (print_long_double, long double, ldtoastr, LDBL_BUFSIZE_BOUND)
#undef PRINT_TYPE
#undef PRINT_FLOATTYPE
static void
dump_hexl_mode_trailer (size_t n_bytes, const char *block)
{
size_t i;
fputs (" >", stdout);
for (i = n_bytes; i > 0; i--)
{
unsigned char c = *block++;
unsigned char c2 = (isprint (c) ? c : '.');
putchar (c2);
}
putchar ('<');
}
static void
print_named_ascii (size_t fields, size_t blank, void const *block,
const char *unused_fmt_string ATTRIBUTE_UNUSED,
int width, int pad)
{
unsigned char const *p = block;
size_t i;
int pad_remaining = pad;
for (i = fields; blank < i; i--)
{
int next_pad = pad * (i - 1) / fields;
int masked_c = *p++ & 0x7f;
const char *s;
char buf[2];
if (masked_c == 127)
s = "del";
else if (masked_c <= 040)
s = charname[masked_c];
else
{
buf[0] = masked_c;
buf[1] = 0;
s = buf;
}
xprintf ("%*s", pad_remaining - next_pad + width, s);
pad_remaining = next_pad;
}
}
static void
print_ascii (size_t fields, size_t blank, void const *block,
const char *unused_fmt_string ATTRIBUTE_UNUSED, int width,
int pad)
{
unsigned char const *p = block;
size_t i;
int pad_remaining = pad;
for (i = fields; blank < i; i--)
{
int next_pad = pad * (i - 1) / fields;
unsigned char c = *p++;
const char *s;
char buf[4];
switch (c)
{
case '\0':
s = "\\0";
break;
case '\a':
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 = buf;
}
xprintf ("%*s", pad_remaining - next_pad + width, s);
pad_remaining = next_pad;
}
}
/* 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 true. 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 false.
Otherwise, advance *P to the first non-digit after S, set *VAL to
the result of the conversion and return true. */
static bool
simple_strtoul (const char *s, const char **p, unsigned long int *val)
{
unsigned long int sum;
sum = 0;
while (ISDIGIT (*s))
{
int c = *s++ - '0';
if (sum > (ULONG_MAX - c) / 10)
return false;
sum = sum * 10 + c;
}
*p = s;
*val = sum;
return true;
}
/* If S points to a single valid modern 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 true. If S is not valid, don't modify *NEXT or *TSPEC,
give a diagnostic, and return false. 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)
field_width = 11;
fmt_string = "%*d";
}
pad_width is determined later, but is at least as large as the
number of fields printed per row.
S_ORIG is solely for reporting errors. It should be the full format
string argument.
*/
static bool
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;
void (*print_function) (size_t, size_t, void const *, char const *,
int, int);
const char *p;
char c;
int field_width;
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 int);
break;
case 'I':
++s;
size = sizeof (int);
break;
case 'L':
++s;
size = sizeof (long int);
break;
default:
if (! simple_strtoul (s, &p, &size))
{
/* The integer at P in S would overflow an unsigned long int.
A digit string that long is sufficiently odd looking
that the following diagnostic is sufficient. */
error (0, 0, _("invalid type string %s"), quote (s_orig));
return false;
}
if (p == s)
size = sizeof (int);
else
{
if (MAX_INTEGRAL_TYPE_SIZE < 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"), quote (s_orig), size);
return false;
}
s = p;
}
break;
}
#define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
((Spec) == LONG_LONG ? (Max_format) \
: ((Spec) == LONG ? (Long_format) \
: (Min_format))) \
size_spec = integral_type_size[size];
switch (c)
{
case 'd':
fmt = SIGNED_DECIMAL;
field_width = bytes_to_signed_dec_digits[size];
sprintf (tspec->fmt_string, "%%*%s",
ISPEC_TO_FORMAT (size_spec, "d", "ld", PRIdMAX));
break;
case 'o':
fmt = OCTAL;
sprintf (tspec->fmt_string, "%%*.%d%s",
(field_width = bytes_to_oct_digits[size]),
ISPEC_TO_FORMAT (size_spec, "o", "lo", PRIoMAX));
break;
case 'u':
fmt = UNSIGNED_DECIMAL;
field_width = bytes_to_unsigned_dec_digits[size];
sprintf (tspec->fmt_string, "%%*%s",
ISPEC_TO_FORMAT (size_spec, "u", "lu", PRIuMAX));
break;
case 'x':
fmt = HEXADECIMAL;
sprintf (tspec->fmt_string, "%%*.%d%s",
(field_width = bytes_to_hex_digits[size]),
ISPEC_TO_FORMAT (size_spec, "x", "lx", PRIxMAX));
break;
default:
abort ();
}
assert (strlen (tspec->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;
case LONG_LONG:
print_function = print_long_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))
{
/* The integer at P in S would overflow an unsigned long int.
A digit string that long is sufficiently odd looking
that the following diagnostic is sufficient. */
error (0, 0, _("invalid type string %s"), quote (s_orig));
return false;
}
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"),
quote (s_orig), size);
return false;
}
s = p;
}
break;
}
size_spec = fp_type_size[size];
struct lconv const *locale = localeconv ();
size_t decimal_point_len =
(locale->decimal_point[0] ? strlen (locale->decimal_point) : 1);
switch (size_spec)
{
case FLOAT_SINGLE:
print_function = print_float;
field_width = FLT_STRLEN_BOUND_L (decimal_point_len);
break;
case FLOAT_DOUBLE:
print_function = print_double;
field_width = DBL_STRLEN_BOUND_L (decimal_point_len);
break;
case FLOAT_LONG_DOUBLE:
print_function = print_long_double;
field_width = LDBL_STRLEN_BOUND_L (decimal_point_len);
break;
default:
abort ();
}
break;
case 'a':
++s;
fmt = NAMED_CHARACTER;
size_spec = CHAR;
print_function = print_named_ascii;
field_width = 3;
break;
case 'c':
++s;
fmt = CHARACTER;
size_spec = CHAR;
print_function = print_ascii;
field_width = 3;
break;
default:
error (0, 0, _("invalid character `%c' in type string %s"),
*s, quote (s_orig));
return false;
}
tspec->size = size_spec;
tspec->fmt = fmt;
tspec->print_function = print_function;
tspec->field_width = field_width;
tspec->hexl_mode_trailer = (*s == 'z');
if (tspec->hexl_mode_trailer)
s++;
if (next != NULL)
*next = s;
return true;
}
/* Given a list of one or more input filenames FILE_LIST, set the global
file pointer IN_STREAM and the global string INPUT_FILENAME to the
first one that can be successfully opened. Modify FILE_LIST to
reference the next filename in the list. A file name of "-" is
interpreted as standard input. If any file open fails, give an error
message and return false. */
static bool
open_next_file (void)
{
bool ok = true;
do
{
input_filename = *file_list;
if (input_filename == NULL)
return ok;
++file_list;
if (STREQ (input_filename, "-"))
{
input_filename = _("standard input");
in_stream = stdin;
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
xfreopen (NULL, "rb", stdin);
}
else
{
in_stream = fopen (input_filename, (O_BINARY ? "rb" : "r"));
if (in_stream == NULL)
{
error (0, errno, "%s", input_filename);
ok = false;
}
}
}
while (in_stream == NULL);
if (limit_bytes_to_format && !flag_dump_strings)
setvbuf (in_stream, NULL, _IONBF, 0);
return ok;
}
/* Test whether there have been errors on in_stream, and close it if
it is not standard input. Return false if there has been an error
on in_stream or stdout; return true otherwise. This function will
report more than one error only if both a read and a write error
have occurred. IN_ERRNO, if nonzero, is the error number
corresponding to the most recent action for IN_STREAM. */
static bool
check_and_close (int in_errno)
{
bool ok = true;
if (in_stream != NULL)
{
if (ferror (in_stream))
{
error (0, in_errno, _("%s: read error"), input_filename);
if (! STREQ (file_list[-1], "-"))
fclose (in_stream);
ok = false;
}
else if (! STREQ (file_list[-1], "-") && fclose (in_stream) != 0)
{
error (0, errno, "%s", input_filename);
ok = false;
}
in_stream = NULL;
}
if (ferror (stdout))
{
error (0, 0, _("write error"));
ok = false;
}
return ok;
}
/* Decode the modern od format string S. Append the decoded
representation to the global array SPEC, reallocating SPEC if
necessary. Return true if S is valid. */
static bool
decode_format_string (const char *s)
{
const char *s_orig = s;
assert (s != NULL);
while (*s != '\0')
{
const char *next;
if (n_specs_allocated <= n_specs)
spec = X2NREALLOC (spec, &n_specs_allocated);
if (! decode_one_format (s_orig, s, &next, &spec[n_specs]))
return false;
assert (s != next);
s = next;
++n_specs;
}
return true;
}
/* 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
false. When possible, use seek rather than read operations to
advance IN_STREAM. */
static bool
skip (uintmax_t n_skip)
{
bool ok = true;
int in_errno = 0;
if (n_skip == 0)
return true;
while (in_stream != NULL) /* EOF. */
{
struct stat file_stats;
/* First try seeking. 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 seek:
- 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. */
if (fstat (fileno (in_stream), &file_stats) == 0)
{
/* The st_size field is valid only for regular files
(and for symbolic links, which cannot occur here).
If the number of bytes left to skip is larger than
the size of the current file, we can decrement n_skip
and go on to the next file. Skip this optimization also
when st_size is 0, because some kernels report that
nonempty files in /proc have st_size == 0. */
if (S_ISREG (file_stats.st_mode) && 0 < file_stats.st_size)
{
if ((uintmax_t) file_stats.st_size < n_skip)
n_skip -= file_stats.st_size;
else
{
if (fseeko (in_stream, n_skip, SEEK_CUR) != 0)
{
in_errno = errno;
ok = false;
}
n_skip = 0;