xref: /onnv-gate/usr/src/cmd/sunpc/other/unix2dos.c (revision 712:1ddaeec71277)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*712Smuffin  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  *	Converts files from one char set to another
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *	Written 11/09/87	Eddy Bell
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  *  INCLUDED and DEFINES
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate #include	<stdio.h>
420Sstevel@tonic-gate #include	<fcntl.h>
430Sstevel@tonic-gate #include	<sys/systeminfo.h>
440Sstevel@tonic-gate #include	<stdlib.h>
450Sstevel@tonic-gate #include	<string.h>
460Sstevel@tonic-gate #include	<errno.h>
470Sstevel@tonic-gate /* #include	<io.h>			for microsoft c 4.0 */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #define		CONTENTS_ASCII	0
510Sstevel@tonic-gate #define		CONTENTS_ASCII8 1
520Sstevel@tonic-gate #define		CONTENTS_ISO	2
530Sstevel@tonic-gate #define		CONTENTS_DOS	3
540Sstevel@tonic-gate #ifdef _F_BIN
550Sstevel@tonic-gate #define	DOS_BUILD 1
560Sstevel@tonic-gate #else
570Sstevel@tonic-gate #define	UNIX_BUILD 1
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * INCLUDES AND DEFINES
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate #ifdef UNIX_BUILD
660Sstevel@tonic-gate #include <sys/types.h>
670Sstevel@tonic-gate #include	<sys/kbio.h>
680Sstevel@tonic-gate #include	<sys/time.h>
690Sstevel@tonic-gate #include	<fcntl.h>
700Sstevel@tonic-gate #include "../sys/dos_iso.h"
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifdef DOS_BUILD
740Sstevel@tonic-gate #include <dos.h>
750Sstevel@tonic-gate #include "..\sys\dos_iso.h"
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define		GLOBAL
800Sstevel@tonic-gate #define		LOCAL	static
810Sstevel@tonic-gate #define		BOOL	int
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define		FALSE	0
840Sstevel@tonic-gate #define		TRUE	~FALSE
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #define		CR	0x0D
870Sstevel@tonic-gate #define		LF	0x0A
880Sstevel@tonic-gate #define		DOS_EOF 0x1A
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #define		MAXLEN	1024
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * FUNCTION AND VARIABLE DECLARATIONS
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate static	void	error();
960Sstevel@tonic-gate static	void	usage();
970Sstevel@tonic-gate static	int	tmpfd = -1;
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * ENTRY POINTS
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate 
102*712Smuffin int
main(int argc,char ** argv)103*712Smuffin main(int argc, char **argv)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	FILE *in_stream = NULL;
1060Sstevel@tonic-gate 	FILE *out_stream = NULL;
1070Sstevel@tonic-gate 	unsigned char tmp_buff[512];
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	unsigned char *src_str, *dest_str;
1100Sstevel@tonic-gate 	char	 *in_file_name, *out_file_name;
1110Sstevel@tonic-gate 	int num_read, numchar, i, j, out_len, translate_mode;
1120Sstevel@tonic-gate 	int same_name;
1130Sstevel@tonic-gate 	/* char count for fread() */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	int	type;
1160Sstevel@tonic-gate 	int	code_page_overide; /* over ride of default codepage */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	unsigned char * dos_to_iso;
1190Sstevel@tonic-gate 	unsigned char iso_to_dos[256];
1200Sstevel@tonic-gate #ifdef UNIX_BUILD
1210Sstevel@tonic-gate 	int	kbdfd;
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 	char	sysinfo_str[MAXLEN];
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	same_name = FALSE;
1260Sstevel@tonic-gate 	out_file_name = (char *)0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * The filename parameter is positionally dependent - in that
1300Sstevel@tonic-gate 	 * the dest file name must follow the source filename
1310Sstevel@tonic-gate 	 */
1320Sstevel@tonic-gate 	argv++;
1330Sstevel@tonic-gate 	in_stream = stdin;
1340Sstevel@tonic-gate 	out_stream = stdout;
1350Sstevel@tonic-gate 	j = 0;  /* count for file names 0 -> source 1-> dest */
1360Sstevel@tonic-gate 	translate_mode = CONTENTS_ISO; /* default trans mode */
1370Sstevel@tonic-gate 	code_page_overide = 0;
1380Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1390Sstevel@tonic-gate 		if (*argv[0] == '-') {
1400Sstevel@tonic-gate 			if (argc > 1 && !strncmp(*argv, "-iso", 4)) {
1410Sstevel@tonic-gate 				translate_mode = CONTENTS_ISO;
1420Sstevel@tonic-gate 				argv++;
1430Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-7", 2)) {
1440Sstevel@tonic-gate 				translate_mode = CONTENTS_ASCII;
1450Sstevel@tonic-gate 				argv++;
1460Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-ascii", 6)) {
1470Sstevel@tonic-gate 				translate_mode = CONTENTS_DOS;
1480Sstevel@tonic-gate 				argv++;
1490Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-437", 4)) {
1500Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_US;
1510Sstevel@tonic-gate 				argv++;
1520Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-850", 4)) {
1530Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_MULTILINGUAL;
1540Sstevel@tonic-gate 				argv++;
1550Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-860", 4)) {
1560Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_PORTUGAL;
1570Sstevel@tonic-gate 				argv++;
1580Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-863", 4)) {
1590Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_CANADA_FRENCH;
1600Sstevel@tonic-gate 				argv++;
1610Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-865", 4)) {
1620Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_NORWAY;
1630Sstevel@tonic-gate 				argv++;
1640Sstevel@tonic-gate 			} else
1650Sstevel@tonic-gate 				argv++;
1660Sstevel@tonic-gate 			continue;
1670Sstevel@tonic-gate 		} else {  /* not a command so must be filename */
1680Sstevel@tonic-gate 			switch (j) {
1690Sstevel@tonic-gate 				case IN_FILE:	/* open in file from cmdline */
1700Sstevel@tonic-gate 					in_file_name = *argv;
1710Sstevel@tonic-gate 					j++;  /* next file name is outfile */
1720Sstevel@tonic-gate 					break;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 				case OUT_FILE:	/* open out file from cmdline */
1750Sstevel@tonic-gate 					out_file_name = *argv;
1760Sstevel@tonic-gate 					j++;
1770Sstevel@tonic-gate 					break;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 				default:
1800Sstevel@tonic-gate 					usage();
1810Sstevel@tonic-gate 			}
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 	argv++;
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* input file is specified */
1870Sstevel@tonic-gate 	if (j > 0) {
1880Sstevel@tonic-gate 		in_stream = fopen(in_file_name, "r");
1890Sstevel@tonic-gate 		if (in_stream == NULL)
1900Sstevel@tonic-gate 			error("Couldn't open input file %s.", in_file_name);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/* output file is specified */
1940Sstevel@tonic-gate 	if (j > 1) {
1950Sstevel@tonic-gate 		if (!strcmp(in_file_name, out_file_name)) {
1960Sstevel@tonic-gate 			/* input and output have same name */
1970Sstevel@tonic-gate 			if (access(out_file_name, 2))
1980Sstevel@tonic-gate 				error("%s not writable.", out_file_name);
1990Sstevel@tonic-gate 			strcpy(out_file_name, "/tmp/udXXXXXX");
2000Sstevel@tonic-gate 			tmpfd = mkstemp(out_file_name);
2010Sstevel@tonic-gate 			if (tmpfd == -1) {
2020Sstevel@tonic-gate 				error("Couldn't create output file %s.",
2030Sstevel@tonic-gate 				    out_file_name);
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			(void) close(tmpfd);
2060Sstevel@tonic-gate 			same_name = TRUE;
2070Sstevel@tonic-gate 		} else
2080Sstevel@tonic-gate 			same_name = FALSE;
2090Sstevel@tonic-gate 		out_stream = fopen(out_file_name, "w");
2100Sstevel@tonic-gate 		if (out_stream == NULL) {
2110Sstevel@tonic-gate 			(void) unlink(out_file_name);
2120Sstevel@tonic-gate 			error("Couldn't open output file %s.", out_file_name);
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate #ifdef _F_BIN
2170Sstevel@tonic-gate 	setmode(fileno(in_stream), O_BINARY);
2180Sstevel@tonic-gate 	setmode(fileno(out_stream), O_BINARY);
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #ifdef UNIX_BUILD
2230Sstevel@tonic-gate 	if (!code_page_overide) {
2240Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, sysinfo_str, MAXLEN)  < 0) {
2250Sstevel@tonic-gate 			fprintf(stderr,
2260Sstevel@tonic-gate 			    "could not obtain system information\n");
2270Sstevel@tonic-gate 			(void) unlink(out_file_name);
2280Sstevel@tonic-gate 			exit(1);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 		if (strcmp(sysinfo_str, "i386")) {
2320Sstevel@tonic-gate 			if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
2330Sstevel@tonic-gate 				fprintf(stderr,
2340Sstevel@tonic-gate 				    "could not open /dev/kbd to get "
2350Sstevel@tonic-gate 				    "keyboard type US keyboard assumed\n");
2360Sstevel@tonic-gate 			}
2370Sstevel@tonic-gate 			if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
2380Sstevel@tonic-gate 				fprintf(stderr,
2390Sstevel@tonic-gate 	"could not get keyboard type US keyboard assumed\n");
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 		} else {
2420Sstevel@tonic-gate 			type = 0;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		switch (type) {
2450Sstevel@tonic-gate 			case	0:
2460Sstevel@tonic-gate 			case	1:	/* United States */
2470Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 			case	2:	/* Belgian French */
2510Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2520Sstevel@tonic-gate 			break;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 			case	3:	/* Canadian French */
2550Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
2560Sstevel@tonic-gate 			break;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 			case	4:	/* Danish */
2590Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2600Sstevel@tonic-gate 			break;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 			case	5:	/* German */
2630Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2640Sstevel@tonic-gate 			break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 			case	6:	/* Italian */
2670Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2680Sstevel@tonic-gate 			break;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 			case	7:	/* Netherlands Dutch */
2710Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2720Sstevel@tonic-gate 			break;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 			case	8:	/* Norwegian */
2750Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 			case	9:	/* Portuguese */
2790Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 			case	10:	/* Spanish */
2830Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2840Sstevel@tonic-gate 			break;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 			case	11:	/* Swedish Finnish */
2870Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2880Sstevel@tonic-gate 			break;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 			case	12:	/* Swiss French */
2910Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2920Sstevel@tonic-gate 			break;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 			case	13:	/* Swiss German */
2950Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2960Sstevel@tonic-gate 			break;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 			case	14:	/* United Kingdom */
2990Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			break;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 			default:
3040Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3050Sstevel@tonic-gate 			break;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 	} else {
3080Sstevel@tonic-gate 		switch (code_page_overide) {
3090Sstevel@tonic-gate 			case CODE_PAGE_US:
3100Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3110Sstevel@tonic-gate 			break;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3140Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3150Sstevel@tonic-gate 			break;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3180Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3190Sstevel@tonic-gate 			break;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3220Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3230Sstevel@tonic-gate 			break;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3260Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3270Sstevel@tonic-gate 			break;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate #endif
3310Sstevel@tonic-gate #ifdef DOS_BUILD
3320Sstevel@tonic-gate 	if (!code_page_overide) {
3330Sstevel@tonic-gate 		{
3340Sstevel@tonic-gate 		union REGS regs;
3350Sstevel@tonic-gate 		regs.h.ah = 0x66;	/* get/set global code page */
3360Sstevel@tonic-gate 		regs.h.al = 0x01;		/* get */
3370Sstevel@tonic-gate 		intdos(&regs, &regs);
3380Sstevel@tonic-gate 		type = regs.x.bx;
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		switch (type) {
3410Sstevel@tonic-gate 			case	437:	/* United States */
3420Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3430Sstevel@tonic-gate 			break;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 			case	850:	/* Multilingual */
3460Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3470Sstevel@tonic-gate 			break;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 			case	860:	/* Portuguese */
3500Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3510Sstevel@tonic-gate 			break;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 			case	863:	/* Canadian French */
3540Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3550Sstevel@tonic-gate 			break;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 			case	865:	/* Danish */
3580Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3590Sstevel@tonic-gate 			break;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 			default:
3620Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3630Sstevel@tonic-gate 			break;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 	} else {
3660Sstevel@tonic-gate 		switch (code_page_overide) {
3670Sstevel@tonic-gate 			case CODE_PAGE_US:
3680Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3690Sstevel@tonic-gate 			break;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3720Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3760Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3770Sstevel@tonic-gate 			break;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3800Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3840Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3850Sstevel@tonic-gate 			break;
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate #endif
3900Sstevel@tonic-gate 	for (i = 0; i <= 255; i++) {
3910Sstevel@tonic-gate 		iso_to_dos[dos_to_iso[i]] = i;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * While not EOF, read in chars and send them to out_stream
3960Sstevel@tonic-gate 	 * if current char is not a CR.
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate     do {
4000Sstevel@tonic-gate 		num_read = fread(&tmp_buff[100], 1, 100, in_stream);
4010Sstevel@tonic-gate 		i = 0;
4020Sstevel@tonic-gate 		out_len = 0;
4030Sstevel@tonic-gate 		src_str = &tmp_buff[100];
4040Sstevel@tonic-gate 		dest_str = &tmp_buff[0];
4050Sstevel@tonic-gate 		switch (translate_mode) {
4060Sstevel@tonic-gate 			case CONTENTS_ISO:
4070Sstevel@tonic-gate 				{
4080Sstevel@tonic-gate 				while (i++ != num_read) {
4090Sstevel@tonic-gate 					if (*src_str == '\n') {
4100Sstevel@tonic-gate 						*dest_str++ = '\r';
4110Sstevel@tonic-gate 						out_len++;
4120Sstevel@tonic-gate 						}
4130Sstevel@tonic-gate 					out_len++;
4140Sstevel@tonic-gate 					*dest_str++ = iso_to_dos[*src_str++];
4150Sstevel@tonic-gate 					}
4160Sstevel@tonic-gate 				}
4170Sstevel@tonic-gate 				break;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 			case CONTENTS_ASCII:
4200Sstevel@tonic-gate 				while (i++ != num_read) {
4210Sstevel@tonic-gate 					if (*src_str > 127) {
4220Sstevel@tonic-gate 						*dest_str++ =
4230Sstevel@tonic-gate 						    (unsigned char) ' ';
4240Sstevel@tonic-gate 						src_str++;
4250Sstevel@tonic-gate 						out_len++;
4260Sstevel@tonic-gate 					} else {
4270Sstevel@tonic-gate 						if (*src_str == '\n') {
4280Sstevel@tonic-gate 							*dest_str++ = '\r';
4290Sstevel@tonic-gate 							out_len++;
4300Sstevel@tonic-gate 						}
4310Sstevel@tonic-gate 						*dest_str++ = *src_str++;
4320Sstevel@tonic-gate 						out_len++;
4330Sstevel@tonic-gate 					}
4340Sstevel@tonic-gate 				}
4350Sstevel@tonic-gate 				break;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 			case CONTENTS_DOS:
4380Sstevel@tonic-gate 				{
4390Sstevel@tonic-gate 				while (i++ != num_read) {
4400Sstevel@tonic-gate 					if (*src_str == '\n') {
4410Sstevel@tonic-gate 						*dest_str++ = '\r';
4420Sstevel@tonic-gate 						out_len++;
4430Sstevel@tonic-gate 						}
4440Sstevel@tonic-gate 					*dest_str++ =	*src_str++;
4450Sstevel@tonic-gate 					out_len++;
4460Sstevel@tonic-gate 					}
4470Sstevel@tonic-gate 				}
4480Sstevel@tonic-gate 				break;
4490Sstevel@tonic-gate 			}
4500Sstevel@tonic-gate 		if (out_len && out_len != fwrite(&tmp_buff[0], 1, out_len,
4510Sstevel@tonic-gate 		    out_stream))
4520Sstevel@tonic-gate 			error("Error writing to %s.", out_file_name);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		} while (!feof(in_stream));
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate #ifdef CTRL_Z_ON_EOF
4570Sstevel@tonic-gate 	tmp_buff[0] = 26;
4580Sstevel@tonic-gate 	fwrite(&tmp_buff[0], 1, 1, out_stream);
4590Sstevel@tonic-gate #endif
4600Sstevel@tonic-gate 	fclose(out_stream);
4610Sstevel@tonic-gate 	fclose(in_stream);
4620Sstevel@tonic-gate 	if (same_name) {
4630Sstevel@tonic-gate 		unlink(in_file_name);
4640Sstevel@tonic-gate 		in_stream = fopen(out_file_name, "r");
4650Sstevel@tonic-gate 		out_stream = fopen(in_file_name, "w");
4660Sstevel@tonic-gate #ifdef _F_BIN
4670Sstevel@tonic-gate 		setmode(fileno(in_stream), O_BINARY);
4680Sstevel@tonic-gate 		setmode(fileno(out_stream), O_BINARY);
4690Sstevel@tonic-gate #endif
4700Sstevel@tonic-gate 		while ((num_read = fread(tmp_buff, 1, sizeof (tmp_buff),
4710Sstevel@tonic-gate 		    in_stream)) != 0) {
4720Sstevel@tonic-gate 			if (num_read != fwrite(tmp_buff, 1, num_read,
4730Sstevel@tonic-gate 			    out_stream))
4740Sstevel@tonic-gate 				error("Error writing to %s.", in_file_name);
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 		fclose(out_stream);
4770Sstevel@tonic-gate 		fclose(in_stream);
4780Sstevel@tonic-gate 		unlink(out_file_name);
4790Sstevel@tonic-gate 	}
480*712Smuffin 	return (0);
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate void
error(format,args)4840Sstevel@tonic-gate error(format, args)
4850Sstevel@tonic-gate 	char	*format;
4860Sstevel@tonic-gate 	char	*args;
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	fprintf(stderr, "unix2dos: ");
4890Sstevel@tonic-gate 	fprintf(stderr, format, args);
4900Sstevel@tonic-gate 	fprintf(stderr, "  %s.\n", strerror(errno));
4910Sstevel@tonic-gate 	exit(1);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate void
usage()4950Sstevel@tonic-gate usage()
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	fprintf(stderr,
4980Sstevel@tonic-gate 	    "usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
4990Sstevel@tonic-gate 	exit(1);
5000Sstevel@tonic-gate }
501