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