DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T k

⟦f0caab069⟧ TextFile

    Length: 2715 (0xa9b)
    Types: TextFile
    Names: »keyb.c«

Derivation

└─⟦db229ac7e⟧ Bits:30007240 EUUGD20: SSBA 1.2 / AFW Benchmarks
    └─⟦this⟧ »EUUGD20/AFUU-ssba1.21/ssba1.21E/musbus/keyb.c« 
    └─⟦this⟧ »EUUGD20/AFUU-ssba1.21/ssba1.21F/musbus/keyb.c« 

TextFile

/*
 *  keyb -- emulate a user typing at a terminal
 *
 *  keyb [ datafile ]
 *
 *  standard input is copied to standard output at a rate not in excess
 *  of "rate" characters per second, and optinally echoed to another
 *  file/device.
 *
 *  environment variables $rate and $tty control typing rate (characters
 *  per second) and destination of echoed output.
 *
 *  $Header: keyb.c,v 5.2 87/12/09 15:01:18 kenj Exp $
 */

#include <stdio.h>
#include "../install/signal.h"

#define DEF_RATE	5
#define GRANULE		5

int	thres;
float	est_rate = DEF_RATE;
int	sigpipe;	/* pipe write error flag */

main(argc, argv)
int	argc;
char	*argv[];
{
    int		i;
    int		l;
    int		fcopy = 0;	/* fd for copy output */
    int		output;		/* aggregate output char count */
    int		c;
    int		nch;		/* # characters read */
    char	buf[16];	/* the current glob of data */
    int		onalarm();
    int		pipeerr();
    char	*getenv();
    int		written;
    char	*p;
    char	*prog;		/* my name */
    double	atof();

    prog = argv[0];
    if ((p = getenv("rate")) != (char *)0) {
	est_rate = (double) atof(p);
	if (est_rate <= 0) {
	    fprintf(stderr, "%s: bad rate, reset to %.2f chars/sec\n", prog, DEF_RATE);
	    est_rate = DEF_RATE;
	}
    }
    /*
     * No echoing if env var either undefined or a null string.
     */
    if ((p = getenv("tty")) != (char *)0 && *p != '\0') {
	fcopy = open(p, 1);
	if (fcopy < 0)
		fcopy = creat(p, 0600);
	if (fcopy < 0) {
	    fprintf(stderr, "%s: cannot open copy file '%s'\n", prog, p);
	    exit(2);
	}
	lseek(fcopy, 0L, 2);	/* append at end of file */
    }
    
    if (argc == 2) {
	/* datafile argument specified */
	close(0);
	if (open(argv[1], 0) < 0) {
	    fprintf(stderr, "%s: Cannot open %s\n", prog, argv[1]);
	    exit(4);
	}
    }

    srand(time(0));
    thres = est_rate = est_rate * GRANULE;
    output = 0;

    signal(SIGPIPE, pipeerr);
    signal(SIGALRM, onalarm);
    alarm(GRANULE);
    while (1) {
	l = rand() % 15 + 1;	/* 1-15 chars */
	if (l == 0) continue;
	nch = read(0, buf, l);
	if (nch < 0) {
	    perror("keyb: ** read error **");
	    exit(4);
	}
	else if (nch == 0)
	    break;
	if ((written = write(1, buf, nch)) != nch) {
	    /* argh! */
	    buf[nch] = '\0';
	    if (sigpipe)
		fprintf(stderr, "keyb: ** SIGPIPE error ** buf: %s\n", buf);
	    else
		fprintf(stderr, "keyb: ** write error ** wrote %d of %d from buf: \"%s\"\n", written, nch, buf);
	    perror("keyb is dying...");
	    exit(4);
	}
	if (fcopy)
	    write(fcopy, buf, nch);
	output += nch;
	while (output > thres)
	    pause();
    }
    alarm(0);
    exit(0);
}

onalarm()
{
    thres += est_rate;
    signal(SIGALRM, onalarm);
    alarm(GRANULE);
}

pipeerr()
{
	sigpipe++;
}