Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Environment, Exams of Computer Programming

char *getenv(const char *name);. Returns: pointer to value associated with name, NULL if not found. Note that this function returns a pointer to the value ...

Typology: Exams

2021/2022

Uploaded on 09/27/2022

gaurish
gaurish 🇺🇸

4.7

(15)

235 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Environment
When a program is executed...
process that does exec can pass command-line arguments to
the new program
this is part of the UNIX system shells
1
1 Environment
1.1 Quick Review—Argument List
When a program is executed, the process that does the exec can pass command-
line arguments to the new program. This is part of the normal operation of
the UNIX system shells.
int main( int argc, char *argv[] )
{
int i;
/* echo all command-line args */
for ( i = 0 ; i < argc ; i++ )
printf( "argv[%d]: %s\n", i, argv[i] );
}
1
Environment
Environment List
each program also passed an environment list
this list is an array of char pointers, with each pointer
containing the address of a null-terminated C string
the address of the array of pointers is contained in the global
variable environ:
extern char **environ;
2
pf3
pf4
pf5

Partial preview of the text

Download Environment and more Exams Computer Programming in PDF only on Docsity!

When a program is executed...

process that does exec can pass command-line arguments to

the new program

this is part of the UNIX system shells

1

1 Environment

1.1 Quick Review—Argument List

When a program is executed, the process that does the exec can pass command-

line arguments to the new program. This is part of the normal operation of

the UNIX system shells.

int main( int argc, char *argv[] )

int i;

/* echo all command-line args */

for ( i = 0 ; i < argc ; i++ )

printf( "argv[%d]: %s\n", i, argv[i] );

Environment

Environment List

each program also passed an environment list

this list is an array of char pointers, with each pointer

containing the address of a null-terminated C string

the address of the array of pointers is contained in the global

variable environ :

extern char **environ;

Example of environment with five strings

the null bytes a tthe end of each string are explicitly shown

3 3

Environment

Terms

environ is called the environment pointer

the array of pointers is called the environment list

the strings they point to are called environment strings

Environment Variables

environment strings are usually of the form name=value

the Unix kernel never looks at these strings

their interpretation is up the the various applications

the shell uses numerous environment variables

some are automatically set at login, e.g., HOME , USER

others are for us to set, e.g., If we set the environment variable

MAILPATH, for example, it tells the Bourne shell, GNU

Bourne-again shell, and Korn shell where to look for mail.

7

Environment

Support for various environment list functions

Support for various environment list functions

9 ISO C defines a function that we can use to fetch values from the en- vironment, but this standard says that the contents of the environment are implementation defined. #include <stdlib.h> char *getenv(const char *name); Returns: pointer to value associated with name, NULL if not found Note that this function returns a pointer to the value of a name=value string. We should always use getenv to fetch a specific value from the envi- ronment, instead of accessing environ directly. 10

Environment

Manipulating environment variables

10 In addition to fetching the value of an environment variable, sometimes we may want to set an environment variable. We may want to change the value of an existing variable or add a new variable to the environment. #include <stdlib.h> int putenv(char *str); int setenv(const char *name, const char *value, int rewrite); int unsetenv(const char *name); All return: 0 if OK, nonzero on error The operation of these three functions is as follows.

  • The putenv function takes a string of the form name=value and places it in the environment list. If name already exists, its old definition is first removed.
  • The setenv function sets name to value. If name already exists in the environment, then (a) if rewrite is nonzero, the existing definition for name is first removed; (b) if rewrite is 0, an existing definition for name is not removed, name is not set to the new value, and no error occurs.

In addition to fetching the value of an environment variable, sometimes

we may want to set an environment variable. We may want to change the

value of an existing variable or add a new variable to the environment.

#include <stdlib.h>

int putenv(char *str);

int setenv(const char *name, const char *value, int rewrite);

int unsetenv(const char *name);

All return: 0 if OK, nonzero on error

The operation of these three functions is as follows.

  • The putenv function takes a string of the form name=value and places

it in the environment list. If name already exists, its old definition is

first removed.

  • The setenv function sets name to value. If name already exists in the

environment, then (a) if rewrite is nonzero, the existing definition for

name is first removed; (b) if rewrite is 0, an existing definition for name

is not removed, name is not set to the new value, and no error occurs.

Environment

Manipulating environment variables

13 In addition to fetching the value of an environment variable, sometimes we may want to set an environment variable. We may want to change the value of an existing variable or add a new variable to the environment. #include <stdlib.h> int putenv(char *str); int setenv(const char *name, const char *value, int rewrite); int unsetenv(const char *name); All return: 0 if OK, nonzero on error The operation of these three functions is as follows.

  • The putenv function takes a string of the form name=value and places it in the environment list. If name already exists, its old definition is first removed.
  • The setenv function sets name to value. If name already exists in the environment, then (a) if rewrite is nonzero, the existing definition for name is first removed; (b) if rewrite is 0, an existing definition for name is not removed, name is not set to the new value, and no error occurs.
  • The unsetenv function removes any definition of name. It is not an error if such a definition does not exist. Note the difference between putenv and setenv. Whereas setenv must allocate memory to create the name=value string from its arguments, putenv is free to place the string passed to it directly into the envi- ronment. On Linux and Solaris, the putenv implementation places the address of the string we pass to it directly into the environment list. In this case, it would be an error to pass it a string allocated on the stack, since the memory would be reused after we return from the current function. int setenv(const char *name, const char *value, int rewrite); int unsetenv(const char *name); All return: 0 if OK, nonzero on error The operation of these three functions is as follows.
  • The putenv function takes a string of the form name=value and places it in the environment list. If name already exists, its old definition is first removed.
  • The setenv function sets name to value. If name already exists in the environment, then (a) if rewrite is nonzero, the existing definition for name is first removed; (b) if rewrite is 0, an existing definition for name is not removed, name is not set to the new value, and no error occurs.
  • The unsetenv function removes any definition of name. It is not an error if such a definition does not exist. Note the difference between putenv and setenv. Whereas setenv must allocate memory to create the name=value string from its arguments, putenv is free to place the string passed to it directly into the envi- ronment. On Linux and Solaris, the putenv implementation places the address of the string we pass to it directly into the environment list. In this case, it would be an error to pass it a string allocated on the stack, since the memory would be reused after we return from the current function. 11