Add new fields XLAT_C and XLONG_C
[WPS-merge.git] / ungrib / src / cio.c
blob30eee8d619f23453b00d2f2510ffe65a5816135a
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 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
59 = 1 : read only
60 = 2 : read/write
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)
67 int *unit;
68 int *nunit;
69 int *mode;
70 int *err;
71 int *oflag;
72 char name[FORT_FILE_LEN];
74 int fd, i;
75 char fname[FORT_FILE_LEN];
76 extern int errno; /* I/O error return */
78 if (*oflag >= 1)
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)
83 fname[i] = name[i];
84 fname[i] = '\0';
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));
91 else READ/WRITE
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);
98 else /* READ/WRITE */
99 fd = open(fname, O_RDWR);
100 if (*oflag >= 1)
101 printf("UNIX File descriptor: %d\n\n", fd);
103 *err = 0;
104 if (fd == -1) { /* error opening file */
105 if (*oflag >= 0){
106 printf("Error opening %s Error status: %d\n", fname, errno);
107 perror("c_open.c");
109 *err = errno;
112 *nunit = fd;
113 return (0);
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;
135 off_t i, offset;
136 int how_to_space;
138 if (*mode == 0)
139 how_to_space = SEEK_CUR;
140 else if (*mode < 0)
141 how_to_space = SEEK_SET;
142 else
143 how_to_space = SEEK_END;
145 offset = *bread;
146 i = lseek(*fd, offset, how_to_space);
147 if (*iprint != 0)
148 printf(" lseek return=%d, *mode=%d\n", i, *mode);
150 return(0);
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:
162 1 = End of File
163 2 = Error in reading
164 * idiag : if non-zero, error and EOF messages will be printed
167 int *fd, *nbuf, buf[], *bread, *ios, *idiag;
169 int bytesread;
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 */
176 if (*idiag != 0)
177 printf("Error reading C unit %d\n", *fd);
178 perror("bn_read.c");
179 *ios = 2;
180 /* *ios = errno; */
181 } else if (bytesread == 0) {/* end-of-file on input */
182 if (*idiag != 0)
183 printf("End of file on C unit %d\n", *fd);
184 *ios = 1;
185 /* *ios = errno; */
186 } else { /* read OK */
189 * printf ("BNREAD - bytes read = %d Buf = %d %d %d\n", bytesread,
190 * buf[0], buf[1], buf[2]);
192 *ios = 0;
195 *bread = bytesread;
196 return(0);
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
212 int byteswritten;
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); */
221 *err = 0;
222 if (byteswritten == -1) { /* error writing file */
223 if (*idiag != 0)
224 printf("Error writing C unit %d\n", *fd);
225 perror("bnwrit.c");
226 *err = errno;
229 *bwritten = byteswritten;
230 return(0);
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 */
247 int istat;
249 if ( *iprint != 0 )
250 printf("\n *** CCLOSE : Closing file descriptor: NUNIT = %d \n",
251 *nunit);
253 istat = close(*nunit);
254 if (istat == 0) {
255 if ( *iprint != 0 )
256 printf(" *** CCLOSE successful: File descriptor: NUNIT = %d \n",
257 *nunit);
259 else
260 printf("CCLOSE error: %d : File descriptor NUNIT = %d \n",
261 istat, *nunit);
263 *err = istat;
264 return(0);