tty.c 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tty.html */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include "busybox.h"
  15. int tty_main(int argc, char **argv)
  16. {
  17. const char *s;
  18. int silent; /* Note: No longer relevant in SUSv3. */
  19. int retval;
  20. xfunc_error_retval = 2; /* SUSv3 requires > 1 for error. */
  21. silent = getopt32(argc, argv, "s");
  22. /* gnu tty outputs a warning that it is ignoring all args. */
  23. bb_warn_ignoring_args(argc - optind);
  24. retval = 0;
  25. if ((s = ttyname(0)) == NULL) {
  26. /* According to SUSv3, ttyname can on fail with EBADF or ENOTTY.
  27. * We know the file descriptor is good, so failure means not a tty. */
  28. s = "not a tty";
  29. retval = 1;
  30. }
  31. if (!silent) {
  32. puts(s);
  33. }
  34. fflush_stdout_and_exit(retval);
  35. }