Mercurial > codedump
comparison 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 |
comparison
equal
deleted
inserted
replaced
92:96b9e046f45d | 93:e5ea19b94639 |
---|---|
1 #include <stdio.h> | |
2 #include <dirent.h> | |
3 #include <stdlib.h> | |
4 #include <time.h> | |
5 | |
6 int main(void) { | |
7 srand((unsigned)time(NULL)); | |
8 DIR *d; | |
9 struct dirent *dir; | |
10 d = opendir("E:\\Movies\\Anime\\Kill Me Baby"); | |
11 int count = 0; | |
12 char** ret = NULL; | |
13 if (d) { | |
14 while ((dir = readdir(d)) != NULL) { | |
15 char* dot = strrchr(dir->d_name, '.'); | |
16 if (dot && !strcmp(dot, ".mkv")) { | |
17 ret = realloc(ret, (count+1)*sizeof(char*)); | |
18 if (ret == NULL) return 1; | |
19 ret[count] = calloc(strlen(dir->d_name), sizeof(char)); | |
20 if (ret[count] == NULL) { free(ret); return 2; } | |
21 strncpy(ret[count], dir->d_name, strlen(dir->d_name)*sizeof(char)); | |
22 ret[count][strlen(dir->d_name)] = '\0'; | |
23 count++; | |
24 } | |
25 } | |
26 closedir(d); | |
27 } | |
28 printf("%s\n", ret[rand() % count]); | |
29 for (int i = 0; i < count; i++) | |
30 free(ret[i]); | |
31 free(ret); | |
32 return 0; | |
33 } |