Files
xserver/hw/xwin/os-compat.h
Enrico Weigelt, metux IT consult 95b6716b6c xwin: winprefs: use safer setenv() instead of putenv()
putenv() is deprecated due several drawbacks: the passed buffer becomes
part of the environment (not copied), thus the caller needs to allocate
a permanent buffer first - and has no way to know whether it might
become used later. And it has to fill in the new entry in the correct
form (<name>+"="+<value>)

setenv() instead is damn simple: pass env variable name and value
separately, and no need to care what's going on under the hood.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
2026-02-11 14:46:50 +01:00

28 lines
644 B
C

#ifndef __XWIN_OS_COMPAT_H
#define __XWIN_OS_COMPAT_H
#include <stdlib.h>
#include <errno.h>
/* special workaround for mingw lacking setenv() */
#ifndef HAVE_SETENV
static inline int setenv(const char *name, const char *value, int overwrite)
{
size_t name_len = strlen(name);
size_t value_len = strlen(value);
size_t bufsz = name_len + value_len + 1;
char *buf = malloc(bufsz);
if (!buf) {
errno = ENOMEM;
return -1;
}
memcpy(buf, name, name_len);
memcpy(buf+name_len, value, value_len);
buf[name_len+value_len] = 0;
putenv(buf);
return 0;
}
#endif
#endif /* __XWIN_OS_COMPAT_H */