od_bloaty.c 36 KB

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