using linux environment variables such as $USER in c++ applications

Michael Eager eager@mvista.com
Thu Aug 9 16:21:00 GMT 2001


ciaran wrote:
> 
> 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     eager@mvista.com	408-328-8426	
MontaVista Software, Inc. 1237 E. Arques Ave., Sunnyvale, CA  94085



More information about the Gcc mailing list