getpass.c 3.1 KB

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