3 * Copyright 2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdarg.h>
11 #include <ac/string.h>
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
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.
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.
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.
38 #include <ac/signal.h>
39 int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
45 if (pipe( fds )) return -1;
47 f = fdopen( fds[1], "w" );
53 setvbuf( f, str, _IOFBF, n );
54 sig = signal( SIGPIPE, SIG_IGN );
57 res = vfprintf( f, fmt, ap );
60 signal( SIGPIPE, sig );
65 int snprintf( char *str, size_t n, const char *fmt, ... )
71 res = vsnprintf( str, n, fmt, ap );
75 #endif /* !HAVE_VSNPRINTF */
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
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.
94 char *lutil_fgets( char *s, int n, FILE *fp )
96 s = (char *)fgets( s, n, fp );
101 int lutil_fputs( const char *str, FILE *fp )
105 strncpy( buf, str, sizeof(buf) );
107 return fputs( buf, fp );
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.
119 int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
121 char *ptr, *pct, *s2, *f2, *end;
133 for (pct = strchr(ptr, '%'); pct; pct = strchr(ptr, '%')) {
137 if (rem < 1) return -1;
138 if (rem < len) len = rem;
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++ = '%';
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.
155 * If it looks like the string is too
156 * long for the remaining buffer, bypass
157 * sprintf and just copy what fits, then
160 if (end && strlen(ss) > (rem=end-s2)) {
161 strncpy(s2, ss, rem);
164 s2 += sprintf(s2, fm2, ss);
167 s2 += sprintf(s2, fm2, va_arg(ap, int));
175 s2 = lutil_strncopy( s2, ptr, rem );
178 if (rem < 0) return -1;
180 s2 = lutil_strcopy( s2, ptr );
185 int lutil_vsprintf( char *str, const char *fmt, va_list ap )
187 return vsnprintf( str, 0, fmt, ap );
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
195 int lutil_vfprintf( FILE *fp, const char *fmt, va_list ap )
200 vsnprintf( buf, sizeof(buf), fmt, ap );
202 res = fputs( buf, fp );
203 if (res == EOF) res = -1;
207 int lutil_printf( const char *fmt, ... )
213 res = lutil_vfprintf( stdout, fmt, ap );
218 int lutil_fprintf( FILE *fp, const char *fmt, ... )
224 res = lutil_vfprintf( fp, fmt, ap );