_SP_snprintf.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #ifdef _HAVE_STDC
00022 #include <stdarg.h>
00023 #else
00024 #include <varargs.h>
00025 #endif
00026 #include <limits.h>
00027 #include <errno.h>
00028 #include <_ansi.h>
00029
00030 #ifndef _SMALL_PRINTF
00031 #include "local.h"
00032 #else
00033 #ifdef INTEGER_ONLY
00034 #define _vfprintf_r _vfiprintf_r
00035 #endif
00036 #endif
00037
00038
00039 #ifndef _SMALL_PRINTF
00040 int
00041 #ifdef _HAVE_STDC
00042 _DEFUN (_snprintf_r, (ptr, str, size, fmt), struct _reent *ptr _AND char *str _AND size_t size _AND _CONST char *fmt _DOTS)
00043 #else
00044 _snprintf_r (ptr, str, size, fmt, va_alist)
00045 struct _reent *ptr;
00046 char *str;
00047 size_t size;
00048 _CONST char *fmt;
00049 va_dcl
00050 #endif
00051 {
00052 int ret;
00053 va_list ap;
00054 FILE f;
00055
00056 if (size > INT_MAX)
00057 {
00058 ptr->_errno = EOVERFLOW;
00059 return EOF;
00060 }
00061
00062 f._flags = __SWR | __SSTR;
00063 f._bf._base = f._p = (unsigned char *) str;
00064 f._bf._size = f._w = (size > 0 ? size - 1 : 0);
00065 f._file = -1;
00066 #ifdef _HAVE_STDC
00067 va_start (ap, fmt);
00068 #else
00069 va_start (ap);
00070 #endif
00071 ret = _vfprintf_r (ptr, &f, fmt, ap);
00072 va_end (ap);
00073 if (ret < EOF)
00074 ptr->_errno = EOVERFLOW;
00075 if (size > 0)
00076 *f._p = 0;
00077 return (ret);
00078 }
00079 #endif
00080
00081 #ifndef _REENT_ONLY
00082 int
00083 #ifdef _HAVE_STDC
00084 _DEFUN (snprintf, (str, size, fmt), char *str _AND size_t size _AND _CONST char *fmt _DOTS)
00085 #else
00086 snprintf (str, size, fmt, va_alist)
00087 char *str;
00088 size_t size;
00089 _CONST char *fmt;
00090 va_dcl
00091 #endif
00092 {
00093 int ret;
00094 va_list ap;
00095 FILE f;
00096
00097 struct _reent *ptr = _REENT;
00098
00099 if (size > INT_MAX)
00100 {
00101 ptr->_errno = EOVERFLOW;
00102 return EOF;
00103 }
00104
00105 f._flags = __SWR | __SSTR;
00106 f._bf._base = f._p = (unsigned char *) str;
00107 f._bf._size = f._w = (size > 0 ? size - 1 : 0);
00108 f._file = -1;
00109 #ifdef _HAVE_STDC
00110 va_start (ap, fmt);
00111 #else
00112 va_start (ap);
00113 #endif
00114 ret = _vfprintf_r (ptr, &f, fmt, ap);
00115 va_end (ap);
00116 if (ret < EOF)
00117 ptr->_errno = EOVERFLOW;
00118 if (size > 0)
00119 *f._p = 0;
00120 return (ret);
00121 }
00122 #endif
00123
00124