From fa3606a495e4df0c6231e433d3dffa19b7471a60 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Tue, 18 Jan 2022 23:04:23 -0500 Subject: Refactor project structure --- edit.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 edit.c (limited to 'edit.c') diff --git a/edit.c b/edit.c new file mode 100644 index 0000000..9017831 --- /dev/null +++ b/edit.c @@ -0,0 +1,42 @@ +#include "weekly.h" + +int edit_file(const char *filename) { + char editor[255] = {0}; + char editor_cmd[PATH_MAX] = {0}; + int result; + const char *user_editor; + + // Allow the user to override the default editor (vi/notepad) + user_editor = getenv("EDITOR"); + char *editor_path; + if (user_editor != NULL) { + sprintf(editor, "%s", user_editor); + } else { + editor_path = find_program("vim"); + if (editor_path != NULL) { + sprintf(editor, "\"%s\"", editor_path); + } +#if HAVE_WINDOWS + else { + strcpy(editor, "notepad"); + } +#endif + } + + if (!strlen(editor)) { + fprintf(stderr, "Unable to find editor: %s\n", editor); + exit(1); + } + + // Tell editor to jump to the end of the file (when supported) + // Standard 'vi' does not support '+' + if(strstr(editor, "vim")) { + strcat(editor, " +"); + } else if (strstr(editor, "nano") != NULL) { + strcat(editor, " +9999"); + } + + sprintf(editor_cmd, "%s %s", editor, filename); + result = system(editor_cmd); + return result; +} -- cgit