xref: /illumos-gate/usr/src/lib/libtecla/common/errmsg.c (revision 4eaa471005973e11a6110b69fe990530b3b95a38)
1 /*
2  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3  *
4  * All rights reserved.
5  *
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.
15  *
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.
25  *
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.
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 
39 #include "errmsg.h"
40 
41 /*
42  * Encapsulate the error reporting buffer in an opaque object.
43  */
44 struct ErrMsg {
45   char msg[ERR_MSG_LEN+1];  /* An error message */
46 };
47 
48 /*.......................................................................
49  * Create a new error-message object.
50  *
51  * Output:
52  *  return  ErrMsg *  The new object, or NULL on error.
53  */
54 ErrMsg *_new_ErrMsg(void)
55 {
56   ErrMsg *err;  /* The object to be returned */
57 /*
58  * Allocate the container.
59  */
60   err = malloc(sizeof(ErrMsg));
61   if(!err) {
62     errno = ENOMEM;
63     return NULL;
64   };
65 /*
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
68  * to del_ErrMsg().
69  */
70   err->msg[0] = '\0';
71   return err;
72 }
73 
74 /*.......................................................................
75  * Delete an error-message object.
76  *
77  * Input:
78  *  err     ErrMsg *  The object to be deleted.
79  * Output:
80  *  return  ErrMsg *  The deleted object (always NULL).
81  */
82 ErrMsg *_del_ErrMsg(ErrMsg *err)
83 {
84   if(err) {
85     free(err);
86   };
87   return NULL;
88 }
89 
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
93  * the argument list.
94  *
95  * Input:
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.
100  */
101 void _err_record_msg(ErrMsg *err, ...)
102 {
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 */
106 /*
107  * Nowhere to record the result?
108  */
109   if(!err) {
110     errno = EINVAL;
111     return;
112   };
113 /*
114  * Concatenate the list of argument strings in err->msg[].
115  */
116   va_start(ap, err);
117   while((s = va_arg(ap, const char *)) != END_ERR_MSG) {
118 /*
119  * How much room is left in the output buffer (note that the output
120  * buffer has ERR_MSG_LEN+1 elements).
121  */
122     int nleft = ERR_MSG_LEN - msglen;
123 /*
124  * How long is the next string to be appended?
125  */
126     size_t slen = strlen(s);
127 /*
128  * If there is any room left, append as much of the string
129  * as will fit.
130  */
131     if(nleft > 0) {
132       int nnew = slen < nleft ? slen : nleft;
133       strncpy(err->msg + msglen, s, nnew);
134       msglen += nnew;
135     };
136   };
137   va_end(ap);
138 /*
139  * Terminate the message.
140  */
141   err->msg[msglen] = '\0';
142   return;
143 }
144 
145 /*.......................................................................
146  * Return a pointer to the error message buffer.
147  *
148  * Input:
149  *  err     ErrMsg *  The container of the error message buffer.
150  * Output:
151  *  return    char *  The current error message, or NULL if err==NULL.
152  */
153 char *_err_get_msg(ErrMsg *err)
154 {
155   return err ? err->msg : NULL;
156 }
157 
158 /*.......................................................................
159  * Replace the current error message with an empty string.
160  *
161  * Input:
162  *  err     ErrMsg *  The container of the error message buffer.
163  */
164 void _err_clear_msg(ErrMsg *err)
165 {
166   if(err)
167     err->msg[0] = '\0';
168 }
169 
170