1. Copyright (C) 92, 1995-2002 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software Foundation,
  12. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  13. /* Written by Jim Meyering. */
  14. #include <config.h>
  15. #include <stdio.h>
  16. #include <assert.h>
  17. #include <getopt.h>
  18. #include <sys/types.h>
  19. #include "system.h"
  20. #include "closeout.h"
  21. #include "error.h"
  22. #include "posixver.h"
  23. #include "xstrtol.h"
  24. /* The official name of this program (e.g., no `g' prefix). */
  25. #define PROGRAM_NAME "od"
  26. #define AUTHORS "Jim Meyering"
  27. #if defined(__GNUC__) || defined(STDC_HEADERS)
  28. # include <float.h>
  29. #endif
  30. #ifdef HAVE_LONG_DOUBLE
  31. typedef long double LONG_DOUBLE;
  32. #else
  33. typedef double LONG_DOUBLE;
  34. #endif
  35. /* The default number of input bytes per output line. */
  36. #define DEFAULT_BYTES_PER_BLOCK 16
  37. /* The number of decimal digits of precision in a float. */
  38. #ifndef FLT_DIG
  39. # define FLT_DIG 7
  40. #endif
  41. /* The number of decimal digits of precision in a double. */
  42. #ifndef DBL_DIG
  43. # define DBL_DIG 15
  44. #endif
  45. /* The number of decimal digits of precision in a long double. */
  46. #ifndef LDBL_DIG
  47. # define LDBL_DIG DBL_DIG
  48. #endif
  49. #if HAVE_UNSIGNED_LONG_LONG
  50. typedef unsigned long long ulonglong_t;
  51. #else
  52. /* This is just a place-holder to avoid a few `#if' directives.
  53. In this case, the type isn't actually used. */
  54. typedef unsigned long int ulonglong_t;
  55. #endif
  56. enum size_spec
  57. {
  58. NO_SIZE,
  59. CHAR,
  60. SHORT,
  61. INT,
  62. LONG,
  63. LONG_LONG,
  64. /* FIXME: add INTMAX support, too */
  65. FLOAT_SINGLE,
  66. FLOAT_DOUBLE,
  67. FLOAT_LONG_DOUBLE,
  68. N_SIZE_SPECS
  69. };
  70. enum output_format
  71. {
  72. SIGNED_DECIMAL,
  73. UNSIGNED_DECIMAL,
  74. OCTAL,
  75. HEXADECIMAL,
  76. FLOATING_POINT,
  77. NAMED_CHARACTER,
  78. CHARACTER
  79. };
  80. /* Each output format specification (from `-t spec' or from
  81. old-style options) is represented by one of these structures. */
  82. struct tspec
  83. {
  84. enum output_format fmt;
  85. enum size_spec size;
  86. void (*print_function) (size_t, const char *, const char *);
  87. char *fmt_string;
  88. int hexl_mode_trailer;
  89. int field_width;
  90. };
  91. /* The name this program was run with. */
  92. char *program_name;
  93. /* Convert the number of 8-bit bytes of a binary representation to
  94. the number of characters (digits + sign if the type is signed)
  95. required to represent the same quantity in the specified base/type.
  96. For example, a 32-bit (4-byte) quantity may require a field width
  97. as wide as the following for these types:
  98. 11 unsigned octal
  99. 11 signed decimal
  100. 10 unsigned decimal
  101. 8 unsigned hexadecimal */
  102. static const unsigned int bytes_to_oct_digits[] =
  103. {0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};
  104. static const unsigned int bytes_to_signed_dec_digits[] =
  105. {1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};
  106. static const unsigned int bytes_to_unsigned_dec_digits[] =
  107. {0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};
  108. static const unsigned int bytes_to_hex_digits[] =
  109. {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};
  110. /* Convert enum size_spec to the size of the named type. */
  111. static const int width_bytes[] =
  112. {
  113. -1,
  114. sizeof (char),
  115. sizeof (short int),
  116. sizeof (int),
  117. sizeof (long int),
  118. sizeof (ulonglong_t),
  119. sizeof (float),
  120. sizeof (double),
  121. sizeof (LONG_DOUBLE)
  122. };
  123. /* Ensure that for each member of `enum size_spec' there is an
  124. initializer in the width_bytes array. */
  125. struct dummy
  126. {
  127. int assert_width_bytes_matches_size_spec_decl
  128. [sizeof width_bytes / sizeof width_bytes[0] == N_SIZE_SPECS ? 1 : -1];
  129. };
  130. /* Names for some non-printing characters. */
  131. static const char *const charname[33] =
  132. {
  133. "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
  134. "bs", "ht", "nl", "vt", "ff", "cr", "so", "si",
  135. "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
  136. "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
  137. "sp"
  138. };
  139. /* Address base (8, 10 or 16). */
  140. static int address_base;
  141. /* The number of octal digits required to represent the largest
  142. address value. */
  143. #define MAX_ADDRESS_LENGTH \
  144. ((sizeof (uintmax_t) * CHAR_BIT + CHAR_BIT - 1) / 3)
  145. /* Width of a normal address. */
  146. static int address_pad_len;
  147. static size_t string_min;
  148. static int flag_dump_strings;
  149. /* Non-zero if we should recognize the older non-option arguments
  150. that specified at most one file and optional arguments specifying
  151. offset and pseudo-start address. */
  152. static int traditional;
  153. /* Non-zero if an old-style `pseudo-address' was specified. */
  154. static int flag_pseudo_start;
  155. /* The difference between the old-style pseudo starting address and
  156. the number of bytes to skip. */
  157. static uintmax_t pseudo_offset;
  158. /* Function that accepts an address and an optional following char,
  159. and prints the address and char to stdout. */
  160. static void (*format_address) (uintmax_t, char);
  161. /* The number of input bytes to skip before formatting and writing. */
  162. static uintmax_t n_bytes_to_skip = 0;
  163. /* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
  164. input is formatted. */
  165. static int limit_bytes_to_format = 0;
  166. /* The maximum number of bytes that will be formatted. */
  167. static uintmax_t max_bytes_to_format;
  168. /* The offset of the first byte after the last byte to be formatted. */
  169. static uintmax_t end_offset;
  170. /* When nonzero and two or more consecutive blocks are equal, format
  171. only the first block and output an asterisk alone on the following
  172. line to indicate that identical blocks have been elided. */
  173. static int abbreviate_duplicate_blocks = 1;
  174. /* An array of specs describing how to format each input block. */
  175. static struct tspec *spec;
  176. /* The number of format specs. */
  177. static size_t n_specs;
  178. /* The allocated length of SPEC. */
  179. static size_t n_specs_allocated;
  180. /* The number of input bytes formatted per output line. It must be
  181. a multiple of the least common multiple of the sizes associated with
  182. the specified output types. It should be as large as possible, but
  183. no larger than 16 -- unless specified with the -w option. */
  184. static size_t bytes_per_block;
  185. /* Human-readable representation of *file_list (for error messages).
  186. It differs from *file_list only when *file_list is "-". */
  187. static char const *input_filename;
  188. /* A NULL-terminated list of the file-arguments from the command line. */
  189. static char const *const *file_list;
  190. /* Initializer for file_list if no file-arguments
  191. were specified on the command line. */
  192. static char const *const default_file_list[] = {"-", NULL};
  193. /* The input stream associated with the current file. */
  194. static FILE *in_stream;
  195. /* If nonzero, at least one of the files we read was standard input. */
  196. static int have_read_stdin;
  197. #define MAX_INTEGRAL_TYPE_SIZE sizeof (ulonglong_t)
  198. static enum size_spec integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1];
  199. #define MAX_FP_TYPE_SIZE sizeof(LONG_DOUBLE)
  200. static enum size_spec fp_type_size[MAX_FP_TYPE_SIZE + 1];
  201. #define COMMON_SHORT_OPTIONS "A:N:abcdfhij:lot:vx"
  202. /* For long options that have no equivalent short option, use a
  203. non-character as a pseudo short option, starting with CHAR_MAX + 1. */
  204. enum
  205. {
  206. TRADITIONAL_OPTION = CHAR_MAX + 1
  207. };
  208. static struct option const long_options[] =
  209. {
  210. {"skip-bytes", required_argument, NULL, 'j'},
  211. {"address-radix", required_argument, NULL, 'A'},
  212. {"read-bytes", required_argument, NULL, 'N'},
  213. {"format", required_argument, NULL, 't'},
  214. {"output-duplicates", no_argument, NULL, 'v'},
  215. {"strings", optional_argument, NULL, 's'},
  216. {"traditional", no_argument, NULL, TRADITIONAL_OPTION},
  217. {"width", optional_argument, NULL, 'w'},
  218. {GETOPT_HELP_OPTION_DECL},
  219. {GETOPT_VERSION_OPTION_DECL},
  220. {NULL, 0, NULL, 0}
  221. };
  222. void
  223. usage (int status)
  224. {
  225. if (status != 0)
  226. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  227. program_name);
  228. else
  229. {
  230. printf (_("\
  231. Usage: %s [OPTION]... [FILE]...\n\
  232. or: %s --traditional [FILE] [[+]OFFSET [[+]LABEL]]\n\
  233. "),
  234. program_name, program_name);
  235. fputs (_("\n\
  236. Write an unambiguous representation, octal bytes by default,\n\
  237. of FILE to standard output. With more than one FILE argument,\n\
  238. concatenate them in the listed order to form the input.\n\
  239. With no FILE, or when FILE is -, read standard input.\n\
  240. \n\
  241. "), stdout);
  242. fputs (_("\
  243. All arguments to long options are mandatory for short options.\n\
  244. "), stdout);
  245. fputs (_("\
  246. -A, --address-radix=RADIX decide how file offsets are printed\n\
  247. -j, --skip-bytes=BYTES skip BYTES input bytes first\n\
  248. "), stdout);
  249. fputs (_("\
  250. -N, --read-bytes=BYTES limit dump to BYTES input bytes\n\
  251. -s, --strings[=BYTES] output strings of at least BYTES graphic chars\n\
  252. -t, --format=TYPE select output format or formats\n\
  253. -v, --output-duplicates do not use * to mark line suppression\n\
  254. -w, --width[=BYTES] output BYTES bytes per output line\n\
  255. --traditional accept arguments in traditional form\n\
  256. "), stdout);
  257. fputs (HELP_OPTION_DESCRIPTION, stdout);
  258. fputs (VERSION_OPTION_DESCRIPTION, stdout);
  259. fputs (_("\
  260. \n\
  261. Traditional format specifications may be intermixed; they accumulate:\n\
  262. -a same as -t a, select named characters\n\
  263. -b same as -t oC, select octal bytes\n\
  264. -c same as -t c, select ASCII characters or backslash escapes\n\
  265. -d same as -t u2, select unsigned decimal shorts\n\
  266. "), stdout);
  267. fputs (_("\
  268. -f same as -t fF, select floats\n\
  269. -h same as -t x2, select hexadecimal shorts\n\
  270. -i same as -t d2, select decimal shorts\n\
  271. -l same as -t d4, select decimal longs\n\
  272. -o same as -t o2, select octal shorts\n\
  273. -x same as -t x2, select hexadecimal shorts\n\
  274. "), stdout);
  275. fputs (_("\
  276. \n\
  277. For older syntax (second call format), OFFSET means -j OFFSET. LABEL\n\
  278. is the pseudo-address at first byte printed, incremented when dump is\n\
  279. progressing. For OFFSET and LABEL, a 0x or 0X prefix indicates\n\
  280. hexadecimal, suffixes may be . for octal and b for multiply by 512.\n\
  281. \n\
  282. TYPE is made up of one or more of these specifications:\n\
  283. \n\
  284. a named character\n\
  285. c ASCII character or backslash escape\n\
  286. "), stdout);
  287. fputs (_("\
  288. d[SIZE] signed decimal, SIZE bytes per integer\n\
  289. f[SIZE] floating point, SIZE bytes per integer\n\
  290. o[SIZE] octal, SIZE bytes per integer\n\
  291. u[SIZE] unsigned decimal, SIZE bytes per integer\n\
  292. x[SIZE] hexadecimal, SIZE bytes per integer\n\
  293. "), stdout);
  294. fputs (_("\
  295. \n\
  296. SIZE is a number. For TYPE in doux, SIZE may also be C for\n\
  297. sizeof(char), S for sizeof(short), I for sizeof(int) or L for\n\
  298. sizeof(long). If TYPE is f, SIZE may also be F for sizeof(float), D\n\
  299. for sizeof(double) or L for sizeof(long double).\n\
  300. "), stdout);
  301. fputs (_("\
  302. \n\
  303. RADIX is d for decimal, o for octal, x for hexadecimal or n for none.\n\
  304. BYTES is hexadecimal with 0x or 0X prefix, it is multiplied by 512\n\
  305. with b suffix, by 1024 with k and by 1048576 with m. Adding a z suffix to\n\
  306. any type adds a display of printable characters to the end of each line\n\
  307. of output. \
  308. "), stdout);
  309. fputs (_("\
  310. --string without a number implies 3. --width without a number\n\
  311. implies 32. By default, od uses -A o -t d2 -w 16.\n\
  312. "), stdout);
  313. printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
  314. }
  315. exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  316. }
  317. /* Compute the greatest common denominator of U and V
  318. using Euclid's algorithm. */
  319. static unsigned int
  320. gcd (unsigned int u, unsigned int v)
  321. {
  322. unsigned int t;
  323. while (v != 0)
  324. {
  325. t = u % v;
  326. u = v;
  327. v = t;
  328. }
  329. return u;
  330. }
  331. /* Compute the least common multiple of U and V. */
  332. static unsigned int
  333. lcm (unsigned int u, unsigned int v)
  334. {
  335. unsigned int t = gcd (u, v);
  336. if (t == 0)
  337. return 0;
  338. return u * v / t;
  339. }
  340. static void
  341. print_s_char (size_t n_bytes, const char *block, const char *fmt_string)
  342. {
  343. size_t i;
  344. for (i = n_bytes; i > 0; i--)
  345. {
  346. int tmp = (unsigned) *(const unsigned char *) block;
  347. if (tmp > SCHAR_MAX)
  348. tmp -= SCHAR_MAX - SCHAR_MIN + 1;
  349. assert (tmp <= SCHAR_MAX);
  350. printf (fmt_string, tmp);
  351. block += sizeof (unsigned char);
  352. }
  353. }
  354. static void
  355. print_char (size_t n_bytes, const char *block, const char *fmt_string)
  356. {
  357. size_t i;
  358. for (i = n_bytes; i > 0; i--)
  359. {
  360. unsigned int tmp = *(const unsigned char *) block;
  361. printf (fmt_string, tmp);
  362. block += sizeof (unsigned char);
  363. }
  364. }
  365. static void
  366. print_s_short (size_t n_bytes, const char *block, const char *fmt_string)
  367. {
  368. size_t i;
  369. for (i = n_bytes / sizeof (unsigned short); i > 0; i--)
  370. {
  371. int tmp = (unsigned) *(const unsigned short *) block;
  372. if (tmp > SHRT_MAX)
  373. tmp -= SHRT_MAX - SHRT_MIN + 1;
  374. assert (tmp <= SHRT_MAX);
  375. printf (fmt_string, tmp);
  376. block += sizeof (unsigned short);
  377. }
  378. }
  379. static void
  380. print_short (size_t n_bytes, const char *block, const char *fmt_string)
  381. {
  382. size_t i;
  383. for (i = n_bytes / sizeof (unsigned short); i > 0; i--)
  384. {
  385. unsigned int tmp = *(const unsigned short *) block;
  386. printf (fmt_string, tmp);
  387. block += sizeof (unsigned short);
  388. }
  389. }
  390. static void
  391. print_int (size_t n_bytes, const char *block, const char *fmt_string)
  392. {
  393. size_t i;
  394. for (i = n_bytes / sizeof (unsigned int); i > 0; i--)
  395. {
  396. unsigned int tmp = *(const unsigned int *) block;
  397. printf (fmt_string, tmp);
  398. block += sizeof (unsigned int);
  399. }
  400. }
  401. static void
  402. print_long (size_t n_bytes, const char *block, const char *fmt_string)
  403. {
  404. size_t i;
  405. for (i = n_bytes / sizeof (unsigned long); i > 0; i--)
  406. {
  407. unsigned long tmp = *(const unsigned long *) block;
  408. printf (fmt_string, tmp);
  409. block += sizeof (unsigned long);
  410. }
  411. }
  412. static void
  413. print_long_long (size_t n_bytes, const char *block, const char *fmt_string)
  414. {
  415. size_t i;
  416. for (i = n_bytes / sizeof (ulonglong_t); i > 0; i--)
  417. {
  418. ulonglong_t tmp = *(const ulonglong_t *) block;
  419. printf (fmt_string, tmp);
  420. block += sizeof (ulonglong_t);
  421. }
  422. }
  423. static void
  424. print_float (size_t n_bytes, const char *block, const char *fmt_string)
  425. {
  426. size_t i;
  427. for (i = n_bytes / sizeof (float); i > 0; i--)
  428. {
  429. float tmp = *(const float *) block;
  430. printf (fmt_string, tmp);
  431. block += sizeof (float);
  432. }
  433. }
  434. static void
  435. print_double (size_t n_bytes, const char *block, const char *fmt_string)
  436. {
  437. size_t i;
  438. for (i = n_bytes / sizeof (double); i > 0; i--)
  439. {
  440. double tmp = *(const double *) block;
  441. printf (fmt_string, tmp);
  442. block += sizeof (double);
  443. }
  444. }
  445. #ifdef HAVE_LONG_DOUBLE
  446. static void
  447. print_long_double (size_t n_bytes, const char *block, const char *fmt_string)
  448. {
  449. size_t i;
  450. for (i = n_bytes / sizeof (LONG_DOUBLE); i > 0; i--)
  451. {
  452. LONG_DOUBLE tmp = *(const LONG_DOUBLE *) block;
  453. printf (fmt_string, tmp);
  454. block += sizeof (LONG_DOUBLE);
  455. }
  456. }
  457. #endif
  458. static void
  459. dump_hexl_mode_trailer (size_t n_bytes, const char *block)
  460. {
  461. size_t i;
  462. fputs (" >", stdout);
  463. for (i = n_bytes; i > 0; i--)
  464. {
  465. unsigned int c = *(const unsigned char *) block;
  466. unsigned int c2 = (ISPRINT(c) ? c : '.');
  467. putchar (c2);
  468. block += sizeof (unsigned char);
  469. }
  470. putchar ('<');
  471. }
  472. static void
  473. print_named_ascii (size_t n_bytes, const char *block,
  474. const char *unused_fmt_string ATTRIBUTE_UNUSED)
  475. {
  476. size_t i;
  477. for (i = n_bytes; i > 0; i--)
  478. {
  479. unsigned int c = *(const unsigned char *) block;
  480. unsigned int masked_c = (0x7f & c);
  481. const char *s;
  482. char buf[5];
  483. if (masked_c == 127)
  484. s = "del";
  485. else if (masked_c <= 040)
  486. s = charname[masked_c];
  487. else
  488. {
  489. sprintf (buf, " %c", masked_c);
  490. s = buf;
  491. }
  492. printf (" %3s", s);
  493. block += sizeof (unsigned char);
  494. }
  495. }
  496. static void
  497. print_ascii (size_t n_bytes, const char *block,
  498. const char *unused_fmt_string ATTRIBUTE_UNUSED)
  499. {
  500. size_t i;
  501. for (i = n_bytes; i > 0; i--)
  502. {
  503. unsigned int c = *(const unsigned char *) block;
  504. const char *s;
  505. char buf[5];
  506. switch (c)
  507. {
  508. case '\0':
  509. s = " \\0";
  510. break;
  511. case '\007':
  512. s = " \\a";
  513. break;
  514. case '\b':
  515. s = " \\b";
  516. break;
  517. case '\f':
  518. s = " \\f";
  519. break;
  520. case '\n':
  521. s = " \\n";
  522. break;
  523. case '\r':
  524. s = " \\r";
  525. break;
  526. case '\t':
  527. s = " \\t";
  528. break;
  529. case '\v':
  530. s = " \\v";
  531. break;
  532. default:
  533. sprintf (buf, (ISPRINT (c) ? " %c" : "%03o"), c);
  534. s = (const char *) buf;
  535. }
  536. printf (" %3s", s);
  537. block += sizeof (unsigned char);
  538. }
  539. }
  540. /* Convert a null-terminated (possibly zero-length) string S to an
  541. unsigned long integer value. If S points to a non-digit set *P to S,
  542. *VAL to 0, and return 0. Otherwise, accumulate the integer value of
  543. the string of digits. If the string of digits represents a value
  544. larger than ULONG_MAX, don't modify *VAL or *P and return nonzero.
  545. Otherwise, advance *P to the first non-digit after S, set *VAL to
  546. the result of the conversion and return zero. */
  547. static int
  548. simple_strtoul (const char *s, const char **p, long unsigned int *val)
  549. {
  550. unsigned long int sum;
  551. sum = 0;
  552. while (ISDIGIT (*s))
  553. {
  554. unsigned int c = *s++ - '0';
  555. if (sum > (ULONG_MAX - c) / 10)
  556. return 1;
  557. sum = sum * 10 + c;
  558. }
  559. *p = s;
  560. *val = sum;
  561. return 0;
  562. }
  563. /* If S points to a single valid modern od format string, put
  564. a description of that format in *TSPEC, make *NEXT point at the
  565. character following the just-decoded format (if *NEXT is non-NULL),
  566. and return zero. If S is not valid, don't modify *NEXT or *TSPEC,
  567. give a diagnostic, and return nonzero. For example, if S were
  568. "d4afL" *NEXT would be set to "afL" and *TSPEC would be
  569. {
  570. fmt = SIGNED_DECIMAL;
  571. size = INT or LONG; (whichever integral_type_size[4] resolves to)
  572. print_function = print_int; (assuming size == INT)
  573. fmt_string = "%011d%c";
  574. }
  575. S_ORIG is solely for reporting errors. It should be the full format
  576. string argument.
  577. */
  578. static int
  579. decode_one_format (const char *s_orig, const char *s, const char **next,
  580. struct tspec *tspec)
  581. {
  582. enum size_spec size_spec;
  583. unsigned long int size;
  584. enum output_format fmt;
  585. const char *pre_fmt_string;
  586. char *fmt_string;
  587. void (*print_function) (size_t, const char *, const char *);
  588. const char *p;
  589. unsigned int c;
  590. unsigned int field_width = 0;
  591. assert (tspec != NULL);
  592. switch (*s)
  593. {
  594. case 'd':
  595. case 'o':
  596. case 'u':
  597. case 'x':
  598. c = *s;
  599. ++s;
  600. switch (*s)
  601. {
  602. case 'C':
  603. ++s;
  604. size = sizeof (char);
  605. break;
  606. case 'S':
  607. ++s;
  608. size = sizeof (short);
  609. break;
  610. case 'I':
  611. ++s;
  612. size = sizeof (int);
  613. break;
  614. case 'L':
  615. ++s;
  616. size = sizeof (long int);
  617. break;
  618. default:
  619. if (simple_strtoul (s, &p, &size) != 0)
  620. {
  621. /* The integer at P in S would overflow an unsigned long.
  622. A digit string that long is sufficiently odd looking
  623. that the following diagnostic is sufficient. */
  624. error (0, 0, _("invalid type string `%s'"), s_orig);
  625. return 1;
  626. }
  627. if (p == s)
  628. size = sizeof (int);
  629. else
  630. {
  631. if (MAX_INTEGRAL_TYPE_SIZE < size
  632. || integral_type_size[size] == NO_SIZE)
  633. {
  634. error (0, 0, _("invalid type string `%s';\n\
  635. this system doesn't provide a %lu-byte integral type"), s_orig, size);
  636. return 1;
  637. }
  638. s = p;
  639. }
  640. break;
  641. }
  642. #define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
  643. ((Spec) == LONG_LONG ? (Max_format) \
  644. : ((Spec) == LONG ? (Long_format) \
  645. : (Min_format))) \
  646. #define FMT_BYTES_ALLOCATED 9
  647. fmt_string = xmalloc (FMT_BYTES_ALLOCATED);
  648. size_spec = integral_type_size[size];
  649. switch (c)
  650. {
  651. case 'd':
  652. fmt = SIGNED_DECIMAL;
  653. sprintf (fmt_string, " %%%u%s",
  654. (field_width = bytes_to_signed_dec_digits[size]),
  655. ISPEC_TO_FORMAT (size_spec, "d", "ld", PRIdMAX));
  656. break;
  657. case 'o':
  658. fmt = OCTAL;
  659. sprintf (fmt_string, " %%0%u%s",
  660. (field_width = bytes_to_oct_digits[size]),
  661. ISPEC_TO_FORMAT (size_spec, "o", "lo", PRIoMAX));
  662. break;
  663. case 'u':
  664. fmt = UNSIGNED_DECIMAL;
  665. sprintf (fmt_string, " %%%u%s",
  666. (field_width = bytes_to_unsigned_dec_digits[size]),
  667. ISPEC_TO_FORMAT (size_spec, "u", "lu", PRIuMAX));
  668. break;
  669. case 'x':
  670. fmt = HEXADECIMAL;
  671. sprintf (fmt_string, " %%0%u%s",
  672. (field_width = bytes_to_hex_digits[size]),
  673. ISPEC_TO_FORMAT (size_spec, "x", "lx", PRIxMAX));
  674. break;
  675. default:
  676. abort ();
  677. }
  678. assert (strlen (fmt_string) < FMT_BYTES_ALLOCATED);
  679. switch (size_spec)
  680. {
  681. case CHAR:
  682. print_function = (fmt == SIGNED_DECIMAL
  683. ? print_s_char
  684. : print_char);
  685. break;
  686. case SHORT:
  687. print_function = (fmt == SIGNED_DECIMAL
  688. ? print_s_short
  689. : print_short);
  690. break;
  691. case INT:
  692. print_function = print_int;
  693. break;
  694. case LONG:
  695. print_function = print_long;
  696. break;
  697. case LONG_LONG:
  698. print_function = print_long_long;
  699. break;
  700. default:
  701. abort ();
  702. }
  703. break;
  704. case 'f':
  705. fmt = FLOATING_POINT;
  706. ++s;
  707. switch (*s)
  708. {
  709. case 'F':
  710. ++s;
  711. size = sizeof (float);
  712. break;
  713. case 'D':
  714. ++s;
  715. size = sizeof (double);
  716. break;
  717. case 'L':
  718. ++s;
  719. size = sizeof (LONG_DOUBLE);
  720. break;
  721. default:
  722. if (simple_strtoul (s, &p, &size) != 0)
  723. {
  724. /* The integer at P in S would overflow an unsigned long.
  725. A digit string that long is sufficiently odd looking
  726. that the following diagnostic is sufficient. */
  727. error (0, 0, _("invalid type string `%s'"), s_orig);
  728. return 1;
  729. }
  730. if (p == s)
  731. size = sizeof (double);
  732. else
  733. {
  734. if (size > MAX_FP_TYPE_SIZE
  735. || fp_type_size[size] == NO_SIZE)
  736. {
  737. error (0, 0, _("invalid type string `%s';\n\
  738. this system doesn't provide a %lu-byte floating point type"), s_orig, size);
  739. return 1;
  740. }
  741. s = p;
  742. }
  743. break;
  744. }
  745. size_spec = fp_type_size[size];
  746. switch (size_spec)
  747. {
  748. case FLOAT_SINGLE:
  749. print_function = print_float;
  750. /* Don't use %#e; not all systems support it. */
  751. pre_fmt_string = " %%%d.%de";
  752. fmt_string = xmalloc (strlen (pre_fmt_string));
  753. sprintf (fmt_string, pre_fmt_string,
  754. (field_width = FLT_DIG + 8), FLT_DIG);
  755. break;
  756. case FLOAT_DOUBLE:
  757. print_function = print_double;
  758. pre_fmt_string = " %%%d.%de";
  759. fmt_string = xmalloc (strlen (pre_fmt_string));
  760. sprintf (fmt_string, pre_fmt_string,
  761. (field_width = DBL_DIG + 8), DBL_DIG);
  762. break;
  763. #ifdef HAVE_LONG_DOUBLE
  764. case FLOAT_LONG_DOUBLE:
  765. print_function = print_long_double;
  766. pre_fmt_string = " %%%d.%dLe";
  767. fmt_string = xmalloc (strlen (pre_fmt_string));
  768. sprintf (fmt_string, pre_fmt_string,
  769. (field_width = LDBL_DIG + 8), LDBL_DIG);
  770. break;
  771. #endif
  772. default:
  773. abort ();
  774. }
  775. break;
  776. case 'a':
  777. ++s;
  778. fmt = NAMED_CHARACTER;
  779. size_spec = CHAR;
  780. fmt_string = NULL;
  781. print_function = print_named_ascii;
  782. field_width = 3;
  783. break;
  784. case 'c':
  785. ++s;
  786. fmt = CHARACTER;
  787. size_spec = CHAR;
  788. fmt_string = NULL;
  789. print_function = print_ascii;
  790. field_width = 3;
  791. break;
  792. default:
  793. error (0, 0, _("invalid character `%c' in type string `%s'"),
  794. *s, s_orig);
  795. return 1;
  796. }
  797. tspec->size = size_spec;
  798. tspec->fmt = fmt;
  799. tspec->print_function = print_function;
  800. tspec->fmt_string = fmt_string;
  801. tspec->field_width = field_width;
  802. tspec->hexl_mode_trailer = (*s == 'z');
  803. if (tspec->hexl_mode_trailer)
  804. s++;
  805. if (next != NULL)
  806. *next = s;
  807. return 0;
  808. }
  809. /* Given a list of one or more input filenames FILE_LIST, set the global
  810. file pointer IN_STREAM and the global string INPUT_FILENAME to the
  811. first one that can be successfully opened. Modify FILE_LIST to
  812. reference the next filename in the list. A file name of "-" is
  813. interpreted as standard input. If any file open fails, give an error
  814. message and return nonzero. */
  815. static int
  816. open_next_file (void)
  817. {
  818. int err = 0;
  819. do
  820. {
  821. input_filename = *file_list;
  822. if (input_filename == NULL)
  823. return err;
  824. ++file_list;
  825. if (STREQ (input_filename, "-"))
  826. {
  827. input_filename = _("standard input");
  828. in_stream = stdin;
  829. have_read_stdin = 1;
  830. }
  831. else
  832. {
  833. in_stream = fopen (input_filename, "r");
  834. if (in_stream == NULL)
  835. {
  836. error (0, errno, "%s", input_filename);
  837. err = 1;
  838. }
  839. }
  840. }
  841. while (in_stream == NULL);
  842. if (limit_bytes_to_format && !flag_dump_strings)
  843. SETVBUF (in_stream, NULL, _IONBF, 0);
  844. SET_BINARY (fileno (in_stream));
  845. return err;
  846. }
  847. /* Test whether there have been errors on in_stream, and close it if
  848. it is not standard input. Return nonzero if there has been an error
  849. on in_stream or stdout; return zero otherwise. This function will
  850. report more than one error only if both a read and a write error
  851. have occurred. */
  852. static int
  853. check_and_close (void)
  854. {
  855. int err = 0;
  856. if (in_stream != NULL)
  857. {
  858. if (ferror (in_stream))
  859. {
  860. error (0, errno, "%s", input_filename);
  861. if (in_stream != stdin)
  862. fclose (in_stream);
  863. err = 1;
  864. }
  865. else if (in_stream != stdin && fclose (in_stream) == EOF)
  866. {
  867. error (0, errno, "%s", input_filename);
  868. err = 1;
  869. }
  870. in_stream = NULL;
  871. }
  872. if (ferror (stdout))
  873. {
  874. error (0, errno, _("standard output"));
  875. err = 1;
  876. }
  877. return err;
  878. }
  879. /* Decode the modern od format string S. Append the decoded
  880. representation to the global array SPEC, reallocating SPEC if
  881. necessary. Return zero if S is valid, nonzero otherwise. */
  882. static int
  883. decode_format_string (const char *s)
  884. {
  885. const char *s_orig = s;
  886. assert (s != NULL);
  887. while (*s != '\0')
  888. {
  889. struct tspec tspec;
  890. const char *next;
  891. if (decode_one_format (s_orig, s, &next, &tspec))
  892. return 1;
  893. assert (s != next);
  894. s = next;
  895. if (n_specs >= n_specs_allocated)
  896. {
  897. n_specs_allocated = 1 + (3 * n_specs_allocated) / 2;
  898. spec = (struct tspec *) xrealloc ((char *) spec,
  899. (n_specs_allocated
  900. * sizeof (struct tspec)));
  901. }
  902. memcpy ((char *) &spec[n_specs], (char *) &tspec,
  903. sizeof (struct tspec));
  904. ++n_specs;
  905. }
  906. return 0;
  907. }
  908. /* Given a list of one or more input filenames FILE_LIST, set the global
  909. file pointer IN_STREAM to position N_SKIP in the concatenation of
  910. those files. If any file operation fails or if there are fewer than
  911. N_SKIP bytes in the combined input, give an error message and return
  912. nonzero. When possible, use seek rather than read operations to
  913. advance IN_STREAM. */
  914. static int
  915. skip (uintmax_t n_skip)
  916. {
  917. int err = 0;
  918. if (n_skip == 0)
  919. return 0;
  920. while (in_stream != NULL) /* EOF. */
  921. {
  922. struct stat file_stats;
  923. /* First try seeking. For large offsets, this extra work is
  924. worthwhile. If the offset is below some threshold it may be
  925. more efficient to move the pointer by reading. There are two
  926. issues when trying to seek:
  927. - the file must be seekable.
  928. - before seeking to the specified position, make sure
  929. that the new position is in the current file.
  930. Try to do that by getting file's size using fstat.
  931. But that will work only for regular files. */
  932. if (fstat (fileno (in_stream), &file_stats) == 0)
  933. {
  934. /* The st_size field is valid only for regular files
  935. (and for symbolic links, which cannot occur here).
  936. If the number of bytes left to skip is at least
  937. as large as the size of the current file, we can
  938. decrement n_skip and go on to the next file. */
  939. if (S_ISREG (file_stats.st_mode) && 0 <= file_stats.st_size)
  940. {
  941. if ((uintmax_t) file_stats.st_size <= n_skip)
  942. n_skip -= file_stats.st_size;
  943. else
  944. {
  945. if (fseeko (in_stream, n_skip, SEEK_CUR) != 0)
  946. {
  947. error (0, errno, "%s", input_filename);
  948. err = 1;
  949. }
  950. n_skip = 0;
  951. }
  952. }
  953. /* If it's not a regular file with nonnegative size,
  954. position the file pointer by reading. */
  955. else
  956. {
  957. char buf[BUFSIZ];
  958. size_t n_bytes_read, n_bytes_to_read = BUFSIZ;
  959. while (0 < n_skip)
  960. {
  961. if (n_skip < n_bytes_to_read)
  962. n_bytes_to_read = n_skip;
  963. n_bytes_read = fread (buf, 1, n_bytes_to_read, in_stream);
  964. n_skip -= n_bytes_read;
  965. if (n_bytes_read != n_bytes_to_read)
  966. break;
  967. }
  968. }
  969. if (n_skip == 0)
  970. break;
  971. }
  972. else /* cannot fstat() file */
  973. {
  974. error (0, errno, "%s", input_filename);
  975. err = 1;
  976. }
  977. err |= check_and_close ();
  978. err |= open_next_file ();
  979. }
  980. if (n_skip != 0)
  981. error (EXIT_FAILURE, 0, _("cannot skip past end of combined input"));
  982. return err;
  983. }
  984. static void
  985. format_address_none (uintmax_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
  986. {
  987. }
  988. static void
  989. format_address_std (uintmax_t address, char c)
  990. {
  991. char buf[MAX_ADDRESS_LENGTH + 2];
  992. char *p = buf + sizeof buf;
  993. char const *pbound;
  994. *--p = '\0';
  995. *--p = c;
  996. pbound = p - address_pad_len;
  997. /* Use a special case of the code for each base. This is measurably
  998. faster than generic code. */
  999. switch (address_base)
  1000. {
  1001. case 8:
  1002. do
  1003. *--p = '0' + (address & 7);
  1004. while ((address >>= 3) != 0);
  1005. break;
  1006. case 10:
  1007. do
  1008. *--p = '0' + (address % 10);
  1009. while ((address /= 10) != 0);
  1010. break;
  1011. case 16:
  1012. do
  1013. *--p = "0123456789abcdef"[address & 15];
  1014. while ((address >>= 4) != 0);
  1015. break;
  1016. }
  1017. while (pbound < p)
  1018. *--p = '0';
  1019. fputs (p, stdout);
  1020. }
  1021. static void
  1022. format_address_paren (uintmax_t address, char c)
  1023. {
  1024. putchar ('(');
  1025. format_address_std (address, ')');
  1026. putchar (c);
  1027. }
  1028. static void
  1029. format_address_label (uintmax_t address, char c)
  1030. {
  1031. format_address_std (address, ' ');
  1032. format_address_paren (address + pseudo_offset, c);
  1033. }
  1034. /* Write N_BYTES bytes from CURR_BLOCK to standard output once for each
  1035. of the N_SPEC format specs. CURRENT_OFFSET is the byte address of
  1036. CURR_BLOCK in the concatenation of input files, and it is printed
  1037. (optionally) only before the output line associated with the first
  1038. format spec. When duplicate blocks are being abbreviated, the output
  1039. for a sequence of identical input blocks is the output for the first
  1040. block followed by an asterisk alone on a line. It is valid to compare
  1041. the blocks PREV_BLOCK and CURR_BLOCK only when N_BYTES == BYTES_PER_BLOCK.
  1042. That condition may be false only for the last input block -- and then
  1043. only when it has not been padded to length BYTES_PER_BLOCK. */
  1044. static void
  1045. write_block (uintmax_t current_offset, size_t n_bytes,
  1046. const char *prev_block, const char *curr_block)
  1047. {
  1048. static int first = 1;
  1049. static int prev_pair_equal = 0;
  1050. #define EQUAL_BLOCKS(b1, b2) (memcmp ((b1), (b2), bytes_per_block) == 0)
  1051. if (abbreviate_duplicate_blocks
  1052. && !first && n_bytes == bytes_per_block
  1053. && EQUAL_BLOCKS (prev_block, curr_block))
  1054. {
  1055. if (prev_pair_equal)
  1056. {
  1057. /* The two preceding blocks were equal, and the current
  1058. block is the same as the last one, so print nothing. */
  1059. }
  1060. else
  1061. {
  1062. printf ("*\n");
  1063. prev_pair_equal = 1;
  1064. }
  1065. }
  1066. else
  1067. {
  1068. size_t i;
  1069. prev_pair_equal = 0;
  1070. for (i = 0; i < n_specs; i++)
  1071. {
  1072. if (i == 0)
  1073. format_address (current_offset, '\0');
  1074. else
  1075. printf ("%*s", address_pad_len, "");
  1076. (*spec[i].print_function) (n_bytes, curr_block, spec[i].fmt_string);
  1077. if (spec[i].hexl_mode_trailer)
  1078. {
  1079. /* space-pad out to full line width, then dump the trailer */
  1080. int datum_width = width_bytes[spec[i].size];
  1081. int blank_fields = (bytes_per_block - n_bytes) / datum_width;
  1082. int field_width = spec[i].field_width + 1;
  1083. printf ("%*s", blank_fields * field_width, "");
  1084. dump_hexl_mode_trailer (n_bytes, curr_block);
  1085. }
  1086. putchar ('\n');
  1087. }
  1088. }
  1089. first = 0;
  1090. }
  1091. /* Read a single byte into *C from the concatenation of the input files
  1092. named in the global array FILE_LIST. On the first call to this
  1093. function, the global variable IN_STREAM is expected to be an open
  1094. stream associated with the input file INPUT_FILENAME. If IN_STREAM
  1095. is at end-of-file, close it and update the global variables IN_STREAM
  1096. and INPUT_FILENAME so they correspond to the next file in the list.
  1097. Then try to read a byte from the newly opened file. Repeat if
  1098. necessary until EOF is reached for the last file in FILE_LIST, then
  1099. set *C to EOF and return. Subsequent calls do likewise. The return
  1100. value is nonzero if any errors occured, zero otherwise. */
  1101. static int
  1102. read_char (int *c)
  1103. {
  1104. int err = 0;
  1105. *c = EOF;
  1106. while (in_stream != NULL) /* EOF. */
  1107. {
  1108. *c = fgetc (in_stream);
  1109. if (*c != EOF)
  1110. break;
  1111. err |= check_and_close ();
  1112. err |= open_next_file ();
  1113. }
  1114. return err;
  1115. }
  1116. /* Read N bytes into BLOCK from the concatenation of the input files
  1117. named in the global array FILE_LIST. On the first call to this
  1118. function, the global variable IN_STREAM is expected to be an open
  1119. stream associated with the input file INPUT_FILENAME. If all N
  1120. bytes cannot be read from IN_STREAM, close IN_STREAM and update
  1121. the global variables IN_STREAM and INPUT_FILENAME. Then try to
  1122. read the remaining bytes from the newly opened file. Repeat if
  1123. necessary until EOF is reached for the last file in FILE_LIST.
  1124. On subsequent calls, don't modify BLOCK and return zero. Set
  1125. *N_BYTES_IN_BUFFER to the number of bytes read. If an error occurs,
  1126. it will be detected through ferror when the stream is about to be
  1127. closed. If there is an error, give a message but continue reading
  1128. as usual and return nonzero. Otherwise return zero. */
  1129. static int
  1130. read_block (size_t n, char *block, size_t *n_bytes_in_buffer)
  1131. {
  1132. int err = 0;
  1133. assert (0 < n && n <= bytes_per_block);
  1134. *n_bytes_in_buffer = 0;
  1135. if (n == 0)
  1136. return 0;
  1137. while (in_stream != NULL) /* EOF. */
  1138. {
  1139. size_t n_needed;
  1140. size_t n_read;
  1141. n_needed = n - *n_bytes_in_buffer;
  1142. n_read = fread (block + *n_bytes_in_buffer, 1, n_needed, in_stream);
  1143. *n_bytes_in_buffer += n_read;
  1144. if (n_read == n_needed)
  1145. break;
  1146. err |= check_and_close ();
  1147. err |= open_next_file ();
  1148. }
  1149. return err;
  1150. }
  1151. /* Return the least common multiple of the sizes associated
  1152. with the format specs. */
  1153. static int
  1154. get_lcm (void)
  1155. {
  1156. size_t i;
  1157. int l_c_m = 1;
  1158. for (i = 0; i < n_specs; i++)
  1159. l_c_m = lcm (l_c_m, width_bytes[(int) spec[i].size]);
  1160. return l_c_m;
  1161. }
  1162. /* If S is a valid traditional offset specification with an optional
  1163. leading '+' return nonzero and set *OFFSET to the offset it denotes. */
  1164. static int
  1165. parse_old_offset (const char *s, uintmax_t *offset)
  1166. {
  1167. int radix;
  1168. enum strtol_error s_err;
  1169. if (*s == '\0')
  1170. return 0;
  1171. /* Skip over any leading '+'. */
  1172. if (s[0] == '+')
  1173. ++s;
  1174. /* Determine the radix we'll use to interpret S. If there is a `.',
  1175. it's decimal, otherwise, if the string begins with `0X'or `0x',
  1176. it's hexadecimal, else octal. */
  1177. if (strchr (s, '.') != NULL)
  1178. radix = 10;
  1179. else
  1180. {
  1181. if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  1182. radix = 16;
  1183. else
  1184. radix = 8;
  1185. }
  1186. s_err = xstrtoumax (s, NULL, radix, offset, "Bb");
  1187. if (s_err != LONGINT_OK)
  1188. {
  1189. STRTOL_FAIL_WARN (s, _("old-style offset"), s_err);
  1190. return 0;
  1191. }
  1192. return 1;
  1193. }
  1194. /* Read a chunk of size BYTES_PER_BLOCK from the input files, write the
  1195. formatted block to standard output, and repeat until the specified
  1196. maximum number of bytes has been read or until all input has been
  1197. processed. If the last block read is smaller than BYTES_PER_BLOCK
  1198. and its size is not a multiple of the size associated with a format
  1199. spec, extend the input block with zero bytes until its length is a
  1200. multiple of all format spec sizes. Write the final block. Finally,
  1201. write on a line by itself the offset of the byte after the last byte
  1202. read. Accumulate return values from calls to read_block and
  1203. check_and_close, and if any was nonzero, return nonzero.
  1204. Otherwise, return zero. */
  1205. static int
  1206. dump (void)
  1207. {
  1208. char *block[2];
  1209. uintmax_t current_offset;
  1210. int idx;
  1211. int err;
  1212. size_t n_bytes_read;
  1213. block[0] = (char *) alloca (bytes_per_block);
  1214. block[1] = (char *) alloca (bytes_per_block);
  1215. current_offset = n_bytes_to_skip;
  1216. idx = 0;
  1217. err = 0;
  1218. if (limit_bytes_to_format)
  1219. {
  1220. while (1)
  1221. {
  1222. size_t n_needed;
  1223. if (current_offset >= end_offset)
  1224. {
  1225. n_bytes_read = 0;
  1226. break;
  1227. }
  1228. n_needed = MIN (end_offset - current_offset,
  1229. (uintmax_t) bytes_per_block);
  1230. err |= read_block (n_needed, block[idx], &n_bytes_read);
  1231. if (n_bytes_read < bytes_per_block)
  1232. break;
  1233. assert (n_bytes_read == bytes_per_block);
  1234. write_block (current_offset, n_bytes_read,
  1235. block[!idx], block[idx]);
  1236. current_offset += n_bytes_read;
  1237. idx = !idx;
  1238. }
  1239. }
  1240. else
  1241. {
  1242. while (1)
  1243. {
  1244. err |= read_block (bytes_per_block, block[idx], &n_bytes_read);
  1245. if (n_bytes_read < bytes_per_block)
  1246. break;
  1247. assert (n_bytes_read == bytes_per_block);
  1248. write_block (current_offset, n_bytes_read,
  1249. block[!idx], block[idx]);
  1250. current_offset += n_bytes_read;
  1251. idx = !idx;
  1252. }
  1253. }
  1254. if (n_bytes_read > 0)
  1255. {
  1256. int l_c_m;
  1257. size_t bytes_to_write;
  1258. l_c_m = get_lcm ();
  1259. /* Make bytes_to_write the smallest multiple of l_c_m that
  1260. is at least as large as n_bytes_read. */
  1261. bytes_to_write = l_c_m * ((n_bytes_read + l_c_m - 1) / l_c_m);
  1262. memset (block[idx] + n_bytes_read, 0, bytes_to_write - n_bytes_read);
  1263. write_block (current_offset, bytes_to_write,
  1264. block[!idx], block[idx]);
  1265. current_offset += n_bytes_read;
  1266. }
  1267. format_address (current_offset, '\n');
  1268. if (limit_bytes_to_format && current_offset >= end_offset)
  1269. err |= check_and_close ();
  1270. return err;
  1271. }
  1272. /* STRINGS mode. Find each "string constant" in the input.
  1273. A string constant is a run of at least `string_min' ASCII
  1274. graphic (or formatting) characters terminated by a null.
  1275. Based on a function written by Richard Stallman for a
  1276. traditional version of od. Return nonzero if an error
  1277. occurs. Otherwise, return zero. */
  1278. static int
  1279. dump_strings (void)
  1280. {
  1281. size_t bufsize = MAX (100, string_min);
  1282. char *buf = xmalloc (bufsize);
  1283. uintmax_t address = n_bytes_to_skip;
  1284. int err;
  1285. err = 0;
  1286. while (1)
  1287. {
  1288. size_t i;
  1289. int c;
  1290. /* See if the next `string_min' chars are all printing chars. */
  1291. tryline:
  1292. if (limit_bytes_to_format
  1293. && (end_offset < string_min || end_offset - string_min <= address))
  1294. break;
  1295. for (i = 0; i < string_min; i++)
  1296. {
  1297. err |= read_char (&c);
  1298. address++;
  1299. if (c < 0)
  1300. {
  1301. free (buf);
  1302. return err;
  1303. }
  1304. if (!ISPRINT (c))
  1305. /* Found a non-printing. Try again starting with next char. */
  1306. goto tryline;
  1307. buf[i] = c;
  1308. }
  1309. /* We found a run of `string_min' printable characters.
  1310. Now see if it is terminated with a null byte. */
  1311. while (!limit_bytes_to_format || address < end_offset)
  1312. {
  1313. if (i == bufsize)
  1314. {
  1315. bufsize = 1 + 3 * bufsize / 2;
  1316. buf = xrealloc (buf, bufsize);
  1317. }
  1318. err |= read_char (&c);
  1319. address++;
  1320. if (c < 0)
  1321. {
  1322. free (buf);
  1323. return err;
  1324. }
  1325. if (c == '\0')
  1326. break; /* It is; print this string. */
  1327. if (!ISPRINT (c))
  1328. goto tryline; /* It isn't; give up on this string. */
  1329. buf[i++] = c; /* String continues; store it all. */
  1330. }
  1331. /* If we get here, the string is all printable and null-terminated,
  1332. so print it. It is all in `buf' and `i' is its length. */
  1333. buf[i] = 0;
  1334. format_address (address - i - 1, ' ');
  1335. for (i = 0; (c = buf[i]); i++)
  1336. {
  1337. switch (c)
  1338. {
  1339. case '\007':
  1340. fputs ("\\a", stdout);
  1341. break;
  1342. case '\b':
  1343. fputs ("\\b", stdout);
  1344. break;
  1345. case '\f':
  1346. fputs ("\\f", stdout);
  1347. break;
  1348. case '\n':
  1349. fputs ("\\n", stdout);
  1350. break;
  1351. case '\r':
  1352. fputs ("\\r", stdout);
  1353. break;
  1354. case '\t':
  1355. fputs ("\\t", stdout);
  1356. break;
  1357. case '\v':
  1358. fputs ("\\v", stdout);
  1359. break;
  1360. default:
  1361. putc (c, stdout);
  1362. }
  1363. }
  1364. putchar ('\n');
  1365. }
  1366. /* We reach this point only if we search through
  1367. (max_bytes_to_format - string_min) bytes before reaching EOF. */
  1368. free (buf);
  1369. err |= check_and_close ();
  1370. return err;
  1371. }
  1372. int
  1373. main (int argc, char **argv)
  1374. {
  1375. int c;
  1376. int n_files;
  1377. size_t i;
  1378. int l_c_m;
  1379. size_t desired_width IF_LINT (= 0);
  1380. int width_specified = 0;
  1381. int n_failed_decodes = 0;
  1382. int err;
  1383. char const *short_options = (posix2_version () < 200112
  1384. ? COMMON_SHORT_OPTIONS "s::w::"
  1385. : COMMON_SHORT_OPTIONS "s:w:");
  1386. /* The old-style `pseudo starting address' to be printed in parentheses
  1387. after any true address. */
  1388. uintmax_t pseudo_start IF_LINT (= 0);
  1389. program_name = argv[0];
  1390. setlocale (LC_ALL, "");
  1391. bindtextdomain (PACKAGE, LOCALEDIR);
  1392. textdomain (PACKAGE);
  1393. atexit (close_stdout);
  1394. err = 0;
  1395. for (i = 0; i <= MAX_INTEGRAL_TYPE_SIZE; i++)
  1396. integral_type_size[i] = NO_SIZE;
  1397. integral_type_size[sizeof (char)] = CHAR;
  1398. integral_type_size[sizeof (short int)] = SHORT;
  1399. integral_type_size[sizeof (int)] = INT;
  1400. integral_type_size[sizeof (long int)] = LONG;
  1401. #if HAVE_UNSIGNED_LONG_LONG
  1402. /* If `long' and `long long' have the same size, it's fine
  1403. to overwrite the entry for `long' with this one. */
  1404. integral_type_size[sizeof (ulonglong_t)] = LONG_LONG;
  1405. #endif
  1406. for (i = 0; i <= MAX_FP_TYPE_SIZE; i++)
  1407. fp_type_size[i] = NO_SIZE;
  1408. fp_type_size[sizeof (float)] = FLOAT_SINGLE;
  1409. /* The array entry for `double' is filled in after that for LONG_DOUBLE
  1410. so that if `long double' is the same type or if long double isn't
  1411. supported FLOAT_LONG_DOUBLE will never be used. */
  1412. fp_type_size[sizeof (LONG_DOUBLE)] = FLOAT_LONG_DOUBLE;
  1413. fp_type_size[sizeof (double)] = FLOAT_DOUBLE;
  1414. n_specs = 0;
  1415. n_specs_allocated = 5;
  1416. spec = (struct tspec *) xmalloc (n_specs_allocated * sizeof (struct tspec));
  1417. format_address = format_address_std;
  1418. address_base = 8;
  1419. address_pad_len = 7;
  1420. flag_dump_strings = 0;
  1421. while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
  1422. != -1)
  1423. {
  1424. uintmax_t tmp;
  1425. enum strtol_error s_err;
  1426. switch (c)
  1427. {
  1428. case 0:
  1429. break;
  1430. case 'A':
  1431. switch (optarg[0])
  1432. {
  1433. case 'd':
  1434. format_address = format_address_std;
  1435. address_base = 10;
  1436. address_pad_len = 7;
  1437. break;
  1438. case 'o':
  1439. format_address = format_address_std;
  1440. address_base = 8;
  1441. address_pad_len = 7;
  1442. break;
  1443. case 'x':
  1444. format_address = format_address_std;
  1445. address_base = 16;
  1446. address_pad_len = 6;
  1447. break;
  1448. case 'n':
  1449. format_address = format_address_none;
  1450. address_pad_len = 0;
  1451. break;
  1452. default:
  1453. error (EXIT_FAILURE, 0,
  1454. _("invalid output address radix `%c'; \
  1455. it must be one character from [doxn]"),
  1456. optarg[0]);
  1457. break;
  1458. }
  1459. break;
  1460. case 'j':
  1461. s_err = xstrtoumax (optarg, NULL, 0, &n_bytes_to_skip, "bkm");
  1462. if (s_err != LONGINT_OK)
  1463. STRTOL_FATAL_ERROR (optarg, _("skip argument"), s_err);
  1464. break;
  1465. case 'N':
  1466. limit_bytes_to_format = 1;
  1467. s_err = xstrtoumax (optarg, NULL, 0, &max_bytes_to_format, "bkm");
  1468. if (s_err != LONGINT_OK)
  1469. STRTOL_FATAL_ERROR (optarg, _("limit argument"), s_err);
  1470. break;
  1471. case 's':
  1472. if (optarg == NULL)
  1473. string_min = 3;
  1474. else
  1475. {
  1476. s_err = xstrtoumax (optarg, NULL, 0, &tmp, "bkm");
  1477. if (s_err != LONGINT_OK)
  1478. STRTOL_FATAL_ERROR (optarg, _("minimum string length"), s_err);
  1479. /* The minimum string length may be no larger than SIZE_MAX,
  1480. since we may allocate a buffer of this size. */
  1481. if (SIZE_MAX < tmp)
  1482. error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
  1483. string_min = tmp;
  1484. }
  1485. flag_dump_strings = 1;
  1486. break;
  1487. case 't':
  1488. if (decode_format_string (optarg))
  1489. ++n_failed_decodes;
  1490. break;
  1491. case 'v':
  1492. abbreviate_duplicate_blocks = 0;
  1493. break;
  1494. case TRADITIONAL_OPTION:
  1495. traditional = 1;
  1496. break;
  1497. /* The next several cases map the traditional format
  1498. specification options to the corresponding modern format
  1499. specs. GNU od accepts any combination of old- and
  1500. new-style options. Format specification options accumulate. */
  1501. #define CASE_OLD_ARG(old_char,new_string) \
  1502. case old_char: \
  1503. { \
  1504. if (decode_format_string (new_string)) \
  1505. ++n_failed_decodes; \
  1506. } \
  1507. break
  1508. CASE_OLD_ARG ('a', "a");
  1509. CASE_OLD_ARG ('b', "oC");
  1510. CASE_OLD_ARG ('c', "c");
  1511. CASE_OLD_ARG ('d', "u2");
  1512. CASE_OLD_ARG ('f', "fF");
  1513. CASE_OLD_ARG ('h', "x2");
  1514. CASE_OLD_ARG ('i', "d2");
  1515. CASE_OLD_ARG ('l', "d4");
  1516. CASE_OLD_ARG ('o', "o2");
  1517. CASE_OLD_ARG ('x', "x2");
  1518. /* FIXME: POSIX 1003.1-2001 with XSI requires this:
  1519. CASE_OLD_ARG ('s', "d2");
  1520. for the traditional syntax, but this conflicts with case
  1521. 's' above. */
  1522. #undef CASE_OLD_ARG
  1523. case 'w':
  1524. width_specified = 1;
  1525. if (optarg == NULL)
  1526. {
  1527. desired_width = 32;
  1528. }
  1529. else
  1530. {
  1531. uintmax_t w_tmp;
  1532. s_err = xstrtoumax (optarg, NULL, 10, &w_tmp, "");
  1533. if (s_err != LONGINT_OK)
  1534. STRTOL_FATAL_ERROR (optarg, _("width specification"), s_err);
  1535. if (SIZE_MAX < w_tmp)
  1536. error (EXIT_FAILURE, 0, _("%s is too large"), optarg);
  1537. desired_width = w_tmp;
  1538. }
  1539. break;
  1540. case_GETOPT_HELP_CHAR;
  1541. case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
  1542. default:
  1543. usage (EXIT_FAILURE);
  1544. break;
  1545. }
  1546. }
  1547. if (n_failed_decodes > 0)
  1548. exit (EXIT_FAILURE);
  1549. if (flag_dump_strings && n_specs > 0)
  1550. error (EXIT_FAILURE, 0,
  1551. _("no type may be specified when dumping strings"));
  1552. n_files = argc - optind;
  1553. /* If the --traditional option is used, there may be from
  1554. 0 to 3 remaining command line arguments; handle each case
  1555. separately.
  1556. od [file] [[+]offset[.][b] [[+]label[.][b]]]
  1557. The offset and pseudo_start have the same syntax.
  1558. FIXME: POSIX 1003.1-2001 with XSI requires support for the
  1559. traditional syntax even if --traditional is not given. */
  1560. if (traditional)
  1561. {
  1562. uintmax_t offset;
  1563. if (n_files == 1)
  1564. {
  1565. if (parse_old_offset (argv[optind], &offset))
  1566. {
  1567. n_bytes_to_skip = offset;
  1568. --n_files;
  1569. ++argv;
  1570. }
  1571. }
  1572. else if (n_files == 2)
  1573. {
  1574. uintmax_t o1, o2;
  1575. if (parse_old_offset (argv[optind], &o1)
  1576. && parse_old_offset (argv[optind + 1], &o2))
  1577. {
  1578. n_bytes_to_skip = o1;
  1579. flag_pseudo_start = 1;
  1580. pseudo_start = o2;
  1581. argv += 2;
  1582. n_files -= 2;
  1583. }
  1584. else if (parse_old_offset (argv[optind + 1], &o2))
  1585. {
  1586. n_bytes_to_skip = o2;
  1587. --n_files;
  1588. argv[optind + 1] = argv[optind];
  1589. ++argv;
  1590. }
  1591. else
  1592. {
  1593. error (0, 0,
  1594. _("invalid second operand in compatibility mode `%s'"),
  1595. argv[optind + 1]);
  1596. usage (EXIT_FAILURE);
  1597. }
  1598. }
  1599. else if (n_files == 3)
  1600. {
  1601. uintmax_t o1, o2;
  1602. if (parse_old_offset (argv[optind + 1], &o1)
  1603. && parse_old_offset (argv[optind + 2], &o2))
  1604. {
  1605. n_bytes_to_skip = o1;
  1606. flag_pseudo_start = 1;
  1607. pseudo_start = o2;
  1608. argv[optind + 2] = argv[optind];
  1609. argv += 2;
  1610. n_files -= 2;
  1611. }
  1612. else
  1613. {
  1614. error (0, 0,
  1615. _("in compatibility mode, the last two arguments must be offsets"));
  1616. usage (EXIT_FAILURE);
  1617. }
  1618. }
  1619. else if (n_files > 3)
  1620. {
  1621. error (0, 0,
  1622. _("compatibility mode supports at most three arguments"));
  1623. usage (EXIT_FAILURE);
  1624. }
  1625. if (flag_pseudo_start)
  1626. {
  1627. if (format_address == format_address_none)
  1628. {
  1629. address_base = 8;
  1630. address_pad_len = 7;
  1631. format_address = format_address_paren;
  1632. }
  1633. else
  1634. format_address = format_address_label;
  1635. }
  1636. }
  1637. if (limit_bytes_to_format)
  1638. {
  1639. end_offset = n_bytes_to_skip + max_bytes_to_format;
  1640. if (end_offset < n_bytes_to_skip)
  1641. error (EXIT_FAILURE, 0, "skip-bytes + read-bytes is too large");
  1642. }
  1643. if (n_specs == 0)
  1644. {
  1645. if (decode_one_format ("o2", "o2", NULL, &(spec[0])))
  1646. {
  1647. /* This happens on Cray systems that don't have a 2-byte
  1648. integral type. */
  1649. exit (EXIT_FAILURE);
  1650. }
  1651. n_specs = 1;
  1652. }
  1653. if (n_files > 0)
  1654. {
  1655. /* Set the global pointer FILE_LIST so that it
  1656. references the first file-argument on the command-line. */
  1657. file_list = (char const *const *) &argv[optind];
  1658. }
  1659. else
  1660. {
  1661. /* No files were listed on the command line.
  1662. Set the global pointer FILE_LIST so that it
  1663. references the null-terminated list of one name: "-". */
  1664. file_list = default_file_list;
  1665. }
  1666. /* open the first input file */
  1667. err |= open_next_file ();
  1668. if (in_stream == NULL)
  1669. goto cleanup;
  1670. /* skip over any unwanted header bytes */
  1671. err |= skip (n_bytes_to_skip);
  1672. if (in_stream == NULL)
  1673. goto cleanup;
  1674. pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);
  1675. /* Compute output block length. */
  1676. l_c_m = get_lcm ();
  1677. if (width_specified)
  1678. {
  1679. if (desired_width != 0 && desired_width % l_c_m == 0)
  1680. bytes_per_block = desired_width;
  1681. else
  1682. {
  1683. error (0, 0, _("warning: invalid width %lu; using %d instead"),
  1684. (unsigned long) desired_width, l_c_m);
  1685. bytes_per_block = l_c_m;
  1686. }
  1687. }
  1688. else
  1689. {
  1690. if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
  1691. bytes_per_block = l_c_m * (DEFAULT_BYTES_PER_BLOCK / l_c_m);
  1692. else
  1693. bytes_per_block = l_c_m;
  1694. }
  1695. #ifdef DEBUG
  1696. for (i = 0; i < n_specs; i++)
  1697. {
  1698. printf (_("%d: fmt=\"%s\" width=%d\n"),
  1699. i, spec[i].fmt_string, width_bytes[spec[i].size]);
  1700. }
  1701. #endif
  1702. err |= (flag_dump_strings ? dump_strings () : dump ());
  1703. cleanup:;
  1704. if (have_read_stdin && fclose (stdin) == EOF)
  1705. error (EXIT_FAILURE, errno, _("standard input"));
  1706. exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  1707. }

od源代码的更多相关文章

  1. 2017-2018-1 20155313 《信息安全系统设计基础》 Myod

    2017-2018-1 20155313 <信息安全系统设计基础> Myod Myod要求 1.复习c文件处理内容 2.编写myod.c 用myod XXX实现Linux下od -tx - ...

  2. 50个C/C++源代码网站

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码 .这份清单提供了源代码的链接以及它们的小说明.我已尽力包括最佳的C/C++源代码的网站.这不是一个完整的 ...

  3. 源代码管理工具(上)-SVN基本使用

    ------------------------------------------------------SVN简介和搭建 ------------------------------------- ...

  4. Java学习-018-EXCEL 文件写入实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  5. Moment.js学习(一)源代码

    本篇主要是学习Moment.js.类库源代码如下: 2.4版本. //! moment.js //! version : 2.4.0 //! authors : Tim Wood, Iskren Ch ...

  6. 50个C/C++源代码网站(转-清风小阁)

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码 .主要转贴: http://blog.csdn.net/nuoshueihe/article/detai ...

  7. 转载:50个C/C++源代码网站

    来源:http://www.cnblogs.com/feisky/archive/2010/03/05/1679160.html C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些 ...

  8. OD使用经验【转载】

    文章整理发布:黑客风云  1.我的os是winXP,无法使用trw2000,而softice装了多次均未成功,还蓝屏死机多次.郁闷. 2.友好的gui界面,不像softice.可以边干活边听歌,不像s ...

  9. (转载)50个c/c++源代码网站

    C/C++是最主要的编程语言.这里列出了50名优秀网站和网页清单,这些网站提供c/c++源代码.这份清单提供了源代码的链接以及它们的小说明.我已 尽力包括最佳的C/C++源代码的网站.这不是一个完整的 ...

随机推荐

  1. .Net Intelligencia.UrlRewriter 重定向参数中文支持配置方法

    在使用.Net 官方 Url重定向组件时,发现若原地址包含中文,如:http://localhost/首页.html 重定向为:http://localhost/index.aspx?id=首页  时 ...

  2. 如何实现本机Windows连接虚拟机中的CentOS

    1.确定CentOS的IP地址,命令为 ifconfig,由此可知,LinuxIP地址为 192.168.85.128 2.WIndows的IP地址为192.168.16.1, 3.保证CentOS和 ...

  3. 大屏FAQ

    1. 大屏可以分为哪几类?帆软有哪些大屏硬件合作商?编辑 拼接屏:通常由单个46-55寸的液晶显示屏组成屏幕墙,存在拼缝,借助矩阵.屏控系统来进行信号的输入与输出控制,可以实现屏幕墙上多个屏幕的组合. ...

  4. 2018-2019-2 网络对抗技术 20165322 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165322 Exp4 恶意代码分析 目录 实验内容与步骤 系统运行监控 恶意软件分析 实验过程中遇到的问题 基础问题回答 实验总结与体会 实验内容与步骤 系 ...

  5. Kali-linux服务的指纹识别

    为了确保有一个成功的渗透测试,必须需要知道目标系统中服务的指纹信息.服务指纹信息包括服务端口.服务名和版本等.在Kali中,可以使用Nmap和Amap工具识别指纹信息.本节将介绍使用Nmap和Amap ...

  6. jq页面加载问题

    Window.onload=function(){ //页面加载,不能同时编写多个,最后面的会覆盖前面的 }   $(document).ready(function(){ //页面加载,能同时编写多 ...

  7. Level/levelup-1-简介

    https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...

  8. ionic 入门创建第一个应用demo

    一.ionic卸载 1.清除旧版本的ionic框架 npm uninstall -g ionic npm uninstall -g cordova npm cache clear npm cache ...

  9. springboot项目用gradle打jar包

    C:\1_work_files\workspace_sts\HR\psn\build\libs

  10. ActiveRecord初始化,可以实现jfinal系统启动完成后,再建立数据库连接

    1.JFinalConfig的afterJFinalStart方法,可以实现系统启动成功后,调用的方法 2.ActiveRecord 多数据源初始化 package com.meiah.common; ...