diff options
Diffstat (limited to 'getutxent.c')
-rw-r--r-- | getutxent.c | 160 |
1 files changed, 4 insertions, 156 deletions
diff --git a/getutxent.c b/getutxent.c index e0d9932..498f4ae 100644 --- a/getutxent.c +++ b/getutxent.c @@ -29,8 +29,6 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); -#include "namespace.h" -#include <sys/endian.h> #include <sys/param.h> #include <sys/stat.h> #include <errno.h> @@ -38,7 +36,9 @@ __FBSDID("$FreeBSD$"); #include <string.h> #include <utmpx.h> #include "utxdb.h" -#include "un-namespace.h" + +#include "utmpx-defines.h" +#define rounddown(x, y) (((x) / (y)) * (y)) #ifdef __NO_TLS static FILE *uf = NULL; @@ -79,7 +79,7 @@ setutxdb(int db, const char *file) if (db != UTXDB_LOG) { /* Safety check: never use broken files. */ - if (_fstat(fileno(uf), &sb) != -1 && + if (fstat(fileno(uf), &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { fclose(uf); uf = NULL; @@ -94,155 +94,3 @@ setutxdb(int db, const char *file) udb = db; return (0); } - -void -setutxent(void) -{ - - setutxdb(UTXDB_ACTIVE, NULL); -} - -void -endutxent(void) -{ - - if (uf != NULL) { - fclose(uf); - uf = NULL; - } -} - -static int -getfutxent(struct futx *fu) -{ - - if (uf == NULL) - setutxent(); - if (uf == NULL) - return (-1); - - if (udb == UTXDB_LOG) { - uint16_t len; - -retry: - if (fread(&len, sizeof(len), 1, uf) != 1) - return (-1); - len = be16toh(len); - if (len == 0) { - /* - * XXX: Though zero-size records are valid in theory, - * they can never occur in practice. Zero-size records - * indicate file corruption. Seek one byte forward, to - * see if we can find a record there. - */ - ungetc('\0', uf); - goto retry; - } - if (len > sizeof *fu) { - /* Forward compatibility. */ - if (fread(fu, sizeof(*fu), 1, uf) != 1) - return (-1); - fseek(uf, len - sizeof(*fu), SEEK_CUR); - } else { - /* Partial record. */ - memset(fu, 0, sizeof(*fu)); - if (fread(fu, len, 1, uf) != 1) - return (-1); - } - } else { - if (fread(fu, sizeof(*fu), 1, uf) != 1) - return (-1); - } - return (0); -} - -struct utmpx * -getutxent(void) -{ - struct futx fu; - - if (getfutxent(&fu) != 0) - return (NULL); - return (futx_to_utx(&fu)); -} - -struct utmpx * -getutxid(const struct utmpx *id) -{ - struct futx fu; - - for (;;) { - if (getfutxent(&fu) != 0) - return (NULL); - - switch (fu.fu_type) { - case USER_PROCESS: - case INIT_PROCESS: - case LOGIN_PROCESS: - case DEAD_PROCESS: - switch (id->ut_type) { - case USER_PROCESS: - case INIT_PROCESS: - case LOGIN_PROCESS: - case DEAD_PROCESS: - if (memcmp(fu.fu_id, id->ut_id, - MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) == - 0) - goto found; - } - break; - default: - if (fu.fu_type == id->ut_type) - goto found; - break; - } - } - -found: - return (futx_to_utx(&fu)); -} - -struct utmpx * -getutxline(const struct utmpx *line) -{ - struct futx fu; - - for (;;) { - if (getfutxent(&fu) != 0) - return (NULL); - - switch (fu.fu_type) { - case USER_PROCESS: - case LOGIN_PROCESS: - if (strncmp(fu.fu_line, line->ut_line, - MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) == - 0) - goto found; - break; - } - } - -found: - return (futx_to_utx(&fu)); -} - -struct utmpx * -getutxuser(const char *user) -{ - struct futx fu; - - for (;;) { - if (getfutxent(&fu) != 0) - return (NULL); - - switch (fu.fu_type) { - case USER_PROCESS: - if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0) - goto found; - break; - } - } - -found: - return (futx_to_utx(&fu)); -} |