1 /* $NetBSD: display.c,v 1.12 2001/12/07 15:14:29 bjh21 Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)display.c 8.1 (Berkeley) 6/6/93"; 40 #else 41 __RCSID("$NetBSD: display.c,v 1.12 2001/12/07 15:14:29 bjh21 Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/stat.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "hexdump.h" 57 58 enum _vflag vflag = FIRST; 59 60 static off_t address; /* address/offset in stream */ 61 static off_t eaddress; /* end address */ 62 63 static inline void print __P((PR *, u_char *)); 64 65 void 66 display() 67 { 68 FS *fs; 69 FU *fu; 70 PR *pr; 71 int cnt; 72 u_char *bp; 73 off_t saveaddress; 74 u_char savech, *savebp; 75 76 savech = 0; 77 while ((bp = get()) != NULL) 78 for (fs = fshead, savebp = bp, saveaddress = address; fs; 79 fs = fs->nextfs, bp = savebp, address = saveaddress) 80 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 81 if (fu->flags&F_IGNORE) 82 break; 83 for (cnt = fu->reps; cnt; --cnt) 84 for (pr = fu->nextpr; pr; address += pr->bcnt, 85 bp += pr->bcnt, pr = pr->nextpr) { 86 if (eaddress && address >= eaddress && 87 !(pr->flags & (F_TEXT|F_BPAD))) 88 bpad(pr); 89 if (cnt == 1 && pr->nospace) { 90 savech = *pr->nospace; 91 *pr->nospace = '\0'; 92 } 93 print(pr, bp); 94 if (cnt == 1 && pr->nospace) 95 *pr->nospace = savech; 96 } 97 } 98 if (endfu) { 99 /* 100 * If eaddress not set, error or file size was multiple of 101 * blocksize, and no partial block ever found. 102 */ 103 if (!eaddress) { 104 if (!address) 105 return; 106 eaddress = address; 107 } 108 for (pr = endfu->nextpr; pr; pr = pr->nextpr) 109 switch(pr->flags) { 110 case F_ADDRESS: 111 (void)printf(pr->fmt, (quad_t)eaddress); 112 break; 113 case F_TEXT: 114 (void)printf("%s", pr->fmt); 115 break; 116 } 117 } 118 } 119 120 static inline void 121 print(pr, bp) 122 PR *pr; 123 u_char *bp; 124 { 125 double f8; 126 float f4; 127 int16_t s2; 128 int32_t s4; 129 int64_t s8; 130 u_int16_t u2; 131 u_int32_t u4; 132 u_int64_t u8; 133 134 switch(pr->flags) { 135 case F_ADDRESS: 136 (void)printf(pr->fmt, (quad_t)address); 137 break; 138 case F_BPAD: 139 (void)printf(pr->fmt, ""); 140 break; 141 case F_C: 142 conv_c(pr, bp); 143 break; 144 case F_CHAR: 145 (void)printf(pr->fmt, *bp); 146 break; 147 case F_DBL: 148 switch(pr->bcnt) { 149 case 4: 150 memmove(&f4, bp, sizeof(f4)); 151 (void)printf(pr->fmt, f4); 152 break; 153 case 8: 154 memmove(&f8, bp, sizeof(f8)); 155 (void)printf(pr->fmt, f8); 156 break; 157 } 158 break; 159 case F_INT: 160 switch(pr->bcnt) { 161 case 1: 162 (void)printf(pr->fmt, (quad_t)*bp); 163 break; 164 case 2: 165 memmove(&s2, bp, sizeof(s2)); 166 (void)printf(pr->fmt, (quad_t)s2); 167 break; 168 case 4: 169 memmove(&s4, bp, sizeof(s4)); 170 (void)printf(pr->fmt, (quad_t)s4); 171 break; 172 case 8: 173 memmove(&s8, bp, sizeof(s8)); 174 (void)printf(pr->fmt, s8); 175 break; 176 } 177 break; 178 case F_P: 179 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.'); 180 break; 181 case F_STR: 182 (void)printf(pr->fmt, (char *)bp); 183 break; 184 case F_TEXT: 185 (void)printf("%s", pr->fmt); 186 break; 187 case F_U: 188 conv_u(pr, bp); 189 break; 190 case F_UINT: 191 switch(pr->bcnt) { 192 case 1: 193 (void)printf(pr->fmt, (u_quad_t)*bp); 194 break; 195 case 2: 196 memmove(&u2, bp, sizeof(u2)); 197 (void)printf(pr->fmt, (u_quad_t)u2); 198 break; 199 case 4: 200 memmove(&u4, bp, sizeof(u4)); 201 (void)printf(pr->fmt, (u_quad_t)u4); 202 break; 203 case 8: 204 memmove(&u8, bp, sizeof(u8)); 205 (void)printf(pr->fmt, u8); 206 break; 207 } 208 break; 209 } 210 } 211 212 void 213 bpad(pr) 214 PR *pr; 215 { 216 static const char *spec = " -0+#"; 217 char *p1, *p2; 218 219 /* 220 * Remove all conversion flags; '-' is the only one valid 221 * with %s, and it's not useful here. 222 */ 223 pr->flags = F_BPAD; 224 pr->cchar[0] = 's'; 225 pr->cchar[1] = '\0'; 226 for (p1 = pr->fmt; *p1 != '%'; ++p1); 227 for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1); 228 while ((*p2++ = *p1++) != '\0'); 229 } 230 231 static char **_argv; 232 233 u_char * 234 get() 235 { 236 static int ateof = 1; 237 static u_char *curp, *savp; 238 int n; 239 int need, nread; 240 u_char *tmpp; 241 242 if (!curp) { 243 curp = emalloc(blocksize); 244 savp = emalloc(blocksize); 245 } else { 246 tmpp = curp; 247 curp = savp; 248 savp = tmpp; 249 address += blocksize; 250 } 251 for (need = blocksize, nread = 0;;) { 252 /* 253 * if read the right number of bytes, or at EOF for one file, 254 * and no other files are available, zero-pad the rest of the 255 * block and set the end flag. 256 */ 257 if (!length || (ateof && !next(NULL))) { 258 if (need == blocksize) 259 return(NULL); 260 if (!need && vflag != ALL && 261 !memcmp(curp, savp, nread)) { 262 if (vflag != DUP) 263 (void)printf("*\n"); 264 return(NULL); 265 } 266 memset((char *)curp + nread, 0, need); 267 eaddress = address + nread; 268 return(curp); 269 } 270 n = fread((char *)curp + nread, sizeof(u_char), 271 length == -1 ? need : MIN(length, need), stdin); 272 if (!n) { 273 if (ferror(stdin)) 274 warn("%s", _argv[-1]); 275 ateof = 1; 276 continue; 277 } 278 ateof = 0; 279 if (length != -1) 280 length -= n; 281 if (!(need -= n)) { 282 if (vflag == ALL || vflag == FIRST || 283 memcmp(curp, savp, blocksize)) { 284 if (vflag == DUP || vflag == FIRST) 285 vflag = WAIT; 286 return(curp); 287 } 288 if (vflag == WAIT) 289 (void)printf("*\n"); 290 vflag = DUP; 291 address += blocksize; 292 need = blocksize; 293 nread = 0; 294 } 295 else 296 nread += n; 297 } 298 } 299 300 int 301 next(argv) 302 char **argv; 303 { 304 static int done; 305 int statok; 306 307 if (argv) { 308 _argv = argv; 309 return(1); 310 } 311 for (;;) { 312 if (*_argv) { 313 if (!(freopen(*_argv, "r", stdin))) { 314 warn("%s", *_argv); 315 exitval = 1; 316 ++_argv; 317 continue; 318 } 319 statok = done = 1; 320 } else { 321 if (done++) 322 return(0); 323 statok = 0; 324 } 325 if (skip) 326 doskip(statok ? *_argv : "stdin", statok); 327 if (*_argv) 328 ++_argv; 329 if (!skip) 330 return(1); 331 } 332 /* NOTREACHED */ 333 } 334 335 void 336 doskip(fname, statok) 337 const char *fname; 338 int statok; 339 { 340 int cnt; 341 struct stat sb; 342 343 if (statok) { 344 if (fstat(fileno(stdin), &sb)) 345 err(1, "fstat %s", fname); 346 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) { 347 address += sb.st_size; 348 skip -= sb.st_size; 349 return; 350 } 351 } 352 if (S_ISREG(sb.st_mode)) { 353 if (fseek(stdin, skip, SEEK_SET)) 354 err(1, "fseek %s", fname); 355 address += skip; 356 skip = 0; 357 } else { 358 for (cnt = 0; cnt < skip; ++cnt) 359 if (getchar() == EOF) 360 break; 361 address += cnt; 362 skip -= cnt; 363 } 364 } 365 366 void * 367 emalloc(allocsize) 368 int allocsize; 369 { 370 void *p; 371 372 if ((p = malloc((u_int)allocsize)) == NULL) 373 nomem(); 374 memset(p, 0, allocsize); 375 return(p); 376 } 377 378 void 379 nomem() 380 { 381 err(1, NULL); 382 } 383