1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * All rights reserved. 5*7c478bd9Sstevel@tonic-gate * 6*7c478bd9Sstevel@tonic-gate * Permission is hereby granted, free of charge, to any person obtaining a 7*7c478bd9Sstevel@tonic-gate * copy of this software and associated documentation files (the 8*7c478bd9Sstevel@tonic-gate * "Software"), to deal in the Software without restriction, including 9*7c478bd9Sstevel@tonic-gate * without limitation the rights to use, copy, modify, merge, publish, 10*7c478bd9Sstevel@tonic-gate * distribute, and/or sell copies of the Software, and to permit persons 11*7c478bd9Sstevel@tonic-gate * to whom the Software is furnished to do so, provided that the above 12*7c478bd9Sstevel@tonic-gate * copyright notice(s) and this permission notice appear in all copies of 13*7c478bd9Sstevel@tonic-gate * the Software and that both the above copyright notice(s) and this 14*7c478bd9Sstevel@tonic-gate * permission notice appear in supporting documentation. 15*7c478bd9Sstevel@tonic-gate * 16*7c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17*7c478bd9Sstevel@tonic-gate * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18*7c478bd9Sstevel@tonic-gate * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 19*7c478bd9Sstevel@tonic-gate * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20*7c478bd9Sstevel@tonic-gate * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 21*7c478bd9Sstevel@tonic-gate * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING 22*7c478bd9Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 23*7c478bd9Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 24*7c478bd9Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 25*7c478bd9Sstevel@tonic-gate * 26*7c478bd9Sstevel@tonic-gate * Except as contained in this notice, the name of a copyright holder 27*7c478bd9Sstevel@tonic-gate * shall not be used in advertising or otherwise to promote the sale, use 28*7c478bd9Sstevel@tonic-gate * or other dealings in this Software without prior written authorization 29*7c478bd9Sstevel@tonic-gate * of the copyright holder. 30*7c478bd9Sstevel@tonic-gate */ 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <string.h> 36*7c478bd9Sstevel@tonic-gate #include <errno.h> 37*7c478bd9Sstevel@tonic-gate #include <stdarg.h> 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #include "errmsg.h" 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate /* 42*7c478bd9Sstevel@tonic-gate * Encapsulate the error reporting buffer in an opaque object. 43*7c478bd9Sstevel@tonic-gate */ 44*7c478bd9Sstevel@tonic-gate struct ErrMsg { 45*7c478bd9Sstevel@tonic-gate char msg[ERR_MSG_LEN+1]; /* An error message */ 46*7c478bd9Sstevel@tonic-gate }; 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /*....................................................................... 49*7c478bd9Sstevel@tonic-gate * Create a new error-message object. 50*7c478bd9Sstevel@tonic-gate * 51*7c478bd9Sstevel@tonic-gate * Output: 52*7c478bd9Sstevel@tonic-gate * return ErrMsg * The new object, or NULL on error. 53*7c478bd9Sstevel@tonic-gate */ 54*7c478bd9Sstevel@tonic-gate ErrMsg *_new_ErrMsg(void) 55*7c478bd9Sstevel@tonic-gate { 56*7c478bd9Sstevel@tonic-gate ErrMsg *err; /* The object to be returned */ 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * Allocate the container. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate err = malloc(sizeof(ErrMsg)); 61*7c478bd9Sstevel@tonic-gate if(!err) { 62*7c478bd9Sstevel@tonic-gate errno = ENOMEM; 63*7c478bd9Sstevel@tonic-gate return NULL; 64*7c478bd9Sstevel@tonic-gate }; 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * Before attempting any operation that might fail, initialize the 67*7c478bd9Sstevel@tonic-gate * container at least up to the point at which it can safely be passed 68*7c478bd9Sstevel@tonic-gate * to del_ErrMsg(). 69*7c478bd9Sstevel@tonic-gate */ 70*7c478bd9Sstevel@tonic-gate err->msg[0] = '\0'; 71*7c478bd9Sstevel@tonic-gate return err; 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate /*....................................................................... 75*7c478bd9Sstevel@tonic-gate * Delete an error-message object. 76*7c478bd9Sstevel@tonic-gate * 77*7c478bd9Sstevel@tonic-gate * Input: 78*7c478bd9Sstevel@tonic-gate * err ErrMsg * The object to be deleted. 79*7c478bd9Sstevel@tonic-gate * Output: 80*7c478bd9Sstevel@tonic-gate * return ErrMsg * The deleted object (always NULL). 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate ErrMsg *_del_ErrMsg(ErrMsg *err) 83*7c478bd9Sstevel@tonic-gate { 84*7c478bd9Sstevel@tonic-gate if(err) { 85*7c478bd9Sstevel@tonic-gate free(err); 86*7c478bd9Sstevel@tonic-gate }; 87*7c478bd9Sstevel@tonic-gate return NULL; 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate /*....................................................................... 91*7c478bd9Sstevel@tonic-gate * Record the concatenation of a list of string arguments in an error 92*7c478bd9Sstevel@tonic-gate * message object. The last argument must be END_ERR_MSG to terminate 93*7c478bd9Sstevel@tonic-gate * the argument list. 94*7c478bd9Sstevel@tonic-gate * 95*7c478bd9Sstevel@tonic-gate * Input: 96*7c478bd9Sstevel@tonic-gate * err ErrMsg * The error-message container. 97*7c478bd9Sstevel@tonic-gate * ... const char * Zero or more strings to be concatenated in buff[]. 98*7c478bd9Sstevel@tonic-gate * ... const char * The last argument must always be END_ERR_MSG to 99*7c478bd9Sstevel@tonic-gate * terminate the argument list. 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate void _err_record_msg(ErrMsg *err, ...) 102*7c478bd9Sstevel@tonic-gate { 103*7c478bd9Sstevel@tonic-gate va_list ap; /* The variable argument list */ 104*7c478bd9Sstevel@tonic-gate const char *s; /* The string being printed */ 105*7c478bd9Sstevel@tonic-gate size_t msglen = 0; /* The total length of the message */ 106*7c478bd9Sstevel@tonic-gate /* 107*7c478bd9Sstevel@tonic-gate * Nowhere to record the result? 108*7c478bd9Sstevel@tonic-gate */ 109*7c478bd9Sstevel@tonic-gate if(!err) { 110*7c478bd9Sstevel@tonic-gate errno = EINVAL; 111*7c478bd9Sstevel@tonic-gate return; 112*7c478bd9Sstevel@tonic-gate }; 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * Concatenate the list of argument strings in err->msg[]. 115*7c478bd9Sstevel@tonic-gate */ 116*7c478bd9Sstevel@tonic-gate va_start(ap, err); 117*7c478bd9Sstevel@tonic-gate while((s = va_arg(ap, const char *)) != END_ERR_MSG) { 118*7c478bd9Sstevel@tonic-gate /* 119*7c478bd9Sstevel@tonic-gate * How much room is left in the output buffer (note that the output 120*7c478bd9Sstevel@tonic-gate * buffer has ERR_MSG_LEN+1 elements). 121*7c478bd9Sstevel@tonic-gate */ 122*7c478bd9Sstevel@tonic-gate int nleft = ERR_MSG_LEN - msglen; 123*7c478bd9Sstevel@tonic-gate /* 124*7c478bd9Sstevel@tonic-gate * How long is the next string to be appended? 125*7c478bd9Sstevel@tonic-gate */ 126*7c478bd9Sstevel@tonic-gate size_t slen = strlen(s); 127*7c478bd9Sstevel@tonic-gate /* 128*7c478bd9Sstevel@tonic-gate * If there is any room left, append as much of the string 129*7c478bd9Sstevel@tonic-gate * as will fit. 130*7c478bd9Sstevel@tonic-gate */ 131*7c478bd9Sstevel@tonic-gate if(nleft > 0) { 132*7c478bd9Sstevel@tonic-gate int nnew = slen < nleft ? slen : nleft; 133*7c478bd9Sstevel@tonic-gate strncpy(err->msg + msglen, s, nnew); 134*7c478bd9Sstevel@tonic-gate msglen += nnew; 135*7c478bd9Sstevel@tonic-gate }; 136*7c478bd9Sstevel@tonic-gate }; 137*7c478bd9Sstevel@tonic-gate va_end(ap); 138*7c478bd9Sstevel@tonic-gate /* 139*7c478bd9Sstevel@tonic-gate * Terminate the message. 140*7c478bd9Sstevel@tonic-gate */ 141*7c478bd9Sstevel@tonic-gate err->msg[msglen] = '\0'; 142*7c478bd9Sstevel@tonic-gate return; 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate /*....................................................................... 146*7c478bd9Sstevel@tonic-gate * Return a pointer to the error message buffer. 147*7c478bd9Sstevel@tonic-gate * 148*7c478bd9Sstevel@tonic-gate * Input: 149*7c478bd9Sstevel@tonic-gate * err ErrMsg * The container of the error message buffer. 150*7c478bd9Sstevel@tonic-gate * Output: 151*7c478bd9Sstevel@tonic-gate * return char * The current error message, or NULL if err==NULL. 152*7c478bd9Sstevel@tonic-gate */ 153*7c478bd9Sstevel@tonic-gate char *_err_get_msg(ErrMsg *err) 154*7c478bd9Sstevel@tonic-gate { 155*7c478bd9Sstevel@tonic-gate return err ? err->msg : NULL; 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate /*....................................................................... 159*7c478bd9Sstevel@tonic-gate * Replace the current error message with an empty string. 160*7c478bd9Sstevel@tonic-gate * 161*7c478bd9Sstevel@tonic-gate * Input: 162*7c478bd9Sstevel@tonic-gate * err ErrMsg * The container of the error message buffer. 163*7c478bd9Sstevel@tonic-gate */ 164*7c478bd9Sstevel@tonic-gate void _err_clear_msg(ErrMsg *err) 165*7c478bd9Sstevel@tonic-gate { 166*7c478bd9Sstevel@tonic-gate if(err) 167*7c478bd9Sstevel@tonic-gate err->msg[0] = '\0'; 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170