Reputation: 642
I was trying to get user name and password for authentication on a linux system and gcc-4.7 compiler as follows:
#include "stdio.h"
#include "stdlib.h"
#include <termios.h>
#include <unistd.h>
void getuserdata(char *passwd)
{
struct termios term, term_orig;
tcgetattr(STDIN_FILENO, &term);
term_orig = term;
term.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
scanf("%s", passwd);
/* Remember to set back, or your commands won't echo! */
tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
}
int main()
{
char *password, *username;
printf("Enter username: ");
scanf("%s", username);
fflush(stdin);
printf("\n");
printf("\nEnter password: ");
getuserdata(password);
printf("Entered username is:%s\n", username);
printf("Entered password is:%s\n", password);
return 0;
}
and expected behaviour was as follows:
Enter username: test
Enter password:
Entered username is: test
Entered password is: xxxx
But it is not working and giving null as username or password.
Guys where am I going wrong?
Any help would be greatly appreciated.
Thanks.
Upvotes: 0
Views: 87
Reputation: 12270
You did not allocate memory for username
and password
.
char *password, *username;
username = malloc(50);
printf("Enter username: ");
scanf("%49s", username);
printf("\n");
printf("\nEnter password: ");
password = malloc(50);
getuserdata(password);
Upvotes: 3