xref: /original-bsd/usr.bin/mesg/mesg.c (revision 4a884f8b)
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.3 (Berkeley) 12/21/92";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 void err __P((const char *fmt, ...));
28 void usage __P((void));
29 
30 int
31 main(argc, argv)
32 	int argc;
33 	char *argv[];
34 {
35 	struct stat sb;
36 	char *tty;
37 	int ch;
38 
39 	while ((ch = getopt(argc, argv, "")) != EOF)
40 		switch (ch) {
41 		case '?':
42 		default:
43 			usage();
44 		}
45 	argc -= optind;
46 	argv += optind;
47 
48 	if ((tty = ttyname(STDERR_FILENO)) == NULL)
49 		err("ttyname: %s", strerror(errno));
50 	if (stat(tty, &sb) < 0)
51 		err("%s: %s", strerror(errno));
52 
53 	if (*argv == NULL) {
54 		if (sb.st_mode & S_IWGRP) {
55 			(void)fprintf(stderr, "is y\n");
56 			exit(0);
57 		}
58 		(void)fprintf(stderr, "is n\n");
59 		exit(1);
60 	}
61 	switch (*argv[0]) {
62 	case 'y':
63 		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
64 			err("%s: %s", strerror(errno));
65 		exit(0);
66 	case 'n':
67 		if (chmod(tty, sb.st_mode & ~S_IWGRP) < 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