]>
git.saurik.com Git - apple/shell_cmds.git/blob - hexdump/conv.c
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char sccsid
[] = "@(#)conv.c 8.1 (Berkeley) 6/6/93";
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
49 conv_c(PR
*pr
, u_char
*p
, size_t bufsize
)
55 int converr
, pad
, width
;
56 u_char peekbuf
[MB_LEN_MAX
];
94 * Multibyte characters are disabled for hexdump(1) for backwards
95 * compatibility and consistency (none of its other output formats
96 * recognize them correctly).
99 if (odmode
&& MB_CUR_MAX
> 1) {
102 clen
= mbrtowc(&wc
, (const char *)p
, bufsize
, &pr
->mbstate
);
105 else if (clen
== (size_t)-1 || (clen
== (size_t)-2 &&
107 memset(&pr
->mbstate
, 0, sizeof(pr
->mbstate
));
111 } else if (clen
== (size_t)-2) {
113 * Incomplete character; peek ahead and see if we
117 bufsize
= peek(p
= peekbuf
, MB_CUR_MAX
);
125 if (!converr
&& iswprint(wc
)) {
128 (void)printf(pr
->fmt
, (int)wc
);
131 assert(strcmp(pr
->fmt
, "%3C") == 0);
137 (void)printf("%*s%C", pad
, "", wc
);
138 pr
->mbleft
= clen
- 1;
141 (void)sprintf(buf
, "%03o", (int)*p
);
143 strpr
: *pr
->cchar
= 's';
144 (void)printf(pr
->fmt
, str
);
149 conv_u(PR
*pr
, u_char
*p
)
151 static char const * list
[] = {
152 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
153 "bs", "ht", "lf", "vt", "ff", "cr", "so", "si",
154 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
155 "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
158 /* od used nl, not lf */
161 if (odmode
&& *p
== 0x0a)
162 (void)printf(pr
->fmt
, "nl");
164 (void)printf(pr
->fmt
, list
[*p
]);
165 } else if (*p
== 0x7f) {
167 (void)printf(pr
->fmt
, "del");
168 } else if (odmode
&& *p
== 0x20) { /* od replaced space with sp */
170 (void)printf(pr
->fmt
, " sp");
171 } else if (isprint(*p
)) {
173 (void)printf(pr
->fmt
, *p
);
176 (void)printf(pr
->fmt
, (int)*p
);