1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <fcntl.h> |
---|
4 | |
---|
5 | #ifdef _WIN32 |
---|
6 | #include <io.h> |
---|
7 | #endif |
---|
8 | |
---|
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 |
---|
32 | |
---|
33 | /* |
---|
34 | * Fortran-callable function to open/close files |
---|
35 | */ |
---|
36 | int OPEN_FILE (char *filename, char *permissions, int *outfd, int *ierr, |
---|
37 | int strlen1, int strlen2) |
---|
38 | { |
---|
39 | char filename2[1000]; |
---|
40 | char permstring[1000]; |
---|
41 | int permvals; |
---|
42 | |
---|
43 | strncpy(filename2,filename,strlen1); |
---|
44 | filename2[strlen1]='\0'; |
---|
45 | |
---|
46 | strncpy(permstring,permissions,strlen2); |
---|
47 | permstring[strlen2]='\0'; |
---|
48 | |
---|
49 | if (strcmp(permstring,"w") == 0) { |
---|
50 | permvals = O_CREAT|O_WRONLY|O_TRUNC; |
---|
51 | } else { |
---|
52 | permvals = O_RDONLY; |
---|
53 | } |
---|
54 | |
---|
55 | *outfd = open(filename2,permvals,0644); |
---|
56 | if (*outfd == -1) |
---|
57 | { |
---|
58 | fprintf(stderr,"setting ierr to -1, filename: %s\n",filename); |
---|
59 | perror(""); |
---|
60 | *ierr = -1; |
---|
61 | return -1; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | *ierr = 0; |
---|
66 | return 0; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | int WRITE_FILE(int *fd, char *buf, int *ierr, int strlen) |
---|
71 | { |
---|
72 | int nbytes; |
---|
73 | |
---|
74 | nbytes = write(*fd,buf,strlen); |
---|
75 | if (nbytes != strlen) |
---|
76 | { |
---|
77 | *ierr = -1; |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | *ierr = 0; |
---|
82 | } |
---|
83 | return *ierr; |
---|
84 | } |
---|
85 | |
---|
86 | int WRITE_FILE_N(int *fd, char *buf, int *nbytes, int *ierr) |
---|
87 | { |
---|
88 | int bytes_written; |
---|
89 | |
---|
90 | bytes_written = write(*fd,buf,*nbytes); |
---|
91 | if (bytes_written != *nbytes) |
---|
92 | { |
---|
93 | *ierr = -1; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | *ierr = 0; |
---|
98 | } |
---|
99 | return *ierr; |
---|
100 | } |
---|
101 | |
---|
102 | int CLOSE_FILE (int *fd) |
---|
103 | { |
---|
104 | close(*fd); |
---|
105 | return 0; |
---|
106 | } |
---|
107 | |
---|
108 | int FLUSH_FILE (int *fd) |
---|
109 | { |
---|
110 | #ifdef _WIN32 |
---|
111 | _commit(*fd); |
---|
112 | #else |
---|
113 | fsync(*fd); |
---|
114 | #endif |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|