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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stddef.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <sys/param.h>
34 #include <config_admin.h>
35 #include "mema_util.h"
36 
37 /*
38  * The libmemadm routines can return arbitrary error strings.  As the
39  * calling program does not know how long these errors might be,
40  * the library routines must allocate the required space and the
41  * calling program must deallocate it.
42  *
43  * This routine povides a printf-like interface for creating the
44  * error strings.
45  */
46 
47 #define	FMT_STR_SLOP		(16)
48 
49 void
__fmt_errstring(char ** errstring,size_t extra_length_hint,const char * fmt,...)50 __fmt_errstring(
51 	char **errstring,
52 	size_t extra_length_hint,
53 	const char *fmt,
54 	...)
55 {
56 	char *ebuf;
57 	size_t elen;
58 	va_list ap;
59 
60 	/*
61 	 * If no errors required or error already set, return.
62 	 */
63 	if ((errstring == NULL) || (*errstring != NULL))
64 		return;
65 
66 	elen = strlen(fmt) + extra_length_hint + FMT_STR_SLOP;
67 
68 	if ((ebuf = (char *)malloc(elen + 1)) == NULL)
69 		return;
70 
71 	va_start(ap, fmt);
72 	(void) vsprintf(ebuf, fmt, ap);
73 	va_end(ap);
74 
75 	if (strlen(ebuf) > elen)
76 		abort();
77 
78 	*errstring = ebuf;
79 }
80