xref: /titanic_50/usr/src/cmd/sunpc/other/unix2dos.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  *	Converts files from one char set to another
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  *	Written 11/09/87	Eddy Bell
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  *  INCLUDED and DEFINES
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
42*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include	<sys/systeminfo.h>
44*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include	<string.h>
46*7c478bd9Sstevel@tonic-gate #include	<errno.h>
47*7c478bd9Sstevel@tonic-gate /* #include	<io.h>			for microsoft c 4.0 */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #define		CONTENTS_ASCII	0
51*7c478bd9Sstevel@tonic-gate #define		CONTENTS_ASCII8 1
52*7c478bd9Sstevel@tonic-gate #define		CONTENTS_ISO	2
53*7c478bd9Sstevel@tonic-gate #define		CONTENTS_DOS	3
54*7c478bd9Sstevel@tonic-gate #ifdef _F_BIN
55*7c478bd9Sstevel@tonic-gate #define	DOS_BUILD 1
56*7c478bd9Sstevel@tonic-gate #else
57*7c478bd9Sstevel@tonic-gate #define	UNIX_BUILD 1
58*7c478bd9Sstevel@tonic-gate #endif
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * INCLUDES AND DEFINES
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
66*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
67*7c478bd9Sstevel@tonic-gate #include	<sys/kbio.h>
68*7c478bd9Sstevel@tonic-gate #include	<sys/time.h>
69*7c478bd9Sstevel@tonic-gate #include	<fcntl.h>
70*7c478bd9Sstevel@tonic-gate #include "../sys/dos_iso.h"
71*7c478bd9Sstevel@tonic-gate #endif
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
74*7c478bd9Sstevel@tonic-gate #include <dos.h>
75*7c478bd9Sstevel@tonic-gate #include "..\sys\dos_iso.h"
76*7c478bd9Sstevel@tonic-gate #endif
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate #define		GLOBAL
80*7c478bd9Sstevel@tonic-gate #define		LOCAL	static
81*7c478bd9Sstevel@tonic-gate #define		BOOL	int
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #define		FALSE	0
84*7c478bd9Sstevel@tonic-gate #define		TRUE	~FALSE
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #define		CR	0x0D
87*7c478bd9Sstevel@tonic-gate #define		LF	0x0A
88*7c478bd9Sstevel@tonic-gate #define		DOS_EOF 0x1A
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate #define		MAXLEN	1024
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * FUNCTION AND VARIABLE DECLARATIONS
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate static	void	error();
96*7c478bd9Sstevel@tonic-gate static	void	usage();
97*7c478bd9Sstevel@tonic-gate static	int	tmpfd = -1;
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * ENTRY POINTS
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate void
103*7c478bd9Sstevel@tonic-gate main(argc, argv)
104*7c478bd9Sstevel@tonic-gate int	argc;
105*7c478bd9Sstevel@tonic-gate char	**argv;
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	FILE *in_stream = NULL;
108*7c478bd9Sstevel@tonic-gate 	FILE *out_stream = NULL;
109*7c478bd9Sstevel@tonic-gate 	unsigned char tmp_buff[512];
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	unsigned char *src_str, *dest_str;
112*7c478bd9Sstevel@tonic-gate 	char	 *in_file_name, *out_file_name;
113*7c478bd9Sstevel@tonic-gate 	int num_read, numchar, i, j, out_len, translate_mode;
114*7c478bd9Sstevel@tonic-gate 	int same_name;
115*7c478bd9Sstevel@tonic-gate 	/* char count for fread() */
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	int	type;
118*7c478bd9Sstevel@tonic-gate 	int	code_page_overide; /* over ride of default codepage */
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	unsigned char * dos_to_iso;
121*7c478bd9Sstevel@tonic-gate 	unsigned char iso_to_dos[256];
122*7c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
123*7c478bd9Sstevel@tonic-gate 	int	kbdfd;
124*7c478bd9Sstevel@tonic-gate #endif
125*7c478bd9Sstevel@tonic-gate 	char	sysinfo_str[MAXLEN];
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	same_name = FALSE;
128*7c478bd9Sstevel@tonic-gate 	out_file_name = (char *)0;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/*
131*7c478bd9Sstevel@tonic-gate 	 * The filename parameter is positionally dependent - in that
132*7c478bd9Sstevel@tonic-gate 	 * the dest file name must follow the source filename
133*7c478bd9Sstevel@tonic-gate 	 */
134*7c478bd9Sstevel@tonic-gate 	argv++;
135*7c478bd9Sstevel@tonic-gate 	in_stream = stdin;
136*7c478bd9Sstevel@tonic-gate 	out_stream = stdout;
137*7c478bd9Sstevel@tonic-gate 	j = 0;  /* count for file names 0 -> source 1-> dest */
138*7c478bd9Sstevel@tonic-gate 	translate_mode = CONTENTS_ISO; /* default trans mode */
139*7c478bd9Sstevel@tonic-gate 	code_page_overide = 0;
140*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
141*7c478bd9Sstevel@tonic-gate 		if (*argv[0] == '-') {
142*7c478bd9Sstevel@tonic-gate 			if (argc > 1 && !strncmp(*argv, "-iso", 4)) {
143*7c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ISO;
144*7c478bd9Sstevel@tonic-gate 				argv++;
145*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-7", 2)) {
146*7c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_ASCII;
147*7c478bd9Sstevel@tonic-gate 				argv++;
148*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-ascii", 6)) {
149*7c478bd9Sstevel@tonic-gate 				translate_mode = CONTENTS_DOS;
150*7c478bd9Sstevel@tonic-gate 				argv++;
151*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-437", 4)) {
152*7c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_US;
153*7c478bd9Sstevel@tonic-gate 				argv++;
154*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-850", 4)) {
155*7c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_MULTILINGUAL;
156*7c478bd9Sstevel@tonic-gate 				argv++;
157*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-860", 4)) {
158*7c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_PORTUGAL;
159*7c478bd9Sstevel@tonic-gate 				argv++;
160*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-863", 4)) {
161*7c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_CANADA_FRENCH;
162*7c478bd9Sstevel@tonic-gate 				argv++;
163*7c478bd9Sstevel@tonic-gate 			} else if (argc > 1 && !strncmp(*argv, "-865", 4)) {
164*7c478bd9Sstevel@tonic-gate 				code_page_overide = CODE_PAGE_NORWAY;
165*7c478bd9Sstevel@tonic-gate 				argv++;
166*7c478bd9Sstevel@tonic-gate 			} else
167*7c478bd9Sstevel@tonic-gate 				argv++;
168*7c478bd9Sstevel@tonic-gate 			continue;
169*7c478bd9Sstevel@tonic-gate 		} else {  /* not a command so must be filename */
170*7c478bd9Sstevel@tonic-gate 			switch (j) {
171*7c478bd9Sstevel@tonic-gate 				case IN_FILE:	/* open in file from cmdline */
172*7c478bd9Sstevel@tonic-gate 					in_file_name = *argv;
173*7c478bd9Sstevel@tonic-gate 					j++;  /* next file name is outfile */
174*7c478bd9Sstevel@tonic-gate 					break;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 				case OUT_FILE:	/* open out file from cmdline */
177*7c478bd9Sstevel@tonic-gate 					out_file_name = *argv;
178*7c478bd9Sstevel@tonic-gate 					j++;
179*7c478bd9Sstevel@tonic-gate 					break;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 				default:
182*7c478bd9Sstevel@tonic-gate 					usage();
183*7c478bd9Sstevel@tonic-gate 			}
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	argv++;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/* input file is specified */
189*7c478bd9Sstevel@tonic-gate 	if (j > 0) {
190*7c478bd9Sstevel@tonic-gate 		in_stream = fopen(in_file_name, "r");
191*7c478bd9Sstevel@tonic-gate 		if (in_stream == NULL)
192*7c478bd9Sstevel@tonic-gate 			error("Couldn't open input file %s.", in_file_name);
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	/* output file is specified */
196*7c478bd9Sstevel@tonic-gate 	if (j > 1) {
197*7c478bd9Sstevel@tonic-gate 		if (!strcmp(in_file_name, out_file_name)) {
198*7c478bd9Sstevel@tonic-gate 			/* input and output have same name */
199*7c478bd9Sstevel@tonic-gate 			if (access(out_file_name, 2))
200*7c478bd9Sstevel@tonic-gate 				error("%s not writable.", out_file_name);
201*7c478bd9Sstevel@tonic-gate 			strcpy(out_file_name, "/tmp/udXXXXXX");
202*7c478bd9Sstevel@tonic-gate 			tmpfd = mkstemp(out_file_name);
203*7c478bd9Sstevel@tonic-gate 			if (tmpfd == -1) {
204*7c478bd9Sstevel@tonic-gate 				error("Couldn't create output file %s.",
205*7c478bd9Sstevel@tonic-gate 				    out_file_name);
206*7c478bd9Sstevel@tonic-gate 			}
207*7c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
208*7c478bd9Sstevel@tonic-gate 			same_name = TRUE;
209*7c478bd9Sstevel@tonic-gate 		} else
210*7c478bd9Sstevel@tonic-gate 			same_name = FALSE;
211*7c478bd9Sstevel@tonic-gate 		out_stream = fopen(out_file_name, "w");
212*7c478bd9Sstevel@tonic-gate 		if (out_stream == NULL) {
213*7c478bd9Sstevel@tonic-gate 			(void) unlink(out_file_name);
214*7c478bd9Sstevel@tonic-gate 			error("Couldn't open output file %s.", out_file_name);
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate #ifdef _F_BIN
219*7c478bd9Sstevel@tonic-gate 	setmode(fileno(in_stream), O_BINARY);
220*7c478bd9Sstevel@tonic-gate 	setmode(fileno(out_stream), O_BINARY);
221*7c478bd9Sstevel@tonic-gate #endif
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
225*7c478bd9Sstevel@tonic-gate 	if (!code_page_overide) {
226*7c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, sysinfo_str, MAXLEN)  < 0) {
227*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
228*7c478bd9Sstevel@tonic-gate 			    "could not obtain system information\n");
229*7c478bd9Sstevel@tonic-gate 			(void) unlink(out_file_name);
230*7c478bd9Sstevel@tonic-gate 			exit(1);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 		if (strcmp(sysinfo_str, "i386")) {
234*7c478bd9Sstevel@tonic-gate 			if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
235*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
236*7c478bd9Sstevel@tonic-gate 				    "could not open /dev/kbd to get "
237*7c478bd9Sstevel@tonic-gate 				    "keyboard type US keyboard assumed\n");
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 			if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
240*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
241*7c478bd9Sstevel@tonic-gate 	"could not get keyboard type US keyboard assumed\n");
242*7c478bd9Sstevel@tonic-gate 			}
243*7c478bd9Sstevel@tonic-gate 		} else {
244*7c478bd9Sstevel@tonic-gate 			type = 0;
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 		switch (type) {
247*7c478bd9Sstevel@tonic-gate 			case	0:
248*7c478bd9Sstevel@tonic-gate 			case	1:	/* United States */
249*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
250*7c478bd9Sstevel@tonic-gate 			break;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 			case	2:	/* Belgian French */
253*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
254*7c478bd9Sstevel@tonic-gate 			break;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 			case	3:	/* Canadian French */
257*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
258*7c478bd9Sstevel@tonic-gate 			break;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 			case	4:	/* Danish */
261*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
262*7c478bd9Sstevel@tonic-gate 			break;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 			case	5:	/* German */
265*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
266*7c478bd9Sstevel@tonic-gate 			break;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 			case	6:	/* Italian */
269*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
270*7c478bd9Sstevel@tonic-gate 			break;
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 			case	7:	/* Netherlands Dutch */
273*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
274*7c478bd9Sstevel@tonic-gate 			break;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 			case	8:	/* Norwegian */
277*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
278*7c478bd9Sstevel@tonic-gate 			break;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 			case	9:	/* Portuguese */
281*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
282*7c478bd9Sstevel@tonic-gate 			break;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 			case	10:	/* Spanish */
285*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
286*7c478bd9Sstevel@tonic-gate 			break;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 			case	11:	/* Swedish Finnish */
289*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
290*7c478bd9Sstevel@tonic-gate 			break;
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 			case	12:	/* Swiss French */
293*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
294*7c478bd9Sstevel@tonic-gate 			break;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 			case	13:	/* Swiss German */
297*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
298*7c478bd9Sstevel@tonic-gate 			break;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 			case	14:	/* United Kingdom */
301*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 			break;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 			default:
306*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
307*7c478bd9Sstevel@tonic-gate 			break;
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 	} else {
310*7c478bd9Sstevel@tonic-gate 		switch (code_page_overide) {
311*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
312*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
313*7c478bd9Sstevel@tonic-gate 			break;
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
316*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
317*7c478bd9Sstevel@tonic-gate 			break;
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
320*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
321*7c478bd9Sstevel@tonic-gate 			break;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
324*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
325*7c478bd9Sstevel@tonic-gate 			break;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
328*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
329*7c478bd9Sstevel@tonic-gate 			break;
330*7c478bd9Sstevel@tonic-gate 		}
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate #endif
333*7c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
334*7c478bd9Sstevel@tonic-gate 	if (!code_page_overide) {
335*7c478bd9Sstevel@tonic-gate 		{
336*7c478bd9Sstevel@tonic-gate 		union REGS regs;
337*7c478bd9Sstevel@tonic-gate 		regs.h.ah = 0x66;	/* get/set global code page */
338*7c478bd9Sstevel@tonic-gate 		regs.h.al = 0x01;		/* get */
339*7c478bd9Sstevel@tonic-gate 		intdos(&regs, &regs);
340*7c478bd9Sstevel@tonic-gate 		type = regs.x.bx;
341*7c478bd9Sstevel@tonic-gate 		}
342*7c478bd9Sstevel@tonic-gate 		switch (type) {
343*7c478bd9Sstevel@tonic-gate 			case	437:	/* United States */
344*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
345*7c478bd9Sstevel@tonic-gate 			break;
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 			case	850:	/* Multilingual */
348*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
349*7c478bd9Sstevel@tonic-gate 			break;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 			case	860:	/* Portuguese */
352*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
353*7c478bd9Sstevel@tonic-gate 			break;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 			case	863:	/* Canadian French */
356*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
357*7c478bd9Sstevel@tonic-gate 			break;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 			case	865:	/* Danish */
360*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
361*7c478bd9Sstevel@tonic-gate 			break;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 			default:
364*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
365*7c478bd9Sstevel@tonic-gate 			break;
366*7c478bd9Sstevel@tonic-gate 		}
367*7c478bd9Sstevel@tonic-gate 	} else {
368*7c478bd9Sstevel@tonic-gate 		switch (code_page_overide) {
369*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_US:
370*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_437[0];
371*7c478bd9Sstevel@tonic-gate 			break;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_MULTILINGUAL:
374*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_850[0];
375*7c478bd9Sstevel@tonic-gate 			break;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_PORTUGAL:
378*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_860[0];
379*7c478bd9Sstevel@tonic-gate 			break;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_CANADA_FRENCH:
382*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_863[0];
383*7c478bd9Sstevel@tonic-gate 			break;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 			case CODE_PAGE_NORWAY:
386*7c478bd9Sstevel@tonic-gate 				dos_to_iso = &dos_to_iso_cp_865[0];
387*7c478bd9Sstevel@tonic-gate 			break;
388*7c478bd9Sstevel@tonic-gate 		}
389*7c478bd9Sstevel@tonic-gate 	}
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate #endif
392*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 255; i++) {
393*7c478bd9Sstevel@tonic-gate 		iso_to_dos[dos_to_iso[i]] = i;
394*7c478bd9Sstevel@tonic-gate 	}
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	/*
397*7c478bd9Sstevel@tonic-gate 	 * While not EOF, read in chars and send them to out_stream
398*7c478bd9Sstevel@tonic-gate 	 * if current char is not a CR.
399*7c478bd9Sstevel@tonic-gate 	 */
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate     do {
402*7c478bd9Sstevel@tonic-gate 		num_read = fread(&tmp_buff[100], 1, 100, in_stream);
403*7c478bd9Sstevel@tonic-gate 		i = 0;
404*7c478bd9Sstevel@tonic-gate 		out_len = 0;
405*7c478bd9Sstevel@tonic-gate 		src_str = &tmp_buff[100];
406*7c478bd9Sstevel@tonic-gate 		dest_str = &tmp_buff[0];
407*7c478bd9Sstevel@tonic-gate 		switch (translate_mode) {
408*7c478bd9Sstevel@tonic-gate 			case CONTENTS_ISO:
409*7c478bd9Sstevel@tonic-gate 				{
410*7c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
411*7c478bd9Sstevel@tonic-gate 					if (*src_str == '\n') {
412*7c478bd9Sstevel@tonic-gate 						*dest_str++ = '\r';
413*7c478bd9Sstevel@tonic-gate 						out_len++;
414*7c478bd9Sstevel@tonic-gate 						}
415*7c478bd9Sstevel@tonic-gate 					out_len++;
416*7c478bd9Sstevel@tonic-gate 					*dest_str++ = iso_to_dos[*src_str++];
417*7c478bd9Sstevel@tonic-gate 					}
418*7c478bd9Sstevel@tonic-gate 				}
419*7c478bd9Sstevel@tonic-gate 				break;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 			case CONTENTS_ASCII:
422*7c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
423*7c478bd9Sstevel@tonic-gate 					if (*src_str > 127) {
424*7c478bd9Sstevel@tonic-gate 						*dest_str++ =
425*7c478bd9Sstevel@tonic-gate 						    (unsigned char) ' ';
426*7c478bd9Sstevel@tonic-gate 						src_str++;
427*7c478bd9Sstevel@tonic-gate 						out_len++;
428*7c478bd9Sstevel@tonic-gate 					} else {
429*7c478bd9Sstevel@tonic-gate 						if (*src_str == '\n') {
430*7c478bd9Sstevel@tonic-gate 							*dest_str++ = '\r';
431*7c478bd9Sstevel@tonic-gate 							out_len++;
432*7c478bd9Sstevel@tonic-gate 						}
433*7c478bd9Sstevel@tonic-gate 						*dest_str++ = *src_str++;
434*7c478bd9Sstevel@tonic-gate 						out_len++;
435*7c478bd9Sstevel@tonic-gate 					}
436*7c478bd9Sstevel@tonic-gate 				}
437*7c478bd9Sstevel@tonic-gate 				break;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 			case CONTENTS_DOS:
440*7c478bd9Sstevel@tonic-gate 				{
441*7c478bd9Sstevel@tonic-gate 				while (i++ != num_read) {
442*7c478bd9Sstevel@tonic-gate 					if (*src_str == '\n') {
443*7c478bd9Sstevel@tonic-gate 						*dest_str++ = '\r';
444*7c478bd9Sstevel@tonic-gate 						out_len++;
445*7c478bd9Sstevel@tonic-gate 						}
446*7c478bd9Sstevel@tonic-gate 					*dest_str++ =	*src_str++;
447*7c478bd9Sstevel@tonic-gate 					out_len++;
448*7c478bd9Sstevel@tonic-gate 					}
449*7c478bd9Sstevel@tonic-gate 				}
450*7c478bd9Sstevel@tonic-gate 				break;
451*7c478bd9Sstevel@tonic-gate 			}
452*7c478bd9Sstevel@tonic-gate 		if (out_len && out_len != fwrite(&tmp_buff[0], 1, out_len,
453*7c478bd9Sstevel@tonic-gate 		    out_stream))
454*7c478bd9Sstevel@tonic-gate 			error("Error writing to %s.", out_file_name);
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 		} while (!feof(in_stream));
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate #ifdef CTRL_Z_ON_EOF
459*7c478bd9Sstevel@tonic-gate 	tmp_buff[0] = 26;
460*7c478bd9Sstevel@tonic-gate 	fwrite(&tmp_buff[0], 1, 1, out_stream);
461*7c478bd9Sstevel@tonic-gate #endif
462*7c478bd9Sstevel@tonic-gate 	fclose(out_stream);
463*7c478bd9Sstevel@tonic-gate 	fclose(in_stream);
464*7c478bd9Sstevel@tonic-gate 	if (same_name) {
465*7c478bd9Sstevel@tonic-gate 		unlink(in_file_name);
466*7c478bd9Sstevel@tonic-gate 		in_stream = fopen(out_file_name, "r");
467*7c478bd9Sstevel@tonic-gate 		out_stream = fopen(in_file_name, "w");
468*7c478bd9Sstevel@tonic-gate #ifdef _F_BIN
469*7c478bd9Sstevel@tonic-gate 		setmode(fileno(in_stream), O_BINARY);
470*7c478bd9Sstevel@tonic-gate 		setmode(fileno(out_stream), O_BINARY);
471*7c478bd9Sstevel@tonic-gate #endif
472*7c478bd9Sstevel@tonic-gate 		while ((num_read = fread(tmp_buff, 1, sizeof (tmp_buff),
473*7c478bd9Sstevel@tonic-gate 		    in_stream)) != 0) {
474*7c478bd9Sstevel@tonic-gate 			if (num_read != fwrite(tmp_buff, 1, num_read,
475*7c478bd9Sstevel@tonic-gate 			    out_stream))
476*7c478bd9Sstevel@tonic-gate 				error("Error writing to %s.", in_file_name);
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 		fclose(out_stream);
479*7c478bd9Sstevel@tonic-gate 		fclose(in_stream);
480*7c478bd9Sstevel@tonic-gate 		unlink(out_file_name);
481*7c478bd9Sstevel@tonic-gate 	}
482*7c478bd9Sstevel@tonic-gate 	exit(0);
483*7c478bd9Sstevel@tonic-gate }
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate void
486*7c478bd9Sstevel@tonic-gate error(format, args)
487*7c478bd9Sstevel@tonic-gate 	char	*format;
488*7c478bd9Sstevel@tonic-gate 	char	*args;
489*7c478bd9Sstevel@tonic-gate {
490*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "unix2dos: ");
491*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, format, args);
492*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "  %s.\n", strerror(errno));
493*7c478bd9Sstevel@tonic-gate 	exit(1);
494*7c478bd9Sstevel@tonic-gate }
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate void
497*7c478bd9Sstevel@tonic-gate usage()
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	fprintf(stderr,
500*7c478bd9Sstevel@tonic-gate 	    "usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
501*7c478bd9Sstevel@tonic-gate 	exit(1);
502*7c478bd9Sstevel@tonic-gate }
503