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