home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / ncftp / part02 / getpass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-25  |  2.6 KB  |  151 lines

  1. /* Getpass.c */
  2.  
  3. /*  $RCSfile: getpass.c,v $
  4.  *  $Revision: 14020.11 $
  5.  *  $Date: 93/05/21 05:44:36 $
  6.  */
  7.  
  8. #include "sys.h"
  9. #include <stdio.h>
  10. #include <signal.h>
  11.  
  12. #include "util.h"
  13. #include "cmds.h"
  14. #include "getpass.h"
  15. #include "copyright.h"
  16.  
  17. #ifndef GETPASS
  18.  
  19. #ifndef NO_UNISTDH
  20. #    include <unistd.h>
  21. #endif
  22.  
  23. #include <sys/ioctl.h>
  24.  
  25. #ifdef TERMIOS
  26. #        include <termios.h>
  27. #else
  28. #    ifdef SGTTYB
  29. #        include <sgtty.h>
  30. #    else
  31. #        include <termio.h>
  32. #    endif
  33. #endif /* !TERMIOS */
  34.  
  35. #ifdef STRICT_PROTOS
  36. int ioctl(int, int, ...);
  37. #endif
  38.  
  39. #endif    /* GETPASS */
  40.  
  41.  
  42.  
  43.  
  44. void echo(FILE *fp, int on)
  45. {
  46. #ifndef GETPASS        /* Otherwise just do nothing which is ok. */
  47.  
  48. #ifdef TERMIOS
  49.     static struct termios orig, noecho, *tp;
  50. #else
  51. #    ifdef SGTTYB
  52.     static struct sgttyb orig, noecho, *tp;
  53. #    else
  54.     static struct termio orig, noecho, *tp;
  55. #    endif
  56. #endif
  57.     static int state = 0;
  58.     int fd = fileno(fp);
  59.     
  60.     if (!isatty(fd))
  61.         return;
  62.  
  63.     if (state == 0) {
  64. #ifdef TERMIOS
  65.         if (tcgetattr(fd, &orig) < 0)
  66.             PERROR("echo", "tcgetattr");
  67.         noecho = orig;
  68.         noecho.c_lflag &= ~ECHO;
  69. #else
  70. #    ifdef SGTTYB
  71.         if (ioctl(fd, TIOCGETP, &orig) < 0)
  72.             PERROR("echo", "ioctl");
  73.         noecho = orig;
  74.         noecho.sg_flags &= ~ECHO;
  75. #    else
  76.         if (ioctl(fd, TCGETA, &orig) < 0)
  77.             PERROR("echo", "ioctl");
  78.         noecho = orig;
  79.         noecho.c_lflag &= ~ECHO;
  80. #    endif
  81. #endif
  82.         state = 1;
  83.     }
  84.     tp = NULL;
  85.     if (on && state == 2) {
  86.         /* Turn echo back on. */
  87.         tp = &orig;
  88.         state = 1;
  89.     } else if (!on && state == 1) {
  90.         /* Turn echo off. */
  91.         tp = &noecho;
  92.         state = 2;
  93.     }
  94.     if (tp != NULL) {
  95. #ifdef TERMIOS
  96.         if (tcsetattr(fd, TCSAFLUSH, tp) < 0)
  97.             PERROR("echo", "tcsetattr");
  98. #else
  99. #    ifdef SGTTYB
  100.         if (ioctl(fd, TIOCSETP, tp) < 0)
  101.             PERROR("echo", "ioctl");
  102. #    else
  103.         if (ioctl(fd, TCSETA, tp) < 0)
  104.             PERROR("echo", "ioctl");
  105. #    endif
  106. #endif    /* !TERMIOS */
  107.     }
  108.  
  109. #endif    /* GETPASS */
  110. }    /* echo */
  111.  
  112.  
  113.  
  114. #ifndef GETPASS
  115.  
  116. char *Getpass(char *promptstr)
  117. {
  118.     register int ch;
  119.     register char *p;
  120.     FILE *fp, *outfp;
  121.     sig_t oldintr;
  122.     static char buf[kMaxPassLen + 1];
  123.  
  124.     /*
  125.      * read and write to /dev/tty if possible; else read from
  126.      * stdin and write to stderr.
  127.      */
  128.     if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) {
  129.         outfp = stderr;
  130.         fp = stdin;
  131.     }
  132.     oldintr = Signal(SIGINT, SIG_IGN);
  133.     echo(fp, 0);        /* Turn echoing off. */
  134.     (void) fputs(promptstr, outfp);
  135.     (void) rewind(outfp);            /* implied flush */
  136.     for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';)
  137.         if (p < buf + kMaxPassLen)
  138.             *p++ = ch;
  139.     *p = '\0';
  140.     (void)write(fileno(outfp), "\n", 1);
  141.     echo(fp, 1);
  142.     (void) Signal(SIGINT, oldintr);
  143.     if (fp != stdin)
  144.         (void)fclose(fp);
  145.     return(buf);
  146. }    /* Getpass */
  147.  
  148. #endif /* GETPASS */
  149.  
  150. /* eof Getpass.c */
  151.