summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-24 21:06:27 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-05 20:00:55 -0400
commit245802dd89ccf10de446faff5577e041d5372062 (patch)
tree30ffc96cbcfa09331168254df4b92f9fc5f558d7 /src
parentc84a94883161073239c35d181e25823ff0454f68 (diff)
Use _cleanup_ when reading config files
Diffstat (limited to 'src')
-rw-r--r--src/core/main.c4
-rw-r--r--src/shared/conf-parser.c44
2 files changed, 14 insertions, 34 deletions
diff --git a/src/core/main.c b/src/core/main.c
index 4d21dd97a3..921476a042 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -704,7 +704,7 @@ static int parse_config_file(void) {
{ NULL, NULL, NULL, 0, NULL }
};
- FILE *f;
+ FILE _cleanup_fclose_ *f;
const char *fn;
int r;
@@ -722,8 +722,6 @@ static int parse_config_file(void) {
if (r < 0)
log_warning("Failed to parse configuration file: %s", strerror(-r));
- fclose(f);
-
return 0;
}
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index a87df382ea..fbacf986a5 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -262,23 +262,19 @@ int config_parse(
void *userdata) {
unsigned line = 0;
- char *section = NULL;
+ char _cleanup_free_ *section = NULL, *continuation = NULL;
+ FILE _cleanup_fclose_ *ours = NULL;
int r;
- bool ours = false;
- char *continuation = NULL;
assert(filename);
assert(lookup);
if (!f) {
- f = fopen(filename, "re");
+ f = ours = fopen(filename, "re");
if (!f) {
- r = -errno;
- log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
- goto finish;
+ log_error("Failed to open configuration file '%s': %m", filename);
+ return -errno;
}
-
- ours = true;
}
while (!feof(f)) {
@@ -289,19 +285,16 @@ int config_parse(
if (feof(f))
break;
- r = -errno;
- log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
- goto finish;
+ log_error("Failed to read configuration file '%s': %m", filename);
+ return -errno;
}
truncate_nl(l);
if (continuation) {
c = strappend(continuation, l);
- if (!c) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!c)
+ return -ENOMEM;
free(continuation);
continuation = NULL;
@@ -323,10 +316,8 @@ int config_parse(
continuation = c;
else {
continuation = strdup(l);
- if (!continuation) {
- r = -ENOMEM;
- goto finish;
- }
+ if (!continuation)
+ return -ENOMEM;
}
continue;
@@ -344,19 +335,10 @@ int config_parse(
free(c);
if (r < 0)
- goto finish;
+ return r;
}
- r = 0;
-
-finish:
- free(section);
- free(continuation);
-
- if (f && ours)
- fclose(f);
-
- return r;
+ return 0;
}
int config_parse_int(