aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-10-07 22:33:25 +0000
committerrodri <rgl@antares-labs.eu>2023-10-07 22:33:25 +0000
commit71c2cd61f6e53a455140f5920cff88cdfd4b4d33 (patch)
tree14983dd6f41f359cbe143dd3dc2bb55691037e99 /util.c
parentc5ca61912a88aeb8ae727c0d217b7dc41efeb00d (diff)
downloadbattleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.gz
battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.bz2
battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.zip
make cell2coords thread-safe.
Diffstat (limited to 'util.c')
-rw-r--r--util.c16
1 files changed, 6 insertions, 10 deletions
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;
}