+/* $NetBSD: save.c,v 1.12 2005/07/01 06:04:54 jmc Exp $ */
+
/*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1993
+ * The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-/*static char sccsid[] = "from: @(#)save.c 5.3 (Berkeley) 6/1/90";*/
-static char rcsid[] = "$Id: save.c,v 1.2 1993/08/01 18:55:52 mycroft Exp $";
-#endif /* not lint */
+#if 0
+static char sccsid[] = "@(#)save.c 8.2 (Berkeley) 4/28/95";
+#else
+__RCSID("$NetBSD: save.c,v 1.12 2005/07/01 06:04:54 jmc Exp $");
+#endif
+#endif /* not lint */
-#include "externs.h"
+#include "extern.h"
-restore()
+void
+restore(const char *filename)
{
- char *getenv();
- char *home;
- char home1[100];
- register int n;
- int tmp;
- register FILE *fp;
+ int n;
+ int tmp;
+ FILE *fp;
- home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
- if ((fp = fopen(home1, "r")) == 0) {
- perror(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++) {
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);
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);
}
-save()
+void
+save(const char *filename)
{
- char *getenv();
- char *home;
- char home1[100];
- register int n;
- int tmp;
- FILE *fp;
+ int n;
+ int tmp;
+ FILE *fp;
- home = getenv("HOME");
- strcpy(home1, home);
- strcat(home1, "/Bstar");
- if ((fp = fopen(home1, "w")) == 0) {
- perror(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++) {
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);
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;
}