11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
3- #*****************************************************************************
3+ # *****************************************************************************
44#
55# Copyright (c) 2013 Andrea Bonomi <andrea.bonomi@gmail.com>
66#
2424# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2525# IN THE SOFTWARE.
2626#
27- #*****************************************************************************
27+ # *****************************************************************************
2828
2929# pts/1 2013-06-06 18:09 23120 id=ts/1 term=0 exit=0
3030# system boot 2013-05-20 21:27
4545# pts/30 2013-07-24 14:40 19054 id=s/30 term=0 exit=0
4646# pts/28 2013-07-30 20:49 24942 id=s/28 term=0 exit=0
4747# pts/27 2013-08-02 17:59 31326 id=s/27 term=0 exit=0
48- #012345678901234567890123456789012345678901234567890123456789012345678901234567890
48+ # 012345678901234567890123456789012345678901234567890123456789012345678901234567890
4949from cstruct import define , typedef , MemCStruct , NATIVE_ORDER
5050import sys
5151import time
5757typedef ("int" , "pid_t" )
5858typedef ("long" , "time_t" )
5959
60+
6061class ExitStatus (MemCStruct ):
6162 __struct__ = """
6263 short e_termination; /* Process termination status. */
6364 short e_exit; /* Process exit status. */
6465 """
66+
67+
6568class Timeval (MemCStruct ):
6669 __struct__ = """
6770 int32_t tv_sec; /* Seconds. */
6871 int32_t tv_usec; /* Microseconds. */
6972 """
7073
74+
7175def str_from_c (string ):
72- #return str(string.split("\0")[0])
76+ # return str(string.split("\0")[0])
7377 return string .decode ().split ("\0 " )[0 ]
7478
79+
7580class Utmp (MemCStruct ):
7681 __byte_order__ = NATIVE_ORDER
7782 __struct__ = """
@@ -94,13 +99,22 @@ class Utmp(MemCStruct):
9499 def print_info (self , all_ ):
95100 "andreax + pts/0 2013-08-21 08:58 . 32341 (l26.box)"
96101 " pts/34 2013-06-12 15:04 26396 id=s/34 term=0 exit=0"
97- if all_ or self .ut_type in [6 ,7 ]:
98- print ("%-10s %-12s %15s %15s %-8s" % (
102+ if all_ or self .ut_type in [6 , 7 ]:
103+ print (
104+ "%-10s %-12s %15s %15s %-8s"
105+ % (
99106 str_from_c (self .ut_user ),
100107 str_from_c (self .ut_line ),
101108 time .strftime ("%Y-%m-%d %H:%M" , time .gmtime (self .ut_tv .tv_sec )),
102109 self .ut_pid ,
103- str_from_c (self .ut_host ) and "(%s)" % str_from_c (self .ut_host ) or str_from_c (self .ut_id ) and "id=%s" % str_from_c (self .ut_id ) or "" ))
110+ str_from_c (self .ut_host )
111+ and "(%s)" % str_from_c (self .ut_host )
112+ or str_from_c (self .ut_id )
113+ and "id=%s" % str_from_c (self .ut_id )
114+ or "" ,
115+ )
116+ )
117+
104118
105119def main ():
106120 utmp = len (sys .argv ) > 1 and sys .argv [1 ] or "/var/run/utmp"
@@ -110,6 +124,6 @@ def main():
110124 while utmp .unpack (f ):
111125 utmp .print_info (all_ )
112126
127+
113128if __name__ == "__main__" :
114129 main ()
115-
0 commit comments