#include #include #include "libc.h" #define _PATH_UTMPX "/etc/utmp" static FILE *f; static struct utmpx ut; void endutxent(void) { memset(&ut, 0, sizeof ut); if (f == NULL) return; fclose(f); f == NULL; } void setutxent(void) { memset(&ut, 0, sizeof ut); if (f == NULL) return; rewind(f); } struct utmpx *getutxent(void) { if (f == NULL) { f = fopen(_PATH_UTMPX, "a+"); if (f == NULL) { f = fopen(_PATH_UTMPX, "r"); if (f == NULL) return NULL; } } if (fread(&ut, sizeof ut, 1, f)) return &ut; return NULL; } struct utmpx *getutxid(const struct utmpx *id) { while(getutxent()) { switch (id->ut_type) { case BOOT_TIME: case OLD_TIME: case NEW_TIME: if (id->ut_type == ut.ut_type) return &ut; break; case INIT_PROCESS: case LOGIN_PROCESS: case USER_PROCESS: case DEAD_PROCESS: if (id->type == ut.ut_type && !strcmp(id->ut_id, ut.ut_id)) return &ut; break; } } return NULL; } struct utmpx *getutxline(const struct utmpx *line) { while(getutxent()) { switch (ut.ut_type) { case LOGIN_PROCESS: case USER_PROCESS: if (!strcmp(line->ut_line, ut.ut_line)) return &ut; break; } } return NULL; } struct utmpx *pututxline(const struct utmpx *utmpx) { setutxent(); if (getutxid(utmpx)) fseek(f, -(sizeof ut), SEEK_CUR); if (fwrite(&ut, sizeof ut, 1, f)) return &ut; return NULL; } void updwtmpx(const char *f, const struct utmpx *u) { } weak_alias(endutxent, endutent); weak_alias(setutxent, setutent); weak_alias(getutxent, getutent); weak_alias(getutxid, getutid); weak_alias(getutxline, getutline); weak_alias(pututxline, pututline); weak_alias(updwtmpx, updwtmp);