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