11590Srgrimes/*
21590Srgrimes * Copyright (c) 1987, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes * (c) UNIX System Laboratories, Inc.
51590Srgrimes * All or some portions of this file are derived from material licensed
61590Srgrimes * to the University of California by American Telephone and Telegraph
71590Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81590Srgrimes * the permission of UNIX System Laboratories, Inc.
91590Srgrimes *
101590Srgrimes * Redistribution and use in source and binary forms, with or without
111590Srgrimes * modification, are permitted provided that the following conditions
121590Srgrimes * are met:
131590Srgrimes * 1. Redistributions of source code must retain the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer.
151590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161590Srgrimes *    notice, this list of conditions and the following disclaimer in the
171590Srgrimes *    documentation and/or other materials provided with the distribution.
181590Srgrimes * 3. All advertising materials mentioning features or use of this software
191590Srgrimes *    must display the following acknowledgement:
201590Srgrimes *	This product includes software developed by the University of
211590Srgrimes *	California, Berkeley and its contributors.
221590Srgrimes * 4. Neither the name of the University nor the names of its contributors
231590Srgrimes *    may be used to endorse or promote products derived from this software
241590Srgrimes *    without specific prior written permission.
251590Srgrimes *
261590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361590Srgrimes * SUCH DAMAGE.
371590Srgrimes */
381590Srgrimes
391590Srgrimes#ifndef lint
4027645Scharnierstatic const char copyright[] =
411590Srgrimes"@(#) Copyright (c) 1987, 1993\n\
421590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
431590Srgrimes#endif /* not lint */
441590Srgrimes
451590Srgrimes#ifndef lint
4627645Scharnier#if 0
471590Srgrimesstatic char sccsid[] = "@(#)mesg.c	8.2 (Berkeley) 1/21/94";
4827645Scharnier#endif
491590Srgrimes#endif /* not lint */
5099112Sobrien#include <sys/cdefs.h>
5199112Sobrien__FBSDID("$FreeBSD$");
521590Srgrimes
531590Srgrimes#include <sys/types.h>
541590Srgrimes#include <sys/stat.h>
551590Srgrimes
561590Srgrimes#include <err.h>
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <stdlib.h>
59200462Sdelphij#include <string.h>
601590Srgrimes#include <unistd.h>
611590Srgrimes
6292921Simpstatic void usage(void);
6327645Scharnier
641590Srgrimesint
65102944Sdwmalonemain(int argc, char *argv[])
661590Srgrimes{
671590Srgrimes	struct stat sb;
681590Srgrimes	char *tty;
691590Srgrimes	int ch;
701590Srgrimes
7124360Simp	while ((ch = getopt(argc, argv, "")) != -1)
721590Srgrimes		switch (ch) {
731590Srgrimes		case '?':
741590Srgrimes		default:
7527645Scharnier			usage();
761590Srgrimes		}
771590Srgrimes	argc -= optind;
781590Srgrimes	argv += optind;
791590Srgrimes
8096093Stjr	if ((tty = ttyname(STDIN_FILENO)) == NULL &&
8196093Stjr	    (tty = ttyname(STDOUT_FILENO)) == NULL &&
8296093Stjr	    (tty = ttyname(STDERR_FILENO)) == NULL)
8396093Stjr		err(2, "ttyname");
841590Srgrimes	if (stat(tty, &sb) < 0)
8596093Stjr		err(2, "%s", tty);
861590Srgrimes
871590Srgrimes	if (*argv == NULL) {
881590Srgrimes		if (sb.st_mode & S_IWGRP) {
8996093Stjr			(void)puts("is y");
901590Srgrimes			exit(0);
911590Srgrimes		}
9296093Stjr		(void)puts("is n");
931590Srgrimes		exit(1);
941590Srgrimes	}
951590Srgrimes
961590Srgrimes	switch (*argv[0]) {
971590Srgrimes	case 'y':
981590Srgrimes		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
9996093Stjr			err(2, "%s", tty);
1001590Srgrimes		exit(0);
1011590Srgrimes	case 'n':
1021590Srgrimes		if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
10396093Stjr			err(2, "%s", tty);
1041590Srgrimes		exit(1);
1051590Srgrimes	}
1061590Srgrimes
10727645Scharnier	usage();
10827645Scharnier	return(0);
10927645Scharnier}
11027645Scharnier
11127645Scharnierstatic void
112102944Sdwmaloneusage(void)
11327645Scharnier{
114146466Sru	(void)fprintf(stderr, "usage: mesg [n | y]\n");
1151590Srgrimes	exit(2);
1161590Srgrimes}
117