xref: /freebsd/sys/contrib/openzfs/lib/libuutil/uu_alloc.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6*eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11*eda14cbcSMatt Macy  * and limitations under the License.
12*eda14cbcSMatt Macy  *
13*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*eda14cbcSMatt Macy  *
19*eda14cbcSMatt Macy  * CDDL HEADER END
20*eda14cbcSMatt Macy  */
21*eda14cbcSMatt Macy /*
22*eda14cbcSMatt Macy  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
23*eda14cbcSMatt Macy  */
24*eda14cbcSMatt Macy 
25*eda14cbcSMatt Macy #include "libuutil_common.h"
26*eda14cbcSMatt Macy 
27*eda14cbcSMatt Macy #include <stdarg.h>
28*eda14cbcSMatt Macy #include <stdio.h>
29*eda14cbcSMatt Macy #include <stdlib.h>
30*eda14cbcSMatt Macy #include <string.h>
31*eda14cbcSMatt Macy 
32*eda14cbcSMatt Macy void *
33*eda14cbcSMatt Macy uu_zalloc(size_t n)
34*eda14cbcSMatt Macy {
35*eda14cbcSMatt Macy 	void *p = malloc(n);
36*eda14cbcSMatt Macy 
37*eda14cbcSMatt Macy 	if (p == NULL) {
38*eda14cbcSMatt Macy 		uu_set_error(UU_ERROR_SYSTEM);
39*eda14cbcSMatt Macy 		return (NULL);
40*eda14cbcSMatt Macy 	}
41*eda14cbcSMatt Macy 
42*eda14cbcSMatt Macy 	(void) memset(p, 0, n);
43*eda14cbcSMatt Macy 
44*eda14cbcSMatt Macy 	return (p);
45*eda14cbcSMatt Macy }
46*eda14cbcSMatt Macy 
47*eda14cbcSMatt Macy void
48*eda14cbcSMatt Macy uu_free(void *p)
49*eda14cbcSMatt Macy {
50*eda14cbcSMatt Macy 	free(p);
51*eda14cbcSMatt Macy }
52*eda14cbcSMatt Macy 
53*eda14cbcSMatt Macy char *
54*eda14cbcSMatt Macy uu_strdup(const char *str)
55*eda14cbcSMatt Macy {
56*eda14cbcSMatt Macy 	char *buf = NULL;
57*eda14cbcSMatt Macy 
58*eda14cbcSMatt Macy 	if (str != NULL) {
59*eda14cbcSMatt Macy 		size_t sz;
60*eda14cbcSMatt Macy 
61*eda14cbcSMatt Macy 		sz = strlen(str) + 1;
62*eda14cbcSMatt Macy 		buf = uu_zalloc(sz);
63*eda14cbcSMatt Macy 		if (buf != NULL)
64*eda14cbcSMatt Macy 			(void) memcpy(buf, str, sz);
65*eda14cbcSMatt Macy 	}
66*eda14cbcSMatt Macy 	return (buf);
67*eda14cbcSMatt Macy }
68*eda14cbcSMatt Macy 
69*eda14cbcSMatt Macy /*
70*eda14cbcSMatt Macy  * Duplicate up to n bytes of a string.  Kind of sort of like
71*eda14cbcSMatt Macy  * strdup(strlcpy(s, n)).
72*eda14cbcSMatt Macy  */
73*eda14cbcSMatt Macy char *
74*eda14cbcSMatt Macy uu_strndup(const char *s, size_t n)
75*eda14cbcSMatt Macy {
76*eda14cbcSMatt Macy 	size_t len;
77*eda14cbcSMatt Macy 	char *p;
78*eda14cbcSMatt Macy 
79*eda14cbcSMatt Macy 	len = strnlen(s, n);
80*eda14cbcSMatt Macy 	p = uu_zalloc(len + 1);
81*eda14cbcSMatt Macy 	if (p == NULL)
82*eda14cbcSMatt Macy 		return (NULL);
83*eda14cbcSMatt Macy 
84*eda14cbcSMatt Macy 	if (len > 0)
85*eda14cbcSMatt Macy 		(void) memcpy(p, s, len);
86*eda14cbcSMatt Macy 	p[len] = '\0';
87*eda14cbcSMatt Macy 
88*eda14cbcSMatt Macy 	return (p);
89*eda14cbcSMatt Macy }
90*eda14cbcSMatt Macy 
91*eda14cbcSMatt Macy /*
92*eda14cbcSMatt Macy  * Duplicate a block of memory.  Combines malloc with memcpy, much as
93*eda14cbcSMatt Macy  * strdup combines malloc, strlen, and strcpy.
94*eda14cbcSMatt Macy  */
95*eda14cbcSMatt Macy void *
96*eda14cbcSMatt Macy uu_memdup(const void *buf, size_t sz)
97*eda14cbcSMatt Macy {
98*eda14cbcSMatt Macy 	void *p;
99*eda14cbcSMatt Macy 
100*eda14cbcSMatt Macy 	p = uu_zalloc(sz);
101*eda14cbcSMatt Macy 	if (p == NULL)
102*eda14cbcSMatt Macy 		return (NULL);
103*eda14cbcSMatt Macy 	(void) memcpy(p, buf, sz);
104*eda14cbcSMatt Macy 	return (p);
105*eda14cbcSMatt Macy }
106*eda14cbcSMatt Macy 
107*eda14cbcSMatt Macy char *
108*eda14cbcSMatt Macy uu_msprintf(const char *format, ...)
109*eda14cbcSMatt Macy {
110*eda14cbcSMatt Macy 	va_list args;
111*eda14cbcSMatt Macy 	char attic[1];
112*eda14cbcSMatt Macy 	uint_t M, m;
113*eda14cbcSMatt Macy 	char *b;
114*eda14cbcSMatt Macy 
115*eda14cbcSMatt Macy 	va_start(args, format);
116*eda14cbcSMatt Macy 	M = vsnprintf(attic, 1, format, args);
117*eda14cbcSMatt Macy 	va_end(args);
118*eda14cbcSMatt Macy 
119*eda14cbcSMatt Macy 	for (;;) {
120*eda14cbcSMatt Macy 		m = M;
121*eda14cbcSMatt Macy 		if ((b = uu_zalloc(m + 1)) == NULL)
122*eda14cbcSMatt Macy 			return (NULL);
123*eda14cbcSMatt Macy 
124*eda14cbcSMatt Macy 		va_start(args, format);
125*eda14cbcSMatt Macy 		M = vsnprintf(b, m + 1, format, args);
126*eda14cbcSMatt Macy 		va_end(args);
127*eda14cbcSMatt Macy 
128*eda14cbcSMatt Macy 		if (M == m)
129*eda14cbcSMatt Macy 			break;		/* sizes match */
130*eda14cbcSMatt Macy 
131*eda14cbcSMatt Macy 		uu_free(b);
132*eda14cbcSMatt Macy 	}
133*eda14cbcSMatt Macy 
134*eda14cbcSMatt Macy 	return (b);
135*eda14cbcSMatt Macy }
136