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.
24 #include "pbd/strsplit.h"
26 extern char** environ
;
31 EnvironmentalProtectionAgency
* EnvironmentalProtectionAgency::_global_epa
= 0;
33 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm
, const std::string
& envname
)
42 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
50 EnvironmentalProtectionAgency::arm ()
56 EnvironmentalProtectionAgency::save ()
60 if (!_envname
.empty()) {
62 /* fetch environment from named environment variable, rather than "environ"
65 const char* estr
= getenv (_envname
.c_str());
71 /* parse line by line, and save into "e"
75 split (estr
, lines
, '\n');
77 for (vector
<string
>::iterator i
= lines
.begin(); i
!= lines
.end(); ++i
) {
80 string::size_type equal
= estring
.find_first_of ('=');
82 if (equal
== string::npos
) {
83 /* say what? an environ value without = ? */
87 string before
= estring
.substr (0, equal
);
88 string after
= estring
.substr (equal
+1);
90 e
.insert (pair
<string
,string
>(before
,after
));
95 /* fetch environment from "environ"
98 for (size_t i
= 0; environ
[i
]; ++i
) {
100 string estring
= environ
[i
];
101 string::size_type equal
= estring
.find_first_of ('=');
103 if (equal
== string::npos
) {
104 /* say what? an environ value without = ? */
108 string before
= estring
.substr (0, equal
);
109 string after
= estring
.substr (equal
+1);
111 e
.insert (pair
<string
,string
>(before
,after
));
116 EnvironmentalProtectionAgency::restore () const
118 for (map
<string
,string
>::const_iterator i
= e
.begin(); i
!= e
.end(); ++i
) {
119 setenv (i
->first
.c_str(), i
->second
.c_str(), 1);