-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomline.c
60 lines (55 loc) · 1.33 KB
/
randomline.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <memory.h>
#include <random.h>
#include <sys.h>
#define BUFSIZE 4096
static void copybytes(char *dest, const char *src, int length) {
register const char *p;
const char *end = src + length;
for (p = src; p < end; p++)
*dest++ = *p;
}
// Copy a line including the terminating \n and returns the length.
static int readline(char *dest, int destsize) {
static char buffer[BUFSIZE];
static int pos;
if (pos < BUFSIZE / 2) {
int n_read = read(0, buffer + pos, BUFSIZE - pos);
if ((n_read == 0) && (pos == 0)) {
write(2, "End of file.\n", 13);
return -1;
}
pos += n_read;
}
for (const char *p = buffer; p < buffer + pos; p++) {
if (*p == '\n') {
p++;
int linelength = p - buffer;
copybytes(dest, buffer, linelength);
copybytes(buffer, p, BUFSIZE - linelength);
pos -= linelength;
return linelength;
}
}
write(2, "Buffer capacity exceeded\n", 25);
return -1;
}
int main() {
char selected[4096];
char buffer[4096];
int lineno = 0;
int selsize = 0;
RandGen g = randinit();
for (;;) {
int len = readline(buffer, 4096);
if (len <= 0)
break;
lineno++;
int64_t rnd = randnext(&g);
if (rnd % lineno == 0) {
copybytes(selected, buffer, len);
selsize = len;
}
}
write(1, selected, selsize);
return 0;
}