On Wed, Nov 25, 2015 at 06:23:49PM +0000, Ricardo Mestre wrote:
> Another thing as well, maybe char *name for getlogin(2) could/should be
> declared as char name[LOGIN_NAME_MAX]?

No, that won't compile.  You'd need to use the reentrant version
getlogin_r(name, LOGIN_NAME_MAX), then.
I think using name = getlogin() is fine.

Here's a slightly tweaked version of Ricardo's diff:

o  use $HOME/.snake.scores to unify high score file names.
o  remove some commented-out code.
o  slightly safer handling of getenv()

ok?


Index: Makefile
===================================================================
RCS file: /cvs/src/games/snake/Makefile,v
retrieving revision 1.10
diff -u -p -r1.10 Makefile
--- Makefile    24 Nov 2015 03:10:10 -0000      1.10
+++ Makefile    25 Nov 2015 18:39:51 -0000
@@ -2,13 +2,9 @@
 #      @(#)Makefile    8.1 (Berkeley) 5/31/93
 
 PROG=  snake
-SRCS=  snake.c snscore.c
+SRCS=  snake.c
 MAN=   snake.6
 DPADD= ${LIBM} ${LIBCURSES}
 LDADD= -lm -lcurses
-
-beforeinstall:
-       ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 664 /dev/null \
-           ${DESTDIR}/var/games/snakerawscores
 
 .include <bsd.prog.mk>
Index: pathnames.h
===================================================================
RCS file: pathnames.h
diff -N pathnames.h
--- pathnames.h 3 Jun 2003 03:01:41 -0000       1.2
+++ /dev/null   1 Jan 1970 00:00:00 -0000
@@ -1,38 +0,0 @@
-/*     $OpenBSD: pathnames.h,v 1.2 2003/06/03 03:01:41 millert Exp $   */
-/*     $NetBSD: pathnames.h,v 1.3 1995/04/22 08:34:33 cgd Exp $        */
-
-/*
- * Copyright (c) 1989, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *     @(#)pathnames.h 8.1 (Berkeley) 5/31/93
- */
-
-#define        _PATH_RAWSCORES "/var/games/snakerawscores"
-#ifdef LOGGING
-#define        _PATH_LOGFILE   "/var/games/snake.log"
-#endif
Index: snake.6
===================================================================
RCS file: /cvs/src/games/snake/snake.6,v
retrieving revision 1.11
diff -u -p -r1.11 snake.6
--- snake.6     13 Nov 2009 21:50:12 -0000      1.11
+++ snake.6     25 Nov 2015 18:39:51 -0000
@@ -115,10 +115,10 @@ To see who wastes time playing snake, ru
 .Nm snake
 .Fl s .
 .Sh FILES
-.Bl -tag -width /var/games/snakerawscores -compact
-.It Pa /var/games/snakerawscores
+.Bl -tag -width $HOME/.snakerawscores -compact
+.It Pa $HOME/.snake.scores
 database of personal bests
-.\".It Pa /var/games/snake.log
+.\".It Pa $HOME/.snake.log
 .\"log of games played
 .El
 .Sh BUGS
Index: snake.c
===================================================================
RCS file: /cvs/src/games/snake/snake.c,v
retrieving revision 1.16
diff -u -p -r1.16 snake.c
--- snake.c     16 Nov 2014 04:49:49 -0000      1.16
+++ snake.c     25 Nov 2015 18:39:51 -0000
@@ -47,8 +47,8 @@
 #include <curses.h>
 #include <err.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <math.h>
-#include <pwd.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -57,8 +57,6 @@
 #include <time.h>
 #include <unistd.h>
 
-#include "pathnames.h"
-
 #ifdef DEBUG
 #define        cashvalue       (loot-penalty)/25
 #else
@@ -96,8 +94,10 @@ int  moves;
 int    fast = 1;
 
 int rawscores;
+char scorepath[PATH_MAX];
 #ifdef LOGGING
 FILE   *logfile;
+char logpath[PATH_MAX];
 #endif
 
 int    lcnt, ccnt;     /* user's idea of screen size */
@@ -132,20 +132,26 @@ int       wantstop;
 int
 main(int argc, char *argv[])
 {
-       int     ch, i;
-       struct sigaction sa;
-       gid_t   gid;
+       struct   sigaction sa;
+       char    *home;
+       int      ch, i;
+
+       if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
+               err(1, "pledge");
+
+       home = getenv("HOME");
+       if (home == NULL || *home == '\0')
+               err(1, "getenv");
+
+       snprintf(scorepath, sizeof(scorepath), "%s/%s", home, ".snake.scores");
+       rawscores = open(scorepath, O_RDWR | O_CREAT, 0644);
 
-       /* don't create the score file if it doesn't exist. */
-       rawscores = open(_PATH_RAWSCORES, O_RDWR, 0664);
 #ifdef LOGGING
-       logfile = fopen(_PATH_LOGFILE, "a");
+       snprintf(logpath, sizeof(logpath), "%s/%s", getenv("HOME"),
+                       ".snake.log");
+       logfile = fopen(logpath, "a");
 #endif
 
-       /* revoke privs */
-       gid = getgid();
-       setresgid(gid, gid, gid);
-
        while ((ch = getopt(argc, argv, "hl:stw:")) != -1)
                switch ((char)ch) {
                case 'w':       /* width */
@@ -495,7 +501,6 @@ post(int iscore, int flag)
 {
        short   score = iscore;
        short   oldbest = 0;
-       uid_t   uid = getuid();
 
        /* I want to printf() the scores for terms that clear on endwin(),
         * but this routine also gets called with flag == 0 to see if
@@ -504,22 +509,21 @@ post(int iscore, int flag)
         */
        if (rawscores == -1) {
                if (flag)
-                       warnx("Can't open score file %s", _PATH_RAWSCORES);
+                       warnx("Can't open score file %s", scorepath);
                return(1);
        }
        /* Figure out what happened in the past */
-       lseek(rawscores, uid * sizeof(short), SEEK_SET);
+       lseek(rawscores, 0, SEEK_SET);
        read(rawscores, &oldbest, sizeof(short));
        if (!flag)
                return (score > oldbest ? 1 : 0);
 
        /* Update this jokers best */
        if (score > oldbest) {
-               lseek(rawscores, uid * sizeof(short), SEEK_SET);
+               lseek(rawscores, 0, SEEK_SET);
                write(rawscores, &score, sizeof(short));
                printf("\nYou bettered your previous best of $%d\n", oldbest);
-       } else
-               printf("\nYour best to date is $%d\n", oldbest);
+       }
 
        fsync(rawscores);
        /* See if we have a new champ */
@@ -922,6 +926,28 @@ void
 length(int num)
 {
        printf("You made %d moves.\n", num);
+}
+
+void
+snscore(int fd, int topn)
+{
+        const char     *name;
+        short           score;
+
+        if (fd < 0) {
+                fd = open(scorepath, O_RDONLY, 0);
+                if (fd < 0)
+                        errx(1, "Couldn't open raw scorefile");
+        }
+
+        lseek(fd, 0, SEEK_SET);
+        if (read(fd, &score, sizeof(short)) == 0)
+                errx(1, "Raw scorefile is empty");
+
+        name = getlogin();
+
+        printf("%sSnake score to date:\n", topn > 0 ? "Top " : "");
+        printf("$%d\t%s\n", score, name);
 }
 
 #ifdef LOGGING
Index: snscore.c
===================================================================
RCS file: snscore.c
diff -N snscore.c
--- snscore.c   18 Nov 2014 20:51:00 -0000      1.11
+++ /dev/null   1 Jan 1970 00:00:00 -0000
@@ -1,115 +0,0 @@
-/*     $OpenBSD: snscore.c,v 1.11 2014/11/18 20:51:00 krw Exp $        */
-/*     $NetBSD: snscore.c,v 1.5 1995/04/24 12:25:43 cgd Exp $  */
-
-/*
- * Copyright (c) 1980, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-#include <err.h>
-#include <fcntl.h>
-#include <pwd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include "pathnames.h"
-
-#define MAXPLAYERS 256
-
-struct player  {
-       uid_t   uids;
-       short   scores;
-       char    *name;
-} players[MAXPLAYERS], temp;
-
-void
-snscore(int fd, int topn)
-{
-       uid_t   uid;
-       short   score;
-       int     noplayers;
-       int     i, j, notsorted;
-       char    *q;
-       struct  passwd  *p;
-
-       if (fd < 0) {
-               fd = open(_PATH_RAWSCORES, O_RDONLY, 0);
-               if (fd < 0)
-                       errx(1, "Couldn't open raw scorefile");
-       }
-
-       lseek(fd, 0, SEEK_SET);
-       printf("%sSnake scores to date:\n", topn > 0 ? "Top " : "");
-       /* read(fd, &whoallbest, sizeof(uid_t));
-        * read(fd, &allbest, sizeof(short));   SCOREFILE FORMAT CHANGE
-        */
-       noplayers = 0;
-       for (uid = 0; ; uid++) {
-               if (read(fd, &score, sizeof(short)) == 0)
-                       break;
-               if (score > 0) {
-                       if (noplayers >= MAXPLAYERS)
-                               errx(2, "Too many entries in scorefile!");
-                       players[noplayers].uids = uid;
-                       players[noplayers].scores = score;
-                       p = getpwuid(uid);
-                       if (p == NULL)
-                               continue;
-                       q = p -> pw_name;
-                       if ((players[noplayers].name = strdup(q)) == NULL)
-                               err(1, "strdup");
-
-                       noplayers++;
-               }
-       }
-
-       /* bubble sort scores */
-       for (notsorted = 1; notsorted; ) {
-               notsorted = 0;
-               for (i = 0; i < noplayers - 1; i++)
-                       if (players[i].scores < players[i + 1].scores) {
-                               temp = players[i];
-                               players[i] = players[i + 1];
-                               players[i + 1] = temp;
-                               notsorted++;
-                       }
-       }
-
-       if ((topn > 0) && (topn < noplayers))
-               noplayers = topn;
-       j = 1;
-       for (i = 0; i < noplayers; i++) {
-               printf("%d:\t$%d\t%s\n", j, players[i].scores, players[i].name);
-               if (i < noplayers - 1 &&
-                   players[i].scores > players[i + 1].scores)
-                       j = i + 2;
-       }
-       if (noplayers == 0)
-               printf("None.\n");
-}

Reply via email to