2 * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, and/or sell copies of the Software, and to permit persons
11 * to whom the Software is furnished to do so, provided that the above
12 * copyright notice(s) and this permission notice appear in all copies of
13 * the Software and that both the above copyright notice(s) and this
14 * permission notice appear in supporting documentation.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder
27 * shall not be used in advertising or otherwise to promote the sale, use
28 * or other dealings in this Software without prior written authorization
29 * of the copyright holder.
32 #pragma ident "%Z%%M% %I% %E% SMI"
42 * Encapsulate the error reporting buffer in an opaque object.
45 char msg
[ERR_MSG_LEN
+1]; /* An error message */
48 /*.......................................................................
49 * Create a new error-message object.
52 * return ErrMsg * The new object, or NULL on error.
54 ErrMsg
*_new_ErrMsg(void)
56 ErrMsg
*err
; /* The object to be returned */
58 * Allocate the container.
60 err
= malloc(sizeof(ErrMsg
));
66 * Before attempting any operation that might fail, initialize the
67 * container at least up to the point at which it can safely be passed
74 /*.......................................................................
75 * Delete an error-message object.
78 * err ErrMsg * The object to be deleted.
80 * return ErrMsg * The deleted object (always NULL).
82 ErrMsg
*_del_ErrMsg(ErrMsg
*err
)
90 /*.......................................................................
91 * Record the concatenation of a list of string arguments in an error
92 * message object. The last argument must be END_ERR_MSG to terminate
96 * err ErrMsg * The error-message container.
97 * ... const char * Zero or more strings to be concatenated in buff[].
98 * ... const char * The last argument must always be END_ERR_MSG to
99 * terminate the argument list.
101 void _err_record_msg(ErrMsg
*err
, ...)
103 va_list ap
; /* The variable argument list */
104 const char *s
; /* The string being printed */
105 size_t msglen
= 0; /* The total length of the message */
107 * Nowhere to record the result?
114 * Concatenate the list of argument strings in err->msg[].
117 while((s
= va_arg(ap
, const char *)) != END_ERR_MSG
) {
119 * How much room is left in the output buffer (note that the output
120 * buffer has ERR_MSG_LEN+1 elements).
122 int nleft
= ERR_MSG_LEN
- msglen
;
124 * How long is the next string to be appended?
126 size_t slen
= strlen(s
);
128 * If there is any room left, append as much of the string
132 int nnew
= slen
< nleft
? slen
: nleft
;
133 strncpy(err
->msg
+ msglen
, s
, nnew
);
139 * Terminate the message.
141 err
->msg
[msglen
] = '\0';
145 /*.......................................................................
146 * Return a pointer to the error message buffer.
149 * err ErrMsg * The container of the error message buffer.
151 * return char * The current error message, or NULL if err==NULL.
153 char *_err_get_msg(ErrMsg
*err
)
155 return err
? err
->msg
: NULL
;
158 /*.......................................................................
159 * Replace the current error message with an empty string.
162 * err ErrMsg * The container of the error message buffer.
164 void _err_clear_msg(ErrMsg
*err
)