home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fido / pup_v2b.zip / SCHED.C < prev    next >
C/C++ Source or Header  |  1988-01-26  |  3KB  |  105 lines

  1. #include <ascii.h>
  2. #include <puppy.h>
  3. #include <pupmem.h>
  4.  
  5. /* Scheduler for Fido, etc. Various scheduling and support routines.
  6. These use the MSDOS time and date, and assumes a continuous seven
  7. day schedule. Resolution is one minute.
  8.  
  9. til_sched(tag,m)
  10.         Returns the index of the soonest event within M
  11.         minutes, or -1 if none found. This ignores events that
  12.         are runnable now but have already been run. (ie. completed
  13.         an External event early.) 
  14.  
  15.         This returns -1 for all events marked "PREEMPT", unless
  16.         the time til the event is zero.
  17.  
  18.         If ? is passed as the search tag, then any runnable
  19.         event A - W is allowed, else the tag must match exactly.
  20.  
  21. til_event(n)    Return the number of minutes until this event number
  22.         should be run, or 0 if it should be running now. 
  23.  
  24. */
  25.  
  26. /* Find the soonest runnable event within M minutes, and return it's 
  27. index, or -1 if none. Ignore events that have already been run. */
  28.  
  29. til_sched(tag,m)
  30. char tag;
  31. int m;
  32. {
  33. int n,i;
  34. unsigned next_time;
  35. int next_event;
  36.  
  37.     next_time= MINS_DAY;                /* oldest possible */
  38.     next_event= -1;                    /* none of them */
  39.  
  40.     for (i= 0; i < SCHEDS; i++) {
  41.         if (! pup.sched[i].tag) break;        /* NUL == end of table */
  42.         if ((tag != '?') && (pup.sched[i].tag != tag))
  43.             continue;
  44.  
  45. /* If we are in the middle of this events window, (time til event == 0)
  46. see if its already run (SCHED_COMPLETE); if so, ignore it, otherwise
  47. run it immediately. (markevt(event#) must be called explicitly to flag an 
  48. event as already run.) */
  49.  
  50.         n= til_event(i);            /* time til this event runs, */
  51.         if (n == 0) {                /* if its runnable NOW, */
  52.             if (pup.sched[i].bits & SCHED_COMPLETE) continue;
  53.             next_event= i;
  54.             next_time= n;            /* not run yet */
  55.             break;                /* so run it now */
  56.         }
  57.  
  58.         pup.sched[i].bits &= ~SCHED_COMPLETE;    /* clear it */
  59.         
  60. /* Remember the next-soonest event, so we can return it when we terminate the
  61. loop. OPTIONAL events are not checked, since are only interested in them
  62. when their time comes up. */
  63.  
  64.         if ((n < next_time) && !(pup.sched[i].bits & SCHED_OPTIONAL)) {
  65.             next_event= i;            /* this is soonest */
  66.             next_time= n;            /* so far, */
  67.         }
  68.     }
  69.  
  70. /* If we found one within the desired time, return it, else return -1. */
  71.  
  72.     if (next_time <= m) return(next_event);        /* one we found */
  73.     else return(-1);
  74. }
  75.  
  76. /* Mark this event as completed */
  77.  
  78. markevt(n)
  79. unsigned n;
  80. {
  81.     pup.sched[n].bits |= SCHED_COMPLETE;
  82. }
  83.  
  84. /* Return the number of minutes until this event can be run, or 0 if it
  85. should be running now. */
  86.  
  87. til_event(n)
  88. int n;
  89. {
  90. WORD now,start,end;
  91.  
  92.     now= gtime();                /* get current time */
  93.     now= ((now >> 11) & 0x3f) + ((now >> 5) & 0x1f); /* in abs. minutes */
  94.     start= pup.sched[n].hr * 60 + pup.sched[n].min;    /* start time */
  95.     end= start + pup.sched[n].len;        /* when it ends */
  96.  
  97.  
  98.     if ((now >= start) && (now < end))     /* should be running now */
  99.         return(0);            /* zero mins until start ... */
  100.  
  101.     start -= now;                /* just time until it starts */
  102.     if (start < 0) start += MINS_DAY;    /* modulo one week */
  103.     return(start);
  104. }
  105.