Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Aug 2010 16:25:44 +0000 (UTC)
From:      Ulrich Spoerlein <uqs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r211431 - stable/8/games/grdc
Message-ID:  <201008171625.o7HGPi9L038025@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: uqs
Date: Tue Aug 17 16:25:44 2010
New Revision: 211431
URL: http://svn.freebsd.org/changeset/base/211431

Log:
  MFC r203920,210755,210809,210827:
  
  - WARNS?=6 cleanup
  - fix timekeeping for user-supplied value n
  - de-obfuscate the timekeeping of now/delay

Modified:
  stable/8/games/grdc/Makefile
  stable/8/games/grdc/grdc.c
Directory Properties:
  stable/8/games/grdc/   (props changed)

Modified: stable/8/games/grdc/Makefile
==============================================================================
--- stable/8/games/grdc/Makefile	Tue Aug 17 15:44:52 2010	(r211430)
+++ stable/8/games/grdc/Makefile	Tue Aug 17 16:25:44 2010	(r211431)
@@ -1,9 +1,9 @@
 # $FreeBSD$
 
-PROG=   grdc
-WARNS?=	2
-MAN=   grdc.6
-DPADD=  ${LIBNCURSES}
-LDADD=  -lncurses
+PROG=	grdc
+MAN=	grdc.6
+WARNS?=	6
+DPADD=	${LIBNCURSES}
+LDADD=	-lncurses
 
 .include <bsd.prog.mk>

Modified: stable/8/games/grdc/grdc.c
==============================================================================
--- stable/8/games/grdc/grdc.c	Tue Aug 17 15:44:52 2010	(r211430)
+++ stable/8/games/grdc/grdc.c	Tue Aug 17 16:25:44 2010	(r211431)
@@ -15,21 +15,18 @@
  */
 
 #include <err.h>
-#include <time.h>
-#include <signal.h>
 #include <ncurses.h>
+#include <signal.h>
 #include <stdlib.h>
-#ifndef NONPOSIX
+#include <time.h>
 #include <unistd.h>
-#endif
 
 #define YBASE	10
 #define XBASE	10
 #define XLENGTH 58
 #define YDEPTH  7
 
-/* it won't be */
-time_t now; /* yeah! */
+struct timespec now;
 struct tm *tm;
 
 short disp[11] = {
@@ -48,24 +45,23 @@ void movto(int, int);
 void sighndl(int);
 void usage(void);
 
-void sighndl(signo)
-int signo;
+void
+sighndl(int signo)
 {
 	sigtermed=signo;
 }
 
 int
-main(argc, argv)
-int argc;
-char **argv;
-{
-struct timespec ts;
-long t, a;
-int i, j, s, k;
-int n;
-int ch;
-int scrol;
-int t12;
+main(int argc, char *argv[])
+{
+	struct timespec delay;
+	time_t prev_sec;
+	long t, a;
+	int i, j, s, k;
+	int n;
+	int ch;
+	int scrol;
+	int t12;
 
 	t12 = scrol = 0;
 
@@ -90,9 +86,14 @@ int t12;
 		/* NOTREACHED */
 	}
 
-	if (argc > 0)
-		n = atoi(*argv);
-	else
+	if (argc > 0) {
+		n = atoi(*argv) + 1;
+		if (n < 1) {
+			warnx("number of seconds is out of range");
+			usage();
+			/* NOTREACHED */
+		}
+	} else
 		n = 0;
 
 	initscr();
@@ -137,10 +138,11 @@ int t12;
 
 		attrset(COLOR_PAIR(2));
 	}
-	time(&now);
+	clock_gettime(CLOCK_REALTIME_FAST, &now);
+	prev_sec = now.tv_sec;
 	do {
 		mask = 0;
-		tm = localtime(&now);
+		tm = localtime(&now.tv_sec);
 		set(tm->tm_sec%10, 0);
 		set(tm->tm_sec/10, 4);
 		set(tm->tm_min%10, 10);
@@ -194,19 +196,20 @@ int t12;
 		}
 		movto(6, 0);
 		refresh();
-		clock_gettime(CLOCK_REALTIME_FAST, &ts);
-		if (ts.tv_sec == now) {
-			if (ts.tv_nsec > 0) {
-				ts.tv_sec = 0;
-				ts.tv_nsec = 1000000000 - ts.tv_nsec;
+		clock_gettime(CLOCK_REALTIME_FAST, &now);
+		if (now.tv_sec == prev_sec) {
+			if (delay.tv_nsec > 0) {
+				delay.tv_sec = 0;
+				delay.tv_nsec = 1000000000 - now.tv_nsec;
 			} else {
-				ts.tv_sec = 1;
-				ts.tv_nsec = 0;
+				delay.tv_sec = 1;
+				delay.tv_nsec = 0;
 			}
-			nanosleep(&ts, NULL);
-			now = ts.tv_sec + 1;
-		} else
-			now = ts.tv_sec;
+			nanosleep(&delay, NULL);
+			clock_gettime(CLOCK_REALTIME_FAST, &now);
+		}
+		n -= now.tv_sec - prev_sec;
+		prev_sec = now.tv_sec;
 		if (sigtermed) {
 			standend();
 			clear();
@@ -214,7 +217,7 @@ int t12;
 			endwin();
 			errx(1, "terminated by signal %d", (int)sigtermed);
 		}
-	} while(--n);
+	} while (n);
 	standend();
 	clear();
 	refresh();
@@ -225,7 +228,7 @@ int t12;
 void
 set(int t, int n)
 {
-int i, m;
+	int i, m;
 
 	m = 7<<n;
 	for(i=0; i<5; i++) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201008171625.o7HGPi9L038025>