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