xref: /original-bsd/usr.bin/mesg/mesg.c (revision 4da674f5)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)mesg.c	4.7 (Berkeley) 03/01/91";
16 #endif /* not lint */
17 
18 /*
19  * mesg -- set current tty to accept or
20  *	forbid write permission.
21  *
22  *	mesg [y] [n]
23  *		y allow messages
24  *		n forbid messages
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 
31 static char *tty;
32 
33 main(argc, argv)
34 	int argc;
35 	char **argv;
36 {
37 	struct stat sbuf;
38 	char *ttyname();
39 
40 	if (!(tty = ttyname(2))) {
41 		fputs("mesg: not a device in /dev.\n", stderr);
42 		exit(-1);
43 	}
44 	if (stat(tty, &sbuf) < 0) {
45 		perror("mesg");
46 		exit(-1);
47 	}
48 	if (argc < 2) {
49 		if (sbuf.st_mode & 020) {
50 			fputs("is y\n", stderr);
51 			exit(0);
52 		}
53 		fputs("is n\n", stderr);
54 		exit(1);
55 	}
56 #define	OTHER_WRITE	020
57 	switch(*argv[1]) {
58 	case 'y':
59 		newmode(sbuf.st_mode | OTHER_WRITE);
60 		exit(0);
61 	case 'n':
62 		newmode(sbuf.st_mode &~ OTHER_WRITE);
63 		exit(1);
64 	default:
65 		fputs("usage: mesg [y] [n]\n", stderr);
66 		exit(-1);
67 	}
68 	/*NOTREACHED*/
69 }
70 
71 newmode(m)
72 	u_short m;
73 {
74 	if (chmod(tty, m) < 0) {
75 		perror("mesg");
76 		exit(-1);
77 	}
78 }
79