xref: /titanic_41/usr/src/lib/libast/common/sfio/_sfputl.c (revision 7ec363dc481bba196d724969022171de4687989f)
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 a long value in a portable format
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
30 int _sfputl(reg Sfio_t* f, Sflong_t v)
31 #else
32 int _sfputl(f,v)
33 reg Sfio_t*	f;	/* write a portable long to this stream */
34 Sflong_t	v;	/* the value to be written */
35 #endif
36 {
37 #define N_ARRAY		(2*sizeof(Sflong_t))
38 	reg uchar	*s, *ps;
39 	reg ssize_t	n, p;
40 	uchar		c[N_ARRAY];
41 
42 	SFMTXSTART(f,-1);
43 	if(f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0)
44 		SFMTXRETURN(f, -1);
45 	SFLOCK(f,0);
46 
47 	s = ps = &(c[N_ARRAY-1]);
48 	if(v < 0)
49 	{	/* add 1 to avoid 2-complement problems with -SF_MAXINT */
50 		v = -(v+1);
51 		*s = (uchar)(SFSVALUE(v) | SF_SIGN);
52 	}
53 	else	*s = (uchar)(SFSVALUE(v));
54 	v = (Sfulong_t)v >> SF_SBITS;
55 
56 	while(v > 0)
57 	{	*--s = (uchar)(SFUVALUE(v) | SF_MORE);
58 		v = (Sfulong_t)v >> SF_UBITS;
59 	}
60 	n = (ps-s)+1;
61 
62 	if(n > 8 || SFWPEEK(f,ps,p) < n)
63 		n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */
64 	else
65 	{	switch(n)
66 		{
67 		case 8 : *ps++ = *s++;
68 		case 7 : *ps++ = *s++;
69 		case 6 : *ps++ = *s++;
70 		case 5 : *ps++ = *s++;
71 		case 4 : *ps++ = *s++;
72 		case 3 : *ps++ = *s++;
73 		case 2 : *ps++ = *s++;
74 		case 1 : *ps++ = *s++;
75 		}
76 		f->next = ps;
77 	}
78 
79 	SFOPEN(f,0);
80 	SFMTXRETURN(f, n);
81 }
82