getpass.c 597 B

123456789101112131415161718192021222324252627282930
  1. /* $Id$ */
  2. #include <signal.h>
  3. #include <sgtty.h>
  4. char * getpass(prompt)
  5. char *prompt;
  6. {
  7. int i = 0;
  8. struct sgttyb tty, ttysave;
  9. static char pwdbuf[9];
  10. int fd;
  11. void (*savesig)();
  12. if ((fd = open("/dev/tty", 0)) < 0) fd = 0;
  13. savesig = signal(SIGINT, SIG_IGN);
  14. write(2, prompt, strlen(prompt));
  15. gtty(fd, &tty);
  16. ttysave = tty;
  17. tty.sg_flags &= ~ECHO;
  18. stty(fd, &tty);
  19. i = read(fd, pwdbuf, 9);
  20. while (pwdbuf[i - 1] != '\n')
  21. read(fd, &pwdbuf[i - 1], 1);
  22. pwdbuf[i - 1] = '\0';
  23. stty(fd, &ttysave);
  24. write(2, "\n", 1);
  25. if (fd != 0) close(fd);
  26. signal(SIGINT, savesig);
  27. return(pwdbuf);
  28. }