Update the README and comments in some of the NCEP Vtables.
[WPS.git] / ungrib / src / cio.c
blob1a545d6a8a90b0bef78de11a092aa1263fc4a55f
1 /* FILE: cio.c */
2 /* C functions to write bytes to UNIX files - called from FORTRAN */
3 /* c_open
4 bn_read
5 bnwrit
6 c_close */
7 /* bsrfil */
8 /* 870417 */
10 #if defined(CRAY)
12 #define c_open C_OPEN
13 #define c_close C_CLOSE
14 #define bn_read BN_READ
15 #define bn_seek BN_SEEK
17 #endif
19 /* length of the char string from the fortran file is 132, plus one for null terminator */
20 #define FORT_FILE_LEN 133
22 #ifdef _UNDERSCORE
24 #define c_open c_open_
25 #define c_close c_close_
26 #define bn_read bn_read_
27 #define bn_seek bn_seek_
29 #endif
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__
38 #endif
40 #include <stdio.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #ifndef _WIN32
45 # include <unistd.h>
46 # include <sys/ioctl.h>
47 # include <sys/uio.h>
48 #endif
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
57 = 1 : read only
58 = 2 : read/write
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)
65 int
66 c_open(
67 int *unit,
68 int *nunit,
69 char name[FORT_FILE_LEN],
70 int *mode,
71 int *err,
72 int *oflag
75 int fd, i;
76 char fname[FORT_FILE_LEN];
77 extern int errno; /* I/O error return */
79 if (*oflag >= 1)
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)
84 fname[i] = name[i];
85 fname[i] = '\0';
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));
92 else READ/WRITE
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);
99 else /* READ/WRITE */
100 fd = open(fname, O_RDWR);
101 if (*oflag >= 1)
102 printf("UNIX File descriptor: %d\n\n", fd);
104 *err = 0;
105 if (fd == -1) { /* error opening file */
106 if (*oflag >= 0){
107 printf("Error opening %s Error status: %d\n", fname, errno);
108 perror("c_open.c");
110 *err = errno;
113 *nunit = fd;
114 return (0);
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.
131 bn_seek(
132 int *fd,
133 int *bread,
134 int *mode,
135 int *iprint
138 off_t i, offset;
139 int how_to_space;
141 if (*mode == 0)
142 how_to_space = SEEK_CUR;
143 else if (*mode < 0)
144 how_to_space = SEEK_SET;
145 else
146 how_to_space = SEEK_END;
148 offset = *bread;
149 i = lseek(*fd, offset, how_to_space);
150 if (*iprint != 0)
151 printf(" lseek return=%d, *mode=%d\n", i, *mode);
153 return(0);
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:
163 1 = End of File
164 2 = Error in reading
165 * idiag : if non-zero, error and EOF messages will be printed
168 bn_read(
169 int *fd,
170 int buf[],
171 int *nbuf,
172 int *bread,
173 int *ios,
174 int *idiag
177 int bytesread;
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 */
184 if (*idiag != 0)
185 printf("Error reading C unit %d\n", *fd);
186 perror("bn_read.c");
187 *ios = 2;
188 /* *ios = errno; */
189 } else if (bytesread == 0) {/* end-of-file on input */
190 if (*idiag != 0)
191 printf("End of file on C unit %d\n", *fd);
192 *ios = 1;
193 /* *ios = errno; */
194 } else { /* read OK */
197 * printf ("BNREAD - bytes read = %d Buf = %d %d %d\n", bytesread,
198 * buf[0], buf[1], buf[2]);
200 *ios = 0;
203 *bread = bytesread;
204 return(0);
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
215 bnwrit_(
216 int *fd,
217 int buf[],
218 int *nbuf,
219 int *bwritten,
220 int *err,
221 int *idiag
224 int byteswritten;
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); */
233 *err = 0;
234 if (byteswritten == -1) { /* error writing file */
235 if (*idiag != 0)
236 printf("Error writing C unit %d\n", *fd);
237 perror("bnwrit.c");
238 *err = errno;
241 *bwritten = byteswritten;
242 return(0);
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)
255 c_close(
256 int *nunit,
257 int *iprint,
258 int *err
261 extern int errno; /* I/O error return */
262 int istat;
264 if ( *iprint != 0 )
265 printf("\n *** CCLOSE : Closing file descriptor: NUNIT = %d \n",
266 *nunit);
268 istat = close(*nunit);
269 if (istat == 0) {
270 if ( *iprint != 0 )
271 printf(" *** CCLOSE successful: File descriptor: NUNIT = %d \n",
272 *nunit);
274 else
275 printf("CCLOSE error: %d : File descriptor NUNIT = %d \n",
276 istat, *nunit);
278 *err = istat;
279 return(0);