]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/utmpx/utmpx.factor
e1756daa0071068518b8b0e5a9c81c2eb1ddd6e5
[factor.git] / basis / unix / utmpx / utmpx.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.syntax combinators continuations
4 io.encodings.string io.encodings.utf8 kernel sequences strings
5 unix calendar system accessors unix.time calendar.unix
6 vocabs.loader ;
7 IN: unix.utmpx
8
9 : EMPTY 0 ; inline
10 : RUN_LVL 1 ; inline
11 : BOOT_TIME 2 ; inline
12 : OLD_TIME 3 ; inline
13 : NEW_TIME 4 ; inline
14 : INIT_PROCESS 5 ; inline
15 : LOGIN_PROCESS 6 ; inline
16 : USER_PROCESS 7 ; inline
17 : DEAD_PROCESS 8 ; inline
18 : ACCOUNTING 9 ; inline
19 : SIGNATURE 10 ; inline
20 : SHUTDOWN_TIME 11 ; inline
21
22 FUNCTION: void setutxent ( ) ;
23 FUNCTION: void endutxent ( ) ;
24 FUNCTION: utmpx* getutxent ( ) ;
25 FUNCTION: utmpx* getutxid ( utmpx* id ) ;
26 FUNCTION: utmpx* getutxline ( utmpx* line ) ;
27 FUNCTION: utmpx* pututxline ( utmpx* utx ) ;
28
29 TUPLE: utmpx-record user id line pid type timestamp host ;
30
31 HOOK: new-utmpx-record os ( -- utmpx-record )
32
33 HOOK: utmpx>utmpx-record os ( utmpx -- utmpx-record )
34
35 : memory>string ( alien n -- string )
36     memory>byte-array utf8 decode [ 0 = ] trim-right ;
37
38 M: unix new-utmpx-record
39     utmpx-record new ;
40     
41 M: unix utmpx>utmpx-record ( utmpx -- utmpx-record )
42     [ new-utmpx-record ] dip
43     {
44         [ utmpx-ut_user _UTX_USERSIZE memory>string >>user ]
45         [ utmpx-ut_id _UTX_IDSIZE memory>string >>id ]
46         [ utmpx-ut_line _UTX_LINESIZE memory>string >>line ]
47         [ utmpx-ut_pid >>pid ]
48         [ utmpx-ut_type >>type ]
49         [ utmpx-ut_tv timeval>unix-time >>timestamp ]
50         [ utmpx-ut_host _UTX_HOSTSIZE memory>string >>host ]
51     } cleave ;
52
53 : with-utmpx ( quot -- )
54     setutxent [ endutxent ] [ ] cleanup ; inline
55
56 : all-utmpx ( -- seq )
57     [
58         [ getutxent dup ]
59         [ utmpx>utmpx-record ]
60         [ drop ] produce
61     ] with-utmpx ;
62     
63 os {
64     { macosx [ "unix.utmpx.macosx" require ] }
65     { netbsd [ "unix.utmpx.netbsd" require ] }
66 } case