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