1 | /* FILE: cio.c */ |
---|
2 | /* C functions to write bytes to UNIX files - called from FORTRAN */ |
---|
3 | /* copen |
---|
4 | bnread |
---|
5 | bnwrit |
---|
6 | cclose */ |
---|
7 | /* bsrfil */ |
---|
8 | /* 870417 */ |
---|
9 | |
---|
10 | #if defined(CRAY) |
---|
11 | |
---|
12 | #define copen COPEN |
---|
13 | #define cclose CCLOSE |
---|
14 | #define bnread BNREAD |
---|
15 | #define bnseek BNSEEK |
---|
16 | |
---|
17 | #endif |
---|
18 | |
---|
19 | /* length of the char string from the fortran file is 132, plus one for null terminator */ |
---|
20 | #define FORT_FILE_LEN 133 |
---|
21 | |
---|
22 | #if defined (SGI) || defined (SOLARIS) || defined (SUN) || defined (DEC) || defined (ALPHA) || defined (_UNDERSCORE) || defined (LINUX) || defined (LINUXG95) |
---|
23 | |
---|
24 | #define copen copen_ |
---|
25 | #define cclose cclose_ |
---|
26 | #define bnread bnread_ |
---|
27 | #define bnseek bnseek_ |
---|
28 | |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdio.h> |
---|
32 | #include <fcntl.h> |
---|
33 | #include <errno.h> |
---|
34 | #include <sys/types.h> |
---|
35 | #include <unistd.h> |
---|
36 | #include <sys/ioctl.h> |
---|
37 | #include <sys/uio.h> |
---|
38 | |
---|
39 | /* ****************************************************************** */ |
---|
40 | |
---|
41 | copen(unit, nunit, name, mode, err, oflag) |
---|
42 | /* |
---|
43 | * unit = Fortran unit number |
---|
44 | * nunit = UNIX file descriptor associated with 'unit' |
---|
45 | * name = UNIX file name |
---|
46 | * mode = 0 : write only - file will be created if it doesn't exist, |
---|
47 | - otherwise will be rewritten |
---|
48 | = 1 : read only |
---|
49 | = 2 : read/write |
---|
50 | * err = 0 : no error opening file. |
---|
51 | != 0 : Error opening file |
---|
52 | * oflag = 0 : no notification if file opened OK (errors are printed) |
---|
53 | = 1 : file name and unit number printed (and errors) |
---|
54 | = -1 : no print at all (not even errors) |
---|
55 | */ |
---|
56 | int *unit; |
---|
57 | int *nunit; |
---|
58 | int *mode; |
---|
59 | int *err; |
---|
60 | int *oflag; |
---|
61 | char name[FORT_FILE_LEN]; |
---|
62 | { |
---|
63 | int fd, i; |
---|
64 | char fname[FORT_FILE_LEN]; |
---|
65 | extern int errno; /* I/O error return */ |
---|
66 | |
---|
67 | if (*oflag >= 1) |
---|
68 | printf("Copen: File = %s\nFortran Unit = %d\n", name, *unit); |
---|
69 | |
---|
70 | /* strip trailing blanks and add null character to name */ |
---|
71 | for (i = 0; name[i] != ' ' && name[i] != '\0' && i < FORT_FILE_LEN; ++i) |
---|
72 | fname[i] = name[i]; |
---|
73 | fname[i] = '\0'; |
---|
74 | |
---|
75 | /* if (*mode == 0) WRITE ONLY |
---|
76 | printf ("UNIX File descriptor: %d\n", fd = open (fname, O_WRONLY)); |
---|
77 | printf ("UNIX File descriptor: %d\n", fd = creat (fname, 0777)); |
---|
78 | else if (*mode == 1) READ ONLY |
---|
79 | printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDONLY)); |
---|
80 | else READ/WRITE |
---|
81 | printf ("UNIX File descriptor: %d\n", fd = open (fname, O_RDWR));*/ |
---|
82 | |
---|
83 | if (*mode == 0) /* WRITE ONLY */ |
---|
84 | fd = creat(fname, 0777); |
---|
85 | else if (*mode == 1) /* READ ONLY */ |
---|
86 | fd = open(fname, O_RDONLY); |
---|
87 | else /* READ/WRITE */ |
---|
88 | fd = open(fname, O_RDWR); |
---|
89 | if (*oflag >= 1) |
---|
90 | printf("UNIX File descriptor: %d\n\n", fd); |
---|
91 | |
---|
92 | *err = 0; |
---|
93 | if (fd == -1) { /* error opening file */ |
---|
94 | if (*oflag >= 0){ |
---|
95 | printf("Error opening %s Error status: %d\n", fname, errno); |
---|
96 | perror("copen.c"); |
---|
97 | }; |
---|
98 | *err = errno; |
---|
99 | }; |
---|
100 | |
---|
101 | *nunit = fd; |
---|
102 | return (0); |
---|
103 | } |
---|
104 | |
---|
105 | /* ****************************************************************** */ |
---|
106 | bnseek(fd, bread, mode, iprint) |
---|
107 | |
---|
108 | /* Move the read/write file pointer |
---|
109 | fd : Unix file descriptor. |
---|
110 | bread : Number of bytes to move the pointer. |
---|
111 | mode : How to move the pointer: |
---|
112 | = 0 : move the pointer ahead BREAD bytes. |
---|
113 | < 0 : move the pointer to location BREAD. |
---|
114 | > 0 : move the pointer to the end + BREAD bytes. (?) |
---|
115 | iprint : Flag to turn on (iprint = 1) or off (iprint = 0) print. |
---|
116 | |
---|
117 | Location 0 [bnseek(fd,0,-1,0)] puts us just before the first byte, |
---|
118 | so the next bnread will get byte 1. |
---|
119 | */ |
---|
120 | |
---|
121 | int *fd, *bread, *mode, *iprint; |
---|
122 | |
---|
123 | { |
---|
124 | off_t i, offset; |
---|
125 | int how_to_space; |
---|
126 | |
---|
127 | if (*mode == 0) |
---|
128 | how_to_space = SEEK_CUR; |
---|
129 | else if (*mode < 0) |
---|
130 | how_to_space = SEEK_SET; |
---|
131 | else |
---|
132 | how_to_space = SEEK_END; |
---|
133 | |
---|
134 | offset = *bread; |
---|
135 | i = lseek(*fd, offset, how_to_space); |
---|
136 | if (*iprint != 0) |
---|
137 | printf(" lseek return=%d, *mode=%d\n", i, *mode); |
---|
138 | |
---|
139 | return(0); |
---|
140 | } |
---|
141 | |
---|
142 | /* ****************************************************************** */ |
---|
143 | |
---|
144 | bnread(fd, buf, nbuf, bread, ios, idiag) |
---|
145 | /* |
---|
146 | * fd = UNIX file descriptor number (NOT a Fortran unit) |
---|
147 | * buf = area into which to read |
---|
148 | * nbuf = number of bytes to read from fd |
---|
149 | * bread = number actually read |
---|
150 | * ios = error number returned to Fortran: |
---|
151 | 1 = End of File |
---|
152 | 2 = Error in reading |
---|
153 | * idiag : if non-zero, error and EOF messages will be printed |
---|
154 | */ |
---|
155 | |
---|
156 | int *fd, *nbuf, buf[], *bread, *ios, *idiag; |
---|
157 | { |
---|
158 | int bytesread; |
---|
159 | |
---|
160 | /* printf ("BNREAD Fd = %d Nbuf = %d\n", *fd, *nbuf); */ |
---|
161 | bytesread = read(*fd, buf, *nbuf); |
---|
162 | /* printf ("Bytes %d stat %d\n", bytesread, errno); */ |
---|
163 | |
---|
164 | if (bytesread == -1) { /* error reading file */ |
---|
165 | if (*idiag != 0) |
---|
166 | printf("Error reading C unit %d\n", *fd); |
---|
167 | perror("bnread.c"); |
---|
168 | *ios = 2; |
---|
169 | /* *ios = errno; */ |
---|
170 | } else if (bytesread == 0) {/* end-of-file on input */ |
---|
171 | if (*idiag != 0) |
---|
172 | printf("End of file on C unit %d\n", *fd); |
---|
173 | *ios = 1; |
---|
174 | /* *ios = errno; */ |
---|
175 | } else { /* read OK */ |
---|
176 | |
---|
177 | /* |
---|
178 | * printf ("BNREAD - bytes read = %d Buf = %d %d %d\n", bytesread, |
---|
179 | * buf[0], buf[1], buf[2]); |
---|
180 | */ |
---|
181 | *ios = 0; |
---|
182 | }; |
---|
183 | |
---|
184 | *bread = bytesread; |
---|
185 | return(0); |
---|
186 | } |
---|
187 | |
---|
188 | /* ****************************************************************** */ |
---|
189 | |
---|
190 | bnwrit_(fd, buf, nbuf, bwritten, err, idiag) |
---|
191 | int *fd, *nbuf, buf[], *bwritten, *err, *idiag; |
---|
192 | |
---|
193 | /* |
---|
194 | * fd = UNIX file descriptor number (NOT a Fortran unit) buf = area from |
---|
195 | * which to write nbuf = number of bytes to write to fd bwritten = number |
---|
196 | * actually written err = UNIX error number returned to FORTRAN idiag : if |
---|
197 | * non-zero, error and EOF messages will be printed |
---|
198 | */ |
---|
199 | |
---|
200 | { |
---|
201 | int byteswritten; |
---|
202 | |
---|
203 | /* |
---|
204 | * printf ("BNWRIT Fd = %d Nbuf = %d Buf = %d %d %d\n", fd, *nbuf, |
---|
205 | * buf[0], buf[1], buf[2]); |
---|
206 | */ |
---|
207 | byteswritten = write(*fd, buf, *nbuf); |
---|
208 | /* printf ("Bytes %d stat %d\n", byteswritten, errno); */ |
---|
209 | |
---|
210 | *err = 0; |
---|
211 | if (byteswritten == -1) { /* error writing file */ |
---|
212 | if (*idiag != 0) |
---|
213 | printf("Error writing C unit %d\n", *fd); |
---|
214 | perror("bnwrit.c"); |
---|
215 | *err = errno; |
---|
216 | }; |
---|
217 | |
---|
218 | *bwritten = byteswritten; |
---|
219 | return(0); |
---|
220 | } |
---|
221 | |
---|
222 | /* ****************************************************************** */ |
---|
223 | |
---|
224 | cclose(nunit, iprint, err) |
---|
225 | /* |
---|
226 | Close a C (UNIX?) file descriptor: |
---|
227 | nunit : (INPUT) : The C (UNIX?) file descriptor to close. |
---|
228 | iprint : (INPUT) : Print flag ( iprint == 0 : no print on successful close) |
---|
229 | ( iprint != 0 : Some printout) |
---|
230 | err : (OUTPUT) : Error flag ( err = 0 : Successful close) |
---|
231 | ( err = 1 : Error on close) |
---|
232 | */ |
---|
233 | int *nunit, *iprint, *err; |
---|
234 | { |
---|
235 | extern int errno; /* I/O error return */ |
---|
236 | int istat; |
---|
237 | |
---|
238 | if ( *iprint != 0 ) |
---|
239 | printf("\n *** CCLOSE : Closing file descriptor: NUNIT = %d \n", |
---|
240 | *nunit); |
---|
241 | |
---|
242 | istat = close(*nunit); |
---|
243 | if (istat == 0) { |
---|
244 | if ( *iprint != 0 ) |
---|
245 | printf(" *** CCLOSE successful: File descriptor: NUNIT = %d \n", |
---|
246 | *nunit); |
---|
247 | } |
---|
248 | else |
---|
249 | printf("CCLOSE error: %d : File descriptor NUNIT = %d \n", |
---|
250 | istat, *nunit); |
---|
251 | |
---|
252 | *err = istat; |
---|
253 | return(0); |
---|
254 | } |
---|