blob: e182b4153604093487af1147de2552b69bdfc150 [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 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000018int mesg_main(int argc, char **argv);
19int mesg_main(int argc, 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
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000025 if (--argc == 0
26 || (argc == 1 && ((c = **++argv) == 'y' || c == 'n'))
27 ) {
28 tty = ttyname(STDERR_FILENO);
29 if (tty == NULL) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000030 tty = "ttyname";
31 } else if (stat(tty, &sb) == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000032 mode_t m;
Glenn L McGrath061c9002002-09-16 04:21:46 +000033 if (argc == 0) {
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000034 puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
Glenn L McGrath061c9002002-09-16 04:21:46 +000035 return EXIT_SUCCESS;
36 }
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000037 m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
38 : sb.st_mode & ~(S_IWGRP|S_IWOTH);
39 if (chmod(tty, m) == 0) {
Glenn L McGrath061c9002002-09-16 04:21:46 +000040 return EXIT_SUCCESS;
41 }
42 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 bb_perror_msg_and_die("%s", tty);
Glenn L McGrath061c9002002-09-16 04:21:46 +000044 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 bb_show_usage();
Glenn L McGrath061c9002002-09-16 04:21:46 +000046}