-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-prefix.c
46 lines (37 loc) · 1021 Bytes
/
random-prefix.c
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
37
38
39
40
41
42
43
44
45
46
/*
* Copyright 2018 Sandipan Das, IBM Corporation
*
* This will generate random-length prefixes of a
* string and attempt to print it with the write
* system call.
*
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int i;
char str[] = "lorem ipsum dolor sit amet", buf[64];
size_t bytes, lbound, ubound;
/* Setup seed */
srand(time(NULL));
/* Setup lower and upper bounds for bytes to copy */
lbound = 1;
ubound = sizeof(str) - 1;
for (i = 0; i < INT32_MAX; i++) {
/* Generate a random number of bytes to copy */
bytes = rand() % (ubound - lbound + 1) + lbound;
/* Copy bytes to buffer */
strncpy(buf, str, bytes);
buf[bytes] = '\0';
/* Print buffer with a write() system call */
write(STDOUT_FILENO, buf, bytes);
/* Wait for a couple of seconds */
sleep(2);
}
return EXIT_SUCCESS;
}