3
0

od.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. //usage:#if !ENABLE_DESKTOP
  14. //usage:#define od_trivial_usage
  15. //usage: "[-aBbcDdeFfHhIiLlOovXx] [FILE]"
  16. //usage:#define od_full_usage "\n\n"
  17. //usage: "Print FILE (or stdin) unambiguously, as octal bytes by default"
  18. //usage:#endif
  19. #include "libbb.h"
  20. #if ENABLE_DESKTOP
  21. /* This one provides -t (busybox's own build script needs it) */
  22. #include "od_bloaty.c"
  23. #else
  24. #include "dump.h"
  25. static void
  26. odoffset(dumper_t *dumper, int argc, char ***argvp)
  27. {
  28. char *num, *p;
  29. int base;
  30. char *end;
  31. /*
  32. * The offset syntax of od(1) was genuinely bizarre. First, if
  33. * it started with a plus it had to be an offset. Otherwise, if
  34. * there were at least two arguments, a number or lower-case 'x'
  35. * followed by a number makes it an offset. By default it was
  36. * octal; if it started with 'x' or '0x' it was hex. If it ended
  37. * in a '.', it was decimal. If a 'b' or 'B' was appended, it
  38. * multiplied the number by 512 or 1024 byte units. There was
  39. * no way to assign a block count to a hex offset.
  40. *
  41. * We assumes it's a file if the offset is bad.
  42. */
  43. p = **argvp;
  44. if (!p) {
  45. /* hey someone is probably piping to us ... */
  46. return;
  47. }
  48. if ((*p != '+')
  49. && (argc < 2
  50. || (!isdigit(p[0])
  51. && ((p[0] != 'x') || !isxdigit(p[1])))))
  52. return;
  53. base = 0;
  54. /*
  55. * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
  56. * set base.
  57. */
  58. if (p[0] == '+')
  59. ++p;
  60. if (p[0] == 'x' && isxdigit(p[1])) {
  61. ++p;
  62. base = 16;
  63. } else if (p[0] == '0' && p[1] == 'x') {
  64. p += 2;
  65. base = 16;
  66. }
  67. /* skip over the number */
  68. if (base == 16)
  69. for (num = p; isxdigit(*p); ++p)
  70. continue;
  71. else
  72. for (num = p; isdigit(*p); ++p)
  73. continue;
  74. /* check for no number */
  75. if (num == p)
  76. return;
  77. /* if terminates with a '.', base is decimal */
  78. if (*p == '.') {
  79. if (base)
  80. return;
  81. base = 10;
  82. }
  83. dumper->dump_skip = strtol(num, &end, base ? base : 8);
  84. /* if end isn't the same as p, we got a non-octal digit */
  85. if (end != p)
  86. dumper->dump_skip = 0;
  87. else {
  88. if (*p) {
  89. if (*p == 'b') {
  90. dumper->dump_skip *= 512;
  91. ++p;
  92. } else if (*p == 'B') {
  93. dumper->dump_skip *= 1024;
  94. ++p;
  95. }
  96. }
  97. if (*p)
  98. dumper->dump_skip = 0;
  99. else {
  100. ++*argvp;
  101. /*
  102. * If the offset uses a non-octal base, the base of
  103. * the offset is changed as well. This isn't pretty,
  104. * but it's easy.
  105. */
  106. #define TYPE_OFFSET 7
  107. {
  108. char x_or_d;
  109. if (base == 16) {
  110. x_or_d = 'x';
  111. goto DO_X_OR_D;
  112. }
  113. if (base == 10) {
  114. x_or_d = 'd';
  115. DO_X_OR_D:
  116. dumper->fshead->nextfu->fmt[TYPE_OFFSET]
  117. = dumper->fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
  118. = x_or_d;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. static const char *const add_strings[] = {
  125. "16/1 \"%3_u \" \"\\n\"", /* a */
  126. "8/2 \" %06o \" \"\\n\"", /* B, o */
  127. "16/1 \"%03o \" \"\\n\"", /* b */
  128. "16/1 \"%3_c \" \"\\n\"", /* c */
  129. "8/2 \" %05u \" \"\\n\"", /* d */
  130. "4/4 \" %010u \" \"\\n\"", /* D */
  131. "2/8 \" %21.14e \" \"\\n\"", /* e (undocumented in od), F */
  132. "4/4 \" %14.7e \" \"\\n\"", /* f */
  133. "4/4 \" %08x \" \"\\n\"", /* H, X */
  134. "8/2 \" %04x \" \"\\n\"", /* h, x */
  135. "4/4 \" %11d \" \"\\n\"", /* I, L, l */
  136. "8/2 \" %6d \" \"\\n\"", /* i */
  137. "4/4 \" %011o \" \"\\n\"", /* O */
  138. };
  139. static const char od_opts[] ALIGN1 = "aBbcDdeFfHhIiLlOoXxv";
  140. static const char od_o2si[] ALIGN1 = {
  141. 0, 1, 2, 3, 5,
  142. 4, 6, 6, 7, 8,
  143. 9, 0xa, 0xb, 0xa, 0xa,
  144. 0xb, 1, 8, 9,
  145. };
  146. int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  147. int od_main(int argc, char **argv)
  148. {
  149. int ch;
  150. int first = 1;
  151. char *p;
  152. dumper_t *dumper = alloc_dumper();
  153. while ((ch = getopt(argc, argv, od_opts)) > 0) {
  154. if (ch == 'v') {
  155. dumper->dump_vflag = ALL;
  156. } else if (((p = strchr(od_opts, ch)) != NULL) && (*p != '\0')) {
  157. if (first) {
  158. first = 0;
  159. bb_dump_add(dumper, "\"%07.7_Ao\n\"");
  160. bb_dump_add(dumper, "\"%07.7_ao \"");
  161. } else {
  162. bb_dump_add(dumper, "\" \"");
  163. }
  164. bb_dump_add(dumper, add_strings[(int)od_o2si[(p - od_opts)]]);
  165. } else { /* P, p, s, w, or other unhandled */
  166. bb_show_usage();
  167. }
  168. }
  169. if (!dumper->fshead) {
  170. bb_dump_add(dumper, "\"%07.7_Ao\n\"");
  171. bb_dump_add(dumper, "\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
  172. }
  173. argc -= optind;
  174. argv += optind;
  175. odoffset(dumper, argc, &argv);
  176. return bb_dump_dump(dumper, argv);
  177. }
  178. #endif /* ENABLE_DESKTOP */
  179. /*-
  180. * Copyright (c) 1990 The Regents of the University of California.
  181. * All rights reserved.
  182. *
  183. * Redistribution and use in source and binary forms, with or without
  184. * modification, are permitted provided that the following conditions
  185. * are met:
  186. * 1. Redistributions of source code must retain the above copyright
  187. * notice, this list of conditions and the following disclaimer.
  188. * 2. Redistributions in binary form must reproduce the above copyright
  189. * notice, this list of conditions and the following disclaimer in the
  190. * documentation and/or other materials provided with the distribution.
  191. * 3. Neither the name of the University nor the names of its contributors
  192. * may be used to endorse or promote products derived from this software
  193. * without specific prior written permission.
  194. *
  195. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  196. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  197. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  198. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  199. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  200. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  201. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  202. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  203. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  204. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  205. * SUCH DAMAGE.
  206. */