xref: /original-bsd/usr.bin/mesg/mesg.c (revision 5133e8a4)
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	5.2 (Berkeley) 11/13/91";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 void err __P((const char *fmt, ...));
27 void usage __P((void));
28 
29 int
30 main(argc, argv)
31 	int argc;
32 	char *argv[];
33 {
34 	struct stat sbuf;
35 	char *tty;
36 	int ch;
37 
38 	while ((ch = getopt(argc, argv, "")) != EOF)
39 		switch(ch) {
40 		case '?':
41 		default:
42 			usage();
43 		}
44 	argc -= optind;
45 	argv += optind;
46 
47 	if (!(tty = ttyname(2)))
48 		err("ttyname: %s", strerror(errno));
49 	if (stat(tty, &sbuf) < 0)
50 		err("%s: %s", strerror(errno));
51 
52 	if (*argv == NULL) {
53 		if (sbuf.st_mode & 020) {
54 			(void)fprintf(stderr, "is y\n");
55 			exit(0);
56 		}
57 		(void)fprintf(stderr, "is n\n");
58 		exit(1);
59 	}
60 #define	OTHER_WRITE	020
61 	switch(*argv[0]) {
62 	case 'y':
63 		if (chmod(tty, sbuf.st_mode | OTHER_WRITE) < 0)
64 			err("%s: %s", strerror(errno));
65 		exit(0);
66 	case 'n':
67 		if (chmod(tty, sbuf.st_mode &~ OTHER_WRITE) < 0)
68 			err("%s: %s", strerror(errno));
69 		exit(1);
70 	}
71 	usage();
72 	/* NOTREACHED */
73 }
74 
75 void
76 usage()
77 {
78 	(void)fprintf(stderr, "usage: mesg [y | n]\n");
79 	exit(2);
80 }
81 
82 #if __STDC__
83 #include <stdarg.h>
84 #else
85 #include <varargs.h>
86 #endif
87 
88 void
89 #if __STDC__
90 err(const char *fmt, ...)
91 #else
92 err(fmt, va_alist)
93 	char *fmt;
94 	va_dcl
95 #endif
96 {
97 	va_list ap;
98 #if __STDC__
99 	va_start(ap, fmt);
100 #else
101 	va_start(ap);
102 #endif
103 	(void)fprintf(stderr, "mesg: ");
104 	(void)vfprintf(stderr, fmt, ap);
105 	va_end(ap);
106 	(void)fprintf(stderr, "\n");
107 	exit(2);
108 	/* NOTREACHED */
109 }
110