4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Converts files from one char set to another
32 * Written 11/09/87 Eddy Bell
38 * INCLUDED and DEFINES
42 #include <sys/systeminfo.h>
47 /*#include <io.h> for microsoft c 4.0 */
49 #define CONTENTS_ASCII 0
50 #define CONTENTS_ASCII8 1
51 #define CONTENTS_ISO 2
52 #define CONTENTS_DOS 3
59 /******************************************************************************
60 * INCLUDES AND DEFINES
61 ******************************************************************************/
63 #include <sys/types.h>
67 #include "../sys/dos_iso.h"
72 #include "..\sys\dos_iso.h"
90 /******************************************************************************
91 * FUNCTION AND VARIABLE DECLARATIONS
92 ******************************************************************************/
95 static int tmpfd
= -1;
97 /******************************************************************************
99 ******************************************************************************/
102 main(int argc
, char **argv
)
104 FILE *in_stream
= NULL
;
105 FILE *out_stream
= NULL
;
106 unsigned char tmp_buff
[512];
107 unsigned char *src_str
, *dest_str
;
108 char *in_file_name
, *out_file_name
;
109 int num_read
, i
, j
, out_len
, translate_mode
, same_name
; /* char count for fread() */
110 unsigned char * dos_to_iso
;
112 int code_page_overide
; /* over ride of default codepage */
116 char sysinfo_str
[MAXLEN
];
119 out_file_name
= NULL
;
121 /* The filename parameter is positionally dependent - it must be the
122 * second argument, immediately following the program name. Except
123 * when a char set switch is passed then the file name must be third
130 j
= 0; /* count for file names 0 -> source 1-> dest */
131 translate_mode
= CONTENTS_ISO
; /*default trans mode*/
132 code_page_overide
= 0;
133 for (i
=1; i
<argc
; i
++) {
134 if (*argv
[0] == '-') {
135 if (argc
> 1 && !strncmp(*argv
,"-iso",4)) {
136 translate_mode
= CONTENTS_ISO
;
138 } else if (argc
> 1 && !strncmp(*argv
,"-7",2)) {
139 translate_mode
= CONTENTS_ASCII
;
141 } else if (argc
> 1 && !strncmp(*argv
,"-ascii",6)) {
142 translate_mode
= CONTENTS_DOS
;
144 } else if (argc
> 1 && !strncmp(*argv
,"-437",4)) {
145 code_page_overide
= CODE_PAGE_US
;
147 } else if (argc
> 1 && !strncmp(*argv
,"-850",4)) {
148 code_page_overide
= CODE_PAGE_MULTILINGUAL
;
150 } else if (argc
> 1 && !strncmp(*argv
,"-860",4)) {
151 code_page_overide
= CODE_PAGE_PORTUGAL
;
153 } else if (argc
> 1 && !strncmp(*argv
,"-863",4)) {
154 code_page_overide
= CODE_PAGE_CANADA_FRENCH
;
156 } else if (argc
> 1 && !strncmp(*argv
,"-865",4)) {
157 code_page_overide
= CODE_PAGE_NORWAY
;
162 }else{ /* not a command so must be filename */
164 case IN_FILE
: /* open in file from cmdline */
165 in_file_name
= *argv
;
166 j
++; /* next file name is outfile */
169 case OUT_FILE
: /* open out file from cmdline */
170 out_file_name
= *argv
;
183 /* input file is specified */
185 in_stream
= fopen(in_file_name
, "r");
186 if (in_stream
== NULL
)
187 error("Couldn't open input file %s.", in_file_name
);
190 /* output file is secified */
192 if(!strcmp(in_file_name
, out_file_name
)){
193 /* input and output have same name */
194 if (access(out_file_name
, 2))
195 error("%s not writable.", out_file_name
);
196 strcpy(out_file_name
, "/tmp/udXXXXXX");
197 tmpfd
= mkstemp(out_file_name
);
199 error("Couldn't create output file %s.",
206 out_stream
= fopen(out_file_name
, "w");
207 if (out_stream
== NULL
) {
208 (void) unlink(out_file_name
);
209 error("Couldn't open output file %s.", out_file_name
);
214 setmode(fileno(in_stream
), O_BINARY
);
215 setmode(fileno(out_stream
), O_BINARY
);
219 if(!code_page_overide
){
220 if (sysinfo(SI_ARCHITECTURE
,sysinfo_str
,MAXLEN
) < 0) {
221 fprintf(stderr
,"could not obtain system information\n");
222 (void) unlink(out_file_name
);
226 if (strcmp(sysinfo_str
,"i386")) {
227 if ((kbdfd
= open("/dev/kbd", O_WRONLY
)) < 0) {
228 fprintf(stderr
, "could not open /dev/kbd to "
229 "get keyboard type US keyboard assumed\n");
231 if (ioctl(kbdfd
, KIOCLAYOUT
, &type
) < 0) {
232 fprintf(stderr
,"could not get keyboard type US keyboard assumed\n");
239 case 1: /* United States */
240 dos_to_iso
= &dos_to_iso_cp_437
[0];
243 case 2: /* Belgian French */
244 dos_to_iso
= &dos_to_iso_cp_437
[0];
247 case 3: /* Canadian French */
248 dos_to_iso
= &dos_to_iso_cp_863
[0];
252 dos_to_iso
= &dos_to_iso_cp_865
[0];
256 dos_to_iso
= &dos_to_iso_cp_437
[0];
259 case 6: /* Italian */
260 dos_to_iso
= &dos_to_iso_cp_437
[0];
263 case 7: /* Netherlands Dutch */
264 dos_to_iso
= &dos_to_iso_cp_437
[0];
267 case 8: /* Norwegian */
268 dos_to_iso
= &dos_to_iso_cp_865
[0];
271 case 9: /* Portuguese */
272 dos_to_iso
= &dos_to_iso_cp_860
[0];
275 case 10: /* Spanish */
276 dos_to_iso
= &dos_to_iso_cp_437
[0];
279 case 11: /* Swedish Finnish */
280 dos_to_iso
= &dos_to_iso_cp_437
[0];
283 case 12: /* Swiss French */
284 dos_to_iso
= &dos_to_iso_cp_437
[0];
287 case 13: /* Swiss German */
288 dos_to_iso
= &dos_to_iso_cp_437
[0];
291 case 14: /* United Kingdom */
292 dos_to_iso
= &dos_to_iso_cp_437
[0];
297 dos_to_iso
= &dos_to_iso_cp_437
[0];
301 switch(code_page_overide
){
303 dos_to_iso
= &dos_to_iso_cp_437
[0];
306 case CODE_PAGE_MULTILINGUAL
:
307 dos_to_iso
= &dos_to_iso_cp_850
[0];
310 case CODE_PAGE_PORTUGAL
:
311 dos_to_iso
= &dos_to_iso_cp_860
[0];
314 case CODE_PAGE_CANADA_FRENCH
:
315 dos_to_iso
= &dos_to_iso_cp_863
[0];
318 case CODE_PAGE_NORWAY
:
319 dos_to_iso
= &dos_to_iso_cp_865
[0];
326 if(!code_page_overide
){
329 regs
.h
.ah
= 0x66; /* get/set global code page */
330 regs
.h
.al
= 0x01; /* get */
331 intdos(®s
, ®s
);
335 case 437: /* United States */
336 dos_to_iso
= &dos_to_iso_cp_437
[0];
339 case 850: /* Multilingual */
340 dos_to_iso
= &dos_to_iso_cp_850
[0];
343 case 860: /* Portuguese */
344 dos_to_iso
= &dos_to_iso_cp_860
[0];
347 case 863: /* Canadian French */
348 dos_to_iso
= &dos_to_iso_cp_863
[0];
351 case 865: /* Danish */
352 dos_to_iso
= &dos_to_iso_cp_865
[0];
356 dos_to_iso
= &dos_to_iso_cp_437
[0];
360 switch(code_page_overide
){
362 dos_to_iso
= &dos_to_iso_cp_437
[0];
365 case CODE_PAGE_MULTILINGUAL
:
366 dos_to_iso
= &dos_to_iso_cp_850
[0];
369 case CODE_PAGE_PORTUGAL
:
370 dos_to_iso
= &dos_to_iso_cp_860
[0];
373 case CODE_PAGE_CANADA_FRENCH
:
374 dos_to_iso
= &dos_to_iso_cp_863
[0];
377 case CODE_PAGE_NORWAY
:
378 dos_to_iso
= &dos_to_iso_cp_865
[0];
386 /* While not EOF, read in chars and send them to out_stream
387 * if current char is not a CR.
391 num_read
= fread(&tmp_buff
[0], 1, 100, in_stream
);
394 src_str
= dest_str
= &tmp_buff
[0];
395 switch (translate_mode
){
398 while ( i
++ != num_read
){
399 if( *src_str
== '\r'){
404 *dest_str
++ = dos_to_iso
[*src_str
++];
412 while ( i
++ != num_read
){
413 if( *src_str
== '\r'){
417 else if ( *src_str
> 127 ){
418 *dest_str
++ = (unsigned char) ' ';
424 *dest_str
++ = *src_str
++;
432 while ( i
++ != num_read
){
433 if( *src_str
== '\r'){
437 *dest_str
++ = *src_str
++;
443 if (out_len
> num_read
)
445 if (tmp_buff
[out_len
-2] == DOS_EOF
)
447 else if (tmp_buff
[out_len
-1] == DOS_EOF
)
451 out_len
!= (i
= fwrite(&tmp_buff
[0], 1, out_len
, out_stream
)))
452 error("Error writing %s.", out_file_name
);
454 } while (!feof(in_stream
));
459 unlink(in_file_name
);
460 in_stream
= fopen(out_file_name
, "r");
461 out_stream
= fopen(in_file_name
, "w");
463 setmode(fileno(in_stream
), O_BINARY
);
464 setmode(fileno(out_stream
), O_BINARY
);
466 while ((num_read
= (unsigned)fread(tmp_buff
, 1, sizeof tmp_buff
, in_stream
)) != 0) {
467 if( num_read
!= fwrite(tmp_buff
, 1, num_read
, out_stream
))
468 error("Error writing %s.", in_file_name
);
472 unlink(out_file_name
);
477 void error(format
, args
)
481 fprintf(stderr
, "dos2unix: ");
482 fprintf(stderr
, format
, args
);
483 fprintf(stderr
, " %s.\n", strerror(errno
));
489 fprintf(stderr
, "usage: dos2unix [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");