od_bloaty.c 38 KB

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