xref: /titanic_52/usr/src/lib/libpkg/common/pkgerr.c (revision 26f3cdf03f1adcc98f6d3d99843ee71e9229a8c0)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 /*
29  * Module:	pkgerr.c
30  * Description:
31  *	Module for handling error messages that come from libpkg libraries.
32  */
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <stdlib.h>
40 #include <sys/varargs.h>
41 #include "pkgerr.h"
42 
43 /* max length of any formatted error message */
44 #define	MAX_ERRMSGLEN	1024
45 
46 /* private structures (not visible outside this file) */
47 struct _pkg_err_struct {
48     int			nerrs;
49     char		**msgs;
50     PKG_ERR_CODE	*errs;
51 };
52 
53 /* ---------------------- public functions ----------------------- */
54 
55 PKG_ERR
56 *pkgerr_new()
57 {
58 	PKG_ERR	*newerr;
59 
60 	newerr = (PKG_ERR *)malloc(sizeof (PKG_ERR));
61 	newerr->nerrs = 0;
62 	newerr->msgs = NULL;
63 	newerr->errs = NULL;
64 	return (newerr);
65 }
66 
67 /*PRINTFLIKE3*/
68 void
69 pkgerr_add(PKG_ERR *err, PKG_ERR_CODE code, char *fmt, ...)
70 {
71 	char		errmsgbuf[1024];
72 	va_list		ap;
73 
74 	va_start(ap, fmt);
75 	(void) vsnprintf(errmsgbuf, MAX_ERRMSGLEN, fmt, ap);
76 	va_end(ap);
77 
78 	err->nerrs++;
79 
80 	err->msgs = (char **)realloc(err->msgs,
81 	    err->nerrs * sizeof (char *));
82 	err->errs = (PKG_ERR_CODE *)realloc(err->errs,
83 	    err->nerrs * sizeof (PKG_ERR_CODE));
84 	err->msgs[err->nerrs - 1] = strdup(errmsgbuf);
85 	err->errs[err->nerrs - 1] = code;
86 }
87 
88 void
89 pkgerr_clear(PKG_ERR *err)
90 {
91 	int i;
92 
93 	for (i = 0; i < err->nerrs; i++) {
94 		free(err->msgs[i]);
95 	}
96 
97 	free(err->msgs);
98 	free(err->errs);
99 	err->msgs = NULL;
100 	err->errs = NULL;
101 	err->nerrs = 0;
102 }
103 
104 int
105 pkgerr_num(PKG_ERR *err)
106 {
107 	return (err->nerrs);
108 }
109 
110 char
111 *pkgerr_get(PKG_ERR *err, int pos)
112 {
113 	if (pos < 0 || pos > (err->nerrs - 1)) {
114 		return (NULL);
115 	}
116 
117 	return (err->msgs[pos]);
118 }
119 
120 void
121 pkgerr_free(PKG_ERR *err)
122 {
123 	pkgerr_clear(err);
124 	free(err);
125 }
126