vancleef@ohstpy.mps.ohio-state.edu (06/23/89)
also replace files_shl.c with this one
(typo error was in the one sent)
Garrett
/*
* Galactic Bloodshed (Robert Chansky, smq@b)
* disk input/output routines & msc stuff
* all read routines lock the data they just accessed (the file is not
* closed). write routines close and thus unlock that area.
*
* openstardata(&fd),openpdata(&fd),openshdata(&fd),opensectdata(&fd)
* -- open different files, return file descriptors
* getsdata(fd,&Sdata) -- get root dir for univ
* getrace(**racetype,racenum) -- get race from disk
* getstar(fd,**startype,starnum) -- gets star from disk
* getplanet(fd,**planettype,filepos) -- gets planet from disk
* getsector(fd,**sectortype,filepos) -- gets 1 sect from disk
* getsmap(fd,*sectortype,planet) -- gets sector map from disk
* getship(fd,**shiptype,shipnum) -- gets ship from disk
* putsdata(fd,&Sdata) -- put root dir for univ
* putrace(*racetype) -- put race to disk
* putstar(fd,*startype,starnum) -- put star to disk
* putplanet(fd,*planettype,filepos) -- put planet to disk
* putsector(fd,*sectortype,filepos) -- write sector to disk
* putsmap(fd,*sectortype,*planet) -- put smap to disk
* putship(fd,*shiptype,shipnum) -- put ship to disk
* int Numships(fd) -- return highest ship # from file
* getdeadship(fd, &filepos) -- filepos of next dead ship from shipfreedatafile
* makeshipdead(shipnum) -- kill a ship
* int Getracenum(char *) -- return race # from login list
* int Numraces() -- return # of races that exist right now
* Putpower() -- puts Power struct to disk for power command
*/
#include "vars.h"
#include "ships.h"
#include "races.h"
#include "power.h"
#include <strings.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
extern int errno;
int sys_nerr;
char *sys_errlist[];
openstardata(fd)
int *fd;
{
/*printf(" openstardata\n");*/
if ( (*fd = open(STARDATAFL, O_RDWR, 0777)) < 0) {
perror("openstardata");
printf("unable to open %s\n",STARDATAFL);
exit(-1);
}
}
openshdata(fd)
int *fd;
{
if ( (*fd = open(SHIPDATAFL, O_RDWR, 0777)) < 0) {
perror("openshdata");
printf("unable to open %s\n",SHIPDATAFL);
exit(-1);
}
/*printf("openshdata %d\n",*fd);*/
}
openpdata(fd)
int *fd;
{
if ( (*fd = open(PLANETDATAFL, O_RDWR, 0777)) < 0) {
perror("openpdata");
printf("unable to open %s\n",PLANETDATAFL);
exit(-1);
}
/*printf("openpdata %d\n",*fd);*/
}
opensectdata(fd)
int *fd;
{
/*printf(" opensectdata\n");*/
if ( (*fd = open(SECTORDATAFL, O_RDWR, 0777)) < 0) {
perror("opensectdata");
printf("unable to open %s\n",SECTORDATAFL);
exit(-1);
}
}
getsdata(fd,S)
int fd;
struct stardata *S;
{
Fileread(fd,(char *)S, sizeof(struct stardata), STARDATAFL, 0 );
}
getrace(r,rnum)
racetype **r;
int rnum;
{
int fd;
*r = (racetype *)malloc(sizeof(racetype));
if ( (fd = open(RACEDATAFL, O_RDWR, 0777)) < 0) {
perror("openrdata");
printf("unable to open %s\n",RACEDATAFL);
exit(-1);
}
/*printf(" getrace rnum %d posn %d.\n",rnum,(rnum-1)*sizeof(racetype) );*/
Fileread(fd, (char *)*r, sizeof(racetype), RACEDATAFL,
(rnum-1)*sizeof(racetype) );
close(fd);
}
getstar(fd,s,star)
int fd;
startype **s;
int star;
{
*s = (startype *)malloc(sizeof(startype));
Fileread(fd,(char *)*s, sizeof(startype), STARDATAFL,
(int)(sizeof(Sdata)+star*sizeof(startype)) );
}
getplanet(fd,p,filepos)
int fd;
planettype **p;
int filepos;
{
*p = (planettype *)malloc(sizeof(planettype));
Fileread(fd,(char *)*p, sizeof(planettype), PLANETDATAFL, filepos );
/*printf(" getplanet pointer=%x, smappos=%d\n",*p,(*p)->sectormappos);*/
}
getsector(fd,s,filepos)
int fd;
sectortype **s;
int filepos;
{
*s = (sectortype *)malloc(sizeof(sectortype));
Fileread(fd,(char *)*s, sizeof(sectortype), SECTORDATAFL, filepos );
}
getsmap(fd,map,p)
int fd;
sectortype *map;
planettype *p;
{
/*printf(" getting map posn %d\n", p->sectormappos);*/
Fileread(fd,(char *)map, p->Maxx * p->Maxy * sizeof(sectortype),
SECTORDATAFL, p->sectormappos);
}
int getship(fd,s,shipnum)
int fd;
shiptype **s;
int shipnum;
{
struct stat buf;
fstat(fd,&buf);
if (buf.st_size / sizeof(shiptype) < shipnum) {
printf("Illegal ship number %d\n",shipnum);
return 0;
} else {
*s = (shiptype *)malloc(sizeof(shiptype));
Fileread(fd, (char *)*s, sizeof(shiptype), SHIPDATAFL,
(shipnum-1)*sizeof(shiptype) );
return 1;
}
}
/* gets the ship # listed in the top of the file SHIPFREEDATAFL. this
** might have no other uses besides build().
** also locks the fd in shipdata.
*/
int getdeadship(shipdata)
int shipdata;
{
struct stat buf;
short shnum;
int fd,lerr,lockt = NUM_TIMES_TO_WAIT_FOR_LOCK;
while ((lerr=flock(shipdata, LOCK_SH|LOCK_EX|LOCK_NB ))== -1 && errno==EWOULDBLOCK) {
if (!lockt--) {
printf("too long. exit.\n");
exit(-1);
}
printf("getdeadship waiting on %s lock...\n",SHIPDATAFL);
sleep(2);
}
if (lerr<0 && errno!=EWOULDBLOCK) {
perror("getdeadship");
exit(-1);
}
stat(SHIPFREEDATAFL,&buf);
if (buf.st_size) {
if ( (fd = open(SHIPFREEDATAFL, O_RDWR, 0777)) < 0) {
perror("getdeadship");
printf("unable to open %s\n",SHIPFREEDATAFL);
exit(-1);
}
/* put topmost entry in fpos */
Fileread(fd, (char *)&shnum, sizeof(short), SHIPFREEDATAFL,
buf.st_size - sizeof(short) );
/* erase that entry, since it will now be filled */
ftruncate(fd, (long)(buf.st_size-sizeof(short)) );
close(fd);
if (shnum == 0)shnum=-1;
return (int)shnum;
} else
return -1;
}
putsdata(fd,S)
int fd;
struct stardata *S;
{
Filewrite(fd,(char *)S, sizeof(struct stardata), STARDATAFL, 0 );
}
putrace(r)
racetype *r;
{
int fd;
if ( (fd = open(RACEDATAFL, O_WRONLY, 0777)) < 0) {
perror("openrdata");
printf("unable to open %s\n",RACEDATAFL);
exit(-1);
}
Filewrite(fd,(char *)r, sizeof(racetype), RACEDATAFL,
(r->Playernum-1)*sizeof(racetype) );
close(fd);
/*printf(" putrace pl#%d posn %d\n",r->Playernum,(r->Playernum-1)*sizeof(racetype) );*/
}
putstar(fd,s,snum)
int fd;
startype *s;
int snum;
{
Filewrite(fd,(char *)s, sizeof(startype), STARDATAFL,
(int)(sizeof(Sdata)+snum*sizeof(startype)) );
}
putplanet(fd,p,filepos)
int fd;
planettype *p;
int filepos;
{
Filewrite(fd,(char *)p, sizeof(planettype), PLANETDATAFL, filepos );
}
putsector(fd,s,filepos)
int fd;
sectortype *s;
int filepos;
{
Filewrite(fd,(char *)s, sizeof(sectortype), SECTORDATAFL, filepos );
}
putsmap(fd,map,p)
int fd;
sectortype *map;
planettype *p;
{
Filewrite(fd, (char *)map,
p->Maxx * p->Maxy * sizeof(sectortype), SECTORDATAFL, p->sectormappos);
}
putship(fd,s,shipnum)
int fd;
shiptype *s;
int shipnum;
{
Filewrite(fd,(char *)s, sizeof(shiptype), SHIPDATAFL,
(shipnum-1)*sizeof(shiptype) );
}
int Numraces()
{
struct stat buf;
stat(RACEDATAFL,&buf);
return( (int)(buf.st_size / sizeof(racetype)) );
}
int Numships(fd) /* return number of ships */
{
struct stat buf;
int lerr;
/* check for locks on the fd and lock */
while ((lerr=flock(fd, LOCK_SH|LOCK_EX|LOCK_NB ))== -1 && errno==EWOULDBLOCK){
printf("waiting on %s lock..\n",SHIPDATAFL);
sleep(1);
}
if (lerr== -1 && errno !=EACCES) {
printf("weird error.\n");
perror("numships");
}
fstat(fd,&buf);
/*printf("numships %d\n",(int)(buf.st_size / sizeof(shiptype)) );*/
return( (int)(buf.st_size / sizeof(shiptype)) );
}
/*
** writes the ship to the dead ship file at its end.
*/
makeshipdead(shipnum)
int shipnum;
{
int fd;
us shipno;
struct stat buf;
shipno = shipnum; /* conv to u_short */
if ( (fd = open(SHIPFREEDATAFL, O_WRONLY, 0777)) < 0) {
perror("openshfdata");
printf("unable to open %s\n",SHIPFREEDATAFL);
exit(-1);
}
printf("ship #%u destroyed.\n", shipno);
/* write the ship # at the very end of SHIPFREEDATAFL */
fstat(fd,&buf);
Filewrite(fd,(char *)&shipno, sizeof(shipno), SHIPFREEDATAFL, buf.st_size );
}
Putpower(p)
struct power p[MAXPLAYERS];
{
int power_fd;
if ( (power_fd = open(POWFL, O_RDWR, 0777)) < 0) {
perror("open power data");
printf("unable to open %s\n",POWFL);
exit(-1);
}
write(power_fd, (char *)p, sizeof(*p)*MAXPLAYERS );
close(power_fd);
}