/*
* misc - general miscellaneous routines
*
* Copyright (C) 1992/93/94/95 Stephen Hebditch <steveh@tqmcomms.co.uk>.
* All rights reserved. TQM Communications, BCM Box 225, London, WC1N 3XX.
*
* See README for more information and disclaimers
*
* Assorted miscellaneous routines.
*
* $Id: misc.c,v 1.10 1995/02/07 14:29:36 root Exp root $
*
* $Log: misc.c,v $
* Revision 1.10 1995/02/07 14:29:36 root
* Only write hostfile if moved onto article retrieval stage.
*
* Revision 1.9 1995/01/10 12:56:37 root
* Moved includes from slurp.h.
* Moved the position of log_doit.
*
* Revision 1.7 1993/06/07 11:08:12 root
* Added stradd function.
*
* Revision 1.6 1993/04/22 18:31:11 root
* Hey, it's 1993!
*
* Revision 1.4 1993/02/14 14:53:27 root
* In log_sys if any message ids are present, then submit the
* currently open batch and write out the unretrieved message
* ids to slurp.<hostname> along with the new time.
*
* Revision 1.0 1992/11/27
* Initial coding.
*
*/
#include "conf.h"
/* POSIX header files */
#define _POSIX_SOURCE 1
#include <sys/types.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Local headers */
#include "slurp.h"
#include "syslog.h"
/* File-scope variables */
static int in_log_sys = FALSE;
/*
* stradd - If string1 is not NULL, then concatenate string1 and
* string2, remallocing the space the first occupies to provide
* enough room for them both. If string1 is null, then malloc space
* for string2 and copy string2 to it. Returns location of string.
*/
char *
stradd (const char *string1, const char *string2)
{
char *new;
size_t len;
if (string1 == NULL)
{
len = strlen (string2) + sizeof (char);
if ((new = (char *) malloc (len)) == NULL)
log_sys ("stradd: malloc %d bytes", len);
(void) strcpy (new, string2);
}
else
{
len = strlen (string1) + strlen (string2) + sizeof (char);
if ((new = (char *) realloc ((void *) string1, len)) == NULL)
log_sys ("stradd: realloc %d bytes", len);
(void) strcat (new, string2);
}
return (new);
}
/*
* log_doit - Write an error message to stderr if debug_flag is set or
* syslog if not set. If sysflag is true then the last system error
* message is appended.
*/
static void
log_doit (int sysflag, const char *fmt, va_list ap)
{
char buf [BUFSIZ];
int errnosave;
errnosave = errno;
(void) vsprintf (buf, fmt, ap);
if (sysflag)
(void) sprintf (buf + strlen (buf), ": %s", strerror (errnosave));
(void) strcat (buf, "\n");
#ifdef SYSLOG
if (!debug_flag)
syslog (LOG_ERR, "%s", buf);
else
#endif
(void) fprintf (stderr, "%s: %s", pname, buf);
}
/*
* log_ret - Log a message to stderr or syslog related to a system call
* containing the appropriate system error message and return.
*/
void
log_ret (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
log_doit (TRUE, fmt, ap);
va_end (ap);
return;
}
/*
* log_sys - Log a message to stderr or syslog related to a system call.
* containing the appropriate system error message and exit program.
* If any message ids in the tree then write out slurp.<hostname> file
* and close the batch if open.
*/
void
log_sys (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
log_doit (TRUE, fmt, ap);
va_end (ap);
if ((!in_log_sys) && (totalsize > 0))
{
in_log_sys = TRUE;
enqueue_batch ();
write_hostfile ();
}
exit (1);
}
/*
* log_msg - Log a message to stderr or syslog unrelated to a system call.
*/
void
log_msg (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
log_doit (FALSE, fmt, ap);
va_end (ap);
return;
}
/* END-OF-FILE */
syntax highlighted by Code2HTML, v. 0.9.1