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*7887SLiane.Praza@Sun.COM * Common Development and Distribution License (the "License"). 6*7887SLiane.Praza@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*7887SLiane.Praza@Sun.COM * Copyright 2008 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 "libuutil_common.h" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <stdarg.h> 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <stdlib.h> 310Sstevel@tonic-gate #include <string.h> 320Sstevel@tonic-gate 330Sstevel@tonic-gate void * 340Sstevel@tonic-gate uu_zalloc(size_t n) 350Sstevel@tonic-gate { 360Sstevel@tonic-gate void *p = malloc(n); 370Sstevel@tonic-gate 380Sstevel@tonic-gate if (p == NULL) { 390Sstevel@tonic-gate uu_set_error(UU_ERROR_SYSTEM); 400Sstevel@tonic-gate return (NULL); 410Sstevel@tonic-gate } 420Sstevel@tonic-gate 430Sstevel@tonic-gate (void) memset(p, 0, n); 440Sstevel@tonic-gate 450Sstevel@tonic-gate return (p); 460Sstevel@tonic-gate } 470Sstevel@tonic-gate 480Sstevel@tonic-gate void 490Sstevel@tonic-gate uu_free(void *p) 500Sstevel@tonic-gate { 510Sstevel@tonic-gate free(p); 520Sstevel@tonic-gate } 530Sstevel@tonic-gate 540Sstevel@tonic-gate char * 55*7887SLiane.Praza@Sun.COM uu_strdup(const char *str) 56*7887SLiane.Praza@Sun.COM { 57*7887SLiane.Praza@Sun.COM char *buf = NULL; 58*7887SLiane.Praza@Sun.COM 59*7887SLiane.Praza@Sun.COM if (str != NULL) { 60*7887SLiane.Praza@Sun.COM size_t sz; 61*7887SLiane.Praza@Sun.COM 62*7887SLiane.Praza@Sun.COM sz = strlen(str) + 1; 63*7887SLiane.Praza@Sun.COM buf = uu_zalloc(sz); 64*7887SLiane.Praza@Sun.COM if (buf != NULL) 65*7887SLiane.Praza@Sun.COM (void) memcpy(buf, str, sz); 66*7887SLiane.Praza@Sun.COM } 67*7887SLiane.Praza@Sun.COM return (buf); 68*7887SLiane.Praza@Sun.COM } 69*7887SLiane.Praza@Sun.COM 70*7887SLiane.Praza@Sun.COM char * 710Sstevel@tonic-gate uu_msprintf(const char *format, ...) 720Sstevel@tonic-gate { 730Sstevel@tonic-gate va_list args; 740Sstevel@tonic-gate char attic[1]; 750Sstevel@tonic-gate uint_t M, m; 760Sstevel@tonic-gate char *b; 770Sstevel@tonic-gate 780Sstevel@tonic-gate va_start(args, format); 790Sstevel@tonic-gate M = vsnprintf(attic, 1, format, args); 800Sstevel@tonic-gate va_end(args); 810Sstevel@tonic-gate 820Sstevel@tonic-gate for (;;) { 830Sstevel@tonic-gate m = M; 840Sstevel@tonic-gate if ((b = uu_zalloc(m + 1)) == NULL) 850Sstevel@tonic-gate return (NULL); 860Sstevel@tonic-gate 870Sstevel@tonic-gate va_start(args, format); 880Sstevel@tonic-gate M = vsnprintf(b, m + 1, format, args); 890Sstevel@tonic-gate va_end(args); 900Sstevel@tonic-gate 910Sstevel@tonic-gate if (M == m) 920Sstevel@tonic-gate break; /* sizes match */ 930Sstevel@tonic-gate 940Sstevel@tonic-gate uu_free(b); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate return (b); 980Sstevel@tonic-gate } 99