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 / amiga / getpass.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  1.5 KB  |  85 lines

  1. /* Getpass.c */
  2.  
  3. #define    PASSWD_LEN    127
  4.  
  5. #include "sys.h"
  6. #include <stdio.h>
  7. #include <signal.h>
  8.  
  9. #include "util.h"
  10. #include "cmds.h"
  11. #include "getpass.h"
  12. #include "copyright.h"
  13.  
  14. void echo(FILE *fp, int on)
  15. {
  16.     static int f_echo = 1;
  17.     int fd = fileno(fp);
  18. #if 0
  19.     fprintf(stderr,"echo(fd = %d, %d)\n", fd, on); 
  20. #endif
  21.     if(fp != stdin)
  22.     {
  23.         fprintf(stderr,"echo: Internal error, echo not called on stdin\n");
  24.     }
  25.     else
  26.     {
  27.         if(on)                    /* always do on */
  28.         {
  29.             if(rawcon(0))
  30.             {
  31.                 PERROR("rawcon(0) failed", "");
  32.             }
  33.             else
  34.                 f_echo = 1;
  35.         }
  36.         else if(f_echo && !on)
  37.         {
  38.             if(rawcon(1))
  39.             {
  40.                 PERROR("rawcon(1) failed", "");
  41.             }
  42.             f_echo = 0;
  43.         }
  44.     }
  45. }
  46.  
  47. #ifndef GETPASS
  48. char *Getpass(char *promptstr)
  49. {
  50.     register int ch;
  51.     register char *p;
  52.     FILE *fp, *outfp;
  53.     void (*oldintr)(int);
  54.     static char buf[PASSWD_LEN + 1];
  55.     /*
  56.      * read and write to /dev/tty if possible; else read from
  57.      * stdin and write to stderr.
  58.      */
  59.         outfp = stderr;
  60.     fp = stdin;
  61.  
  62.     oldintr = signal(SIGINT, SIG_IGN);
  63.     echo(fp, 0);                /* Turn echoing off. */
  64.     (void) fputs(promptstr, outfp);
  65.     (void) rewind(outfp);        /* implied flush */
  66.     for (p = buf; (ch = getc(fp)) != EOF && ch != '\n' && ch != '\r';)
  67.     {
  68. #if 0
  69.         printf("ch = 0x%02lx\n", (int) ch); 
  70. #endif
  71.         if (p < buf + PASSWD_LEN)
  72.             *p++ = ch;
  73.     }
  74.     *p = '\0';
  75.     (void)write(fileno(outfp), "\n", 1);
  76.     echo(fp, 1);
  77.     (void) signal(SIGINT, oldintr);
  78.     if (fp != stdin)
  79.         (void)fclose(fp);
  80.     return(buf);
  81. }                                /* Getpass */
  82. #endif /* GETPASS */
  83.  
  84. /* eof Getpass.c */
  85.