3
0

tty.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tty implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config TTY
  10. //config: bool "tty (3.6 kb)"
  11. //config: default y
  12. //config: help
  13. //config: tty is used to print the name of the current terminal to
  14. //config: standard output.
  15. //applet:IF_TTY(APPLET_NOFORK(tty, tty, BB_DIR_USR_BIN, BB_SUID_DROP, tty))
  16. //kbuild:lib-$(CONFIG_TTY) += tty.o
  17. /* BB_AUDIT SUSv4 compliant */
  18. /* http://www.opengroup.org/onlinepubs/9699919799/utilities/tty.html */
  19. //usage:#define tty_trivial_usage
  20. //usage: ""
  21. //usage:#define tty_full_usage "\n\n"
  22. //usage: "Print file name of stdin's terminal"
  23. //usage: IF_INCLUDE_SUSv2( "\n"
  24. //usage: "\n -s Print nothing, only return exit status"
  25. //usage: )
  26. //usage:
  27. //usage:#define tty_example_usage
  28. //usage: "$ tty\n"
  29. //usage: "/dev/tty2\n"
  30. #include "libbb.h"
  31. int tty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int tty_main(int argc UNUSED_PARAM, char **argv)
  33. {
  34. const char *s;
  35. IF_INCLUDE_SUSv2(int silent;) /* Note: No longer relevant in SUSv3. */
  36. int retval;
  37. xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
  38. IF_INCLUDE_SUSv2(silent = getopt32(argv, "s");)
  39. IF_INCLUDE_SUSv2(argv += optind;)
  40. IF_NOT_INCLUDE_SUSv2(argv += 1;)
  41. /* gnu tty outputs a warning that it is ignoring all args. */
  42. bb_warn_ignoring_args(argv[0]);
  43. retval = EXIT_SUCCESS;
  44. s = xmalloc_ttyname(STDIN_FILENO);
  45. if (s == NULL) {
  46. /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
  47. * We know the file descriptor is good, so failure means not a tty. */
  48. s = "not a tty";
  49. retval = EXIT_FAILURE;
  50. }
  51. IF_INCLUDE_SUSv2(if (!silent) puts(s);)
  52. IF_NOT_INCLUDE_SUSv2(puts(s);)
  53. fflush_stdout_and_exit(retval);
  54. }