xref: /illumos-gate/usr/src/cmd/sunpc/other/unix2dos.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 /*
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 /*
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 /* #include	<io.h>			for microsoft c 4.0 */
457c478bd9Sstevel@tonic-gate 
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 /*
597c478bd9Sstevel@tonic-gate  * INCLUDES AND DEFINES
607c478bd9Sstevel@tonic-gate  *
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		BOOL	int
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define		FALSE	0
817c478bd9Sstevel@tonic-gate #define		TRUE	~FALSE
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define		CR	0x0D
847c478bd9Sstevel@tonic-gate #define		LF	0x0A
857c478bd9Sstevel@tonic-gate #define		DOS_EOF 0x1A
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define		MAXLEN	1024
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * FUNCTION AND VARIABLE DECLARATIONS
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate static	void	error();
937c478bd9Sstevel@tonic-gate static	void	usage();
947c478bd9Sstevel@tonic-gate static	int	tmpfd = -1;
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * ENTRY POINTS
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate 
99*be10f7d9Smuffin int
main(int argc,char ** argv)100*be10f7d9Smuffin 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 
1067c478bd9Sstevel@tonic-gate 	unsigned char *src_str, *dest_str;
1077c478bd9Sstevel@tonic-gate 	char	 *in_file_name, *out_file_name;
1087c478bd9Sstevel@tonic-gate 	int num_read, numchar, i, j, out_len, translate_mode;
1097c478bd9Sstevel@tonic-gate 	int same_name;
1107c478bd9Sstevel@tonic-gate 	/* char count for fread() */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	int	type;
1137c478bd9Sstevel@tonic-gate 	int	code_page_overide; /* over ride of default codepage */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	unsigned char * dos_to_iso;
1167c478bd9Sstevel@tonic-gate 	unsigned char iso_to_dos[256];
1177c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
1187c478bd9Sstevel@tonic-gate 	int	kbdfd;
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 	char	sysinfo_str[MAXLEN];
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	same_name = FALSE;
1237c478bd9Sstevel@tonic-gate 	out_file_name = (char *)0;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * The filename parameter is positionally dependent - in that
1277c478bd9Sstevel@tonic-gate 	 * the dest file name must follow the source filename
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	argv++;
1307c478bd9Sstevel@tonic-gate 	in_stream = stdin;
1317c478bd9Sstevel@tonic-gate 	out_stream = stdout;
1327c478bd9Sstevel@tonic-gate 	j = 0;  /* count for file names 0 -> source 1-> dest */
1337c478bd9Sstevel@tonic-gate 	translate_mode = CONTENTS_ISO; /* default trans mode */
1347c478bd9Sstevel@tonic-gate 	code_page_overide = 0;
1357c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1367c478bd9Sstevel@tonic-gate 		if (*argv[0] == '-') {
1377c478bd9Sstevel@tonic-gate 			if (argc > 1 && !strncmp(*argv, "-iso", 4)) {
1387c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ISO;
1397c478bd9Sstevel@tonic-gate 				argv++;
1407c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-7", 2)) {
1417c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ASCII;
1427c478bd9Sstevel@tonic-gate 				argv++;
1437c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-ascii", 6)) {
1447c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_DOS;
1457c478bd9Sstevel@tonic-gate 				argv++;
1467c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-437", 4)) {
1477c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_US;
1487c478bd9Sstevel@tonic-gate 				argv++;
1497c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-850", 4)) {
1507c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_MULTILINGUAL;
1517c478bd9Sstevel@tonic-gate 				argv++;
1527c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-860", 4)) {
1537c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_PORTUGAL;
1547c478bd9Sstevel@tonic-gate 				argv++;
1557c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-863", 4)) {
1567c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_CANADA_FRENCH;
1577c478bd9Sstevel@tonic-gate 				argv++;
1587c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-865", 4)) {
1597c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_NORWAY;
1607c478bd9Sstevel@tonic-gate 				argv++;
1617c478bd9Sstevel@tonic-gate 			} else
1627c478bd9Sstevel@tonic-gate 				argv++;
1637c478bd9Sstevel@tonic-gate 			continue;
1647c478bd9Sstevel@tonic-gate 		} else {  /* not a command so must be filename */
1657c478bd9Sstevel@tonic-gate 			switch (j) {
1667c478bd9Sstevel@tonic-gate 				case IN_FILE:	/* open in file from cmdline */
1677c478bd9Sstevel@tonic-gate 					in_file_name = *argv;
1687c478bd9Sstevel@tonic-gate 					j++;  /* next file name is outfile */
1697c478bd9Sstevel@tonic-gate 					break;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 				case OUT_FILE:	/* open out file from cmdline */
1727c478bd9Sstevel@tonic-gate 					out_file_name = *argv;
1737c478bd9Sstevel@tonic-gate 					j++;
1747c478bd9Sstevel@tonic-gate 					break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 				default:
1777c478bd9Sstevel@tonic-gate 					usage();
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 specified */
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 
2197c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
2207c478bd9Sstevel@tonic-gate 	if (!code_page_overide) {
2217c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, sysinfo_str, MAXLEN)  < 0) {
2227c478bd9Sstevel@tonic-gate 			fprintf(stderr,
2237c478bd9Sstevel@tonic-gate 			    "could not obtain system information\n");
2247c478bd9Sstevel@tonic-gate 			(void) unlink(out_file_name);
2257c478bd9Sstevel@tonic-gate 			exit(1);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		if (strcmp(sysinfo_str, "i386")) {
2297c478bd9Sstevel@tonic-gate 			if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
2307c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2317c478bd9Sstevel@tonic-gate 				    "could not open /dev/kbd to get "
2327c478bd9Sstevel@tonic-gate 				    "keyboard type US keyboard assumed\n");
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 			if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
2357c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2367c478bd9Sstevel@tonic-gate 	"could not get keyboard type US keyboard assumed\n");
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 		} else {
2397c478bd9Sstevel@tonic-gate 			type = 0;
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 		switch (type) {
2427c478bd9Sstevel@tonic-gate 			case	0:
2437c478bd9Sstevel@tonic-gate 			case	1:	/* United States */
2447c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 			case	2:	/* Belgian French */
2487c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 			case	3:	/* Canadian French */
2527c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			case	4:	/* Danish */
2567c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			case	5:	/* German */
2607c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			case	6:	/* Italian */
2647c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2657c478bd9Sstevel@tonic-gate 			break;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			case	7:	/* Netherlands Dutch */
2687c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			case	8:	/* Norwegian */
2727c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			case	9:	/* Portuguese */
2767c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 			case	10:	/* Spanish */
2807c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			case	11:	/* Swedish Finnish */
2847c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			case	12:	/* Swiss French */
2887c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 			case	13:	/* Swiss German */
2927c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			case	14:	/* United Kingdom */
2967c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			break;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			default:
3017c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		switch (code_page_overide) {
3067c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
3077c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3117c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3157c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3197c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3237c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3247c478bd9Sstevel@tonic-gate 			break;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate #endif
3287c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
3297c478bd9Sstevel@tonic-gate 	if (!code_page_overide) {
3307c478bd9Sstevel@tonic-gate 		{
3317c478bd9Sstevel@tonic-gate 		union REGS regs;
3327c478bd9Sstevel@tonic-gate 		regs.h.ah = 0x66;	/* get/set global code page */
3337c478bd9Sstevel@tonic-gate 		regs.h.al = 0x01;		/* get */
3347c478bd9Sstevel@tonic-gate 		intdos(&regs, &regs);
3357c478bd9Sstevel@tonic-gate 		type = regs.x.bx;
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 		switch (type) {
3387c478bd9Sstevel@tonic-gate 			case	437:	/* United States */
3397c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			case	850:	/* Multilingual */
3437c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 			case	860:	/* Portuguese */
3477c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 			case	863:	/* Canadian French */
3517c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 			case	865:	/* Danish */
3557c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3567c478bd9Sstevel@tonic-gate 			break;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 			default:
3597c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 	} else {
3637c478bd9Sstevel@tonic-gate 		switch (code_page_overide) {
3647c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
3657c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
3667c478bd9Sstevel@tonic-gate 			break;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
3697c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
3737c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
3777c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
3817c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
3827c478bd9Sstevel@tonic-gate 			break;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate #endif
3877c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 255; i++) {
3887c478bd9Sstevel@tonic-gate 		iso_to_dos[dos_to_iso[i]] = i;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * While not EOF, read in chars and send them to out_stream
3937c478bd9Sstevel@tonic-gate 	 * if current char is not a CR.
3947c478bd9Sstevel@tonic-gate 	 */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate     do {
3977c478bd9Sstevel@tonic-gate 		num_read = fread(&tmp_buff[100], 1, 100, in_stream);
3987c478bd9Sstevel@tonic-gate 		i = 0;
3997c478bd9Sstevel@tonic-gate 		out_len = 0;
4007c478bd9Sstevel@tonic-gate 		src_str = &tmp_buff[100];
4017c478bd9Sstevel@tonic-gate 		dest_str = &tmp_buff[0];
4027c478bd9Sstevel@tonic-gate 		switch (translate_mode) {
4037c478bd9Sstevel@tonic-gate 			case CONTENTS_ISO:
4047c478bd9Sstevel@tonic-gate 				{
4057c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
4067c478bd9Sstevel@tonic-gate 					if (*src_str == '\n') {
4077c478bd9Sstevel@tonic-gate 						*dest_str++ = '\r';
4087c478bd9Sstevel@tonic-gate 						out_len++;
4097c478bd9Sstevel@tonic-gate 						}
4107c478bd9Sstevel@tonic-gate 					out_len++;
4117c478bd9Sstevel@tonic-gate 					*dest_str++ = iso_to_dos[*src_str++];
4127c478bd9Sstevel@tonic-gate 					}
4137c478bd9Sstevel@tonic-gate 				}
4147c478bd9Sstevel@tonic-gate 				break;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			case CONTENTS_ASCII:
4177c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
4187c478bd9Sstevel@tonic-gate 					if (*src_str > 127) {
4197c478bd9Sstevel@tonic-gate 						*dest_str++ =
4207c478bd9Sstevel@tonic-gate 						    (unsigned char) ' ';
4217c478bd9Sstevel@tonic-gate 						src_str++;
4227c478bd9Sstevel@tonic-gate 						out_len++;
4237c478bd9Sstevel@tonic-gate 					} else {
4247c478bd9Sstevel@tonic-gate 						if (*src_str == '\n') {
4257c478bd9Sstevel@tonic-gate 							*dest_str++ = '\r';
4267c478bd9Sstevel@tonic-gate 							out_len++;
4277c478bd9Sstevel@tonic-gate 						}
4287c478bd9Sstevel@tonic-gate 						*dest_str++ = *src_str++;
4297c478bd9Sstevel@tonic-gate 						out_len++;
4307c478bd9Sstevel@tonic-gate 					}
4317c478bd9Sstevel@tonic-gate 				}
4327c478bd9Sstevel@tonic-gate 				break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 			case CONTENTS_DOS:
4357c478bd9Sstevel@tonic-gate 				{
4367c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
4377c478bd9Sstevel@tonic-gate 					if (*src_str == '\n') {
4387c478bd9Sstevel@tonic-gate 						*dest_str++ = '\r';
4397c478bd9Sstevel@tonic-gate 						out_len++;
4407c478bd9Sstevel@tonic-gate 						}
4417c478bd9Sstevel@tonic-gate 					*dest_str++ =	*src_str++;
4427c478bd9Sstevel@tonic-gate 					out_len++;
4437c478bd9Sstevel@tonic-gate 					}
4447c478bd9Sstevel@tonic-gate 				}
4457c478bd9Sstevel@tonic-gate 				break;
4467c478bd9Sstevel@tonic-gate 			}
4477c478bd9Sstevel@tonic-gate 		if (out_len && out_len != fwrite(&tmp_buff[0], 1, out_len,
4487c478bd9Sstevel@tonic-gate 		    out_stream))
4497c478bd9Sstevel@tonic-gate 			error("Error writing to %s.", out_file_name);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		} while (!feof(in_stream));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate #ifdef CTRL_Z_ON_EOF
4547c478bd9Sstevel@tonic-gate 	tmp_buff[0] = 26;
4557c478bd9Sstevel@tonic-gate 	fwrite(&tmp_buff[0], 1, 1, out_stream);
4567c478bd9Sstevel@tonic-gate #endif
4577c478bd9Sstevel@tonic-gate 	fclose(out_stream);
4587c478bd9Sstevel@tonic-gate 	fclose(in_stream);
4597c478bd9Sstevel@tonic-gate 	if (same_name) {
4607c478bd9Sstevel@tonic-gate 		unlink(in_file_name);
4617c478bd9Sstevel@tonic-gate 		in_stream = fopen(out_file_name, "r");
4627c478bd9Sstevel@tonic-gate 		out_stream = fopen(in_file_name, "w");
4637c478bd9Sstevel@tonic-gate #ifdef _F_BIN
4647c478bd9Sstevel@tonic-gate 		setmode(fileno(in_stream), O_BINARY);
4657c478bd9Sstevel@tonic-gate 		setmode(fileno(out_stream), O_BINARY);
4667c478bd9Sstevel@tonic-gate #endif
4677c478bd9Sstevel@tonic-gate 		while ((num_read = fread(tmp_buff, 1, sizeof (tmp_buff),
4687c478bd9Sstevel@tonic-gate 		    in_stream)) != 0) {
4697c478bd9Sstevel@tonic-gate 			if (num_read != fwrite(tmp_buff, 1, num_read,
4707c478bd9Sstevel@tonic-gate 			    out_stream))
4717c478bd9Sstevel@tonic-gate 				error("Error writing to %s.", in_file_name);
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 		fclose(out_stream);
4747c478bd9Sstevel@tonic-gate 		fclose(in_stream);
4757c478bd9Sstevel@tonic-gate 		unlink(out_file_name);
4767c478bd9Sstevel@tonic-gate 	}
477*be10f7d9Smuffin 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate void
error(format,args)4817c478bd9Sstevel@tonic-gate error(format, args)
4827c478bd9Sstevel@tonic-gate 	char	*format;
4837c478bd9Sstevel@tonic-gate 	char	*args;
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	fprintf(stderr, "unix2dos: ");
4867c478bd9Sstevel@tonic-gate 	fprintf(stderr, format, args);
4877c478bd9Sstevel@tonic-gate 	fprintf(stderr, "  %s.\n", strerror(errno));
4887c478bd9Sstevel@tonic-gate 	exit(1);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate void
usage()4927c478bd9Sstevel@tonic-gate usage()
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	fprintf(stderr,
4957c478bd9Sstevel@tonic-gate 	    "usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
4967c478bd9Sstevel@tonic-gate 	exit(1);
4977c478bd9Sstevel@tonic-gate }
498