1 /* cio.c $Revision: 7.7 $ */
2 /******************************************************************************/
3 /*                                                                            */
4 /*                               A I X - I / O                                */
5 /*                                                                            */
6 /*  The fast I/O calls the following C-langue primitives:                     */
7 /*  open, close, read, write, lseek, remove and fsync                         */
8 /*  This file includes the FORTRAN to C-language interfaces.                  */
9 
10 #include <fcntl.h>
11 #ifndef _WIN32_
12 #include <unistd.h>
13 #else
14 #include <windows.h>
15 #define open  _lopen
16 #define close _lclose
17 #define read  _lread
18 #define write _lwrite
19 #define lseek _llseek
20 #endif
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include "molcastype.h"
25 
26 #define MIN(x,y) (x<y? x : y)
27 /*--- c_open -----------------------------------------------------------------*/
28 
29 #ifdef _CAPITALS_
30 #define c_open C_OPEN
31 #else
32 #ifndef ADD_
33 #define c_open c_open_
34 #endif
35 #endif
36 
c_open(Path)37 INT c_open(Path)
38  char *Path;
39 
40 {
41 
42  INT rc;
43  INT oFlag;
44  INT oMode;
45 #ifdef _CRAY_C90_
46  char fn[256];
47  oFlag=O_CREAT|O_RDWR;
48  (void)strcpy(fn,Path);
49  rc = open(fn,oFlag,0644);
50 #else
51 #ifndef _WIN32_
52  oFlag=O_CREAT|O_RDWR;
53  oMode=S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR;
54  rc = open(Path,oFlag,oMode);
55 #else
56  oFlag=OF_READWRITE;
57  rc=open(Path,oFlag);
58 #endif
59 #endif
60  if(rc<0) {
61    oFlag=O_RDONLY;
62 #ifdef _CRAY_C90_
63    rc = open(fn,oFlag);
64 #else
65 #ifndef _WIN32_
66    rc = open(Path,oFlag);
67 #else
68    oFlag=OF_READ;
69    rc=open(Path,oFlag);
70 #endif
71 #endif
72  }
73 
74  return rc;
75 
76 }
77 /*--- c_open_w -----------------------------------------------------------------*/
78 
79 #ifdef _CAPITALS_
80 #define c_openw C_OPENW
81 #else
82 #ifndef ADD_
83 #define c_openw c_openw_
84 #endif
85 #endif
86 
c_openw(Path)87 INT c_openw(Path)
88  char *Path;
89 
90 {
91 
92  INT rc;
93  INT oFlag;
94  INT oMode;
95 #ifdef _CRAY_C90_
96  char fn[256];
97  oFlag=O_CREAT|O_RDWR|O_TRUNC;
98  (void)strcpy(fn,Path);
99  rc = open(fn,oFlag,0644);
100 #else
101 #ifndef _WIN32_
102  oFlag=O_CREAT|O_RDWR|O_TRUNC;
103  oMode=S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR;
104  rc = open(Path,oFlag,oMode);
105 #else
106  oFlag=OF_READWRITE;
107  rc=open(Path,oFlag);
108 #endif
109 #endif
110  return rc;
111 
112 }
113 
114 /*--- c_close ----------------------------------------------------------------*/
115 #ifdef _CAPITALS_
116 #define c_close C_CLOSE
117 #else
118 #ifndef ADD_
119 #define c_close c_close_
120 #endif
121 #endif
122 
c_close(FileDescriptor)123 INT c_close(FileDescriptor)
124  INT *FileDescriptor;
125 
126 {
127  INT rc;
128  rc = close(*FileDescriptor);
129  return rc;
130 }
131 
132 /*--- c_read -----------------------------------------------------------------*/
133 
134 #ifdef _CAPITALS_
135 #define c_read C_READ
136 #else
137 #ifndef ADD_
138 #define c_read c_read_
139 #endif
140 #endif
141 
c_read(FileDescriptor,Buffer,nBytes)142 INT c_read(FileDescriptor,Buffer,nBytes)
143  INT *FileDescriptor;
144  char *Buffer;
145  INT *nBytes;
146 
147 {
148  INT rc=0;
149  INT bfrblk=1024*1024;
150  INT i=0;
151  INT j;
152  INT remains;
153  INT readlength;
154  remains=*nBytes;
155  while (remains > 0){
156       readlength = MIN(bfrblk,remains);
157       rc = (INT)read(*FileDescriptor,(void *)(Buffer+i),(size_t)(readlength));
158       if ( rc == readlength ) { i = i+readlength; rc = i; remains = remains - bfrblk;}
159       else { rc = 0; return rc ;}
160  }
161  return rc;
162 }
163 
164 /*--- c_write ----------------------------------------------------------------*/
165 
166 #ifdef _CAPITALS_
167 #define c_write C_WRITE
168 #else
169 #ifndef ADD_
170 #define c_write c_write_
171 #endif
172 #endif
173 
c_write(FileDescriptor,Buffer,nBytes)174 INT c_write(FileDescriptor,Buffer,nBytes)
175  INT *FileDescriptor;
176  char *Buffer;
177  INT *nBytes;
178 
179 {
180  INT rc=0;
181  INT bfrblk=1024*1024;
182  INT i=0;
183  INT remains;
184  INT writelength;
185  remains=*nBytes;
186  while (remains > 0){
187       writelength = MIN(bfrblk,remains);
188       rc = (INT)write(*FileDescriptor,(void *)(Buffer+i),(size_t)(writelength));
189       if ( rc == writelength ) { i = i+writelength; rc = i; remains = remains - bfrblk;}
190       else { rc = 0; return rc ;}
191  }
192  return rc;
193 }
194 
195 /*--- c_lseek ----------------------------------------------------------------*/
196 
197 #ifdef _CAPITALS_
198 #define c_lseek C_LSEEK
199 #else
200 #ifndef ADD_
201 #define c_lseek c_lseek_
202 #endif
203 #endif
204 
c_lseek(FileDescriptor,Offset)205 INT c_lseek(FileDescriptor,Offset)
206  INT *FileDescriptor;
207  INT *Offset;
208 
209 {
210 #ifdef _WIN32_
211 typedef long off_t;
212 #endif
213  INT rc;
214  rc = (INT)lseek(*FileDescriptor,(off_t)(*Offset),SEEK_SET);
215  return rc;
216 }
217 
218 /*--- c_remove ---------------------------------------------------------------*/
219 
220 #ifdef _CAPITALS_
221 #define c_remove C_REMOVE
222 #else
223 #ifndef ADD_
224 #define c_remove c_remove_
225 #endif
226 #endif
227 
c_remove(FileName)228 INT c_remove(FileName)
229  char *FileName;
230 
231 {
232  INT rc;
233 #ifdef _CAPITALS_
234  char fn[256];
235 #endif
236 
237 #ifdef _CAPITALS_
238  (void)strcpy(fn,FileName);
239  rc = remove(fn);
240 #else
241 #ifndef _WIN32_
242  rc = remove(FileName);
243 #else
244  rc = DeleteFile(FileName);
245 #endif
246 #endif
247  return rc;
248 }
249 
250 /*--- c_fsync ----------------------------------------------------------------*/
251 
252 #ifdef _CAPITALS_
253 #define c_fsync C_FSYNC
254 #else
255 #ifndef ADD_
256 #define c_fsync c_fsync_
257 #endif
258 #endif
259 
c_fsync(FileDescriptor)260 INT c_fsync(FileDescriptor)
261  INT *FileDescriptor;
262 
263 {
264  INT rc;
265 #ifndef _WIN32_
266  rc = fsync(*FileDescriptor);
267 #else
268  rc=0;
269 #endif
270  return rc;
271 }
272 
273 /*--- c_copy ----------------------------------------------------------------*/
274 
275 #ifdef _CAPITALS_
276 #define c_copy C_COPY
277 #else
278 #ifndef ADD_
279 #define c_copy c_copy_
280 #endif
281 #endif
282 
c_copy(FileDescriptor1,FileDescriptor2)283 INT c_copy(FileDescriptor1, FileDescriptor2)
284  INT *FileDescriptor1, *FileDescriptor2;
285 {
286  INT rc;
287  char *Buffer;
288  struct stat stat;
289  size_t rce;
290 
291  rc=fstat(*FileDescriptor1, &stat);
292 
293  rce=stat.st_size;
294  Buffer=(char*) malloc(sizeof(char)*(rce+1));
295       rc = (INT)read(*FileDescriptor1,(void *)(Buffer),(size_t)(rce));
296       rc = (INT)write(*FileDescriptor2,(void *)(Buffer),(size_t)(rce));
297  free(Buffer);
298  return rc;
299 }
300 /* $Id$ */
301