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