xref: /onnv-gate/usr/src/lib/libast/common/sfio/_sfputm.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #include	"sfhdr.h"
23*4887Schin 
24*4887Schin /*	Write out an unsigned long value in a portable format.
25*4887Schin **
26*4887Schin **	Written by Kiem-Phong Vo.
27*4887Schin */
28*4887Schin 
29*4887Schin #if __STD_C
30*4887Schin int _sfputm(reg Sfio_t* f, Sfulong_t v, Sfulong_t m)
31*4887Schin #else
32*4887Schin int _sfputm(f,v,m)
33*4887Schin reg Sfio_t*	f;	/* write a portable ulong to this stream */
34*4887Schin Sfulong_t	v;	/* the unsigned value to be written */
35*4887Schin Sfulong_t	m;	/* the max value of the range */
36*4887Schin #endif
37*4887Schin {
38*4887Schin #define N_ARRAY		(2*sizeof(Sfulong_t))
39*4887Schin 	reg uchar	*s, *ps;
40*4887Schin 	reg ssize_t	n, p;
41*4887Schin 	uchar		c[N_ARRAY];
42*4887Schin 
43*4887Schin 	SFMTXSTART(f, -1);
44*4887Schin 
45*4887Schin 	if(v > m || (f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) )
46*4887Schin 		SFMTXRETURN(f, -1);
47*4887Schin 	SFLOCK(f,0);
48*4887Schin 
49*4887Schin 	/* code v as integers in base SF_UBASE */
50*4887Schin 	s = ps = &(c[N_ARRAY-1]);
51*4887Schin 	*s = (uchar)SFBVALUE(v);
52*4887Schin 	while((m >>= SF_BBITS) > 0 )
53*4887Schin 	{	v >>= SF_BBITS;
54*4887Schin 		*--s = (uchar)SFBVALUE(v);
55*4887Schin 	}
56*4887Schin 	n = (ps-s)+1;
57*4887Schin 
58*4887Schin 	if(n > 8 || SFWPEEK(f,ps,p) < n)
59*4887Schin 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
60*4887Schin 	else
61*4887Schin 	{	switch(n)
62*4887Schin 		{
63*4887Schin 		case 8 : *ps++ = *s++;
64*4887Schin 		case 7 : *ps++ = *s++;
65*4887Schin 		case 6 : *ps++ = *s++;
66*4887Schin 		case 5 : *ps++ = *s++;
67*4887Schin 		case 4 : *ps++ = *s++;
68*4887Schin 		case 3 : *ps++ = *s++;
69*4887Schin 		case 2 : *ps++ = *s++;
70*4887Schin 		case 1 : *ps++ = *s++;
71*4887Schin 		}
72*4887Schin 		f->next = ps;
73*4887Schin 	}
74*4887Schin 
75*4887Schin 	SFOPEN(f,0);
76*4887Schin 	SFMTXRETURN(f, (int)n);
77*4887Schin }
78