comparison src/xxd/xxd.c @ 27118:2f854597399f v8.2.4088

patch 8.2.4088: xxd cannot output everything in one line Commit: https://github.com/vim/vim/commit/c0a1d370fa655cea9eaa74f5e605b95825dc9de1 Author: Erik Auerswald <auerswal@unix-ag.uni-kl.de> Date: Fri Jan 14 11:58:48 2022 +0000 patch 8.2.4088: xxd cannot output everything in one line Problem: Xxd cannot output everything in one line. Solution: Make zero columns mean infinite columns. (Erik Auerswald, closes #9524)
author Bram Moolenaar <Bram@vim.org>
date Fri, 14 Jan 2022 13:00:07 +0100
parents b9ede1952107
children 948c947cb1ed
comparison
equal deleted inserted replaced
27117:a11e7c054f98 27118:2f854597399f
52 * 2011 March Better error handling by Florian Zumbiehl. 52 * 2011 March Better error handling by Florian Zumbiehl.
53 * 2011 April Formatting by Bram Moolenaar 53 * 2011 April Formatting by Bram Moolenaar
54 * 08.06.2013 Little-endian hexdump (-e) and offset (-o) by Vadim Vygonets. 54 * 08.06.2013 Little-endian hexdump (-e) and offset (-o) by Vadim Vygonets.
55 * 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen. 55 * 11.01.2019 Add full 64/32 bit range to -o and output by Christer Jensen.
56 * 04.02.2020 Add -d for decimal offsets by Aapo Rantalainen 56 * 04.02.2020 Add -d for decimal offsets by Aapo Rantalainen
57 * 14.01.2022 Disable extra newlines with -c0 -p by Erik Auerswald.
57 * 58 *
58 * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com) 59 * (c) 1990-1998 by Juergen Weigert (jnweiger@gmail.com)
59 * 60 *
60 * I hereby grant permission to distribute and use xxd 61 * I hereby grant permission to distribute and use xxd
61 * under X11-MIT or GPL-2.0 (at the user's choice). 62 * under X11-MIT or GPL-2.0 (at the user's choice).
133 #endif 134 #endif
134 135
135 extern long int strtol(); 136 extern long int strtol();
136 extern long int ftell(); 137 extern long int ftell();
137 138
138 char version[] = "xxd 2021-10-22 by Juergen Weigert et al."; 139 char version[] = "xxd 2022-01-14 by Juergen Weigert et al.";
139 #ifdef WIN32 140 #ifdef WIN32
140 char osver[] = " (Win32)"; 141 char osver[] = " (Win32)";
141 #else 142 #else
142 char osver[] = ""; 143 char osver[] = "";
143 #endif 144 #endif
485 int 486 int
486 main(int argc, char *argv[]) 487 main(int argc, char *argv[])
487 { 488 {
488 FILE *fp, *fpo; 489 FILE *fp, *fpo;
489 int c, e, p = 0, relseek = 1, negseek = 0, revert = 0; 490 int c, e, p = 0, relseek = 1, negseek = 0, revert = 0;
490 int cols = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL; 491 int cols = 0, colsgiven = 0, nonzero = 0, autoskip = 0, hextype = HEX_NORMAL;
491 int capitalize = 0, decimal_offset = 0; 492 int capitalize = 0, decimal_offset = 0;
492 int ebcdic = 0; 493 int ebcdic = 0;
493 int octspergrp = -1; /* number of octets grouped in output */ 494 int octspergrp = -1; /* number of octets grouped in output */
494 int grplen; /* total chars per octet group */ 495 int grplen; /* total chars per octet group */
495 long length = -1, n = 0, seekoff = 0; 496 long length = -1, n = 0, seekoff = 0;
538 else if (!STRNCMP(pp, "-c", 2)) 539 else if (!STRNCMP(pp, "-c", 2))
539 { 540 {
540 if (pp[2] && !STRNCMP("apitalize", pp + 2, 9)) 541 if (pp[2] && !STRNCMP("apitalize", pp + 2, 9))
541 capitalize = 1; 542 capitalize = 1;
542 else if (pp[2] && STRNCMP("ols", pp + 2, 3)) 543 else if (pp[2] && STRNCMP("ols", pp + 2, 3))
543 cols = (int)strtol(pp + 2, NULL, 0); 544 {
545 colsgiven = 1;
546 cols = (int)strtol(pp + 2, NULL, 0);
547 }
544 else 548 else
545 { 549 {
546 if (!argv[2]) 550 if (!argv[2])
547 exit_with_usage(); 551 exit_with_usage();
552 colsgiven = 1;
548 cols = (int)strtol(argv[2], NULL, 0); 553 cols = (int)strtol(argv[2], NULL, 0);
549 argv++; 554 argv++;
550 argc--; 555 argc--;
551 } 556 }
552 } 557 }
643 648
644 argv++; /* advance to next argument */ 649 argv++; /* advance to next argument */
645 argc--; 650 argc--;
646 } 651 }
647 652
648 if (!cols) 653 if (!colsgiven || (!cols && hextype != HEX_POSTSCRIPT))
649 switch (hextype) 654 switch (hextype)
650 { 655 {
651 case HEX_POSTSCRIPT: cols = 30; break; 656 case HEX_POSTSCRIPT: cols = 30; break;
652 case HEX_CINCLUDE: cols = 12; break; 657 case HEX_CINCLUDE: cols = 12; break;
653 case HEX_BITS: cols = 6; break; 658 case HEX_BITS: cols = 6; break;
665 case HEX_POSTSCRIPT: 670 case HEX_POSTSCRIPT:
666 case HEX_CINCLUDE: 671 case HEX_CINCLUDE:
667 default: octspergrp = 0; break; 672 default: octspergrp = 0; break;
668 } 673 }
669 674
670 if (cols < 1 || ((hextype == HEX_NORMAL || hextype == HEX_BITS || hextype == HEX_LITTLEENDIAN) 675 if ((hextype == HEX_POSTSCRIPT && cols < 0) ||
676 (hextype != HEX_POSTSCRIPT && cols < 1) ||
677 ((hextype == HEX_NORMAL || hextype == HEX_BITS || hextype == HEX_LITTLEENDIAN)
671 && (cols > COLS))) 678 && (cols > COLS)))
672 { 679 {
673 fprintf(stderr, "%s: invalid number of columns (max. %d).\n", pname, COLS); 680 fprintf(stderr, "%s: invalid number of columns (max. %d).\n", pname, COLS);
674 exit(1); 681 exit(1);
675 } 682 }
785 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF) 792 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF)
786 { 793 {
787 putc_or_die(hexx[(e >> 4) & 0xf], fpo); 794 putc_or_die(hexx[(e >> 4) & 0xf], fpo);
788 putc_or_die(hexx[e & 0xf], fpo); 795 putc_or_die(hexx[e & 0xf], fpo);
789 n++; 796 n++;
790 if (!--p) 797 if (cols > 0 && !--p)
791 { 798 {
792 putc_or_die('\n', fpo); 799 putc_or_die('\n', fpo);
793 p = cols; 800 p = cols;
794 } 801 }
795 } 802 }
796 if (p < cols) 803 if (cols == 0 || p < cols)
797 putc_or_die('\n', fpo); 804 putc_or_die('\n', fpo);
798 fclose_or_die(fp, fpo); 805 fclose_or_die(fp, fpo);
799 return 0; 806 return 0;
800 } 807 }
801 808