Copyright (C) 92, 1995-2002 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. */
#include <config.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include "closeout.h"
#include "error.h"
#include "posixver.h"
#include "xstrtol.h"
#define PROGRAM_NAME "od"
#define AUTHORS "Jim Meyering"
#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
#define DEFAULT_BYTES_PER_BLOCK 16
#ifndef FLT_DIG
# define FLT_DIG 7
#endif
#ifndef DBL_DIG
# define DBL_DIG 15
#endif
#ifndef LDBL_DIG
# define LDBL_DIG DBL_DIG
#endif
#if HAVE_UNSIGNED_LONG_LONG
typedef unsigned long long ulonglong_t;
#else
typedef unsigned long int ulonglong_t;
#endif
enum size_spec
{
NO_SIZE,
CHAR,
SHORT,
INT,
LONG,
LONG_LONG,
FLOAT_SINGLE,
FLOAT_DOUBLE,
FLOAT_LONG_DOUBLE,
N_SIZE_SPECS
};
enum output_format
{
SIGNED_DECIMAL,
UNSIGNED_DECIMAL,
OCTAL,
HEXADECIMAL,
FLOATING_POINT,
NAMED_CHARACTER,
CHARACTER
};
struct tspec
{
enum output_format fmt;
enum size_spec size;
void (*print_function) (size_t, const char *, const char *);
char *fmt_string;
int hexl_mode_trailer;
int field_width;
};
char *program_name;
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};
static const int width_bytes[] =
{
-1,
sizeof (char),
sizeof (short int),
sizeof (int),
sizeof (long int),
sizeof (ulonglong_t),
sizeof (float),
sizeof (double),
sizeof (LONG_DOUBLE)
};
struct dummy
{
int assert_width_bytes_matches_size_spec_decl
[sizeof width_bytes / sizeof width_bytes[0] == N_SIZE_SPECS ? 1 : -1];
};
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"
};
static int address_base;
#define MAX_ADDRESS_LENGTH \
((sizeof (uintmax_t) * CHAR_BIT + CHAR_BIT - 1) / 3)
static int address_pad_len;
static size_t string_min;
static int flag_dump_strings;
static int traditional;
static int flag_pseudo_start;
static uintmax_t pseudo_offset;
static void (*format_address) (uintmax_t, char);
static uintmax_t n_bytes_to_skip = 0;
static int limit_bytes_to_format = 0;
static uintmax_t max_bytes_to_format;
static uintmax_t end_offset;
static int abbreviate_duplicate_blocks = 1;
static struct tspec *spec;
static size_t n_specs;
static size_t n_specs_allocated;
static size_t bytes_per_block;
static char const *input_filename;
static char const *const *file_list;
static char const *const default_file_list[] = {"-", NULL};
static FILE *in_stream;
static int have_read_stdin;
#define MAX_INTEGRAL_TYPE_SIZE sizeof (ulonglong_t)
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];
#define COMMON_SHORT_OPTIONS "A:N:abcdfhij:lot:vx"
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 != 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);
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\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\
"), stdout);
fputs (_("\
-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\
"), stdout);
fputs (_("\
\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 may be . for octal and b for 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\
"), 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, it is multiplied by 512\n\
with b suffix, by 1024 with k and by 1048576 with m. Adding a z suffix to\n\
any type adds a display of printable characters to the end of each line\n\
of output. \
"), stdout);
fputs (_("\
--string without a number implies 3. --width without a number\n\
implies 32. By default, od uses -A o -t d2 -w 16.\n\
"), stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
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;
}
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 (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t 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);
block += sizeof (unsigned char);
}
}
static void
print_char (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes; i > 0; i--)
{
unsigned int tmp = *(const unsigned char *) block;
printf (fmt_string, tmp);
block += sizeof (unsigned char);
}
}
static void
print_s_short (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t 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);
block += sizeof (unsigned short);
}
}
static void
print_short (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (unsigned short); i > 0; i--)
{
unsigned int tmp = *(const unsigned short *) block;
printf (fmt_string, tmp);
block += sizeof (unsigned short);
}
}
static void
print_int (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (unsigned int); i > 0; i--)
{
unsigned int tmp = *(const unsigned int *) block;
printf (fmt_string, tmp);
block += sizeof (unsigned int);
}
}
static void
print_long (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (unsigned long); i > 0; i--)
{
unsigned long tmp = *(const unsigned long *) block;
printf (fmt_string, tmp);
block += sizeof (unsigned long);
}
}
static void
print_long_long (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (ulonglong_t); i > 0; i--)
{
ulonglong_t tmp = *(const ulonglong_t *) block;
printf (fmt_string, tmp);
block += sizeof (ulonglong_t);
}
}
static void
print_float (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (float); i > 0; i--)
{
float tmp = *(const float *) block;
printf (fmt_string, tmp);
block += sizeof (float);
}
}
static void
print_double (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (double); i > 0; i--)
{
double tmp = *(const double *) block;
printf (fmt_string, tmp);
block += sizeof (double);
}
}
#ifdef HAVE_LONG_DOUBLE
static void
print_long_double (size_t n_bytes, const char *block, const char *fmt_string)
{
size_t i;
for (i = n_bytes / sizeof (LONG_DOUBLE); i > 0; i--)
{
LONG_DOUBLE tmp = *(const LONG_DOUBLE *) block;
printf (fmt_string, tmp);
block += sizeof (LONG_DOUBLE);
}
}
#endif
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 int c = *(const unsigned char *) block;
unsigned int c2 = (ISPRINT(c) ? c : '.');
putchar (c2);
block += sizeof (unsigned char);
}
putchar ('<');
}
static void
print_named_ascii (size_t n_bytes, const char *block,
const char *unused_fmt_string ATTRIBUTE_UNUSED)
{
size_t 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", s);
block += sizeof (unsigned char);
}
}
static void
print_ascii (size_t n_bytes, const char *block,
const char *unused_fmt_string ATTRIBUTE_UNUSED)
{
size_t 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", s);
block += sizeof (unsigned char);
}
}
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;
}
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) (size_t, const char *, const char *);
const char *p;
unsigned int c;
unsigned int field_width = 0;
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)
{
error (0, 0, _("invalid type string `%s'"), s_orig);
return 1;
}
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"), s_orig, size);
return 1;
}
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))) \
#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%s",
(field_width = bytes_to_signed_dec_digits[size]),
ISPEC_TO_FORMAT (size_spec, "d", "ld", PRIdMAX));
break;
case 'o':
fmt = OCTAL;
sprintf (fmt_string, " %%0%u%s",
(field_width = bytes_to_oct_digits[size]),
ISPEC_TO_FORMAT (size_spec, "o", "lo", PRIoMAX));
break;
case 'u':
fmt = UNSIGNED_DECIMAL;
sprintf (fmt_string, " %%%u%s",
(field_width = bytes_to_unsigned_dec_digits[size]),
ISPEC_TO_FORMAT (size_spec, "u", "lu", PRIuMAX));
break;
case 'x':
fmt = HEXADECIMAL;
sprintf (fmt_string, " %%0%u%s",
(field_width = bytes_to_hex_digits[size]),
ISPEC_TO_FORMAT (size_spec, "x", "lx", PRIxMAX));
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;
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) != 0)
{
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;
pre_fmt_string = " %%%d.%de";
fmt_string = xmalloc (strlen (pre_fmt_string));
sprintf (fmt_string, pre_fmt_string,
(field_width = FLT_DIG + 8), FLT_DIG);
break;
case FLOAT_DOUBLE:
print_function = print_double;
pre_fmt_string = " %%%d.%de";
fmt_string = xmalloc (strlen (pre_fmt_string));
sprintf (fmt_string, pre_fmt_string,
(field_width = DBL_DIG + 8), DBL_DIG);
break;
#ifdef HAVE_LONG_DOUBLE
case FLOAT_LONG_DOUBLE:
print_function = print_long_double;
pre_fmt_string = " %%%d.%dLe";
fmt_string = xmalloc (strlen (pre_fmt_string));
sprintf (fmt_string, pre_fmt_string,
(field_width = 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;
field_width = 3;
break;
case 'c':
++s;
fmt = CHARACTER;
size_spec = CHAR;
fmt_string = NULL;
print_function = print_ascii;
field_width = 3;
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;
tspec->field_width = field_width;
tspec->hexl_mode_trailer = (*s == 'z');
if (tspec->hexl_mode_trailer)
s++;
if (next != NULL)
*next = s;
return 0;
}
static int
open_next_file (void)
{
int err = 0;
do
{
input_filename = *file_list;
if (input_filename == NULL)
return err;
++file_list;
if (STREQ (input_filename, "-"))
{
input_filename = _("standard input");
in_stream = stdin;
have_read_stdin = 1;
}
else
{
in_stream = fopen (input_filename, "r");
if (in_stream == NULL)
{
error (0, errno, "%s", input_filename);
err = 1;
}
}
}
while (in_stream == NULL);
if (limit_bytes_to_format && !flag_dump_strings)
SETVBUF (in_stream, NULL, _IONBF, 0);
SET_BINARY (fileno (in_stream));
return err;
}
static int
check_and_close (void)
{
int err = 0;
if (in_stream != NULL)
{
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;
}
in_stream = NULL;
}
if (ferror (stdout))
{
error (0, errno, _("standard output"));
err = 1;
}
return err;
}
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 ((char *) spec,
(n_specs_allocated
* sizeof (struct tspec)));
}
memcpy ((char *) &spec[n_specs], (char *) &tspec,
sizeof (struct tspec));
++n_specs;
}
return 0;
}
static int
skip (uintmax_t n_skip)
{
int err = 0;
if (n_skip == 0)
return 0;
while (in_stream != NULL)
{
struct stat file_stats;
if (fstat (fileno (in_stream), &file_stats) == 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)
{
error (0, errno, "%s", input_filename);
err = 1;
}
n_skip = 0;
}
}
else
{
char buf[BUFSIZ];
size_t n_bytes_read, n_bytes_to_read = BUFSIZ;
while (0 < n_skip)
{
if (n_skip < n_bytes_to_read)
n_bytes_to_read = n_skip;
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;
}
else
{
error (0, errno, "%s", input_filename);
err = 1;
}
err |= check_and_close ();
err |= open_next_file ();
}
if (n_skip != 0)
error (EXIT_FAILURE, 0, _("cannot skip past end of combined input"));
return err;
}
static void
format_address_none (uintmax_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
{
}
static void
format_address_std (uintmax_t address, char c)
{
char buf[MAX_ADDRESS_LENGTH + 2];
char *p = buf + sizeof buf;
char const *pbound;
*--p = '\0';
*--p = c;
pbound = p - address_pad_len;
switch (address_base)
{
case 8:
do
*--p = '0' + (address & 7);
while ((address >>= 3) != 0);
break;
case 10:
do
*--p = '0' + (address % 10);
while ((address /= 10) != 0);
break;
case 16:
do
*--p = "0123456789abcdef"[address & 15];
while ((address >>= 4) != 0);
break;
}
while (pbound < p)
*--p = '0';
fputs (p, stdout);
}
static void
format_address_paren (uintmax_t address, char c)
{
putchar ('(');
format_address_std (address, ')');
putchar (c);
}
static void
format_address_label (uintmax_t address, char c)
{
format_address_std (address, ' ');
format_address_paren (address + pseudo_offset, c);
}
static void
write_block (uintmax_t current_offset, size_t 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)
{
}
else
{
printf ("*\n");
prev_pair_equal = 1;
}
}
else
{
size_t i;
prev_pair_equal = 0;
for (i = 0; i < n_specs; i++)
{
if (i == 0)
format_address (current_offset, '\0');
else
printf ("%*s", address_pad_len, "");
(*spec[i].print_function) (n_bytes, curr_block, spec[i].fmt_string);
if (spec[i].hexl_mode_trailer)
{
int datum_width = width_bytes[spec[i].size];
int blank_fields = (bytes_per_block - n_bytes) / datum_width;
int field_width = spec[i].field_width + 1;
printf ("%*s", blank_fields * field_width, "");
dump_hexl_mode_trailer (n_bytes, curr_block);
}
putchar ('\n');
}
}
first = 0;
}
static int
read_char (int *c)
{
int err = 0;
*c = EOF;
while (in_stream != NULL)
{
*c = fgetc (in_stream);
if (*c != EOF)
break;
err |= check_and_close ();
err |= open_next_file ();
}
return err;
}
static int
read_block (size_t n, char *block, size_t *n_bytes_in_buffer)
{
int err = 0;
assert (0 < n && n <= bytes_per_block);
*n_bytes_in_buffer = 0;
if (n == 0)
return 0;
while (in_stream != NULL)
{
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)
break;
err |= check_and_close ();
err |= open_next_file ();
}
return err;
}
static int
get_lcm (void)
{
size_t 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;
}
static int
parse_old_offset (const char *s, uintmax_t *offset)
{
int radix;
enum strtol_error s_err;
if (*s == '\0')
return 0;
if (s[0] == '+')
++s;
if (strchr (s, '.') != NULL)
radix = 10;
else
{
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
radix = 16;
else
radix = 8;
}
s_err = xstrtoumax (s, NULL, radix, offset, "Bb");
if (s_err != LONGINT_OK)
{
STRTOL_FAIL_WARN (s, _("old-style offset"), s_err);
return 0;
}
return 1;
}
static int
dump (void)
{
char *block[2];
uintmax_t current_offset;
int idx;
int err;
size_t n_bytes_read;
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)
{
while (1)
{
size_t n_needed;
if (current_offset >= end_offset)
{
n_bytes_read = 0;
break;
}
n_needed = MIN (end_offset - current_offset,
(uintmax_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 ();
bytes_to_write = l_c_m * ((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;
}
format_address (current_offset, '\n');
if (limit_bytes_to_format && current_offset >= end_offset)
err |= check_and_close ();
return err;
}
static int
dump_strings (void)
{
size_t bufsize = MAX (100, string_min);
char *buf = xmalloc (bufsize);
uintmax_t address = n_bytes_to_skip;
int err;
err = 0;
while (1)
{
size_t i;
int c;
tryline:
if (limit_bytes_to_format
&& (end_offset < string_min || end_offset - string_min <= address))
break;
for (i = 0; i < string_min; i++)
{
err |= read_char (&c);
address++;
if (c < 0)
{
free (buf);
return err;
}
if (!ISPRINT (c))
goto tryline;
buf[i] = c;
}
while (!limit_bytes_to_format || address < end_offset)
{
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;
if (!ISPRINT (c))
goto tryline;
buf[i++] = c;
}
buf[i] = 0;
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');
}
free (buf);
err |= check_and_close ();
return err;
}
int
main (int argc, char **argv)
{
int c;
int n_files;
size_t i;
int l_c_m;
size_t desired_width IF_LINT (= 0);
int width_specified = 0;
int n_failed_decodes = 0;
int err;
char const *short_options = (posix2_version () < 200112
? COMMON_SHORT_OPTIONS "s::w::"
: COMMON_SHORT_OPTIONS "s:w:");
uintmax_t pseudo_start IF_LINT (= 0);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
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;
#if HAVE_UNSIGNED_LONG_LONG
integral_type_size[sizeof (ulonglong_t)] = LONG_LONG;
#endif
for (i = 0; i <= MAX_FP_TYPE_SIZE; i++)
fp_type_size[i] = NO_SIZE;
fp_type_size[sizeof (float)] = FLOAT_SINGLE;
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));
format_address = format_address_std;
address_base = 8;
address_pad_len = 7;
flag_dump_strings = 0;
while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
{
uintmax_t tmp;
enum strtol_error s_err;
switch (c)
{
case 0:
break;
case 'A':
switch (optarg[0])
{
case 'd':
format_address = format_address_std;
address_base = 10;
address_pad_len = 7;
break;
case 'o':
format_address = format_address_std;
address_base = 8;
address_pad_len = 7;
break;
case 'x':
format_address = format_address_std;
address_base = 16;
address_pad_len = 6;
break;
case 'n':
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 = xstrtoumax (optarg, NULL, 0, &n_bytes_to_skip, "bkm");
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("skip argument"), s_err);
break;
case 'N':
limit_bytes_to_format = 1;
s_err = xstrtoumax (optarg, NULL, 0, &max_bytes_to_format, "bkm");
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("limit argument"), s_err);
break;
case 's':
if (optarg == NULL)
string_min = 3;
else
{
s_err = xstrtoumax (optarg, NULL, 0, &tmp, "bkm");
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("minimum string length"), s_err);
if (SIZE_MAX < tmp)
error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
string_min = tmp;
}
flag_dump_strings = 1;
break;
case 't':
if (decode_format_string (optarg))
++n_failed_decodes;
break;
case 'v':
abbreviate_duplicate_blocks = 0;
break;
case TRADITIONAL_OPTION:
traditional = 1;
break;
#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
{
uintmax_t w_tmp;
s_err = xstrtoumax (optarg, NULL, 10, &w_tmp, "");
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("width specification"), s_err);
if (SIZE_MAX < w_tmp)
error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
desired_width = w_tmp;
}
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
break;
}
}
if (n_failed_decodes > 0)
exit (EXIT_FAILURE);
if (flag_dump_strings && n_specs > 0)
error (EXIT_FAILURE, 0,
_("no type may be specified when dumping strings"));
n_files = argc - optind;
if (traditional)
{
uintmax_t offset;
if (n_files == 1)
{
if (parse_old_offset (argv[optind], &offset))
{
n_bytes_to_skip = offset;
--n_files;
++argv;
}
}
else if (n_files == 2)
{
uintmax_t o1, o2;
if (parse_old_offset (argv[optind], &o1)
&& parse_old_offset (argv[optind + 1], &o2))
{
n_bytes_to_skip = o1;
flag_pseudo_start = 1;
pseudo_start = o2;
argv += 2;
n_files -= 2;
}
else if (parse_old_offset (argv[optind + 1], &o2))
{
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 (EXIT_FAILURE);
}
}
else if (n_files == 3)
{
uintmax_t o1, o2;
if (parse_old_offset (argv[optind + 1], &o1)
&& parse_old_offset (argv[optind + 2], &o2))
{
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 two arguments must be offsets"));
usage (EXIT_FAILURE);
}
}
else if (n_files > 3)
{
error (0, 0,
_("compatibility mode supports at most three arguments"));
usage (EXIT_FAILURE);
}
if (flag_pseudo_start)
{
if (format_address == format_address_none)
{
address_base = 8;
address_pad_len = 7;
format_address = format_address_paren;
}
else
format_address = format_address_label;
}
}
if (limit_bytes_to_format)
{
end_offset = n_bytes_to_skip + max_bytes_to_format;
if (end_offset < n_bytes_to_skip)
error (EXIT_FAILURE, 0, "skip-bytes + read-bytes is too large");
}
if (n_specs == 0)
{
if (decode_one_format ("o2", "o2", NULL, &(spec[0])))
{
exit (EXIT_FAILURE);
}
n_specs = 1;
}
if (n_files > 0)
{
file_list = (char const *const *) &argv[optind];
}
else
{
file_list = default_file_list;
}
err |= open_next_file ();
if (in_stream == NULL)
goto cleanup;
err |= skip (n_bytes_to_skip);
if (in_stream == NULL)
goto cleanup;
pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);
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"),
(unsigned long) 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 * (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"));
exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)