Skip to content

Instantly share code, notes, and snippets.

@gcmurphy
Created November 1, 2012 05:17
Show Gist options
  • Save gcmurphy/3991998 to your computer and use it in GitHub Desktop.
Save gcmurphy/3991998 to your computer and use it in GitHub Desktop.
who
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <utmpx.h>
int main(void)
{
struct timeval last_hour;
struct timeval last_login;
struct utmpx *entry = NULL;
gettimeofday(&last_hour, NULL);
last_hour.tv_sec = last_hour.tv_sec - 3600;
while ((entry = getutxent()) != NULL) {
printf("User %s is using %s. The pid is %d\n",
entry->ut_user, entry->ut_line, entry->ut_pid);
if (0 == timercmp(&(entry->ut_tv), &(last_hour), <)){
printf("%s logged in within the last hour\n", entry->ut_user);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment