od_bloaty.c 37 KB

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