od.c (9082B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include "queue.h" 4 #include "util.h" 5 6 #include <ctype.h> 7 #include <fcntl.h> 8 #include <stdint.h> 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <unistd.h> 13 14 struct type { 15 unsigned char format; 16 unsigned int len; 17 TAILQ_ENTRY(type) entry; 18 }; 19 20 static TAILQ_HEAD(head, type) head = TAILQ_HEAD_INITIALIZER(head); 21 static unsigned char addr_format = 'o'; 22 static off_t skip = 0; 23 static off_t max = -1; 24 static size_t linelen = 1; 25 static int big_endian; 26 27 static void 28 printaddress(off_t addr) 29 { 30 char fmt[] = "%07j#"; 31 32 if (addr_format == 'n') { 33 fputc(' ', stdout); 34 } else { 35 fmt[4] = addr_format; 36 printf(fmt, (intmax_t)addr); 37 } 38 } 39 40 static void 41 printchunk(const unsigned char *s, unsigned char format, size_t len) 42 { 43 long long res, basefac; 44 size_t i; 45 char fmt[] = " %#*ll#"; 46 unsigned char c; 47 48 const char *namedict[] = { 49 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", 50 "vt", "ff", "cr", "so", "si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", 51 "syn", "etb", "can", "em", "sub", "esc", "fs", "gs", "rs", "us", "sp", 52 }; 53 const char *escdict[] = { 54 ['\0'] = "\\0", 55 ['\a'] = "\\a", 56 ['\b'] = "\\b", 57 ['\t'] = "\\t", 58 ['\n'] = "\\n", 59 ['\v'] = "\\v", 60 ['\f'] = "\\f", 61 ['\r'] = "\\r", 62 }; 63 64 switch (format) { 65 case 'a': 66 c = *s & ~128; /* clear high bit as required by standard */ 67 if (c < LEN(namedict) || c == 127) { 68 printf(" %3s", (c == 127) ? "del" : namedict[c]); 69 } else { 70 printf(" %3c", c); 71 } 72 break; 73 case 'c': 74 if (strchr("\a\b\t\n\v\f\r\0", *s)) { 75 printf(" %3s", escdict[*s]); 76 } else if (!isprint(*s)) { 77 printf(" %3o", *s); 78 } else { 79 printf(" %3c", *s); 80 } 81 break; 82 default: 83 if (big_endian) { 84 for (res = 0, basefac = 1, i = len; i; i--) { 85 res += s[i - 1] * basefac; 86 basefac <<= 8; 87 } 88 } else { 89 for (res = 0, basefac = 1, i = 0; i < len; i++) { 90 res += s[i] * basefac; 91 basefac <<= 8; 92 } 93 } 94 /* x/o print a fixed-width, zero-padded field: 2 hex digits per 95 * byte, or ceil(8*len/3) octal digits. d/u stay right-justified 96 * with spaces at the older estimate, where an oversized width 97 * only adds leading whitespace */ 98 fmt[2] = (format == 'x' || format == 'o') ? '0' : ' '; 99 fmt[6] = format; 100 switch (format) { 101 case 'x': 102 printf(fmt, (int)(2 * len), res); 103 break; 104 case 'o': 105 printf(fmt, (int)((8 * len + 2) / 3), res); 106 break; 107 default: 108 printf(fmt, (int)(3 * len + len - 1), res); 109 break; 110 } 111 } 112 } 113 114 static void 115 printline(const unsigned char *line, size_t len, off_t addr) 116 { 117 struct type *t = NULL; 118 size_t i; 119 int first = 1; 120 unsigned char *tmp; 121 122 if (TAILQ_EMPTY(&head)) 123 goto once; 124 TAILQ_FOREACH(t, &head, entry) 125 { 126 once: 127 if (first) { 128 printaddress(addr); 129 first = 0; 130 } else { 131 printf("%*c", (addr_format == 'n') ? 1 : 7, ' '); 132 } 133 for (i = 0; i < len; i += MIN(len - i, t ? t->len : 4)) { 134 if (len - i < (t ? t->len : 4)) { 135 tmp = ecalloc(t ? t->len : 4, 1); 136 memcpy(tmp, line + i, len - i); 137 printchunk(tmp, t ? t->format : 'o', t ? t->len : 4); 138 free(tmp); 139 } else { 140 printchunk(line + i, t ? t->format : 'o', t ? t->len : 4); 141 } 142 } 143 fputc('\n', stdout); 144 if (TAILQ_EMPTY(&head) || (!len && !first)) 145 break; 146 } 147 } 148 149 static int 150 od(int fd, char *fname, int last) 151 { 152 static unsigned char *line; 153 static size_t lineoff; 154 static off_t addr; 155 unsigned char buf[BUFSIZ]; 156 size_t i, size = sizeof(buf); 157 ssize_t n; 158 159 while (skip - addr > 0) { 160 n = read(fd, buf, MIN((size_t)(skip - addr), sizeof(buf))); 161 if (n < 0) 162 weprintf("read %s:", fname); 163 if (n <= 0) 164 return n; 165 addr += n; 166 } 167 if (!line) 168 line = emalloc(linelen); 169 170 for (;;) { 171 if (max >= 0) 172 size = MIN((size_t)(max - (addr - skip)), size); 173 if ((n = read(fd, buf, size)) <= 0) 174 break; 175 for (i = 0; i < (size_t)n; i++, addr++) { 176 line[lineoff++] = buf[i]; 177 if (lineoff == linelen) { 178 printline(line, lineoff, addr - lineoff + 1); 179 lineoff = 0; 180 } 181 } 182 } 183 if (n < 0) { 184 weprintf("read %s:", fname); 185 return n; 186 } 187 if (lineoff && last) 188 printline(line, lineoff, addr - lineoff); 189 /* the trailing all-data-consumed line exists to show the final 190 * address (the file's total size); with -an there is no address to 191 * show, so it degrades to a content-free, address-column-only blank 192 * line nothing after it can use, unlike every other od */ 193 if (last && addr_format != 'n') 194 printline((unsigned char *)"", 0, addr); 195 return 0; 196 } 197 198 static int 199 lcm(unsigned int a, unsigned int b) 200 { 201 unsigned int c, d, e; 202 203 for (c = a, d = b; c;) { 204 e = c; 205 c = d % c; 206 d = e; 207 } 208 209 return a / d * b; 210 } 211 212 static void 213 addtype(char format, int len) 214 { 215 struct type *t; 216 217 t = emalloc(sizeof(*t)); 218 t->format = format; 219 t->len = len; 220 TAILQ_INSERT_TAIL(&head, t, entry); 221 } 222 223 static void 224 usage(void) 225 { 226 eprintf( 227 "usage: %s [-bdosvx] [-A addressformat] " 228 #if FEATURE_OD_ENDIAN 229 "[-E | -e] " 230 #endif 231 "[-j skip] [-t outputformat] [file ...]\n", 232 argv0 233 ); 234 } 235 236 // ?man od: dump files in formats 237 // ?man display file contents in octal, hex, or other formats 238 int 239 main(int argc, char *argv[]) 240 { 241 struct type *t; 242 char *s, *end; 243 int fd, ret = 0, len, fmt_char; 244 245 big_endian = (*(uint16_t *)"\0\xff" == 0xff); 246 247 ARGBEGIN 248 { 249 // ?man -A:str: specify option flag 250 case 'A': 251 s = EARGF(usage()); 252 if (strlen(s) != 1 || !strchr("doxn", s[0])) 253 usage(); 254 addr_format = s[0]; 255 break; 256 // ?man -b: specify block size or base directory 257 case 'b': 258 addtype('o', 1); 259 break; 260 // ?man -d: specify directory 261 case 'd': 262 addtype('u', 2); 263 break; 264 #if FEATURE_OD_ENDIAN 265 // ?man -E: specify option flag 266 case 'E': 267 // ?man -e: specify expression or pattern 268 case 'e': 269 big_endian = (ARGC() == 'E'); 270 break; 271 #endif 272 // ?man -j:str: specify option flag 273 case 'j': 274 if ((skip = parseoffset(EARGF(usage()))) < 0) 275 usage(); 276 break; 277 // ?man -N:str: specify option flag 278 case 'N': 279 if ((max = parseoffset(EARGF(usage()))) < 0) 280 usage(); 281 break; 282 // ?man -o: specify output file 283 case 'o': 284 addtype('o', 2); 285 break; 286 // ?man -s: silent mode or print summary 287 case 's': 288 addtype('d', 2); 289 break; 290 // ?man -t:str: sort or specify timestamp 291 case 't': 292 s = EARGF(usage()); 293 for (; *s; s++) { 294 switch (*s) { 295 case 'a': 296 case 'c': 297 addtype(*s, 1); 298 break; 299 case 'd': 300 case 'o': 301 case 'u': 302 case 'x': 303 fmt_char = *s; 304 if (isdigit((unsigned char)*(s + 1))) { 305 len = strtol(s + 1, &end, 10); 306 s = end - 1; 307 } else { 308 switch (*(s + 1)) { 309 case 'C': 310 len = sizeof(char); 311 s++; 312 break; 313 case 'S': 314 len = sizeof(short); 315 s++; 316 break; 317 case 'I': 318 len = sizeof(int); 319 s++; 320 break; 321 case 'L': 322 len = sizeof(long); 323 s++; 324 break; 325 default: 326 len = sizeof(int); 327 } 328 } 329 addtype(fmt_char, len); 330 break; 331 default: 332 usage(); 333 } 334 } 335 break; 336 // ?man -v: verbose mode; show progress 337 case 'v': 338 /* always set, use uniq(1) to handle duplicate lines */ 339 break; 340 // ?man -x: hex format or match whole lines 341 case 'x': 342 addtype('x', 2); 343 break; 344 default: 345 usage(); 346 } 347 ARGEND 348 349 /* line length is lcm of type lengths and >= 16 by doubling */ 350 TAILQ_FOREACH(t, &head, entry) 351 linelen = lcm(linelen, t->len); 352 if (TAILQ_EMPTY(&head)) 353 linelen = 16; 354 while (linelen < 16) 355 linelen *= 2; 356 357 if (!argc) { 358 if (od(0, "<stdin>", 1) < 0) 359 ret = 1; 360 } else { 361 for (; *argv; argc--, argv++) { 362 if (!strcmp(*argv, "-")) { 363 *argv = "<stdin>"; 364 fd = 0; 365 } else if ((fd = open(*argv, O_RDONLY)) < 0) { 366 weprintf("open %s:", *argv); 367 ret = 1; 368 continue; 369 } 370 if (od(fd, *argv, (!*(argv + 1))) < 0) 371 ret = 1; 372 if (fd != 0) 373 close(fd); 374 } 375 } 376 377 ret |= fshut(stdout, "<stdout>") | fshut(stderr, "<stderr>"); 378 379 return ret; 380 }