od_bloaty.c 37 KB

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