dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sunpc / other / dos2unix.c
blobf4065478edffd27cf5efafb9372472a92dd5eeef
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <sys/systeminfo.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <errno.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
53 #ifdef _F_BIN
54 #define DOS_BUILD 1
55 #else
56 #define UNIX_BUILD 1
57 #endif
59 /******************************************************************************
60 * INCLUDES AND DEFINES
61 ******************************************************************************/
62 #ifdef UNIX_BUILD
63 #include <sys/types.h>
64 #include <sys/kbio.h>
65 #include <sys/time.h>
66 #include <fcntl.h>
67 #include "../sys/dos_iso.h"
68 #endif
70 #ifdef DOS_BUILD
71 #include <dos.h>
72 #include "..\sys\dos_iso.h"
73 #endif
76 #define GLOBAL
77 #define LOCAL static
78 #define VOID int
79 #define BOOL int
81 #define FALSE 0
82 #define TRUE ~FALSE
84 #define CR 0x0D
85 #define LF 0x0A
86 #define DOS_EOF 0x1A
87 #define MAXLEN 1024
90 /******************************************************************************
91 * FUNCTION AND VARIABLE DECLARATIONS
92 ******************************************************************************/
93 static void error();
94 static void usage();
95 static int tmpfd = -1;
97 /******************************************************************************
98 * ENTRY POINTS
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;
111 int type;
112 int code_page_overide; /* over ride of default codepage */
113 #ifdef UNIX_BUILD
114 int kbdfd;
115 #endif
116 char sysinfo_str[MAXLEN];
118 same_name = FALSE;
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
124 * argument.
127 argv++;
128 in_stream = stdin;
129 out_stream = stdout;
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;
137 argv++;
138 } else if (argc > 1 && !strncmp(*argv,"-7",2)) {
139 translate_mode = CONTENTS_ASCII;
140 argv++;
141 } else if (argc > 1 && !strncmp(*argv,"-ascii",6)) {
142 translate_mode = CONTENTS_DOS;
143 argv++;
144 } else if (argc > 1 && !strncmp(*argv,"-437",4)) {
145 code_page_overide = CODE_PAGE_US;
146 argv++;
147 } else if (argc > 1 && !strncmp(*argv,"-850",4)) {
148 code_page_overide = CODE_PAGE_MULTILINGUAL;
149 argv++;
150 } else if (argc > 1 && !strncmp(*argv,"-860",4)) {
151 code_page_overide = CODE_PAGE_PORTUGAL;
152 argv++;
153 } else if (argc > 1 && !strncmp(*argv,"-863",4)) {
154 code_page_overide = CODE_PAGE_CANADA_FRENCH;
155 argv++;
156 } else if (argc > 1 && !strncmp(*argv,"-865",4)) {
157 code_page_overide = CODE_PAGE_NORWAY;
158 argv++;
159 } else
160 argv++;
161 continue;
162 }else{ /* not a command so must be filename */
163 switch(j){
164 case IN_FILE: /* open in file from cmdline */
165 in_file_name = *argv;
166 j++; /* next file name is outfile */
167 break;
169 case OUT_FILE: /* open out file from cmdline */
170 out_file_name = *argv;
171 j++;
172 break;
174 default:
175 usage();
180 argv++;
183 /* input file is specified */
184 if (j > 0) {
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 */
191 if (j > 1) {
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);
198 if (tmpfd == -1) {
199 error("Couldn't create output file %s.",
200 out_file_name);
202 (void) close(tmpfd);
203 same_name = TRUE;
204 } else
205 same_name = FALSE;
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);
213 #ifdef _F_BIN
214 setmode(fileno(in_stream), O_BINARY);
215 setmode(fileno(out_stream), O_BINARY);
216 #endif
218 #ifdef UNIX_BUILD
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);
223 exit(1);
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");
234 } else {
235 type = 0;
237 switch(type){
238 case 0:
239 case 1: /* United States */
240 dos_to_iso = &dos_to_iso_cp_437[0];
241 break;
243 case 2: /* Belgian French */
244 dos_to_iso = &dos_to_iso_cp_437[0];
245 break;
247 case 3: /* Canadian French */
248 dos_to_iso = &dos_to_iso_cp_863[0];
249 break;
251 case 4: /* Danish */
252 dos_to_iso = &dos_to_iso_cp_865[0];
253 break;
255 case 5: /* German */
256 dos_to_iso = &dos_to_iso_cp_437[0];
257 break;
259 case 6: /* Italian */
260 dos_to_iso = &dos_to_iso_cp_437[0];
261 break;
263 case 7: /* Netherlands Dutch */
264 dos_to_iso = &dos_to_iso_cp_437[0];
265 break;
267 case 8: /* Norwegian */
268 dos_to_iso = &dos_to_iso_cp_865[0];
269 break;
271 case 9: /* Portuguese */
272 dos_to_iso = &dos_to_iso_cp_860[0];
273 break;
275 case 10: /* Spanish */
276 dos_to_iso = &dos_to_iso_cp_437[0];
277 break;
279 case 11: /* Swedish Finnish */
280 dos_to_iso = &dos_to_iso_cp_437[0];
281 break;
283 case 12: /* Swiss French */
284 dos_to_iso = &dos_to_iso_cp_437[0];
285 break;
287 case 13: /* Swiss German */
288 dos_to_iso = &dos_to_iso_cp_437[0];
289 break;
291 case 14: /* United Kingdom */
292 dos_to_iso = &dos_to_iso_cp_437[0];
294 break;
296 default:
297 dos_to_iso = &dos_to_iso_cp_437[0];
298 break;
300 }else{
301 switch(code_page_overide){
302 case CODE_PAGE_US:
303 dos_to_iso = &dos_to_iso_cp_437[0];
304 break;
306 case CODE_PAGE_MULTILINGUAL:
307 dos_to_iso = &dos_to_iso_cp_850[0];
308 break;
310 case CODE_PAGE_PORTUGAL:
311 dos_to_iso = &dos_to_iso_cp_860[0];
312 break;
314 case CODE_PAGE_CANADA_FRENCH:
315 dos_to_iso = &dos_to_iso_cp_863[0];
316 break;
318 case CODE_PAGE_NORWAY:
319 dos_to_iso = &dos_to_iso_cp_865[0];
320 break;
324 #endif
325 #ifdef DOS_BUILD
326 if(!code_page_overide){
328 union REGS regs;
329 regs.h.ah = 0x66; /* get/set global code page */
330 regs.h.al = 0x01; /* get */
331 intdos(&regs, &regs);
332 type = regs.x.bx;
334 switch(type){
335 case 437: /* United States */
336 dos_to_iso = &dos_to_iso_cp_437[0];
337 break;
339 case 850: /* Multilingual */
340 dos_to_iso = &dos_to_iso_cp_850[0];
341 break;
343 case 860: /* Portuguese */
344 dos_to_iso = &dos_to_iso_cp_860[0];
345 break;
347 case 863: /* Canadian French */
348 dos_to_iso = &dos_to_iso_cp_863[0];
349 break;
351 case 865: /* Danish */
352 dos_to_iso = &dos_to_iso_cp_865[0];
353 break;
355 default:
356 dos_to_iso = &dos_to_iso_cp_437[0];
357 break;
359 }else{
360 switch(code_page_overide){
361 case CODE_PAGE_US:
362 dos_to_iso = &dos_to_iso_cp_437[0];
363 break;
365 case CODE_PAGE_MULTILINGUAL:
366 dos_to_iso = &dos_to_iso_cp_850[0];
367 break;
369 case CODE_PAGE_PORTUGAL:
370 dos_to_iso = &dos_to_iso_cp_860[0];
371 break;
373 case CODE_PAGE_CANADA_FRENCH:
374 dos_to_iso = &dos_to_iso_cp_863[0];
375 break;
377 case CODE_PAGE_NORWAY:
378 dos_to_iso = &dos_to_iso_cp_865[0];
379 break;
384 #endif
386 /* While not EOF, read in chars and send them to out_stream
387 * if current char is not a CR.
390 do {
391 num_read = fread(&tmp_buff[0], 1, 100, in_stream);
392 i = 0;
393 out_len = 0;
394 src_str = dest_str = &tmp_buff[0];
395 switch (translate_mode){
396 case CONTENTS_ISO:
398 while ( i++ != num_read ){
399 if( *src_str == '\r'){
400 src_str++;
402 else{
403 out_len++;
404 *dest_str++ = dos_to_iso[*src_str++];
408 break;
410 case CONTENTS_ASCII:
412 while ( i++ != num_read){
413 if( *src_str == '\r'){
414 src_str++;
415 continue;
417 else if ( *src_str > 127 ){
418 *dest_str++ = (unsigned char) ' ';
419 src_str++;
420 out_len++;
422 else{
423 out_len++;
424 *dest_str++ = *src_str++;
428 break;
430 case CONTENTS_DOS:
432 while ( i++ != num_read){
433 if( *src_str == '\r'){
434 src_str++;
435 continue;
437 *dest_str++ = *src_str++;
438 out_len++;
441 break;
443 if (out_len > num_read)
444 out_len = num_read;
445 if (tmp_buff[out_len-2] == DOS_EOF)
446 out_len -= 2;
447 else if (tmp_buff[out_len-1] == DOS_EOF)
448 out_len -= 1;
450 if( out_len > 0 &&
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));
456 fclose(out_stream);
457 fclose(in_stream);
458 if(same_name){
459 unlink(in_file_name);
460 in_stream = fopen(out_file_name, "r");
461 out_stream = fopen(in_file_name, "w");
462 #ifdef _F_BIN
463 setmode(fileno(in_stream), O_BINARY);
464 setmode(fileno(out_stream), O_BINARY);
465 #endif
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);
470 fclose(out_stream);
471 fclose(in_stream);
472 unlink(out_file_name);
474 return (0);
477 void error(format, args)
478 char *format;
479 char *args;
481 fprintf(stderr, "dos2unix: ");
482 fprintf(stderr, format, args);
483 fprintf(stderr, " %s.\n", strerror(errno));
484 exit(1);
487 void usage()
489 fprintf(stderr, "usage: dos2unix [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
490 exit(1);