Following is the final code I was working on. I can sleep and show again other messages with sleep() but I can't print what I originally wanted which is inside 2nd while loop. As far as I tested, while((ptr=getutent()) != NULL) would be the problem but I don't know how to solve. I would really appreciate if anyone can help me. Thanks
Note: The program is showing current login user for every 5 seconds until user stop with Ctrl+c.
#include <utmp.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
struct utmp *ptr;
struct passwd *pwd;
while(1)
{
while((ptr=getutent()) != NULL)
{
if(ptr->ut_type==USER_PROCESS)
{
pwd=getpwnam(ptr->ut_user);
printf("USERNAME = %s | ID = %d | GID = %d | ",p
tr->ut_user,pwd->pw_uid,pwd->pw_gid);
printf("HomeDir = %s | HOST = %s\n",pwd->pw_dir,
ptr->ut_host);
}
}
sleep(3);
fflush(stdout);
}
}