xref: /original-bsd/usr.bin/pascal/libpc/PFCLOSE.c (revision 2301fdfb)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static	char sccsid[] = "@(#)PFCLOSE.c	1.3	(Berkeley)	01/21/83";
4 
5 /*
6  * Close a Pascal file deallocating resources as appropriate.
7  */
8 
9 #include "h00vars.h"
10 #include "libpc.h"
11 
12 struct iorec *
13 PFCLOSE(filep, lastuse)
14 	register struct iorec *filep;
15 	bool lastuse;
16 {
17 	if ((filep->funit & FDEF) == 0 && filep->fbuf != NULL) {
18 		/*
19 		 * Have a previous buffer, close associated file.
20 		 */
21 		if (filep->fblk > PREDEF) {
22 			fflush(filep->fbuf);
23 			setbuf(filep->fbuf, NULL);
24 		}
25 		fclose(filep->fbuf);
26 		if (ferror(filep->fbuf)) {
27 			ERROR("%s: Close failed\n", filep->pfname);
28 			return;
29 		}
30 		/*
31 		 * Temporary files are discarded.
32 		 */
33 		if ((filep->funit & TEMP) != 0 && lastuse &&
34 		    unlink(filep->pfname)) {
35 			PERROR("Could not remove ", filep->pfname);
36 			return;
37 		}
38 	}
39 	_actfile[filep->fblk] = FILNIL;
40 	return (filep->fchain);
41 }
42