aboutsummaryrefslogtreecommitdiff
path: root/booki.c
diff options
context:
space:
mode:
authorBen Winston2024-05-23 21:29:58 -0400
committerBen Winston2024-05-23 21:29:58 -0400
commit25dc82dd9d2d5e6283dad0bae4a32916c5a233d2 (patch)
tree410e168e52ed1da41fa2cb6a510ad440f9560710 /booki.c
parent177e0bff0df05c57f00d58a55baba4aa8e0661ec (diff)
refactor: pass booki file along
Diffstat (limited to 'booki.c')
-rw-r--r--booki.c21
1 files changed, 6 insertions, 15 deletions
diff --git a/booki.c b/booki.c
index a90edeb..dbe6392 100644
--- a/booki.c
+++ b/booki.c
@@ -53,8 +53,6 @@ struct Book* init_book(struct Book* book) {
return book;
}
-char* BOOKI_FILE;
-
int PARSE_LINE = 1;
const char* get_last_word(const char* str) {
@@ -383,12 +381,12 @@ void print_book(struct Book book, bool all_fields) {
}
}
-char** search(int argc, char* argv[]) {
+char** search(int argc, char* argv[], char* booki_file) {
struct search_opt search_opts = parse_search_options(argc, argv);
bool print = false;
// get the books array
- char* data = load_file(BOOKI_FILE);
+ char* data = load_file(booki_file);
if (!data) {
printf("no such array: 'books'");
return NULL;
@@ -451,13 +449,7 @@ char** search(int argc, char* argv[]) {
}
-void open() {
-
- char* home = getenv("HOME");
- if (!home) {
- printf("no home?\n");
- home = "/"; // no way
- }
+void open(char* filepath) {
char* editor = getenv("EDITOR");
if (!editor)
editor = "nano";
@@ -469,7 +461,7 @@ void open() {
printf("fork has failed!\n");
break;
case 0:
- execlp(editor, editor, BOOKI_FILE, NULL);
+ execlp(editor, editor, filepath, NULL);
printf("child failed :(\n");
break;
default:
@@ -491,16 +483,15 @@ int main(int argc, char* argv[]) {
char* home = getenv("HOME");
char booki_file[strlen(home) + strlen(BOOKI_FILE_REL) + 1];
sprintf(booki_file, "%s/%s", home, BOOKI_FILE_REL);
- BOOKI_FILE = booki_file;
char** remaining = NULL;
if (argc == 1) {
help(false);
return 0;
} else if (strcmp(argv[1], "open") == 0) {
- open();
+ open(booki_file);
} else if (strcmp(argv[1], "search") == 0) {
- remaining = search(argc - 1, argv + 1);
+ remaining = search(argc - 1, argv + 1, booki_file);
}
return 0;