#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include "../inc/debug.h"
#include "../inc/files.h"
#include "../inc/getopt.h"

/*
  RCONV.EXE リターンコード変換(UNIX -> DOS)
  結構スピードがはやい。            Y.Tsuzi
*/

/*
 LOG:
  1995, 5, 8	v1.3	←↑これを書いた
*/

#define MSG(a) fprintf(stderr, "%s", a)

#define INPUT_BUF_SIZE	(15 * 1024)
#define	OUTPUT_BUF_SIZE (INPUT_BUF_SIZE * 2)
#define	OUTPUT(a)	(outbuf + (a))

#define __VER__ "v1.3"

char   *inbuf,
       *outbuf;

int     help(void);		/* ヘルプ表示 */
int     rconv(char *name);	/* 変換処理の本体 */
int	mem_init(void);		/* 使用するメモリを確保 */
int	mem_term(void);		/* 確保したメモリを開放 */

int     main(int argc, char **argv)
{
    int     i = 1, c;
    char   *file;
    char   *file_list[100];
    char  **list = file_list;
    
    if (argc < 2) {
	help();
	return 0;
    }
    
    setopt(argc, argv);
    
    while((c = getopt('-', "hH?", &file)) >= 0) {
	switch(c) {
	case 'h':
	case 'H':
	case '?':
	    help();
	    return 1;
	case 0:
	    *list = file;
	    list++;
	    break;
	default:
	    help();
	    return 1;
	}
    }
    *list = NULL;
    
    if(mem_init()) {
	mem_term();
	MSG("rconv: メモリ確保に失敗しました。\n");
	return 1;
    }
    
    list = file_list;
    while(*list) {
	if (access(*list, 02) == 0)
	    if(rconv(*list)) return 2;
	list++;
    }

    mem_term();
    
    return 0;
}

int     rconv(char *name)
{
    char    tmp[80];
    int     src,
            out;
    register
    char   *c;
    register
    int     i,
            j;
    int     n;

    printf("%-15s", name);

    dir_file(name, tmp);	DEBUG(DBprintf("\ndir_file : %s\n", tmp));
    strcat(tmp, ".BAK");	DEBUG(DBprintf("\nstrcat .bak : %s\n", tmp));

    if (access(tmp, 0) == 0) {
	if (access(tmp, 6) == 0) {
	    remove(tmp);
	} else {
	    MSG("rconv: .BAK-file can't rename\n");
	    return 1;
	}
    }
    rename(name, tmp);

    if ((src = open(tmp, O_RDONLY | O_BINARY)) < 0) {
	MSG("rconv: tmp-file can't open\n");
	return 1;
    } else if ((out = open(name, 
			   O_WRONLY | O_BINARY |
			   O_CREAT | O_TRUNC, S_IWRITE)) < 0) {
			       MSG("rconv: out-file can't open\n");
	return 1;
    }
    
    while (!eof(src)) {
	c = inbuf;
	j = 0;
	n = read(src, c, INPUT_BUF_SIZE);

	MSG("*");

	for (i = 0; i < n; i++, c++) {
	    if (*c == '\x0a') {
		*OUTPUT(j) = '\x0d';
		*OUTPUT(++j) = '\x0a';
		j++;
	    } else if (*c != '\x0d') {
		*OUTPUT(j) = *c;
		j++;
	    }
	}
	write(out, outbuf, j);
    }

    close(src);
    close(out);

    MSG("\n");

    return 0;
}

int     help()
{
    MSG("rconv: help\n");
    MSG("--- rconv.exe " __VER__ " --- Y.Tsuzi[" __DATE__ "]\n");
    MSG(" リターンコードを変換する\n");
    MSG(" 使い方: rconv [-hH?] [テキストファイル名...]\n");

    return 0;
}

int	mem_init(void)
{
    if ((inbuf = (char *) malloc(INPUT_BUF_SIZE)) == NULL) return 1;
    if ((outbuf = (char *) malloc(OUTPUT_BUF_SIZE)) == NULL) return 1;
    return 0;
}

int	mem_term(void)
{
    free(outbuf);
    free(inbuf);
    return 0;
}
