od.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * od implementation for busybox
  4. * Based on code from util-linux v 2.11l
  5. *
  6. * Copyright (c) 1990
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. *
  11. * Original copyright notice is retained at the end of this file.
  12. */
  13. //config:config OD
  14. //config: bool "od (11 kb)"
  15. //config: default y
  16. //config: help
  17. //config: od is used to dump binary files in octal and other formats.
  18. //applet:IF_OD(APPLET(od, BB_DIR_USR_BIN, BB_SUID_DROP))
  19. //kbuild:lib-$(CONFIG_OD) += od.o
  20. //usage:#if !ENABLE_DESKTOP
  21. //usage:#define od_trivial_usage
  22. //usage: "[-abcdeFfhiloxsv] [FILE]"
  23. // We also support -BDOHXIL, but they are not documented in coreutils 9.1
  24. // manpage/help, so don't show them either.
  25. //usage:#define od_full_usage "\n\n"
  26. //usage: "Print FILE (or stdin) unambiguously, as octal bytes by default"
  27. //usage:#endif
  28. #include "libbb.h"
  29. #if ENABLE_DESKTOP
  30. /* This one provides -t (busybox's own build script needs it) */
  31. #include "od_bloaty.c"
  32. #else
  33. #include "dump.h"
  34. static void
  35. odoffset(dumper_t *dumper, int argc, char ***argvp)
  36. {
  37. char *num, *p;
  38. int base;
  39. char *end;
  40. /*
  41. * The offset syntax of od(1) was genuinely bizarre. First, if
  42. * it started with a plus it had to be an offset. Otherwise, if
  43. * there were at least two arguments, a number or lower-case 'x'
  44. * followed by a number makes it an offset. By default it was
  45. * octal; if it started with 'x' or '0x' it was hex. If it ended
  46. * in a '.', it was decimal. If a 'b' or 'B' was appended, it
  47. * multiplied the number by 512 or 1024 byte units. There was
  48. * no way to assign a block count to a hex offset.
  49. *
  50. * We assumes it's a file if the offset is bad.
  51. */
  52. p = **argvp;
  53. if (!p) {
  54. /* hey someone is probably piping to us ... */
  55. return;
  56. }
  57. if ((*p != '+')
  58. && (argc < 2
  59. || (!isdigit(p[0])
  60. && ((p[0] != 'x') || !isxdigit(p[1])))))
  61. return;
  62. base = 0;
  63. /*
  64. * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
  65. * set base.
  66. */
  67. if (p[0] == '+')
  68. ++p;
  69. if (p[0] == 'x' && isxdigit(p[1])) {
  70. ++p;
  71. base = 16;
  72. } else if (p[0] == '0' && p[1] == 'x') {
  73. p += 2;
  74. base = 16;
  75. }
  76. /* skip over the number */
  77. if (base == 16)
  78. for (num = p; isxdigit(*p); ++p)
  79. continue;
  80. else
  81. for (num = p; isdigit(*p); ++p)
  82. continue;
  83. /* check for no number */
  84. if (num == p)
  85. return;
  86. /* if terminates with a '.', base is decimal */
  87. if (*p == '.') {
  88. if (base)
  89. return;
  90. base = 10;
  91. }
  92. dumper->dump_skip = strtol(num, &end, base ? base : 8);
  93. /* if end isn't the same as p, we got a non-octal digit */
  94. if (end != p)
  95. dumper->dump_skip = 0;
  96. else {
  97. if (*p) {
  98. if (*p == 'b') {
  99. dumper->dump_skip *= 512;
  100. ++p;
  101. } else if (*p == 'B') {
  102. dumper->dump_skip *= 1024;
  103. ++p;
  104. }
  105. }
  106. if (*p)
  107. dumper->dump_skip = 0;
  108. else {
  109. ++*argvp;
  110. /*
  111. * If the offset uses a non-octal base, the base of
  112. * the offset is changed as well. This isn't pretty,
  113. * but it's easy.
  114. */
  115. #define TYPE_OFFSET 7
  116. {
  117. char x_or_d;
  118. if (base == 16) {
  119. x_or_d = 'x';
  120. goto DO_X_OR_D;
  121. }
  122. if (base == 10) {
  123. x_or_d = 'd';
  124. DO_X_OR_D:
  125. dumper->fshead->nextfu->fmt[TYPE_OFFSET]
  126. = dumper->fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
  127. = x_or_d;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. // bb_dump_add():
  134. // A format string contains format units separated by [optional] whitespace.
  135. // A format unit contains up to three items: an iteration count, a byte count,
  136. // and a format.
  137. // The iteration count is an optional integer (default 1).
  138. // Each format is applied iteration count times.
  139. // The byte count is an optional integer. It defines the number
  140. // of bytes to be interpreted by each iteration of the format.
  141. // If an iteration count and/or a byte count is specified, a slash must be
  142. // placed after the iteration count and/or before the byte count
  143. // to disambiguate them.
  144. // The printf-style format is required and must be surrounded by " "s.
  145. // (Below, each string contains two format units)
  146. static const char *const add_strings[] ALIGN_PTR = {
  147. "16/1 \" %3_u\"" "\"\n\"", /* 0: a */
  148. "8/2 \" %06o\"" "\"\n\"", /* 1: B (undocumented in od), o */
  149. "16/1 \" %03o\"" "\"\n\"", /* 2: b */
  150. "16/1 \" %3_c\"" "\"\n\"", /* 3: c */
  151. "8/2 \" %5u\"" "\"\n\"", /* 4: d */
  152. "4/4 \" %10u\"" "\"\n\"", /* 5: D */
  153. "2/8 \" %24.14e\"" "\"\n\"", /* 6: e (undocumented in od), F */
  154. "4/4 \" %15.7e\"" "\"\n\"", /* 7: f */
  155. "4/4 \" %08x\"" "\"\n\"", /* 8: H, X */
  156. "8/2 \" %04x\"" "\"\n\"", /* 9: h, x */
  157. "4/4 \" %11d\"" "\"\n\"", /* 10: i */
  158. "4/4 \" %011o\"" "\"\n\"", /* 11: O */
  159. "8/2 \" %6d\"" "\"\n\"", /* 12: s */
  160. /* -I,L,l: depend on word width of the arch (what is "long"?) */
  161. #if ULONG_MAX > 0xffffffff
  162. "2/8 \" %20lld\"" "\"\n\"", /* 13: I, L, l */
  163. #define L_ 13
  164. #else
  165. /* 32-bit arch: -I,L,l are the same as -i */
  166. #define L_ 10
  167. #endif
  168. };
  169. static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxsv";
  170. static const char od_o2si[] ALIGN1 = {
  171. 0, 1, 2, 3, 5, /* aBbcD */
  172. 4, 6, 6, 7, 8, /* deFfH */
  173. 9, L_, 10, L_, L_, /* hIiLl */
  174. 11, 1, 8, 9, 12 /* OoXxs */
  175. };
  176. int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  177. int od_main(int argc, char **argv)
  178. {
  179. int ch;
  180. int first = 1;
  181. char *p;
  182. dumper_t *dumper = alloc_dumper();
  183. while ((ch = getopt(argc, argv, od_opts)) > 0) {
  184. if (ch == 'v') {
  185. dumper->dump_vflag = ALL;
  186. } else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) {
  187. if (first) {
  188. first = 0;
  189. bb_dump_add(dumper, "\"%07.7_Ao\n\"");
  190. bb_dump_add(dumper, "\"%07.7_ao\"");
  191. } else {
  192. bb_dump_add(dumper, "\" \"");
  193. }
  194. bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
  195. } else { /* P, p, w, or other unhandled */
  196. bb_show_usage();
  197. }
  198. }
  199. if (!dumper->fshead) {
  200. bb_dump_add(dumper, "\"%07.7_Ao\n\"");
  201. bb_dump_add(dumper, "\"%07.7_ao\"");
  202. bb_dump_add(dumper, add_strings[1]); /* -o format is default */
  203. }
  204. dumper->od_eofstring = "\n";
  205. argc -= optind;
  206. argv += optind;
  207. odoffset(dumper, argc, &argv);
  208. return bb_dump_dump(dumper, argv);
  209. }
  210. #endif /* !ENABLE_DESKTOP */
  211. /*-
  212. * Copyright (c) 1990 The Regents of the University of California.
  213. * All rights reserved.
  214. *
  215. * Redistribution and use in source and binary forms, with or without
  216. * modification, are permitted provided that the following conditions
  217. * are met:
  218. * 1. Redistributions of source code must retain the above copyright
  219. * notice, this list of conditions and the following disclaimer.
  220. * 2. Redistributions in binary form must reproduce the above copyright
  221. * notice, this list of conditions and the following disclaimer in the
  222. * documentation and/or other materials provided with the distribution.
  223. * 3. Neither the name of the University nor the names of its contributors
  224. * may be used to endorse or promote products derived from this software
  225. * without specific prior written permission.
  226. *
  227. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
  228. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  229. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  230. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  231. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  232. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  233. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  234. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  235. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  236. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  237. * SUCH DAMAGE.
  238. */