How to set environment variables in C

I need to add some environment variables to eventually be used by a forked child. I have:
main(int argc, char *argv[], char *envp[])
{
:
    for (i = 0; *(envp +i); i++) printf("%s\n", *(envp+i));
:
        setenv("AUTHORIZED", "2", 1 /*overwrite*/);
        setenv("USER", "mark", 1);
        setenv("$USER", "mark", 1);
        setenv("userdb_uid", "3000026", 1);
        setenv("userdb_gid", "100", 1);
        setenv("EXTRA", "userdb_uid userdb_gid", 1);

    for (i = 0; *(envp +i); i++) printf("%s\n", *(envp+i));
:
}

Open in new window

                               
However, after doing all those setenv()'s my environment variable list is the same as before. No changes. This includes the one pre-existing variable USER; does not get my new setting. What am I doing wrong?
LVL 1
jmarkfoleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
You are probably exiting the program, which terminates the process along with its environment.
If you fork a child, the child will inherit its parents environment, but the any changes that the child makes to its environment will not be seen by the parent when the child exits.

You are also missing a declaration and some includes, and have extraneous :

Also, envp is the pointer that was passed to your program when it was invoked, not the current environment, which you can see with getenv  or
       extern char **environ;
       for (i = 0; *(environ +i); i++) printf("%s\n", *(environ+i));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmarkfoleyAuthor Commented:
ozo:
You are probably exiting the program, which terminates the process along with its environment.
Not at this point. Just setting ENVs in program, then trying to read back.
You are also missing a declaration and some includes ...
Well, probably not really. I just stripped out the extraneous/obvious stuff to focus on the important things. Which in this case was ...
Also, envp is the pointer that was passed to your program when it was invoked, not the current environment, which you can see with ... extern char **environ;
That did the trick! If, after setting the environment variables I looked at them with environ, I could see them! Even better, when I did do the fork()/execve(), and passed environ instead of envp as the 3rd argument to execve(), the child process could finally see the new environment variables. Mission accomplished!

I used to be brilliant at C 2 or 3 decades ago, writing low-level OS stuff, DB access methods, process control, device drivers, you name it -- it's been a (long) time. Glad to see C programmers are still doin' their thing!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.