summaryrefslogtreecommitdiffstats
path: root/Settings.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2021-03-12 16:56:06 +0100
committerChristian Göttsche <cgzones@googlemail.com>2021-03-12 16:56:06 +0100
commit521f1343e372e011326e10cf58a9ba1d191b7bdb (patch)
treeaea050326b3d520f96b6e6a1480c9b21229b50d3 /Settings.c
parent350b48e44c45a280520c15762a04940670b6aec9 (diff)
Settings: check if writing configuration file was successful
Writing to the file stream might fail due to a immutable file or a filesystem error. Check the error indicator for the stream and for fclose() failures.
Diffstat (limited to 'Settings.c')
-rw-r--r--Settings.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/Settings.c b/Settings.c
index ab1927de..62fadf07 100644
--- a/Settings.c
+++ b/Settings.c
@@ -7,6 +7,7 @@ in the source distribution for its full text.
#include "Settings.h"
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -280,10 +281,10 @@ static void writeMeterModes(const Settings* this, FILE* fd, int column) {
fprintf(fd, "\n");
}
-bool Settings_write(const Settings* this) {
+int Settings_write(const Settings* this) {
FILE* fd = fopen(this->filename, "w");
if (fd == NULL)
- return false;
+ return -errno;
fprintf(fd, "# Beware! This file is rewritten by htop when settings are changed in the interface.\n");
fprintf(fd, "# The parser is also very primitive, and not human-friendly.\n");
@@ -331,8 +332,10 @@ bool Settings_write(const Settings* this) {
#ifdef HAVE_LIBHWLOC
fprintf(fd, "topology_affinity=%d\n", (int) this->topologyAffinity);
#endif
- fclose(fd);
- return true;
+
+ int r1 = ferror(fd);
+ int r2 = fclose(fd);
+ return r1 ? r1 : r2;
}
Settings* Settings_new(int initialCpuCount) {
@@ -422,7 +425,7 @@ Settings* Settings_new(int initialCpuCount) {
ok = Settings_read(this, legacyDotfile, initialCpuCount);
if (ok) {
// Transition to new location and delete old configuration file
- if (Settings_write(this)) {
+ if (Settings_write(this) == 0) {
unlink(legacyDotfile);
}
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy