]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - rogue/save.c
Whitespace/KNF nits.
[bsdgames-darwin.git] / rogue / save.c
index 8cbf40298fde911857893f4bff2d6d8918cd475f..59aa6f2696d73522e4cbcb0d665abdb922bba964 100644 (file)
@@ -1,4 +1,4 @@
-/*     $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
@@ -37,7 +37,7 @@
 #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 */
 
@@ -57,19 +57,19 @@ __RCSID("$NetBSD: save.c,v 1.9 2003/08/07 09:37:40 agc Exp $");
 #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);
 }
 
@@ -89,23 +89,30 @@ save_into_file(sfile)
                        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));
@@ -145,7 +152,7 @@ save_into_file(sfile)
        fclose(fp);
 
        if (write_failed) {
-               (void) md_df(sfile);    /* delete file */
+               (void)md_df(sfile);     /* delete file */
        } else {
                clean_up("");
        }
@@ -158,7 +165,7 @@ restore(fname)
        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;
@@ -169,13 +176,13 @@ restore(fname)
        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");
@@ -258,7 +265,7 @@ read_pack(pack, fp, is_rogue)
        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();
@@ -349,10 +356,13 @@ read_string(s, fp, len)
        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
@@ -387,7 +397,7 @@ r_write(fp, buf, n)
 {
        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;
                }