mesg.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 UNUSED_PARAM, char **argv)
  17. {
  18. struct stat sb;
  19. const char *tty;
  20. char c = 0;
  21. argv++;
  22. if (!argv[0]
  23. || (!argv[1] && ((c = argv[0][0]) == 'y' || c == 'n'))
  24. ) {
  25. tty = xmalloc_ttyname(STDERR_FILENO);
  26. if (tty == NULL) {
  27. tty = "ttyname";
  28. } else if (stat(tty, &sb) == 0) {
  29. mode_t m;
  30. if (c == 0) {
  31. puts((sb.st_mode & (S_IWGRP|S_IWOTH)) ? "is y" : "is n");
  32. return EXIT_SUCCESS;
  33. }
  34. m = (c == 'y') ? sb.st_mode | S_IWGRP_OR_S_IWOTH
  35. : sb.st_mode & ~(S_IWGRP|S_IWOTH);
  36. if (chmod(tty, m) == 0) {
  37. return EXIT_SUCCESS;
  38. }
  39. }
  40. bb_simple_perror_msg_and_die(tty);
  41. }
  42. bb_show_usage();
  43. }