From 71c2cd61f6e53a455140f5920cff88cdfd4b4d33 Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 7 Oct 2023 22:33:25 +0000 Subject: make cell2coords thread-safe. --- util.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 1ff9a42..cedd89e 100644 --- a/util.c +++ b/util.c @@ -10,7 +10,7 @@ #include "dat.h" #include "fns.h" -static char rowtab[] = "abcdefghijklmnopq"; +static char rowtab[] = "abcdefghijklmnopq"; /* |rowtab| = MAPH */ static int shiplentab[] = { [Scarrier] 5, [Sbattleship] 4, @@ -42,15 +42,11 @@ isoob(Point2 cell) cell.y < 0 || cell.y >= MAPH; } -char * -cell2coords(Point2 cell) +int +cell2coords(char *buf, ulong len, Point2 cell) { - static char s[3+1]; - - assert(!isoob(cell)); - - snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x); - return s; + assert(len >= 3+1 && !isoob(cell)); + return snprint(buf, len, "%c%d", rowtab[(int)cell.y], (int)cell.x); } Point2 @@ -66,7 +62,7 @@ coords2cell(char *s) cell.y = p-rowtab; cell.x = strtol(s+1, nil, 10); - assert(cell.x >= 0 && cell.x < MAPW); + assert(!isoob(cell)); return cell; } -- cgit v1.2.3