getenv problem

J

jain-neeraj

Hi everyone,

I came across a strange problem today while doing putenv() and
getenv(). It seems that getenv() is doing "something" with the
variables exported using putenv().

Immediately after putenv(), each variable is available; However after
putenv() of all of them, some get messed up (getenv problem???). This
does not look like to be a heap problem, because putenv() is always
successful. Also, the actual results show that they are intially "put"
into the env successfully. It is more mystifying because the alternate
values are getting "lost".

Any suggestions will be of great help. Following is a snippet
alongwith the actual results.

int some_function()
{
char env_str[500];

if (! (user_connected))
{
sprintf (env_str,"ORA_USER=%s", g_env.username);
if (putenv (env_str) != 0)
{
ERR_StdReportAndReturn_1 (SA_PUTENV_ERROR,
"ORA_USER");
}
printf("\n ORA_USER=%s",getenv("ORA_USER"));

sprintf (env_str,"ORA_PASS=%s", g_env.password);
if (putenv (env_str) != 0)
{
ERR_StdReportAndReturn_1 (SA_PUTENV_ERROR,
"ORA_PASS");
}
printf("\n ORA_PASS=%s",getenv("ORA_PASS"));

sprintf (env_str,"ORA_INST=%s", g_env.db_instance);
if (putenv (env_str) != 0)
{
ERR_StdReportAndReturn_1 (SA_PUTENV_ERROR,
"ORA_INST");
}
printf("\n ORA_INST=%s",getenv("ORA_INST"));

sprintf (env_str,"ORA_INST_NODE=%s",
g_env.db_instance_node);
if (putenv (env_str) != 0)
{
ERR_StdReportAndReturn_1 (SA_PUTENV_ERROR,
"ORA_INST_NODE");
}
printf("\n ORA_INST_NODE=%s",getenv("ORA_INST_NODE"));

printf ("\n inside block");
printf("\n ORA_USER=%s",getenv("ORA_USER"));
printf("\n ORA_PASS=%s",getenv("ORA_PASS"));
printf("\n ORA_INST=%s",getenv("ORA_INST"));
printf("\n ORA_INST_NODE=%s",getenv("ORA_INST_NODE"));

} //end-if

printf ("\n outside block");
printf("\n ORA_USER=%s",getenv("ORA_USER"));
printf("\n ORA_PASS=%s",getenv("ORA_PASS"));
printf("\n ORA_INST=%s",getenv("ORA_INST"));
printf("\n ORA_INST_NODE=%s",getenv("ORA_INST_NODE"));

// call 3rd party product which needs these variables

} // end some_function()

output:

ORA_USER=neeraj
ORA_PASS=jain
ORA_INST=njinst
ORA_INST_NODE=njnode
inside block
ORA_USER=
ORA_PASS=jain
ORA_INST=
ORA_INST_NODE=njnode
outside block
ORA_USER=
ORA_PASS=jain
ORA_INST=
ORA_INST_NODE=njnode

Thanks in advance,
Neeraj.
 
J

jain-neeraj

Thanks for your co-operation.

Martin Ambuhl said:
(e-mail address removed) wrote (27 Jun 2003) in
/ comp.lang.c:


putenv() is not standard C. Hell, it isn't even Posix.
Even when posting off-topic, try to post compilable code.
Needing to track down the header inclusions you snipped is not welcome.
Nor is needing to create a structure for your undeclared g_env and
having to create your undeclared user_connected. Whateve
"ERR_StdReportAndReturn_1" is, it ain't standard. And, if it doesn't
come from your implementation, it is an invasion of the implementation's
namespace. Using '//' comments in a newsgroup posting is just asking for
your comments to be munged. If you feel an overwhelming need for these
instead of cleanly-delimited '/* ... */' comments, keep them out of
newsgroup postings.


You can't have output in a program in a hosted environment without a
main function.
 
J

jain-neeraj

Thanks Emmanuel, your advice was absolutely to-the-point. I found the solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top