17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*be10f7d9Smuffin * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * Converts files from one char set to another
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * Written 11/09/87 Eddy Bell
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate */
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * INCLUDED and DEFINES
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <fcntl.h>
437c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate /* #include <io.h> for microsoft c 4.0 */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #define CONTENTS_ASCII 0
517c478bd9Sstevel@tonic-gate #define CONTENTS_ASCII8 1
527c478bd9Sstevel@tonic-gate #define CONTENTS_ISO 2
537c478bd9Sstevel@tonic-gate #define CONTENTS_DOS 3
547c478bd9Sstevel@tonic-gate #ifdef _F_BIN
557c478bd9Sstevel@tonic-gate #define DOS_BUILD 1
567c478bd9Sstevel@tonic-gate #else
577c478bd9Sstevel@tonic-gate #define UNIX_BUILD 1
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * INCLUDES AND DEFINES
637c478bd9Sstevel@tonic-gate *
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
667c478bd9Sstevel@tonic-gate #include <sys/types.h>
677c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
687c478bd9Sstevel@tonic-gate #include <sys/time.h>
697c478bd9Sstevel@tonic-gate #include <fcntl.h>
707c478bd9Sstevel@tonic-gate #include "../sys/dos_iso.h"
717c478bd9Sstevel@tonic-gate #endif
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
747c478bd9Sstevel@tonic-gate #include <dos.h>
757c478bd9Sstevel@tonic-gate #include "..\sys\dos_iso.h"
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate #define GLOBAL
807c478bd9Sstevel@tonic-gate #define LOCAL static
817c478bd9Sstevel@tonic-gate #define BOOL int
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate #define FALSE 0
847c478bd9Sstevel@tonic-gate #define TRUE ~FALSE
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #define CR 0x0D
877c478bd9Sstevel@tonic-gate #define LF 0x0A
887c478bd9Sstevel@tonic-gate #define DOS_EOF 0x1A
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #define MAXLEN 1024
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * FUNCTION AND VARIABLE DECLARATIONS
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate static void error();
967c478bd9Sstevel@tonic-gate static void usage();
977c478bd9Sstevel@tonic-gate static int tmpfd = -1;
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * ENTRY POINTS
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate
102*be10f7d9Smuffin int
main(int argc,char ** argv)103*be10f7d9Smuffin main(int argc, char **argv)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate FILE *in_stream = NULL;
1067c478bd9Sstevel@tonic-gate FILE *out_stream = NULL;
1077c478bd9Sstevel@tonic-gate unsigned char tmp_buff[512];
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate unsigned char *src_str, *dest_str;
1107c478bd9Sstevel@tonic-gate char *in_file_name, *out_file_name;
1117c478bd9Sstevel@tonic-gate int num_read, numchar, i, j, out_len, translate_mode;
1127c478bd9Sstevel@tonic-gate int same_name;
1137c478bd9Sstevel@tonic-gate /* char count for fread() */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate int type;
1167c478bd9Sstevel@tonic-gate int code_page_overide; /* over ride of default codepage */
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate unsigned char * dos_to_iso;
1197c478bd9Sstevel@tonic-gate unsigned char iso_to_dos[256];
1207c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
1217c478bd9Sstevel@tonic-gate int kbdfd;
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate char sysinfo_str[MAXLEN];
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate same_name = FALSE;
1267c478bd9Sstevel@tonic-gate out_file_name = (char *)0;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * The filename parameter is positionally dependent - in that
1307c478bd9Sstevel@tonic-gate * the dest file name must follow the source filename
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate argv++;
1337c478bd9Sstevel@tonic-gate in_stream = stdin;
1347c478bd9Sstevel@tonic-gate out_stream = stdout;
1357c478bd9Sstevel@tonic-gate j = 0; /* count for file names 0 -> source 1-> dest */
1367c478bd9Sstevel@tonic-gate translate_mode = CONTENTS_ISO; /* default trans mode */
1377c478bd9Sstevel@tonic-gate code_page_overide = 0;
1387c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) {
1397c478bd9Sstevel@tonic-gate if (*argv[0] == '-') {
1407c478bd9Sstevel@tonic-gate if (argc > 1 && !strncmp(*argv, "-iso", 4)) {
1417c478bd9Sstevel@tonic-gate translate_mode = CONTENTS_ISO;
1427c478bd9Sstevel@tonic-gate argv++;
1437c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-7", 2)) {
1447c478bd9Sstevel@tonic-gate translate_mode = CONTENTS_ASCII;
1457c478bd9Sstevel@tonic-gate argv++;
1467c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-ascii", 6)) {
1477c478bd9Sstevel@tonic-gate translate_mode = CONTENTS_DOS;
1487c478bd9Sstevel@tonic-gate argv++;
1497c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-437", 4)) {
1507c478bd9Sstevel@tonic-gate code_page_overide = CODE_PAGE_US;
1517c478bd9Sstevel@tonic-gate argv++;
1527c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-850", 4)) {
1537c478bd9Sstevel@tonic-gate code_page_overide = CODE_PAGE_MULTILINGUAL;
1547c478bd9Sstevel@tonic-gate argv++;
1557c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-860", 4)) {
1567c478bd9Sstevel@tonic-gate code_page_overide = CODE_PAGE_PORTUGAL;
1577c478bd9Sstevel@tonic-gate argv++;
1587c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-863", 4)) {
1597c478bd9Sstevel@tonic-gate code_page_overide = CODE_PAGE_CANADA_FRENCH;
1607c478bd9Sstevel@tonic-gate argv++;
1617c478bd9Sstevel@tonic-gate } else if (argc > 1 && !strncmp(*argv, "-865", 4)) {
1627c478bd9Sstevel@tonic-gate code_page_overide = CODE_PAGE_NORWAY;
1637c478bd9Sstevel@tonic-gate argv++;
1647c478bd9Sstevel@tonic-gate } else
1657c478bd9Sstevel@tonic-gate argv++;
1667c478bd9Sstevel@tonic-gate continue;
1677c478bd9Sstevel@tonic-gate } else { /* not a command so must be filename */
1687c478bd9Sstevel@tonic-gate switch (j) {
1697c478bd9Sstevel@tonic-gate case IN_FILE: /* open in file from cmdline */
1707c478bd9Sstevel@tonic-gate in_file_name = *argv;
1717c478bd9Sstevel@tonic-gate j++; /* next file name is outfile */
1727c478bd9Sstevel@tonic-gate break;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate case OUT_FILE: /* open out file from cmdline */
1757c478bd9Sstevel@tonic-gate out_file_name = *argv;
1767c478bd9Sstevel@tonic-gate j++;
1777c478bd9Sstevel@tonic-gate break;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate default:
1807c478bd9Sstevel@tonic-gate usage();
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate argv++;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate /* input file is specified */
1877c478bd9Sstevel@tonic-gate if (j > 0) {
1887c478bd9Sstevel@tonic-gate in_stream = fopen(in_file_name, "r");
1897c478bd9Sstevel@tonic-gate if (in_stream == NULL)
1907c478bd9Sstevel@tonic-gate error("Couldn't open input file %s.", in_file_name);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /* output file is specified */
1947c478bd9Sstevel@tonic-gate if (j > 1) {
1957c478bd9Sstevel@tonic-gate if (!strcmp(in_file_name, out_file_name)) {
1967c478bd9Sstevel@tonic-gate /* input and output have same name */
1977c478bd9Sstevel@tonic-gate if (access(out_file_name, 2))
1987c478bd9Sstevel@tonic-gate error("%s not writable.", out_file_name);
1997c478bd9Sstevel@tonic-gate strcpy(out_file_name, "/tmp/udXXXXXX");
2007c478bd9Sstevel@tonic-gate tmpfd = mkstemp(out_file_name);
2017c478bd9Sstevel@tonic-gate if (tmpfd == -1) {
2027c478bd9Sstevel@tonic-gate error("Couldn't create output file %s.",
2037c478bd9Sstevel@tonic-gate out_file_name);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate (void) close(tmpfd);
2067c478bd9Sstevel@tonic-gate same_name = TRUE;
2077c478bd9Sstevel@tonic-gate } else
2087c478bd9Sstevel@tonic-gate same_name = FALSE;
2097c478bd9Sstevel@tonic-gate out_stream = fopen(out_file_name, "w");
2107c478bd9Sstevel@tonic-gate if (out_stream == NULL) {
2117c478bd9Sstevel@tonic-gate (void) unlink(out_file_name);
2127c478bd9Sstevel@tonic-gate error("Couldn't open output file %s.", out_file_name);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate #ifdef _F_BIN
2177c478bd9Sstevel@tonic-gate setmode(fileno(in_stream), O_BINARY);
2187c478bd9Sstevel@tonic-gate setmode(fileno(out_stream), O_BINARY);
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate #ifdef UNIX_BUILD
2237c478bd9Sstevel@tonic-gate if (!code_page_overide) {
2247c478bd9Sstevel@tonic-gate if (sysinfo(SI_ARCHITECTURE, sysinfo_str, MAXLEN) < 0) {
2257c478bd9Sstevel@tonic-gate fprintf(stderr,
2267c478bd9Sstevel@tonic-gate "could not obtain system information\n");
2277c478bd9Sstevel@tonic-gate (void) unlink(out_file_name);
2287c478bd9Sstevel@tonic-gate exit(1);
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate if (strcmp(sysinfo_str, "i386")) {
2327c478bd9Sstevel@tonic-gate if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
2337c478bd9Sstevel@tonic-gate fprintf(stderr,
2347c478bd9Sstevel@tonic-gate "could not open /dev/kbd to get "
2357c478bd9Sstevel@tonic-gate "keyboard type US keyboard assumed\n");
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
2387c478bd9Sstevel@tonic-gate fprintf(stderr,
2397c478bd9Sstevel@tonic-gate "could not get keyboard type US keyboard assumed\n");
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate } else {
2427c478bd9Sstevel@tonic-gate type = 0;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate switch (type) {
2457c478bd9Sstevel@tonic-gate case 0:
2467c478bd9Sstevel@tonic-gate case 1: /* United States */
2477c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate case 2: /* Belgian French */
2517c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2527c478bd9Sstevel@tonic-gate break;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate case 3: /* Canadian French */
2557c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_863[0];
2567c478bd9Sstevel@tonic-gate break;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate case 4: /* Danish */
2597c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_865[0];
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate case 5: /* German */
2637c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate case 6: /* Italian */
2677c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2687c478bd9Sstevel@tonic-gate break;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate case 7: /* Netherlands Dutch */
2717c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate case 8: /* Norwegian */
2757c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_865[0];
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate case 9: /* Portuguese */
2797c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_860[0];
2807c478bd9Sstevel@tonic-gate break;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate case 10: /* Spanish */
2837c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2847c478bd9Sstevel@tonic-gate break;
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate case 11: /* Swedish Finnish */
2877c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2887c478bd9Sstevel@tonic-gate break;
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate case 12: /* Swiss French */
2917c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2927c478bd9Sstevel@tonic-gate break;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate case 13: /* Swiss German */
2957c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate case 14: /* United Kingdom */
2997c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate break;
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate default:
3047c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3057c478bd9Sstevel@tonic-gate break;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate } else {
3087c478bd9Sstevel@tonic-gate switch (code_page_overide) {
3097c478bd9Sstevel@tonic-gate case CODE_PAGE_US:
3107c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3117c478bd9Sstevel@tonic-gate break;
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate case CODE_PAGE_MULTILINGUAL:
3147c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_850[0];
3157c478bd9Sstevel@tonic-gate break;
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate case CODE_PAGE_PORTUGAL:
3187c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_860[0];
3197c478bd9Sstevel@tonic-gate break;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate case CODE_PAGE_CANADA_FRENCH:
3227c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_863[0];
3237c478bd9Sstevel@tonic-gate break;
3247c478bd9Sstevel@tonic-gate
3257c478bd9Sstevel@tonic-gate case CODE_PAGE_NORWAY:
3267c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_865[0];
3277c478bd9Sstevel@tonic-gate break;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate #endif
3317c478bd9Sstevel@tonic-gate #ifdef DOS_BUILD
3327c478bd9Sstevel@tonic-gate if (!code_page_overide) {
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate union REGS regs;
3357c478bd9Sstevel@tonic-gate regs.h.ah = 0x66; /* get/set global code page */
3367c478bd9Sstevel@tonic-gate regs.h.al = 0x01; /* get */
3377c478bd9Sstevel@tonic-gate intdos(®s, ®s);
3387c478bd9Sstevel@tonic-gate type = regs.x.bx;
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate switch (type) {
3417c478bd9Sstevel@tonic-gate case 437: /* United States */
3427c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3437c478bd9Sstevel@tonic-gate break;
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate case 850: /* Multilingual */
3467c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_850[0];
3477c478bd9Sstevel@tonic-gate break;
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate case 860: /* Portuguese */
3507c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_860[0];
3517c478bd9Sstevel@tonic-gate break;
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate case 863: /* Canadian French */
3547c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_863[0];
3557c478bd9Sstevel@tonic-gate break;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate case 865: /* Danish */
3587c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_865[0];
3597c478bd9Sstevel@tonic-gate break;
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate default:
3627c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate } else {
3667c478bd9Sstevel@tonic-gate switch (code_page_overide) {
3677c478bd9Sstevel@tonic-gate case CODE_PAGE_US:
3687c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_437[0];
3697c478bd9Sstevel@tonic-gate break;
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate case CODE_PAGE_MULTILINGUAL:
3727c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_850[0];
3737c478bd9Sstevel@tonic-gate break;
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate case CODE_PAGE_PORTUGAL:
3767c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_860[0];
3777c478bd9Sstevel@tonic-gate break;
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate case CODE_PAGE_CANADA_FRENCH:
3807c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_863[0];
3817c478bd9Sstevel@tonic-gate break;
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate case CODE_PAGE_NORWAY:
3847c478bd9Sstevel@tonic-gate dos_to_iso = &dos_to_iso_cp_865[0];
3857c478bd9Sstevel@tonic-gate break;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate #endif
3907c478bd9Sstevel@tonic-gate for (i = 0; i <= 255; i++) {
3917c478bd9Sstevel@tonic-gate iso_to_dos[dos_to_iso[i]] = i;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate * While not EOF, read in chars and send them to out_stream
3967c478bd9Sstevel@tonic-gate * if current char is not a CR.
3977c478bd9Sstevel@tonic-gate */
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate do {
4007c478bd9Sstevel@tonic-gate num_read = fread(&tmp_buff[100], 1, 100, in_stream);
4017c478bd9Sstevel@tonic-gate i = 0;
4027c478bd9Sstevel@tonic-gate out_len = 0;
4037c478bd9Sstevel@tonic-gate src_str = &tmp_buff[100];
4047c478bd9Sstevel@tonic-gate dest_str = &tmp_buff[0];
4057c478bd9Sstevel@tonic-gate switch (translate_mode) {
4067c478bd9Sstevel@tonic-gate case CONTENTS_ISO:
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate while (i++ != num_read) {
4097c478bd9Sstevel@tonic-gate if (*src_str == '\n') {
4107c478bd9Sstevel@tonic-gate *dest_str++ = '\r';
4117c478bd9Sstevel@tonic-gate out_len++;
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate out_len++;
4147c478bd9Sstevel@tonic-gate *dest_str++ = iso_to_dos[*src_str++];
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate break;
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate case CONTENTS_ASCII:
4207c478bd9Sstevel@tonic-gate while (i++ != num_read) {
4217c478bd9Sstevel@tonic-gate if (*src_str > 127) {
4227c478bd9Sstevel@tonic-gate *dest_str++ =
4237c478bd9Sstevel@tonic-gate (unsigned char) ' ';
4247c478bd9Sstevel@tonic-gate src_str++;
4257c478bd9Sstevel@tonic-gate out_len++;
4267c478bd9Sstevel@tonic-gate } else {
4277c478bd9Sstevel@tonic-gate if (*src_str == '\n') {
4287c478bd9Sstevel@tonic-gate *dest_str++ = '\r';
4297c478bd9Sstevel@tonic-gate out_len++;
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate *dest_str++ = *src_str++;
4327c478bd9Sstevel@tonic-gate out_len++;
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate case CONTENTS_DOS:
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate while (i++ != num_read) {
4407c478bd9Sstevel@tonic-gate if (*src_str == '\n') {
4417c478bd9Sstevel@tonic-gate *dest_str++ = '\r';
4427c478bd9Sstevel@tonic-gate out_len++;
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate *dest_str++ = *src_str++;
4457c478bd9Sstevel@tonic-gate out_len++;
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate break;
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate if (out_len && out_len != fwrite(&tmp_buff[0], 1, out_len,
4517c478bd9Sstevel@tonic-gate out_stream))
4527c478bd9Sstevel@tonic-gate error("Error writing to %s.", out_file_name);
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate } while (!feof(in_stream));
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate #ifdef CTRL_Z_ON_EOF
4577c478bd9Sstevel@tonic-gate tmp_buff[0] = 26;
4587c478bd9Sstevel@tonic-gate fwrite(&tmp_buff[0], 1, 1, out_stream);
4597c478bd9Sstevel@tonic-gate #endif
4607c478bd9Sstevel@tonic-gate fclose(out_stream);
4617c478bd9Sstevel@tonic-gate fclose(in_stream);
4627c478bd9Sstevel@tonic-gate if (same_name) {
4637c478bd9Sstevel@tonic-gate unlink(in_file_name);
4647c478bd9Sstevel@tonic-gate in_stream = fopen(out_file_name, "r");
4657c478bd9Sstevel@tonic-gate out_stream = fopen(in_file_name, "w");
4667c478bd9Sstevel@tonic-gate #ifdef _F_BIN
4677c478bd9Sstevel@tonic-gate setmode(fileno(in_stream), O_BINARY);
4687c478bd9Sstevel@tonic-gate setmode(fileno(out_stream), O_BINARY);
4697c478bd9Sstevel@tonic-gate #endif
4707c478bd9Sstevel@tonic-gate while ((num_read = fread(tmp_buff, 1, sizeof (tmp_buff),
4717c478bd9Sstevel@tonic-gate in_stream)) != 0) {
4727c478bd9Sstevel@tonic-gate if (num_read != fwrite(tmp_buff, 1, num_read,
4737c478bd9Sstevel@tonic-gate out_stream))
4747c478bd9Sstevel@tonic-gate error("Error writing to %s.", in_file_name);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate fclose(out_stream);
4777c478bd9Sstevel@tonic-gate fclose(in_stream);
4787c478bd9Sstevel@tonic-gate unlink(out_file_name);
4797c478bd9Sstevel@tonic-gate }
480*be10f7d9Smuffin return (0);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate void
error(format,args)4847c478bd9Sstevel@tonic-gate error(format, args)
4857c478bd9Sstevel@tonic-gate char *format;
4867c478bd9Sstevel@tonic-gate char *args;
4877c478bd9Sstevel@tonic-gate {
4887c478bd9Sstevel@tonic-gate fprintf(stderr, "unix2dos: ");
4897c478bd9Sstevel@tonic-gate fprintf(stderr, format, args);
4907c478bd9Sstevel@tonic-gate fprintf(stderr, " %s.\n", strerror(errno));
4917c478bd9Sstevel@tonic-gate exit(1);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate void
usage()4957c478bd9Sstevel@tonic-gate usage()
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate fprintf(stderr,
4987c478bd9Sstevel@tonic-gate "usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
4997c478bd9Sstevel@tonic-gate exit(1);
5007c478bd9Sstevel@tonic-gate }
501