--- atd/atd.c~atd-select.patch	Mon Feb  2 15:30:39 2004
+++ atd/atd.c	Tue Nov  9 22:27:41 2004
@@ -68,10 +68,10 @@
 unsigned long data;
 struct rtc_time rtc_tm;
 time_t now, then;
-struct tm *tm;
 struct timeval tv;
 int nfds;
 fd_set afds;
+int timeslip = -1;
 
 #ifdef DEBUG
 	printf("waitfor %ld\n", t);
@@ -91,6 +91,7 @@
 		exit(errno);
 	}
 
+#if 0
 	/* Set the RTC time/date */
 	now = time (NULL);
 	tm = gmtime (&now);
@@ -105,27 +106,32 @@
 		perror ("ioctl");
 		exit (errno);
 	}
-
+#endif
+	
 #ifdef DEBUG
 	printf ("Current RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
 		rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
 		rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
 #endif
 
-	tm = gmtime (&t);
-
 #ifdef DEBUG
 	printf ("Alarm date/time is %d-%d-%d, %02d:%02d:%02d.\n",
 		tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900,
 		tm->tm_hour, tm->tm_min, tm->tm_sec);
 #endif
-	if (t && compare_rtc_to_tm (&rtc_tm, tm) >= 0) {
-		close (rtcfd);
-		close (tfd);
-		return;
-	}
 
 	if (t) {
+		struct tm *tm;
+		tm = gmtime (&t);
+
+		if (compare_rtc_to_tm (&rtc_tm, tm) >= 0) {
+			close (rtcfd);
+			close (tfd);
+			return;
+		}
+
+		timeslip = t - now;
+
 		/* set the alarm */
 		rtc_tm.tm_mday = tm->tm_mday;
 		rtc_tm.tm_mon = tm->tm_mon;
@@ -178,7 +184,8 @@
 	if (tfd > rtcfd)
 		nfds = tfd + 1;
 	/* Wait up to ten minutes. */
-	tv.tv_sec = 10 * 60;
+#define TIMEOUT 10 * 60
+	tv.tv_sec = (timeslip == -1 || timeslip > TIMEOUT) ? TIMEOUT : timeslip;
 	tv.tv_usec = 0;
 	then = now;
 	if (select (nfds, &afds, (fd_set *) 0, (fd_set *) 0, &tv) < 0) {
