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