xref: /xnu-10002.1.13/libsyscall/mach/string.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2010 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions 
29*1031c584SApple OSS Distributions #include "string.h"
30*1031c584SApple OSS Distributions 
31*1031c584SApple OSS Distributions static const char hex[] = "0123456789abcdef";
32*1031c584SApple OSS Distributions 
33*1031c584SApple OSS Distributions static int
_mach_strlen(const char * str)34*1031c584SApple OSS Distributions _mach_strlen(const char *str)
35*1031c584SApple OSS Distributions {
36*1031c584SApple OSS Distributions 	const char *p;
37*1031c584SApple OSS Distributions 	for (p = str; p; p++) {
38*1031c584SApple OSS Distributions 		if (*p == '\0') {
39*1031c584SApple OSS Distributions 			return (int)(p - str);
40*1031c584SApple OSS Distributions 		}
41*1031c584SApple OSS Distributions 	}
42*1031c584SApple OSS Distributions 	/* NOTREACHED */
43*1031c584SApple OSS Distributions 	return 0;
44*1031c584SApple OSS Distributions }
45*1031c584SApple OSS Distributions 
46*1031c584SApple OSS Distributions static void
_mach_hex(char ** buffer,int * length,unsigned long long n)47*1031c584SApple OSS Distributions _mach_hex(char **buffer, int *length, unsigned long long n)
48*1031c584SApple OSS Distributions {
49*1031c584SApple OSS Distributions 	char buf[32];
50*1031c584SApple OSS Distributions 	char *cp = buf + sizeof(buf);
51*1031c584SApple OSS Distributions 
52*1031c584SApple OSS Distributions 	if (n) {
53*1031c584SApple OSS Distributions 		*--cp = '\0';
54*1031c584SApple OSS Distributions 		while (n) {
55*1031c584SApple OSS Distributions 			*--cp = hex[n & 0xf];
56*1031c584SApple OSS Distributions 			n >>= 4;
57*1031c584SApple OSS Distributions 		}
58*1031c584SApple OSS Distributions 
59*1031c584SApple OSS Distributions 		int width = _mach_strlen(cp);
60*1031c584SApple OSS Distributions 		while (width > 0 && length > 0) {
61*1031c584SApple OSS Distributions 			*(*buffer)++ = *cp++;
62*1031c584SApple OSS Distributions 			(*length)--;
63*1031c584SApple OSS Distributions 			width--;
64*1031c584SApple OSS Distributions 		}
65*1031c584SApple OSS Distributions 	}
66*1031c584SApple OSS Distributions }
67*1031c584SApple OSS Distributions 
68*1031c584SApple OSS Distributions int
_mach_vsnprintf(char * buffer,int length,const char * fmt,va_list ap)69*1031c584SApple OSS Distributions _mach_vsnprintf(char *buffer, int length, const char *fmt, va_list ap)
70*1031c584SApple OSS Distributions {
71*1031c584SApple OSS Distributions 	int width, max = length;
72*1031c584SApple OSS Distributions 	char *out_ptr = buffer;
73*1031c584SApple OSS Distributions 
74*1031c584SApple OSS Distributions 	// we only ever write n-1 bytes so we can put a \0 at the end
75*1031c584SApple OSS Distributions 	length--;
76*1031c584SApple OSS Distributions 	while (length > 0 && *fmt) {
77*1031c584SApple OSS Distributions 		if (*fmt == '\0') {
78*1031c584SApple OSS Distributions 			break;
79*1031c584SApple OSS Distributions 		}
80*1031c584SApple OSS Distributions 		if (*fmt != '%') {
81*1031c584SApple OSS Distributions 			*(out_ptr++) = *(fmt++);
82*1031c584SApple OSS Distributions 			length--;
83*1031c584SApple OSS Distributions 			continue;
84*1031c584SApple OSS Distributions 		}
85*1031c584SApple OSS Distributions 		fmt++;
86*1031c584SApple OSS Distributions 		// only going to support a specific subset of sprintf flags
87*1031c584SApple OSS Distributions 		// namely %s, %x, with no padding modifiers
88*1031c584SApple OSS Distributions 		switch (*fmt++) {
89*1031c584SApple OSS Distributions 		case 's':
90*1031c584SApple OSS Distributions 		{
91*1031c584SApple OSS Distributions 			char *cp = va_arg(ap, char*);
92*1031c584SApple OSS Distributions 			width = _mach_strlen(cp);
93*1031c584SApple OSS Distributions 			while (width > 0 && length > 0) {
94*1031c584SApple OSS Distributions 				*(out_ptr++) = *(cp++);
95*1031c584SApple OSS Distributions 				width--;
96*1031c584SApple OSS Distributions 				length--;
97*1031c584SApple OSS Distributions 			}
98*1031c584SApple OSS Distributions 			break;
99*1031c584SApple OSS Distributions 		}
100*1031c584SApple OSS Distributions 		case 'x':
101*1031c584SApple OSS Distributions 		{
102*1031c584SApple OSS Distributions 			_mach_hex(&out_ptr, &length, va_arg(ap, unsigned int));
103*1031c584SApple OSS Distributions 			break;
104*1031c584SApple OSS Distributions 		}
105*1031c584SApple OSS Distributions 		}
106*1031c584SApple OSS Distributions 	}
107*1031c584SApple OSS Distributions 	if (max > 0) {
108*1031c584SApple OSS Distributions 		*out_ptr = '\0';
109*1031c584SApple OSS Distributions 	}
110*1031c584SApple OSS Distributions 	return max - (length + 1); /* don't include the final NULL in the return value */
111*1031c584SApple OSS Distributions }
112*1031c584SApple OSS Distributions 
113*1031c584SApple OSS Distributions int
_mach_snprintf(char * buffer,int length,const char * fmt,...)114*1031c584SApple OSS Distributions _mach_snprintf(char *buffer, int length, const char *fmt, ...)
115*1031c584SApple OSS Distributions {
116*1031c584SApple OSS Distributions 	int ret;
117*1031c584SApple OSS Distributions 	va_list ap;
118*1031c584SApple OSS Distributions 	va_start(ap, fmt);
119*1031c584SApple OSS Distributions 	ret = _mach_vsnprintf(buffer, length, fmt, ap);
120*1031c584SApple OSS Distributions 	va_end(ap);
121*1031c584SApple OSS Distributions 	return ret;
122*1031c584SApple OSS Distributions }
123