[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::digital_unix

Title:DIGITAL UNIX(FORMERLY KNOWN AS DEC OSF/1)
Notice:Welcome to the Digital UNIX Conference
Moderator:SMURF::DENHAM
Created:Thu Mar 16 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:10068
Total number of notes:35879

9069.0. "Environment variables in application program" by MLNCSC::DELLAPERGOLA () Fri Mar 07 1997 08:18


	Hi

	I would like to know if it is possible to use 
	environment variables from an application program
	(like DEC C or Fortran 77)

	Example I would like to open a file using as filename
	$HOME/test instead of /usr/users/anna/test.

	If yes, which is the rigth statement syntax?

	Thanks and best regards 
	Anna Della Pergola
T.RTitleUserPersonal
Name
DateLines
9069.1getenv(3) ?RHETT::PARKERFri Mar 07 1997 09:4710
    
    
    Anna,
    
    Have a look at getenv(3) - it should provide what you need.
    
    Hth,
    
    Lee
    
9069.2It's the key part of the solution...WTFN::SCALESDespair is appropriate and inevitable.Fri Mar 07 1997 13:418
.1> getenv(3) - it should provide what you need.

Anna, in case it's not clear, what Lee means is that, in your program, you can
use getenv() to translate $HOME into "/usr/users/anna", and then you can append
"/test" to that and specify the result to open().


				Webb
9069.3getpwent is also usable (though higher overhead) but sometimes preferableVAXCPU::michaudJeff Michaud - ObjectBrokerFri Mar 07 1997 15:0627
> Anna, in case it's not clear, what Lee means is that, in your program, you can
> use getenv() to translate $HOME into "/usr/users/anna", and then you can append
> "/test" to that and specify the result to open().

	Of course you only want to append "/test" if HOME is defined :-)
	Example:

		char *home = getenv("HOME");
		char filespec[MAXPATHLEN];

		if( home )
		    sprintf(filespec, "%s/test", home);
		else
		    strcpy(filespec, "test"); /* relative to cwd then */

	actually however one would normally also not prepend the HOME
	directory if you are actually getting the filename you are appending
	to the home directory from the user if they specified an absolute
	filespec, such that you would modify the test above to:

		if( home  &&  usersuppliedfilename[0] != '/' )

	also if the HOME environment variable is not defined (or you
	are in a situation where you can't trust it, since the user
	can set environment variables to anything), you can use
	the getpwent family of routines (such as getpwuid(getuid()))
	and use the pw_dir field of the passwd structure.