slight improvement to index file parsing; ok mlarkin
authorderaadt <deraadt@openbsd.org>
Fri, 23 Aug 2024 14:50:16 +0000 (14:50 +0000)
committerderaadt <deraadt@openbsd.org>
Fri, 23 Aug 2024 14:50:16 +0000 (14:50 +0000)
games/atc/main.c

index d6c3c64..47c5103 100644 (file)
@@ -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++;
        }