File: fio.c

package info (click to toggle)
lfc-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,676 kB
  • ctags: 10,779
  • sloc: ansic: 146,136; sh: 13,176; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 861; fortran: 113
file content (508 lines) | stat: -rw-r--r-- 10,465 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 * $Id: fio.c 3172 2010-02-15 08:51:54Z baud $
 */

/*
 * Copyright (C) 1990-2010 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: fio.c,v $ $Revision: 3172 $ $Date: 2010-02-15 09:51:54 +0100 (Mon, 15 Feb 2010) $ CERN/IT/PDP/DM Felix Hassine";
#endif /* not lint */

/*      fortran.c    C callable Fortran I/O                     */

/*
 *  int usf_open(int *unit, char *file, int *append) returns irc
 *  int udf_open(int *unit, char *file, int *lrecl , int *trunc) returns irc
 *  int usf_write(int *unit, char *buf, int *nwrit) returns irc
 *  int udf_write(int *unit, char *buf, int *nrec, int *nwrit) returns irc
 *  int usf_read(int *unit, char *buf, int *nwant) returns irc
 *  int udf_read(int *unit, char *buf, int *nrec, int *nwant) returns irc
 *  int uf_close(int *unit) returns irc
 *  void uf_cread(int *unit, char *buf, int *nrec, int *nwant, int *ngot, int *irc)
 *
 *              unit = logical fortran unit;
 *              file = filename;
 *              filen= filename length, used only by fortran code;
 *              append= open mode "append" for file when append > 0;
 *              irc = error (errno) status if any occured, 0 otherwise;
 *              reclen= record length for files open in DIRECT mode.
 *              nrec= record number to be accessed.
 *
 * WARNING : uf_cread returns the following status values:
 * status = -1	a few bytes remain in the record
 * 	     0  EOR
 *	     2  EOF
 * 	     >=4 Error
 */
	
#define OPEN_MODE 0644
#define MAXFTNLUN 99

#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <serrno.h>

#define SET SEEK_SET
#define CUR SEEK_CUR
#define END SEEK_END
#if defined(_WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif

static int lun2fd[MAXFTNLUN];  /* Fortran logical units to file descr. mapping */

/*
 * Fortran logical unit record length (direct access). -1 for
 * sequential access. Initialized by usf_open(), udf_open()
 */

static int lun2reclen[MAXFTNLUN];
static int tested = 0 ;	/* Test is_usf() done ? */

int usf_open(unit, file,append,trunc)

int 	*unit;
char 	*file;
int 	*append;
int     *trunc;

{	int fd;
	int flags;
	int errno1;

	if (*unit>MAXFTNLUN)
		return (EBADF); 
	if (*unit <1)
		return (EINVAL);
	else {
		if (*append==0) {
			if (*trunc)
				flags= O_RDWR | O_CREAT | O_TRUNC;
			else
				flags= O_RDWR | O_CREAT;
			if ( (fd = open (file, flags , OPEN_MODE))<0 ) {
                                errno1=errno;
                                if  ( (fd = open (file, O_RDONLY, OPEN_MODE) )<0 ) {
                                        if (errno1==EACCES)
                                                return(errno1);
                                        else
                                                return(errno);
                                }
			}
			lun2fd[*unit-1]=fd;
			lun2reclen[*unit-1] = -1;
		
		}
		/*
		 * In append mode we do not take
		 * the value of trunc int account
		 */
		else {
			if( ( fd = open (file , O_RDWR | O_CREAT| O_APPEND , OPEN_MODE) ) <0) {
				return(errno);
			}
			else {	
				lun2fd[*unit-1]=fd;
                                lun2reclen[*unit-1] = -1;/* Not in Direct access */

			}
		}
	}
	return (0);
}

int udf_open(unit, file, lrecl,trunc)

int 	*unit;
char 	*file;
int 	*lrecl;
int	*trunc;

{
	int flags;
	int fd;
	int errno1;
	if ( (*unit>MAXFTNLUN) || (*unit<1) || (*lrecl<0)  || ( (*lrecl) % 8 ) )
		return(EBADF);
	else {
		if (*trunc) {
			flags= O_RDWR | O_CREAT | O_TRUNC;
		}
		else
			flags=  O_RDWR | O_CREAT ;
		if ( ( fd = open (file, flags , OPEN_MODE) )   <0 ) {
                        errno1=errno;
                        if ( (fd = open (file, O_RDONLY , OPEN_MODE)) <0 ) {
                                if ( errno1==EACCES )
                                        return(errno1);
                                else
                                        return(errno);
                        }
		}
		lun2fd[*unit-1]=fd;
		lun2reclen[(*unit) -1]= (*lrecl) ;
	}
	return (0);
}

int usf_write(unit, buf, nwrit)

int	*unit;
char	*buf;
int	*nwrit;

{	int fd,reclen;
	fd=lun2fd[*unit-1];
	reclen=lun2reclen[*unit-1];

	if (reclen != -1) {
		return(ENOENT);
	}
	else {
		if (*nwrit > 0) 
			write( fd, nwrit , sizeof(int) );
		if ( write ( fd , (char *)buf , *nwrit) <0 ) {
			return(errno);
		}
		if (*nwrit >0)
			write( fd, nwrit , sizeof(int) );
		return(0);
	}
}

int udf_write(unit, buf, nrec, nwrit)

int 	*unit;
char 	*buf;
int 	*nrec;
int 	*nwrit;

{	int fd;
	int reclen,i;
	int zero=0;

	fd=lun2fd[*unit-1];
	reclen=lun2reclen[*unit-1];
	if (reclen <=0 ) {
		return(ENOENT);
	}
	else {
		if (*nwrit <= reclen) {
				lseek(fd , (*nrec-1)*reclen , SET );
				if ( write ( fd , (char *)buf , *nwrit) <0 ){
					return(errno);
				}

				else {
					for (i= (*nwrit) +1; i <=reclen; i++) {
						 write ( fd , &zero , 1);
					}
					return(0);
				}
		}
		else { 
			return(EINVAL);
		}
	}
}


/*
 *	 usf_read performs the read for unformatted sequential files.  
 *	 irc is given the value 0 if no error occured,
 *	 and the C errno value otherwise.
 */

	
int usf_read(unit, buf, nwant)

int	*unit;
char	*buf;
int	*nwant;

{	int fd,reclen;
	int c,d;
	int got;

	fd=lun2fd[*unit-1];
	reclen=lun2reclen[*unit-1];

	if (reclen != -1) {
		*nwant=0;
		errno = ENOENT ;
		return(ENOENT);
	}	
	else {
		if ( (read(fd, &c , sizeof (int) ) ) <0 )  {
				*nwant=0;
				return(errno);
		}
		else {
			if (*nwant >=c) {
				*nwant=c;
				if ( (got=read(fd,buf,*nwant) )<0) {
					*nwant=0;
					return(errno);
				}
				else {
					*nwant=got;
				}
				
			}
			else {
				if ( (got=read(fd,buf,*nwant) )<0 ) {
					*nwant=0;
					return(errno);
				}
				else {
					*nwant=got;
				}
				lseek(fd, c-got, CUR);
			}
		}

	/* Checking that the following integer read is equal
	 * to the first one :
	 */

		if ( ( read( fd, &d , sizeof (int) ) <0 ) )   {
			return(errno);
		}
		else {
			if (c != d ) {
				serrno = SEBADFFORM ;
				return(ESPIPE) ; 
			}
				
		}
	return(0);
	}
}


int udf_read(unit, buf, nrec, nwant)

int 	*unit;
char 	*buf;
int 	*nrec;
int 	*nwant;

{	int fd=lun2fd[*unit-1];
	int reclen=lun2reclen[*unit-1];
	int got;

	if ((reclen<=0) ||(*unit>MAXFTNLUN) || (*unit<1) || (*nrec<0) || (*nwant < 0)) {
		*nwant=0;
		if (reclen<=0) {
			errno = ENOENT ;
			return(ENOENT);
		}
		else {
			errno = EINVAL ;
			return(EINVAL) ; 
		}
	}
	else {
		if (*nwant>reclen)  
				*nwant=reclen;
		lseek(fd , reclen*(*nrec-1) , SET ) ;
 		if ( ( got=read(fd, buf , *nwant) ) <0) {
				*nwant=0;
				return(errno);
		}
		else{
				*nwant=got;
				return(0);
		}
	}
}


int uf_close(unit)

int 	*unit;

{	
	if ( close( lun2fd[*unit-1] ) < 0 ) {
		return(errno);
	}
	else {
		lun2reclen[*unit-1] = -1;
		return(0);
	}

}	

/*
 *	 uf_cread replaces the frdc function
 */

void uf_cread(unit, buf, nrec, nwant, ngot, irc)

int 	*unit;
char 	*buf;
int 	*nrec;
int 	*nwant;
int	*ngot;
int 	*irc;

{
	int fd=lun2fd[*unit-1];
	int sequential=0;	 
	int reclen=lun2reclen[*unit-1];
	int len=0;
	int rcode=0;

/*
 *	detecting  wether the file is
 *		Sequentially
 *	or 	Directly
 *	accessed in fortran
 */

	if (*nwant == 0) {
		*ngot = 0;
		*irc= -1 ; /*Still to read */
		return ;
	}
	if (reclen<0) 
		sequential=1; 	/* The file is sequential */

	if (!sequential) { 	/* Direct access */

		if (*nwant > reclen)
			*nwant=reclen;
		lseek(fd, (*nrec -1)*reclen, SET);
		rcode=read(fd,buf, (*nwant) );
		if (rcode<0) {
			(*irc)=5; /* Error */
			(*ngot)=0;
		}
		else {
			if ( (rcode >=0 ) && (rcode < *nwant) )
				(*irc)=2; /* EOF reached */
			else if ( (rcode == *nwant) && (*nwant==reclen) )
				(*irc)=0; /* EOR reached */
			else if (  (rcode == *nwant) && (*nwant < reclen) )
				(*irc)= -1; /* There is still bytes to read */
			(*ngot)=rcode;
		}
	}
	else {
		/* Sequential access 
		 */
		int rrc =0 ;
		if (!tested) {
		    if ( (rrc = is_usf(fd)) == 0 || rrc == 3 ) {
			*irc = SEBADFFORM  ;
			*ngot = 0 ;
			return ;
		    }
		    tested ++ ;
		}
		rcode=read(fd,&len, sizeof(int) );
		if (rcode <0) {
			(*irc)=5; /* Error */
			(*ngot)=0;
		}
		else if (rcode==0){
			(*irc)=2; /* EOF reached */
			(*ngot)=0;
		}
		else {
			if ( len <*nwant ) { *ngot=len;
					     *nwant=len;
			}
			if ( (rcode=read(fd, buf, *nwant) ) <0) {
					*irc=5; /* Error */
					*ngot=0;
			}
			else {
					*ngot=rcode;
					if (rcode == 0)
						*irc=2 ; /* EOF */
					else if (rcode < *nwant) 
						*irc=2 ; /* EOF */
					else if ( (rcode== *nwant) && (len > *nwant) )
						(*irc)= -1 ; /* Still to read */
					else if  ((rcode == *nwant) && (len == *nwant) )
						(*irc)=0 ; /* EOR */ 
				
				/* Skipping the end of record if 
				 * necessary, and also the length 
				 * integer.
				 */
					lseek(fd,len-(*ngot)+sizeof(int), CUR);
			}
		}
	}
}

/*
 * is the disk file an unformatted sequential file ?
 * returns -1 on failure,
 *         0  if not sequential file
 *         1  if file may be  unformatted sequential
 *         2  file is empty
 *         3  first record is null
 * It leaves the file pointer at the place it was.
 *
 */
int is_usf( fd )
int fd ;
{
        int len,llen ;
        int rcode,rc ;
	int curr ;

	curr = lseek(fd,0,CUR) ;
	if (curr > 0)
		lseek(fd,0, SET) ;
	if (curr < 0)
		return -1 ;
        if ( (rcode = read(fd,&len,sizeof(int))) < 0 )
                return -1 ;
        if (rcode == 0 ) {
                lseek(fd,curr,SET) ;
                return 2 ;
        }
        if ( len < 0 ) {
                lseek(fd,curr,SET) ;
                return 0 ;
        }
        if (len == 0){
                lseek(fd,curr,SET) ;
                return 3 ;
        }
        else {
                rcode=lseek(fd,len, CUR) ;
                if (rcode < 0) {
                        lseek(fd,curr,SET) ;
                        return 0 ;
                }
                rc=read(fd,&llen,sizeof(int));
                if (rc < sizeof(int)) {
                        /* We need at least 1 record to decide
                         * it is sequential
                         */
                        lseek(fd,curr,SET) ;
                        return 0 ;
                }
                if (llen != len) {
                        lseek(fd,curr,SET) ;
                        return 0 ;
                }
                else {
                        lseek(fd,curr,SET) ;
                        return 1 ;
                }
        }
}