Files
fastwc/benchmarks/tools/genfile.c
T
huntedbytheirs c4d4f0b713 first: setup
setting up repo.
2026-08-29 13:48:15 -04:00

71 lines
1.7 KiB
C

/*
* genfile — generate a text file of N lines, each a random 10-character
* alphanumeric string followed by a newline. Used by the fastwc benchmarks
* (createtxt in std.sh) to build test data quickly; std.sh falls back to a
* slow shell loop when this binary has not been built.
*
* usage: genfile <lines>
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define LINE_LEN 10
#define CHUNK (64 * 1024)
static const char alpha[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
#define ALPHA_LEN (sizeof(alpha) - 1)
static uint64_t state;
static uint64_t next(void)
{
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
return state;
}
int main(int argc, char **argv)
{
long n;
long i;
int j;
char buf[CHUNK];
size_t used = 0;
if (argc != 2) {
fprintf(stderr, "usage: %s <lines>\n", argv[0]);
return 2;
}
n = atol(argv[1]);
if (n < 0) {
fprintf(stderr, "genfile: invalid line count: %s\n", argv[1]);
return 2;
}
/* seed from time + pid so each run produces fresh data */
state = (uint64_t)time(NULL) ^ ((uint64_t)getpid() << 32) ^ 0x9E3779B97F4A7C15ULL;
for (i = 0; i < n; i++) {
for (j = 0; j < LINE_LEN; j++)
buf[used++] = alpha[next() % ALPHA_LEN];
buf[used++] = '\n';
if (used + LINE_LEN + 1 > CHUNK) {
if (fwrite(buf, 1, used, stdout) != used)
return 1;
used = 0;
}
}
if (used && fwrite(buf, 1, used, stdout) != used)
return 1;
if (fclose(stdout) != 0)
return 1;
return 0;
}