diff options
author | rodri <rgl@antares-labs.eu> | 2023-10-07 22:33:25 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2023-10-07 22:33:25 +0000 |
commit | 71c2cd61f6e53a455140f5920cff88cdfd4b4d33 (patch) | |
tree | 14983dd6f41f359cbe143dd3dc2bb55691037e99 /util.c | |
parent | c5ca61912a88aeb8ae727c0d217b7dc41efeb00d (diff) | |
download | battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.gz battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.tar.bz2 battleship-71c2cd61f6e53a455140f5920cff88cdfd4b4d33.zip |
make cell2coords thread-safe.
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 16 |
1 files changed, 6 insertions, 10 deletions
@@ -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; } |