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