ufmcheck

#!/bin/csh

#

# check FORTRAN unformatted file type.

#

# usage: ufmcheck [ FORTRAN_compiler [ c_compiler] ]

#

# created by Sakagami,H. 94/07/18

# modified by Sakagami,H. 05/12/21 for 8 byte header

#

set cc = gcc

set f77 = gfortran

#set f77 = "gfortran -frecord-marker=8 -fconvert=swap"


if ( $#argv >= 1 ) then

   set f77 = "$1"

   if ( $#argv == 2 ) then

      set cc  = "$2"

   endif

endif


set fsrc = ufmw$$.f90

set fbin = ufmw$$

set csrc = ufmr$$.c

set cbin = ufmr$$

set dat  = ufm$$.dat


cat > $fsrc << EOD

character(80) :: cfile

read( *, '(a80)' ) cfile

ia = 305419896

open( 7, file=trim(cfile), form='unformatted', status='unknown' )

write( 7 ) ia

close( 7 )

stop

end

EOD


cat > $csrc << EOD

#include <stdio.h>


/* void main( int argc, char *argv[] ) */

int main( argc, argv )

int argc;

char *argv[];

{

    int ihn, i, icheck;

    char c[4];

    char head[8], tail[8];

    FILE *fpi;


    fpi = fopen( argv[1], "r" );

    fread( head, 8, 1, fpi );

/* check header length */

    if ( head[4] == 0x12 || head[4] == 0x78 ) {

       ihn = 4;

       c[0] = head[4];

       c[1] = head[5];

       c[2] = head[6];

       c[3] = head[7];

    } else {

       ihn = 8;

       fread( c, 4, 1, fpi );

    }

    fread( tail, ihn, 1, fpi );

    fclose( fpi );


/* check head & tail */

    icheck = 0;

    for( i=0; i<ihn; ++i ) if ( head[i] == tail[i] ) icheck++;

    if ( icheck == ihn ) {

/* check endian */

       if ( c[0] == 0x12 && c[1] == 0x34 && c[2] == 0x56 && c[3] == 0x78 ) {

          printf("FORTRAN unformatted file in this machine is\n'%1d Byte Header', 'Big Endian' and ", ihn);

  if ( head[3] == 0x04 )

    printf("'Byte Boundary'.\n");

  else {

    if ( head[3] == 0x01 ) 

printf("'Word Boundary'.\n");

    else

printf("'Unknown Boundary'.\n");

    }

          }

       else {

          if ( c[3] == 0x12 && c[2] == 0x34 && c[1] == 0x56 && c[0] == 0x78 ) {

             printf("FORTRAN unformatted file in this machine is\n'%1d Byte Header', 'Little Endian' and ", ihn);

    if ( head[0] == 0x04 )

        printf("'Byte Boundary'.\n");

    else {

        if ( head[0] == 0x01 ) 

  printf("'Word Boundary'.\n");

        else

  printf("'Unknown Boundary'.\n");

        }

             }

  else

             printf("*** ERROR: unknown endian ***\n");

  }

       }

    else

       printf("*** ERROR: head mismatched with tail ***\n");


}

EOD


$f77 -o $fbin $fsrc

$cc  -o $cbin $csrc


./$fbin << EOD

$dat

EOD


./$cbin $dat


/bin/rm $fsrc $fbin $csrc $cbin $dat


exit