Patch so grdc ticks when it should

Paul Herman pherman at frenchfries.net
Wed Jul 28 17:12:31 PDT 2004


Right now /usr/games/grdc simply sleeps one second and then 
refreshes then screen.  Slightly annoying if you have two synced 
machines next to each other that display different times.

The following patch fixes that behavior.

-Paul.

--- grdc.c.orig	2004-07-28 15:49:50.000000000 -0700
+++ grdc.c	2004-07-28 16:59:10.000000000 -0700
@@ -25,7 +25,7 @@
 #define XLENGTH 58
 #define YDEPTH  7
-time_t now;
+struct timespec now;
 struct tm *tm;
 short disp[11] = {
@@ -55,6 +55,7 @@
 int
 main(int argc, char **argv)
 {
+	struct timespec ts;
 	int i, s, k;
 	int n;
 	int ch;
@@ -144,8 +145,19 @@
 	}
 	do {
 		mask = 0;
-		time(&now);
-		tm = localtime(&now);
+		clock_gettime(CLOCK_REALTIME, &now);
+		if (scrol) {
+			/*
+			 * The time we really wish to display is now + scroll_msecs
+			 */
+			now.tv_nsec /= 1000;
+			now.tv_nsec += scroll_msecs * 1000;
+			while (now.tv_nsec > 1000000) {
+				now.tv_sec++;
+				now.tv_nsec -= 1000000;
+			}
+		}
+		tm = localtime(&now.tv_sec);
 		set(tm->tm_sec % 10, 0);
 		set(tm->tm_sec / 10, 4);
 		set(tm->tm_min % 10, 10);
@@ -156,7 +168,7 @@
 		set(10, 17);
 		for(k = 0; k < 6; k++) {
 			if (scrol) {
-				snooze(scroll_msecs / 6);
+				usleep(1000 * scroll_msecs / 6);
 				for(i = 0; i < 5; i++)
 					new[i] = (new[i] & ~mask) |
 						 (new[i+1] & mask);
@@ -177,7 +189,7 @@
 		}
 		move(ybase, 0);
 		refresh();
-		snooze(1000 - (scrol ? scroll_msecs : 0));
+		snooze(scrol ? scroll_msecs : 0);
 	} while (forever ? 1 : --n);
 	standend();
 	clear();
@@ -191,9 +203,9 @@
 {
 	struct timespec ts;
+	clock_gettime(CLOCK_REALTIME, &ts);
+	ts.tv_nsec = 1000000000L - ts.tv_nsec - 1000000*msecs;
 	ts.tv_sec = 0;
-	ts.tv_nsec = 1000000 * msecs;
-
 	nanosleep(&ts, NULL);
 	if (sigtermed) {





More information about the Submit mailing list