#include <stdio.h>
#include <fcntl.h>
#include <sys/systeminfo.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define CONTENTS_ASCII 0
#define CONTENTS_ASCII8 1
#define CONTENTS_ISO 2
#define CONTENTS_DOS 3
#ifdef _F_BIN
#define DOS_BUILD 1
#else
#define UNIX_BUILD 1
#endif
#ifdef UNIX_BUILD
#include <sys/types.h>
#include <sys/kbio.h>
#include <sys/time.h>
#include <fcntl.h>
#include "../sys/dos_iso.h"
#endif
#ifdef DOS_BUILD
#include <dos.h>
#include "..\sys\dos_iso.h"
#endif
#define GLOBAL
#define LOCAL static
#define BOOL int
#define FALSE 0
#define TRUE ~FALSE
#define CR 0x0D
#define LF 0x0A
#define DOS_EOF 0x1A
#define MAXLEN 1024
static void error();
static void usage();
static int tmpfd = -1;
int
main(int argc, char **argv)
{
FILE *in_stream = NULL;
FILE *out_stream = NULL;
unsigned char tmp_buff[512];
unsigned char *src_str, *dest_str;
char *in_file_name, *out_file_name;
int num_read, numchar, i, j, out_len, translate_mode;
int same_name;
int type;
int code_page_overide;
unsigned char * dos_to_iso;
unsigned char iso_to_dos[256];
#ifdef UNIX_BUILD
int kbdfd;
#endif
char sysinfo_str[MAXLEN];
same_name = FALSE;
out_file_name = (char *)0;
argv++;
in_stream = stdin;
out_stream = stdout;
j = 0;
translate_mode = CONTENTS_ISO;
code_page_overide = 0;
for (i = 1; i < argc; i++) {
if (*argv[0] == '-') {
if (argc > 1 && !strncmp(*argv, "-iso", 4)) {
translate_mode = CONTENTS_ISO;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-7", 2)) {
translate_mode = CONTENTS_ASCII;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-ascii", 6)) {
translate_mode = CONTENTS_DOS;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-437", 4)) {
code_page_overide = CODE_PAGE_US;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-850", 4)) {
code_page_overide = CODE_PAGE_MULTILINGUAL;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-860", 4)) {
code_page_overide = CODE_PAGE_PORTUGAL;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-863", 4)) {
code_page_overide = CODE_PAGE_CANADA_FRENCH;
argv++;
} else if (argc > 1 && !strncmp(*argv, "-865", 4)) {
code_page_overide = CODE_PAGE_NORWAY;
argv++;
} else
argv++;
continue;
} else {
switch (j) {
case IN_FILE:
in_file_name = *argv;
j++;
break;
case OUT_FILE:
out_file_name = *argv;
j++;
break;
default:
usage();
}
}
argv++;
}
if (j > 0) {
in_stream = fopen(in_file_name, "r");
if (in_stream == NULL)
error("Couldn't open input file %s.", in_file_name);
}
if (j > 1) {
if (!strcmp(in_file_name, out_file_name)) {
if (access(out_file_name, 2))
error("%s not writable.", out_file_name);
strcpy(out_file_name, "/tmp/udXXXXXX");
tmpfd = mkstemp(out_file_name);
if (tmpfd == -1) {
error("Couldn't create output file %s.",
out_file_name);
}
(void) close(tmpfd);
same_name = TRUE;
} else
same_name = FALSE;
out_stream = fopen(out_file_name, "w");
if (out_stream == NULL) {
(void) unlink(out_file_name);
error("Couldn't open output file %s.", out_file_name);
}
}
#ifdef _F_BIN
setmode(fileno(in_stream), O_BINARY);
setmode(fileno(out_stream), O_BINARY);
#endif
#ifdef UNIX_BUILD
if (!code_page_overide) {
if (sysinfo(SI_ARCHITECTURE, sysinfo_str, MAXLEN) < 0) {
fprintf(stderr,
"could not obtain system information\n");
(void) unlink(out_file_name);
exit(1);
}
if (strcmp(sysinfo_str, "i386")) {
if ((kbdfd = open("/dev/kbd", O_WRONLY)) < 0) {
fprintf(stderr,
"could not open /dev/kbd to get "
"keyboard type US keyboard assumed\n");
}
if (ioctl(kbdfd, KIOCLAYOUT, &type) < 0) {
fprintf(stderr,
"could not get keyboard type US keyboard assumed\n");
}
} else {
type = 0;
}
switch (type) {
case 0:
case 1:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 2:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 3:
dos_to_iso = &dos_to_iso_cp_863[0];
break;
case 4:
dos_to_iso = &dos_to_iso_cp_865[0];
break;
case 5:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 6:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 7:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 8:
dos_to_iso = &dos_to_iso_cp_865[0];
break;
case 9:
dos_to_iso = &dos_to_iso_cp_860[0];
break;
case 10:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 11:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 12:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 13:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 14:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
default:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
}
} else {
switch (code_page_overide) {
case CODE_PAGE_US:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case CODE_PAGE_MULTILINGUAL:
dos_to_iso = &dos_to_iso_cp_850[0];
break;
case CODE_PAGE_PORTUGAL:
dos_to_iso = &dos_to_iso_cp_860[0];
break;
case CODE_PAGE_CANADA_FRENCH:
dos_to_iso = &dos_to_iso_cp_863[0];
break;
case CODE_PAGE_NORWAY:
dos_to_iso = &dos_to_iso_cp_865[0];
break;
}
}
#endif
#ifdef DOS_BUILD
if (!code_page_overide) {
{
union REGS regs;
regs.h.ah = 0x66;
regs.h.al = 0x01;
intdos(®s, ®s);
type = regs.x.bx;
}
switch (type) {
case 437:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case 850:
dos_to_iso = &dos_to_iso_cp_850[0];
break;
case 860:
dos_to_iso = &dos_to_iso_cp_860[0];
break;
case 863:
dos_to_iso = &dos_to_iso_cp_863[0];
break;
case 865:
dos_to_iso = &dos_to_iso_cp_865[0];
break;
default:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
}
} else {
switch (code_page_overide) {
case CODE_PAGE_US:
dos_to_iso = &dos_to_iso_cp_437[0];
break;
case CODE_PAGE_MULTILINGUAL:
dos_to_iso = &dos_to_iso_cp_850[0];
break;
case CODE_PAGE_PORTUGAL:
dos_to_iso = &dos_to_iso_cp_860[0];
break;
case CODE_PAGE_CANADA_FRENCH:
dos_to_iso = &dos_to_iso_cp_863[0];
break;
case CODE_PAGE_NORWAY:
dos_to_iso = &dos_to_iso_cp_865[0];
break;
}
}
#endif
for (i = 0; i <= 255; i++) {
iso_to_dos[dos_to_iso[i]] = i;
}
do {
num_read = fread(&tmp_buff[100], 1, 100, in_stream);
i = 0;
out_len = 0;
src_str = &tmp_buff[100];
dest_str = &tmp_buff[0];
switch (translate_mode) {
case CONTENTS_ISO:
{
while (i++ != num_read) {
if (*src_str == '\n') {
*dest_str++ = '\r';
out_len++;
}
out_len++;
*dest_str++ = iso_to_dos[*src_str++];
}
}
break;
case CONTENTS_ASCII:
while (i++ != num_read) {
if (*src_str > 127) {
*dest_str++ =
(unsigned char) ' ';
src_str++;
out_len++;
} else {
if (*src_str == '\n') {
*dest_str++ = '\r';
out_len++;
}
*dest_str++ = *src_str++;
out_len++;
}
}
break;
case CONTENTS_DOS:
{
while (i++ != num_read) {
if (*src_str == '\n') {
*dest_str++ = '\r';
out_len++;
}
*dest_str++ = *src_str++;
out_len++;
}
}
break;
}
if (out_len && out_len != fwrite(&tmp_buff[0], 1, out_len,
out_stream))
error("Error writing to %s.", out_file_name);
} while (!feof(in_stream));
#ifdef CTRL_Z_ON_EOF
tmp_buff[0] = 26;
fwrite(&tmp_buff[0], 1, 1, out_stream);
#endif
fclose(out_stream);
fclose(in_stream);
if (same_name) {
unlink(in_file_name);
in_stream = fopen(out_file_name, "r");
out_stream = fopen(in_file_name, "w");
#ifdef _F_BIN
setmode(fileno(in_stream), O_BINARY);
setmode(fileno(out_stream), O_BINARY);
#endif
while ((num_read = fread(tmp_buff, 1, sizeof (tmp_buff),
in_stream)) != 0) {
if (num_read != fwrite(tmp_buff, 1, num_read,
out_stream))
error("Error writing to %s.", in_file_name);
}
fclose(out_stream);
fclose(in_stream);
unlink(out_file_name);
}
return (0);
}
void
error(format, args)
char *format;
char *args;
{
fprintf(stderr, "unix2dos: ");
fprintf(stderr, format, args);
fprintf(stderr, " %s.\n", strerror(errno));
exit(1);
}
void
usage()
{
fprintf(stderr,
"usage: unix2dos [ -ascii ] [ -iso ] [ -7 ] [ originalfile [ convertedfile ] ]\n");
exit(1);
}