compat / setenv.con commit Tidy up git mergetool's backup file behaviour (44c36d1)
   1#include "../git-compat-util.h"
   2
   3int gitsetenv(const char *name, const char *value, int replace)
   4{
   5        int out;
   6        size_t namelen, valuelen;
   7        char *envstr;
   8
   9        if (!name || !value) return -1;
  10        if (!replace) {
  11                char *oldval = NULL;
  12                oldval = getenv(name);
  13                if (oldval) return 0;
  14        }
  15
  16        namelen = strlen(name);
  17        valuelen = strlen(value);
  18        envstr = malloc((namelen + valuelen + 2));
  19        if (!envstr) return -1;
  20
  21        memcpy(envstr, name, namelen);
  22        envstr[namelen] = '=';
  23        memcpy(envstr + namelen + 1, value, valuelen);
  24        envstr[namelen + valuelen + 1] = 0;
  25
  26        out = putenv(envstr);
  27        /* putenv(3) makes the argument string part of the environment,
  28         * and changing that string modifies the environment --- which
  29         * means we do not own that storage anymore.  Do not free
  30         * envstr.
  31         */
  32
  33        return out;
  34}