This is the mail archive of the
mailing list for the GCC project.
Re: using linux environment variables such as $USER in c++ applications
- To: ciaran <pcxmac at yahoo dot com>
- Subject: Re: using linux environment variables such as $USER in c++ applications
- From: Michael Eager <eager at mvista dot com>
- Date: Thu, 09 Aug 2001 16:21:55 -0700
- CC: gcc at gcc dot gnu dot org
- Organization: MontaVista Software, Inc.
- References: <3B71F618.firstname.lastname@example.org>
> how do i get the compiler to recognize variables like $PATH or $USER? i
> wanna use them like how i would in a shell script
This is probably off topic for the GCC mailing list.
I presume you want to have a string which contains an environment variable
which you want to expand to it's contents. For example:
char * filename = "$USER/myfile";
You need to scan the string for occurances of environment variables:
char * env = strchr (filename, '$');
Find the end of the environment variable (look at strspn()). Remove
the environment variable from the string, save it somewhere, say in "env".
Call getenv(env) to get the value of the environment variable, remembering
to remove the '$'. Insert the value into the string where you removed
the environment variable.
There are similar functions in the C++ string class which you can use.
Michael Eager email@example.com 408-328-8426
MontaVista Software, Inc. 1237 E. Arques Ave., Sunnyvale, CA 94085