Loading...
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 | /* * Copyright (c) 2005 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <string.h> #include <stdarg.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> /* we use a small buffer to minimize stack usage constraints */ #define MYBUFSIZE 32 typedef struct { char buf[MYBUFSIZE]; char *ptr; char *end; int fd; } BUF; /* flush the buffer and reset the pointer */ static inline void flush(BUF *b) { char *buf = b->buf; int n = b->ptr - buf; int w; while(n > 0) { w = write(b->fd, buf, n); if(w < 0) { if(errno == EINTR || errno == EAGAIN) continue; break; } n -= w; buf += n; } b->ptr = b->buf; } /* output a single character */ static inline void put_c(BUF *b, int c) { if(b->ptr >= b->end) flush(b); *b->ptr++ = c; } /* output a null-terminated string */ static inline void put_s(BUF *b, const char *str) { while(*str) put_c(b, *str++); } /* output a string of the specified size */ static inline void put_n(BUF *b, const char *str, int n) { while(n-- > 0) put_c(b, *str++); } /* * Output the signed decimal string representing the number in "in". "width" is * the minimum field width, and "zero" is a boolean value, true for zero padding * (otherwise blank padding). */ static void dec(BUF *b, long long in, int width, int zero) { char buf[32]; char *cp = buf + sizeof(buf); int pad; int neg = 0; unsigned long long n = (unsigned long long)in; if(in < 0) { neg++; width--; n = ~n + 1; } *--cp = 0; if(n) { while(n) { *--cp = (n % 10) + '0'; n /= 10; } } else *--cp = '0'; if(neg && zero) { put_c(b, '-'); neg = 0; } pad = width - strlen(cp); zero = zero ? '0' : ' '; while(pad-- > 0) put_c(b, zero); if(neg) put_c(b, '-'); put_s(b, cp); } /* * Output the hex string representing the number in "i". "width" is the * minimum field width, and "zero" is a boolean value, true for zero padding * (otherwise blank padding). "upper" is a boolean value, true for upper * case hex characters, lower case otherwise. "p" is a boolean value, true * if 0x should be prepended (for %p), otherwise nothing. */ static char _h[] = "0123456789abcdef"; static char _H[] = "0123456789ABCDEF"; static char _0x[] = "0x"; static void hex(BUF *b, unsigned long long n, int width, int zero, int upper, int p) { char buf[32]; char *cp = buf + sizeof(buf); char *h = upper ? _H : _h; *--cp = 0; if(n) { while(n) { *--cp = h[n & 0xf]; n >>= 4; } } else *--cp = '0'; if(p) { width -= 2; if(zero) { put_s(b, _0x); p = 0; } } width -= strlen(cp); zero = zero ? '0' : ' '; while(width-- > 0) put_c(b, zero); if(p) put_s(b, _0x); put_s(b, cp); } /* * Output the unsigned decimal string representing the number in "in". "width" * is the minimum field width, and "zero" is a boolean value, true for zero * padding (otherwise blank padding). */ static void udec(BUF *b, unsigned long long n, int width, int zero) { char buf[32]; char *cp = buf + sizeof(buf); int pad; *--cp = 0; if(n) { while(n) { *--cp = (n % 10) + '0'; n /= 10; } } else *--cp = '0'; pad = width - strlen(cp); zero = zero ? '0' : ' '; while(pad-- > 0) put_c(b, zero); put_s(b, cp); } /* * A simplified vfprintf variant. The format string is interpreted with * arguments for the va_list, and the results are written to the given * file descriptor. */ void _simple_vdprintf(int fd, const char *fmt, va_list ap) { BUF b; b.fd = fd; b.ptr = b.buf; b.end = b.buf + MYBUFSIZE; while(*fmt) { int lflag, zero, width; char *cp; if(!(cp = strchr(fmt, '%'))) { put_s(&b, fmt); break; } put_n(&b, fmt, cp - fmt); fmt = cp + 1; if(*fmt == '%') { put_c(&b, '%'); fmt++; continue; } lflag = zero = width = 0; for(;;) { switch(*fmt) { case '0': zero++; fmt++; /* drop through */ case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': while(*fmt >= '0' && *fmt <= '9') width = 10 * width + (*fmt++ - '0'); continue; case 'c': zero = zero ? '0' : ' '; width--; while(width-- > 0) put_c(&b, zero); put_c(&b, va_arg(ap, int)); break; case 'd': case 'i': switch(lflag) { case 0: dec(&b, va_arg(ap, int), width, zero); break; case 1: dec(&b, va_arg(ap, long), width, zero); break; default: dec(&b, va_arg(ap, long long), width, zero); break; } break; case 'l': lflag++; fmt++; continue; case 'p': hex(&b, (unsigned long)va_arg(ap, void *), width, zero, 0, 1); break; case 's': cp = va_arg(ap, char *); width -= strlen(cp); zero = zero ? '0' : ' '; while(width-- > 0) put_c(&b, zero); put_s(&b, cp); break; case 'u': switch(lflag) { case 0: udec(&b, va_arg(ap, unsigned int), width, zero); break; case 1: udec(&b, va_arg(ap, unsigned long), width, zero); break; default: udec(&b, va_arg(ap, unsigned long long), width, zero); break; } break; case 'X': case 'x': switch(lflag) { case 0: hex(&b, va_arg(ap, unsigned int), width, zero, *fmt == 'X', 0); break; case 1: hex(&b, va_arg(ap, unsigned long), width, zero, *fmt == 'X', 0); break; default: hex(&b, va_arg(ap, unsigned long long), width, zero, *fmt == 'X', 0); break; } break; default: put_c(&b, *fmt); break; } break; } fmt++; } flush(&b); } /* * A simplified fprintf variant. The format string is interpreted with * arguments for the variable argument list, and the results are written * to the given file descriptor. */ void _simple_dprintf(int fd, const char *format, ...) { va_list ap; va_start(ap, format); _simple_vdprintf(fd, format, ap); va_end(ap); } |