| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include "ppport.h" |
| 5 |
|
| |
| 6 |
|
| #include <stdlib.h> /* setenv/getenv */ |
| 7 |
|
| #include <stdio.h> /* sprintf */ |
| 8 |
|
| |
| 9 |
|
| /* configure-less detection of unsetenv for solaris */ |
| 10 |
|
| #if defined(sun) |
| 11 |
|
| # if defined(__EXTENSIONS__) ||\ |
| 12 |
|
| (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ |
| 13 |
|
| defined(_XPG6) |
| 14 |
|
| # define HAVE_UNSETENV 1 |
| 15 |
|
| # define HAVE_SETENV 1 |
| 16 |
|
| # endif |
| 17 |
|
| #endif |
| 18 |
|
| |
| 19 |
|
| #ifndef HAVE_UNSETENV |
| 20 |
|
| # if !defined(sun) && !defined(_AIX) |
| 21 |
|
| # define HAVE_UNSETENV 1 |
| 22 |
|
| # endif |
| 23 |
|
| #endif |
| 24 |
|
| #ifndef HAVE_SETENV |
| 25 |
|
| # if !defined(WIN32) && !defined(sun) |
| 26 |
|
| # define HAVE_SETENV 1 |
| 27 |
|
| # endif |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| /* in order to work around system and perl implementation bugs/leaks, we need |
| 31 |
|
| * to sometimes force PERL_USE_SAFE_PUTENV mode. |
| 32 |
|
| */ |
| 33 |
|
| #ifndef PERL_USE_SAFE_PUTENV |
| 34 |
|
| /* Threaded perl with PERL_TRACK_MEMPOOL enabled causes |
| 35 |
|
| * "panic: free from wrong pool at exit" |
| 36 |
|
| * starting at 5.9.4 (confirmed through 5.20.1) |
| 37 |
|
| * see: https://rt.cpan.org/Ticket/Display.html?id=99962 |
| 38 |
|
| */ |
| 39 |
|
| # if PERL_BCDVERSION >= 0x5009004 && defined(USE_ITHREADS) && defined(PERL_TRACK_MEMPOOL) |
| 40 |
|
| # define USE_SAFE_PUTENV 1 |
| 41 |
|
| # elif PERL_BCDVERSION >= 0x5008000 && PERL_BCDVERSION < 0x5019006 |
| 42 |
|
| /* FreeBSD: SIGV at exit on perls prior to 5.19.6 |
| 43 |
|
| * see: https://rt.cpan.org/Ticket/Display.html?id=49872 |
| 44 |
|
| */ |
| 45 |
|
| # if defined(__FreeBSD__) |
| 46 |
|
| # define USE_SAFE_PUTENV 1 |
| 47 |
|
| # endif |
| 48 |
|
| # endif |
| 49 |
|
| #endif |
| 50 |
|
| |
| 51 |
|
| MODULE = Env::C PACKAGE = Env::C PREFIX = env_c_ |
| 52 |
|
| |
| 53 |
|
| char * |
| 54 |
|
| env_c_getenv(key) |
| 55 |
|
| char *key |
| 56 |
|
| |
| 57 |
|
| CODE: |
| 58 |
6 |
| RETVAL = getenv(key); |
| 59 |
|
| |
| 60 |
|
| OUTPUT: |
| 61 |
|
| RETVAL |
| 62 |
|
| |
| 63 |
|
| MODULE = Env::C PACKAGE = Env::C PREFIX = env_c_ |
| 64 |
|
| |
| 65 |
|
| int |
| 66 |
|
| env_c_setenv(key, val, override=1) |
| 67 |
|
| char *key |
| 68 |
|
| char *val |
| 69 |
|
| int override; |
| 70 |
|
| |
| 71 |
|
| |
| 72 |
|
| CODE: |
| 73 |
|
| #if !HAVE_SETENV |
| 74 |
|
| if (override || getenv(key) == NULL) { |
| 75 |
|
| char *old_env = getenv( key ); |
| 76 |
|
| char *buff = malloc(strlen(key) + strlen(val) + 2); |
| 77 |
|
| if (buff != NULL) { |
| 78 |
|
| sprintf(buff, "%s=%s", key, val); |
| 79 |
|
| #ifdef WIN32 |
| 80 |
|
| RETVAL = _putenv(buff); |
| 81 |
|
| free(buff); |
| 82 |
|
| #else |
| 83 |
|
| RETVAL = putenv(buff); |
| 84 |
|
| if (old_env == NULL) { |
| 85 |
|
| free(old_env); |
| 86 |
|
| } |
| 87 |
|
| #endif |
| 88 |
|
| } |
| 89 |
|
| else { |
| 90 |
|
| RETVAL = -1; |
| 91 |
|
| } |
| 92 |
|
| } |
| 93 |
|
| else { |
| 94 |
|
| RETVAL = -1; |
| 95 |
|
| } |
| 96 |
|
| #else |
| 97 |
|
| # ifdef USE_SAFE_PUTENV |
| 98 |
|
| PL_use_safe_putenv = 1; |
| 99 |
|
| # endif |
| 100 |
4 |
| RETVAL = setenv(key, val, override); |
| 101 |
|
| #endif |
| 102 |
|
| |
| 103 |
|
| OUTPUT: |
| 104 |
|
| RETVAL |
| 105 |
|
| |
| 106 |
|
| MODULE = Env::C PACKAGE = Env::C PREFIX = env_c_ |
| 107 |
|
| |
| 108 |
|
| void |
| 109 |
|
| env_c_unsetenv(key) |
| 110 |
|
| char *key |
| 111 |
|
| |
| 112 |
|
| PREINIT: |
| 113 |
|
| #ifdef WIN32 |
| 114 |
|
| char *buff; |
| 115 |
|
| #endif |
| 116 |
|
| #if defined( sun ) || defined( _AIX ) |
| 117 |
|
| int key_len; |
| 118 |
|
| extern char **environ; |
| 119 |
|
| char **envp; |
| 120 |
|
| #endif |
| 121 |
|
| |
| 122 |
|
| CODE: |
| 123 |
|
| #ifdef WIN32 |
| 124 |
|
| buff = malloc(strlen(key) + 2); |
| 125 |
|
| sprintf(buff, "%s=", key); |
| 126 |
|
| _putenv(buff); |
| 127 |
|
| free(buff); |
| 128 |
|
| #else |
| 129 |
|
| #if HAVE_UNSETENV |
| 130 |
1 |
| unsetenv(key); |
| 131 |
|
| #else |
| 132 |
|
| key_len = strlen(key); |
| 133 |
|
| for (envp = environ; *envp != NULL; envp++) { |
| 134 |
|
| if (strncmp(key, *envp, key_len) == 0 && |
| 135 |
|
| (*envp)[key_len] == '=') { |
| 136 |
|
| free(*envp); |
| 137 |
|
| do { |
| 138 |
|
| envp[0] = envp[1]; |
| 139 |
|
| } while (*envp++); |
| 140 |
|
| break; |
| 141 |
|
| } |
| 142 |
|
| } |
| 143 |
|
| #endif |
| 144 |
|
| #endif |
| 145 |
|
| |
| 146 |
|
| MODULE = Env::C PACKAGE = Env::C PREFIX = env_c_ |
| 147 |
|
| |
| 148 |
|
| AV* |
| 149 |
|
| env_c_getallenv() |
| 150 |
|
| |
| 151 |
|
| PREINIT: |
| 152 |
1 |
| int i = 0; |
| 153 |
|
| char *p; |
| 154 |
1 |
| AV *av = Nullav; |
| 155 |
|
| #ifndef __BORLANDC__ |
| 156 |
|
| extern char **environ; |
| 157 |
|
| #endif |
| 158 |
|
| |
| 159 |
|
| CODE: |
| 160 |
1 |
| RETVAL = newAV(); |
| 161 |
|
| |
| 162 |
32 |
▶ | while ((char*)environ[i] != '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((char*)environ[i] != '\0') { |
31 |
1 |
32 |
|
| 163 |
31 |
| Perl_av_push(aTHX_ RETVAL, newSVpv((char*)environ[i++], 0)); |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| OUTPUT: |
| 167 |
|
| RETVAL |
| 168 |
|
| |
| 169 |
|
| MODULE = Env::C PACKAGE = Env::C PREFIX = env_c_ |
| 170 |
|
| |
| 171 |
|
| # this is for leak.t, which needs to know if PERL_USE_SAFE_PUTENV is in |
| 172 |
|
| # effect |
| 173 |
|
| int |
| 174 |
|
| env_c_using_safe_putenv() |
| 175 |
|
| CODE: |
| 176 |
|
| #if defined(PERL_USE_SAFE_PUTENV) || defined(USE_SAFE_PUTENV) |
| 177 |
1 |
▶ | RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; |
0 |
1 |
1 |
|
| 178 |
|
| #else |
| 179 |
|
| RETVAL = 0; |
| 180 |
|
| #endif |
| 181 |
|
| |
| 182 |
|
| OUTPUT: |
| 183 |
|
| RETVAL |