xref: /minix3/usr.bin/mesg/mesg.c (revision b36343fc42f8e1ea4036ffebd97257177e11005c)
1*b36343fcSThomas Cort /*	$NetBSD: mesg.c,v 1.8 2008/07/21 14:19:24 lukem Exp $	*/
2*b36343fcSThomas Cort 
3*b36343fcSThomas Cort /*
4*b36343fcSThomas Cort  * Copyright (c) 1987, 1993
5*b36343fcSThomas Cort  *	The Regents of the University of California.  All rights reserved.
6*b36343fcSThomas Cort  * (c) UNIX System Laboratories, Inc.
7*b36343fcSThomas Cort  * All or some portions of this file are derived from material licensed
8*b36343fcSThomas Cort  * to the University of California by American Telephone and Telegraph
9*b36343fcSThomas Cort  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10*b36343fcSThomas Cort  * the permission of UNIX System Laboratories, Inc.
11*b36343fcSThomas Cort  *
12*b36343fcSThomas Cort  * Redistribution and use in source and binary forms, with or without
13*b36343fcSThomas Cort  * modification, are permitted provided that the following conditions
14*b36343fcSThomas Cort  * are met:
15*b36343fcSThomas Cort  * 1. Redistributions of source code must retain the above copyright
16*b36343fcSThomas Cort  *    notice, this list of conditions and the following disclaimer.
17*b36343fcSThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
18*b36343fcSThomas Cort  *    notice, this list of conditions and the following disclaimer in the
19*b36343fcSThomas Cort  *    documentation and/or other materials provided with the distribution.
20*b36343fcSThomas Cort  * 3. Neither the name of the University nor the names of its contributors
21*b36343fcSThomas Cort  *    may be used to endorse or promote products derived from this software
22*b36343fcSThomas Cort  *    without specific prior written permission.
23*b36343fcSThomas Cort  *
24*b36343fcSThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*b36343fcSThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*b36343fcSThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*b36343fcSThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*b36343fcSThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*b36343fcSThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*b36343fcSThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*b36343fcSThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*b36343fcSThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*b36343fcSThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*b36343fcSThomas Cort  * SUCH DAMAGE.
35*b36343fcSThomas Cort  */
36*b36343fcSThomas Cort 
37*b36343fcSThomas Cort #include <sys/cdefs.h>
38*b36343fcSThomas Cort 
39*b36343fcSThomas Cort #ifndef lint
40*b36343fcSThomas Cort __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
41*b36343fcSThomas Cort  The Regents of the University of California.  All rights reserved.");
42*b36343fcSThomas Cort #endif /* not lint */
43*b36343fcSThomas Cort 
44*b36343fcSThomas Cort #ifndef lint
45*b36343fcSThomas Cort #if 0
46*b36343fcSThomas Cort static char sccsid[] = "@(#)mesg.c	8.2 (Berkeley) 1/21/94";
47*b36343fcSThomas Cort #endif
48*b36343fcSThomas Cort __RCSID("$NetBSD: mesg.c,v 1.8 2008/07/21 14:19:24 lukem Exp $");
49*b36343fcSThomas Cort #endif /* not lint */
50*b36343fcSThomas Cort 
51*b36343fcSThomas Cort #include <sys/types.h>
52*b36343fcSThomas Cort #include <sys/stat.h>
53*b36343fcSThomas Cort 
54*b36343fcSThomas Cort #include <err.h>
55*b36343fcSThomas Cort #include <errno.h>
56*b36343fcSThomas Cort #include <stdio.h>
57*b36343fcSThomas Cort #include <stdlib.h>
58*b36343fcSThomas Cort #include <string.h>
59*b36343fcSThomas Cort #include <unistd.h>
60*b36343fcSThomas Cort 
61*b36343fcSThomas Cort int
main(int argc,char * argv[])62*b36343fcSThomas Cort main(int argc, char *argv[])
63*b36343fcSThomas Cort {
64*b36343fcSThomas Cort 	struct stat sb;
65*b36343fcSThomas Cort 	char *tty;
66*b36343fcSThomas Cort 	int ch;
67*b36343fcSThomas Cort 
68*b36343fcSThomas Cort 	setprogname(*argv);
69*b36343fcSThomas Cort 
70*b36343fcSThomas Cort 	while ((ch = getopt(argc, argv, "")) != -1)
71*b36343fcSThomas Cort 		switch (ch) {
72*b36343fcSThomas Cort 		case '?':
73*b36343fcSThomas Cort 		default:
74*b36343fcSThomas Cort 			goto usage;
75*b36343fcSThomas Cort 		}
76*b36343fcSThomas Cort 	argc -= optind;
77*b36343fcSThomas Cort 	argv += optind;
78*b36343fcSThomas Cort 
79*b36343fcSThomas Cort 	if ((tty = ttyname(STDIN_FILENO)) == NULL &&
80*b36343fcSThomas Cort 	    (tty = ttyname(STDOUT_FILENO)) == NULL &&
81*b36343fcSThomas Cort 	    (tty = ttyname(STDERR_FILENO)) == NULL)
82*b36343fcSThomas Cort 		err(2, "ttyname");
83*b36343fcSThomas Cort 	if (stat(tty, &sb) == -1)
84*b36343fcSThomas Cort 		err(2, "%s", tty);
85*b36343fcSThomas Cort 
86*b36343fcSThomas Cort 	if (*argv == NULL) {
87*b36343fcSThomas Cort 		if (sb.st_mode & S_IWGRP) {
88*b36343fcSThomas Cort 			(void)fprintf(stderr, "is y\n");
89*b36343fcSThomas Cort 			return 0;
90*b36343fcSThomas Cort 		}
91*b36343fcSThomas Cort 		(void)fprintf(stderr, "is n\n");
92*b36343fcSThomas Cort 		return 1;
93*b36343fcSThomas Cort 	}
94*b36343fcSThomas Cort 
95*b36343fcSThomas Cort 	switch (*argv[0]) {
96*b36343fcSThomas Cort 	case 'y':
97*b36343fcSThomas Cort 		if (chmod(tty, sb.st_mode | S_IWGRP) == -1)
98*b36343fcSThomas Cort 			err(2, "%s", tty);
99*b36343fcSThomas Cort 		return 0;
100*b36343fcSThomas Cort 	case 'n':
101*b36343fcSThomas Cort 		if (chmod(tty, sb.st_mode & ~S_IWGRP) == -1)
102*b36343fcSThomas Cort 			err(2, "%s", tty);
103*b36343fcSThomas Cort 		return 1;
104*b36343fcSThomas Cort 	}
105*b36343fcSThomas Cort 
106*b36343fcSThomas Cort usage:	(void)fprintf(stderr, "Usage: %s [y | n]\n", getprogname());
107*b36343fcSThomas Cort 	return 2;
108*b36343fcSThomas Cort }
109