newuaf.c
./abermud/newuaf.c · 3.0 KB
From the archive abermud-89.tar.gz
Download the archive « Back to the archive contents
/*
** User access files routines.
*/
#include "kernel.h"
#ifdef RCSID
static char RCS[] =
"$Header: newuaf.c,v 1.2 89/07/03 08:18:05 rsalz Exp $";
#endif /* RCSID */
#define PCTL_GET 0
#define PCTL_FIND 1
static FILE *
openuaf(perm)
char *perm;
{
FILE *i;
if ((i = openlock(UAF_RAND, perm)) == NULL)
crapup("Cannot access UAF\n");
return i;
}
static FILE *
personactl(name, d, act)
char *name;
PERSONA *d;
int act;
{
FILE *a;
char c[32], e[32];
a = openuaf("r+");
lowercase(strcpy(e, name));
while (fread((char *)d, sizeof *d, 1, a) == 1)
if (EQ(lowercase(strcpy(c, d->p_name)), e))
switch (act) {
case PCTL_GET:
closelock(a);
/* Hack, return pointer to closed file. */
return a;
case PCTL_FIND:
/* Back up one spot. */
fseek(a, ftell(a) - sizeof *d, 0);
return a;
}
closelock(a);
return NULL;
}
findpers(name, d)
char *name;
PERSONA *d;
{
return personactl(name, d, PCTL_GET) != NULL;
}
delpers(name)
char *name;
{
FILE *i;
PERSONA d;
/* Was while loop but something happened and this looped forever... */
if (i = personactl(name, &d, PCTL_FIND)) {
mudlog("Deleted %s", lowercase(name));
if (!EQ(lowercase(d.p_name), name))
crapup("Panic: Invalid Persona Delete");
d.p_name[0] = '\0';
d.p_level = -1;
fwrite((char *)&d, sizeof d, 1, i);
closelock(i);
}
}
putpers(name, d)
char *name;
PERSONA *d;
{
FILE *i;
off_t flen;
PERSONA s;
if ((i = personactl(name, &s, PCTL_FIND)) == NULL) {
flen = -1;
if ((i = personactl("", &s, PCTL_FIND)) == NULL) {
i = openuaf("a");
flen = ftell(i);
}
if (fwrite((char *)d, sizeof *d, 1, i) != 1) {
bprintf("Save Failed - Device Full ?\n");
if (flen != -1)
ftruncate(fileno(i), (off_t)flen);
}
}
else
fwrite((char *)d, sizeof *d, 1, i);
closelock(i);
}
decpers(d, name, str, score, lev, sex)
PERSONA *d;
char *name;
int *str, *score, *lev, *sex;
{
strcpy(name, d->p_name);
*str = d->p_strength;
*score = d->p_score;
*lev = d->p_level;
*sex = d->p_sex;
}
initme()
{
PERSONA d;
char s[32];
if (findpers(globme, &d)) {
decpers(&d, s, &my_str, &my_sco, &my_lev, &my_sex);
if (my_str >= 0)
return;
}
d.p_score = 0;
bprintf("Creating character....\n");
my_sco = 0;
my_str = 40;
my_lev = 1;
for (my_sex = -1; my_sex < 0; ) {
bprintf("\nSex (M/F) : ");
pbfr();
key_input(">", 2);
switch (key_buff[0]) {
case 'M': case 'm':
my_sex = 0;
break;
case 'F': case 'f':
my_sex = 1;
break;
default:
bprintf("M or F");
}
}
strcpy(d.p_name, globme);
d.p_strength = my_str;
d.p_level = my_lev;
d.p_sex = my_sex;
d.p_score = my_sco;
putpers(globme, &d);
}
saveme()
{
PERSONA d;
strcpy(d.p_name, globme);
d.p_strength = pstr(mynum);
d.p_level = plev(mynum);
d.p_sex = psexall(mynum);
d.p_score = pscore(mynum);
if (!zapped) {
bprintf("\nSaving %s\n", pname(mynum));
putpers(globme, &d);
}
}