od_bloaty.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. /* od -- dump files in octal and other formats
  2. Copyright (C) 92, 1995-2004 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /* Written by Jim Meyering. */
  16. /* Busyboxed by Denys Vlasenko, based on od.c from coreutils-5.2.1 */
  17. /* #include "libbb.h" - done in od.c */
  18. #include "common_bufsiz.h"
  19. #define assert(a) ((void)0)
  20. //usage:#if ENABLE_DESKTOP
  21. //usage:#define od_trivial_usage
  22. //usage: "[-abcdfhilovxs] [-t TYPE] [-A RADIX] [-N SIZE] [-j SKIP] [-S MINSTR] [-w WIDTH] [FILE]..."
  23. // We don't support:
  24. // ... [FILE] [[+]OFFSET[.][b]]
  25. // Support is buggy for:
  26. // od --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]
  27. //usage:#define od_full_usage "\n\n"
  28. //usage: "Print FILEs (or stdin) unambiguously, as octal bytes by default"
  29. //usage:#endif
  30. enum {
  31. OPT_A = 1 << 0,
  32. OPT_N = 1 << 1,
  33. OPT_a = 1 << 2,
  34. OPT_b = 1 << 3,
  35. OPT_c = 1 << 4,
  36. OPT_d = 1 << 5,
  37. OPT_f = 1 << 6,
  38. OPT_h = 1 << 7,
  39. OPT_i = 1 << 8,
  40. OPT_j = 1 << 9,
  41. OPT_l = 1 << 10,
  42. OPT_o = 1 << 11,
  43. OPT_t = 1 << 12,
  44. /* When zero and two or more consecutive blocks are equal, format
  45. only the first block and output an asterisk alone on the following
  46. line to indicate that identical blocks have been elided: */
  47. OPT_v = 1 << 13,
  48. OPT_x = 1 << 14,
  49. OPT_s = 1 << 15,
  50. OPT_S = 1 << 16,
  51. OPT_w = 1 << 17,
  52. OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
  53. };
  54. #define OD_GETOPT32() getopt32(argv, \
  55. "A:N:abcdfhij:lot:vxsS:w::", \
  56. /* -w with optional param */ \
  57. /* -S was -s and also had optional parameter */ \
  58. /* but in coreutils 6.3 it was renamed and now has */ \
  59. /* _mandatory_ parameter */ \
  60. &str_A, &str_N, &str_j, &lst_t, &str_S, &G.bytes_per_block)
  61. /* Check for 0x7f is a coreutils 6.3 addition */
  62. #define ISPRINT(c) (((c) >= ' ') && (c) < 0x7f)
  63. typedef long double longdouble_t;
  64. typedef unsigned long long ulonglong_t;
  65. typedef long long llong;
  66. #if ENABLE_LFS
  67. # define xstrtooff_sfx xstrtoull_sfx
  68. #else
  69. # define xstrtooff_sfx xstrtoul_sfx
  70. #endif
  71. /* The default number of input bytes per output line. */
  72. #define DEFAULT_BYTES_PER_BLOCK 16
  73. /* The number of decimal digits of precision in a float. */
  74. #ifndef FLT_DIG
  75. # define FLT_DIG 7
  76. #endif
  77. /* The number of decimal digits of precision in a double. */
  78. #ifndef DBL_DIG
  79. # define DBL_DIG 15
  80. #endif
  81. /* The number of decimal digits of precision in a long double. */
  82. #ifndef LDBL_DIG
  83. # define LDBL_DIG DBL_DIG
  84. #endif
  85. enum size_spec {
  86. NO_SIZE,
  87. CHAR,
  88. SHORT,
  89. INT,
  90. LONG,
  91. LONG_LONG,
  92. FLOAT_SINGLE,
  93. FLOAT_DOUBLE,
  94. FLOAT_LONG_DOUBLE,
  95. N_SIZE_SPECS
  96. };
  97. enum output_format {
  98. SIGNED_DECIMAL,
  99. UNSIGNED_DECIMAL,
  100. OCTAL,
  101. HEXADECIMAL,
  102. FLOATING_POINT,
  103. NAMED_CHARACTER,
  104. CHARACTER
  105. };
  106. /* Each output format specification (from '-t spec' or from
  107. old-style options) is represented by one of these structures. */
  108. struct tspec {
  109. enum output_format fmt;
  110. enum size_spec size;
  111. void (*print_function) (size_t, const char *, const char *);
  112. char *fmt_string;
  113. int hexl_mode_trailer;
  114. int field_width;
  115. };
  116. /* Convert the number of 8-bit bytes of a binary representation to
  117. the number of characters (digits + sign if the type is signed)
  118. required to represent the same quantity in the specified base/type.
  119. For example, a 32-bit (4-byte) quantity may require a field width
  120. as wide as the following for these types:
  121. 11 unsigned octal
  122. 11 signed decimal
  123. 10 unsigned decimal
  124. 8 unsigned hexadecimal */
  125. static const uint8_t bytes_to_oct_digits[] ALIGN1 =
  126. {0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};
  127. static const uint8_t bytes_to_signed_dec_digits[] ALIGN1 =
  128. {1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};
  129. static const uint8_t bytes_to_unsigned_dec_digits[] ALIGN1 =
  130. {0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};
  131. static const uint8_t bytes_to_hex_digits[] ALIGN1 =
  132. {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};
  133. /* Convert enum size_spec to the size of the named type. */
  134. static const signed char width_bytes[] ALIGN1 = {
  135. -1,
  136. sizeof(char),
  137. sizeof(short),
  138. sizeof(int),
  139. sizeof(long),
  140. sizeof(ulonglong_t),
  141. sizeof(float),
  142. sizeof(double),
  143. sizeof(longdouble_t)
  144. };
  145. /* Ensure that for each member of 'enum size_spec' there is an
  146. initializer in the width_bytes array. */
  147. struct ERR_width_bytes_has_bad_size {
  148. char ERR_width_bytes_has_bad_size[ARRAY_SIZE(width_bytes) == N_SIZE_SPECS ? 1 : -1];
  149. };
  150. struct globals {
  151. smallint exit_code;
  152. unsigned string_min;
  153. /* An array of specs describing how to format each input block. */
  154. unsigned n_specs;
  155. struct tspec *spec;
  156. /* Function that accepts an address and an optional following char,
  157. and prints the address and char to stdout. */
  158. void (*format_address)(off_t, char);
  159. /* The difference between the old-style pseudo starting address and
  160. the number of bytes to skip. */
  161. #if ENABLE_LONG_OPTS
  162. off_t pseudo_offset;
  163. # define G_pseudo_offset G.pseudo_offset
  164. #endif
  165. /* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
  166. input is formatted. */
  167. /* The number of input bytes formatted per output line. It must be
  168. a multiple of the least common multiple of the sizes associated with
  169. the specified output types. It should be as large as possible, but
  170. no larger than 16 -- unless specified with the -w option. */
  171. unsigned bytes_per_block; /* have to use unsigned, not size_t */
  172. /* A NULL-terminated list of the file-arguments from the command line. */
  173. const char *const *file_list;
  174. /* The input stream associated with the current file. */
  175. FILE *in_stream;
  176. bool not_first;
  177. bool prev_pair_equal;
  178. } FIX_ALIASING;
  179. #if !ENABLE_LONG_OPTS
  180. enum { G_pseudo_offset = 0 };
  181. #endif
  182. #define G (*(struct globals*)bb_common_bufsiz1)
  183. #define INIT_G() do { \
  184. setup_common_bufsiz(); \
  185. BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
  186. G.bytes_per_block = 32; \
  187. } while (0)
  188. #define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t)
  189. static const unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1] ALIGN1 = {
  190. [sizeof(char)] = CHAR,
  191. #if USHRT_MAX != UCHAR_MAX
  192. [sizeof(short)] = SHORT,
  193. #endif
  194. #if UINT_MAX != USHRT_MAX
  195. [sizeof(int)] = INT,
  196. #endif
  197. #if ULONG_MAX != UINT_MAX
  198. [sizeof(long)] = LONG,
  199. #endif
  200. #if ULLONG_MAX != ULONG_MAX
  201. [sizeof(ulonglong_t)] = LONG_LONG,
  202. #endif
  203. };
  204. #define MAX_FP_TYPE_SIZE sizeof(longdouble_t)
  205. static const unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1] ALIGN1 = {
  206. /* gcc seems to allow repeated indexes. Last one wins */
  207. [sizeof(longdouble_t)] = FLOAT_LONG_DOUBLE,
  208. [sizeof(double)] = FLOAT_DOUBLE,
  209. [sizeof(float)] = FLOAT_SINGLE
  210. };
  211. static unsigned
  212. gcd(unsigned u, unsigned v)
  213. {
  214. unsigned t;
  215. while (v != 0) {
  216. t = u % v;
  217. u = v;
  218. v = t;
  219. }
  220. return u;
  221. }
  222. /* Compute the least common multiple of U and V. */
  223. static unsigned
  224. lcm(unsigned u, unsigned v) {
  225. unsigned t = gcd(u, v);
  226. if (t == 0)
  227. return 0;
  228. return u * v / t;
  229. }
  230. static void
  231. print_s_char(size_t n_bytes, const char *block, const char *fmt_string)
  232. {
  233. while (n_bytes--) {
  234. int tmp = *(signed char *) block;
  235. printf(fmt_string, tmp);
  236. block += sizeof(unsigned char);
  237. }
  238. }
  239. static void
  240. print_char(size_t n_bytes, const char *block, const char *fmt_string)
  241. {
  242. while (n_bytes--) {
  243. unsigned tmp = *(unsigned char *) block;
  244. printf(fmt_string, tmp);
  245. block += sizeof(unsigned char);
  246. }
  247. }
  248. static void
  249. print_s_short(size_t n_bytes, const char *block, const char *fmt_string)
  250. {
  251. n_bytes /= sizeof(signed short);
  252. while (n_bytes--) {
  253. int tmp = *(signed short *) block;
  254. printf(fmt_string, tmp);
  255. block += sizeof(unsigned short);
  256. }
  257. }
  258. static void
  259. print_short(size_t n_bytes, const char *block, const char *fmt_string)
  260. {
  261. n_bytes /= sizeof(unsigned short);
  262. while (n_bytes--) {
  263. unsigned tmp = *(unsigned short *) block;
  264. printf(fmt_string, tmp);
  265. block += sizeof(unsigned short);
  266. }
  267. }
  268. static void
  269. print_int(size_t n_bytes, const char *block, const char *fmt_string)
  270. {
  271. n_bytes /= sizeof(unsigned);
  272. while (n_bytes--) {
  273. unsigned tmp = *(unsigned *) block;
  274. printf(fmt_string, tmp);
  275. block += sizeof(unsigned);
  276. }
  277. }
  278. #if UINT_MAX == ULONG_MAX
  279. # define print_long print_int
  280. #else
  281. static void
  282. print_long(size_t n_bytes, const char *block, const char *fmt_string)
  283. {
  284. n_bytes /= sizeof(unsigned long);
  285. while (n_bytes--) {
  286. unsigned long tmp = *(unsigned long *) block;
  287. printf(fmt_string, tmp);
  288. block += sizeof(unsigned long);
  289. }
  290. }
  291. #endif
  292. #if ULONG_MAX == ULLONG_MAX
  293. # define print_long_long print_long
  294. #else
  295. static void
  296. print_long_long(size_t n_bytes, const char *block, const char *fmt_string)
  297. {
  298. n_bytes /= sizeof(ulonglong_t);
  299. while (n_bytes--) {
  300. ulonglong_t tmp = *(ulonglong_t *) block;
  301. printf(fmt_string, tmp);
  302. block += sizeof(ulonglong_t);
  303. }
  304. }
  305. #endif
  306. static void
  307. print_float(size_t n_bytes, const char *block, const char *fmt_string)
  308. {
  309. n_bytes /= sizeof(float);
  310. while (n_bytes--) {
  311. float tmp = *(float *) block;
  312. printf(fmt_string, tmp);
  313. block += sizeof(float);
  314. }
  315. }
  316. static void
  317. print_double(size_t n_bytes, const char *block, const char *fmt_string)
  318. {
  319. n_bytes /= sizeof(double);
  320. while (n_bytes--) {
  321. double tmp = *(double *) block;
  322. printf(fmt_string, tmp);
  323. block += sizeof(double);
  324. }
  325. }
  326. static void
  327. print_long_double(size_t n_bytes, const char *block, const char *fmt_string)
  328. {
  329. n_bytes /= sizeof(longdouble_t);
  330. while (n_bytes--) {
  331. longdouble_t tmp = *(longdouble_t *) block;
  332. printf(fmt_string, tmp);
  333. block += sizeof(longdouble_t);
  334. }
  335. }
  336. /* print_[named]_ascii are optimized for speed.
  337. * Remember, someday you may want to pump gigabytes through this thing.
  338. * Saving a dozen of .text bytes here is counter-productive */
  339. static void
  340. print_named_ascii(size_t n_bytes, const char *block,
  341. const char *unused_fmt_string UNUSED_PARAM)
  342. {
  343. /* Names for some non-printing characters. */
  344. static const char charname[33][3] ALIGN1 = {
  345. "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
  346. " bs", " ht", " nl", " vt", " ff", " cr", " so", " si",
  347. "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
  348. "can", " em", "sub", "esc", " fs", " gs", " rs", " us",
  349. " sp"
  350. };
  351. // buf[N] pos: 01234 56789
  352. char buf[12] = " x\0 xxx\0";
  353. // [12] because we take three 32bit stack slots anyway, and
  354. // gcc is too dumb to initialize with constant stores,
  355. // it copies initializer from rodata. Oh well.
  356. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65410
  357. while (n_bytes--) {
  358. unsigned masked_c = *(unsigned char *) block++;
  359. masked_c &= 0x7f;
  360. if (masked_c == 0x7f) {
  361. fputs(" del", stdout);
  362. continue;
  363. }
  364. if (masked_c > ' ') {
  365. buf[3] = masked_c;
  366. fputs(buf, stdout);
  367. continue;
  368. }
  369. /* Why? Because printf(" %3.3s") is much slower... */
  370. buf[6] = charname[masked_c][0];
  371. buf[7] = charname[masked_c][1];
  372. buf[8] = charname[masked_c][2];
  373. fputs(buf+5, stdout);
  374. }
  375. }
  376. static void
  377. print_ascii(size_t n_bytes, const char *block,
  378. const char *unused_fmt_string UNUSED_PARAM)
  379. {
  380. // buf[N] pos: 01234 56789
  381. char buf[12] = " x\0 xxx\0";
  382. while (n_bytes--) {
  383. const char *s;
  384. unsigned c = *(unsigned char *) block++;
  385. if (ISPRINT(c)) {
  386. buf[3] = c;
  387. fputs(buf, stdout);
  388. continue;
  389. }
  390. switch (c) {
  391. case '\0':
  392. s = " \\0";
  393. break;
  394. case '\007':
  395. s = " \\a";
  396. break;
  397. case '\b':
  398. s = " \\b";
  399. break;
  400. case '\f':
  401. s = " \\f";
  402. break;
  403. case '\n':
  404. s = " \\n";
  405. break;
  406. case '\r':
  407. s = " \\r";
  408. break;
  409. case '\t':
  410. s = " \\t";
  411. break;
  412. case '\v':
  413. s = " \\v";
  414. break;
  415. default:
  416. buf[6] = (c >> 6 & 3) + '0';
  417. buf[7] = (c >> 3 & 7) + '0';
  418. buf[8] = (c & 7) + '0';
  419. s = buf + 5;
  420. }
  421. fputs(s, stdout);
  422. }
  423. }
  424. /* Given a list of one or more input filenames FILE_LIST, set the global
  425. file pointer IN_STREAM and the global string INPUT_FILENAME to the
  426. first one that can be successfully opened. Modify FILE_LIST to
  427. reference the next filename in the list. A file name of "-" is
  428. interpreted as standard input. If any file open fails, give an error
  429. message and return nonzero. */
  430. static void
  431. open_next_file(void)
  432. {
  433. while (1) {
  434. if (!*G.file_list)
  435. return;
  436. G.in_stream = fopen_or_warn_stdin(*G.file_list++);
  437. if (G.in_stream) {
  438. break;
  439. }
  440. G.exit_code = 1;
  441. }
  442. if ((option_mask32 & (OPT_N|OPT_S)) == OPT_N)
  443. setbuf(G.in_stream, NULL);
  444. }
  445. /* Test whether there have been errors on in_stream, and close it if
  446. it is not standard input. Return nonzero if there has been an error
  447. on in_stream or stdout; return zero otherwise. This function will
  448. report more than one error only if both a read and a write error
  449. have occurred. IN_ERRNO, if nonzero, is the error number
  450. corresponding to the most recent action for IN_STREAM. */
  451. static void
  452. check_and_close(void)
  453. {
  454. if (G.in_stream) {
  455. if (ferror(G.in_stream)) {
  456. bb_error_msg("%s: read error", (G.in_stream == stdin)
  457. ? bb_msg_standard_input
  458. : G.file_list[-1]
  459. );
  460. G.exit_code = 1;
  461. }
  462. fclose_if_not_stdin(G.in_stream);
  463. G.in_stream = NULL;
  464. }
  465. if (ferror(stdout)) {
  466. bb_error_msg_and_die(bb_msg_write_error);
  467. }
  468. }
  469. /* If S points to a single valid modern od format string, put
  470. a description of that format in *TSPEC, return pointer to
  471. character following the just-decoded format.
  472. For example, if S were "d4afL", we will return a rtp to "afL"
  473. and *TSPEC would be
  474. {
  475. fmt = SIGNED_DECIMAL;
  476. size = INT or LONG; (whichever integral_type_size[4] resolves to)
  477. print_function = print_int; (assuming size == INT)
  478. fmt_string = "%011d%c";
  479. }
  480. S_ORIG is solely for reporting errors. It should be the full format
  481. string argument. */
  482. static NOINLINE const char *
  483. decode_one_format(const char *s_orig, const char *s, struct tspec *tspec)
  484. {
  485. enum size_spec size_spec;
  486. unsigned size;
  487. enum output_format fmt;
  488. const char *p;
  489. char *end;
  490. char *fmt_string = NULL;
  491. void (*print_function) (size_t, const char *, const char *);
  492. unsigned c;
  493. unsigned field_width = 0;
  494. int pos;
  495. switch (*s) {
  496. case 'd':
  497. case 'o':
  498. case 'u':
  499. case 'x': {
  500. static const char CSIL[] ALIGN1 = "CSIL";
  501. c = *s++;
  502. p = strchr(CSIL, *s);
  503. /* if *s == NUL, p != NULL! Testcase: "od -tx" */
  504. if (!p || *p == '\0') {
  505. size = sizeof(int);
  506. if (isdigit(s[0])) {
  507. size = bb_strtou(s, &end, 0);
  508. if (errno == ERANGE
  509. || MAX_INTEGRAL_TYPE_SIZE < size
  510. || integral_type_size[size] == NO_SIZE
  511. ) {
  512. bb_error_msg_and_die("invalid type string '%s'; "
  513. "%u-byte %s type is not supported",
  514. s_orig, size, "integral");
  515. }
  516. s = end;
  517. }
  518. } else {
  519. static const uint8_t CSIL_sizeof[4] = {
  520. sizeof(char),
  521. sizeof(short),
  522. sizeof(int),
  523. sizeof(long),
  524. };
  525. size = CSIL_sizeof[p - CSIL];
  526. s++; /* skip C/S/I/L */
  527. }
  528. #define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
  529. ((Spec) == LONG_LONG ? (Max_format) \
  530. : ((Spec) == LONG ? (Long_format) : (Min_format)))
  531. #define FMT_BYTES_ALLOCATED 9
  532. size_spec = integral_type_size[size];
  533. {
  534. static const char doux[] ALIGN1 = "doux";
  535. static const char doux_fmt_letter[][4] = {
  536. "lld", "llo", "llu", "llx"
  537. };
  538. static const enum output_format doux_fmt[] = {
  539. SIGNED_DECIMAL,
  540. OCTAL,
  541. UNSIGNED_DECIMAL,
  542. HEXADECIMAL,
  543. };
  544. static const uint8_t *const doux_bytes_to_XXX[] = {
  545. bytes_to_signed_dec_digits,
  546. bytes_to_oct_digits,
  547. bytes_to_unsigned_dec_digits,
  548. bytes_to_hex_digits,
  549. };
  550. static const char doux_fmtstring[][sizeof(" %%0%u%s")] = {
  551. " %%%u%s",
  552. " %%0%u%s",
  553. " %%%u%s",
  554. " %%0%u%s",
  555. };
  556. pos = strchr(doux, c) - doux;
  557. fmt = doux_fmt[pos];
  558. field_width = doux_bytes_to_XXX[pos][size];
  559. p = doux_fmt_letter[pos] + 2;
  560. if (size_spec == LONG) p--;
  561. if (size_spec == LONG_LONG) p -= 2;
  562. fmt_string = xasprintf(doux_fmtstring[pos], field_width, p);
  563. }
  564. switch (size_spec) {
  565. case CHAR:
  566. print_function = (fmt == SIGNED_DECIMAL
  567. ? print_s_char
  568. : print_char);
  569. break;
  570. case SHORT:
  571. print_function = (fmt == SIGNED_DECIMAL
  572. ? print_s_short
  573. : print_short);
  574. break;
  575. case INT:
  576. print_function = print_int;
  577. break;
  578. case LONG:
  579. print_function = print_long;
  580. break;
  581. default: /* case LONG_LONG: */
  582. print_function = print_long_long;
  583. break;
  584. }
  585. break;
  586. }
  587. case 'f': {
  588. static const char FDL[] ALIGN1 = "FDL";
  589. fmt = FLOATING_POINT;
  590. ++s;
  591. p = strchr(FDL, *s);
  592. if (!p) {
  593. size = sizeof(double);
  594. if (isdigit(s[0])) {
  595. size = bb_strtou(s, &end, 0);
  596. if (errno == ERANGE || size > MAX_FP_TYPE_SIZE
  597. || fp_type_size[size] == NO_SIZE
  598. ) {
  599. bb_error_msg_and_die("invalid type string '%s'; "
  600. "%u-byte %s type is not supported",
  601. s_orig, size, "floating point");
  602. }
  603. s = end;
  604. }
  605. } else {
  606. static const uint8_t FDL_sizeof[] = {
  607. sizeof(float),
  608. sizeof(double),
  609. sizeof(longdouble_t),
  610. };
  611. size = FDL_sizeof[p - FDL];
  612. }
  613. size_spec = fp_type_size[size];
  614. switch (size_spec) {
  615. case FLOAT_SINGLE:
  616. print_function = print_float;
  617. field_width = FLT_DIG + 8;
  618. /* Don't use %#e; not all systems support it. */
  619. fmt_string = xasprintf(" %%%d.%de", field_width, FLT_DIG);
  620. break;
  621. case FLOAT_DOUBLE:
  622. print_function = print_double;
  623. field_width = DBL_DIG + 8;
  624. fmt_string = xasprintf(" %%%d.%de", field_width, DBL_DIG);
  625. break;
  626. default: /* case FLOAT_LONG_DOUBLE: */
  627. print_function = print_long_double;
  628. field_width = LDBL_DIG + 8;
  629. fmt_string = xasprintf(" %%%d.%dLe", field_width, LDBL_DIG);
  630. break;
  631. }
  632. break;
  633. }
  634. case 'a':
  635. ++s;
  636. fmt = NAMED_CHARACTER;
  637. size_spec = CHAR;
  638. print_function = print_named_ascii;
  639. field_width = 3;
  640. break;
  641. case 'c':
  642. ++s;
  643. fmt = CHARACTER;
  644. size_spec = CHAR;
  645. print_function = print_ascii;
  646. field_width = 3;
  647. break;
  648. default:
  649. bb_error_msg_and_die("invalid character '%c' "
  650. "in type string '%s'", *s, s_orig);
  651. }
  652. tspec->size = size_spec;
  653. tspec->fmt = fmt;
  654. tspec->print_function = print_function;
  655. tspec->fmt_string = fmt_string;
  656. tspec->field_width = field_width;
  657. tspec->hexl_mode_trailer = (*s == 'z');
  658. if (tspec->hexl_mode_trailer)
  659. s++;
  660. return s;
  661. }
  662. /* Decode the modern od format string S. Append the decoded
  663. representation to the global array SPEC, reallocating SPEC if
  664. necessary. */
  665. static void
  666. decode_format_string(const char *s)
  667. {
  668. const char *s_orig = s;
  669. while (*s != '\0') {
  670. struct tspec tspec;
  671. const char *next;
  672. next = decode_one_format(s_orig, s, &tspec);
  673. assert(s != next);
  674. s = next;
  675. G.spec = xrealloc_vector(G.spec, 4, G.n_specs);
  676. memcpy(&G.spec[G.n_specs], &tspec, sizeof(G.spec[0]));
  677. G.n_specs++;
  678. }
  679. }
  680. /* Given a list of one or more input filenames FILE_LIST, set the global
  681. file pointer IN_STREAM to position N_SKIP in the concatenation of
  682. those files. If any file operation fails or if there are fewer than
  683. N_SKIP bytes in the combined input, give an error message and return
  684. nonzero. When possible, use seek rather than read operations to
  685. advance IN_STREAM. */
  686. static void
  687. skip(off_t n_skip)
  688. {
  689. if (n_skip == 0)
  690. return;
  691. while (G.in_stream) { /* !EOF */
  692. struct stat file_stats;
  693. /* First try seeking. For large offsets, this extra work is
  694. worthwhile. If the offset is below some threshold it may be
  695. more efficient to move the pointer by reading. There are two
  696. issues when trying to seek:
  697. - the file must be seekable.
  698. - before seeking to the specified position, make sure
  699. that the new position is in the current file.
  700. Try to do that by getting file's size using fstat.
  701. But that will work only for regular files. */
  702. /* The st_size field is valid only for regular files
  703. (and for symbolic links, which cannot occur here).
  704. If the number of bytes left to skip is at least
  705. as large as the size of the current file, we can
  706. decrement n_skip and go on to the next file. */
  707. if (fstat(fileno(G.in_stream), &file_stats) == 0
  708. && S_ISREG(file_stats.st_mode) && file_stats.st_size > 0
  709. ) {
  710. if (file_stats.st_size < n_skip) {
  711. n_skip -= file_stats.st_size;
  712. /* take "check & close / open_next" route */
  713. } else {
  714. if (fseeko(G.in_stream, n_skip, SEEK_CUR) != 0)
  715. G.exit_code = 1;
  716. return;
  717. }
  718. } else {
  719. /* If it's not a regular file with positive size,
  720. position the file pointer by reading. */
  721. char buf[1024];
  722. size_t n_bytes_to_read = 1024;
  723. size_t n_bytes_read;
  724. while (n_skip > 0) {
  725. if (n_skip < n_bytes_to_read)
  726. n_bytes_to_read = n_skip;
  727. n_bytes_read = fread(buf, 1, n_bytes_to_read, G.in_stream);
  728. n_skip -= n_bytes_read;
  729. if (n_bytes_read != n_bytes_to_read)
  730. break; /* EOF on this file or error */
  731. }
  732. }
  733. if (n_skip == 0)
  734. return;
  735. check_and_close();
  736. open_next_file();
  737. }
  738. if (n_skip)
  739. bb_error_msg_and_die("can't skip past end of combined input");
  740. }
  741. typedef void FN_format_address(off_t address, char c);
  742. static void
  743. format_address_none(off_t address UNUSED_PARAM, char c UNUSED_PARAM)
  744. {
  745. }
  746. static char address_fmt[] ALIGN1 = "%0n"OFF_FMT"xc";
  747. /* Corresponds to 'x' above */
  748. #define address_base_char address_fmt[sizeof(address_fmt)-3]
  749. /* Corresponds to 'n' above */
  750. #define address_pad_len_char address_fmt[2]
  751. static void
  752. format_address_std(off_t address, char c)
  753. {
  754. /* Corresponds to 'c' */
  755. address_fmt[sizeof(address_fmt)-2] = c;
  756. printf(address_fmt, address);
  757. }
  758. #if ENABLE_LONG_OPTS
  759. /* only used with --traditional */
  760. static void
  761. format_address_paren(off_t address, char c)
  762. {
  763. putchar('(');
  764. format_address_std(address, ')');
  765. if (c) putchar(c);
  766. }
  767. static void
  768. format_address_label(off_t address, char c)
  769. {
  770. format_address_std(address, ' ');
  771. format_address_paren(address + G_pseudo_offset, c);
  772. }
  773. #endif
  774. static void
  775. dump_hexl_mode_trailer(size_t n_bytes, const char *block)
  776. {
  777. fputs(" >", stdout);
  778. while (n_bytes--) {
  779. unsigned c = *(unsigned char *) block++;
  780. c = (ISPRINT(c) ? c : '.');
  781. putchar(c);
  782. }
  783. putchar('<');
  784. }
  785. /* Write N_BYTES bytes from CURR_BLOCK to standard output once for each
  786. of the N_SPEC format specs. CURRENT_OFFSET is the byte address of
  787. CURR_BLOCK in the concatenation of input files, and it is printed
  788. (optionally) only before the output line associated with the first
  789. format spec. When duplicate blocks are being abbreviated, the output
  790. for a sequence of identical input blocks is the output for the first
  791. block followed by an asterisk alone on a line. It is valid to compare
  792. the blocks PREV_BLOCK and CURR_BLOCK only when N_BYTES == BYTES_PER_BLOCK.
  793. That condition may be false only for the last input block -- and then
  794. only when it has not been padded to length BYTES_PER_BLOCK. */
  795. static void
  796. write_block(off_t current_offset, size_t n_bytes,
  797. const char *prev_block, const char *curr_block)
  798. {
  799. unsigned i;
  800. if (!(option_mask32 & OPT_v)
  801. && G.not_first
  802. && n_bytes == G.bytes_per_block
  803. && memcmp(prev_block, curr_block, G.bytes_per_block) == 0
  804. ) {
  805. if (G.prev_pair_equal) {
  806. /* The two preceding blocks were equal, and the current
  807. block is the same as the last one, so print nothing. */
  808. } else {
  809. puts("*");
  810. G.prev_pair_equal = 1;
  811. }
  812. } else {
  813. G.not_first = 1;
  814. G.prev_pair_equal = 0;
  815. for (i = 0; i < G.n_specs; i++) {
  816. if (i == 0)
  817. G.format_address(current_offset, '\0');
  818. else
  819. printf("%*s", address_pad_len_char - '0', "");
  820. (*G.spec[i].print_function) (n_bytes, curr_block, G.spec[i].fmt_string);
  821. if (G.spec[i].hexl_mode_trailer) {
  822. /* space-pad out to full line width, then dump the trailer */
  823. unsigned datum_width = width_bytes[G.spec[i].size];
  824. unsigned blank_fields = (G.bytes_per_block - n_bytes) / datum_width;
  825. unsigned field_width = G.spec[i].field_width + 1;
  826. printf("%*s", blank_fields * field_width, "");
  827. dump_hexl_mode_trailer(n_bytes, curr_block);
  828. }
  829. putchar('\n');
  830. }
  831. }
  832. }
  833. static void
  834. read_block(size_t n, char *block, size_t *n_bytes_in_buffer)
  835. {
  836. assert(0 < n && n <= G.bytes_per_block);
  837. *n_bytes_in_buffer = 0;
  838. if (n == 0)
  839. return;
  840. while (G.in_stream != NULL) { /* EOF. */
  841. size_t n_needed;
  842. size_t n_read;
  843. n_needed = n - *n_bytes_in_buffer;
  844. n_read = fread(block + *n_bytes_in_buffer, 1, n_needed, G.in_stream);
  845. *n_bytes_in_buffer += n_read;
  846. if (n_read == n_needed)
  847. break;
  848. /* error check is done in check_and_close */
  849. check_and_close();
  850. open_next_file();
  851. }
  852. }
  853. /* Return the least common multiple of the sizes associated
  854. with the format specs. */
  855. static int
  856. get_lcm(void)
  857. {
  858. size_t i;
  859. int l_c_m = 1;
  860. for (i = 0; i < G.n_specs; i++)
  861. l_c_m = lcm(l_c_m, width_bytes[(int) G.spec[i].size]);
  862. return l_c_m;
  863. }
  864. /* Read a chunk of size BYTES_PER_BLOCK from the input files, write the
  865. formatted block to standard output, and repeat until the specified
  866. maximum number of bytes has been read or until all input has been
  867. processed. If the last block read is smaller than BYTES_PER_BLOCK
  868. and its size is not a multiple of the size associated with a format
  869. spec, extend the input block with zero bytes until its length is a
  870. multiple of all format spec sizes. Write the final block. Finally,
  871. write on a line by itself the offset of the byte after the last byte
  872. read. */
  873. static void
  874. dump(off_t current_offset, off_t end_offset)
  875. {
  876. char *block[2];
  877. int idx;
  878. size_t n_bytes_read;
  879. block[0] = xmalloc(2 * G.bytes_per_block);
  880. block[1] = block[0] + G.bytes_per_block;
  881. idx = 0;
  882. if (option_mask32 & OPT_N) {
  883. while (1) {
  884. size_t n_needed;
  885. if (current_offset >= end_offset) {
  886. n_bytes_read = 0;
  887. break;
  888. }
  889. n_needed = MIN(end_offset - current_offset, (off_t) G.bytes_per_block);
  890. read_block(n_needed, block[idx], &n_bytes_read);
  891. if (n_bytes_read < G.bytes_per_block)
  892. break;
  893. assert(n_bytes_read == G.bytes_per_block);
  894. write_block(current_offset, n_bytes_read, block[idx ^ 1], block[idx]);
  895. current_offset += n_bytes_read;
  896. idx ^= 1;
  897. }
  898. } else {
  899. while (1) {
  900. read_block(G.bytes_per_block, block[idx], &n_bytes_read);
  901. if (n_bytes_read < G.bytes_per_block)
  902. break;
  903. assert(n_bytes_read == G.bytes_per_block);
  904. write_block(current_offset, n_bytes_read, block[idx ^ 1], block[idx]);
  905. current_offset += n_bytes_read;
  906. idx ^= 1;
  907. }
  908. }
  909. if (n_bytes_read > 0) {
  910. int l_c_m;
  911. size_t bytes_to_write;
  912. l_c_m = get_lcm();
  913. /* Make bytes_to_write the smallest multiple of l_c_m that
  914. is at least as large as n_bytes_read. */
  915. bytes_to_write = l_c_m * ((n_bytes_read + l_c_m - 1) / l_c_m);
  916. memset(block[idx] + n_bytes_read, 0, bytes_to_write - n_bytes_read);
  917. write_block(current_offset, bytes_to_write,
  918. block[idx ^ 1], block[idx]);
  919. current_offset += n_bytes_read;
  920. }
  921. G.format_address(current_offset, '\n');
  922. if ((option_mask32 & OPT_N) && current_offset >= end_offset)
  923. check_and_close();
  924. free(block[0]);
  925. }
  926. /* Read N bytes into BLOCK from the concatenation of the input files
  927. named in the global array FILE_LIST. On the first call to this
  928. function, the global variable IN_STREAM is expected to be an open
  929. stream associated with the input file INPUT_FILENAME. If all N
  930. bytes cannot be read from IN_STREAM, close IN_STREAM and update
  931. the global variables IN_STREAM and INPUT_FILENAME. Then try to
  932. read the remaining bytes from the newly opened file. Repeat if
  933. necessary until EOF is reached for the last file in FILE_LIST.
  934. On subsequent calls, don't modify BLOCK and return zero. Set
  935. *N_BYTES_IN_BUFFER to the number of bytes read. If an error occurs,
  936. it will be detected through ferror when the stream is about to be
  937. closed. If there is an error, give a message but continue reading
  938. as usual and return nonzero. Otherwise return zero. */
  939. /* STRINGS mode. Find each "string constant" in the input.
  940. A string constant is a run of at least 'string_min' ASCII
  941. graphic (or formatting) characters terminated by a null.
  942. Based on a function written by Richard Stallman for a
  943. traditional version of od. */
  944. static void
  945. dump_strings(off_t address, off_t end_offset)
  946. {
  947. unsigned bufsize = MAX(100, G.string_min);
  948. unsigned char *buf = xmalloc(bufsize);
  949. while (1) {
  950. size_t i;
  951. int c;
  952. /* See if the next 'G.string_min' chars are all printing chars. */
  953. tryline:
  954. if ((option_mask32 & OPT_N) && (end_offset - G.string_min <= address))
  955. break;
  956. i = 0;
  957. while (!(option_mask32 & OPT_N) || address < end_offset) {
  958. if (i == bufsize) {
  959. bufsize += bufsize/8;
  960. buf = xrealloc(buf, bufsize);
  961. }
  962. while (G.in_stream) { /* !EOF */
  963. c = fgetc(G.in_stream);
  964. if (c != EOF)
  965. goto got_char;
  966. check_and_close();
  967. open_next_file();
  968. }
  969. /* EOF */
  970. goto ret;
  971. got_char:
  972. address++;
  973. if (!c)
  974. break;
  975. if (!ISPRINT(c))
  976. goto tryline; /* It isn't; give up on this string. */
  977. buf[i++] = c; /* String continues; store it all. */
  978. }
  979. if (i < G.string_min) /* Too short! */
  980. goto tryline;
  981. /* If we get here, the string is all printable and NUL-terminated */
  982. buf[i] = 0;
  983. G.format_address(address - i - 1, ' ');
  984. for (i = 0; (c = buf[i]); i++) {
  985. switch (c) {
  986. case '\007': fputs("\\a", stdout); break;
  987. case '\b': fputs("\\b", stdout); break;
  988. case '\f': fputs("\\f", stdout); break;
  989. case '\n': fputs("\\n", stdout); break;
  990. case '\r': fputs("\\r", stdout); break;
  991. case '\t': fputs("\\t", stdout); break;
  992. case '\v': fputs("\\v", stdout); break;
  993. default: putchar(c);
  994. }
  995. }
  996. putchar('\n');
  997. }
  998. /* We reach this point only if we search through
  999. (max_bytes_to_format - G.string_min) bytes before reaching EOF. */
  1000. check_and_close();
  1001. ret:
  1002. free(buf);
  1003. }
  1004. #if ENABLE_LONG_OPTS
  1005. /* If S is a valid traditional offset specification with an optional
  1006. leading '+' return nonzero and set *OFFSET to the offset it denotes. */
  1007. static int
  1008. parse_old_offset(const char *s, off_t *offset)
  1009. {
  1010. static const struct suffix_mult Bb[] = {
  1011. { "B", 1024 },
  1012. { "b", 512 },
  1013. { "", 0 }
  1014. };
  1015. char *p;
  1016. int radix;
  1017. /* Skip over any leading '+'. */
  1018. if (s[0] == '+') ++s;
  1019. if (!isdigit(s[0])) return 0; /* not a number */
  1020. /* Determine the radix we'll use to interpret S. If there is a '.',
  1021. * it's decimal, otherwise, if the string begins with '0X'or '0x',
  1022. * it's hexadecimal, else octal. */
  1023. p = strchr(s, '.');
  1024. radix = 8;
  1025. if (p) {
  1026. p[0] = '\0'; /* cheating */
  1027. radix = 10;
  1028. } else if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  1029. radix = 16;
  1030. *offset = xstrtooff_sfx(s, radix, Bb);
  1031. if (p) p[0] = '.';
  1032. return (*offset >= 0);
  1033. }
  1034. #endif
  1035. int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1036. int od_main(int argc UNUSED_PARAM, char **argv)
  1037. {
  1038. #if ENABLE_LONG_OPTS
  1039. static const char od_longopts[] ALIGN1 =
  1040. "skip-bytes\0" Required_argument "j"
  1041. "address-radix\0" Required_argument "A"
  1042. "read-bytes\0" Required_argument "N"
  1043. "format\0" Required_argument "t"
  1044. "output-duplicates\0" No_argument "v"
  1045. /* Yes, it's true: -S NUM, but --strings[=NUM]!
  1046. * that is, NUM is mandatory for -S but optional for --strings!
  1047. */
  1048. "strings\0" Optional_argument "S"
  1049. "width\0" Optional_argument "w"
  1050. "traditional\0" No_argument "\xff"
  1051. ;
  1052. #endif
  1053. const char *str_A, *str_N, *str_j, *str_S = "3";
  1054. llist_t *lst_t = NULL;
  1055. unsigned opt;
  1056. int l_c_m;
  1057. /* The number of input bytes to skip before formatting and writing. */
  1058. off_t n_bytes_to_skip = 0;
  1059. /* The offset of the first byte after the last byte to be formatted. */
  1060. off_t end_offset = 0;
  1061. /* The maximum number of bytes that will be formatted. */
  1062. off_t max_bytes_to_format = 0;
  1063. INIT_G();
  1064. /*G.spec = NULL; - already is */
  1065. G.format_address = format_address_std;
  1066. address_base_char = 'o';
  1067. address_pad_len_char = '7';
  1068. /* Parse command line */
  1069. opt_complementary = "w+:t::"; /* -w N, -t is a list */
  1070. #if ENABLE_LONG_OPTS
  1071. applet_long_options = od_longopts;
  1072. #endif
  1073. opt = OD_GETOPT32();
  1074. argv += optind;
  1075. if (opt & OPT_A) {
  1076. static const char doxn[] ALIGN1 = "doxn";
  1077. static const char doxn_address_base_char[] ALIGN1 = {
  1078. 'u', 'o', 'x', /* '?' fourth one is not important */
  1079. };
  1080. static const uint8_t doxn_address_pad_len_char[] ALIGN1 = {
  1081. '7', '7', '6', /* '?' */
  1082. };
  1083. char *p;
  1084. int pos;
  1085. p = strchr(doxn, str_A[0]);
  1086. if (!p)
  1087. bb_error_msg_and_die("bad output address radix "
  1088. "'%c' (must be [doxn])", str_A[0]);
  1089. pos = p - doxn;
  1090. if (pos == 3) G.format_address = format_address_none;
  1091. address_base_char = doxn_address_base_char[pos];
  1092. address_pad_len_char = doxn_address_pad_len_char[pos];
  1093. }
  1094. if (opt & OPT_N) {
  1095. max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm_suffixes);
  1096. }
  1097. if (opt & OPT_a) decode_format_string("a");
  1098. if (opt & OPT_b) decode_format_string("oC");
  1099. if (opt & OPT_c) decode_format_string("c");
  1100. if (opt & OPT_d) decode_format_string("u2");
  1101. if (opt & OPT_f) decode_format_string("fF");
  1102. if (opt & OPT_h) decode_format_string("x2");
  1103. if (opt & OPT_i) decode_format_string("d2");
  1104. if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm_suffixes);
  1105. if (opt & OPT_l) decode_format_string("d4");
  1106. if (opt & OPT_o) decode_format_string("o2");
  1107. while (lst_t) {
  1108. decode_format_string(llist_pop(&lst_t));
  1109. }
  1110. if (opt & OPT_x) decode_format_string("x2");
  1111. if (opt & OPT_s) decode_format_string("d2");
  1112. if (opt & OPT_S) {
  1113. G.string_min = xstrtou_sfx(str_S, 0, bkm_suffixes);
  1114. }
  1115. // Bloat:
  1116. //if ((option_mask32 & OPT_S) && G.n_specs > 0)
  1117. // bb_error_msg_and_die("no type may be specified when dumping strings");
  1118. /* If the --traditional option is used, there may be from
  1119. * 0 to 3 remaining command line arguments; handle each case
  1120. * separately.
  1121. * od [FILE] [[+]OFFSET[.][b] [[+]LABEL[.][b]]]
  1122. * The offset and pseudo_start have the same syntax.
  1123. *
  1124. * FIXME: POSIX 1003.1-2001 with XSI requires support for the
  1125. * traditional syntax even if --traditional is not given. */
  1126. #if ENABLE_LONG_OPTS
  1127. if (opt & OPT_traditional) {
  1128. if (argv[0]) {
  1129. off_t pseudo_start = -1;
  1130. off_t o1, o2;
  1131. if (!argv[1]) { /* one arg */
  1132. if (parse_old_offset(argv[0], &o1)) {
  1133. /* od --traditional OFFSET */
  1134. n_bytes_to_skip = o1;
  1135. argv++;
  1136. }
  1137. /* od --traditional FILE */
  1138. } else if (!argv[2]) { /* two args */
  1139. if (parse_old_offset(argv[0], &o1)
  1140. && parse_old_offset(argv[1], &o2)
  1141. ) {
  1142. /* od --traditional OFFSET LABEL */
  1143. n_bytes_to_skip = o1;
  1144. pseudo_start = o2;
  1145. argv += 2;
  1146. } else if (parse_old_offset(argv[1], &o2)) {
  1147. /* od --traditional FILE OFFSET */
  1148. n_bytes_to_skip = o2;
  1149. argv[1] = NULL;
  1150. } else {
  1151. bb_error_msg_and_die("invalid second argument '%s'", argv[1]);
  1152. }
  1153. } else if (!argv[3]) { /* three args */
  1154. if (parse_old_offset(argv[1], &o1)
  1155. && parse_old_offset(argv[2], &o2)
  1156. ) {
  1157. /* od --traditional FILE OFFSET LABEL */
  1158. n_bytes_to_skip = o1;
  1159. pseudo_start = o2;
  1160. argv[1] = NULL;
  1161. } else {
  1162. bb_error_msg_and_die("the last two arguments must be offsets");
  1163. }
  1164. } else { /* >3 args */
  1165. bb_error_msg_and_die("too many arguments");
  1166. }
  1167. if (pseudo_start >= 0) {
  1168. if (G.format_address == format_address_none) {
  1169. address_base_char = 'o';
  1170. address_pad_len_char = '7';
  1171. G.format_address = format_address_paren;
  1172. } else {
  1173. G.format_address = format_address_label;
  1174. }
  1175. G_pseudo_offset = pseudo_start - n_bytes_to_skip;
  1176. }
  1177. }
  1178. /* else: od --traditional (without args) */
  1179. }
  1180. #endif
  1181. if (option_mask32 & OPT_N) {
  1182. end_offset = n_bytes_to_skip + max_bytes_to_format;
  1183. if (end_offset < n_bytes_to_skip)
  1184. bb_error_msg_and_die("SKIP + SIZE is too large");
  1185. }
  1186. if (G.n_specs == 0) {
  1187. decode_format_string("o2");
  1188. /*G.n_specs = 1; - done by decode_format_string */
  1189. }
  1190. /* If no files were listed on the command line,
  1191. set the global pointer FILE_LIST so that it
  1192. references the null-terminated list of one name: "-". */
  1193. G.file_list = bb_argv_dash;
  1194. if (argv[0]) {
  1195. /* Set the global pointer FILE_LIST so that it
  1196. references the first file-argument on the command-line. */
  1197. G.file_list = (char const *const *) argv;
  1198. }
  1199. /* Open the first input file */
  1200. open_next_file();
  1201. /* Skip over any unwanted header bytes */
  1202. skip(n_bytes_to_skip);
  1203. if (!G.in_stream)
  1204. return EXIT_FAILURE;
  1205. /* Compute output block length */
  1206. l_c_m = get_lcm();
  1207. if (opt & OPT_w) { /* -w: width */
  1208. if (!G.bytes_per_block || G.bytes_per_block % l_c_m != 0) {
  1209. bb_error_msg("warning: invalid width %u; using %d instead",
  1210. (unsigned)G.bytes_per_block, l_c_m);
  1211. G.bytes_per_block = l_c_m;
  1212. }
  1213. } else {
  1214. G.bytes_per_block = l_c_m;
  1215. if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
  1216. G.bytes_per_block *= DEFAULT_BYTES_PER_BLOCK / l_c_m;
  1217. }
  1218. #ifdef DEBUG
  1219. for (i = 0; i < G.n_specs; i++) {
  1220. printf("%d: fmt=\"%s\" width=%d\n",
  1221. i, spec[i].fmt_string, width_bytes[spec[i].size]);
  1222. }
  1223. #endif
  1224. if (option_mask32 & OPT_S)
  1225. dump_strings(n_bytes_to_skip, end_offset);
  1226. else
  1227. dump(n_bytes_to_skip, end_offset);
  1228. if (fclose(stdin))
  1229. bb_perror_msg_and_die(bb_msg_standard_input);
  1230. return G.exit_code;
  1231. }