/usr.bin/hexdump/hexsyntax.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 140 lines · 101 code · 10 blank · 29 comment · 10 complexity · f983d14072020da5c1820c9d3cd9b6b0 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. #if 0
  31. static char sccsid[] = "@(#)hexsyntax.c 8.2 (Berkeley) 5/4/95";
  32. #endif
  33. #endif /* not lint */
  34. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. #include <sys/types.h>
  37. #include <err.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include "hexdump.h"
  43. off_t skip; /* bytes to skip */
  44. void
  45. newsyntax(int argc, char ***argvp)
  46. {
  47. int ch;
  48. char *p, **argv;
  49. argv = *argvp;
  50. if ((p = strrchr(argv[0], 'h')) != NULL &&
  51. strcmp(p, "hd") == 0) {
  52. /* "Canonical" format, implies -C. */
  53. add("\"%08.8_Ax\n\"");
  54. add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  55. add("\" |\" 16/1 \"%_p\" \"|\\n\"");
  56. }
  57. while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
  58. switch (ch) {
  59. case 'b':
  60. add("\"%07.7_Ax\n\"");
  61. add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
  62. break;
  63. case 'c':
  64. add("\"%07.7_Ax\n\"");
  65. add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
  66. break;
  67. case 'C':
  68. add("\"%08.8_Ax\n\"");
  69. add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  70. add("\" |\" 16/1 \"%_p\" \"|\\n\"");
  71. break;
  72. case 'd':
  73. add("\"%07.7_Ax\n\"");
  74. add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"");
  75. break;
  76. case 'e':
  77. add(optarg);
  78. break;
  79. case 'f':
  80. addfile(optarg);
  81. break;
  82. case 'n':
  83. if ((length = atoi(optarg)) < 0)
  84. errx(1, "%s: bad length value", optarg);
  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':
  91. if ((skip = strtoll(optarg, &p, 0)) < 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. if (!fshead) {
  116. add("\"%07.7_Ax\n\"");
  117. add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  118. }
  119. *argvp += optind;
  120. }
  121. void
  122. usage(void)
  123. {
  124. (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
  125. "usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
  126. " [-s skip] [file ...]",
  127. " hd [-bcdovx] [-e fmt] [-f fmt_file] [-n length]",
  128. " [-s skip] [file ...]");
  129. exit(1);
  130. }