BSD 4_4 release
[unix-history] / usr / src / usr.bin / f77 / libI77 / close.c
CommitLineData
82492b51
KB
1/*-
2 * Copyright (c) 1980 The Regents of the University of California.
3 * All rights reserved.
e798709f 4 *
ad787160
C
5 * This module is believed to contain source code proprietary to AT&T.
6 * Use and redistribution is subject to the Berkeley Software License
7 * Agreement and your Software Agreement with AT&T (Western Electric).
161423a6
RE
8 */
9
82492b51 10#ifndef lint
ad787160 11static char sccsid[] = "@(#)close.c 5.4 (Berkeley) 4/12/91";
82492b51
KB
12#endif /* not lint */
13
161423a6 14/*
5e52dbf7
DL
15 * f_clos(): f77 file close
16 * t_runc(): truncation
17 * f_exit(): I/O library exit routines
e798709f
DW
18 */
19
20#include "fio.h"
21
84c777c9 22static char FROM_OPEN[] = "\2";
331ef099 23static char clse[] = "close";
e798709f
DW
24
25f_clos(a) cllist *a;
26{ unit *b;
2a05aacc
DW
27 int n;
28
e798709f
DW
29 lfname = NULL;
30 elist = NO;
31 external = YES;
32 errflag = a->cerr;
33 lunit = a->cunit;
70307d95 34 if(not_legal(lunit)) return(OK);
84c777c9 35 if(lunit==STDERR && (!a->csta || *a->csta != FROM_OPEN[0]))
3bbbaca0 36 err(errflag,F_ERUNIT,"can't close stderr");
e798709f 37 b= &units[lunit];
70307d95 38 if(!b->ufd) return(OK);
84c777c9 39 if(a->csta && *a->csta != FROM_OPEN[0])
e798709f
DW
40 switch(lcase(*a->csta))
41 {
42 delete:
43 case 'd':
44 fclose(b->ufd);
45 if(b->ufnm) unlink(b->ufnm); /*SYSDEP*/
46 break;
47 default:
48 keep:
49 case 'k':
331ef099 50 if(b->uwrt && (n=t_runc(b,errflag,clse))) return(n);
e798709f
DW
51 fclose(b->ufd);
52 break;
53 }
54 else if(b->uscrtch) goto delete;
55 else goto keep;
56 if(b->ufnm) free(b->ufnm);
57 b->ufnm=NULL;
58 b->ufd=NULL;
59 return(OK);
60}
61
62f_exit()
63{
64 ftnint lu, dofirst = YES;
65 cllist xx;
66 xx.cerr=1;
84c777c9 67 xx.csta=FROM_OPEN;
e798709f
DW
68 for(lu=STDOUT; (dofirst || lu!=STDOUT); lu = ++lu % MXUNIT)
69 {
70 xx.cunit=lu;
71 f_clos(&xx);
72 dofirst = NO;
73 }
74}
75
7784fa72 76t_runc (b, flg, str)
5e52dbf7 77unit *b;
7784fa72 78ioflag flg;
5e52dbf7 79char *str;
e798709f 80{
5e52dbf7 81 long loc;
c4793abe 82
5e52dbf7
DL
83 if (b->uwrt)
84 fflush (b->ufd);
85 if (b->url || !b->useek || !b->ufnm)
86 return (OK); /* don't truncate direct access files, etc. */
87 loc = ftell (b->ufd);
88 if (truncate (b->ufnm, loc) != 0)
7784fa72 89 err (flg, errno, str)
5e52dbf7 90 if (b->uwrt && ! nowreading(b))
7784fa72 91 err (flg, errno, str)
5e52dbf7 92 return (OK);
e798709f 93}