]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - rogue/save.c
Whitespace/KNF nits.
[bsdgames-darwin.git] / rogue / save.c
index d306d924e2190aee4b10bbf53d923898f3e01e1a..59aa6f2696d73522e4cbcb0d665abdb922bba964 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: save.c,v 1.6 1999/09/13 17:19:55 jsm Exp $     */
+/*     $NetBSD: save.c,v 1.12 2008/01/14 00:23:52 dholland Exp $       */
 
 /*
  * Copyright (c) 1988, 1993
  * 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.
  *
@@ -41,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)save.c     8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: save.c,v 1.6 1999/09/13 17:19:55 jsm Exp $");
+__RCSID("$NetBSD: save.c,v 1.12 2008/01/14 00:23:52 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -61,19 +57,19 @@ __RCSID("$NetBSD: save.c,v 1.6 1999/09/13 17:19:55 jsm 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);
 }
 
@@ -93,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 = fopen(sfile, "w")) == NULL) ||
+           ((file_id = md_get_file_id(sfile)) == -1)) {
+               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));
@@ -149,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("");
        }
@@ -162,25 +165,25 @@ 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;
-       if (    ((new_file_id = md_get_file_id(fname)) == -1) ||
-                       ((fp = fopen(fname, "r")) == NULL)) {
+       if (((new_file_id = md_get_file_id(fname)) == -1) ||
+           ((fp = fopen(fname, "r")) == NULL)) {
                clean_up("cannot open file");
        }
        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);
+       read_string(hunger_str, fp, sizeof hunger_str);
 
-       (void) strcpy(tbuf, login_name);
-       read_string(login_name, fp);
+       (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");
        }
@@ -262,16 +265,16 @@ 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();
                *new_obj = read_obj;
                if (is_rogue) {
                        if (new_obj->in_use_flags & BEING_WORN) {
-                                       do_wear(new_obj);
+                               do_wear(new_obj);
                        } else if (new_obj->in_use_flags & BEING_WIELDED) {
-                                       do_wield(new_obj);
+                               do_wield(new_obj);
                        } else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
                                do_put_on(new_obj,
                                        ((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
@@ -326,7 +329,7 @@ rw_id(id_table, fp, n, wr)
                        r_read(fp, (char *) &(id_table[i].value), sizeof(short));
                        r_read(fp, (char *) &(id_table[i].id_status),
                                sizeof(unsigned short));
-                       read_string(id_table[i].title, fp);
+                       read_string(id_table[i].title, fp, MAX_ID_TITLE_LEN);
                }
        }
 }
@@ -345,15 +348,21 @@ write_string(s, fp)
 }
 
 void
-read_string(s, fp)
+read_string(s, fp, len)
        char *s;
        FILE *fp;
+       size_t len;
 {
        short n;
 
        r_read(fp, (char *) &n, sizeof(short));
+       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
@@ -375,7 +384,7 @@ r_read(fp, buf, n)
        char *buf;
        int n;
 {
-       if (fread(buf, sizeof(char), n, fp) != n) {
+       if (fread(buf, sizeof(char), n, fp) != (size_t)n) {
                clean_up("read() failed, don't know why");
        }
 }
@@ -387,8 +396,8 @@ r_write(fp, buf, n)
        int n;
 {
        if (!write_failed) {
-               if (fwrite(buf, sizeof(char), n, fp) != n) {
-                       message("write() failed, don't know why", 0);
+               if (fwrite(buf, sizeof(char), n, fp) != (size_t)n) {
+                       messagef(0, "write() failed, don't know why");
                        sound_bell();
                        write_failed = 1;
                }