Unveils snake and pledges earlier during execution.

Index: snake.c
===================================================================
RCS file: /cvs/src/games/snake/snake.c,v
retrieving revision 1.33
diff -u -p -r1.33 snake.c
--- snake.c     20 Jan 2019 04:14:19 -0000      1.33
+++ snake.c     19 May 2019 19:51:33 -0000
@@ -135,17 +135,23 @@ int
 main(int argc, char *argv[])
 {
        struct  sigaction sa;
-       int     ch, i;
+       int     ch, i, ret;
+       const char *home;

-#ifdef LOGGING
-       const char      *home;
+       if (pledge("stdio tty rpath wpath cpath unveil", NULL) == -1)
+               err(1, "pledge");

        home = getenv("HOME");
        if (home == NULL || *home == '\0')
                err(1, "getenv");

+       ret = snprintf(scorepath, sizeof(scorepath), "%s/%s", home,
+           ".snake.scores");
+       if (ret < 0 || ret >= PATH_MAX)
+               errc(1, ENAMETOOLONG, "%s/%s", home, ".snake.scores");
+
+#ifdef LOGGING
        snprintf(logpath, sizeof(logpath), "%s/%s", home, ".snake.log");
-       logfile = fopen(logpath, "a");
 #endif

        while ((ch = getopt(argc, argv, "hl:stw:")) != -1)
@@ -173,11 +179,18 @@ main(int argc, char *argv[])
                        return 1;
                }

-       readscores(1);
        penalty = loot = 0;
        initscr();
+       if (unveil(scorepath, "rwc") == -1)
+               err(1, "unveil");
+#ifdef LOGGING
+       if (unveil(logpath, "rwc") == -1)
+               err(1, "unveil");
+       logfile = fopen(logpath, "a");
+#endif
+       readscores(1);

-       if (pledge("stdio tty", NULL) == -1)
+       if (pledge("stdio tty rpath wpath cpath", NULL) == -1)
                err(1, "pledge");

 #ifdef KEY_LEFT
@@ -947,11 +960,9 @@ logit(char *msg)
 int
 readscores(int create)
 {
-       const char      *home;
        const char      *name;
        const char      *modstr;
        int              modint;
-       int              ret;

        if (create == 0) {
                modint = O_RDONLY;
@@ -960,15 +971,6 @@ readscores(int create)
                modint = O_RDWR | O_CREAT;
                modstr = "r+";
        }
-
-       home = getenv("HOME");
-       if (home == NULL || *home == '\0')
-               err(1, "getenv");
-
-       ret = snprintf(scorepath, sizeof(scorepath), "%s/%s", home,
-           ".snake.scores");
-       if (ret < 0 || ret >= PATH_MAX)
-               errc(1, ENAMETOOLONG, "%s/%s", home, ".snake.scores");

        rawscores = open(scorepath, modint, 0666);
        if (rawscores < 0) {

Reply via email to