env.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_arch_misc.h"
  20. #include "fspr_arch_utf8.h"
  21. #include "fspr_env.h"
  22. #include "fspr_errno.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_strings.h"
  25. #if APR_HAS_UNICODE_FS
  26. static fspr_status_t widen_envvar_name (fspr_wchar_t *buffer,
  27. fspr_size_t bufflen,
  28. const char *envvar)
  29. {
  30. fspr_size_t inchars;
  31. fspr_status_t status;
  32. inchars = strlen(envvar) + 1;
  33. status = fspr_conv_utf8_to_ucs2(envvar, &inchars, buffer, &bufflen);
  34. if (status == APR_INCOMPLETE)
  35. status = APR_ENAMETOOLONG;
  36. return status;
  37. }
  38. #endif
  39. APR_DECLARE(fspr_status_t) fspr_env_get(char **value,
  40. const char *envvar,
  41. fspr_pool_t *pool)
  42. {
  43. char *val = NULL;
  44. DWORD size;
  45. #if APR_HAS_UNICODE_FS
  46. IF_WIN_OS_IS_UNICODE
  47. {
  48. fspr_wchar_t wenvvar[APR_PATH_MAX];
  49. fspr_size_t inchars, outchars;
  50. fspr_wchar_t *wvalue, dummy;
  51. fspr_status_t status;
  52. status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
  53. if (status)
  54. return status;
  55. SetLastError(0);
  56. size = GetEnvironmentVariableW(wenvvar, &dummy, 0);
  57. if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
  58. /* The environment variable doesn't exist. */
  59. return APR_ENOENT;
  60. if (size == 0) {
  61. /* The environment value exists, but is zero-length. */
  62. *value = fspr_pstrdup(pool, "");
  63. return APR_SUCCESS;
  64. }
  65. wvalue = fspr_palloc(pool, size * sizeof(*wvalue));
  66. size = GetEnvironmentVariableW(wenvvar, wvalue, size);
  67. if (size == 0)
  68. /* Mid-air collision?. Somebody must've changed the env. var. */
  69. return APR_INCOMPLETE;
  70. inchars = wcslen(wvalue) + 1;
  71. outchars = 3 * inchars; /* Enougn for any UTF-8 representation */
  72. val = fspr_palloc(pool, outchars);
  73. status = fspr_conv_ucs2_to_utf8(wvalue, &inchars, val, &outchars);
  74. if (status)
  75. return status;
  76. }
  77. #endif
  78. #if APR_HAS_ANSI_FS
  79. ELSE_WIN_OS_IS_ANSI
  80. {
  81. char dummy;
  82. SetLastError(0);
  83. size = GetEnvironmentVariableA(envvar, &dummy, 0);
  84. if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
  85. /* The environment variable doesn't exist. */
  86. return APR_ENOENT;
  87. if (size == 0) {
  88. /* The environment value exists, but is zero-length. */
  89. *value = fspr_pstrdup(pool, "");
  90. return APR_SUCCESS;
  91. }
  92. val = fspr_palloc(pool, size);
  93. size = GetEnvironmentVariableA(envvar, val, size);
  94. if (size == 0)
  95. /* Mid-air collision?. Somebody must've changed the env. var. */
  96. return APR_INCOMPLETE;
  97. }
  98. #endif
  99. *value = val;
  100. return APR_SUCCESS;
  101. }
  102. APR_DECLARE(fspr_status_t) fspr_env_set(const char *envvar,
  103. const char *value,
  104. fspr_pool_t *pool)
  105. {
  106. #if APR_HAS_UNICODE_FS
  107. IF_WIN_OS_IS_UNICODE
  108. {
  109. fspr_wchar_t wenvvar[APR_PATH_MAX];
  110. fspr_wchar_t *wvalue;
  111. fspr_size_t inchars, outchars;
  112. fspr_status_t status;
  113. status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
  114. if (status)
  115. return status;
  116. outchars = inchars = strlen(value) + 1;
  117. wvalue = fspr_palloc(pool, outchars * sizeof(*wvalue));
  118. status = fspr_conv_utf8_to_ucs2(value, &inchars, wvalue, &outchars);
  119. if (status)
  120. return status;
  121. if (!SetEnvironmentVariableW(wenvvar, wvalue))
  122. return fspr_get_os_error();
  123. }
  124. #endif
  125. #if APR_HAS_ANSI_FS
  126. ELSE_WIN_OS_IS_ANSI
  127. {
  128. if (!SetEnvironmentVariableA(envvar, value))
  129. return fspr_get_os_error();
  130. }
  131. #endif
  132. return APR_SUCCESS;
  133. }
  134. APR_DECLARE(fspr_status_t) fspr_env_delete(const char *envvar, fspr_pool_t *pool)
  135. {
  136. #if APR_HAS_UNICODE_FS
  137. IF_WIN_OS_IS_UNICODE
  138. {
  139. fspr_wchar_t wenvvar[APR_PATH_MAX];
  140. fspr_status_t status;
  141. status = widen_envvar_name(wenvvar, APR_PATH_MAX, envvar);
  142. if (status)
  143. return status;
  144. if (!SetEnvironmentVariableW(wenvvar, NULL))
  145. return fspr_get_os_error();
  146. }
  147. #endif
  148. #if APR_HAS_ANSI_FS
  149. ELSE_WIN_OS_IS_ANSI
  150. {
  151. if (!SetEnvironmentVariableA(envvar, NULL))
  152. return fspr_get_os_error();
  153. }
  154. #endif
  155. return APR_SUCCESS;
  156. }