getpass.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (C) 1992,93,94,95,96,97,98,99 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 Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #define setvbuf __setvbuf
  16. #define tcsetattr __tcsetattr
  17. #define tcgetattr __tcgetattr
  18. #define fileno __fileno
  19. #define fflush __fflush
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <termios.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. /* It is desirable to use this bit on systems that have it.
  26. The only bit of terminal state we want to twiddle is echoing, which is
  27. done in software; there is no need to change the state of the terminal
  28. hardware. */
  29. #ifndef TCSASOFT
  30. #define TCSASOFT 0
  31. #endif
  32. #define PWD_BUFFER_SIZE 256
  33. char *
  34. getpass (prompt)
  35. const char *prompt;
  36. {
  37. FILE *in, *out;
  38. struct termios s, t;
  39. int tty_changed;
  40. static char buf[PWD_BUFFER_SIZE];
  41. int nread;
  42. /* Try to write to and read from the terminal if we can.
  43. If we can't open the terminal, use stderr and stdin. */
  44. in = fopen ("/dev/tty", "r+");
  45. if (in == NULL)
  46. {
  47. in = stdin;
  48. out = stderr;
  49. }
  50. else
  51. out = in;
  52. /* Turn echoing off if it is on now. */
  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. if (in != stdin) {
  61. /* Disable buffering for read/write FILE to prevent problems with
  62. * fseek and buffering for read/write auto-transitioning. */
  63. setvbuf(in, NULL, _IONBF, 0);
  64. }
  65. }
  66. else
  67. tty_changed = 0;
  68. /* Write the prompt. */
  69. fputs(prompt, out);
  70. fflush(out);
  71. /* Read the password. */
  72. fgets (buf, PWD_BUFFER_SIZE-1, in);
  73. if (buf != NULL)
  74. {
  75. nread = __strlen(buf);
  76. if (nread < 0)
  77. buf[0] = '\0';
  78. else if (buf[nread - 1] == '\n')
  79. {
  80. /* Remove the newline. */
  81. buf[nread - 1] = '\0';
  82. if (tty_changed)
  83. /* Write the newline that was not echoed. */
  84. putc('\n', out);
  85. }
  86. }
  87. /* Restore the original setting. */
  88. if (tty_changed) {
  89. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  90. }
  91. if (in != stdin)
  92. /* We opened the terminal; now close it. */
  93. fclose (in);
  94. return buf;
  95. }