From 8501c01b33abba8d470d59e05105e7215f0a95a4 Mon Sep 17 00:00:00 2001 From: christos Date: Wed, 11 Nov 2020 19:55:33 +0000 Subject: use strchr, strrchr, random, more lint removal, savefile in /var/games/warp not /usr/share/games/warp... --- warp/config.h | 6 ------ warp/config.h.SH | 12 ++++++------ warp/config.sh | 2 +- warp/init.c | 6 +----- warp/intrp.c | 18 +++++++++--------- warp/score.c | 16 +++++++++------- warp/score.h | 2 +- warp/sig.c | 6 ++---- warp/term.c | 4 ++-- warp/util.h | 36 +++--------------------------------- warp/warp.c | 21 ++------------------- 11 files changed, 36 insertions(+), 93 deletions(-) diff --git a/warp/config.h b/warp/config.h index c4b30b45..8f4d28a4 100644 --- a/warp/config.h +++ b/warp/config.h @@ -85,12 +85,6 @@ */ #define PREFSHELL "/bin/csh" /**/ -/* RANDBITS: - * This symbol contains the number of bits of random number the rand() - * function produces. Usual values are 15, 16, and 31. - */ -#define RANDBITS 15 /**/ - /* ROOTID: * This symbol contains the uid of root, normally 0. */ diff --git a/warp/config.h.SH b/warp/config.h.SH index cb47e620..ba3af514 100644 --- a/warp/config.h.SH +++ b/warp/config.h.SH @@ -87,16 +87,16 @@ cat <config.h */ #$d_havetlib HAVETERMLIB /**/ -/* index: - * This preprocessor symbol is defined, along with rindex, if the system +/* strchr: + * This preprocessor symbol is defined, along with strrchr, if the system * uses the strchr and strrchr routines instead. */ -/* rindex: - * This preprocessor symbol is defined, along with index, if the system +/* strrchr: + * This preprocessor symbol is defined, along with strchr, if the system * uses the strchr and strrchr routines instead. */ -#$d_index index strchr /* cultural */ -#$d_index rindex strrchr /* differences? */ +#$d_strchr strchr strchr /* cultural */ +#$d_strchr strrchr strrchr /* differences? */ /* IOCTL: * This symbol, if defined, indicates that sys/ioctl.h exists and should diff --git a/warp/config.sh b/warp/config.sh index 68c88418..7514db40 100644 --- a/warp/config.sh +++ b/warp/config.sh @@ -60,7 +60,7 @@ d_phostname='/*undef' d_getpwent='/*undef' d_havetlib='define' termlib='-ltermlib' -d_index='/*undef' +d_strchr='/*undef' d_ioctl='define' d_normsig='define' jobslib='' diff --git a/warp/init.c b/warp/init.c index fc9bea13..a11a499c 100644 --- a/warp/init.c +++ b/warp/init.c @@ -246,11 +246,9 @@ stars_again: x = rand_mod(XSIZE); /* pick from 0..39, uniform */ break; case 1: case 2: case 3: -#ifndef lint x = (int)((((double)(myrand()-HALFRAND)) * ((double)(myrand()-HALFRAND))/RANDRAND) * 20.0) + xoff; /* pick from -20..20, clumped */ -#endif break; case 4: if (fscanf(mapfp,"%d %d\n",&ypred,&xpred) == EOF) @@ -271,11 +269,9 @@ stars_again: y = rand_mod(YSIZE); break; case 1: -#ifndef lint y = (int)((((double)(myrand()-HALFRAND)) * ((double)(myrand()-HALFRAND))/RANDRAND) * 12.0) + yoff; /* pick from -12..12, clumped */ -#endif break; case 2: #ifndef lint @@ -492,7 +488,7 @@ stars_again: dist = rand_mod(20); ch = let[dist]; - } /* grr, venix doesn't like indexing into string */ + } /* grr, venix doesn't like strchring into string */ obj = make_object(Enemy,ch,y,x,0,0, e + rand_mod(super*200+2),e/4,&root); if (numpirates-- > 0) { diff --git a/warp/intrp.c b/warp/intrp.c index 6b6b8be4..2b6df825 100644 --- a/warp/intrp.c +++ b/warp/intrp.c @@ -67,7 +67,7 @@ intrp_init(char *tcbuf) gethostname(buf,sizeof buf); hostname = savestr(buf); - if (index(hostname,'.')) + if (strchr(hostname,'.')) hostname = savestr(hostname); else { char hname[128]; @@ -191,7 +191,7 @@ static char * skipinterp(const char *pattern, const char *stoppers) { - while (*pattern && (!stoppers || !index(stoppers,*pattern))) { + while (*pattern && (!stoppers || !strchr(stoppers,*pattern))) { #ifdef DEBUGGING if (debug & 8) printf("skipinterp till %s at %s\r\n",stoppers?stoppers:"",pattern); @@ -276,7 +276,7 @@ dointerp(char *dest, size_t destsize, const char *pattern, const char *stoppers) bool lastcomp = false; int metabit = 0; - while (*pattern && (!stoppers || !index(stoppers,*pattern))) { + while (*pattern && (!stoppers || !strchr(stoppers,*pattern))) { #ifdef DEBUGGING if (debug & 8) printf("dointerp till %s at %s\r\n",stoppers?stoppers:"",pattern); @@ -294,7 +294,7 @@ dointerp(char *dest, size_t destsize, const char *pattern, const char *stoppers) break; case '{': pattern = cpytill(scrbuf,pattern+1,'}'); - if ((s = index(scrbuf,'-')) != NULL) + if ((s = strchr(scrbuf,'-')) != NULL) *s++ = '\0'; else s = nullstr; @@ -424,7 +424,7 @@ dointerp(char *dest, size_t destsize, const char *pattern, const char *stoppers) safecpy(scrbuf,s,(sizeof scrbuf)); s = scrbuf; } - if (upper || !(t=rindex(s,'/'))) + if (upper || !(t=strrchr(s,'/'))) t = s; while (*t && !isalpha((unsigned char)*t)) { t++; @@ -531,9 +531,9 @@ getrealname(uid_t uid) #ifdef BERKJUNK while (*s && !isalnum(*s) && *s != '&') s++; #endif - if ((c = index(s, ',')) != NULL) + if ((c = strchr(s, ',')) != NULL) *c = '\0'; - if ((c = index(s, ';')) != NULL) + if ((c = strchr(s, ';')) != NULL) *c = '\0'; s = cpytill(buf,s,'&'); if (*s == '&') { /* whoever thought this one up was */ @@ -544,9 +544,9 @@ getrealname(uid_t uid) *c = toupper((unsigned char)*c); /* gack and double gack */ } #else - if ((c = index(s, '(')) != NULL) + if ((c = strchr(s, '(')) != NULL) *c = '\0'; - if ((c = index(s, '-')) != NULL) + if ((c = strchr(s, '-')) != NULL) s = c; strcpy(buf,tmpbuf); #endif diff --git a/warp/score.c b/warp/score.c index 3736f4a3..8e9991d6 100644 --- a/warp/score.c +++ b/warp/score.c @@ -39,7 +39,7 @@ score_init(void) printf("Cannot access %s\r\n",SAVEDIR); finalize(1); } - if (filestat.st_gid != getegid()) { + if (filestat.st_gid != getegid() && geteuid() != 0) { printf("Warp will not run right without being setgid.\r\n"); finalize(1); } @@ -63,7 +63,8 @@ score_init(void) if (scorespec) wscore(); - snprintf(savefilename, sizeof(savefilename), "save.%s", logname); + snprintf(savefilename, sizeof(savefilename), "%s/save.%s", + SAVEDIR, logname); savfil = experimenting ? NULL : fopen(savefilename,"r"); if (savfil != NULL && fgets(spbuf,100,savfil) != NULL) { @@ -75,7 +76,7 @@ score_init(void) tmpbuf[strlen(tmpbuf)-1] = '\0'; printf("You seem to have left a game %s.\r\n",tmpbuf+9); - s = index(tmpbuf+9, ','); + s = strchr(tmpbuf+9, ','); *s = '\0'; processnum = atoi(s+11); if (kill(processnum, SIGINT)) { @@ -615,8 +616,8 @@ score(void) eat_typeahead(); do { getcmd(&tmp); - } while (tmp != INTRCH && tmp != BREAKCH && !index(" rqQ",tmp)); - if (index("qQr",tmp)) { + } while (tmp != INTRCH && tmp != BREAKCH && !strchr(" rqQ",tmp)); + if (strchr("qQr",tmp)) { justonemoretime = (tmp == 'r'); if (logfd != NULL) fclose(logfd); @@ -646,7 +647,7 @@ score(void) eat_typeahead(); do { getcmd(&tmp); - } while (tmp != INTRCH && tmp != BREAKCH && !index("nNyY \n\r",tmp)); + } while (tmp != INTRCH && tmp != BREAKCH && !strchr("nNyY \n\r",tmp)); if (tmp == 'n' || tmp == 'N' || tmp == INTRCH || tmp == BREAKCH) justonemoretime = false; } @@ -672,8 +673,9 @@ save_game(void) if (experimenting) return; if ((savfil = fopen(savefilename,"w")) == NULL) { + int e = errno; resetty(); - printf("Cannot save game\r\n"); + printf("Cannot save game in %s (%s)\r\n", savefilename, strerror(e)); finalize(1); } fprintf(savfil, "%-8s %10ld, %2d,%5d,%2d,%2d,%3d %c%c%c%c%c%c%c%c%c\n", diff --git a/warp/score.h b/warp/score.h index 2cab9825..c3810c04 100644 --- a/warp/score.h +++ b/warp/score.h @@ -26,7 +26,7 @@ EXT long oldeenergy; EXT long oldbenergy; EXT long oldcurscore; -EXT char savefilename[40]; +EXT char savefilename[512]; #ifdef SCOREFULL #define COMPOFF 0 diff --git a/warp/sig.c b/warp/sig.c index ca95175c..f12a6b11 100644 --- a/warp/sig.c +++ b/warp/sig.c @@ -25,9 +25,6 @@ void sig_init(void) { -#ifdef lint - ; -#else sigignore(SIGINT); /* for inquiry of existence via kill call */ #ifdef SIGTTOU sigignore(SIGTTOU); @@ -38,8 +35,10 @@ sig_init(void) sigset(SIGQUIT, sig_catcher); sigset(SIGILL, sig_catcher); sigset(SIGFPE, sig_catcher); +#if 0 sigset(SIGBUS, sig_catcher); sigset(SIGSEGV, sig_catcher); +#endif sigset(SIGSYS, sig_catcher); sigset(SIGTERM, sig_catcher); } @@ -53,7 +52,6 @@ sig_init(void) sigset(SIGTSTP, stop_catcher); sigset(SIGSTOP, stop_catcher); #endif -#endif /* lint */ } #ifdef SIGTSTP diff --git a/warp/term.c b/warp/term.c index af20896b..21b3b12c 100644 --- a/warp/term.c +++ b/warp/term.c @@ -286,8 +286,8 @@ term_set(char *tcbuf) /* temp area for "uncompiled" termcap entry */ no_can_do("dumb"); if (!scorespec && (LINES < 24 || COLS < 80)) no_can_do("puny"); - if (LINES > 25) - no_can_do("humongus"); +// if (LINES > 25) +// no_can_do("humongous"); crmode(); raw(); diff --git a/warp/util.h b/warp/util.h index eca4cea9..444e0866 100644 --- a/warp/util.h +++ b/warp/util.h @@ -6,49 +6,19 @@ * */ -#if RANDBITS < 15 || defined(lint) -#define rand_mod(m) getpid() -#define RANDRAND 0.0 -#define HALFRAND 0 -#define myrand() getpid() -#else -#if RANDBITS == 15 /* 15 bits of rand()? */ -#define RANDRAND 268435456.0 /* that's 2**28 */ -#define HALFRAND 0x4000 /* that's 2**14 */ -int rand(void); -#define myrand() (rand()&32767) -#define rand_mod(m) ((int)((double)myrand() / 32768.0 * ((double)(m)))) -/* pick number in 0..m-1 */ - -#else - -#if RANDBITS < 31 /* 16 bits of rand()? */ -#define RANDRAND 1073741824.0 /* that's 2**30 */ -#define HALFRAND 0x8000 /* that's 2**15 */ -unsigned rand(); -#define myrand() (rand()&65535) -#define rand_mod(m) ((int)((double)myrand() / 65536.0 * ((double)(m)))) -/* pick number in 0..m-1 */ - -#else /* assume 31 bits */ #define RANDRAND 1152921504606846976.0 /* that's 2**60 */ #define HALFRAND 0x40000000 /* that's 2**30 */ -long rand(); -#define myrand() rand() +#define myrand() (int)random() #define rand_mod(m) ((myrand() / 37) % (m)) /* pick number in 0..m-1 */ /* * The reason for the /37 above is that our random number generator yields * successive evens and odds, for some reason. This makes strange star maps. */ -#endif -#endif -#endif - /* we get fractions of seconds from calling ftime on timebuf */ -EXT struct timeb timebuf; -#define roundsleep(x) (ftime(&timebuf),sleep(timebuf.millitm > 500?x+1:x)) +EXT struct timespec timebuf; +#define roundsleep(x) (clock_gettime(CLOCK_REALTIME, &timebuf),sleep(timebuf.tv_nsec > 5000000000 ?x+1:x)) #define waiting 0 diff --git a/warp/warp.c b/warp/warp.c index b600f0a5..5f0c045f 100644 --- a/warp/warp.c +++ b/warp/warp.c @@ -155,23 +155,6 @@ main(int argc, char *argv[]) FILE *savfil; -#if RANDBITS > 16 - for (i=100; i; i--) - if (rand() >= 65536) - goto rand_ok; - printf("Recompile with RANDBITS = 15 or 16.\n"); - exit(1); -#else -#if RANDBITS > 15 - for (i=100; i; i--) { - if (rand() >= 32768) - goto rand_ok; - } - printf("Recompile with RANDBITS = 15.\n"); - exit(1); -#endif -#endif - #ifdef lint /* to suppress "defined but never used" */ # ifdef SIGTSTP (void)stop_catcher(); @@ -334,7 +317,7 @@ main(int argc, char *argv[]) roundsleep(1); } - srand(getpid()); + srandom(getpid()); do { for (keepgoing = true;;) { @@ -395,7 +378,7 @@ main(int argc, char *argv[]) else tmp = 1; } - } while (tmp != INTRCH && tmp != BREAKCH && !index(" qQs",tmp)); + } while (tmp != INTRCH && tmp != BREAKCH && !strchr(" qQs",tmp)); if (tmp != ' ' && tmp != 's') break; if (!beginner && smarts < 20) smarts += 4; -- cgit v1.2.3-56-ge451