]> git.sur5r.net Git - openldap/blob - libraries/liblutil/stdio.c
Import ITS#2007 and ITS#2009 bug fixes from HEAD
[openldap] / libraries / liblutil / stdio.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10 #include <ac/stdarg.h>
11 #include <ac/string.h>
12
13 #include <lutil.h>
14
15 #ifndef HAVE_VSNPRINTF
16 /* Write at most n characters to the buffer in str, return the
17  * number of chars written or -1 if the buffer would have been
18  * overflowed.
19  *
20  * This is portable to any POSIX-compliant system. We use pipe()
21  * to create a valid file descriptor, and then fdopen() it to get
22  * a valid FILE pointer. The user's buffer and size are assigned
23  * to the FILE pointer using setvbuf. Then we close the read side
24  * of the pipe to invalidate the descriptor.
25  *
26  * If the write arguments all fit into size n, the write will
27  * return successfully. If the write is too large, the stdio
28  * buffer will need to be flushed to the underlying file descriptor.
29  * The flush will fail because it is attempting to write to a
30  * broken pipe, and the write will be terminated.
31  * -- hyc, 2002-07-19
32  */
33 #ifndef HAVE_EBCDIC
34 /* This emulation uses vfprintf; on OS/390 we're also emulating
35  * that function so it's more efficient just to have a separate
36  * version of vsnprintf there.
37  */
38 #include <ac/signal.h>
39 int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
40 {
41         int fds[2], res;
42         FILE *f;
43         RETSIGTYPE (*sig)();
44
45         if (pipe( fds )) return -1;
46
47         f = fdopen( fds[1], "w" );
48         if ( !f ) {
49                 close( fds[1] );
50                 close( fds[0] );
51                 return -1;
52         }
53         setvbuf( f, str, _IOFBF, n );
54         sig = signal( SIGPIPE, SIG_IGN );
55         close( fds[0] );
56
57         res = vfprintf( f, fmt, ap );
58
59         fclose( f );
60         signal( SIGPIPE, sig );
61         return res;
62 }
63 #endif
64
65 int snprintf( char *str, size_t n, const char *fmt, ... )
66 {
67         va_list ap;
68         int res;
69
70         va_start( ap, fmt );
71         res = vsnprintf( str, n, fmt, ap );
72         va_end( ap );
73         return res;
74 }
75 #endif /* !HAVE_VSNPRINTF */
76
77 #ifdef HAVE_EBCDIC
78 /* stdio replacements with ASCII/EBCDIC translation for OS/390.
79  * The OS/390 port depends on the CONVLIT compiler option being
80  * used to force character and string literals to be compiled in
81  * ISO8859-1, and the __LIBASCII cpp symbol to be defined to use the
82  * OS/390 ASCII-compatibility library. This library only supplies
83  * an ASCII version of sprintf, so other needed functions are
84  * provided here.
85  *
86  * All of the internal character manipulation is done in ASCII,
87  * but file I/O is EBCDIC, so we catch any stdio reading/writing
88  * of files here and do the translations.
89  */
90
91 #undef fputs
92 #undef fgets
93
94 char *lutil_fgets( char *s, int n, FILE *fp )
95 {
96         s = (char *)fgets( s, n, fp );
97         if ( s ) __etoa( s );
98         return s;
99 }
100
101 int lutil_fputs( const char *str, FILE *fp )
102 {
103         char buf[8192];
104
105         strncpy( buf, str, sizeof(buf) );
106         __atoe( buf );
107         return fputs( buf, fp );
108 }
109
110 /* The __LIBASCII doesn't include a working vsprintf, so we make do
111  * using just sprintf. This is a very simplistic parser that looks for
112  * format strings and uses sprintf to process them one at a time.
113  * Literal text is just copied straight to the destination.
114  * The result is appended to the destination string. The parser
115  * recognizes field-width specifiers and the 'l' qualifier; it
116  * may need to be extended to recognize other qualifiers but so
117  * far this seems to be enough.
118  */
119 int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
120 {
121         char *ptr, *pct, *s2, *f2, *end;
122         char fm2[64];
123         int len, rem;
124
125         ptr = (char *)fmt;
126         s2 = str;
127         fm2[0] = '%';
128         if (n)
129                 end = str + n;
130         else
131                 end = NULL;
132
133         for (pct = strchr(ptr, '%'); pct; pct = strchr(ptr, '%')) {
134                 len = pct-ptr;
135                 if (end) {
136                         rem = end-s2;
137                         if (rem < 1) return -1;
138                         if (rem < len) len = rem;
139                 }
140                 s2 = lutil_strncopy( s2, ptr, len );
141                 /* Did we cheat the length above? If so, bail out */
142                 if (len < pct-ptr) return -1;
143                 for (pct++, f2 = fm2+1; isdigit(*pct);) *f2++ = *pct++;
144                 if (*pct == 'l') *f2++ = *pct++;
145                 if (*pct == '%') *s2++ = '%';
146                 else {
147                         *f2++ = *pct;
148                         *f2 = '\0';
149                         if (*pct == 's') {
150                                 char *ss = va_arg(ap, char *);
151                                 /* Attempt to limit sprintf output. This
152                                  * may be thrown off if field widths were
153                                  * specified for this string.
154                                  *
155                                  * If it looks like the string is too
156                                  * long for the remaining buffer, bypass
157                                  * sprintf and just copy what fits, then
158                                  * quit.
159                                  */
160                                 if (end && strlen(ss) > (rem=end-s2)) {
161                                         strncpy(s2, ss, rem);
162                                         return -1;
163                                 } else {
164                                         s2 += sprintf(s2, fm2, ss);
165                                 }
166                         } else
167                                 s2 += sprintf(s2, fm2, va_arg(ap, int));
168                 }
169                 ptr = pct + 1;
170         }
171         if (end) {
172                 rem = end-s2;
173                 if (rem > 0) {
174                         len = strlen(ptr);
175                         s2 = lutil_strncopy( s2, ptr, rem );
176                         rem -= len;
177                 }
178                 if (rem < 0) return -1;
179         } else {
180                 s2 = lutil_strcopy( s2, ptr );
181         }
182         return s2 - str;
183 }
184
185 int lutil_vsprintf( char *str, const char *fmt, va_list ap )
186 {
187         return vsnprintf( str, 0, fmt, ap );
188 }
189
190 /* The fixed buffer size here is a problem, we don't know how
191  * to flush the buffer and keep printing if the msg is too big. 
192  * Hopefully we never try to write something bigger than this
193  * in a log msg...
194  */
195 int lutil_vfprintf( FILE *fp, const char *fmt, va_list ap )
196 {
197         char buf[8192];
198         int res;
199
200         vsnprintf( buf, sizeof(buf), fmt, ap );
201         __atoe( buf );
202         res = fputs( buf, fp );
203         if (res == EOF) res = -1;
204         return res;
205 }
206
207 int lutil_printf( const char *fmt, ... )
208 {
209         va_list ap;
210         int res;
211
212         va_start( ap, fmt );
213         res = lutil_vfprintf( stdout, fmt, ap );
214         va_end( ap );
215         return res;
216 }
217
218 int lutil_fprintf( FILE *fp, const char *fmt, ... )
219 {
220         va_list ap;
221         int res;
222
223         va_start( ap, fmt );
224         res = lutil_vfprintf( fp, fmt, ap );
225         va_end( ap );
226         return res;
227 }
228 #endif