xref: /illumos-gate/usr/src/cmd/dd/dd.c (revision c53c97f7)
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.
29*c53c97f7SRobert Mustacchi  * Copyright 2021 Oxide Computer Company
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
32a77d64afScf /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
33b928ac84SToomas Soome /*		All Rights Reserved	*/
34a77d64afScf 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  *	convert and copy
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	<stdio.h>
407c478bd9Sstevel@tonic-gate #include	<signal.h>
417c478bd9Sstevel@tonic-gate #include	<fcntl.h>
427c478bd9Sstevel@tonic-gate #include	<sys/param.h>
437c478bd9Sstevel@tonic-gate #include	<sys/types.h>
447c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
467c478bd9Sstevel@tonic-gate #include	<unistd.h>
477c478bd9Sstevel@tonic-gate #include	<stdlib.h>
487c478bd9Sstevel@tonic-gate #include	<locale.h>
497c478bd9Sstevel@tonic-gate #include	<string.h>
5019d32b9aSRobert Mustacchi #include	<sys/time.h>
5119d32b9aSRobert Mustacchi #include	<errno.h>
5219d32b9aSRobert Mustacchi #include	<strings.h>
53b928ac84SToomas Soome #include	<libcmdutils.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* The BIG parameter is machine dependent.  It should be a long integer	*/
567c478bd9Sstevel@tonic-gate /* constant that can be used by the number parser to check the validity	*/
577c478bd9Sstevel@tonic-gate /* of numeric parameters.  On 16-bit machines, it should probably be	*/
587c478bd9Sstevel@tonic-gate /* the maximum unsigned integer, 0177777L.  On 32-bit machines where	*/
597c478bd9Sstevel@tonic-gate /* longs are the same size as ints, the maximum signed integer is more	*/
607c478bd9Sstevel@tonic-gate /* appropriate.  This value is 017777777777L. In 64 bit environments,   */
617c478bd9Sstevel@tonic-gate /* the maximum signed integer value is 0777777777777777777777LL		*/
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	BIG	0777777777777777777777LL
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	BSIZE	512
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* Option parameters */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	COPY		0	/* file copy, preserve input block size */
707c478bd9Sstevel@tonic-gate #define	REBLOCK		1	/* file copy, change block size */
717c478bd9Sstevel@tonic-gate #define	LCREBLOCK	2	/* file copy, convert to lower case */
727c478bd9Sstevel@tonic-gate #define	UCREBLOCK	3	/* file copy, convert to upper case */
737c478bd9Sstevel@tonic-gate #define	NBASCII		4	/* file copy, convert from EBCDIC to ASCII */
747c478bd9Sstevel@tonic-gate #define	LCNBASCII	5	/* file copy, EBCDIC to lower case ASCII */
757c478bd9Sstevel@tonic-gate #define	UCNBASCII	6	/* file copy, EBCDIC to upper case ASCII */
767c478bd9Sstevel@tonic-gate #define	NBEBCDIC	7	/* file copy, convert from ASCII to EBCDIC */
777c478bd9Sstevel@tonic-gate #define	LCNBEBCDIC	8	/* file copy, ASCII to lower case EBCDIC */
787c478bd9Sstevel@tonic-gate #define	UCNBEBCDIC	9	/* file copy, ASCII to upper case EBCDIC */
797c478bd9Sstevel@tonic-gate #define	NBIBM		10	/* file copy, convert from ASCII to IBM */
807c478bd9Sstevel@tonic-gate #define	LCNBIBM		11	/* file copy, ASCII to lower case IBM */
817c478bd9Sstevel@tonic-gate #define	UCNBIBM		12	/* file copy, ASCII to upper case IBM */
827c478bd9Sstevel@tonic-gate #define	UNBLOCK		13	/* convert blocked ASCII to ASCII */
837c478bd9Sstevel@tonic-gate #define	LCUNBLOCK	14	/* convert blocked ASCII to lower case ASCII */
847c478bd9Sstevel@tonic-gate #define	UCUNBLOCK	15	/* convert blocked ASCII to upper case ASCII */
857c478bd9Sstevel@tonic-gate #define	ASCII		16	/* convert blocked EBCDIC to ASCII */
867c478bd9Sstevel@tonic-gate #define	LCASCII		17	/* convert blocked EBCDIC to lower case ASCII */
877c478bd9Sstevel@tonic-gate #define	UCASCII		18	/* convert blocked EBCDIC to upper case ASCII */
887c478bd9Sstevel@tonic-gate #define	BLOCK		19	/* convert ASCII to blocked ASCII */
897c478bd9Sstevel@tonic-gate #define	LCBLOCK		20	/* convert ASCII to lower case blocked ASCII */
907c478bd9Sstevel@tonic-gate #define	UCBLOCK		21	/* convert ASCII to upper case blocked ASCII */
917c478bd9Sstevel@tonic-gate #define	EBCDIC		22	/* convert ASCII to blocked EBCDIC */
927c478bd9Sstevel@tonic-gate #define	LCEBCDIC	23	/* convert ASCII to lower case blocked EBCDIC */
937c478bd9Sstevel@tonic-gate #define	UCEBCDIC	24	/* convert ASCII to upper case blocked EBCDIC */
947c478bd9Sstevel@tonic-gate #define	IBM		25	/* convert ASCII to blocked IBM */
957c478bd9Sstevel@tonic-gate #define	LCIBM		26	/* convert ASCII to lower case blocked IBM */
967c478bd9Sstevel@tonic-gate #define	UCIBM		27	/* convert ASCII to upper case blocked IBM */
977c478bd9Sstevel@tonic-gate #define	LCASE		01	/* flag - convert to lower case */
987c478bd9Sstevel@tonic-gate #define	UCASE		02	/* flag - convert to upper case */
997c478bd9Sstevel@tonic-gate #define	SWAB		04	/* flag - swap bytes before conversion */
1007c478bd9Sstevel@tonic-gate #define	NERR		010	/* flag - proceed on input errors */
1017c478bd9Sstevel@tonic-gate #define	SYNC		020	/* flag - pad short input blocks with nulls */
102c232df92SToomas Soome #define	FULLBLOCK	040	/* flag - accumulate full blocks of input */
1037c478bd9Sstevel@tonic-gate #define	BADLIMIT	5	/* give up if no progress after BADLIMIT trys */
1047c478bd9Sstevel@tonic-gate #define	SVR4XLATE	0	/* use default EBCDIC translation */
1057c478bd9Sstevel@tonic-gate #define	BSDXLATE	1	/* use BSD-compatible EBCDIC translation */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	USAGE\
1087c478bd9Sstevel@tonic-gate 	"usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
1097c478bd9Sstevel@tonic-gate 	"	   [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
11061304e4fSPaul Dagnelie 	"	   [iseek=n] [oseek=n] [seek=n] [stride=n] [istride=n]\n"\
111c232df92SToomas Soome 	"	   [ostride=n] [count=n] [conv=[ascii][,ebcdic][,ibm]\n"\
11261304e4fSPaul Dagnelie 	"	   [,asciib][,ebcdicb][,ibmb][,block|unblock][,lcase|ucase]\n"\
11361304e4fSPaul Dagnelie 	"	   [,swab][,noerror][,notrunc][,sync]]\n"\
114c232df92SToomas Soome 	"	   [iflag=[fullblock]] [oflag=[dsync][sync]]\n"
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* Global references */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* Local routine declarations */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static int	match(char *);
121*c53c97f7SRobert Mustacchi static void		term(int);
122*c53c97f7SRobert Mustacchi static unsigned long long	number(long long);
123*c53c97f7SRobert Mustacchi static unsigned char	*flsh(void);
124*c53c97f7SRobert Mustacchi static void		stats(boolean_t);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* Local data definitions */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static unsigned ibs;	/* input buffer size */
1297c478bd9Sstevel@tonic-gate static unsigned obs;	/* output buffer size */
1307c478bd9Sstevel@tonic-gate static unsigned bs;	/* buffer size, overrules ibs and obs */
1317c478bd9Sstevel@tonic-gate static unsigned cbs;	/* conversion buffer size, used for block conversions */
1327c478bd9Sstevel@tonic-gate static unsigned ibc;	/* number of bytes still in the input buffer */
1337c478bd9Sstevel@tonic-gate static unsigned obc;	/* number of bytes in the output buffer */
1347c478bd9Sstevel@tonic-gate static unsigned cbc;	/* number of bytes in the conversion buffer */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int	ibf;	/* input file descriptor */
1377c478bd9Sstevel@tonic-gate static int	obf;	/* output file descriptor */
1387c478bd9Sstevel@tonic-gate static int	cflag;	/* conversion option flags */
139c232df92SToomas Soome static int	iflag;	/* input flag options */
14019d32b9aSRobert Mustacchi static int	oflag;	/* output flag options */
1417c478bd9Sstevel@tonic-gate static int	skipf;	/* if skipf == 1, skip rest of input line */
1427c478bd9Sstevel@tonic-gate static unsigned long long	nifr;	/* count of full input records */
1437c478bd9Sstevel@tonic-gate static unsigned long long	nipr;	/* count of partial input records */
1447c478bd9Sstevel@tonic-gate static unsigned long long	nofr;	/* count of full output records */
1457c478bd9Sstevel@tonic-gate static unsigned long long	nopr;	/* count of partial output records */
1467c478bd9Sstevel@tonic-gate static unsigned long long	ntrunc;	/* count of truncated input lines */
1477c478bd9Sstevel@tonic-gate static unsigned long long	nbad;	/* count of bad records since last */
1487c478bd9Sstevel@tonic-gate 					/* good one */
1497c478bd9Sstevel@tonic-gate static int	files;	/* number of input files to concatenate (tape only) */
1507c478bd9Sstevel@tonic-gate static off_t	skip;	/* number of input records to skip */
1517c478bd9Sstevel@tonic-gate static off_t	iseekn;	/* number of input records to seek past */
1527c478bd9Sstevel@tonic-gate static off_t	oseekn;	/* number of output records to seek past */
1537c478bd9Sstevel@tonic-gate static unsigned long long	count;	/* number of input records to copy */
1547c478bd9Sstevel@tonic-gate 			/* (0 = all) */
155e3bee069SRobert Mustacchi static boolean_t ecount;	/* explicit count given */
15661304e4fSPaul Dagnelie static off_t	ostriden;	/* number of output blocks to skip between */
15761304e4fSPaul Dagnelie 				/* records */
15861304e4fSPaul Dagnelie static off_t	istriden;	/* number of input blocks to skip between */
15961304e4fSPaul Dagnelie 				/* records */
16061304e4fSPaul Dagnelie 
1617c478bd9Sstevel@tonic-gate static int	trantype; /* BSD or SVr4 compatible EBCDIC */
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static char		*string;	/* command arg pointer */
1647c478bd9Sstevel@tonic-gate static char		*ifile;		/* input file name pointer */
1657c478bd9Sstevel@tonic-gate static char		*ofile;		/* output file name pointer */
1667c478bd9Sstevel@tonic-gate static unsigned char	*ibuf;		/* input buffer pointer */
1677c478bd9Sstevel@tonic-gate static unsigned char	*obuf;		/* output buffer pointer */
1687c478bd9Sstevel@tonic-gate 
16919d32b9aSRobert Mustacchi static hrtime_t		startt;		/* hrtime copy started */
170*c53c97f7SRobert Mustacchi static uint64_t		prog_secs;	/* number of seconds of progress */
17119d32b9aSRobert Mustacchi static unsigned long long	obytes;	/* output bytes */
17219d32b9aSRobert Mustacchi static sig_atomic_t	nstats;		/* do we need to output stats */
17319d32b9aSRobert Mustacchi 
174*c53c97f7SRobert Mustacchi typedef enum dd_status {
175*c53c97f7SRobert Mustacchi 	DD_STATUS_DEFAULT	= 0,
176*c53c97f7SRobert Mustacchi 	DD_STATUS_NONE		= 1 << 0,
177*c53c97f7SRobert Mustacchi 	DD_STATUS_PROGRESS	= 1 << 1,
178*c53c97f7SRobert Mustacchi 	DD_STATUS_NOXFER	= 1 << 2
179*c53c97f7SRobert Mustacchi } dd_status_t;
180*c53c97f7SRobert Mustacchi 
181*c53c97f7SRobert Mustacchi static dd_status_t	status_arg = DD_STATUS_DEFAULT;
182*c53c97f7SRobert Mustacchi static boolean_t	stderr_tty = B_FALSE;
183*c53c97f7SRobert Mustacchi static boolean_t	progress_printed = B_FALSE;
184*c53c97f7SRobert Mustacchi 
1857c478bd9Sstevel@tonic-gate /* This is an EBCDIC to ASCII conversion table	*/
1867c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
1877c478bd9Sstevel@tonic-gate 
188b928ac84SToomas Soome static unsigned char svr4_etoa[] =
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
1917c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
1927c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
1937c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
1947c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
1957c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
1967c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
1977c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
1987c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
1997c478bd9Sstevel@tonic-gate 	0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174,
2007c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
2017c478bd9Sstevel@tonic-gate 	0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176,
2027c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
2037c478bd9Sstevel@tonic-gate 	0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077,
2047c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
2057c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
2067c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
2077c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
2087c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
2097c478bd9Sstevel@tonic-gate 	0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320,
2107c478bd9Sstevel@tonic-gate 	0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170,
2117c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327,
2127c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
2137c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347,
2147c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
2157c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
2167c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
2177c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
2187c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
2197c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
2207c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
2217c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* This is an ASCII to EBCDIC conversion table	*/
2257c478bd9Sstevel@tonic-gate /* from a proposed BTL standard April 16, 1979	*/
2267c478bd9Sstevel@tonic-gate 
227b928ac84SToomas Soome static unsigned char svr4_atoe[] =
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2307c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2317c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2327c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2337c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2347c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2357c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2367c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2377c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2387c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2397c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2407c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155,
2417c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2427c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2437c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2447c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007,
2457c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2467c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2477c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2487c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2497c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2507c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2517c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2527c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2537c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2547c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236,
2557c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257,
2567c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2577c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277,
2587c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2597c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2607c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2617c478bd9Sstevel@tonic-gate };
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /* Table for ASCII to IBM (alternate EBCDIC) code conversion	*/
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate static unsigned char svr4_atoibm[] =
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
2687c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
2697c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
2707c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
2717c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
2727c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
2737c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
2747c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
2757c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
2767c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
2777c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
2787c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
2797c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
2807c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
2817c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
2827c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
2837c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
2847c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
2857c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
2867c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
2877c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
2887c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
2897c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
2907c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
2917c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
2927c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
2937c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
2947c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
2957c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
2967c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
2977c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
2987c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
2997c478bd9Sstevel@tonic-gate };
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to lower case ASCII	*/
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static unsigned char utol[] =
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
3067c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
3077c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
3087c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
3097c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
3107c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
3117c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3127c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
3137c478bd9Sstevel@tonic-gate 	0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3147c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
3157c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3167c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
3177c478bd9Sstevel@tonic-gate 	0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3187c478bd9Sstevel@tonic-gate 	0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
3197c478bd9Sstevel@tonic-gate 	0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
3207c478bd9Sstevel@tonic-gate 	0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
3217c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3227c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3237c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3247c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3257c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3267c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3277c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3287c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3297c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3307c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3317c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3327c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3337c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3347c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3357c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3367c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3377c478bd9Sstevel@tonic-gate };
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /* Table for conversion of ASCII to upper case ASCII	*/
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate static unsigned char ltou[] =
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
3447c478bd9Sstevel@tonic-gate 	0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
3457c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
3467c478bd9Sstevel@tonic-gate 	0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
3477c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
3487c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
3497c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
3507c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
3517c478bd9Sstevel@tonic-gate 	0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3527c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3537c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3547c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
3557c478bd9Sstevel@tonic-gate 	0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
3567c478bd9Sstevel@tonic-gate 	0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
3577c478bd9Sstevel@tonic-gate 	0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
3587c478bd9Sstevel@tonic-gate 	0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
3597c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
3607c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
3617c478bd9Sstevel@tonic-gate 	0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
3627c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
3637c478bd9Sstevel@tonic-gate 	0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
3647c478bd9Sstevel@tonic-gate 	0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3657c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
3667c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
3677c478bd9Sstevel@tonic-gate 	0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
3687c478bd9Sstevel@tonic-gate 	0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
3697c478bd9Sstevel@tonic-gate 	0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
3707c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
3717c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
3727c478bd9Sstevel@tonic-gate 	0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
3737c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
3747c478bd9Sstevel@tonic-gate 	0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /* BSD-compatible EBCDIC to ASCII translate table */
3787c478bd9Sstevel@tonic-gate 
379b928ac84SToomas Soome static unsigned char bsd_etoa[] =
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
3827c478bd9Sstevel@tonic-gate 	0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
3837c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
3847c478bd9Sstevel@tonic-gate 	0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
3857c478bd9Sstevel@tonic-gate 	0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
3867c478bd9Sstevel@tonic-gate 	0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
3877c478bd9Sstevel@tonic-gate 	0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
3887c478bd9Sstevel@tonic-gate 	0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
3897c478bd9Sstevel@tonic-gate 	0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
3907c478bd9Sstevel@tonic-gate 	0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041,
3917c478bd9Sstevel@tonic-gate 	0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
3927c478bd9Sstevel@tonic-gate 	0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136,
3937c478bd9Sstevel@tonic-gate 	0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
3947c478bd9Sstevel@tonic-gate 	0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077,
3957c478bd9Sstevel@tonic-gate 	0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
3967c478bd9Sstevel@tonic-gate 	0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
3977c478bd9Sstevel@tonic-gate 	0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
3987c478bd9Sstevel@tonic-gate 	0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
3997c478bd9Sstevel@tonic-gate 	0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
4007c478bd9Sstevel@tonic-gate 	0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
4017c478bd9Sstevel@tonic-gate 	0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
4027c478bd9Sstevel@tonic-gate 	0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
4037c478bd9Sstevel@tonic-gate 	0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
4047c478bd9Sstevel@tonic-gate 	0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
4057c478bd9Sstevel@tonic-gate 	0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
4067c478bd9Sstevel@tonic-gate 	0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
4077c478bd9Sstevel@tonic-gate 	0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
4087c478bd9Sstevel@tonic-gate 	0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
4097c478bd9Sstevel@tonic-gate 	0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
4107c478bd9Sstevel@tonic-gate 	0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
4117c478bd9Sstevel@tonic-gate 	0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
4127c478bd9Sstevel@tonic-gate 	0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
4137c478bd9Sstevel@tonic-gate };
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to EBCDIC translate table */
4167c478bd9Sstevel@tonic-gate 
417b928ac84SToomas Soome static unsigned char bsd_atoe[] =
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4207c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4217c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4227c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4237c478bd9Sstevel@tonic-gate 	0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
4247c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4257c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4267c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4277c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4287c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4297c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4307c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
4317c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4327c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4337c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4347c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007,
4357c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4367c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4377c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4387c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4397c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4407c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4417c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4427c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4437c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4447c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4457c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4467c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4477c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4487c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4497c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4507c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4517c478bd9Sstevel@tonic-gate };
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /* BSD-compatible ASCII to IBM translate table */
4547c478bd9Sstevel@tonic-gate 
455b928ac84SToomas Soome static unsigned char bsd_atoibm[] =
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
4587c478bd9Sstevel@tonic-gate 	0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
4597c478bd9Sstevel@tonic-gate 	0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
4607c478bd9Sstevel@tonic-gate 	0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
4617c478bd9Sstevel@tonic-gate 	0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
4627c478bd9Sstevel@tonic-gate 	0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
4637c478bd9Sstevel@tonic-gate 	0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
4647c478bd9Sstevel@tonic-gate 	0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
4657c478bd9Sstevel@tonic-gate 	0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
4667c478bd9Sstevel@tonic-gate 	0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
4677c478bd9Sstevel@tonic-gate 	0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
4687c478bd9Sstevel@tonic-gate 	0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
4697c478bd9Sstevel@tonic-gate 	0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
4707c478bd9Sstevel@tonic-gate 	0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
4717c478bd9Sstevel@tonic-gate 	0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
4727c478bd9Sstevel@tonic-gate 	0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
4737c478bd9Sstevel@tonic-gate 	0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
4747c478bd9Sstevel@tonic-gate 	0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
4757c478bd9Sstevel@tonic-gate 	0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
4767c478bd9Sstevel@tonic-gate 	0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
4777c478bd9Sstevel@tonic-gate 	0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
4787c478bd9Sstevel@tonic-gate 	0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
4797c478bd9Sstevel@tonic-gate 	0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
4807c478bd9Sstevel@tonic-gate 	0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
4817c478bd9Sstevel@tonic-gate 	0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
4827c478bd9Sstevel@tonic-gate 	0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
4837c478bd9Sstevel@tonic-gate 	0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
4847c478bd9Sstevel@tonic-gate 	0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
4857c478bd9Sstevel@tonic-gate 	0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
4867c478bd9Sstevel@tonic-gate 	0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
4877c478bd9Sstevel@tonic-gate 	0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
4887c478bd9Sstevel@tonic-gate 	0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
4897c478bd9Sstevel@tonic-gate };
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /* set up to use SVr4 ascii-ebcdic translation by default */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate static unsigned char *atoe = svr4_atoe;
4947c478bd9Sstevel@tonic-gate static unsigned char *etoa = svr4_etoa;
4957c478bd9Sstevel@tonic-gate static unsigned char *atoibm = svr4_atoibm;
4967c478bd9Sstevel@tonic-gate 
49719d32b9aSRobert Mustacchi /*ARGSUSED*/
49819d32b9aSRobert Mustacchi static void
siginfo_handler(int sig,siginfo_t * sip,void * ucp)49919d32b9aSRobert Mustacchi siginfo_handler(int sig, siginfo_t *sip, void *ucp)
50019d32b9aSRobert Mustacchi {
50119d32b9aSRobert Mustacchi 	nstats = 1;
50219d32b9aSRobert Mustacchi }
5037c478bd9Sstevel@tonic-gate 
504c232df92SToomas Soome static ssize_t
iread(int fd,char * buf,size_t nbyte)505c232df92SToomas Soome iread(int fd, char *buf, size_t nbyte)
506c232df92SToomas Soome {
507c232df92SToomas Soome 	ssize_t count;
508c232df92SToomas Soome 
509c232df92SToomas Soome 	count = 0;
510c232df92SToomas Soome 	while (nbyte != 0) {
511c232df92SToomas Soome 		ssize_t nr = read(fd, buf, nbyte);
512c232df92SToomas Soome 
513c232df92SToomas Soome 		if (nr < 0)
514c232df92SToomas Soome 			return (nr);
515c232df92SToomas Soome 
516c232df92SToomas Soome 		if (nr == 0)
517c232df92SToomas Soome 			break;
518c232df92SToomas Soome 		buf += nr;
519c232df92SToomas Soome 		count += nr;
520c232df92SToomas Soome 		nbyte -= nr;
521c232df92SToomas Soome 
522c232df92SToomas Soome 		if ((iflag & FULLBLOCK) == 0)
523c232df92SToomas Soome 			break;
524c232df92SToomas Soome 	}
525c232df92SToomas Soome 	return (count);
526c232df92SToomas Soome }
527c232df92SToomas Soome 
528a77d64afScf int
main(int argc,char ** argv)529a77d64afScf main(int argc, char **argv)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	unsigned char *ip, *op; /* input and output buffer pointers */
5327c478bd9Sstevel@tonic-gate 	int c;		/* character counter */
5337c478bd9Sstevel@tonic-gate 	int ic;		/* input character */
5347c478bd9Sstevel@tonic-gate 	int conv;		/* conversion option code */
5357c478bd9Sstevel@tonic-gate 	int trunc;		/* whether output file is truncated */
5367c478bd9Sstevel@tonic-gate 	struct stat file_stat;
53719d32b9aSRobert Mustacchi 	struct sigaction sact;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* Set option defaults */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	ibs = BSIZE;
5427c478bd9Sstevel@tonic-gate 	obs = BSIZE;
5437c478bd9Sstevel@tonic-gate 	files = 1;
5447c478bd9Sstevel@tonic-gate 	conv = COPY;
5457c478bd9Sstevel@tonic-gate 	trunc = 1;			/* default: truncate output file */
5467c478bd9Sstevel@tonic-gate 	trantype = SVR4XLATE;  /* use SVR4 EBCDIC by default */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* Parse command options */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
5517c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
5527c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
5537c478bd9Sstevel@tonic-gate #endif
5547c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "")) != EOF)
5577c478bd9Sstevel@tonic-gate 		switch (c) {
5587c478bd9Sstevel@tonic-gate 			case '?':
5597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, USAGE);
5607c478bd9Sstevel@tonic-gate 			exit(2);
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* not getopt()'ed because dd has no options but only operand(s) */
5647c478bd9Sstevel@tonic-gate 
565b928ac84SToomas Soome 	for (c = optind; c < argc; c++) {
5667c478bd9Sstevel@tonic-gate 		string = argv[c];
567b928ac84SToomas Soome 		if (match("ibs=")) {
5687c478bd9Sstevel@tonic-gate 			ibs = (unsigned)number(BIG);
5697c478bd9Sstevel@tonic-gate 			continue;
5707c478bd9Sstevel@tonic-gate 		}
571b928ac84SToomas Soome 		if (match("obs=")) {
5727c478bd9Sstevel@tonic-gate 			obs = (unsigned)number(BIG);
5737c478bd9Sstevel@tonic-gate 			continue;
5747c478bd9Sstevel@tonic-gate 		}
575b928ac84SToomas Soome 		if (match("cbs=")) {
5767c478bd9Sstevel@tonic-gate 			cbs = (unsigned)number(BIG);
5777c478bd9Sstevel@tonic-gate 			continue;
5787c478bd9Sstevel@tonic-gate 		}
579b928ac84SToomas Soome 		if (match("bs=")) {
5807c478bd9Sstevel@tonic-gate 			bs = (unsigned)number(BIG);
5817c478bd9Sstevel@tonic-gate 			continue;
5827c478bd9Sstevel@tonic-gate 		}
583b928ac84SToomas Soome 		if (match("if=")) {
5847c478bd9Sstevel@tonic-gate 			ifile = string;
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 		}
587b928ac84SToomas Soome 		if (match("of=")) {
5887c478bd9Sstevel@tonic-gate 			ofile = string;
5897c478bd9Sstevel@tonic-gate 			continue;
5907c478bd9Sstevel@tonic-gate 		}
591b928ac84SToomas Soome 		if (match("skip=")) {
5927c478bd9Sstevel@tonic-gate 			skip = number(BIG);
5937c478bd9Sstevel@tonic-gate 			continue;
5947c478bd9Sstevel@tonic-gate 		}
595b928ac84SToomas Soome 		if (match("iseek=")) {
5967c478bd9Sstevel@tonic-gate 			iseekn = number(BIG);
5977c478bd9Sstevel@tonic-gate 			continue;
5987c478bd9Sstevel@tonic-gate 		}
599b928ac84SToomas Soome 		if (match("oseek=")) {
6007c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
6017c478bd9Sstevel@tonic-gate 			continue;
6027c478bd9Sstevel@tonic-gate 		}
603b928ac84SToomas Soome 		if (match("seek=")) {		/* retained for compatibility */
6047c478bd9Sstevel@tonic-gate 			oseekn = number(BIG);
6057c478bd9Sstevel@tonic-gate 			continue;
6067c478bd9Sstevel@tonic-gate 		}
607b928ac84SToomas Soome 		if (match("ostride=")) {
60861304e4fSPaul Dagnelie 			ostriden = ((off_t)number(BIG)) - 1;
60961304e4fSPaul Dagnelie 			continue;
61061304e4fSPaul Dagnelie 		}
611b928ac84SToomas Soome 		if (match("istride=")) {
61261304e4fSPaul Dagnelie 			istriden = ((off_t)number(BIG)) - 1;
61361304e4fSPaul Dagnelie 			continue;
61461304e4fSPaul Dagnelie 		}
615b928ac84SToomas Soome 		if (match("stride=")) {
61661304e4fSPaul Dagnelie 			istriden = ostriden = ((off_t)number(BIG)) - 1;
61761304e4fSPaul Dagnelie 			continue;
61861304e4fSPaul Dagnelie 		}
619b928ac84SToomas Soome 		if (match("count=")) {
6207c478bd9Sstevel@tonic-gate 			count = number(BIG);
621e3bee069SRobert Mustacchi 			ecount = B_TRUE;
6227c478bd9Sstevel@tonic-gate 			continue;
6237c478bd9Sstevel@tonic-gate 		}
624b928ac84SToomas Soome 		if (match("files=")) {
6257c478bd9Sstevel@tonic-gate 			files = (int)number(BIG);
6267c478bd9Sstevel@tonic-gate 			continue;
6277c478bd9Sstevel@tonic-gate 		}
628b928ac84SToomas Soome 		if (match("conv=")) {
629b928ac84SToomas Soome 			for (;;) {
630b928ac84SToomas Soome 				if (match(",")) {
6317c478bd9Sstevel@tonic-gate 					continue;
6327c478bd9Sstevel@tonic-gate 				}
633b928ac84SToomas Soome 				if (*string == '\0') {
6347c478bd9Sstevel@tonic-gate 					break;
6357c478bd9Sstevel@tonic-gate 				}
636b928ac84SToomas Soome 				if (match("block")) {
6377c478bd9Sstevel@tonic-gate 					conv = BLOCK;
6387c478bd9Sstevel@tonic-gate 					continue;
6397c478bd9Sstevel@tonic-gate 				}
640b928ac84SToomas Soome 				if (match("unblock")) {
6417c478bd9Sstevel@tonic-gate 					conv = UNBLOCK;
6427c478bd9Sstevel@tonic-gate 					continue;
6437c478bd9Sstevel@tonic-gate 				}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 				/* ebcdicb, ibmb, and asciib must precede */
6467c478bd9Sstevel@tonic-gate 				/* ebcdic, ibm, and ascii in this test */
6477c478bd9Sstevel@tonic-gate 
648b928ac84SToomas Soome 				if (match("ebcdicb")) {
6497c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6507c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6517c478bd9Sstevel@tonic-gate 					continue;
6527c478bd9Sstevel@tonic-gate 				}
653b928ac84SToomas Soome 				if (match("ibmb")) {
6547c478bd9Sstevel@tonic-gate 					conv = IBM;
6557c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6567c478bd9Sstevel@tonic-gate 					continue;
6577c478bd9Sstevel@tonic-gate 				}
658b928ac84SToomas Soome 				if (match("asciib")) {
6597c478bd9Sstevel@tonic-gate 					conv = ASCII;
6607c478bd9Sstevel@tonic-gate 					trantype = BSDXLATE;
6617c478bd9Sstevel@tonic-gate 					continue;
6627c478bd9Sstevel@tonic-gate 				}
663b928ac84SToomas Soome 				if (match("ebcdic")) {
6647c478bd9Sstevel@tonic-gate 					conv = EBCDIC;
6657c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6667c478bd9Sstevel@tonic-gate 					continue;
6677c478bd9Sstevel@tonic-gate 				}
668b928ac84SToomas Soome 				if (match("ibm")) {
6697c478bd9Sstevel@tonic-gate 					conv = IBM;
6707c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6717c478bd9Sstevel@tonic-gate 					continue;
6727c478bd9Sstevel@tonic-gate 				}
673b928ac84SToomas Soome 				if (match("ascii")) {
6747c478bd9Sstevel@tonic-gate 					conv = ASCII;
6757c478bd9Sstevel@tonic-gate 					trantype = SVR4XLATE;
6767c478bd9Sstevel@tonic-gate 					continue;
6777c478bd9Sstevel@tonic-gate 				}
678b928ac84SToomas Soome 				if (match("lcase")) {
6797c478bd9Sstevel@tonic-gate 					cflag |= LCASE;
6807c478bd9Sstevel@tonic-gate 					continue;
6817c478bd9Sstevel@tonic-gate 				}
682b928ac84SToomas Soome 				if (match("ucase")) {
6837c478bd9Sstevel@tonic-gate 					cflag |= UCASE;
6847c478bd9Sstevel@tonic-gate 					continue;
6857c478bd9Sstevel@tonic-gate 				}
686b928ac84SToomas Soome 				if (match("swab")) {
6877c478bd9Sstevel@tonic-gate 					cflag |= SWAB;
6887c478bd9Sstevel@tonic-gate 					continue;
6897c478bd9Sstevel@tonic-gate 				}
690b928ac84SToomas Soome 				if (match("noerror")) {
6917c478bd9Sstevel@tonic-gate 					cflag |= NERR;
6927c478bd9Sstevel@tonic-gate 					continue;
6937c478bd9Sstevel@tonic-gate 				}
694b928ac84SToomas Soome 				if (match("notrunc")) {
6957c478bd9Sstevel@tonic-gate 					trunc = 0;
6967c478bd9Sstevel@tonic-gate 					continue;
6977c478bd9Sstevel@tonic-gate 				}
698b928ac84SToomas Soome 				if (match("sync")) {
6997c478bd9Sstevel@tonic-gate 					cflag |= SYNC;
7007c478bd9Sstevel@tonic-gate 					continue;
7017c478bd9Sstevel@tonic-gate 				}
7027c478bd9Sstevel@tonic-gate 				goto badarg;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			continue;
7057c478bd9Sstevel@tonic-gate 		}
706c232df92SToomas Soome 		if (match("iflag=")) {
707c232df92SToomas Soome 			for (;;) {
708c232df92SToomas Soome 				if (match(",")) {
709c232df92SToomas Soome 					continue;
710c232df92SToomas Soome 				}
711c232df92SToomas Soome 				if (*string == '\0') {
712c232df92SToomas Soome 					break;
713c232df92SToomas Soome 				}
714c232df92SToomas Soome 				if (match("fullblock")) {
715c232df92SToomas Soome 					iflag |= FULLBLOCK;
716c232df92SToomas Soome 					continue;
717c232df92SToomas Soome 				}
718c232df92SToomas Soome 				goto badarg;
719c232df92SToomas Soome 			}
720c232df92SToomas Soome 			continue;
721c232df92SToomas Soome 		}
722b928ac84SToomas Soome 		if (match("oflag=")) {
723b928ac84SToomas Soome 			for (;;) {
724b928ac84SToomas Soome 				if (match(",")) {
72519d32b9aSRobert Mustacchi 					continue;
72619d32b9aSRobert Mustacchi 				}
727b928ac84SToomas Soome 				if (*string == '\0') {
72819d32b9aSRobert Mustacchi 					break;
72919d32b9aSRobert Mustacchi 				}
730b928ac84SToomas Soome 				if (match("dsync")) {
73119d32b9aSRobert Mustacchi 					oflag |= O_DSYNC;
73219d32b9aSRobert Mustacchi 					continue;
73319d32b9aSRobert Mustacchi 				}
734b928ac84SToomas Soome 				if (match("sync")) {
73519d32b9aSRobert Mustacchi 					oflag |= O_SYNC;
73619d32b9aSRobert Mustacchi 					continue;
73719d32b9aSRobert Mustacchi 				}
73819d32b9aSRobert Mustacchi 				goto badarg;
73919d32b9aSRobert Mustacchi 			}
74019d32b9aSRobert Mustacchi 			continue;
74119d32b9aSRobert Mustacchi 		}
742*c53c97f7SRobert Mustacchi 		if (match("status=")) {
743*c53c97f7SRobert Mustacchi 			if (match("none")) {
744*c53c97f7SRobert Mustacchi 				status_arg = DD_STATUS_NONE;
745*c53c97f7SRobert Mustacchi 			} else if (match("noxfer")) {
746*c53c97f7SRobert Mustacchi 				status_arg = DD_STATUS_NOXFER;
747*c53c97f7SRobert Mustacchi 			} else if (match("progress")) {
748*c53c97f7SRobert Mustacchi 				status_arg = DD_STATUS_PROGRESS;
749*c53c97f7SRobert Mustacchi 			} else {
750*c53c97f7SRobert Mustacchi 				goto badarg;
751*c53c97f7SRobert Mustacchi 			}
752*c53c97f7SRobert Mustacchi 			continue;
753*c53c97f7SRobert Mustacchi 		}
7547c478bd9Sstevel@tonic-gate 		badarg:
7557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s \"%s\"\n",
756b928ac84SToomas Soome 		    gettext("bad argument:"), string);
7577c478bd9Sstevel@tonic-gate 		exit(2);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/* Perform consistency checks on options, decode strange conventions */
7617c478bd9Sstevel@tonic-gate 
762b928ac84SToomas Soome 	if (bs) {
7637c478bd9Sstevel@tonic-gate 		ibs = obs = bs;
7647c478bd9Sstevel@tonic-gate 	}
765b928ac84SToomas Soome 	if ((ibs == 0) || (obs == 0)) {
7667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s\n",
767b928ac84SToomas Soome 		    gettext("buffer sizes cannot be zero"));
7687c478bd9Sstevel@tonic-gate 		exit(2);
7697c478bd9Sstevel@tonic-gate 	}
77061304e4fSPaul Dagnelie 	if (ostriden == (off_t)-1) {
77161304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
772b928ac84SToomas Soome 		    gettext("stride must be greater than zero"));
77361304e4fSPaul Dagnelie 		exit(2);
77461304e4fSPaul Dagnelie 	}
77561304e4fSPaul Dagnelie 	if (istriden == (off_t)-1) {
77661304e4fSPaul Dagnelie 		(void) fprintf(stderr, "dd: %s\n",
777b928ac84SToomas Soome 		    gettext("stride must be greater than zero"));
77861304e4fSPaul Dagnelie 		exit(2);
77961304e4fSPaul Dagnelie 	}
780b928ac84SToomas Soome 	if (conv == COPY) {
781b928ac84SToomas Soome 		if ((bs == 0) || (cflag & (LCASE | UCASE))) {
7827c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
785b928ac84SToomas Soome 	if (cbs == 0) {
786b928ac84SToomas Soome 		switch (conv) {
7877c478bd9Sstevel@tonic-gate 		case BLOCK:
7887c478bd9Sstevel@tonic-gate 		case UNBLOCK:
7897c478bd9Sstevel@tonic-gate 			conv = REBLOCK;
7907c478bd9Sstevel@tonic-gate 			break;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		case ASCII:
7937c478bd9Sstevel@tonic-gate 			conv = NBASCII;
7947c478bd9Sstevel@tonic-gate 			break;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		case EBCDIC:
7977c478bd9Sstevel@tonic-gate 			conv = NBEBCDIC;
7987c478bd9Sstevel@tonic-gate 			break;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		case IBM:
8017c478bd9Sstevel@tonic-gate 			conv = NBIBM;
8027c478bd9Sstevel@tonic-gate 			break;
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* Expand options into lower and upper case versions if necessary */
8077c478bd9Sstevel@tonic-gate 
808b928ac84SToomas Soome 	switch (conv) {
8097c478bd9Sstevel@tonic-gate 	case REBLOCK:
810b928ac84SToomas Soome 		if (cflag & LCASE)
8117c478bd9Sstevel@tonic-gate 			conv = LCREBLOCK;
812b928ac84SToomas Soome 		else if (cflag & UCASE)
8137c478bd9Sstevel@tonic-gate 			conv = UCREBLOCK;
8147c478bd9Sstevel@tonic-gate 		break;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	case UNBLOCK:
817b928ac84SToomas Soome 		if (cflag & LCASE)
8187c478bd9Sstevel@tonic-gate 			conv = LCUNBLOCK;
819b928ac84SToomas Soome 		else if (cflag & UCASE)
8207c478bd9Sstevel@tonic-gate 			conv = UCUNBLOCK;
8217c478bd9Sstevel@tonic-gate 		break;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	case BLOCK:
824b928ac84SToomas Soome 		if (cflag & LCASE)
8257c478bd9Sstevel@tonic-gate 			conv = LCBLOCK;
826b928ac84SToomas Soome 		else if (cflag & UCASE)
8277c478bd9Sstevel@tonic-gate 			conv = UCBLOCK;
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	case ASCII:
831b928ac84SToomas Soome 		if (cflag & LCASE)
8327c478bd9Sstevel@tonic-gate 			conv = LCASCII;
833b928ac84SToomas Soome 		else if (cflag & UCASE)
8347c478bd9Sstevel@tonic-gate 			conv = UCASCII;
8357c478bd9Sstevel@tonic-gate 		break;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	case NBASCII:
838b928ac84SToomas Soome 		if (cflag & LCASE)
8397c478bd9Sstevel@tonic-gate 			conv = LCNBASCII;
840b928ac84SToomas Soome 		else if (cflag & UCASE)
8417c478bd9Sstevel@tonic-gate 			conv = UCNBASCII;
8427c478bd9Sstevel@tonic-gate 		break;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	case EBCDIC:
845b928ac84SToomas Soome 		if (cflag & LCASE)
8467c478bd9Sstevel@tonic-gate 			conv = LCEBCDIC;
847b928ac84SToomas Soome 		else if (cflag & UCASE)
8487c478bd9Sstevel@tonic-gate 			conv = UCEBCDIC;
8497c478bd9Sstevel@tonic-gate 		break;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	case NBEBCDIC:
852b928ac84SToomas Soome 		if (cflag & LCASE)
8537c478bd9Sstevel@tonic-gate 			conv = LCNBEBCDIC;
854b928ac84SToomas Soome 		else if (cflag & UCASE)
8557c478bd9Sstevel@tonic-gate 			conv = UCNBEBCDIC;
8567c478bd9Sstevel@tonic-gate 		break;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	case IBM:
859b928ac84SToomas Soome 		if (cflag & LCASE)
8607c478bd9Sstevel@tonic-gate 			conv = LCIBM;
861b928ac84SToomas Soome 		else if (cflag & UCASE)
8627c478bd9Sstevel@tonic-gate 			conv = UCIBM;
8637c478bd9Sstevel@tonic-gate 		break;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	case NBIBM:
866b928ac84SToomas Soome 		if (cflag & LCASE)
8677c478bd9Sstevel@tonic-gate 			conv = LCNBIBM;
868b928ac84SToomas Soome 		else if (cflag & UCASE)
8697c478bd9Sstevel@tonic-gate 			conv = UCNBIBM;
8707c478bd9Sstevel@tonic-gate 		break;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/* If BSD-compatible translation is selected, change the tables */
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (trantype == BSDXLATE) {
8767c478bd9Sstevel@tonic-gate 		atoe = bsd_atoe;
8777c478bd9Sstevel@tonic-gate 		atoibm = bsd_atoibm;
8787c478bd9Sstevel@tonic-gate 		etoa = bsd_etoa;
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	/* Open the input file, or duplicate standard input */
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	ibf = -1;
883b928ac84SToomas Soome 	if (ifile) {
8847c478bd9Sstevel@tonic-gate 		ibf = open(ifile, 0);
885b928ac84SToomas Soome 	} else {
8867c478bd9Sstevel@tonic-gate 		ifile = "";
887b928ac84SToomas Soome 		ibf = dup(STDIN_FILENO);
8887c478bd9Sstevel@tonic-gate 	}
88919d32b9aSRobert Mustacchi 
890b928ac84SToomas Soome 	if (ibf == -1) {
8917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ifile);
8927c478bd9Sstevel@tonic-gate 		perror("open");
8937c478bd9Sstevel@tonic-gate 		exit(2);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	/* Open the output file, or duplicate standard output */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	obf = -1;
899b928ac84SToomas Soome 	if (ofile) {
900b928ac84SToomas Soome 		if (trunc == 0)	{	/* do not truncate output file */
901b928ac84SToomas Soome 			obf = open(ofile, (O_WRONLY | O_CREAT | oflag),
902b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
903b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
904b928ac84SToomas Soome 		} else if (oseekn && (trunc == 1)) {
905b928ac84SToomas Soome 			obf = open(ofile, O_WRONLY | O_CREAT | oflag,
906b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
907b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
908b928ac84SToomas Soome 			if (obf == -1) {
9097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s: ", ofile);
9107c478bd9Sstevel@tonic-gate 				perror("open");
9117c478bd9Sstevel@tonic-gate 				exit(2);
9127c478bd9Sstevel@tonic-gate 			}
9137c478bd9Sstevel@tonic-gate 			(void) fstat(obf, &file_stat);
9147c478bd9Sstevel@tonic-gate 			if (((file_stat.st_mode & S_IFMT) == S_IFREG) &&
9157c478bd9Sstevel@tonic-gate 			    (ftruncate(obf, (((off_t)oseekn) * ((off_t)obs)))
916b928ac84SToomas Soome 			    == -1)) {
9177c478bd9Sstevel@tonic-gate 				perror("ftruncate");
9187c478bd9Sstevel@tonic-gate 				exit(2);
9197c478bd9Sstevel@tonic-gate 			}
920b928ac84SToomas Soome 		} else {
921b928ac84SToomas Soome 			obf = open(ofile, O_WRONLY | O_CREAT | O_TRUNC | oflag,
922b928ac84SToomas Soome 			    (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
923b928ac84SToomas Soome 			    S_IROTH | S_IWOTH));
9247c478bd9Sstevel@tonic-gate 		}
925b928ac84SToomas Soome 	} else {
9267c478bd9Sstevel@tonic-gate 		ofile = "";
927b928ac84SToomas Soome 		obf = dup(STDOUT_FILENO);
9287c478bd9Sstevel@tonic-gate 	}
92919d32b9aSRobert Mustacchi 
930b928ac84SToomas Soome 	if (obf == -1) {
9317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "dd: %s: ", ofile);
9327c478bd9Sstevel@tonic-gate 		perror("open");
9337c478bd9Sstevel@tonic-gate 		exit(2);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	/* Expand memory to get an input buffer */
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	ibuf = (unsigned char *)valloc(ibs + 10);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/* If no conversions, the input buffer is the output buffer */
9417c478bd9Sstevel@tonic-gate 
942b928ac84SToomas Soome 	if (conv == COPY) {
9437c478bd9Sstevel@tonic-gate 		obuf = ibuf;
944b928ac84SToomas Soome 	} else {
9457c478bd9Sstevel@tonic-gate 
946b928ac84SToomas Soome 		/*
947b928ac84SToomas Soome 		 * Expand memory to get an output buffer. Leave enough room
948b928ac84SToomas Soome 		 * at the end to convert a logical record when doing block
949b928ac84SToomas Soome 		 * conversions.
950b928ac84SToomas Soome 		 */
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		obuf = (unsigned char *)valloc(obs + cbs + 10);
9537c478bd9Sstevel@tonic-gate 	}
954b928ac84SToomas Soome 	if ((ibuf == NULL) || (obuf == NULL)) {
9557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
956b928ac84SToomas Soome 		    "dd: %s\n", gettext("not enough memory"));
9577c478bd9Sstevel@tonic-gate 		exit(2);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
96019d32b9aSRobert Mustacchi 	/*
96119d32b9aSRobert Mustacchi 	 * Enable a statistics message when we terminate on SIGINT
96219d32b9aSRobert Mustacchi 	 * Also enable it to be queried via SIGINFO and SIGUSR1
96319d32b9aSRobert Mustacchi 	 */
9647c478bd9Sstevel@tonic-gate 
965b928ac84SToomas Soome 	if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
9667c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, term);
9677c478bd9Sstevel@tonic-gate 	}
96819d32b9aSRobert Mustacchi 
96919d32b9aSRobert Mustacchi 	bzero(&sact, sizeof (struct sigaction));
97019d32b9aSRobert Mustacchi 	sact.sa_flags = SA_SIGINFO;
97119d32b9aSRobert Mustacchi 	sact.sa_sigaction = siginfo_handler;
97219d32b9aSRobert Mustacchi 	(void) sigemptyset(&sact.sa_mask);
97319d32b9aSRobert Mustacchi 	if (sigaction(SIGINFO, &sact, NULL) != 0) {
97419d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
97519d32b9aSRobert Mustacchi 		    gettext("failed to enable siginfo handler"),
97619d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
97719d32b9aSRobert Mustacchi 		exit(2);
97819d32b9aSRobert Mustacchi 	}
97919d32b9aSRobert Mustacchi 	if (sigaction(SIGUSR1, &sact, NULL) != 0) {
98019d32b9aSRobert Mustacchi 		(void) fprintf(stderr, "dd: %s: %s\n",
98119d32b9aSRobert Mustacchi 		    gettext("failed to enable sigusr1 handler"),
98219d32b9aSRobert Mustacchi 		    gettext(strerror(errno)));
98319d32b9aSRobert Mustacchi 		exit(2);
98419d32b9aSRobert Mustacchi 	}
98519d32b9aSRobert Mustacchi 
986*c53c97f7SRobert Mustacchi 	/*
987*c53c97f7SRobert Mustacchi 	 * Determine if stderr is a tty in case someone has invoked
988*c53c97f7SRobert Mustacchi 	 * status=progress
989*c53c97f7SRobert Mustacchi 	 */
990*c53c97f7SRobert Mustacchi 	if (isatty(STDERR_FILENO) == 1) {
991*c53c97f7SRobert Mustacchi 		stderr_tty = B_TRUE;
992*c53c97f7SRobert Mustacchi 	}
993*c53c97f7SRobert Mustacchi 
9947c478bd9Sstevel@tonic-gate 	/* Skip input blocks */
9957c478bd9Sstevel@tonic-gate 
996b928ac84SToomas Soome 	while (skip) {
997c232df92SToomas Soome 		ibc = iread(ibf, (char *)ibuf, ibs);
998b928ac84SToomas Soome 		if (ibc == (unsigned)-1) {
999b928ac84SToomas Soome 			if (++nbad > BADLIMIT) {
10007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
1001b928ac84SToomas Soome 				    gettext("skip failed"));
10027c478bd9Sstevel@tonic-gate 				exit(2);
1003b928ac84SToomas Soome 			} else {
10047c478bd9Sstevel@tonic-gate 				perror("read");
10057c478bd9Sstevel@tonic-gate 			}
1006b928ac84SToomas Soome 		} else {
1007b928ac84SToomas Soome 			if (ibc == 0) {
10087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "dd: %s\n",
10097c478bd9Sstevel@tonic-gate 				gettext("cannot skip past end-of-file"));
10107c478bd9Sstevel@tonic-gate 				exit(3);
1011b928ac84SToomas Soome 			} else {
10127c478bd9Sstevel@tonic-gate 				nbad = 0;
10137c478bd9Sstevel@tonic-gate 			}
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 		skip--;
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	/* Seek past input blocks */
10197c478bd9Sstevel@tonic-gate 
1020b928ac84SToomas Soome 	if (iseekn && lseek(ibf, (((off_t)iseekn) * ((off_t)ibs)), 1) == -1) {
10217c478bd9Sstevel@tonic-gate 		perror("lseek");
10227c478bd9Sstevel@tonic-gate 		exit(2);
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	/* Seek past output blocks */
10267c478bd9Sstevel@tonic-gate 
1027b928ac84SToomas Soome 	if (oseekn && lseek(obf, (((off_t)oseekn) * ((off_t)obs)), 1) == -1) {
10287c478bd9Sstevel@tonic-gate 		perror("lseek");
10297c478bd9Sstevel@tonic-gate 		exit(2);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	/* Initialize all buffer pointers */
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	skipf = 0;	/* not skipping an input line */
10357c478bd9Sstevel@tonic-gate 	ibc = 0;	/* no input characters yet */
10367c478bd9Sstevel@tonic-gate 	obc = 0;	/* no output characters yet */
10377c478bd9Sstevel@tonic-gate 	cbc = 0;	/* the conversion buffer is empty */
10387c478bd9Sstevel@tonic-gate 	op = obuf;	/* point to the output buffer */
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/* Read and convert input blocks until end of file(s) */
10417c478bd9Sstevel@tonic-gate 
104219d32b9aSRobert Mustacchi 	/* Grab our start time for siginfo purposes */
104319d32b9aSRobert Mustacchi 	startt = gethrtime();
104419d32b9aSRobert Mustacchi 
1045b928ac84SToomas Soome 	for (;;) {
1046