blob: b560df205f7ea3d5e63c50d69f1ad65ec45bb783 [file] [log] [blame]
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath061c9002002-09-16 04:21:46 +00002/*
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00003 * mesg implementation for busybox
Glenn L McGrath061c9002002-09-16 04:21:46 +00004 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00005 * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org>
Glenn L McGrath061c9002002-09-16 04:21:46 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath061c9002002-09-16 04:21:46 +00008 */
9
Denys Vlasenkob1db09b2010-10-12 13:47:15 +020010//applet:IF_MESG(APPLET(mesg, _BB_DIR_USR_BIN, _BB_SUID_DROP))
11
12//kbuild:lib-$(CONFIG_MESG) += mesg.o
13
14//config:config MESG
15//config: bool "mesg"
16//config: default y
17//config: help
18//config: Mesg controls access to your terminal by others. It is typically
19//config: used to allow or disallow other users to write to your terminal
20
21//usage:#define mesg_trivial_usage
22//usage: "[y|n]"
23//usage:#define mesg_full_usage "\n\n"
24//usage: "Control write access to your terminal\n"
25//usage: " y Allow write access to your terminal\n"
26//usage: " n Disallow write access to your terminal"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Glenn L McGrath061c9002002-09-16 04:21:46 +000029
30#ifdef USE_TTY_GROUP
31#define S_IWGRP_OR_S_IWOTH S_IWGRP
32#else
33#define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
34#endif
35
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010037int mesg_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath061c9002002-09-16 04:21:46 +000038{
39 struct stat sb;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000040 const char *tty;
Eric Andersen78500142004-08-27 19:55:28 +000041 char c = 0;
Glenn L McGrath061c9002002-09-16 04:21:46 +000042
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010043 argv++;
44
45 if (!argv[0]
46 || (!argv[1] && ((c = argv[0][0]) == 'y' || c == 'n'))
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000047 ) {
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +000048 tty = xmalloc_ttyname(STDERR_FILENO);
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000049 if (tty == NULL) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000050 tty = "ttyname";
51 } else if (stat(tty, &sb) == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000052 mode_t m;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010053 if (c == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000054 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
Glenn L McGrath061c9002002-09-16 04:21:46 +000055 return EXIT_SUCCESS;
56 }
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000057 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
58 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
59 if (chmod(tty, m) == 0) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000060 return EXIT_SUCCESS;
61 }
62 }
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000063 bb_simple_perror_msg_and_die(tty);
Glenn L McGrath061c9002002-09-16 04:21:46 +000064 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 bb_show_usage();
Glenn L McGrath061c9002002-09-16 04:21:46 +000066}