summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--games/battlestar/cypher.c29
-rw-r--r--games/battlestar/extern.h4
-rw-r--r--games/battlestar/init.c4
-rw-r--r--games/battlestar/save.c46
-rw-r--r--games/hunt/huntd/conf.c30
-rw-r--r--games/monop/execute.c31
6 files changed, 65 insertions, 79 deletions
diff --git a/games/battlestar/cypher.c b/games/battlestar/cypher.c
index f7b08498dfd..00b1ac8209e 100644
--- a/games/battlestar/cypher.c
+++ b/games/battlestar/cypher.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cypher.c,v 1.20 2019/05/09 20:19:22 tedu Exp $ */
+/* $OpenBSD: cypher.c,v 1.21 2022/08/08 17:57:05 op Exp $ */
/* $NetBSD: cypher.c,v 1.3 1995/03/21 15:07:15 cgd Exp $ */
/*
@@ -84,8 +84,9 @@ cypher(void)
int n;
int junk;
int lflag = -1;
- char *filename, *rfilename;
- size_t filename_len;
+ char *line = NULL, *filename;
+ size_t linesize = 0;
+ ssize_t linelen;
while (wordnumber <= wordcount) {
if (wordtype[wordnumber] != VERB &&
@@ -357,19 +358,16 @@ cypher(void)
case SAVE:
printf("\nSave file name (default %s): ",
DEFAULT_SAVE_FILE);
- filename = fgetln(stdin, &filename_len);
- if (filename_len == 0
- || (filename_len == 1 && filename[0] == '\n'))
- rfilename = save_file_name(DEFAULT_SAVE_FILE,
- strlen(DEFAULT_SAVE_FILE));
+ linelen = getline(&line, &linesize, stdin);
+ if (linelen == -1 || *line == '\n')
+ filename = save_file_name(DEFAULT_SAVE_FILE);
else {
- if (filename[filename_len - 1] == '\n')
- filename_len--;
- rfilename = save_file_name(filename,
- filename_len);
+ if (line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
+ filename = save_file_name(line);
}
- save(rfilename);
- free(rfilename);
+ save(filename);
+ free(filename);
break;
case VERBOSE:
@@ -463,6 +461,9 @@ cypher(void)
}
if (!lflag)
newlocation();
+
+ free(line);
+
if (wordnumber < wordcount && !stop_cypher &&
(*words[wordnumber] == ',' || *words[wordnumber] == '.')) {
wordnumber++;
diff --git a/games/battlestar/extern.h b/games/battlestar/extern.h
index 21f8dc72260..598d5087c9d 100644
--- a/games/battlestar/extern.h
+++ b/games/battlestar/extern.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: extern.h,v 1.22 2022/02/05 23:00:20 gnezdo Exp $ */
+/* $OpenBSD: extern.h,v 1.23 2022/08/08 17:57:05 op Exp $ */
/* $NetBSD: extern.h,v 1.5 1995/04/24 12:22:18 cgd Exp $ */
/*
@@ -357,7 +357,7 @@ void ravage(void);
void restore(const char *);
int ride(void);
void save(const char *);
-char *save_file_name(const char *, size_t);
+char *save_file_name(const char *);
int shoot(void);
int take(unsigned int[]);
int takeoff(void);
diff --git a/games/battlestar/init.c b/games/battlestar/init.c
index 5c2044a918e..2e99402e81f 100644
--- a/games/battlestar/init.c
+++ b/games/battlestar/init.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: init.c,v 1.17 2019/05/09 20:19:23 tedu Exp $ */
+/* $OpenBSD: init.c,v 1.18 2022/08/08 17:57:05 op Exp $ */
/* $NetBSD: init.c,v 1.4 1995/03/21 15:07:35 cgd Exp $ */
/*
@@ -66,7 +66,7 @@ initialize(const char *filename)
for (p = dayobjs; p->room != 0; p++)
SetBit(location[p->room].objects, p->obj);
} else {
- savefile = save_file_name(filename, strlen(filename));
+ savefile = save_file_name(filename);
restore(savefile);
free(savefile);
}
diff --git a/games/battlestar/save.c b/games/battlestar/save.c
index 088bb2d2c5c..8484d1ec8b4 100644
--- a/games/battlestar/save.c
+++ b/games/battlestar/save.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: save.c,v 1.13 2015/12/31 17:51:19 mestre Exp $ */
+/* $OpenBSD: save.c,v 1.14 2022/08/08 17:57:05 op Exp $ */
/* $NetBSD: save.c,v 1.3 1995/03/21 15:07:57 cgd Exp $ */
/*
@@ -145,44 +145,26 @@ save(const char *filename)
}
/*
- * Given a save file name (possibly from fgetln, so without terminating NUL),
- * determine the name of the file to be saved to by adding the HOME
- * directory if the name does not contain a slash. Name will be allocated
- * with malloc(3).
+ * Given a save file name determine the name of the file to be saved
+ * to by adding the HOME directory if the name does not contain a
+ * slash. Name will be allocated with malloc(3).
*/
char *
-save_file_name(const char *filename, size_t len)
+save_file_name(const char *filename)
{
char *home;
char *newname;
- size_t tmpl;
- if (memchr(filename, '/', len)) {
- if ((newname = malloc(len + 1)) == NULL) {
+ home = getenv("HOME");
+ if (strchr(filename, '/') != NULL || home == NULL) {
+ if ((newname = strdup(filename)) == NULL)
warnx("out of memory");
- return NULL;
- }
- memcpy(newname, filename, len);
- newname[len] = 0;
- } else {
- if ((home = getenv("HOME")) != NULL) {
- tmpl = strlen(home);
- if ((newname = malloc(tmpl + len + 2)) == NULL) {
- warnx("out of memory");
- return NULL;
- }
- memcpy(newname, home, tmpl);
- newname[tmpl] = '/';
- memcpy(newname + tmpl + 1, filename, len);
- newname[tmpl + len + 1] = 0;
- } else {
- if ((newname = malloc(len + 1)) == NULL) {
- warnx("out of memory");
- return NULL;
- }
- memcpy(newname, filename, len);
- newname[len] = 0;
- }
+ return(newname);
+ }
+
+ if (asprintf(&newname, "%s/%s", home, filename) == -1) {
+ warnx("out of memory");
+ return(NULL);
}
return(newname);
}
diff --git a/games/hunt/huntd/conf.c b/games/hunt/huntd/conf.c
index 8c36bb1f110..4cf86f90db5 100644
--- a/games/hunt/huntd/conf.c
+++ b/games/hunt/huntd/conf.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: conf.c,v 1.13 2017/01/21 08:22:57 krw Exp $ */
+/* $OpenBSD: conf.c,v 1.14 2022/08/08 17:57:05 op Exp $ */
/* David Leonard <d@openbsd.org>, 1999. Public domain. */
#include <sys/select.h>
@@ -247,24 +247,18 @@ parse_line(char *buf, char *fnm, int *line)
static void
load_config(FILE *f, char *fnm)
{
- char buf[BUFSIZ];
- size_t len;
- int line;
- char *p;
-
- line = 0;
- while ((p = fgetln(f, &len)) != NULL) {
- line++;
- if (p[len-1] == '\n')
- len--;
- if (len >= sizeof(buf)) {
- logx(LOG_ERR, "%s:%d: line too long", fnm, line);
- continue;
- }
- (void)memcpy(buf, p, len);
- buf[len] = '\0';
- parse_line(buf, fnm, &line);
+ int lineno = 0;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
+
+ while ((linelen = getline(&line, &linesize, stdin)) != -1) {
+ lineno++;
+ if (line[linelen - 1] == '\n')
+ line[linelen - 1] = '\0';
+ parse_line(line, fnm, &lineno);
}
+ free(line);
}
/*
diff --git a/games/monop/execute.c b/games/monop/execute.c
index e8804e1a23d..29fd1a56079 100644
--- a/games/monop/execute.c
+++ b/games/monop/execute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: execute.c,v 1.15 2019/06/28 13:32:52 deraadt Exp $ */
+/* $OpenBSD: execute.c,v 1.16 2022/08/08 17:57:05 op Exp $ */
/* $NetBSD: execute.c,v 1.3 1995/03/23 08:34:38 cgd Exp $ */
/*
@@ -285,7 +285,8 @@ rest_f(char *file)
int i, j, num;
FILE *inf;
char *st, *a, *b;
- size_t len;
+ size_t linesize;
+ ssize_t len;
STAT sbuf;
int t1;
short t2, t3, t4;
@@ -302,19 +303,22 @@ rest_f(char *file)
}
num = 1;
- st = fgetln(inf, &len);
- if (st == NULL || len != strlen(MONOP_TAG) + 1 ||
+ st = NULL;
+ linesize = 0;
+ len = getline(&st, &linesize, inf);
+ if (len == -1 || len != strlen(MONOP_TAG) + 1 ||
strncmp(st, MONOP_TAG, strlen(MONOP_TAG))) {
badness:
warnx("%s line %d", file, num);
+ free(st);
fclose(inf);
return(FALSE);
}
num++;
- if (fgetln(inf, &len) == NULL)
+ if (getline(&st, &linesize, inf) == -1)
goto badness;
num++;
- if ((st = fgetln(inf, &len)) == NULL || st[len - 1] != '\n')
+ if ((len = getline(&st, &linesize, inf)) == -1 || st[len - 1] != '\n')
goto badness;
st[len - 1] = '\0';
if (sscanf(st, "%d %d %d", &num_play, &player, &num_doub) != 3 ||
@@ -328,7 +332,8 @@ badness:
/* Names */
for (i = 0; i < num_play; i++) {
num++;
- if ((st = fgetln(inf, &len)) == NULL || st[len - 1] != '\n')
+ if ((len = getline(&st, &linesize, inf)) == -1 ||
+ st[len - 1] != '\n')
goto badness;
st[len - 1] = '\0';
if ((name_list[i] = play[i].name = strdup(st)) == NULL)
@@ -340,7 +345,8 @@ badness:
/* Money, location, GOJF cards, turns in jail */
for (i = 0; i < num_play; i++) {
num++;
- if ((st = fgetln(inf, &len)) == NULL || st[len - 1] != '\n')
+ if ((len = getline(&st, &linesize, inf)) == -1 ||
+ st[len - 1] != '\n')
goto badness;
st[len - 1] = '\0';
if (sscanf(st, "%d %hd %hd %hd", &(play[i].money), &t2,
@@ -355,7 +361,8 @@ badness:
/* Deck status; init_decks() must have been called. */
for (i = 0; i < 2; i++) {
num++;
- if ((st = fgetln(inf, &len)) == NULL || st[len - 1] != '\n')
+ if ((len = getline(&st, &linesize, inf)) == -1 ||
+ st[len - 1] != '\n')
goto badness;
st[len - 1] = '\0';
if (sscanf(st, "%d %d %hd", &t1, &j, &t2) != 3 ||
@@ -365,7 +372,8 @@ badness:
deck[i].top_card = j;
deck[i].gojf_used = t2;
num++;
- if ((st = fgetln(inf, &len)) == NULL || st[len - 1] != '\n')
+ if ((len = getline(&st, &linesize, inf)) == -1 ||
+ st[len - 1] != '\n')
goto badness;
st[len - 1] = '\0';
a = st;
@@ -379,7 +387,7 @@ badness:
/* Ignore anything trailing */
}
trading = FALSE;
- while ((st = fgetln(inf, &len)) != NULL) {
+ while ((len = getline(&st, &linesize, inf)) != -1) {
num++;
if (st[len - 1] != '\n')
goto badness;
@@ -403,6 +411,7 @@ badness:
* within 1 in each monopoly
*/
}
+ free(st);
fclose(inf);
/* Check total hotel and house count */
t1 = j = 0;