Replace setenv() with putenv() on old systems (+937)

Add system check for setenv() function in configure and CMake.
This commit is contained in:
Albrecht Schlosser 2024-03-18 22:29:50 +01:00
parent 1d43ae0b0a
commit d3a3ab40b7
5 changed files with 41 additions and 3 deletions

View File

@ -204,6 +204,8 @@ CHECK_FUNCTION_EXISTS (strlcat HAVE_STRLCAT)
CHECK_FUNCTION_EXISTS (strlcpy HAVE_STRLCPY)
CHECK_FUNCTION_EXISTS (vsnprintf HAVE_VSNPRINTF)
check_function_exists(setenv HAVE_SETENV)
if(HAVE_SCANDIR AND NOT HAVE_SCANDIR_POSIX)
set(MSG "POSIX compatible scandir")
message(STATUS "Looking for ${MSG}")

View File

@ -240,6 +240,14 @@
#cmakedefine01 USE_POLL
/*
* HAVE_SETENV:
*
* Whether or not POSIX setenv() is available from stdlib.h.
*/
#cmakedefine01 HAVE_SETENV
/*
* Do we have various image libraries?
*/

View File

@ -239,6 +239,14 @@
#define USE_POLL 0
/*
* HAVE_SETENV:
*
* Whether or not POSIX setenv() is available from stdlib.h.
*/
#define HAVE_SETENV 0
/*
* Do we have various image libraries?
*/

View File

@ -620,6 +620,9 @@ AC_CHECK_FUNCS([strcasecmp strlcat strlcpy])
AC_CHECK_HEADERS([locale.h])
AC_CHECK_FUNCS([localeconv])
dnl HP-UX 11.11 does not provide setenv()
AC_CHECK_FUNCS([setenv])
dnl FLTK library uses math library functions...
AC_SEARCH_LIBS([pow], [m])

View File

@ -75,9 +75,26 @@ Fl_X11_Screen_Driver::Fl_X11_Screen_Driver() : Fl_Unix_Screen_Driver() {
key_table_size = 0;
}
void Fl_X11_Screen_Driver::display(const char *d)
{
if (d) setenv("DISPLAY", d, 1);
void Fl_X11_Screen_Driver::display(const char *d) {
if (!d) return;
// Issue #937:
// setenv() is available since POSIX.1-2001
// https://pubs.opengroup.org/onlinepubs/009604499/functions/setenv.html
#if HAVE_SETENV
setenv("DISPLAY", d, 1);
#else // HAVE_SETENV
// Use putenv() for old systems (similar to FLTK 1.3)
static char e[1024];
strcpy(e, "DISPLAY=");
strlcat(e, d, sizeof(e));
for (char *c = e + 8; *c != ':'; c++) {
if (!*c) {
strlcat(e,":0.0",sizeof(e));
break;
}
}
putenv(e);
#endif // HAVE_SETENV
}
void fl_x11_use_display(Display *d) {