#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <conio.h>
#include <ctype.h>

void getpw(char *password, int max);
#ifdef TESTING_GETPASS
int main(int argc, char *argv[])
{
	char bf[300];

	printf("Password: "); getpw(bf,100);
	printf("result %d (%s)\n",strlen(bf),bf);
	return 0;
}
#endif

/*	Gets a password from the console, of length < max, without echoing  */

#include "defines.h"

void getpw(char *password, int max)
{
	char pw[BFSZ];
	int ch, n;

	n = 0;
	ch = -1;
	while ((ch != 0) && (n<max)){
		ch = getch();

		if ((ch == 10) || (ch == 13) || (ch == 3) || (ch == 27)) ch = 0;

		if ((ch == 8) || (ch==127)) {
			pw[n] = 0;
			if (n>0) n--;
			printf("%c",8);
			printf(" ",8);
			printf("%c",8);
		} else {
			printf("*");
			pw[n] = ch;
			n++;
		}
	}
	printf("\n");

	strcpy(password, pw);
}
char *getpass(char *prompt)  /* This function works on unix */
{
	static char bf[300];
	printf("%s",prompt);
	getpw(bf,200);
	return bf;
}
