[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getpass.c



Waay back when, Andreas had this to say about Wolfgang Lux's
patch to getpass.c:

<flashback>
From: Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
Date: Fri, 28 Apr 95 14:37:46 +0200

|> diff -u -xChangeLog -xMakefile /usr/src/libsrc/getpass.c. /getpass.c
|> --- /usr/src/libsrc/getpass.c	Tue Mar 14 21:01:08 1995
|> +++ ./getpass.c	Thu Apr 13 22:52:16 1995
[...]
|> +	  l = strlen(buf);
|> +	  if (buf[l-1] != '\n') {
|> +	    while (fgetc(tty) != '\n')
|> +	      /* wait for a newline */ ;
|> +	  }

    Another Problem: this should check for EOF!  Otherwise
    you'll get an infinite loop.
</flashback>

Background: this loop is part of a routine that takes a string
from a tty, then strips the trailing end-of-line character.
I modified it to do the same thing if it encounters an EOF:

	{
	  /* zap the newline; if we get an EOF instead, 
             we zap that, too. */
	  l = strlen(buf);
	  if (buf[l-1] != '\n') {
	    while (((c = fgetc(tty)) != '\n') && ((int) c != EOF))
	      /* wait for a newline or an EOF */ ;
	  }
	  if (l > PASS_MAX)
	    buf[PASS_MAX] = '\0';
	  else if ((buf[l-1] == '\n') || ((int) buf[l-1] == EOF)) 
	    buf[l-1] = '\0';
	}

I need to know if I did this correctly; I hate to say it again,
but my C is a bit shaky.  Is handling EOF this way, acceptable
for what getpass() does?  Should it return an error code
instead? 

Thanks

Yves