Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / external / io_grib_share / open_file.c
blobb629bddfd8c29ff7d6fa24a47089aec4984a9633
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #ifdef _WIN32
5 #include <io.h>
6 #include <strings.h>
7 #else
8 #include <unistd.h>
9 #include <string.h>
10 #endif
12 #ifndef CRAY
13 # ifdef NOUNDERSCORE
14 # define OPEN_FILE open_file
15 # define CLOSE_FILE close_file
16 # define WRITE_FILE write_file
17 # define WRITE_FILE_N write_file_n
18 # define FLUSH_FILE flush_file
19 # else
20 # ifdef F2CSTYLE
21 # define OPEN_FILE open_file__
22 # define CLOSE_FILE close_file__
23 # define WRITE_FILE write_file__
24 # define WRITE_FILE_N write_file_n__
25 # define FLUSH_FILE flush_file__
26 # else
27 # define OPEN_FILE open_file_
28 # define CLOSE_FILE close_file_
29 # define WRITE_FILE write_file_
30 # define WRITE_FILE_N write_file_n_
31 # define FLUSH_FILE flush_file_
32 # endif
33 # endif
34 #endif
36 /*
37 * Fortran-callable function to open/close files
39 int OPEN_FILE (char *filename, char *permissions, int *outfd, int *ierr,
40 int strlen1, int strlen2)
42 char filename2[1000];
43 char permstring[1000];
44 int permvals;
46 strncpy(filename2,filename,strlen1);
47 filename2[strlen1]='\0';
49 strncpy(permstring,permissions,strlen2);
50 permstring[strlen2]='\0';
52 if (strcmp(permstring,"w") == 0) {
53 permvals = O_CREAT|O_WRONLY|O_TRUNC;
54 } else {
55 permvals = O_RDONLY;
58 *outfd = open(filename2,permvals,0644);
59 if (*outfd == -1)
61 fprintf(stderr,"setting ierr to -1, filename: %s\n",filename);
62 perror("");
63 *ierr = -1;
64 return -1;
66 else
68 *ierr = 0;
69 return 0;
73 int WRITE_FILE(int *fd, char *buf, int *ierr, int strlen)
75 int nbytes;
77 nbytes = write(*fd,buf,strlen);
78 if (nbytes != strlen)
80 *ierr = -1;
82 else
84 *ierr = 0;
86 return *ierr;
89 int WRITE_FILE_N(int *fd, char *buf, int *nbytes, int *ierr)
91 int bytes_written;
93 bytes_written = write(*fd,buf,*nbytes);
94 if (bytes_written != *nbytes)
96 *ierr = -1;
98 else
100 *ierr = 0;
102 return *ierr;
105 int CLOSE_FILE (int *fd)
107 close(*fd);
108 return 0;
111 int FLUSH_FILE (int *fd)
113 #ifdef _WIN32
114 _commit(*fd);
115 #else
116 fsync(*fd);
117 #endif
118 return 0;