/*
Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/
/* configure all options BEFORE including system stuff. */
#include "config.h"
#include "mud.h"
/*
LOW-level miscellaneous routines. Do not call anything high level
from in here. that goes in misc2.c
*/
static FILE *logfil = (FILE *) 0;
int logf_open (char *f)
{
if (logfil != (FILE *) 0)
(void) fclose (logfil);
logfil = fopen (f, "ab");
if (logfil == (FILE *) 0)
return (1);
return (0);
}
void logf_close ()
{
if (logfil != (FILE *) 0)
(void) fclose (logfil);
logfil = (FILE *) 0;
}
/*
fatal error handler. puts strings to stderr as given. if a
given string is == -1, then put the system error message instead.
*/
/* VARARGS */
void fatal (char *first, ...)
{
char *p = first;
va_list ap;
if (logfil == (FILE *) 0) {
WIN32CLEANUP
exit (1);
}
va_start (ap, first);
do {
if (p == (char *) 0)
break;
if (p == (char *) -1)
p = (char *) sys_errlist[errno];
(void) fprintf (logfil, "%s", p);
(void) fprintf (stderr, "%s", p);
} while (p = va_arg (ap, char *));
va_end (ap);
(void) fclose (logfil);
WIN32CLEANUP
exit (1);
}
extern char *ctime ();
/*
print a series of warnings - do not exit
*/
/* VARARGS */
void log_printf (char *first, ...) {
char *p = first;
char *ts;
time_t tm;
va_list ap;
if (logfil == (FILE *) 0)
return;
tm = time ((time_t *) 0);
ts = ctime (&tm);
ts[24] = 0;
(void) fprintf (logfil, "%s ", ts + 4);
va_start (ap, first);
do {
if (p == (char *) 0)
break;
if (p == (char *) -1)
p = (char *) sys_errlist[errno];
(void) fprintf (logfil, "%s", p);
} while (p = va_arg (ap, char *));
va_end (ap);
(void) fflush (logfil);
}
/* print a series of warnings, with a printf() interface. */
/*VARARGS0*/
void plogf (char *fmt, ...) {
va_list args;
char *ts;
#ifdef NO_VFPRINTF
char *a[4];
int argc = 0;
#endif
time_t t;
if (logfil == (FILE *) 0)
return;
t = time ((time_t *) 0);
ts = ctime (&t);
ts[24] = 0;
(void) fprintf (logfil, "%s ", ts + 4);
va_start (args, fmt);
#ifdef NO_VFPRINTF
while ((a[argc++] = va_arg (args, char *)) != NULL)
if (argc >= 4)
break;
switch (argc) {
case 1:
(void) fprintf (logfil, fmt, a[0]);
break;
case 2:
(void) fprintf (logfil, fmt, a[0], a[1]);
break;
case 3:
(void) fprintf (logfil, fmt, a[0], a[1], a[2]);
break;
default:
(void) fprintf (logfil, fmt, a[0], a[1], a[2], a[3]);
break;
}
#else
(void) vfprintf (logfil, fmt, args);
#endif
va_end (args);
fflush (logfil);
}
/*
WARNING - overflows ignored. no big deal
*/
unsigned int objid_hash (char *oid, int hw) {
unsigned int n = 0;
while (*oid != '\0' && isdigit (*oid))
n = *oid++ + 65599 * n;
return (n % hw);
}
/* WARNING - may need to do different things to set up random #s */
int get_random (int num) {
static int initt = 0;
if (!initt) {
OS_SRAND (time ((time_t *) 0));
initt++;
}
return ((int) OS_RAND () % num);
}
/* from K&R, mostly */
// base ignored but added to signature for Windows compatability - JAL
char *itoa (int num, char *rbuf, int base) {
int sign;
int i = 0;
if ((sign = num) < 0)
num = -num;
do {
rbuf[i++] = num % 10 + '0';
} while ((num /= 10) > 0);
if (sign < 0)
rbuf[i++] = '-';
rbuf[i--] = '\0';
for (sign = 0; sign < i; sign++, i--) {
num = rbuf[sign];
rbuf[sign] = rbuf[i];
rbuf[i] = num;
}
return (rbuf);
}