od_bloaty.c 37 KB

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