103 lines
2.9 KiB
C
103 lines
2.9 KiB
C
#include "fio.h"
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
/*
|
|
================================
|
|
struct LList *dirToLList(const char *dir_name, int opts)
|
|
|
|
This function takes a given directory path and attempts to populate a linked list(LList) with the contents of the directory (excluding ".." and "."). The opts argument must be provided with F_DIRS, F_FILES, or both F_DIRS|F_FILES. This limits the items added to the linked list to only directories, only files, or both.
|
|
|
|
================================
|
|
*/
|
|
struct LList *dirToLList(const char *dir_name, int opts) {
|
|
struct dirent *entry;
|
|
DIR *dir = opendir(dir_name);
|
|
if (dir == NULL)
|
|
return NULL;
|
|
|
|
struct LList *start = NULL;
|
|
struct LList *llist = NULL;
|
|
struct LList *llist_last = NULL;
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
continue;
|
|
int entry_size = strlen(entry->d_name)+1;
|
|
// ugly
|
|
char file_path[strlen(dir_name)+entry_size];
|
|
strcpy(file_path, dir_name);
|
|
file_path[strlen(dir_name)] = '/';
|
|
memcpy(file_path+strlen(dir_name)+1, entry->d_name, entry_size);
|
|
// ok, let's get file properties and see if we want it
|
|
struct stat file_stat;
|
|
stat(file_path, &file_stat);
|
|
switch(file_stat.st_mode & S_IFMT) {
|
|
case S_IFDIR:
|
|
if (~opts & F_DIRS)
|
|
continue;
|
|
break;
|
|
case S_IFREG:
|
|
if (~opts & F_FILES)
|
|
continue;
|
|
break;
|
|
}
|
|
// if we got here, then the entry is valid
|
|
llist = newLList();
|
|
llist->data = malloc(entry_size);
|
|
llist->size = entry_size;
|
|
memcpy(llist->data, entry->d_name, entry_size);
|
|
if (start == NULL)
|
|
start = llist;
|
|
if (llist_last != NULL)
|
|
llist_last->next = llist;
|
|
llist_last = llist;
|
|
}
|
|
closedir(dir);
|
|
return start;
|
|
}
|
|
|
|
/*
|
|
================================
|
|
int fileExists(const char *filename)
|
|
|
|
A simple function that checks if the given filename exists.
|
|
================================
|
|
*/
|
|
int fileExists(const char *filename) {
|
|
struct stat buffer;
|
|
return (stat (filename, &buffer) == 0);
|
|
}
|
|
|
|
/*
|
|
================================
|
|
int fileToMemory(char **buffer, const char *file_name)
|
|
|
|
This function takes a pointer to an uninitialized char array, along with a char array for a file name. It then attempts to read the provided file into the passed buffer, returning the size on completion.
|
|
================================
|
|
*/
|
|
int fileToMemory(char **buffer, const char *file_name) {
|
|
int i, n, pos = 0;
|
|
char t_buf[16];
|
|
|
|
FILE *file = fopen(file_name, "r");
|
|
if (file == NULL) {
|
|
return -1;
|
|
}
|
|
fseek(file, 0, SEEK_END);
|
|
int size = ftell(file);
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
char *new_buffer = malloc(size);
|
|
while ((n = fread(t_buf, 1, 16, file))) {
|
|
for (i = 0; i < n; i++) {
|
|
new_buffer[pos++] = t_buf[i];
|
|
}
|
|
}
|
|
fclose(file);
|
|
*buffer = new_buffer;
|
|
return size;
|
|
}
|