/*
Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
*/
#ifndef lint
static char RCSid[] = "$Header: /usr/users/mjr/hacks/umud/RCS/misc.c,v 1.3 91/08/24 22:40:22 mjr Exp $";
#endif
/* configure all options BEFORE including system stuff. */
#include "config.h"
#include <stdio.h>
#ifndef NOSYSTYPES_H
#include <sys/types.h>
#endif
#include <ctype.h>
#include <varargs.h>
#include <errno.h>
#include <time.h>
#include "mud.h"
/*
LOW-level miscellaneous routines. Do not call anything high level
from in here. that goes in misc2.c
*/
extern int errno;
extern char *sys_errlist[];
static FILE *logfil = (FILE *)0;
logf_open(f)
char *f;
{
if(logfil != (FILE *)0)
(void)fclose(logfil);
logfil = fopen(f,"a");
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(va_alist)
va_dcl
{
char *p;
va_list ap;
if(logfil == (FILE *)0)
exit(1);
va_start(ap);
while(1) {
p = va_arg(ap,char *);
if(p == (char *)0)
break;
if(p == (char *)-1)
p = sys_errlist[errno];
(void)fprintf(logfil,"%s",p);
(void)fprintf(stderr,"%s",p);
}
va_end(ap);
(void)fclose(logfil);
exit(1);
}
extern char *ctime();
/*
print a series of warnings - do not exit
*/
/* VARARGS */
void
logf(va_alist)
va_dcl
{
char *p;
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);
while(1) {
p = va_arg(ap,char *);
if(p == (char *)0)
break;
if(p == (char *)-1)
p = sys_errlist[errno];
(void)fprintf(logfil,"%s",p);
}
va_end(ap);
(void)fflush(logfil);
}
/* print a series of warnings, with a printf() interface. */
/*VARARGS0*/
plogf(va_alist)
va_dcl
{
va_list args;
char *fmt;
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 = va_arg(args, char *);
#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(oid,hw)
register char *oid;
int hw;
{
register 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(num)
int num;
{
static int initt = 0;
extern long random();
if(!initt) {
srandom(time((time_t *)0));
initt++;
}
return((int)random() % num);
}
/* from K&R, mostly */
char *
itoa(num,rbuf)
int num;
char *rbuf;
{
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);
}