Cleanups of the GSSAPI support. On Windows, standard GSS libraries
[u/mdw/putty] / mac / vsnprint.c
1
2 #include <limits.h>
3 #include <stdarg.h>
4
5 #include "putty.h"
6
7 #define __P(decl) decl
8
9 /* XXX */
10 typedef unsigned long u_quad_t;
11 typedef long quad_t;
12
13 typedef unsigned long u_long;
14 typedef unsigned int u_int;
15 typedef unsigned short u_short;
16 typedef long intmax_t;
17 typedef unsigned long uintmax_t;
18 typedef long intptr_t;
19 typedef unsigned long uintptr_t;
20 typedef int ssize_t;
21
22 #define NBBY CHAR_BIT
23
24 /*-
25 * Copyright (c) 1986, 1988, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * From NetBSD: subr_prf.c,v 1.86 2002/11/02 07:25:22 perry Exp
58 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
59 */
60
61 /* flags for kprintf */
62 #define TOCONS 0x01 /* to the console */
63 #define TOTTY 0x02 /* to the process' tty */
64 #define TOLOG 0x04 /* to the kernel message buffer */
65 #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */
66 #define TODDB 0x10 /* to ddb console */
67
68 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
69 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
70
71
72 /*
73 * local prototypes
74 */
75
76 static int kprintf __P((const char *, int, void *,
77 char *, va_list));
78
79 /*
80 * vsnprintf: print a message to a buffer [already have va_alist]
81 */
82 int
83 vsnprintf(buf, size, fmt, ap)
84 char *buf;
85 size_t size;
86 const char *fmt;
87 va_list ap;
88 {
89 int retval;
90 char *p;
91
92 if (size < 1)
93 return (-1);
94 p = buf + size - 1;
95 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
96 *(p) = 0; /* null terminate */
97 return(retval);
98 }
99
100
101 /*
102 * kprintf: scaled down version of printf(3).
103 *
104 * this version based on vfprintf() from libc which was derived from
105 * software contributed to Berkeley by Chris Torek.
106 *
107 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
108 */
109
110 /*
111 * macros for converting digits to letters and vice versa
112 */
113 #define to_digit(c) ((c) - '0')
114 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
115 #define to_char(n) ((n) + '0')
116
117 /*
118 * flags used during conversion.
119 */
120 #define ALT 0x001 /* alternate form */
121 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
122 #define LADJUST 0x004 /* left adjustment */
123 #define LONGDBL 0x008 /* long double; unimplemented */
124 #define LONGINT 0x010 /* long integer */
125 #define QUADINT 0x020 /* quad integer */
126 #define SHORTINT 0x040 /* short integer */
127 #define MAXINT 0x080 /* intmax_t */
128 #define PTRINT 0x100 /* intptr_t */
129 #define SIZEINT 0x200 /* size_t */
130 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
131 #define FPT 0x800 /* Floating point number */
132
133 /*
134 * To extend shorts properly, we need both signed and unsigned
135 * argument extraction methods.
136 */
137 #define SARG() \
138 (flags&MAXINT ? va_arg(ap, intmax_t) : \
139 flags&PTRINT ? va_arg(ap, intptr_t) : \
140 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
141 flags&QUADINT ? va_arg(ap, quad_t) : \
142 flags&LONGINT ? va_arg(ap, long) : \
143 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
144 (long)va_arg(ap, int))
145 #define UARG() \
146 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
147 flags&PTRINT ? va_arg(ap, uintptr_t) : \
148 flags&SIZEINT ? va_arg(ap, size_t) : \
149 flags&QUADINT ? va_arg(ap, u_quad_t) : \
150 flags&LONGINT ? va_arg(ap, u_long) : \
151 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
152 (u_long)va_arg(ap, u_int))
153
154 #define KPRINTF_PUTCHAR(C) { \
155 if (oflags == TOBUFONLY) { \
156 if ((vp != NULL) && (sbuf == tailp)) { \
157 ret += 1; /* indicate error */ \
158 goto overflow; \
159 } \
160 *sbuf++ = (C); \
161 } \
162 }
163
164 /*
165 * Guts of kernel printf. Note, we already expect to be in a mutex!
166 */
167 static int
168 kprintf(fmt0, oflags, vp, sbuf, ap)
169 const char *fmt0;
170 int oflags;
171 void *vp;
172 char *sbuf;
173 va_list ap;
174 {
175 char *fmt; /* format string */
176 int ch; /* character from fmt */
177 int n; /* handy integer (short term usage) */
178 char *cp; /* handy char pointer (short term usage) */
179 int flags; /* flags as above */
180 int ret; /* return value accumulator */
181 int width; /* width from format (%8d), or 0 */
182 int prec; /* precision from format (%.3d), or -1 */
183 char sign; /* sign prefix (' ', '+', '-', or \0) */
184
185 u_quad_t _uquad; /* integer arguments %[diouxX] */
186 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
187 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
188 int realsz; /* field size expanded by dprec */
189 int size; /* size of converted field or string */
190 char *xdigs; /* digits for [xX] conversion */
191 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
192 char *tailp; /* tail pointer for snprintf */
193
194 tailp = NULL; /* XXX: shutup gcc */
195 if (oflags == TOBUFONLY && (vp != NULL))
196 tailp = *(char **)vp;
197
198 cp = NULL; /* XXX: shutup gcc */
199 size = 0; /* XXX: shutup gcc */
200
201 fmt = (char *)fmt0;
202 ret = 0;
203
204 xdigs = NULL; /* XXX: shut up gcc warning */
205
206 /*
207 * Scan the format for conversions (`%' character).
208 */
209 for (;;) {
210 while (*fmt != '%' && *fmt) {
211 ret++;
212 KPRINTF_PUTCHAR(*fmt++);
213 }
214 if (*fmt == 0)
215 goto done;
216
217 fmt++; /* skip over '%' */
218
219 flags = 0;
220 dprec = 0;
221 width = 0;
222 prec = -1;
223 sign = '\0';
224
225 rflag: ch = *fmt++;
226 reswitch: switch (ch) {
227 case ' ':
228 /*
229 * ``If the space and + flags both appear, the space
230 * flag will be ignored.''
231 * -- ANSI X3J11
232 */
233 if (!sign)
234 sign = ' ';
235 goto rflag;
236 case '#':
237 flags |= ALT;
238 goto rflag;
239 case '*':
240 /*
241 * ``A negative field width argument is taken as a
242 * - flag followed by a positive field width.''
243 * -- ANSI X3J11
244 * They don't exclude field widths read from args.
245 */
246 if ((width = va_arg(ap, int)) >= 0)
247 goto rflag;
248 width = -width;
249 /* FALLTHROUGH */
250 case '-':
251 flags |= LADJUST;
252 goto rflag;
253 case '+':
254 sign = '+';
255 goto rflag;
256 case '.':
257 if ((ch = *fmt++) == '*') {
258 n = va_arg(ap, int);
259 prec = n < 0 ? -1 : n;
260 goto rflag;
261 }
262 n = 0;
263 while (is_digit(ch)) {
264 n = 10 * n + to_digit(ch);
265 ch = *fmt++;
266 }
267 prec = n < 0 ? -1 : n;
268 goto reswitch;
269 case '0':
270 /*
271 * ``Note that 0 is taken as a flag, not as the
272 * beginning of a field width.''
273 * -- ANSI X3J11
274 */
275 flags |= ZEROPAD;
276 goto rflag;
277 case '1': case '2': case '3': case '4':
278 case '5': case '6': case '7': case '8': case '9':
279 n = 0;
280 do {
281 n = 10 * n + to_digit(ch);
282 ch = *fmt++;
283 } while (is_digit(ch));
284 width = n;
285 goto reswitch;
286 case 'h':
287 flags |= SHORTINT;
288 goto rflag;
289 case 'j':
290 flags |= MAXINT;
291 goto rflag;
292 case 'l':
293 if (*fmt == 'l') {
294 fmt++;
295 flags |= QUADINT;
296 } else {
297 flags |= LONGINT;
298 }
299 goto rflag;
300 case 'q':
301 flags |= QUADINT;
302 goto rflag;
303 case 't':
304 flags |= PTRINT;
305 goto rflag;
306 case 'z':
307 flags |= SIZEINT;
308 goto rflag;
309 case 'c':
310 *(cp = buf) = va_arg(ap, int);
311 size = 1;
312 sign = '\0';
313 break;
314 case 'D':
315 flags |= LONGINT;
316 /*FALLTHROUGH*/
317 case 'd':
318 case 'i':
319 _uquad = SARG();
320 if ((quad_t)_uquad < 0) {
321 _uquad = -_uquad;
322 sign = '-';
323 }
324 base = DEC;
325 goto number;
326 case 'n':
327 if (flags & MAXINT)
328 *va_arg(ap, intmax_t *) = ret;
329 else if (flags & PTRINT)
330 *va_arg(ap, intptr_t *) = ret;
331 else if (flags & SIZEINT)
332 *va_arg(ap, ssize_t *) = ret;
333 else if (flags & QUADINT)
334 *va_arg(ap, quad_t *) = ret;
335 else if (flags & LONGINT)
336 *va_arg(ap, long *) = ret;
337 else if (flags & SHORTINT)
338 *va_arg(ap, short *) = ret;
339 else
340 *va_arg(ap, int *) = ret;
341 continue; /* no output */
342 case 'O':
343 flags |= LONGINT;
344 /*FALLTHROUGH*/
345 case 'o':
346 _uquad = UARG();
347 base = OCT;
348 goto nosign;
349 case 'p':
350 /*
351 * ``The argument shall be a pointer to void. The
352 * value of the pointer is converted to a sequence
353 * of printable characters, in an implementation-
354 * defined manner.''
355 * -- ANSI X3J11
356 */
357 /* NOSTRICT */
358 _uquad = (u_long)va_arg(ap, void *);
359 base = HEX;
360 xdigs = "0123456789abcdef";
361 flags |= HEXPREFIX;
362 ch = 'x';
363 goto nosign;
364 case 's':
365 if ((cp = va_arg(ap, char *)) == NULL)
366 cp = "(null)";
367 if (prec >= 0) {
368 /*
369 * can't use strlen; can only look for the
370 * NUL in the first `prec' characters, and
371 * strlen() will go further.
372 */
373 char *p = memchr(cp, 0, prec);
374
375 if (p != NULL) {
376 size = p - cp;
377 if (size > prec)
378 size = prec;
379 } else
380 size = prec;
381 } else
382 size = strlen(cp);
383 sign = '\0';
384 break;
385 case 'U':
386 flags |= LONGINT;
387 /*FALLTHROUGH*/
388 case 'u':
389 _uquad = UARG();
390 base = DEC;
391 goto nosign;
392 case 'X':
393 xdigs = "0123456789ABCDEF";
394 goto hex;
395 case 'x':
396 xdigs = "0123456789abcdef";
397 hex: _uquad = UARG();
398 base = HEX;
399 /* leading 0x/X only if non-zero */
400 if (flags & ALT && _uquad != 0)
401 flags |= HEXPREFIX;
402
403 /* unsigned conversions */
404 nosign: sign = '\0';
405 /*
406 * ``... diouXx conversions ... if a precision is
407 * specified, the 0 flag will be ignored.''
408 * -- ANSI X3J11
409 */
410 number: if ((dprec = prec) >= 0)
411 flags &= ~ZEROPAD;
412
413 /*
414 * ``The result of converting a zero value with an
415 * explicit precision of zero is no characters.''
416 * -- ANSI X3J11
417 */
418 cp = buf + KPRINTF_BUFSIZE;
419 if (_uquad != 0 || prec != 0) {
420 /*
421 * Unsigned mod is hard, and unsigned mod
422 * by a constant is easier than that by
423 * a variable; hence this switch.
424 */
425 switch (base) {
426 case OCT:
427 do {
428 *--cp = to_char(_uquad & 7);
429 _uquad >>= 3;
430 } while (_uquad);
431 /* handle octal leading 0 */
432 if (flags & ALT && *cp != '0')
433 *--cp = '0';
434 break;
435
436 case DEC:
437 /* many numbers are 1 digit */
438 while (_uquad >= 10) {
439 *--cp = to_char(_uquad % 10);
440 _uquad /= 10;
441 }
442 *--cp = to_char(_uquad);
443 break;
444
445 case HEX:
446 do {
447 *--cp = xdigs[_uquad & 15];
448 _uquad >>= 4;
449 } while (_uquad);
450 break;
451
452 default:
453 cp = "bug in kprintf: bad base";
454 size = strlen(cp);
455 goto skipsize;
456 }
457 }
458 size = buf + KPRINTF_BUFSIZE - cp;
459 skipsize:
460 break;
461 default: /* "%?" prints ?, unless ? is NUL */
462 if (ch == '\0')
463 goto done;
464 /* pretend it was %c with argument ch */
465 cp = buf;
466 *cp = ch;
467 size = 1;
468 sign = '\0';
469 break;
470 }
471
472 /*
473 * All reasonable formats wind up here. At this point, `cp'
474 * points to a string which (if not flags&LADJUST) should be
475 * padded out to `width' places. If flags&ZEROPAD, it should
476 * first be prefixed by any sign or other prefix; otherwise,
477 * it should be blank padded before the prefix is emitted.
478 * After any left-hand padding and prefixing, emit zeroes
479 * required by a decimal [diouxX] precision, then print the
480 * string proper, then emit zeroes required by any leftover
481 * floating precision; finally, if LADJUST, pad with blanks.
482 *
483 * Compute actual size, so we know how much to pad.
484 * size excludes decimal prec; realsz includes it.
485 */
486 realsz = dprec > size ? dprec : size;
487 if (sign)
488 realsz++;
489 else if (flags & HEXPREFIX)
490 realsz+= 2;
491
492 /* adjust ret */
493 ret += width > realsz ? width : realsz;
494
495 /* right-adjusting blank padding */
496 if ((flags & (LADJUST|ZEROPAD)) == 0) {
497 n = width - realsz;
498 while (n-- > 0)
499 KPRINTF_PUTCHAR(' ');
500 }
501
502 /* prefix */
503 if (sign) {
504 KPRINTF_PUTCHAR(sign);
505 } else if (flags & HEXPREFIX) {
506 KPRINTF_PUTCHAR('0');
507 KPRINTF_PUTCHAR(ch);
508 }
509
510 /* right-adjusting zero padding */
511 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
512 n = width - realsz;
513 while (n-- > 0)
514 KPRINTF_PUTCHAR('0');
515 }
516
517 /* leading zeroes from decimal precision */
518 n = dprec - size;
519 while (n-- > 0)
520 KPRINTF_PUTCHAR('0');
521
522 /* the string or number proper */
523 while (size--)
524 KPRINTF_PUTCHAR(*cp++);
525 /* left-adjusting padding (always blank) */
526 if (flags & LADJUST) {
527 n = width - realsz;
528 while (n-- > 0)
529 KPRINTF_PUTCHAR(' ');
530 }
531 }
532
533 done:
534 if ((oflags == TOBUFONLY) && (vp != NULL))
535 *(char **)vp = sbuf;
536 overflow:
537 return (ret);
538 /* NOTREACHED */
539 }