getpass.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <termios.h>
  17. #include <unistd.h>
  18. #include <malloc.h>
  19. #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
  20. /* It is desirable to use this bit on systems that have it.
  21. The only bit of terminal state we want to twiddle is echoing, which is
  22. done in software; there is no need to change the state of the terminal
  23. hardware. */
  24. #ifndef TCSASOFT
  25. #define TCSASOFT 0
  26. #endif
  27. #define PWD_BUFFER_SIZE 256
  28. char * getpass (const char *prompt)
  29. {
  30. FILE *in, *out;
  31. struct termios s, t;
  32. int tty_changed;
  33. static char *buf = NULL;
  34. int nread;
  35. if (buf == NULL)
  36. buf = (char *)__uc_malloc(PWD_BUFFER_SIZE);
  37. /* Try to write to and read from the terminal if we can.
  38. If we can't open the terminal, use stderr and stdin. */
  39. out = in = fopen ("/dev/tty", "r+");
  40. if (in == NULL)
  41. {
  42. in = stdin;
  43. out = stderr;
  44. }
  45. else
  46. {
  47. /* Disable buffering for read/write FILE to prevent problems with
  48. * fseek and buffering for read/write auto-transitioning. */
  49. setvbuf(in, NULL, _IONBF, 0);
  50. }
  51. /* Turn echoing off if it is on now. */
  52. tty_changed = 0;
  53. if (tcgetattr (fileno (in), &t) == 0)
  54. {
  55. /* Save the old one. */
  56. s = t;
  57. /* Tricky, tricky. */
  58. t.c_lflag &= ~(ECHO|ISIG);
  59. tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
  60. }
  61. /* Write the prompt. */
  62. fputs(prompt, out);
  63. fflush(out);
  64. /* Read the password. */
  65. if (!fgets (buf, PWD_BUFFER_SIZE, in))
  66. buf[0] = '\0';
  67. nread = strlen(buf);
  68. if (nread > 0 && buf[nread - 1] == '\n')
  69. /* Remove the newline. */
  70. buf[nread - 1] = '\0';
  71. if (tty_changed)
  72. {
  73. /* Write the newline that was not echoed. */
  74. putc('\n', out);
  75. /* Restore the original setting. */
  76. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  77. }
  78. if (in != stdin)
  79. /* We opened the terminal; now close it. */
  80. fclose (in);
  81. return buf;
  82. }
  83. #endif