/* * CALLING SEQUENCE: * when calling from cft77 or f77 use: * binopen(filename,len,unit) * binclose(unit) * binwrite(unit,buf,buf_size) * binread(unit,buf,buf_size) * * when calling from cmf or for on a VAX use: * _binopen(filename,len,unit) * _binclose(unit) * _binwrite(unit,buf,buf_size) * _binread(unit,buf,buf_size) * also: compile cio.c with the -DCMF option * i.e. Define CMF only if compiling on a VAX machine with cmf or for * * In the routines in this file * * filename is of FORTRAN type CHARACTER*x where x can be as large as 32 * This is the name of the file to be opened. * * len is of FORTRAN type INTEGER * This is the length of filename (i.e. 7 characters, 8 characters, etc.) * * unit is of FORTRAN type INTEGER * This is the file descriptor of the open file (opened in binopen) * * buf is of FORTRAN type INTEGER or REAL * This is the array that is to be read/written * * buf_size is of FORTRAN type INTEGER * This is the length of buf in ** BYTES **. * */ /* return endian-ness: returns 1 for littleend (intel, sun, etc) * 0 for bigend (mips, ...) */ #ifdef WIN32 #define ENDIAN ENDIAN #else #define ENDIAN endian_ #endif int ENDIAN() { unsigned int i=1; if ( *((unsigned char*)&i) ) return 1; else return 0; } #ifndef CMF #undef vax #endif /* The character delimiting path names is: */ #ifdef WIN32 #define CSLASH '\\' #else #define SLASH '/' #endif #ifdef vax #include #include struct ftn_string {short len; char * str}; #else #include #endif #ifdef WIN32 #include #include #define open _open #define read _read #define write _write #define close _close #define BINARY | _O_BINARY #else #define BINARY #endif #include #include #include #include #include #ifdef vax int BINOPEN(filename,len,unit) struct ftn_string *filename; #elif cray int BINOPEN(filename,len,unit) char filename[]; #elif WIN32 int BINOPEN(filename,len,unit) char *filename; #else int binopen_(filename,len,unit,len2) char filename[]; #endif int *len, len2; int *unit; { int myunit; char name[256]; int i; #ifdef vax char fname[32]; strncpy(fname,filename->str,filename->len); fname[filename->len] = '\0'; i= 0; while( (i < *len) && isalnum(fname[i])) name[i] = fname[i],i++; #else /* allow almost all characters, such as ':' and directory slashes */ i= 0; while( (i< *len) && filename[i]!=0 && filename[i]!=' ' ) name[i] = filename[i],i++; #endif name[i] = '\0'; /* fprintf(stderr,"**** unit=%d, name at open is '%s'\n", *unit,name ); */ if ((myunit = open(name,O_CREAT | O_RDWR BINARY,(S_IREAD | S_IWRITE))) == -1) { perror(name); exit(2); } /* fprintf(stderr,"*** returning unit %d\n", myunit ); */ *unit= myunit; return myunit; } #ifdef vax void BINCLOSE(unit) #elif cray void BINCLOSE(unit) #elif WIN32 void BINCLOSE(unit) #else void binclose_(unit) #endif int *unit; { close(*unit); } #if vax | cray | WIN32 void BINWRITE(unit,buf,buf_size) #else void binwrite_(unit,buf,buf_size) #endif int *unit; int *buf; int *buf_size; { if (write(*unit,buf,(*buf_size )) != (*buf_size )) { fprintf(stderr,"Error: write unit %d of buffer %p size %d failed\n", *unit, buf, *buf_size ); perror("error writing file"); exit(2); } } #ifdef vax void BINREAD(unit,buf,buf_size) #elif cray void BINREAD(unit,buf,buf_size) #elif WIN32 void BINREAD(unit,buf,buf_size) #else void binread_(unit,buf,buf_size) #endif int *unit; int *buf; int *buf_size; { if (read(*unit,buf,(*buf_size )) != (*buf_size )) { printf("*buf_size= %d\n", *buf_size ); perror("error reading file"); exit(2); } }