Chapter Contents

Previous

Next
Run-Time Argument Processing

Environment Variables

Environment variables are used to pass variable values that are set outside the program to be passed into the C program. An environment variable assignment begins with an equal sign (=) and contains one other equal sign either embedded or trailing, as in the following example:

=name=value

Environment variables are normally accessed using the standard getenv function or the POSIX variable environ . Alternately, you can define a third argument to main that is an array of pointers. You can access the values specified for environment variables by stepping through this array until you reach an argument that contains a null pointer. For example, you can specify the TSO or CMS command line

pgm =ABC=DEF =GHI=JKL =PATH=C:/LC

or the OS/390 PARM string

//STEP EXEC PGM=pgm,PARM='=ABC=DEF =GHI=JKL =PATH=C:/LC'

to invoke the following program:

void main(int argc, char **argv, char **envp)
{
   int i;
   i=0;
   printf( "program name is %s\n", *argv );

      /* Obtain program arguments. */
   while(--argc > 0)
      printf("argv[%d]  is %s\n", ++i, *++argv);

   i=0;
      /* Obtain environment variables. */
   while(*envp)
      printf("envp[%d]  is %s\n", i++, *envp++);
}

The program prints the following:

program name is pgm
envp[0]  is ABC=DEF
envp[1]  is GHI=JKL
envp[2]  is PATH=C:/LC

If an environment variable assignment has no value (for example, =name=), any previously entered value for that environment variable is removed. For example, if you invoke the example program under CMS or in TSO with

pgm =ABC=DEF =GHI=JKL =ABC=

or under OS/390 with

//STEP EXEC PGM=pgm,PARM='=ABC=DEF =GHI=JKL =ABC='

the program prints the following:

program name is pgm
envp[0]  is GHI=JKL

The value portion of an environment variable assignment can be enclosed in parentheses. The parentheses are removed from the environment variable string before it is stored. Parentheses enclosing environment variable assignments should balance. If there are more left parentheses than right, the remainder of the command line is regarded as part of the environment variable value. If there are more right parentheses than left, those parentheses become program arguments. No error message is issued if parentheses do not balance.

The colon (:) is not valid in the name portion of an environment variable entered on the command line.

Note:    Under CMS and in TSO, environment variables can be defined externally to the program. Under CMS, the GLOBALV command defines environment variables. In TSO you can use the SAS/C PUTENV command. (See Using the GETENV and PUTENV TSO Commands for more information.) These environment variables are not stored in the envp array in either case.  [cautionend]

Also note that when you run a SAS/C program under the USS shell, it inherits environment variables exported from the shell as well as any specific to the command line. Both sets of variables are stored in the envp array.


POSIX Considerations

For historical reasons, SAS/C environment variable support prior to Release 6.00 did not treat environment variable names as case-sensitive. That is, getenv("sauce") , getenv("Sauce") , and getenv("SAUCE") always returned the same result.

With Release 6.00, POSIX support has been added as described in Chapter 19, "Introduction to POSIX," in SAS/C Library Reference, Volume 2. The POSIX standards do not permit this case-insensitive behavior. For this reason, the names of environment variables in a program invoked with exec -linkage are considered case sensitive. Programs running in other environments, for example, TSO, retain the old behavior.

In the TSO environment, SAS/C supports several scopes of environment variables: program scope, external scope, and permanent scope. When a TSO program, or a child of a TSO program that was created by a call to the fork function, invokes an exec function, only the program scope variables are passed to the executed program. Note that although the library performs TSO environment variable name comparisons without reference to case distinctions, it preserves the case of characters in the environment variable name for accurate transmission by the exec function. For instance, if a TSO program calls putenv("Sauce=Bernaise") followed by an exec , the new process will receive the environment variable Sauce, not sauce or SAUCE.

SAS/C POSIX support also removes the previous limitations on the size of program scope environment variable names and values. However, these limitations still apply to external and permanent scope variables.

The values of program scope environment variables can be inspected by the program without calling getenv . For programs compiled with the posix option, the environment variables are chained from the extern char **environ , as required by the POSIX.1 Standard. For programs not compiled with the posix option, the variable name environ is not reserved (as that would be a C Standards violation). However, there is a SAS/C extern char ***_environ . The pointer *_environ always addresses the program scope environment variable chain, regardless of whether the program is compiled with or without the posix option.

Updates to the program scope environment variables can be performed using the functions putenv , setenv , and clearenv . ( setenv and clearenv are defined by the POSIX.1a Standard; putenv is a SAS/C extension.) Note that alteration of environ to change the program's environment variables should not be attempted, since portable functions to do this are provided.

As nonstandard extensions, SAS/C still supports the specification of environment variable values on the command line (even for programs invoked by the USS shell). It also supports supplying a third argument for the main function to receive a pointer to the program scope environment variables.


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.