xref: /titanic_53/usr/src/uts/common/os/strext.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * SunOS-specific extensions to libc's standard set of string routines.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * NOTE: The standard libc string routines are in $SRC/common/util/string.c,
38*7c478bd9Sstevel@tonic-gate  * to facilitate sharing with standalone.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  * Historical entry point: remove in Solaris 2.8.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate char *
45*7c478bd9Sstevel@tonic-gate vsprintf_len(size_t buflen, char *buf, const char *fmt, va_list args)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, buflen, fmt, args);
48*7c478bd9Sstevel@tonic-gate 	return (buf);
49*7c478bd9Sstevel@tonic-gate }
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * Historical entry point: remove in Solaris 2.8.
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
55*7c478bd9Sstevel@tonic-gate char *
56*7c478bd9Sstevel@tonic-gate sprintf_len(size_t buflen, char *buf, const char *fmt, ...)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	va_list args;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
61*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, buflen, fmt, args);
62*7c478bd9Sstevel@tonic-gate 	va_end(args);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	return (buf);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * Simple-minded conversion of a long into a null-terminated character
69*7c478bd9Sstevel@tonic-gate  * string.  Caller must ensure there's enough space to hold the result.
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate void
72*7c478bd9Sstevel@tonic-gate numtos(unsigned long num, char *s)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	char prbuf[40];
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	char *cp = prbuf;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	do {
79*7c478bd9Sstevel@tonic-gate 		*cp++ = "0123456789"[num % 10];
80*7c478bd9Sstevel@tonic-gate 		num /= 10;
81*7c478bd9Sstevel@tonic-gate 	} while (num);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	do {
84*7c478bd9Sstevel@tonic-gate 		*s++ = *--cp;
85*7c478bd9Sstevel@tonic-gate 	} while (cp > prbuf);
86*7c478bd9Sstevel@tonic-gate 	*s = '\0';
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate /*
90*7c478bd9Sstevel@tonic-gate  * Returns the integer value of the string of decimal numeric
91*7c478bd9Sstevel@tonic-gate  * chars beginning at **str.  Does no overflow checking.
92*7c478bd9Sstevel@tonic-gate  * Note: updates *str to point at the last character examined.
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate int
95*7c478bd9Sstevel@tonic-gate stoi(char **str)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	char	*p = *str;
98*7c478bd9Sstevel@tonic-gate 	int	n;
99*7c478bd9Sstevel@tonic-gate 	int	c;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	for (n = 0; (c = *p) >= '0' && c <= '9'; p++) {
102*7c478bd9Sstevel@tonic-gate 		n = n * 10 + c - '0';
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 	*str = p;
105*7c478bd9Sstevel@tonic-gate 	return (n);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /*
109*7c478bd9Sstevel@tonic-gate  * Like strrchr(), except
110*7c478bd9Sstevel@tonic-gate  * (a) it takes a maximum length for the string to be searched, and
111*7c478bd9Sstevel@tonic-gate  * (b) if the string ends with a null, it is not considered part of the string.
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate char *
114*7c478bd9Sstevel@tonic-gate strnrchr(const char *sp, int c, size_t n)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	const char *r = 0;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	while (n-- > 0 && *sp) {
119*7c478bd9Sstevel@tonic-gate 		if (*sp == c)
120*7c478bd9Sstevel@tonic-gate 			r = sp;
121*7c478bd9Sstevel@tonic-gate 		sp++;
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	return ((char *)r);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * NOTE: These routines aren't shared with standalone because the DDI mandates
129*7c478bd9Sstevel@tonic-gate  *	 that they return the buffer rather than its length.
130*7c478bd9Sstevel@tonic-gate  */
131*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
132*7c478bd9Sstevel@tonic-gate char *
133*7c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *fmt, ...)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	va_list args;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
138*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, INT_MAX, fmt, args);
139*7c478bd9Sstevel@tonic-gate 	va_end(args);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	return (buf);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate char *
145*7c478bd9Sstevel@tonic-gate vsprintf(char *buf, const char *fmt, va_list args)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, INT_MAX, fmt, args);
148*7c478bd9Sstevel@tonic-gate 	return (buf);
149*7c478bd9Sstevel@tonic-gate }
150