summaryrefslogtreecommitdiffstats
path: root/games/battlestar
diff options
context:
space:
mode:
authoreivind <eivind@FreeBSD.org>1997-02-20 03:04:54 +0000
committereivind <eivind@FreeBSD.org>1997-02-20 03:04:54 +0000
commit27eb6d09a03b6d51d025fafa9e46e0f64544d722 (patch)
treec09c18b60a67afd2e34d5a6138c86fe56288b8e3 /games/battlestar
parent1a5d9e17d08eb1b79c5918e8596ba8f18ea36461 (diff)
downloadFreeBSD-src-27eb6d09a03b6d51d025fafa9e46e0f64544d722.zip
FreeBSD-src-27eb6d09a03b6d51d025fafa9e46e0f64544d722.tar.gz
Remove buffer overflow and tempfile race, remove <sys/time.h> hack (and
need for it), change definition of setbit() macro and friends to be compatible with <sys/param.h>. The bugs were discovered and fixed as a result of the FreeBSD code audit. Submitted by: Aaron Bornstein <aaronb@j51.com>, Mark Huizer <xaa@stack.nl>
Diffstat (limited to 'games/battlestar')
-rw-r--r--games/battlestar/Makefile1
-rw-r--r--games/battlestar/com1.c28
-rw-r--r--games/battlestar/com2.c12
-rw-r--r--games/battlestar/com3.c10
-rw-r--r--games/battlestar/com4.c14
-rw-r--r--games/battlestar/com5.c26
-rw-r--r--games/battlestar/com6.c8
-rw-r--r--games/battlestar/com7.c12
-rw-r--r--games/battlestar/cypher.c10
-rw-r--r--games/battlestar/externs.h17
-rw-r--r--games/battlestar/fly.c8
-rw-r--r--games/battlestar/init.c4
-rw-r--r--games/battlestar/room.c2
-rw-r--r--games/battlestar/save.c57
14 files changed, 122 insertions, 87 deletions
diff --git a/games/battlestar/Makefile b/games/battlestar/Makefile
index c6bef1f..005e9e9 100644
--- a/games/battlestar/Makefile
+++ b/games/battlestar/Makefile
@@ -8,5 +8,6 @@ MAN6= battlestar.6
DPADD= ${LIBCURSES} ${LIBTERMCAP} ${LIBCOMPAT}
LDADD= -lcurses -ltermcap -lcompat
HIDEGAME=hidegame
+#CFLAGS+= -Wall -W
.include <bsd.prog.mk>
diff --git a/games/battlestar/com1.c b/games/battlestar/com1.c
index d74f862..c099f76 100644
--- a/games/battlestar/com1.c
+++ b/games/battlestar/com1.c
@@ -45,7 +45,7 @@ int thataway, token;
if (thataway) {
position = thataway;
newway(token);
- time++;
+ gtime++;
}
else {
puts("You can't go this way.");
@@ -88,24 +88,24 @@ news()
register int n;
int hurt;
- if (time > 30 && position < 32){
+ if (gtime > 30 && position < 32){
puts("An explosion of shuddering magnitude splinters bulkheads and");
puts("ruptures the battlestar's hull. You are sucked out into the");
puts("frozen void of space and killed.");
die();
}
- if (time > 20 && position < 32)
+ if (gtime > 20 && position < 32)
puts("Explosions rock the battlestar.");
- if (time > snooze){
+ if (gtime > snooze){
puts("You drop from exhaustion...");
zzz();
}
- if (time > snooze - 5)
+ if (gtime > snooze - 5)
puts("You're getting tired.");
- if (time > (rythmn + CYCLE)) {
+ if (gtime > (rythmn + CYCLE)) {
if (location == nightfile) {
convert(TODAY);
- if (OUTSIDE && time - rythmn - CYCLE < 10) {
+ if (OUTSIDE && gtime - rythmn - CYCLE < 10) {
puts("Dew lit sunbeams stretch out from a watery sunrise and herald the dawn.");
puts("You awake from a misty dream-world into stark reality.");
puts("It is day.");
@@ -113,14 +113,14 @@ news()
} else {
convert(TONIGHT);
clearbit(location[POOLS].objects, BATHGOD);
- if (OUTSIDE && time - rythmn - CYCLE < 10) {
+ if (OUTSIDE && gtime - rythmn - CYCLE < 10) {
puts("The dying sun sinks into the ocean, leaving a blood stained sunset.");
puts("The sky slowly fades from orange to violet to black. A few stars");
puts("flicker on, and it is night.");
puts("The world seems completly different at night.");
}
}
- rythmn = time - time % CYCLE;
+ rythmn = gtime - gtime % CYCLE;
}
if (!wiz && !tempwiz)
if ((testbit(inven,TALISMAN) || testbit(wear,TALISMAN)) && (testbit(inven,MEDALION) || testbit(wear,MEDALION)) && (testbit(inven,AMULET) || testbit(wear,AMULET))){
@@ -157,16 +157,16 @@ news()
}
if (testbit(location[position].objects, GIRL))
meetgirl = 1;
- if (meetgirl && CYCLE * 1.5 - time < 10){
+ if (meetgirl && CYCLE * 1.5 - gtime < 10){
setbit(location[GARDEN].objects,GIRLTALK);
setbit(location[GARDEN].objects,LAMPON);
setbit(location[GARDEN].objects,ROPE);
}
- if (position == DOCK && (beenthere[position] || time > CYCLE)){
+ if (position == DOCK && (beenthere[position] || gtime > CYCLE)){
clearbit(location[DOCK].objects, GIRL);
clearbit(location[DOCK].objects,MAN);
}
- if (meetgirl && time - CYCLE * 1.5 > 10){
+ if (meetgirl && gtime - CYCLE * 1.5 > 10){
clearbit(location[GARDEN].objects,GIRLTALK);
clearbit(location[GARDEN].objects,LAMPON);
clearbit(location[GARDEN].objects,ROPE);
@@ -174,7 +174,7 @@ news()
}
if (testbit(location[position].objects,CYLON)){
puts("Oh my God, you're being shot at by an alien spacecraft!");
- printf("The targeting computer says we have %d seconds to attack!\n",clock);
+ printf("The targeting computer says we have %d seconds to attack!\n",gclock);
fflush(stdout);
sleep(1);
if (!visual()){
@@ -236,7 +236,7 @@ crash()
}
notes[LAUNCHED] = 0;
setbit(location[position].objects,CRASH);
- time += rnd(CYCLE/4);
+ gtime += rnd(CYCLE/4);
puts("The viper explodes into the ground and you lose consciousness...");
zzz();
hurt1 = rnd(NUMOFINJURIES - 2) + 2;
diff --git a/games/battlestar/com2.c b/games/battlestar/com2.c
index 39148fc..e7d1af6 100644
--- a/games/battlestar/com2.c
+++ b/games/battlestar/com2.c
@@ -81,7 +81,7 @@ wearit() /* synonyms = {sheathe, sheath} */
setbit(wear,value);
carrying -= objwt[value];
encumber -= objcumber[value];
- time++;
+ gtime++;
printf("You are now wearing %s %s.\n",(objsht[value][n-1] == 's' ? "the" : "a"), objsht[value]);
}
else if (testbit(wear,value))
@@ -129,7 +129,7 @@ use()
location[position].down = 160;
whichway(location[position]);
puts("The waves subside and it is possible to descend to the sea cave now.");
- time++;
+ gtime++;
return(-1);
}
}
@@ -139,7 +139,7 @@ use()
position = 224;
else
position = 229;
- time++;
+ gtime++;
return(0);
}
else if (position == FINAL)
@@ -225,7 +225,7 @@ ravage()
{
while (wordtype[++wordnumber] != NOUNS && wordnumber <= wordcount);
if (wordtype[wordnumber] == NOUNS && testbit(location[position].objects,wordvalue[wordnumber])){
- time++;
+ gtime++;
switch(wordvalue[wordnumber]){
case NORMGOD:
puts("You attack the goddess, and she screams as you beat her. She falls down");
@@ -272,7 +272,7 @@ ravage()
follow()
{
- if (followfight == time){
+ if (followfight == gtime){
puts("The Dark Lord leaps away and runs down secret tunnels and corridoors.");
puts("You chase him through the darkness and splash in pools of water.");
puts("You have cornered him. His laser sword extends as he steps forward.");
@@ -282,7 +282,7 @@ follow()
setbit(location[position].objects,AMULET);
return(0);
}
- else if (followgod == time){
+ else if (followgod == gtime){
puts("The goddess leads you down a steamy tunnel and into a high, wide chamber.");
puts("She sits down on a throne.");
position = 268;
diff --git a/games/battlestar/com3.c b/games/battlestar/com3.c
index bc4ec9f..1189ecc 100644
--- a/games/battlestar/com3.c
+++ b/games/battlestar/com3.c
@@ -41,7 +41,7 @@ dig()
{
if (testbit(inven,SHOVEL)){
puts("OK");
- time++;
+ gtime++;
switch(position){
case 144: /* copse near beach */
if (!notes[DUG]){
@@ -176,7 +176,7 @@ drink()
CUMBER = MAXCUMBER;
for (n=0; n < NUMOFINJURIES; n++)
injuries[n] = 0;
- time++;
+ gtime++;
zzz();
}
else
@@ -199,7 +199,7 @@ shoot()
for (n=0; objsht[value][n]; n++);
if (testbit(location[position].objects,value)){
clearbit(location[position].objects,value);
- time++;
+ gtime++;
printf("The %s explode%s\n",objsht[value],(objsht[value][n-1]=='s' ? (objsht[value][n-2]=='s' ? "s." : ".") : "s."));
if (value == BOMB)
die();
@@ -214,7 +214,7 @@ shoot()
/* special cases with their own return()'s */
if (wordnumber <= wordcount && wordtype[wordnumber] == NOUNS){
- time++;
+ gtime++;
switch(wordvalue[wordnumber]){
case DOOR:
@@ -278,7 +278,7 @@ shoot()
clearbit(location[position].objects,TIMER);
setbit(location[position].objects,DEADTIME);
}
- else puts("What old timer?");
+ else puts("What old gtimer?");
break;
case MAN:
if (testbit(location[position].objects,MAN)){
diff --git a/games/battlestar/com4.c b/games/battlestar/com4.c
index fe1d96b..eac4cf9 100644
--- a/games/battlestar/com4.c
+++ b/games/battlestar/com4.c
@@ -61,7 +61,7 @@ unsigned int from[];
setbit(inven,value);
carrying += objwt[value];
encumber += objcumber[value];
- time++;
+ gtime++;
if (testbit(from,value))
printf("Taken.\n");
else
@@ -165,7 +165,7 @@ unsigned int from[];
puts("ties it at the waist. Around her neck hangs a golden amulet.");
puts("She bids you to follow her.");
pleasure++;
- followgod = time;
+ followgod = gtime;
clearbit(location[position].objects,BATHGOD);
} else if (!testbit(location[position].objects,BATHGOD))
puts("You're in no position to take her.");
@@ -285,7 +285,7 @@ char *name;
setbit(location[position].objects,value);
else
tempwiz = 0;
- time++;
+ gtime++;
if (*name == 'K')
puts("Drop kicked.");
else
@@ -354,16 +354,16 @@ eat()
case MANGO:
printf("%s:\n",objsht[value]);
- if (testbit(inven,value) && time > ate - CYCLE && testbit(inven,KNIFE)){
+ if (testbit(inven,value) && gtime > ate - CYCLE && testbit(inven,KNIFE)){
clearbit(inven,value);
carrying -= objwt[value];
encumber -= objcumber[value];
- ate = max(time,ate) + CYCLE/3;
+ ate = max(gtime,ate) + CYCLE/3;
snooze += CYCLE/10;
- time++;
+ gtime++;
puts("Eaten. You can explore a little longer now.");
}
- else if (time < ate - CYCLE)
+ else if (gtime < ate - CYCLE)
puts("You're stuffed.");
else if (!testbit(inven,KNIFE))
puts("You need a knife.");
diff --git a/games/battlestar/com5.c b/games/battlestar/com5.c
index 04e9cfe..5bdea6a 100644
--- a/games/battlestar/com5.c
+++ b/games/battlestar/com5.c
@@ -100,7 +100,7 @@ love()
if (!loved)
setbit(location[position].objects,MEDALION);
loved = 1;
- time += 10;
+ gtime += 10;
zzz();
}
else {
@@ -112,7 +112,7 @@ love()
power++;
pleasure += 5;
printf("Girl:\n");
- time += 10;
+ gtime += 10;
zzz();
}
printf("Loved.\n");
@@ -125,16 +125,16 @@ zzz()
int oldtime;
register int n;
- oldtime = time;
- if ((snooze - time) < (0.75 * CYCLE)){
- time += 0.75 * CYCLE - (snooze - time);
+ oldtime = gtime;
+ if ((snooze - gtime) < (0.75 * CYCLE)){
+ gtime += 0.75 * CYCLE - (snooze - gtime);
printf("<zzz>");
- for (n = 0; n < time - oldtime; n++)
+ for (n = 0; n < gtime - oldtime; n++)
printf(".");
printf("\n");
- snooze += 3 * (time - oldtime);
+ snooze += 3 * (gtime - oldtime);
if (notes[LAUNCHED]){
- fuel -= (time - oldtime);
+ fuel -= (gtime - oldtime);
if (location[position].down){
position = location[position].down;
crash();
@@ -177,8 +177,8 @@ zzz()
chime()
{
- if ((time / CYCLE + 1) % 2 && OUTSIDE)
- switch((time % CYCLE)/(CYCLE / 7)){
+ if ((gtime / CYCLE + 1) % 2 && OUTSIDE)
+ switch((gtime % CYCLE)/(CYCLE / 7)){
case 0:
puts("It is just after sunrise.");
break;
@@ -202,7 +202,7 @@ chime()
break;
}
else if (OUTSIDE)
- switch((time % CYCLE)/(CYCLE / 7)){
+ switch((gtime % CYCLE)/(CYCLE / 7)){
case 0:
puts("It is just after sunset.");
break;
@@ -259,7 +259,7 @@ give()
}
if (result != -1 && (testbit(location[position].objects,obj) || obj == AMULET || obj == MEDALION || obj == TALISMAN)){
clearbit(location[position].objects,obj);
- time++;
+ gtime++;
ego++;
switch(person){
case NATIVE:
@@ -288,7 +288,7 @@ give()
puts("after having been out drinking with the girls, she kicks the throne particulary");
puts("hard and wakes you up. (If you want to win this game, you're going to have to\nshoot her!)");
clearbit(location[position].objects,MEDALION);
- wintime = time;
+ wintime = gtime;
}
}
break;
diff --git a/games/battlestar/com6.c b/games/battlestar/com6.c
index a7fa41e..a6dc158 100644
--- a/games/battlestar/com6.c
+++ b/games/battlestar/com6.c
@@ -45,7 +45,7 @@ launch()
clearbit(location[position].objects,VIPER);
position = location[position].up;
notes[LAUNCHED] = 1;
- time++;
+ gtime++;
fuel -= 4;
puts("You climb into the viper and prepare for launch.");
puts("With a touch of your thumb the turbo engines ignite, thrusting you back into\nyour seat.");
@@ -66,7 +66,7 @@ land()
position = location[position].down;
setbit(location[position].objects,VIPER);
fuel -= 2;
- time++;
+ gtime++;
puts("You are down.");
return(1);
}
@@ -168,7 +168,7 @@ drive()
clearbit(location[position].objects,CAR);
setbit(location[position].objects,CRASH);
injuries[5] = injuries[6] = injuries[7] = injuries[8] = 1;
- time += 15;
+ gtime += 15;
zzz();
return(0);
}
@@ -203,7 +203,7 @@ light() /* synonyms = {strike, smoke} */
{ /* for matches, cigars */
if (testbit(inven,MATCHES) && matchcount){
puts("Your match splutters to life.");
- time++;
+ gtime++;
matchlight = 1;
matchcount--;
if (position == 217){
diff --git a/games/battlestar/com7.c b/games/battlestar/com7.c
index ca86de8..6ae8f14 100644
--- a/games/battlestar/com7.c
+++ b/games/battlestar/com7.c
@@ -48,15 +48,15 @@ int enemy,strength;
int exhaustion;
fighton:
- time++;
+ gtime++;
snooze -= 5;
- if (snooze > time)
- exhaustion = CYCLE/(snooze - time);
+ if (snooze > gtime)
+ exhaustion = CYCLE/(snooze - gtime);
else {
puts("You collapse exhausted, and he pulverizes your skull.");
die();
}
- if (snooze - time < 20)
+ if (snooze - gtime < 20)
puts("You look tired! I hope you're able to fight.");
next = getcom(auxbuf, LINELENGTH, "<fight!>-: ", 0);
for (i=0; next && i < 10; i++)
@@ -186,7 +186,7 @@ fighton:
puts("he flees down the dark caverns.");
clearbit(location[position].objects,DARK);
injuries[SKULL] = 1;
- followfight = time;
+ followfight = gtime;
return (0);
}
else{
@@ -231,7 +231,7 @@ fighton:
case DROP:
case DRAW:
cypher();
- time--;
+ gtime--;
break;
default:
diff --git a/games/battlestar/cypher.c b/games/battlestar/cypher.c
index ec02a79..ff5da4e 100644
--- a/games/battlestar/cypher.c
+++ b/games/battlestar/cypher.c
@@ -283,10 +283,10 @@ cypher()
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d", &position);
- printf("Time (was %d) = ",time);
+ printf("Time (was %d) = ",gtime);
fgets(buffer,10,stdin);
if (*buffer != '\n')
- sscanf(buffer,"%d", &time);
+ sscanf(buffer,"%d", &gtime);
printf("Fuel (was %d) = ",fuel);
fgets(buffer,10,stdin);
if (*buffer != '\n')
@@ -303,10 +303,10 @@ cypher()
fgets(buffer,10,stdin);
if (*buffer != '\n')
sscanf(buffer,"%d",&WEIGHT);
- printf("Clock (was %d) = ",clock);
+ printf("Clock (was %d) = ",gclock);
fgets(buffer,10,stdin);
if (*buffer != '\n')
- sscanf(buffer,"%d",&clock);
+ sscanf(buffer,"%d",&gclock);
printf("Wizard (was %d, %d) = ",wiz, tempwiz);
fgets(buffer,10,stdin);
if (*buffer != '\n'){
@@ -324,7 +324,7 @@ cypher()
case SCORE:
printf("\tPLEASURE\tPOWER\t\tEGO\n");
printf("\t%3d\t\t%3d\t\t%3d\n\n",pleasure,power,ego);
- printf("This gives you the rating of %s in %d turns.\n",rate(),time);
+ printf("This gives you the rating of %s in %d turns.\n",rate(),gtime);
printf("You have visited %d out of %d rooms this run (%d%%).\n",card(beenthere,NUMOFROOMS),NUMOFROOMS,card(beenthere,NUMOFROOMS)*100/NUMOFROOMS);
break;
diff --git a/games/battlestar/externs.h b/games/battlestar/externs.h
index ef66948..2d85352 100644
--- a/games/battlestar/externs.h
+++ b/games/battlestar/externs.h
@@ -33,16 +33,19 @@
* @(#)externs.h 8.1 (Berkeley) 5/31/93
*/
+#include <sys/param.h>
#include <sys/signal.h>
#include <stdio.h>
-#define BITS (8 * sizeof (int))
+#define BITS (8)
#define OUTSIDE (position > 68 && position < 246 && position != 218)
#define rnd(x) (rand() % (x))
#define max(a,b) ((a) < (b) ? (b) : (a))
#define testbit(array, index) (array[index/BITS] & (1 << (index % BITS)))
+#ifndef setbit
#define setbit(array, index) (array[index/BITS] |= (1 << (index % BITS)))
+#endif
#define clearbit(array, index) (array[index/BITS] &= ~(1 << (index % BITS)))
/* well known rooms */
@@ -224,7 +227,7 @@ struct room {
#define down link[6]
#define flyhere link[7]
char *desc;
- unsigned int objects[NUMOFWORDS];
+ int objects[NUMOFWORDS];
};
struct room dayfile[];
struct room nightfile[];
@@ -248,11 +251,11 @@ char *truedirec(), *rate();
char *getcom(), *getword();
/* state of the game */
-int time;
+int gtime;
int position;
int direction;
int left, right, ahead, back;
-int clock, fuel, torps;
+int gclock, fuel, torps;
int carrying, encumber;
int rythmn;
int followfight;
@@ -271,12 +274,12 @@ int pleasure, power, ego;
int WEIGHT;
int CUMBER;
int notes[NUMOFNOTES];
-unsigned int inven[NUMOFWORDS];
-unsigned int wear[NUMOFWORDS];
+int inven[NUMOFWORDS];
+int wear[NUMOFWORDS];
char beenthere[NUMOFROOMS+1];
char injuries[NUMOFINJURIES];
-char uname[9];
+char uname[MAXLOGNAME];
struct wlist {
char *string;
diff --git a/games/battlestar/fly.c b/games/battlestar/fly.c
index 4c24a0d..118ff16 100644
--- a/games/battlestar/fly.c
+++ b/games/battlestar/fly.c
@@ -46,7 +46,7 @@ static char sccsid[] = "@(#)fly.c 8.1 (Berkeley) 5/31/93";
int row, column;
int dr = 0, dc = 0;
char destroyed;
-int clock = 120; /* time for all the flights in the game */
+int gclock = 120; /* gtime for all the flights in the game */
char cross = 0;
sig_t oldsig;
@@ -166,7 +166,7 @@ visual()
endfly();
return(1);
}
- if (clock <= 0){
+ if (gclock <= 0){
endfly();
die();
}
@@ -256,7 +256,7 @@ moveenemy()
row += (rnd(9) - 4) % (4 - abs(row - MIDR));
column += (rnd(9) - 4) % (4 - abs(column - MIDC));
}
- clock--;
+ gclock--;
mvaddstr(oldr, oldc - 1, " ");
if (cross)
target();
@@ -266,7 +266,7 @@ moveenemy()
move(LINES-1, 42);
printw("%3d", fuel);
move(LINES-1, 57);
- printw("%3d", clock);
+ printw("%3d", gclock);
refresh();
signal(SIGALRM, moveenemy);
alarm(1);
diff --git a/games/battlestar/init.c b/games/battlestar/init.c
index d0359de..6d3a2ed 100644
--- a/games/battlestar/init.c
+++ b/games/battlestar/init.c
@@ -56,7 +56,7 @@ initialize(startup)
wordinit();
if (startup) {
direction = NORTH;
- time = 0;
+ gtime = 0;
snooze = CYCLE * 1.5;
position = 22;
setbit(wear, PAJAMAS);
@@ -121,7 +121,7 @@ checkout(uname)
if (location != NULL) {
CUMBER = 3;
WEIGHT = 9; /* that'll get him! */
- clock = 10;
+ gclock = 10;
setbit(location[7].objects, WOODSMAN); /* viper room */
setbit(location[20].objects, WOODSMAN); /* laser " */
setbit(location[13].objects, DARK); /* amulet " */
diff --git a/games/battlestar/room.c b/games/battlestar/room.c
index 80fc2b2..db585ea 100644
--- a/games/battlestar/room.c
+++ b/games/battlestar/room.c
@@ -59,7 +59,7 @@ writedes()
printobjs()
{
- register unsigned int *p = location[position].objects;
+ register int *p = location[position].objects;
register n;
printf("\n");
diff --git a/games/battlestar/save.c b/games/battlestar/save.c
index 4a6ccba..ec4b4f5 100644
--- a/games/battlestar/save.c
+++ b/games/battlestar/save.c
@@ -35,27 +35,33 @@
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
#endif /* not lint */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h> /* MAXPATHLEN */
+#include <fcntl.h>
#include "externs.h"
+void
restore()
{
char *getenv();
char *home;
- char home1[100];
+ char home1[MAXPATHLEN];
register int n;
int tmp;
register FILE *fp;
- home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
+ if ( (home = getenv("HOME")) != NULL)
+ sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
+ else return;
+
if ((fp = fopen(home1, "r")) == 0) {
perror(home1);
return;
}
fread(&WEIGHT, sizeof WEIGHT, 1, fp);
fread(&CUMBER, sizeof CUMBER, 1, fp);
- fread(&clock, sizeof clock, 1, fp);
+ fread(&gclock, sizeof gclock, 1, fp);
fread(&tmp, sizeof tmp, 1, fp);
location = tmp ? dayfile : nightfile;
for (n = 1; n <= NUMOFROOMS; n++) {
@@ -68,7 +74,7 @@ restore()
fread(notes, sizeof notes, 1, fp);
fread(&direction, sizeof direction, 1, fp);
fread(&position, sizeof position, 1, fp);
- fread(&time, sizeof time, 1, fp);
+ fread(&gtime, sizeof gtime, 1, fp);
fread(&fuel, sizeof fuel, 1, fp);
fread(&torps, sizeof torps, 1, fp);
fread(&carrying, sizeof carrying, 1, fp);
@@ -90,26 +96,51 @@ restore()
fread(&ego, sizeof ego, 1, fp);
}
+void
save()
{
+ struct stat sbuf;
char *getenv();
char *home;
- char home1[100];
+ char home1[MAXPATHLEN];
register int n;
- int tmp;
+ int tmp, fd;
FILE *fp;
home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
- if ((fp = fopen(home1, "w")) == 0) {
+ if (home == 0)
+ return;
+ sprintf(home1, "%.*s/Bstar", MAXPATHLEN - 7, home);
+
+ /* Try to open the file safely. */
+ if (stat(home1, &sbuf) < 0) {
+ fd = open(home1, O_WRONLY|O_CREAT|O_EXCL);
+ if (fd < 0) {
+ fprintf(stderr, "Can't create %s\n", home1);
+ return;
+ }
+ } else {
+ if (sbuf.st_nlink > 1) {
+ fprintf(stderr, "No symlinks!\n");
+ return;
+ }
+
+ fd = open(home1, O_WRONLY|O_EXCL);
+ if (fd < 0) {
+ fprintf(stderr, "Can't open %s for writing\n", home1);
+ return;
+ }
+ }
+
+ if ((fp = fdopen(fd, "w")) == 0) {
perror(home1);
return;
}
+
printf("Saved in %s.\n", home1);
fwrite(&WEIGHT, sizeof WEIGHT, 1, fp);
fwrite(&CUMBER, sizeof CUMBER, 1, fp);
- fwrite(&clock, sizeof clock, 1, fp);
+ fwrite(&gclock, sizeof gclock, 1, fp);
tmp = location == dayfile;
fwrite(&tmp, sizeof tmp, 1, fp);
for (n = 1; n <= NUMOFROOMS; n++) {
@@ -122,7 +153,7 @@ save()
fwrite(notes, sizeof notes, 1, fp);
fwrite(&direction, sizeof direction, 1, fp);
fwrite(&position, sizeof position, 1, fp);
- fwrite(&time, sizeof time, 1, fp);
+ fwrite(&gtime, sizeof gtime, 1, fp);
fwrite(&fuel, sizeof fuel, 1, fp);
fwrite(&torps, sizeof torps, 1, fp);
fwrite(&carrying, sizeof carrying, 1, fp);
OpenPOWER on IntegriCloud