fix for prematurely removing temp files
[unix-history] / usr / src / usr.bin / pascal / libpc / PFCLOSE.c
CommitLineData
3989f6f8
KM
1/* Copyright (c) 1982 Regents of the University of California */
2
9e012f32 3static char sccsid[] = "@(#)PFCLOSE.c 1.3 (Berkeley) %G%";
3989f6f8
KM
4
5/*
6 * Close a Pascal file deallocating resources as appropriate.
7 */
8
3989f6f8
KM
9#include "h00vars.h"
10#include "libpc.h"
11
12struct iorec *
9e012f32 13PFCLOSE(filep, lastuse)
3989f6f8 14 register struct iorec *filep;
9e012f32 15 bool lastuse;
3989f6f8 16{
3989f6f8
KM
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 */
9e012f32
KM
33 if ((filep->funit & TEMP) != 0 && lastuse &&
34 unlink(filep->pfname)) {
3989f6f8
KM
35 PERROR("Could not remove ", filep->pfname);
36 return;
37 }
38 }
39 _actfile[filep->fblk] = FILNIL;
40 return (filep->fchain);
41}