changeset 252:3d4252fa2ed3

Use more portable random seed generation. The new function md_random_seed() has replaced time() + getpid() and similar methods. Putting everything in mdport.c slightly reduces the warnings and workarounds.
author John "Elwin" Edwards
date Sat, 28 Jan 2017 15:49:41 -0500
parents e7862a021609
children d9badb9c0179 e940e6c00751
files arogue5/main.c arogue5/mdport.c arogue5/rogue.h arogue5/save.c arogue7/main.c arogue7/mdport.c arogue7/rogue.h arogue7/save.c rogue3/main.c rogue3/mdport.c rogue3/mdport.h rogue3/save.c rogue4/extern.h rogue4/main.c rogue4/mdport.c rogue4/new_level.c rogue4/save.c rogue5/extern.h rogue5/main.c rogue5/mdport.c rogue5/save.c rogue5/scedit.c srogue/main.c srogue/mdport.c srogue/rogue.h srogue/save.c xrogue/main.c xrogue/rogue.h xrogue/state.c
diffstat 29 files changed, 123 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/arogue5/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue5/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -49,8 +49,6 @@
 main(int argc, char *argv[], char *envp[])
 {
     register char *env;
-    int lowtime;
-    time_t now;
     char *roguedir;
 
     roguedir = md_getroguedir();
@@ -100,7 +98,7 @@
         strucpy(whoami, md_getusername(), strlen(md_getusername()));
 
     if (env == NULL || fruit[0] == '\0') {
-	md_srand((long)(getpid()+time(0)));
+	md_srand(md_random_seed());
 	strcpy(fruit, funfruit[rnd(NFRUIT)]);
     }
 
@@ -177,10 +175,9 @@
     else if (argc == 2)
 	if (!restore(argv[1], envp)) /* Note: restore will never return */
 	    exit(1);
-    lowtime = (int) time(&now);
     dnum = (wizard && getenv("SEED") != NULL ?
 	atoi(getenv("SEED")) :
-	lowtime + getpid());
+	md_random_seed());
     if (wizard)
 	printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
     else
--- a/arogue5/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue5/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -78,6 +78,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 
 #define MOD_MOVE(c) (toupper(c) )
 
@@ -1232,3 +1233,16 @@
 
     return(ch & 0x7F);
 }
+
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
--- a/arogue5/rogue.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue5/rogue.h	Sat Jan 28 15:49:41 2017 -0500
@@ -1198,6 +1198,7 @@
 extern int md_rand(void);
 extern int md_erasechar(void);
 extern int md_killchar(void);
+extern unsigned int md_random_seed(void);
 
 /*
  * Now all the global variables
--- a/arogue5/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue5/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -255,7 +255,7 @@
     setup();
     clearok(curscr, TRUE);
     touchwin(cw);
-    srand(getpid());
+    srand(md_random_seed());
     playit();
     /*NOTREACHED*/ 
     return(FALSE);
--- a/arogue7/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue7/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -47,8 +47,6 @@
 main(int argc, char *argv[], char *envp[])
 {
     register char *env;
-    int lowtime;
-    time_t now;
 #ifdef PC7300
     int hardwindow;	/* Do we have a hardware window? */
 #endif
@@ -179,10 +177,9 @@
     if (argc == 2)
 	if (!restore(argv[1], envp)) /* Note: restore will never return */
 	    exit(1);
-    lowtime = (int) time(&now);
     dnum = (wizard && getenv("SEED") != NULL ?
 	atoi(getenv("SEED")) :
-	lowtime + getpid());
+	md_random_seed());
     if (wizard)
 	printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
     else
--- a/arogue7/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue7/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -71,6 +71,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 
 #define MOD_MOVE(c) (toupper(c) )
 
@@ -1214,3 +1215,16 @@
 
     return(ch & 0x7F);
 }
+
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
--- a/arogue7/rogue.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue7/rogue.h	Sat Jan 28 15:49:41 2017 -0500
@@ -1418,6 +1418,7 @@
 long    md_memused(void);
 int     md_normaluser(void);
 int     md_rand(void);
+unsigned int md_random_seed(void);
 void    md_reopen_score(void);
 int     md_readchar(WINDOW *win);
 int     md_shellescape(void);
--- a/arogue7/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/arogue7/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -254,7 +254,7 @@
     setup();
     clearok(curscr, TRUE);
     touchwin(cw);
-    srand(getpid());
+    srand(md_random_seed());
     playit();
     /*NOTREACHED*/
     return(0);
--- a/rogue3/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue3/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -31,8 +31,6 @@
     char *env;
     struct linked_list *item;
     struct object *obj;
-    int lowtime;
-    time_t now;
 
     md_init(MD_STRIP_CTRL_KEYPAD);
 
@@ -130,9 +128,6 @@
     if (!use_savedir)
       md_normaluser();
     
-    time(&now);
-    lowtime = (int) now;
-
     env = getenv("SEED");
 
     if (env)
@@ -146,7 +141,7 @@
         dnum = seed;
     }
     else
-        dnum = lowtime + md_getpid();
+        dnum = md_random_seed();
 
     if (wizard || env)
 	printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
--- a/rogue3/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue3/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -102,6 +102,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 
 #define NOOP(x) (x += 0)
 
@@ -415,16 +416,6 @@
 #endif
 }
 
-pid_t
-md_getpid(void)
-{
-#ifdef _WIN32
-    return( _getpid() );
-#else
-    return( getpid() );
-#endif
-}
-
 char *
 md_getusername(void)
 {
@@ -1612,3 +1603,16 @@
 
     return ret;
 }
+
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
--- a/rogue3/mdport.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue3/mdport.h	Sat Jan 28 15:49:41 2017 -0500
@@ -65,9 +65,6 @@
 #ifndef uid_t
 typedef unsigned int uid_t;
 #endif
-#ifndef pid_t
-typedef unsigned int pid_t;
-#endif
 
 #elif defined(__CYGWIN__)
 #define HAVE_SYS_TYPES_H 1
@@ -163,7 +160,6 @@
 char *	md_getusername(void);
 uid_t	md_getuid(void);
 char *	md_getpass(char *prompt);
-pid_t	md_getpid(void);
 char *	md_getrealname(uid_t uid);
 void	md_init(int options);
 int	md_killchar(void);
@@ -190,6 +186,7 @@
 void    md_start_checkout_timer(int time);
 void    md_stop_checkout_timer(void);
 long    md_memused(void);
-int     md_ucount(void);
-int     md_unlockfile(FILE *fp);
-int     md_lockfile(FILE *fp);
+int     md_ucount(void);
+int     md_unlockfile(FILE *fp);
+int     md_lockfile(FILE *fp);
+unsigned int md_random_seed(void);
--- a/rogue3/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue3/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -245,7 +245,7 @@
     setup();
     clearok(curscr, TRUE);
     touchwin(cw);
-    srand(md_getpid());
+    srand(md_random_seed());
     status();
     playit();
     /*NOTREACHED*/
--- a/rogue4/extern.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue4/extern.h	Sat Jan 28 15:49:41 2017 -0500
@@ -93,3 +93,4 @@
 extern void  md_sleep(int s);
 extern int   md_unlink(char *file);
 extern int   md_unlink_open_file(char *file, int inf);
+extern unsigned int md_random_seed(void);
--- a/rogue4/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue4/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -29,7 +29,6 @@
 main(int argc, char *argv[], char *envp[])
 {
     register char *env;
-    int lowtime;
 
     md_init();
 
@@ -138,8 +137,6 @@
     if (!use_savedir)
         md_normaluser();
 
-    lowtime = (int) time(NULL);
-
 #ifdef WIZARD
     noscore = wizard;
 #endif
@@ -149,7 +146,7 @@
 	noscore = TRUE;
     }
     else
-	dnum = lowtime + getpid();
+	dnum = md_random_seed();
 #ifdef WIZARD
     if (wizard)
 	printf("Hello %s, welcome to dungeon #%d", whoami, dnum);
--- a/rogue4/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue4/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -82,6 +82,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 
 #define MOD_MOVE(c) (toupper(c) )
 
@@ -1313,3 +1314,16 @@
 
     return(ch & 0x7F);
 }
+
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
--- a/rogue4/new_level.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue4/new_level.c	Sat Jan 28 15:49:41 2017 -0500
@@ -73,7 +73,7 @@
 	if (i++ > 100)
 	{
 	    i = 0;
-	    srand(getpid() + (int) time((time_t *) NULL));
+	    srand(md_random_seed());
 	}
     } until (_level[index] == FLOOR);
     _level[index] = STAIRS;
--- a/rogue4/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue4/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -317,7 +317,7 @@
     setup();
     clearok(curscr, TRUE);
     touchwin(stdscr);
-    srand(getpid());
+    srand(md_random_seed());
     msg("file name: %s", file);
 	status();
     playit();
--- a/rogue5/extern.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue5/extern.h	Sat Jan 28 15:49:41 2017 -0500
@@ -44,9 +44,6 @@
 #ifndef uid_t
 typedef unsigned int uid_t;
 #endif
-#ifndef pid_t
-typedef unsigned int pid_t;
-#endif
 #elif defined(__CYGWIN__)
 #define HAVE_SYS_TYPES_H 1
 #define HAVE_PWD_H 1
@@ -145,7 +142,6 @@
 char	*md_getusername(void);
 uid_t	md_getuid(void);
 char	*md_getpass(char *prompt);
-pid_t	md_getpid(void);
 char	*md_getrealname(uid_t uid);
 void	md_init(void);
 int	md_killchar(void);
@@ -168,5 +164,6 @@
 void md_onsignal_exit(void);
 void md_onsignal_default(void);
 int md_issymlink(char *sp);
+unsigned int md_random_seed(void);
 extern char *xcrypt(const char *key, const char *setting);
 
--- a/rogue5/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue5/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -23,7 +23,6 @@
 main(int argc, char **argv)
 {
     char *env;
-    time_t lowtime;
 
     md_init();
 
@@ -77,14 +76,13 @@
 	parse_opts(env);
     if (!use_savedir && (env == NULL || whoami[0] == '\0'))
         strucpy(whoami, md_getusername(), strlen(md_getusername()));
-    lowtime = time(NULL);
     if (getenv("SEED") != NULL)
     {
 	dnum = atoi(getenv("SEED"));
 	noscore = 1;
     }
     else
-	dnum = (unsigned int) lowtime + md_getpid();
+	dnum = md_random_seed();
     seed = dnum;
 
     /*
--- a/rogue5/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue5/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -98,6 +98,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 #include "extern.h"
 
 #if !defined(PATH_MAX) && defined(_MAX_PATH)
@@ -411,16 +412,6 @@
 #endif
 }
 
-pid_t
-md_getpid(void)
-{
-#ifdef _WIN32
-    return( _getpid() );
-#else
-    return( getpid() );
-#endif
-}
-
 char *
 md_getusername(void)
 {
@@ -1493,6 +1484,19 @@
 #endif
 }
 
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
+
 #if defined(CHECKTIME)
 void
 md_start_checkout_timer(int time)
--- a/rogue5/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue5/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -264,7 +264,7 @@
 
     strcpy(file_name, file);
     clearok(curscr, TRUE);
-    srand(md_getpid());
+    srand(md_random_seed());
     msg("file name: %s", file);
     playit();
     /*NOTREACHED*/
--- a/rogue5/scedit.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/rogue5/scedit.c	Sat Jan 28 15:49:41 2017 -0500
@@ -52,7 +52,7 @@
 		scorefile = "rogue54.scr";
 	else
 		scorefile = av[1];
-	seed = md_getpid();
+	seed = md_random_seed();
 
 	if ((inf = fopen(scorefile, "r+")) < 0) {
 		perror(scorefile);
--- a/srogue/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/srogue/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -48,8 +48,6 @@
 	register struct object *obj;
 	char alldone, wpt;
 	char *getpass(), *xcrypt(), *strrchr();
-	int lowtime;
-	time_t now;
 	char *homedir = roguehome();
 
 	md_init();
@@ -102,8 +100,6 @@
 		}
 	}
 #endif
-	time(&now);
-	lowtime = (int) now;
 
 #ifdef SAVEDIR
 	if (argc >= 3 && !strcmp(argv[1], "-n")) {
@@ -169,7 +165,7 @@
 	/* START NEW GAME */
 
 	dnum = (wizard && getenv("SEED") != NULL ?
-		atoi(getenv("SEED")) : lowtime + md_getpid());
+		atoi(getenv("SEED")) : md_random_seed());
 
 	if(wizard)
 		printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
--- a/srogue/mdport.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/srogue/mdport.c	Sat Jan 28 15:49:41 2017 -0500
@@ -96,6 +96,7 @@
 #include <limits.h>
 #include <sys/stat.h>
 #include <signal.h>
+#include <time.h>
 
 #if !defined(PATH_MAX) && defined(_MAX_PATH)
 #define PATH_MAX _MAX_PATH
@@ -431,16 +432,6 @@
 #endif
 }
 
-pid_t
-md_getpid(void)
-{
-#ifdef _WIN32
-    return( _getpid() );
-#else
-    return( getpid() );
-#endif
-}
-
 char *
 md_getusername(void)
 {
@@ -1564,3 +1555,16 @@
 	srand(seed);
 #endif
 }
+
+unsigned int
+md_random_seed(void)
+{
+    unsigned int seed;
+    seed = (unsigned int) time((time_t *) NULL);
+#ifdef _WIN32
+    seed += _getpid();
+#else
+    seed += getpid();
+#endif
+    return seed;
+}
--- a/srogue/rogue.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/srogue/rogue.h	Sat Jan 28 15:49:41 2017 -0500
@@ -45,9 +45,6 @@
 #ifndef gid_t
 typedef unsigned int gid_t;
 #endif
-#ifndef pid_t
-typedef int pid_t;
-#endif
 
 int	md_chmod(const char *filename, int mode);
 char	*md_crypt(const char *key, const char *salt);
@@ -59,7 +56,6 @@
 uid_t	md_getuid(void);
 gid_t	md_getgid(void);
 char	*md_getpass(char *prompt);
-pid_t	md_getpid(void);
 char	*md_getrealname(uid_t uid);
 void	md_init(void);
 int	md_killchar(void);
@@ -85,6 +81,7 @@
 long md_memused(void);
 int md_random(void);
 void md_srandom(unsigned int seed);
+unsigned int md_random_seed(void);
 char *xcrypt(const char *key, const char *setting);
 
 #define reg     register        /* register abbr.       */
--- a/srogue/save.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/srogue/save.c	Sat Jan 28 15:49:41 2017 -0500
@@ -302,6 +302,6 @@
 	strcpy(file_name, file);
 	setup();
 	restscr(cw);
-	md_srandom(md_getpid());
+	md_srandom(md_random_seed());
 	playit();
 }
--- a/xrogue/main.c	Sat Jan 28 11:45:36 2017 -0500
+++ b/xrogue/main.c	Sat Jan 28 15:49:41 2017 -0500
@@ -166,7 +166,7 @@
         seed = atoi(getenv("SEED")); 
     }
     else {
-        seed = (int) time(&now) + getpid();
+        seed = md_random_seed();
     }
     if (wizard)
         printf("Hello %s, welcome to dungeon #%d\n", whoami, seed);
--- a/xrogue/rogue.h	Sat Jan 28 11:45:36 2017 -0500
+++ b/xrogue/rogue.h	Sat Jan 28 15:49:41 2017 -0500
@@ -1505,6 +1505,7 @@
 long    md_memused(void);
 int     md_normaluser(void);
 int     md_rand(int range);
+unsigned int md_random_seed(void);