]>
git.saurik.com Git - apple/libc.git/blob - gen/simple_dprintf.c
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
29 /* we use a small buffer to minimize stack usage constraints */
39 /* flush the buffer and reset the pointer */
48 w
= write(b
->fd
, buf
, n
);
50 if(errno
== EINTR
|| errno
== EAGAIN
)
60 /* output a single character */
69 /* output a null-terminated string */
71 put_s(BUF
*b
, const char *str
)
77 /* output a string of the specified size */
79 put_n(BUF
*b
, const char *str
, int n
)
86 * Output the signed decimal string representing the number in "in". "width" is
87 * the minimum field width, and "zero" is a boolean value, true for zero padding
88 * (otherwise blank padding).
91 dec(BUF
*b
, long long in
, int width
, int zero
)
94 char *cp
= buf
+ sizeof(buf
);
97 unsigned long long n
= (unsigned long long)in
;
107 *--cp
= (n
% 10) + '0';
116 pad
= width
- strlen(cp
);
117 zero
= zero
? '0' : ' ';
126 * Output the hex string representing the number in "i". "width" is the
127 * minimum field width, and "zero" is a boolean value, true for zero padding
128 * (otherwise blank padding). "upper" is a boolean value, true for upper
129 * case hex characters, lower case otherwise. "p" is a boolean value, true
130 * if 0x should be prepended (for %p), otherwise nothing.
132 static char _h
[] = "0123456789abcdef";
133 static char _H
[] = "0123456789ABCDEF";
134 static char _0x
[] = "0x";
137 hex(BUF
*b
, unsigned long long n
, int width
, int zero
, int upper
, int p
)
140 char *cp
= buf
+ sizeof(buf
);
141 char *h
= upper
? _H
: _h
;
159 zero
= zero
? '0' : ' ';
168 * Output the unsigned decimal string representing the number in "in". "width"
169 * is the minimum field width, and "zero" is a boolean value, true for zero
170 * padding (otherwise blank padding).
173 udec(BUF
*b
, unsigned long long n
, int width
, int zero
)
176 char *cp
= buf
+ sizeof(buf
);
182 *--cp
= (n
% 10) + '0';
187 pad
= width
- strlen(cp
);
188 zero
= zero
? '0' : ' ';
195 * A simplified vfprintf variant. The format string is interpreted with
196 * arguments for the va_list, and the results are written to the given
200 _simple_vdprintf(int fd
, const char *fmt
, va_list ap
)
206 b
.end
= b
.buf
+ MYBUFSIZE
;
208 int lflag
, zero
, width
;
210 if(!(cp
= strchr(fmt
, '%'))) {
214 put_n(&b
, fmt
, cp
- fmt
);
221 lflag
= zero
= width
= 0;
228 case '1': case '2': case '3': case '4': case '5':
229 case '6': case '7': case '8': case '9':
230 while(*fmt
>= '0' && *fmt
<= '9')
231 width
= 10 * width
+ (*fmt
++ - '0');
234 zero
= zero
? '0' : ' ';
238 put_c(&b
, va_arg(ap
, int));
243 dec(&b
, va_arg(ap
, int), width
, zero
);
246 dec(&b
, va_arg(ap
, long), width
, zero
);
249 dec(&b
, va_arg(ap
, long long), width
, zero
);
258 hex(&b
, (unsigned long)va_arg(ap
, void *), width
, zero
, 0, 1);
261 cp
= va_arg(ap
, char *);
263 zero
= zero
? '0' : ' ';
271 udec(&b
, va_arg(ap
, unsigned int), width
, zero
);
274 udec(&b
, va_arg(ap
, unsigned long), width
, zero
);
277 udec(&b
, va_arg(ap
, unsigned long long), width
, zero
);
284 hex(&b
, va_arg(ap
, unsigned int), width
, zero
,
288 hex(&b
, va_arg(ap
, unsigned long), width
, zero
,
292 hex(&b
, va_arg(ap
, unsigned long long), width
, zero
,
309 * A simplified fprintf variant. The format string is interpreted with
310 * arguments for the variable argument list, and the results are written
311 * to the given file descriptor.
314 _simple_dprintf(int fd
, const char *format
, ...)
318 va_start(ap
, format
);
319 _simple_vdprintf(fd
, format
, ap
);