diff options
| author | Ben Winston | 2024-05-30 21:22:46 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-30 21:22:46 -0400 |
| commit | 5cf31cbf086564fbf2f937a921c183fec77c8487 (patch) | |
| tree | 7c63ca8a99d47e55cdd96799c97d531d37b5218a | |
| parent | 9557e49f43b7aae9470ec5466442a124d54dc1bf (diff) | |
move working files to /tmp, replace BOOKI_FILE and clean up after
| -rw-r--r-- | booki.c | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -7,8 +7,8 @@ #include <sys/wait.h> #include <unistd.h> -#define EDIT_FILE "edit.toml" -#define FIXED_FILE "fixed.toml" +#define EDIT_FILE "/tmp/booki-edit.toml" +#define FIXED_FILE "/tmp/booki-fixed.toml" #define MAX_SEARCH_OPTS 5 @@ -488,6 +488,24 @@ void open(char* filepath) { } } +bool copy(char* src, char* dest) { + pid_t pid; + int status; + switch((pid = fork())) { + case -1: + printf("fork failed\n"); + return false; + case 0: + execl("/bin/cp", "/bin/cp", src, dest, NULL); + printf("copy failed\n"); + return false; + default: + // wait for the chlid to finish + pid = wait(&status); + return true; + } +} + void free_es(struct es* str) { if (str == NULL) return; struct es* tmp; @@ -572,8 +590,11 @@ void search(int argc, char* argv[], char* booki_file) { // if we're editing, both files are open at this point if (search_opts.edit) { + // first, close and let the user edit fclose(edit_file); open(EDIT_FILE); + + // after they've edited, read it in and add it to fixed_file data = load_file(EDIT_FILE); if (!data) { printf("can't open edit file\n"); @@ -588,6 +609,15 @@ void search(int argc, char* argv[], char* booki_file) { } fclose(fixed_file); + + // copy the fixed_file to booki file + bool success = copy(FIXED_FILE, booki_file); + if (!success) { + printf("failed! fixed file is here: %s\n", FIXED_FILE); + } else { + unlink(EDIT_FILE); + unlink(FIXED_FILE); + } } } |
