14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*8462SApril.Chin@Sun.COM * Copyright (c) 1985-2008 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 7*8462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * Phong Vo <kpv@research.att.com> * 204887Schin * * 214887Schin ***********************************************************************/ 224887Schin #include "sfhdr.h" 234887Schin 244887Schin /* Write out an unsigned long value in a portable format. 254887Schin ** 264887Schin ** Written by Kiem-Phong Vo. 274887Schin */ 284887Schin 294887Schin #if __STD_C 30*8462SApril.Chin@Sun.COM int _sfputm(Sfio_t* f, Sfulong_t v, Sfulong_t m) 314887Schin #else 324887Schin int _sfputm(f,v,m) 33*8462SApril.Chin@Sun.COM Sfio_t* f; /* write a portable ulong to this stream */ 344887Schin Sfulong_t v; /* the unsigned value to be written */ 354887Schin Sfulong_t m; /* the max value of the range */ 364887Schin #endif 374887Schin { 384887Schin #define N_ARRAY (2*sizeof(Sfulong_t)) 394887Schin reg uchar *s, *ps; 404887Schin reg ssize_t n, p; 414887Schin uchar c[N_ARRAY]; 42*8462SApril.Chin@Sun.COM SFMTXDECL(f); 434887Schin 44*8462SApril.Chin@Sun.COM SFMTXENTER(f, -1); 454887Schin 464887Schin if(v > m || (f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) ) 474887Schin SFMTXRETURN(f, -1); 484887Schin SFLOCK(f,0); 494887Schin 504887Schin /* code v as integers in base SF_UBASE */ 514887Schin s = ps = &(c[N_ARRAY-1]); 524887Schin *s = (uchar)SFBVALUE(v); 534887Schin while((m >>= SF_BBITS) > 0 ) 544887Schin { v >>= SF_BBITS; 554887Schin *--s = (uchar)SFBVALUE(v); 564887Schin } 574887Schin n = (ps-s)+1; 584887Schin 594887Schin if(n > 8 || SFWPEEK(f,ps,p) < n) 604887Schin n = SFWRITE(f,(Void_t*)s,n); /* write the hard way */ 614887Schin else 624887Schin { switch(n) 634887Schin { 644887Schin case 8 : *ps++ = *s++; 654887Schin case 7 : *ps++ = *s++; 664887Schin case 6 : *ps++ = *s++; 674887Schin case 5 : *ps++ = *s++; 684887Schin case 4 : *ps++ = *s++; 694887Schin case 3 : *ps++ = *s++; 704887Schin case 2 : *ps++ = *s++; 714887Schin case 1 : *ps++ = *s++; 724887Schin } 734887Schin f->next = ps; 744887Schin } 754887Schin 764887Schin SFOPEN(f,0); 774887Schin SFMTXRETURN(f, (int)n); 784887Schin } 79