Skip to content

Commit

Permalink
Add dir traversal to SD demo
Browse files Browse the repository at this point in the history
  • Loading branch information
roscopeco committed Oct 6, 2024
1 parent 5149de3 commit e52c783
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion code/software/newlib/libc_sd/main.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <rosco_m68k/debug.h>

#define BUF_SIZE 64

static void existing_file_test();
static void new_file_test();
static void list_directory(const char *path);

int main() {
printf("## libc file test\n\n");

existing_file_test();
new_file_test();


list_directory("/sd/");

FILE *f = fopen("/sd/gemsys", "r");
if (f) {
printf("fopen dir okay\n");
fclose(f);
} else {
printf("fopen dir not okay\n");
}


struct stat statbuf;
if (stat("/sd/gemsys", &statbuf) == 0) {
printf("Stat ok\n");
if (S_ISDIR(statbuf.st_mode)) {
printf("Is a dir\n");
} else {
printf("Is not a dir\n");
}

/*
int subdir_count = count_entries_in_directory(full_path);
if (subdir_count >= 0) {
printf(" --> Subdirectory '%s' contains %d entries\n", entry->d_name, subdir_count);
}
*/
} else {
printf("Stat not okay\n");
}

printf("Tests are done!\n");

breakpoint();
Expand Down Expand Up @@ -87,3 +121,23 @@ static void new_file_test() {
printf("New file test done\n\n");
}

static void list_directory(const char *path) {
struct dirent *entry;
DIR *dp = opendir(path);

if (dp == NULL) {
perror("opendir");
return;
}

// Loop through directory entries
while ((entry = readdir(dp)) != NULL) {
// Ignore "." and ".." entries
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
printf("%s [DIR: %d]\n", entry->d_name, entry->d_is_dir);
}
}

closedir(dp);
}

0 comments on commit e52c783

Please sign in to comment.