[BACK]Return to conv.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / hexdump

Annotation of src/usr.bin/hexdump/conv.c, Revision 1.8

1.8     ! deraadt     1: /*     $OpenBSD: conv.c,v 1.7 2003/06/12 20:58:09 deraadt 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.
1.6       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/types.h>
1.4       pvalchev   34:
                     35: #include <stdio.h>
1.1       deraadt    36: #include <ctype.h>
1.4       pvalchev   37:
1.1       deraadt    38: #include "hexdump.h"
                     39:
1.3       deraadt    40: void
1.7       deraadt    41: conv_c(PR *pr, u_char *p)
1.1       deraadt    42: {
1.4       pvalchev   43:        char buf[10];
                     44:        char const *str;
1.1       deraadt    45:
                     46:        switch(*p) {
                     47:        case '\0':
                     48:                str = "\\0";
                     49:                goto strpr;
                     50:        /* case '\a': */
                     51:        case '\007':
                     52:                if (deprecated)         /* od didn't know about \a */
                     53:                        break;
                     54:                str = "\\a";
                     55:                goto strpr;
                     56:        case '\b':
                     57:                str = "\\b";
                     58:                goto strpr;
                     59:        case '\f':
                     60:                str = "\\f";
                     61:                goto strpr;
                     62:        case '\n':
                     63:                str = "\\n";
                     64:                goto strpr;
                     65:        case '\r':
                     66:                str = "\\r";
                     67:                goto strpr;
                     68:        case '\t':
                     69:                str = "\\t";
                     70:                goto strpr;
                     71:        case '\v':
                     72:                if (deprecated)
                     73:                        break;
                     74:                str = "\\v";
                     75:                goto strpr;
                     76:        default:
                     77:                break;
                     78:        }
                     79:        if (isprint(*p)) {
                     80:                *pr->cchar = 'c';
                     81:                (void)printf(pr->fmt, *p);
                     82:        } else {
1.5       deraadt    83:                (void)snprintf(buf, sizeof buf, "%03o", (int)*p);
1.4       pvalchev   84:                str = buf;
1.1       deraadt    85: strpr:         *pr->cchar = 's';
                     86:                (void)printf(pr->fmt, str);
                     87:        }
                     88: }
                     89:
1.3       deraadt    90: void
1.7       deraadt    91: conv_u(PR *pr, u_char *p)
1.1       deraadt    92: {
1.4       pvalchev   93:        static const char *list[] = {
1.1       deraadt    94:                "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
                     95:                 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
                     96:                "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
                     97:                "can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
                     98:        };
                     99:
                    100:                                                /* od used nl, not lf */
                    101:        if (*p <= 0x1f) {
                    102:                *pr->cchar = 's';
                    103:                if (deprecated && *p == 0x0a)
                    104:                        (void)printf(pr->fmt, "nl");
                    105:                else
                    106:                        (void)printf(pr->fmt, list[*p]);
                    107:        } else if (*p == 0x7f) {
                    108:                *pr->cchar = 's';
                    109:                (void)printf(pr->fmt, "del");
1.4       pvalchev  110:        } else if (deprecated && *p == 0x20) {  /* od replaced space with sp */
1.1       deraadt   111:                *pr->cchar = 's';
                    112:                (void)printf(pr->fmt, " sp");
                    113:        } else if (isprint(*p)) {
                    114:                *pr->cchar = 'c';
                    115:                (void)printf(pr->fmt, *p);
                    116:        } else {
                    117:                *pr->cchar = 'x';
                    118:                (void)printf(pr->fmt, (int)*p);
                    119:        }
                    120: }