/* * detect the local user * * rml@ximian.com */ #include #include #include int main() { struct utmp *u; char user[UT_NAMESIZE] = "root"; time_t recent = 0; setutent(); while ((u = getutent())) { /* is this a user login ? */ if (u->ut_type != USER_PROCESS) continue; /* XXX: should probably check if the pid is stale */ /* is this a local login ? */ if (strcmp(u->ut_host, "")) continue; if (u->ut_time > recent) { recent = u->ut_time; strcpy(user, u->ut_user); } } endutent(); printf("%s\n", user); return 0; }