/*
 * include needed system headers
 */
#include <stdio.h>       /* include standard i/o library */
#include <stdlib.h>      /* include standard library */
#include <string.h>      /* include string library */
#include <unistd.h>      /* include unix standard library */
#include <sys/types.h>   /* */
#include <sys/stat.h>    /* */
#include <fcntl.h>       /* */
#include <errno.h>       /* error simbol definitions */
#include <time.h>        /* system time definitions */
#include <math.h>        /* math library */

void readbad_(unsigned short *bad_buf, int *error, int *fd)

{
    int nread;
    int i,j,ii,jj;
    unsigned short word;
    unsigned short skip;
    unsigned short bad_buf_temp[64];

    *error=0;

/*** read the buffer ***/
    nread=read(*fd, bad_buf_temp, 2*64);
    if( nread==0 ) {
	printf("readbad: Unexpected end of file %d at %d \n",*fd,nread);
	*error=1;
	return ;
    }
    if ( nread != 2*64) {
	printf("readbad: Error on reading: nread = %d \n",nread);
	*error=-1;
 	return ; 
    }


/*** correct byte ***/ //endianess...
    for(i=0; i<64; i++) {
	word = bad_buf_temp[i];
	bad_buf_temp[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 );
    }



    for(i=0; i<64; i++) {

	word = bad_buf_temp[i];

	for(j=0; j<16; j++) {
	    
	    jj=15-j;
	    ii=j+i*16;

	    bad_buf[ii] = ( (word >> jj) & 0x0001 );


	    
	    
	}
    }
    

//    for( i=0; i<64; i++) printf("%i %x \n",i,bad_buf_temp[i]);//??? */

//    printf("\n");//??? */

//    for( i=0; i<1024; i++) printf("%i %i \n",i,bad_buf[i]);//??? */












//legge la parola di fine ladder... per saltarla...

/*** read the buffer ***/
    nread=read(*fd, &skip, 2);
    if( nread==0 ) {
	printf("readbad: Unexpected end of file %d at %d \n",*fd,nread);
	*error=1;
	return ;
    }
    if ( nread != 2) {
	printf("readbad: Error on reading: nread = %d \n",nread);
	*error=-1;
 	return ; 
    }




//    for( i=0; i<64; i++) printf("%x \n",bad_buf[i]);//???




/*     *error=0; */

/* /\*** read the buffer ***\/ */
/*     nread=read(*fd, bad_buf, 2*64); */
/*     if( nread==0 ) { */
/* 	printf("readbad: Unexpected end of file %d at %d \n",*fd,nread); */
/* 	*error=1; */
/* 	return ; */
/*     } */
/*     if ( nread != 2*64) { */
/* 	printf("readbad: Error on reading: nread = %d \n",nread); */
/* 	*error=-1; */
/*  	return ;  */
/*     } */


/* /\*** correct byte ***\/ //endianess... */
/*     for(i=0; i<64; i++) { */
/* 	word = bad_buf[i]; */
/* 	bad_buf[i] = ( (word&0x00ff) << 8 ) | ( (word&0xff00) >> 8 ); */
/*     } */

/* //legge la parola di fine ladder... per saltarla... */

/* /\*** read the buffer ***\/ */
/*     nread=read(*fd, &skip, 2); */
/*     if( nread==0 ) { */
/* 	printf("readbad: Unexpected end of file %d at %d \n",*fd,nread); */
/* 	*error=1; */
/* 	return ; */
/*     } */
/*     if ( nread != 2) { */
/* 	printf("readbad: Error on reading: nread = %d \n",nread); */
/* 	*error=-1; */
/*  	return ;  */
/*     } */




/* //    for( i=0; i<64; i++) printf("%x \n",bad_buf[i]);//??? */

    return; 
}
