11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1986, 1988, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * (c) UNIX System Laboratories, Inc. 51541Srgrimes * All or some portions of this file are derived from material licensed 61541Srgrimes * to the University of California by American Telephone and Telegraph 71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81541Srgrimes * the permission of UNIX System Laboratories, Inc. 91541Srgrimes * 101541Srgrimes * Redistribution and use in source and binary forms, with or without 111541Srgrimes * modification, are permitted provided that the following conditions 121541Srgrimes * are met: 131541Srgrimes * 1. Redistributions of source code must retain the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer. 151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161541Srgrimes * notice, this list of conditions and the following disclaimer in the 171541Srgrimes * documentation and/or other materials provided with the distribution. 181541Srgrimes * 4. Neither the name of the University nor the names of its contributors 191541Srgrimes * may be used to endorse or promote products derived from this software 201541Srgrimes * without specific prior written permission. 211541Srgrimes * 221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321541Srgrimes * SUCH DAMAGE. 331541Srgrimes * 341541Srgrimes * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 351541Srgrimes */ 361541Srgrimes 37116182Sobrien#include <sys/cdefs.h> 38116182Sobrien__FBSDID("$FreeBSD$"); 39116182Sobrien 40180161Sjhb#include <sys/types.h> 41180161Sjhb#include <libutil.h> 42180161Sjhb#include <stdio.h> 43108678Sphk 441541Srgrimesvoid 45144706Sphkhexdump(const void *ptr, int length, const char *hdr, int flags) 46123215Sscottl{ 47123215Sscottl int i, j, k; 48123215Sscottl int cols; 49144706Sphk const unsigned char *cp; 50123215Sscottl char delim; 51123215Sscottl 52123215Sscottl if ((flags & HD_DELIM_MASK) != 0) 53123215Sscottl delim = (flags & HD_DELIM_MASK) >> 8; 54123215Sscottl else 55123215Sscottl delim = ' '; 56123215Sscottl 57123215Sscottl if ((flags & HD_COLUMN_MASK) != 0) 58123215Sscottl cols = flags & HD_COLUMN_MASK; 59123215Sscottl else 60123215Sscottl cols = 16; 61123215Sscottl 62123215Sscottl cp = ptr; 63123215Sscottl for (i = 0; i < length; i+= cols) { 64123215Sscottl if (hdr != NULL) 65123215Sscottl printf("%s", hdr); 66123215Sscottl 67123215Sscottl if ((flags & HD_OMIT_COUNT) == 0) 68123215Sscottl printf("%04x ", i); 69123215Sscottl 70123215Sscottl if ((flags & HD_OMIT_HEX) == 0) { 71123215Sscottl for (j = 0; j < cols; j++) { 72123215Sscottl k = i + j; 73123215Sscottl if (k < length) 74123215Sscottl printf("%c%02x", delim, cp[k]); 75123215Sscottl else 76123215Sscottl printf(" "); 77123215Sscottl } 78123215Sscottl } 79123215Sscottl 80123215Sscottl if ((flags & HD_OMIT_CHARS) == 0) { 81123215Sscottl printf(" |"); 82123215Sscottl for (j = 0; j < cols; j++) { 83123215Sscottl k = i + j; 84123215Sscottl if (k >= length) 85123215Sscottl printf(" "); 86123215Sscottl else if (cp[k] >= ' ' && cp[k] <= '~') 87123215Sscottl printf("%c", cp[k]); 88123215Sscottl else 89123215Sscottl printf("."); 90123215Sscottl } 91156001Sscottl printf("|"); 92123215Sscottl } 93156001Sscottl printf("\n"); 94123215Sscottl } 95123215Sscottl} 96123215Sscottl 97