[comp.unix.questions] ENVIRONMENT settings

tclark@hawk.ulowell.edu (T.C. Clark) (12/08/88)

	I'm writing a C program in which I would like to read in
	some of the user's ENVIRONMENT settings (ie. logname, printer)
	Can someone give me an idea on how to read these in and use
	them?

			thanks much,

			  tom ------> tclark@hawk.ulowell.edu

unniks@notrees.ACA.MCC.COM (C. Unnikrishnan) (12/09/88)

From article <10551@swan.ulowell.edu>, by tclark@hawk.ulowell.edu (T.C. Clark):
> 
> 	I'm writing a C program in which I would like to read in
> 	some of the user's ENVIRONMENT settings (ie. logname, printer)
> 	Can someone give me an idea on how to read these in and use
> 	them?
here's a portion of the code pulled out from a source file

main (argc, argv, envp)
     register int argc;
     register char **argv;
     char **envp;
{
	/* do something */
 	for (; *envp; envp++) {
	printf("%s\n", *envp);
	}
	/* do whatever with the variables */
}


*********************************************************************
C. Unni Krishnan
unni@mcc.COM	##Arpa Internet 
{ihnp4,seismo,ucb-vax,gatech}!cs.utexas.edu!pp!unniks
*********************************************************************

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/09/88)

In article <10551@swan.ulowell.edu> tclark@hawk.ulowell.edu (T.C. Clark) writes:
>	I'm writing a C program in which I would like to read in
>	some of the user's ENVIRONMENT settings (ie. logname, printer)
>	Can someone give me an idea on how to read these in and use
>	them?

	extern char *getenv();	/* in standard C library */
	char *logname;
	...
	if ( (logname = getenv( "LOGNAME" )) == 0 )
		logname = "nobody";	/* use default if not set */
	...
	printf( "Your LOGNAME is \"%s\"\n", logname );	/* for example */

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/09/88)

In article <76@notrees.ACA.MCC.COM> unniks@notrees.ACA.MCC.COM (C. Unnikrishnan) writes:
>main (argc, argv, envp)

Don't do this.
1.  Not all UNIX implementations support it.
2.  Very few non-UNIX C implementations support it.
3.  POSIX (IEEE 1003.1) doesn't require it.
4.  ANSI C (X3.159) doesn't require it.
5.  POSIX provides a global variable "environ" if you need to examine
    the environment rather than just look up a particular value.
6.  All recent UNIX implementations I know of support "environ".
7.  ANSI C requires support for getenv() to look up a particular value.
8.  All recent UNIX implementations I know of support getenv().

hollombe@ttidca.TTI.COM (The Polymath) (12/10/88)

In article <10551@swan.ulowell.edu> tclark@hawk.ulowell.edu (T.C. Clark) writes:
}I'm writing a C program in which I would like to read in
}some of the user's ENVIRONMENT settings (ie. logname, printer)
}Can someone give me an idea on how to read these in and use
}them?

Nothing to it.  Code your main() as follows:

main (argc, argv, envp)
int  argc;
char *argv[];
char *envp[];
{
/*  etc. */
}

envp stands for ENVironment Pointers.  It's a null terminated array of
pointers to null terminated strings in the format name=value, where name
is the name of the environment variable and value is what it's set to.  It
will contain all the user's currently set environment variables.

Alternatively, you can use the getenv(3) function if you only want to
access some specific environment variables.

As usual, RTFM. (-:

-- 
The Polymath (aka: Jerry Hollombe, hollombe@ttidca.tti.com)  Illegitimati Nil
Citicorp(+)TTI                                                 Carborundum
3100 Ocean Park Blvd.   (213) 452-9191, x2483
Santa Monica, CA  90405 {csun|philabs|psivax}!ttidca!hollombe

ok@quintus.uucp (Richard A. O'Keefe) (12/10/88)

In article <76@notrees.ACA.MCC.COM> unniks@notrees.ACA.MCC.COM (C. Unnikrishnan) writes:
>From article <10551@swan.ulowell.edu>, by tclark@hawk.ulowell.edu (T.C. Clark):
>> 	I'm writing a C program in which I would like to read in
>> 	some of the user's ENVIRONMENT settings (ie. logname, printer)
>here's a portion of the code pulled out from a source file
>main (argc, argv, envp)
>     char **envp;

Wouldn't it be simpler to do just
	extern char *getenv();
	char *LogName = getenv("LOGNAME");
	char *Printer = getenv("PRINTER");
and so on?

guy@auspex.UUCP (Guy Harris) (12/10/88)

>here's a portion of the code pulled out from a source file
>
>main (argc, argv, envp)
...
>     char **envp;
>{
>	/* do something */
> 	for (; *envp; envp++) {
>	printf("%s\n", *envp);
	...

You pulled it from the wrong source file; this may be something like
the "printenv" or "env" commands.  If you want to dump the entire
environment, you have to do something like that; however, if you just
want to look at some *particular* environment variable, it's easier (and
quite possibly more portable) to just do something like

	char *p;
	extern char *getenv();

	p = getenv("LOGNAME");

which will get the value of the environment variable "LOGNAME".

unniks@notrees.ACA.MCC.COM (C. Unnikrishnan) (12/11/88)

From article <666@auspex.UUCP>, by guy@auspex.UUCP (Guy Harris):
>>here's a portion of the code pulled out from a source file
>>
>>main (argc, argv, envp)
> 	...
i stand corrected. i misread the original post to as wanting the
whole environment. this is what i would do in such cases. though
i didnot know about the non-portability part. as many people have
pointed out getenv() is the best solution for the purpose.

*******************************************************************************
Disclaimer: This reflects only my current state of mind.  
	    I change my mind every five seconds or so.

C. Unni Krishnan
unni@mcc.COM	##Arpa Internet 
{ihnp4,seismo,ucb-vax,gatech}!cs.utexas.edu!pp!unniks

*******************************************************************************

guy@auspex.UUCP (Guy Harris) (12/11/88)

>Nothing to it.  Code your main() as follows:
>
>main (argc, argv, envp)

No, don't do that; see Doug Gwyn's earlier comments for why this is a
Bad Idea.

>Alternatively, you can use the getenv(3) function if you only want to
>access some specific environment variables.

Somewhere between 90% and 99% (maybe even more) of the code that wants
to do something with the environment only wants to access some specific
environment variables, and from the original poster's description it
sounds like that's what he wanted to do as well.