env-util.c revision 6086c1babba050999c2945edada676a1106554cc
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/*
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen env-util.c : Environment variable handling
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen
46552a931924c2d743f045e95b08c3ce6beda91aTimo Sirainen Copyright (c) 2002 Timo Sirainen
ce1a6c9b82117d253df9acd77e54ac84dd8a247eTimo Sirainen
c5f932968281763df360b9c97cef60f5f80d5e3dTimo Sirainen Permission is hereby granted, free of charge, to any person obtaining
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen a copy of this software and associated documentation files (the
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen "Software"), to deal in the Software without restriction, including
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen without limitation the rights to use, copy, modify, merge, publish,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen distribute, sublicense, and/or sell copies of the Software, and to
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen permit persons to whom the Software is furnished to do so, subject to
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen the following conditions:
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
de754cb78f75e8b3b994cddafe41c9ed1467c33dTimo Sirainen The above copyright notice and this permission notice shall be
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen included in all copies or substantial portions of the Software.
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
53dfcefa9440a49d703e49193819a79be99c9ba6Timo Sirainen OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
f0d93763f210ecdb85a115fdd0210a16cfc5ff5cTimo Sirainen MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
53dfcefa9440a49d703e49193819a79be99c9ba6Timo Sirainen IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen*/
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch#include "lib.h"
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch#include "env-util.h"
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch#include <stdlib.h>
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Boschstatic pool_t pool = NULL;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Boschvoid env_put(const char *env)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch{
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch if (pool == NULL)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch pool = pool_alloconly_create("Environment", 1024);
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch if (putenv(p_strdup(pool, env)) != 0)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch i_fatal("Environment full, can't add: %s", env);
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch}
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Boschvoid env_remove(const char *env)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch{
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch extern char **environ;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch size_t len;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch if (environ == NULL)
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch return;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch len = strlen(env);
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch for (; *environ != NULL; environ++) {
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch if (strncmp(*environ, env, len) == 0 &&
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch (*environ)[len] == '=') {
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch char **p;
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch
36b072d84a9076c3c483bf710444a716e987ccc3Stephan Bosch for (p = environ; *p != NULL; p++)
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen p[0] = p[1];
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainen}
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid env_clean(void)
d1e7425048c61d71f41f737ba947687198842dc2Timo Sirainen{
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen extern char **environ;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen if (environ != NULL)
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen *environ = NULL;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen if (pool != NULL) {
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen pool_unref(pool);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen pool = NULL;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen }
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen}
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen