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.
23 #include "pbd/strsplit.h"
25 extern char** environ;
30 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
32 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname)
41 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
49 EnvironmentalProtectionAgency::arm ()
55 EnvironmentalProtectionAgency::save ()
59 if (!_envname.empty()) {
61 /* fetch environment from named environment variable, rather than "environ"
64 const char* estr = getenv (_envname.c_str());
70 /* parse line by line, and save into "e"
74 split (estr, lines, '\n');
76 for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) {
79 string::size_type equal = estring.find_first_of ('=');
81 if (equal == string::npos) {
82 /* say what? an environ value without = ? */
86 string before = estring.substr (0, equal);
87 string after = estring.substr (equal+1);
89 e.insert (pair<string,string>(before,after));
94 /* fetch environment from "environ"
97 for (size_t i = 0; environ[i]; ++i) {
99 string estring = environ[i];
100 string::size_type equal = estring.find_first_of ('=');
102 if (equal == string::npos) {
103 /* say what? an environ value without = ? */
107 string before = estring.substr (0, equal);
108 string after = estring.substr (equal+1);
110 e.insert (pair<string,string>(before,after));
115 EnvironmentalProtectionAgency::restore () const
119 for (map<string,string>::const_iterator i = e.begin(); i != e.end(); ++i) {
120 setenv (i->first.c_str(), i->second.c_str(), 1);
125 EnvironmentalProtectionAgency::clear () const
127 char** the_environ = environ;
129 for (size_t i = 0; the_environ[i]; ++i) {
131 string estring = the_environ[i];
132 string::size_type equal = estring.find_first_of ('=');
134 if (equal == string::npos) {
135 /* say what? an environ value without = ? */
139 string before = estring.substr (0, equal);
140 unsetenv(before.c_str());