DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T f

⟦aefbdd6f9⟧ TextFile

    Length: 8074 (0x1f8a)
    Types: TextFile
    Names: »files_shl.c«

Derivation

└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
    └─⟦this⟧ »EUUGD18/General/Gb/files_shl.c« 

TextFile

/*
 * 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)
 *     openshfdata(&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,filepos,Maxx*Maxy) -- 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,filepos,Maxx*Maxy) -- 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
 *  destroyship(fd, 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);
 }
}


openshfdata(fd)
int *fd;
{
 /*printf("openshfdata\n");*/
 if ( (*fd = open(SHIPFREEDATAFL, O_RDWR, 0777)) < 0) {
	  perror("openshfdata");
	  printf("unable to open %s\n",SHIPFREEDATAFL);
	  exit(-1);
 }
}



getsdata(fd,S)
int fd;
struct data *S;
{
  Fileread(fd,(char *)S, sizeof(struct data), 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,filepos,nsects)
int fd;
sectortype *map;
int filepos;
int nsects;
{
  /* be sure to malloc() the amt of memory needed by getsmap */
 Fileread(fd,(char *)map, nsects*sizeof(sectortype), SECTORDATAFL, filepos );
}


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;	/* must use a pointer here; Fileread re-allocates the addr */
 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) {
	openshfdata(&fd);
		/* 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 */
	printf("top dead ship == %d, read from posn %d\n",shnum,buf.st_size-sizeof(short));
	ftruncate(fd, (long)(buf.st_size-sizeof(short)) );
	close(fd);
	return (int)shnum;
 } else
	return -1;
}



putsdata(fd,S)
int fd;
struct data *S;
{
  Filewrite(fd,(char *)S, sizeof(struct data), 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,filepos,nsects)
int fd;
sectortype *map;
int filepos;
int nsects;
{
 Filewrite(fd,(char*)map, nsects*sizeof(sectortype), SECTORDATAFL, 
	filepos );
}

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)) );
}




/* 
** destroys the ship at the file position given.
*/
destroyship(fd,shipnum)
int fd;
short shipnum;
{
 struct stat buf;

 printf("ship #%d destroyed.\n", shipnum);
	/* write the ship # at the very end of SHIPFREEDATAFL */
  fstat(fd,&buf);
  Filewrite(fd,(char *)&shipnum, sizeof(shipnum), 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);
}