home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / musbus / part03 / keyb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  2.5 KB  |  118 lines

  1. /*
  2.  *  keyb -- emulate a user typing at a terminal
  3.  *
  4.  *  keyb [ datafile ]
  5.  *
  6.  *  standard input is copied to standard output at a rate not in excess
  7.  *  of "rate" characters per second, and copied to "copyfile" if
  8.  *  specified
  9.  *
  10.  *  environment variables $rate and $tty control typing rate (characters
  11.  *  per second) and destination of echoed output.
  12.  *
  13.  *  $Header: keyb.c,v 3.5 87/06/22 14:24:28 kjmcdonell Beta $
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <signal.h>
  18.  
  19. #define DEF_RATE    5
  20. #define GRANULE        5
  21.  
  22. int    thres;
  23. int    est_rate = DEF_RATE;
  24. int    sigpipe;    /* pipe write error flag */
  25.  
  26. main(argc, argv)
  27. int    argc;
  28. char    *argv[];
  29. {
  30.     int        i;
  31.     int        l;
  32.     int        fcopy = 0;    /* fd for copy output */
  33.     int        output;        /* aggregate output char count */
  34.     int        c;
  35.     int        nch;        /* # characters read */
  36.     char    buf[16];    /* the current glob of data */
  37.     int        onalarm();
  38.     int        pipeerr();
  39.     char    *getenv();
  40.     int        written;
  41.     char    *p;
  42.     char    *prog;        /* my name */
  43.  
  44.     prog = argv[0];
  45.     if ((p = getenv("rate")) != (char *)0) {
  46.     sscanf(p, "%f", &est_rate);
  47.     if (est_rate <= 0) {
  48.         fprintf(stderr, "%s: bad rate, reset to %.2f chars/sec\n", prog, DEF_RATE);
  49.         est_rate = DEF_RATE;
  50.     }
  51. #ifdef DEBUG
  52.     else
  53.         fprintf(stderr, "%s: typing rate %.2f chars/sec\n", est_rate);
  54. #endif
  55.     }
  56.     if ((p = getenv("tty")) != (char *)0) {
  57.     fcopy = open(p, 1);
  58.     if (fcopy < 0)
  59.         fcopy = creat(p, 0600);
  60.     if (fcopy < 0) {
  61.         fprintf(stderr, "%s: cannot open copy file '%s'\n", prog, p);
  62.         exit(2);
  63.     }
  64.     lseek(fcopy, 0L, 2);    /* append at end of file */
  65.     }
  66.     
  67.     if (argc == 2) {
  68.     /* datafile argument specified */
  69.     close(0);
  70.     if (open(argv[1], 0) < 0) {
  71.         fprintf(stderr, "%s: Cannot open %s\n", prog, argv[1]);
  72.         exit(4);
  73.     }
  74.     }
  75.  
  76.     srand(time(0));
  77.     thres = est_rate = est_rate * GRANULE;
  78.     output = 0;
  79.  
  80.     signal(SIGPIPE, pipeerr);
  81.     signal(SIGALRM, onalarm);
  82.     alarm(GRANULE);
  83.     while (1) {
  84.     l = rand() % 15 + 1;    /* 1-15 chars */
  85.     if (l == 0) continue;
  86.     nch = read(0, buf, l);
  87.     if (nch == 0)
  88.         break;
  89.     if ((written = write(1, buf, nch)) != nch) {
  90.         /* argh! */
  91.         if (sigpipe)
  92.         fprintf(stderr, "type: ** SIGPIPE error ** buf: %s\n", buf);
  93.         else
  94.         fprintf(stderr, "type: ** write error ** buf: %s\n", buf);
  95.         exit(4);
  96.     }
  97.     if (fcopy)
  98.         write(fcopy, buf, nch);
  99.     output += nch;
  100.     while (output > thres)
  101.         pause();
  102.     }
  103.     alarm(0);
  104.     exit(0);
  105. }
  106.  
  107. onalarm()
  108. {
  109.     thres += est_rate;
  110.     signal(SIGALRM, onalarm);
  111.     alarm(GRANULE);
  112. }
  113.  
  114. pipeerr()
  115. {
  116.     sigpipe++;
  117. }
  118.