]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - monop/execute.c
Remove config for the return type of signal handlers, which is also a
[bsdgames-darwin.git] / monop / execute.c
index 9b0e0829460908a69f887a59a32c7fb201768b91..6b5334a3e3d8b775fceb75a2a1655e0398bf625a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: execute.c,v 1.15 2008/02/24 01:30:56 dholland Exp $    */
+/*     $NetBSD: execute.c,v 1.21 2008/02/24 06:12:49 dholland Exp $    */
 
 /*
  * Copyright (c) 1980, 1993
 #if 0
 static char sccsid[] = "@(#)execute.c  8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: execute.c,v 1.15 2008/02/24 01:30:56 dholland Exp $");
+__RCSID("$NetBSD: execute.c,v 1.21 2008/02/24 06:12:49 dholland Exp $");
 #endif
 #endif /* not lint */
 
-#include "monop.ext"
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -49,6 +48,9 @@ __RCSID("$NetBSD: execute.c,v 1.15 2008/02/24 01:30:56 dholland Exp $");
 #include <time.h>
 #include <errno.h>
 
+#include "deck.h"
+#include "monop.h"
+
 #define MIN_FORMAT_VERSION 1
 #define CUR_FORMAT_VERSION 1
 #define MAX_FORMAT_VERSION 1
@@ -59,7 +61,6 @@ typedef       struct tm       TIME;
 static char    buf[257];
 
 static bool    new_play;       /* set if move on to new player         */
-extern void    *heapstart;
 
 static void show_move(void);
 
@@ -296,9 +297,9 @@ save()
                fprintf(outf, "    numcards %d\n", deck[i].num_cards);
                fprintf(outf, "    topcard %d\n", deck[i].top_card);
                fprintf(outf, "    gojf_used %d\n", deck[i].gojf_used);
-               fprintf(outf, "    offsets");
+               fprintf(outf, "    cards");
                for (j = 0; j < deck[i].num_cards; j++)
-                       fprintf(outf, " %ld", (long)(deck[i].offsets[j]));
+                       fprintf(outf, " %d", deck[i].cards[j]);
                fprintf(outf, "\n");
                fprintf(outf, "}\n");
        }
@@ -332,38 +333,41 @@ save()
  *     This routine restores an old game from a file
  */
 void
-restore()
+restore(void)
 {
        char *sp;
 
-       printf("Which file do you wish to restore from? ");
-       fgets(buf, sizeof(buf), stdin);
-       if (feof(stdin))
-               return;
-       sp = strchr(buf, '\n');
-       if (sp)
-               *sp = '\0';
-       rest_f(buf);
+       for (;;) {
+               printf("Which file do you wish to restore from? ");
+               fgets(buf, sizeof(buf), stdin);
+               if (feof(stdin))
+                       return;
+               sp = strchr(buf, '\n');
+               if (sp)
+                       *sp = '\0';
+               if (rest_f(buf) == 0)
+                       break;
+       }
 }
 
 /*
- *     This does the actual restoring.  It returns TRUE if the
- * backup was successful, else false.
+ * This does the actual restoring.  It returns zero on success,
+ * and -1 on failure.
  */
 int
-rest_f(file)
-       const char *file;
+rest_f(const char *file)
 {
        char *sp;
        FILE *inf;
        char xbuf[80];
        STAT sbuf;
        char readbuf[512];
+       int ret = 0;
 
        inf = fopen(file, "r");
        if (inf == NULL) {
                warn("%s", file);
-               return FALSE;
+               return -1;
        }
        printf("\"%s\" ", file);
        if (fstat(fileno(inf), &sbuf) < 0) {
@@ -386,11 +390,13 @@ rest_f(file)
                sp = strchr(readbuf, '\n');
                if (sp == NULL) {
                        printf("file is corrupt: long lines.\n");
+                       ret = -1;
                        break;
                }
                *sp = '\0';
 
                if (restore_parseline(readbuf)) {
+                       ret = -1;
                        break;
                }
        }
@@ -399,8 +405,16 @@ rest_f(file)
                warnx("%s: read error", file);
        fclose(inf);
 
+       if (ret < 0)
+               return -1;
+
        name_list[num_play] = "done";
 
+       if (play == NULL || cur_p == NULL || num_play < 2) {
+               printf("save file is incomplete.\n");
+               return -1;
+       }
+
        /*
         * We could at this point crosscheck the following:
         *    - there are only two GOJF cards floating around
@@ -414,7 +428,7 @@ rest_f(file)
                continue;
        *sp = '\0';
        printf("[%s]\n", xbuf);
-       return TRUE;
+       return 0;
 }
 
 /*
@@ -425,7 +439,7 @@ static enum {
        RI_NONE,
        RI_PLAYER,
        RI_DECK,
-       RI_SQUARE,
+       RI_SQUARE
 } restore_item;
 static int restore_itemnum;
 
@@ -524,7 +538,7 @@ restore_toplevel_attr(const char *attribute, char *txt)
                        printf("numplayers: multiple settings\n");
                        return -1;
                }
-               play = calloc(num_play, sizeof(play[0]));
+               play = calloc((size_t)num_play, sizeof(play[0]));
                if (play == NULL) {
                        err(1, "calloc");
                }
@@ -646,14 +660,19 @@ restore_deck_attr(const char *attribute, char *txt)
                        return -1;
                }
                dp->gojf_used = tmp;
-       } else if (!strcmp(attribute, "offsets")) {
+       } else if (!strcmp(attribute, "cards")) {
                errno = 0;
                s = txt;
                for (j = 0; j<dp->num_cards; j++) {
-                       dp->offsets[j] = strtol(s, &s, 10);
+                       tmp = strtol(s, &s, 10);
+                       if (tmp < 0 || tmp >= dp->num_cards) {
+                               printf("cards: out of range value\n");
+                               return -1;
+                       }
+                       dp->cards[j] = tmp;
                }
                if (errno) {
-                       printf("offsets: invalid values\n");
+                       printf("cards: invalid values\n");
                        return -1;
                }
        } else {