xref: /onnv-gate/usr/src/lib/libast/common/sfio/_sfputl.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 a long value in a portable format
254887Schin **
264887Schin **	Written by Kiem-Phong Vo.
274887Schin */
284887Schin 
294887Schin #if __STD_C
_sfputl(Sfio_t * f,Sflong_t v)308462SApril.Chin@Sun.COM int _sfputl(Sfio_t* f, Sflong_t v)
314887Schin #else
324887Schin int _sfputl(f,v)
338462SApril.Chin@Sun.COM Sfio_t*		f;	/* write a portable long to this stream */
344887Schin Sflong_t	v;	/* the value to be written */
354887Schin #endif
364887Schin {
374887Schin #define N_ARRAY		(2*sizeof(Sflong_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 	if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
454887Schin 		SFMTXRETURN(f, -1);
464887Schin 	SFLOCK(f,0);
474887Schin 
484887Schin 	s = ps = &(c[N_ARRAY-1]);
494887Schin 	if(v < 0)
504887Schin 	{	/* add 1 to avoid 2-complement problems with -SF_MAXINT */
514887Schin 		v = -(v+1);
524887Schin 		*s = (uchar)(SFSVALUE(v) | SF_SIGN);
534887Schin 	}
544887Schin 	else	*s = (uchar)(SFSVALUE(v));
554887Schin 	v = (Sfulong_t)v >> SF_SBITS;
564887Schin 
574887Schin 	while(v > 0)
584887Schin 	{	*--s = (uchar)(SFUVALUE(v) | SF_MORE);
594887Schin 		v = (Sfulong_t)v >> SF_UBITS;
604887Schin 	}
614887Schin 	n = (ps-s)+1;
624887Schin 
634887Schin 	if(n > 8 || SFWPEEK(f,ps,p) < n)
644887Schin 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
654887Schin 	else
664887Schin 	{	switch(n)
674887Schin 		{
684887Schin 		case 8 : *ps++ = *s++;
694887Schin 		case 7 : *ps++ = *s++;
704887Schin 		case 6 : *ps++ = *s++;
714887Schin 		case 5 : *ps++ = *s++;
724887Schin 		case 4 : *ps++ = *s++;
734887Schin 		case 3 : *ps++ = *s++;
744887Schin 		case 2 : *ps++ = *s++;
754887Schin 		case 1 : *ps++ = *s++;
764887Schin 		}
774887Schin 		f->next = ps;
784887Schin 	}
794887Schin 
804887Schin 	SFOPEN(f,0);
814887Schin 	SFMTXRETURN(f, n);
824887Schin }
83