File Coverage

blib/lib/Sys/Utmp.pm
Criterion Covered Total %
statement 38 40 95.0
branch 5 10 50.0
condition 3 6 50.0
subroutine 10 10 100.0
pod 1 1 100.0
total 57 67 85.0


line stmt bran cond sub pod time code
1             #*****************************************************************************
2             #* *
3             #* Gellyfish Software *
4             #* *
5             #* *
6             #*****************************************************************************
7             #* *
8             #* MODULE : Sys::Utmp *
9             #* *
10             #* AUTHOR : JNS *
11             #* *
12             #* DESCRIPTION : Object(ish) interface to utmp information *
13             #* *
14             #* *
15             #*****************************************************************************
16              
17             package Sys::Utmp;
18              
19             =head1 NAME
20              
21             Sys::Utmp - Object(ish) Interface to UTMP files.
22              
23             =head1 SYNOPSIS
24              
25             use Sys::Utmp;
26              
27             my $utmp = Sys::Utmp->new();
28              
29             while ( my $utent = $utmp->getutent() )
30             {
31             if ( $utent->user_process )
32             {
33             print $utent->ut_user,"\n";
34             }
35             }
36              
37             $utmp->endutent;
38              
39             See also examples/pwho in the distribution directory.
40              
41             =head1 DESCRIPTION
42              
43             Sys::Utmp provides a vaguely object oriented interface to the Unix user
44             accounting file ( sometimes /etc/utmp or /var/run/utmp). Whilst it would
45             prefer to use the getutent() function from the systems C libraries it
46             will attempt to provide its own if they are missing.
47              
48             This may not be the module that you are looking for - there is a User::Utmp
49             which provides a different procedural interface and may well be more complete
50             for your purposes.
51              
52             =head2 METHODS
53              
54             =over 4
55              
56             =item new
57              
58             The constructor of the class. Arguments may be provided in Key => Value
59             pairs : it currently takes one argument 'Filename' which will set the file
60             which is to be used in place of that defined in _PATH_UTMP.
61              
62             =item getutent
63              
64             Iterates of the records in the utmp file returning a Sys::Utmp::Utent object
65             for each record in turn - the methods that are available on these objects
66             are descrived in the L documentation. If called in a list
67             context it will return a list containing the elements of th Utent entry
68             rather than an object. If the import flag ':fields' is used then constants
69             defining the indexes into this list will be defined, these are uppercase
70             versions of the methods described in L.
71              
72             =item setutent
73              
74             Rewinds the file pointer on the utmp filehandle so repeated searches can be
75             done.
76              
77             =item endutent
78              
79             Closes the file handle on the utmp file.
80              
81             =item utmpname SCALAR filename
82              
83             Sets the file that will be used in place of that defined in _PATH_UTMP.
84             It is not defined what will happen if this is done between two calls to
85             getutent() - it is recommended that endutent() is called first.
86              
87             =back
88              
89             =cut
90              
91 6     6   55662 use strict;
  6         12  
  6         265  
92 6     6   30 use Carp;
  6         9  
  6         627  
93              
94             require Exporter;
95             require DynaLoader;
96              
97 6         882 use vars qw(
98             @ISA
99             %EXPORT_TAGS
100             @EXPORT_OK
101             @EXPORT
102             $VERSION
103             $AUTOLOAD
104             @constants
105 6     6   38 );
  6         16  
106              
107             @ISA = qw(Exporter DynaLoader);
108              
109             BEGIN
110             {
111 6     6   141 @constants = qw(
112             ACCOUNTING
113             BOOT_TIME
114             DEAD_PROCESS
115             EMPTY
116             INIT_PROCESS
117             LOGIN_PROCESS
118             NEW_TIME
119             OLD_TIME
120             RUN_LVL
121             USER_PROCESS
122             );
123             }
124 6     6   3396 use Sys::Utmp::Utent;
  6         13  
  6         840  
125              
126             BEGIN
127             {
128 6     6   43 %EXPORT_TAGS = (
129             'constants' => [ @constants ],
130             'fields' => [ @Sys::Utmp::Utent::EXPORT]
131             );
132              
133 6         11 @EXPORT_OK = ( @{ $EXPORT_TAGS{'constants'} }, @{ $EXPORT_TAGS{'fields'}} );
  6         14  
  6         1499  
134             }
135              
136             $VERSION = '1.7';
137              
138             sub new
139             {
140 5     5 1 1415 my ( $proto, %args ) = @_;
141              
142 5         13 my $self = {};
143              
144 5   33     38 my $class = ref($proto) || $proto;
145              
146 5         12 bless $self, $class;
147              
148 5 50 66     113 if ( exists $args{Filename} and -s $args{Filename} )
149             {
150 0         0 $self->utmpname($args{Filename});
151             }
152            
153 5         18 return $self;
154             }
155              
156              
157             sub AUTOLOAD
158             {
159 10     10   52 my ( $self ) = @_;
160              
161 10         14 my $constname;
162 10 50       48 return if $AUTOLOAD =~ /DESTROY/;
163              
164 10         68 ($constname = $AUTOLOAD) =~ s/.*:://;
165 10 50       29 croak "& not defined" if $constname eq 'constant';
166 10 50       63 my $val = constant($constname, @_ ? $_[0] : 0);
167 10 50       42 if ($! != 0)
168             {
169 0         0 croak "Your vendor has not defined Sys::Utmp macro $constname";
170             }
171             {
172 6     6   34 no strict 'refs';
  6         10  
  6         693  
  10         12  
173 10     10   53 *{$AUTOLOAD} = sub { $val };
  10         57  
  10         50  
174             }
175 10         47 goto &$AUTOLOAD;
176             }
177              
178              
179             1;
180              
181             bootstrap Sys::Utmp $VERSION;
182              
183             __END__