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