Team-FLY |
An environment list consists of an array of pointers to strings of the form name = value . The name specifies an environment variable , and the value specifies a string value associated with the environment variable. The last entry of the array is NULL . The external variable environ points to the process environment list when the process begins executing. The strings in the process environment list can appear in any order. SYNOPSIS extern char **environ ISO C If the process is initiated by execl , execlp , execv or execvp , then the process inherits the environment list of the process just before the execution of exec . The execle and execve functions specifically set the environment list as discussed in Section 3.5. Example 2.22 environ.cThe following C program outputs the contents of its environment list and exits. #include <stdio.h> extern char **environ; int main(void) { int i; printf("The environment list follows:\n"); for(i = 0; environ[i] != NULL; i++) printf("environ[%d]: %s\n", i, environ[i]); return 0; } Environment variables provide a mechanism for using system-specific or user -specific information in setting defaults within a program. For example, a program may need to write status information in the user's home directory or may need to find an executable file in a particular place. The user can set the information about where to look for executables in a single variable. Applications interpret the value of an environment variable in an application-specific way. Some of the environment variables described by POSIX are shown in Table 2.1. These environment variables are not required, but if one of these variables is present, it must have the meaning specified in the table. Use getenv to determine whether a specific variable has a value in the process environment. Pass the name of the environment variable as a string. SYNOPSIS #include <stdlib.h> char *getenv(const char *name); POSIX:CX The getenv function returns NULL if the variable does not have a value. If the variable has a value, getenv returns a pointer to the string containing that value. Be careful about calling getenv more than once without copying the first return string into a buffer . Some implementations of getenv use a static buffer for the return strings and overwrite the buffer on each call. Table 2.1. POSIX environment variables and their meanings.
Example 2.23POSIX specifies that the shell sh should use the environment variable MAIL as the pathname of the mailbox for incoming mail, provided that the MAILPATH variable is not set. The following code segment sets mailp to the value of the environment variable MAIL if this variable is defined and MAILPATH is not defined. Otherwise, the segment sets mailp to a default value. #define MAILDEFAULT "/var/mail" char *mailp = NULL; if (getenv("MAILPATH") == NULL) mailp = getenv("MAIL"); if (mailp == NULL) mailp = MAILDEFAULT; The first call to getenv in Example 2.23 merely checks for the existence of MAILPATH , so it is not necessary to copy the return value to a separate buffer before calling getenv again. Do not confuse environment variables with predefined constants like MAX_CANON . The predefined constants are defined in header files with #define . Their values are constants and known at compile time. To see whether a definition of such a constant exists, use the # ifndef compiler directive as in Program 2.8. In contrast, environment variables are dynamic, and their values are not known until run time. Exercise 2.24 getpaths.cWrite a function to produce an argument array containing the components of the PATH environment variable. Answer: #include <stdlib.h> #define PATH_DELIMITERS ":" int makeargv(const char *s, const char *delimiters, char ***argvp); char **getpaths(void) { char **myargv; char *path; path = getenv("PATH"); if (makeargv(path, PATH_DELIMITERS, &myargv) == -1) return NULL; else return myargv; } |
Team-FLY |