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 * unit = Fortran unit number
53 * nunit = UNIX file descriptor associated with 'unit'
54 * name = UNIX file name
55 * mode = 0 : write only - file will be created if it doesn't exist,
56 - otherwise will be rewritten
59 * err = 0 : no error opening file.
60 != 0 : Error opening file
61 * oflag = 0 : no notification if file opened OK (errors are printed)
62 = 1 : file name and unit number printed (and errors)
63 = -1 : no print at all (not even errors)
69 char name
[FORT_FILE_LEN
],
76 char fname
[FORT_FILE_LEN
];
77 extern int errno
; /* I/O error return */
80 printf("Copen: File = %s\nFortran Unit = %d\n", name
, *unit
);
82 /* strip trailing blanks and add null character to name */
83 for (i
= 0; name
[i
] != ' ' && name
[i
] != '\0' && i
< FORT_FILE_LEN
; ++i
)
87 /* if (*mode == 0) WRITE ONLY
88 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_WRONLY));
89 printf ("UNIX File descriptor: %d\n", fd = creat (fname, 0777));
90 else if (*mode == 1) READ ONLY
91 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDONLY));
93 printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDWR));*/
95 if (*mode
== 0) /* WRITE ONLY */
96 fd
= creat(fname
, 0777);
97 else if (*mode
== 1) /* READ ONLY */
98 fd
= open(fname
, O_RDONLY
);
100 fd
= open(fname
, O_RDWR
);
102 printf("UNIX File descriptor: %d\n\n", fd
);
105 if (fd
== -1) { /* error opening file */
107 printf("Error opening %s Error status: %d\n", fname
, errno
);
117 /* ****************************************************************** */
118 /* Move the read/write file pointer
119 fd : Unix file descriptor.
120 bread : Number of bytes to move the pointer.
121 mode : How to move the pointer:
122 = 0 : move the pointer ahead BREAD bytes.
123 < 0 : move the pointer to location BREAD.
124 > 0 : move the pointer to the end + BREAD bytes. (?)
125 iprint : Flag to turn on (iprint = 1) or off (iprint = 0) print.
127 Location 0 [bn_seek(fd,0,-1,0)] puts us just before the first byte,
128 so the next bn_read will get byte 1.
142 how_to_space
= SEEK_CUR
;
144 how_to_space
= SEEK_SET
;
146 how_to_space
= SEEK_END
;
149 i
= lseek(*fd
, offset
, how_to_space
);
151 printf(" lseek return=%d, *mode=%d\n", i
, *mode
);
156 /* ****************************************************************** */
158 * fd = UNIX file descriptor number (NOT a Fortran unit)
159 * buf = area into which to read
160 * nbuf = number of bytes to read from fd
161 * bread = number actually read
162 * ios = error number returned to Fortran:
165 * idiag : if non-zero, error and EOF messages will be printed
179 /* printf ("BNREAD Fd = %d Nbuf = %d\n", *fd, *nbuf); */
180 bytesread
= read(*fd
, buf
, *nbuf
);
181 /* printf ("Bytes %d stat %d\n", bytesread, errno); */
183 if (bytesread
== -1) { /* error reading file */
185 printf("Error reading C unit %d\n", *fd
);
189 } else if (bytesread
== 0) {/* end-of-file on input */
191 printf("End of file on C unit %d\n", *fd
);
194 } else { /* read OK */
197 * printf ("BNREAD - bytes read = %d Buf = %d %d %d\n", bytesread,
198 * buf[0], buf[1], buf[2]);
207 /* ****************************************************************** */
209 * fd = UNIX file descriptor number (NOT a Fortran unit) buf = area from
210 * which to write nbuf = number of bytes to write to fd bwritten = number
211 * actually written err = UNIX error number returned to FORTRAN idiag : if
212 * non-zero, error and EOF messages will be printed
227 * printf ("BNWRIT Fd = %d Nbuf = %d Buf = %d %d %d\n", fd, *nbuf,
228 * buf[0], buf[1], buf[2]);
230 byteswritten
= write(*fd
, buf
, *nbuf
);
231 /* printf ("Bytes %d stat %d\n", byteswritten, errno); */
234 if (byteswritten
== -1) { /* error writing file */
236 printf("Error writing C unit %d\n", *fd
);
241 *bwritten
= byteswritten
;
245 /* ****************************************************************** */
247 Close a C (UNIX?) file descriptor:
248 nunit : (INPUT) : The C (UNIX?) file descriptor to close.
249 iprint : (INPUT) : Print flag ( iprint == 0 : no print on successful close)
250 ( iprint != 0 : Some printout)
251 err : (OUTPUT) : Error flag ( err = 0 : Successful close)
252 ( err = 1 : Error on close)
261 extern int errno
; /* I/O error return */
265 printf("\n *** CCLOSE : Closing file descriptor: NUNIT = %d \n",
268 istat
= close(*nunit
);
271 printf(" *** CCLOSE successful: File descriptor: NUNIT = %d \n",
275 printf("CCLOSE error: %d : File descriptor NUNIT = %d \n",