#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
	srand((unsigned)time(NULL));
	DIR *d;
	struct dirent *dir;
	d = opendir("E:\\Movies\\Anime\\Kill Me Baby");
	int count = 0;
	char** ret = NULL;
	if (d) {
		while ((dir = readdir(d)) != NULL) {
			char* dot = strrchr(dir->d_name, '.');
			if (dot && !strcmp(dot, ".mkv")) {
				ret = realloc(ret, (count+1)*sizeof(char*));
				if (ret == NULL) return 1;
				ret[count] = calloc(strlen(dir->d_name), sizeof(char));
				if (ret[count] == NULL) { free(ret); return 2; }
				strncpy(ret[count], dir->d_name, strlen(dir->d_name)*sizeof(char));
				ret[count][strlen(dir->d_name)] = '\0';
				count++;
			}
		}
		closedir(d);
	}
	printf("%s\n", ret[rand() % count]);
	for (int i = 0; i < count; i++)
		free(ret[i]);
	free(ret);
	return 0;
}