home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / des_os2.zip / des / src / getpass.c < prev    next >
C/C++ Source or Header  |  1993-09-21  |  1KB  |  55 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sgtty.h>
  4.  
  5. /*#define    TTY    "/dev/tty"*/   /* Change to "con" for OS/2 or MS-DOS */
  6. #define TTY "con"
  7.  
  8. /* Issue prompt and read reply with echo turned off */
  9. char *
  10. getpass(prompt)
  11. char *prompt;
  12. {
  13. #ifndef IBMPC
  14.     struct sgttyb ttyb,ttysav;
  15. #endif
  16.     register char *cp;
  17.     int c;
  18.     FILE *tty;
  19.     static char pbuf[128];
  20.     int (*sig)();
  21.  
  22.     if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
  23.         tty = stdin;
  24.     else
  25.         setbuf(tty, (char *)NULL);
  26.     sig = signal(SIGINT, SIG_IGN);
  27. #ifndef IBMPC
  28.     ioctl(fileno(tty), TIOCGETP, &ttyb);
  29.     ioctl(fileno(tty), TIOCGETP, &ttysav);
  30.     ttyb.sg_flags |= RAW;
  31.     ttyb.sg_flags &= ~ECHO;
  32.     ioctl(fileno(tty), TIOCSETP, &ttyb);
  33. #endif
  34.     fprintf(stderr, "%s", prompt);
  35.     fflush(stderr);
  36.     cp = pbuf;
  37.     for (;;) {
  38.         c = getc(tty);
  39.         if(c == '\r' || c == '\n' || c == EOF)
  40.             break;
  41.         if (cp < &pbuf[127])
  42.             *cp++ = c;
  43.     }
  44.     *cp = '\0';
  45.     fprintf(stderr,"\r\n");
  46.     fflush(stderr);
  47. #ifndef IBMPC
  48.     ioctl(fileno(tty), TIOCSETP, &ttysav);
  49. #endif
  50.     signal(SIGINT, sig);
  51.     if (tty != stdin)
  52.         fclose(tty);
  53.     return(pbuf);
  54. }
  55.