xref: /original-bsd/usr.bin/pascal/libpc/PCLOSE.c (revision 1f3a482a)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)PCLOSE.c 1.4 06/10/81";
4 
5 #include "h00vars.h"
6 
7 PCLOSE(level)
8 
9 	struct iorec		*level;
10 {
11 	register struct iorec	*next;
12 
13 	next = _fchain.fchain;
14 	while(next != FILNIL && next->flev <= level) {
15 		if (next->fbuf != 0) {
16 			if ((next->funit & FDEF) == 0) {
17 				if (next->fblk > PREDEF) {
18 					fflush(next->fbuf);
19 					setbuf(next->fbuf, NULL);
20 				}
21 				fclose(next->fbuf);
22 				if (ferror(next->fbuf)) {
23 					ERROR("%s: Close failed\n",
24 						next->pfname);
25 					return;
26 				}
27 			}
28 			if ((next->funit & TEMP) != 0 &&
29 			    unlink(next->pfname)) {
30 				PERROR("Could not remove ", next->pfname);
31 				return;
32 			}
33 		}
34 		_actfile[next->fblk] = FILNIL;
35 		next = next->fchain;
36 	}
37 	_fchain.fchain = next;
38 }
39