mesg.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //applet:IF_MESG(APPLET(mesg, _BB_DIR_USR_BIN, _BB_SUID_DROP))
  10. //kbuild:lib-$(CONFIG_MESG) += mesg.o
  11. //config:config MESG
  12. //config: bool "mesg"
  13. //config: default y
  14. //config: help
  15. //config: Mesg controls access to your terminal by others. It is typically
  16. //config: used to allow or disallow other users to write to your terminal
  17. //usage:#define mesg_trivial_usage
  18. //usage: "[y|n]"
  19. //usage:#define mesg_full_usage "\n\n"
  20. //usage: "Control write access to your terminal\n"
  21. //usage: " y Allow write access to your terminal\n"
  22. //usage: " n Disallow write access to your terminal"
  23. #include "libbb.h"
  24. #ifdef USE_TTY_GROUP
  25. #define S_IWGRP_OR_S_IWOTH S_IWGRP
  26. #else
  27. #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
  28. #endif
  29. int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  30. int mesg_main(int argc UNUSED_PARAM, char **argv)
  31. {
  32. struct stat sb;
  33. const char *tty;
  34. char c = 0;
  35. argv++;
  36. if (!argv[0]
  37. || (!argv[1] && ((c = argv[0][0]) == 'y' || c == 'n'))
  38. ) {
  39. tty = xmalloc_ttyname(STDERR_FILENO);
  40. if (tty == NULL) {
  41. tty = "ttyname";
  42. } else if (stat(tty, &sb) == 0) {
  43. mode_t m;
  44. if (c == 0) {
  45. puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
  46. return EXIT_SUCCESS;
  47. }
  48. m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
  49. : sb.st_mode & ~(S_IWGRP|S_IWOTH);
  50. if (chmod(tty, m) == 0) {
  51. return EXIT_SUCCESS;
  52. }
  53. }
  54. bb_simple_perror_msg_and_die(tty);
  55. }
  56. bb_show_usage();
  57. }