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