diff options
author | cflip <cflip@cflip.net> | 2023-01-31 13:31:21 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-31 14:00:17 -0700 |
commit | 1b8261071ef3ad3b8d1c2db6e712b0e590c563b3 (patch) | |
tree | ca7e8528b37ccd1d7748b5c70582239b506f8b6a /file.c | |
parent | 264c4186ed8e3d2e3275a6514c8714903036cb26 (diff) |
Now there there is a new way to prompt for text, we can reimplement the
long abandoned editor_prompt function and use it to allow saving to new
files.
The new editor_prompt function takes a callback that will be called
after the user presses enter on the prompt.
Diffstat (limited to 'file.c')
-rw-r--r-- | file.c | 48 |
1 files changed, 25 insertions, 23 deletions
@@ -10,6 +10,7 @@ #include "error.h" #include "line.h" #include "syntax.h" +#include "window.h" static char *lines_to_string(struct editor_state *editor, int *buffer_length) { @@ -70,34 +71,35 @@ void editor_open(struct editor_state* editor, char* filename) editor->dirty = 0; } -void editor_save(struct editor_state* editor) +int file_save_current_file(struct editor_state *editor) { - if (editor->filename == NULL) { - editor->filename = editor_prompt(editor, "Save as: %s", NULL); - - if (editor->filename == NULL) - return; - - editor_select_syntax_highlight(editor); - window_set_filename(editor->filename); - } + /* Assume that the editor has already prompted the user for a name */ + if (editor->filename == NULL) + return EINVAL; int length; char *buffer = lines_to_string(editor, &length); int fd = open(editor->filename, O_RDWR | O_CREAT, 0644); - if (fd != -1) { - if (ftruncate(fd, length) != -1) { - if (write(fd, buffer, length) == length) { - close(fd); - free(buffer); - editor_set_status_message(editor, "%d bytes written to disk", length); - editor->dirty = 0; - return; - } - } - close(fd); - } + if (fd == -1) + goto fail; + + if (ftruncate(fd, length) == -1) + goto fail; + + if (write(fd, buffer, length) != length) + goto fail; + + close(fd); + free(buffer); + editor_set_status_message(editor, "%d bytes written to disk", length); + editor->dirty = 0; + + return 0; + +fail: + int saved_errno = errno; + close(fd); free(buffer); - editor_set_status_message(editor, "Failed to write to disk: %s", strerror(errno)); + return saved_errno; } |