This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]