/**********************************************************

                       FAX ADAPTER PCF9600
                               TO
                           TOWNS TIFF
                           <CONVERTOR>

**********************************************************/
/*
typedef struct runlengs{

unsigned	int		rno;		
unsigned	int		rrun;		
unsigned	char	rlen;
};
*/

/*
Facsimile(G3規格) 圧縮符号テーブル

１次元圧縮符号 (ﾀｰﾐﾈｰﾄ符号) 
    白ﾗﾝﾚﾝｸﾞｽ       符号   
*/
static runlengs[2][105][3] = {
         2,0x000E,4,
         3,0x0001,4,
         4,0x000D,4,
         5,0x0003,4,
         6,0x0007,4,
         7,0x000F,4,
         8,0x0019,5,
         9,0x0005,5,
        10,0x001C,5,
        11,0x0002,5,
        64,0x001B,5,
       128,0x0009,5,
         1,0x0038,6,
        12,0x0004,6,
        13,0x0030,6,
        14,0x000B,6,
        15,0x002B,6,
        16,0x0015,6,
        17,0x0035,6,
       192,0x003A,6,
      1664,0x0006,6,
        18,0x0072,7,
        19,0x0018,7,
        20,0x0008,7,
        21,0x0074,7,
        22,0x0060,7,
        23,0x0010,7,
        24,0x000A,7,
        25,0x006A,7,
        26,0x0064,7,
        27,0x0012,7,
        28,0x000C,7,
       256,0x0076,7,
         0,0x00AC,8,
        29,0x0040,8,
        30,0x00C0,8,
        31,0x0058,8,
        32,0x00D8,8,
        33,0x0048,8,
        34,0x00C8,8,
        35,0x0028,8,
        36,0x00A8,8,
        37,0x0068,8,
        38,0x00E8,8,
        39,0x0014,8,
        40,0x0094,8,
        41,0x0054,8,
        42,0x00D4,8,
        43,0x0034,8,
        44,0x00B4,8,
        45,0x0020,8,
        46,0x00A0,8,
        47,0x0050,8,
        48,0x00D0,8,
        49,0x004A,8,
        50,0x00CA,8,
        51,0x002A,8,
        52,0x00AA,8,
        53,0x0024,8,
        54,0x00A4,8,
        55,0x001A,8,
        56,0x009A,8,
        57,0x005A,8,
        58,0x00DA,8,
        59,0x0052,8,
        60,0x00D2,8,
        61,0x004C,8,
        62,0x00CC,8,
        63,0x002C,8,
       320,0x006C,8,
       384,0x00EC,8,
       448,0x0026,8,
       512,0x00A6,8,
       576,0x0016,8,
       640,0x00E6,8,
       704,0x0066,9,
       768,0x0166,9,
       832,0x0096,9,
       896,0x0196,9,
       960,0x0056,9,
      1024,0x0156,9,
      1088,0x00D6,9,
      1152,0x01D6,9,
      1216,0x0036,9,
      1280,0x0136,9,
      1344,0x00B6,9,
      1408,0x01B6,9,
      1472,0x0032,9,
      1536,0x0132,9,
      1600,0x00B2,9,
      1728,0x01B2,9,
      1792,0x0080,11,
      1856,0x0180,11,
      1920,0x0580,11,
         0,0x0800,12,
      1984,0x0480,12,
      2048,0x0C80,12,
      2112,0x0280,12,
      2176,0x0A80,12,
      2240,0x0680,12,
      2304,0x0E80,12,
      2368,0x0380,12,
      2432,0x0B80,12,
      2496,0x0780,12,
      2560,0x0F80,12,
         2,0x0003,2,
         3,0x0001,2,
         1,0x0002,3,
         4,0x0006,3,
         5,0x000C,4,
         6,0x0004,4,
         7,0x0018,5,
         8,0x0028,6,
         9,0x0008,6,
        10,0x0010,7,
        11,0x0050,7,
        12,0x0070,7,
        13,0x0020,8,
        14,0x00E0,8,
        15,0x0030,9,
         0,0x03B0,10,
        16,0x03A0,10,
        17,0x0060,10,
        18,0x0040,10,
        64,0x03C0,10,
        19,0x0730,11,
        20,0x00B0,11,
        21,0x01B0,11,
        22,0x0760,11,
        23,0x00A0,11,
        24,0x0740,11,
        25,0x00C0,11,
      1792,0x0080,11,
      1856,0x0180,11,
      1920,0x0580,11,
        26,0x0530,12,
        27,0x0D30,12,
        28,0x0330,12,
        29,0x0B30,12,
        30,0x0160,12,
        31,0x0960,12,
        32,0x0560,12,
        33,0x0D60,12,
        34,0x04B0,12,
        35,0x0CB0,12,
        36,0x02B0,12,
        37,0x0AB0,12,
        38,0x06B0,12,
        39,0x0EB0,12,
        40,0x0360,12,
        41,0x0B60,12,
        42,0x05B0,12,
        43,0x0DB0,12,
        44,0x02A0,12,
        45,0x0AA0,12,
        46,0x06A0,12,
        47,0x0EA0,12,
        48,0x0260,12,
        49,0x0A60,12,
        50,0x04A0,12,
        51,0x0CA0,12,
        52,0x0240,12,
        53,0x0EC0,12,
        54,0x01C0,12,
        55,0x0E40,12,
        56,0x0140,12,
        57,0x01A0,12,
        58,0x09A0,12,
        59,0x0D40,12,
        60,0x0340,12,
        61,0x05A0,12,
        62,0x0660,12,
        63,0x0E60,12,
       128,0x0130,12,
       192,0x0930,12,
       256,0x0DA0,12,
       320,0x0CC0,12,
       384,0x02C0,12,
       448,0x0AC0,12,
         0,0x0800,12,
      1984,0x0480,12,
      2048,0x0C80,12,
      2112,0x0280,12,
      2176,0x0A80,12,
      2240,0x0680,12,
      2304,0x0E80,12,
      2368,0x0380,12,
      2432,0x0B80,12,
      2496,0x0780,12,
      2560,0x0F80,12,
       512,0x06C0,13,
       576,0x16C0,13,
       640,0x0A40,13,
       704,0x1A40,13,
       768,0x0640,13,
       832,0x1640,13,
       896,0x09C0,13,
       960,0x19C0,13,
      1024,0x05C0,13,
      1088,0x15C0,13,
      1152,0x0DC0,13,
      1216,0x1DC0,13,
      1280,0x0940,13,
      1344,0x1940,13,
      1408,0x0540,13,
      1472,0x1540,13,
      1536,0x0B40,13,
      1600,0x1B40,13,
      1664,0x04C0,13,
      1728,0x14C0,13,
  };
static  runlno[2][14][2] = {
		0,0,
		0,0,
		0,0,
		0,0,
		0,5,
		6,11,
		12,20,
		21,32,
		33,74,
		75,90,
		90,90,
		91,93,
		94,104,
		104,104,
	/* black */
		0,0,
		0,0,
		0,1,
		2,3,
		4,5,
		6,6,
		7,8,
		9,11,
		12,13,
		14,14,
		15,19,
		20,29,
		30,84,
		85,104,
	};

 
/********************************************************************
    プロトタイプ
*********************************************************************/

int optionset( int, char** );

 
static unsigned long ret,pan;

#include <io.h>
#include <fcntl.h>
#include <stat.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <mos.h>
#include <math.h>
#include <stat.h>

/* T-BIOS C ﾗｲﾌﾞﾗﾘ ﾍｯﾀﾞｰ */
#include <fmcfrb.h>
char str[256];
char extname[256];
char namehead[256];
char fname[256];
char filepath[256];
char lsfname[7][256];
char datname[256];
char wildfname[100][256];
char sccext[16];
static char Pcfdata[304*3360];
static char cmpdata[304*256];
static char Monodata[4][2500];
static char Faxdata[1024*256];
static char Faxhead[256];
static char Pcfhead[256];
static char Tifhead[512];
static char Workdata[2063];
static struct find_t wildfbuf;
int wildcard;
int w_ret;
int mode;
int fileno;

unsigned int 	monopoint,monoline,bw,bwdot,i,moji,encode;
int	extend;
unsigned long	savesize;
unsigned short	findbit,ffound,wlong;
unsigned long	faxwidth;
unsigned long	pcfwidth;
unsigned long   a1,a2,a,b,bb,j;
unsigned long	dat_size;
unsigned long	x,xx,y,dd;
unsigned char d1,d2;
int faxshiftbit = 0;
int findflag;
int wildct;
int namelen;

unsigned int 	faxtotif = 2;
unsigned int 	flong = 0;
unsigned int 	point = 0;
unsigned int 	mix = 0;
unsigned long 	linepoint = 0x95;
unsigned long 	faxpoint = 0;
unsigned long 	faxdatalong = 0;
unsigned int 	tifline = 0;
unsigned int 	faxline = 0;
unsigned int 	tifpage =0;
unsigned long	dteol = 0x8000;
unsigned long	faxextlong = 0;
unsigned long	cmpct =0;
unsigned long	cmppoint =0;
unsigned long	savepoint =0;
int cmpzflag =0;
int option = 0;						/* ｵﾌﾟｼｮﾝ ﾃﾞｰﾀ */
int cmpflag = 0;
int revflag = 0;
/**************************************************************

					変換メイン処理

**************************************************************/


int chgmain(){

faxshiftbit = 0;
faxtotif = 2;
flong = 0;
point = 0;
mix = 0;
linepoint = 0x95;
faxpoint = 0;
faxdatalong = 0;
tifline = 0;
faxline = 0;
tifpage =0;
dteol = 0x8000;
faxextlong = 0;
cmpct =0;
cmppoint =0;
savepoint =0;
cmpzflag =0;


	printf("read file = %s \n",lsfname[0] );
	printf("write file = %s \n",lsfname[1] );

/*ret = 0;
return ret;
*/
/*printf ("INPUT FILE=%s  \n", lsfname[0] );
printf (" B/S CHECK = %d \n",mix );
*/
/*KYB_init();
KYB_setcode( 0 );
*/
KYB_clrbuf();


strcpy(fname,datname); 

	if (_access(fname,0) !=0 ){
		printf("FILE %s not found \n",fname);
		exit(-1);
	}
	else{
		pan=_open(fname,_O_BINARY | _O_RDWR );
		if (pan== -1 ){
			exit(-1);
		}
		ret=_read(pan,Faxhead,255);
		if (ret== -1){
			_close(pan);
			exit (-1);
		}
	}
	dat_size=0;
	for (i=0x9d;i<ret;i++){
		if (Faxhead[i]==0x0d){
			break;
		}
		else{
			dat_size=dat_size*10+Faxhead[i]-0x30;
		}
	}
	printf("Data size= %d \n",dat_size);
strcpy(fname,"TIFFMONO.HED"); 

	if (_access(fname,0) !=0 ){
		printf("FILE %s not found \n",fname);
		exit(-1);
	}
	else{
		pan=_open(fname,_O_BINARY | _O_RDWR );
		if (pan== -1 ){
			exit(-1);
		}
		ret=_read(pan,Tifhead,512);
		if (ret== -1){
			_close(pan);
			exit (-1);
		}
	}
	
strcpy(fname,lsfname[0]); 

	if (_access(fname,0) !=0 ){
		printf("no faxdata file");
		exit(-1);
	}
	else{
		pan=_open(fname,_O_BINARY | _O_RDWR );
		if (pan== -1 ){
			exit(-1);
		}
	/*	printf(" file loaded successfly ");*/
		ret = _read(pan,&Faxdata[0],dat_size);
		if (ret == -1 || ret != dat_size ){
			_close(pan);
			printf("読み込みエラ−が発生しました。%d \n",ret);
			exit (-1);
		}
		else{
			faxdatalong = ret;
			_close(pan);
			printf("読み込み終了 datalong= %d \n",faxdatalong);
/*			printf(" faxpoint= %x \n",faxpoint);*/
		}
		switch ( Faxhead[0x45] ) {
			case '0':
				faxwidth = 1728;
				pcfwidth = 216;
				Pcfhead[0] = 0xa4;
				strcpy( extname, ".A4" ); 
				break;
			case '1':
				faxwidth = 2432;
				pcfwidth = 304;
				Pcfhead[0] = 0xa3;
				strcpy( extname, ".A3" ); 
				break;
			case '2':
				faxwidth = 2048;
				pcfwidth = 256;
				Pcfhead[0] = 0xb4;
				strcpy( extname, ".B4" ); 
				break;
			default:
				faxwidth = 2432;
				pcfwidth = 304;
				Pcfhead[0] = 0xa3;
				strcpy( extname, ".A3" ); 
				break;
		}
		if(cmpflag==0){
				strcpy( extname, ".TIF" ); 
		}
		strncat( lsfname[1],extname,4 );
		printf ("OUTPUT FILE=%s \n", lsfname[1] );

	}
/*	printf (" rrs= %x \n",Faxhead[0x3f] );
*/	if ( Faxhead[0x3f] == 0x31 ){
		faxtotif = 1;
	}
	if ( Faxhead[0x3f] == 0x30 ){
		faxtotif = 2;
	}
	printf("fax-->tiff line = %d \n",faxtotif );
	
	extract ();
	pcfsave ();
	ret = 0;
	return ret;
}
/*****************************************************************

						デ−タ展開ル−チン


*****************************************************************/


int extract ( void ){

	monopoint = 0;
	monoline = 0;
	bw = 0;
	point =2;
	faxshiftbit = 0;
	extend=0;

	if ( revflag == 1){
		bwdot = 1;
	}
	else{
		bwdot = 0;
	}
	a1 = Faxdata[0]+Faxdata[1]*256;
	if ( a1 != dteol ){
		printf("先頭がEOLではありません｡data= %x >< %x \n",a1,dteol );
		exit (-1);
	}
	while ( extend == 0 ){
		runext();
		if ( wlong < 64 ){
			switch ( bw ){
				case	0:
					bw = 1;
					if ( revflag == 1){
						bwdot = 0;
					}
					else{
						bwdot = 1;
					}
					break;
				case	1:
					bw = 0;
					if ( revflag == 1){
						bwdot = 1;
					}
					else{
						bwdot = 0;
					}
					break;
			}
		}
		chkeol();
		moji = KYB_read( 1, &encode );
		if ( moji == 0x71 ){
			printf("moji = %x \n ",moji );
			printf("BREAK at TIFPAGE= %d TIFLINE= %d ADRESS= %x MONOLINE= %d monopoint= %d \n",tifpage+1,tifline,point,monoline,monopoint);
			exit(-1);
		}
	}
	printf("展開が終了しました｡ペ−ジ数は % d Y方向ライン数は %d \n",tifpage,tifline);
return (0);
}
/***********************************************************

			          ランレングス展開
			
***********************************************************/

int runext ( void ){

/*	printf("%x %x %x \n",Faxdata[point],Faxdata[point+1],Faxdata[point+2]);
*/
	a1=Faxdata[point]+Faxdata[point+1]*256+Faxdata[point+2]*256*256;
	a2= a1;
	if ( faxshiftbit >0 ){
		for (j = 0; j<faxshiftbit; j++ ){
			a1 /= 2;
		}
	} 
/*	printf("point= %x \n",point);  
	printf("a1= %x \n",a1);  
	printf("a2= %x \n",a2);  
*/	ffound = 0;
	
	for ( j = 2; j<14; j++ ){
		b = 1;
		bb = 1;
		i = 1;
		while ( i < j ){
			bb *= 2;
			b += bb;
			i++;
		}
		
		a = ( a1 & b );
/*		printf("a= %x b = %x \n",a,b ); 
*/		findbit = 0;
		for (i = runlno[bw][j][0]; i <= runlno[bw][j][1]; i++ ){
			if ( a == runlengs[bw][i][1] ){
				ffound = 1;
				wlong = runlengs[bw][i][0];
/*				if( wlong==0 ){
					printf("白ランの長さ 0 \n");
				}
*/

/*				printf("data found! bit=%d bw=%d wlong=%d \n",j ,bw ,wlong);*/ 
				faxshiftbit += j;
				if ( ( faxshiftbit>7 ) && ( faxshiftbit<16 ) ){
					point ++;
				}
				if ( faxshiftbit >15 ){
					point += 2;
				}
				faxshiftbit = fmod ( faxshiftbit, 8 );  
				break;
			}
		}
		if ( ffound == 1){
			break;
		}
	}
	
	if ( (ffound == 0 ) && (a2 == 0x800000 ) ){
		point += 1;
		ffound = 1;
		wlong = 0;
	}
	if  (ffound == 0){
		printf("符号が見つかりません｡ 3byte-FaxData= %x Adress= %x BW= %d \n",a2,point,bw);
		printf("shiftbit= %d bitlong= %d a1= %x  a= %x \n",faxshiftbit,j,a1,a );		printf("%x %x %x \n",Faxdata[point],Faxdata[point+1],Faxdata[point+2]);

		exit (-1);
	}
	if ( wlong != 0 ){ 
		for ( i = 0; i < wlong; i++ ){
			Monodata[ monoline ][ monopoint ] = bwdot;
			monopoint ++;
		}
	}
	faxextlong = faxextlong + wlong;
return (0);
}
/**********************************************************

                    ライン切替え

**********************************************************/

int chgfaxline( void ){
monoline += 1;
/*printf("--1 Line long = %d \n",faxextlong ); */
monopoint = 0; 
faxshiftbit = 0;
faxextlong = 0;
if(faxtotif==2 || cmpflag==1 ){
	tifconv();
}
else{
	tifconv();
	tifconv();
}
monoline = 0;
bw = 0;
if ( revflag == 1){
	bwdot = 1;
}
else{
	bwdot = 0;
}
wlong=0;
return (0);
}
/*********************************************************

					ＴＩＦ変換

*********************************************************/

int tifconv( void ){

	dd = 0;
	
	for ( x =0; x< faxwidth ; x+=8 ){
		d1 = 0;
		d2 = 0;
		for (xx = 0; xx <8; xx++ ){
			d1 <<= 1;
			d2 = Monodata[0][ xx+x ];
			d1 = d1 | d2;
		}
		if(cmpflag==1){
			Pcfdata[pcfwidth*faxline+dd ] =  d1;
		}
		else{
			Pcfdata[pcfwidth*faxline+dd ] =  255-d1;
		}
		dd += 1;
	}
faxline += 1;
return(0);
}
/*******************************************************

				エラ−ライン
				
*******************************************************/

int errfaxline( void ){
	switch ( faxtotif ){
	case  2: 
		if ( monoline == 1 ){
			tifconv();
			monoline = 0;
		}
		break;
	case 1:
		tifconv();
		monoline = 0;
		break;
	}
	monopoint = 0; 
	printf("error eol found %d \n",faxline );
	return(0);
}
/*********************************************************

			ＥＯＬ及びＲＴＣ検出

*********************************************************/

int chkeol( void ){

if ( faxshiftbit > 0 ){
	a1=Faxdata[point];
	for (j = 0; j<faxshiftbit; j++ ){
		a1 /= 2;
	}
	if ( a1 != 0 ){
		return(0);
	}
	point += 1;
}
a1=Faxdata[point]+Faxdata[point+1]*256;
	if ( a1 != dteol ){
		if ( faxshiftbit > 0 ){
			point -= 1;
		}
	return(0);
	}
/************
  EOL検出
************/
/*printf("fax line change! point= %x \n",point );*/ 
chgfaxline();
point += 2;
/*printf("point inc= %x \n",point );*/
faxshiftbit = 0;
a1=Faxdata[point]+Faxdata[point+1]*256;
	switch ( a1 ){
	case	0x8000:
				extend = 1;
				tifconv();
				point += 10;
				break;
	case	0xc000:
				printf("------errfaxline %d -------- \n",tifline );
				errfaxline();
				point += 2;
				break;
	default:
				break;
	}
return (0);
}

/*******************************************************

						PCF save

*******************************************************/

int pcfsave ( void ){
	if ( cmpflag != 1 ){
		Tifhead[0x1e] = faxwidth % 256;
		Tifhead[0x1f] = faxwidth / ( 256 );
		Tifhead[0x20] = 0;
		Tifhead[0x21] = 0;
		Tifhead[0x2a] = faxline % 256;
		Tifhead[0x2b] = faxline / 256;
		Tifhead[0x2c] = 0;
		Tifhead[0x2d] = 0;
	
		savesize = pcfwidth*faxline;
		strcpy( fname, lsfname[1] );
	/*	Tifhead[10] = savesize % 256;
		Tifhead[11] = (savesize /256) % 256;
		Tifhead[12] = ((savesize /256) / 256) % 256;
		Tifhead[13] = ((savesize /256) / 256) / 256;
	*/
		savedata();
	}
	else{
		Pcfhead[1] = faxwidth % 256;
		Pcfhead[2] = faxwidth / ( 256 );
		Pcfhead[3] = 0;
		Pcfhead[4] = 0;
		Pcfhead[5] = faxline % 256;
		Pcfhead[6] = faxline / 256;
		Pcfhead[7] = 0;
		Pcfhead[8] = 0;
		Pcfhead[9] = faxtotif % 256;
	
		cmppoint =0;
		savepoint =0;
		savesize=0;		
		for ( i =0 ; i< pcfwidth*faxline ; i+=256){
			for( j=0;j<256;j++){
				d1 = Pcfdata[i+j];
				if( d1==0 ){   /*  0を発見 */
					cmpct = 1;
					for ( ; ; ){
						if(Pcfdata[i+j+1] != 0 || j == 255 ){
							break;
						}
						else{
							j++;
							cmpct++;
						}
					}	
					if( cmpct==256 ){
						cmpdata[cmppoint+0]=0x00 % 256;
						cmpdata[cmppoint+1]=0x00 % 256;
						cmpdata[cmppoint+2]=0xff % 256;
						cmppoint+=3;
						savesize+=3;
					}
					else{
						cmpdata[cmppoint+0] = 0x00 % 256;
						cmpdata[cmppoint+1] = cmpct % 256;
						cmppoint+=2;
						savesize+=2;
					}
				}
				else{
					cmpdata[cmppoint]=Pcfdata[i+j];
					cmppoint++;
					savesize++;
				}
			}
			if(cmppoint<i+256){
				for(j=0;j<cmppoint;j++){
					Pcfdata[savepoint+j]=cmpdata[j];
				}
				savepoint+=cmppoint;
				cmppoint=0;
			}
			else{
				if(cmppoint>304*250){
					printf("Bad Compress late! Work failed  \n");
					exit(-1);
				}
			}
		}
		if(cmppoint>0){
			for(j=0;j<cmppoint;j++){
				Pcfdata[savepoint+j]=cmpdata[j];
			}
			savepoint+=cmppoint;
			cmppoint=0;
		}
		Pcfhead[10] = savesize % 256;
		Pcfhead[11] = (savesize /256) % 256;
		Pcfhead[12] = ((savesize /256) / 256) % 256;
		Pcfhead[13] = ((savesize /256) / 256) / 256;
		Pcfhead[14] = 1;
		strcpy( fname, lsfname[1] );
		savedatacmp();
	}



		return (0);
}

/***************************************************************
【ﾃﾞ-ﾀﾌｧｲﾙを作成する】

		概要	
		用法	savedata();
		引数	無し
***************************************************************/

int savedata ( void )
{
	int		wsize;

	if ( _access( fname, 0 ) == 0 ) {
		printf( "同名のファイルがあります。上書きします。\n" );
		ret = _unlink ( fname );
	}
	ret = 0;
	pan = _open( fname,_O_BINARY | _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE );
	if ( pan == -1 ) {
		printf("書き込みのオ−プンに失敗しました。\n" );
		ret = -1;
	}
	if ( ret == 0 ) {
		wsize = _write( pan, &Tifhead[0], 512  );
		if ( wsize == -1 || wsize != 512 ) {
			printf("ヘッダの書き込みに失敗しました。\n" );
			_close( pan );
			exit (-1);
		}
		else{
			wsize = _write( pan, &Pcfdata[0], savesize  );
			if ( wsize == -1 || wsize != ( savesize ) ) {
				printf("書き込みに失敗しました。\n" );
				_close( pan );
				exit (-1);
			}
			else{
				printf("書き込みました \n" );
			}
		}
	}
	return (0);
}
/***************************************************************
【圧縮ﾃﾞ-ﾀﾌｧｲﾙを作成する】

		概要	
		用法	savedatacmp();
		引数	無し
***************************************************************/

int savedatacmp ( void )
{
	int		wsize;

	if ( _access( fname, 0 ) == 0 ) {
		printf( "同名のファイルがあります。上書きします。\n" );
		ret = _unlink ( fname );
	}
	ret = 0;
	pan = _open( fname,_O_BINARY | _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE );
	if ( pan == -1 ) {
		printf("書き込みのオ−プンに失敗しました。\n" );
		ret = -1;
	}
	if ( ret == 0 ) {
		wsize = _write( pan, &Pcfhead[0], 16  );
		if ( wsize == -1 || wsize != 16 ) {
			printf("ヘッダの書き込みに失敗しました。\n" );
			_close( pan );
			exit (-1);
		}
		else{
			wsize = _write( pan, &Pcfdata[0], savesize  );
			if ( wsize == -1 || wsize != ( savesize ) ) {
				printf("書き込みに失敗しました。\n" );
				_close( pan );
				exit (-1);
			}
			else{
				printf("書き込みました \n" );
			}
		}
	}
	return (0);
}
/************************************************************
【オプション解析】

		概要	ｵﾌﾟｼｮﾝﾊﾟﾗﾒ-ﾀの解析を行い結果を返す
		用法	optionset( argc, *argv[] );
		引数	int		argc
				char	*argv[]
		戻り値 	int		0	正常終了
						-1	ｵﾌﾟｼｮﾝの指定が正しくない
************************************************************/

int optionset( int argc, char *argv[] ){

	ret = 0;
	int		count;
	char	c;
	int		ii = 0;

	/* ｵﾌﾟｼｮﾝ ﾊﾟﾗﾒｰﾀ 解析 */
/*	printf ( "argc= %d \n",argc );*/
	if ( argc == 1 ) {
		printf(" no input file name \n ");
		exit (-1);
	} 
	else{
/*		printf("argc > 1 \n ");*/
	}
/*	for ( i=0; i<argc; i++){
		printf("para %d = %s \n",i,argv[i]);
	}
*/	for ( count = 1; count < argc; count++ ) {
		c = *argv[count];

		strcpy( str, argv[count] );
		putmessage();

/*		printf("argv %d : %s \n ",count,argv[count] );
*/		if ( (  c == '-' ) || (c == '/' ) ) {
			/*printf (" option - find \n "); */
			mix = 0;
			c = *(argv[count] + 1);
			switch ( c ) {
				case 'h':
				case 'H':
				case '?':
					strcpy(str,"*************** FAX-ADAPTER I/O PCF9600 CONVERTER <FAX2PCF>************** \n");
					putmessage();
					strcpy(str,"用法) \n");
					putmessage();
					
					strcpy(str,"RUN386 -nocrt FAX2PCF.EXP [-h/-c] inputfile output file \n");
					putmessage();
					strcpy(str," -h ヘルプを表示 \n");
					putmessage();
					exit(-1);
					break;

				case 'c':
				case 'C':
					cmpflag=1;
					break;

				case 'r':
				case 'R':
					revflag=1;
					break;
				/* 無効 ﾊﾟﾗﾒｰﾀ */

				default:
					printf("error option \n");
						
			}/* switch  */
		}/* if end */
			
		else{
/*			printf( "argv no. %d : %s \n ",count,argv[count] );*/
			strcpy(fname, argv[count] );
			strcpy( str, fname);
			putmessage();
			ii += 1;
		}
	}		/* for count */


	namelen=strlen(fname);
	flong=namelen;
	for ( i =namelen-1; (i+1)>0 ;i-- ){
		fname[i]=toupper(fname[i]);
	}
	for ( i =namelen-1; (i+1)>0 ;i-- ){		/* "\"検索 */
		if ( fname[i] != 0x5c ){
			flong -= 1;
		}
		else {
			findflag=1;
			break;
		}
	}
	if(findflag==0){
		flong=1;
		for ( i = 0; i <2 ;i ++ ){	/* ":"検索 */
			if ( fname[i] != 0x3a ){
				flong += 1;
			}
			else {
				findflag=2;
				break;
			}
		}
	}
	if(findflag!=0){

		printf( "Drive/Dir name find \n");
		strncpy( filepath,fname,flong );
		for (i=flong;i<namelen;i++){
			namehead[i-flong]=fname[i];
		}
	}
	else{
		for (i=0; i<namelen-1;i++){
			strcpy(namehead,fname);
		} 
	}
	for ( i = 0; i <254 ;i ++ ){
		if ( namehead[i] == 0x2e ){ 	/*  "."検索  */
			namehead[i] = 0;
			break;
		}
	}
	strcpy( str,"filepath=");
	strcat( str, filepath);
	putmessage();
	strcpy( str, "fnamehead=");
	strcat( str, namehead);
	putmessage();

	strcpy(sccext,"FAX");
	flong =strlen(fname);
	for (i=0;i<3;i++){
		if(fname[flong-3+i]!=sccext[i]&&fname[flong-3+i]!=(sccext[i]+0x20)){
			printf("拡張子が .FAX でありません｡\n");
			return(-1);
		}
	}




	ret = 0;
	return ret;
}

/*********************************************************

					PUT MESSAGE

*********************************************************/

int putmessage ( void ){

printf (" %s \n ",str );
return (0);
}
/*********************************************************

			  			メイン

*********************************************************/


void main( int argc, char *argv[] ){
	fileno=0;
	mode=0x16;
	if(optionset( argc, argv )!=0){
		exit(-1);
	}
	for ( i = 0; i <251 ;i ++ ){
		if ( fname[i] != 0x2a ){ 	/*  "*"検索  */
			flong += 1;
		}
		else {
			wildcard=1;
			break;
		}
	}
	if(wildcard==1){
/*		strcpy(fname,filepath);
		strcat(fname,namehead);
*/
		w_ret=_dos_findfirst((char*)fname,mode,&wildfbuf);
		if(w_ret){
			printf("指定のワイルドカ−ドが見つかりません\n");
			exit(-1);
		}
		else{
			strcpy(wildfname[fileno],wildfbuf.name);
			fileno++;
			for (;;){
				w_ret=_dos_findnext(&wildfbuf);
				if(w_ret){
					printf("指定のワイルドカ−ドを %d 発見しました\n",fileno);
					break;
				}
				strcpy(wildfname[fileno],wildfbuf.name);
				fileno++;
			}
		}
		for(wildct=0;wildct<fileno;wildct++){
			strcpy(lsfname[0],filepath);
			strcat(lsfname[0],wildfname[wildct]);

			ret = 0;
			for ( i = 0; i < 9 ;i ++ ){
				if ( wildfname[wildct][i] == 0x2e ){
					ret = 1;
				}
				else{
					extname[i]=wildfname[wildct][i];
				}
			}
			extname[i+1]=0;
			strcpy(lsfname[1],filepath);
			strcat(lsfname[1],extname);
			strcpy(datname,filepath);
			strcat(datname,extname);
			strcat(datname,".DAT" );




 			if(chgmain()!=0){
				exit(-1);
			}
		}
	}
/*******************************************************************

                       ここからワイルドカ−ドでない場合

*******************************************************************/


	else{
/*		findflag=0;
		namelen=strlen(fname);
		flong=namelen;
		for ( i =namelen-1; i > namelen-4 ;i-- ){
			if ( fname[i] != 0x2e ){
				flong -= 1;
			}
			else {
				findflag=1;
				break;
			}
		}
		if(findflag!=0){
			strncpy(extname,fname,flong);
			extname[flong]=0;
		}
*/

		strcpy(lsfname[0],filepath);
		strcat(lsfname[0],namehead);
		strcat(lsfname[0],".FAX");
		strcpy(lsfname[1],filepath);
		strcat(lsfname[1],namehead);
		strcpy(datname,filepath);
		strcat(datname,namehead);
		strcat(datname,".DAT" );
		if(chgmain()!=0){
			exit(-1);
		}
	}
	exit(0);
}
