1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*
This file was taken from the proftpd package (www.proftpd.net) and
adapted for use in the Posadis Domain Name Server.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
330, Boston, MA 02111-1307, USA. */
#include <sys/types.h>
#include <ctype.h>
#include <stdarg.h>
#include "dibbler-config.h"
#ifndef HAVE_VSNPRINTF
static size_t strnlen(const char *s, size_t count)
{
const char *sc;
for(sc = s; count-- && *sc != '\0'; ++sc) ;
return sc - s;
}
static int skip_atoi(const char **s)
{
int i = 0;
while(isdigit(**s))
i = i * 10 + *((*s)++) - '0';
return i;
}
#define ZEROPAD 1
#define SIGN 2
#define PLUS 4
#define SPACE 8
#define LEFT 16
#define SPECIAL 32
#define LARGE 64
static char *number(char *str, long num, int base, int size, int
precision, int type, size_t *max_size)
{
char c,sign,tmp[66] = {'\0'};
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
int i;
size_t msize;
msize = *max_size;
if(type & LARGE)
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(type & LEFT)
type &= ~ZEROPAD;
if(base < 2 || base > 36)
return 0;
c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if(type & SIGN) {
if(num < 0) {
sign = '-';
num = -num;
size--;
} else if(type & PLUS) {
sign = '+';
size--;
} else if(type & SPACE) {
sign = ' ';
size--;
}
}
if(type & SPECIAL) {
if(base == 16)
size -= 2;
else if(base == 8)
size--;
}
i = 0;
if(num == 0)
tmp[i++] = '0';
else while(num != 0) {
tmp[i++] = digits[((unsigned long) num) % (unsigned) base];
num /= base;
}
if(i > precision)
precision = i;
size -= precision;
if(!(type & (ZEROPAD+LEFT)))
while(size-- > 0 && msize) {
*str++ = ' ';
msize--;
}
if(sign && msize)
{ *str++ = sign; msize--; }
if(msize) {
if(type & SPECIAL)
if(base == 8)
{ *str++ = '0'; msize--; }
else if(base == 16) {
*str++ = '0'; msize--;
if(msize)
{ *str++ = digits[33]; msize--; }
}
}
if(!(type & LEFT))
while(size-- > 0 && msize)
{ *str++ = c; msize--; }
while(i < precision-- && msize)
{ *str++ = '0'; msize--; }
while(i-- > 0 && msize)
{ *str++ = tmp[i]; msize--; }
while(size-- > 0 && msize)
{ *str++ = ' '; msize--; }
*max_size = msize;
return str;
}
/*
** This vsnprintf() emulation does not implement the conversions:
** %e, %E, %g, %G, %wc, %ws
** The %f implementation is limited.
*/
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
int len;
unsigned long num;
int i, base;
char *str;
const char *s;
int flags;
int dotflag;
int field_width;
int precision;
int qualifier;
size--;
for(str = buf; *fmt && size; ++fmt) {
if(*fmt != '%') {
*str++ = *fmt;
size--;
continue;
}
flags = 0;
dotflag = 0;
repeat:
++fmt;
switch(*fmt) {
case '-': flags |= LEFT; goto repeat;
case '+': flags |= PLUS; goto repeat;
case ' ': flags |= SPACE; goto repeat;
case '#': flags |= SPECIAL; goto repeat;
case '0': flags |= ZEROPAD; goto repeat;
}
field_width = -1;
if(isdigit(*fmt))
field_width = skip_atoi(&fmt);
else if(*fmt == '*') {
++fmt;
field_width = va_arg(args,int);
if(field_width < 0) {
field_width = - field_width;
flags |= LEFT;
}
}
precision = -1;
if(*fmt == '.') {
dotflag++;
++fmt;
if(isdigit(*fmt))
precision = skip_atoi(&fmt);
else if(*fmt == '*') {
++fmt;
precision = va_arg(args,int);
}
/* NB: the default precision value is conversion dependent */
}
qualifier = -1;
if(*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
qualifier = *fmt;
++fmt;
}
base = 10;
switch(*fmt) {
case 'c':
if(!(flags & LEFT))
while(--field_width > 0 && size)
{ *str++ = ' '; size--; }
if(size)
{ *str++ = (unsigned char)va_arg(args,int); size--; }
while(--field_width > 0 && size)
{ *str++ = ' '; size--; }
continue;
case 's':
if ( dotflag && precision < 0 )
precision = 0;
s = va_arg(args,char*);
if(!s)
s = "(null)";
len = strnlen(s, precision);
if(!(flags & LEFT))
while(len < field_width-- && size) {
*str++ = ' ';
size--;
}
for(i = 0; i < len && size; ++i) {
*str++ = *s++;
size--;
}
while(len < field_width-- && size) {
*str++ = ' ';
size--;
}
continue;
case 'p':
if ( dotflag && precision < 0 )
precision = 0;
if(field_width == -1) {
field_width = 2 * sizeof(void*);
flags |= ZEROPAD;
}
str = number(str,
(unsigned long)va_arg(args,void*),16,
field_width, precision, flags, &size);
continue;
case 'n':
if(qualifier == 'l') {
long *ip = va_arg(args,long*);
*ip = (str - buf);
} else {
int *ip = va_arg(args,int*);
*ip = (str - buf);
}
continue;
case 'o':
base = 8;
break;
case 'X':
flags |= LARGE;
case 'x':
base = 16;
break;
case 'd':
case 'i':
flags |= SIGN;
case 'u':
break;
default:
if(*fmt != '%')
*str++ = '%';
if(*fmt && size)
{ *str++ = *fmt; size--; }
else
--fmt;
continue;
}
if(qualifier == 'l')
num = va_arg(args,unsigned long);
else if(qualifier == 'h') {
if(flags & SIGN)
num = (short)va_arg(args, int);
else
num = (unsigned short)va_arg(args,unsigned);
} else if(flags & SIGN)
num = va_arg(args,int);
else
num = va_arg(args, unsigned int);
if ( dotflag && precision < 0 )
precision = 0;
str = number(str,num,base,field_width,precision,flags,&size);
}
*str = '\0';
return str - buf;
}
#ifndef HAVE_SNPRINTF
int snprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
int i;
va_start(args,fmt);
i = vsnprintf(buf,size,fmt,args);
va_end(args);
return i;
}
#endif /* HAVE_SNPRINTF */
#else
int foo_bar_baz() { return 0; }
#endif /* HAVE_VSNPRINTF */
|