summaryrefslogtreecommitdiff
path: root/mcastseed/src/random_speed_dd.c
blob: 1fbc8634972694071e3a12865e67a15428b7a636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

char buf[0xffff];

int main() {
	ssize_t nread, nwrite, remains;

	srandom(1); /* Always the same pseudo-random sequence */

	while ( (nread=read(0, buf, 0xfff & rand())) > 0 ) {
		remains = nread;
		while ( remains ) {
			nwrite=write(1, buf, nread);
			if ( nwrite < 0 )  {
				if ( !(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) ) {
					perror("write");
					return nwrite;
				}
			} else {
				remains -= nwrite;
			}
		}
		/*fprintf(stderr, "nread==%zu, nwrite==%zu\n", nread, nwrite);*/
		usleep( 0xffff & rand() );
	}
	if ( nread < 0 ) {
		perror("read");
		return nread;
	}

	return 0;
}