mesg.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "libbb.h"
  10. #ifdef USE_TTY_GROUP
  11. #define S_IWGRP_OR_S_IWOTH S_IWGRP
  12. #else
  13. #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
  14. #endif
  15. int mesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  16. int mesg_main(int argc, char **argv)
  17. {
  18. struct stat sb;
  19. const char *tty;
  20. char c = 0;
  21. if (--argc == 0
  22. || (argc == 1 && ((c = **++argv) == 'y' || c == 'n'))
  23. ) {
  24. tty = xmalloc_ttyname(STDERR_FILENO);
  25. if (tty == NULL) {
  26. tty = "ttyname";
  27. } else if (stat(tty, &sb) == 0) {
  28. mode_t m;
  29. if (argc == 0) {
  30. puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
  31. return EXIT_SUCCESS;
  32. }
  33. m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
  34. : sb.st_mode & ~(S_IWGRP|S_IWOTH);
  35. if (chmod(tty, m) == 0) {
  36. return EXIT_SUCCESS;
  37. }
  38. }
  39. bb_simple_perror_msg_and_die(tty);
  40. }
  41. bb_show_usage();
  42. }