xref: /titanic_44/usr/src/lib/libuutil/common/uu_alloc.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "libuutil_common.h"
30 
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 void *
37 uu_zalloc(size_t n)
38 {
39 	void *p = malloc(n);
40 
41 	if (p == NULL) {
42 		uu_set_error(UU_ERROR_SYSTEM);
43 		return (NULL);
44 	}
45 
46 	(void) memset(p, 0, n);
47 
48 	return (p);
49 }
50 
51 void
52 uu_free(void *p)
53 {
54 	free(p);
55 }
56 
57 char *
58 uu_msprintf(const char *format, ...)
59 {
60 	va_list args;
61 	char attic[1];
62 	uint_t M, m;
63 	char *b;
64 
65 	va_start(args, format);
66 	M = vsnprintf(attic, 1, format, args);
67 	va_end(args);
68 
69 	for (;;) {
70 		m = M;
71 		if ((b = uu_zalloc(m + 1)) == NULL)
72 			return (NULL);
73 
74 		va_start(args, format);
75 		M = vsnprintf(b, m + 1, format, args);
76 		va_end(args);
77 
78 		if (M == m)
79 			break;		/* sizes match */
80 
81 		uu_free(b);
82 	}
83 
84 	return (b);
85 }
86