xref: /original-bsd/usr.bin/mesg/mesg.c (revision a4d3ae46)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific written prior permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 char copyright[] =
15 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
16  All rights reserved.\n";
17 #endif /* not lint */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)mesg.c	4.4 (Berkeley) 11/24/87";
21 #endif /* not lint */
22 
23 /*
24  * mesg -- set current tty to accept or
25  *	forbid write permission.
26  *
27  *	mesg [y] [n]
28  *		y allow messages
29  *		n forbid messages
30  */
31 
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <stdio.h>
35 
36 static char *tty;
37 
38 main(argc, argv)
39 	int argc;
40 	char **argv;
41 {
42 	struct stat sbuf;
43 	char *ttyname();
44 
45 	if (!(tty = ttyname(2))) {
46 		fputs("mesg: not a device in /dev.\n", stderr);
47 		exit(-1);
48 	}
49 	if (stat(tty, &sbuf) < 0) {
50 		perror("mesg");
51 		exit(-1);
52 	}
53 	if (argc < 2) {
54 		if (sbuf.st_mode & 020) {
55 			fputs("is y\n", stderr);
56 			exit(0);
57 		}
58 		fputs("is n\n", stderr);
59 		exit(1);
60 	}
61 #define	OTHER_WRITE	020
62 	switch(*argv[1]) {
63 	case 'y':
64 		newmode(sbuf.st_mode | OTHER_WRITE);
65 		exit(0);
66 	case 'n':
67 		newmode(sbuf.st_mode &~ OTHER_WRITE);
68 		exit(1);
69 	default:
70 		fputs("usage: mesg [y] [n]\n", stderr);
71 		exit(-1);
72 	}
73 	/*NOTREACHED*/
74 }
75 
76 static
77 newmode(m)
78 	u_short m;
79 {
80 	if (chmod(tty, m) < 0) {
81 		perror("mesg");
82 		exit(-1);
83 	}
84 }
85