xref: /onnv-gate/usr/src/lib/libast/common/sfio/_sfputu.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #include	"sfhdr.h"
234887Schin 
244887Schin /*	Write out an unsigned long value in a portable format.
254887Schin **
264887Schin **	Written by Kiem-Phong Vo.
274887Schin */
284887Schin 
294887Schin #if __STD_C
_sfputu(Sfio_t * f,Sfulong_t v)308462SApril.Chin@Sun.COM int _sfputu(Sfio_t* f, Sfulong_t v)
314887Schin #else
324887Schin int _sfputu(f,v)
338462SApril.Chin@Sun.COM Sfio_t*		f;	/* write a portable ulong to this stream */
344887Schin Sfulong_t	v;	/* the unsigned value to be written */
354887Schin #endif
364887Schin {
374887Schin #define N_ARRAY		(2*sizeof(Sfulong_t))
384887Schin 	reg uchar	*s, *ps;
394887Schin 	reg ssize_t	n, p;
404887Schin 	uchar		c[N_ARRAY];
418462SApril.Chin@Sun.COM 	SFMTXDECL(f);
424887Schin 
438462SApril.Chin@Sun.COM 	SFMTXENTER(f, -1);
444887Schin 
454887Schin 	if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
464887Schin 		SFMTXRETURN(f, -1);
474887Schin 	SFLOCK(f,0);
484887Schin 
494887Schin 	/* code v as integers in base SF_UBASE */
504887Schin 	s = ps = &(c[N_ARRAY-1]);
514887Schin 	*s = (uchar)SFUVALUE(v);
524887Schin 	while((v >>= SF_UBITS) )
534887Schin 		*--s = (uchar)(SFUVALUE(v) | SF_MORE);
544887Schin 	n = (ps-s)+1;
554887Schin 
564887Schin 	if(n > 8 || SFWPEEK(f,ps,p) < n)
574887Schin 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
584887Schin 	else
594887Schin 	{	switch(n)
604887Schin 		{
614887Schin 		case 8 : *ps++ = *s++;
624887Schin 		case 7 : *ps++ = *s++;
634887Schin 		case 6 : *ps++ = *s++;
644887Schin 		case 5 : *ps++ = *s++;
654887Schin 		case 4 : *ps++ = *s++;
664887Schin 		case 3 : *ps++ = *s++;
674887Schin 		case 2 : *ps++ = *s++;
684887Schin 		case 1 : *ps++ = *s++;
694887Schin 		}
704887Schin 		f->next = ps;
714887Schin 	}
724887Schin 
734887Schin 	SFOPEN(f,0);
744887Schin 	SFMTXRETURN(f, (int)n);
754887Schin }
76