-/* $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 $ */
/*
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 &&
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:
}
if (!lflag)
newlocation();
+
+ free(line);
+
if (wordnumber < wordcount && !stop_cypher &&
(*words[wordnumber] == ',' || *words[wordnumber] == '.')) {
wordnumber++;
-/* $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 $ */
/*
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);
-/* $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 $ */
/*
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);
}
-/* $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 $ */
/*
}
/*
- * 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);
}
-/* $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>
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);
}
/*
-/* $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 $ */
/*
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;
}
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 ||
/* 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)
/* 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,
/* 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 ||
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;
/* 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;
* within 1 in each monopoly
*/
}
+ free(st);
fclose(inf);
/* Check total hotel and house count */
t1 = j = 0;