From 99d0e43d6d3df12d3091fae0123ca1c90f1b1a8e Mon Sep 17 00:00:00 2001 From: deraadt Date: Fri, 23 Aug 2024 14:50:16 +0000 Subject: [PATCH] slight improvement to index file parsing; ok mlarkin --- games/atc/main.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/games/atc/main.c b/games/atc/main.c index d6c3c6491ef..47c51031bc9 100644 --- a/games/atc/main.c +++ b/games/atc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.32 2024/08/23 04:25:46 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.33 2024/08/23 14:50:16 deraadt Exp $ */ /* $NetBSD: main.c,v 1.4 1995/04/27 21:22:25 mycroft Exp $ */ /*- @@ -218,7 +218,7 @@ default_game(void) { FILE *fp; static char file[256]; - char line[256], games[256]; + char line[256], games[256], *p; strlcpy(games, _PATH_GAMES, sizeof games); strlcat(games, GAMES, sizeof games); @@ -233,10 +233,13 @@ default_game(void) fclose(fp); return (NULL); } - } while (line[0] == '#'); + line[strcspn(line, "\n")] = '\0'; + p = strrchr(line, '#'); + if (p) + *p = '\0'; + } while (line[0] == '\0'); fclose(fp); - line[strcspn(line, "\n")] = '\0'; if (strlen(line) + strlen(_PATH_GAMES) >= sizeof(file)) { warnx("default game name too long"); return (NULL); @@ -252,7 +255,7 @@ okay_game(const char *s) FILE *fp; static char file[256]; const char *ret = NULL; - char line[256], games[256]; + char line[256], games[256], *p; strlcpy(games, _PATH_GAMES, sizeof games); strlcat(games, GAMES, sizeof games); @@ -263,8 +266,9 @@ okay_game(const char *s) } while (fgets(line, sizeof(line), fp) != NULL) { line[strcspn(line, "\n")] = '\0'; - if (line[0] == '#') - continue; + p = strrchr(line, '#'); + if (p) + *p = '\0'; if (strcmp(s, line) == 0) { if (strlen(line) + strlen(_PATH_GAMES) >= sizeof(file)) { warnx("game name too long"); @@ -291,7 +295,7 @@ int list_games(void) { FILE *fp; - char line[256], games[256]; + char line[256], games[256], *p; int num_games = 0; strlcpy(games, _PATH_GAMES, sizeof games); @@ -304,6 +308,11 @@ list_games(void) puts("available games:"); while (fgets(line, sizeof(line), fp) != NULL) { line[strcspn(line, "\n")] = '\0'; + p = strrchr(line, '#'); + if (p) + *p = '\0'; + if (line[0] == '\0') + continue; printf(" %s\n", line); num_games++; } -- 2.20.1