10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*10272SMatthew.Ahrens@Sun.COM * Common Development and Distribution License (the "License").
6*10272SMatthew.Ahrens@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*10272SMatthew.Ahrens@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/cmn_err.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/varargs.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * SunOS-specific extensions to libc's standard set of string routines.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * NOTE: The standard libc string routines are in $SRC/common/util/string.c,
350Sstevel@tonic-gate * to facilitate sharing with standalone.
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * Historical entry point: remove in Solaris 2.8.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate char *
vsprintf_len(size_t buflen,char * buf,const char * fmt,va_list args)420Sstevel@tonic-gate vsprintf_len(size_t buflen, char *buf, const char *fmt, va_list args)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate (void) vsnprintf(buf, buflen, fmt, args);
450Sstevel@tonic-gate return (buf);
460Sstevel@tonic-gate }
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * Historical entry point: remove in Solaris 2.8.
500Sstevel@tonic-gate */
510Sstevel@tonic-gate /*PRINTFLIKE3*/
520Sstevel@tonic-gate char *
sprintf_len(size_t buflen,char * buf,const char * fmt,...)530Sstevel@tonic-gate sprintf_len(size_t buflen, char *buf, const char *fmt, ...)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate va_list args;
560Sstevel@tonic-gate
570Sstevel@tonic-gate va_start(args, fmt);
580Sstevel@tonic-gate (void) vsnprintf(buf, buflen, fmt, args);
590Sstevel@tonic-gate va_end(args);
600Sstevel@tonic-gate
610Sstevel@tonic-gate return (buf);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * Simple-minded conversion of a long into a null-terminated character
660Sstevel@tonic-gate * string. Caller must ensure there's enough space to hold the result.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate void
numtos(unsigned long num,char * s)690Sstevel@tonic-gate numtos(unsigned long num, char *s)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate char prbuf[40];
720Sstevel@tonic-gate
730Sstevel@tonic-gate char *cp = prbuf;
740Sstevel@tonic-gate
750Sstevel@tonic-gate do {
760Sstevel@tonic-gate *cp++ = "0123456789"[num % 10];
770Sstevel@tonic-gate num /= 10;
780Sstevel@tonic-gate } while (num);
790Sstevel@tonic-gate
800Sstevel@tonic-gate do {
810Sstevel@tonic-gate *s++ = *--cp;
820Sstevel@tonic-gate } while (cp > prbuf);
830Sstevel@tonic-gate *s = '\0';
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * Returns the integer value of the string of decimal numeric
880Sstevel@tonic-gate * chars beginning at **str. Does no overflow checking.
890Sstevel@tonic-gate * Note: updates *str to point at the last character examined.
900Sstevel@tonic-gate */
910Sstevel@tonic-gate int
stoi(char ** str)920Sstevel@tonic-gate stoi(char **str)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate char *p = *str;
950Sstevel@tonic-gate int n;
960Sstevel@tonic-gate int c;
970Sstevel@tonic-gate
980Sstevel@tonic-gate for (n = 0; (c = *p) >= '0' && c <= '9'; p++) {
990Sstevel@tonic-gate n = n * 10 + c - '0';
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate *str = p;
1020Sstevel@tonic-gate return (n);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * Like strrchr(), except
1070Sstevel@tonic-gate * (a) it takes a maximum length for the string to be searched, and
1080Sstevel@tonic-gate * (b) if the string ends with a null, it is not considered part of the string.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate char *
strnrchr(const char * sp,int c,size_t n)1110Sstevel@tonic-gate strnrchr(const char *sp, int c, size_t n)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate const char *r = 0;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate while (n-- > 0 && *sp) {
1160Sstevel@tonic-gate if (*sp == c)
1170Sstevel@tonic-gate r = sp;
1180Sstevel@tonic-gate sp++;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate return ((char *)r);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * NOTE: These routines aren't shared with standalone because the DDI mandates
1260Sstevel@tonic-gate * that they return the buffer rather than its length.
1270Sstevel@tonic-gate */
1280Sstevel@tonic-gate /*PRINTFLIKE2*/
1290Sstevel@tonic-gate char *
sprintf(char * buf,const char * fmt,...)1300Sstevel@tonic-gate sprintf(char *buf, const char *fmt, ...)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate va_list args;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate va_start(args, fmt);
1350Sstevel@tonic-gate (void) vsnprintf(buf, INT_MAX, fmt, args);
1360Sstevel@tonic-gate va_end(args);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate return (buf);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate char *
vsprintf(char * buf,const char * fmt,va_list args)1420Sstevel@tonic-gate vsprintf(char *buf, const char *fmt, va_list args)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate (void) vsnprintf(buf, INT_MAX, fmt, args);
1450Sstevel@tonic-gate return (buf);
1460Sstevel@tonic-gate }
147*10272SMatthew.Ahrens@Sun.COM
148*10272SMatthew.Ahrens@Sun.COM /*
149*10272SMatthew.Ahrens@Sun.COM * Do not change the length of the returned string; it must be freed
150*10272SMatthew.Ahrens@Sun.COM * with strfree().
151*10272SMatthew.Ahrens@Sun.COM */
152*10272SMatthew.Ahrens@Sun.COM char *
kmem_asprintf(const char * fmt,...)153*10272SMatthew.Ahrens@Sun.COM kmem_asprintf(const char *fmt, ...)
154*10272SMatthew.Ahrens@Sun.COM {
155*10272SMatthew.Ahrens@Sun.COM int size;
156*10272SMatthew.Ahrens@Sun.COM va_list adx;
157*10272SMatthew.Ahrens@Sun.COM char *buf;
158*10272SMatthew.Ahrens@Sun.COM
159*10272SMatthew.Ahrens@Sun.COM va_start(adx, fmt);
160*10272SMatthew.Ahrens@Sun.COM size = vsnprintf(NULL, 0, fmt, adx) + 1;
161*10272SMatthew.Ahrens@Sun.COM va_end(adx);
162*10272SMatthew.Ahrens@Sun.COM
163*10272SMatthew.Ahrens@Sun.COM buf = kmem_alloc(size, KM_SLEEP);
164*10272SMatthew.Ahrens@Sun.COM
165*10272SMatthew.Ahrens@Sun.COM va_start(adx, fmt);
166*10272SMatthew.Ahrens@Sun.COM size = vsnprintf(buf, size, fmt, adx);
167*10272SMatthew.Ahrens@Sun.COM va_end(adx);
168*10272SMatthew.Ahrens@Sun.COM
169*10272SMatthew.Ahrens@Sun.COM return (buf);
170*10272SMatthew.Ahrens@Sun.COM }
171