17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*1da57d55SToomas Soome  *
47c478bd9Sstevel@tonic-gate  * All rights reserved.
5*1da57d55SToomas Soome  *
67c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
77c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
87c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
97c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
107c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
117c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
127c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
137c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
147c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*1da57d55SToomas Soome  *
167c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
187c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
197c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
207c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
217c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
227c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
237c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
247c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*1da57d55SToomas Soome  *
267c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
277c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
287c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
297c478bd9Sstevel@tonic-gate  * of the copyright holder.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <stdarg.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "errmsg.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Encapsulate the error reporting buffer in an opaque object.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate struct ErrMsg {
437c478bd9Sstevel@tonic-gate   char msg[ERR_MSG_LEN+1];  /* An error message */
447c478bd9Sstevel@tonic-gate };
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*.......................................................................
477c478bd9Sstevel@tonic-gate  * Create a new error-message object.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Output:
507c478bd9Sstevel@tonic-gate  *  return  ErrMsg *  The new object, or NULL on error.
517c478bd9Sstevel@tonic-gate  */
_new_ErrMsg(void)527c478bd9Sstevel@tonic-gate ErrMsg *_new_ErrMsg(void)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate   ErrMsg *err;  /* The object to be returned */
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Allocate the container.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate   err = malloc(sizeof(ErrMsg));
597c478bd9Sstevel@tonic-gate   if(!err) {
607c478bd9Sstevel@tonic-gate     errno = ENOMEM;
617c478bd9Sstevel@tonic-gate     return NULL;
627c478bd9Sstevel@tonic-gate   };
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
657c478bd9Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
667c478bd9Sstevel@tonic-gate  * to del_ErrMsg().
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate   err->msg[0] = '\0';
697c478bd9Sstevel@tonic-gate   return err;
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*.......................................................................
737c478bd9Sstevel@tonic-gate  * Delete an error-message object.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Input:
767c478bd9Sstevel@tonic-gate  *  err     ErrMsg *  The object to be deleted.
777c478bd9Sstevel@tonic-gate  * Output:
787c478bd9Sstevel@tonic-gate  *  return  ErrMsg *  The deleted object (always NULL).
797c478bd9Sstevel@tonic-gate  */
_del_ErrMsg(ErrMsg * err)807c478bd9Sstevel@tonic-gate ErrMsg *_del_ErrMsg(ErrMsg *err)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate   if(err) {
837c478bd9Sstevel@tonic-gate     free(err);
847c478bd9Sstevel@tonic-gate   };
857c478bd9Sstevel@tonic-gate   return NULL;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*.......................................................................
897c478bd9Sstevel@tonic-gate  * Record the concatenation of a list of string arguments in an error
907c478bd9Sstevel@tonic-gate  * message object. The last argument must be END_ERR_MSG to terminate
917c478bd9Sstevel@tonic-gate  * the argument list.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * Input:
947c478bd9Sstevel@tonic-gate  *  err      ErrMsg *   The error-message container.
957c478bd9Sstevel@tonic-gate  *  ...  const char *   Zero or more strings to be concatenated in buff[].
967c478bd9Sstevel@tonic-gate  *  ...  const char *   The last argument must always be END_ERR_MSG to
977c478bd9Sstevel@tonic-gate  *                      terminate the argument list.
987c478bd9Sstevel@tonic-gate  */
_err_record_msg(ErrMsg * err,...)997c478bd9Sstevel@tonic-gate void _err_record_msg(ErrMsg *err, ...)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate   va_list ap;         /* The variable argument list */
1027c478bd9Sstevel@tonic-gate   const char *s;      /* The string being printed */
1037c478bd9Sstevel@tonic-gate   size_t msglen = 0;  /* The total length of the message */
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Nowhere to record the result?
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate   if(!err) {
1087c478bd9Sstevel@tonic-gate     errno = EINVAL;
1097c478bd9Sstevel@tonic-gate     return;
1107c478bd9Sstevel@tonic-gate   };
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Concatenate the list of argument strings in err->msg[].
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate   va_start(ap, err);
1157c478bd9Sstevel@tonic-gate   while((s = va_arg(ap, const char *)) != END_ERR_MSG) {
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * How much room is left in the output buffer (note that the output
1187c478bd9Sstevel@tonic-gate  * buffer has ERR_MSG_LEN+1 elements).
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate     int nleft = ERR_MSG_LEN - msglen;
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * How long is the next string to be appended?
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate     size_t slen = strlen(s);
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * If there is any room left, append as much of the string
1277c478bd9Sstevel@tonic-gate  * as will fit.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate     if(nleft > 0) {
1307c478bd9Sstevel@tonic-gate       int nnew = slen < nleft ? slen : nleft;
1317c478bd9Sstevel@tonic-gate       strncpy(err->msg + msglen, s, nnew);
1327c478bd9Sstevel@tonic-gate       msglen += nnew;
1337c478bd9Sstevel@tonic-gate     };
1347c478bd9Sstevel@tonic-gate   };
1357c478bd9Sstevel@tonic-gate   va_end(ap);
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Terminate the message.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate   err->msg[msglen] = '\0';
1407c478bd9Sstevel@tonic-gate   return;
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /*.......................................................................
1447c478bd9Sstevel@tonic-gate  * Return a pointer to the error message buffer.
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  * Input:
1477c478bd9Sstevel@tonic-gate  *  err     ErrMsg *  The container of the error message buffer.
1487c478bd9Sstevel@tonic-gate  * Output:
1497c478bd9Sstevel@tonic-gate  *  return    char *  The current error message, or NULL if err==NULL.
1507c478bd9Sstevel@tonic-gate  */
_err_get_msg(ErrMsg * err)1517c478bd9Sstevel@tonic-gate char *_err_get_msg(ErrMsg *err)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate   return err ? err->msg : NULL;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*.......................................................................
1577c478bd9Sstevel@tonic-gate  * Replace the current error message with an empty string.
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * Input:
1607c478bd9Sstevel@tonic-gate  *  err     ErrMsg *  The container of the error message buffer.
1617c478bd9Sstevel@tonic-gate  */
_err_clear_msg(ErrMsg * err)1627c478bd9Sstevel@tonic-gate void _err_clear_msg(ErrMsg *err)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate   if(err)
1657c478bd9Sstevel@tonic-gate     err->msg[0] = '\0';
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
168