Mercurial > codedump
view kmbscreens/randfile.c @ 93:e5ea19b94639
add kmb screens bot stuff
committer: GitHub <noreply@github.com>
author | Paper <37962225+mrpapersonic@users.noreply.github.com> |
---|---|
date | Thu, 04 Aug 2022 05:26:54 -0400 |
parents | |
children |
line wrap: on
line source
#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; }