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