
#include	<stdio.h>
#include	<fcntl.h>
#include	"p2c.h"

fn_binary(ENV * env,FILE * out,char * buf)
{
ENV new;
int i;
int in;
int cnt,length,comma;
char ch;
int end;
char * var,* len;

	len = var = 0;
	for ( var = buf ; *var != ' ' && *var != '\t' && *var != 0 ; var ++);
	if ( *var == 0 ) {
		var = 0;
		goto next;
	}
	*var = 0;
	var ++;
	for ( ; *var && (*var == ' ' || *var == '\t' ) ; var ++ );
	if ( *var == 0 ) {
		var = 0;
		goto next;
	}
	for ( len = var ;
		*len != ' ' &&
		*len != '\t' &&
		*len != 0 ;
		len ++);
	if ( *len == 0 ) {
		len = 0;
		goto next;
	}
	*len = 0;
	len ++;
	for ( ; *len && (*len == ' ' || *len == '\t' ) ; len ++ );
	if ( *len == 0 ) {
		len = 0;
		goto next;
	}
next:
	new = *env;
	if ( buf[0] == ':' )
		strcpy(new.path,buf);
	else {
		for ( i = strlen(new.path)-1 ;
			i > 0 && new.path[i] != ':' ; i -- );
		if ( new.path[i] == ':' )
			i ++;
		strcpy(&new.path[i],buf);
	}
	in = open(new.path,O_RDONLY);
	if ( in <  0 ) {
		fprintf(stderr, "path : %s\n", new.path);
		perror("read error 10");
		exit(1);
	}
	length = 0;
	if ( var )
		fprintf(out,"unsigned char %s[] = {\n",var);
	else	fprintf(out,"{\n");
	end = 0;
	comma = 0;
	for ( ; ; ) {
		if ( comma )
			fprintf(out,",\n\t");
		else	fprintf(out,"\n\t");
		comma = 0;
		for ( cnt = 0 ; cnt < 8 ; cnt ++ ) {
			if ( read(in,&ch,1) < 1 ) {
				end = 1;
				break;
			}
			if ( comma )
				fprintf(out,",");
			fprintf(out,"0x%02x",0xff&((unsigned int)ch));
			length ++;
			comma = 1;
		}
		if ( end )
			break;
	}
	fprintf(out,"\n};\n");
	if ( len )
		fprintf(out,"int %s = %i;\n",len,length);
	close(in);
	return;
err1:
	perror("write error\n");
	exit(1);
}

