int utmpx_get_entry(struct logininfo *li) { struct utmpx *utx; if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0) return (0); utx = getutxuser(li->username); if (utx == NULL) { endutxent(); return (0); } line_fullname(li->line, utx->ut_line, MIN_SIZEOF(li->line, utx->ut_line)); strlcpy(li->hostname, utx->ut_host, MIN_SIZEOF(li->hostname, utx->ut_host)); li->tv_sec = utx->ut_tv.tv_sec; li->tv_usec = utx->ut_tv.tv_usec; endutxent(); return (1); }
void enter_lastlog(PERSON *pn) { WHERE *w; struct utmpx *ut = NULL; char doit = 0; if (setutxdb(UTXDB_LASTLOGIN, NULL) == 0) ut = getutxuser(pn->name); if ((w = pn->whead) == NULL) doit = 1; else if (ut != NULL && ut->ut_type == USER_PROCESS) { /* if last login is earlier than some current login */ for (; !doit && w != NULL; w = w->next) if (w->info == LOGGEDIN && w->loginat < ut->ut_tv.tv_sec) doit = 1; /* * and if it's not any of the current logins * can't use time comparison because there may be a small * discrepancy since login calls time() twice */ for (w = pn->whead; doit && w != NULL; w = w->next) if (w->info == LOGGEDIN && strcmp(w->tty, ut->ut_line) == 0) doit = 0; } if (ut != NULL && doit) { w = walloc(pn); w->info = LASTLOG; strcpy(w->tty, ut->ut_line); strcpy(w->host, ut->ut_host); w->loginat = ut->ut_tv.tv_sec; } endutxent(); }
int main(int argc, char *argv[]) { int ch, i, ulistsize; struct utmpx *u, *ulist; while ((ch = getopt(argc, argv, "f:rt")) != -1) { switch (ch) { case 'f': file = optarg; break; case 'r': order = -1; break; case 't': utcmp = utcmp_time; break; default: usage(); } } argc -= optind; argv += optind; if (argc > 0) { /* Process usernames given on the command line. */ for (i = 0; i < argc; i++) { if (setutxdb(UTXDB_LASTLOGIN, file) != 0) err(1, "failed to open lastlog database"); if ((u = getutxuser(argv[i])) == NULL) { warnx("user '%s' not found", argv[i]); continue; } output(u); endutxent(); } } else { /* Read all lastlog entries, looking for active ones. */ if (setutxdb(UTXDB_LASTLOGIN, file) != 0) err(1, "failed to open lastlog database"); ulist = NULL; ulistsize = 0; while ((u = getutxent()) != NULL) { if (u->ut_type != USER_PROCESS) continue; if ((ulistsize % 16) == 0) { ulist = realloc(ulist, (ulistsize + 16) * sizeof(struct utmpx)); if (ulist == NULL) err(1, "malloc"); } ulist[ulistsize++] = *u; } endutxent(); qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp); for (i = 0; i < ulistsize; i++) output(&ulist[i]); } exit(0); }