15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland * CDDL HEADER START
35c51f124SMoriah Waterland *
45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland *
85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland * See the License for the specific language governing permissions
115c51f124SMoriah Waterland * and limitations under the License.
125c51f124SMoriah Waterland *
135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland *
195c51f124SMoriah Waterland * CDDL HEADER END
205c51f124SMoriah Waterland */
215c51f124SMoriah Waterland
225c51f124SMoriah Waterland /*
235c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
245c51f124SMoriah Waterland * Use is subject to license terms.
255c51f124SMoriah Waterland */
265c51f124SMoriah Waterland
275c51f124SMoriah Waterland
285c51f124SMoriah Waterland /*
295c51f124SMoriah Waterland * Module: pkgerr.c
305c51f124SMoriah Waterland * Description:
315c51f124SMoriah Waterland * Module for handling error messages that come from libpkg libraries.
325c51f124SMoriah Waterland */
335c51f124SMoriah Waterland
345c51f124SMoriah Waterland #include <stdio.h>
355c51f124SMoriah Waterland #include <string.h>
365c51f124SMoriah Waterland #include <sys/types.h>
375c51f124SMoriah Waterland #include <locale.h>
385c51f124SMoriah Waterland #include <libintl.h>
395c51f124SMoriah Waterland #include <stdlib.h>
405c51f124SMoriah Waterland #include <sys/varargs.h>
415c51f124SMoriah Waterland #include "pkgerr.h"
425c51f124SMoriah Waterland
435c51f124SMoriah Waterland /* max length of any formatted error message */
445c51f124SMoriah Waterland #define MAX_ERRMSGLEN 1024
455c51f124SMoriah Waterland
465c51f124SMoriah Waterland /* private structures (not visible outside this file) */
475c51f124SMoriah Waterland struct _pkg_err_struct {
485c51f124SMoriah Waterland int nerrs;
495c51f124SMoriah Waterland char **msgs;
505c51f124SMoriah Waterland PKG_ERR_CODE *errs;
515c51f124SMoriah Waterland };
525c51f124SMoriah Waterland
535c51f124SMoriah Waterland /* ---------------------- public functions ----------------------- */
545c51f124SMoriah Waterland
555c51f124SMoriah Waterland PKG_ERR
pkgerr_new()565c51f124SMoriah Waterland *pkgerr_new()
575c51f124SMoriah Waterland {
585c51f124SMoriah Waterland PKG_ERR *newerr;
595c51f124SMoriah Waterland
605c51f124SMoriah Waterland newerr = (PKG_ERR *)malloc(sizeof (PKG_ERR));
615c51f124SMoriah Waterland newerr->nerrs = 0;
625c51f124SMoriah Waterland newerr->msgs = NULL;
635c51f124SMoriah Waterland newerr->errs = NULL;
645c51f124SMoriah Waterland return (newerr);
655c51f124SMoriah Waterland }
665c51f124SMoriah Waterland
67*4656d474SGarrett D'Amore /*PRINTFLIKE3*/
685c51f124SMoriah Waterland void
pkgerr_add(PKG_ERR * err,PKG_ERR_CODE code,char * fmt,...)695c51f124SMoriah Waterland pkgerr_add(PKG_ERR *err, PKG_ERR_CODE code, char *fmt, ...)
705c51f124SMoriah Waterland {
715c51f124SMoriah Waterland char errmsgbuf[1024];
725c51f124SMoriah Waterland va_list ap;
735c51f124SMoriah Waterland
745c51f124SMoriah Waterland va_start(ap, fmt);
755c51f124SMoriah Waterland (void) vsnprintf(errmsgbuf, MAX_ERRMSGLEN, fmt, ap);
765c51f124SMoriah Waterland va_end(ap);
775c51f124SMoriah Waterland
785c51f124SMoriah Waterland err->nerrs++;
795c51f124SMoriah Waterland
805c51f124SMoriah Waterland err->msgs = (char **)realloc(err->msgs,
815c51f124SMoriah Waterland err->nerrs * sizeof (char *));
825c51f124SMoriah Waterland err->errs = (PKG_ERR_CODE *)realloc(err->errs,
835c51f124SMoriah Waterland err->nerrs * sizeof (PKG_ERR_CODE));
845c51f124SMoriah Waterland err->msgs[err->nerrs - 1] = strdup(errmsgbuf);
855c51f124SMoriah Waterland err->errs[err->nerrs - 1] = code;
865c51f124SMoriah Waterland }
875c51f124SMoriah Waterland
885c51f124SMoriah Waterland void
pkgerr_clear(PKG_ERR * err)895c51f124SMoriah Waterland pkgerr_clear(PKG_ERR *err)
905c51f124SMoriah Waterland {
915c51f124SMoriah Waterland int i;
925c51f124SMoriah Waterland
935c51f124SMoriah Waterland for (i = 0; i < err->nerrs; i++) {
945c51f124SMoriah Waterland free(err->msgs[i]);
955c51f124SMoriah Waterland }
965c51f124SMoriah Waterland
975c51f124SMoriah Waterland free(err->msgs);
985c51f124SMoriah Waterland free(err->errs);
995c51f124SMoriah Waterland err->msgs = NULL;
1005c51f124SMoriah Waterland err->errs = NULL;
1015c51f124SMoriah Waterland err->nerrs = 0;
1025c51f124SMoriah Waterland }
1035c51f124SMoriah Waterland
1045c51f124SMoriah Waterland int
pkgerr_num(PKG_ERR * err)1055c51f124SMoriah Waterland pkgerr_num(PKG_ERR *err)
1065c51f124SMoriah Waterland {
1075c51f124SMoriah Waterland return (err->nerrs);
1085c51f124SMoriah Waterland }
1095c51f124SMoriah Waterland
1105c51f124SMoriah Waterland char
pkgerr_get(PKG_ERR * err,int pos)1115c51f124SMoriah Waterland *pkgerr_get(PKG_ERR *err, int pos)
1125c51f124SMoriah Waterland {
1135c51f124SMoriah Waterland if (pos < 0 || pos > (err->nerrs - 1)) {
1145c51f124SMoriah Waterland return (NULL);
1155c51f124SMoriah Waterland }
1165c51f124SMoriah Waterland
1175c51f124SMoriah Waterland return (err->msgs[pos]);
1185c51f124SMoriah Waterland }
1195c51f124SMoriah Waterland
1205c51f124SMoriah Waterland void
pkgerr_free(PKG_ERR * err)1215c51f124SMoriah Waterland pkgerr_free(PKG_ERR *err)
1225c51f124SMoriah Waterland {
1235c51f124SMoriah Waterland pkgerr_clear(err);
1245c51f124SMoriah Waterland free(err);
1255c51f124SMoriah Waterland }
126