xref: /titanic_41/usr/src/lib/libast/common/sfio/sfnputc.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
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 character n times
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
30 ssize_t sfnputc(reg Sfio_t* f, reg int c, reg size_t n)
31 #else
32 ssize_t sfnputc(f,c,n)
33 reg Sfio_t*	f;	/* file to write */
34 reg int		c;	/* char to be written */
35 reg size_t	n;	/* number of time to repeat */
36 #endif
37 {
38 	reg uchar*	ps;
39 	reg ssize_t	p, w;
40 	uchar		buf[128];
41 	reg int		local;
42 
43 	SFMTXSTART(f,-1);
44 
45 	GETLOCAL(f,local);
46 	if(SFMODE(f,local) != SF_WRITE && _sfmode(f,SF_WRITE,local) < 0)
47 		SFMTXRETURN(f, -1);
48 
49 	SFLOCK(f,local);
50 
51 	/* write into a suitable buffer */
52 	if((size_t)(p = (f->endb-(ps = f->next))) < n)
53 		{ ps = buf; p = sizeof(buf); }
54 	if((size_t)p > n)
55 		p = n;
56 	MEMSET(ps,c,p);
57 	ps -= p;
58 
59 	w = n;
60 	if(ps == f->next)
61 	{	/* simple sfwrite */
62 		f->next += p;
63 		if(c == '\n')
64 			(void)SFFLSBUF(f,-1);
65 		goto done;
66 	}
67 
68 	for(;;)
69 	{	/* hard write of data */
70 		if((p = SFWRITE(f,(Void_t*)ps,p)) <= 0 || (n -= p) <= 0)
71 		{	w -= n;
72 			goto done;
73 		}
74 		if((size_t)p > n)
75 			p = n;
76 	}
77 done :
78 	SFOPEN(f,local);
79 	SFMTXRETURN(f, w);
80 }
81