Back to index

openldap  2.4.31
Defines | Functions
getpass.c File Reference
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/ctype.h>
#include <ac/signal.h>
#include <ac/string.h>
#include <ac/termios.h>
#include <ac/time.h>
#include <ac/unistd.h>
#include <lber.h>
#include <ldap.h>
#include "ldap_defaults.h"

Go to the source code of this file.

Defines

#define PBUF   512
#define TTY   "/dev/tty"

Functions

char * lutil_getpass (const char *prompt)

Define Documentation

#define PBUF   512

Definition at line 61 of file getpass.c.

#define TTY   "/dev/tty"

Definition at line 66 of file getpass.c.


Function Documentation

char* lutil_getpass ( const char *  prompt)

Definition at line 70 of file getpass.c.

{
       static char pbuf[PBUF];
       FILE *fi;
       int c;
       unsigned i;
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
       TERMIO_TYPE ttyb;
       TERMFLAG_TYPE flags;
       RETSIGTYPE (*sig)( int sig );
#endif

       if( prompt == NULL ) prompt = _("Password: ");

#ifdef DEBUG
       if (debug & D_TRACE)
              printf("->getpass(%s)\n", prompt);
#endif

#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
       if ((fi = fopen(TTY, "r")) == NULL)
              fi = stdin;
       else
              setbuf(fi, (char *)NULL);
       if (fi != stdin) {
              if (GETATTR(fileno(fi), &ttyb) < 0)
                     perror("GETATTR");
              sig = SIGNAL (SIGINT, SIG_IGN);
              flags = GETFLAGS( ttyb );
              SETFLAGS( ttyb, flags & ~ECHO );
              if (SETATTR(fileno(fi), &ttyb) < 0)
                     perror("SETATTR");
       }
#else
       fi = stdin;
#endif
       fprintf(stderr, "%s", prompt); 
       fflush(stderr);
       i = 0;
       while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
              if ( i < (sizeof(pbuf)-1) )
                     pbuf[i++] = c;
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
       /* tidy up */
       if (fi != stdin) {
              fprintf(stderr, "\n"); 
              fflush(stderr);
              SETFLAGS( ttyb, flags );
              if (SETATTR(fileno(fi), &ttyb) < 0)
                     perror("SETATTR");
              (void) SIGNAL (SIGINT, sig);
              (void) fclose(fi);
       }
#endif
       if ( c == EOF )
              return( NULL );
       pbuf[i] = '\0';
       return (pbuf);
}

Here is the call graph for this function: