xref: /original-bsd/usr.bin/pascal/libpc/IOSYNC.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1979, 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[] = "@(#)IOSYNC.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "h00vars.h"
13 
14 /*
15  * insure that a usable image is in the buffer window
16  */
17 IOSYNC(curfile)
18 
19 	register struct iorec	*curfile;
20 {
21 	char			*limit, *ptr;
22 
23 	if (curfile->funit & FWRITE) {
24 		ERROR("%s: Attempt to read, but open for writing\n",
25 			curfile->pfname);
26 		return;
27 	}
28 	if ((curfile->funit & SYNC) == 0) {
29 		return;
30 	}
31 	if (curfile->funit & EOFF) {
32 		ERROR("%s: Tried to read past end of file\n", curfile->pfname);
33 		return;
34 	}
35 	curfile->funit &= ~SYNC;
36 	if (curfile->funit & SPEOLN) {
37 		curfile->funit &= ~(SPEOLN|EOLN);
38 		curfile->funit |= EOFF;
39 		return;
40 	}
41 	fread(curfile->fileptr, (int)curfile->fsize, 1, curfile->fbuf);
42 	if (ferror(curfile->fbuf)) {
43 		ERROR("%s: Tried to read past end of file\n", curfile->pfname);
44 		return;
45 	}
46 	if (feof(curfile->fbuf)) {
47 		if (curfile->funit & FTEXT) {
48 			*curfile->fileptr = ' ';
49 			if (curfile->funit & EOLN) {
50 				curfile->funit &= ~EOLN;
51 				curfile->funit |= EOFF;
52 				return;
53 			}
54 			curfile->funit |= (SPEOLN|EOLN);
55 			return;
56 		}
57 		curfile->funit |= EOFF;
58 		limit = &curfile->fileptr[curfile->fsize];
59 		for (ptr = curfile->fileptr; ptr < limit; )
60 			*ptr++ = 0;
61 		return;
62 	}
63 	if (curfile->funit & FTEXT) {
64 		if (*curfile->fileptr == '\n') {
65 			curfile->funit |= EOLN;
66 			*curfile->fileptr = ' ';
67 			return;
68 		}
69 		curfile->funit &= ~EOLN;
70 	}
71 }
72