21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (!(new_environ = (char **)malloc(envsz))) return 0;<br />

envptr = (char *)new_environ + (sizeof(char *) * (envc + 1));<br />

/* copy the old environment into the new environment, replacing the named<br />

* environment variable if it already exists; otherwise, add it at the end.<br />

*/<br />

for (envc = i = 0; environ[envc]; envc++) {<br />

if (del && !strncmp(environ[envc], name, namelen) &&<br />

environ[envc][namelen] = = '=') continue;<br />

new_environ[i++] = envptr;<br />

if (envc != mod) {<br />

envsz = strlen(environ[envc]);<br />

memcpy(envptr, environ[envc], envsz + 1);<br />

envptr += (envsz + 1);<br />

} else {<br />

memcpy(envptr, name, namelen);<br />

memcpy(envptr + namelen + 1, value, valuelen);<br />

envptr[namelen] = '=';<br />

envptr[namelen + valuelen + 1] = 0;<br />

envptr += (namelen + valuelen + 1 + 1);<br />

}<br />

}<br />

if (mod = = -1) {<br />

new_environ[i++] = envptr;<br />

memcpy(envptr, name, namelen);<br />

memcpy(envptr + namelen + 1, value, valuelen);<br />

envptr[namelen] = '=';<br />

envptr[namelen + valuelen + 1] = 0;<br />

}<br />

new_environ[i] = 0;<br />

/* possibly free the old environment, then replace it with the new one */<br />

if (spc_environ) free(environ);<br />

environ = new_environ;<br />

spc_environ = 1;<br />

return 1;<br />

}<br />

Deleting an environment variable<br />

No method for deleting an environment variable is defined in any standard. Some<br />

implementations of putenv( ) will delete environment variables if the assigned value<br />

is a zero-length string. Other systems provide implementations of a function called<br />

unsetenv( ), but it is nonstandard and thus nonportable.<br />

None of these methods of deleting environment variables take into account the possibility<br />

that multiple occurrences of the same environment variable may exist in the<br />

environment. Usually, only the first occurrence will be deleted, rather than all of<br />

them. The result is that the environment variable won’t actually be deleted because<br />

getenv( ) will return the next occurrence of the environment variable.<br />

Using Environment Variables Securely | 95<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!