xref: /titanic_41/usr/src/lib/libast/common/sfio/sfnputc.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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(Sfio_t* f, int c, size_t n)
31 #else
32 ssize_t sfnputc(f,c,n)
33 Sfio_t*		f;	/* file to write */
34 int		c;	/* char to be written */
35 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 	SFMTXDECL(f);
43 
44 	SFMTXENTER(f,-1);
45 
46 	GETLOCAL(f,local);
47 	if(SFMODE(f,local) != SF_WRITE && _sfmode(f,SF_WRITE,local) < 0)
48 		SFMTXRETURN(f, -1);
49 
50 	SFLOCK(f,local);
51 
52 	/* write into a suitable buffer */
53 	if((size_t)(p = (f->endb-(ps = f->next))) < n)
54 		{ ps = buf; p = sizeof(buf); }
55 	if((size_t)p > n)
56 		p = n;
57 	MEMSET(ps,c,p);
58 	ps -= p;
59 
60 	w = n;
61 	if(ps == f->next)
62 	{	/* simple sfwrite */
63 		f->next += p;
64 		if(c == '\n')
65 			(void)SFFLSBUF(f,-1);
66 		goto done;
67 	}
68 
69 	for(;;)
70 	{	/* hard write of data */
71 		if((p = SFWRITE(f,(Void_t*)ps,p)) <= 0 || (n -= p) <= 0)
72 		{	w -= n;
73 			goto done;
74 		}
75 		if((size_t)p > n)
76 			p = n;
77 	}
78 done :
79 	SFOPEN(f,local);
80 	SFMTXRETURN(f, w);
81 }
82