xref: /original-bsd/usr.bin/pascal/libpc/READE.c (revision 1f3a482a)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)READE.c 1.5 06/10/81";
4 
5 #include "h00vars.h"
6 
7 long
8 READE(curfile, name)
9 
10 	register struct iorec	*curfile;
11 	char			*name;
12 {
13 	register short	*sptr;
14 	register int	len;
15 	register int	nextlen;
16 	register int	cnt;
17 	char		*cp;
18 	char		namebuf[NAMSIZ];
19 	int		retval;
20 
21 	if (curfile->funit & FWRITE) {
22 		ERROR("%s: Attempt to read, but open for writing\n",
23 			curfile->pfname);
24 		return;
25 	}
26 	UNSYNC(curfile);
27 	retval = fscanf(curfile->fbuf,
28 	    "%*[ \t\n]%74[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]",
29 	    namebuf);
30 	if (retval == EOF) {
31 		ERROR("%s: Tried to read past end of file\n", curfile->pfname);
32 		return;
33 	}
34 	if (retval == 0)
35 		goto ename;
36 	curfile->funit &= ~EOLN;
37 	curfile->funit |= SYNC;
38 	for (len = 0; len < NAMSIZ && namebuf[len]; len++)
39 		/* void */;
40 	len++;
41 	sptr = (short *)name;
42 	cnt = *sptr++;
43 	cp = name + sizeof (short) + *sptr;
44 	do	{
45 		nextlen = *sptr++;
46 		nextlen = *sptr - nextlen;
47 		if (nextlen == len && RELEQ(len, namebuf, cp)) {
48 			return *((short *) name) - cnt;
49 		}
50 		cp += (int)nextlen;
51 	} while (--cnt);
52 ename:
53 	ERROR("Unknown name \"%s\" found on enumerated type read\n", namebuf);
54 }
55