comparison src/xxd/xxd.c @ 26366:d227481dbe8e v8.2.3714

patch 8.2.3714: some unused assignments and ugly code in xxd Commit: https://github.com/vim/vim/commit/7e5503c17a3f142e6b28f344d899c9ab9e75a844 Author: DungSaga <dungsaga@users.noreply.github.com> Date: Wed Dec 1 11:24:52 2021 +0000 patch 8.2.3714: some unused assignments and ugly code in xxd Problem: Some unused assignments and ugly code in xxd. Solution: Leave out assignments. Use marcro for fprintf(). (closes https://github.com/vim/vim/issues/9246)
author Bram Moolenaar <Bram@vim.org>
date Wed, 01 Dec 2021 12:30:04 +0100
parents bb58c4530851
children b9ede1952107
comparison
equal deleted inserted replaced
26365:9722ad38bd53 26366:d227481dbe8e
273 { 273 {
274 if (fputs(s, fpo) == EOF) 274 if (fputs(s, fpo) == EOF)
275 perror_exit(3); 275 perror_exit(3);
276 } 276 }
277 277
278 static void 278 /* Use a macro to allow for different arguments. */
279 fprintf_or_die(FILE *fpo, char *format, char *s, int d) 279 #define FPRINTF_OR_DIE(args) if (fprintf args < 0) perror_exit(3)
280 {
281 if (fprintf(fpo, format, s, d) < 0)
282 perror_exit(3);
283 }
284 280
285 static void 281 static void
286 fclose_or_die(FILE *fpi, FILE *fpo) 282 fclose_or_die(FILE *fpi, FILE *fpo)
287 { 283 {
288 if (fclose(fpo) != 0) 284 if (fclose(fpo) != 0)
375 #ifdef TRY_SEEK 371 #ifdef TRY_SEEK
376 if (fseek(fpo, base_off + want_off - have_off, SEEK_CUR) >= 0) 372 if (fseek(fpo, base_off + want_off - have_off, SEEK_CUR) >= 0)
377 have_off = base_off + want_off; 373 have_off = base_off + want_off;
378 #endif 374 #endif
379 if (base_off + want_off < have_off) 375 if (base_off + want_off < have_off)
380 error_exit(5, "sorry, cannot seek backwards."); 376 error_exit(5, "Sorry, cannot seek backwards.");
381 for (; have_off < base_off + want_off; have_off++) 377 for (; have_off < base_off + want_off; have_off++)
382 putc_or_die(0, fpo); 378 putc_or_die(0, fpo);
383 } 379 }
384 380
385 if (n2 >= 0 && n1 >= 0) 381 if (n2 >= 0 && n1 >= 0)
712 } 708 }
713 709
714 if (revert) 710 if (revert)
715 { 711 {
716 if (hextype && (hextype != HEX_POSTSCRIPT)) 712 if (hextype && (hextype != HEX_POSTSCRIPT))
717 error_exit(-1, "sorry, cannot revert this type of hexdump"); 713 error_exit(-1, "Sorry, cannot revert this type of hexdump");
718 return huntype(fp, fpo, cols, hextype, 714 return huntype(fp, fpo, cols, hextype,
719 negseek ? -seekoff : seekoff); 715 negseek ? -seekoff : seekoff);
720 } 716 }
721 717
722 if (seekoff || negseek || !relseek) 718 if (seekoff || negseek || !relseek)
726 e = fseek(fp, negseek ? -seekoff : seekoff, SEEK_CUR); 722 e = fseek(fp, negseek ? -seekoff : seekoff, SEEK_CUR);
727 else 723 else
728 e = fseek(fp, negseek ? -seekoff : seekoff, 724 e = fseek(fp, negseek ? -seekoff : seekoff,
729 negseek ? SEEK_END : SEEK_SET); 725 negseek ? SEEK_END : SEEK_SET);
730 if (e < 0 && negseek) 726 if (e < 0 && negseek)
731 error_exit(4, "sorry cannot seek."); 727 error_exit(4, "Sorry, cannot seek.");
732 if (e >= 0) 728 if (e >= 0)
733 seekoff = ftell(fp); 729 seekoff = ftell(fp);
734 else 730 else
735 #endif 731 #endif
736 { 732 {
737 long s = seekoff; 733 long s = seekoff;
738 734
739 while (s--) 735 while (s--)
740 if ((c = getc_or_die(fp)) == EOF) 736 if (getc_or_die(fp) == EOF)
741 { 737 {
742 error_exit(4, "sorry cannot seek."); 738 error_exit(4, "Sorry, cannot seek.");
743 } 739 }
744 } 740 }
745 } 741 }
746 742
747 if (hextype == HEX_CINCLUDE) 743 if (hextype == HEX_CINCLUDE)
748 { 744 {
749 if (fp != stdin) 745 if (fp != stdin)
750 { 746 {
751 fprintf_or_die(fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : "", 0); 747 FPRINTF_OR_DIE((fpo, "unsigned char %s", isdigit((int)argv[1][0]) ? "__" : ""));
752 for (e = 0; (c = argv[1][e]) != 0; e++) 748 for (e = 0; (c = argv[1][e]) != 0; e++)
753 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); 749 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
754 fputs_or_die("[] = {\n", fpo); 750 fputs_or_die("[] = {\n", fpo);
755 } 751 }
756 752
757 p = 0; 753 p = 0;
758 c = 0; 754 c = 0;
759 while ((length < 0 || p < length) && (c = getc_or_die(fp)) != EOF) 755 while ((length < 0 || p < length) && (c = getc_or_die(fp)) != EOF)
760 { 756 {
761 fprintf_or_die(fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X", 757 FPRINTF_OR_DIE((fpo, (hexx == hexxa) ? "%s0x%02x" : "%s0X%02X",
762 (p % cols) ? ", " : (!p ? " " : ",\n "), c); 758 (p % cols) ? ", " : (!p ? " " : ",\n "), c));
763 p++; 759 p++;
764 } 760 }
765 761
766 if (p) 762 if (p)
767 fputs_or_die("\n", fpo); 763 fputs_or_die("\n", fpo);
768 764
769 if (fp != stdin) 765 if (fp != stdin)
770 { 766 {
771 fputs_or_die("};\n", fpo); 767 fputs_or_die("};\n", fpo);
772 fprintf_or_die(fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : "", 0); 768 FPRINTF_OR_DIE((fpo, "unsigned int %s", isdigit((int)argv[1][0]) ? "__" : ""));
773 for (e = 0; (c = argv[1][e]) != 0; e++) 769 for (e = 0; (c = argv[1][e]) != 0; e++)
774 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo); 770 putc_or_die(isalnum(c) ? CONDITIONAL_CAPITALIZE(c) : '_', fpo);
775 fprintf_or_die(fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p); 771 FPRINTF_OR_DIE((fpo, "_%s = %d;\n", capitalize ? "LEN" : "len", p));
776 } 772 }
777 773
778 fclose_or_die(fp, fpo); 774 fclose_or_die(fp, fpo);
779 return 0; 775 return 0;
780 } 776 }
781 777
782 if (hextype == HEX_POSTSCRIPT) 778 if (hextype == HEX_POSTSCRIPT)
783 { 779 {
784 p = cols; 780 p = cols;
785 e = 0;
786 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF) 781 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF)
787 { 782 {
788 putc_or_die(hexx[(e >> 4) & 0xf], fpo); 783 putc_or_die(hexx[(e >> 4) & 0xf], fpo);
789 putc_or_die(hexx[e & 0xf], fpo); 784 putc_or_die(hexx[e & 0xf], fpo);
790 n++; 785 n++;
805 if (hextype != HEX_BITS) 800 if (hextype != HEX_BITS)
806 grplen = octspergrp + octspergrp + 1; /* chars per octet group */ 801 grplen = octspergrp + octspergrp + 1; /* chars per octet group */
807 else /* hextype == HEX_BITS */ 802 else /* hextype == HEX_BITS */
808 grplen = 8 * octspergrp + 1; 803 grplen = 8 * octspergrp + 1;
809 804
810 e = 0;
811 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF) 805 while ((length < 0 || n < length) && (e = getc_or_die(fp)) != EOF)
812 { 806 {
813 int x; 807 int x;
814 808
815 if (p == 0) 809 if (p == 0)