Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/gomoku gomoku: warn before overwriting a saved game file



details:   https://anonhg.NetBSD.org/src/rev/5663d2922289
branches:  trunk
changeset: 366272:5663d2922289
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 21 12:29:34 2022 +0000

description:
gomoku: warn before overwriting a saved game file

diffstat:

 games/gomoku/bdisp.c  |  10 +++++++---
 games/gomoku/gomoku.h |   4 ++--
 games/gomoku/main.c   |  31 ++++++++++++++++++++++++-------
 3 files changed, 33 insertions(+), 12 deletions(-)

diffs (144 lines):

diff -r 76e9567c2ed0 -r 5663d2922289 games/gomoku/bdisp.c
--- a/games/gomoku/bdisp.c      Sat May 21 12:16:53 2022 +0000
+++ b/games/gomoku/bdisp.c      Sat May 21 12:29:34 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bdisp.c,v 1.39 2022/05/21 12:16:53 rillig Exp $        */
+/*     $NetBSD: bdisp.c,v 1.40 2022/05/21 12:29:34 rillig Exp $        */
 
 /*
  * Copyright (c) 1994
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 /*     @(#)bdisp.c     8.2 (Berkeley) 5/3/95   */
-__RCSID("$NetBSD: bdisp.c,v 1.39 2022/05/21 12:16:53 rillig Exp $");
+__RCSID("$NetBSD: bdisp.c,v 1.40 2022/05/21 12:29:34 rillig Exp $");
 
 #include <curses.h>
 #include <string.h>
@@ -272,7 +272,7 @@
 }
 
 bool
-get_line(char *buf, int size)
+get_line(char *buf, int size, void (*on_change)(const char *))
 {
        char *cp, *end;
        int c;
@@ -311,6 +311,10 @@
                        } else
                                beep();
                }
+               if (on_change != NULL) {
+                       *cp = '\0';
+                       on_change(buf);
+               }
                refresh();
        }
        *cp = '\0';
diff -r 76e9567c2ed0 -r 5663d2922289 games/gomoku/gomoku.h
--- a/games/gomoku/gomoku.h     Sat May 21 12:16:53 2022 +0000
+++ b/games/gomoku/gomoku.h     Sat May 21 12:29:34 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gomoku.h,v 1.32 2022/05/21 09:25:51 rillig Exp $       */
+/*     $NetBSD: gomoku.h,v 1.33 2022/05/21 12:29:34 rillig Exp $       */
 
 /*
  * Copyright (c) 1994
@@ -229,7 +229,7 @@
 void   bdinit(struct spotstr *);
 int    get_coord(void);
 int    get_key(const char *);
-bool   get_line(char *, int);
+bool   get_line(char *, int, void (*)(const char *));
 void   ask(const char *);
 void   dislog(const char *);
 void   bdump(FILE *);
diff -r 76e9567c2ed0 -r 5663d2922289 games/gomoku/main.c
--- a/games/gomoku/main.c       Sat May 21 12:16:53 2022 +0000
+++ b/games/gomoku/main.c       Sat May 21 12:29:34 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.44 2022/05/21 09:57:53 rillig Exp $ */
+/*     $NetBSD: main.c,v 1.45 2022/05/21 12:29:34 rillig Exp $ */
 
 /*
  * Copyright (c) 1994
@@ -36,8 +36,9 @@
 __COPYRIGHT("@(#) Copyright (c) 1994\
  The Regents of the University of California.  All rights reserved.");
 /*     @(#)main.c      8.4 (Berkeley) 5/4/95   */
-__RCSID("$NetBSD: main.c,v 1.44 2022/05/21 09:57:53 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.45 2022/05/21 12:29:34 rillig Exp $");
 
+#include <sys/stat.h>
 #include <curses.h>
 #include <err.h>
 #include <limits.h>
@@ -80,6 +81,20 @@
 static void quitsig(int) __dead;
 #endif
 
+static void
+warn_if_exists(const char *fname)
+{
+       struct stat st;
+
+       if (lstat(fname, &st) == 0) {
+               int x, y;
+               getyx(stdscr, y, x);
+               addstr("  (already exists)");
+               move(y, x);
+       } else
+               clrtoeol();
+}
+
 int
 main(int argc, char **argv)
 {
@@ -180,7 +195,7 @@
                }
        } else {
                setbuf(stdout, 0);
-               get_line(buf, sizeof(buf));
+               get_line(buf, sizeof(buf), NULL);
                if (strcmp(buf, "black") == 0)
                        color = BLACK;
                else if (strcmp(buf, "white") == 0)
@@ -258,7 +273,8 @@
                                        FILE *fp;
 
                                        ask("Save file name? ");
-                                       (void)get_line(fname, sizeof(fname));
+                                       (void)get_line(fname, sizeof(fname),
+                                           warn_if_exists);
                                        if ((fp = fopen(fname, "w")) == NULL) {
                                                misclog("cannot create save file");
                                                goto getinput;
@@ -276,7 +292,7 @@
                                        goto getinput;
                                }
                        } else {
-                               if (!get_line(buf, sizeof(buf))) {
+                               if (!get_line(buf, sizeof(buf), NULL)) {
                                        curmove = RESIGN;
                                        break;
                                }
@@ -332,7 +348,8 @@
                                FILE *fp;
 
                                ask("Save file name? ");
-                               (void)get_line(fname, sizeof(fname));
+                               (void)get_line(fname, sizeof(fname),
+                                   warn_if_exists);
                                if ((fp = fopen(fname, "w")) == NULL) {
                                        misclog("cannot create save file");
                                        goto replay;
@@ -383,7 +400,7 @@
                quit();
 top:
        ask("debug command: ");
-       if (!get_line(input, sizeof(input)))
+       if (!get_line(input, sizeof(input), NULL))
                quit();
        switch (*input) {
        case '\0':



Home | Main Index | Thread Index | Old Index