mesg.c revision 99112
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: head/usr.bin/mesg/mesg.c 99112 2002-06-30 05:25:07Z obrien $");
521590Srgrimes
531590Srgrimes#include <sys/types.h>
541590Srgrimes#include <sys/stat.h>
551590Srgrimes
561590Srgrimes#include <err.h>
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <stdlib.h>
591590Srgrimes#include <string.h>
601590Srgrimes#include <unistd.h>
611590Srgrimes
6292921Simpstatic void usage(void);
6327645Scharnier
641590Srgrimesint
651590Srgrimesmain(argc, argv)
661590Srgrimes	int argc;
671590Srgrimes	char *argv[];
681590Srgrimes{
691590Srgrimes	struct stat sb;
701590Srgrimes	char *tty;
711590Srgrimes	int ch;
721590Srgrimes
7324360Simp	while ((ch = getopt(argc, argv, "")) != -1)
741590Srgrimes		switch (ch) {
751590Srgrimes		case '?':
761590Srgrimes		default:
7727645Scharnier			usage();
781590Srgrimes		}
791590Srgrimes	argc -= optind;
801590Srgrimes	argv += optind;
811590Srgrimes
8296093Stjr	if ((tty = ttyname(STDIN_FILENO)) == NULL &&
8396093Stjr	    (tty = ttyname(STDOUT_FILENO)) == NULL &&
8496093Stjr	    (tty = ttyname(STDERR_FILENO)) == NULL)
8596093Stjr		err(2, "ttyname");
861590Srgrimes	if (stat(tty, &sb) < 0)
8796093Stjr		err(2, "%s", tty);
881590Srgrimes
891590Srgrimes	if (*argv == NULL) {
901590Srgrimes		if (sb.st_mode & S_IWGRP) {
9196093Stjr			(void)puts("is y");
921590Srgrimes			exit(0);
931590Srgrimes		}
9496093Stjr		(void)puts("is n");
951590Srgrimes		exit(1);
961590Srgrimes	}
971590Srgrimes
981590Srgrimes	switch (*argv[0]) {
991590Srgrimes	case 'y':
1001590Srgrimes		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
10196093Stjr			err(2, "%s", tty);
1021590Srgrimes		exit(0);
1031590Srgrimes	case 'n':
1041590Srgrimes		if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
10596093Stjr			err(2, "%s", tty);
1061590Srgrimes		exit(1);
1071590Srgrimes	}
1081590Srgrimes
10927645Scharnier	usage();
11027645Scharnier	return(0);
11127645Scharnier}
11227645Scharnier
11327645Scharnierstatic void
11427645Scharnierusage()
11527645Scharnier{
11627645Scharnier	(void)fprintf(stderr, "usage: mesg [y | n]\n");
1171590Srgrimes	exit(2);
1181590Srgrimes}
119