PostgreSQL Source Code  git master
setenv.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * setenv.c
4  * setenv() emulation for machines without it
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/port/setenv.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "c.h"
17 
18 
19 int
20 setenv(const char *name, const char *value, int overwrite)
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
static struct @141 value
#define sprintf
Definition: port.h:217
int setenv(const char *name, const char *value, int overwrite)
Definition: setenv.c:20
static void overwrite(PGconn *conn, Oid lobjId, int start, int len)
Definition: testlo.c:108
#define putenv(x)
Definition: win32_port.h:496
const char * name
Definition: encode.c:515