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
57887SLiane.Praza@Sun.COM * Common Development and Distribution License (the "License").
67887SLiane.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*12890SJoyce.McIntosh@Sun.COM * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include "libuutil_common.h"
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdarg.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate void *
uu_zalloc(size_t n)330Sstevel@tonic-gate uu_zalloc(size_t n)
340Sstevel@tonic-gate {
350Sstevel@tonic-gate void *p = malloc(n);
360Sstevel@tonic-gate
370Sstevel@tonic-gate if (p == NULL) {
380Sstevel@tonic-gate uu_set_error(UU_ERROR_SYSTEM);
390Sstevel@tonic-gate return (NULL);
400Sstevel@tonic-gate }
410Sstevel@tonic-gate
420Sstevel@tonic-gate (void) memset(p, 0, n);
430Sstevel@tonic-gate
440Sstevel@tonic-gate return (p);
450Sstevel@tonic-gate }
460Sstevel@tonic-gate
470Sstevel@tonic-gate void
uu_free(void * p)480Sstevel@tonic-gate uu_free(void *p)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate free(p);
510Sstevel@tonic-gate }
520Sstevel@tonic-gate
530Sstevel@tonic-gate char *
uu_strdup(const char * str)547887SLiane.Praza@Sun.COM uu_strdup(const char *str)
557887SLiane.Praza@Sun.COM {
567887SLiane.Praza@Sun.COM char *buf = NULL;
577887SLiane.Praza@Sun.COM
587887SLiane.Praza@Sun.COM if (str != NULL) {
597887SLiane.Praza@Sun.COM size_t sz;
607887SLiane.Praza@Sun.COM
617887SLiane.Praza@Sun.COM sz = strlen(str) + 1;
627887SLiane.Praza@Sun.COM buf = uu_zalloc(sz);
637887SLiane.Praza@Sun.COM if (buf != NULL)
647887SLiane.Praza@Sun.COM (void) memcpy(buf, str, sz);
657887SLiane.Praza@Sun.COM }
667887SLiane.Praza@Sun.COM return (buf);
677887SLiane.Praza@Sun.COM }
687887SLiane.Praza@Sun.COM
69*12890SJoyce.McIntosh@Sun.COM /*
70*12890SJoyce.McIntosh@Sun.COM * Duplicate up to n bytes of a string. Kind of sort of like
71*12890SJoyce.McIntosh@Sun.COM * strdup(strlcpy(s, n)).
72*12890SJoyce.McIntosh@Sun.COM */
73*12890SJoyce.McIntosh@Sun.COM char *
uu_strndup(const char * s,size_t n)74*12890SJoyce.McIntosh@Sun.COM uu_strndup(const char *s, size_t n)
75*12890SJoyce.McIntosh@Sun.COM {
76*12890SJoyce.McIntosh@Sun.COM size_t len;
77*12890SJoyce.McIntosh@Sun.COM char *p;
78*12890SJoyce.McIntosh@Sun.COM
79*12890SJoyce.McIntosh@Sun.COM len = strnlen(s, n);
80*12890SJoyce.McIntosh@Sun.COM p = uu_zalloc(len + 1);
81*12890SJoyce.McIntosh@Sun.COM if (p == NULL)
82*12890SJoyce.McIntosh@Sun.COM return (NULL);
83*12890SJoyce.McIntosh@Sun.COM
84*12890SJoyce.McIntosh@Sun.COM if (len > 0)
85*12890SJoyce.McIntosh@Sun.COM (void) memcpy(p, s, len);
86*12890SJoyce.McIntosh@Sun.COM p[len] = '\0';
87*12890SJoyce.McIntosh@Sun.COM
88*12890SJoyce.McIntosh@Sun.COM return (p);
89*12890SJoyce.McIntosh@Sun.COM }
90*12890SJoyce.McIntosh@Sun.COM
91*12890SJoyce.McIntosh@Sun.COM /*
92*12890SJoyce.McIntosh@Sun.COM * Duplicate a block of memory. Combines malloc with memcpy, much as
93*12890SJoyce.McIntosh@Sun.COM * strdup combines malloc, strlen, and strcpy.
94*12890SJoyce.McIntosh@Sun.COM */
95*12890SJoyce.McIntosh@Sun.COM void *
uu_memdup(const void * buf,size_t sz)96*12890SJoyce.McIntosh@Sun.COM uu_memdup(const void *buf, size_t sz)
97*12890SJoyce.McIntosh@Sun.COM {
98*12890SJoyce.McIntosh@Sun.COM void *p;
99*12890SJoyce.McIntosh@Sun.COM
100*12890SJoyce.McIntosh@Sun.COM p = uu_zalloc(sz);
101*12890SJoyce.McIntosh@Sun.COM if (p == NULL)
102*12890SJoyce.McIntosh@Sun.COM return (NULL);
103*12890SJoyce.McIntosh@Sun.COM (void) memcpy(p, buf, sz);
104*12890SJoyce.McIntosh@Sun.COM return (p);
105*12890SJoyce.McIntosh@Sun.COM }
106*12890SJoyce.McIntosh@Sun.COM
1077887SLiane.Praza@Sun.COM char *
uu_msprintf(const char * format,...)1080Sstevel@tonic-gate uu_msprintf(const char *format, ...)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate va_list args;
1110Sstevel@tonic-gate char attic[1];
1120Sstevel@tonic-gate uint_t M, m;
1130Sstevel@tonic-gate char *b;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate va_start(args, format);
1160Sstevel@tonic-gate M = vsnprintf(attic, 1, format, args);
1170Sstevel@tonic-gate va_end(args);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate for (;;) {
1200Sstevel@tonic-gate m = M;
1210Sstevel@tonic-gate if ((b = uu_zalloc(m + 1)) == NULL)
1220Sstevel@tonic-gate return (NULL);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate va_start(args, format);
1250Sstevel@tonic-gate M = vsnprintf(b, m + 1, format, args);
1260Sstevel@tonic-gate va_end(args);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (M == m)
1290Sstevel@tonic-gate break; /* sizes match */
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate uu_free(b);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate return (b);
1350Sstevel@tonic-gate }
136