123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "libbb.h"
- #include "dump.h"
- int xxd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int xxd_main(int argc UNUSED_PARAM, char **argv)
- {
- char buf[80];
- dumper_t *dumper;
- char *opt_l, *opt_s;
- unsigned bytes = 2;
- unsigned cols = 0;
- unsigned opt;
- dumper = alloc_dumper();
- #define OPT_l (1 << 0)
- #define OPT_s (1 << 1)
- #define OPT_a (1 << 2)
- #define OPT_p (1 << 3)
- opt = getopt32(argv, "^" "l:s:apg:+c:+" "\0" "?1" ,
- &opt_l, &opt_s, &bytes, &cols
- );
- argv += optind;
- dumper->dump_vflag = ALL;
- if (opt & OPT_l) {
- dumper->dump_length = xstrtou_range(
- opt_l,
- 0,
- 0, INT_MAX
- );
- }
- if (opt & OPT_s) {
- dumper->dump_skip = xstrtoull_range(
- opt_s,
- 0,
- 0, OFF_T_MAX
- );
-
- }
- if (opt & OPT_p) {
- if (cols == 0)
- cols = 30;
- bytes = cols;
- } else {
- if (cols == 0)
- cols = 16;
- bb_dump_add(dumper, "\"%08.8_ax: \"");
- }
- if (bytes < 1 || bytes >= cols) {
- sprintf(buf, "%u/1 \"%%02x\"", cols);
- bb_dump_add(dumper, buf);
- }
- else if (bytes == 1) {
- sprintf(buf, "%u/1 \"%%02x \"", cols);
- bb_dump_add(dumper, buf);
- }
- else {
- #define BS "/1 \"%02x \""
- #define B "/1 \"%02x\""
- unsigned i;
- char *bigbuf = xmalloc(cols * (sizeof(BS)-1));
- char *p = bigbuf;
- for (i = 1; i <= cols; i++) {
- if (i == cols || i % bytes)
- p = stpcpy(p, B);
- else
- p = stpcpy(p, BS);
- }
-
-
-
-
- bb_dump_add(dumper, bigbuf);
- free(bigbuf);
- }
- if (!(opt & OPT_p)) {
- sprintf(buf, "\" \"%u/1 \"%%_p\"\"\n\"", cols);
- bb_dump_add(dumper, buf);
- } else {
- bb_dump_add(dumper, "\"\n\"");
- }
- return bb_dump_dump(dumper, argv);
- }
|