home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1320 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: eichin@athena.mit.edu (Mark W. Eichin)
  2. Newsgroups: alt.sources
  3. Subject: sleep hack
  4. Message-ID: <1990May12.074200.12109@athena.mit.edu>
  5. Date: 12 May 90 07:42:00 GMT
  6.  
  7. This is twisted and sick, but was inspired thus: I have a habit of
  8. doing 
  9.     (sleep 7200; echo "Wake up bozo")&    
  10. to wake me up in two hours in for whatever reason... This gives me
  11. good control of messages ("jobs", "kill", and "ps"). However, it
  12. doesn't tell me how much time is remaining on the clock.
  13.     In the grand UNIX tradition of using a small hack to avoid
  14. solving a real (and large) problem, I hacked sleep to update the time
  15. stamp in argv. Since this is what "ps" reads to get its process names,
  16. you can quickly check and see how much time is left.
  17.     This works on BSD systems. I have no idea if it will work on
  18. anything else. I'm not sure that I care :-). The code is all mine;
  19. feel free to post enhancements or bug fixes to alt.sources, especially
  20. since I don't read it.
  21.                         Mark Eichin
  22.                         <eichin@athena.mit.edu>
  23.  
  24. /*
  25.  * Usage: sleep [-i interval] delay
  26.  *
  27.  * without -i, merely sleeps delay seconds using sleep(3).
  28.  * with -i, uses select(2) to sleep, and mutates the "delay" string every
  29.  *    "interval" seconds to allow the user to see how much time is left
  30.  *    using ps(3).
  31.  */
  32.     
  33. #include <sys/types.h>
  34. #include <sys/time.h>
  35. #include <strings.h>
  36. #include <stdio.h>
  37.  
  38. static char *rcsid_sleep_c = "$Header: /afs/athena.mit.edu/user/e/eichin/Src/RCS/sleep.c,v 1.1 90/05/12 02:54:39 eichin Exp $";
  39.  
  40. void Usage(pn)
  41.     char *pn;
  42. {
  43.     fprintf(stderr,"Usage: %s [-i interval] delay\n%s\n%s\n%s\n%s\n",
  44.         pn,
  45.         "\twithout -i, merely sleeps delay seconds using sleep(3).",
  46.         "\twith -i, uses select(2) to sleep, and mutates the 'delay' string every",
  47.         "\t'interval' seconds to allow the user to see how much time is left",
  48.         "\tusing ps(3).");
  49. }
  50.     
  51. void do_sleep(n,interv,mutate)
  52.     int n, interv;
  53.     char *mutate;
  54. {
  55.     struct timeval timeout;
  56.     timeout.tv_usec = 0;
  57.     
  58.     while (n) {
  59.         if(n>interv)
  60.             timeout.tv_sec = interv;
  61.         else
  62.             timeout.tv_sec = n;
  63.         n -= timeout.tv_sec;
  64.         select(0,0,0,0,&timeout);
  65.         sprintf(mutate,"%d",n);
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. int main(argc, argv)
  72.     int argc;
  73.     char **argv;
  74. {
  75.     int interval = 0;
  76.     int delay = 0;
  77.     char *progname = argv[0];
  78.     
  79.     if(!strcmp("-i",argv[1])) {
  80.         if(argc != 4) {
  81.             Usage(progname);
  82.             return 1;
  83.         }
  84.         interval = atoi(argv[2]);
  85.         argc--; argv++;
  86.         argc--; argv++;
  87.     }
  88.     if(argc != 2) {
  89.         Usage(progname);
  90.         return 1;
  91.     }
  92.     delay = atoi(argv[1]);
  93.     if(interval) {
  94.         do_sleep(delay, interval, argv[1]);
  95.     } else {
  96.         sleep(delay);
  97.     }
  98.     return 0;
  99. }
  100. /* end of sleep.c */
  101.