2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "pbd/strsplit.h"
28 #define environ _environ
29 _CRTIMP extern char ** _environ;
31 extern char** environ;
37 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
39 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname)
48 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
56 EnvironmentalProtectionAgency::arm ()
62 EnvironmentalProtectionAgency::save ()
66 if (!_envname.empty()) {
68 /* fetch environment from named environment variable, rather than "environ"
71 const char* estr = g_getenv (_envname.c_str());
77 /* parse line by line, and save into "e"
81 split (estr, lines, '\n');
83 for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) {
86 string::size_type equal = estring.find_first_of ('=');
88 if (equal == string::npos) {
89 /* say what? an environ value without = ? */
93 string before = estring.substr (0, equal);
94 string after = estring.substr (equal+1);
96 e.insert (pair<string,string>(before,after));
101 /* fetch environment from "environ"
104 for (size_t i = 0; environ[i]; ++i) {
106 string estring = environ[i];
107 string::size_type equal = estring.find_first_of ('=');
109 if (equal == string::npos) {
110 /* say what? an environ value without = ? */
114 string before = estring.substr (0, equal);
115 string after = estring.substr (equal+1);
117 e.insert (pair<string,string>(before,after));
122 EnvironmentalProtectionAgency::restore () const
126 for (map<string,string>::const_iterator i = e.begin(); i != e.end(); ++i) {
127 g_setenv (i->first.c_str(), i->second.c_str(), 1);
132 EnvironmentalProtectionAgency::clear () const
134 char** the_environ = environ;
136 for (size_t i = 0; the_environ[i]; ++i) {
138 string estring = the_environ[i];
139 string::size_type equal = estring.find_first_of ('=');
141 if (equal == string::npos) {
142 /* say what? an environ value without = ? */
146 string before = estring.substr (0, equal);
147 g_unsetenv(before.c_str());