new copyright notice
[unix-history] / usr / src / usr.bin / mesg / mesg.c
CommitLineData
56b5a10f
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
56b5a10f
KB
6 */
7
8#ifndef lint
9char 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
f15db449 15static char sccsid[] = "@(#)mesg.c 4.6 (Berkeley) %G%";
56b5a10f
KB
16#endif /* not lint */
17
69d8f2f4
BJ
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
69d8f2f4
BJ
27#include <sys/types.h>
28#include <sys/stat.h>
56b5a10f 29#include <stdio.h>
69d8f2f4 30
56b5a10f 31static char *tty;
69d8f2f4
BJ
32
33main(argc, argv)
56b5a10f
KB
34 int argc;
35 char **argv;
69d8f2f4 36{
56b5a10f
KB
37 struct stat sbuf;
38 char *ttyname();
69d8f2f4 39
56b5a10f
KB
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);
69d8f2f4 52 }
56b5a10f
KB
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*/
69d8f2f4
BJ
69}
70
56b5a10f 71static
69d8f2f4 72newmode(m)
56b5a10f 73 u_short m;
69d8f2f4 74{
56b5a10f
KB
75 if (chmod(tty, m) < 0) {
76 perror("mesg");
77 exit(-1);
78 }
69d8f2f4 79}