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