1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * All rights reserved.
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
7*7c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
8*7c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
9*7c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
10*7c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
11*7c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
12*7c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
13*7c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
14*7c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*7c478bd9Sstevel@tonic-gate  *
16*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*7c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*7c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19*7c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20*7c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21*7c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22*7c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23*7c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24*7c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
27*7c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
28*7c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
29*7c478bd9Sstevel@tonic-gate  * of the copyright holder.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "ioutil.h"
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static int _io_pad_line(GlWriteFn *write_fn, void *data, int c, int n);
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*.......................................................................
43*7c478bd9Sstevel@tonic-gate  * Display a left-justified string over multiple terminal lines,
44*7c478bd9Sstevel@tonic-gate  * taking account of the specified width of the terminal. Optional
45*7c478bd9Sstevel@tonic-gate  * indentation and an option prefix string can be specified to be
46*7c478bd9Sstevel@tonic-gate  * displayed at the start of each new terminal line used, and if
47*7c478bd9Sstevel@tonic-gate  * needed, a single paragraph can be broken across multiple calls.
48*7c478bd9Sstevel@tonic-gate  * Note that literal newlines in the input string can be used to force
49*7c478bd9Sstevel@tonic-gate  * a newline at any point, and that in order to allow individual
50*7c478bd9Sstevel@tonic-gate  * paragraphs to be written using multiple calls to this function,
51*7c478bd9Sstevel@tonic-gate  * unless an explicit newline character is specified at the end of the
52*7c478bd9Sstevel@tonic-gate  * string, a newline will not be started at the end of the last word
53*7c478bd9Sstevel@tonic-gate  * in the string. Note that when a new line is started between two
54*7c478bd9Sstevel@tonic-gate  * words that are separated by spaces, those spaces are not output,
55*7c478bd9Sstevel@tonic-gate  * whereas when a new line is started because a newline character was
56*7c478bd9Sstevel@tonic-gate  * found in the string, only the spaces before the newline character
57*7c478bd9Sstevel@tonic-gate  * are discarded.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * Input:
60*7c478bd9Sstevel@tonic-gate  *  write_fn  GlWriteFn *  The callback function to use to write the
61*7c478bd9Sstevel@tonic-gate  *                         output.
62*7c478bd9Sstevel@tonic-gate  *  data           void *  A pointer to arbitrary data to be passed to
63*7c478bd9Sstevel@tonic-gate  *                         write_fn() whenever it is called.
64*7c478bd9Sstevel@tonic-gate  *  fp             FILE *  The stdio stream to write to.
65*7c478bd9Sstevel@tonic-gate  *  indentation     int    The number of fill characters to use to
66*7c478bd9Sstevel@tonic-gate  *                        indent the start of each new terminal line.
67*7c478bd9Sstevel@tonic-gate  *  prefix   const char *  An optional prefix string to write after the
68*7c478bd9Sstevel@tonic-gate  *                         indentation margin at the start of each new
69*7c478bd9Sstevel@tonic-gate  *                         terminal line. You can specify NULL if no
70*7c478bd9Sstevel@tonic-gate  *                         prefix is required.
71*7c478bd9Sstevel@tonic-gate  *  suffix   const char *  An optional suffix string to draw at the end
72*7c478bd9Sstevel@tonic-gate  *                         of the terminal line. The line will be padded
73*7c478bd9Sstevel@tonic-gate  *                         where necessary to ensure that the suffix ends
74*7c478bd9Sstevel@tonic-gate  *                         in the last column of the terminal line. If
75*7c478bd9Sstevel@tonic-gate  *                         no suffix is desired, specify NULL.
76*7c478bd9Sstevel@tonic-gate  *  fill_char       int    The padding character to use when indenting
77*7c478bd9Sstevel@tonic-gate  *                         and filling up to the suffix.
78*7c478bd9Sstevel@tonic-gate  *  term_width      int    The width of the terminal being written to.
79*7c478bd9Sstevel@tonic-gate  *  start           int    The number of characters already written to
80*7c478bd9Sstevel@tonic-gate  *                         the start of the current terminal line. This
81*7c478bd9Sstevel@tonic-gate  *                         is primarily used to allow individual
82*7c478bd9Sstevel@tonic-gate  *                         paragraphs to be written over multiple calls
83*7c478bd9Sstevel@tonic-gate  *                         to this function, but can also be used to
84*7c478bd9Sstevel@tonic-gate  *                         allow you to start the first line of a
85*7c478bd9Sstevel@tonic-gate  *                         paragraph with a different prefix or
86*7c478bd9Sstevel@tonic-gate  *                         indentation than those specified above.
87*7c478bd9Sstevel@tonic-gate  *  string   const char *  The string to be written.
88*7c478bd9Sstevel@tonic-gate  * Output:
89*7c478bd9Sstevel@tonic-gate  *  return          int    On error -1 is returned. Otherwise the
90*7c478bd9Sstevel@tonic-gate  *                         return value is the terminal column index at
91*7c478bd9Sstevel@tonic-gate  *                         which the cursor was left after writing the
92*7c478bd9Sstevel@tonic-gate  *                         final word in the string. Successful return
93*7c478bd9Sstevel@tonic-gate  *                         values can thus be passed verbatim to the
94*7c478bd9Sstevel@tonic-gate  *                         'start' arguments of subsequent calls to
95*7c478bd9Sstevel@tonic-gate  *                         _io_display_text() to allow the printing of a
96*7c478bd9Sstevel@tonic-gate  *                         paragraph to be broken across multiple calls
97*7c478bd9Sstevel@tonic-gate  *                         to _io_display_text().
98*7c478bd9Sstevel@tonic-gate  */
_io_display_text(GlWriteFn * write_fn,void * data,int indentation,const char * prefix,const char * suffix,int fill_char,int term_width,int start,const char * string)99*7c478bd9Sstevel@tonic-gate int _io_display_text(GlWriteFn *write_fn, void *data, int indentation,
100*7c478bd9Sstevel@tonic-gate 		     const char *prefix, const char *suffix, int fill_char,
101*7c478bd9Sstevel@tonic-gate 		     int term_width, int start, const char *string)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate   int ndone;        /* The number of characters written from string[] */
104*7c478bd9Sstevel@tonic-gate   int nnew;         /* The number of characters to be displayed next */
105*7c478bd9Sstevel@tonic-gate   int was_space;    /* True if the previous character was a space or tab */
106*7c478bd9Sstevel@tonic-gate   int last = start; /* The column number of the last character written */
107*7c478bd9Sstevel@tonic-gate   int prefix_len;   /* The length of the optional line prefix string */
108*7c478bd9Sstevel@tonic-gate   int suffix_len;   /* The length of the optional line prefix string */
109*7c478bd9Sstevel@tonic-gate   int margin_width; /* The total number of columns used by the indentation */
110*7c478bd9Sstevel@tonic-gate                     /*  margin and the prefix string. */
111*7c478bd9Sstevel@tonic-gate   int i;
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * Check the arguments?
114*7c478bd9Sstevel@tonic-gate  */
115*7c478bd9Sstevel@tonic-gate   if(!string || !write_fn) {
116*7c478bd9Sstevel@tonic-gate     errno = EINVAL;
117*7c478bd9Sstevel@tonic-gate     return -1;
118*7c478bd9Sstevel@tonic-gate   };
119*7c478bd9Sstevel@tonic-gate /*
120*7c478bd9Sstevel@tonic-gate  * Enforce sensible values on the arguments.
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate   if(term_width < 0)
123*7c478bd9Sstevel@tonic-gate     term_width = 0;
124*7c478bd9Sstevel@tonic-gate   if(indentation > term_width)
125*7c478bd9Sstevel@tonic-gate     indentation = term_width;
126*7c478bd9Sstevel@tonic-gate   else if(indentation < 0)
127*7c478bd9Sstevel@tonic-gate     indentation = 0;
128*7c478bd9Sstevel@tonic-gate   if(start > term_width)
129*7c478bd9Sstevel@tonic-gate     start = term_width;
130*7c478bd9Sstevel@tonic-gate   else if(start < 0)
131*7c478bd9Sstevel@tonic-gate     start = 0;
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate  * Get the length of the prefix string.
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate   prefix_len = prefix ? strlen(prefix) : 0;
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate  * Get the length of the suffix string.
138*7c478bd9Sstevel@tonic-gate  */
139*7c478bd9Sstevel@tonic-gate   suffix_len = suffix ? strlen(suffix) : 0;
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * How many characters are devoted to indenting and prefixing each line?
142*7c478bd9Sstevel@tonic-gate  */
143*7c478bd9Sstevel@tonic-gate   margin_width = indentation + prefix_len;
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate  * Write as many terminal lines as are needed to display the whole string.
146*7c478bd9Sstevel@tonic-gate  */
147*7c478bd9Sstevel@tonic-gate   for(ndone=0; string[ndone]; start=0) {
148*7c478bd9Sstevel@tonic-gate     last = start;
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate  * Write spaces from the current position in the terminal line to the
151*7c478bd9Sstevel@tonic-gate  * width of the requested indentation margin.
152*7c478bd9Sstevel@tonic-gate  */
153*7c478bd9Sstevel@tonic-gate     if(indentation > 0 && last < indentation) {
154*7c478bd9Sstevel@tonic-gate       if(_io_pad_line(write_fn, data, fill_char, indentation - last))
155*7c478bd9Sstevel@tonic-gate 	return -1;
156*7c478bd9Sstevel@tonic-gate       last = indentation;
157*7c478bd9Sstevel@tonic-gate     };
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate  * If a prefix string has been specified, display it unless we have
160*7c478bd9Sstevel@tonic-gate  * passed where it should end in the terminal output line.
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate     if(prefix_len > 0 && last < margin_width) {
163*7c478bd9Sstevel@tonic-gate       int pstart = last - indentation;
164*7c478bd9Sstevel@tonic-gate       int plen = prefix_len - pstart;
165*7c478bd9Sstevel@tonic-gate       if(write_fn(data, prefix+pstart, plen) != plen)
166*7c478bd9Sstevel@tonic-gate 	return -1;
167*7c478bd9Sstevel@tonic-gate       last = margin_width;
168*7c478bd9Sstevel@tonic-gate     };
169*7c478bd9Sstevel@tonic-gate /*
170*7c478bd9Sstevel@tonic-gate  * Locate the end of the last complete word in the string before
171*7c478bd9Sstevel@tonic-gate  * (term_width - start) characters have been seen. To handle the case
172*7c478bd9Sstevel@tonic-gate  * where a single word is wider than the available space after the
173*7c478bd9Sstevel@tonic-gate  * indentation and prefix margins, always make sure that at least one
174*7c478bd9Sstevel@tonic-gate  * word is printed after the margin, regardless of whether it won't
175*7c478bd9Sstevel@tonic-gate  * fit on the line. The two exceptions to this rule are if an embedded
176*7c478bd9Sstevel@tonic-gate  * newline is found in the string or the end of the string is reached
177*7c478bd9Sstevel@tonic-gate  * before any word has been seen.
178*7c478bd9Sstevel@tonic-gate  */
179*7c478bd9Sstevel@tonic-gate     nnew = 0;
180*7c478bd9Sstevel@tonic-gate     was_space = 0;
181*7c478bd9Sstevel@tonic-gate     for(i=ndone; string[i] && (last+i-ndone < term_width - suffix_len ||
182*7c478bd9Sstevel@tonic-gate 			   (nnew==0 && last==margin_width)); i++) {
183*7c478bd9Sstevel@tonic-gate       if(string[i] == '\n') {
184*7c478bd9Sstevel@tonic-gate 	if(!was_space)
185*7c478bd9Sstevel@tonic-gate 	  nnew = i-ndone;
186*7c478bd9Sstevel@tonic-gate 	break;
187*7c478bd9Sstevel@tonic-gate       } else if(isspace((int) string[i])) {
188*7c478bd9Sstevel@tonic-gate 	if(!was_space) {
189*7c478bd9Sstevel@tonic-gate 	  nnew = i-ndone+1;
190*7c478bd9Sstevel@tonic-gate 	  was_space = 1;
191*7c478bd9Sstevel@tonic-gate 	};
192*7c478bd9Sstevel@tonic-gate       } else {
193*7c478bd9Sstevel@tonic-gate 	was_space = 0;
194*7c478bd9Sstevel@tonic-gate       };
195*7c478bd9Sstevel@tonic-gate     };
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  * Does the end of the string delimit the last word that will fit on the
198*7c478bd9Sstevel@tonic-gate  * output line?
199*7c478bd9Sstevel@tonic-gate  */
200*7c478bd9Sstevel@tonic-gate     if(nnew==0 && string[i] == '\0')
201*7c478bd9Sstevel@tonic-gate       nnew = i-ndone;
202*7c478bd9Sstevel@tonic-gate /*
203*7c478bd9Sstevel@tonic-gate  * Write the new line.
204*7c478bd9Sstevel@tonic-gate  */
205*7c478bd9Sstevel@tonic-gate     if(write_fn(data, string+ndone, nnew) != nnew)
206*7c478bd9Sstevel@tonic-gate       return -1;
207*7c478bd9Sstevel@tonic-gate     ndone += nnew;
208*7c478bd9Sstevel@tonic-gate     last += nnew;
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate  * Start a newline unless we have reached the end of the input string.
211*7c478bd9Sstevel@tonic-gate  * In the latter case, in order to give the caller the chance to
212*7c478bd9Sstevel@tonic-gate  * concatenate multiple calls to _io_display_text(), omit the newline,
213*7c478bd9Sstevel@tonic-gate  * leaving it up to the caller to write this.
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate     if(string[ndone] != '\0') {
216*7c478bd9Sstevel@tonic-gate /*
217*7c478bd9Sstevel@tonic-gate  * If a suffix has been provided, pad out the end of the line with spaces
218*7c478bd9Sstevel@tonic-gate  * such that the suffix will end in the right-most terminal column.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate       if(suffix_len > 0) {
221*7c478bd9Sstevel@tonic-gate 	int npad = term_width - suffix_len - last;
222*7c478bd9Sstevel@tonic-gate 	if(npad > 0 && _io_pad_line(write_fn, data, fill_char, npad))
223*7c478bd9Sstevel@tonic-gate 	  return -1;
224*7c478bd9Sstevel@tonic-gate 	last += npad;
225*7c478bd9Sstevel@tonic-gate 	if(write_fn(data, suffix, suffix_len) != suffix_len)
226*7c478bd9Sstevel@tonic-gate 	  return -1;
227*7c478bd9Sstevel@tonic-gate 	last += suffix_len;
228*7c478bd9Sstevel@tonic-gate       };
229*7c478bd9Sstevel@tonic-gate /*
230*7c478bd9Sstevel@tonic-gate  * Start a new line.
231*7c478bd9Sstevel@tonic-gate  */
232*7c478bd9Sstevel@tonic-gate       if(write_fn(data, "\n",  1) != 1)
233*7c478bd9Sstevel@tonic-gate 	return -1;
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * Skip any spaces and tabs that follow the last word that was written.
236*7c478bd9Sstevel@tonic-gate  */
237*7c478bd9Sstevel@tonic-gate       while(string[ndone] && isspace((int)string[ndone]) &&
238*7c478bd9Sstevel@tonic-gate 	    string[ndone] != '\n')
239*7c478bd9Sstevel@tonic-gate 	ndone++;
240*7c478bd9Sstevel@tonic-gate /*
241*7c478bd9Sstevel@tonic-gate  * If the terminating character was a literal newline character,
242*7c478bd9Sstevel@tonic-gate  * skip it in the input string, since we just wrote it.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate       if(string[ndone] == '\n')
245*7c478bd9Sstevel@tonic-gate 	ndone++;
246*7c478bd9Sstevel@tonic-gate       last = 0;
247*7c478bd9Sstevel@tonic-gate     };
248*7c478bd9Sstevel@tonic-gate   };
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * Return the column number of the last character printed.
251*7c478bd9Sstevel@tonic-gate  */
252*7c478bd9Sstevel@tonic-gate   return last;
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate /*.......................................................................
256*7c478bd9Sstevel@tonic-gate  * Write a given number of spaces to the specified stdio output string.
257*7c478bd9Sstevel@tonic-gate  *
258*7c478bd9Sstevel@tonic-gate  * Input:
259*7c478bd9Sstevel@tonic-gate  *  write_fn  GlWriteFn *  The callback function to use to write the
260*7c478bd9Sstevel@tonic-gate  *                         output.
261*7c478bd9Sstevel@tonic-gate  *  data           void *  A pointer to arbitrary data to be passed to
262*7c478bd9Sstevel@tonic-gate  *                         write_fn() whenever it is called.
263*7c478bd9Sstevel@tonic-gate  *  c               int    The padding character.
264*7c478bd9Sstevel@tonic-gate  *  n               int    The number of spaces to be written.
265*7c478bd9Sstevel@tonic-gate  * Output:
266*7c478bd9Sstevel@tonic-gate  *  return          int    0 - OK.
267*7c478bd9Sstevel@tonic-gate  *                         1 - Error.
268*7c478bd9Sstevel@tonic-gate  */
_io_pad_line(GlWriteFn * write_fn,void * data,int c,int n)269*7c478bd9Sstevel@tonic-gate static int _io_pad_line(GlWriteFn *write_fn, void *data, int c, int n)
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate   enum {FILL_SIZE=20};
272*7c478bd9Sstevel@tonic-gate   char fill[FILL_SIZE+1];
273*7c478bd9Sstevel@tonic-gate /*
274*7c478bd9Sstevel@tonic-gate  * Fill the buffer with the specified padding character.
275*7c478bd9Sstevel@tonic-gate  */
276*7c478bd9Sstevel@tonic-gate   memset(fill, c, FILL_SIZE);
277*7c478bd9Sstevel@tonic-gate   fill[FILL_SIZE] = '\0';
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * Write the spaces using the above literal string of spaces as
280*7c478bd9Sstevel@tonic-gate  * many times as needed to output the requested number of spaces.
281*7c478bd9Sstevel@tonic-gate  */
282*7c478bd9Sstevel@tonic-gate   while(n > 0) {
283*7c478bd9Sstevel@tonic-gate     int nnew = n <= FILL_SIZE ? n : FILL_SIZE;
284*7c478bd9Sstevel@tonic-gate     if(write_fn(data, fill, nnew) != nnew)
285*7c478bd9Sstevel@tonic-gate       return 1;
286*7c478bd9Sstevel@tonic-gate     n -= nnew;
287*7c478bd9Sstevel@tonic-gate   };
288*7c478bd9Sstevel@tonic-gate   return 0;
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate /*.......................................................................
292*7c478bd9Sstevel@tonic-gate  * The following is an output callback function which uses fwrite()
293*7c478bd9Sstevel@tonic-gate  * to write to the stdio stream specified via its callback data argument.
294*7c478bd9Sstevel@tonic-gate  *
295*7c478bd9Sstevel@tonic-gate  * Input:
296*7c478bd9Sstevel@tonic-gate  *  data     void *  The stdio stream to write to, specified via a
297*7c478bd9Sstevel@tonic-gate  *                   (FILE *) pointer cast to (void *).
298*7c478bd9Sstevel@tonic-gate  *  s  const char *  The string to be written.
299*7c478bd9Sstevel@tonic-gate  *  n         int    The length of the prefix of s[] to attempt to
300*7c478bd9Sstevel@tonic-gate  *                   write.
301*7c478bd9Sstevel@tonic-gate  * Output:
302*7c478bd9Sstevel@tonic-gate  *  return    int    The number of characters written from s[]. This
303*7c478bd9Sstevel@tonic-gate  *                   should normally be a number in the range 0 to n.
304*7c478bd9Sstevel@tonic-gate  *                   To signal that an I/O error occurred, return -1.
305*7c478bd9Sstevel@tonic-gate  */
GL_WRITE_FN(_io_write_stdio)306*7c478bd9Sstevel@tonic-gate GL_WRITE_FN(_io_write_stdio)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate   int ndone;   /* The total number of characters written */
309*7c478bd9Sstevel@tonic-gate   int nnew;    /* The number of characters written in the latest write */
310*7c478bd9Sstevel@tonic-gate /*
311*7c478bd9Sstevel@tonic-gate  * The callback data is the stdio stream to write to.
312*7c478bd9Sstevel@tonic-gate  */
313*7c478bd9Sstevel@tonic-gate   FILE *fp = (FILE *) data;
314*7c478bd9Sstevel@tonic-gate /*
315*7c478bd9Sstevel@tonic-gate  * Because of signals we may need to do more than one write to output
316*7c478bd9Sstevel@tonic-gate  * the whole string.
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate   for(ndone=0; ndone<n; ndone += nnew) {
319*7c478bd9Sstevel@tonic-gate     int nmore = n - ndone;
320*7c478bd9Sstevel@tonic-gate     nnew = fwrite(s, sizeof(char), nmore, fp);
321*7c478bd9Sstevel@tonic-gate     if(nnew < nmore) {
322*7c478bd9Sstevel@tonic-gate       if(errno == EINTR)
323*7c478bd9Sstevel@tonic-gate 	clearerr(fp);
324*7c478bd9Sstevel@tonic-gate       else
325*7c478bd9Sstevel@tonic-gate 	return ferror(fp) ? -1 : ndone + nnew;
326*7c478bd9Sstevel@tonic-gate     };
327*7c478bd9Sstevel@tonic-gate   };
328*7c478bd9Sstevel@tonic-gate   return ndone;
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate 
331