env.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define APR_WANT_STRFUNC
  17. #include "fspr_want.h"
  18. #include "fspr.h"
  19. #include "fspr_private.h"
  20. #include "fspr_env.h"
  21. #if APR_HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #if APR_HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. APR_DECLARE(fspr_status_t) fspr_env_get(char **value,
  28. const char *envvar,
  29. fspr_pool_t *pool)
  30. {
  31. #ifdef HAVE_GETENV
  32. char *val = getenv(envvar);
  33. if (!val)
  34. return APR_ENOENT;
  35. *value = val;
  36. return APR_SUCCESS;
  37. #else
  38. return APR_ENOTIMPL;
  39. #endif
  40. }
  41. APR_DECLARE(fspr_status_t) fspr_env_set(const char *envvar,
  42. const char *value,
  43. fspr_pool_t *pool)
  44. {
  45. #if defined(HAVE_SETENV)
  46. if (0 > setenv(envvar, value, 1))
  47. return APR_ENOMEM;
  48. return APR_SUCCESS;
  49. #elif defined(HAVE_PUTENV)
  50. fspr_size_t elen = strlen(envvar);
  51. fspr_size_t vlen = strlen(value);
  52. char *env = fspr_palloc(pool, elen + vlen + 2);
  53. char *p = env + elen;
  54. memcpy(env, envvar, elen);
  55. *p++ = '=';
  56. memcpy(p, value, vlen);
  57. p[vlen] = '\0';
  58. if (0 > putenv(env))
  59. return APR_ENOMEM;
  60. return APR_SUCCESS;
  61. #else
  62. return APR_ENOTIMPL;
  63. #endif
  64. }
  65. APR_DECLARE(fspr_status_t) fspr_env_delete(const char *envvar, fspr_pool_t *pool)
  66. {
  67. #ifdef HAVE_UNSETENV
  68. unsetenv(envvar);
  69. return APR_SUCCESS;
  70. #else
  71. /* hint: some platforms allow envvars to be unset via
  72. * putenv("varname")... that isn't Single Unix spec,
  73. * but if your platform doesn't have unsetenv() it is
  74. * worth investigating and potentially adding a
  75. * configure check to decide when to use that form of
  76. * putenv() here
  77. */
  78. return APR_ENOTIMPL;
  79. #endif
  80. }