1 /* @(#)gtprintf.c	1.1 18/06/16 Copyright 1985, 1995-2018 J. Schilling */
2 /*
3  *	Copyright (c) 1985, 1995-2018 J. Schilling
4  *	These routines call gettext() on the format first.
5  */
6 /*
7  * The contents of this file are subject to the terms of the
8  * Common Development and Distribution License, Version 1.0 only
9  * (the "License").  You may not use this file except in compliance
10  * with the License.
11  *
12  * See the file CDDL.Schily.txt in this distribution for details.
13  * A copy of the CDDL is also available via the Internet at
14  * http://www.opensource.org/licenses/cddl1.txt
15  *
16  * When distributing Covered Code, include this CDDL HEADER in each
17  * file and include the License file CDDL.Schily.txt from this distribution.
18  */
19 
20 #include <schily/mconfig.h>
21 #include <schily/stdio.h>
22 #include <schily/types.h>
23 #include <schily/varargs.h>
24 #include <schily/standard.h>
25 #include <schily/schily.h>
26 #include <schily/nlsdefs.h>
27 
28 #ifdef	NO_FPRFORMAT
29 #undef	USE_FPRFORMAT
30 #else
31 #define	USE_FPRFORMAT
32 #endif
33 
34 #ifdef	USE_FPRFORMAT
35 /*
36  * This is the speed-optimized version that currently only has been tested
37  * on Solaris.
38  * It is based on fprformat() instead of format() and is faster than the
39  * the format() based standard implementation, in case that putc() or
40  * putc_unlocked() is a macro.
41  */
42 
43 /* VARARGS1 */
44 #ifdef	PROTOTYPES
45 EXPORT int
gtprintf(const char * form,...)46 gtprintf(const char *form, ...)
47 #else
48 EXPORT int
49 gtprintf(form, va_alist)
50 	char	*form;
51 	va_dcl
52 #endif
53 {
54 	va_list	args;
55 	int	ret;
56 
57 #ifdef	PROTOTYPES
58 	va_start(args, form);
59 #else
60 	va_start(args);
61 #endif
62 	ret = fprformat(stdout, _(form), args);
63 	va_end(args);
64 	return (ret);
65 }
66 
67 /* VARARGS2 */
68 #ifdef	PROTOTYPES
69 EXPORT int
fgtprintf(FILE * file,const char * form,...)70 fgtprintf(FILE *file, const char *form, ...)
71 #else
72 EXPORT int
73 fgtprintf(file, form, va_alist)
74 	FILE	*file;
75 	char	*form;
76 	va_dcl
77 #endif
78 {
79 	va_list	args;
80 	int	ret;
81 
82 #ifdef	PROTOTYPES
83 	va_start(args, form);
84 #else
85 	va_start(args);
86 #endif
87 	ret = fprformat(file, _(form), args);
88 	va_end(args);
89 	return (ret);
90 }
91 
92 #else	/* !USE_FPRFORMAT */
93 /*
94  * This is the portable standard implementation that works anywhere.
95  */
96 
97 #define	BFSIZ	256
98 
99 typedef struct {
100 	short	cnt;
101 	char	*ptr;
102 	char	buf[BFSIZ];
103 	int	count;
104 	FILE	*f;
105 } *BUF, _BUF;
106 
107 LOCAL	void	_bflush		__PR((BUF));
108 LOCAL	void	_bput		__PR((char, void *));
109 EXPORT	int	fgtprintf	__PR((FILE *, const char *, ...));
110 EXPORT	int	gtprintf	__PR((const char *, ...));
111 
112 LOCAL void
_bflush(bp)113 _bflush(bp)
114 	register BUF	bp;
115 {
116 	bp->count += bp->ptr - bp->buf;
117 	if (filewrite(bp->f, bp->buf, bp->ptr - bp->buf) < 0)
118 		bp->count = EOF;
119 	bp->ptr = bp->buf;
120 	bp->cnt = BFSIZ;
121 }
122 
123 #ifdef	PROTOTYPES
124 LOCAL void
_bput(char c,void * l)125 _bput(char c, void *l)
126 #else
127 LOCAL void
128 _bput(c, l)
129 		char	c;
130 		void	*l;
131 #endif
132 {
133 	register BUF	bp = (BUF)l;
134 
135 	*bp->ptr++ = c;
136 	if (--bp->cnt <= 0)
137 		_bflush(bp);
138 }
139 
140 /* VARARGS1 */
141 #ifdef	PROTOTYPES
142 EXPORT int
gtprintf(const char * form,...)143 gtprintf(const char *form, ...)
144 #else
145 EXPORT int
146 gtprintf(form, va_alist)
147 	char	*form;
148 	va_dcl
149 #endif
150 {
151 	va_list	args;
152 	_BUF	bb;
153 
154 	bb.ptr = bb.buf;
155 	bb.cnt = BFSIZ;
156 	bb.count = 0;
157 	bb.f = stdout;
158 #ifdef	PROTOTYPES
159 	va_start(args, form);
160 #else
161 	va_start(args);
162 #endif
163 	format(_bput, &bb, _(form), args);
164 	va_end(args);
165 	if (bb.cnt < BFSIZ)
166 		_bflush(&bb);
167 	return (bb.count);
168 }
169 
170 /* VARARGS2 */
171 #ifdef	PROTOTYPES
172 EXPORT int
fgtprintf(FILE * file,const char * form,...)173 fgtprintf(FILE *file, const char *form, ...)
174 #else
175 EXPORT int
176 fgtprintf(file, form, va_alist)
177 	FILE	*file;
178 	char	*form;
179 	va_dcl
180 #endif
181 {
182 	va_list	args;
183 	_BUF	bb;
184 
185 	bb.ptr = bb.buf;
186 	bb.cnt = BFSIZ;
187 	bb.count = 0;
188 	bb.f = file;
189 #ifdef	PROTOTYPES
190 	va_start(args, form);
191 #else
192 	va_start(args);
193 #endif
194 	format(_bput, &bb, _(form), args);
195 	va_end(args);
196 	if (bb.cnt < BFSIZ)
197 		_bflush(&bb);
198 	return (bb.count);
199 }
200 #endif
201