Skip to content

Instantly share code, notes, and snippets.

@verma
Created December 8, 2016 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verma/3e75192d3af24ff17a5ca3f21409a789 to your computer and use it in GitHub Desktop.
Save verma/3e75192d3af24ff17a5ca3f21409a789 to your computer and use it in GitHub Desktop.
// gencarol.c
// generate carol set
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
void makedir(const char *path) {
int ret = mkdir(path, 0755);
if (ret != 0 && errno != EEXIST) {
perror("Failed to create destination folder:");
exit(1);
}
}
void file_with_random_content(const char *path) {
int length = 1000 + rand() % 1000;
FILE *fp = fopen(path, "w");
char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (!fp) {
perror("Failed to create output file:");
exit(1);
}
while(length --) {
fputc(chars[rand() % 62], fp);
}
fclose(fp);
}
void three_deep (const char *base_path) {
int i, j, k;
for (i = 0 ; i < 10 ; i ++) {
char ipath[1024];
sprintf(ipath, "%s/%d", base_path, i);
makedir(ipath);
printf(":: i = %d\n", i);
for (j = 0 ; j < 10 ; j ++) {
char jpath[1024];
sprintf(jpath, "%s/%d/%d", base_path, i, j);
makedir(jpath);
for (k = 0 ; k < 100 ; k ++) {
char kpath[1024];
sprintf(kpath, "%s/%d/%d/%02d.txt", base_path, i, j, k);
file_with_random_content(kpath);
}
}
}
}
void two_deep (const char *base_path) {
int i, j, k;
for (i = 0 ; i < 10 ; i ++) {
char ipath[1024];
sprintf(ipath, "%s/%d", base_path, i);
makedir(ipath);
printf(":: i = %d\n", i);
for (k = 0 ; k < 1000 ; k ++) {
char kpath[1024];
sprintf(kpath, "%s/%d/%03d.txt", base_path, i, k);
file_with_random_content(kpath);
}
}
}
int main() {
const char *base_path = "/Users/verma/tmp/ccset2";
srand(time(NULL));
two_deep(base_path);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment