xref: /titanic_52/usr/src/cmd/sunpc/other/dos2unix.c (revision be10f7d91e42e379302ee5ead703b5f0bf8b47c6)
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 /*
23*be10f7d9Smuffin  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *	Converts files from one char set to another
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  *	Written 11/09/87	Eddy Bell
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  *  INCLUDED and DEFINES
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate #include	<stdio.h>
417c478bd9Sstevel@tonic-gate #include	<fcntl.h>
427c478bd9Sstevel@tonic-gate #include	<sys/systeminfo.h>
437c478bd9Sstevel@tonic-gate #include	<stdlib.h>
447c478bd9Sstevel@tonic-gate #include	<string.h>
457c478bd9Sstevel@tonic-gate #include	<errno.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*#include	<io.h>			for microsoft c 4.0 */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define 	CONTENTS_ASCII	0
507c478bd9Sstevel@tonic-gate #define 	CONTENTS_ASCII8 1
517c478bd9Sstevel@tonic-gate #define 	CONTENTS_ISO	2
527c478bd9Sstevel@tonic-gate #define 	CONTENTS_DOS	3
537c478bd9Sstevel@tonic-gate #ifdef _F_BIN
547c478bd9Sstevel@tonic-gate #define DOS_BUILD 1
557c478bd9Sstevel@tonic-gate #else
567c478bd9Sstevel@tonic-gate #define UNIX_BUILD 1
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /******************************************************************************
607c478bd9Sstevel@tonic-gate  * INCLUDES AND DEFINES
617c478bd9Sstevel@tonic-gate  ******************************************************************************/
627c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include	<sys/kbio.h>
657c478bd9Sstevel@tonic-gate #include	<sys/time.h>
667c478bd9Sstevel@tonic-gate #include	<fcntl.h>
677c478bd9Sstevel@tonic-gate #include "../sys/dos_iso.h"
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
717c478bd9Sstevel@tonic-gate #include <dos.h>
727c478bd9Sstevel@tonic-gate #include "..\sys\dos_iso.h"
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define 	GLOBAL
777c478bd9Sstevel@tonic-gate #define 	LOCAL	static
787c478bd9Sstevel@tonic-gate #define 	VOID	int
797c478bd9Sstevel@tonic-gate #define 	BOOL	int
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define 	FALSE	0
827c478bd9Sstevel@tonic-gate #define 	TRUE	~FALSE
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define 	CR	0x0D
857c478bd9Sstevel@tonic-gate #define 	LF	0x0A
867c478bd9Sstevel@tonic-gate #define 	DOS_EOF 0x1A
877c478bd9Sstevel@tonic-gate #define		MAXLEN	1024
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /******************************************************************************
917c478bd9Sstevel@tonic-gate  * FUNCTION AND VARIABLE DECLARATIONS
927c478bd9Sstevel@tonic-gate  ******************************************************************************/
937c478bd9Sstevel@tonic-gate static	void	error();
947c478bd9Sstevel@tonic-gate static	void	usage();
957c478bd9Sstevel@tonic-gate static int	tmpfd = -1;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /******************************************************************************
987c478bd9Sstevel@tonic-gate * ENTRY POINTS
997c478bd9Sstevel@tonic-gate  ******************************************************************************/
1007c478bd9Sstevel@tonic-gate 
101*be10f7d9Smuffin int
102*be10f7d9Smuffin main(int argc, char **argv)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate    FILE *in_stream = NULL;
1057c478bd9Sstevel@tonic-gate    FILE *out_stream = NULL;
1067c478bd9Sstevel@tonic-gate 	unsigned char tmp_buff[512];
1077c478bd9Sstevel@tonic-gate 	unsigned char *src_str, *dest_str;
1087c478bd9Sstevel@tonic-gate 	char	 *in_file_name, *out_file_name;
1097c478bd9Sstevel@tonic-gate    int num_read, i, j, out_len, translate_mode, same_name;			       /* char count for fread() */
1107c478bd9Sstevel@tonic-gate    unsigned char * dos_to_iso;
1117c478bd9Sstevel@tonic-gate 	int	type;
1127c478bd9Sstevel@tonic-gate 	int	code_page_overide; /* over ride of default codepage */
1137c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
1147c478bd9Sstevel@tonic-gate 	int	kbdfd;
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 	char	sysinfo_str[MAXLEN];
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	same_name = FALSE;
1197c478bd9Sstevel@tonic-gate 	out_file_name = (char *)0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate     /*	The filename parameter is positionally dependent - it must be the
1227c478bd9Sstevel@tonic-gate      *	second argument, immediately following the program name. Except
1237c478bd9Sstevel@tonic-gate      *	when a char set switch is passed then the file name must be third
1247c478bd9Sstevel@tonic-gate      *	argument.
1257c478bd9Sstevel@tonic-gate      */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	argv++;
1287c478bd9Sstevel@tonic-gate 	in_stream = stdin;
1297c478bd9Sstevel@tonic-gate 	out_stream = stdout;
1307c478bd9Sstevel@tonic-gate 	j = 0;  /* count for file names 0 -> source 1-> dest */
1317c478bd9Sstevel@tonic-gate 	translate_mode = CONTENTS_ISO; /*default trans mode*/
1327c478bd9Sstevel@tonic-gate 	code_page_overide = 0;
1337c478bd9Sstevel@tonic-gate 	for (i=1; i<argc; i++) {
1347c478bd9Sstevel@tonic-gate       		if (*argv[0] == '-') {
1357c478bd9Sstevel@tonic-gate 			if (argc > 1 && !strncmp(*argv,"-iso",4)) {
1367c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ISO;
1377c478bd9Sstevel@tonic-gate 				argv++;
1387c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-7",2)) {
1397c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ASCII;
1407c478bd9Sstevel@tonic-gate 				argv++;
1417c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-ascii",6)) {
1427c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_DOS;
1437c478bd9Sstevel@tonic-gate 				argv++;
1447c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-437",4)) {
1457c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_US;
1467c478bd9Sstevel@tonic-gate 				argv++;
1477c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-850",4)) {
1487c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_MULTILINGUAL;
1497c478bd9Sstevel@tonic-gate 				argv++;
1507c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-860",4)) {
1517c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_PORTUGAL;
1527c478bd9Sstevel@tonic-gate 				argv++;
1537c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-863",4)) {
1547c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_CANADA_FRENCH;
1557c478bd9Sstevel@tonic-gate 				argv++;
1567c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv,"-865",4)) {
1577c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_NORWAY;
1587c478bd9Sstevel@tonic-gate 				argv++;
1597c478bd9Sstevel@tonic-gate 			} else
1607c478bd9Sstevel@tonic-gate 				argv++;
1617c478bd9Sstevel@tonic-gate 			continue;
1627c478bd9Sstevel@tonic-gate 		}else{  /* not a command so must be filename */
1637c478bd9Sstevel@tonic-gate 			switch(j){
1647c478bd9Sstevel@tonic-gate 				case IN_FILE:	/* open in file from cmdline */
1657c478bd9Sstevel@tonic-gate 		       			in_file_name = *argv;
1667c478bd9Sstevel@tonic-gate 		       			j++;  /* next file name is outfile */
1677c478bd9Sstevel@tonic-gate 			       	break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 				case OUT_FILE:	/* open out file from cmdline */
1707c478bd9Sstevel@tonic-gate 					out_file_name = *argv;
1717c478bd9Sstevel@tonic-gate 					j++;
1727c478bd9Sstevel@tonic-gate 			   	break;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 				default:
1757c478bd9Sstevel@tonic-gate 					usage();
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	argv++;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* input file is specified */
1847c478bd9Sstevel@tonic-gate 	if (j > 0) {
1857c478bd9Sstevel@tonic-gate 		in_stream = fopen(in_file_name, "r");
1867c478bd9Sstevel@tonic-gate 		if (in_stream == NULL)
1877c478bd9Sstevel@tonic-gate 			error("Couldn't open input file %s.", in_file_name);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/* output file is secified */
1917c478bd9Sstevel@tonic-gate 	if (j > 1) {
1927c478bd9Sstevel@tonic-gate 		if(!strcmp(in_file_name, out_file_name)){
1937c478bd9Sstevel@tonic-gate 			/* input and output have same name */
1947c478bd9Sstevel@tonic-gate 			if (access(out_file_name, 2))
1957c478bd9Sstevel@tonic-gate 				error("%s not writable.", out_file_name);
1967c478bd9Sstevel@tonic-gate 			strcpy(out_file_name, "/tmp/udXXXXXX");
1977c478bd9Sstevel@tonic-gate 			tmpfd = mkstemp(out_file_name);
1987c478bd9Sstevel@tonic-gate 			if (tmpfd == -1) {
1997c478bd9Sstevel@tonic-gate 				error("Couldn't create output file %s.",
2007c478bd9Sstevel@tonic-gate 				    out_file_name);
2017c478bd9Sstevel@tonic-gate 			}
2027c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
2037c478bd9Sstevel@tonic-gate 			same_name = TRUE;
2047c478bd9Sstevel@tonic-gate 		} else
2057c478bd9Sstevel@tonic-gate 			same_name = FALSE;
2067c478bd9Sstevel@tonic-gate 		out_stream = fopen(out_file_name, "w");
2077c478bd9Sstevel@tonic-gate 		if (out_stream == NULL) {
2087c478bd9Sstevel@tonic-gate 			(void) unlink(out_file_name);
2097c478bd9Sstevel@tonic-gate 			error("Couldn't open output file %s.", out_file_name);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate #ifdef _F_BIN
2147c478bd9Sstevel@tonic-gate 	setmode(fileno(in_stream), O_BINARY);
2157c478bd9Sstevel@tonic-gate 	setmode(fileno(out_stream), O_BINARY);
2167c478bd9Sstevel@tonic-gate #endif
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
2197c478bd9Sstevel@tonic-gate 	if(!code_page_overide){
2207c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE,sysinfo_str,MAXLEN)  < 0) {
2217c478bd9Sstevel@tonic-gate 			fprintf(stderr,"could not obtain system information\n");
2227c478bd9Sstevel@tonic-gate 			(void) unlink(out_file_name);
2237c478bd9Sstevel@tonic-gate 			exit(1);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		if (strcmp(sysinfo_str,"i386")) {
2277c478bd9Sstevel@tonic-gate 			if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
2287c478bd9Sstevel@tonic-gate 				fprintf(stderr, "could not open /dev/kbd to "
2297c478bd9Sstevel@tonic-gate 				    "get keyboard type US keyboard assumed\n");
2307c478bd9Sstevel@tonic-gate 			}
2317c478bd9Sstevel@tonic-gate 			if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
2327c478bd9Sstevel@tonic-gate 				fprintf(stderr,"could not get keyboard type US keyboard assumed\n");
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 		} else {
2357c478bd9Sstevel@tonic-gate 			type = 0;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 		switch(type){
2387c478bd9Sstevel@tonic-gate 			case	0:
2397c478bd9Sstevel@tonic-gate 			case	1:	/* United States */
2407c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 			case	2:	/* Belgian French */
2447c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			case	3:	/* Canadian French */
2487c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			case	4:	/* Danish */
2527c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			case	5:	/* German */
2567c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			case	6:	/* Italian */
2607c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			case	7:	/* Netherlands Dutch */
2647c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2657c478bd9Sstevel@tonic-gate 			break;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			case	8:	/* Norwegian */
2687c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			case	9:	/* Portuguese */
2727c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			case	10:	/* Spanish */
2767c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			case	11:	/* Swedish Finnish */
2807c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			case	12:	/* Swiss French */
2847c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			case	13:	/* Swiss German */
2887c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			case	14:	/* United Kingdom */
2927c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 			default:
2977c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}else{
3017c478bd9Sstevel@tonic-gate 		switch(code_page_overide){
3027c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
3037c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3077c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3117c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3157c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3197c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate #endif
3257c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
3267c478bd9Sstevel@tonic-gate 	if(!code_page_overide){
3277c478bd9Sstevel@tonic-gate 		{
3287c478bd9Sstevel@tonic-gate 		union REGS regs;
3297c478bd9Sstevel@tonic-gate 		regs.h.ah = 0x66;	/* get/set global code page */
3307c478bd9Sstevel@tonic-gate 		regs.h.al = 0x01;		/* get */
3317c478bd9Sstevel@tonic-gate 		intdos(&regs, &regs);
3327c478bd9Sstevel@tonic-gate 		type = regs.x.bx;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		switch(type){
3357c478bd9Sstevel@tonic-gate 			case	437:	/* United States */
3367c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			case	850:	/* Multilingual */
3407c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			case	860:	/* Portuguese */
3447c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			case	863:	/* Canadian French */
3487c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			case	865:	/* Danish */
3527c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 			default:
3567c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3577c478bd9Sstevel@tonic-gate 			break;
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}else{
3607c478bd9Sstevel@tonic-gate 		switch(code_page_overide){
3617c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
3627c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3667c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3707c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3717c478bd9Sstevel@tonic-gate 			break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3747c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3757c478bd9Sstevel@tonic-gate 			break;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3787c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate     /*	While not EOF, read in chars and send them to out_stream
3877c478bd9Sstevel@tonic-gate      *	if current char is not a CR.
3887c478bd9Sstevel@tonic-gate      */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate     do {
3917c478bd9Sstevel@tonic-gate 		num_read = fread(&tmp_buff[0], 1, 100, in_stream);
3927c478bd9Sstevel@tonic-gate 		i = 0;
3937c478bd9Sstevel@tonic-gate 		out_len = 0;
3947c478bd9Sstevel@tonic-gate 		src_str = dest_str = &tmp_buff[0];
3957c478bd9Sstevel@tonic-gate 		switch (translate_mode){
3967c478bd9Sstevel@tonic-gate 			case CONTENTS_ISO:
3977c478bd9Sstevel@tonic-gate 				{
3987c478bd9Sstevel@tonic-gate 				while ( i++ != num_read ){
3997c478bd9Sstevel@tonic-gate 					if( *src_str == '\r'){
4007c478bd9Sstevel@tonic-gate 						src_str++;
4017c478bd9Sstevel@tonic-gate 						}
4027c478bd9Sstevel@tonic-gate 					else{
4037c478bd9Sstevel@tonic-gate 						out_len++;
4047c478bd9Sstevel@tonic-gate 						*dest_str++ = dos_to_iso[*src_str++];
4057c478bd9Sstevel@tonic-gate 						}
4067c478bd9Sstevel@tonic-gate 					}
4077c478bd9Sstevel@tonic-gate 				}
4087c478bd9Sstevel@tonic-gate 				break;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			case CONTENTS_ASCII:
4117c478bd9Sstevel@tonic-gate 				{
4127c478bd9Sstevel@tonic-gate 				while ( i++ != num_read){
4137c478bd9Sstevel@tonic-gate 					if( *src_str == '\r'){
4147c478bd9Sstevel@tonic-gate 						src_str++;
4157c478bd9Sstevel@tonic-gate 						continue;
4167c478bd9Sstevel@tonic-gate 						}
4177c478bd9Sstevel@tonic-gate 					else if ( *src_str > 127 ){
4187c478bd9Sstevel@tonic-gate 						*dest_str++ = (unsigned char) ' ';
4197c478bd9Sstevel@tonic-gate 						src_str++;
4207c478bd9Sstevel@tonic-gate 						out_len++;
4217c478bd9Sstevel@tonic-gate 						}
4227c478bd9Sstevel@tonic-gate 					else{
4237c478bd9Sstevel@tonic-gate 						out_len++;
4247c478bd9Sstevel@tonic-gate 						*dest_str++ = *src_str++;
4257c478bd9Sstevel@tonic-gate 						}
4267c478bd9Sstevel@tonic-gate 					}
4277c478bd9Sstevel@tonic-gate 				}
4287c478bd9Sstevel@tonic-gate 				break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 			case CONTENTS_DOS:
4317c478bd9Sstevel@tonic-gate 				{
4327c478bd9Sstevel@tonic-gate 				while ( i++ != num_read){
4337c478bd9Sstevel@tonic-gate 					if( *src_str == '\r'){
4347c478bd9Sstevel@tonic-gate 						src_str++;
4357c478bd9Sstevel@tonic-gate 						continue;
4367c478bd9Sstevel@tonic-gate 						}
4377c478bd9Sstevel@tonic-gate 						*dest_str++ =	*src_str++;
4387c478bd9Sstevel@tonic-gate 						out_len++;
4397c478bd9Sstevel@tonic-gate 					}
4407c478bd9Sstevel@tonic-gate 				}
4417c478bd9Sstevel@tonic-gate 				break;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 		if (out_len > num_read)
4447c478bd9Sstevel@tonic-gate 			out_len = num_read;
4457c478bd9Sstevel@tonic-gate 		if (tmp_buff[out_len-2] == DOS_EOF)
4467c478bd9Sstevel@tonic-gate 			out_len -= 2;
4477c478bd9Sstevel@tonic-gate 		else if (tmp_buff[out_len-1] == DOS_EOF)
4487c478bd9Sstevel@tonic-gate 			out_len -= 1;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		if( out_len > 0 &&
4517c478bd9Sstevel@tonic-gate 		    out_len != (i= fwrite(&tmp_buff[0], 1, out_len, out_stream)))
4527c478bd9Sstevel@tonic-gate 			error("Error writing %s.", out_file_name);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		} while (!feof(in_stream));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	fclose(out_stream);
4577c478bd9Sstevel@tonic-gate 	fclose(in_stream);
4587c478bd9Sstevel@tonic-gate 	if(same_name){
4597c478bd9Sstevel@tonic-gate 		unlink(in_file_name);
4607c478bd9Sstevel@tonic-gate 		in_stream = fopen(out_file_name, "r");
4617c478bd9Sstevel@tonic-gate 		out_stream = fopen(in_file_name, "w");
4627c478bd9Sstevel@tonic-gate #ifdef _F_BIN
4637c478bd9Sstevel@tonic-gate 		setmode(fileno(in_stream), O_BINARY);
4647c478bd9Sstevel@tonic-gate 		setmode(fileno(out_stream), O_BINARY);
4657c478bd9Sstevel@tonic-gate #endif
4667c478bd9Sstevel@tonic-gate 		while ((num_read = (unsigned)fread(tmp_buff, 1, sizeof tmp_buff, in_stream)) != 0) {
4677c478bd9Sstevel@tonic-gate 		   if( num_read != fwrite(tmp_buff, 1, num_read, out_stream))
4687c478bd9Sstevel@tonic-gate 			error("Error writing %s.", in_file_name);
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		fclose(out_stream);
4717c478bd9Sstevel@tonic-gate 		fclose(in_stream);
4727c478bd9Sstevel@tonic-gate 		unlink(out_file_name);
4737c478bd9Sstevel@tonic-gate 	}
474*be10f7d9Smuffin 	return (0);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate void	error(format, args)
4787c478bd9Sstevel@tonic-gate 	char	*format;
4797c478bd9Sstevel@tonic-gate 	char	*args;
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	fprintf(stderr, "dos2unix: ");
4827c478bd9Sstevel@tonic-gate 	fprintf(stderr, format, args);
4837c478bd9Sstevel@tonic-gate 	fprintf(stderr, "  %s.\n", strerror(errno));
4847c478bd9Sstevel@tonic-gate 	exit(1);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate void usage()
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	fprintf(stderr, "usage: dos2unix [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
4907c478bd9Sstevel@tonic-gate 	exit(1);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
493