diff options
author | hubertf <hubertf@NetBSD.org> | 1999-02-10 00:45:45 +0000 |
---|---|---|
committer | hubertf <hubertf@NetBSD.org> | 1999-02-10 00:45:45 +0000 |
commit | 22a27dede77166c7bdf1de647d4d2b961fcd92c2 (patch) | |
tree | eba164fcad44498207f63281968e224f52d40bcb /sail/sync.c | |
parent | e0e186091a777dfe9cf3c22deeff989f459d8db9 (diff) | |
download | bsdgames-darwin-22a27dede77166c7bdf1de647d4d2b961fcd92c2.tar.gz bsdgames-darwin-22a27dede77166c7bdf1de647d4d2b961fcd92c2.tar.zst bsdgames-darwin-22a27dede77166c7bdf1de647d4d2b961fcd92c2.zip |
The game sail(6) has a function Write() which is used both with
integer arguments and with string arguments (cast to long, and in one
place to int). The patch here cleans this up, making it into two
separate functions; this allows for the game to be made const-correct
in future and improves portability.
The patch also contains two other fragments: a change to use the
symbolic constant SEEK_END with fseek(), and a change to use snprintf
in one place to avoid a buffer overrun.
Via PR 6569 by Joseph Myers <jsm28@cam.ac.uk>.
Diffstat (limited to 'sail/sync.c')
-rw-r--r-- | sail/sync.c | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/sail/sync.c b/sail/sync.c index 7bab0f9a..db9f4834 100644 --- a/sail/sync.c +++ b/sail/sync.c @@ -1,4 +1,4 @@ -/* $NetBSD: sync.c,v 1.10 1998/09/11 14:15:46 hubertf Exp $ */ +/* $NetBSD: sync.c,v 1.11 1999/02/10 00:45:46 hubertf Exp $ */ /* * Copyright (c) 1983, 1993 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95"; #else -__RCSID("$NetBSD: sync.c,v 1.10 1998/09/11 14:15:46 hubertf Exp $"); +__RCSID("$NetBSD: sync.c,v 1.11 1999/02/10 00:45:46 hubertf Exp $"); #endif #endif /* not lint */ @@ -121,7 +121,7 @@ makesignal(va_alias) fmtship(format, sizeof(format), fmt, ship); (void) vsprintf(message, format, ap); va_end(ap); - Write(W_SIGNAL, from, 1, (long)message, 0, 0, 0); + Writestr(W_SIGNAL, from, message); } void @@ -146,8 +146,9 @@ makemsg(va_alias) #endif (void) vsprintf(message, fmt, ap); va_end(ap); - Write(W_SIGNAL, from, 1, (long)message, 0, 0, 0); + Writestr(W_SIGNAL, from, message); } + int sync_exists(game) int game; @@ -199,25 +200,37 @@ sync_close(remove) } void -Write(type, ship, isstr, a, b, c, d) +Write(type, ship, a, b, c, d) int type; struct ship *ship; - int isstr; long a, b, c, d; { - if (isstr) - (void) sprintf(sync_bp, "%d %d %d %s\n", - type, ship->file->index, isstr, (char *) a); - else - (void) sprintf(sync_bp, "%d %d %d %ld %ld %ld %ld\n", - type, ship->file->index, isstr, a, b, c, d); + (void) sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n", + type, ship->file->index, a, b, c, d); + while (*sync_bp++) + ; + sync_bp--; + if (sync_bp >= &sync_buf[sizeof sync_buf]) + abort(); + (void) sync_update(type, ship, NULL, a, b, c, d); +} + +void +Writestr(type, ship, a) + int type; + struct ship *ship; + const char *a; +{ + + (void) sprintf(sync_bp, "%d %d 1 %s\n", + type, ship->file->index, a); while (*sync_bp++) ; sync_bp--; if (sync_bp >= &sync_buf[sizeof sync_buf]) abort(); - (void) sync_update(type, ship, a, b, c, d); + (void) sync_update(type, ship, a, 0, 0, 0, 0); } int @@ -226,6 +239,7 @@ Sync() sig_t sighup, sigint; int n; int type, shipnum, isstr; + char *astr; long a, b, c, d; char buf[80]; char erred = 0; @@ -280,19 +294,21 @@ Sync() *p = 0; for (p = buf; *p == ' '; p++) ; - a = (long)p; - b = c = d = 0; - } else + astr = p; + a = b = c = d = 0; + } else { if (fscanf(sync_fp, "%ld%ld%ld%ld", &a, &b, &c, &d) != 4) goto bad; - if (sync_update(type, SHIP(shipnum), a, b, c, d) < 0) + astr = NULL; + } + if (sync_update(type, SHIP(shipnum), astr, a, b, c, d) < 0) goto bad; } bad: erred++; out: if (!erred && sync_bp != sync_buf) { - (void) fseek(sync_fp, 0L, 2); + (void) fseek(sync_fp, 0L, SEEK_END); (void) fwrite(sync_buf, sizeof *sync_buf, sync_bp - sync_buf, sync_fp); (void) fflush(sync_fp); @@ -310,9 +326,10 @@ out: } int -sync_update(type, ship, a, b, c, d) +sync_update(type, ship, astr, a, b, c, d) int type; struct ship *ship; + const char *astr; long a, b, c, d; { switch (type) { @@ -377,9 +394,9 @@ sync_update(type, ship, a, b, c, d) case W_SIGNAL: if (mode == MODE_PLAYER) { if (nobells) - Signal("$$: %s", ship, (char *) a); + Signal("$$: %s", ship, astr); else - Signal("\7$$: %s", ship, (char *) a); + Signal("\7$$: %s", ship, astr); } break; case W_CREW: { @@ -390,7 +407,7 @@ sync_update(type, ship, a, b, c, d) break; } case W_CAPTAIN: - (void) strncpy(ship->file->captain, (char *)a, + (void) strncpy(ship->file->captain, astr, sizeof ship->file->captain - 1); ship->file->captain[sizeof ship->file->captain - 1] = 0; break; @@ -429,7 +446,7 @@ sync_update(type, ship, a, b, c, d) ship->specs->hull = a; break; case W_MOVE: - (void) strncpy(ship->file->movebuf, (char *)a, + (void) strncpy(ship->file->movebuf, astr, sizeof ship->file->movebuf - 1); ship->file->movebuf[sizeof ship->file->movebuf - 1] = 0; break; |