xref: /titanic_41/usr/src/lib/libast/common/sfio/_sfputm.c (revision 9ec394dbf343c1f23c6e13c39df427f238e5a369)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 /*	Write out an unsigned long value in a portable format.
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
30 int _sfputm(reg Sfio_t* f, Sfulong_t v, Sfulong_t m)
31 #else
32 int _sfputm(f,v,m)
33 reg Sfio_t*	f;	/* write a portable ulong to this stream */
34 Sfulong_t	v;	/* the unsigned value to be written */
35 Sfulong_t	m;	/* the max value of the range */
36 #endif
37 {
38 #define N_ARRAY		(2*sizeof(Sfulong_t))
39 	reg uchar	*s, *ps;
40 	reg ssize_t	n, p;
41 	uchar		c[N_ARRAY];
42 
43 	SFMTXSTART(f, -1);
44 
45 	if(v > m || (f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) )
46 		SFMTXRETURN(f, -1);
47 	SFLOCK(f,0);
48 
49 	/* code v as integers in base SF_UBASE */
50 	s = ps = &(c[N_ARRAY-1]);
51 	*s = (uchar)SFBVALUE(v);
52 	while((m >>= SF_BBITS) > 0 )
53 	{	v >>= SF_BBITS;
54 		*--s = (uchar)SFBVALUE(v);
55 	}
56 	n = (ps-s)+1;
57 
58 	if(n > 8 || SFWPEEK(f,ps,p) < n)
59 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
60 	else
61 	{	switch(n)
62 		{
63 		case 8 : *ps++ = *s++;
64 		case 7 : *ps++ = *s++;
65 		case 6 : *ps++ = *s++;
66 		case 5 : *ps++ = *s++;
67 		case 4 : *ps++ = *s++;
68 		case 3 : *ps++ = *s++;
69 		case 2 : *ps++ = *s++;
70 		case 1 : *ps++ = *s++;
71 		}
72 		f->next = ps;
73 	}
74 
75 	SFOPEN(f,0);
76 	SFMTXRETURN(f, (int)n);
77 }
78