1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 #endif /* not lint */ 34 #include <sys/cdefs.h> 35 #include <sys/types.h> 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <errno.h> 40 #include <float.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #include "hexdump.h" 47 48 #define PADDING " " 49 50 int odmode; 51 52 static void odadd(const char *); 53 static void odformat(const char *); 54 static const char *odformatfp(char, const char *); 55 static const char *odformatint(char, const char *); 56 static void odoffset(int, char ***); 57 static void odusage(void); 58 59 void 60 oldsyntax(int argc, char ***argvp) 61 { 62 static char empty[] = "", padding[] = PADDING; 63 int ch; 64 char **argv, *end; 65 66 /* Add initial (default) address format. -A may change it later. */ 67 #define TYPE_OFFSET 7 68 add("\"%07.7_Ao\n\""); 69 add("\"%07.7_ao \""); 70 71 odmode = 1; 72 argv = *argvp; 73 while ((ch = getopt(argc, argv, "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1) 74 switch (ch) { 75 case 'A': 76 switch (*optarg) { 77 case 'd': case 'o': case 'x': 78 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg; 79 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 80 *optarg; 81 break; 82 case 'n': 83 fshead->nextfu->fmt = empty; 84 fshead->nextfs->nextfu->fmt = padding; 85 break; 86 default: 87 errx(1, "%s: invalid address base", optarg); 88 } 89 break; 90 case 'a': 91 odformat("a"); 92 break; 93 case 'B': 94 case 'o': 95 odformat("o2"); 96 break; 97 case 'b': 98 odformat("o1"); 99 break; 100 case 'c': 101 odformat("c"); 102 break; 103 case 'd': 104 odformat("u2"); 105 break; 106 case 'D': 107 odformat("u4"); 108 break; 109 case 'e': /* undocumented in od */ 110 case 'F': 111 odformat("fD"); 112 break; 113 case 'f': 114 odformat("fF"); 115 break; 116 case 'H': 117 case 'X': 118 odformat("x4"); 119 break; 120 case 'h': 121 case 'x': 122 odformat("x2"); 123 break; 124 case 'I': 125 case 'L': 126 case 'l': 127 odformat("dL"); 128 break; 129 case 'i': 130 odformat("dI"); 131 break; 132 case 'j': 133 errno = 0; 134 skip = strtoll(optarg, &end, 0); 135 if (*end == 'b') 136 skip *= 512; 137 else if (*end == 'k') 138 skip *= 1024; 139 else if (*end == 'm') 140 skip *= 1048576L; 141 if (errno != 0 || skip < 0 || strlen(end) > 1) 142 errx(1, "%s: invalid skip amount", optarg); 143 break; 144 case 'N': 145 if ((length = atoi(optarg)) <= 0) 146 errx(1, "%s: invalid length", optarg); 147 break; 148 case 'O': 149 odformat("o4"); 150 break; 151 case 's': 152 odformat("d2"); 153 break; 154 case 't': 155 odformat(optarg); 156 break; 157 case 'v': 158 vflag = ALL; 159 break; 160 case '?': 161 default: 162 odusage(); 163 } 164 165 if (fshead->nextfs->nextfs == NULL) 166 odformat("oS"); 167 168 argc -= optind; 169 *argvp += optind; 170 171 if (argc) 172 odoffset(argc, argvp); 173 } 174 175 static void 176 odusage(void) 177 { 178 179 fprintf(stderr, 180 "usage: od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]\n"); 181 fprintf(stderr, 182 " [[+]offset[.][Bb]] [file ...]\n"); 183 exit(1); 184 } 185 186 static void 187 odoffset(int argc, char ***argvp) 188 { 189 char *p, *num, *end; 190 int base; 191 192 /* 193 * The offset syntax of od(1) was genuinely bizarre. First, if 194 * it started with a plus it had to be an offset. Otherwise, if 195 * there were at least two arguments, a number or lower-case 'x' 196 * followed by a number makes it an offset. By default it was 197 * octal; if it started with 'x' or '0x' it was hex. If it ended 198 * in a '.', it was decimal. If a 'b' or 'B' was appended, it 199 * multiplied the number by 512 or 1024 byte units. There was 200 * no way to assign a block count to a hex offset. 201 * 202 * We assume it's a file if the offset is bad. 203 */ 204 p = argc == 1 ? (*argvp)[0] : (*argvp)[1]; 205 206 if (*p != '+' && (argc < 2 || 207 (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1]))))) 208 return; 209 210 base = 0; 211 /* 212 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and 213 * set base. 214 */ 215 if (p[0] == '+') 216 ++p; 217 if (p[0] == 'x' && isxdigit(p[1])) { 218 ++p; 219 base = 16; 220 } else if (p[0] == '0' && p[1] == 'x') { 221 p += 2; 222 base = 16; 223 } 224 225 /* skip over the number */ 226 if (base == 16) 227 for (num = p; isxdigit(*p); ++p); 228 else 229 for (num = p; isdigit(*p); ++p); 230 231 /* check for no number */ 232 if (num == p) 233 return; 234 235 /* if terminates with a '.', base is decimal */ 236 if (*p == '.') { 237 if (base) 238 return; 239 base = 10; 240 } 241 242 skip = strtoll(num, &end, base ? base : 8); 243 244 /* if end isn't the same as p, we got a non-octal digit */ 245 if (end != p) { 246 skip = 0; 247 return; 248 } 249 250 if (*p) { 251 if (*p == 'B') { 252 skip *= 1024; 253 ++p; 254 } else if (*p == 'b') { 255 skip *= 512; 256 ++p; 257 } 258 } 259 260 if (*p) { 261 skip = 0; 262 return; 263 } 264 265 /* 266 * If the offset uses a non-octal base, the base of the offset 267 * is changed as well. This isn't pretty, but it's easy. 268 */ 269 if (base == 16) { 270 fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; 271 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; 272 } else if (base == 10) { 273 fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; 274 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; 275 } 276 277 /* Terminate file list. */ 278 (*argvp)[1] = NULL; 279 } 280 281 static void 282 odformat(const char *fmt) 283 { 284 char fchar; 285 286 while (*fmt != '\0') { 287 switch ((fchar = *fmt++)) { 288 case 'a': 289 odadd("16/1 \"%3_u \" \"\\n\""); 290 break; 291 case 'c': 292 odadd("16/1 \"%3_c \" \"\\n\""); 293 break; 294 case 'o': case 'u': case 'd': case 'x': 295 fmt = odformatint(fchar, fmt); 296 break; 297 case 'f': 298 fmt = odformatfp(fchar, fmt); 299 break; 300 default: 301 errx(1, "%c: unrecognised format character", fchar); 302 } 303 } 304 } 305 306 static const char * 307 odformatfp(char fchar __unused, const char *fmt) 308 { 309 size_t isize; 310 int digits; 311 char *end, *hdfmt; 312 313 isize = sizeof(double); 314 switch (*fmt) { 315 case 'F': 316 isize = sizeof(float); 317 fmt++; 318 break; 319 case 'D': 320 isize = sizeof(double); 321 fmt++; 322 break; 323 case 'L': 324 isize = sizeof(long double); 325 fmt++; 326 break; 327 default: 328 if (isdigit((unsigned char)*fmt)) { 329 errno = 0; 330 isize = (size_t)strtoul(fmt, &end, 10); 331 if (errno != 0 || isize == 0) 332 errx(1, "%s: invalid size", fmt); 333 fmt = (const char *)end; 334 } 335 } 336 switch (isize) { 337 case sizeof(float): 338 digits = FLT_DIG; 339 break; 340 case sizeof(double): 341 digits = DBL_DIG; 342 break; 343 default: 344 if (isize == sizeof(long double)) 345 digits = LDBL_DIG; 346 else 347 errx(1, "unsupported floating point size %lu", 348 (u_long)isize); 349 } 350 351 asprintf(&hdfmt, "%lu/%lu \" %%%d.%de \" \"\\n\"", 352 16UL / (u_long)isize, (u_long)isize, digits + 8, digits); 353 if (hdfmt == NULL) 354 err(1, NULL); 355 odadd(hdfmt); 356 free(hdfmt); 357 358 return (fmt); 359 } 360 361 static const char * 362 odformatint(char fchar, const char *fmt) 363 { 364 unsigned long long n; 365 size_t isize; 366 int digits; 367 char *end, *hdfmt; 368 369 isize = sizeof(int); 370 switch (*fmt) { 371 case 'C': 372 isize = sizeof(char); 373 fmt++; 374 break; 375 case 'I': 376 isize = sizeof(int); 377 fmt++; 378 break; 379 case 'L': 380 isize = sizeof(long); 381 fmt++; 382 break; 383 case 'S': 384 isize = sizeof(short); 385 fmt++; 386 break; 387 default: 388 if (isdigit((unsigned char)*fmt)) { 389 errno = 0; 390 isize = (size_t)strtoul(fmt, &end, 10); 391 if (errno != 0 || isize == 0) 392 errx(1, "%s: invalid size", fmt); 393 if (isize != sizeof(char) && isize != sizeof(short) && 394 isize != sizeof(int) && isize != sizeof(long)) 395 errx(1, "unsupported int size %lu", 396 (u_long)isize); 397 fmt = (const char *)end; 398 } 399 } 400 401 /* 402 * Calculate the maximum number of digits we need to 403 * fit the number. Overestimate for decimal with log 404 * base 8. We need one extra space for signed numbers 405 * to store the sign. 406 */ 407 n = (1ULL << (8 * isize)) - 1; 408 digits = 0; 409 while (n != 0) { 410 digits++; 411 n >>= (fchar == 'x') ? 4 : 3; 412 } 413 if (fchar == 'd') 414 digits++; 415 asprintf(&hdfmt, "%lu/%lu \"%*s%%%s%d%c\" \"\\n\"", 416 16UL / (u_long)isize, (u_long)isize, (int)(4 * isize - digits), 417 "", (fchar == 'd' || fchar == 'u') ? "" : "0", digits, fchar); 418 if (hdfmt == NULL) 419 err(1, NULL); 420 odadd(hdfmt); 421 free(hdfmt); 422 423 return (fmt); 424 } 425 426 static void 427 odadd(const char *fmt) 428 { 429 static int needpad; 430 431 if (needpad) 432 add("\""PADDING"\""); 433 add(fmt); 434 needpad = 1; 435 } 436