mesg.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * mesg implementation for busybox
  3. *
  4. * Copyright (c) 2002 Manuel Novoa III <mjn3@codepoet.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include "libbb.h"
  23. #ifdef USE_TTY_GROUP
  24. #define S_IWGRP_OR_S_IWOTH S_IWGRP
  25. #else
  26. #define S_IWGRP_OR_S_IWOTH (S_IWGRP | S_IWOTH)
  27. #endif
  28. extern int mesg_main(int argc, char *argv[])
  29. {
  30. struct stat sb;
  31. char *tty;
  32. char c = 0;
  33. if ((--argc == 0)
  34. || ((argc == 1) && (((c = **++argv) == 'y') || (c == 'n')))) {
  35. if ((tty = ttyname(STDERR_FILENO)) == NULL) {
  36. tty = "ttyname";
  37. } else if (stat(tty, &sb) == 0) {
  38. if (argc == 0) {
  39. puts(((sb.st_mode & (S_IWGRP | S_IWOTH)) ==
  40. 0) ? "is n" : "is y");
  41. return EXIT_SUCCESS;
  42. }
  43. if (chmod
  44. (tty,
  45. (c ==
  46. 'y') ? sb.st_mode | (S_IWGRP_OR_S_IWOTH) : sb.
  47. st_mode & ~(S_IWGRP | S_IWOTH)) == 0) {
  48. return EXIT_SUCCESS;
  49. }
  50. }
  51. bb_perror_msg_and_die("%s", tty);
  52. }
  53. bb_show_usage();
  54. }