dnl CF_TTY_GROUP version: 17 updated: 2023/12/01 17:22:50
dnl ------------
dnl Check if the system has a tty-group defined.  This is used in xterm when
dnl setting pty ownership.
dnl
dnl The check relies upon running a test-program, which calls ttyname.  If this
dnl is run in batch mode, or if autoconf uses shell functions, the extra layer
dnl of eval/call may close stdin, making calls to ttyname fail.  To work around
dnl that, fall back to "/dev/tty".
AC_DEFUN([CF_TTY_GROUP],
[AC_REQUIRE([AC_PROG_EGREP])dnl
AC_MSG_CHECKING(for explicit tty group name)
AC_ARG_WITH(tty-group,
	[[  --with-tty-group[=XXX]  use XXX for the tty-group]],
	[cf_tty_group=$withval],
	[cf_tty_group=auto...])
test -z "$cf_tty_group"    && cf_tty_group=auto...
test "$cf_tty_group" = yes && cf_tty_group=auto...
AC_MSG_RESULT($cf_tty_group)

if test "$cf_tty_group" = "auto..." ; then
AC_CACHE_CHECK(for tty group name,cf_cv_tty_group_name,[

# If we are configuring as root, it is hard to get a clue about the tty group.
# But we'll guess based on how our connection is set up - assuming it is done
# properly.

cf_uid="`id | sed -e 's/^[^=]*=//' -e 's/(.*$//'`"
# )vi
if test "$cf_uid" != 0 ; then
cf_cv_tty_group_name=
cf_tty_name="`tty`"
test "$cf_tty_name" = "not a tty" && cf_tty_name=/dev/tty
test -z "$cf_tty_name" && cf_tty_name=/dev/tty
if test -c "$cf_tty_name"
then
	cf_option="-lL"

	# Expect listing to have fields like this:
	#-rwxrwxrwx   1 user      group       34293 Jul 18 16:29 pathname
	ls $cf_option "$cf_tty_name" >conftest.out
	read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <conftest.out
	if test -z "$cf_rest" ; then
		cf_option="${cf_option}g"
		ls "$cf_option" "$cf_tty_name" >conftest.out
		read cf_mode cf_links cf_usr cf_grp cf_size cf_date1 cf_date2 cf_date3 cf_rest <conftest.out
	fi
	rm -f conftest.out
	cf_cv_tty_group_name=$cf_grp
fi
fi

# If we cannot deduce the tty group, fall back on hardcoded cases

if test -z "$cf_cv_tty_group_name"
then
case $host_os in
(osf*)
	cf_cv_tty_group_name="terminal"
	;;
(*)
	cf_cv_tty_group_name="unknown"
	if ( ${EGREP-egrep} '^tty:' /etc/group 2>/dev/null 1>/dev/null ) then
		cf_cv_tty_group_name="tty"
	fi
	;;
esac
fi
])
cf_tty_group="$cf_cv_tty_group_name"
else
	# if configure option, always do this
	AC_DEFINE(USE_TTY_GROUP,1,[Define to 1 if we have a tty groupname])
fi

AC_DEFINE_UNQUOTED(TTY_GROUP_NAME,"$cf_tty_group",[Define to the name use for tty group])

# This is only a double-check that the group-name we obtained above really
# does apply to the device.  We cannot perform this test if we are in batch
# mode, or if we are cross-compiling.

AC_CACHE_CHECK(if we may use the $cf_tty_group group,cf_cv_tty_group,[
cf_tty_name="`tty`"
if test "$cf_tty_name" != "not a tty"
then
AC_TRY_RUN([
$ac_includes_default

#include <grp.h>

int main(void)
{
	static char default_tty[] = "/dev/tty";
	struct stat sb;
	struct group *ttygrp;
	int fd;
	char *name;

	for (fd = 0; fd < 3; ++fd) {
		if ((name = ttyname(fd)) != 0)
			break;
	}
	if (name == 0)
		name = default_tty;

	ttygrp = getgrnam(TTY_GROUP_NAME);
	endgrent();

	if (ttygrp != 0
	 && name != 0
	 && stat(name, &sb) == 0
	 && sb.st_gid != getgid()
	 && sb.st_gid == ttygrp->gr_gid) {
		${cf_cv_main_return:-return} (0);
	}
	${cf_cv_main_return:-return} (1);
}
	],
	[cf_cv_tty_group=yes],
	[cf_cv_tty_group=no],
	[cf_cv_tty_group=unknown])
elif test "$cross_compiling" = yes; then
	cf_cv_tty_group=unknown
else
	cf_cv_tty_group=yes
fi
])

if test $cf_cv_tty_group = no ; then
	AC_MSG_WARN(Cannot use $cf_tty_group group)
else
	AC_DEFINE(USE_TTY_GROUP)
fi
])dnl
