home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, John F. Haugh II
- * An unpublished work.
- * All rights reserved.
- *
- * Permission is granted to copy and create derivative works for any
- * non-commercial purpose, provided this copyright notice is preserved
- * in all copies of source code, or included in human readable form
- * and conspicuously displayed on all copies of object code or
- * distribution media.
- */
-
- #ifndef lint
- static char sccsid[] = "@(#)od.c 2.1 07:15:25 2/27/91";
- #endif
-
- #define SKIP_SPACE(cp) while (*cp && isspace (*cp)) *cp++
-
- #include <sys/types.h>
- #include <ctype.h>
- #include "crash.h"
-
- od (string)
- char *string;
- {
- long base;
- long length = 0;
- char *cp;
- char *fmt = 0;
- int i, j;
- unsigned char c;
- unsigned short s;
- long l;
-
- for (cp = string;*cp && *cp != ',';cp++)
- ;
-
- if (*cp == ',')
- *cp++ = '\0';
-
- if (expr (&string, &base))
- return -1;
- else
- string = cp;
-
- if (*string) {
- SKIP_SPACE(string);
-
- for (cp = string;*cp && *cp != ',';cp++)
- ;
-
- if (*cp == ',')
- *cp++ = '\0';
-
- if (expr (&string, &length))
- return -1;
- else
- string = cp;
-
- SKIP_SPACE(string);
- if (*string)
- fmt = string;
- else
- fmt = "x";
- } else {
- length = 16;
- fmt = "x";
- }
- if (strlen (fmt) > 1) {
- while (length > 0) {
- printf ("%0.8x", base);
-
- for (cp = fmt;length > 0 && *cp;) {
- if (l_lseek (memfd, base, 0) == -1)
- return;
-
- switch (*cp) {
- case 'a':
- printf ("\n%0.8x", base);
- break;
- case 'n':
- printf ("\n ");
- break;
- case 'c':
- case 'b':
- if (r_read (memfd, &c, 1) == -1)
- return;
- base += 1;
- length -= 1;
- if (*fmt == 'b' ||
- (c <= ' ' || c > '~'))
- printf (" %0.2x", c);
- else
- printf (" '%c", c);
- break;
- case 'd':
- case 'o':
- case 'x':
- if (r_read (memfd, &s, 2) == -1)
- return;
- base += 2;
- length -= 2;
- printf (*cp == 'd' ? " %5u":
- (*cp == 'o' ? " %6o":
- " %0.4x"), s);
- break;
- case 'D':
- case 'O':
- case 'X':
- if (r_read (memfd, &l, 4) == -1)
- return;
- base += 4;
- length -= 4;
- printf (*cp == 'D' ? " %9lu":
- (*cp == 'O' ? " %11lo":
- " %0.8lx"), l);
- break;
- case 'Y':
- if (r_read (memfd, &l, 4) == -1)
- return;
- base += 4;
- printf (" %.24s", ctime (&l));
- break;
- }
- cp++;
- }
- putchar ('\n');
- }
- } else {
- while (length > 0) {
- printf ("%0.8x", base);
-
- for (i = 0;i < 16 && length > 0;) {
- if (l_lseek (memfd, base, 0) == -1)
- return;
-
- switch (*fmt) {
- case 'c':
- case 'b':
- if (r_read (memfd, &c, 1) == -1)
- return;
- base += 1;
- i += 1;
- length -= 1;
- if (*fmt == 'b' ||
- (c <= ' ' || c > '~'))
- printf (" %0.2x", c);
- else
- printf (" '%c", c);
- break;
- case 'd':
- case 'o':
- case 'x':
- if (r_read (memfd, &s, 2) == -1)
- return;
- base += 2;
- i += 2;
- length -= 2;
- printf (*fmt == 'd' ? " %5u":
- (*fmt == 'o' ? " %6o":
- " %0.4x"), s);
- break;
- case 'D':
- case 'O':
- case 'X':
- if (r_read (memfd, &l, 4) == -1)
- return;
- base += 4;
- i += 4;
- length -= 4;
- printf (*fmt == 'D' ? " %9lu":
- (*fmt == 'O' ? " %11lo":
- " %0.8lx"), l);
- break;
- case 'Y':
- if (r_read (memfd, &l, 4) == -1)
- return;
- base += 4;
- i += 16;
- length -= 4;
- printf (" %.24s", ctime (&l));
- break;
- }
- }
- putchar ('\n');
- }
- if (i != 16)
- printf ("%0.8x", base);
- }
- }
-