-/* $NetBSD: save.c,v 1.9 2003/08/07 09:37:40 agc Exp $ */
+/* $NetBSD: save.c,v 1.12 2008/01/14 00:23:52 dholland Exp $ */
/*
* Copyright (c) 1988, 1993
#if 0
static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
#else
-__RCSID("$NetBSD: save.c,v 1.9 2003/08/07 09:37:40 agc Exp $");
+__RCSID("$NetBSD: save.c,v 1.12 2008/01/14 00:23:52 dholland Exp $");
#endif
#endif /* not lint */
#include "rogue.h"
short write_failed = 0;
-char *save_file = (char *) 0;
+char *save_file = (char *)0;
void
save_game()
{
char fname[64];
- if (!get_input_line("file name?", save_file, fname, "game not saved",
- 0, 1)) {
+ if (!get_input_line("file name?", save_file, fname, sizeof(fname),
+ "game not saved", 0, 1)) {
return;
}
check_message();
- message(fname, 0);
+ messagef(0, "%s", fname);
save_into_file(fname);
}
len = strlen(hptr) + strlen(sfile);
name_buffer = md_malloc(len);
if (name_buffer == NULL) {
- message("out of memory for save file name", 0);
+ messagef(0,
+ "out of memory for save file name");
sfile = error_file;
} else {
- (void) strcpy(name_buffer, hptr);
- (void) strcat(name_buffer, sfile+1);
+ (void)strcpy(name_buffer, hptr);
+ (void)strcat(name_buffer, sfile+1);
sfile = name_buffer;
}
+ /*
+ * Note: name_buffer gets leaked. But it's small,
+ * and in the common case we're about to exit.
+ */
}
}
if (((fp = fopen(sfile, "w")) == NULL) ||
((file_id = md_get_file_id(sfile)) == -1)) {
- message("problem accessing the save file", 0);
+ if (fp)
+ fclose(fp);
+ messagef(0, "problem accessing the save file");
return;
}
md_ignore_signals();
write_failed = 0;
- (void) xxx(1);
+ (void)xxx(1);
r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
r_write(fp, (char *) &cur_level, sizeof(cur_level));
r_write(fp, (char *) &max_level, sizeof(max_level));
fclose(fp);
if (write_failed) {
- (void) md_df(sfile); /* delete file */
+ (void)md_df(sfile); /* delete file */
} else {
clean_up("");
}
FILE *fp;
struct rogue_time saved_time, mod_time;
char buf[4];
- char tbuf[40];
+ char tbuf[MAX_OPT_LEN];
int new_file_id, saved_file_id;
fp = NULL;
if (md_link_count(fname) > 1) {
clean_up("file has link");
}
- (void) xxx(1);
+ (void)xxx(1);
r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
r_read(fp, (char *) &cur_level, sizeof(cur_level));
r_read(fp, (char *) &max_level, sizeof(max_level));
read_string(hunger_str, fp, sizeof hunger_str);
- (void) strlcpy(tbuf, login_name, sizeof tbuf);
+ (void)strlcpy(tbuf, login_name, sizeof tbuf);
read_string(login_name, fp, sizeof login_name);
if (strcmp(tbuf, login_name)) {
clean_up("you're not the original player");
for (;;) {
r_read(fp, (char *) &read_obj, sizeof(object));
if (read_obj.ichar == 0) {
- pack->next_object = (object *) 0;
+ pack->next_object = (object *)0;
break;
}
new_obj = alloc_object();
short n;
r_read(fp, (char *) &n, sizeof(short));
- if (n > len)
+ if (n<=0 || (size_t)(unsigned short)n > len) {
clean_up("read_string: corrupt game file");
+ }
r_read(fp, s, n);
xxxx(s, n);
+ /* ensure null termination */
+ s[n-1] = 0;
}
void
{
if (!write_failed) {
if (fwrite(buf, sizeof(char), n, fp) != (size_t)n) {
- message("write() failed, don't know why", 0);
+ messagef(0, "write() failed, don't know why");
sound_bell();
write_failed = 1;
}