473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

getenv returns same string as putenv?

Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?

Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.

The following code demponstrates that with at least glibc the same string
is in fact returned.

unsigned char *s1, *s2;

s2 = "SOMEVAR=whatever";
s1 = malloc(strlen(s2) + 1);
strcpy(s1, s2);

if (putenv(s1) == -1) {
PMNO(errno);
return -1;
}

s2 = getenv("SOMEVAR");

printf("s1=%p,s2 - 8=%p\n", s1, s2 - 8);

free(s2 - 8);

output: s1=0x9925828,s2 - 8=0x9925828

Mike

Dec 11 '06 #1
8 3200
Michael B Allen wrote:
Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?
There is no `putenv`, at least not in Standard C.

(fx:ot)

I see from `man putenv` on my Linux box that `putenv` is supposed there to
conform to SVID 3, POSIX, 4.3BSD. So it looks like you will have to
appeal to implementation-specific documentation or newsgroups.

That same man page says

int putenv(char *string);
....
The string pointed to by string becomes part of the environment,
so altering the string changes the environment.

(fx:bot)

Can you redesign to eliminate this use of `putenv` and eliminate the
problem? That's what I'd [try to] do.

--
Chris "Perikles triumphant" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

Dec 11 '06 #2
Michael B Allen wrote:
Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?
No, because there is no putenv() in the Standard C library.
As far as C is concerned, the environment is read-only.
Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.
... and this may be one of the reasons the Standard doesn't
define putenv(): deciding who manages the memory could turn out
to be troublesome. Should putenv() copy the provided string,
or should it require that the string continue to exist? If it
copies, what should it do if unable to allocate memory to hold
the string (in particular, can it free() a prior value and then
fail to set the new one, or must it guarantee all-or-nothing)?

Questions like this can, of course, be settled, even if by
arbitrary choice. However, the Committee chose not to settle
it, writing (in the Rationale)

A corresponding putenv function was omitted from the
Standard, since its utility outside a multi-process
environment is questionable, and since its definition
is properly the domain of an operating system standard.

It's possible that this is face-saving language for "We couldn't
find a way to reconcile the conflicting behaviors of different
existing putenv() implementations, so we punted." But face-saving
or face-value, that's the state of affairs: There's no putenv() in
the C library, and systems that provide it as an extension have
defined their extensions in somewhat different ways.

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 11 '06 #3
Michael B Allen <mb*****@ioplex.comwrites:
Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?
No: the implementation is allowed to reuse a static buffer on
each call to getenv. (I am assuming that "putenv" is the "method
for altering the environment list" that the Standard says is
implementation-defined.)
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Dec 11 '06 #4
On Mon, 11 Dec 2006 16:43:08 +0000, Chris Dollin wrote:
Can you redesign to eliminate this use of `putenv` and eliminate the
problem? That's what I'd [try to] do.
Yeah, on second thought, even if getenv was guaranteed to return what
was supplied to putenv there would be no way to guarantee that putenv
was not called by code outside of my scope and therefore freeing the
result of getenv is inherently flawed.

I'll have to wrap them and use a global to ensure it doesn't leak.

Thanks,
Mike

Dec 11 '06 #5
Eric Sosman <es*****@acm-dot-org.invalidwrote:
Questions like this can, of course, be settled, even if by
arbitrary choice. However, the Committee chose not to settle
it, writing (in the Rationale)

A corresponding putenv function was omitted from the
Standard, since its utility outside a multi-process
environment is questionable, and since its definition
is properly the domain of an operating system standard.

It's possible that this is face-saving language for "We couldn't
find a way to reconcile the conflicting behaviors of different
existing putenv() implementations, so we punted."
Possibly more like "We couldn't find a way to reconcile the conflicting
behaviours of different OSes, so we were forced to punt".

Richard
Dec 12 '06 #6
On Mon, 11 Dec 2006 11:31:59 -0500, Michael B Allen wrote:
Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?

Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.
Just use setenv() and free immediately, it has sane semantics.

http://www.opengroup.org/onlinepubs/...ns/setenv.html

--
James Antill -- ja***@and.org
http://www.and.org/and-httpd/ -- $2,000 security guarantee
http://www.and.org/vstr/
Dec 12 '06 #7
James Antill wrote:
On Mon, 11 Dec 2006 11:31:59 -0500, Michael B Allen wrote:
>Is the string returned by getenv guaranteed to be the same string
supplied to putenv plus the offset of the variable name and equals
sign?

Because of API constraints I do not want to save a pointer to the
string passed to putenv but I need to be able to free it later or
I will have a memory leak.

Just use setenv() and free immediately, it has sane semantics.

http://www.opengroup.org/onlinepubs/...ns/setenv.html
There is no such routine in standard C. Not portable, thus OT.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 12 '06 #8
James Antill wrote:
On Mon, 11 Dec 2006 11:31:59 -0500, Michael B Allen wrote:
>Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?

Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.

Just use setenv() and free immediately, it has sane semantics.
There is no `setenv` in Standard C.

--
Chris "Perikles triumphant" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/

Dec 13 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JDJones | last post by:
I am trying to write a PHP action into my custom 404 error page that will email me when someone tries to access a bad link. I can get it to work and tell me the person's IP using <$ip = getenv...
3
by: Warren Oates | last post by:
I've been using putenv() to change the timezone in a script (my server is in CST, me in EST). My reading of the docs suggests that this changes the time zone environment variable for _the server_...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
0
by: Xavier | last post by:
Greetings, While messing around with the "dl" module I ran into a segfault. *DO NOTE THAT THE FOLLOWING OCCURED ON 2 OF MY LINUX WORKSTATIONS* ------ # python -c 'import dl;...
5
by: Chad Paquette | last post by:
Hi, We have a legacy cgi app that's written in C. We are encountering an error when we try to retrieve a cgi environment variable. The variable we are getting contains a Base64 encoded...
10
by: Protoman | last post by:
What does getenv() do and why would you use it?
17
by: Protoman | last post by:
Why does getenv work backwards? When I'm testing an env variable, I have to test if its NOT equal to, rather than equal to, to get the desired result. As evidenced by this simple piece of code: ...
2
by: silrandir | last post by:
When calling getenv() globally, the function returns NULL, yet when called from main, returns an appropriate value. #include <stdlib.h> #include <stdio.h> #include <unistd.h> //using...
4
by: Yogi Watcher | last post by:
Hi, Recently I have observed some odd behavior of getenv and putenv function. I am developing some code that integrates with several other libraries. This program is not using MFC. It is plain C...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.