getpass.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <termios.h>
  18. #include <unistd.h>
  19. #include <malloc.h>
  20. #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
  21. libc_hidden_proto(strlen)
  22. libc_hidden_proto(tcsetattr)
  23. libc_hidden_proto(tcgetattr)
  24. libc_hidden_proto(setvbuf)
  25. libc_hidden_proto(fopen)
  26. libc_hidden_proto(fclose)
  27. libc_hidden_proto(fileno)
  28. libc_hidden_proto(fflush)
  29. libc_hidden_proto(fgets)
  30. libc_hidden_proto(fputs)
  31. libc_hidden_proto(fputc)
  32. libc_hidden_proto(putc)
  33. libc_hidden_proto(__fputc_unlocked)
  34. /* It is desirable to use this bit on systems that have it.
  35. The only bit of terminal state we want to twiddle is echoing, which is
  36. done in software; there is no need to change the state of the terminal
  37. hardware. */
  38. #ifndef TCSASOFT
  39. #define TCSASOFT 0
  40. #endif
  41. #define PWD_BUFFER_SIZE 256
  42. char *
  43. getpass (prompt)
  44. const char *prompt;
  45. {
  46. static char *buf;
  47. FILE *in, *out;
  48. struct termios s, t;
  49. int tty_changed;
  50. int nread;
  51. free(buf);
  52. buf = __uc_malloc(PWD_BUFFER_SIZE);
  53. /* Try to write to and read from the terminal if we can.
  54. If we can't open the terminal, use stderr and stdin. */
  55. in = fopen ("/dev/tty", "r+");
  56. if (in == NULL)
  57. {
  58. in = stdin;
  59. out = stderr;
  60. }
  61. else
  62. out = in;
  63. /* Turn echoing off if it is on now. */
  64. if (tcgetattr (fileno (in), &t) == 0)
  65. {
  66. /* Save the old one. */
  67. s = t;
  68. /* Tricky, tricky. */
  69. t.c_lflag &= ~(ECHO|ISIG);
  70. tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
  71. if (in != stdin) {
  72. /* Disable buffering for read/write FILE to prevent problems with
  73. * fseek and buffering for read/write auto-transitioning. */
  74. setvbuf(in, NULL, _IONBF, 0);
  75. }
  76. }
  77. else
  78. tty_changed = 0;
  79. /* Write the prompt. */
  80. fputs(prompt, out);
  81. fflush(out);
  82. /* Read the password. */
  83. fgets (buf, PWD_BUFFER_SIZE-1, in);
  84. if (buf != NULL)
  85. {
  86. nread = strlen(buf);
  87. if (nread < 0)
  88. buf[0] = '\0';
  89. else if (buf[nread - 1] == '\n')
  90. {
  91. /* Remove the newline. */
  92. buf[nread - 1] = '\0';
  93. if (tty_changed)
  94. /* Write the newline that was not echoed. */
  95. putc('\n', out);
  96. }
  97. }
  98. /* Restore the original setting. */
  99. if (tty_changed) {
  100. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  101. }
  102. if (in != stdin)
  103. /* We opened the terminal; now close it. */
  104. fclose (in);
  105. return buf;
  106. }
  107. #endif