xref: /illumos-gate/usr/src/cmd/mesg/mesg.c (revision fc910014e8a32a65612105835a10995f2c13d942)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 /*
32  * mesg -- set current tty to accept or
33  *	forbid write permission.
34  *
35  *	mesg [-y | -n | y | n]
36  *		y allow messages
37  *		n forbid messages
38  *	return codes
39  *		0 if messages are ON or turned ON
40  *		1 if messages are OFF or turned OFF
41  *		2 if an error occurs
42  */
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <locale.h>
48 #include <libintl.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 
52 static void error(const char *s);
53 static void newmode(mode_t m);
54 static void usage(void);
55 
56 static char *tty;
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	int i, c, r = 0;
62 	int action = 0;
63 	struct stat sbuf;
64 
65 	extern int optind;
66 
67 	(void) setlocale(LC_ALL, "");
68 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
69 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
70 #endif
71 	(void) textdomain(TEXT_DOMAIN);
72 
73 	/*
74 	 * Check stdin, stdout and stderr, in order, for a tty
75 	 */
76 	for (i = 0; i <= 2; i++) {
77 		if ((tty = ttyname(i)) != NULL)
78 			break;
79 	}
80 
81 	if (stat(tty, &sbuf) < 0)
82 		error("cannot stat");
83 
84 	if (argc < 2) {
85 		if (sbuf.st_mode & (S_IWGRP | S_IWOTH)) {
86 			(void) printf("is y\n");
87 		} else {
88 			r = 1;
89 			(void) printf("is n\n");
90 		}
91 		exit(r);
92 	}
93 
94 	while ((c = getopt(argc, argv, "yn")) != EOF) {
95 		switch (c) {
96 		case 'y':
97 			if (action > 0)
98 				usage();
99 
100 			newmode(S_IRUSR | S_IWUSR | S_IWGRP);
101 			action++;
102 			break;
103 
104 		case 'n':
105 			if (action > 0)
106 				usage();
107 
108 			newmode(S_IRUSR | S_IWUSR);
109 			r = 1;
110 			action++;
111 			break;
112 
113 		case '?':
114 			usage();
115 			break;
116 		}
117 	}
118 
119 	/*
120 	 * Required for POSIX.2
121 	 */
122 	if (argc > optind) {
123 		if (action > 0)
124 			usage();
125 
126 		switch (*argv[optind]) {
127 		case 'y':
128 			newmode(S_IRUSR | S_IWUSR | S_IWGRP);
129 			break;
130 
131 		case 'n':
132 			newmode(S_IRUSR | S_IWUSR);
133 			r = 1;
134 			break;
135 
136 		default:
137 			usage();
138 			break;
139 		}
140 	}
141 
142 	return (r);
143 }
144 
145 void
146 error(const char *s)
147 {
148 	(void) fprintf(stderr, "mesg: ");
149 	(void) fprintf(stderr, "%s\n", s);
150 	exit(2);
151 }
152 
153 void
154 newmode(mode_t m)
155 {
156 	if (chmod(tty, m) < 0)
157 		error("cannot change mode");
158 }
159 
160 void
161 usage(void)
162 {
163 	(void) fprintf(stderr, gettext("usage: mesg [-y | -n | y | n]\n"));
164 	exit(2);
165 }
166