diff options
author | Michael Vetter <jubalh@iodoru.org> | 2021-11-01 12:24:28 +0100 |
---|---|---|
committer | Michael Vetter <jubalh@iodoru.org> | 2021-11-01 12:24:28 +0100 |
commit | 9a9122c148a8462eb612c7a28ab0f2e3f2f0464e (patch) | |
tree | bfa24ac504dd418acc4d908e932c6f726c698789 | |
parent | 7f5f334cd72bfb407d4c32c376752fadfc9a142e (diff) | |
download | profani-tty-9a9122c148a8462eb612c7a28ab0f2e3f2f0464e.tar.gz |
Cleanup _get_message_from_editor a bit
* Fix `src/command/cmd_funcs.c:9463:9: error: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]` * Free memory earlier and on less places * Check for succesful open() and write()
-rw-r--r-- | src/command/cmd_funcs.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 9256a933..74bbc5e0 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9443,29 +9443,36 @@ _get_message_from_editor(gchar* message, gchar** returned_message) // create editor dir if not present char* jid = connection_get_barejid(); gchar* path = files_get_account_data_path(DIR_EDITOR, jid); + free(jid); if (g_mkdir_with_parents(path, S_IRWXU) != 0) { cons_show_error("Failed to create directory at '%s' with error '%s'", path, strerror(errno)); - free(jid); g_free(path); return TRUE; } + // build temp file name. Example: /home/user/.local/share/profanity/editor/jid/compose.md char* filename = g_strdup_printf("%s/compose.md", path); - free(jid); g_free(path); GError* creation_error = NULL; GFile* file = g_file_new_for_path(filename); GFileOutputStream* fos = g_file_create(file, G_FILE_CREATE_PRIVATE, NULL, &creation_error); + free(filename); + if (message != NULL && strlen(message) > 0) { int fd_output_file = open(g_file_get_path(file), O_WRONLY); - write(fd_output_file, message, strlen(message)); + if (fd_output_file < 0) { + cons_show_error("Editor: Could not open file '%s': %s", file, strerror(errno)); + return TRUE; + } + if (-1 == write(fd_output_file, message, strlen(message))) { + cons_show_error("Editor: failed to write '%s' to file: %s", message, strerror(errno)); + return TRUE; + } close(fd_output_file); } - free(filename); - if (creation_error) { cons_show_error("Editor: could not create temp file"); return TRUE; |