xref: /original-bsd/usr.bin/f77/libI77/sue.c (revision 5d95f126)
1 /*-
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)sue.c	5.3 (Berkeley) 04/12/91";
10 #endif /* not lint */
11 
12 /*
13  * sequential unformatted external read/write routines
14  */
15 
16 #include "fio.h"
17 
18 extern int reclen;
19 LOCAL long recloc;
20 LOCAL char rsue[] = "read sue";
21 LOCAL char wsue[] = "write sue";
22 
23 s_rsue(a) cilist *a;
24 {
25 	int n;
26 	reading = YES;
27 	if(n=c_sue(a,READ)) return(n);
28 	if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rsue)
29 	recpos = 0;
30 	if(fread(&reclen,sizeof(int),1,cf) == 1) return(OK);
31 	if(feof(cf))
32 	{	curunit->uend = YES;
33 		err(endflag, EOF, rsue)
34 	}
35 	clearerr(cf);
36 	err(errflag, errno, rsue)
37 }
38 
39 s_wsue(a) cilist *a;
40 {
41 	int n;
42 	reading = NO;
43 	if(n=c_sue(a,WRITE)) return(n);
44 	if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wsue)
45 	reclen = 0;
46 	recloc=ftell(cf);
47 	fseek(cf,(long)sizeof(int),1);
48 	curunit->uend = NO;
49 	return(OK);
50 }
51 
52 LOCAL
53 c_sue(a,flg) cilist *a;
54 {	int n;
55 	external = sequential = YES;
56 	formatted = NO;
57 	lfname = NULL;
58 	elist = NO;
59 	errflag = a->cierr;
60 	endflag = a->ciend;
61 	lunit = a->ciunit;
62 	if(not_legal(lunit)) err(errflag,F_ERUNIT,rsue+5)
63 	curunit = &units[lunit];
64 	if(!curunit->ufd && (n=fk_open(flg,SEQ,UNF,(ftnint)lunit)))
65 		err(errflag,n,rsue+5)
66 	cf = curunit->ufd;
67 	elist = YES;
68 	lfname = curunit->ufnm;
69 	if(curunit->ufmt) err(errflag,F_ERNOUIO,rsue+5)
70 	if(curunit->url) err(errflag,F_ERNOSIO,rsue+5)
71 	if(!curunit->useek) err(errflag,F_ERSEEK,rsue+5)
72 	return(OK);
73 }
74 
75 e_wsue()
76 {	long loc;
77 	fwrite(&reclen,sizeof(int),1,cf);
78 	loc=ftell(cf);
79 	fseek(cf,recloc,0);
80 	fwrite(&reclen,sizeof(int),1,cf);
81 	fseek(cf,loc,0);
82 	return(OK);
83 }
84 
85 e_rsue()
86 {
87 	fseek(cf,(long)(reclen-recpos+sizeof(int)),1);
88 	return(OK);
89 }
90