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
51f6eb021SLiane Praza  * Common Development and Distribution License (the "License").
61f6eb021SLiane Praza  * 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*cb174861Sjoyce mcintosh  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "libuutil_common.h"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdarg.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate void *
uu_zalloc(size_t n)337c478bd9Sstevel@tonic-gate uu_zalloc(size_t n)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	void *p = malloc(n);
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 	if (p == NULL) {
387c478bd9Sstevel@tonic-gate 		uu_set_error(UU_ERROR_SYSTEM);
397c478bd9Sstevel@tonic-gate 		return (NULL);
407c478bd9Sstevel@tonic-gate 	}
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	(void) memset(p, 0, n);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	return (p);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void
uu_free(void * p)487c478bd9Sstevel@tonic-gate uu_free(void *p)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	free(p);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
531f6eb021SLiane Praza char *
uu_strdup(const char * str)541f6eb021SLiane Praza uu_strdup(const char *str)
551f6eb021SLiane Praza {
561f6eb021SLiane Praza 	char *buf = NULL;
571f6eb021SLiane Praza 
581f6eb021SLiane Praza 	if (str != NULL) {
591f6eb021SLiane Praza 		size_t sz;
601f6eb021SLiane Praza 
611f6eb021SLiane Praza 		sz = strlen(str) + 1;
621f6eb021SLiane Praza 		buf = uu_zalloc(sz);
631f6eb021SLiane Praza 		if (buf != NULL)
641f6eb021SLiane Praza 			(void) memcpy(buf, str, sz);
651f6eb021SLiane Praza 	}
661f6eb021SLiane Praza 	return (buf);
671f6eb021SLiane Praza }
681f6eb021SLiane Praza 
69*cb174861Sjoyce mcintosh /*
70*cb174861Sjoyce mcintosh  * Duplicate up to n bytes of a string.  Kind of sort of like
71*cb174861Sjoyce mcintosh  * strdup(strlcpy(s, n)).
72*cb174861Sjoyce mcintosh  */
73*cb174861Sjoyce mcintosh char *
uu_strndup(const char * s,size_t n)74*cb174861Sjoyce mcintosh uu_strndup(const char *s, size_t n)
75*cb174861Sjoyce mcintosh {
76*cb174861Sjoyce mcintosh 	size_t len;
77*cb174861Sjoyce mcintosh 	char *p;
78*cb174861Sjoyce mcintosh 
79*cb174861Sjoyce mcintosh 	len = strnlen(s, n);
80*cb174861Sjoyce mcintosh 	p = uu_zalloc(len + 1);
81*cb174861Sjoyce mcintosh 	if (p == NULL)
82*cb174861Sjoyce mcintosh 		return (NULL);
83*cb174861Sjoyce mcintosh 
84*cb174861Sjoyce mcintosh 	if (len > 0)
85*cb174861Sjoyce mcintosh 		(void) memcpy(p, s, len);
86*cb174861Sjoyce mcintosh 	p[len] = '\0';
87*cb174861Sjoyce mcintosh 
88*cb174861Sjoyce mcintosh 	return (p);
89*cb174861Sjoyce mcintosh }
90*cb174861Sjoyce mcintosh 
91*cb174861Sjoyce mcintosh /*
92*cb174861Sjoyce mcintosh  * Duplicate a block of memory.  Combines malloc with memcpy, much as
93*cb174861Sjoyce mcintosh  * strdup combines malloc, strlen, and strcpy.
94*cb174861Sjoyce mcintosh  */
95*cb174861Sjoyce mcintosh void *
uu_memdup(const void * buf,size_t sz)96*cb174861Sjoyce mcintosh uu_memdup(const void *buf, size_t sz)
97*cb174861Sjoyce mcintosh {
98*cb174861Sjoyce mcintosh 	void *p;
99*cb174861Sjoyce mcintosh 
100*cb174861Sjoyce mcintosh 	p = uu_zalloc(sz);
101*cb174861Sjoyce mcintosh 	if (p == NULL)
102*cb174861Sjoyce mcintosh 		return (NULL);
103*cb174861Sjoyce mcintosh 	(void) memcpy(p, buf, sz);
104*cb174861Sjoyce mcintosh 	return (p);
105*cb174861Sjoyce mcintosh }
106*cb174861Sjoyce mcintosh 
1077c478bd9Sstevel@tonic-gate char *
uu_msprintf(const char * format,...)1087c478bd9Sstevel@tonic-gate uu_msprintf(const char *format, ...)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	va_list args;
1117c478bd9Sstevel@tonic-gate 	char attic[1];
1127c478bd9Sstevel@tonic-gate 	uint_t M, m;
1137c478bd9Sstevel@tonic-gate 	char *b;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	va_start(args, format);
1167c478bd9Sstevel@tonic-gate 	M = vsnprintf(attic, 1, format, args);
1177c478bd9Sstevel@tonic-gate 	va_end(args);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	for (;;) {
1207c478bd9Sstevel@tonic-gate 		m = M;
1217c478bd9Sstevel@tonic-gate 		if ((b = uu_zalloc(m + 1)) == NULL)
1227c478bd9Sstevel@tonic-gate 			return (NULL);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		va_start(args, format);
1257c478bd9Sstevel@tonic-gate 		M = vsnprintf(b, m + 1, format, args);
1267c478bd9Sstevel@tonic-gate 		va_end(args);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		if (M == m)
1297c478bd9Sstevel@tonic-gate 			break;		/* sizes match */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		uu_free(b);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (b);
1357c478bd9Sstevel@tonic-gate }
136