📄 od.c
字号:
#ifndef lintstatic char *sccsid = "@(#)od.c 4.1 (ULTRIX) 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1985 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//*static char *sccsid = "@(#)od.c 5.11 (Berkeley) 4/29/83";*//* * od -- octal, hex, decimal, character dump of data in a file. * * usage: od [-abBcdDefFhHiIlLopPs[n]vw[n]xX] [file] [[+]offset[.][b] [label]] * * where the option flags have the following meaning: * character object radix signed? * a byte (10) (n.a.) ASCII named byte stream * b byte 8 no byte octal * c byte (8) (no) character with octal non-graphic bytes * d short 10 no * D long 10 no * e,F double (10) double precision floating pt. * f float (10) single precision floating pt. * h,x short 16 no * H,X long 16 no * i short 10 yes * I,l,L long 10 yes * o,B short 8 no (default conversion) * O long 8 no * s[n] string (8) ASCII graphic strings * * p indicate EVEN parity on 'a' conversion * P indicate ODD parity on 'a' conversion * v show all data - don't skip like lines. * w[n] bytes per display line * * More than one format character may be given. * If {file} is not specified, standard input is read. * If {file} is not specified, then {offset} must start with '+'. * {Offset} may be HEX (0xnnn), OCTAL (0nn), or decimal (nnn.). * The default is octal. The same radix will be used to display the address. */#include <stdio.h>#define DBUF_SIZE BUFSIZ#define BIG_DBUF 32#define NO 0#define YES 1#define EVEN -1#define ODD 1#define UNSIGNED 0#define SIGNED 1#define PADDR 1#define MIN_SLEN 3int a_put();int b_put();int c_put();int s_put();int us_put();int l_put();int f_put();int d_put();int st_put();struct dfmt { int df_field; /* external field required for object */ int df_size; /* size (bytes) of object */ int df_radix; /* conversion radix */ int df_signed; /* signed? flag */ int df_paddr; /* "put address on each line?" flag */ int (*df_put)(); /* function to output object */ char *df_fmt; /* output string format */} *conv_vec[32]; /* vector of conversions to be done */struct dfmt ascii = { 3, sizeof (char), 10, 0, PADDR, a_put, 0};struct dfmt byte = { 3, sizeof (char), 8, UNSIGNED, PADDR, b_put, 0};struct dfmt cchar = { 3, sizeof (char), 8, UNSIGNED, PADDR, c_put, 0};struct dfmt u_s_oct = { 6, sizeof (short), 8, UNSIGNED, PADDR, us_put, 0};struct dfmt u_s_dec = { 5, sizeof (short), 10, UNSIGNED, PADDR, us_put, 0};struct dfmt u_s_hex = { 4, sizeof (short), 16, UNSIGNED, PADDR, us_put, 0};struct dfmt u_l_oct = {11, sizeof (long), 8, UNSIGNED, PADDR, l_put, 0};struct dfmt u_l_dec = {10, sizeof (long), 10, UNSIGNED, PADDR, l_put, 0};struct dfmt u_l_hex = { 8, sizeof (long), 16, UNSIGNED, PADDR, l_put, 0};struct dfmt s_s_dec = { 6, sizeof (short), 10, SIGNED, PADDR, s_put, 0};struct dfmt s_l_dec = {11, sizeof (long), 10, SIGNED, PADDR, l_put, 0};struct dfmt flt = {14, sizeof (float), 10, SIGNED, PADDR, f_put, 0};struct dfmt dble = {21, sizeof (double), 10, SIGNED, PADDR, d_put, 0};struct dfmt string = { 0, 0, 8, 0, NO, st_put, 0};char usage[] ="usage: od [-abcdfhilopswvx] [file] [[+]offset[.][b] [label]]";char dbuf[DBUF_SIZE];char lastdbuf[DBUF_SIZE];int addr_base = 8; /* default address base is OCTAL */long addr = 0L; /* current file offset */long label = -1L; /* current label; -1 is "off" */int dbuf_size = 16; /* file bytes / display line */int _parity = NO; /* show parity on ascii bytes */char fmt[] = " %s"; /* 12 blanks */char *icvt();char *scvt();char *underline();long get_addr();/* * special form of _ctype */#define A 01#define G 02#define D 04#define P 010#define X 020#define isdigit(c) (_ctype[c] & D)#define isascii(c) (_ctype[c] & A)#define isgraphic(c) (_ctype[c] & G)#define isprint(c) (_ctype[c] & P)#define ishex(c) (_ctype[c] & (X|D))char _ctype[256] = {/* 000 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 010 */ A, A, A, 0, A, A, 0, 0,/* 020 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 030 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 040 */ P|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 050 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 060 */ P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,/* 070 */ P|G|D|A,P|G|D|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 100 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 110 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 120 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 130 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 140 */ P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A, P|G|A,/* 150 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 160 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A,/* 170 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, 0,/* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 210 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 220 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 230 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 250 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 260 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 270 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 300 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 310 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 320 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 330 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 340 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 350 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 360 */ 0, 0, 0, 0, 0, 0, 0, 0,/* 370 */ 0, 0, 0, 0, 0, 0, 0, 0,};main(argc, argv)int argc;char **argv;{ register char *p; register char *l; register int nelm, n, same; struct dfmt *d; struct dfmt **cv = conv_vec; int showall = NO; int field, llen; int max_llen = 0; argv++; argc--; if(argc > 0) { p = *argv; if(*p == '-') { while(*++p != '\0') { switch(*p) { case 'a': d = &ascii; break; case 'b': d = &byte; break; case 'c': d = &cchar; break; case 'd': d = &u_s_dec; break; case 'D': d = &u_l_dec; break; case 'e': case 'F': d = &dble; break; case 'f': d = &flt; break; case 'h': case 'x': d = &u_s_hex; break; case 'H': case 'X': d = &u_l_hex; break; case 'i': d = &s_s_dec; break; case 'I': case 'l': case 'L': d = &s_l_dec; break; case 'o': case 'B': d = &u_s_oct; break; case 'O': d = &u_l_oct; break; case 'p': _parity = EVEN; continue; case 'P': _parity = ODD; continue; case 's': d = &string; *(cv++) = d; while (isdigit(p[1])) d->df_size = (10 * d->df_size) + (*++p - '0'); if (d->df_size <= 0) d->df_size = MIN_SLEN; showall = YES; continue; case 'w': dbuf_size = 0; while (isdigit(p[1])) dbuf_size = (10 * dbuf_size) + (*++p - '0'); if (dbuf_size == 0) dbuf_size = BIG_DBUF; continue; case 'v': showall = YES; continue; default: printf("od: bad flag -%c\n", *p); puts(usage); exit(1); } *(cv++) = d; } argc--; argv++; } } /* * if nothing spec'd, setup default conversion. */ if(cv == conv_vec) *(cv++) = &u_s_oct; *cv = (struct dfmt *)0; /* * calculate display parameters */ for (cv = conv_vec; d = *cv; cv++) { nelm = (dbuf_size + d->df_size - 1) / d->df_size; llen = nelm * (d->df_field + 1); if (llen > max_llen) max_llen = llen; } /* * setup df_fmt to point to uniform output fields. */ for (cv = conv_vec; d = *cv; cv++) { if (d->df_field) /* only if external field is known */ { nelm = (dbuf_size + d->df_size - 1) / d->df_size; field = max_llen / nelm; d->df_fmt = fmt + 12 - (field - d->df_field); } } /* * input file specified ? */ if(argc > 0 && **argv != '+') { if (freopen(*argv, "r", stdin) == NULL) { perror(*argv); exit(1); } argv++; argc--; } /* * check for possible offset [label] */ if (argc > 0) { addr = get_addr(*argv); offset(addr); argv++; argc--; if (argc > 0) label = get_addr(*argv); } /* * main dump loop */ same = -1; while ((n = fread(dbuf, 1, dbuf_size, stdin)) > 0) { if (same>=0 && bcmp(dbuf, lastdbuf, dbuf_size) == 0 && !showall) { if (same==0) { printf("*\n"); same = 1; } } else { line(n); same = 0; p = dbuf; l = lastdbuf; for (nelm = 0; nelm < dbuf_size; nelm++) { *l++ = *p; *p++ = '\0'; } } addr += n; if (label >= 0) label += n; } /* * Some conversions require "flushing". */ n = 0; for (cv = conv_vec; *cv; cv++) { if ((*cv)->df_paddr) { if (n++ == 0) put_addr(addr, label, '\n'); } else (*((*cv)->df_put))(0, *cv); }}put_addr(a, l, c)long a;long l;char c;{ fputs(icvt(a, addr_base, UNSIGNED, 7), stdout); if (l >= 0) printf(" (%s)", icvt(l, addr_base, UNSIGNED, 7)); putchar(c);}line(n)int n;{ register i, first; register struct dfmt *c; register struct dfmt **cv = conv_vec; first = YES; while (c = *cv++) { if (c->df_paddr) { if (first) { put_addr(addr, label, ' '); first = NO; } else { putchar('\t'); if (label >= 0) fputs("\t ", stdout); } } i = 0; while (i < n) i += (*(c->df_put))(dbuf+i, c); if (c->df_paddr) putchar('\n'); }}s_put(n, d)short *n;struct dfmt *d;{ printf(d->df_fmt, icvt((long)*n, d->df_radix, d->df_signed, d->df_field)); return(d->df_size);}us_put(n, d)unsigned short *n;struct dfmt *d;{ printf(d->df_fmt, icvt((long)*n, d->df_radix, d->df_signed, d->df_field)); return(d->df_size);}l_put(n, d)long *n;struct dfmt *d;{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -