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