fatbase

portable OpenBSD tools
git clone git@git.2f30.org/fatbase.git
Log | Files | Refs

hexsyntax.c (3634B)


      1 /*	$OpenBSD: hexsyntax.c,v 1.12 2011/05/06 15:46:29 otto Exp $	*/
      2 /*	$NetBSD: hexsyntax.c,v 1.8 1998/04/08 23:48:57 jeremy Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 1990, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      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.
     16  * 3. Neither the name of the University nor the names of its contributors
     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>
     34 
     35 #include <err.h>
     36 #include <errno.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <unistd.h>
     41 
     42 #include "hexdump.h"
     43 
     44 off_t skip;				/* bytes to skip */
     45 
     46 void
     47 newsyntax(int argc, char ***argvp)
     48 {
     49 	int ch;
     50 	char *p, **argv;
     51 
     52 	argv = *argvp;
     53 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
     54 		switch (ch) {
     55 		case 'b':
     56 			add("\"%07.7_Ax\n\"");
     57 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
     58 			break;
     59 		case 'c':
     60 			add("\"%07.7_Ax\n\"");
     61 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
     62 			break;
     63 		case 'C':
     64 			add("\"%08.8_Ax\n\"");
     65 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
     66 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
     67 			break;
     68 		case 'd':
     69 			add("\"%07.7_Ax\n\"");
     70 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
     71 			break;
     72 		case 'e':
     73 			add(optarg);
     74 			break;
     75 		case 'f':
     76 			addfile(optarg);
     77 			break;
     78 		case 'n':
     79 			errno = 0;
     80 			if ((length = strtol(optarg, NULL, 0)) < 0 ||
     81 			    errno != 0)
     82 				errx(1, "%s: bad length value", optarg);
     83 			break;
     84 		case 'o':
     85 			add("\"%07.7_Ax\n\"");
     86 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
     87 			break;
     88 		case 's':
     89 			errno = 0;
     90 			if ((skip = (off_t)strtoll(optarg, &p, 0)) < 0 ||
     91 			    errno != 0)
     92 				errx(1, "%s: bad skip value", optarg);
     93 			switch(*p) {
     94 			case 'b':
     95 				skip *= 512;
     96 				break;
     97 			case 'k':
     98 				skip *= 1024;
     99 				break;
    100 			case 'm':
    101 				skip *= 1048576;
    102 				break;
    103 			}
    104 			break;
    105 		case 'v':
    106 			vflag = ALL;
    107 			break;
    108 		case 'x':
    109 			add("\"%07.7_Ax\n\"");
    110 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
    111 			break;
    112 		case '?':
    113 			usage();
    114 		}
    115 
    116 	if (!fshead) {
    117 		add("\"%07.7_Ax\n\"");
    118 		add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
    119 	}
    120 
    121 	*argvp += optind;
    122 }
    123 
    124 void
    125 usage(void)
    126 {
    127 	extern char *__progname;
    128 	fprintf(stderr, "usage: %s [-bCcdovx] [-e format_string] "
    129 			"[-f format_file] [-n length]\n"
    130 			"\t[-s offset] [file ...]\n", __progname);
    131 	exit(1);
    132 }