+notify ()
+{
+ time_t now, tdiff, waitfor = -1;
+ register struct event *ep;
+
+ /* Inhibit interference with alarms while changing global vars. */
+ defer_alarms = 1;
+ alarm_deferred = 0;
+
+ now = time ((time_t *) NULL);
+
+ for (ep = events; ep < events + num_events; ep++)
+ /* Are any events ready to fire? */
+ if (ep->reply_at <= now)
+ {
+ fputs (ep->token, stdout);
+ putc ('\n', stdout);
+ fflush (stdout);
+ free (ep->token);
+
+ /* We now have a hole in the event array; fill it with the last
+ event. */
+ ep->token = events[num_events - 1].token;
+ ep->reply_at = events[num_events - 1].reply_at;
+ num_events--;
+
+ /* We ought to scan this event again. */
+ ep--;
+ }
+ else
+ {
+ /* next timeout should be the soonest of any remaining */
+ if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
+ waitfor = (long)tdiff;
+ }
+
+ /* If there are no more events, we needn't bother setting an alarm. */
+ if (num_events > 0)
+ alarm (waitfor);
+
+ /* Now check if there was another alarm
+ while we were handling an explicit request. */
+ defer_alarms = 0;
+ if (alarm_deferred)
+ notify ();
+ alarm_deferred = 0;
+}
+\f
+/* Read one command from command from standard input
+ and schedule the event for it. */