From 17dd9cdc6d5beb636695e5becbc93275524665af Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 26 Apr 2023 09:36:16 +0200 Subject: os2w2 klaar --- os2w2/.gitignore | 2 ++ os2w2/main.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ os2w2/makefile | 16 +++++++++++++++ os2w2/werkt.png | Bin 0 -> 29215 bytes 4 files changed, 78 insertions(+) create mode 100644 os2w2/.gitignore create mode 100644 os2w2/main.c create mode 100644 os2w2/makefile create mode 100644 os2w2/werkt.png (limited to 'os2w2') diff --git a/os2w2/.gitignore b/os2w2/.gitignore new file mode 100644 index 0000000..f0c9b81 --- /dev/null +++ b/os2w2/.gitignore @@ -0,0 +1,2 @@ +*.o +main diff --git a/os2w2/main.c b/os2w2/main.c new file mode 100644 index 0000000..18108fd --- /dev/null +++ b/os2w2/main.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +/** @brief stat file to get modified date and print name */ +void print_file(const char* path) { + struct stat *info = malloc(sizeof(struct stat)); + stat(path, info); + + struct tm mtime; + localtime_r(&(info->st_mtime), &mtime); + + free(info); + + printf("%04d-%02d-%02d %02d:%02d:%02d %s\n", + mtime.tm_year+1900, mtime.tm_mon+1, mtime.tm_mday, + mtime.tm_hour, mtime.tm_min, mtime.tm_sec, + path); +} + +/** @brief list directory tree recursively */ +void ls_rec(const char *path) { + DIR *dir = opendir(path); // get directory handle (used until all content is listed) + if (dir == NULL) return; // return early if path is not a directory + + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { // read next file in dir + char full_path[1024]; // current entry name with full path prefix + snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); // prepend current directory to current entry + + if (entry->d_type != DT_DIR) { + print_file(full_path); + continue; // don't try to print files recursively + } + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; // ignore current and parent directory + + print_file(full_path); + ls_rec(full_path); + } + + closedir(dir); // free directory handle +} + +int main(int argc, const char** argv) { + if (argc < 2) { + printf("no input directory!\n"); + return 1; + } + + ls_rec(argv[1]); + + return 0; +} + diff --git a/os2w2/makefile b/os2w2/makefile new file mode 100644 index 0000000..1b726be --- /dev/null +++ b/os2w2/makefile @@ -0,0 +1,16 @@ +CC = gcc +LD = gcc + +CFLAGS += -g + +all: main + +main: main.o + $(LD) $^ $(LFLAGS) -o $@ + +%.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +clean: + $(RM) main.o main + diff --git a/os2w2/werkt.png b/os2w2/werkt.png new file mode 100644 index 0000000..fa30636 Binary files /dev/null and b/os2w2/werkt.png differ -- cgit v1.2.3