open_file.c
References to this file elsewhere.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4
5 #ifndef CRAY
6 # ifdef NOUNDERSCORE
7 # define OPEN_FILE open_file
8 # define CLOSE_FILE close_file
9 # define WRITE_FILE write_file
10 # define WRITE_FILE_N write_file_n
11 # define FLUSH_FILE flush_file
12 # else
13 # ifdef F2CSTYLE
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 # define OPEN_FILE open_file_
21 # define CLOSE_FILE close_file_
22 # define WRITE_FILE write_file_
23 # define WRITE_FILE_N write_file_n_
24 # define FLUSH_FILE flush_file_
25 # endif
26 # endif
27 #endif
28
29 /*
30 * Fortran-callable function to open/close files
31 */
32 int OPEN_FILE (char *filename, char *permissions, int *outfd, int *ierr,
33 int strlen1, int strlen2)
34 {
35 char filename2[1000];
36 char permstring[1000];
37 int permvals;
38
39 strncpy(filename2,filename,strlen1);
40 filename2[strlen1]='\0';
41
42 strncpy(permstring,permissions,strlen2);
43 permstring[strlen2]='\0';
44
45 if (strcmp(permstring,"w") == 0) {
46 permvals = O_CREAT|O_WRONLY|O_TRUNC;
47 } else {
48 permvals = O_RDONLY;
49 }
50
51 *outfd = open(filename2,permvals,0644);
52 if (*outfd == -1)
53 {
54 fprintf(stderr,"setting ierr to -1, filename: %s\n",filename);
55 perror("");
56 *ierr = -1;
57 return -1;
58 }
59 else
60 {
61 *ierr = 0;
62 return 0;
63 }
64 }
65
66 int WRITE_FILE(int *fd, char *buf, int *ierr, int strlen)
67 {
68 int nbytes;
69
70 nbytes = write(*fd,buf,strlen);
71 if (nbytes != strlen)
72 {
73 *ierr = -1;
74 }
75 else
76 {
77 *ierr = 0;
78 }
79 return *ierr;
80 }
81
82 int WRITE_FILE_N(int *fd, char *buf, int *nbytes, int *ierr)
83 {
84 int bytes_written;
85
86 bytes_written = write(*fd,buf,*nbytes);
87 if (bytes_written != *nbytes)
88 {
89 *ierr = -1;
90 }
91 else
92 {
93 *ierr = 0;
94 }
95 return *ierr;
96 }
97
98 int CLOSE_FILE (int *fd)
99 {
100 close(*fd);
101 return 0;
102 }
103
104 int FLUSH_FILE (int *fd)
105 {
106 fsync(*fd);
107 return 0;
108 }
109