2 * \file environment.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author João Luis M. Assirati
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "support/environment.h"
17 #include "support/docstring.h"
18 #include "support/lstrings.h"
19 #include "support/os.h"
21 #include <algorithm> // for remove
32 bool hasEnv(string const & name)
34 return getenv(name.c_str());
38 string const getEnv(string const & name)
40 // f.ex. what about error checking?
41 char const * const ch = getenv(name.c_str());
42 return ch ? to_utf8(from_local8bit(ch)) : string();
46 vector<string> const getEnvPath(string const & name)
48 string const env_var = getEnv(name);
49 string const separator(1, os::path_separator());
51 return getVectorFromString(env_var, separator);
55 bool setEnv(string const & name, string const & value)
57 // CHECK Look at and fix this.
58 // f.ex. what about error checking?
62 encoded = to_local8bit(from_utf8(value));
67 #if defined (HAVE_SETENV)
68 return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
69 #elif defined (HAVE_PUTENV)
70 // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
71 // the argument of putenv() needs to be static, because changing its
72 // value will change the environment. Therefore we need a different static
73 // storage for each variable.
75 static map<string, string> varmap;
76 varmap[name] = name + '=' + encoded;
77 return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
79 #error No environment-setting function has been defined.
85 void setEnvPath(string const & name, vector<string> const & env)
87 char const separator(os::path_separator());
89 vector<string>::const_iterator const begin = env.begin();
90 vector<string>::const_iterator const end = env.end();
91 vector<string>::const_iterator it = begin;
92 for (; it != end; ++it) {
95 ss << os::external_path(*it);
97 setEnv(name, ss.str());
101 void prependEnvPath(string const & name, string const & prefix)
103 string const separator(1, os::path_separator());
104 vector<string> reversed_tokens
105 = getVectorFromString(prefix, separator);
106 vector<string> env_var = getEnvPath(name);
108 // Prepend each new element to the list, removing identical elements
109 // that occur later in the list.
110 typedef vector<string>::const_reverse_iterator token_iterator;
111 token_iterator it = reversed_tokens.rbegin();
112 token_iterator const end = reversed_tokens.rend();
113 for (; it != end; ++it) {
114 vector<string>::iterator remove_it =
115 remove(env_var.begin(), env_var.end(), *it);
116 env_var.erase(remove_it, env_var.end());
117 env_var.insert(env_var.begin(), *it);
120 setEnvPath(name, env_var);
124 bool unsetEnv(string const & name)
126 #if defined(HAVE_UNSETENV)
127 // FIXME: does it leak?
128 return ::unsetenv(name.c_str()) == 0;
129 #elif defined(HAVE_PUTENV)
130 // This is OK with MSVC and MinGW at least.
131 // The argument of putenv() does not need to be a static variable in this
132 // case, since the variable is removed from the environment.
133 return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
135 #error No environment-unsetting function has been defined.
140 } // namespace support