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 s

⟦965b380cd⟧ TextFile

    Length: 16115 (0x3ef3)
    Types: TextFile
    Names: »slave.c«

Derivation

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

TextFile


/********VERSION 1.4!!! (Files from different Versions are INCOMPATIBLE!*******/
/*******************************************************************************
********************************************************************************
********************************************************************************

PERMISSION TO COPY THIS SOFTWARE IS HEREBY GIVEN BY THE AUTHOR PROVIDED THAT
THIS LEADING MESSAGE IS INCLUDED IN ALL OF THE RELEVANT SOURCE FILES.

		P. SCHMITZ, UNIVERSITY OF KEELE, MAY 1988.


********************************************************************************
********************************************************************************
*******************************************************************************/

#include "files.h"
#include "ships.h"
#include <stdio.h>
#include <signal.h>
#include <sgtty.h>

#define ON 1
#define OFF 0

FILE *f, *fopen();
/*variables for extracting termcap info*/
char cm[20];
char cl[20];
char bon[20];
char boff[20];
int shipnumber,x,y,tty_type;
int nointro, termcap;
struct sgttyb params;
char masterpidname[15];
char *tnam;
int statflg;
int fd,pid,masterpid,status; 
char terminalfile[14];
extern char *ttyname();
extern void slaveoutc();
char *p;
struct scores {
		int reason;
		int sno;
		char name[10];
		int score;
	      } sb[50];

lognewprocess(procno)
int procno;
{
char arr[15];

	if ((f=fopen(PROCESSFILE,"a"))==NULL)
		{
		fprintf(stdout,"slave.c : error trying to log processnumber\n");
		abnormdie();
		} else
		{
		sprintf(arr,"%d\n",procno);
		fputs(arr,f);
		fclose(f);
		}
}

checkmasterpidfile()
{
int rpid;
char string[15];
FILE *ff,*fopen();

if ((ff=fopen(MASTERPIDFILE,"r"))==NULL) 
	{
	fprintf(stdout,"slave.c: Can't read masterpidfile\n");
	abnormdie();
	} else
	{
	fgets(string,15,ff); /*get pid*/
	fclose(ff);
	}
sscanf(string,"%d\n",&rpid);
if (rpid!=masterpid) 
	{
	fprintf(stdout,"slave.c: pids not equal!!! %d %d\n",rpid,masterpid);
	abnormdie();
	}
signal(SIGALRM,checkmasterpidfile);
/*fprintf(stdout,"slave.c : checked masterpid. its OK!!\n");*/
alarm(20);
}
 
readinscores()
{
int reason,score,i;
char string[50];
char *getpwuid(), *getlogin();
char c;

if ((f=fopen(PLAYERSCOREFILE,"r"))!=NULL)
	{
		fgets(string,50,f);
        	sscanf(string,"%d %d\n",&reason,&score);
		fclose(f);
	sleep(3);
	clrscreenslave(cl,tty_type);
	} else
	{
	sleep(3);
	fprintf(stdout,"Either the program aborted abnormally or two games were created at the same time.\n");
	fprintf(stdout,"If two games were created at once. Please try again. That should work.\n");
	fprintf(stdout,"(This is likely if you & someone else startred a game at the same instant) \n");
	fprintf(stdout,"If nobody else is playing and you keep getting this message, do an mpssclean!\n");
	return(0);
	}

for (i=0;i<50;++i)
	{
	sb[i].reason=0;
	sb[i].sno=0;
	for (y=0;y<10;y++)
	    sb[i].name[y]=' ';
	sb[i].score=0;
	}

if ((f=fopen(SCOREFILE,"r"))!=NULL)
	{
	for (i=0;i<50;++i)
		{
		fgets(string,50,f);
        	sscanf(string,"%d %d %s %d\n",&sb[i].sno,&sb[i].reason,sb[i].name,&sb[i].score);
		}
	fclose(f);
	}
i=0;
while ((i<50)&&(score<=sb[i].score)) ++i;

if (i>=50)
	{
	fprintf(stdout,"\n      You did so badly, that you didn't even make the scoreboard!\n");
	unlink(PLAYERSCOREFILE);
	return(0);
	}

x=48;
while(x>(i-1))
	{
	sb[x+1].score=sb[x].score;
	sb[x+1].sno=sb[x].sno;
	sb[x+1].reason=sb[x].reason;
	for (y=0;y<10;y++)
		sb[x+1].name[y]=sb[x].name[y];
	x--;
	}
	
sb[i].reason=reason;
sb[i].score=score;
for (y=0;y<10;y++)
	sb[i].name[y]=' ';

	if ((p=getlogin())==NULL)/*can we get it from /etc/utmp?*/
	{
	if ((p=getpwuid(getuid()))==NULL)/*can we get it from /etc/utmp?*/
		{
		  write(2, "Can't find your logname.  Who Are You?\n",39);
		  unlink(PLAYERSCOREFILE);
		  return(0);
		}
	}

strcpy(sb[i].name,p);
sb[i].sno=shipnumber;
/*write out scoreboard*/
unlink(SCOREFILE);
x=creat(SCOREFILE,0666);
if ((f=fopen(SCOREFILE,"w"))!=NULL)
	{
	for (x=0;x<50;++x)
		{
        	sprintf(string,"%d %d %s %d\n",sb[x].sno,sb[x].reason,sb[x].name,sb[x].score);
		fputs(string,f);
		}
	fclose(f);
	}
unlink(PLAYERSCOREFILE);

/*display scores*/
fprintf(stdout,"\n");
tputs(bon,1,slaveoutc);
fprintf(stdout,"*****************************************************************************\n");
fprintf(stdout,"*             MULTI-PLAYER-STAR-TREK's ALL TIME HIGHS (V 1.4)               *\n");
fprintf(stdout,"*****************************************************************************\n");
tputs(boff,1,slaveoutc);
fprintf(stdout,"*Position Name     Ships name          Cause of Death                 Score *\n");
fprintf(stdout,"*****************************************************************************\n");
y=i+6;
for (x=(i-6); x<y; ++x)
	{
	if (x>49) break;
	if (x<0) {y++; 
		continue;
		}
	if (x==i)
	{
	tputs(bon,1,slaveoutc);
fprintf(stdout,"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n");
	}
	sprintf(string,"* %2d      ",(x+1));
	write(0,string,10);
	if (sb[x].reason==0)
	{
	write(0,"JT Kirk  ",9);
	} else
	{
	write(0,sb[x].name,10);
	}
	write(0,ship[sb[x].sno].name,20);
	switch (sb[x].reason)
	{	
	case -1:
		write(0,"Rammed the ship into a planet",29);
		break;
	case -2:
		write(0,"Ran out of energy in flight!!",29);
		break;
	case -3:
		write(0,"Self-destructed the ship/quit",29);
		break;
	case -4:
		write(0,"Blew up under heavy attack...",29);
		break;
	case -5:
		write(0,"Tried time warping twice.....",29);
		break;
	case -6:
		write(0,"Flew into a star.............",29);
		break;
	case -7:
		write(0,"Struck a Romulan warning buoy",29);
		break;
	case -8:
		write(0,"Hit a Klingon mevisic mine...",29);
		break;
	case -9:
		write(0,"Shot by a Klingon destroyer..",29);
		break;
	case -10:
		write(0,"Shot by a Romulan cruiser....",29);
		break;
	case -11:
		write(0,"Was hit by flying ship debris",29);
		break;
	case -12:
		write(0,"Fried in a planets atmosphere",29);
		break;
	default:
		write(0,"Burnt up over Genesis........",29);
		break;
	}
	fprintf(stdout," %6d *\n",sb[x].score);
	if (x==i)
	{
fprintf(stdout,"*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*\n");
	tputs(boff,1,slaveoutc);
	}
	}
fprintf(stdout,"*****************************************************************************\n");
}

die()
{
	readinscores();
	fprintf(stdout,"                       Live long and Prosper!\n");
	unsetrawmode();
	exit(0);
}

abnormdie()
{
	fprintf(stdout,"abnormal termination of program, try mpssclean.\n");
	unsetrawmode();
	exit(0);
}
	
/*
 *	setrawmode()	Function to set cbreak -echo for the terminal
 */

setrawmode()
	{
	ioctl(0,TIOCGETP,&params); /*get terminal sgttyb structure*/
	params.sg_flags |= CBREAK;
	params.sg_flags &= ~ECHO;
	ioctl(0,TIOCSETP,&params); /*write out sgttyb structure*/
	signal(SIGINT, SIG_IGN);
	signal(SIGHUP, SIG_IGN);
	}

/*
 *	unsetrawmode()	Function to set -cbreak echo for the terminal
 */

unsetrawmode()
	{
	ioctl(0,TIOCGETP,&params); /*get terminal sgttyb structure*/
	params.sg_flags &= ~CBREAK;
	params.sg_flags |= ECHO;
	ioctl(0,TIOCSETP,&params); /*write out sgttyb structure*/
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
	}

gettermdata()
{
char *u;
char *getenv();
	
	if((u=getenv("TERM"))==NULL) goto label1;
	if (strcmp(u,"vt200")==0) tty_type=1;
	if (strcmp(u,"a980")==0) tty_type=1;
	if (strcmp(u,"tvi910")==0) tty_type=2;
	if (strcmp(u,"tvi905")==0) tty_type=2;
	if (strcmp(u,"adm3")==0) tty_type=2;
	if (strcmp(u,"vt100")==0) tty_type=3;
	if (strcmp(u,"sun")==0) tty_type=4;

label1:

	while ((tty_type<0) || (tty_type>4))
	{
	fprintf(stdout,"                 ******************************************\n");
	fprintf(stdout,"                 *PLEASE   SPECIFY   YOUR  TERMINAL  TYPE!*\n");
	fprintf(stdout,"                 ******************************************\n");
	fprintf(stdout,"                 *1 for vt200/adds Terminals..............*\n");
	fprintf(stdout,"                 *2 for Televideo 910's/905's & adm3......*\n");
	fprintf(stdout,"                 *3 for vt100.............................*\n");
	fprintf(stdout,"                 *4 for Sun Workstations..................*\n");
	fprintf(stdout,"                 *0 NONE OF THE ABOVE / DONT KNOW.........*\n");
	fprintf(stdout,"                 *Input value now. If in doubt type 0.....*\n");
	fprintf(stdout,"                 ******************************************\n");
	fprintf(stdout,"                                  Input:");
	scanf("%d",&tty_type);
	if ((tty_type<0) || (tty_type>4))
	{
	fprintf(stdout,"\n\n                 ******************************************\n");
	fprintf(stdout,"                 * INVALID TERMINAL...TRY AGAIN PLASE.    *\n");
	fprintf(stdout,"                 ******************************************\n\n");
	}
	}
	if (tty_type==0) 
		{
		if (gettermcapentry(cl,cm,bon,boff)!=1)
				{
if(!termcap)
{
fprintf(stderr,"This means you will haveto modify the code of the program to cater for\n");
fprintf(stderr,"your specific terminal. Otherwise you cannot play.\n");
}
else
{
fprintf(stderr,"Try again without the -t option. If you get the same result,\n");
fprintf(stderr,"you must modify the code. (See rules)\n");
}
				abnormdie();
				}
		} else
		{
		assignterminal(tty_type,cl,cm);
		return(1);
		}
}

main(argc,argv)
int argc;
char *argv[];
{
char string[80];
int childpid,i;
char *s;
char c;
	
	lognewprocess(getpid());
	nointro=0; termcap=0;
	while(--argc>0 && (*++argv)[0]=='-')
		for (s=argv[0]+1; *s!='\0'; s++)
			switch (*s) {
			case 'i':
				nointro=1;
				break;
			case 't':
				termcap=1;
				break;
			default :
			fprintf(stderr,"Usage: mpss {-i} {-t}\n");
			exit(0);
			}
			

	tnam=ttyname(1);
	strcpy(terminalfile,tnam);
	
	tty_type=999;
	if ((termcap)&&(gettermcapentry(cl,cm,bon,boff)==1))
				{
				tty_type=0;
				} else
				{
				tty_type=999;
				gettermdata();
				}
	setrawmode();

	fprintf(stdout,"                Please wait..this can take a moment. \n");
	srand(time()%10);
	sleep(rand()%10);
	
	i=1;
	while ((i<8)&&(f=fopen(LOCK,"r"))!=NULL) 
	{ 
	fprintf(stdout,"            %d) Please keep waiting (timeout at 10).\n",i);
	fclose(f); 
	sleep(3+rand()%10);
	i++;
	}
	
	if (i==8) 
		{
		unlink(LOCK);
		i=0;
		while ((i<6)&&(f=fopen(LOCK,"r"))!=NULL) 
			{ 
			fclose(f); 
			fprintf(stdout,"  Your'e still in the queue to join. Please be patient a little more...\n");
			sleep(2+rand()%10);
			i++;
			}
		if (i==6)
			{
		fprintf(stdout,"Can't get LOCKfile to open. Try 'mpssclean' first.\n");
		abnormdie();
			}
		}
			

	if ((i=creat(LOCK,0666))<0)
		{
		fprintf(stdout,"                  Cant create LOCK file.\n");
		abnormdie();
		}
	
	statflg=OFF;
	/*CHECK IF THE MASTER IS ALREADY RUNNING!!!*/
	if ((f=fopen(MASTERPIDFILE,"r"))==NULL)
	{
	
	/*tell others the game is starting......*/
	char run[80];
	sprintf(run,"%s",MAILFILE);
	system(run);
	/*tell others the game is starting......*/
	
	
	clrscreenslave(cl,tty_type);
	fprintf(stdout,"                 Invoking the game for the first player....\n");
	fprintf(stdout,"                 This takes about 30 seconds...............\n");
	if ((pid=fork())==0) {
		execl(MASTERPROGRAM,"peer",">&!/dev/null","&",(char *) 0);
		fprintf(stderr,"cannot invoke master process!");
		abnormdie();
			     } 
	statflg=ON;
	i=0;
	while ((i<20)&&(f=fopen(MASTERPIDFILE,"r"))==NULL)
		{
		fprintf(stdout,"                          Wait a bit longer.......\n");
		sleep(5);
		i++;
		}
	if (i==20) {
		fprintf(stdout,"                          mpss : timed out waiting. Do mpssclean!\n");
		 unlink(LOCK);
		 abnormdie();
		}
	} 
	fgets(masterpidname,15,f);
	fclose(f);
	sscanf(masterpidname,"%d\n",&masterpid);
	for (i=0;i<15;++i) masterpidname[i]='\0';
	if ((f=fopen(SHIPNOFILE,"r"))==NULL)
		{
		i=creat(SHIPNOFILE,0666);
		close(i);
		f=fopen(SHIPNOFILE,"w");
		sprintf(masterpidname,"%d\n",0);
		fputs(masterpidname,f);
		shipnumber=0;
		fclose(f);
		}
		else
		{
		fgets(masterpidname,15,f);
		fclose(f);
		sscanf(masterpidname,"%d\n",&shipnumber);
		shipnumber++;
		if (shipnumber>30) shipnumber=0;
		f=fopen(SHIPNOFILE,"w");
		sprintf(masterpidname,"%d\n",shipnumber);
		fputs(masterpidname,f);
		fclose(f);
		 }
	clrscreenslave(cl,tty_type);
	if (nointro) goto nextlabel;
	poscursslave(cm,tty_type,0,0);
	tputs(bon,1,slaveoutc);
	fprintf(stdout,"                  \n\n\n\n\n                                  Space.                           \n");
	tputs(boff,1,slaveoutc);
	sleep(3);
	fprintf(stdout,"                            The final frontier.                  \n");
	sleep(3);
	fprintf(stdout,"              These are the voyages of the Starship  Enterprise,\n");
	sleep(3);
	fprintf(stdout,"              her ongoing mission to explore strange,new worlds.\n");
	sleep(3);
	fprintf(stdout,"              To seekout new lifeforms, and  new  civilizations,\n");
	sleep(3);
	fprintf(stdout,"              To boldly go where no man has gone................\n");
	sleep(4);
	fprintf(stdout,"                                before.....\n");
	sleep(3);
	clrscreenslave(cl,tty_type);
	poscursslave(cm,tty_type,0,0);
	fprintf(stdout,"\n");
	fprintf(stdout,"\n");
	fprintf(stdout,"\n");
	fprintf(stdout,"     *****************************************************************\n");
	fprintf(stdout,"     *     Your     vessel    details     are     as     follows.    *\n");
	fprintf(stdout,"     *****************************************************************\n");
	fprintf(stdout,"     *    Ships   Name       : ");
	fprintf(stdout," %s*\n",ship[shipnumber].name);
	fprintf(stdout,"     *   Ships Registration  : ");
	fprintf(stdout," %s*\n",ship[shipnumber].registration);
	fprintf(stdout,"     *          Class        : ");
	fprintf(stdout," %s*\n",ship[shipnumber].class);
	fprintf(stdout,"     *      Ships   origin   : ");
	fprintf(stdout," %s*\n",ship[shipnumber].flag);
	fprintf(stdout,"     *      Number of crew   : ");
	fprintf(stdout," %s*\n",ship[shipnumber].noofcrew);
	fprintf(stdout,"     *      Commision  date  : ");
	fprintf(stdout," %s*\n",ship[shipnumber].commissiondate);
	fprintf(stdout,"     *     Mission Objective :  Blast anything in sight.             *\n");
	fprintf(stdout,"     *                                                               *\n");
	fprintf(stdout,"     * Written by Peer Schmitz, University of Keele, Keele, Staffs,  *\n");
	fprintf(stdout,"     * ST5-5BG, Jan. - Jul 88, due to a lack of STAR TREK on the box.*\n");
	fprintf(stdout,"     *                         Version 1.4....                       *\n");
	fprintf(stdout,"     *****************************************************************\n");
	sleep(17);
	clrscreenslave(cl,tty_type);
nextlabel:
	if ((pid=fork())==0)
		{
		signal(SIGALRM,checkmasterpidfile);
		childpid=getpid();
		lognewprocess(childpid);
		i=1;
		while ((i<5)&&((f=fopen(PLAYERFILE,"r"))!=NULL))
						{
						fclose(f);
	fprintf(stdout,"                     %d) Please be patient a little more...\n",i);
						sleep((2+(rand()%10)));
						i++;
						}
	
		if (i==5) 
		{
		unlink(PLAYERFILE);
		i=1;
		while ((i<6)&&(f=fopen(PLAYERFILE,"r"))!=NULL) 
			{ 
			fclose(f); 
	fprintf(stdout,"                     %d) Please be patient a little more...\n",i);
	fprintf(stdout,"                        Hang in there a little more...\n");
	fprintf(stdout,"                        (Ill time out soon if this fails.)\n");
			sleep(2+rand()%10);
			i++;
			}
		if (i==6)
			{
		fprintf(stdout,"Can't get Playerfile to open. Try 'mpssclean' first.\n");
		abnormdie();
			}
		}
			
		/*Now we know the file isnt there yet*/
		i=creat(PLAYERFILE,0666);
		if ((f=fopen(PLAYERFILE,"w"))==NULL)
			{
			fprintf(stdout,"CANNOT OPEN PLAYERFILE\n");
		 	abnormdie();
			} else
			{
			char c;
sprintf(string,"%s#%s#%s#%d#%d#%d#\n",terminalfile,cm,cl,shipnumber,childpid,tty_type);
			fputs(string,f);
			fclose(f);
	/*
	for (i=0;i<20;i++) fprintf(stderr,"*%c*",cm[i]); 
	read(0,&c,1);
	fprintf(stderr,"\n\n");
	for (i=0;i<80;i++) fprintf(stderr,"*%c*",string[i]); 
	read(0,&c,1);
	*/
			clrscreenslave(cl,tty_type);
			kill(masterpid,29);
			alarm(20);
			while(1);
			}
		}
while(wait(&status)!=pid);
die();
}