PostgreSQL Source Code  git master
setenv.c File Reference
#include "c.h"
Include dependency graph for setenv.c:

Go to the source code of this file.

Functions

int setenv (const char *name, const char *value, int overwrite)
 

Function Documentation

◆ setenv()

int setenv ( const char *  name,
const char *  value,
int  overwrite 
)

Definition at line 20 of file setenv.c.

References malloc, putenv, and sprintf.

21 {
22  char *envstr;
23 
24  /* Error conditions, per POSIX */
25  if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
26  value == NULL)
27  {
28  errno = EINVAL;
29  return -1;
30  }
31 
32  /* No work if variable exists and we're not to replace it */
33  if (overwrite == 0 && getenv(name) != NULL)
34  return 0;
35 
36  /*
37  * Add or replace the value using putenv(). This will leak memory if the
38  * same variable is repeatedly redefined, but there's little we can do
39  * about that when sitting atop putenv().
40  */
41  envstr = (char *) malloc(strlen(name) + strlen(value) + 2);
42  if (!envstr) /* not much we can do if no memory */
43  return -1;
44 
45  sprintf(envstr, "%s=%s", name, value);
46 
47  return putenv(envstr);
48 }
#define malloc(a)
Definition: header.h:50
#define sprintf
Definition: port.h:218
static void overwrite(PGconn *conn, Oid lobjId, int start, int len)
Definition: testlo.c:108
static struct @143 value
#define putenv(x)
Definition: win32_port.h:496
const char * name
Definition: encode.c:561