xref: /titanic_50/usr/src/lib/libast/common/sfio/sfgetm.c (revision a0563a48b6bba0177dc249048ea515ca080c73af)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"sfhdr.h"
23 
24 /*	Read an unsigned long value coded portably for a given range.
25 **
26 **	Written by Kiem-Phong Vo
27 */
28 
29 #if __STD_C
30 Sfulong_t sfgetm(Sfio_t* f, Sfulong_t m)
31 #else
32 Sfulong_t sfgetm(f, m)
33 Sfio_t*		f;
34 Sfulong_t	m;
35 #endif
36 {
37 	Sfulong_t	v;
38 	reg uchar	*s, *ends, c;
39 	reg int		p;
40 	SFMTXDECL(f);
41 
42 	SFMTXENTER(f, (Sfulong_t)(-1));
43 
44 	if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
45 		SFMTXRETURN(f, (Sfulong_t)(-1));
46 
47 	SFLOCK(f,0);
48 
49 	for(v = 0;; )
50 	{	if(SFRPEEK(f,s,p) <= 0)
51 		{	f->flags |= SF_ERROR;
52 			v = (Sfulong_t)(-1);
53 			goto done;
54 		}
55 		for(ends = s+p; s < ends;)
56 		{	c = *s++;
57 			v = (v << SF_BBITS) | SFBVALUE(c);
58 			if((m >>= SF_BBITS) <= 0)
59 			{	f->next = s;
60 				goto done;
61 			}
62 		}
63 		f->next = s;
64 	}
65 done:
66 	SFOPEN(f,0);
67 	SFMTXRETURN(f, v);
68 }
69