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