OS1/directories/directory_list.c

122 lines
2 KiB
C
Raw Permalink Normal View History

2023-10-30 03:25:48 -07:00
#include <stdio.h>
#include "stdlib.h"
#include <dirent.h>
#include <sys/stat.h>
#include "string.h"
static int
filter_movies (const struct dirent *unused)
{
if(strstr(unused->d_name, "movies_") == unused->d_name) {
char* temp = strchr(unused->d_name, '.');
if(temp != NULL)
if( !strcmp(temp, ".csv"))
return 1;
}
return 0;
}
void dir_free(struct dirent** ent, int n) {
int i = 0;
while(n--) {
free(ent[n]);
i++;
}
free(ent);
}
char* largest_val() {
struct dirent** eps;
int num = scandir ("./", &eps, filter_movies, alphasort);
if(num < 1)
exit(1);
struct dirent* max = eps[0];
struct stat *cur_stat = malloc(sizeof(struct stat));
stat(eps[0]->d_name, cur_stat);
int n = 1;
struct stat *st = malloc(sizeof(struct stat));
while(n < num) {
stat (eps[n]->d_name, st);
if(st->st_size > cur_stat->st_size) {
struct stat* temp = cur_stat;
cur_stat = st;
st = temp;
max = eps[n];
}
n++;
}
char* ret = malloc((strlen(max->d_name) + 1) * sizeof(char));
strcpy(ret, max->d_name);
free(cur_stat);
free(st);
dir_free(eps, n);
return ret;
}
char* smallest_val() {
struct dirent** eps;
int num = scandir ("./", &eps, filter_movies, alphasort);
if(num < 1)
exit(1);
struct dirent* max = eps[0];
struct stat *cur_stat = malloc(sizeof(struct stat));
stat(eps[0]->d_name, cur_stat);
int n = 1;
struct stat *st = malloc(sizeof(struct stat));
while(n < num) {
stat (eps[n]->d_name, st);
if(st->st_size < cur_stat->st_size) {
struct stat* temp = cur_stat;
cur_stat = st;
st = temp;
max = eps[n];
}
n++;
}
char* ret = malloc((strlen(max->d_name) + 1) * sizeof(char));
strcpy(ret, max->d_name);
free(cur_stat);
free(st);
dir_free(eps, n);
return ret;
}
int temp() {
char* large = largest_val();
printf("Largest Moives: %s\n", large);
char* small = smallest_val();
printf("Smallest Moives: %s\n", small);
free(small);
free(large);
return 0;
}