#!/usr/bin/env python
# $Id: who.py,v 1.2 2001-03-20 10:24:33-06 annis Exp $
# $Source: /u/annis/code/NewMom/kstat/examples/RCS/who.py,v $
#
# Copyright (c) 2001 William S. Annis.  All rights reserved.
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl (the Artistic Licence).  Developed at
# the Department of Biostatistics and Medical Informatics, University
# of Wisconsin, Madison.

"""Reproduce the solaris who(1) program.

I don't bother to create the exact same formatting, but you get
the idea.
"""

from utmpx import *
from kstats import *
from time import localtime, strftime
import sys

def who():
    """who() - get info on users currently logged in"""
    # rewind first
    setutxent()
    ut = getutxent()
    users = []
    while ut:
        if ut['ut_type'] == 'USER_PROCESS':
            users.append(ut)
        ut = getutxent()
    endutxent()

    return users

def format_who(out=sys.stdout):
    """pretend to be Solaris' who(1) program"""
    users = who()
    for u in users:
        out.write("%-8s   %-8s\t%s \t(%s)\n" % (u['ut_user'],
                  u['ut_line'],
                  strftime("%h %d %R", localtime(u['ut_tv'])),
                  u['ut_host']))
                                             

if __name__ == '__main__':
    #print who()
    format_who()


# EOF