X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/61a235f9c8a332df14e8fe40b7b1fcaa98ecc548..fcf2c9d81824f115900bc2e3e5ed8be2afbb7d46:/battlestar/save.c diff --git a/battlestar/save.c b/battlestar/save.c index cb173ef2..8af9142b 100644 --- a/battlestar/save.c +++ b/battlestar/save.c @@ -1,4 +1,4 @@ -/* $NetBSD: save.c,v 1.6 1997/10/11 02:07:37 lukem Exp $ */ +/* $NetBSD: save.c,v 1.12 2005/07/01 06:04:54 jmc Exp $ */ /* * Copyright (c) 1983, 1993 @@ -12,11 +12,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -38,31 +34,27 @@ #if 0 static char sccsid[] = "@(#)save.c 8.2 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: save.c,v 1.6 1997/10/11 02:07:37 lukem Exp $"); +__RCSID("$NetBSD: save.c,v 1.12 2005/07/01 06:04:54 jmc Exp $"); #endif #endif /* not lint */ #include "extern.h" void -restore() +restore(const char *filename) { - char *home; - char home1[100]; int n; int tmp; FILE *fp; - home = getenv("HOME"); - strcpy(home1, home); - strcat(home1, "/Bstar"); - if ((fp = fopen(home1, "r")) == 0) { - warn("fopen %s", home1); - return; + if (filename == NULL) + exit(1); /* Error determining save file name. */ + if ((fp = fopen(filename, "r")) == 0) { + err(1, "fopen %s", filename); } fread(&WEIGHT, sizeof WEIGHT, 1, fp); fread(&CUMBER, sizeof CUMBER, 1, fp); - fread(&clock, sizeof clock, 1, fp); + fread(&ourclock, sizeof ourclock, 1, fp); fread(&tmp, sizeof tmp, 1, fp); location = tmp ? dayfile : nightfile; for (n = 1; n <= NUMOFROOMS; n++) { @@ -75,7 +67,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(&ourtime, sizeof ourtime, 1, fp); fread(&fuel, sizeof fuel, 1, fp); fread(&torps, sizeof torps, 1, fp); fread(&carrying, sizeof carrying, 1, fp); @@ -94,29 +86,29 @@ restore() fread(&loved, sizeof loved, 1, fp); fread(&pleasure, sizeof pleasure, 1, fp); fread(&power, sizeof power, 1, fp); - fread(&ego, sizeof ego, 1, fp); + /* We must check the last read, to catch truncated save files */ + if (fread(&ego, sizeof ego, 1, fp) < 1) + errx(1, "save file %s too short", filename); + fclose(fp); } void -save() +save(const char *filename) { - char *home; - char home1[100]; int n; int tmp; FILE *fp; - home = getenv("HOME"); - strcpy(home1, home); - strcat(home1, "/Bstar"); - if ((fp = fopen(home1, "w")) == 0) { - warn("fopen %s", home1); + if (filename == NULL) + return; /* Error determining save file name. */ + if ((fp = fopen(filename, "w")) == NULL) { + warn("fopen %s", filename); return; } - printf("Saved in %s.\n", home1); + printf("Saved in %s.\n", filename); fwrite(&WEIGHT, sizeof WEIGHT, 1, fp); fwrite(&CUMBER, sizeof CUMBER, 1, fp); - fwrite(&clock, sizeof clock, 1, fp); + fwrite(&ourclock, sizeof ourclock, 1, fp); tmp = location == dayfile; fwrite(&tmp, sizeof tmp, 1, fp); for (n = 1; n <= NUMOFROOMS; n++) { @@ -129,7 +121,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(&ourtime, sizeof ourtime, 1, fp); fwrite(&fuel, sizeof fuel, 1, fp); fwrite(&torps, sizeof torps, 1, fp); fwrite(&carrying, sizeof carrying, 1, fp); @@ -149,4 +141,55 @@ save() fwrite(&pleasure, sizeof pleasure, 1, fp); fwrite(&power, sizeof power, 1, fp); fwrite(&ego, sizeof ego, 1, fp); + fflush(fp); + if (ferror(fp)) + warn("fwrite %s", filename); + fclose(fp); +} + +/* + * Given a save file name (possibly from fgetln, so without terminating NUL), + * determine the name of the file to be saved to by adding the HOME + * directory if the name does not contain a slash. Name will be allocated + * with malloc(3). + */ +char * +save_file_name(const char *filename, size_t len) +{ + char *home; + char *newname; + size_t tmpl; + + if (memchr(filename, '/', len)) { + newname = malloc(len + 1); + if (newname == NULL) { + warn(NULL); + return NULL; + } + memcpy(newname, filename, len); + newname[len] = 0; + } else { + home = getenv("HOME"); + if (home != NULL) { + tmpl = strlen(home); + newname = malloc(tmpl + len + 2); + if (newname == NULL) { + warn(NULL); + return NULL; + } + memcpy(newname, home, tmpl); + newname[tmpl] = '/'; + memcpy(newname + tmpl + 1, filename, len); + newname[tmpl + len + 1] = 0; + } else { + newname = malloc(len + 1); + if (newname == NULL) { + warn(NULL); + return NULL; + } + memcpy(newname, filename, len); + newname[len] = 0; + } + } + return newname; }