File Coverage

blib/lib/Sys/Utmp.pm
Criterion Covered Total %
statement 41 43 95.3
branch 5 10 50.0
condition 3 6 50.0
subroutine 11 11 100.0
pod 1 1 100.0
total 61 71 85.9


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   185766 use strict;
  6         70  
  6         224  
92 6     6   45 use warnings;
  6         14  
  6         248  
93 6     6   46 use Carp qw(croak);
  6         16  
  6         526  
94              
95             require Exporter;
96             require DynaLoader;
97              
98 6     6   47 use base qw(Exporter DynaLoader);
  6         14  
  6         1451  
99              
100             BEGIN
101             {
102 6     6   204 our @constants = qw(
103             ACCOUNTING
104             BOOT_TIME
105             DEAD_PROCESS
106             EMPTY
107             INIT_PROCESS
108             LOGIN_PROCESS
109             NEW_TIME
110             OLD_TIME
111             RUN_LVL
112             USER_PROCESS
113             );
114             }
115              
116 6     6   2456 use Sys::Utmp::Utent;
  6         20  
  6         1772  
117              
118             BEGIN
119             {
120 6     6   60 our %EXPORT_TAGS = (
121             'constants' => [ @Sys::Utmp::constants ],
122             'fields' => [ @Sys::Utmp::Utent::EXPORT]
123             );
124              
125 6         17 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'constants'} }, @{ $EXPORT_TAGS{'fields'}} );
  6         23  
  6         1999  
126             }
127              
128             our $VERSION = '1.8';
129              
130             sub new {
131 5     5 1 1713 my ( $proto, %args ) = @_;
132              
133 5         21 my $self = {};
134 5   33     41 my $class = ref($proto) || $proto;
135 5         16 bless $self, $class;
136              
137 5 50 66     68 if ( exists $args{Filename} and -s $args{Filename} ) {
138 0         0 $self->utmpname($args{Filename});
139             }
140 5         23 return $self;
141             }
142              
143             our $AUTOLOAD;
144              
145             sub AUTOLOAD {
146 10     10   85 my ( $self ) = @_;
147              
148 10         12 my $constname;
149 10 50       28 return if $AUTOLOAD =~ /DESTROY/;
150              
151 10         47 ($constname = $AUTOLOAD) =~ s/.*:://;
152 10 50       20 croak "& not defined" if $constname eq 'constant';
153 10 50       34 my $val = constant($constname, @_ ? $_[0] : 0);
154 10 50       27 if ($! != 0) {
155 0         0 croak "Your vendor has not defined Sys::Utmp macro $constname";
156             }
157             {
158 6     6   55 no strict 'refs';
  6         15  
  6         693  
  10         11  
159 10     10   32 *{$AUTOLOAD} = sub { $val };
  10         36  
  10         34  
160             }
161 10         26 goto &$AUTOLOAD;
162             }
163              
164              
165             1;
166              
167             bootstrap Sys::Utmp $VERSION;
168              
169             __END__