Module Name:    src
Committed By:   rillig
Date:           Sat May 21 12:29:34 UTC 2022

Modified Files:
        src/games/gomoku: bdisp.c gomoku.h main.c

Log Message:
gomoku: warn before overwriting a saved game file


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/games/gomoku/bdisp.c
cvs rdiff -u -r1.32 -r1.33 src/games/gomoku/gomoku.h
cvs rdiff -u -r1.44 -r1.45 src/games/gomoku/main.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/games/gomoku/bdisp.c
diff -u src/games/gomoku/bdisp.c:1.39 src/games/gomoku/bdisp.c:1.40
--- src/games/gomoku/bdisp.c:1.39	Sat May 21 12:16:53 2022
+++ src/games/gomoku/bdisp.c	Sat May 21 12:29:34 2022
@@ -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 @@ get_key(const char *allowed)
 }
 
 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 @@ get_line(char *buf, int size)
 			} else
 				beep();
 		}
+		if (on_change != NULL) {
+			*cp = '\0';
+			on_change(buf);
+		}
 		refresh();
 	}
 	*cp = '\0';

Index: src/games/gomoku/gomoku.h
diff -u src/games/gomoku/gomoku.h:1.32 src/games/gomoku/gomoku.h:1.33
--- src/games/gomoku/gomoku.h:1.32	Sat May 21 09:25:51 2022
+++ src/games/gomoku/gomoku.h	Sat May 21 12:29:34 2022
@@ -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 @@ extern const char *plyr[];
 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 *);

Index: src/games/gomoku/main.c
diff -u src/games/gomoku/main.c:1.44 src/games/gomoku/main.c:1.45
--- src/games/gomoku/main.c:1.44	Sat May 21 09:57:53 2022
+++ src/games/gomoku/main.c	Sat May 21 12:29:34 2022
@@ -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 quit(void) __dead;
 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 @@ again:
 		}
 	} 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 @@ again:
 					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 @@ again:
 					goto getinput;
 				}
 			} else {
-				if (!get_line(buf, sizeof(buf))) {
+				if (!get_line(buf, sizeof(buf), NULL)) {
 					curmove = RESIGN;
 					break;
 				}
@@ -332,7 +348,8 @@ again:
 				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 @@ whatsup(int signum)
 		quit();
 top:
 	ask("debug command: ");
-	if (!get_line(input, sizeof(input)))
+	if (!get_line(input, sizeof(input), NULL))
 		quit();
 	switch (*input) {
 	case '\0':

Reply via email to