1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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
_sfputm(Sfio_t * f,Sfulong_t v,Sfulong_t m)30 int _sfputm(Sfio_t* f, Sfulong_t v, Sfulong_t m)
31 #else
32 int _sfputm(f,v,m)
33 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 	SFMTXDECL(f);
43 
44 	SFMTXENTER(f, -1);
45 
46 	if(v > m || (f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) )
47 		SFMTXRETURN(f, -1);
48 	SFLOCK(f,0);
49 
50 	/* code v as integers in base SF_UBASE */
51 	s = ps = &(c[N_ARRAY-1]);
52 	*s = (uchar)SFBVALUE(v);
53 	while((m >>= SF_BBITS) > 0 )
54 	{	v >>= SF_BBITS;
55 		*--s = (uchar)SFBVALUE(v);
56 	}
57 	n = (ps-s)+1;
58 
59 	if(n > 8 || SFWPEEK(f,ps,p) < n)
60 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
61 	else
62 	{	switch(n)
63 		{
64 		case 8 : *ps++ = *s++;
65 		/* FALLTHROUGH */
66 		case 7 : *ps++ = *s++;
67 		/* FALLTHROUGH */
68 		case 6 : *ps++ = *s++;
69 		/* FALLTHROUGH */
70 		case 5 : *ps++ = *s++;
71 		/* FALLTHROUGH */
72 		case 4 : *ps++ = *s++;
73 		/* FALLTHROUGH */
74 		case 3 : *ps++ = *s++;
75 		/* FALLTHROUGH */
76 		case 2 : *ps++ = *s++;
77 		/* FALLTHROUGH */
78 		case 1 : *ps++ = *s++;
79 		}
80 		f->next = ps;
81 	}
82 
83 	SFOPEN(f,0);
84 	SFMTXRETURN(f, (int)n);
85 }
86