Annotation of src/usr.bin/hexdump/conv.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: conv.c,v 1.4 2001/12/30 08:17:32 pvalchev Exp $ */
1.4 pvalchev 2: /* $NetBSD: conv.c,v 1.7 2001/12/07 15:14:29 bjh21 Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*
1.4 pvalchev 5: * Copyright (c) 1989, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: /*static char sccsid[] = "from: @(#)conv.c 5.4 (Berkeley) 6/1/90";*/
1.5 ! deraadt 39: static char rcsid[] = "$OpenBSD: conv.c,v 1.4 2001/12/30 08:17:32 pvalchev Exp $";
1.1 deraadt 40: #endif /* not lint */
41:
42: #include <sys/types.h>
1.4 pvalchev 43:
44: #include <stdio.h>
1.1 deraadt 45: #include <ctype.h>
1.4 pvalchev 46:
1.1 deraadt 47: #include "hexdump.h"
48:
1.3 deraadt 49: void
1.1 deraadt 50: conv_c(pr, p)
51: PR *pr;
52: u_char *p;
53: {
1.4 pvalchev 54: char buf[10];
55: char const *str;
1.1 deraadt 56:
57: switch(*p) {
58: case '\0':
59: str = "\\0";
60: goto strpr;
61: /* case '\a': */
62: case '\007':
63: if (deprecated) /* od didn't know about \a */
64: break;
65: str = "\\a";
66: goto strpr;
67: case '\b':
68: str = "\\b";
69: goto strpr;
70: case '\f':
71: str = "\\f";
72: goto strpr;
73: case '\n':
74: str = "\\n";
75: goto strpr;
76: case '\r':
77: str = "\\r";
78: goto strpr;
79: case '\t':
80: str = "\\t";
81: goto strpr;
82: case '\v':
83: if (deprecated)
84: break;
85: str = "\\v";
86: goto strpr;
87: default:
88: break;
89: }
90: if (isprint(*p)) {
91: *pr->cchar = 'c';
92: (void)printf(pr->fmt, *p);
93: } else {
1.5 ! deraadt 94: (void)snprintf(buf, sizeof buf, "%03o", (int)*p);
1.4 pvalchev 95: str = buf;
1.1 deraadt 96: strpr: *pr->cchar = 's';
97: (void)printf(pr->fmt, str);
98: }
99: }
100:
1.3 deraadt 101: void
1.1 deraadt 102: conv_u(pr, p)
103: PR *pr;
104: u_char *p;
105: {
1.4 pvalchev 106: static const char *list[] = {
1.1 deraadt 107: "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
108: "bs", "ht", "lf", "vt", "ff", "cr", "so", "si",
109: "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
110: "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
111: };
112:
113: /* od used nl, not lf */
114: if (*p <= 0x1f) {
115: *pr->cchar = 's';
116: if (deprecated && *p == 0x0a)
117: (void)printf(pr->fmt, "nl");
118: else
119: (void)printf(pr->fmt, list[*p]);
120: } else if (*p == 0x7f) {
121: *pr->cchar = 's';
122: (void)printf(pr->fmt, "del");
1.4 pvalchev 123: } else if (deprecated && *p == 0x20) { /* od replaced space with sp */
1.1 deraadt 124: *pr->cchar = 's';
125: (void)printf(pr->fmt, " sp");
126: } else if (isprint(*p)) {
127: *pr->cchar = 'c';
128: (void)printf(pr->fmt, *p);
129: } else {
130: *pr->cchar = 'x';
131: (void)printf(pr->fmt, (int)*p);
132: }
133: }