On Sun, 3 Sep 2006, Ola Lundqvist wrote:

I tried to implement a getpass function myself but I got into a number
of problems, like: surpressing character echoing, getc seem not to return
until newline is pressed and so on... I have tried fread and many other
ways but without success.

I have had a look at getpass in glibc, and it seems to suffer somewhat from relying on the internals of glibc, although a brief inspection suggests that it is possible to extract working POSIX code.

However, I'm more attracted by the following code, which I found in the glibc info documentation:

#include <termios.h>
#include <stdio.h>

ssize_t
my_getpass (char **lineptr, size_t *n, FILE *stream)
{
  struct termios old, new;
  int nread;

  /* Turn echoing off and fail if we can't. */
  if (tcgetattr (fileno (stream), &old) != 0)
    return -1;
  new = old;
  new.c_lflag &= ~ECHO;
  if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
    return -1;

  /* Read the password. */
  nread = getline (lineptr, n, stream);

  /* Restore terminal. */
  (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);

  return nread;
}

How does that look?

BTW, this brings up one vexed point: the info docs are under the GFDL, not the GPL. I'll check that the code examples are (as the GFDL itself suggests) under a suitable license; I can't find anything in the docs themselves, so I'm posting to glibc-bugs.

--
http://rrt.sc3d.org/ | fantasize, a.  as big as fizzy orange


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to