blob: ef5f0eb66f8e780bf122cfee4d4b269a48418505 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Glenn L McGrath061c9002002-09-16 04:21:46 +000011
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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000018int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010019int mesg_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath061c9002002-09-16 04:21:46 +000020{
21 struct stat sb;
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000022 const char *tty;
Eric Andersen78500142004-08-27 19:55:28 +000023 char c = 0;
Glenn L McGrath061c9002002-09-16 04:21:46 +000024
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010025 argv++;
26
27 if (!argv[0]
28 || (!argv[1] && ((c = argv[0][0]) == 'y' || c == 'n'))
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000029 ) {
Denis Vlasenko4e12b1a2008-12-23 23:36:47 +000030 tty = xmalloc_ttyname(STDERR_FILENO);
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000031 if (tty == NULL) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000032 tty = "ttyname";
33 } else if (stat(tty, &sb) == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000034 mode_t m;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +010035 if (c == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000036 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
Glenn L McGrath061c9002002-09-16 04:21:46 +000037 return EXIT_SUCCESS;
38 }
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000039 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
40 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
41 if (chmod(tty, m) == 0) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000042 return EXIT_SUCCESS;
43 }
44 }
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000045 bb_simple_perror_msg_and_die(tty);
Glenn L McGrath061c9002002-09-16 04:21:46 +000046 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_show_usage();
Glenn L McGrath061c9002002-09-16 04:21:46 +000048}