/*
 * dumputmp.
 *
 * Written by Andrew G. Morgan. Copylefted and subject to the GNU GPL.
 */

#include <stdio.h>
#include <time.h>
#include <paths.h>
#include <utmp.h>

#ifndef UT_IDSIZE
#define UT_IDSIZE 4            /* XXX - this is sizeof(struct utmp.ut_id) */
#endif


static void dumputmp(void)
{
    time_t temp;
    struct utmp u_tmp, *u_tmp_p;

    utmpname(_PATH_UTMP);
    setutent();                                              /* rewind file */

    /* work through entries */
    while ((u_tmp_p = getutent()) != NULL) {
	static const char *entry_type[] = {
	    "*not known*",
	    "run level",
	    "boot time",
	    "new time",
	    "old time",
	    "init process",
	    "login process",
	    "user process",
	    "dead process"
	};
	if (u_tmp_p->ut_type > 0 && u_tmp_p->ut_type <= DEAD_PROCESS)
	    printf("[%s]", entry_type[u_tmp_p->ut_type]);
	else
	    printf("[?type:%d]", u_tmp_p->ut_type);
	printf("<pid=%d>", u_tmp_p->ut_pid);
#if UT_LINESIZE == 32
	printf("[%.32s]", u_tmp_p->ut_line);
#else
	printf("[%.12s]", u_tmp_p->ut_line);
#endif
	printf("<%.4s>", u_tmp_p->ut_id);
	temp = (time_t) u_tmp_p->ut_time;
	printf("<%.24s>", ctime(&temp));
#if UT_NAMESIZE == 32
	printf("[%.32s]", u_tmp_p->ut_user);
#else
	printf("[%.8s]", u_tmp_p->ut_user);
#endif
#if UT_HOSTSIZE == 256
	printf("{%.256s}", u_tmp_p->ut_host);
#else
	printf("{%.16s}", u_tmp_p->ut_host);
#endif
	printf("{%d.",  u_tmp_p->ut_addr & 0xFF);
	printf("%d.", (u_tmp_p->ut_addr >> 8) & 0xFF);
	printf("%d.", (u_tmp_p->ut_addr >> 16) & 0xFF);
	printf("%d}.", (u_tmp_p->ut_addr >> 24) & 0xFF);
	printf("\n");
    }

    endutent();                                              /* close file */
}

main()
{
    dumputmp();
    return 0;
}
