/contrib/cvs/lib/getpass.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 111 lines · 66 code · 14 blank · 31 comment · 17 complexity · c2e8cafbabe31efba0838a4921374636 MD5 · raw file

  1. /* Copyright (C) 1992,93,94,95,96,97,98,99,2000, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #if HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <stdio.h>
  18. #ifndef SEEK_CUR
  19. #define SEEK_CUR 1
  20. #endif
  21. #include <termios.h>
  22. #include <unistd.h>
  23. #include "getline.h"
  24. /* It is desirable to use this bit on systems that have it.
  25. The only bit of terminal state we want to twiddle is echoing, which is
  26. done in software; there is no need to change the state of the terminal
  27. hardware. */
  28. #ifndef TCSASOFT
  29. # define TCSASOFT 0
  30. #endif
  31. char *
  32. #if __STDC__
  33. getpass (const char *prompt)
  34. #else
  35. getpass (prompt)
  36. const char *prompt;
  37. #endif
  38. {
  39. FILE *in, *out;
  40. struct termios s, t;
  41. int tty_changed;
  42. static char *buf;
  43. static size_t bufsize;
  44. ssize_t nread;
  45. /* Try to write to and read from the terminal if we can.
  46. If we can't open the terminal, use stderr and stdin. */
  47. in = fopen ("/dev/tty", "w+");
  48. if (in == NULL)
  49. {
  50. in = stdin;
  51. out = stderr;
  52. }
  53. else
  54. out = in;
  55. /* Turn echoing off if it is on now. */
  56. if (tcgetattr (fileno (in), &t) == 0)
  57. {
  58. /* Save the old one. */
  59. s = t;
  60. /* Tricky, tricky. */
  61. t.c_lflag &= ~(ECHO|ISIG);
  62. tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
  63. }
  64. else
  65. tty_changed = 0;
  66. /* Write the prompt. */
  67. fputs (prompt, out);
  68. fflush (out);
  69. /* Read the password. */
  70. nread = getline (&buf, &bufsize, in);
  71. if (buf != NULL)
  72. {
  73. if (nread < 0)
  74. buf[0] = '\0';
  75. else if (buf[nread - 1] == '\n')
  76. {
  77. /* Remove the newline. */
  78. buf[nread - 1] = '\0';
  79. if (tty_changed)
  80. {
  81. /* Write the newline that was not echoed. */
  82. if (out == in) fseek (out, 0, SEEK_CUR);
  83. putc ('\n', out);
  84. }
  85. }
  86. }
  87. /* Restore the original setting. */
  88. if (tty_changed)
  89. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  90. if (in != stdin)
  91. /* We opened the terminal; now close it. */
  92. fclose (in);
  93. return buf;
  94. }