From: Eric Backus Subject: getpass() To: djgpp AT sun DOT soe DOT clarkson DOT edu (djgpp) Date: Mon, 10 Feb 92 12:34:22 PST Mailer: Elm [revision: 66.25] Status: O Several weeks ago I asked for help turning off echoing so I could prompt for a password. In return for the help, here is a UNIX compatible getpass() function for djgpp. It can be put into /djgpp/libsrc/c/gen, but then getkey() will have to be put into the libc.a library. -- Eric Backus ericb%hplsla AT hplabs DOT hp DOT com (206) 335-2495 /* "@(#)$Header: getpass.c,v 1.0 92/02/10 12:07:30 ericb Rel $" */ /* (c) Copyright 1992 Eric Backus This software may be used freely so long as this copyright notice is left intact. There is no warrantee on this software. */ #include #include /* For exit() */ #include /* For getkey() */ /* This is superior to the standard getpass(), because it doesn't overwrite previous calls and allows for passwords of any length. */ int getlongpass(const char *prompt, char *password, int max_length) { char *p = password; int c, count = 0; /* If we can't prompt, abort */ if (fputs(prompt, stderr) < 0) { *p = '\0'; return -1; } while (1) { /* Get a character with no echo */ c = getkey(); /* Exit on interrupt (^c or ^break) */ if (c == '\003' || c == 0x100) exit(1); /* Terminate on end of line or file (^j, ^m, ^d, ^z) */ if (c == '\r' || c == '\n' || c == '\004' || c == '\032') break; /* Back up on backspace */ if (c == '\b') { if (count) count--; else if (p > password) p--; continue; } /* Ignore DOS extended characters */ if ((c & 0xff) != c) continue; /* Add to password if it isn't full */ if (p < password + max_length - 1) *p++ = c; else count++; } *p = '\0'; (void) fputc('\n', stderr); return 0; } /* UNIX compatible getpass(). Returns a pointer to a null-terminated static buffer of at most eight characters. */ char * getpass(const char *prompt) { static char password_buffer[9]; if (getlongpass(prompt, password_buffer, 9) < 0) return (char *) NULL; return password_buffer; }