od_bloaty.c 37 KB

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