xref: /original-bsd/usr.bin/mesg/mesg.c (revision 333da485)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  */
12 
13 #ifndef lint
14 static char copyright[] =
15 "@(#) Copyright (c) 1987, 1993\n\
16 	The Regents of the University of California.  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)mesg.c	8.2 (Berkeley) 01/21/94";
21 #endif /* not lint */
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 
26 #include <err.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 int
34 main(argc, argv)
35 	int argc;
36 	char *argv[];
37 {
38 	struct stat sb;
39 	char *tty;
40 	int ch;
41 
42 	while ((ch = getopt(argc, argv, "")) != EOF)
43 		switch (ch) {
44 		case '?':
45 		default:
46 			goto usage;
47 		}
48 	argc -= optind;
49 	argv += optind;
50 
51 	if ((tty = ttyname(STDERR_FILENO)) == NULL)
52 		err(1, "ttyname");
53 	if (stat(tty, &sb) < 0)
54 		err(1, "%s", tty);
55 
56 	if (*argv == NULL) {
57 		if (sb.st_mode & S_IWGRP) {
58 			(void)fprintf(stderr, "is y\n");
59 			exit(0);
60 		}
61 		(void)fprintf(stderr, "is n\n");
62 		exit(1);
63 	}
64 
65 	switch (*argv[0]) {
66 	case 'y':
67 		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
68 			err(1, "%s", tty);
69 		exit(0);
70 	case 'n':
71 		if (chmod(tty, sb.st_mode & ~S_IWGRP) < 0)
72 			err(1, "%s", tty);
73 		exit(1);
74 	}
75 
76 usage:	(void)fprintf(stderr, "usage: mesg [y | n]\n");
77 	exit(2);
78 }
79