summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorattilamolnar <attilamolnar@hush.com>2012-07-09 18:06:08 +0200
committerattilamolnar <attilamolnar@hush.com>2012-07-11 16:34:50 +0200
commit074a96c9ffc85763da833bccadefc9978152482c (patch)
tree76af533d362cc62a348f5beeb3c2a8ab0ceeab8a
parentfa586546fa8b7d17199d08bc7f9814de7aaa3ac7 (diff)
Close files opened with popen() with pclose() instead of fclose()
-rw-r--r--include/configparser.h10
-rw-r--r--src/configparser.cpp4
2 files changed, 10 insertions, 4 deletions
diff --git a/include/configparser.h b/include/configparser.h
index 478899ed9..4b83d26d7 100644
--- a/include/configparser.h
+++ b/include/configparser.h
@@ -61,13 +61,19 @@ struct ParseStack
struct FileWrapper
{
FILE* const f;
- FileWrapper(FILE* file) : f(file) {}
+ bool close_with_pclose;
+ FileWrapper(FILE* file, bool use_pclose = false) : f(file), close_with_pclose(use_pclose) {}
operator bool() { return f; }
operator FILE*() { return f; }
~FileWrapper()
{
if (f)
- fclose(f);
+ {
+ if (close_with_pclose)
+ pclose(f);
+ else
+ fclose(f);
+ }
}
};
diff --git a/src/configparser.cpp b/src/configparser.cpp
index a8e36f6e0..b6e3f7187 100644
--- a/src/configparser.cpp
+++ b/src/configparser.cpp
@@ -307,7 +307,7 @@ void ParseStack::DoReadFile(const std::string& key, const std::string& name, int
if (exec && (flags & FLAG_NO_EXEC))
throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
- FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"));
+ FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"), exec);
if (!file)
throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
@@ -364,7 +364,7 @@ bool ParseStack::ParseExec(const std::string& name, int flags)
/* It's not already included, add it to the list of files we've loaded */
- FileWrapper file(popen(name.c_str(), "r"));
+ FileWrapper file(popen(name.c_str(), "r"), true);
if (!file)
throw CoreException("Could not open executable \"" + name + "\" for include");