mesg.c revision 200462
1193323Sed/*
2193323Sed * Copyright (c) 1987, 1993
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed * (c) UNIX System Laboratories, Inc.
5193323Sed * All or some portions of this file are derived from material licensed
6193323Sed * to the University of California by American Telephone and Telegraph
7193323Sed * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8193323Sed * the permission of UNIX System Laboratories, Inc.
9193323Sed *
10193323Sed * Redistribution and use in source and binary forms, with or without
11193323Sed * modification, are permitted provided that the following conditions
12193323Sed * are met:
13193323Sed * 1. Redistributions of source code must retain the above copyright
14193323Sed *    notice, this list of conditions and the following disclaimer.
15252723Sdim * 2. Redistributions in binary form must reproduce the above copyright
16218893Sdim *    notice, this list of conditions and the following disclaimer in the
17218893Sdim *    documentation and/or other materials provided with the distribution.
18252723Sdim * 3. All advertising materials mentioning features or use of this software
19252723Sdim *    must display the following acknowledgement:
20252723Sdim *	This product includes software developed by the University of
21198090Srdivacky *	California, Berkeley and its contributors.
22252723Sdim * 4. Neither the name of the University nor the names of its contributors
23193323Sed *    may be used to endorse or promote products derived from this software
24195340Sed *    without specific prior written permission.
25193323Sed *
26218893Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32218893Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33235633Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36193323Sed * SUCH DAMAGE.
37193323Sed */
38193323Sed
39193323Sed#ifndef lint
40193323Sedstatic const char copyright[] =
41193323Sed"@(#) Copyright (c) 1987, 1993\n\
42193323Sed	The Regents of the University of California.  All rights reserved.\n";
43193323Sed#endif /* not lint */
44193323Sed
45193323Sed#ifndef lint
46193323Sed#if 0
47193323Sedstatic char sccsid[] = "@(#)mesg.c	8.2 (Berkeley) 1/21/94";
48193323Sed#endif
49193323Sed#endif /* not lint */
50193323Sed#include <sys/cdefs.h>
51193323Sed__FBSDID("$FreeBSD: head/usr.bin/mesg/mesg.c 200462 2009-12-13 03:14:06Z delphij $");
52193323Sed
53193323Sed#include <sys/types.h>
54193323Sed#include <sys/stat.h>
55193323Sed
56193323Sed#include <err.h>
57193323Sed#include <stdio.h>
58193323Sed#include <stdlib.h>
59193323Sed#include <string.h>
60193323Sed#include <unistd.h>
61193323Sed
62193323Sedstatic void usage(void);
63193323Sed
64252723Sdimint
65193323Sedmain(int argc, char *argv[])
66193323Sed{
67193323Sed	struct stat sb;
68193323Sed	char *tty;
69193323Sed	int ch;
70193323Sed
71193323Sed	while ((ch = getopt(argc, argv, "")) != -1)
72193323Sed		switch (ch) {
73193323Sed		case '?':
74193323Sed		default:
75193323Sed			usage();
76193323Sed		}
77193323Sed	argc -= optind;
78193323Sed	argv += optind;
79193323Sed
80252723Sdim	if ((tty = ttyname(STDIN_FILENO)) == NULL &&
81193323Sed	    (tty = ttyname(STDOUT_FILENO)) == NULL &&
82193323Sed	    (tty = ttyname(STDERR_FILENO)) == NULL)
83193323Sed		err(2, "ttyname");
84193323Sed	if (stat(tty, &sb) < 0)
85252723Sdim		err(2, "%s", tty);
86193323Sed
87193323Sed	if (*argv == NULL) {
88193323Sed		if (sb.st_mode & S_IWGRP) {
89193323Sed			(void)puts("is y");
90193323Sed			exit(0);
91193323Sed		}
92193323Sed		(void)puts("is n");
93193323Sed		exit(1);
94193323Sed	}
95193323Sed
96193323Sed	switch (*argv[0]) {
97193323Sed	case 'y':
98193323Sed		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
99252723Sdim			err(2, "%s", tty);
100193323Sed		exit(0);
101193323Sed	case 'n':
102193323Sed		if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
103193323Sed			err(2, "%s", tty);
104252723Sdim		exit(1);
105193323Sed	}
106193323Sed
107193323Sed	usage();
108193323Sed	return(0);
109193323Sed}
110193323Sed
111193323Sedstatic void
112193323Sedusage(void)
113193323Sed{
114193323Sed	(void)fprintf(stderr, "usage: mesg [n | y]\n");
115193323Sed	exit(2);
116193323Sed}
117193323Sed