Re: hack game: fix launch without /usr/games in path

2023-05-31 Thread Anton Konyahin

On 31/05, Omar Polo wrote:


Agreed.  I prefer the second patch too, which I'm reattaching since it
was mangled (whitespaces; 'patch -l' is not enough, but 'got patch'
managed to apply it.)


My bad, I am still not very comfortable with mailing patches, but I will learn.


Will wait a bit still in case someone disagrees, but I don't really
see the point in having hack scraping $PATH for finding itself; the
format wasn't changed since the initial import so I guess we'll be
fine :-)


Thank you!



Re: hack game: fix launch without /usr/games in path

2023-05-28 Thread Anton Konyahin

I can suggest another (much less) patch, which still allows users to
play hack without path modification. But all this stuff with checking
saves creating time doesn't looks actual for me, so I keep original
patch below.

Index: hack.unix.c
===
RCS file: /cvs/src/games/hack/hack.unix.c,v
retrieving revision 1.21
diff -u -p -u -p -r1.21 hack.unix.c
--- hack.unix.c 15 Dec 2021 16:29:29 -  1.21
+++ hack.unix.c 29 May 2023 05:44:35 -
@@ -182,8 +182,9 @@ gethdate(char *name)
return;
}
}
-   error("Cannot get status of %s.",
-   (p = strrchr(name, '/')) ? p+1 : name);
+
+   /* can't find hack in path, use old good time as default */
+   hbuf.st_mtime = 0;
    free(path);
 }


On 21/05, Anton Konyahin wrote:

Hello.

After removing /usr/games from PATH, hack failing with error:


Cannot get status of hack.


Hack tries to get information about st_mtime of execuatble, so it
search for hack in all directories from path. Game wants to compare
modification time of hack and saves, so saves from previous version
won't be load.

I think hack is mature enough and has stable format of saves, we can
remove this check and bring back ability to run it without path's
modification.



Old patch:

Index: hack.bones.c
===
RCS file: /cvs/src/games/hack/hack.bones.c,v
retrieving revision 1.11
diff -u -p -u -p -r1.11 hack.bones.c
--- hack.bones.c28 Jun 2019 13:32:52 -  1.11
+++ hack.bones.c21 May 2023 12:18:07 -
@@ -139,17 +139,15 @@ savebones(void)
 int
 getbones(void)
 {
-   int fd,x,y,ok;
+   int fd,x,y;
if(rn2(3)) return(0);   /* only once in three times do we find bones */
bones[6] = '0' + dlevel/10;
bones[7] = '0' + dlevel%10;
if((fd = open(bones, O_RDONLY)) == -1) return(0);
-   if((ok = uptodate(fd)) != 0){
-   getlev(fd, 0, dlevel);
-   for(x = 0; x < COLNO; x++) for(y = 0; y < ROWNO; y++)
-   levl[x][y].seen = levl[x][y].new = 0;
-   }
+   getlev(fd, 0, dlevel);
+   for(x = 0; x < COLNO; x++) for(y = 0; y < ROWNO; y++)
+   levl[x][y].seen = levl[x][y].new = 0;
(void) close(fd);
 #ifdef WIZARD
if(!wizard) /* duvel!frans: don't remove bones while debugging */
@@ -158,5 +156,5 @@ getbones(void)
pline("Cannot unlink %s .", bones);
return(0);
}
-   return(ok);
+   return(1);
 }
Index: hack.h
===
RCS file: /cvs/src/games/hack/hack.h,v
retrieving revision 1.13
diff -u -p -u -p -r1.13 hack.h
--- hack.h  9 Jan 2016 18:33:15 -   1.13
+++ hack.h  21 May 2023 12:18:07 -
@@ -681,7 +681,6 @@ int  phase_of_the_moon(void);
 int  night(void);
 int  midnight(void);
 void gethdate(char *);
-int  uptodate(int);
 void getlock(void);
 #ifdef MAIL
 void getmailstatus(void);
Index: hack.main.c
===
RCS file: /cvs/src/games/hack/hack.main.c,v
retrieving revision 1.24
diff -u -p -u -p -r1.24 hack.main.c
--- hack.main.c 28 Jun 2019 13:32:52 -  1.24
+++ hack.main.c 21 May 2023 12:18:08 -
@@ -103,7 +103,6 @@ static void chdirx(char *, boolean);
 int
 main(int argc, char **argv)
 {
-   extern char *__progname;
int fd;
 #ifdef CHDIR
char *dir;
@@ -183,15 +182,6 @@ main(int argc, char **argv)
u.ux = FAR; /* prevent nscr() */
(void) signal(SIGHUP, hackhangup);
-   /*
-* Find the creation date of this game,
-* so as to avoid restoring outdated savefiles.
-*/
-   gethdate(__progname);
-
-   /*
-* We cannot do chdir earlier, otherwise gethdate will fail.
-*/
 #ifdef CHDIR
chdirx(dir,1);
 #endif
@@ -298,8 +288,7 @@ main(int argc, char **argv)
setftty();
(void) snprintf(SAVEF, sizeof SAVEF, "save/%u%s", getuid(), plname);
regularize(SAVEF+5);/* avoid . or / in name */
-   if((fd = open(SAVEF, O_RDONLY)) >= 0 &&
-  (uptodate(fd) || unlink(SAVEF) == 666)) {
+   if((fd = open(SAVEF, O_RDONLY)) >= 0) {
(void) signal(SIGINT,done1);
pline("Restoring old save file...");
(void) fflush(stdout);
Index: hack.unix.c
===
RCS file: /cvs/src/games/hack/hack.unix.c,v
retrieving revision 1.21
diff -u -p -u -p -r1.21 hack.unix.c
--- hack.unix.c 15 Dec 2021 16:29:29 -  1.21
+++ hack.unix.c 21 May 2023 12:18:08 -
@@ -154,52 +154,7 @@ midnight(void)
return(getlt()->tm_hour == 0);
 }
-struct stat buf, hb

hack game: fix launch without /usr/games in path

2023-05-21 Thread Anton Konyahin

Hello.

After removing /usr/games from PATH, hack failing with error:


Cannot get status of hack.


Hack tries to get information about st_mtime of execuatble, so it
search for hack in all directories from path. Game wants to compare
modification time of hack and saves, so saves from previous version
won't be load.

I think hack is mature enough and has stable format of saves, we can
remove this check and bring back ability to run it without path's
modification.


Index: hack.bones.c
===
RCS file: /cvs/src/games/hack/hack.bones.c,v
retrieving revision 1.11
diff -u -p -u -p -r1.11 hack.bones.c
--- hack.bones.c28 Jun 2019 13:32:52 -  1.11
+++ hack.bones.c21 May 2023 12:18:07 -
@@ -139,17 +139,15 @@ savebones(void)
 int
 getbones(void)
 {
-   int fd,x,y,ok;
+   int fd,x,y;
 
 	if(rn2(3)) return(0);	/* only once in three times do we find bones */

bones[6] = '0' + dlevel/10;
bones[7] = '0' + dlevel%10;
if((fd = open(bones, O_RDONLY)) == -1) return(0);
-   if((ok = uptodate(fd)) != 0){
-   getlev(fd, 0, dlevel);
-   for(x = 0; x < COLNO; x++) for(y = 0; y < ROWNO; y++)
-   levl[x][y].seen = levl[x][y].new = 0;
-   }
+   getlev(fd, 0, dlevel);
+   for(x = 0; x < COLNO; x++) for(y = 0; y < ROWNO; y++)
+   levl[x][y].seen = levl[x][y].new = 0;
(void) close(fd);
 #ifdef WIZARD
if(!wizard) /* duvel!frans: don't remove bones while debugging */
@@ -158,5 +156,5 @@ getbones(void)
pline("Cannot unlink %s .", bones);
return(0);
}
-   return(ok);
+   return(1);
 }
Index: hack.h
===
RCS file: /cvs/src/games/hack/hack.h,v
retrieving revision 1.13
diff -u -p -u -p -r1.13 hack.h
--- hack.h  9 Jan 2016 18:33:15 -   1.13
+++ hack.h  21 May 2023 12:18:07 -
@@ -681,7 +681,6 @@ int  phase_of_the_moon(void);
 int  night(void);
 int  midnight(void);
 void gethdate(char *);
-int  uptodate(int);
 void getlock(void);
 #ifdef MAIL
 void getmailstatus(void);
Index: hack.main.c
===
RCS file: /cvs/src/games/hack/hack.main.c,v
retrieving revision 1.24
diff -u -p -u -p -r1.24 hack.main.c
--- hack.main.c 28 Jun 2019 13:32:52 -  1.24
+++ hack.main.c 21 May 2023 12:18:08 -
@@ -103,7 +103,6 @@ static void chdirx(char *, boolean);
 int
 main(int argc, char **argv)
 {
-   extern char *__progname;
int fd;
 #ifdef CHDIR
char *dir;
@@ -183,15 +182,6 @@ main(int argc, char **argv)
u.ux = FAR; /* prevent nscr() */
(void) signal(SIGHUP, hackhangup);
 
-	/*

-* Find the creation date of this game,
-* so as to avoid restoring outdated savefiles.
-*/
-   gethdate(__progname);
-
-   /*
-* We cannot do chdir earlier, otherwise gethdate will fail.
-*/
 #ifdef CHDIR
chdirx(dir,1);
 #endif
@@ -298,8 +288,7 @@ main(int argc, char **argv)
setftty();
(void) snprintf(SAVEF, sizeof SAVEF, "save/%u%s", getuid(), plname);
regularize(SAVEF+5);/* avoid . or / in name */
-   if((fd = open(SAVEF, O_RDONLY)) >= 0 &&
-  (uptodate(fd) || unlink(SAVEF) == 666)) {
+   if((fd = open(SAVEF, O_RDONLY)) >= 0) {
(void) signal(SIGINT,done1);
pline("Restoring old save file...");
(void) fflush(stdout);
Index: hack.unix.c
===
RCS file: /cvs/src/games/hack/hack.unix.c,v
retrieving revision 1.21
diff -u -p -u -p -r1.21 hack.unix.c
--- hack.unix.c 15 Dec 2021 16:29:29 -  1.21
+++ hack.unix.c 21 May 2023 12:18:08 -
@@ -154,52 +154,7 @@ midnight(void)
return(getlt()->tm_hour == 0);
 }
 
-struct stat buf, hbuf;

-
-void
-gethdate(char *name)
-{
-   char *p, *np, *path;
-   char filename[PATH_MAX];
-
-   if (strchr(name, '/') != NULL || (p = getenv("PATH")) == NULL)
-   p = "";
-   np = path = strdup(p);  /* Make a copy for strsep. */
-   if (path == NULL)
-   err(1, NULL);
-
-   for (;;) {
-   if ((p = strsep(&np, ":")) == NULL)
-   break;
-   if (*p == '\0') /* :: */
-   (void) strlcpy(filename, name, sizeof filename);
-   else
-   (void) snprintf(filename, sizeof filename,
-   "%s/%s", p, name);
-
-   if (stat(filename, &hbuf) == 0) {
-   free(path);
-   return;
-   }
-   }
-   error("Cannot get status of %s.",
-   (p = strrchr(name, '/')) ? p+1 : name);
-   free(path);
-}
-
-int
-uptodate(int fd)
-{
-   if(fstat(fd, &buf)) {