if BUILD_MESG
usrbin_exec_PROGRAMS += mesg
+mesg_LDADD = $(LDADD) libcommon.la
dist_man_MANS += term-utils/mesg.1
mesg_SOURCES = term-utils/mesg.c
endif
.PP
The
.B mesg
-utility silently exits with error status 2 if the current standard error output does
-not refer to the terminal. In this case execute
+utility silently exits with error status 2 if not executed on terminal. In this
+case execute
.B mesg
is pointless. The command line option \fB\-\-verbose\fR forces
mesg to print a warning in this situation. This behaviour has been introduced
#include "nls.h"
#include "c.h"
#include "rpmatch.h"
+#include "ttyutils.h"
+#include "pathnames.h"
/* exit codes */
{
struct stat sb;
char *tty;
+ char ttybuf[sizeof(_PATH_PROC_FDDIR) + sizeof(stringify_value(INT_MAX))];
int ch, fd, verbose = FALSE, ret;
static const struct option longopts[] = {
argc -= optind;
argv += optind;
- if (!isatty(STDERR_FILENO)) {
+ fd = get_terminal_stdfd();
+ if (fd < 0) {
if (verbose)
warnx(_("no tty"));
exit(MESG_EXIT_FAILURE);
}
- if ((tty = ttyname(STDERR_FILENO)) == NULL)
- err(MESG_EXIT_FAILURE, _("ttyname failed"));
+
+ tty = ttyname(fd);
+ if (!tty) {
+ snprintf(ttybuf, sizeof(ttybuf), "%s/%d", _PATH_PROC_FDDIR, fd);
+ tty = ttybuf;
+ if (verbose)
+ warnx(_("ttyname() failed, attempting to go around using: %s"), tty);
+ }
+
if ((fd = open(tty, O_RDONLY)) < 0)
err(MESG_EXIT_FAILURE, _("cannot open %s"), tty);
if (fstat(fd, &sb))