With some assistance, I wrote this code in C to perform the functions of the
last command. It displays some of the fields from the wtmp file. Instead of reporting the hostname, I'd like to report the IP address. From the man pages, it looks as though the
host command will find IP addresses. But, do I need to use pipes to perform this action? Maybe something like
read_fp = popen("host", "r"). Of course, the IP address field may be empty and in this case I can report something such as "empty" and perform no DNS lookup.
Code:
#include <utmp.h>
#include <utmpx.h>
#include <stdio.h>
#include <iostream>
#define WTMP_FILENAME "/var/log/wtmp"
int main()
{
struct utmp record;
FILE *fd = fopen(WTMP_FILENAME, "r");
if (fd == NULL) {
perror(WTMP_FILENAME);
return 1;
}
while (fread(&record, sizeof record, 1, fd) != 0) {
printf("%s: %s\t: %s\t", record.ut_line, record.ut_user, record.ut_id);
}
return 0;
}