home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / kit / part01 / des / getpass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.1 KB  |  119 lines

  1. /*
  2.  * $Id: getpass.c,v 2.0.1.2 91/04/30 13:33:30 ram Exp $
  3.  *
  4.  * $Log:    getpass.c,v $
  5.  * Revision 2.0.1.2  91/04/30  13:33:30  ram
  6.  * patch3: now relies on the new metaconfig symbol SIGNAL_T
  7.  * 
  8.  * Revision 2.0.1.1  91/04/01  15:40:01  ram
  9.  * patch1: created
  10.  * 
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <signal.h>
  15. #include "../config.h"
  16.  
  17. /* These defines should be correctly set up by Configure */
  18. #ifdef I_TERMIOS
  19. #include <termios.h>
  20. #endif
  21. #ifdef I_TERMIO
  22. #include <termio.h>
  23. #endif
  24. #ifdef I_SGTTY
  25. #include <sgtty.h>
  26. #endif
  27. #ifdef I_SYSIOCTL
  28. #include <sys/ioctl.h>
  29. #endif
  30.  
  31. #define    TTY    "/dev/tty"    /* Change to "con" for MS-DOS */
  32.  
  33. /* Issue prompt and read reply with echo turned off */
  34. char *
  35. getpass(prompt)
  36. char *prompt;
  37. {
  38.     register char *cp;
  39.     int c;
  40.     FILE *tty;
  41.     static char pbuf[128];
  42.  
  43. #ifdef I_SGTTY
  44.     struct sgttyb ttyb,ttysav;
  45. #else
  46. #ifdef I_TERMIOS
  47.     struct termios ttyb, ttysav;
  48. #else
  49.     struct termio ttyb, ttysav;
  50. #endif
  51. #endif
  52.  
  53.     extern SIGNAL_T (*signal())();
  54.     SIGNAL_T (*sig)();
  55.  
  56.     if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
  57.         tty = stdin;
  58.     else
  59.         setbuf(tty, (char *)NULL);
  60.     sig = signal(SIGINT, SIG_IGN);
  61.  
  62. #ifdef I_SGTTY
  63.     ioctl(fileno(tty), TIOCGETP, &ttyb);
  64.     ioctl(fileno(tty), TIOCGETP, &ttysav);
  65.     ttyb.sg_flags |= RAW;
  66.     ttyb.sg_flags &= ~ECHO;
  67.     ioctl(fileno(tty), TIOCSETP, &ttyb);
  68. #else
  69. #ifdef I_TERMIOS
  70.     tcgetattr(fileno(tty), &ttyb);
  71.     tcgetattr(fileno(tty), &ttysav);
  72. #else
  73.     ioctl(fileno(tty), TCGETA, &ttyb);
  74.     ioctl(fileno(tty), TCGETA, &ttysav);
  75. #endif
  76. #ifdef CBREAK
  77.     ttyb.c_lflag |= CBREAK;
  78. #endif
  79. #ifdef RAW
  80.     ttyb.c_lflag |= RAW;
  81. #endif
  82.     ttyb.c_lflag &= ~ECHO;
  83. #ifdef I_TERMIOS
  84.     tcsetattr(fileno(tty), TCSANOW, &ttyb);
  85. #else
  86.     ioctl(fileno(tty), TCSETA, &ttyb);
  87. #endif
  88. #endif
  89.  
  90.     fprintf(stderr, "%s", prompt);
  91.     fflush(stderr);
  92.     cp = pbuf;
  93.     for (;;) {
  94.         c = getc(tty);
  95.         if(c == '\r' || c == '\n' || c == EOF)
  96.             break;
  97.         if (cp < &pbuf[127])
  98.             *cp++ = c;
  99.     }
  100.     *cp = '\0';
  101.     fprintf(stderr,"\r\n");
  102.     fflush(stderr);
  103.  
  104. #ifdef I_SGTTY
  105.     ioctl(fileno(tty), TIOCSETP, &ttysav);
  106. #else
  107. #ifdef I_TERMIOS
  108.     tcsetattr(fileno(tty), TCSANOW, &ttysav);
  109. #else
  110.     ioctl(fileno(tty), TCSETA, &ttysav);
  111. #endif
  112. #endif
  113.  
  114.     signal(SIGINT, sig);
  115.     if (tty != stdin)
  116.         fclose(tty);
  117.     return(pbuf);
  118. }
  119.