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"
31 * Converts files from one char set to another
33 * Written 11/09/87 Eddy Bell
39 * INCLUDED and DEFINES
43 #include <sys/systeminfo.h>
47 /* #include <io.h> for microsoft c 4.0 */
50 #define CONTENTS_ASCII 0
51 #define CONTENTS_ASCII8 1
52 #define CONTENTS_ISO 2
53 #define CONTENTS_DOS 3
62 * INCLUDES AND DEFINES
66 #include <sys/types.h>
70 #include "../sys/dos_iso.h"
75 #include "..\sys\dos_iso.h"
93 * FUNCTION AND VARIABLE DECLARATIONS
97 static int tmpfd
= -1;
103 main(int argc
, char **argv
)
105 FILE *in_stream
= NULL
;
106 FILE *out_stream
= NULL
;
107 unsigned char tmp_buff
[512];
109 unsigned char *src_str
, *dest_str
;
110 char *in_file_name
, *out_file_name
;
111 int num_read
, numchar
, i
, j
, out_len
, translate_mode
;
113 /* char count for fread() */
116 int code_page_overide
; /* over ride of default codepage */
118 unsigned char * dos_to_iso
;
119 unsigned char iso_to_dos
[256];
123 char sysinfo_str
[MAXLEN
];
126 out_file_name
= NULL
;
129 * The filename parameter is positionally dependent - in that
130 * the dest file name must follow the source filename
135 j
= 0; /* count for file names 0 -> source 1-> dest */
136 translate_mode
= CONTENTS_ISO
; /* default trans mode */
137 code_page_overide
= 0;
138 for (i
= 1; i
< argc
; i
++) {
139 if (*argv
[0] == '-') {
140 if (argc
> 1 && !strncmp(*argv
, "-iso", 4)) {
141 translate_mode
= CONTENTS_ISO
;
143 } else if (argc
> 1 && !strncmp(*argv
, "-7", 2)) {
144 translate_mode
= CONTENTS_ASCII
;
146 } else if (argc
> 1 && !strncmp(*argv
, "-ascii", 6)) {
147 translate_mode
= CONTENTS_DOS
;
149 } else if (argc
> 1 && !strncmp(*argv
, "-437", 4)) {
150 code_page_overide
= CODE_PAGE_US
;
152 } else if (argc
> 1 && !strncmp(*argv
, "-850", 4)) {
153 code_page_overide
= CODE_PAGE_MULTILINGUAL
;
155 } else if (argc
> 1 && !strncmp(*argv
, "-860", 4)) {
156 code_page_overide
= CODE_PAGE_PORTUGAL
;
158 } else if (argc
> 1 && !strncmp(*argv
, "-863", 4)) {
159 code_page_overide
= CODE_PAGE_CANADA_FRENCH
;
161 } else if (argc
> 1 && !strncmp(*argv
, "-865", 4)) {
162 code_page_overide
= CODE_PAGE_NORWAY
;
167 } else { /* not a command so must be filename */
169 case IN_FILE
: /* open in file from cmdline */
170 in_file_name
= *argv
;
171 j
++; /* next file name is outfile */
174 case OUT_FILE
: /* open out file from cmdline */
175 out_file_name
= *argv
;
186 /* input file is specified */
188 in_stream
= fopen(in_file_name
, "r");
189 if (in_stream
== NULL
)
190 error("Couldn't open input file %s.", in_file_name
);
193 /* output file is specified */
195 if (!strcmp(in_file_name
, out_file_name
)) {
196 /* input and output have same name */
197 if (access(out_file_name
, 2))
198 error("%s not writable.", out_file_name
);
199 strcpy(out_file_name
, "/tmp/udXXXXXX");
200 tmpfd
= mkstemp(out_file_name
);
202 error("Couldn't create output file %s.",
209 out_stream
= fopen(out_file_name
, "w");
210 if (out_stream
== NULL
) {
211 (void) unlink(out_file_name
);
212 error("Couldn't open output file %s.", out_file_name
);
217 setmode(fileno(in_stream
), O_BINARY
);
218 setmode(fileno(out_stream
), O_BINARY
);
223 if (!code_page_overide
) {
224 if (sysinfo(SI_ARCHITECTURE
, sysinfo_str
, MAXLEN
) < 0) {
226 "could not obtain system information\n");
227 (void) unlink(out_file_name
);
231 if (strcmp(sysinfo_str
, "i386")) {
232 if ((kbdfd
= open("/dev/kbd", O_WRONLY
)) < 0) {
234 "could not open /dev/kbd to get "
235 "keyboard type US keyboard assumed\n");
237 if (ioctl(kbdfd
, KIOCLAYOUT
, &type
) < 0) {
239 "could not get keyboard type US keyboard assumed\n");
246 case 1: /* United States */
247 dos_to_iso
= &dos_to_iso_cp_437
[0];
250 case 2: /* Belgian French */
251 dos_to_iso
= &dos_to_iso_cp_437
[0];
254 case 3: /* Canadian French */
255 dos_to_iso
= &dos_to_iso_cp_863
[0];
259 dos_to_iso
= &dos_to_iso_cp_865
[0];
263 dos_to_iso
= &dos_to_iso_cp_437
[0];
266 case 6: /* Italian */
267 dos_to_iso
= &dos_to_iso_cp_437
[0];
270 case 7: /* Netherlands Dutch */
271 dos_to_iso
= &dos_to_iso_cp_437
[0];
274 case 8: /* Norwegian */
275 dos_to_iso
= &dos_to_iso_cp_865
[0];
278 case 9: /* Portuguese */
279 dos_to_iso
= &dos_to_iso_cp_860
[0];
282 case 10: /* Spanish */
283 dos_to_iso
= &dos_to_iso_cp_437
[0];
286 case 11: /* Swedish Finnish */
287 dos_to_iso
= &dos_to_iso_cp_437
[0];
290 case 12: /* Swiss French */
291 dos_to_iso
= &dos_to_iso_cp_437
[0];
294 case 13: /* Swiss German */
295 dos_to_iso
= &dos_to_iso_cp_437
[0];
298 case 14: /* United Kingdom */
299 dos_to_iso
= &dos_to_iso_cp_437
[0];
304 dos_to_iso
= &dos_to_iso_cp_437
[0];
308 switch (code_page_overide
) {
310 dos_to_iso
= &dos_to_iso_cp_437
[0];
313 case CODE_PAGE_MULTILINGUAL
:
314 dos_to_iso
= &dos_to_iso_cp_850
[0];
317 case CODE_PAGE_PORTUGAL
:
318 dos_to_iso
= &dos_to_iso_cp_860
[0];
321 case CODE_PAGE_CANADA_FRENCH
:
322 dos_to_iso
= &dos_to_iso_cp_863
[0];
325 case CODE_PAGE_NORWAY
:
326 dos_to_iso
= &dos_to_iso_cp_865
[0];
332 if (!code_page_overide
) {
335 regs
.h
.ah
= 0x66; /* get/set global code page */
336 regs
.h
.al
= 0x01; /* get */
337 intdos(®s
, ®s
);
341 case 437: /* United States */
342 dos_to_iso
= &dos_to_iso_cp_437
[0];
345 case 850: /* Multilingual */
346 dos_to_iso
= &dos_to_iso_cp_850
[0];
349 case 860: /* Portuguese */
350 dos_to_iso
= &dos_to_iso_cp_860
[0];
353 case 863: /* Canadian French */
354 dos_to_iso
= &dos_to_iso_cp_863
[0];
357 case 865: /* Danish */
358 dos_to_iso
= &dos_to_iso_cp_865
[0];
362 dos_to_iso
= &dos_to_iso_cp_437
[0];
366 switch (code_page_overide
) {
368 dos_to_iso
= &dos_to_iso_cp_437
[0];
371 case CODE_PAGE_MULTILINGUAL
:
372 dos_to_iso
= &dos_to_iso_cp_850
[0];
375 case CODE_PAGE_PORTUGAL
:
376 dos_to_iso
= &dos_to_iso_cp_860
[0];
379 case CODE_PAGE_CANADA_FRENCH
:
380 dos_to_iso
= &dos_to_iso_cp_863
[0];
383 case CODE_PAGE_NORWAY
:
384 dos_to_iso
= &dos_to_iso_cp_865
[0];
390 for (i
= 0; i
<= 255; i
++) {
391 iso_to_dos
[dos_to_iso
[i
]] = i
;
395 * While not EOF, read in chars and send them to out_stream
396 * if current char is not a CR.
400 num_read
= fread(&tmp_buff
[100], 1, 100, in_stream
);
403 src_str
= &tmp_buff
[100];
404 dest_str
= &tmp_buff
[0];
405 switch (translate_mode
) {
408 while (i
++ != num_read
) {
409 if (*src_str
== '\n') {
414 *dest_str
++ = iso_to_dos
[*src_str
++];
420 while (i
++ != num_read
) {
421 if (*src_str
> 127) {
427 if (*src_str
== '\n') {
431 *dest_str
++ = *src_str
++;
439 while (i
++ != num_read
) {
440 if (*src_str
== '\n') {
444 *dest_str
++ = *src_str
++;
450 if (out_len
&& out_len
!= fwrite(&tmp_buff
[0], 1, out_len
,
452 error("Error writing to %s.", out_file_name
);
454 } while (!feof(in_stream
));
458 fwrite(&tmp_buff
[0], 1, 1, out_stream
);
463 unlink(in_file_name
);
464 in_stream
= fopen(out_file_name
, "r");
465 out_stream
= fopen(in_file_name
, "w");
467 setmode(fileno(in_stream
), O_BINARY
);
468 setmode(fileno(out_stream
), O_BINARY
);
470 while ((num_read
= fread(tmp_buff
, 1, sizeof (tmp_buff
),
472 if (num_read
!= fwrite(tmp_buff
, 1, num_read
,
474 error("Error writing to %s.", in_file_name
);
478 unlink(out_file_name
);
488 fprintf(stderr
, "unix2dos: ");
489 fprintf(stderr
, format
, args
);
490 fprintf(stderr
, " %s.\n", strerror(errno
));
498 "usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");