]>
git.saurik.com Git - apple/xnu.git/blob - libsyscall/mach/string.c
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 static const char hex
[] = "0123456789abcdef";
34 _mach_strlen(const char *str
)
37 for (p
= str
; p
; p
++) {
39 return (int)(p
- str
);
47 _mach_hex(char **buffer
, int *length
, unsigned long long n
)
50 char *cp
= buf
+ sizeof(buf
);
59 int width
= _mach_strlen(cp
);
60 while (width
> 0 && length
> 0) {
69 _mach_vsnprintf(char *buffer
, int length
, const char *fmt
, va_list ap
)
71 int width
, max
= length
;
72 char *out_ptr
= buffer
;
74 // we only ever write n-1 bytes so we can put a \0 at the end
76 while (length
> 0 && *fmt
) {
81 *(out_ptr
++) = *(fmt
++);
86 // only going to support a specific subset of sprintf flags
87 // namely %s, %x, with no padding modifiers
91 char *cp
= va_arg(ap
, char*);
92 width
= _mach_strlen(cp
);
93 while (width
> 0 && length
> 0) {
94 *(out_ptr
++) = *(cp
++);
102 _mach_hex(&out_ptr
, &length
, va_arg(ap
, unsigned int));
112 _mach_snprintf(char *buffer
, int length
, const char *fmt
, ...)
117 ret
= _mach_vsnprintf(buffer
, length
, fmt
, ap
);