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 #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #include "errmsg.h"
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate * Encapsulate the error reporting buffer in an opaque object.
41*7c478bd9Sstevel@tonic-gate */
42*7c478bd9Sstevel@tonic-gate struct ErrMsg {
43*7c478bd9Sstevel@tonic-gate char msg[ERR_MSG_LEN+1]; /* An error message */
44*7c478bd9Sstevel@tonic-gate };
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate /*.......................................................................
47*7c478bd9Sstevel@tonic-gate * Create a new error-message object.
48*7c478bd9Sstevel@tonic-gate *
49*7c478bd9Sstevel@tonic-gate * Output:
50*7c478bd9Sstevel@tonic-gate * return ErrMsg * The new object, or NULL on error.
51*7c478bd9Sstevel@tonic-gate */
_new_ErrMsg(void)52*7c478bd9Sstevel@tonic-gate ErrMsg *_new_ErrMsg(void)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate ErrMsg *err; /* The object to be returned */
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate * Allocate the container.
57*7c478bd9Sstevel@tonic-gate */
58*7c478bd9Sstevel@tonic-gate err = malloc(sizeof(ErrMsg));
59*7c478bd9Sstevel@tonic-gate if(!err) {
60*7c478bd9Sstevel@tonic-gate errno = ENOMEM;
61*7c478bd9Sstevel@tonic-gate return NULL;
62*7c478bd9Sstevel@tonic-gate };
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate * Before attempting any operation that might fail, initialize the
65*7c478bd9Sstevel@tonic-gate * container at least up to the point at which it can safely be passed
66*7c478bd9Sstevel@tonic-gate * to del_ErrMsg().
67*7c478bd9Sstevel@tonic-gate */
68*7c478bd9Sstevel@tonic-gate err->msg[0] = '\0';
69*7c478bd9Sstevel@tonic-gate return err;
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate /*.......................................................................
73*7c478bd9Sstevel@tonic-gate * Delete an error-message object.
74*7c478bd9Sstevel@tonic-gate *
75*7c478bd9Sstevel@tonic-gate * Input:
76*7c478bd9Sstevel@tonic-gate * err ErrMsg * The object to be deleted.
77*7c478bd9Sstevel@tonic-gate * Output:
78*7c478bd9Sstevel@tonic-gate * return ErrMsg * The deleted object (always NULL).
79*7c478bd9Sstevel@tonic-gate */
_del_ErrMsg(ErrMsg * err)80*7c478bd9Sstevel@tonic-gate ErrMsg *_del_ErrMsg(ErrMsg *err)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate if(err) {
83*7c478bd9Sstevel@tonic-gate free(err);
84*7c478bd9Sstevel@tonic-gate };
85*7c478bd9Sstevel@tonic-gate return NULL;
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate /*.......................................................................
89*7c478bd9Sstevel@tonic-gate * Record the concatenation of a list of string arguments in an error
90*7c478bd9Sstevel@tonic-gate * message object. The last argument must be END_ERR_MSG to terminate
91*7c478bd9Sstevel@tonic-gate * the argument list.
92*7c478bd9Sstevel@tonic-gate *
93*7c478bd9Sstevel@tonic-gate * Input:
94*7c478bd9Sstevel@tonic-gate * err ErrMsg * The error-message container.
95*7c478bd9Sstevel@tonic-gate * ... const char * Zero or more strings to be concatenated in buff[].
96*7c478bd9Sstevel@tonic-gate * ... const char * The last argument must always be END_ERR_MSG to
97*7c478bd9Sstevel@tonic-gate * terminate the argument list.
98*7c478bd9Sstevel@tonic-gate */
_err_record_msg(ErrMsg * err,...)99*7c478bd9Sstevel@tonic-gate void _err_record_msg(ErrMsg *err, ...)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate va_list ap; /* The variable argument list */
102*7c478bd9Sstevel@tonic-gate const char *s; /* The string being printed */
103*7c478bd9Sstevel@tonic-gate size_t msglen = 0; /* The total length of the message */
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate * Nowhere to record the result?
106*7c478bd9Sstevel@tonic-gate */
107*7c478bd9Sstevel@tonic-gate if(!err) {
108*7c478bd9Sstevel@tonic-gate errno = EINVAL;
109*7c478bd9Sstevel@tonic-gate return;
110*7c478bd9Sstevel@tonic-gate };
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate * Concatenate the list of argument strings in err->msg[].
113*7c478bd9Sstevel@tonic-gate */
114*7c478bd9Sstevel@tonic-gate va_start(ap, err);
115*7c478bd9Sstevel@tonic-gate while((s = va_arg(ap, const char *)) != END_ERR_MSG) {
116*7c478bd9Sstevel@tonic-gate /*
117*7c478bd9Sstevel@tonic-gate * How much room is left in the output buffer (note that the output
118*7c478bd9Sstevel@tonic-gate * buffer has ERR_MSG_LEN+1 elements).
119*7c478bd9Sstevel@tonic-gate */
120*7c478bd9Sstevel@tonic-gate int nleft = ERR_MSG_LEN - msglen;
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate * How long is the next string to be appended?
123*7c478bd9Sstevel@tonic-gate */
124*7c478bd9Sstevel@tonic-gate size_t slen = strlen(s);
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate * If there is any room left, append as much of the string
127*7c478bd9Sstevel@tonic-gate * as will fit.
128*7c478bd9Sstevel@tonic-gate */
129*7c478bd9Sstevel@tonic-gate if(nleft > 0) {
130*7c478bd9Sstevel@tonic-gate int nnew = slen < nleft ? slen : nleft;
131*7c478bd9Sstevel@tonic-gate strncpy(err->msg + msglen, s, nnew);
132*7c478bd9Sstevel@tonic-gate msglen += nnew;
133*7c478bd9Sstevel@tonic-gate };
134*7c478bd9Sstevel@tonic-gate };
135*7c478bd9Sstevel@tonic-gate va_end(ap);
136*7c478bd9Sstevel@tonic-gate /*
137*7c478bd9Sstevel@tonic-gate * Terminate the message.
138*7c478bd9Sstevel@tonic-gate */
139*7c478bd9Sstevel@tonic-gate err->msg[msglen] = '\0';
140*7c478bd9Sstevel@tonic-gate return;
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate /*.......................................................................
144*7c478bd9Sstevel@tonic-gate * Return a pointer to the error message buffer.
145*7c478bd9Sstevel@tonic-gate *
146*7c478bd9Sstevel@tonic-gate * Input:
147*7c478bd9Sstevel@tonic-gate * err ErrMsg * The container of the error message buffer.
148*7c478bd9Sstevel@tonic-gate * Output:
149*7c478bd9Sstevel@tonic-gate * return char * The current error message, or NULL if err==NULL.
150*7c478bd9Sstevel@tonic-gate */
_err_get_msg(ErrMsg * err)151*7c478bd9Sstevel@tonic-gate char *_err_get_msg(ErrMsg *err)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate return err ? err->msg : NULL;
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate /*.......................................................................
157*7c478bd9Sstevel@tonic-gate * Replace the current error message with an empty string.
158*7c478bd9Sstevel@tonic-gate *
159*7c478bd9Sstevel@tonic-gate * Input:
160*7c478bd9Sstevel@tonic-gate * err ErrMsg * The container of the error message buffer.
161*7c478bd9Sstevel@tonic-gate */
_err_clear_msg(ErrMsg * err)162*7c478bd9Sstevel@tonic-gate void _err_clear_msg(ErrMsg *err)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate if(err)
165*7c478bd9Sstevel@tonic-gate err->msg[0] = '\0';
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate
168