I want to compare user given input i.e a username to the already stored usernames in /etc/passwd file in ubuntu. I am trying it in C. any help please
A:
This code prints all the usernames from /etc/passwd.
#include <stdio.h>
int main()
{
char buffer[128];
char* username;
FILE* passwdFile;
passwdFile = fopen("/etc/passwd", "r");
while (fgets(buffer, 128, passwdFile) != NULL)
{
username = strtok(buffer, ":");
printf("username: %s\n", username);
}
fclose(passwdFile);
return 0;
}
Modify to compare username
to your input.
Starkey
2010-08-26 16:28:55
Much better to use the getpwnam() function that is designed to find the given user name in the password database, which might not always be (just) the /etc/passwd file.
Jonathan Leffler
2010-08-27 02:10:53