E-MailRelay

genvironment_unix.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
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 3 of the License, or
7// (at your option) any later version.
8//
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.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file genvironment_unix.cpp
19///
20
21#include "gdef.h"
22#include "genvironment.h"
23#include <cstdlib> // std::getenv()
24#include <cstring>
25#include <stdexcept>
26
27 namespace G
28{
29 namespace EnvironmentUnixImp
30 {
31 char * stringdup( const std::string & ) ;
32 }
33}
34
35std::string G::Environment::get( const std::string & name , const std::string & default_ )
36{
37 const char * p = std::getenv( name.c_str() ) ;
38 return p ? std::string(p) : default_ ;
39}
40
41#ifndef G_LIB_SMALL
42 G::Path G::Environment::getPath( const std::string & name , const G::Path & default_ )
43{
44 const char * p = std::getenv( name.c_str() ) ;
45 return p ? G::Path(p) : default_ ;
46}
47#endif
48
49 char * G::EnvironmentUnixImp::stringdup( const std::string & s )
50{
51 void * p = std::memcpy( new char[s.size()+1U] , s.c_str() , s.size()+1U ) ; // NOLINT
52 return static_cast<char*>(p) ;
53}
54
55 void G::Environment::put( const std::string & name , const std::string & value )
56{
57 // see man putenv(3) NOTES
58 namespace imp = EnvironmentUnixImp ;
59 char * deliberately_leaky_copy = imp::stringdup( std::string().append(name).append(1U,'=').append(value) ) ; // NOLINT
60 ::putenv( deliberately_leaky_copy ) ;
61} // NOLINT
62
63 G::Environment G::Environment::minimal( bool sbin )
64{
65 std::string path = sbin ? "/usr/bin:/bin:/usr/sbin:/sbin" : "/usr/bin:/bin" ; // no "."
66 return Environment( {{"PATH",path},{"IFS"," \t\n"}} ) ;
67}
68
G::Environment
Holds a set of environment variables and also provides static methods to wrap getenv() and putenv().
Definition: genvironment.h:42
G::Environment::put
static void put(const std::string &name, const std::string &value)
Sets the environment variable value.
Definition: genvironment_unix.cpp:55
G::Environment::get
static std::string get(const std::string &name, const std::string &default_)
Returns the environment variable value or the given default.
Definition: genvironment_unix.cpp:35
G::Environment::getPath
static G::Path getPath(const std::string &name, const G::Path &={})
Returns the environment variable value as a G::Path object.
Definition: genvironment_unix.cpp:42
G::Environment::minimal
static Environment minimal(bool sbin=false)
Returns a minimal, safe set of environment variables.
Definition: genvironment_unix.cpp:63
G::Path
A Path object represents a file system path.
Definition: gpath.h:82
genvironment.h
G
Low-level classes.
Definition: garg.h:36