getpass.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
  19. /* It is desirable to use this bit on systems that have it.
  20. The only bit of terminal state we want to twiddle is echoing, which is
  21. done in software; there is no need to change the state of the terminal
  22. hardware. */
  23. #ifndef TCSASOFT
  24. #define TCSASOFT 0
  25. #endif
  26. #define PWD_BUFFER_SIZE 256
  27. char * getpass (const char *prompt)
  28. {
  29. FILE *in, *out;
  30. struct termios s, t;
  31. int tty_changed;
  32. static char buf[PWD_BUFFER_SIZE];
  33. int nread;
  34. /* Try to write to and read from the terminal if we can.
  35. If we can't open the terminal, use stderr and stdin. */
  36. out = in = fopen ("/dev/tty", "r+");
  37. if (in == NULL)
  38. {
  39. in = stdin;
  40. out = stderr;
  41. }
  42. else
  43. {
  44. /* Disable buffering for read/write FILE to prevent problems with
  45. * fseek and buffering for read/write auto-transitioning. */
  46. setvbuf(in, NULL, _IONBF, 0);
  47. }
  48. /* Turn echoing off if it is on now. */
  49. tty_changed = 0;
  50. if (tcgetattr (fileno (in), &t) == 0)
  51. {
  52. /* Save the old one. */
  53. s = t;
  54. /* Tricky, tricky. */
  55. t.c_lflag &= ~(ECHO|ISIG);
  56. tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
  57. }
  58. /* Write the prompt. */
  59. fputs(prompt, out);
  60. fflush(out);
  61. /* Read the password. */
  62. if (!fgets (buf, sizeof(buf), in))
  63. buf[0] = '\0';
  64. nread = strlen(buf);
  65. if (nread > 0 && buf[nread - 1] == '\n')
  66. /* Remove the newline. */
  67. buf[nread - 1] = '\0';
  68. if (tty_changed)
  69. {
  70. /* Write the newline that was not echoed. */
  71. putc('\n', out);
  72. /* Restore the original setting. */
  73. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  74. }
  75. if (in != stdin)
  76. /* We opened the terminal; now close it. */
  77. fclose (in);
  78. return buf;
  79. }
  80. #endif