home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part02 / tplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  1.6 KB  |  82 lines

  1. /* tplay.c: play back a tape
  2. Daniel J. Bernstein, brnstnd@nyu.edu.
  3. Depends on sigsched.h, timer.h.
  4. Requires read()/write(), i.e., UNIX.
  5. 7/27/91: Baseline. tplay 1.0, public domain.
  6. No known patent problems.
  7.  
  8. Documentation in tplay.1.
  9.  
  10. */
  11.  
  12. #include "sigsched.h"
  13. #include "timer.h"
  14.  
  15. static char hdr[8];
  16. static char buf[256];
  17. static timer_clock latest;
  18. static timer_sig tsig;
  19. static int flageof = 0;
  20.  
  21. void doit(n)
  22. int n; /* if nonzero, there's data waiting to be written */
  23. {
  24.  int r;
  25.  timer_clock now;
  26.  timer_clock diff;
  27.  int x;
  28.  
  29.  while (!flageof)
  30.   {
  31.    x = 1 + (unsigned int) (unsigned char) hdr[7];
  32.    if (n)
  33.     {
  34.      n = 0;
  35.      while (n < x)
  36.       {
  37.        r = write(1,buf + n,x - n);
  38.        if (r <= 0) ; /*XXX*/
  39.        n += r;
  40.       }
  41.     }
  42.    r = read(0,hdr,8); /* eighth byte is length of data */
  43.    if (r == -1)
  44.      ; /*XXX*/
  45.    if (r < 7)
  46.      ; /*XXX*/
  47.    if (r == 7)
  48.      flageof = 1;
  49.    else
  50.     {
  51.      x = 1 + (unsigned int) (unsigned char) hdr[7];
  52.      n = 0;
  53.      while (n < x)
  54.       {
  55.        r = read(0,buf + n,x - n);
  56.        if (r == -1) ; /*XXX*/
  57.        if (r == 0) ; /*XXX*/
  58.        n += r;
  59.       }
  60.     }
  61.    diff.usec = (hdr[2] * 256 + hdr[1]) * 256 + hdr[0];
  62.    diff.sec = ((hdr[6] * 256 + hdr[5]) * 256 + hdr[4]) * 256 + hdr[3];
  63.    if (diff.sec || diff.usec)
  64.     {
  65.      now = latest; /*XXX: structure copying */
  66.      timer_sum(&now,&diff,&latest);
  67.      timer_setsig(&tsig,TIMER_REAL,&latest);
  68.      ss_schedonce(&tsig.sig,doit,1);
  69.      break;
  70.     }
  71.   }
  72. }
  73.  
  74. main()
  75. {
  76.  timer_now(TIMER_REAL,&latest);
  77.  ss_schedonce(ss_asap(),doit,0);
  78.  timer_init();
  79.  ss_exec();
  80.  exit(0);
  81. }
  82.