xref: /titanic_44/usr/src/lib/libpkg/common/pkgerr.c (revision 349b53dd4e695e3d833b5380540385145b2d3ae8)
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 void
68 pkgerr_add(PKG_ERR *err, PKG_ERR_CODE code, char *fmt, ...)
69 {
70 	char		errmsgbuf[1024];
71 	va_list		ap;
72 
73 	va_start(ap, fmt);
74 	(void) vsnprintf(errmsgbuf, MAX_ERRMSGLEN, fmt, ap);
75 	va_end(ap);
76 
77 	err->nerrs++;
78 
79 	err->msgs = (char **)realloc(err->msgs,
80 	    err->nerrs * sizeof (char *));
81 	err->errs = (PKG_ERR_CODE *)realloc(err->errs,
82 	    err->nerrs * sizeof (PKG_ERR_CODE));
83 	err->msgs[err->nerrs - 1] = strdup(errmsgbuf);
84 	err->errs[err->nerrs - 1] = code;
85 }
86 
87 void
88 pkgerr_clear(PKG_ERR *err)
89 {
90 	int i;
91 
92 	for (i = 0; i < err->nerrs; i++) {
93 		free(err->msgs[i]);
94 	}
95 
96 	free(err->msgs);
97 	free(err->errs);
98 	err->msgs = NULL;
99 	err->errs = NULL;
100 	err->nerrs = 0;
101 }
102 
103 int
104 pkgerr_dump(PKG_ERR *err, FILE *fp)
105 {
106 	int i;
107 
108 	for (i = 0; i < err->nerrs; i++) {
109 		(void) fprintf(fp, err->msgs[i]);
110 	}
111 	return (0);
112 }
113 
114 int
115 pkgerr_num(PKG_ERR *err)
116 {
117 	return (err->nerrs);
118 }
119 
120 char
121 *pkgerr_get(PKG_ERR *err, int pos)
122 {
123 	if (pos < 0 || pos > (err->nerrs - 1)) {
124 		return (NULL);
125 	}
126 
127 	return (err->msgs[pos]);
128 }
129 
130 void
131 pkgerr_free(PKG_ERR *err)
132 {
133 	pkgerr_clear(err);
134 	free(err);
135 }
136