Print this page
10911 dd: add input flag fullblock


  81 #define UNBLOCK         13      /* convert blocked ASCII to ASCII */
  82 #define LCUNBLOCK       14      /* convert blocked ASCII to lower case ASCII */
  83 #define UCUNBLOCK       15      /* convert blocked ASCII to upper case ASCII */
  84 #define ASCII           16      /* convert blocked EBCDIC to ASCII */
  85 #define LCASCII         17      /* convert blocked EBCDIC to lower case ASCII */
  86 #define UCASCII         18      /* convert blocked EBCDIC to upper case ASCII */
  87 #define BLOCK           19      /* convert ASCII to blocked ASCII */
  88 #define LCBLOCK         20      /* convert ASCII to lower case blocked ASCII */
  89 #define UCBLOCK         21      /* convert ASCII to upper case blocked ASCII */
  90 #define EBCDIC          22      /* convert ASCII to blocked EBCDIC */
  91 #define LCEBCDIC        23      /* convert ASCII to lower case blocked EBCDIC */
  92 #define UCEBCDIC        24      /* convert ASCII to upper case blocked EBCDIC */
  93 #define IBM             25      /* convert ASCII to blocked IBM */
  94 #define LCIBM           26      /* convert ASCII to lower case blocked IBM */
  95 #define UCIBM           27      /* convert ASCII to upper case blocked IBM */
  96 #define LCASE           01      /* flag - convert to lower case */
  97 #define UCASE           02      /* flag - convert to upper case */
  98 #define SWAB            04      /* flag - swap bytes before conversion */
  99 #define NERR            010     /* flag - proceed on input errors */
 100 #define SYNC            020     /* flag - pad short input blocks with nulls */

 101 #define BADLIMIT        5       /* give up if no progress after BADLIMIT trys */
 102 #define SVR4XLATE       0       /* use default EBCDIC translation */
 103 #define BSDXLATE        1       /* use BSD-compatible EBCDIC translation */
 104 
 105 #define USAGE\
 106         "usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
 107         "          [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
 108         "          [iseek=n] [oseek=n] [seek=n] [stride=n] [istride=n]\n"\
 109         "          [ostride=n] [count=n] [conv=[ascii] [,ebcdic][,ibm]\n"\
 110         "          [,asciib][,ebcdicb][,ibmb][,block|unblock][,lcase|ucase]\n"\
 111         "          [,swab][,noerror][,notrunc][,sync]]\n"\
 112         "          [oflag=[dsync][sync]]\n"
 113 
 114 /* Global references */
 115 
 116 /* Local routine declarations */
 117 
 118 static int      match(char *);
 119 static void             term();
 120 static unsigned long long       number();
 121 static unsigned char    *flsh();
 122 static void             stats();
 123 
 124 /* Local data definitions */
 125 
 126 static unsigned ibs;    /* input buffer size */
 127 static unsigned obs;    /* output buffer size */
 128 static unsigned bs;     /* buffer size, overrules ibs and obs */
 129 static unsigned cbs;    /* conversion buffer size, used for block conversions */
 130 static unsigned ibc;    /* number of bytes still in the input buffer */
 131 static unsigned obc;    /* number of bytes in the output buffer */
 132 static unsigned cbc;    /* number of bytes in the conversion buffer */
 133 
 134 static int      ibf;    /* input file descriptor */
 135 static int      obf;    /* output file descriptor */
 136 static int      cflag;  /* conversion option flags */

 137 static int      oflag;  /* output flag options */
 138 static int      skipf;  /* if skipf == 1, skip rest of input line */
 139 static unsigned long long       nifr;   /* count of full input records */
 140 static unsigned long long       nipr;   /* count of partial input records */
 141 static unsigned long long       nofr;   /* count of full output records */
 142 static unsigned long long       nopr;   /* count of partial output records */
 143 static unsigned long long       ntrunc; /* count of truncated input lines */
 144 static unsigned long long       nbad;   /* count of bad records since last */
 145                                         /* good one */
 146 static int      files;  /* number of input files to concatenate (tape only) */
 147 static off_t    skip;   /* number of input records to skip */
 148 static off_t    iseekn; /* number of input records to seek past */
 149 static off_t    oseekn; /* number of output records to seek past */
 150 static unsigned long long       count;  /* number of input records to copy */
 151                         /* (0 = all) */
 152 static boolean_t ecount;        /* explicit count given */
 153 static off_t    ostriden;       /* number of output blocks to skip between */
 154                                 /* records */
 155 static off_t    istriden;       /* number of input blocks to skip between */
 156                                 /* records */


 469         0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
 470         0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
 471         0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
 472         0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
 473         0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
 474 };
 475 
 476 /* set up to use SVr4 ascii-ebcdic translation by default */
 477 
 478 static unsigned char *atoe = svr4_atoe;
 479 static unsigned char *etoa = svr4_etoa;
 480 static unsigned char *atoibm = svr4_atoibm;
 481 
 482 /*ARGSUSED*/
 483 static void
 484 siginfo_handler(int sig, siginfo_t *sip, void *ucp)
 485 {
 486         nstats = 1;
 487 }
 488 
























 489 int
 490 main(int argc, char **argv)
 491 {
 492         unsigned char *ip, *op; /* input and output buffer pointers */
 493         int c;          /* character counter */
 494         int ic;         /* input character */
 495         int conv;               /* conversion option code */
 496         int trunc;              /* whether output file is truncated */
 497         struct stat file_stat;
 498         struct sigaction sact;
 499 
 500         /* Set option defaults */
 501 
 502         ibs = BSIZE;
 503         obs = BSIZE;
 504         files = 1;
 505         conv = COPY;
 506         trunc = 1;                      /* default: truncate output file */
 507         trantype = SVR4XLATE;  /* use SVR4 EBCDIC by default */
 508 


 647                                 if (match("swab")) {
 648                                         cflag |= SWAB;
 649                                         continue;
 650                                 }
 651                                 if (match("noerror")) {
 652                                         cflag |= NERR;
 653                                         continue;
 654                                 }
 655                                 if (match("notrunc")) {
 656                                         trunc = 0;
 657                                         continue;
 658                                 }
 659                                 if (match("sync")) {
 660                                         cflag |= SYNC;
 661                                         continue;
 662                                 }
 663                                 goto badarg;
 664                         }
 665                         continue;
 666                 }
















 667                 if (match("oflag=")) {
 668                         for (;;) {
 669                                 if (match(",")) {
 670                                         continue;
 671                                 }
 672                                 if (*string == '\0') {
 673                                         break;
 674                                 }
 675                                 if (match("dsync")) {
 676                                         oflag |= O_DSYNC;
 677                                         continue;
 678                                 }
 679                                 if (match("sync")) {
 680                                         oflag |= O_SYNC;
 681                                         continue;
 682                                 }
 683                                 goto badarg;
 684                         }
 685                         continue;
 686                 }


 902         bzero(&sact, sizeof (struct sigaction));
 903         sact.sa_flags = SA_SIGINFO;
 904         sact.sa_sigaction = siginfo_handler;
 905         (void) sigemptyset(&sact.sa_mask);
 906         if (sigaction(SIGINFO, &sact, NULL) != 0) {
 907                 (void) fprintf(stderr, "dd: %s: %s\n",
 908                     gettext("failed to enable siginfo handler"),
 909                     gettext(strerror(errno)));
 910                 exit(2);
 911         }
 912         if (sigaction(SIGUSR1, &sact, NULL) != 0) {
 913                 (void) fprintf(stderr, "dd: %s: %s\n",
 914                     gettext("failed to enable sigusr1 handler"),
 915                     gettext(strerror(errno)));
 916                 exit(2);
 917         }
 918 
 919         /* Skip input blocks */
 920 
 921         while (skip) {
 922                 ibc = read(ibf, (char *)ibuf, ibs);
 923                 if (ibc == (unsigned)-1) {
 924                         if (++nbad > BADLIMIT) {
 925                                 (void) fprintf(stderr, "dd: %s\n",
 926                                     gettext("skip failed"));
 927                                 exit(2);
 928                         } else {
 929                                 perror("read");
 930                         }
 931                 } else {
 932                         if (ibc == 0) {
 933                                 (void) fprintf(stderr, "dd: %s\n",
 934                                 gettext("cannot skip past end-of-file"));
 935                                 exit(3);
 936                         } else {
 937                                 nbad = 0;
 938                         }
 939                 }
 940                 skip--;
 941         }
 942 


 977                 /* If proceed on error is enabled, zero the input buffer */
 978 
 979                         if (cflag & NERR) {
 980                                 ip = ibuf + ibs;
 981                                 c = ibs;
 982                                 if (c & 1)  /* if the size is odd, */
 983                                 {
 984                                         *--ip = 0;      /* clear the odd byte */
 985                                 }
 986                                 if (c >>= 1)              /* divide by two */
 987                                 {
 988                                         do {    /* clear two at a time */
 989                                                 *--ip = 0;
 990                                                 *--ip = 0;
 991                                         } while (--c);
 992                                 }
 993                         }
 994 
 995                         /* Read the next input block */
 996 
 997                         ibc = read(ibf, (char *)ibuf, ibs);
 998 
 999                         if (istriden > 0 && lseek(ibf, istriden * ((off_t)ibs),
1000                             SEEK_CUR) == -1) {
1001                                 perror("lseek");
1002                                 exit(2);
1003                         }
1004 
1005                         /* Process input errors */
1006 
1007                         if (ibc == (unsigned)-1) {
1008                                 perror("read");
1009                                 if (((cflag & NERR) == 0) ||
1010                                     (++nbad > BADLIMIT)) {
1011                                         while (obc) {
1012                                                 (void) flsh();
1013                                         }
1014                                         term(2);
1015                                 } else {
1016                                         stats();
1017                                         ibc = ibs; /* assume a full block */




  81 #define UNBLOCK         13      /* convert blocked ASCII to ASCII */
  82 #define LCUNBLOCK       14      /* convert blocked ASCII to lower case ASCII */
  83 #define UCUNBLOCK       15      /* convert blocked ASCII to upper case ASCII */
  84 #define ASCII           16      /* convert blocked EBCDIC to ASCII */
  85 #define LCASCII         17      /* convert blocked EBCDIC to lower case ASCII */
  86 #define UCASCII         18      /* convert blocked EBCDIC to upper case ASCII */
  87 #define BLOCK           19      /* convert ASCII to blocked ASCII */
  88 #define LCBLOCK         20      /* convert ASCII to lower case blocked ASCII */
  89 #define UCBLOCK         21      /* convert ASCII to upper case blocked ASCII */
  90 #define EBCDIC          22      /* convert ASCII to blocked EBCDIC */
  91 #define LCEBCDIC        23      /* convert ASCII to lower case blocked EBCDIC */
  92 #define UCEBCDIC        24      /* convert ASCII to upper case blocked EBCDIC */
  93 #define IBM             25      /* convert ASCII to blocked IBM */
  94 #define LCIBM           26      /* convert ASCII to lower case blocked IBM */
  95 #define UCIBM           27      /* convert ASCII to upper case blocked IBM */
  96 #define LCASE           01      /* flag - convert to lower case */
  97 #define UCASE           02      /* flag - convert to upper case */
  98 #define SWAB            04      /* flag - swap bytes before conversion */
  99 #define NERR            010     /* flag - proceed on input errors */
 100 #define SYNC            020     /* flag - pad short input blocks with nulls */
 101 #define FULLBLOCK       040     /* flag - accumulate full blocks of input */
 102 #define BADLIMIT        5       /* give up if no progress after BADLIMIT trys */
 103 #define SVR4XLATE       0       /* use default EBCDIC translation */
 104 #define BSDXLATE        1       /* use BSD-compatible EBCDIC translation */
 105 
 106 #define USAGE\
 107         "usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
 108         "          [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
 109         "          [iseek=n] [oseek=n] [seek=n] [stride=n] [istride=n]\n"\
 110         "          [ostride=n] [count=n] [conv=[ascii][,ebcdic][,ibm]\n"\
 111         "          [,asciib][,ebcdicb][,ibmb][,block|unblock][,lcase|ucase]\n"\
 112         "          [,swab][,noerror][,notrunc][,sync]]\n"\
 113         "          [iflag=[fullblock]] [oflag=[dsync][sync]]\n"
 114 
 115 /* Global references */
 116 
 117 /* Local routine declarations */
 118 
 119 static int      match(char *);
 120 static void             term();
 121 static unsigned long long       number();
 122 static unsigned char    *flsh();
 123 static void             stats();
 124 
 125 /* Local data definitions */
 126 
 127 static unsigned ibs;    /* input buffer size */
 128 static unsigned obs;    /* output buffer size */
 129 static unsigned bs;     /* buffer size, overrules ibs and obs */
 130 static unsigned cbs;    /* conversion buffer size, used for block conversions */
 131 static unsigned ibc;    /* number of bytes still in the input buffer */
 132 static unsigned obc;    /* number of bytes in the output buffer */
 133 static unsigned cbc;    /* number of bytes in the conversion buffer */
 134 
 135 static int      ibf;    /* input file descriptor */
 136 static int      obf;    /* output file descriptor */
 137 static int      cflag;  /* conversion option flags */
 138 static int      iflag;  /* input flag options */
 139 static int      oflag;  /* output flag options */
 140 static int      skipf;  /* if skipf == 1, skip rest of input line */
 141 static unsigned long long       nifr;   /* count of full input records */
 142 static unsigned long long       nipr;   /* count of partial input records */
 143 static unsigned long long       nofr;   /* count of full output records */
 144 static unsigned long long       nopr;   /* count of partial output records */
 145 static unsigned long long       ntrunc; /* count of truncated input lines */
 146 static unsigned long long       nbad;   /* count of bad records since last */
 147                                         /* good one */
 148 static int      files;  /* number of input files to concatenate (tape only) */
 149 static off_t    skip;   /* number of input records to skip */
 150 static off_t    iseekn; /* number of input records to seek past */
 151 static off_t    oseekn; /* number of output records to seek past */
 152 static unsigned long long       count;  /* number of input records to copy */
 153                         /* (0 = all) */
 154 static boolean_t ecount;        /* explicit count given */
 155 static off_t    ostriden;       /* number of output blocks to skip between */
 156                                 /* records */
 157 static off_t    istriden;       /* number of input blocks to skip between */
 158                                 /* records */


 471         0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
 472         0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
 473         0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
 474         0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
 475         0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
 476 };
 477 
 478 /* set up to use SVr4 ascii-ebcdic translation by default */
 479 
 480 static unsigned char *atoe = svr4_atoe;
 481 static unsigned char *etoa = svr4_etoa;
 482 static unsigned char *atoibm = svr4_atoibm;
 483 
 484 /*ARGSUSED*/
 485 static void
 486 siginfo_handler(int sig, siginfo_t *sip, void *ucp)
 487 {
 488         nstats = 1;
 489 }
 490 
 491 static ssize_t
 492 iread(int fd, char *buf, size_t nbyte)
 493 {
 494         ssize_t count;
 495 
 496         count = 0;
 497         while (nbyte != 0) {
 498                 ssize_t nr = read(fd, buf, nbyte);
 499 
 500                 if (nr < 0)
 501                         return (nr);
 502 
 503                 if (nr == 0)
 504                         break;
 505                 buf += nr;
 506                 count += nr;
 507                 nbyte -= nr;
 508 
 509                 if ((iflag & FULLBLOCK) == 0)
 510                         break;
 511         }
 512         return (count);
 513 }
 514 
 515 int
 516 main(int argc, char **argv)
 517 {
 518         unsigned char *ip, *op; /* input and output buffer pointers */
 519         int c;          /* character counter */
 520         int ic;         /* input character */
 521         int conv;               /* conversion option code */
 522         int trunc;              /* whether output file is truncated */
 523         struct stat file_stat;
 524         struct sigaction sact;
 525 
 526         /* Set option defaults */
 527 
 528         ibs = BSIZE;
 529         obs = BSIZE;
 530         files = 1;
 531         conv = COPY;
 532         trunc = 1;                      /* default: truncate output file */
 533         trantype = SVR4XLATE;  /* use SVR4 EBCDIC by default */
 534 


 673                                 if (match("swab")) {
 674                                         cflag |= SWAB;
 675                                         continue;
 676                                 }
 677                                 if (match("noerror")) {
 678                                         cflag |= NERR;
 679                                         continue;
 680                                 }
 681                                 if (match("notrunc")) {
 682                                         trunc = 0;
 683                                         continue;
 684                                 }
 685                                 if (match("sync")) {
 686                                         cflag |= SYNC;
 687                                         continue;
 688                                 }
 689                                 goto badarg;
 690                         }
 691                         continue;
 692                 }
 693                 if (match("iflag=")) {
 694                         for (;;) {
 695                                 if (match(",")) {
 696                                         continue;
 697                                 }
 698                                 if (*string == '\0') {
 699                                         break;
 700                                 }
 701                                 if (match("fullblock")) {
 702                                         iflag |= FULLBLOCK;
 703                                         continue;
 704                                 }
 705                                 goto badarg;
 706                         }
 707                         continue;
 708                 }
 709                 if (match("oflag=")) {
 710                         for (;;) {
 711                                 if (match(",")) {
 712                                         continue;
 713                                 }
 714                                 if (*string == '\0') {
 715                                         break;
 716                                 }
 717                                 if (match("dsync")) {
 718                                         oflag |= O_DSYNC;
 719                                         continue;
 720                                 }
 721                                 if (match("sync")) {
 722                                         oflag |= O_SYNC;
 723                                         continue;
 724                                 }
 725                                 goto badarg;
 726                         }
 727                         continue;
 728                 }


 944         bzero(&sact, sizeof (struct sigaction));
 945         sact.sa_flags = SA_SIGINFO;
 946         sact.sa_sigaction = siginfo_handler;
 947         (void) sigemptyset(&sact.sa_mask);
 948         if (sigaction(SIGINFO, &sact, NULL) != 0) {
 949                 (void) fprintf(stderr, "dd: %s: %s\n",
 950                     gettext("failed to enable siginfo handler"),
 951                     gettext(strerror(errno)));
 952                 exit(2);
 953         }
 954         if (sigaction(SIGUSR1, &sact, NULL) != 0) {
 955                 (void) fprintf(stderr, "dd: %s: %s\n",
 956                     gettext("failed to enable sigusr1 handler"),
 957                     gettext(strerror(errno)));
 958                 exit(2);
 959         }
 960 
 961         /* Skip input blocks */
 962 
 963         while (skip) {
 964                 ibc = iread(ibf, (char *)ibuf, ibs);
 965                 if (ibc == (unsigned)-1) {
 966                         if (++nbad > BADLIMIT) {
 967                                 (void) fprintf(stderr, "dd: %s\n",
 968                                     gettext("skip failed"));
 969                                 exit(2);
 970                         } else {
 971                                 perror("read");
 972                         }
 973                 } else {
 974                         if (ibc == 0) {
 975                                 (void) fprintf(stderr, "dd: %s\n",
 976                                 gettext("cannot skip past end-of-file"));
 977                                 exit(3);
 978                         } else {
 979                                 nbad = 0;
 980                         }
 981                 }
 982                 skip--;
 983         }
 984 


1019                 /* If proceed on error is enabled, zero the input buffer */
1020 
1021                         if (cflag & NERR) {
1022                                 ip = ibuf + ibs;
1023                                 c = ibs;
1024                                 if (c & 1)  /* if the size is odd, */
1025                                 {
1026                                         *--ip = 0;      /* clear the odd byte */
1027                                 }
1028                                 if (c >>= 1)              /* divide by two */
1029                                 {
1030                                         do {    /* clear two at a time */
1031                                                 *--ip = 0;
1032                                                 *--ip = 0;
1033                                         } while (--c);
1034                                 }
1035                         }
1036 
1037                         /* Read the next input block */
1038 
1039                         ibc = iread(ibf, (char *)ibuf, ibs);
1040 
1041                         if (istriden > 0 && lseek(ibf, istriden * ((off_t)ibs),
1042                             SEEK_CUR) == -1) {
1043                                 perror("lseek");
1044                                 exit(2);
1045                         }
1046 
1047                         /* Process input errors */
1048 
1049                         if (ibc == (unsigned)-1) {
1050                                 perror("read");
1051                                 if (((cflag & NERR) == 0) ||
1052                                     (++nbad > BADLIMIT)) {
1053                                         while (obc) {
1054                                                 (void) flsh();
1055                                         }
1056                                         term(2);
1057                                 } else {
1058                                         stats();
1059                                         ibc = ibs; /* assume a full block */