658c82447057900702249211d3e18e818ec03a5f
[oweals/busybox.git] / init / mesg.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mesg implementation for busybox
4  *
5  * Copyright (c) 2002 Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 #include "busybox.h"
11
12 #ifdef USE_TTY_GROUP
13 #define S_IWGRP_OR_S_IWOTH      S_IWGRP
14 #else
15 #define S_IWGRP_OR_S_IWOTH      (S_IWGRP | S_IWOTH)
16 #endif
17
18 int mesg_main(int argc, char *argv[])
19 {
20         struct stat sb;
21         const char *tty;
22         char c = 0;
23
24         if (--argc == 0
25          || (argc == 1 && ((c = **++argv) == 'y' || c == 'n'))
26         ) {
27                 tty = ttyname(STDERR_FILENO);
28                 if (tty == NULL) {
29                         tty = "ttyname";
30                 } else if (stat(tty, &sb) == 0) {
31                         mode_t m;
32                         if (argc == 0) {
33                                 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
34                                 return EXIT_SUCCESS;
35                         }
36                         m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
37                                        : sb.st_mode & ~(S_IWGRP|S_IWOTH);
38                         if (chmod(tty, m) == 0) {
39                                 return EXIT_SUCCESS;
40                         }
41                 }
42                 bb_perror_msg_and_die("%s", tty);
43         }
44         bb_show_usage();
45 }