]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/mach/string.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libsyscall / mach / string.c
CommitLineData
6d2010ae
A
1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
6d2010ae
A
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.
0a7de745 14 *
6d2010ae
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
6d2010ae
A
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.
0a7de745 25 *
6d2010ae
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include "string.h"
30
31static const char hex[] = "0123456789abcdef";
32
33static int
34_mach_strlen(const char *str)
35{
36 const char *p;
37 for (p = str; p; p++) {
38 if (*p == '\0') {
fe8ab488 39 return (int)(p - str);
6d2010ae
A
40 }
41 }
42 /* NOTREACHED */
43 return 0;
44}
45
46static void
47_mach_hex(char **buffer, int *length, unsigned long long n)
48{
49 char buf[32];
50 char *cp = buf + sizeof(buf);
0a7de745 51
6d2010ae
A
52 if (n) {
53 *--cp = '\0';
54 while (n) {
55 *--cp = hex[n & 0xf];
56 n >>= 4;
57 }
0a7de745 58
6d2010ae
A
59 int width = _mach_strlen(cp);
60 while (width > 0 && length > 0) {
61 *(*buffer)++ = *cp++;
62 (*length)--;
63 width--;
64 }
65 }
66}
67
68int
69_mach_vsnprintf(char *buffer, int length, const char *fmt, va_list ap)
70{
71 int width, max = length;
72 char *out_ptr = buffer;
0a7de745 73
6d2010ae
A
74 // we only ever write n-1 bytes so we can put a \0 at the end
75 length--;
76 while (length > 0 && *fmt) {
77 if (*fmt == '\0') {
78 break;
79 }
80 if (*fmt != '%') {
81 *(out_ptr++) = *(fmt++);
82 length--;
83 continue;
84 }
85 fmt++;
86 // only going to support a specific subset of sprintf flags
87 // namely %s, %x, with no padding modifiers
88 switch (*fmt++) {
0a7de745
A
89 case 's':
90 {
91 char *cp = va_arg(ap, char*);
92 width = _mach_strlen(cp);
93 while (width > 0 && length > 0) {
94 *(out_ptr++) = *(cp++);
95 width--;
96 length--;
6d2010ae 97 }
0a7de745
A
98 break;
99 }
100 case 'x':
101 {
102 _mach_hex(&out_ptr, &length, va_arg(ap, unsigned int));
103 break;
104 }
6d2010ae
A
105 }
106 }
0a7de745 107 if (max > 0) {
39037602 108 *out_ptr = '\0';
0a7de745 109 }
39037602 110 return max - (length + 1); /* don't include the final NULL in the return value */
6d2010ae
A
111}
112
113int
114_mach_snprintf(char *buffer, int length, const char *fmt, ...)
115{
116 int ret;
117 va_list ap;
118 va_start(ap, fmt);
119 ret = _mach_vsnprintf(buffer, length, fmt, ap);
120 va_end(ap);
121 return ret;
122}