2 /* C functions to write bytes to UNIX files - called from FORTRAN */
13 #define c_close C_CLOSE
14 #define bn_read BN_READ
15 #define bn_seek BN_SEEK
19 /* length of the char string from the fortran file is 132, plus one for null terminator */
20 #define FORT_FILE_LEN 133
24 #define c_open c_open_
25 #define c_close c_close_
26 #define bn_read bn_read_
27 #define bn_seek bn_seek_
31 #ifdef _DOUBLEUNDERSCORE
33 #define c_open c_open__
34 #define c_close c_close__
35 #define bn_read bn_read__
36 #define bn_seek bn_seek__
43 #include <sys/types.h>
46 # include <sys/ioctl.h>
50 /* ****************************************************************** */
52 c_open(unit
, nunit
, name
, mode
, err
, oflag
)
54 * unit = Fortran unit number
55 * nunit = UNIX file descriptor associated with 'unit'
56 * name = UNIX file name
57 * mode = 0 : write only - file will be created if it doesn't exist,
58 - otherwise will be rewritten
61 * err = 0 : no error opening file.
62 != 0 : Error opening file
63 * oflag = 0 : no notification if file opened OK (errors are printed)
64 = 1 : file name and unit number printed (and errors)
65 = -1 : no print at all (not even errors)
72 char name
[FORT_FILE_LEN
];
75 char fname
[FORT_FILE_LEN
];
76 extern int errno
; /* I/O error return */
79 printf("Copen: File = %s\nFortran Unit = %d\n", name
, *unit
);
81 /* strip trailing blanks and add null character to name */
82 for (i
= 0; name
[i
] != ' ' && name
[i
] != '\0' && i
< FORT_FILE_LEN
; ++i
)
86 /* if (*mode == 0) WRITE ONLY
87 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_WRONLY));
88 printf ("UNIX File descriptor: %d\n", fd = creat (fname, 0777));
89 else if (*mode == 1) READ ONLY
90 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDONLY));
92 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDWR));*/
94 if (*mode
== 0) /* WRITE ONLY */
95 fd
= creat(fname
, 0777);
96 else if (*mode
== 1) /* READ ONLY */
97 fd
= open(fname
, O_RDONLY
);
99 fd
= open(fname
, O_RDWR
);
101 printf("UNIX File descriptor: %d\n\n", fd
);
104 if (fd
== -1) { /* error opening file */
106 printf("Error opening %s Error status: %d\n", fname
, errno
);
116 /* ****************************************************************** */
117 bn_seek(fd
, bread
, mode
, iprint
)
119 /* Move the read/write file pointer
120 fd : Unix file descriptor.
121 bread : Number of bytes to move the pointer.
122 mode : How to move the pointer:
123 = 0 : move the pointer ahead BREAD bytes.
124 < 0 : move the pointer to location BREAD.
125 > 0 : move the pointer to the end + BREAD bytes. (?)
126 iprint : Flag to turn on (iprint = 1) or off (iprint = 0) print.
128 Location 0 [bn_seek(fd,0,-1,0)] puts us just before the first byte,
129 so the next bn_read will get byte 1.
132 int *fd
, *bread
, *mode
, *iprint
;
139 how_to_space
= SEEK_CUR
;
141 how_to_space
= SEEK_SET
;
143 how_to_space
= SEEK_END
;
146 i
= lseek(*fd
, offset
, how_to_space
);
148 printf(" lseek return=%d, *mode=%d\n", i
, *mode
);
153 /* ****************************************************************** */
155 bn_read(fd
, buf
, nbuf
, bread
, ios
, idiag
)
157 * fd = UNIX file descriptor number (NOT a Fortran unit)
158 * buf = area into which to read
159 * nbuf = number of bytes to read from fd
160 * bread = number actually read
161 * ios = error number returned to Fortran:
164 * idiag : if non-zero, error and EOF messages will be printed
167 int *fd
, *nbuf
, buf
[], *bread
, *ios
, *idiag
;
171 /* printf ("BNREAD Fd = %d Nbuf = %d\n", *fd, *nbuf); */
172 bytesread
= read(*fd
, buf
, *nbuf
);
173 /* printf ("Bytes %d stat %d\n", bytesread, errno); */
175 if (bytesread
== -1) { /* error reading file */
177 printf("Error reading C unit %d\n", *fd
);
181 } else if (bytesread
== 0) {/* end-of-file on input */
183 printf("End of file on C unit %d\n", *fd
);
186 } else { /* read OK */
189 * printf ("BNREAD - bytes read = %d Buf = %d %d %d\n", bytesread,
190 * buf[0], buf[1], buf[2]);
199 /* ****************************************************************** */
201 bnwrit_(fd
, buf
, nbuf
, bwritten
, err
, idiag
)
202 int *fd
, *nbuf
, buf
[], *bwritten
, *err
, *idiag
;
205 * fd = UNIX file descriptor number (NOT a Fortran unit) buf = area from
206 * which to write nbuf = number of bytes to write to fd bwritten = number
207 * actually written err = UNIX error number returned to FORTRAN idiag : if
208 * non-zero, error and EOF messages will be printed
215 * printf ("BNWRIT Fd = %d Nbuf = %d Buf = %d %d %d\n", fd, *nbuf,
216 * buf[0], buf[1], buf[2]);
218 byteswritten
= write(*fd
, buf
, *nbuf
);
219 /* printf ("Bytes %d stat %d\n", byteswritten, errno); */
222 if (byteswritten
== -1) { /* error writing file */
224 printf("Error writing C unit %d\n", *fd
);
229 *bwritten
= byteswritten
;
233 /* ****************************************************************** */
235 c_close(nunit
, iprint
, err
)
237 Close a C (UNIX?) file descriptor:
238 nunit : (INPUT) : The C (UNIX?) file descriptor to close.
239 iprint : (INPUT) : Print flag ( iprint == 0 : no print on successful close)
240 ( iprint != 0 : Some printout)
241 err : (OUTPUT) : Error flag ( err = 0 : Successful close)
242 ( err = 1 : Error on close)
244 int *nunit
, *iprint
, *err
;
246 extern int errno
; /* I/O error return */
250 printf("\n *** CCLOSE : Closing file descriptor: NUNIT = %d \n",
253 istat
= close(*nunit
);
256 printf(" *** CCLOSE successful: File descriptor: NUNIT = %d \n",
260 printf("CCLOSE error: %d : File descriptor NUNIT = %d \n",