home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / DD.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  28KB  |  1,036 lines

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