]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Close files opened with popen() with pclose() instead of fclose()
authorattilamolnar <attilamolnar@hush.com>
Mon, 9 Jul 2012 16:06:08 +0000 (18:06 +0200)
committerattilamolnar <attilamolnar@hush.com>
Wed, 11 Jul 2012 14:34:50 +0000 (16:34 +0200)
include/configparser.h
src/configparser.cpp

index 478899ed92f203481515541e6df68d04552af6ee..4b83d26d7e01dfceb37a879649f17259710aaa6a 100644 (file)
@@ -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);
+               }
        }
 };
 
index a8e36f6e0048c1c1c2a91a719ee81d575dcccbcb..b6e3f7187e2814e7276056de0d5898eec8633469 100644 (file)
@@ -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");