xref: /illumos-gate/usr/src/cmd/dd/dd.c (revision 61304e4f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24a77d64afScf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
264cddff70SJosef 'Jeff' Sipek  * Copyright 2012, Josef 'Jeff' Sipek <jeffpc@31bits.net>. All rights reserved.
2719d32b9aSRobert Mustacchi  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
28*61304e4fSPaul Dagnelie  * Copyright (c) 2014 by Delphix. All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31a77d64afScf /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
32a77d64afScf /*	  All Rights Reserved  	*/
33a77d64afScf 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  *	convert and copy
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include	<stdio.h>
397c478bd9Sstevel@tonic-gate #include	<signal.h>
407c478bd9Sstevel@tonic-gate #include	<fcntl.h>
417c478bd9Sstevel@tonic-gate #include	<sys/param.h>
427c478bd9Sstevel@tonic-gate #include	<sys/types.h>
437c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
457c478bd9Sstevel@tonic-gate #include	<unistd.h>
467c478bd9Sstevel@tonic-gate #include	<stdlib.h>
477c478bd9Sstevel@tonic-gate #include	<locale.h>
487c478bd9Sstevel@tonic-gate #include	<string.h>
4919d32b9aSRobert Mustacchi #include	<sys/time.h>
5019d32b9aSRobert Mustacchi #include	<errno.h>
5119d32b9aSRobert Mustacchi #include	<strings.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* The BIG parameter is machine dependent.  It should be a long integer	*/
547c478bd9Sstevel@tonic-gate /* constant that can be used by the number parser to check the validity	*/
557c478bd9Sstevel@tonic-gate /* of numeric parameters.  On 16-bit machines, it should probably be	*/
567c478bd9Sstevel@tonic-gate /* the maximum unsigned integer, 0177777L.  On 32-bit machines where	*/
577c478bd9Sstevel@tonic-gate /* longs are the same size as ints, the maximum signed integer is more	*/
587c478bd9Sstevel@tonic-gate /* appropriate.  This value is 017777777777L. In 64 bit environments,   */
597c478bd9Sstevel@tonic-gate /* the maximum signed integer value is 0777777777777777777777LL		*/
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	BIG	0777777777777777777777LL
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	BSIZE	512
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* Option parameters */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	COPY		0	/* file copy, preserve input block size */
687c478bd9Sstevel@tonic-gate #define	REBLOCK		1	/* file copy, change block size */
697c478bd9Sstevel@tonic-gate #define	LCREBLOCK	2	/* file copy, convert to lower case */
707c478bd9Sstevel@tonic-gate #define	UCREBLOCK	3	/* file copy, convert to upper case */
717c478bd9Sstevel@tonic-gate #define	NBASCII		4	/* file copy, convert from EBCDIC to ASCII */
727c478bd9Sstevel@tonic-gate #define	LCNBASCII	5	/* file copy, EBCDIC to lower case ASCII */
737c478bd9Sstevel@tonic-gate #define	UCNBASCII	6	/* file copy, EBCDIC to upper case ASCII */
747c478bd9Sstevel@tonic-gate #define	NBEBCDIC	7	/* file copy, convert from ASCII to EBCDIC */
757c478bd9Sstevel@tonic-gate #define	LCNBEBCDIC	8	/* file copy, ASCII to lower case EBCDIC */
767c478bd9Sstevel@tonic-gate #define	UCNBEBCDIC	9	/* file copy, ASCII to upper case EBCDIC */
777c478bd9Sstevel@tonic-gate #define	NBIBM		10	/* file copy, convert from ASCII to IBM */
787c478bd9Sstevel@tonic-gate #define	LCNBIBM		11	/* file copy, ASCII to lower case IBM */
797c478bd9Sstevel@tonic-gate #define	UCNBIBM		12	/* file copy, ASCII to upper case IBM */
807c478bd9Sstevel@tonic-gate #define	UNBLOCK		13	/* convert blocked ASCII to ASCII */
817c478bd9Sstevel@tonic-gate #define	LCUNBLOCK	14	/* convert blocked ASCII to lower case ASCII */
827c478bd9Sstevel@tonic-gate #define	UCUNBLOCK	15	/* convert blocked ASCII to upper case ASCII */
837c478bd9Sstevel@tonic-gate #define	ASCII		16	/* convert blocked EBCDIC to ASCII */
847c478bd9Sstevel@tonic-gate #define	LCASCII		17	/* convert blocked EBCDIC to lower case ASCII */
857c478bd9Sstevel@tonic-gate #define	UCASCII		18	/* convert blocked EBCDIC to upper case ASCII */
867c478bd9Sstevel@tonic-gate #define	BLOCK		19	/* convert ASCII to blocked ASCII */
877c478bd9Sstevel@tonic-gate #define	LCBLOCK		20	/* convert ASCII to lower case blocked ASCII */
887c478bd9Sstevel@tonic-gate #define	UCBLOCK		21	/* convert ASCII to upper case blocked ASCII */
897c478bd9Sstevel@tonic-gate #define	EBCDIC		22	/* convert ASCII to blocked EBCDIC */
907c478bd9Sstevel@tonic-gate #define	LCEBCDIC	23	/* convert ASCII to lower case blocked EBCDIC */
917c478bd9Sstevel@tonic-gate #define	UCEBCDIC	24	/* convert ASCII to upper case blocked EBCDIC */
927c478bd9Sstevel@tonic-gate #define	IBM		25	/* convert ASCII to blocked IBM */
937c478bd9Sstevel@tonic-gate #define	LCIBM		26	/* convert ASCII to lower case blocked IBM */
947c478bd9Sstevel@tonic-gate #define	UCIBM		27	/* convert ASCII to upper case blocked IBM */
957c478bd9Sstevel@tonic-gate #define	LCASE		01	/* flag - convert to lower case */
967c478bd9Sstevel@tonic-gate #define	UCASE		02	/* flag - convert to upper case */
977c478bd9Sstevel@tonic-gate #define	SWAB		04	/* flag - swap bytes before conversion */
987c478bd9Sstevel@tonic-gate #define	NERR		010	/* flag - proceed on input errors */
997c478bd9Sstevel@tonic-gate #define	SYNC		020	/* flag - pad short input blocks with nulls */
1007c478bd9Sstevel@tonic-gate #define	BADLIMIT	5	/* give up if no progress after BADLIMIT trys */
1017c478bd9Sstevel@tonic-gate #define	SVR4XLATE	0	/* use default EBCDIC translation */
1027c478bd9Sstevel@tonic-gate #define	BSDXLATE	1	/* use BSD-compatible EBCDIC translation */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	USAGE\
1057c478bd9Sstevel@tonic-gate 	"usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
1067c478bd9Sstevel@tonic-gate 	"	   [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
107*61304e4fSPaul Dagnelie 	"	   [iseek=n] [oseek=n] [seek=n] [stride=n] [istride=n]\n"\
108*61304e4fSPaul Dagnelie 	"	   [ostride=n] [count=n] [conv=[ascii] [,ebcdic][,ibm]\n"\
109*61304e4fSPaul Dagnelie 	"	   [,asciib][,ebcdicb][,ibmb][,block|unblock][,lcase|ucase]\n"\
110*61304e4fSPaul Dagnelie 	"	   [,swab][,noerror][,notrunc][,sync]]\n"\
11119d32b9aSRobert Mustacchi 	"	   [oflag=[dsync][sync]]\n"
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* Global references */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Local routine declarations */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static int	match(char *);
1187c478bd9Sstevel@tonic-gate static void		term();
1197c478bd9Sstevel@tonic-gate static unsigned long long	number();
1207c478bd9Sstevel@tonic-gate static unsigned char	*flsh();
1217c478bd9Sstevel@tonic-gate static void		stats();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* Local data definitions */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static unsigned ibs;	/* input buffer size */
1267c478bd9Sstevel@tonic-gate static unsigned obs;	/* output buffer size */
1277c478bd9Sstevel@tonic-gate static unsigned bs;	/* buffer size, overrules ibs and obs */
1287c478bd9Sstevel@tonic-gate static unsigned cbs;	/* conversion buffer size, used for block conversions */
1297c478bd9Sstevel@tonic-gate static unsigned ibc;	/* number of bytes still in the input buffer */
1307c478bd9Sstevel@tonic-gate static unsigned obc;	/* number of bytes in the output buffer */
1317c478bd9Sstevel@tonic-gate static unsigned cbc;	/* number of bytes in the conversion buffer */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int	ibf;	/* input file descriptor */
1347c478bd9Sstevel@tonic-gate static int	obf;	/* output file descriptor */
1357c478bd9Sstevel@tonic-gate static int	cflag;	/* conversion option flags */
13619d32b9aSRobert Mustacchi static int	oflag;	/* output flag options */
1377c478bd9Sstevel@tonic-gate static int	skipf;	/* if skipf == 1, skip rest of input line */
1387c478bd9Sstevel@tonic-gate static unsigned long long	nifr;	/* count of full input records */
1397c478bd9Sstevel@tonic-gate static unsigned long long	nipr;	/* count of partial input records */
1407c478bd9Sstevel@tonic-gate static unsigned long long	nofr;	/* count of full output records */
1417c478bd9Sstevel@tonic-gate static unsigned long long	nopr;	/* count of partial output records */
1427c478bd9Sstevel@tonic-gate static unsigned long long	ntrunc;	/* count of truncated input lines */
1437c478bd9Sstevel@tonic-gate static unsigned long long	nbad;	/* count of bad records since last */
1447c478bd9Sstevel@tonic-gate 					/* good one */
1457c478bd9Sstevel@tonic-gate static int	files;	/* number of input files to concatenate (tape only) */
1467c478bd9Sstevel@tonic-gate static off_t	skip;	/* number of input records to skip */
1477c478bd9Sstevel@tonic-gate static off_t	iseekn;	/* number of input records to seek past */
1487c478bd9Sstevel@tonic-gate static off_t	oseekn;	/* number of output records to seek past */
1497c478bd9Sstevel@tonic-gate static unsigned long long	count;	/* number of input records to copy */
1507c478bd9Sstevel@tonic-gate 			/* (0 = all) */
151e3bee069SRobert Mustacchi static boolean_t ecount;	/* explicit count given */
152*61304e4fSPaul Dagnelie static off_t	ostriden;	/* number of output blocks to skip between */
153*61304e4fSPaul Dagnelie 				/* records */
154*61304e4fSPaul Dagnelie static off_t	istriden;	/* number of input blocks to skip between */
155*61304e4fSPaul Dagnelie 				/* records */
156*61304e4fSPaul Dagnelie 
1577c478bd9Sstevel@tonic-gate static int	trantype; /* BSD or SVr4 compatible EBCDIC */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static char		*string;	/* command arg pointer */
1607c478bd9Sstevel@tonic-gate static char		*ifile;		/* input file name pointer */
1617c478bd9Sstevel@tonic-gate static char		*ofile;		/* output file name pointer */
1627c478bd9Sstevel@tonic-gate static unsigned char	*ibuf;		/* input buffer pointer */
1637c478bd9Sstevel@tonic-gate static unsigned char	*obuf;		/* output buffer pointer */
1647c478bd9Sstevel@tonic-gate 
16519d32b9aSRobert Mustacchi static hrtime_t		startt;		/* hrtime copy started */
16619d32b9aSRobert Mustacchi static unsigned long long	obytes;	/* output bytes */
16719d32b9aSRobert Mustacchi static sig_atomic_t	nstats;		/* do we need to output stats */
16819d32b9aSRobert Mustacchi 
1697c478bd9Sstevel@tonic-gate /* This is an EBCDIC to ASCII conversion table	*/
1707c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static unsigned char svr4_etoa [] =
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
1757c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
1767c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
1777c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
1787c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
1797c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
1807c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
1817c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
1827c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
1837c478bd9Sstevel@tonic-gate 	0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174,
1847c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
1857c478bd9Sstevel@tonic-gate 	0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176,
1867c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
1877c478bd9Sstevel@tonic-gate 	0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077,
1887c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
1897c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
1907c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
1917c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
1927c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
1937c478bd9Sstevel@tonic-gate 	0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320,
1947c478bd9Sstevel@tonic-gate 	0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170,
1957c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327,
1967c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
1977c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347,
1987c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
1997c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
2007c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
2017c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
2027c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
2037c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
2047c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2057c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
2067c478bd9Sstevel@tonic-gate };
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /* This is an ASCII to EBCDIC conversion table	*/
2097c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static unsigned char svr4_atoe [] =
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2147c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2157c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2167c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2177c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2187c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2197c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2207c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2217c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2227c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2237c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2247c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155,
2257c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2267c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2277c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2287c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007,
2297c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2307c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2317c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2327c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2337c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2347c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2357c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2367c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2377c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2387c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236,
2397c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257,
2407c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2417c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277,
2427c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2437c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2447c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2457c478bd9Sstevel@tonic-gate };
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /* Table for ASCII to IBM (alternate EBCDIC) code conversion	*/
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static unsigned char svr4_atoibm[] =
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2527c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2537c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2547c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2557c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2567c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2577c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2587c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2597c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2607c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2617c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2627c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
2637c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2647c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2657c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2667c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
2677c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2687c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2697c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2707c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2717c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2727c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2737c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2747c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2757c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2767c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
2777c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
2787c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2797c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
2807c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2817c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2827c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2837c478bd9Sstevel@tonic-gate };
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to lower case ASCII	*/
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static unsigned char utol[] =
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
2907c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
2917c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
2927c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
2937c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
2947c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
2957c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2967c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
2977c478bd9Sstevel@tonic-gate 	0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
2987c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
2997c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3007c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
3017c478bd9Sstevel@tonic-gate 	0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3027c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
3037c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3047c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
3057c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3067c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3077c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3087c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3097c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3107c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3117c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3127c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3137c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3147c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3157c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3167c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3177c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3187c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3197c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3207c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3217c478bd9Sstevel@tonic-gate };
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to upper case ASCII	*/
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static unsigned char ltou[] =
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
3287c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
3297c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
3307c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
3317c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
3327c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
3337c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3347c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
3357c478bd9Sstevel@tonic-gate 	0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3367c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3377c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3387c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
3397c478bd9Sstevel@tonic-gate 	0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3407c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3417c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3427c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
3437c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3447c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3457c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3467c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3477c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3487c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3497c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3507c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3517c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3527c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3537c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3547c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3557c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3567c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3577c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3587c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3597c478bd9Sstevel@tonic-gate };
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /* BSD-compatible EBCDIC to ASCII translate table */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static unsigned char	bsd_etoa[] =
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
3667c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
3677c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
3687c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
3697c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
3707c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
3717c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
3727c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
3737c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
3747c478bd9Sstevel@tonic-gate 	0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041,
3757c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3767c478bd9Sstevel@tonic-gate 	0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136,
3777c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
3787c478bd9Sstevel@tonic-gate 	0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077,
3797c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
3807c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
3817c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3827c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
3837c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
3847c478bd9Sstevel@tonic-gate 	0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
3857c478bd9Sstevel@tonic-gate 	0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
3867c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
3877c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3887c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3897c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3907c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
3917c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
3927c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
3937c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
3947c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
3957c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3967c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
3977c478bd9Sstevel@tonic-gate };
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to EBCDIC translate table */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static unsigned char	bsd_atoe[] =
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4047c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4057c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4067c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4077c478bd9Sstevel@tonic-gate 	0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
4087c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4097c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4107c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4117c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4127c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4137c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4147c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
4157c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4167c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4177c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4187c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007,
4197c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4207c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4217c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4227c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4237c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4247c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4257c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4267c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4277c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4287c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4297c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4307c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4317c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4327c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4337c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4347c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4357c478bd9Sstevel@tonic-gate };
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to IBM translate table */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate static unsigned char	bsd_atoibm[] =
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4427c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4437c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4447c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4457c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
4467c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4477c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4487c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4497c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4507c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4517c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4527c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
4537c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4547c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4557c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4567c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
4577c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4587c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4597c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4607c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4617c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4627c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4637c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4647c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4657c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4667c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4677c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4687c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4697c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4707c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4717c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4727c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4737c478bd9Sstevel@tonic-gate };
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /* set up to use SVr4 ascii-ebcdic translation by default */
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static unsigned char *atoe = svr4_atoe;
4787c478bd9Sstevel@tonic-gate static unsigned char *etoa = svr4_etoa;
4797c478bd9Sstevel@tonic-gate static unsigned char *atoibm = svr4_atoibm;
4807c478bd9Sstevel@tonic-gate 
48119d32b9aSRobert Mustacchi /*ARGSUSED*/
48219d32b9aSRobert Mustacchi static void
48319d32b9aSRobert Mustacchi siginfo_handler(int sig, siginfo_t *sip, void *ucp)
48419d32b9aSRobert Mustacchi {
48519d32b9aSRobert Mustacchi 	nstats = 1;
48619d32b9aSRobert Mustacchi }
4877c478bd9Sstevel@tonic-gate 
488a77d64afScf int
489a77d64afScf main(int argc, char **argv)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	unsigned char *ip, *op; /* input and output buffer pointers */
4927c478bd9Sstevel@tonic-gate 	int c;		/* character counter */
4937c478bd9Sstevel@tonic-gate 	int ic;		/* input character */
4947c478bd9Sstevel@tonic-gate 	int conv;		/* conversion option code */
4957c478bd9Sstevel@tonic-gate 	int trunc;		/* whether output file is truncated */
4967c478bd9Sstevel@tonic-gate 	struct stat file_stat;
49719d32b9aSRobert Mustacchi 	struct sigaction sact;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/* Set option defaults */
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	ibs = BSIZE;
5027c478bd9Sstevel@tonic-gate 	obs = BSIZE;
5037c478bd9Sstevel@tonic-gate 	files = 1;
5047c478bd9Sstevel@tonic-gate 	conv = COPY;
5057c478bd9Sstevel@tonic-gate 	trunc = 1;			/* default: truncate output file */
5067c478bd9Sstevel@tonic-gate 	trantype = SVR4XLATE;  /* use SVR4 EBCDIC by default */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* Parse command options */
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5117c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
5127c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
5137c478bd9Sstevel@tonic-gate #endif
5147c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "")) != EOF)
5177c478bd9Sstevel@tonic-gate 		switch (c) {
5187c478bd9Sstevel@tonic-gate 			case '?':
5197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE);
5207c478bd9Sstevel@tonic-gate 			exit(2);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/* not getopt()'ed because dd has no options but only operand(s) */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	for (c = optind; c < argc; c++)
5267c478bd9Sstevel@tonic-gate 	{
5277c478bd9Sstevel@tonic-gate 		string = argv[c];
5287c478bd9Sstevel@tonic-gate 		if (match("ibs="))
5297c478bd9Sstevel@tonic-gate 		{
5307c478bd9Sstevel@tonic-gate 			ibs = (unsigned)number(BIG);
5317c478bd9Sstevel@tonic-gate 			continue;
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		if (match("obs="))
5347c478bd9Sstevel@tonic-gate 		{
5357c478bd9Sstevel@tonic-gate 			obs = (unsigned)number(BIG);
5367c478bd9Sstevel@tonic-gate 			continue;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		if (match("cbs="))
5397c478bd9Sstevel@tonic-gate 		{
5407c478bd9Sstevel@tonic-gate 			cbs = (unsigned)number(BIG);
5417c478bd9Sstevel@tonic-gate 			continue;
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		if (match("bs="))
5447c478bd9Sstevel@tonic-gate 		{
5457c478bd9Sstevel@tonic-gate 			bs = (unsigned)number(BIG);
5467c478bd9Sstevel@tonic-gate 			continue;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		if (match("if="))
5497c478bd9Sstevel@tonic-gate 		{
5507c478bd9Sstevel@tonic-gate 			ifile = string;
5517c478bd9Sstevel@tonic-gate 			continue;
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 		if (match("of="))
5547c478bd9Sstevel@tonic-gate 		{
5557c478bd9Sstevel@tonic-gate 			ofile = string;
5567c478bd9Sstevel@tonic-gate 			continue;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 		if (match("skip="))
5597c478bd9Sstevel@tonic-gate 		{
5607c478bd9Sstevel@tonic-gate 			skip = number(BIG);
5617c478bd9Sstevel@tonic-gate 			continue;
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		if (match("iseek="))
5647c478bd9Sstevel@tonic-gate 		{
5657c478bd9Sstevel@tonic-gate 			iseekn = number(BIG);
5667c478bd9Sstevel@tonic-gate 			continue;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		if (match("oseek="))
5697c478bd9Sstevel@tonic-gate 		{
5707c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
5717c478bd9Sstevel@tonic-gate 			continue;
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 		if (match("seek="))		/* retained for compatibility */
5747c478bd9Sstevel@tonic-gate 		{
5757c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
5767c478bd9Sstevel@tonic-gate 			continue;
5777c478bd9Sstevel@tonic-gate 		}
578*61304e4fSPaul Dagnelie 		if (match("ostride="))
579*61304e4fSPaul Dagnelie 		{
580*61304e4fSPaul Dagnelie 			ostriden = ((off_t)number(BIG)) - 1;
581*61304e4fSPaul Dagnelie 			continue;
582*61304e4fSPaul Dagnelie 		}
583*61304e4fSPaul Dagnelie 		if (match("istride="))
584*61304e4fSPaul Dagnelie 		{
585*61304e4fSPaul Dagnelie 			istriden = ((off_t)number(BIG)) - 1;
586*61304e4fSPaul Dagnelie 			continue;
587*61304e4fSPaul Dagnelie 		}
588*61304e4fSPaul Dagnelie 		if (match("stride="))
589*61304e4fSPaul Dagnelie 		{
590*61304e4fSPaul Dagnelie 			istriden = ostriden = ((off_t)number(BIG)) - 1;
591*61304e4fSPaul Dagnelie 			continue;
592*61304e4fSPaul Dagnelie 		}
5937c478bd9Sstevel@tonic-gate 		if (match("count="))
5947c478bd9Sstevel@tonic-gate 		{
5957c478bd9Sstevel@tonic-gate 			count = number(BIG);
596e3bee069SRobert Mustacchi 			ecount = B_TRUE;
5977c478bd9Sstevel@tonic-gate 			continue;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		if (match("files="))
6007c478bd9Sstevel@tonic-gate 		{
6017c478bd9Sstevel@tonic-gate 			files = (int)number(BIG);
6027c478bd9Sstevel@tonic-gate 			continue;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		if (match("conv="))
6057c478bd9Sstevel@tonic-gate 		{
6067c478bd9Sstevel@tonic-gate 			for (;;)
6077c478bd9Sstevel@tonic-gate 			{
6087c478bd9Sstevel@tonic-gate 				if (match(","))
6097c478bd9Sstevel@tonic-gate 				{
6107c478bd9Sstevel@tonic-gate 					continue;
6117c478bd9Sstevel@tonic-gate 				}
6127c478bd9Sstevel@tonic-gate 				if (*string == '\0')
6137c478bd9Sstevel@tonic-gate 				{
6147c478bd9Sstevel@tonic-gate 					break;
6157c478bd9Sstevel@tonic-gate 				}
6167c478bd9Sstevel@tonic-gate 				if (match("block"))
6177c478bd9Sstevel@tonic-gate 				{
6187c478bd9Sstevel@tonic-gate 					conv = BLOCK;
6197c478bd9Sstevel@tonic-gate 					continue;
6207c478bd9Sstevel@tonic-gate 				}
6217c478bd9Sstevel@tonic-gate 				if (match("unblock"))
6227c478bd9Sstevel@tonic-gate 				{
6237c478bd9Sstevel@tonic-gate 					conv = UNBLOCK;
6247c478bd9Sstevel@tonic-gate 					continue;
6257c478bd9Sstevel@tonic-gate 				}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 				/* ebcdicb, ibmb, and asciib must precede */
6287c478bd9Sstevel@tonic-gate 				/* ebcdic, ibm, and ascii in this test */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 				if (match("ebcdicb"))
6317c478bd9Sstevel@tonic-gate 				{
6327c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6337c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6347c478bd9Sstevel@tonic-gate 					continue;
6357c478bd9Sstevel@tonic-gate 				}
6367c478bd9Sstevel@tonic-gate 				if (match("ibmb"))
6377c478bd9Sstevel@tonic-gate 				{
6387c478bd9Sstevel@tonic-gate 					conv = IBM;
6397c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6407c478bd9Sstevel@tonic-gate 					continue;
6417c478bd9Sstevel@tonic-gate 				}
6427c478bd9Sstevel@tonic-gate 				if (match("asciib"))
6437c478bd9Sstevel@tonic-gate 				{
6447c478bd9Sstevel@tonic-gate 					conv = ASCII;
6457c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6467c478bd9Sstevel@tonic-gate 					continue;
6477c478bd9Sstevel@tonic-gate 				}
6487c478bd9Sstevel@tonic-gate 				if (match("ebcdic"))
6497c478bd9Sstevel@tonic-gate 				{
6507c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6517c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6527c478bd9Sstevel@tonic-gate 					continue;
6537c478bd9Sstevel@tonic-gate 				}
6547c478bd9Sstevel@tonic-gate 				if (match("ibm"))
6557c478bd9Sstevel@tonic-gate 				{
6567c478bd9Sstevel@tonic-gate 					conv = IBM;
6577c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6587c478bd9Sstevel@tonic-gate 					continue;
6597c478bd9Sstevel@tonic-gate 				}
6607c478bd9Sstevel@tonic-gate 				if (match("ascii"))
6617c478bd9Sstevel@tonic-gate 				{
6627c478bd9Sstevel@tonic-gate 					conv = ASCII;
6637c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6647c478bd9Sstevel@tonic-gate 					continue;
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 				if (match("lcase"))
6677c478bd9Sstevel@tonic-gate 				{
6687c478bd9Sstevel@tonic-gate 					cflag |= LCASE;
6697c478bd9Sstevel@tonic-gate 					continue;
6707c478bd9Sstevel@tonic-gate 				}
6717c478bd9Sstevel@tonic-gate 				if (match("ucase"))
6727c478bd9Sstevel@tonic-gate 				{
6737c478bd9Sstevel@tonic-gate 					cflag |= UCASE;
6747c478bd9Sstevel@tonic-gate 					continue;
6757c478bd9Sstevel@tonic-gate 				}
6767c478bd9Sstevel@tonic-gate 				if (match("swab"))
6777c478bd9Sstevel@tonic-gate 				{
6787c478bd9Sstevel@tonic-gate 					cflag |= SWAB;
6797c478bd9Sstevel@tonic-gate 					continue;
6807c478bd9Sstevel@tonic-gate 				}
6817c478bd9Sstevel@tonic-gate 				if (match("noerror"))
6827c478bd9Sstevel@tonic-gate 				{
6837c478bd9Sstevel@tonic-gate 					cflag |= NERR;
6847c478bd9Sstevel@tonic-gate 					continue;
6857c478bd9Sstevel@tonic-gate 				}
6867c478bd9Sstevel@tonic-gate 				if (match("notrunc"))
6877c478bd9Sstevel@tonic-gate 				{
6887c478bd9Sstevel@tonic-gate 					trunc = 0;
6897c478bd9Sstevel@tonic-gate 					continue;
6907c478bd9Sstevel@tonic-gate 				}
6917c478bd9Sstevel@tonic-gate 				if (match("sync"))
6927c478bd9Sstevel@tonic-gate 				{
6937c478bd9Sstevel@tonic-gate 					cflag |= SYNC;
6947c478bd9Sstevel@tonic-gate 					continue;
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 				goto badarg;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 			continue;
6997c478bd9Sstevel@tonic-gate 		}
70019d32b9aSRobert Mustacchi 		if (match("oflag="))
70119d32b9aSRobert Mustacchi 		{
70219d32b9aSRobert Mustacchi 			for (;;)
70319d32b9aSRobert Mustacchi 			{
70419d32b9aSRobert Mustacchi 				if (match(","))
70519d32b9aSRobert Mustacchi 				{
70619d32b9aSRobert Mustacchi 					continue;
70719d32b9aSRobert Mustacchi 				}
70819d32b9aSRobert Mustacchi 				if (*string == '\0')
70919d32b9aSRobert Mustacchi 				{
71019d32b9aSRobert Mustacchi 					break;
71119d32b9aSRobert Mustacchi 				}
71219d32b9aSRobert Mustacchi 				if (match("dsync"))
71319d32b9aSRobert Mustacchi 				{
71419d32b9aSRobert Mustacchi 					oflag |= O_DSYNC;
71519d32b9aSRobert Mustacchi 					continue;
71619d32b9aSRobert Mustacchi 				}
71719d32b9aSRobert Mustacchi 				if (match("sync"))
71819d32b9aSRobert Mustacchi 				{
71919d32b9aSRobert Mustacchi 					oflag |= O_SYNC;
72019d32b9aSRobert Mustacchi 					continue;
72119d32b9aSRobert Mustacchi 				}
72219d32b9aSRobert Mustacchi 				goto badarg;
72319d32b9aSRobert Mustacchi 			}
72419d32b9aSRobert Mustacchi 			continue;
72519d32b9aSRobert Mustacchi 		}
7267c478bd9Sstevel@tonic-gate 		badarg:
7277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s \"%s\"\n",
7287c478bd9Sstevel@tonic-gate 			gettext("bad argument:"), string);
7297c478bd9Sstevel@tonic-gate 		exit(2);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* Perform consistency checks on options, decode strange conventions */
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (bs)
7357c478bd9Sstevel@tonic-gate 	{
7367c478bd9Sstevel@tonic-gate 		ibs = obs = bs;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	if ((ibs == 0) || (obs == 0))
7397c478bd9Sstevel@tonic-gate 	{
7407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s\n",
7417c478bd9Sstevel@tonic-gate 			gettext("buffer sizes cannot be zero"));
7427c478bd9Sstevel@tonic-gate 		exit(2);
7437c478bd9Sstevel@tonic-gate 	}
744*61304e4fSPaul Dagnelie 	if (ostriden == (off_t)-1) {
745*61304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
746*61304e4fSPaul Dagnelie 			gettext("stride must be greater than zero"));
747*61304e4fSPaul Dagnelie 		exit(2);
748*61304e4fSPaul Dagnelie 	}
749*61304e4fSPaul Dagnelie 	if (istriden == (off_t)-1) {
750*61304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
751*61304e4fSPaul Dagnelie 			gettext("stride must be greater than zero"));
752*61304e4fSPaul Dagnelie 		exit(2);
753*61304e4fSPaul Dagnelie 	}
7547c478bd9Sstevel@tonic-gate 	if (conv == COPY)
7557c478bd9Sstevel@tonic-gate 	{
7567c478bd9Sstevel@tonic-gate 		if ((bs == 0) || (cflag&(LCASE|UCASE)))
7577c478bd9Sstevel@tonic-gate 		{
7587c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	if (cbs == 0)
7627c478bd9Sstevel@tonic-gate 	{
7637c478bd9Sstevel@tonic-gate 		switch (conv)
7647c478bd9Sstevel@tonic-gate 		{
7657c478bd9Sstevel@tonic-gate 		case BLOCK:
7667c478bd9Sstevel@tonic-gate 		case UNBLOCK:
7677c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7687c478bd9Sstevel@tonic-gate 			break;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		case ASCII:
7717c478bd9Sstevel@tonic-gate 			conv = NBASCII;
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		case EBCDIC:
7757c478bd9Sstevel@tonic-gate 			conv = NBEBCDIC;
7767c478bd9Sstevel@tonic-gate 			break;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		case IBM:
7797c478bd9Sstevel@tonic-gate 			conv = NBIBM;
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		}
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/* Expand options into lower and upper case versions if necessary */
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	switch (conv)
7877c478bd9Sstevel@tonic-gate 	{
7887c478bd9Sstevel@tonic-gate 	case REBLOCK:
7897c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
7907c478bd9Sstevel@tonic-gate 			conv = LCREBLOCK;
7917c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
7927c478bd9Sstevel@tonic-gate 			conv = UCREBLOCK;
7937c478bd9Sstevel@tonic-gate 		break;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	case UNBLOCK:
7967c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
7977c478bd9Sstevel@tonic-gate 			conv = LCUNBLOCK;
7987c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
7997c478bd9Sstevel@tonic-gate 			conv = UCUNBLOCK;
8007c478bd9Sstevel@tonic-gate 		break;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	case BLOCK:
8037c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8047c478bd9Sstevel@tonic-gate 			conv = LCBLOCK;
8057c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8067c478bd9Sstevel@tonic-gate 			conv = UCBLOCK;
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	case ASCII:
8107c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8117c478bd9Sstevel@tonic-gate 			conv = LCASCII;
8127c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8137c478bd9Sstevel@tonic-gate 			conv = UCASCII;
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	case NBASCII:
8177c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8187c478bd9Sstevel@tonic-gate 			conv = LCNBASCII;
8197c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8207c478bd9Sstevel@tonic-gate 			conv = UCNBASCII;
8217c478bd9Sstevel@tonic-gate 		break;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	case EBCDIC:
8247c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8257c478bd9Sstevel@tonic-gate 			conv = LCEBCDIC;
8267c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8277c478bd9Sstevel@tonic-gate 			conv = UCEBCDIC;
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	case NBEBCDIC:
8317c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8327c478bd9Sstevel@tonic-gate 			conv = LCNBEBCDIC;
8337c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8347c478bd9Sstevel@tonic-gate 			conv = UCNBEBCDIC;
8357c478bd9Sstevel@tonic-gate 		break;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	case IBM:
8387c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8397c478bd9Sstevel@tonic-gate 			conv = LCIBM;
8407c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8417c478bd9Sstevel@tonic-gate 			conv = UCIBM;
8427c478bd9Sstevel@tonic-gate 		break;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	case NBIBM:
8457c478bd9Sstevel@tonic-gate 		if (cflag&LCASE)
8467c478bd9Sstevel@tonic-gate 			conv = LCNBIBM;
8477c478bd9Sstevel@tonic-gate 		else if (cflag&UCASE)
8487c478bd9Sstevel@tonic-gate 			conv = UCNBIBM;
8497c478bd9Sstevel@tonic-gate 		break;
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/* If BSD-compatible translation is selected, change the tables */
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (trantype == BSDXLATE) {
8557c478bd9Sstevel@tonic-gate 		atoe = bsd_atoe;
8567c478bd9Sstevel@tonic-gate 		atoibm = bsd_atoibm;
8577c478bd9Sstevel@tonic-gate 		etoa = bsd_etoa;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	/* Open the input file, or duplicate standard input */
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	ibf = -1;
8627c478bd9Sstevel@tonic-gate 	if (ifile)
8637c478bd9Sstevel@tonic-gate 	{
8647c478bd9Sstevel@tonic-gate 		ibf = open(ifile, 0);
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	else
8677c478bd9Sstevel@tonic-gate 	{
8687c478bd9Sstevel@tonic-gate 		ifile = "";
8697c478bd9Sstevel@tonic-gate 		ibf = dup(0);
8707c478bd9Sstevel@tonic-gate 	}
87119d32b9aSRobert Mustacchi 
8727c478bd9Sstevel@tonic-gate 	if (ibf == -1)
8737c478bd9Sstevel@tonic-gate 	{
8747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ifile);
8757c478bd9Sstevel@tonic-gate 		perror("open");
8767c478bd9Sstevel@tonic-gate 		exit(2);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/* Open the output file, or duplicate standard output */
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	obf = -1;
8827c478bd9Sstevel@tonic-gate 	if (ofile)
8837c478bd9Sstevel@tonic-gate 	{
8847c478bd9Sstevel@tonic-gate 		if (trunc == 0)	/* do not truncate output file */
88519d32b9aSRobert Mustacchi 			obf = open(ofile, (O_WRONLY|O_CREAT|oflag),
8867c478bd9Sstevel@tonic-gate 			(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
8877c478bd9Sstevel@tonic-gate 		else if (oseekn && (trunc == 1))
8887c478bd9Sstevel@tonic-gate 		{
88919d32b9aSRobert Mustacchi 			obf = open(ofile, O_WRONLY|O_CREAT|oflag,
8907c478bd9Sstevel@tonic-gate 			(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
8917c478bd9Sstevel@tonic-gate 			if (obf == -1)
8927c478bd9Sstevel@tonic-gate 			{
8937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s: ", ofile);
8947c478bd9Sstevel@tonic-gate 				perror("open");
8957c478bd9Sstevel@tonic-gate 				exit(2);
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 			(void) fstat(obf, &file_stat);
8987c478bd9Sstevel@tonic-gate 			if (((file_stat.st_mode & S_IFMT) == S_IFREG) &&
8997c478bd9Sstevel@tonic-gate 			    (ftruncate(obf, (((off_t)oseekn) * ((off_t)obs)))
9007c478bd9Sstevel@tonic-gate 				== -1))
9017c478bd9Sstevel@tonic-gate 			{
9027c478bd9Sstevel@tonic-gate 				perror("ftruncate");
9037c478bd9Sstevel@tonic-gate 				exit(2);
9047c478bd9Sstevel@tonic-gate 			}
9057c478bd9Sstevel@tonic-gate 		}
9067c478bd9Sstevel@tonic-gate 		else
90719d32b9aSRobert Mustacchi 			obf = open(ofile, O_WRONLY|O_CREAT|O_TRUNC|oflag,
9087c478bd9Sstevel@tonic-gate 			(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 	else
9117c478bd9Sstevel@tonic-gate 	{
9127c478bd9Sstevel@tonic-gate 		ofile = "";
9137c478bd9Sstevel@tonic-gate 		obf = dup(1);
9147c478bd9Sstevel@tonic-gate 	}
91519d32b9aSRobert Mustacchi 
9167c478bd9Sstevel@tonic-gate 	if (obf == -1)
9177c478bd9Sstevel@tonic-gate 	{
9187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ofile);
9197c478bd9Sstevel@tonic-gate 		perror("open");
9207c478bd9Sstevel@tonic-gate 		exit(2);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/* Expand memory to get an input buffer */
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	ibuf = (unsigned char *)valloc(ibs + 10);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/* If no conversions, the input buffer is the output buffer */
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (conv == COPY)
9307c478bd9Sstevel@tonic-gate 	{
9317c478bd9Sstevel@tonic-gate 		obuf = ibuf;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	/* Expand memory to get an output buffer. Leave enough room at the */
9357c478bd9Sstevel@tonic-gate 	/* end to convert a logical record when doing block conversions. */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	else
9387c478bd9Sstevel@tonic-gate 	{
9397c478bd9Sstevel@tonic-gate 		obuf = (unsigned char *)valloc(obs + cbs + 10);
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 	if ((ibuf == (unsigned char *)NULL) || (obuf == (unsigned char *)NULL))
9427c478bd9Sstevel@tonic-gate 	{
9437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9447c478bd9Sstevel@tonic-gate 			"dd: %s\n", gettext("not enough memory"));
9457c478bd9Sstevel@tonic-gate 		exit(2);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
94819d32b9aSRobert Mustacchi 	/*
94919d32b9aSRobert Mustacchi 	 * Enable a statistics message when we terminate on SIGINT
95019d32b9aSRobert Mustacchi 	 * Also enable it to be queried via SIGINFO and SIGUSR1
95119d32b9aSRobert Mustacchi 	 */
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
9547c478bd9Sstevel@tonic-gate 	{
9557c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, term);
9567c478bd9Sstevel@tonic-gate 	}
95719d32b9aSRobert Mustacchi 
95819d32b9aSRobert Mustacchi 	bzero(&sact, sizeof (struct sigaction));
95919d32b9aSRobert Mustacchi 	sact.sa_flags = SA_SIGINFO;
96019d32b9aSRobert Mustacchi 	sact.sa_sigaction = siginfo_handler;
96119d32b9aSRobert Mustacchi 	(void) sigemptyset(&sact.sa_mask);
96219d32b9aSRobert Mustacchi 	if (sigaction(SIGINFO, &sact, NULL) != 0) {
96319d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
96419d32b9aSRobert Mustacchi 		    gettext("failed to enable siginfo handler"),
96519d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
96619d32b9aSRobert Mustacchi 		exit(2);
96719d32b9aSRobert Mustacchi 	}
96819d32b9aSRobert Mustacchi 	if (sigaction(SIGUSR1, &sact, NULL) != 0) {
96919d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
97019d32b9aSRobert Mustacchi 		    gettext("failed to enable sigusr1 handler"),
97119d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
97219d32b9aSRobert Mustacchi 		exit(2);
97319d32b9aSRobert Mustacchi 	}
97419d32b9aSRobert Mustacchi 
9757c478bd9Sstevel@tonic-gate 	/* Skip input blocks */
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	while (skip)
9787c478bd9Sstevel@tonic-gate 	{
9797c478bd9Sstevel@tonic-gate 		ibc = read(ibf, (char *)ibuf, ibs);
9807c478bd9Sstevel@tonic-gate 		if (ibc == (unsigned)-1)
9817c478bd9Sstevel@tonic-gate 		{
9827c478bd9Sstevel@tonic-gate 			if (++nbad > BADLIMIT)
9837c478bd9Sstevel@tonic-gate 			{
9847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
9857c478bd9Sstevel@tonic-gate 					gettext("skip failed"));
9867c478bd9Sstevel@tonic-gate 				exit(2);
9877c478bd9Sstevel@tonic-gate 			}
9887c478bd9Sstevel@tonic-gate 			else
9897c478bd9Sstevel@tonic-gate 			{
9907c478bd9Sstevel@tonic-gate 				perror("read");
9917c478bd9Sstevel@tonic-gate 			}
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		else
9947c478bd9Sstevel@tonic-gate 		{
9957c478bd9Sstevel@tonic-gate 			if (ibc == 0)
9967c478bd9Sstevel@tonic-gate 			{
9977c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
9987c478bd9Sstevel@tonic-gate 				gettext("cannot skip past end-of-file"));
9997c478bd9Sstevel@tonic-gate 				exit(3);
10007c478bd9Sstevel@tonic-gate 			}
10017c478bd9Sstevel@tonic-gate 			else
10027c478bd9Sstevel@tonic-gate 			{
10037c478bd9Sstevel@tonic-gate 				nbad = 0;
10047c478bd9Sstevel@tonic-gate 			}
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		skip--;
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	/* Seek past input blocks */
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if (iseekn && lseek(ibf, (((off_t)iseekn) * ((off_t)ibs)), 1) == -1)
10127c478bd9Sstevel@tonic-gate 	{
10137c478bd9Sstevel@tonic-gate 		perror("lseek");
10147c478bd9Sstevel@tonic-gate 		exit(2);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/* Seek past output blocks */
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (oseekn && lseek(obf, (((off_t)oseekn) * ((off_t)obs)), 1) == -1)
10207c478bd9Sstevel@tonic-gate 	{
10217c478bd9Sstevel@tonic-gate 		perror("lseek");
10227c478bd9Sstevel@tonic-gate 		exit(2);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/* Initialize all buffer pointers */
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	skipf = 0;	/* not skipping an input line */
10287c478bd9Sstevel@tonic-gate 	ibc = 0;	/* no input characters yet */
10297c478bd9Sstevel@tonic-gate 	obc = 0;	/* no output characters yet */
10307c478bd9Sstevel@tonic-gate 	cbc = 0;	/* the conversion buffer is empty */
10317c478bd9Sstevel@tonic-gate 	op = obuf;	/* point to the output buffer */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/* Read and convert input blocks until end of file(s) */
10347c478bd9Sstevel@tonic-gate 
103519d32b9aSRobert Mustacchi 	/* Grab our start time for siginfo purposes */
103619d32b9aSRobert Mustacchi 	startt = gethrtime();
103719d32b9aSRobert Mustacchi 
10387c478bd9Sstevel@tonic-gate 	for (;;)
10397c478bd9Sstevel@tonic-gate 	{
104019d32b9aSRobert Mustacchi 		if (nstats != 0) {
104119d32b9aSRobert Mustacchi 			stats();
104219d32b9aSRobert Mustacchi 			nstats = 0;
104319d32b9aSRobert Mustacchi 		}
104419d32b9aSRobert Mustacchi 
1045e3bee069SRobert Mustacchi 		if ((count == 0 && ecount == B_FALSE) || (nifr+nipr < count)) {
10467c478bd9Sstevel@tonic-gate 		/* If proceed on error is enabled, zero the input buffer */
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 			if (cflag&NERR)
10497c478bd9Sstevel@tonic-gate 			{
10507c478bd9Sstevel@tonic-gate 				ip = ibuf + ibs;
10517c478bd9Sstevel@tonic-gate 				c = ibs;
10527c478bd9Sstevel@tonic-gate 				if (c & 1)	/* if the size is odd, */
10537c478bd9Sstevel@tonic-gate 				{
10547c478bd9Sstevel@tonic-gate 					*--ip = 0;	/* clear the odd byte */
10557c478bd9Sstevel@tonic-gate 				}
10567c478bd9Sstevel@tonic-gate 				if (c >>= 1)		/* divide by two */
10577c478bd9Sstevel@tonic-gate 				{
10587c478bd9Sstevel@tonic-gate 					do {	/* clear two at a time */
10597c478bd9Sstevel@tonic-gate 						*--ip = 0;
10607c478bd9Sstevel@tonic-gate 						*--ip = 0;
10617c478bd9Sstevel@tonic-gate 					} while (--c);
10627c478bd9Sstevel@tonic-gate 				}
10637c478bd9Sstevel@tonic-gate 			}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 			/* Read the next input block */
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 			ibc = read(ibf, (char *)ibuf, ibs);
10687c478bd9Sstevel@tonic-gate 
1069*61304e4fSPaul Dagnelie 			if (istriden > 0 && lseek(ibf, istriden * ((off_t)ibs),
1070*61304e4fSPaul Dagnelie 			    SEEK_CUR) == -1) {
1071*61304e4fSPaul Dagnelie 				perror("lseek");
1072*61304e4fSPaul Dagnelie 				exit(2);
1073*61304e4fSPaul Dagnelie 			}
1074*61304e4fSPaul Dagnelie 
10757c478bd9Sstevel@tonic-gate 			/* Process input errors */
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 			if (ibc == (unsigned)-1)
10787c478bd9Sstevel@tonic-gate 			{
10797c478bd9Sstevel@tonic-gate 				perror("read");
10807c478bd9Sstevel@tonic-gate 				if (((cflag&NERR) == 0) || (++nbad > BADLIMIT))
10817c478bd9Sstevel@tonic-gate 				{
10827c478bd9Sstevel@tonic-gate 					while (obc)
10837c478bd9Sstevel@tonic-gate 					{
10847c478bd9Sstevel@tonic-gate 						(void) flsh();
10857c478bd9Sstevel@tonic-gate 					}
10867c478bd9Sstevel@tonic-gate 					term(2);
10877c478bd9Sstevel@tonic-gate 				}
10887c478bd9Sstevel@tonic-gate 				else
10897c478bd9Sstevel@tonic-gate 				{
10907c478bd9Sstevel@tonic-gate 					stats();
10917c478bd9Sstevel@tonic-gate 					ibc = ibs; /* assume a full block */
10927c478bd9Sstevel@tonic-gate 				}
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 			else
10957c478bd9Sstevel@tonic-gate 			{
10967c478bd9Sstevel@tonic-gate 				nbad = 0;
10977c478bd9Sstevel@tonic-gate 			}
10987c478bd9Sstevel@tonic-gate 		}
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/* Record count satisfied, simulate end of file */
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		else
11037c478bd9Sstevel@tonic-gate 		{
11047c478bd9Sstevel@tonic-gate 			ibc = 0;
11057c478bd9Sstevel@tonic-gate 			files = 1;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		/* Process end of file */
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		if (ibc == 0)
11117c478bd9Sstevel@tonic-gate 		{
11127c478bd9Sstevel@tonic-gate 			switch (conv)
11137c478bd9Sstevel@tonic-gate 			{
11147c478bd9Sstevel@tonic-gate 			case UNBLOCK:
11157c478bd9Sstevel@tonic-gate 			case LCUNBLOCK:
11167c478bd9Sstevel@tonic-gate 			case UCUNBLOCK:
11177c478bd9Sstevel@tonic-gate 			case ASCII:
11187c478bd9Sstevel@tonic-gate 			case LCASCII:
11197c478bd9Sstevel@tonic-gate 			case UCASCII:
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 				/* Trim trailing blanks from the last line */
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 				if ((c = cbc) != 0)
11247c478bd9Sstevel@tonic-gate 				{
11257c478bd9Sstevel@tonic-gate 					do {
11267c478bd9Sstevel@tonic-gate 						if ((*--op) != ' ')
11277c478bd9Sstevel@tonic-gate 						{
11287c478bd9Sstevel@tonic-gate 							op++;
11297c478bd9Sstevel@tonic-gate 							break;
11307c478bd9Sstevel@tonic-gate 						}
11317c478bd9Sstevel@tonic-gate 					} while (--c);
11327c478bd9Sstevel@tonic-gate 					*op++ = '\n';
11337c478bd9Sstevel@tonic-gate 					obc -= cbc - c - 1;
11347c478bd9Sstevel@tonic-gate 					cbc = 0;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 					/* Flush the output buffer if full */
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 					while (obc >= obs)
11397c478bd9Sstevel@tonic-gate 					{
11407c478bd9Sstevel@tonic-gate 						op = flsh();
11417c478bd9Sstevel@tonic-gate 					}
11427c478bd9Sstevel@tonic-gate 				}
11437c478bd9Sstevel@tonic-gate 				break;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 			case BLOCK:
11467c478bd9Sstevel@tonic-gate 			case LCBLOCK:
11477c478bd9Sstevel@tonic-gate 			case UCBLOCK:
11487c478bd9Sstevel@tonic-gate 			case EBCDIC:
11497c478bd9Sstevel@tonic-gate 			case LCEBCDIC:
11507c478bd9Sstevel@tonic-gate 			case UCEBCDIC:
11517c478bd9Sstevel@tonic-gate 			case IBM:
11527c478bd9Sstevel@tonic-gate 			case LCIBM:
11537c478bd9Sstevel@tonic-gate 			case UCIBM:
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 			/* Pad trailing blanks if the last line is short */
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 				if (cbc)
11587c478bd9Sstevel@tonic-gate 				{
11597c478bd9Sstevel@tonic-gate 					obc += c = cbs - cbc;
11607c478bd9Sstevel@tonic-gate 					cbc = 0;
11617c478bd9Sstevel@tonic-gate 					if (c > 0)
11627c478bd9Sstevel@tonic-gate 					{
11637c478bd9Sstevel@tonic-gate 					/* Use the right kind of blank */
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 						switch (conv)
11667c478bd9Sstevel@tonic-gate 						{
11677c478bd9Sstevel@tonic-gate 						case BLOCK:
11687c478bd9Sstevel@tonic-gate 						case LCBLOCK:
11697c478bd9Sstevel@tonic-gate 						case UCBLOCK:
11707c478bd9Sstevel@tonic-gate 							ic = ' ';
11717c478bd9Sstevel@tonic-gate 							break;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 						case EBCDIC:
11747c478bd9Sstevel@tonic-gate 						case LCEBCDIC:
11757c478bd9Sstevel@tonic-gate 						case UCEBCDIC:
11767c478bd9Sstevel@tonic-gate 							ic = atoe[' '];
11777c478bd9Sstevel@tonic-gate 							break;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 						case IBM:
11807c478bd9Sstevel@tonic-gate 						case LCIBM:
11817c478bd9Sstevel@tonic-gate 						case UCIBM:
11827c478bd9Sstevel@tonic-gate 							ic = atoibm[' '];
11837c478bd9Sstevel@tonic-gate 							break;
11847c478bd9Sstevel@tonic-gate 						}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 						/* Pad with trailing blanks */
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 						do {
11897c478bd9Sstevel@tonic-gate 							*op++ = ic;
11907c478bd9Sstevel@tonic-gate 						} while (--c);
11917c478bd9Sstevel@tonic-gate 					}
11927c478bd9Sstevel@tonic-gate 				}
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 				/* Flush the output buffer if full */
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 				while (obc >= obs)
11987c478bd9Sstevel@tonic-gate 				{
11997c478bd9Sstevel@tonic-gate 					op = flsh();
12007c478bd9Sstevel@tonic-gate 				}
12017c478bd9Sstevel@tonic-gate 				break;
12027c478bd9Sstevel@tonic-gate 			}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 			/* If no more files to read, flush the output buffer */
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 			if (--files <= 0)
12077c478bd9Sstevel@tonic-gate 			{
12087c478bd9Sstevel@tonic-gate 				(void) flsh();
12097c478bd9Sstevel@tonic-gate 				if ((close(obf) != 0) || (fclose(stdout) != 0))
12107c478bd9Sstevel@tonic-gate 				{
12117c478bd9Sstevel@tonic-gate 					perror(gettext("dd: close error"));
12127c478bd9Sstevel@tonic-gate 					exit(2);
12137c478bd9Sstevel@tonic-gate 				}
12147c478bd9Sstevel@tonic-gate 				term(0);	/* successful exit */
12157c478bd9Sstevel@tonic-gate 			}
12167c478bd9Sstevel@tonic-gate 			else
12177c478bd9Sstevel@tonic-gate 			{
12187c478bd9Sstevel@tonic-gate 				continue;	/* read the next file */
12197c478bd9Sstevel@tonic-gate 			}
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		/* Normal read, check for special cases */
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		else if (ibc == ibs)
12257c478bd9Sstevel@tonic-gate 		{
12267c478bd9Sstevel@tonic-gate 			nifr++;		/* count another full input record */
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 		else
12297c478bd9Sstevel@tonic-gate 		{
12307c478bd9Sstevel@tonic-gate 			nipr++;		/* count a partial input record */
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			/* If `sync' enabled, pad nulls */
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 			if ((cflag&SYNC) && ((cflag&NERR) == 0))
12357c478bd9Sstevel@tonic-gate 			{
12367c478bd9Sstevel@tonic-gate 				c = ibs - ibc;
12377c478bd9Sstevel@tonic-gate 				ip = ibuf + ibs;
12387c478bd9Sstevel@tonic-gate 				do {
12397c478bd9Sstevel@tonic-gate 				if ((conv == BLOCK) || (conv == UNBLOCK))
12407c478bd9Sstevel@tonic-gate 					*--ip = ' ';
12417c478bd9Sstevel@tonic-gate 				else
12427c478bd9Sstevel@tonic-gate 					*--ip = '\0';
12437c478bd9Sstevel@tonic-gate 				} while (--c);
12447c478bd9Sstevel@tonic-gate 				ibc = ibs;
12457c478bd9Sstevel@tonic-gate 			}
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/* Swap the bytes in the input buffer if necessary */
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		if (cflag&SWAB)
12517c478bd9Sstevel@tonic-gate 		{
12527c478bd9Sstevel@tonic-gate 			ip = ibuf;
12537c478bd9Sstevel@tonic-gate 			if (ibc & 1)	/* if the byte count is odd, */
12547c478bd9Sstevel@tonic-gate 			{
12557c478bd9Sstevel@tonic-gate 				ip[ibc] = 0;  /* make it even, pad with zero */
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate 			c = ibc >> 1;	/* compute the pair count */
12587c478bd9Sstevel@tonic-gate 			do {
12597c478bd9Sstevel@tonic-gate 				ic = *ip++;
12607c478bd9Sstevel@tonic-gate 				ip[-1] = *ip;
12617c478bd9Sstevel@tonic-gate 				*ip++ = ic;
12627c478bd9Sstevel@tonic-gate 			} while (--c);		/* do two bytes at a time */
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 		/* Select the appropriate conversion loop */
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		ip = ibuf;
12687c478bd9Sstevel@tonic-gate 		switch (conv)
12697c478bd9Sstevel@tonic-gate 		{
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		/* Simple copy: no conversion, preserve the input block size */
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 		case COPY:
12747c478bd9Sstevel@tonic-gate 			obc = ibc;
12757c478bd9Sstevel@tonic-gate 			(void) flsh();
12767c478bd9Sstevel@tonic-gate 			break;
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		/* Simple copy: pack all output into equal sized blocks */
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 		case REBLOCK:
12817c478bd9Sstevel@tonic-gate 		case LCREBLOCK:
12827c478bd9Sstevel@tonic-gate 		case UCREBLOCK:
12837c478bd9Sstevel@tonic-gate 		case NBASCII:
12847c478bd9Sstevel@tonic-gate 		case LCNBASCII:
12857c478bd9Sstevel@tonic-gate 		case UCNBASCII:
12867c478bd9Sstevel@tonic-gate 		case NBEBCDIC:
12877c478bd9Sstevel@tonic-gate 		case LCNBEBCDIC:
12887c478bd9Sstevel@tonic-gate 		case UCNBEBCDIC:
12897c478bd9Sstevel@tonic-gate 		case NBIBM:
12907c478bd9Sstevel@tonic-gate 		case LCNBIBM:
12917c478bd9Sstevel@tonic-gate 		case UCNBIBM:
12927c478bd9Sstevel@tonic-gate 			while ((c = ibc) != 0)
12937c478bd9Sstevel@tonic-gate 			{
12947c478bd9Sstevel@tonic-gate 				if (c > (obs - obc))
12957c478bd9Sstevel@tonic-gate 				{
12967c478bd9Sstevel@tonic-gate 					c = obs - obc;
12977c478bd9Sstevel@tonic-gate 				}
12987c478bd9Sstevel@tonic-gate 				ibc -= c;
12997c478bd9Sstevel@tonic-gate 				obc += c;
13007c478bd9Sstevel@tonic-gate 				switch (conv)
13017c478bd9Sstevel@tonic-gate 				{
13027c478bd9Sstevel@tonic-gate 				case REBLOCK:
13037c478bd9Sstevel@tonic-gate 					do {
13047c478bd9Sstevel@tonic-gate 						*op++ = *ip++;
13057c478bd9Sstevel@tonic-gate 					} while (--c);
13067c478bd9Sstevel@tonic-gate 					break;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 				case LCREBLOCK:
13097c478bd9Sstevel@tonic-gate 					do {
13107c478bd9Sstevel@tonic-gate 						*op++ = utol[*ip++];
13117c478bd9Sstevel@tonic-gate 					} while (--c);
13127c478bd9Sstevel@tonic-gate 					break;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 				case UCREBLOCK:
13157c478bd9Sstevel@tonic-gate 					do {
13167c478bd9Sstevel@tonic-gate 						*op++ = ltou[*ip++];
13177c478bd9Sstevel@tonic-gate 					} while (--c);
13187c478bd9Sstevel@tonic-gate 					break;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 				case NBASCII:
13217c478bd9Sstevel@tonic-gate 					do {
13227c478bd9Sstevel@tonic-gate 						*op++ = etoa[*ip++];
13237c478bd9Sstevel@tonic-gate 					} while (--c);
13247c478bd9Sstevel@tonic-gate 					break;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 				case LCNBASCII:
13277c478bd9Sstevel@tonic-gate 					do {
13287c478bd9Sstevel@tonic-gate 						*op++ = utol[etoa[*ip++]];
13297c478bd9Sstevel@tonic-gate 					} while (--c);
13307c478bd9Sstevel@tonic-gate 					break;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 				case UCNBASCII:
13337c478bd9Sstevel@tonic-gate 					do {
13347c478bd9Sstevel@tonic-gate 						*op++ = ltou[etoa[*ip++]];
13357c478bd9Sstevel@tonic-gate 					} while (--c);
13367c478bd9Sstevel@tonic-gate 					break;
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 				case NBEBCDIC:
13397c478bd9Sstevel@tonic-gate 					do {
13407c478bd9Sstevel@tonic-gate 						*op++ = atoe[*ip++];
13417c478bd9Sstevel@tonic-gate 					} while (--c);
13427c478bd9Sstevel@tonic-gate 					break;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 				case LCNBEBCDIC:
13457c478bd9Sstevel@tonic-gate 					do {
13467c478bd9Sstevel@tonic-gate 						*op++ = atoe[utol[*ip++]];
13477c478bd9Sstevel@tonic-gate 					} while (--c);
13487c478bd9Sstevel@tonic-gate 					break;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 				case UCNBEBCDIC:
13517c478bd9Sstevel@tonic-gate 					do {
13527c478bd9Sstevel@tonic-gate 						*op++ = atoe[ltou[*ip++]];
13537c478bd9Sstevel@tonic-gate 					} while (--c);
13547c478bd9Sstevel@tonic-gate 					break;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 				case NBIBM:
13577c478bd9Sstevel@tonic-gate 					do {
13587c478bd9Sstevel@tonic-gate 						*op++ = atoibm[*ip++];
13597c478bd9Sstevel@tonic-gate 					} while (--c);
13607c478bd9Sstevel@tonic-gate 					break;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 				case LCNBIBM:
13637c478bd9Sstevel@tonic-gate 					do {
13647c478bd9Sstevel@tonic-gate 						*op++ = atoibm[utol[*ip++]];
13657c478bd9Sstevel@tonic-gate 					} while (--c);
13667c478bd9Sstevel@tonic-gate 					break;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 				case UCNBIBM:
13697c478bd9Sstevel@tonic-gate 					do {
13707c478bd9Sstevel@tonic-gate 						*op++ = atoibm[ltou[*ip++]];
13717c478bd9Sstevel@tonic-gate 					} while (--c);
13727c478bd9Sstevel@tonic-gate 					break;
13737c478bd9Sstevel@tonic-gate 				}
13747c478bd9Sstevel@tonic-gate 				if (obc >= obs)
13757c478bd9Sstevel@tonic-gate 				{
13767c478bd9Sstevel@tonic-gate 					op = flsh();
13777c478bd9Sstevel@tonic-gate 				}
13787c478bd9Sstevel@tonic-gate 			}
13797c478bd9Sstevel@tonic-gate 			break;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/* Convert from blocked records to lines terminated by newline */
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		case UNBLOCK:
13847c478bd9Sstevel@tonic-gate 		case LCUNBLOCK:
13857c478bd9Sstevel@tonic-gate 		case UCUNBLOCK:
13867c478bd9Sstevel@tonic-gate 		case ASCII:
13877c478bd9Sstevel@tonic-gate 		case LCASCII:
13887c478bd9Sstevel@tonic-gate 		case UCASCII:
13897c478bd9Sstevel@tonic-gate 			while ((c = ibc) != 0)
13907c478bd9Sstevel@tonic-gate 			{
13917c478bd9Sstevel@tonic-gate 				if (c > (cbs - cbc))
13927c478bd9Sstevel@tonic-gate 						/* if more than one record, */
13937c478bd9Sstevel@tonic-gate 				{
13947c478bd9Sstevel@tonic-gate 					c = cbs - cbc;
13957c478bd9Sstevel@tonic-gate 						/* only copy one record */
13967c478bd9Sstevel@tonic-gate 				}
13977c478bd9Sstevel@tonic-gate 				ibc -= c;
13987c478bd9Sstevel@tonic-gate 				cbc += c;
13997c478bd9Sstevel@tonic-gate 				obc += c;
14007c478bd9Sstevel@tonic-gate 				switch (conv)
14017c478bd9Sstevel@tonic-gate 				{
14027c478bd9Sstevel@tonic-gate 				case UNBLOCK:
14037c478bd9Sstevel@tonic-gate 					do {
14047c478bd9Sstevel@tonic-gate 						*op++ = *ip++;
14057c478bd9Sstevel@tonic-gate 					} while (--c);
14067c478bd9Sstevel@tonic-gate 					break;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 				case LCUNBLOCK:
14097c478bd9Sstevel@tonic-gate 					do {
14107c478bd9Sstevel@tonic-gate 						*op++ = utol[*ip++];
14117c478bd9Sstevel@tonic-gate 					} while (--c);
14127c478bd9Sstevel@tonic-gate 					break;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 				case UCUNBLOCK:
14157c478bd9Sstevel@tonic-gate 					do {
14167c478bd9Sstevel@tonic-gate 						*op++ = ltou[*ip++];
14177c478bd9Sstevel@tonic-gate 					} while (--c);
14187c478bd9Sstevel@tonic-gate 					break;
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 				case ASCII:
14217c478bd9Sstevel@tonic-gate 					do {
14227c478bd9Sstevel@tonic-gate 						*op++ = etoa[*ip++];
14237c478bd9Sstevel@tonic-gate 					} while (--c);
14247c478bd9Sstevel@tonic-gate 					break;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 				case LCASCII:
14277c478bd9Sstevel@tonic-gate 					do {
14287c478bd9Sstevel@tonic-gate 						*op++ = utol[etoa[*ip++]];
14297c478bd9Sstevel@tonic-gate 					} while (--c);
14307c478bd9Sstevel@tonic-gate 					break;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 				case UCASCII:
14337c478bd9Sstevel@tonic-gate 					do {
14347c478bd9Sstevel@tonic-gate 						*op++ = ltou[etoa[*ip++]];
14357c478bd9Sstevel@tonic-gate 					} while (--c);
14367c478bd9Sstevel@tonic-gate 					break;
14377c478bd9Sstevel@tonic-gate 				}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 				/* Trim trailing blanks if the line is full */
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 				if (cbc == cbs)
14427c478bd9Sstevel@tonic-gate 				{
14437c478bd9Sstevel@tonic-gate 					c = cbs; /* `do - while' is usually */
14447c478bd9Sstevel@tonic-gate 					do {		/* faster than `for' */
14457c478bd9Sstevel@tonic-gate 						if ((*--op) != ' ')
14467c478bd9Sstevel@tonic-gate 						{
14477c478bd9Sstevel@tonic-gate 							op++;
14487c478bd9Sstevel@tonic-gate 							break;
14497c478bd9Sstevel@tonic-gate 						}
14507c478bd9Sstevel@tonic-gate 					} while (--c);
14517c478bd9Sstevel@tonic-gate 					*op++ = '\n';
14527c478bd9Sstevel@tonic-gate 					obc -= cbs - c - 1;
14537c478bd9Sstevel@tonic-gate 					cbc = 0;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 					/* Flush the output buffer if full */
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 					while (obc >= obs)
14587c478bd9Sstevel@tonic-gate 					{
14597c478bd9Sstevel@tonic-gate 						op = flsh();
14607c478bd9Sstevel@tonic-gate 					}
14617c478bd9Sstevel@tonic-gate 				}
14627c478bd9Sstevel@tonic-gate 			}
14637c478bd9Sstevel@tonic-gate 			break;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		/* Convert to blocked records */
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 		case BLOCK:
14687c478bd9Sstevel@tonic-gate 		case LCBLOCK:
14697c478bd9Sstevel@tonic-gate 		case UCBLOCK:
14707c478bd9Sstevel@tonic-gate 		case EBCDIC:
14717c478bd9Sstevel@tonic-gate 		case LCEBCDIC:
14727c478bd9Sstevel@tonic-gate 		case UCEBCDIC:
14737c478bd9Sstevel@tonic-gate 		case IBM:
14747c478bd9Sstevel@tonic-gate 		case LCIBM:
14757c478bd9Sstevel@tonic-gate 		case UCIBM:
14767c478bd9Sstevel@tonic-gate 			while ((c = ibc) != 0)
14777c478bd9Sstevel@tonic-gate 			{
14787c478bd9Sstevel@tonic-gate 				int nlflag = 0;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			/* We may have to skip to the end of a long line */
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 				if (skipf)
14837c478bd9Sstevel@tonic-gate 				{
14847c478bd9Sstevel@tonic-gate 					do {
14857c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) == '\n')
14867c478bd9Sstevel@tonic-gate 						{
14877c478bd9Sstevel@tonic-gate 							skipf = 0;
14887c478bd9Sstevel@tonic-gate 							c--;
14897c478bd9Sstevel@tonic-gate 							break;
14907c478bd9Sstevel@tonic-gate 						}
14917c478bd9Sstevel@tonic-gate 					} while (--c);
14927c478bd9Sstevel@tonic-gate 					if ((ibc = c) == 0)
14937c478bd9Sstevel@tonic-gate 					{
14947c478bd9Sstevel@tonic-gate 						continue;
14957c478bd9Sstevel@tonic-gate 							/* read another block */
14967c478bd9Sstevel@tonic-gate 					}
14977c478bd9Sstevel@tonic-gate 				}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 				/* If anything left, copy until newline */
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 				if (c > (cbs - cbc + 1))
15027c478bd9Sstevel@tonic-gate 				{
15037c478bd9Sstevel@tonic-gate 					c = cbs - cbc + 1;
15047c478bd9Sstevel@tonic-gate 				}
15057c478bd9Sstevel@tonic-gate 				ibc -= c;
15067c478bd9Sstevel@tonic-gate 				cbc += c;
15077c478bd9Sstevel@tonic-gate 				obc += c;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 				switch (conv)
15107c478bd9Sstevel@tonic-gate 				{
15117c478bd9Sstevel@tonic-gate 				case BLOCK:
15127c478bd9Sstevel@tonic-gate 					do {
15137c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15147c478bd9Sstevel@tonic-gate 						{
15157c478bd9Sstevel@tonic-gate 							*op++ = ic;
15167c478bd9Sstevel@tonic-gate 						}
15177c478bd9Sstevel@tonic-gate 						else
15187c478bd9Sstevel@tonic-gate 						{
15197c478bd9Sstevel@tonic-gate 							nlflag = 1;
15207c478bd9Sstevel@tonic-gate 							break;
15217c478bd9Sstevel@tonic-gate 						}
15227c478bd9Sstevel@tonic-gate 					} while (--c);
15237c478bd9Sstevel@tonic-gate 					break;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 				case LCBLOCK:
15267c478bd9Sstevel@tonic-gate 					do {
15277c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15287c478bd9Sstevel@tonic-gate 						{
15297c478bd9Sstevel@tonic-gate 							*op++ = utol[ic];
15307c478bd9Sstevel@tonic-gate 						}
15317c478bd9Sstevel@tonic-gate 						else
15327c478bd9Sstevel@tonic-gate 						{
15337c478bd9Sstevel@tonic-gate 							nlflag = 1;
15347c478bd9Sstevel@tonic-gate 							break;
15357c478bd9Sstevel@tonic-gate 						}
15367c478bd9Sstevel@tonic-gate 					} while (--c);
15377c478bd9Sstevel@tonic-gate 					break;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 				case UCBLOCK:
15407c478bd9Sstevel@tonic-gate 					do {
15417c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15427c478bd9Sstevel@tonic-gate 						{
15437c478bd9Sstevel@tonic-gate 							*op++ = ltou[ic];
15447c478bd9Sstevel@tonic-gate 						}
15457c478bd9Sstevel@tonic-gate 						else
15467c478bd9Sstevel@tonic-gate 						{
15477c478bd9Sstevel@tonic-gate 							nlflag = 1;
15487c478bd9Sstevel@tonic-gate 							break;
15497c478bd9Sstevel@tonic-gate 						}
15507c478bd9Sstevel@tonic-gate 					} while (--c);
15517c478bd9Sstevel@tonic-gate 					break;
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 				case EBCDIC:
15547c478bd9Sstevel@tonic-gate 					do {
15557c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15567c478bd9Sstevel@tonic-gate 						{
15577c478bd9Sstevel@tonic-gate 							*op++ = atoe[ic];
15587c478bd9Sstevel@tonic-gate 						}
15597c478bd9Sstevel@tonic-gate 						else
15607c478bd9Sstevel@tonic-gate 						{
15617c478bd9Sstevel@tonic-gate 							nlflag = 1;
15627c478bd9Sstevel@tonic-gate 							break;
15637c478bd9Sstevel@tonic-gate 						}
15647c478bd9Sstevel@tonic-gate 					} while (--c);
15657c478bd9Sstevel@tonic-gate 					break;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 				case LCEBCDIC:
15687c478bd9Sstevel@tonic-gate 					do {
15697c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15707c478bd9Sstevel@tonic-gate 						{
15717c478bd9Sstevel@tonic-gate 							*op++ = atoe[utol[ic]];
15727c478bd9Sstevel@tonic-gate 						}
15737c478bd9Sstevel@tonic-gate 						else
15747c478bd9Sstevel@tonic-gate 						{
15757c478bd9Sstevel@tonic-gate 							nlflag = 1;
15767c478bd9Sstevel@tonic-gate 							break;
15777c478bd9Sstevel@tonic-gate 						}
15787c478bd9Sstevel@tonic-gate 					} while (--c);
15797c478bd9Sstevel@tonic-gate 					break;
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 				case UCEBCDIC:
15827c478bd9Sstevel@tonic-gate 					do {
15837c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15847c478bd9Sstevel@tonic-gate 						{
15857c478bd9Sstevel@tonic-gate 							*op++ = atoe[ltou[ic]];
15867c478bd9Sstevel@tonic-gate 						}
15877c478bd9Sstevel@tonic-gate 						else
15887c478bd9Sstevel@tonic-gate 						{
15897c478bd9Sstevel@tonic-gate 							nlflag = 1;
15907c478bd9Sstevel@tonic-gate 							break;
15917c478bd9Sstevel@tonic-gate 						}
15927c478bd9Sstevel@tonic-gate 					} while (--c);
15937c478bd9Sstevel@tonic-gate 					break;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 				case IBM:
15967c478bd9Sstevel@tonic-gate 					do {
15977c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
15987c478bd9Sstevel@tonic-gate 						{
15997c478bd9Sstevel@tonic-gate 							*op++ = atoibm[ic];
16007c478bd9Sstevel@tonic-gate 						}
16017c478bd9Sstevel@tonic-gate 						else
16027c478bd9Sstevel@tonic-gate 						{
16037c478bd9Sstevel@tonic-gate 							nlflag = 1;
16047c478bd9Sstevel@tonic-gate 							break;
16057c478bd9Sstevel@tonic-gate 						}
16067c478bd9Sstevel@tonic-gate 					} while (--c);
16077c478bd9Sstevel@tonic-gate 					break;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 				case LCIBM:
16107c478bd9Sstevel@tonic-gate 					do {
16117c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
16127c478bd9Sstevel@tonic-gate 						{
16137c478bd9Sstevel@tonic-gate 						*op++ = atoibm[utol[ic]];
16147c478bd9Sstevel@tonic-gate 						}
16157c478bd9Sstevel@tonic-gate 						else
16167c478bd9Sstevel@tonic-gate 						{
16177c478bd9Sstevel@tonic-gate 							nlflag = 1;
16187c478bd9Sstevel@tonic-gate 							break;
16197c478bd9Sstevel@tonic-gate 						}
16207c478bd9Sstevel@tonic-gate 					} while (--c);
16217c478bd9Sstevel@tonic-gate 					break;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 				case UCIBM:
16247c478bd9Sstevel@tonic-gate 					do {
16257c478bd9Sstevel@tonic-gate 						if ((ic = *ip++) != '\n')
16267c478bd9Sstevel@tonic-gate 						{
16277c478bd9Sstevel@tonic-gate 						*op++ = atoibm[ltou[ic]];
16287c478bd9Sstevel@tonic-gate 						}
16297c478bd9Sstevel@tonic-gate 						else
16307c478bd9Sstevel@tonic-gate 						{
16317c478bd9Sstevel@tonic-gate 							nlflag = 1;
16327c478bd9Sstevel@tonic-gate 							break;
16337c478bd9Sstevel@tonic-gate 						}
16347c478bd9Sstevel@tonic-gate 					} while (--c);
16357c478bd9Sstevel@tonic-gate 					break;
16367c478bd9Sstevel@tonic-gate 				}
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 			/* If newline found, update all the counters and */
16397c478bd9Sstevel@tonic-gate 			/* pointers, pad with trailing blanks if necessary */
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 				if (nlflag)
16427c478bd9Sstevel@tonic-gate 				{
16437c478bd9Sstevel@tonic-gate 					ibc += c - 1;
16447c478bd9Sstevel@tonic-gate 					obc += cbs - cbc;
16457c478bd9Sstevel@tonic-gate 					c += cbs - cbc;
16467c478bd9Sstevel@tonic-gate 					cbc = 0;
16477c478bd9Sstevel@tonic-gate 					if (c > 0)
16487c478bd9Sstevel@tonic-gate 					{
16497c478bd9Sstevel@tonic-gate 					/* Use the right kind of blank */
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 						switch (conv)
16527c478bd9Sstevel@tonic-gate 						{
16537c478bd9Sstevel@tonic-gate 						case BLOCK:
16547c478bd9Sstevel@tonic-gate 						case LCBLOCK:
16557c478bd9Sstevel@tonic-gate 						case UCBLOCK:
16567c478bd9Sstevel@tonic-gate 							ic = ' ';
16577c478bd9Sstevel@tonic-gate 							break;
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 						case EBCDIC:
16607c478bd9Sstevel@tonic-gate 						case LCEBCDIC:
16617c478bd9Sstevel@tonic-gate 						case UCEBCDIC:
16627c478bd9Sstevel@tonic-gate 							ic = atoe[' '];
16637c478bd9Sstevel@tonic-gate 							break;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 						case IBM:
16667c478bd9Sstevel@tonic-gate 						case LCIBM:
16677c478bd9Sstevel@tonic-gate 						case UCIBM:
16687c478bd9Sstevel@tonic-gate 							ic = atoibm[' '];
16697c478bd9Sstevel@tonic-gate 							break;
16707c478bd9Sstevel@tonic-gate 						}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 						/* Pad with trailing blanks */
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 						do {
16757c478bd9Sstevel@tonic-gate 							*op++ = ic;
16767c478bd9Sstevel@tonic-gate 						} while (--c);
16777c478bd9Sstevel@tonic-gate 					}
16787c478bd9Sstevel@tonic-gate 				}
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 			/* If not end of line, this line may be too long */
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 				else if (cbc > cbs)
16837c478bd9Sstevel@tonic-gate 				{
16847c478bd9Sstevel@tonic-gate 					skipf = 1; /* note skip in progress */
16857c478bd9Sstevel@tonic-gate 					obc--;
16867c478bd9Sstevel@tonic-gate 					op--;
16877c478bd9Sstevel@tonic-gate 					cbc = 0;
16887c478bd9Sstevel@tonic-gate 					ntrunc++;  /* count another long line */
16897c478bd9Sstevel@tonic-gate 				}
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 				/* Flush the output buffer if full */
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 				while (obc >= obs)
16947c478bd9Sstevel@tonic-gate 				{
16957c478bd9Sstevel@tonic-gate 					op = flsh();
16967c478bd9Sstevel@tonic-gate 				}
16977c478bd9Sstevel@tonic-gate 			}
16987c478bd9Sstevel@tonic-gate 			break;
16997c478bd9Sstevel@tonic-gate 		}
17007c478bd9Sstevel@tonic-gate 	}
1701a77d64afScf 	/* NOTREACHED */
1702a77d64afScf 	return (0);
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /* match ************************************************************** */
17067c478bd9Sstevel@tonic-gate /*									*/
17077c478bd9Sstevel@tonic-gate /* Compare two text strings for equality				*/
17087c478bd9Sstevel@tonic-gate /*									*/
17097c478bd9Sstevel@tonic-gate /* Arg:		s - pointer to string to match with a command arg	*/
17107c478bd9Sstevel@tonic-gate /* Global arg:	string - pointer to command arg				*/
17117c478bd9Sstevel@tonic-gate /*									*/
17127c478bd9Sstevel@tonic-gate /* Return:	1 if match, 0 if no match				*/
17137c478bd9Sstevel@tonic-gate /*		If match, also reset `string' to point to the text	*/
17147c478bd9Sstevel@tonic-gate /*		that follows the matching text.				*/
17157c478bd9Sstevel@tonic-gate /*									*/
17167c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate static int
17197c478bd9Sstevel@tonic-gate match(s)
17207c478bd9Sstevel@tonic-gate char *s;
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	char *cs;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	cs = string;
17257c478bd9Sstevel@tonic-gate 	while (*cs++ == *s)
17267c478bd9Sstevel@tonic-gate 	{
17277c478bd9Sstevel@tonic-gate 		if (*s++ == '\0')
17287c478bd9Sstevel@tonic-gate 		{
17297c478bd9Sstevel@tonic-gate 			goto true;
17307c478bd9Sstevel@tonic-gate 		}
17317c478bd9Sstevel@tonic-gate 	}
17327c478bd9Sstevel@tonic-gate 	if (*s != '\0')
17337c478bd9Sstevel@tonic-gate 	{
17347c478bd9Sstevel@tonic-gate 		return (0);
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate true:
17387c478bd9Sstevel@tonic-gate 	cs--;
17397c478bd9Sstevel@tonic-gate 	string = cs;
17407c478bd9Sstevel@tonic-gate 	return (1);
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /* number ************************************************************* */
17447c478bd9Sstevel@tonic-gate /*									*/
17457c478bd9Sstevel@tonic-gate /* Convert a numeric arg to binary					*/
17467c478bd9Sstevel@tonic-gate /*									*/
17477c478bd9Sstevel@tonic-gate /* Arg:		big - maximum valid input number			*/
17487c478bd9Sstevel@tonic-gate /* Global arg:	string - pointer to command arg				*/
17497c478bd9Sstevel@tonic-gate /*									*/
17504cddff70SJosef 'Jeff' Sipek /* Valid forms:	123 | 123k | 123M | 123G | 123T | 123P | 123E | 123Z |	*/
17514cddff70SJosef 'Jeff' Sipek /*		123w | 123b | 123*123 | 123x123				*/
17527c478bd9Sstevel@tonic-gate /*		plus combinations such as 2b*3kw*4w			*/
17537c478bd9Sstevel@tonic-gate /*									*/
17547c478bd9Sstevel@tonic-gate /* Return:	converted number					*/
17557c478bd9Sstevel@tonic-gate /*									*/
17567c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate static unsigned long long
17597c478bd9Sstevel@tonic-gate number(big)
17607c478bd9Sstevel@tonic-gate long long big;
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate 	char *cs;
17637c478bd9Sstevel@tonic-gate 	long long n;
17647c478bd9Sstevel@tonic-gate 	long long cut = BIG / 10;	/* limit to avoid overflow */
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	cs = string;
17677c478bd9Sstevel@tonic-gate 	n = 0;
17687c478bd9Sstevel@tonic-gate 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut))
17697c478bd9Sstevel@tonic-gate 	{
17707c478bd9Sstevel@tonic-gate 		n = n*10 + *cs++ - '0';
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate 	for (;;)
17737c478bd9Sstevel@tonic-gate 	{
17747c478bd9Sstevel@tonic-gate 		switch (*cs++)
17757c478bd9Sstevel@tonic-gate 		{
17767c478bd9Sstevel@tonic-gate 
17774cddff70SJosef 'Jeff' Sipek 		case 'Z':
17784cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17794cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
17804cddff70SJosef 'Jeff' Sipek 
17814cddff70SJosef 'Jeff' Sipek 		case 'E':
17824cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17834cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
17844cddff70SJosef 'Jeff' Sipek 
17854cddff70SJosef 'Jeff' Sipek 		case 'P':
17864cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17874cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
17884cddff70SJosef 'Jeff' Sipek 
17894cddff70SJosef 'Jeff' Sipek 		case 'T':
17904cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17914cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
17924cddff70SJosef 'Jeff' Sipek 
17934cddff70SJosef 'Jeff' Sipek 		case 'G':
17944cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17954cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
17964cddff70SJosef 'Jeff' Sipek 
17974cddff70SJosef 'Jeff' Sipek 		case 'M':
17984cddff70SJosef 'Jeff' Sipek 			n *= 1024;
17994cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
18004cddff70SJosef 'Jeff' Sipek 
18017c478bd9Sstevel@tonic-gate 		case 'k':
18027c478bd9Sstevel@tonic-gate 			n *= 1024;
18037c478bd9Sstevel@tonic-gate 			continue;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 		case 'w':
18067c478bd9Sstevel@tonic-gate 			n *= 2;
18077c478bd9Sstevel@tonic-gate 			continue;
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 		case 'b':
18107c478bd9Sstevel@tonic-gate 			n *= BSIZE;
18117c478bd9Sstevel@tonic-gate 			continue;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 		case '*':
18147c478bd9Sstevel@tonic-gate 		case 'x':
18157c478bd9Sstevel@tonic-gate 			string = cs;
18167c478bd9Sstevel@tonic-gate 			n *= number(BIG);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
18197c478bd9Sstevel@tonic-gate 		/* Fall into exit test, recursion has read rest of string */
18207c478bd9Sstevel@tonic-gate 		/* End of string, check for a valid number */
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 		case '\0':
18237c478bd9Sstevel@tonic-gate 			if ((n > big) || (n < 0))
18247c478bd9Sstevel@tonic-gate 			{
18257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s \"%llu\"\n",
18267c478bd9Sstevel@tonic-gate 					gettext("argument out of range:"), n);
18277c478bd9Sstevel@tonic-gate 				exit(2);
18287c478bd9Sstevel@tonic-gate 			}
18297c478bd9Sstevel@tonic-gate 			return (n);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 		default:
18327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dd: %s \"%s\"\n",
18337c478bd9Sstevel@tonic-gate 				gettext("bad numeric argument:"), string);
18347c478bd9Sstevel@tonic-gate 			exit(2);
18357c478bd9Sstevel@tonic-gate 		}
18367c478bd9Sstevel@tonic-gate 	} /* never gets here */
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /* flsh *************************************************************** */
18407c478bd9Sstevel@tonic-gate /*									*/
18417c478bd9Sstevel@tonic-gate /* Flush the output buffer, move any excess bytes down to the beginning	*/
18427c478bd9Sstevel@tonic-gate /*									*/
18437c478bd9Sstevel@tonic-gate /* Arg:		none							*/
1844*61304e4fSPaul Dagnelie /* Global args:	obuf, obc, obs, nofr, nopr, ostriden			*/
18457c478bd9Sstevel@tonic-gate /*									*/
18467c478bd9Sstevel@tonic-gate /* Return:	Pointer to the first free byte in the output buffer.	*/
18477c478bd9Sstevel@tonic-gate /*		Also reset `obc' to account for moved bytes.		*/
18487c478bd9Sstevel@tonic-gate /*									*/
18497c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate static unsigned char
18527c478bd9Sstevel@tonic-gate *flsh()
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	unsigned char *op, *cp;
18557c478bd9Sstevel@tonic-gate 	int bc;
18567c478bd9Sstevel@tonic-gate 	unsigned int oc;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	if (obc)			/* don't flush if the buffer is empty */
18597c478bd9Sstevel@tonic-gate 	{
18607c478bd9Sstevel@tonic-gate 		if (obc >= obs) {
18617c478bd9Sstevel@tonic-gate 			oc = obs;
18627c478bd9Sstevel@tonic-gate 			nofr++;		/* count a full output buffer */
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 		else
18657c478bd9Sstevel@tonic-gate 		{
18667c478bd9Sstevel@tonic-gate 			oc = obc;
18677c478bd9Sstevel@tonic-gate 			nopr++;		/* count a partial output buffer */
18687c478bd9Sstevel@tonic-gate 		}
18697c478bd9Sstevel@tonic-gate 		bc = write(obf, (char *)obuf, oc);
18707c478bd9Sstevel@tonic-gate 		if (bc != oc) {
18717c478bd9Sstevel@tonic-gate 			if (bc < 0)
18727c478bd9Sstevel@tonic-gate 				perror("write");
18737c478bd9Sstevel@tonic-gate 			else
18747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
18757c478bd9Sstevel@tonic-gate 				gettext("dd: unexpected short write, "
18767c478bd9Sstevel@tonic-gate 				"wrote %d bytes, expected %d\n"), bc, oc);
18777c478bd9Sstevel@tonic-gate 			term(2);
18787c478bd9Sstevel@tonic-gate 		}
1879*61304e4fSPaul Dagnelie 
1880*61304e4fSPaul Dagnelie 		if (ostriden > 0 && lseek(obf, ostriden * ((off_t)obs),
1881*61304e4fSPaul Dagnelie 		    SEEK_CUR) == -1) {
1882*61304e4fSPaul Dagnelie 			perror("lseek");
1883*61304e4fSPaul Dagnelie 			exit(2);
1884*61304e4fSPaul Dagnelie 		}
1885*61304e4fSPaul Dagnelie 
18867c478bd9Sstevel@tonic-gate 		obc -= oc;
18877c478bd9Sstevel@tonic-gate 		op = obuf;
188819d32b9aSRobert Mustacchi 		obytes += bc;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		/* If any data in the conversion buffer, move it into */
18917c478bd9Sstevel@tonic-gate 		/* the output buffer */
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 		if (obc) {
18947c478bd9Sstevel@tonic-gate 			cp = obuf + obs;
18957c478bd9Sstevel@tonic-gate 			bc = obc;
18967c478bd9Sstevel@tonic-gate 			do {
18977c478bd9Sstevel@tonic-gate 				*op++ = *cp++;
18987c478bd9Sstevel@tonic-gate 			} while (--bc);
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		return (op);
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 	return (obuf);
19037c478bd9Sstevel@tonic-gate }
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate /* term *************************************************************** */
19067c478bd9Sstevel@tonic-gate /*									*/
19077c478bd9Sstevel@tonic-gate /* Write record statistics, then exit					*/
19087c478bd9Sstevel@tonic-gate /*									*/
19097c478bd9Sstevel@tonic-gate /* Arg:		c - exit status code					*/
19107c478bd9Sstevel@tonic-gate /*									*/
19117c478bd9Sstevel@tonic-gate /* Return:	no return, calls exit					*/
19127c478bd9Sstevel@tonic-gate /*									*/
19137c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate static void
19167c478bd9Sstevel@tonic-gate term(c)
19177c478bd9Sstevel@tonic-gate int c;
19187c478bd9Sstevel@tonic-gate {
19197c478bd9Sstevel@tonic-gate 	stats();
19207c478bd9Sstevel@tonic-gate 	exit(c);
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /* stats ************************************************************** */
19247c478bd9Sstevel@tonic-gate /*									*/
19257c478bd9Sstevel@tonic-gate /* Write record statistics onto standard error				*/
19267c478bd9Sstevel@tonic-gate /*									*/
19277c478bd9Sstevel@tonic-gate /* Args:	none							*/
19287c478bd9Sstevel@tonic-gate /* Global args:	nifr, nipr, nofr, nopr, ntrunc				*/
19297c478bd9Sstevel@tonic-gate /*									*/
19307c478bd9Sstevel@tonic-gate /* Return:	void							*/
19317c478bd9Sstevel@tonic-gate /*									*/
19327c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate static void
19357c478bd9Sstevel@tonic-gate stats()
19367c478bd9Sstevel@tonic-gate {
193719d32b9aSRobert Mustacchi 	hrtime_t delta = gethrtime() - startt;
193819d32b9aSRobert Mustacchi 	double secs = delta * 1e-9;
193919d32b9aSRobert Mustacchi 
19407c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%llu+%llu records in\n"), nifr, nipr);
19417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%llu+%llu records out\n"), nofr, nopr);
19427c478bd9Sstevel@tonic-gate 	if (ntrunc) {
19437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19447c478bd9Sstevel@tonic-gate 			gettext("%llu truncated record(s)\n"), ntrunc);
19457c478bd9Sstevel@tonic-gate 	}
194619d32b9aSRobert Mustacchi 
194719d32b9aSRobert Mustacchi 	/*
194819d32b9aSRobert Mustacchi 	 * If we got here before we started copying somehow, don't bother
194919d32b9aSRobert Mustacchi 	 * printing the rest.
195019d32b9aSRobert Mustacchi 	 */
195119d32b9aSRobert Mustacchi 	if (startt == 0)
195219d32b9aSRobert Mustacchi 		return;
195319d32b9aSRobert Mustacchi 
195419d32b9aSRobert Mustacchi 	(void) fprintf(stderr,
195519d32b9aSRobert Mustacchi 	    gettext("%llu bytes transferred in %.6f secs (%.0f bytes/sec)\n"),
195619d32b9aSRobert Mustacchi 	    obytes, secs, obytes / secs);
19577c478bd9Sstevel@tonic-gate }
1958