master xplshn/aruu / cmd / posix / mesg.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <sys/stat.h>
 5#include <sys/types.h>
 6
 7#include <stdio.h>
 8#include <stdlib.h>
 9#include <unistd.h>
10
11#include "util.h"
12
13static void
14usage(void)
15{
16	eprintf("usage: %s [n|y]\n", argv0);
17}
18
19// ?man mesg: control write access
20// ?man arguments: n|y
21// ?man allow or disallow other users to write to the terminal
22int
23main(int argc, char *argv[])
24{
25	struct stat sb;
26	mode_t mode;
27
28	ARGBEGIN {
29	default:
30		usage();
31	} ARGEND;
32
33	if (argc > 1)
34		usage();
35
36	if (isatty(2) == 0)
37		eprintf("stderr: not a tty\n");
38
39	if (fstat(2, &sb) < 0)
40		eprintf("fstat stderr:");
41
42	if (argc == 0) {
43		puts(sb.st_mode & (S_IWGRP | S_IWOTH) ? "is y" : "is n");
44		return 0;
45	}
46
47	if (argv[0][0] == 'y' && argv[0][1] == '\0')
48		mode = sb.st_mode | S_IWGRP | S_IWOTH;
49	else if (argv[0][0] == 'n' && argv[0][1] == '\0')
50		mode = sb.st_mode & ~(S_IWGRP | S_IWOTH);
51	else
52		usage();
53
54	if (fchmod(2, mode) < 0)
55		eprintf("fchmod stderr:");
56
57	return 0;
58}