home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / ncftp-1.9.5.tar.gz / ncftp-1.9.5.tar / ncftp-1.9.5 / getpass.c < prev    next >
C/C++ Source or Header  |  1995-10-01  |  3KB  |  161 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.  
  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 sun    /* ...both unnecessary, and conflicting with <termios.h> */
  20. #include <sys/ioctl.h>
  21. #endif
  22.  
  23. #ifdef TERMIOS
  24. #        include <termios.h>
  25. #else
  26. #    ifdef SGTTYB
  27. #        include <sgtty.h>
  28. #    else
  29. #        include <termio.h>
  30. #    endif
  31. #endif /* !TERMIOS */
  32.  
  33. #ifdef STRICT_PROTOS
  34. int ioctl(int, int, ...);
  35. #endif
  36.  
  37. #endif    /* GETPASS */
  38.  
  39.  
  40.  
  41.  
  42. void Echo(FILE *fp, int on)
  43. {
  44. #ifndef GETPASS        /* Otherwise just do nothing which is ok. */
  45.  
  46. #ifdef TERMIOS
  47.     static struct termios orig, noecho, *tp;
  48. #else
  49. #    ifdef SGTTYB
  50.     static struct sgttyb orig, noecho, *tp;
  51. #    else
  52.     static struct termio orig, noecho, *tp;
  53. #    endif
  54. #endif
  55.     static int state = 0;
  56.     int fd = fileno(fp);
  57.     
  58.     if (!isatty(fd))
  59.         return;
  60.  
  61.     if (state == 0) {
  62. #ifdef TERMIOS
  63.         if (tcgetattr(fd, &orig) < 0)
  64.             PERROR("echo", "tcgetattr");
  65.         noecho = orig;
  66.         noecho.c_lflag &= ~ECHO;
  67. #else
  68. #    ifdef SGTTYB
  69.         if (ioctl(fd, TIOCGETP, &orig) < 0)
  70.             PERROR("echo", "ioctl");
  71.         noecho = orig;
  72.         noecho.sg_flags &= ~ECHO;
  73. #    else
  74.         if (ioctl(fd, TCGETA, &orig) < 0)
  75.             PERROR("echo", "ioctl");
  76.         noecho = orig;
  77.         noecho.c_lflag &= ~ECHO;
  78. #    endif
  79. #endif
  80.         state = 1;
  81.     }
  82.     tp = NULL;
  83.     if (on && state == 2) {
  84.         /* Turn echo back on. */
  85.         tp = &orig;
  86.         state = 1;
  87.     } else if (!on && state == 1) {
  88.         /* Turn echo off. */
  89.         tp = &noecho;
  90.         state = 2;
  91.     }
  92.     if (tp != NULL) {
  93. #ifdef TERMIOS
  94.         if (tcsetattr(fd, TCSANOW, tp) < 0)
  95.             PERROR("echo", "tcsetattr");
  96. #else
  97. #    ifdef SGTTYB
  98.         if (ioctl(fd, TIOCSETP, tp) < 0)
  99.             PERROR("echo", "ioctl");
  100. #    else
  101.         if (ioctl(fd, TCSETA, tp) < 0)
  102.             PERROR("echo", "ioctl");
  103. #    endif
  104. #endif    /* !TERMIOS */
  105.     }
  106.  
  107. #endif    /* GETPASS */
  108. }    /* Echo */
  109.  
  110.  
  111.  
  112. #ifndef GETPASS
  113.  
  114. char *Getpass(char *promptstr)
  115. {
  116.     register int ch;
  117.     register char *p;
  118.     FILE *fp, *outfp;
  119.     Sig_t oldintr;
  120.     static char buf[kMaxPassLen + 1];
  121.  
  122.     /*
  123.      * read and write to /dev/tty if possible; else read from
  124.      * stdin and write to stderr.
  125.      */
  126. #if !defined(BOTCHED_FOPEN_RW)
  127.       if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) {
  128.           outfp = stderr;
  129.           fp = stdin;
  130.       }
  131. #else
  132.     /* SCO 32v2 botches "w+" open */
  133.     if ((fp = fopen("/dev/tty", "r")) == NULL)
  134.         fp = stdin;
  135.     if ((outfp = fopen("/dev/tty", "w")) == NULL)
  136.         outfp = stderr;
  137. #endif
  138.     oldintr = Signal(SIGINT, SIG_IGN);
  139.     Echo(fp, 0);        /* Turn echoing off. */
  140.     (void) fputs(promptstr, outfp);
  141.     (void) rewind(outfp);            /* implied flush */
  142.     for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';)
  143.         if (p < buf + kMaxPassLen)
  144.             *p++ = ch;
  145.     *p = '\0';
  146.     (void)write(fileno(outfp), "\n", 1);
  147.     Echo(fp, 1);
  148.     (void) Signal(SIGINT, oldintr);
  149.     if (fp != stdin)
  150.         (void)fclose(fp);
  151. #if defined(BOTCHED_FOPEN_RW)
  152.     if (outfp != stderr)
  153.         (void)fclose(outfp);
  154. #endif
  155.     return(buf);
  156. }    /* Getpass */
  157.  
  158. #endif /* GETPASS */
  159.  
  160. /* eof Getpass.c */
  161.