updated top-level README and version_decl for V4.4.2 (#1795)
[WRF.git] / external / io_grib_share / open_file.c
blob673b8ff544cac88dce1f7bfccedb300cf9f95226
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
5 #ifdef _WIN32
6 #include <io.h>
7 #endif
9 #ifndef CRAY
10 # ifdef NOUNDERSCORE
11 # define OPEN_FILE open_file
12 # define CLOSE_FILE close_file
13 # define WRITE_FILE write_file
14 # define WRITE_FILE_N write_file_n
15 # define FLUSH_FILE flush_file
16 # else
17 # ifdef F2CSTYLE
18 # define OPEN_FILE open_file__
19 # define CLOSE_FILE close_file__
20 # define WRITE_FILE write_file__
21 # define WRITE_FILE_N write_file_n__
22 # define FLUSH_FILE flush_file__
23 # else
24 # define OPEN_FILE open_file_
25 # define CLOSE_FILE close_file_
26 # define WRITE_FILE write_file_
27 # define WRITE_FILE_N write_file_n_
28 # define FLUSH_FILE flush_file_
29 # endif
30 # endif
31 #endif
33 /*
34 * Fortran-callable function to open/close files
36 int OPEN_FILE (char *filename, char *permissions, int *outfd, int *ierr,
37 int strlen1, int strlen2)
39 char filename2[1000];
40 char permstring[1000];
41 int permvals;
43 strncpy(filename2,filename,strlen1);
44 filename2[strlen1]='\0';
46 strncpy(permstring,permissions,strlen2);
47 permstring[strlen2]='\0';
49 if (strcmp(permstring,"w") == 0) {
50 permvals = O_CREAT|O_WRONLY|O_TRUNC;
51 } else {
52 permvals = O_RDONLY;
55 *outfd = open(filename2,permvals,0644);
56 if (*outfd == -1)
58 fprintf(stderr,"setting ierr to -1, filename: %s\n",filename);
59 perror("");
60 *ierr = -1;
61 return -1;
63 else
65 *ierr = 0;
66 return 0;
70 int WRITE_FILE(int *fd, char *buf, int *ierr, int strlen)
72 int nbytes;
74 nbytes = write(*fd,buf,strlen);
75 if (nbytes != strlen)
77 *ierr = -1;
79 else
81 *ierr = 0;
83 return *ierr;
86 int WRITE_FILE_N(int *fd, char *buf, int *nbytes, int *ierr)
88 int bytes_written;
90 bytes_written = write(*fd,buf,*nbytes);
91 if (bytes_written != *nbytes)
93 *ierr = -1;
95 else
97 *ierr = 0;
99 return *ierr;
102 int CLOSE_FILE (int *fd)
104 close(*fd);
105 return 0;
108 int FLUSH_FILE (int *fd)
110 #ifdef _WIN32
111 _commit(*fd);
112 #else
113 fsync(*fd);
114 #endif
115 return 0;