/*
 * $Id: fakesyslog.c,v 1.9 1995/01/10 12:52:35 root Exp $
 *
 * Fake syslog routines for systems that don't have syslog. Taken from
 * the nntp 1.5.11 package and cleaned up and modernised.
 *
 * $Log: fakesyslog.c,v $
 * Revision 1.9  1995/01/10  12:52:35  root
 * Rewritten for POSIX conformance.
 *
 * Revision 1.7  1993/06/07  11:05:52  root
 * Include fcntl.h if SYSV is defined, rather than if FCNTL is.
 *
 * Revision 1.4  1993/02/14  14:51:30  root
 * No changes.
 *
 */

#include "conf.h"

#ifdef FAKESYSLOG

/* POSIX headers */

#include <sys/signal.h>
#include <sys/types.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>

/* Local headers */

#include "slurp.h"
#include "syslog.h"

/* File-scope variables */

static FILE *logfp;
static int failed = 0;
static char *ident = "syslog";
static int  opt = 0;
static int fac = 0;


	int
resetlog (void)
	{
	closelog ();
	failed = 0;
	if (logfp == NULL)
		{
		openlog (ident, opt, fac);
		if (logfp == NULL)
			{
			failed = 1;
			return;
			}
		}
	}


	int
openlog (const char *newident, const int logopt, const int facility)
	{
	logfp = fopen (FAKESYSLOG, "a");

	(void) signal (SIGHUP, resetlog);

	if ((newident) && (*newident))
		ident = (char *) newident;
	opt = logopt;
	fac = facility;
	}


	int
closelog (void)
	{
	if (logfp != NULL)
		{
		(void) fclose (logfp);
		failed = 0;
		logfp = NULL;
		}
	}


	int
setlogmask (const int maskpri)
	{
	}


	int
syslog (const int pri, const char *fmt, ...)
	{
	char buf [1024];
	char *bp;
	char *cp;
	time_t clock;
	va_list ap;

	va_start (ap, fmt);

	if (failed)
		{
		va_end (ap);
		return;
		}

	if (logfp == NULL)
		{
		openlog (ident, opt, fac);
		if (logfp == NULL)
			{
			failed = 1;
			va_end (ap);
			return;
			}
		}

	(void) time (&clock);
	(void) strcpy (buf, ctime (&clock) + 4);
	*(bp = buf + 16) = '\0';

	(void) sprintf (bp, "localhost %s", ident ? ident : "");
	bp += strlen (bp);

	if (opt & LOG_PID)
		{
		/* don't cache getpid() - who knows when we'll fork() */
		(void) sprintf (bp, "[%d]", getpid ());
		bp += strlen (bp);
		}

	if (ident)
		{
		(void) strcat (bp, ": ");
		bp += 2;
		}
	else
		{
		(void) strcat (bp, " ");
		bp++;
		}

	for (cp = (char *) fmt; *cp != '\0'; cp++)
		{
		if (*cp == '%' && cp [1] == 'm')
			{
			*bp = '\0';
			(void) strcat (bp, strerror (errno));
			bp = buf + strlen (buf);
			cp++;
			}
		else
			*bp++ = *cp;
		}
	*bp = '\0';

	if (bp [-1] != '\n')
		(void) strcat (bp, "\n");

	(void) vfprintf (logfp, buf, ap);
	(void) fflush (logfp);

	va_end (ap);
	}

#endif


syntax highlighted by Code2HTML, v. 0.9.1