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