/*
 *  Copyright (c) 2000 David Parsons. All rights reserved.
 *
 *  This software is released under a berkeley-style copyright; the
 *  details can be found in the file COPYRIGHT.
 */

/*
 * turn messages on or off
 */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

main(int argc, char **argv)
{
    struct stat st;
    char *pgm = basename(argv[0]);

    if (fstat(0, &st) == -1) {
	fprintf(stderr, "%s: cannot stat stdin: %s", pgm, strerror(errno));
	exit(1);
    }

    if (argc == 1) {
	printf("is %s\n", isatty(0) && ((st.st_mode & 022) == 022) ? "y" : "n");
    }
    else {
	switch (toupper(argv[1][0])) {
	case 'Y':	st.st_mode |= 022;
			break;
	case 'N':	st.st_mode &= ~022;
			break;
	default:	fprintf(stderr, "usage: %s [y|n]\n", pgm);
			exit(1);
	}
	if (isatty(0))
	    fchmod(0, st.st_mode);
	else {
	    fprintf(stderr, "%s: stdin is not a tty\n", pgm);
	    exit(1);
	}
    }
    exit(0);
} /* mesg */
