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

Annotation of src/usr.bin/hexdump/hexsyntax.c, Revision 1.13

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