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