ysor
ysor

Reputation: 23

Function similar to putenv()

I need some help on creating a function similar to the putenv() function of the C standard library, but instead of:

int putenv(char *string);

it is prototyped as:

void env_add(char varname[], char varvalue[]);

where varname[] and varvalue[] are entered by the user and are of type char.

Upvotes: 2

Views: 162

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126917

You should measure the length of varname and varvalue (with e.g. strlen), allocate dynamically a string long enough to hold them, the equal sign and the null terminator, build the varname=varvalue string (you can do that with e.g. snprintf or strncat, or with two for loops if you are the masochist type), pass the new string to putenv and deallocate the string you've built.

By the way, I'd change the type of varname and varvalue to const char *, because in your function you are not actually modifying them.

Upvotes: 1

K-ballo
K-ballo

Reputation: 81389

That's not very hard:

void env_add( char varname[], char varvalue[] )
{
    char* argument = ...;

    /* ...do something to create an argument out of name and value... */

    putenv( argument );
};

Upvotes: 0

Related Questions