8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libtecla / common / errmsg.c
blob6dd30f523533566d764ad18176b65ab814e73693
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.
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"
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdarg.h>
39 #include "errmsg.h"
42 * Encapsulate the error reporting buffer in an opaque object.
44 struct ErrMsg {
45 char msg[ERR_MSG_LEN+1]; /* An error message */
48 /*.......................................................................
49 * Create a new error-message object.
51 * Output:
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));
61 if(!err) {
62 errno = ENOMEM;
63 return NULL;
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().
70 err->msg[0] = '\0';
71 return err;
74 /*.......................................................................
75 * Delete an error-message object.
77 * Input:
78 * err ErrMsg * The object to be deleted.
79 * Output:
80 * return ErrMsg * The deleted object (always NULL).
82 ErrMsg *_del_ErrMsg(ErrMsg *err)
84 if(err) {
85 free(err);
87 return NULL;
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.
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.
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?
109 if(!err) {
110 errno = EINVAL;
111 return;
114 * Concatenate the list of argument strings in err->msg[].
116 va_start(ap, err);
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
129 * as will fit.
131 if(nleft > 0) {
132 int nnew = slen < nleft ? slen : nleft;
133 strncpy(err->msg + msglen, s, nnew);
134 msglen += nnew;
137 va_end(ap);
139 * Terminate the message.
141 err->msg[msglen] = '\0';
142 return;
145 /*.......................................................................
146 * Return a pointer to the error message buffer.
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.
153 char *_err_get_msg(ErrMsg *err)
155 return err ? err->msg : NULL;
158 /*.......................................................................
159 * Replace the current error message with an empty string.
161 * Input:
162 * err ErrMsg * The container of the error message buffer.
164 void _err_clear_msg(ErrMsg *err)
166 if(err)
167 err->msg[0] = '\0';