17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*ae46e4c7SMatthew Ahrens * Common Development and Distribution License (the "License"). 6*ae46e4c7SMatthew Ahrens * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*ae46e4c7SMatthew Ahrens * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 287c478bd9Sstevel@tonic-gate #include <sys/systm.h> 297c478bd9Sstevel@tonic-gate #include <sys/varargs.h> 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * SunOS-specific extensions to libc's standard set of string routines. 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * NOTE: The standard libc string routines are in $SRC/common/util/string.c, 357c478bd9Sstevel@tonic-gate * to facilitate sharing with standalone. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * Historical entry point: remove in Solaris 2.8. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate char * 427c478bd9Sstevel@tonic-gate vsprintf_len(size_t buflen, char *buf, const char *fmt, va_list args) 437c478bd9Sstevel@tonic-gate { 447c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, buflen, fmt, args); 457c478bd9Sstevel@tonic-gate return (buf); 467c478bd9Sstevel@tonic-gate } 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Historical entry point: remove in Solaris 2.8. 507c478bd9Sstevel@tonic-gate */ 517c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/ 527c478bd9Sstevel@tonic-gate char * 537c478bd9Sstevel@tonic-gate sprintf_len(size_t buflen, char *buf, const char *fmt, ...) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate va_list args; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate va_start(args, fmt); 587c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, buflen, fmt, args); 597c478bd9Sstevel@tonic-gate va_end(args); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate return (buf); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate * Simple-minded conversion of a long into a null-terminated character 667c478bd9Sstevel@tonic-gate * string. Caller must ensure there's enough space to hold the result. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate void 697c478bd9Sstevel@tonic-gate numtos(unsigned long num, char *s) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate char prbuf[40]; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate char *cp = prbuf; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate do { 767c478bd9Sstevel@tonic-gate *cp++ = "0123456789"[num % 10]; 777c478bd9Sstevel@tonic-gate num /= 10; 787c478bd9Sstevel@tonic-gate } while (num); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate do { 817c478bd9Sstevel@tonic-gate *s++ = *--cp; 827c478bd9Sstevel@tonic-gate } while (cp > prbuf); 837c478bd9Sstevel@tonic-gate *s = '\0'; 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * Returns the integer value of the string of decimal numeric 887c478bd9Sstevel@tonic-gate * chars beginning at **str. Does no overflow checking. 897c478bd9Sstevel@tonic-gate * Note: updates *str to point at the last character examined. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate int 927c478bd9Sstevel@tonic-gate stoi(char **str) 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate char *p = *str; 957c478bd9Sstevel@tonic-gate int n; 967c478bd9Sstevel@tonic-gate int c; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate for (n = 0; (c = *p) >= '0' && c <= '9'; p++) { 997c478bd9Sstevel@tonic-gate n = n * 10 + c - '0'; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate *str = p; 1027c478bd9Sstevel@tonic-gate return (n); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * Like strrchr(), except 1077c478bd9Sstevel@tonic-gate * (a) it takes a maximum length for the string to be searched, and 1087c478bd9Sstevel@tonic-gate * (b) if the string ends with a null, it is not considered part of the string. 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate char * 1117c478bd9Sstevel@tonic-gate strnrchr(const char *sp, int c, size_t n) 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate const char *r = 0; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate while (n-- > 0 && *sp) { 1167c478bd9Sstevel@tonic-gate if (*sp == c) 1177c478bd9Sstevel@tonic-gate r = sp; 1187c478bd9Sstevel@tonic-gate sp++; 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate return ((char *)r); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * NOTE: These routines aren't shared with standalone because the DDI mandates 1267c478bd9Sstevel@tonic-gate * that they return the buffer rather than its length. 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1297c478bd9Sstevel@tonic-gate char * 1307c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *fmt, ...) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate va_list args; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate va_start(args, fmt); 1357c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, INT_MAX, fmt, args); 1367c478bd9Sstevel@tonic-gate va_end(args); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate return (buf); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate char * 1427c478bd9Sstevel@tonic-gate vsprintf(char *buf, const char *fmt, va_list args) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate (void) vsnprintf(buf, INT_MAX, fmt, args); 1457c478bd9Sstevel@tonic-gate return (buf); 1467c478bd9Sstevel@tonic-gate } 147*ae46e4c7SMatthew Ahrens 148*ae46e4c7SMatthew Ahrens /* 149*ae46e4c7SMatthew Ahrens * Do not change the length of the returned string; it must be freed 150*ae46e4c7SMatthew Ahrens * with strfree(). 151*ae46e4c7SMatthew Ahrens */ 152*ae46e4c7SMatthew Ahrens char * 153*ae46e4c7SMatthew Ahrens kmem_asprintf(const char *fmt, ...) 154*ae46e4c7SMatthew Ahrens { 155*ae46e4c7SMatthew Ahrens int size; 156*ae46e4c7SMatthew Ahrens va_list adx; 157*ae46e4c7SMatthew Ahrens char *buf; 158*ae46e4c7SMatthew Ahrens 159*ae46e4c7SMatthew Ahrens va_start(adx, fmt); 160*ae46e4c7SMatthew Ahrens size = vsnprintf(NULL, 0, fmt, adx) + 1; 161*ae46e4c7SMatthew Ahrens va_end(adx); 162*ae46e4c7SMatthew Ahrens 163*ae46e4c7SMatthew Ahrens buf = kmem_alloc(size, KM_SLEEP); 164*ae46e4c7SMatthew Ahrens 165*ae46e4c7SMatthew Ahrens va_start(adx, fmt); 166*ae46e4c7SMatthew Ahrens size = vsnprintf(buf, size, fmt, adx); 167*ae46e4c7SMatthew Ahrens va_end(adx); 168*ae46e4c7SMatthew Ahrens 169*ae46e4c7SMatthew Ahrens return (buf); 170*ae46e4c7SMatthew Ahrens } 171