String buffer cleanup. sprintf bad.

This commit is contained in:
dholland 2009-03-14 18:32:47 +00:00
parent 2a86bfea6b
commit 25e9926383
7 changed files with 53 additions and 48 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: dr_2.c,v 1.21 2009/03/14 17:10:01 dholland Exp $ */
/* $NetBSD: dr_2.c,v 1.22 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)dr_2.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: dr_2.c,v 1.21 2009/03/14 17:10:01 dholland Exp $");
__RCSID("$NetBSD: dr_2.c,v 1.22 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -48,9 +48,9 @@ __RCSID("$NetBSD: dr_2.c,v 1.21 2009/03/14 17:10:01 dholland Exp $");
#define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
static int str_end(const char *);
static int score(struct ship *, struct ship *, char *, int);
static int score(struct ship *, struct ship *, char *, size_t, int);
static void move_ship(struct ship *, const char *, unsigned char *, short *, short *, int *);
static void try(struct ship *, struct ship *, char *, char *, int, int, int, int, int, int *, int);
static void try(struct ship *f, struct ship *t, char *command, size_t commandmax, char *temp, size_t tempmax, int ma, int ta, int af, int vma, int dir, int *high, int rakeme);
static void rmend(char *);
const int dtab[] = {0,1,1,2,3,4,4,5}; /* diagonal distances in x==y */
@ -146,18 +146,18 @@ str_end(const char *str)
}
void
closeon(struct ship *from, struct ship *to, char *command, int ta, int ma, int af)
closeon(struct ship *from, struct ship *to, char *command, size_t commandmax, int ta, int ma, int af)
{
int high;
char temp[10];
temp[0] = command[0] = '\0';
high = -30000;
try(from, to, command, temp, ma, ta, af, ma, from->file->dir, &high, 0);
try(from, to, command, commandmax, temp, sizeof(temp), ma, ta, af, ma, from->file->dir, &high, 0);
}
static int
score(struct ship *ship, struct ship *to, char *movement, int onlytemp)
score(struct ship *ship, struct ship *to, char *movement, size_t movementmax, int onlytemp)
{
int drift;
int row, col, dir, total, ran;
@ -170,7 +170,7 @@ score(struct ship *ship, struct ship *to, char *movement, int onlytemp)
drift = fp->drift;
move_ship(ship, movement, &fp->dir, &fp->row, &fp->col, &drift);
if (!*movement)
strcpy(movement, "d");
strlcpy(movement, "d", movementmax);
ran = range(ship, to);
total = -50 * ran;
@ -228,7 +228,7 @@ move_ship(struct ship *ship, const char *p, unsigned char *dir, short *row, shor
}
static void
try(struct ship *f, struct ship *t, char *command, char *temp, int ma, int ta, int af, int vma, int dir, int *high, int rakeme)
try(struct ship *f, struct ship *t, char *command, size_t commandmax, char *temp, size_t tempmax, int ma, int ta, int af, int vma, int dir, int *high, int rakeme)
{
int new, n;
char st[4];
@ -236,35 +236,35 @@ try(struct ship *f, struct ship *t, char *command, char *temp, int ma, int ta, i
if ((n = str_end(temp)) < '1' || n > '9')
for (n = 1; vma - n >= 0; n++) {
sprintf(st, "%d", n);
strcat(temp, st);
new = score(f, t, temp, rakeme);
snprintf(st, sizeof(st), "%d", n);
strlcat(temp, st, tempmax);
new = score(f, t, temp, tempmax, rakeme);
if (new > *high && (!rakeme || rakeyou)) {
*high = new;
strcpy(command, temp);
strlcpy(command, temp, commandmax);
}
try(f, t, command, temp, ma-n, ta, af, vma-n,
try(f, t, command, commandmax, temp, tempmax, ma-n, ta, af, vma-n,
dir, high, rakeme);
rmend(temp);
}
if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)) {
strcat(temp, "r");
new = score(f, t, temp, rakeme);
strlcat(temp, "r", tempmax);
new = score(f, t, temp, tempmax, rakeme);
if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))) {
*high = new;
strcpy(command, temp);
strlcpy(command, temp, commandmax);
}
try(f, t, command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1), high, rakeme);
try(f, t, command, commandmax, temp, tempmax, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1), high, rakeme);
rmend(temp);
}
if ((ma > 0 && ta > 0 && (n = str_end(temp)) != 'l' && n != 'r') || !strlen(temp)){
strcat(temp, "l");
new = score(f, t, temp, rakeme);
strlcat(temp, "l", sizeof(temp));
new = score(f, t, temp, tempmax, rakeme);
if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
*high = new;
strcpy(command, temp);
strlcpy(command, temp, commandmax);
}
try(f, t, command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), high, rakeme);
try(f, t, command, commandmax, temp, tempmax, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), high, rakeme);
rmend(temp);
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: dr_3.c,v 1.15 2003/08/07 09:37:42 agc Exp $ */
/* $NetBSD: dr_3.c,v 1.16 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)dr_3.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: dr_3.c,v 1.15 2003/08/07 09:37:42 agc Exp $");
__RCSID("$NetBSD: dr_3.c,v 1.16 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -77,6 +77,7 @@ moveall(void)
*sp->file->movebuf = '\0';
else
closeon(sp, closest, sp->file->movebuf,
sizeof(sp->file->movebuf),
ta, ma, af);
} else
*sp->file->movebuf = '\0';

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.28 2006/03/18 01:43:52 abs Exp $ */
/* $NetBSD: extern.h,v 1.29 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -304,7 +304,7 @@ int next (void);
void thinkofgrapples (void);
void checkup (void);
void prizecheck (void);
void closeon (struct ship *, struct ship *, char *, int, int, int);
void closeon (struct ship *, struct ship *, char *, size_t, int, int, int);
/* dr_3.c */
void moveall (void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: lo_main.c,v 1.13 2008/01/28 01:58:01 dholland Exp $ */
/* $NetBSD: lo_main.c,v 1.14 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)lo_main.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: lo_main.c,v 1.13 2008/01/28 01:58:01 dholland Exp $");
__RCSID("$NetBSD: lo_main.c,v 1.14 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -83,9 +83,11 @@ lo_main(void)
while (fread((char *)&log, sizeof log, 1, fp) == 1 &&
log.l_name[0] != '\0') {
if (longfmt && (pass = getpwuid(log.l_uid)) != NULL)
sprintf(sbuf, "%10.10s (%s)", log.l_name, pass->pw_name);
snprintf(sbuf, sizeof(sbuf),
"%10.10s (%s)", log.l_name, pass->pw_name);
else
sprintf(sbuf, "%20.20s", log.l_name);
snprintf(sbuf, sizeof(sbuf),
"%20.20s", log.l_name);
ship = &scene[log.l_gamenum].ship[log.l_shipnum];
printf("%-10s %21s of the %15s %3d points, %5.2f equiv\n",
title[n++], sbuf, ship->shipname, log.l_netpoints,

View File

@ -1,4 +1,4 @@
/* $NetBSD: pl_5.c,v 1.19 2008/01/28 01:58:01 dholland Exp $ */
/* $NetBSD: pl_5.c,v 1.20 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)pl_5.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: pl_5.c,v 1.19 2008/01/28 01:58:01 dholland Exp $");
__RCSID("$NetBSD: pl_5.c,v 1.20 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -146,9 +146,9 @@ acceptmove(void)
}
}
if (*buf)
strcpy(movebuf, buf);
strlcpy(movebuf, buf, sizeof(movebuf));
else
strcpy(movebuf, "d");
strlcpy(movebuf, "d", sizeof(movebuf));
Writestr(W_MOVE, ms, movebuf);
Msg("Helm: %s.", movebuf);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pl_main.c,v 1.19 2009/03/02 07:33:30 dholland Exp $ */
/* $NetBSD: pl_main.c,v 1.20 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)pl_main.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: pl_main.c,v 1.19 2009/03/02 07:33:30 dholland Exp $");
__RCSID("$NetBSD: pl_main.c,v 1.20 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -203,7 +203,7 @@ reprint:
ms->shipname, mc->guns, classname[mc->class],
qualname[mc->qual]);
if ((nameptr = (char *) getenv("SAILNAME")) && *nameptr)
strncpy(captain, nameptr, sizeof captain);
strlcpy(captain, nameptr, sizeof captain);
else {
printf("Your name, Captain? ");
fflush(stdout);
@ -214,7 +214,6 @@ reprint:
else
captain[strlen(captain) - 1] = '\0';
}
captain[sizeof captain - 1] = '\0';
Writestr(W_CAPTAIN, ms, captain);
for (n = 0; n < 2; n++) {
char buf[10];

View File

@ -1,4 +1,4 @@
/* $NetBSD: sync.c,v 1.25 2008/01/28 01:58:01 dholland Exp $ */
/* $NetBSD: sync.c,v 1.26 2009/03/14 18:32:47 dholland Exp $ */
/*
* Copyright (c) 1983, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)sync.c 8.2 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: sync.c,v 1.25 2008/01/28 01:58:01 dholland Exp $");
__RCSID("$NetBSD: sync.c,v 1.26 2009/03/14 18:32:47 dholland Exp $");
#endif
#endif /* not lint */
@ -99,7 +99,7 @@ makesignal(struct ship *from, const char *fmt, struct ship *ship, ...)
va_start(ap, ship);
fmtship(format, sizeof(format), fmt, ship);
vsprintf(message, format, ap);
vsnprintf(message, sizeof(message), format, ap);
va_end(ap);
Writestr(W_SIGNAL, from, message);
}
@ -112,7 +112,7 @@ makemsg(struct ship *from, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
vsprintf(message, fmt, ap);
vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);
Writestr(W_SIGNAL, from, message);
}
@ -124,7 +124,7 @@ sync_exists(int gamenum)
struct stat s;
time_t t;
sprintf(buf, SF, gamenum);
snprintf(buf, sizeof(buf), SF, gamenum);
time(&t);
setegid(egid);
if (stat(buf, &s) < 0) {
@ -133,7 +133,7 @@ sync_exists(int gamenum)
}
if (s.st_mtime < t - 60*60*2) { /* 2 hours */
unlink(buf);
sprintf(buf, LF, gamenum);
snprintf(buf, sizeof(buf), LF, gamenum);
unlink(buf);
setegid(gid);
return 0;
@ -149,8 +149,8 @@ sync_open(void)
struct stat tmp;
if (sync_fp != NULL)
fclose(sync_fp);
sprintf(sync_lock, LF, game);
sprintf(sync_file, SF, game);
snprintf(sync_lock, sizeof(sync_lock), LF, game);
snprintf(sync_file, sizeof(sync_file), SF, game);
setegid(egid);
if (stat(sync_file, &tmp) < 0) {
mode_t omask = umask(002);
@ -180,8 +180,9 @@ sync_close(int doremove)
void
Write(int type, struct ship *ship, long a, long b, long c, long d)
{
size_t max = sizeof(sync_buf) - (sync_bp - sync_buf);
sprintf(sync_bp, "%d %d 0 %ld %ld %ld %ld\n",
snprintf(sync_bp, max, "%d %d 0 %ld %ld %ld %ld\n",
type, ship->file->index, a, b, c, d);
while (*sync_bp++)
;
@ -194,7 +195,9 @@ Write(int type, struct ship *ship, long a, long b, long c, long d)
void
Writestr(int type, struct ship *ship, const char *a)
{
sprintf(sync_bp, "%d %d 1 %s\n", type, ship->file->index, a);
size_t max = sizeof(sync_buf) - (sync_bp - sync_buf);
snprintf(sync_bp, max, "%d %d 1 %s\n", type, ship->file->index, a);
while (*sync_bp++)
;
sync_bp--;