home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / tcpip / amitcp-support / ncftp-1.5.6 / src / rcs / getpass.c,v < prev    next >
Encoding:
Text File  |  1994-06-29  |  3.1 KB  |  188 lines

  1. head    14020.11;
  2. access;
  3. symbols
  4.     ORIGINAL:14020.11;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 14020.11
  10. date    93.05.21.05.44.36;    author alph;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @Original version
  17. @
  18.  
  19.  
  20. 14020.11
  21. log
  22. @checked in with -k by alph at 1993/10/10 19:59:56
  23. @
  24. text
  25. @/* Getpass.c */
  26.  
  27. /*  $RCSfile: getpass.c,v $
  28.  *  $Revision: 14020.11 $
  29.  *  $Date: 93/05/21 05:44:36 $
  30.  */
  31.  
  32. #include "sys.h"
  33. #include <stdio.h>
  34. #include <signal.h>
  35.  
  36. #include "util.h"
  37. #include "cmds.h"
  38. #include "getpass.h"
  39. #include "copyright.h"
  40.  
  41. #ifndef GETPASS
  42.  
  43. #ifndef NO_UNISTDH
  44. #    include <unistd.h>
  45. #endif
  46.  
  47. #include <sys/ioctl.h>
  48.  
  49. #ifdef TERMIOS
  50. #        include <termios.h>
  51. #else
  52. #    ifdef SGTTYB
  53. #        include <sgtty.h>
  54. #    else
  55. #        include <termio.h>
  56. #    endif
  57. #endif /* !TERMIOS */
  58.  
  59. #ifdef STRICT_PROTOS
  60. int ioctl(int, int, ...);
  61. #endif
  62.  
  63. #endif    /* GETPASS */
  64.  
  65.  
  66.  
  67.  
  68. void echo(FILE *fp, int on)
  69. {
  70. #ifndef GETPASS        /* Otherwise just do nothing which is ok. */
  71.  
  72. #ifdef TERMIOS
  73.     static struct termios orig, noecho, *tp;
  74. #else
  75. #    ifdef SGTTYB
  76.     static struct sgttyb orig, noecho, *tp;
  77. #    else
  78.     static struct termio orig, noecho, *tp;
  79. #    endif
  80. #endif
  81.     static int state = 0;
  82.     int fd = fileno(fp);
  83.     
  84.     if (!isatty(fd))
  85.         return;
  86.  
  87.     if (state == 0) {
  88. #ifdef TERMIOS
  89.         if (tcgetattr(fd, &orig) < 0)
  90.             PERROR("echo", "tcgetattr");
  91.         noecho = orig;
  92.         noecho.c_lflag &= ~ECHO;
  93. #else
  94. #    ifdef SGTTYB
  95.         if (ioctl(fd, TIOCGETP, &orig) < 0)
  96.             PERROR("echo", "ioctl");
  97.         noecho = orig;
  98.         noecho.sg_flags &= ~ECHO;
  99. #    else
  100.         if (ioctl(fd, TCGETA, &orig) < 0)
  101.             PERROR("echo", "ioctl");
  102.         noecho = orig;
  103.         noecho.c_lflag &= ~ECHO;
  104. #    endif
  105. #endif
  106.         state = 1;
  107.     }
  108.     tp = NULL;
  109.     if (on && state == 2) {
  110.         /* Turn echo back on. */
  111.         tp = &orig;
  112.         state = 1;
  113.     } else if (!on && state == 1) {
  114.         /* Turn echo off. */
  115.         tp = &noecho;
  116.         state = 2;
  117.     }
  118.     if (tp != NULL) {
  119. #ifdef TERMIOS
  120.         if (tcsetattr(fd, TCSAFLUSH, tp) < 0)
  121.             PERROR("echo", "tcsetattr");
  122. #else
  123. #    ifdef SGTTYB
  124.         if (ioctl(fd, TIOCSETP, tp) < 0)
  125.             PERROR("echo", "ioctl");
  126. #    else
  127.         if (ioctl(fd, TCSETA, tp) < 0)
  128.             PERROR("echo", "ioctl");
  129. #    endif
  130. #endif    /* !TERMIOS */
  131.     }
  132.  
  133. #endif    /* GETPASS */
  134. }    /* echo */
  135.  
  136.  
  137.  
  138. #ifndef GETPASS
  139.  
  140. char *Getpass(char *promptstr)
  141. {
  142.     register int ch;
  143.     register char *p;
  144.     FILE *fp, *outfp;
  145.     Sig_t oldintr;
  146.     static char buf[kMaxPassLen + 1];
  147.  
  148.     /*
  149.      * read and write to /dev/tty if possible; else read from
  150.      * stdin and write to stderr.
  151.      */
  152. #if !defined(BOTCHED_FOPEN_RW)
  153.       if ((outfp = fp = fopen("/dev/tty", "w+")) == NULL) {
  154.           outfp = stderr;
  155.           fp = stdin;
  156.       }
  157. #else
  158.     /* SCO 32v2 botches "w+" open */
  159.     if ((fp = fopen("/dev/tty", "r")) == NULL)
  160.         fp = stdin;
  161.     if ((outfp = fopen("/dev/tty", "w")) == NULL)
  162.         outfp = stderr;
  163. #endif
  164.     oldintr = Signal(SIGINT, SIG_IGN);
  165.     echo(fp, 0);        /* Turn echoing off. */
  166.     (void) fputs(promptstr, outfp);
  167.     (void) rewind(outfp);            /* implied flush */
  168.     for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';)
  169.         if (p < buf + kMaxPassLen)
  170.             *p++ = ch;
  171.     *p = '\0';
  172.     (void)write(fileno(outfp), "\n", 1);
  173.     echo(fp, 1);
  174.     (void) Signal(SIGINT, oldintr);
  175.     if (fp != stdin)
  176.         (void)fclose(fp);
  177. #if defined(BOTCHED_FOPEN_RW)
  178.     if (outfp != stderr)
  179.         (void)fclose(outfp);
  180. #endif
  181.     return(buf);
  182. }    /* Getpass */
  183.  
  184. #endif /* GETPASS */
  185.  
  186. /* eof Getpass.c */
  187. @
  188.