aboutsummaryrefslogtreecommitdiff
path: root/edit.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2022-01-18 23:04:23 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2022-01-18 23:04:23 -0500
commitfa3606a495e4df0c6231e433d3dffa19b7471a60 (patch)
treeb82e449f91e4f9002decd9b4a6c1e24db1ada910 /edit.c
parent76063b6148ee7f0d274799c562f896c0a2efa2cb (diff)
downloadweekly-fa3606a495e4df0c6231e433d3dffa19b7471a60.tar.gz
Refactor project structure
Diffstat (limited to 'edit.c')
-rw-r--r--edit.c42
1 files changed, 42 insertions, 0 deletions
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;
+}