Annotation of coherent/b/lib/libc/gen/getpass.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Coherent I/O Library.
                      3:  * getpass -- get a password from the controlling
                      4:  * terminal with echo suppressed.
                      5:  */
                      6: 
                      7: #include <stdio.h>
                      8: #include <sgtty.h>
                      9: #include <signal.h>
                     10: 
                     11: #define        NPASS   50              /* Characters in a password */
                     12: 
                     13: static char    tty[] = "/dev/tty";
                     14: static struct  sgttyb sg;
                     15: static int     omode;
                     16: static FILE    *ifp;
                     17: static FILE    *ofp;
                     18: static int     (*ofunc)();
                     19: 
                     20: char *
                     21: getpass(pp)
                     22: char *pp;
                     23: {
                     24:        static char passwd[NPASS+1];
                     25:        char *p;
                     26:        int   c;
                     27:        int reset();
                     28: 
                     29:        if ((ofp = fopen(tty, "r+w")) != NULL)
                     30:                ifp = ofp;
                     31:        else {
                     32:                ifp = stdin;
                     33:                ofp = stderr;
                     34:        }
                     35:        if ((ofunc = signal(SIGINT, reset)) != SIG_IGN)
                     36:                signal(SIGINT, reset);
                     37:        ioctl(fileno(ofp), TIOCGETP, &sg);
                     38:        omode = sg.sg_flags;
                     39:        sg.sg_flags &= ~ECHO;
                     40:        ioctl(fileno(ofp), TIOCSETP, &sg);
                     41:        while (*pp)
                     42:                putc(*pp++, ofp);
                     43:        p = passwd;
                     44:        while ((c=getc(ifp))!=EOF && c!='\n') {
                     45:                if (p < &passwd[NPASS])
                     46:                        *p++ = c;
                     47:        }
                     48:        while (p < &passwd[NPASS])
                     49:                *p++ = '\0';
                     50:        reset();
                     51:        if (ifp != stdin)
                     52:                fclose(ifp);
                     53:        signal(SIGINT, ofunc);
                     54:        return (c==EOF ? NULL : passwd);
                     55: }
                     56: 
                     57: /*
                     58:  * Reset echo on and signal catching.
                     59:  */
                     60: static
                     61: reset()
                     62: {
                     63:        signal(SIGINT, ofunc);
                     64:        sg.sg_flags = omode;
                     65:        ioctl(fileno(ofp), TIOCSETP, &sg);
                     66:        if (omode & ECHO)
                     67:                putc('\n', ofp);
                     68: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.