Home > front end >  How can i concatenate the value of a variable of enviorment to another variable of enviorment using
How can i concatenate the value of a variable of enviorment to another variable of enviorment using

Time:01-10

I know how to do it using strcat and strings but I wonder if I could have done it using env functions.

CodePudding user response:

Environment variables are exposed to your C program as an array of string pointers. Each points to a fixed size string so you cannot concatenate anything to said string without causing a buffer overflow. Instead you want to do something along these lines (purposely invoking Cunningham's Law). I am using strcat() as you mentioned it but sprintf() might be more convenient:

#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define AWESOME " is awesome"

int main(void) {
    const char *user = getenv("USER");
    if(!user) {
       // handle error
       return 1;
    }
    char *user_is_awesome = malloc(strlen(user)   sizeof(AWESOME));
    if(!user_is_awesome) {
       // impossible! handle malloc failure though
       return 1;
    }
    strcpy(user_is_awesome, user);
    strcat(user_is_awesome, AWESOME);
    setenv("USER", user_is_awesome, 1);
    printf("%s\n", getenv("USER"));
}

and the example out is:

allan is awesome

CodePudding user response:

How can i concatenate the value of a variable of enviorment to another variable of enviorment using getenv(), setenv(),putenv() etc

The standard library's functions for accessing environment strings provide only for searching / reading them and for setting them. If you want to perform manipulations such as concatenating their values then you need to do that separately.

Do note, however, that

  • the strings provided via getenv() must not be modified by the program, but they might be modified by subsequent calls to getenv() and / or by calls to putenv() or setenv() (see also below).

  • the C language specification defines getenv() but not putenv() or setenv(). The latter two are specified by POSIX, and they may also appear in non-POSIX C implementations, but you cannot rely on them to the same extent that you can rely on getenv().

  • these functions are not guaranteed to be thread-safe.

  • getenv() returns a null pointer if no value is set in the environment for the specified name.

For maximum generality, you would need to dynamically allocate space for a copy of the first environment variable, and also for the concatenation. This is left as an exercise. If it is sufficient to put an upper bound on the length of the result, and the result does not need to survive return from the function in which it is formed, then something like this would suffice:

#define MAX_CONCAT 2048

void concat_env(const char *name1, const char *name2) {
    char concatenation[MAX_CONCAT];

    // start with an empty string
    concatenation[0] = '\0';

    // Read the first value
    const char *value = getenv(name1);

    // Treat null as an empty string
    if (value) {
        strncat(concatenation, value, MAX_CONCAT - 1);
    }

    // Read the second value
    value = getenv(name2);

    // Treat null as an empty string
    if (value) {
        strncat(concatenation, value, MAX_CONCAT - 1 - strlen(concatenation));
    }

    printf("The concatenation is '%s'\n", concatenation);
}
  •  Tags:  
  • c
  • Related