home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / dd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  22.4 KB  |  859 lines

  1. /* dd -- convert and copy files.
  2.    Copyright (C) 1985, 1989, 1990 Free Software Foundation, Inc.
  3.    ChangeLog: changelog
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 1, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/fileutil/RCS/dd.c'v 1.3.0.2 90/06/29 00:46:39 tho Stable $
  34.  */
  35.  
  36. /* Written by Paul Rubin and David MacKenzie. */
  37.  
  38. /* Options:
  39.  
  40.    Numbers can be followed by a multiplier:
  41.    b=512, k=1024, w=2, xm=number m
  42.  
  43.    if=FILE            Read from FILE instead of stdin.
  44.    of=FILE            Write to FILE instead of stdout.
  45.    ibs=BYTES            Read BYTES bytes at a time.
  46.    obs=BYTES            Write BYTES bytes at a time.
  47.    bs=BYTES            Override ibs and obs.
  48.    cbs=BYTES            Convert BYTES bytes at a time.
  49.    skip=BLOCKS            Skip BLOCKS ibs-sized blocks at
  50.                 start of input.
  51.    seek=BLOCKS            Skip BLOCKS obs-sized blocks at
  52.                 start of output.
  53.    count=BLOCKS            Copy only BLOCKS input blocks.
  54.    conv=CONVERSION[,CONVERSION...]
  55.  
  56.    Conversions:
  57.    ascii            Convert EBCDIC to ASCII.
  58.    ebcdic            Convert ASCII to EBCDIC.
  59.    ibm                Convert ASCII to alternate EBCDIC.
  60.    block            Pad newline-terminated records to size of
  61.                 cbs, replacing newline with trailing spaces.
  62.    unblock            Replace trailing spaces in cbs-sized block
  63.                 with newline.
  64.    lcase            Change uppercase characters to lowercase.
  65.    ucase            Change lowercase characters to uppercase.
  66.    swab                Swap every pair of bytes.
  67.    noerror            Continue after read and write errors.
  68.    sync                Pad every input block to size of ibs with
  69.                 trailing spaces. */
  70.  
  71. #include <stdio.h>
  72. #include <ctype.h>
  73. #include <sys/types.h>
  74. #include <signal.h>
  75. #include "system.h"
  76. #ifdef STDC_HEADERS
  77. #include <errno.h>
  78. #include <stdlib.h>
  79. #else
  80. char *malloc ();
  81.  
  82. extern int errno;
  83. #endif
  84.  
  85. #ifndef _POSIX_SOURCE
  86. long lseek ();
  87. #endif
  88.  
  89. #define equal(p, q) (strcmp ((p),(q)) == 0)
  90. #ifdef __STDC__
  91. #include <stdlib.h>
  92. #else
  93. #define max(a, b) ((a) > (b) ? (a) : (b))
  94. #endif /* __STDC__ */
  95.  
  96. /* Default input and output blocksize. */
  97. #define BLOCKSIZE 512
  98.  
  99. /* Conversions bit masks. */
  100. #define C_ASCII 01
  101. #define C_EBCDIC 02
  102. #define C_IBM 04
  103. #define C_BLOCK 010
  104. #define C_UNBLOCK 020
  105. #define C_LCASE 040
  106. #define C_UCASE 0100
  107. #define C_SWAB 0200
  108. #define C_NOERROR 0400
  109. #define C_SYNC 01000
  110. #define C_HARDWAY 04000        /* Use separate input and output buffers. */
  111.  
  112.  
  113. #ifdef MSDOS
  114.  
  115. #include <fcntl.h>
  116. #include <io.h>
  117.  
  118. extern void main (int, char **);
  119. extern void copy (void);
  120. extern void scanargs (int, char **);
  121. extern int parse_integer (char *);
  122. extern void parse_conversion (char *);
  123. extern int bit_count (int);
  124. extern void pfatal_with_name (char *, char *, int);
  125. extern void quit (int);
  126. extern char *xmalloc (int n);
  127. extern void fatal (char *, ... );
  128. extern void error (int status, int errnum, char *message, ...);
  129. extern void usage (char *, ... );
  130. extern SIGTYPE interrupt_handler (void);
  131.  
  132. #else /* not MSDOS */
  133.  
  134. char *xmalloc ();
  135. SIGTYPE interrupt_handler ();
  136. int bit_count ();
  137. int parse_integer ();
  138. void copy ();
  139. void error ();
  140. void parse_conversion ();
  141. void quit ();
  142. void scanargs ();
  143. void usage ();
  144.  
  145. #endif /* not MSDOS */
  146.  
  147. /* The name this program was run with. */
  148. char *program_name;
  149.  
  150. /* The name of the input file, or NULL for the standard input. */
  151. char *input_file = NULL;
  152.  
  153. /* The input file descriptor. */
  154. int input_fd = 0;
  155.  
  156. /* The name of the output file, or NULL for the standard output. */
  157. char *output_file = NULL;
  158.  
  159. /* The output file descriptor. */
  160. int output_fd = 1;
  161.  
  162. /* The number of bytes in which atomic reads are done. */
  163. #ifdef MSDOS
  164. int input_blocksize = BLOCKSIZE;
  165. #else /* not MSDOS */
  166. long input_blocksize = BLOCKSIZE;
  167. #endif /* not MSDOS */
  168.  
  169.  
  170. /* The number of bytes in which atomic writes are done. */
  171. #ifdef MSDOS
  172. int output_blocksize = BLOCKSIZE;
  173. #else /* not MSDOS */
  174. long output_blocksize = BLOCKSIZE;
  175. #endif /* not MSDOS */
  176.  
  177.  
  178. /* Conversion buffer size, in bytes. */
  179. #ifdef MSDOS
  180. int conversion_blocksize = BLOCKSIZE;
  181. #else /* not MSDOS */
  182. long conversion_blocksize = BLOCKSIZE;
  183. #endif /* not MSDOS */
  184.  
  185.  
  186. /* Skip this many records of `input_blocksize' bytes on input. */
  187. int skip_records = 0;
  188.  
  189. /* Seek to this record of `output_blocksize' bytes on output. */
  190. long seek_record = 0;
  191.  
  192. /* Copy only this many records.  <0 means no limit. */
  193. int max_record = -1;
  194.  
  195. /* Bit vector of conversions to apply. */
  196. int conversions_mask = 0;
  197.  
  198. /* Number of partial blocks written. */
  199. unsigned w_partial = 0;
  200.  
  201. /* Number of full blocks written. */
  202. unsigned w_full = 0;
  203.  
  204. /* Number of partial blocks read. */
  205. unsigned r_partial = 0;
  206.  
  207. /* Number of full blocks read. */
  208. unsigned r_full = 0;
  209.  
  210. /* Records truncated by conv=block. */
  211. unsigned r_truncate = 0;
  212.  
  213. /* Output representation of newline and space characters. */
  214. int newline_character = '\n';
  215. int space_character = ' ';
  216.  
  217. struct conversion
  218. {
  219.   char *convname;
  220.   int conversion;
  221. };
  222.  
  223. struct conversion conversions[] =
  224. {
  225.   "ascii", C_ASCII | C_HARDWAY,    /* ebcdic to ascii */
  226.   "ebcdic", C_EBCDIC | C_HARDWAY,    /* ascii to ebcdic */
  227.   "ibm", C_IBM | C_HARDWAY,    /* slightly different atoe */
  228.   "block", C_BLOCK | C_HARDWAY,    /* var to fixed len recs */
  229.   "unblock", C_UNBLOCK | C_HARDWAY,    /* fixed to var */
  230.   "lcase", C_LCASE | C_HARDWAY,    /* translate upper to lower */
  231.   "ucase", C_UCASE | C_HARDWAY,    /* translate lower to upper */
  232.   "swab", C_SWAB | C_HARDWAY,    /* swap bytes of input */
  233.   "noerror", C_NOERROR,        /* ignore i/o errors */
  234.   "sync", C_SYNC,        /* pad input recs to ibs */
  235.   NULL, 0
  236. };
  237.  
  238. /* Translation table formed by applying successive transformations. */
  239. unsigned char trans_table[256];
  240.  
  241. unsigned char ascii_to_ebcdic[] =
  242. {
  243.   0, 01, 02, 03, 067, 055, 056, 057,
  244.   026, 05, 045, 013, 014, 015, 016, 017,
  245.   020, 021, 022, 023, 074, 075, 062, 046,
  246.   030, 031, 077, 047, 034, 035, 036, 037,
  247.   0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
  248.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  249.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  250.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  251.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  252.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  253.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  254.   0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
  255.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  256.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  257.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  258.   0247, 0250, 0251, 0300, 0152, 0320, 0241, 07,
  259.   040, 041, 042, 043, 044, 025, 06, 027,
  260.   050, 051, 052, 053, 054, 011, 012, 033,
  261.   060, 061, 032, 063, 064, 065, 066, 010,
  262.   070, 071, 072, 073, 04, 024, 076, 0341,
  263.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  264.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  265.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  266.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  267.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  268.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  269.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  270.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  271.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  272.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  273.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  274.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  275. };
  276.  
  277. unsigned char ascii_to_ibm[] =
  278. {
  279.   0, 01, 02, 03, 067, 055, 056, 057,
  280.   026, 05, 045, 013, 014, 015, 016, 017,
  281.   020, 021, 022, 023, 074, 075, 062, 046,
  282.   030, 031, 077, 047, 034, 035, 036, 037,
  283.   0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
  284.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  285.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  286.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  287.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  288.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  289.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  290.   0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
  291.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  292.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  293.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  294.   0247, 0250, 0251, 0300, 0117, 0320, 0241, 07,
  295.   040, 041, 042, 043, 044, 025, 06, 027,
  296.   050, 051, 052, 053, 054, 011, 012, 033,
  297.   060, 061, 032, 063, 064, 065, 066, 010,
  298.   070, 071, 072, 073, 04, 024, 076, 0341,
  299.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  300.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  301.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  302.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  303.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  304.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  305.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  306.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  307.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  308.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  309.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  310.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  311. };
  312.  
  313. unsigned char ebcdic_to_ascii[] =
  314. {
  315.   0, 01, 02, 03, 0234, 011, 0206, 0177,
  316.   0227, 0215, 0216, 013, 014, 015, 016, 017,
  317.   020, 021, 022, 023, 0235, 0205, 010, 0207,
  318.   030, 031, 0222, 0217, 034, 035, 036, 037,
  319.   0200, 0201, 0202, 0203, 0204, 012, 027, 033,
  320.   0210, 0211, 0212, 0213, 0214, 05, 06, 07,
  321.   0220, 0221, 026, 0223, 0224, 0225, 0226, 04,
  322.   0230, 0231, 0232, 0233, 024, 025, 0236, 032,
  323.   040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
  324.   0247, 0250, 0133, 056, 074, 050, 053, 041,
  325.   046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  326.   0260, 0261, 0135, 044, 052, 051, 073, 0136,
  327.   055, 057, 0262, 0263, 0264, 0265, 0266, 0267,
  328.   0270, 0271, 0174, 054, 045, 0137, 076, 077,
  329.   0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
  330.   0302, 0140, 072, 043, 0100, 047, 075, 042,
  331.   0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
  332.   0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
  333.   0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
  334.   0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
  335.   0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
  336.   0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
  337.   0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  338.   0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  339.   0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  340.   0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
  341.   0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
  342.   0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
  343.   0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
  344.   0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
  345.   060, 061, 062, 063, 064, 065, 066, 067,
  346.   070, 071, 0372, 0373, 0374, 0375, 0376, 0377
  347. };
  348.  
  349. void
  350. main (argc, argv)
  351.      int argc;
  352.      char **argv;
  353. {
  354.   int i;
  355.  
  356.   program_name = argv[0];
  357. #ifdef MSDOS
  358.   strlwr (program_name);
  359. #endif /* MSDOS */
  360.  
  361.   /* Initialize translation table to identity translation. */
  362.   for (i = 0; i < 256; i++)
  363.     trans_table[i] = i;
  364.  
  365.   /* Decode arguments. */
  366.   scanargs (argc, argv);
  367.  
  368.   if (input_file != NULL)
  369.     {
  370.       input_fd = open (input_file, 0);
  371.       if (input_fd < 0)
  372.     error (1, errno, "%s", input_file);
  373.     }
  374.   else
  375.     input_file = "standard input";
  376.  
  377.   if (output_file != NULL)
  378.     {
  379.       output_fd = creat (output_file, 0666);
  380.       if (output_fd < 0)
  381.     error (1, errno, "%s", output_file);
  382.     }
  383.   else
  384.     output_file = "standard output";
  385.  
  386. #ifdef MSDOS
  387.   if (setmode (input_fd, O_BINARY) == -1
  388.       || setmode (output_fd, O_BINARY) == -1)
  389.     error (1, errno, "can't set binary mode");
  390. #endif /* MSDOS */
  391.  
  392.   signal (SIGINT, interrupt_handler);
  393.   copy ();
  394.   quit (0);
  395. }
  396.  
  397. void
  398. copy ()
  399. {
  400.   struct stat stats;
  401.   register unsigned char c;    /* Each char being converted. */
  402.   register int i;        /* Index into `ibuf'. */
  403.   int oc = 0;            /* Index into `obuf'. */
  404.   int spaces = 0;        /* Number of pending blanks to add. */
  405.   int col = 0;            /* Index into each line. */
  406.   unsigned char *ibuf, *obuf;    /* Buffers. */
  407.   int nread, nwritten;        /* Byte counts for each block. */
  408.  
  409.   ibuf = (unsigned char *) xmalloc (input_blocksize);
  410.   if (conversions_mask & C_HARDWAY)
  411.     obuf = (unsigned char *) xmalloc (output_blocksize);
  412.   else
  413.     obuf = ibuf;
  414.  
  415.   /* Use fstat instead of checking for errno == ESPIPE because
  416.      lseek doesn't work on some special files but doesn't return an
  417.      error, either. */
  418.   if (fstat (input_fd, &stats))
  419.     {
  420.       error (0, errno, "%s", input_file);
  421.       quit (1);
  422.     }
  423.   if ((stats.st_mode & S_IFMT) == S_IFREG)
  424.     {
  425. #ifdef MSDOS
  426.       if (lseek (input_fd, skip_records * (long) input_blocksize, 0) < 0)
  427. #else /* not MSDOS */
  428.       if (lseek (input_fd, skip_records * input_blocksize, 0) < 0)
  429. #endif /* not MSDOS */
  430.     {
  431.       error (0, errno, "%s", input_file);
  432.       quit (1);
  433.     }
  434.     }
  435.   else
  436.     {
  437.       for (i = 0; i < skip_records; i++)
  438.     if (read (input_fd, ibuf, input_blocksize) < 0)
  439.       {
  440.         error (0, errno, "%s", input_file);
  441.         quit (1);
  442.       }
  443.     }
  444.  
  445.   if (fstat (output_fd, &stats))
  446.     {
  447.       error (0, errno, "%s", output_file);
  448.       quit (1);
  449.     }
  450.   if ((stats.st_mode & S_IFMT) == S_IFREG)
  451.     {
  452.       if (lseek (output_fd, seek_record * output_blocksize, 0) < 0)
  453.     {
  454.       error (0, errno, "%s", output_file);
  455.       quit (1);
  456.     }
  457.     }
  458.   
  459.   if (max_record >= 0 && r_partial + r_full >= max_record)
  460.     return;
  461.  
  462.   while ((max_record < 0 || r_partial + r_full < max_record)
  463.      && (nread = read (input_fd, ibuf, input_blocksize)) != 0)
  464.     {
  465.       if (nread < 0)
  466.     {
  467.       error (0, errno, "%s", input_file);
  468.       if (conversions_mask & C_NOERROR)
  469.         continue;
  470.       else
  471.         quit (2);
  472.     }
  473.  
  474.       if (nread < input_blocksize)
  475.     {
  476.       r_partial++;
  477.       if (conversions_mask & C_SYNC)
  478.         while (nread < input_blocksize)
  479.           ibuf[nread++] = space_character;
  480.     }
  481.       else
  482.     r_full++;
  483.  
  484.       /* If blocksizes are the same and no conversion, just flush. */
  485.       if (ibuf == obuf)
  486.     {
  487.       nwritten = write (output_fd, obuf, nread);
  488.       if (nwritten != nread)
  489.         {
  490.           error (0, errno, "%s", output_file);
  491.           if (nwritten > 0)
  492.         w_partial++;
  493.           if (!(conversions_mask & C_NOERROR))
  494.         quit (1);
  495.         }
  496.       else if (nread == input_blocksize)
  497.         w_full++;
  498.       else
  499.         w_partial++;
  500.       continue;
  501.     }
  502.  
  503.       /* Copy input to output, doing conversion and flushing output
  504.      as necessary. */
  505.       for (i = 0; i < nread; i++)
  506.     {
  507.       c = trans_table[ibuf[i]];
  508.  
  509.       if (conversions_mask & C_BLOCK)
  510.         {
  511.           if (spaces > 0)
  512.         {
  513.           spaces--;
  514.           i--;        /* Push char back; get it next time. */
  515.           c = space_character;
  516.         }
  517.           else if (c == newline_character)
  518.         {
  519.           spaces = max (0, conversion_blocksize - col);
  520.           col = 0;
  521.           continue;
  522.         }
  523.           else if (col++ >= conversion_blocksize)
  524.         {
  525.           r_truncate++;
  526.           continue;
  527.         }
  528.         }
  529.       else if (conversions_mask & C_UNBLOCK)
  530.         {
  531.           if (col++ >= conversion_blocksize)
  532.         {
  533.           col = spaces = 0;
  534.           i--;        /* Push char back; get it next time. */
  535.           c = newline_character;
  536.         }
  537.           else if (c == space_character)
  538.         {
  539.           spaces++;
  540.           continue;
  541.         }
  542.           else if (spaces > 0)
  543.         {
  544.           /* Hit the end of a run of spaces that were not at the
  545.              end of the conversion buffer. */
  546.           spaces--;
  547.           col--;
  548.           i--;
  549.           c = space_character;
  550.         }
  551.         }
  552.  
  553.       if (conversions_mask & C_SWAB)
  554.         obuf[oc++ ^ 1] = c;
  555.       else
  556.         obuf[oc++] = c;
  557.  
  558.       if (oc >= output_blocksize)
  559.         {
  560.           nwritten = write (output_fd, obuf, output_blocksize);
  561.           if (nwritten != output_blocksize)
  562.         {
  563.           error (0, errno, "%s", output_file);
  564.           if (nwritten > 0)
  565.             w_partial++;
  566.           if (!(conversions_mask & C_NOERROR))
  567.             quit (1);
  568.         }
  569.           else
  570.         w_full++;
  571.           oc = 0;
  572.         }
  573.     }
  574.     }
  575.  
  576.   if (conversions_mask & C_BLOCK)
  577.     {
  578.       /* The last record might need to be padded. */
  579.       while (spaces-- > 0)
  580.     {
  581.       if (conversions_mask & C_SWAB)
  582.         obuf[oc++ ^ 1] = space_character;
  583.       else
  584.         obuf[oc++] = space_character;
  585.       if (oc >= output_blocksize)
  586.         {
  587.           nwritten = write (output_fd, obuf, output_blocksize);
  588.           if (nwritten != output_blocksize)
  589.         {
  590.           error (0, errno, "%s", output_file);
  591.           if (nwritten > 0)
  592.             w_partial++;
  593.           if (!(conversions_mask & C_NOERROR))
  594.             quit (1);
  595.         }
  596.           else
  597.         w_full++;
  598.           oc = 0;
  599.         }
  600.     }
  601.     }
  602.   else if (conversions_mask & C_UNBLOCK)
  603.     {
  604.       /* Add a final '\n' if there are exactly `conversion_blocksize'
  605.      characters in the final record. */
  606.       if (col == conversion_blocksize)
  607.     {
  608.       if (conversions_mask & C_SWAB)
  609.         obuf[oc++ ^ 1] = newline_character;
  610.       else
  611.         obuf[oc++] = newline_character;
  612.     }
  613.     }
  614.  
  615.   /* Flush last block. */
  616.   if (oc > 0)
  617.     {
  618.       /* First, fix earlier screw-up if swapping bytes and `oc' is odd. */
  619.       if ((conversions_mask & C_SWAB) && (oc & 1))
  620.     obuf[oc - 1] = obuf[oc];
  621.  
  622.       nwritten = write (output_fd, obuf, oc);
  623.       if (nwritten > 0)
  624.     w_partial++;
  625.       if (nwritten != oc)
  626.     {
  627.       error (0, errno, "%s", output_file);
  628.       if (!(conversions_mask & C_NOERROR))
  629.         quit (1);
  630.     }
  631.     }
  632. }
  633.  
  634. void
  635. scanargs (argc, argv)
  636.      int argc;
  637.      char **argv;
  638. {
  639.   int i, n;
  640.  
  641.   for (i = 1; i < argc; i++)
  642.     {
  643.       char *name, *val;
  644.  
  645.       name = argv[i];
  646.       val = index (name, '=');
  647.       if (val == NULL)
  648.     usage ("unrecognized option `%s'", name);
  649.       *val++ = '\0';
  650.  
  651.       if (equal (name, "if"))
  652.     input_file = val;
  653.       else if (equal (name, "of"))
  654.     output_file = val;
  655.       else if (equal (name, "conv"))
  656.     parse_conversion (val);
  657.       else
  658.     {
  659.       n = parse_integer (val);
  660.  
  661.       if (equal (name, "ibs"))
  662.         {
  663.           input_blocksize = n;
  664.           conversions_mask |= C_HARDWAY;
  665.         }
  666.       else if (equal (name, "obs"))
  667.         {
  668.           output_blocksize = n;
  669.           conversions_mask |= C_HARDWAY;
  670.         }
  671.       else if (equal (name, "bs"))
  672.         output_blocksize = input_blocksize = n;
  673.       else if (equal (name, "cbs"))
  674.         conversion_blocksize = n;
  675.       else if (equal (name, "skip"))
  676.         skip_records = n;
  677.       else if (equal (name, "seek"))
  678.         seek_record = n;
  679.       else if (equal (name, "count"))
  680.         max_record = n;
  681.       else
  682.         usage ("unrecognized option `%s=%s'", name, val);
  683.     }
  684.     }
  685. }
  686.  
  687. int
  688. parse_integer (str)
  689.      char *str;
  690. {
  691.   int n = 0;
  692.   char *p = str;
  693.  
  694.   while (isdigit (*p))
  695.     {
  696.       n = n * 10 + *p - '0';
  697.       p++;
  698.     }
  699. loop:
  700.   switch (*p++)
  701.     {
  702.     case '\0':
  703.       return n;
  704.     case 'b':
  705.       n *= 512;
  706.       goto loop;
  707.     case 'k':
  708.       n *= 1024;
  709.       goto loop;
  710.     case 'w':
  711.       n *= 2;
  712.       goto loop;
  713.     case 'x':
  714.       n *= parse_integer (p);
  715.       break;
  716.     default:
  717.       return 0;
  718.     }
  719.   return n;
  720. }
  721.  
  722. void
  723. parse_conversion (str)
  724.      char *str;
  725. {
  726.   char *new;
  727.   int i, j;
  728.   unsigned char *new_trans;
  729.  
  730.   do
  731.     {
  732.       new = index (str, ',');
  733.       if (new != NULL)
  734.     *new++ = '\0';
  735.       for (i = 0; conversions[i].convname != NULL; i++)
  736.     if (equal (conversions[i].convname, str))
  737.       {
  738.         conversions_mask |= conversions[i].conversion;
  739.         break;
  740.       }
  741.       if (conversions[i].convname == NULL)
  742.     {
  743.       usage ("%s: invalid conversion", str);
  744.       exit (1);
  745.     }
  746. #define MX(a) (bit_count (conversions_mask & (a)))
  747.       if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
  748.       || (MX (C_BLOCK | C_UNBLOCK) > 1)
  749.       || (MX (C_LCASE | C_UCASE) > 1)
  750.       || (MX (C_UNBLOCK | C_SYNC) > 1))
  751.     {
  752.       error (1, 0, "\
  753. only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}");
  754.     }
  755. #undef MX
  756.       str = new;
  757.   } while (new != NULL);
  758.  
  759.   /* I don't know if the following restriction is stupid,
  760.      but it's convenient. */
  761.   if ((conversions_mask & C_SWAB) && (input_blocksize % 2 != 0
  762.                     || output_blocksize % 2 != 0))
  763.     {
  764.       error (1, 0, "ibs and obs must both be even for swab to work");
  765.     }
  766.  
  767.   /* Fix up translation table. */
  768.  
  769.   /* Do upper and lower case if necessary. */
  770.   if (conversions_mask & C_UCASE)
  771.     for (j = 'a'; j <= 'z'; j++)
  772.       trans_table[j] = toupper (j);
  773.   else if (conversions_mask & C_LCASE)
  774.     for (j = 'A'; j <= 'Z'; j++)
  775.       trans_table[j] = tolower (j);
  776.  
  777.   /* Now find and apply char set translation. */
  778.   if (conversions_mask & C_ASCII)
  779.     new_trans = ebcdic_to_ascii;
  780.   else if (conversions_mask & C_EBCDIC)
  781.     {
  782.       new_trans = ascii_to_ebcdic;
  783.       newline_character = ascii_to_ebcdic['\n'];
  784.       space_character = ascii_to_ebcdic[' '];
  785.     }
  786.   else if (conversions_mask & C_IBM)
  787.     {
  788.       new_trans = ascii_to_ibm;
  789.       newline_character = ascii_to_ibm['\n'];
  790.       space_character = ascii_to_ibm[' '];
  791.     }
  792.   else
  793.     return;
  794.  
  795.   for (i = 0; i < 256; i++)
  796.     trans_table[i] = new_trans[trans_table[i]];
  797. }
  798.  
  799. /* Return number of 1 bits in `i'. */
  800. int
  801. bit_count (i)
  802.      register int i;
  803. {
  804.   register int n;
  805.  
  806.   for (n = 0; i != 0; i = (i >> 1) & ~(1 << 31))
  807.     n += i & 1;
  808.   return n;
  809. }
  810.  
  811. /* Print statistics and exit with status `code'. */
  812. void
  813. quit (code)
  814.      int code;
  815. {
  816.   fprintf (stderr, "%u+%u records in\n", r_full, r_partial);
  817.   fprintf (stderr, "%u+%u records out\n", w_full, w_partial);
  818.   if (r_truncate > 0)
  819.     fprintf (stderr, "%u truncated records\n", r_truncate);
  820.  
  821.   exit (code);
  822. }
  823.  
  824. SIGTYPE
  825. interrupt_handler ()
  826. {
  827.   quit (1);
  828. }
  829.  
  830. /* malloc or die. */
  831. char *
  832. xmalloc (n)
  833.      int n;
  834. {
  835.   char *p;
  836.  
  837.   p = malloc (n);
  838.   if (p == NULL)
  839.     error (3, 0, "virtual memory exhausted");
  840.   return p;
  841. }
  842.  
  843. void
  844. usage (string, arg0, arg1)
  845.      char *string, *arg0, *arg1;
  846. {
  847.   fprintf (stderr, "%s: ", program_name);
  848.   fprintf (stderr, string, arg0, arg1);
  849.   fprintf (stderr, "\n");
  850.   fprintf (stderr, "\
  851. Usage: %s [if=file] [of=file] [ibs=bytes] [obs=bytes] [bs=bytes] [cbs=bytes]\n\
  852.        [skip=blocks] [seek=blocks] [count=blocks]\n\
  853.        [conv={ascii,ebcdic,ibm,block,unblock,lcase,ucase,swab,noerror,sync}]\n\
  854. Numbers can be followed by a multiplier:\n\
  855. b=512, k=1024, w=2, xm=number m\n",
  856.        program_name);
  857.   exit (1);
  858. }
  859.