xref: /illumos-gate/usr/src/cmd/dd/dd.c (revision b928ac84)
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.
2861304e4fSPaul 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	*/
32*b928ac84SToomas Soome /*		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>
52*b928ac84SToomas Soome #include	<libcmdutils.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* The BIG parameter is machine dependent.  It should be a long integer	*/
557c478bd9Sstevel@tonic-gate /* constant that can be used by the number parser to check the validity	*/
567c478bd9Sstevel@tonic-gate /* of numeric parameters.  On 16-bit machines, it should probably be	*/
577c478bd9Sstevel@tonic-gate /* the maximum unsigned integer, 0177777L.  On 32-bit machines where	*/
587c478bd9Sstevel@tonic-gate /* longs are the same size as ints, the maximum signed integer is more	*/
597c478bd9Sstevel@tonic-gate /* appropriate.  This value is 017777777777L. In 64 bit environments,   */
607c478bd9Sstevel@tonic-gate /* the maximum signed integer value is 0777777777777777777777LL		*/
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	BIG	0777777777777777777777LL
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	BSIZE	512
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Option parameters */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	COPY		0	/* file copy, preserve input block size */
697c478bd9Sstevel@tonic-gate #define	REBLOCK		1	/* file copy, change block size */
707c478bd9Sstevel@tonic-gate #define	LCREBLOCK	2	/* file copy, convert to lower case */
717c478bd9Sstevel@tonic-gate #define	UCREBLOCK	3	/* file copy, convert to upper case */
727c478bd9Sstevel@tonic-gate #define	NBASCII		4	/* file copy, convert from EBCDIC to ASCII */
737c478bd9Sstevel@tonic-gate #define	LCNBASCII	5	/* file copy, EBCDIC to lower case ASCII */
747c478bd9Sstevel@tonic-gate #define	UCNBASCII	6	/* file copy, EBCDIC to upper case ASCII */
757c478bd9Sstevel@tonic-gate #define	NBEBCDIC	7	/* file copy, convert from ASCII to EBCDIC */
767c478bd9Sstevel@tonic-gate #define	LCNBEBCDIC	8	/* file copy, ASCII to lower case EBCDIC */
777c478bd9Sstevel@tonic-gate #define	UCNBEBCDIC	9	/* file copy, ASCII to upper case EBCDIC */
787c478bd9Sstevel@tonic-gate #define	NBIBM		10	/* file copy, convert from ASCII to IBM */
797c478bd9Sstevel@tonic-gate #define	LCNBIBM		11	/* file copy, ASCII to lower case IBM */
807c478bd9Sstevel@tonic-gate #define	UCNBIBM		12	/* file copy, ASCII to upper case IBM */
817c478bd9Sstevel@tonic-gate #define	UNBLOCK		13	/* convert blocked ASCII to ASCII */
827c478bd9Sstevel@tonic-gate #define	LCUNBLOCK	14	/* convert blocked ASCII to lower case ASCII */
837c478bd9Sstevel@tonic-gate #define	UCUNBLOCK	15	/* convert blocked ASCII to upper case ASCII */
847c478bd9Sstevel@tonic-gate #define	ASCII		16	/* convert blocked EBCDIC to ASCII */
857c478bd9Sstevel@tonic-gate #define	LCASCII		17	/* convert blocked EBCDIC to lower case ASCII */
867c478bd9Sstevel@tonic-gate #define	UCASCII		18	/* convert blocked EBCDIC to upper case ASCII */
877c478bd9Sstevel@tonic-gate #define	BLOCK		19	/* convert ASCII to blocked ASCII */
887c478bd9Sstevel@tonic-gate #define	LCBLOCK		20	/* convert ASCII to lower case blocked ASCII */
897c478bd9Sstevel@tonic-gate #define	UCBLOCK		21	/* convert ASCII to upper case blocked ASCII */
907c478bd9Sstevel@tonic-gate #define	EBCDIC		22	/* convert ASCII to blocked EBCDIC */
917c478bd9Sstevel@tonic-gate #define	LCEBCDIC	23	/* convert ASCII to lower case blocked EBCDIC */
927c478bd9Sstevel@tonic-gate #define	UCEBCDIC	24	/* convert ASCII to upper case blocked EBCDIC */
937c478bd9Sstevel@tonic-gate #define	IBM		25	/* convert ASCII to blocked IBM */
947c478bd9Sstevel@tonic-gate #define	LCIBM		26	/* convert ASCII to lower case blocked IBM */
957c478bd9Sstevel@tonic-gate #define	UCIBM		27	/* convert ASCII to upper case blocked IBM */
967c478bd9Sstevel@tonic-gate #define	LCASE		01	/* flag - convert to lower case */
977c478bd9Sstevel@tonic-gate #define	UCASE		02	/* flag - convert to upper case */
987c478bd9Sstevel@tonic-gate #define	SWAB		04	/* flag - swap bytes before conversion */
997c478bd9Sstevel@tonic-gate #define	NERR		010	/* flag - proceed on input errors */
1007c478bd9Sstevel@tonic-gate #define	SYNC		020	/* flag - pad short input blocks with nulls */
1017c478bd9Sstevel@tonic-gate #define	BADLIMIT	5	/* give up if no progress after BADLIMIT trys */
1027c478bd9Sstevel@tonic-gate #define	SVR4XLATE	0	/* use default EBCDIC translation */
1037c478bd9Sstevel@tonic-gate #define	BSDXLATE	1	/* use BSD-compatible EBCDIC translation */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	USAGE\
1067c478bd9Sstevel@tonic-gate 	"usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
1077c478bd9Sstevel@tonic-gate 	"	   [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
10861304e4fSPaul Dagnelie 	"	   [iseek=n] [oseek=n] [seek=n] [stride=n] [istride=n]\n"\
10961304e4fSPaul Dagnelie 	"	   [ostride=n] [count=n] [conv=[ascii] [,ebcdic][,ibm]\n"\
11061304e4fSPaul Dagnelie 	"	   [,asciib][,ebcdicb][,ibmb][,block|unblock][,lcase|ucase]\n"\
11161304e4fSPaul Dagnelie 	"	   [,swab][,noerror][,notrunc][,sync]]\n"\
11219d32b9aSRobert Mustacchi 	"	   [oflag=[dsync][sync]]\n"
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* Global references */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* Local routine declarations */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static int	match(char *);
1197c478bd9Sstevel@tonic-gate static void		term();
1207c478bd9Sstevel@tonic-gate static unsigned long long	number();
1217c478bd9Sstevel@tonic-gate static unsigned char	*flsh();
1227c478bd9Sstevel@tonic-gate static void		stats();
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* Local data definitions */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static unsigned ibs;	/* input buffer size */
1277c478bd9Sstevel@tonic-gate static unsigned obs;	/* output buffer size */
1287c478bd9Sstevel@tonic-gate static unsigned bs;	/* buffer size, overrules ibs and obs */
1297c478bd9Sstevel@tonic-gate static unsigned cbs;	/* conversion buffer size, used for block conversions */
1307c478bd9Sstevel@tonic-gate static unsigned ibc;	/* number of bytes still in the input buffer */
1317c478bd9Sstevel@tonic-gate static unsigned obc;	/* number of bytes in the output buffer */
1327c478bd9Sstevel@tonic-gate static unsigned cbc;	/* number of bytes in the conversion buffer */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static int	ibf;	/* input file descriptor */
1357c478bd9Sstevel@tonic-gate static int	obf;	/* output file descriptor */
1367c478bd9Sstevel@tonic-gate static int	cflag;	/* conversion option flags */
13719d32b9aSRobert Mustacchi static int	oflag;	/* output flag options */
1387c478bd9Sstevel@tonic-gate static int	skipf;	/* if skipf == 1, skip rest of input line */
1397c478bd9Sstevel@tonic-gate static unsigned long long	nifr;	/* count of full input records */
1407c478bd9Sstevel@tonic-gate static unsigned long long	nipr;	/* count of partial input records */
1417c478bd9Sstevel@tonic-gate static unsigned long long	nofr;	/* count of full output records */
1427c478bd9Sstevel@tonic-gate static unsigned long long	nopr;	/* count of partial output records */
1437c478bd9Sstevel@tonic-gate static unsigned long long	ntrunc;	/* count of truncated input lines */
1447c478bd9Sstevel@tonic-gate static unsigned long long	nbad;	/* count of bad records since last */
1457c478bd9Sstevel@tonic-gate 					/* good one */
1467c478bd9Sstevel@tonic-gate static int	files;	/* number of input files to concatenate (tape only) */
1477c478bd9Sstevel@tonic-gate static off_t	skip;	/* number of input records to skip */
1487c478bd9Sstevel@tonic-gate static off_t	iseekn;	/* number of input records to seek past */
1497c478bd9Sstevel@tonic-gate static off_t	oseekn;	/* number of output records to seek past */
1507c478bd9Sstevel@tonic-gate static unsigned long long	count;	/* number of input records to copy */
1517c478bd9Sstevel@tonic-gate 			/* (0 = all) */
152e3bee069SRobert Mustacchi static boolean_t ecount;	/* explicit count given */
15361304e4fSPaul Dagnelie static off_t	ostriden;	/* number of output blocks to skip between */
15461304e4fSPaul Dagnelie 				/* records */
15561304e4fSPaul Dagnelie static off_t	istriden;	/* number of input blocks to skip between */
15661304e4fSPaul Dagnelie 				/* records */
15761304e4fSPaul Dagnelie 
1587c478bd9Sstevel@tonic-gate static int	trantype; /* BSD or SVr4 compatible EBCDIC */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static char		*string;	/* command arg pointer */
1617c478bd9Sstevel@tonic-gate static char		*ifile;		/* input file name pointer */
1627c478bd9Sstevel@tonic-gate static char		*ofile;		/* output file name pointer */
1637c478bd9Sstevel@tonic-gate static unsigned char	*ibuf;		/* input buffer pointer */
1647c478bd9Sstevel@tonic-gate static unsigned char	*obuf;		/* output buffer pointer */
1657c478bd9Sstevel@tonic-gate 
16619d32b9aSRobert Mustacchi static hrtime_t		startt;		/* hrtime copy started */
16719d32b9aSRobert Mustacchi static unsigned long long	obytes;	/* output bytes */
16819d32b9aSRobert Mustacchi static sig_atomic_t	nstats;		/* do we need to output stats */
16919d32b9aSRobert Mustacchi 
1707c478bd9Sstevel@tonic-gate /* This is an EBCDIC to ASCII conversion table	*/
1717c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
1727c478bd9Sstevel@tonic-gate 
173*b928ac84SToomas Soome static unsigned char svr4_etoa[] =
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
1767c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
1777c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
1787c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
1797c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
1807c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
1817c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
1827c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
1837c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
1847c478bd9Sstevel@tonic-gate 	0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174,
1857c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
1867c478bd9Sstevel@tonic-gate 	0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176,
1877c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
1887c478bd9Sstevel@tonic-gate 	0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077,
1897c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
1907c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
1917c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
1927c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
1937c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
1947c478bd9Sstevel@tonic-gate 	0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320,
1957c478bd9Sstevel@tonic-gate 	0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170,
1967c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327,
1977c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
1987c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347,
1997c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
2007c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
2017c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
2027c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
2037c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
2047c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
2057c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2067c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
2077c478bd9Sstevel@tonic-gate };
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* This is an ASCII to EBCDIC conversion table	*/
2107c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
2117c478bd9Sstevel@tonic-gate 
212*b928ac84SToomas Soome static unsigned char svr4_atoe[] =
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2157c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2167c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2177c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2187c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2197c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2207c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2217c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2227c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2237c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2247c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2257c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155,
2267c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2277c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2287c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2297c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007,
2307c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2317c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2327c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2337c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2347c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2357c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2367c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2377c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2387c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2397c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236,
2407c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257,
2417c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2427c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277,
2437c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2447c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2457c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2467c478bd9Sstevel@tonic-gate };
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /* Table for ASCII to IBM (alternate EBCDIC) code conversion	*/
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static unsigned char svr4_atoibm[] =
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2537c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2547c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2557c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2567c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2577c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2587c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2597c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2607c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2617c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2627c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2637c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
2647c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2657c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2667c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2677c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
2687c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2697c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2707c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2717c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2727c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2737c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2747c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2757c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2767c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2777c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
2787c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
2797c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2807c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
2817c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2827c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2837c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2847c478bd9Sstevel@tonic-gate };
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to lower case ASCII	*/
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static unsigned char utol[] =
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
2917c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
2927c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
2937c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
2947c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
2957c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
2967c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2977c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
2987c478bd9Sstevel@tonic-gate 	0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
2997c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
3007c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3017c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
3027c478bd9Sstevel@tonic-gate 	0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3037c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
3047c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3057c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
3067c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3077c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3087c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3097c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3107c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3117c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3127c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3137c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3147c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3157c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3167c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3177c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3187c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3197c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3207c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3217c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3227c478bd9Sstevel@tonic-gate };
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to upper case ASCII	*/
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate static unsigned char ltou[] =
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
3297c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
3307c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
3317c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
3327c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
3337c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
3347c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3357c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
3367c478bd9Sstevel@tonic-gate 	0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3377c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3387c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3397c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
3407c478bd9Sstevel@tonic-gate 	0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3417c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3427c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3437c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
3447c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3457c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3467c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3477c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3487c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3497c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3507c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3517c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3527c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3537c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3547c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3557c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3567c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3577c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3587c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3597c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3607c478bd9Sstevel@tonic-gate };
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /* BSD-compatible EBCDIC to ASCII translate table */
3637c478bd9Sstevel@tonic-gate 
364*b928ac84SToomas Soome static unsigned char bsd_etoa[] =
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
3677c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
3687c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
3697c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
3707c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
3717c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
3727c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
3737c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
3747c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
3757c478bd9Sstevel@tonic-gate 	0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041,
3767c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3777c478bd9Sstevel@tonic-gate 	0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136,
3787c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
3797c478bd9Sstevel@tonic-gate 	0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077,
3807c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
3817c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
3827c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3837c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
3847c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
3857c478bd9Sstevel@tonic-gate 	0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
3867c478bd9Sstevel@tonic-gate 	0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
3877c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
3887c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3897c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3907c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3917c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
3927c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
3937c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
3947c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
3957c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
3967c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3977c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
3987c478bd9Sstevel@tonic-gate };
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to EBCDIC translate table */
4017c478bd9Sstevel@tonic-gate 
402*b928ac84SToomas Soome static unsigned char bsd_atoe[] =
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4057c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4067c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4077c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4087c478bd9Sstevel@tonic-gate 	0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
4097c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4107c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4117c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4127c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4137c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4147c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4157c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
4167c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4177c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4187c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4197c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007,
4207c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4217c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4227c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4237c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4247c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4257c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4267c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4277c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4287c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4297c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4307c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4317c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4327c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4337c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4347c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4357c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4367c478bd9Sstevel@tonic-gate };
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to IBM translate table */
4397c478bd9Sstevel@tonic-gate 
440*b928ac84SToomas Soome static unsigned char bsd_atoibm[] =
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4437c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4447c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4457c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4467c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
4477c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4487c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4497c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4507c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4517c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4527c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4537c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
4547c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4557c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4567c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4577c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
4587c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4597c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4607c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4617c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4627c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4637c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4647c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4657c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4667c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4677c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4687c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4697c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4707c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4717c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4727c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4737c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4747c478bd9Sstevel@tonic-gate };
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /* set up to use SVr4 ascii-ebcdic translation by default */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static unsigned char *atoe = svr4_atoe;
4797c478bd9Sstevel@tonic-gate static unsigned char *etoa = svr4_etoa;
4807c478bd9Sstevel@tonic-gate static unsigned char *atoibm = svr4_atoibm;
4817c478bd9Sstevel@tonic-gate 
48219d32b9aSRobert Mustacchi /*ARGSUSED*/
48319d32b9aSRobert Mustacchi static void
48419d32b9aSRobert Mustacchi siginfo_handler(int sig, siginfo_t *sip, void *ucp)
48519d32b9aSRobert Mustacchi {
48619d32b9aSRobert Mustacchi 	nstats = 1;
48719d32b9aSRobert Mustacchi }
4887c478bd9Sstevel@tonic-gate 
489a77d64afScf int
490a77d64afScf main(int argc, char **argv)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	unsigned char *ip, *op; /* input and output buffer pointers */
4937c478bd9Sstevel@tonic-gate 	int c;		/* character counter */
4947c478bd9Sstevel@tonic-gate 	int ic;		/* input character */
4957c478bd9Sstevel@tonic-gate 	int conv;		/* conversion option code */
4967c478bd9Sstevel@tonic-gate 	int trunc;		/* whether output file is truncated */
4977c478bd9Sstevel@tonic-gate 	struct stat file_stat;
49819d32b9aSRobert Mustacchi 	struct sigaction sact;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/* Set option defaults */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	ibs = BSIZE;
5037c478bd9Sstevel@tonic-gate 	obs = BSIZE;
5047c478bd9Sstevel@tonic-gate 	files = 1;
5057c478bd9Sstevel@tonic-gate 	conv = COPY;
5067c478bd9Sstevel@tonic-gate 	trunc = 1;			/* default: truncate output file */
5077c478bd9Sstevel@tonic-gate 	trantype = SVR4XLATE;  /* use SVR4 EBCDIC by default */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/* Parse command options */
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5127c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
5137c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
5147c478bd9Sstevel@tonic-gate #endif
5157c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "")) != EOF)
5187c478bd9Sstevel@tonic-gate 		switch (c) {
5197c478bd9Sstevel@tonic-gate 			case '?':
5207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE);
5217c478bd9Sstevel@tonic-gate 			exit(2);
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* not getopt()'ed because dd has no options but only operand(s) */
5257c478bd9Sstevel@tonic-gate 
526*b928ac84SToomas Soome 	for (c = optind; c < argc; c++) {
5277c478bd9Sstevel@tonic-gate 		string = argv[c];
528*b928ac84SToomas Soome 		if (match("ibs=")) {
5297c478bd9Sstevel@tonic-gate 			ibs = (unsigned)number(BIG);
5307c478bd9Sstevel@tonic-gate 			continue;
5317c478bd9Sstevel@tonic-gate 		}
532*b928ac84SToomas Soome 		if (match("obs=")) {
5337c478bd9Sstevel@tonic-gate 			obs = (unsigned)number(BIG);
5347c478bd9Sstevel@tonic-gate 			continue;
5357c478bd9Sstevel@tonic-gate 		}
536*b928ac84SToomas Soome 		if (match("cbs=")) {
5377c478bd9Sstevel@tonic-gate 			cbs = (unsigned)number(BIG);
5387c478bd9Sstevel@tonic-gate 			continue;
5397c478bd9Sstevel@tonic-gate 		}
540*b928ac84SToomas Soome 		if (match("bs=")) {
5417c478bd9Sstevel@tonic-gate 			bs = (unsigned)number(BIG);
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 		}
544*b928ac84SToomas Soome 		if (match("if=")) {
5457c478bd9Sstevel@tonic-gate 			ifile = string;
5467c478bd9Sstevel@tonic-gate 			continue;
5477c478bd9Sstevel@tonic-gate 		}
548*b928ac84SToomas Soome 		if (match("of=")) {
5497c478bd9Sstevel@tonic-gate 			ofile = string;
5507c478bd9Sstevel@tonic-gate 			continue;
5517c478bd9Sstevel@tonic-gate 		}
552*b928ac84SToomas Soome 		if (match("skip=")) {
5537c478bd9Sstevel@tonic-gate 			skip = number(BIG);
5547c478bd9Sstevel@tonic-gate 			continue;
5557c478bd9Sstevel@tonic-gate 		}
556*b928ac84SToomas Soome 		if (match("iseek=")) {
5577c478bd9Sstevel@tonic-gate 			iseekn = number(BIG);
5587c478bd9Sstevel@tonic-gate 			continue;
5597c478bd9Sstevel@tonic-gate 		}
560*b928ac84SToomas Soome 		if (match("oseek=")) {
5617c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
5627c478bd9Sstevel@tonic-gate 			continue;
5637c478bd9Sstevel@tonic-gate 		}
564*b928ac84SToomas Soome 		if (match("seek=")) {		/* retained for compatibility */
5657c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
5667c478bd9Sstevel@tonic-gate 			continue;
5677c478bd9Sstevel@tonic-gate 		}
568*b928ac84SToomas Soome 		if (match("ostride=")) {
56961304e4fSPaul Dagnelie 			ostriden = ((off_t)number(BIG)) - 1;
57061304e4fSPaul Dagnelie 			continue;
57161304e4fSPaul Dagnelie 		}
572*b928ac84SToomas Soome 		if (match("istride=")) {
57361304e4fSPaul Dagnelie 			istriden = ((off_t)number(BIG)) - 1;
57461304e4fSPaul Dagnelie 			continue;
57561304e4fSPaul Dagnelie 		}
576*b928ac84SToomas Soome 		if (match("stride=")) {
57761304e4fSPaul Dagnelie 			istriden = ostriden = ((off_t)number(BIG)) - 1;
57861304e4fSPaul Dagnelie 			continue;
57961304e4fSPaul Dagnelie 		}
580*b928ac84SToomas Soome 		if (match("count=")) {
5817c478bd9Sstevel@tonic-gate 			count = number(BIG);
582e3bee069SRobert Mustacchi 			ecount = B_TRUE;
5837c478bd9Sstevel@tonic-gate 			continue;
5847c478bd9Sstevel@tonic-gate 		}
585*b928ac84SToomas Soome 		if (match("files=")) {
5867c478bd9Sstevel@tonic-gate 			files = (int)number(BIG);
5877c478bd9Sstevel@tonic-gate 			continue;
5887c478bd9Sstevel@tonic-gate 		}
589*b928ac84SToomas Soome 		if (match("conv=")) {
590*b928ac84SToomas Soome 			for (;;) {
591*b928ac84SToomas Soome 				if (match(",")) {
5927c478bd9Sstevel@tonic-gate 					continue;
5937c478bd9Sstevel@tonic-gate 				}
594*b928ac84SToomas Soome 				if (*string == '\0') {
5957c478bd9Sstevel@tonic-gate 					break;
5967c478bd9Sstevel@tonic-gate 				}
597*b928ac84SToomas Soome 				if (match("block")) {
5987c478bd9Sstevel@tonic-gate 					conv = BLOCK;
5997c478bd9Sstevel@tonic-gate 					continue;
6007c478bd9Sstevel@tonic-gate 				}
601*b928ac84SToomas Soome 				if (match("unblock")) {
6027c478bd9Sstevel@tonic-gate 					conv = UNBLOCK;
6037c478bd9Sstevel@tonic-gate 					continue;
6047c478bd9Sstevel@tonic-gate 				}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 				/* ebcdicb, ibmb, and asciib must precede */
6077c478bd9Sstevel@tonic-gate 				/* ebcdic, ibm, and ascii in this test */
6087c478bd9Sstevel@tonic-gate 
609*b928ac84SToomas Soome 				if (match("ebcdicb")) {
6107c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6117c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6127c478bd9Sstevel@tonic-gate 					continue;
6137c478bd9Sstevel@tonic-gate 				}
614*b928ac84SToomas Soome 				if (match("ibmb")) {
6157c478bd9Sstevel@tonic-gate 					conv = IBM;
6167c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6177c478bd9Sstevel@tonic-gate 					continue;
6187c478bd9Sstevel@tonic-gate 				}
619*b928ac84SToomas Soome 				if (match("asciib")) {
6207c478bd9Sstevel@tonic-gate 					conv = ASCII;
6217c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6227c478bd9Sstevel@tonic-gate 					continue;
6237c478bd9Sstevel@tonic-gate 				}
624*b928ac84SToomas Soome 				if (match("ebcdic")) {
6257c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6267c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6277c478bd9Sstevel@tonic-gate 					continue;
6287c478bd9Sstevel@tonic-gate 				}
629*b928ac84SToomas Soome 				if (match("ibm")) {
6307c478bd9Sstevel@tonic-gate 					conv = IBM;
6317c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6327c478bd9Sstevel@tonic-gate 					continue;
6337c478bd9Sstevel@tonic-gate 				}
634*b928ac84SToomas Soome 				if (match("ascii")) {
6357c478bd9Sstevel@tonic-gate 					conv = ASCII;
6367c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6377c478bd9Sstevel@tonic-gate 					continue;
6387c478bd9Sstevel@tonic-gate 				}
639*b928ac84SToomas Soome 				if (match("lcase")) {
6407c478bd9Sstevel@tonic-gate 					cflag |= LCASE;
6417c478bd9Sstevel@tonic-gate 					continue;
6427c478bd9Sstevel@tonic-gate 				}
643*b928ac84SToomas Soome 				if (match("ucase")) {
6447c478bd9Sstevel@tonic-gate 					cflag |= UCASE;
6457c478bd9Sstevel@tonic-gate 					continue;
6467c478bd9Sstevel@tonic-gate 				}
647*b928ac84SToomas Soome 				if (match("swab")) {
6487c478bd9Sstevel@tonic-gate 					cflag |= SWAB;
6497c478bd9Sstevel@tonic-gate 					continue;
6507c478bd9Sstevel@tonic-gate 				}
651*b928ac84SToomas Soome 				if (match("noerror")) {
6527c478bd9Sstevel@tonic-gate 					cflag |= NERR;
6537c478bd9Sstevel@tonic-gate 					continue;
6547c478bd9Sstevel@tonic-gate 				}
655*b928ac84SToomas Soome 				if (match("notrunc")) {
6567c478bd9Sstevel@tonic-gate 					trunc = 0;
6577c478bd9Sstevel@tonic-gate 					continue;
6587c478bd9Sstevel@tonic-gate 				}
659*b928ac84SToomas Soome 				if (match("sync")) {
6607c478bd9Sstevel@tonic-gate 					cflag |= SYNC;
6617c478bd9Sstevel@tonic-gate 					continue;
6627c478bd9Sstevel@tonic-gate 				}
6637c478bd9Sstevel@tonic-gate 				goto badarg;
6647c478bd9Sstevel@tonic-gate 			}
6657c478bd9Sstevel@tonic-gate 			continue;
6667c478bd9Sstevel@tonic-gate 		}
667*b928ac84SToomas Soome 		if (match("oflag=")) {
668*b928ac84SToomas Soome 			for (;;) {
669*b928ac84SToomas Soome 				if (match(",")) {
67019d32b9aSRobert Mustacchi 					continue;
67119d32b9aSRobert Mustacchi 				}
672*b928ac84SToomas Soome 				if (*string == '\0') {
67319d32b9aSRobert Mustacchi 					break;
67419d32b9aSRobert Mustacchi 				}
675*b928ac84SToomas Soome 				if (match("dsync")) {
67619d32b9aSRobert Mustacchi 					oflag |= O_DSYNC;
67719d32b9aSRobert Mustacchi 					continue;
67819d32b9aSRobert Mustacchi 				}
679*b928ac84SToomas Soome 				if (match("sync")) {
68019d32b9aSRobert Mustacchi 					oflag |= O_SYNC;
68119d32b9aSRobert Mustacchi 					continue;
68219d32b9aSRobert Mustacchi 				}
68319d32b9aSRobert Mustacchi 				goto badarg;
68419d32b9aSRobert Mustacchi 			}
68519d32b9aSRobert Mustacchi 			continue;
68619d32b9aSRobert Mustacchi 		}
6877c478bd9Sstevel@tonic-gate 		badarg:
6887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s \"%s\"\n",
689*b928ac84SToomas Soome 		    gettext("bad argument:"), string);
6907c478bd9Sstevel@tonic-gate 		exit(2);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* Perform consistency checks on options, decode strange conventions */
6947c478bd9Sstevel@tonic-gate 
695*b928ac84SToomas Soome 	if (bs) {
6967c478bd9Sstevel@tonic-gate 		ibs = obs = bs;
6977c478bd9Sstevel@tonic-gate 	}
698*b928ac84SToomas Soome 	if ((ibs == 0) || (obs == 0)) {
6997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s\n",
700*b928ac84SToomas Soome 		    gettext("buffer sizes cannot be zero"));
7017c478bd9Sstevel@tonic-gate 		exit(2);
7027c478bd9Sstevel@tonic-gate 	}
70361304e4fSPaul Dagnelie 	if (ostriden == (off_t)-1) {
70461304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
705*b928ac84SToomas Soome 		    gettext("stride must be greater than zero"));
70661304e4fSPaul Dagnelie 		exit(2);
70761304e4fSPaul Dagnelie 	}
70861304e4fSPaul Dagnelie 	if (istriden == (off_t)-1) {
70961304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
710*b928ac84SToomas Soome 		    gettext("stride must be greater than zero"));
71161304e4fSPaul Dagnelie 		exit(2);
71261304e4fSPaul Dagnelie 	}
713*b928ac84SToomas Soome 	if (conv == COPY) {
714*b928ac84SToomas Soome 		if ((bs == 0) || (cflag & (LCASE | UCASE))) {
7157c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 	}
718*b928ac84SToomas Soome 	if (cbs == 0) {
719*b928ac84SToomas Soome 		switch (conv) {
7207c478bd9Sstevel@tonic-gate 		case BLOCK:
7217c478bd9Sstevel@tonic-gate 		case UNBLOCK:
7227c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7237c478bd9Sstevel@tonic-gate 			break;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		case ASCII:
7267c478bd9Sstevel@tonic-gate 			conv = NBASCII;
7277c478bd9Sstevel@tonic-gate 			break;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		case EBCDIC:
7307c478bd9Sstevel@tonic-gate 			conv = NBEBCDIC;
7317c478bd9Sstevel@tonic-gate 			break;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		case IBM:
7347c478bd9Sstevel@tonic-gate 			conv = NBIBM;
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	/* Expand options into lower and upper case versions if necessary */
7407c478bd9Sstevel@tonic-gate 
741*b928ac84SToomas Soome 	switch (conv) {
7427c478bd9Sstevel@tonic-gate 	case REBLOCK:
743*b928ac84SToomas Soome 		if (cflag & LCASE)
7447c478bd9Sstevel@tonic-gate 			conv = LCREBLOCK;
745*b928ac84SToomas Soome 		else if (cflag & UCASE)
7467c478bd9Sstevel@tonic-gate 			conv = UCREBLOCK;
7477c478bd9Sstevel@tonic-gate 		break;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	case UNBLOCK:
750*b928ac84SToomas Soome 		if (cflag & LCASE)
7517c478bd9Sstevel@tonic-gate 			conv = LCUNBLOCK;
752*b928ac84SToomas Soome 		else if (cflag & UCASE)
7537c478bd9Sstevel@tonic-gate 			conv = UCUNBLOCK;
7547c478bd9Sstevel@tonic-gate 		break;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	case BLOCK:
757*b928ac84SToomas Soome 		if (cflag & LCASE)
7587c478bd9Sstevel@tonic-gate 			conv = LCBLOCK;
759*b928ac84SToomas Soome 		else if (cflag & UCASE)
7607c478bd9Sstevel@tonic-gate 			conv = UCBLOCK;
7617c478bd9Sstevel@tonic-gate 		break;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	case ASCII:
764*b928ac84SToomas Soome 		if (cflag & LCASE)
7657c478bd9Sstevel@tonic-gate 			conv = LCASCII;
766*b928ac84SToomas Soome 		else if (cflag & UCASE)
7677c478bd9Sstevel@tonic-gate 			conv = UCASCII;
7687c478bd9Sstevel@tonic-gate 		break;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	case NBASCII:
771*b928ac84SToomas Soome 		if (cflag & LCASE)
7727c478bd9Sstevel@tonic-gate 			conv = LCNBASCII;
773*b928ac84SToomas Soome 		else if (cflag & UCASE)
7747c478bd9Sstevel@tonic-gate 			conv = UCNBASCII;
7757c478bd9Sstevel@tonic-gate 		break;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	case EBCDIC:
778*b928ac84SToomas Soome 		if (cflag & LCASE)
7797c478bd9Sstevel@tonic-gate 			conv = LCEBCDIC;
780*b928ac84SToomas Soome 		else if (cflag & UCASE)
7817c478bd9Sstevel@tonic-gate 			conv = UCEBCDIC;
7827c478bd9Sstevel@tonic-gate 		break;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	case NBEBCDIC:
785*b928ac84SToomas Soome 		if (cflag & LCASE)
7867c478bd9Sstevel@tonic-gate 			conv = LCNBEBCDIC;
787*b928ac84SToomas Soome 		else if (cflag & UCASE)
7887c478bd9Sstevel@tonic-gate 			conv = UCNBEBCDIC;
7897c478bd9Sstevel@tonic-gate 		break;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	case IBM:
792*b928ac84SToomas Soome 		if (cflag & LCASE)
7937c478bd9Sstevel@tonic-gate 			conv = LCIBM;
794*b928ac84SToomas Soome 		else if (cflag & UCASE)
7957c478bd9Sstevel@tonic-gate 			conv = UCIBM;
7967c478bd9Sstevel@tonic-gate 		break;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	case NBIBM:
799*b928ac84SToomas Soome 		if (cflag & LCASE)
8007c478bd9Sstevel@tonic-gate 			conv = LCNBIBM;
801*b928ac84SToomas Soome 		else if (cflag & UCASE)
8027c478bd9Sstevel@tonic-gate 			conv = UCNBIBM;
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* If BSD-compatible translation is selected, change the tables */
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (trantype == BSDXLATE) {
8097c478bd9Sstevel@tonic-gate 		atoe = bsd_atoe;
8107c478bd9Sstevel@tonic-gate 		atoibm = bsd_atoibm;
8117c478bd9Sstevel@tonic-gate 		etoa = bsd_etoa;
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 	/* Open the input file, or duplicate standard input */
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	ibf = -1;
816*b928ac84SToomas Soome 	if (ifile) {
8177c478bd9Sstevel@tonic-gate 		ibf = open(ifile, 0);
818*b928ac84SToomas Soome 	} else {
8197c478bd9Sstevel@tonic-gate 		ifile = "";
820*b928ac84SToomas Soome 		ibf = dup(STDIN_FILENO);
8217c478bd9Sstevel@tonic-gate 	}
82219d32b9aSRobert Mustacchi 
823*b928ac84SToomas Soome 	if (ibf == -1) {
8247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ifile);
8257c478bd9Sstevel@tonic-gate 		perror("open");
8267c478bd9Sstevel@tonic-gate 		exit(2);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/* Open the output file, or duplicate standard output */
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	obf = -1;
832*b928ac84SToomas Soome 	if (ofile) {
833*b928ac84SToomas Soome 		if (trunc == 0)	{	/* do not truncate output file */
834*b928ac84SToomas Soome 			obf = open(ofile, (O_WRONLY | O_CREAT | oflag),
835*b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
836*b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
837*b928ac84SToomas Soome 		} else if (oseekn && (trunc == 1)) {
838*b928ac84SToomas Soome 			obf = open(ofile, O_WRONLY | O_CREAT | oflag,
839*b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
840*b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
841*b928ac84SToomas Soome 			if (obf == -1) {
8427c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s: ", ofile);
8437c478bd9Sstevel@tonic-gate 				perror("open");
8447c478bd9Sstevel@tonic-gate 				exit(2);
8457c478bd9Sstevel@tonic-gate 			}
8467c478bd9Sstevel@tonic-gate 			(void) fstat(obf, &file_stat);
8477c478bd9Sstevel@tonic-gate 			if (((file_stat.st_mode & S_IFMT) == S_IFREG) &&
8487c478bd9Sstevel@tonic-gate 			    (ftruncate(obf, (((off_t)oseekn) * ((off_t)obs)))
849*b928ac84SToomas Soome 			    == -1)) {
8507c478bd9Sstevel@tonic-gate 				perror("ftruncate");
8517c478bd9Sstevel@tonic-gate 				exit(2);
8527c478bd9Sstevel@tonic-gate 			}
853*b928ac84SToomas Soome 		} else {
854*b928ac84SToomas Soome 			obf = open(ofile, O_WRONLY | O_CREAT | O_TRUNC | oflag,
855*b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
856*b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
8577c478bd9Sstevel@tonic-gate 		}
858*b928ac84SToomas Soome 	} else {
8597c478bd9Sstevel@tonic-gate 		ofile = "";
860*b928ac84SToomas Soome 		obf = dup(STDOUT_FILENO);
8617c478bd9Sstevel@tonic-gate 	}
86219d32b9aSRobert Mustacchi 
863*b928ac84SToomas Soome 	if (obf == -1) {
8647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ofile);
8657c478bd9Sstevel@tonic-gate 		perror("open");
8667c478bd9Sstevel@tonic-gate 		exit(2);
8677c478bd9Sstevel@tonic-gate 	}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	/* Expand memory to get an input buffer */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	ibuf = (unsigned char *)valloc(ibs + 10);
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/* If no conversions, the input buffer is the output buffer */
8747c478bd9Sstevel@tonic-gate 
875*b928ac84SToomas Soome 	if (conv == COPY) {
8767c478bd9Sstevel@tonic-gate 		obuf = ibuf;
877*b928ac84SToomas Soome 	} else {
8787c478bd9Sstevel@tonic-gate 
879*b928ac84SToomas Soome 		/*
880*b928ac84SToomas Soome 		 * Expand memory to get an output buffer. Leave enough room
881*b928ac84SToomas Soome 		 * at the end to convert a logical record when doing block
882*b928ac84SToomas Soome 		 * conversions.
883*b928ac84SToomas Soome 		 */
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		obuf = (unsigned char *)valloc(obs + cbs + 10);
8867c478bd9Sstevel@tonic-gate 	}
887*b928ac84SToomas Soome 	if ((ibuf == NULL) || (obuf == NULL)) {
8887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
889*b928ac84SToomas Soome 		    "dd: %s\n", gettext("not enough memory"));
8907c478bd9Sstevel@tonic-gate 		exit(2);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
89319d32b9aSRobert Mustacchi 	/*
89419d32b9aSRobert Mustacchi 	 * Enable a statistics message when we terminate on SIGINT
89519d32b9aSRobert Mustacchi 	 * Also enable it to be queried via SIGINFO and SIGUSR1
89619d32b9aSRobert Mustacchi 	 */
8977c478bd9Sstevel@tonic-gate 
898*b928ac84SToomas Soome 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
8997c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, term);
9007c478bd9Sstevel@tonic-gate 	}
90119d32b9aSRobert Mustacchi 
90219d32b9aSRobert Mustacchi 	bzero(&sact, sizeof (struct sigaction));
90319d32b9aSRobert Mustacchi 	sact.sa_flags = SA_SIGINFO;
90419d32b9aSRobert Mustacchi 	sact.sa_sigaction = siginfo_handler;
90519d32b9aSRobert Mustacchi 	(void) sigemptyset(&sact.sa_mask);
90619d32b9aSRobert Mustacchi 	if (sigaction(SIGINFO, &sact, NULL) != 0) {
90719d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
90819d32b9aSRobert Mustacchi 		    gettext("failed to enable siginfo handler"),
90919d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
91019d32b9aSRobert Mustacchi 		exit(2);
91119d32b9aSRobert Mustacchi 	}
91219d32b9aSRobert Mustacchi 	if (sigaction(SIGUSR1, &sact, NULL) != 0) {
91319d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
91419d32b9aSRobert Mustacchi 		    gettext("failed to enable sigusr1 handler"),
91519d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
91619d32b9aSRobert Mustacchi 		exit(2);
91719d32b9aSRobert Mustacchi 	}
91819d32b9aSRobert Mustacchi 
9197c478bd9Sstevel@tonic-gate 	/* Skip input blocks */
9207c478bd9Sstevel@tonic-gate 
921*b928ac84SToomas Soome 	while (skip) {
9227c478bd9Sstevel@tonic-gate 		ibc = read(ibf, (char *)ibuf, ibs);
923*b928ac84SToomas Soome 		if (ibc == (unsigned)-1) {
924*b928ac84SToomas Soome 			if (++nbad > BADLIMIT) {
9257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
926*b928ac84SToomas Soome 				    gettext("skip failed"));
9277c478bd9Sstevel@tonic-gate 				exit(2);
928*b928ac84SToomas Soome 			} else {
9297c478bd9Sstevel@tonic-gate 				perror("read");
9307c478bd9Sstevel@tonic-gate 			}
931*b928ac84SToomas Soome 		} else {
932*b928ac84SToomas Soome 			if (ibc == 0) {
9337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
9347c478bd9Sstevel@tonic-gate 				gettext("cannot skip past end-of-file"));
9357c478bd9Sstevel@tonic-gate 				exit(3);
936*b928ac84SToomas Soome 			} else {
9377c478bd9Sstevel@tonic-gate 				nbad = 0;
9387c478bd9Sstevel@tonic-gate 			}
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		skip--;
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	/* Seek past input blocks */
9447c478bd9Sstevel@tonic-gate 
945*b928ac84SToomas Soome 	if (iseekn && lseek(ibf, (((off_t)iseekn) * ((off_t)ibs)), 1) == -1) {
9467c478bd9Sstevel@tonic-gate 		perror("lseek");
9477c478bd9Sstevel@tonic-gate 		exit(2);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/* Seek past output blocks */
9517c478bd9Sstevel@tonic-gate 
952*b928ac84SToomas Soome 	if (oseekn && lseek(obf, (((off_t)oseekn) * ((off_t)obs)), 1) == -1) {
9537c478bd9Sstevel@tonic-gate 		perror("lseek");
9547c478bd9Sstevel@tonic-gate 		exit(2);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/* Initialize all buffer pointers */
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	skipf = 0;	/* not skipping an input line */
9607c478bd9Sstevel@tonic-gate 	ibc = 0;	/* no input characters yet */
9617c478bd9Sstevel@tonic-gate 	obc = 0;	/* no output characters yet */
9627c478bd9Sstevel@tonic-gate 	cbc = 0;	/* the conversion buffer is empty */
9637c478bd9Sstevel@tonic-gate 	op = obuf;	/* point to the output buffer */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/* Read and convert input blocks until end of file(s) */
9667c478bd9Sstevel@tonic-gate 
96719d32b9aSRobert Mustacchi 	/* Grab our start time for siginfo purposes */
96819d32b9aSRobert Mustacchi 	startt = gethrtime();
96919d32b9aSRobert Mustacchi 
970*b928ac84SToomas Soome 	for (;;) {
97119d32b9aSRobert Mustacchi 		if (nstats != 0) {
97219d32b9aSRobert Mustacchi 			stats();
97319d32b9aSRobert Mustacchi 			nstats = 0;
97419d32b9aSRobert Mustacchi 		}
97519d32b9aSRobert Mustacchi 
976e3bee069SRobert Mustacchi 		if ((count == 0 && ecount == B_FALSE) || (nifr+nipr < count)) {
9777c478bd9Sstevel@tonic-gate 		/* If proceed on error is enabled, zero the input buffer */
9787c478bd9Sstevel@tonic-gate 
979*b928ac84SToomas Soome 			if (cflag & NERR) {
9807c478bd9Sstevel@tonic-gate 				ip = ibuf + ibs;
9817c478bd9Sstevel@tonic-gate 				c = ibs;
9827c478bd9Sstevel@tonic-gate 				if (c & 1)	/* if the size is odd, */
9837c478bd9Sstevel@tonic-gate 				{
9847c478bd9Sstevel@tonic-gate 					*--ip = 0;	/* clear the odd byte */
9857c478bd9Sstevel@tonic-gate 				}
9867c478bd9Sstevel@tonic-gate 				if (c >>= 1)		/* divide by two */
9877c478bd9Sstevel@tonic-gate 				{
9887c478bd9Sstevel@tonic-gate 					do {	/* clear two at a time */
9897c478bd9Sstevel@tonic-gate 						*--ip = 0;
9907c478bd9Sstevel@tonic-gate 						*--ip = 0;
9917c478bd9Sstevel@tonic-gate 					} while (--c);
9927c478bd9Sstevel@tonic-gate 				}
9937c478bd9Sstevel@tonic-gate 			}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 			/* Read the next input block */
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			ibc = read(ibf, (char *)ibuf, ibs);
9987c478bd9Sstevel@tonic-gate 
99961304e4fSPaul Dagnelie 			if (istriden > 0 && lseek(ibf, istriden * ((off_t)ibs),
100061304e4fSPaul Dagnelie 			    SEEK_CUR) == -1) {
100161304e4fSPaul Dagnelie 				perror("lseek");
100261304e4fSPaul Dagnelie 				exit(2);
100361304e4fSPaul Dagnelie 			}
100461304e4fSPaul Dagnelie 
10057c478bd9Sstevel@tonic-gate 			/* Process input errors */
10067c478bd9Sstevel@tonic-gate 
1007*b928ac84SToomas Soome 			if (ibc == (unsigned)-1) {
10087c478bd9Sstevel@tonic-gate 				perror("read");
1009*b928ac84SToomas Soome 				if (((cflag & NERR) == 0) ||
1010*b928ac84SToomas Soome 				    (++nbad > BADLIMIT)) {
1011*b928ac84SToomas Soome 					while (obc) {
10127c478bd9Sstevel@tonic-gate 						(void) flsh();
10137c478bd9Sstevel@tonic-gate 					}
10147c478bd9Sstevel@tonic-gate 					term(2);
1015*b928ac84SToomas Soome 				} else {
10167c478bd9Sstevel@tonic-gate 					stats();
10177c478bd9Sstevel@tonic-gate 					ibc = ibs; /* assume a full block */
10187c478bd9Sstevel@tonic-gate 				}
1019*b928ac84SToomas Soome 			} else {
10207c478bd9Sstevel@tonic-gate 				nbad = 0;
10217c478bd9Sstevel@tonic-gate 			}
1022*b928ac84SToomas Soome 		} else {
1023*b928ac84SToomas Soome 			/* Record count satisfied, simulate end of file */
10247c478bd9Sstevel@tonic-gate 			ibc = 0;
10257c478bd9Sstevel@tonic-gate 			files = 1;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		/* Process end of file */
10297c478bd9Sstevel@tonic-gate 
1030*b928ac84SToomas Soome 		if (ibc == 0) {
1031*b928ac84SToomas Soome 			switch (conv) {
10327c478bd9Sstevel@tonic-gate 			case UNBLOCK:
10337c478bd9Sstevel@tonic-gate 			case LCUNBLOCK:
10347c478bd9Sstevel@tonic-gate 			case UCUNBLOCK:
10357c478bd9Sstevel@tonic-gate 			case ASCII:
10367c478bd9Sstevel@tonic-gate 			case LCASCII:
10377c478bd9Sstevel@tonic-gate 			case UCASCII:
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 				/* Trim trailing blanks from the last line */
10407c478bd9Sstevel@tonic-gate 
1041*b928ac84SToomas Soome 				if ((c = cbc) != 0) {
10427c478bd9Sstevel@tonic-gate 					do {
1043*b928ac84SToomas Soome 						if ((*--op) != ' ') {
10447c478bd9Sstevel@tonic-gate 							op++;
10457c478bd9Sstevel@tonic-gate 							break;
10467c478bd9Sstevel@tonic-gate 						}
10477c478bd9Sstevel@tonic-gate 					} while (--c);
10487c478bd9Sstevel@tonic-gate 					*op++ = '\n';
10497c478bd9Sstevel@tonic-gate 					obc -= cbc - c - 1;
10507c478bd9Sstevel@tonic-gate 					cbc = 0;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 					/* Flush the output buffer if full */
10537c478bd9Sstevel@tonic-gate 
1054*b928ac84SToomas Soome 					while (obc >= obs) {
10557c478bd9Sstevel@tonic-gate 						op = flsh();
10567c478bd9Sstevel@tonic-gate 					}
10577c478bd9Sstevel@tonic-gate 				}
10587c478bd9Sstevel@tonic-gate 				break;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			case BLOCK:
10617c478bd9Sstevel@tonic-gate 			case LCBLOCK:
10627c478bd9Sstevel@tonic-gate 			case UCBLOCK:
10637c478bd9Sstevel@tonic-gate 			case EBCDIC:
10647c478bd9Sstevel@tonic-gate 			case LCEBCDIC:
10657c478bd9Sstevel@tonic-gate 			case UCEBCDIC:
10667c478bd9Sstevel@tonic-gate 			case IBM:
10677c478bd9Sstevel@tonic-gate 			case LCIBM:
10687c478bd9Sstevel@tonic-gate 			case UCIBM:
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 			/* Pad trailing blanks if the last line is short */
10717c478bd9Sstevel@tonic-gate 
1072*b928ac84SToomas Soome 				if (cbc) {
10737c478bd9Sstevel@tonic-gate 					obc += c = cbs - cbc;
10747c478bd9Sstevel@tonic-gate 					cbc = 0;
1075*b928ac84SToomas Soome 					if (c > 0) {
10767c478bd9Sstevel@tonic-gate 					/* Use the right kind of blank */
10777c478bd9Sstevel@tonic-gate 
1078*b928ac84SToomas Soome 						switch (conv) {
10797c478bd9Sstevel@tonic-gate 						case BLOCK:
10807c478bd9Sstevel@tonic-gate 						case LCBLOCK:
10817c478bd9Sstevel@tonic-gate 						case UCBLOCK:
10827c478bd9Sstevel@tonic-gate 							ic = ' ';
10837c478bd9Sstevel@tonic-gate 							break;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 						case EBCDIC:
10867c478bd9Sstevel@tonic-gate 						case LCEBCDIC:
10877c478bd9Sstevel@tonic-gate 						case UCEBCDIC:
10887c478bd9Sstevel@tonic-gate 							ic = atoe[' '];
10897c478bd9Sstevel@tonic-gate 							break;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 						case IBM:
10927c478bd9Sstevel@tonic-gate 						case LCIBM:
10937c478bd9Sstevel@tonic-gate 						case UCIBM:
10947c478bd9Sstevel@tonic-gate 							ic = atoibm[' '];
10957c478bd9Sstevel@tonic-gate 							break;
10967c478bd9Sstevel@tonic-gate 						}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 						/* Pad with trailing blanks */
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 						do {
11017c478bd9Sstevel@tonic-gate 							*op++ = ic;
11027c478bd9Sstevel@tonic-gate 						} while (--c);
11037c478bd9Sstevel@tonic-gate 					}
11047c478bd9Sstevel@tonic-gate 				}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 				/* Flush the output buffer if full */
11087c478bd9Sstevel@tonic-gate 
1109*b928ac84SToomas Soome 				while (obc >= obs) {
11107c478bd9Sstevel@tonic-gate 					op = flsh();
11117c478bd9Sstevel@tonic-gate 				}
11127c478bd9Sstevel@tonic-gate 				break;
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 			/* If no more files to read, flush the output buffer */
11167c478bd9Sstevel@tonic-gate 
1117*b928ac84SToomas Soome 			if (--files <= 0) {
11187c478bd9Sstevel@tonic-gate 				(void) flsh();
1119*b928ac84SToomas Soome 				if ((close(obf) != 0) ||
1120*b928ac84SToomas Soome 				    (fclose(stdout) != 0)) {
11217c478bd9Sstevel@tonic-gate 					perror(gettext("dd: close error"));
11227c478bd9Sstevel@tonic-gate 					exit(2);
11237c478bd9Sstevel@tonic-gate 				}
11247c478bd9Sstevel@tonic-gate 				term(0);	/* successful exit */
1125*b928ac84SToomas Soome 			} else {
11267c478bd9Sstevel@tonic-gate 				continue;	/* read the next file */
11277c478bd9Sstevel@tonic-gate 			}
1128*b928ac84SToomas Soome 		} else if (ibc == ibs) {
1129*b928ac84SToomas Soome 			/* Normal read, check for special cases */
11307c478bd9Sstevel@tonic-gate 			nifr++;		/* count another full input record */
1131*b928ac84SToomas Soome 		} else {
11327c478bd9Sstevel@tonic-gate 			nipr++;		/* count a partial input record */
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 			/* If `sync' enabled, pad nulls */
11357c478bd9Sstevel@tonic-gate 
1136*b928ac84SToomas Soome 			if ((cflag & SYNC) && ((cflag & NERR) == 0)) {
11377c478bd9Sstevel@tonic-gate 				c = ibs - ibc;
11387c478bd9Sstevel@tonic-gate 				ip = ibuf + ibs;
11397c478bd9Sstevel@tonic-gate 				do {
1140*b928ac84SToomas Soome 					if ((conv == BLOCK) ||
1141*b928ac84SToomas Soome 					    (conv == UNBLOCK))
1142*b928ac84SToomas Soome 						*--ip = ' ';
1143*b928ac84SToomas Soome 					else
1144*b928ac84SToomas Soome 						*--ip = '\0';
11457c478bd9Sstevel@tonic-gate 				} while (--c);
11467c478bd9Sstevel@tonic-gate 				ibc = ibs;
11477c478bd9Sstevel@tonic-gate 			}
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		/* Swap the bytes in the input buffer if necessary */
11517c478bd9Sstevel@tonic-gate 
1152*b928ac84SToomas Soome 		if (cflag & SWAB) {
11537c478bd9Sstevel@tonic-gate 			ip = ibuf;
1154*b928ac84SToomas Soome 			if (ibc & 1) {	/* if the byte count is odd, */
11557c478bd9Sstevel@tonic-gate 				ip[ibc] = 0;  /* make it even, pad with zero */
11567c478bd9Sstevel@tonic-gate 			}
11577c478bd9Sstevel@tonic-gate 			c = ibc >> 1;	/* compute the pair count */
11587c478bd9Sstevel@tonic-gate 			do {
11597c478bd9Sstevel@tonic-gate 				ic = *ip++;
11607c478bd9Sstevel@tonic-gate 				ip[-1] = *ip;
11617c478bd9Sstevel@tonic-gate 				*ip++ = ic;
11627c478bd9Sstevel@tonic-gate 			} while (--c);		/* do two bytes at a time */
11637c478bd9Sstevel@tonic-gate 		}
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 		/* Select the appropriate conversion loop */
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		ip = ibuf;
1168*b928ac84SToomas Soome 		switch (conv) {
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		/* Simple copy: no conversion, preserve the input block size */
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		case COPY:
11737c478bd9Sstevel@tonic-gate 			obc = ibc;
11747c478bd9Sstevel@tonic-gate 			(void) flsh();
11757c478bd9Sstevel@tonic-gate 			break;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		/* Simple copy: pack all output into equal sized blocks */
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		case REBLOCK:
11807c478bd9Sstevel@tonic-gate 		case LCREBLOCK:
11817c478bd9Sstevel@tonic-gate 		case UCREBLOCK:
11827c478bd9Sstevel@tonic-gate 		case NBASCII:
11837c478bd9Sstevel@tonic-gate 		case LCNBASCII:
11847c478bd9Sstevel@tonic-gate 		case UCNBASCII:
11857c478bd9Sstevel@tonic-gate 		case NBEBCDIC:
11867c478bd9Sstevel@tonic-gate 		case LCNBEBCDIC:
11877c478bd9Sstevel@tonic-gate 		case UCNBEBCDIC:
11887c478bd9Sstevel@tonic-gate 		case NBIBM:
11897c478bd9Sstevel@tonic-gate 		case LCNBIBM:
11907c478bd9Sstevel@tonic-gate 		case UCNBIBM:
1191*b928ac84SToomas Soome 			while ((c = ibc) != 0) {
1192*b928ac84SToomas Soome 				if (c > (obs - obc)) {
11937c478bd9Sstevel@tonic-gate 					c = obs - obc;
11947c478bd9Sstevel@tonic-gate 				}
11957c478bd9Sstevel@tonic-gate 				ibc -= c;
11967c478bd9Sstevel@tonic-gate 				obc += c;
1197*b928ac84SToomas Soome 				switch (conv) {
11987c478bd9Sstevel@tonic-gate 				case REBLOCK:
11997c478bd9Sstevel@tonic-gate 					do {
12007c478bd9Sstevel@tonic-gate 						*op++ = *ip++;
12017c478bd9Sstevel@tonic-gate 					} while (--c);
12027c478bd9Sstevel@tonic-gate 					break;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 				case LCREBLOCK:
12057c478bd9Sstevel@tonic-gate 					do {
12067c478bd9Sstevel@tonic-gate 						*op++ = utol[*ip++];
12077c478bd9Sstevel@tonic-gate 					} while (--c);
12087c478bd9Sstevel@tonic-gate 					break;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 				case UCREBLOCK:
12117c478bd9Sstevel@tonic-gate 					do {
12127c478bd9Sstevel@tonic-gate 						*op++ = ltou[*ip++];
12137c478bd9Sstevel@tonic-gate 					} while (--c);
12147c478bd9Sstevel@tonic-gate 					break;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 				case NBASCII:
12177c478bd9Sstevel@tonic-gate 					do {
12187c478bd9Sstevel@tonic-gate 						*op++ = etoa[*ip++];
12197c478bd9Sstevel@tonic-gate 					} while (--c);
12207c478bd9Sstevel@tonic-gate 					break;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 				case LCNBASCII:
12237c478bd9Sstevel@tonic-gate 					do {
12247c478bd9Sstevel@tonic-gate 						*op++ = utol[etoa[*ip++]];
12257c478bd9Sstevel@tonic-gate 					} while (--c);
12267c478bd9Sstevel@tonic-gate 					break;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 				case UCNBASCII:
12297c478bd9Sstevel@tonic-gate 					do {
12307c478bd9Sstevel@tonic-gate 						*op++ = ltou[etoa[*ip++]];
12317c478bd9Sstevel@tonic-gate 					} while (--c);
12327c478bd9Sstevel@tonic-gate 					break;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 				case NBEBCDIC:
12357c478bd9Sstevel@tonic-gate 					do {
12367c478bd9Sstevel@tonic-gate 						*op++ = atoe[*ip++];
12377c478bd9Sstevel@tonic-gate 					} while (--c);
12387c478bd9Sstevel@tonic-gate 					break;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 				case LCNBEBCDIC:
12417c478bd9Sstevel@tonic-gate 					do {
12427c478bd9Sstevel@tonic-gate 						*op++ = atoe[utol[*ip++]];
12437c478bd9Sstevel@tonic-gate 					} while (--c);
12447c478bd9Sstevel@tonic-gate 					break;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 				case UCNBEBCDIC:
12477c478bd9Sstevel@tonic-gate 					do {
12487c478bd9Sstevel@tonic-gate 						*op++ = atoe[ltou[*ip++]];
12497c478bd9Sstevel@tonic-gate 					} while (--c);
12507c478bd9Sstevel@tonic-gate 					break;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 				case NBIBM:
12537c478bd9Sstevel@tonic-gate 					do {
12547c478bd9Sstevel@tonic-gate 						*op++ = atoibm[*ip++];
12557c478bd9Sstevel@tonic-gate 					} while (--c);
12567c478bd9Sstevel@tonic-gate 					break;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 				case LCNBIBM:
12597c478bd9Sstevel@tonic-gate 					do {
12607c478bd9Sstevel@tonic-gate 						*op++ = atoibm[utol[*ip++]];
12617c478bd9Sstevel@tonic-gate 					} while (--c);
12627c478bd9Sstevel@tonic-gate 					break;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 				case UCNBIBM:
12657c478bd9Sstevel@tonic-gate 					do {
12667c478bd9Sstevel@tonic-gate 						*op++ = atoibm[ltou[*ip++]];
12677c478bd9Sstevel@tonic-gate 					} while (--c);
12687c478bd9Sstevel@tonic-gate 					break;
12697c478bd9Sstevel@tonic-gate 				}
1270*b928ac84SToomas Soome 				if (obc >= obs) {
12717c478bd9Sstevel@tonic-gate 					op = flsh();
12727c478bd9Sstevel@tonic-gate 				}
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 			break;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 	/* Convert from blocked records to lines terminated by newline */
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 		case UNBLOCK:
12797c478bd9Sstevel@tonic-gate 		case LCUNBLOCK:
12807c478bd9Sstevel@tonic-gate 		case UCUNBLOCK:
12817c478bd9Sstevel@tonic-gate 		case ASCII:
12827c478bd9Sstevel@tonic-gate 		case LCASCII:
12837c478bd9Sstevel@tonic-gate 		case UCASCII:
1284*b928ac84SToomas Soome 			while ((c = ibc) != 0) {
1285*b928ac84SToomas Soome 				if (c > (cbs - cbc)) {
1286*b928ac84SToomas Soome 					/* if more than one record, */
12877c478bd9Sstevel@tonic-gate 					c = cbs - cbc;
1288*b928ac84SToomas Soome 					/* only copy one record */
12897c478bd9Sstevel@tonic-gate 				}
12907c478bd9Sstevel@tonic-gate 				ibc -= c;
12917c478bd9Sstevel@tonic-gate 				cbc += c;
12927c478bd9Sstevel@tonic-gate 				obc += c;
1293*b928ac84SToomas Soome 				switch (conv) {
12947c478bd9Sstevel@tonic-gate 				case UNBLOCK:
12957c478bd9Sstevel@tonic-gate 					do {
12967c478bd9Sstevel@tonic-gate 						*op++ = *ip++;
12977c478bd9Sstevel@tonic-gate 					} while (--c);
12987c478bd9Sstevel@tonic-gate 					break;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 				case LCUNBLOCK:
13017c478bd9Sstevel@tonic-gate 					do {
13027c478bd9Sstevel@tonic-gate 						*op++ = utol[*ip++];
13037c478bd9Sstevel@tonic-gate 					} while (--c);
13047c478bd9Sstevel@tonic-gate 					break;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 				case UCUNBLOCK:
13077c478bd9Sstevel@tonic-gate 					do {
13087c478bd9Sstevel@tonic-gate 						*op++ = ltou[*ip++];
13097c478bd9Sstevel@tonic-gate 					} while (--c);
13107c478bd9Sstevel@tonic-gate 					break;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 				case ASCII:
13137c478bd9Sstevel@tonic-gate 					do {
13147c478bd9Sstevel@tonic-gate 						*op++ = etoa[*ip++];
13157c478bd9Sstevel@tonic-gate 					} while (--c);
13167c478bd9Sstevel@tonic-gate 					break;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 				case LCASCII:
13197c478bd9Sstevel@tonic-gate 					do {
13207c478bd9Sstevel@tonic-gate 						*op++ = utol[etoa[*ip++]];
13217c478bd9Sstevel@tonic-gate 					} while (--c);
13227c478bd9Sstevel@tonic-gate 					break;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 				case UCASCII:
13257c478bd9Sstevel@tonic-gate 					do {
13267c478bd9Sstevel@tonic-gate 						*op++ = ltou[etoa[*ip++]];
13277c478bd9Sstevel@tonic-gate 					} while (--c);
13287c478bd9Sstevel@tonic-gate 					break;
13297c478bd9Sstevel@tonic-gate 				}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 				/* Trim trailing blanks if the line is full */
13327c478bd9Sstevel@tonic-gate 
1333*b928ac84SToomas Soome 				if (cbc == cbs) {
13347c478bd9Sstevel@tonic-gate 					c = cbs; /* `do - while' is usually */
13357c478bd9Sstevel@tonic-gate 					do {		/* faster than `for' */
1336*b928ac84SToomas Soome 						if ((*--op) != ' ') {
13377c478bd9Sstevel@tonic-gate 							op++;
13387c478bd9Sstevel@tonic-gate 							break;
13397c478bd9Sstevel@tonic-gate 						}
13407c478bd9Sstevel@tonic-gate 					} while (--c);
13417c478bd9Sstevel@tonic-gate 					*op++ = '\n';
13427c478bd9Sstevel@tonic-gate 					obc -= cbs - c - 1;
13437c478bd9Sstevel@tonic-gate 					cbc = 0;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 					/* Flush the output buffer if full */
13467c478bd9Sstevel@tonic-gate 
1347*b928ac84SToomas Soome 					while (obc >= obs) {
13487c478bd9Sstevel@tonic-gate 						op = flsh();
13497c478bd9Sstevel@tonic-gate 					}
13507c478bd9Sstevel@tonic-gate 				}
13517c478bd9Sstevel@tonic-gate 			}
13527c478bd9Sstevel@tonic-gate 			break;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		/* Convert to blocked records */
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 		case BLOCK:
13577c478bd9Sstevel@tonic-gate 		case LCBLOCK:
13587c478bd9Sstevel@tonic-gate 		case UCBLOCK:
13597c478bd9Sstevel@tonic-gate 		case EBCDIC:
13607c478bd9Sstevel@tonic-gate 		case LCEBCDIC:
13617c478bd9Sstevel@tonic-gate 		case UCEBCDIC:
13627c478bd9Sstevel@tonic-gate 		case IBM:
13637c478bd9Sstevel@tonic-gate 		case LCIBM:
13647c478bd9Sstevel@tonic-gate 		case UCIBM:
1365*b928ac84SToomas Soome 			while ((c = ibc) != 0) {
13667c478bd9Sstevel@tonic-gate 				int nlflag = 0;
13677c478bd9Sstevel@tonic-gate 
1368*b928ac84SToomas Soome 				/*
1369*b928ac84SToomas Soome 				 * We may have to skip to the end of a long
1370*b928ac84SToomas Soome 				 * line.
1371*b928ac84SToomas Soome 				 */
13727c478bd9Sstevel@tonic-gate 
1373*b928ac84SToomas Soome 				if (skipf) {
13747c478bd9Sstevel@tonic-gate 					do {
1375*b928ac84SToomas Soome 						if ((ic = *ip++) == '\n') {
13767c478bd9Sstevel@tonic-gate 							skipf = 0;
13777c478bd9Sstevel@tonic-gate 							c--;
13787c478bd9Sstevel@tonic-gate 							break;
13797c478bd9Sstevel@tonic-gate 						}
13807c478bd9Sstevel@tonic-gate 					} while (--c);
1381*b928ac84SToomas Soome 					if ((ibc = c) == 0) {
13827c478bd9Sstevel@tonic-gate 						continue;
1383*b928ac84SToomas Soome 						/* read another block */
13847c478bd9Sstevel@tonic-gate 					}
13857c478bd9Sstevel@tonic-gate 				}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 				/* If anything left, copy until newline */
13887c478bd9Sstevel@tonic-gate 
1389*b928ac84SToomas Soome 				if (c > (cbs - cbc + 1)) {
13907c478bd9Sstevel@tonic-gate 					c = cbs - cbc + 1;
13917c478bd9Sstevel@tonic-gate 				}
13927c478bd9Sstevel@tonic-gate 				ibc -= c;
13937c478bd9Sstevel@tonic-gate 				cbc += c;
13947c478bd9Sstevel@tonic-gate 				obc += c;
13957c478bd9Sstevel@tonic-gate 
1396*b928ac84SToomas Soome 				switch (conv) {
13977c478bd9Sstevel@tonic-gate 				case BLOCK:
13987c478bd9Sstevel@tonic-gate 					do {
1399*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14007c478bd9Sstevel@tonic-gate 							*op++ = ic;
1401*b928ac84SToomas Soome 						} else {
14027c478bd9Sstevel@tonic-gate 							nlflag = 1;
14037c478bd9Sstevel@tonic-gate 							break;
14047c478bd9Sstevel@tonic-gate 						}
14057c478bd9Sstevel@tonic-gate 					} while (--c);
14067c478bd9Sstevel@tonic-gate 					break;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 				case LCBLOCK:
14097c478bd9Sstevel@tonic-gate 					do {
1410*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14117c478bd9Sstevel@tonic-gate 							*op++ = utol[ic];
1412*b928ac84SToomas Soome 						} else {
14137c478bd9Sstevel@tonic-gate 							nlflag = 1;
14147c478bd9Sstevel@tonic-gate 							break;
14157c478bd9Sstevel@tonic-gate 						}
14167c478bd9Sstevel@tonic-gate 					} while (--c);
14177c478bd9Sstevel@tonic-gate 					break;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 				case UCBLOCK:
14207c478bd9Sstevel@tonic-gate 					do {
1421*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14227c478bd9Sstevel@tonic-gate 							*op++ = ltou[ic];
1423*b928ac84SToomas Soome 						} else {
14247c478bd9Sstevel@tonic-gate 							nlflag = 1;
14257c478bd9Sstevel@tonic-gate 							break;
14267c478bd9Sstevel@tonic-gate 						}
14277c478bd9Sstevel@tonic-gate 					} while (--c);
14287c478bd9Sstevel@tonic-gate 					break;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 				case EBCDIC:
14317c478bd9Sstevel@tonic-gate 					do {
1432*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14337c478bd9Sstevel@tonic-gate 							*op++ = atoe[ic];
1434*b928ac84SToomas Soome 						} else {
14357c478bd9Sstevel@tonic-gate 							nlflag = 1;
14367c478bd9Sstevel@tonic-gate 							break;
14377c478bd9Sstevel@tonic-gate 						}
14387c478bd9Sstevel@tonic-gate 					} while (--c);
14397c478bd9Sstevel@tonic-gate 					break;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 				case LCEBCDIC:
14427c478bd9Sstevel@tonic-gate 					do {
1443*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14447c478bd9Sstevel@tonic-gate 							*op++ = atoe[utol[ic]];
1445*b928ac84SToomas Soome 						} else {
14467c478bd9Sstevel@tonic-gate 							nlflag = 1;
14477c478bd9Sstevel@tonic-gate 							break;
14487c478bd9Sstevel@tonic-gate 						}
14497c478bd9Sstevel@tonic-gate 					} while (--c);
14507c478bd9Sstevel@tonic-gate 					break;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 				case UCEBCDIC:
14537c478bd9Sstevel@tonic-gate 					do {
1454*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14557c478bd9Sstevel@tonic-gate 							*op++ = atoe[ltou[ic]];
1456*b928ac84SToomas Soome 						} else {
14577c478bd9Sstevel@tonic-gate 							nlflag = 1;
14587c478bd9Sstevel@tonic-gate 							break;
14597c478bd9Sstevel@tonic-gate 						}
14607c478bd9Sstevel@tonic-gate 					} while (--c);
14617c478bd9Sstevel@tonic-gate 					break;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 				case IBM:
14647c478bd9Sstevel@tonic-gate 					do {
1465*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
14667c478bd9Sstevel@tonic-gate 							*op++ = atoibm[ic];
1467*b928ac84SToomas Soome 						} else {
14687c478bd9Sstevel@tonic-gate 							nlflag = 1;
14697c478bd9Sstevel@tonic-gate 							break;
14707c478bd9Sstevel@tonic-gate 						}
14717c478bd9Sstevel@tonic-gate 					} while (--c);
14727c478bd9Sstevel@tonic-gate 					break;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 				case LCIBM:
14757c478bd9Sstevel@tonic-gate 					do {
1476*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
1477*b928ac84SToomas Soome 							*op++ =
1478*b928ac84SToomas Soome 							    atoibm[utol[ic]];
1479*b928ac84SToomas Soome 						} else {
14807c478bd9Sstevel@tonic-gate 							nlflag = 1;
14817c478bd9Sstevel@tonic-gate 							break;
14827c478bd9Sstevel@tonic-gate 						}
14837c478bd9Sstevel@tonic-gate 					} while (--c);
14847c478bd9Sstevel@tonic-gate 					break;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 				case UCIBM:
14877c478bd9Sstevel@tonic-gate 					do {
1488*b928ac84SToomas Soome 						if ((ic = *ip++) != '\n') {
1489*b928ac84SToomas Soome 							*op++ =
1490*b928ac84SToomas Soome 							    atoibm[ltou[ic]];
1491*b928ac84SToomas Soome 						} else {
14927c478bd9Sstevel@tonic-gate 							nlflag = 1;
14937c478bd9Sstevel@tonic-gate 							break;
14947c478bd9Sstevel@tonic-gate 						}
14957c478bd9Sstevel@tonic-gate 					} while (--c);
14967c478bd9Sstevel@tonic-gate 					break;
14977c478bd9Sstevel@tonic-gate 				}
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 			/* If newline found, update all the counters and */
15007c478bd9Sstevel@tonic-gate 			/* pointers, pad with trailing blanks if necessary */
15017c478bd9Sstevel@tonic-gate 
1502*b928ac84SToomas Soome 				if (nlflag) {
15037c478bd9Sstevel@tonic-gate 					ibc += c - 1;
15047c478bd9Sstevel@tonic-gate 					obc += cbs - cbc;
15057c478bd9Sstevel@tonic-gate 					c += cbs - cbc;
15067c478bd9Sstevel@tonic-gate 					cbc = 0;
1507*b928ac84SToomas Soome 					if (c > 0) {
15087c478bd9Sstevel@tonic-gate 					/* Use the right kind of blank */
15097c478bd9Sstevel@tonic-gate 
1510*b928ac84SToomas Soome 						switch (conv) {
15117c478bd9Sstevel@tonic-gate 						case BLOCK:
15127c478bd9Sstevel@tonic-gate 						case LCBLOCK:
15137c478bd9Sstevel@tonic-gate 						case UCBLOCK:
15147c478bd9Sstevel@tonic-gate 							ic = ' ';
15157c478bd9Sstevel@tonic-gate 							break;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 						case EBCDIC:
15187c478bd9Sstevel@tonic-gate 						case LCEBCDIC:
15197c478bd9Sstevel@tonic-gate 						case UCEBCDIC:
15207c478bd9Sstevel@tonic-gate 							ic = atoe[' '];
15217c478bd9Sstevel@tonic-gate 							break;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 						case IBM:
15247c478bd9Sstevel@tonic-gate 						case LCIBM:
15257c478bd9Sstevel@tonic-gate 						case UCIBM:
15267c478bd9Sstevel@tonic-gate 							ic = atoibm[' '];
15277c478bd9Sstevel@tonic-gate 							break;
15287c478bd9Sstevel@tonic-gate 						}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 						/* Pad with trailing blanks */
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 						do {
15337c478bd9Sstevel@tonic-gate 							*op++ = ic;
15347c478bd9Sstevel@tonic-gate 						} while (--c);
15357c478bd9Sstevel@tonic-gate 					}
15367c478bd9Sstevel@tonic-gate 				}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 			/* If not end of line, this line may be too long */
15397c478bd9Sstevel@tonic-gate 
1540*b928ac84SToomas Soome 				else if (cbc > cbs) {
15417c478bd9Sstevel@tonic-gate 					skipf = 1; /* note skip in progress */
15427c478bd9Sstevel@tonic-gate 					obc--;
15437c478bd9Sstevel@tonic-gate 					op--;
15447c478bd9Sstevel@tonic-gate 					cbc = 0;
15457c478bd9Sstevel@tonic-gate 					ntrunc++;  /* count another long line */
15467c478bd9Sstevel@tonic-gate 				}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 				/* Flush the output buffer if full */
15497c478bd9Sstevel@tonic-gate 
1550*b928ac84SToomas Soome 				while (obc >= obs) {
15517c478bd9Sstevel@tonic-gate 					op = flsh();
15527c478bd9Sstevel@tonic-gate 				}
15537c478bd9Sstevel@tonic-gate 			}
15547c478bd9Sstevel@tonic-gate 			break;
15557c478bd9Sstevel@tonic-gate 		}
15567c478bd9Sstevel@tonic-gate 	}
1557a77d64afScf 	/* NOTREACHED */
1558a77d64afScf 	return (0);
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /* match ************************************************************** */
15627c478bd9Sstevel@tonic-gate /*									*/
15637c478bd9Sstevel@tonic-gate /* Compare two text strings for equality				*/
15647c478bd9Sstevel@tonic-gate /*									*/
15657c478bd9Sstevel@tonic-gate /* Arg:		s - pointer to string to match with a command arg	*/
15667c478bd9Sstevel@tonic-gate /* Global arg:	string - pointer to command arg				*/
15677c478bd9Sstevel@tonic-gate /*									*/
15687c478bd9Sstevel@tonic-gate /* Return:	1 if match, 0 if no match				*/
15697c478bd9Sstevel@tonic-gate /*		If match, also reset `string' to point to the text	*/
15707c478bd9Sstevel@tonic-gate /*		that follows the matching text.				*/
15717c478bd9Sstevel@tonic-gate /*									*/
15727c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate static int
1575*b928ac84SToomas Soome match(char *s)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	char *cs;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	cs = string;
1580*b928ac84SToomas Soome 	while (*cs++ == *s) {
1581*b928ac84SToomas Soome 		if (*s++ == '\0') {
15827c478bd9Sstevel@tonic-gate 			goto true;
15837c478bd9Sstevel@tonic-gate 		}
15847c478bd9Sstevel@tonic-gate 	}
1585*b928ac84SToomas Soome 	if (*s != '\0') {
15867c478bd9Sstevel@tonic-gate 		return (0);
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate true:
15907c478bd9Sstevel@tonic-gate 	cs--;
15917c478bd9Sstevel@tonic-gate 	string = cs;
15927c478bd9Sstevel@tonic-gate 	return (1);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate /* number ************************************************************* */
15967c478bd9Sstevel@tonic-gate /*									*/
15977c478bd9Sstevel@tonic-gate /* Convert a numeric arg to binary					*/
15987c478bd9Sstevel@tonic-gate /*									*/
15997c478bd9Sstevel@tonic-gate /* Arg:		big - maximum valid input number			*/
16007c478bd9Sstevel@tonic-gate /* Global arg:	string - pointer to command arg				*/
16017c478bd9Sstevel@tonic-gate /*									*/
16024cddff70SJosef 'Jeff' Sipek /* Valid forms:	123 | 123k | 123M | 123G | 123T | 123P | 123E | 123Z |	*/
16034cddff70SJosef 'Jeff' Sipek /*		123w | 123b | 123*123 | 123x123				*/
16047c478bd9Sstevel@tonic-gate /*		plus combinations such as 2b*3kw*4w			*/
16057c478bd9Sstevel@tonic-gate /*									*/
16067c478bd9Sstevel@tonic-gate /* Return:	converted number					*/
16077c478bd9Sstevel@tonic-gate /*									*/
16087c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static unsigned long long
1611*b928ac84SToomas Soome number(long long big)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	char *cs;
16147c478bd9Sstevel@tonic-gate 	long long n;
16157c478bd9Sstevel@tonic-gate 	long long cut = BIG / 10;	/* limit to avoid overflow */
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	cs = string;
16187c478bd9Sstevel@tonic-gate 	n = 0;
1619*b928ac84SToomas Soome 	while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) {
1620*b928ac84SToomas Soome 		n = n * 10 + *cs++ - '0';
16217c478bd9Sstevel@tonic-gate 	}
1622*b928ac84SToomas Soome 	for (;;) {
1623*b928ac84SToomas Soome 		switch (*cs++) {
16247c478bd9Sstevel@tonic-gate 
16254cddff70SJosef 'Jeff' Sipek 		case 'Z':
16264cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16274cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16284cddff70SJosef 'Jeff' Sipek 
16294cddff70SJosef 'Jeff' Sipek 		case 'E':
16304cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16314cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16324cddff70SJosef 'Jeff' Sipek 
16334cddff70SJosef 'Jeff' Sipek 		case 'P':
16344cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16354cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16364cddff70SJosef 'Jeff' Sipek 
16374cddff70SJosef 'Jeff' Sipek 		case 'T':
16384cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16394cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16404cddff70SJosef 'Jeff' Sipek 
16414cddff70SJosef 'Jeff' Sipek 		case 'G':
16424cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16434cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16444cddff70SJosef 'Jeff' Sipek 
16454cddff70SJosef 'Jeff' Sipek 		case 'M':
16464cddff70SJosef 'Jeff' Sipek 			n *= 1024;
16474cddff70SJosef 'Jeff' Sipek 			/* FALLTHROUGH */
16484cddff70SJosef 'Jeff' Sipek 
16497c478bd9Sstevel@tonic-gate 		case 'k':
16507c478bd9Sstevel@tonic-gate 			n *= 1024;
16517c478bd9Sstevel@tonic-gate 			continue;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		case 'w':
16547c478bd9Sstevel@tonic-gate 			n *= 2;
16557c478bd9Sstevel@tonic-gate 			continue;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 		case 'b':
16587c478bd9Sstevel@tonic-gate 			n *= BSIZE;
16597c478bd9Sstevel@tonic-gate 			continue;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		case '*':
16627c478bd9Sstevel@tonic-gate 		case 'x':
16637c478bd9Sstevel@tonic-gate 			string = cs;
16647c478bd9Sstevel@tonic-gate 			n *= number(BIG);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
16677c478bd9Sstevel@tonic-gate 		/* Fall into exit test, recursion has read rest of string */
16687c478bd9Sstevel@tonic-gate 		/* End of string, check for a valid number */
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		case '\0':
1671*b928ac84SToomas Soome 			if ((n > big) || (n < 0)) {
16727c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s \"%llu\"\n",
1673*b928ac84SToomas Soome 				    gettext("argument out of range:"), n);
16747c478bd9Sstevel@tonic-gate 				exit(2);
16757c478bd9Sstevel@tonic-gate 			}
16767c478bd9Sstevel@tonic-gate 			return (n);
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		default:
16797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dd: %s \"%s\"\n",
1680*b928ac84SToomas Soome 			    gettext("bad numeric argument:"), string);
16817c478bd9Sstevel@tonic-gate 			exit(2);
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 	} /* never gets here */
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /* flsh *************************************************************** */
16877c478bd9Sstevel@tonic-gate /*									*/
16887c478bd9Sstevel@tonic-gate /* Flush the output buffer, move any excess bytes down to the beginning	*/
16897c478bd9Sstevel@tonic-gate /*									*/
16907c478bd9Sstevel@tonic-gate /* Arg:		none							*/
169161304e4fSPaul Dagnelie /* Global args:	obuf, obc, obs, nofr, nopr, ostriden			*/
16927c478bd9Sstevel@tonic-gate /*									*/
16937c478bd9Sstevel@tonic-gate /* Return:	Pointer to the first free byte in the output buffer.	*/
16947c478bd9Sstevel@tonic-gate /*		Also reset `obc' to account for moved bytes.		*/
16957c478bd9Sstevel@tonic-gate /*									*/
16967c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
16977c478bd9Sstevel@tonic-gate 
1698*b928ac84SToomas Soome static unsigned char *
1699*b928ac84SToomas Soome flsh(void)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	unsigned char *op, *cp;
17027c478bd9Sstevel@tonic-gate 	int bc;
17037c478bd9Sstevel@tonic-gate 	unsigned int oc;
17047c478bd9Sstevel@tonic-gate 
1705*b928ac84SToomas Soome 	if (obc) {			/* don't flush if the buffer is empty */
17067c478bd9Sstevel@tonic-gate 		if (obc >= obs) {
17077c478bd9Sstevel@tonic-gate 			oc = obs;
17087c478bd9Sstevel@tonic-gate 			nofr++;		/* count a full output buffer */
1709*b928ac84SToomas Soome 		} else {
17107c478bd9Sstevel@tonic-gate 			oc = obc;
17117c478bd9Sstevel@tonic-gate 			nopr++;		/* count a partial output buffer */
17127c478bd9Sstevel@tonic-gate 		}
17137c478bd9Sstevel@tonic-gate 		bc = write(obf, (char *)obuf, oc);
17147c478bd9Sstevel@tonic-gate 		if (bc != oc) {
1715*b928ac84SToomas Soome 			if (bc < 0) {
17167c478bd9Sstevel@tonic-gate 				perror("write");
1717*b928ac84SToomas Soome 			} else {
1718*b928ac84SToomas Soome 				(void) fprintf(stderr,
1719*b928ac84SToomas Soome 				    gettext("dd: unexpected short write, "
1720*b928ac84SToomas Soome 				    "wrote %d bytes, expected %d\n"), bc, oc);
1721*b928ac84SToomas Soome 			}
17227c478bd9Sstevel@tonic-gate 			term(2);
17237c478bd9Sstevel@tonic-gate 		}
172461304e4fSPaul Dagnelie 
172561304e4fSPaul Dagnelie 		if (ostriden > 0 && lseek(obf, ostriden * ((off_t)obs),
172661304e4fSPaul Dagnelie 		    SEEK_CUR) == -1) {
172761304e4fSPaul Dagnelie 			perror("lseek");
172861304e4fSPaul Dagnelie 			exit(2);
172961304e4fSPaul Dagnelie 		}
173061304e4fSPaul Dagnelie 
17317c478bd9Sstevel@tonic-gate 		obc -= oc;
17327c478bd9Sstevel@tonic-gate 		op = obuf;
173319d32b9aSRobert Mustacchi 		obytes += bc;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		/* If any data in the conversion buffer, move it into */
17367c478bd9Sstevel@tonic-gate 		/* the output buffer */
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		if (obc) {
17397c478bd9Sstevel@tonic-gate 			cp = obuf + obs;
17407c478bd9Sstevel@tonic-gate 			bc = obc;
17417c478bd9Sstevel@tonic-gate 			do {
17427c478bd9Sstevel@tonic-gate 				*op++ = *cp++;
17437c478bd9Sstevel@tonic-gate 			} while (--bc);
17447c478bd9Sstevel@tonic-gate 		}
17457c478bd9Sstevel@tonic-gate 		return (op);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 	return (obuf);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /* term *************************************************************** */
17517c478bd9Sstevel@tonic-gate /*									*/
17527c478bd9Sstevel@tonic-gate /* Write record statistics, then exit					*/
17537c478bd9Sstevel@tonic-gate /*									*/
17547c478bd9Sstevel@tonic-gate /* Arg:		c - exit status code					*/
17557c478bd9Sstevel@tonic-gate /*									*/
17567c478bd9Sstevel@tonic-gate /* Return:	no return, calls exit					*/
17577c478bd9Sstevel@tonic-gate /*									*/
17587c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate static void
1761*b928ac84SToomas Soome term(int c)
17627c478bd9Sstevel@tonic-gate {
17637c478bd9Sstevel@tonic-gate 	stats();
17647c478bd9Sstevel@tonic-gate 	exit(c);
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate /* stats ************************************************************** */
17687c478bd9Sstevel@tonic-gate /*									*/
17697c478bd9Sstevel@tonic-gate /* Write record statistics onto standard error				*/
17707c478bd9Sstevel@tonic-gate /*									*/
17717c478bd9Sstevel@tonic-gate /* Args:	none							*/
1772*b928ac84SToomas Soome /* Global args:	nifr, nipr, nofr, nopr, ntrunc, obytes			*/
17737c478bd9Sstevel@tonic-gate /*									*/
17747c478bd9Sstevel@tonic-gate /* Return:	void							*/
17757c478bd9Sstevel@tonic-gate /*									*/
17767c478bd9Sstevel@tonic-gate /* ********************************************************************	*/
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate static void
1779*b928ac84SToomas Soome stats(void)
17807c478bd9Sstevel@tonic-gate {
178119d32b9aSRobert Mustacchi 	hrtime_t delta = gethrtime() - startt;
178219d32b9aSRobert Mustacchi 	double secs = delta * 1e-9;
1783*b928ac84SToomas Soome 	char nnum[NN_NUMBUF_SZ];
178419d32b9aSRobert Mustacchi 
17857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%llu+%llu records in\n"), nifr, nipr);
17867c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%llu+%llu records out\n"), nofr, nopr);
17877c478bd9Sstevel@tonic-gate 	if (ntrunc) {
17887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1789*b928ac84SToomas Soome 		    gettext("%llu truncated record(s)\n"), ntrunc);
17907c478bd9Sstevel@tonic-gate 	}
179119d32b9aSRobert Mustacchi 
179219d32b9aSRobert Mustacchi 	/*
179319d32b9aSRobert Mustacchi 	 * If we got here before we started copying somehow, don't bother
179419d32b9aSRobert Mustacchi 	 * printing the rest.
179519d32b9aSRobert Mustacchi 	 */
179619d32b9aSRobert Mustacchi 	if (startt == 0)
179719d32b9aSRobert Mustacchi 		return;
179819d32b9aSRobert Mustacchi 
1799*b928ac84SToomas Soome 	nicenum((uint64_t)obytes / secs, nnum, sizeof (nnum));
180019d32b9aSRobert Mustacchi 	(void) fprintf(stderr,
1801*b928ac84SToomas Soome 	    gettext("%llu bytes transferred in %.6f secs (%sB/sec)\n"),
1802*b928ac84SToomas Soome 	    obytes, secs, nnum);
18037c478bd9Sstevel@tonic-gate }
1804