mesg.c 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "busybox.h"
  10. #include <unistd.h>
  11. #include <stdlib.h>
  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. int mesg_main(int argc, char *argv[])
  18. {
  19. struct stat sb;
  20. char *tty;
  21. char c = 0;
  22. if ((--argc == 0)
  23. || ((argc == 1) && (((c = **++argv) == 'y') || (c == 'n')))) {
  24. if ((tty = ttyname(STDERR_FILENO)) == NULL) {
  25. tty = "ttyname";
  26. } else if (stat(tty, &sb) == 0) {
  27. if (argc == 0) {
  28. puts(((sb.st_mode & (S_IWGRP | S_IWOTH)) ==
  29. 0) ? "is n" : "is y");
  30. return EXIT_SUCCESS;
  31. }
  32. if (chmod
  33. (tty,
  34. (c ==
  35. 'y') ? sb.st_mode | (S_IWGRP_OR_S_IWOTH) : sb.
  36. st_mode & ~(S_IWGRP | S_IWOTH)) == 0) {
  37. return EXIT_SUCCESS;
  38. }
  39. }
  40. bb_perror_msg_and_die("%s", tty);
  41. }
  42. bb_show_usage();
  43. }