[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.6

1.6     ! millert     1: /*     $OpenBSD: conv.c,v 1.5 2003/03/13 09:09:31 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: #ifndef lint
                     34: /*static char sccsid[] = "from: @(#)conv.c     5.4 (Berkeley) 6/1/90";*/
1.6     ! millert    35: static char rcsid[] = "$OpenBSD: conv.c,v 1.5 2003/03/13 09:09:31 deraadt Exp $";
1.1       deraadt    36: #endif /* not lint */
                     37:
                     38: #include <sys/types.h>
1.4       pvalchev   39:
                     40: #include <stdio.h>
1.1       deraadt    41: #include <ctype.h>
1.4       pvalchev   42:
1.1       deraadt    43: #include "hexdump.h"
                     44:
1.3       deraadt    45: void
1.1       deraadt    46: conv_c(pr, p)
                     47:        PR *pr;
                     48:        u_char *p;
                     49: {
1.4       pvalchev   50:        char buf[10];
                     51:        char const *str;
1.1       deraadt    52:
                     53:        switch(*p) {
                     54:        case '\0':
                     55:                str = "\\0";
                     56:                goto strpr;
                     57:        /* case '\a': */
                     58:        case '\007':
                     59:                if (deprecated)         /* od didn't know about \a */
                     60:                        break;
                     61:                str = "\\a";
                     62:                goto strpr;
                     63:        case '\b':
                     64:                str = "\\b";
                     65:                goto strpr;
                     66:        case '\f':
                     67:                str = "\\f";
                     68:                goto strpr;
                     69:        case '\n':
                     70:                str = "\\n";
                     71:                goto strpr;
                     72:        case '\r':
                     73:                str = "\\r";
                     74:                goto strpr;
                     75:        case '\t':
                     76:                str = "\\t";
                     77:                goto strpr;
                     78:        case '\v':
                     79:                if (deprecated)
                     80:                        break;
                     81:                str = "\\v";
                     82:                goto strpr;
                     83:        default:
                     84:                break;
                     85:        }
                     86:        if (isprint(*p)) {
                     87:                *pr->cchar = 'c';
                     88:                (void)printf(pr->fmt, *p);
                     89:        } else {
1.5       deraadt    90:                (void)snprintf(buf, sizeof buf, "%03o", (int)*p);
1.4       pvalchev   91:                str = buf;
1.1       deraadt    92: strpr:         *pr->cchar = 's';
                     93:                (void)printf(pr->fmt, str);
                     94:        }
                     95: }
                     96:
1.3       deraadt    97: void
1.1       deraadt    98: conv_u(pr, p)
                     99:        PR *pr;
                    100:        u_char *p;
                    101: {
1.4       pvalchev  102:        static const char *list[] = {
1.1       deraadt   103:                "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
                    104:                 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
                    105:                "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
                    106:                "can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
                    107:        };
                    108:
                    109:                                                /* od used nl, not lf */
                    110:        if (*p <= 0x1f) {
                    111:                *pr->cchar = 's';
                    112:                if (deprecated && *p == 0x0a)
                    113:                        (void)printf(pr->fmt, "nl");
                    114:                else
                    115:                        (void)printf(pr->fmt, list[*p]);
                    116:        } else if (*p == 0x7f) {
                    117:                *pr->cchar = 's';
                    118:                (void)printf(pr->fmt, "del");
1.4       pvalchev  119:        } else if (deprecated && *p == 0x20) {  /* od replaced space with sp */
1.1       deraadt   120:                *pr->cchar = 's';
                    121:                (void)printf(pr->fmt, " sp");
                    122:        } else if (isprint(*p)) {
                    123:                *pr->cchar = 'c';
                    124:                (void)printf(pr->fmt, *p);
                    125:        } else {
                    126:                *pr->cchar = 'x';
                    127:                (void)printf(pr->fmt, (int)*p);
                    128:        }
                    129: }