xref: /illumos-gate/usr/src/lib/cfgadm_plugins/ac/common/mema_util.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*3db86aabSstevel /*
2*3db86aabSstevel  * CDDL HEADER START
3*3db86aabSstevel  *
4*3db86aabSstevel  * The contents of this file are subject to the terms of the
5*3db86aabSstevel  * Common Development and Distribution License, Version 1.0 only
6*3db86aabSstevel  * (the "License").  You may not use this file except in compliance
7*3db86aabSstevel  * with the License.
8*3db86aabSstevel  *
9*3db86aabSstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*3db86aabSstevel  * or http://www.opensolaris.org/os/licensing.
11*3db86aabSstevel  * See the License for the specific language governing permissions
12*3db86aabSstevel  * and limitations under the License.
13*3db86aabSstevel  *
14*3db86aabSstevel  * When distributing Covered Code, include this CDDL HEADER in each
15*3db86aabSstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*3db86aabSstevel  * If applicable, add the following below this CDDL HEADER, with the
17*3db86aabSstevel  * fields enclosed by brackets "[]" replaced with your own identifying
18*3db86aabSstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
19*3db86aabSstevel  *
20*3db86aabSstevel  * CDDL HEADER END
21*3db86aabSstevel  */
22*3db86aabSstevel /*
23*3db86aabSstevel  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*3db86aabSstevel  * Use is subject to license terms.
25*3db86aabSstevel  */
26*3db86aabSstevel 
27*3db86aabSstevel #include <stddef.h>
28*3db86aabSstevel #include <stdlib.h>
29*3db86aabSstevel #include <stdio.h>
30*3db86aabSstevel #include <stdarg.h>
31*3db86aabSstevel #include <string.h>
32*3db86aabSstevel #include <locale.h>
33*3db86aabSstevel #include <sys/param.h>
34*3db86aabSstevel #include <config_admin.h>
35*3db86aabSstevel #include "mema_util.h"
36*3db86aabSstevel 
37*3db86aabSstevel /*
38*3db86aabSstevel  * The libmemadm routines can return arbitrary error strings.  As the
39*3db86aabSstevel  * calling program does not know how long these errors might be,
40*3db86aabSstevel  * the library routines must allocate the required space and the
41*3db86aabSstevel  * calling program must deallocate it.
42*3db86aabSstevel  *
43*3db86aabSstevel  * This routine povides a printf-like interface for creating the
44*3db86aabSstevel  * error strings.
45*3db86aabSstevel  */
46*3db86aabSstevel 
47*3db86aabSstevel #define	FMT_STR_SLOP		(16)
48*3db86aabSstevel 
49*3db86aabSstevel void
__fmt_errstring(char ** errstring,size_t extra_length_hint,const char * fmt,...)50*3db86aabSstevel __fmt_errstring(
51*3db86aabSstevel 	char **errstring,
52*3db86aabSstevel 	size_t extra_length_hint,
53*3db86aabSstevel 	const char *fmt,
54*3db86aabSstevel 	...)
55*3db86aabSstevel {
56*3db86aabSstevel 	char *ebuf;
57*3db86aabSstevel 	size_t elen;
58*3db86aabSstevel 	va_list ap;
59*3db86aabSstevel 
60*3db86aabSstevel 	/*
61*3db86aabSstevel 	 * If no errors required or error already set, return.
62*3db86aabSstevel 	 */
63*3db86aabSstevel 	if ((errstring == NULL) || (*errstring != NULL))
64*3db86aabSstevel 		return;
65*3db86aabSstevel 
66*3db86aabSstevel 	elen = strlen(fmt) + extra_length_hint + FMT_STR_SLOP;
67*3db86aabSstevel 
68*3db86aabSstevel 	if ((ebuf = (char *)malloc(elen + 1)) == NULL)
69*3db86aabSstevel 		return;
70*3db86aabSstevel 
71*3db86aabSstevel 	va_start(ap, fmt);
72*3db86aabSstevel 	(void) vsprintf(ebuf, fmt, ap);
73*3db86aabSstevel 	va_end(ap);
74*3db86aabSstevel 
75*3db86aabSstevel 	if (strlen(ebuf) > elen)
76*3db86aabSstevel 		abort();
77*3db86aabSstevel 
78*3db86aabSstevel 	*errstring = ebuf;
79*3db86aabSstevel }
80