od_bloaty.c 36 KB

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