stdio.c 6.0 KB

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