summaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2014-07-21 22:56:37 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2014-07-22 11:47:18 +0200
commit66ce00153bd35bb83a296f7eb31efec6d6e6768b (patch)
treef3afa343afd79ef7a854b21e15289c43d4537281 /src/io.c
parent21fc7a4b7422f8b441a6266a11cc8e337aae190d (diff)
downloadcalcurse-66ce00153bd35bb83a296f7eb31efec6d6e6768b.zip
Refactor new_tempfile()
Avoid preallocating buffers on the stack, use dynamic memory allocation instead. Also, change the semantics of new_tempfile() so that it returns the full name of the temporary file and fix all call sites. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/src/io.c b/src/io.c
index 70f0ede..5bdba77 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1135,25 +1135,32 @@ void io_import_data(enum import_type type, const char *stream_name)
struct io_file *io_log_init(void)
{
char *logprefix, *logname;
- struct io_file *log;
+ struct io_file *log = mem_malloc(sizeof(struct io_file));
- asprintf(&logprefix, "%s/calcurse_log.", get_tempdir());
- logname = new_tempfile(logprefix, TMPEXTSIZ);
- RETVAL_IF(logname == NULL, 0,
- _("Warning: could not create temporary log file, Aborting..."));
- log = mem_malloc(sizeof(struct io_file));
- RETVAL_IF(log == NULL, 0,
- _("Warning: could not open temporary log file, Aborting..."));
- snprintf(log->name, sizeof(log->name), "%s%s", logprefix, logname);
- mem_free(logprefix);
- mem_free(logname);
+ if (!log) {
+ ERROR_MSG(_("Warning: could not open temporary log file, Aborting..."));
+ return NULL;
+ }
+ asprintf(&logprefix, "%s/calcurse_log", get_tempdir());
+ logname = new_tempfile(logprefix);
+ if (!logname) {
+ ERROR_MSG(_("Warning: could not create temporary log file, Aborting..."));
+ goto error;
+ }
+ strncpy(log->name, logname, sizeof(log->name));
log->fd = fopen(log->name, "w");
if (log->fd == NULL) {
ERROR_MSG(_("Warning: could not open temporary log file, Aborting..."));
- mem_free(log);
- return 0;
+ goto error;
}
+ goto cleanup;
+error:
+ mem_free(log);
+ log = NULL;
+cleanup:
+ mem_free(logprefix);
+ mem_free(logname);
return log;
}