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
_sfputu(Sfio_t * f,Sfulong_t v)30 int _sfputu(Sfio_t* f, Sfulong_t v)
31 #else
32 int _sfputu(f,v)
33 Sfio_t*		f;	/* write a portable ulong to this stream */
34 Sfulong_t	v;	/* the unsigned value to be written */
35 #endif
36 {
37 #define N_ARRAY		(2*sizeof(Sfulong_t))
38 	reg uchar	*s, *ps;
39 	reg ssize_t	n, p;
40 	uchar		c[N_ARRAY];
41 	SFMTXDECL(f);
42 
43 	SFMTXENTER(f, -1);
44 
45 	if(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)SFUVALUE(v);
52 	while((v >>= SF_UBITS) )
53 		*--s = (uchar)(SFUVALUE(v) | SF_MORE);
54 	n = (ps-s)+1;
55 
56 	if(n > 8 || SFWPEEK(f,ps,p) < n)
57 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
58 	else
59 	{	switch(n)
60 		{
61 		case 8 : *ps++ = *s++;
62 		/* FALLTHROUGH */
63 		case 7 : *ps++ = *s++;
64 		/* FALLTHROUGH */
65 		case 6 : *ps++ = *s++;
66 		/* FALLTHROUGH */
67 		case 5 : *ps++ = *s++;
68 		/* FALLTHROUGH */
69 		case 4 : *ps++ = *s++;
70 		/* FALLTHROUGH */
71 		case 3 : *ps++ = *s++;
72 		/* FALLTHROUGH */
73 		case 2 : *ps++ = *s++;
74 		/* FALLTHROUGH */
75 		case 1 : *ps++ = *s++;
76 		}
77 		f->next = ps;
78 	}
79 
80 	SFOPEN(f,0);
81 	SFMTXRETURN(f, (int)n);
82 }
83