xref: /original-bsd/usr.bin/pascal/libpc/READE.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[] = "@(#)READE.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "h00vars.h"
13 
14 long
15 READE(curfile, name)
16 
17 	register struct iorec	*curfile;
18 	char			*name;
19 {
20 	register short	*sptr;
21 	register int	len;
22 	register int	nextlen;
23 	register int	cnt;
24 	char		*cp;
25 	char		namebuf[NAMSIZ];
26 	int		retval;
27 
28 	if (curfile->funit & FWRITE) {
29 		ERROR("%s: Attempt to read, but open for writing\n",
30 			curfile->pfname);
31 	}
32 	UNSYNC(curfile);
33 	retval = fscanf(curfile->fbuf,
34 	    "%*[ \t\n]%74[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]",
35 	    namebuf);
36 	if (retval == EOF) {
37 		ERROR("%s: Tried to read past end of file\n", curfile->pfname);
38 	}
39 	if (retval == 0)
40 		goto ename;
41 	for (len = 0; len < NAMSIZ && namebuf[len]; len++)
42 		/* void */;
43 	len++;
44 	sptr = (short *)name;
45 	cnt = *sptr++;
46 	cp = name + sizeof (short) + *sptr;
47 	do	{
48 		nextlen = *sptr++;
49 		nextlen = *sptr - nextlen;
50 		if (nextlen == len && RELEQ(len, namebuf, cp)) {
51 			return *((short *) name) - cnt;
52 		}
53 		cp += (int)nextlen;
54 	} while (--cnt);
55 ename:
56 	ERROR("Unknown name \"%s\" found on enumerated type read\n", namebuf);
57 	return 0;
58 }
59