Skip to content

Refactor fopencookie check #14346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 30 additions & 47 deletions build/php.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1319,24 +1319,20 @@ int main(void)
dnl
dnl PHP_FOPENCOOKIE
dnl
AC_DEFUN([PHP_FOPENCOOKIE], [
AC_CHECK_FUNC(fopencookie, [have_glibc_fopencookie=yes])

if test "$have_glibc_fopencookie" = "yes"; then
dnl glibcs (since 2.1.2?) have a type called cookie_io_functions_t.
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
]], [[cookie_io_functions_t cookie;]])],[have_cookie_io_functions_t=yes],[])

if test "$have_cookie_io_functions_t" = "yes"; then
dnl Newer glibcs have a different seeker definition.
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
dnl Check for cookie_io_functions_t type and fopencookie(). When using glibc,
dnl checks here require _GNU_SOURCE defined via AC_USE_SYSTEM_EXTENSIONS. Some
dnl glibc versions started using the off64_t in fopencookie seeker definition,
dnl which is in transition to use the POSIX standard off_t on 32-bit (with large
dnl file support enabled) and 64-bit.
dnl
AC_DEFUN([PHP_FOPENCOOKIE],
[AC_CHECK_TYPE([cookie_io_functions_t], [AC_CHECK_FUNCS([fopencookie])],,
[#include <stdio.h>])
dnl Check if glibc might use off64_t seeker definition.
AS_VAR_IF([ac_cv_func_fopencookie], [yes],
[AC_CACHE_CHECK([whether fopencookie seeker uses off64_t],
[php_cv_type_cookie_off64_t],
[AC_RUN_IFELSE([AC_LANG_SOURCE([
#include <stdio.h>
#include <stdlib.h>

Expand All @@ -1345,48 +1341,35 @@ struct cookiedata {
};

ssize_t reader(void *cookie, char *buffer, size_t size)
{ return size; }
{ (void)cookie; (void)buffer; return size; }
ssize_t writer(void *cookie, const char *buffer, size_t size)
{ return size; }
{ (void)cookie; (void)buffer; return size; }
int closer(void *cookie)
{ return 0; }
{ (void)cookie; return 0; }
int seeker(void *cookie, off64_t *position, int whence)
{ ((struct cookiedata*)cookie)->pos = *position; return 0; }
{ ((struct cookiedata*)cookie)->pos = *position; (void)whence; return 0; }

cookie_io_functions_t funcs = {reader, writer, seeker, closer};

int main(void) {
struct cookiedata g = { 0 };
FILE *fp = fopencookie(&g, "r", funcs);

if (fp && fseek(fp, 8192, SEEK_SET) == 0 && g.pos == 8192)
if (fp && fseek(fp, 8192, SEEK_SET) == 0 && g.pos == 8192) {
return 0;
}
return 1;
}

]])], [
cookie_io_functions_use_off64_t=yes
], [
cookie_io_functions_use_off64_t=no
], [
dnl Cross compilation.
case $host_alias in
*linux*)
cookie_io_functions_use_off64_t=yes
;;
*)
cookie_io_functions_use_off64_t=no
;;
esac
])],
[php_cv_type_cookie_off64_t=yes],
[php_cv_type_cookie_off64_t=no],
[AS_CASE([$host_alias],
[*linux*], [php_cv_type_cookie_off64_t=yes],
[php_cv_type_cookie_off64_t=no])]
)])
AS_VAR_IF([php_cv_type_cookie_off64_t], [yes],
[AC_DEFINE([COOKIE_SEEKER_USES_OFF64_T], [1],
[Whether fopencookie seeker uses off64_t.])])
])

AC_DEFINE(HAVE_FOPENCOOKIE, 1, [ ])

if test "$cookie_io_functions_use_off64_t" = "yes" ; then
AC_DEFINE(COOKIE_SEEKER_USES_OFF64_T, 1, [ ])
fi
fi
fi
])

dnl ----------------------------------------------------------------------------
Expand Down
Loading