Lastlog.xs

filter: Statementstmt 35.7% 25 / 70 Branchbran 28.9% 11 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.3% 36 / 108 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Sys/Lastlog.pm ↑ Summary
1 /*
2 #*****************************************************************************
3 #* *
4 #* Gellyfish Software *
5 #* *
6 #* *
7 #*****************************************************************************
8 #* *
9 #* PROGRAM : Sys::Lastlog *
10 #* *
11 #* AUTHOR : JNS *
12 #* *
13 #* DESCRIPTION : Provide Object(ish) interface to lastlog file *
14 #* *
15 #* *
16 #*****************************************************************************
17 #* *
18 #* $Id$
19 #* *
20 #* Copyright (C) Jonathan Stowe 2000 - 2009 *
21 #*
22 #*****************************************************************************
23 */
24  
25 #include "EXTERN.h"
26 #include "perl.h"
27 #include "XSUB.h"
28 #include <utmp.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <pwd.h>
34 #include <unistd.h>
35  
36 #ifdef USE_LASTLOG_H
37 #include <lastlog.h>
38 #endif
39  
40 int get_lastlog_fd(void);
41 char *lastlog_path(void);
42  
43 0 struct lastlog *getllent(void)
44 {
45  
46 static struct lastlog llent;
47  
48 int ll_fd;
49  
50 0 ▶ if ( (ll_fd = get_lastlog_fd() ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( (ll_fd = get_lastlog_fd() ) == -1 ) 0 0 0
51 {
52 0 return( ( void *)0);
53 }
54  
55 0 ▶ if(read( ll_fd,&llent, sizeof( struct lastlog )) != sizeof( struct lastlog))
Branch: 1 branch
BranchTrueFalseTotal
if(read( ll_fd,&llent, sizeof( struct lastlog )) != sizeof( struct lastlog)) 0 0 0
56 {
57 0 close(ll_fd);
58 0 return ( (void *)0 );
59 }
60 else
61 {
62 0 return ( &llent );
63 }
64 }
65  
66 2 struct lastlog *getlluid(int uid)
67 {
68 static struct lastlog llent;
69 int ll_fd;
70  
71 off_t where;
72  
73 2 ▶ if ( (ll_fd = get_lastlog_fd() ) == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( (ll_fd = get_lastlog_fd() ) == -1 ) 0 2 2
74 {
75 0 return( ( void *)0);
76 }
77  
78  
79 2 where = lseek(ll_fd,0, SEEK_CUR);
80  
81 2 lseek(ll_fd, (off_t)(uid * sizeof( struct lastlog)), SEEK_SET);
82  
83  
84 2 ▶ if(read( ll_fd,&llent, sizeof( struct lastlog )) != sizeof( struct lastlog))
Branch: 1 branch
BranchTrueFalseTotal
if(read( ll_fd,&llent, sizeof( struct lastlog )) != sizeof( struct lastlog)) 2 0 2
85 {
86 2 lseek(ll_fd,where, SEEK_SET );
87 2 return ( (void *)0 );
88 }
89 else
90 {
91 0 lseek(ll_fd,where, SEEK_SET );
92 0 return ( &llent );
93 }
94 }
95  
96 2 int get_lastlog_fd(void)
97 {
98  
99 static int ll_fd = -1;
100  
101 2 ▶ if ( ll_fd == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ( ll_fd == -1 ) 1 1 2
102 {
103 1 ll_fd = open((char *)lastlog_path(),O_RDONLY);
104 }
105  
106 2 return(ll_fd);
107 }
108  
109 2 char *lastlog_path(void)
110 {
111 2 return _PATH_LASTLOG;
112 }
113 0 void setllent(void)
114 {
115 int ll_fd;
116  
117 0 ▶ if ((ll_fd = get_lastlog_fd()) != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ((ll_fd = get_lastlog_fd()) != -1) 0 0 0
118 {
119 0 lseek(ll_fd,0, SEEK_SET);
120 }
121 0 }
122  
123 0 SV *llent2hashref(IV count, struct lastlog *llent)
124 {
125 HV *ll;
126 SV *ll_ref;
127 HV *meth_stash;
128  
129 0 ll = newHV();
130
131 0 (void *)hv_store(ll,"uid",3,newSViv(count),0);
132 0 (void *)hv_store(ll,"ll_time",7,newSViv((IV)llent->ll_time),0);
133 0 (void *)hv_store(ll,"ll_line",7,newSVpv(llent->ll_line,0),0);
134 0 (void *)hv_store(ll,"ll_host",7,newSVpv(llent->ll_host,0),0);
135 0 meth_stash = gv_stashpv("Sys::Lastlog::Entry",1);
136 0 ll_ref = newRV((SV *)ll);
137 0 sv_bless(ll_ref, meth_stash);
138  
139 0 return(ll_ref);
140 }
141  
142 MODULE = Sys::Lastlog PACKAGE = Sys::Lastlog
143  
144 PROTOTYPES: ENABLE
145  
146 void
147 getllent(self)
148 SV *self
149 PPCODE:
150 struct lastlog *llent;
151 SV *ll_ref;
152  
153 static IV count = 0;
154  
155 0 ▶ if(!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) 0 0 0
156 0 croak("Must be called as an object method");
157  
158  
159 0 llent = getllent();
160  
161 0 ▶ if ( llent )
Branch: 1 branch
BranchTrueFalseTotal
if ( llent ) 0 0 0
162 {
163 0 ll_ref = llent2hashref(count++,llent);
164 0 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 0 0
165 0 PUSHs(sv_2mortal(ll_ref));
166 }
167 else
168 {
169 0 XSRETURN_EMPTY;
170 }
171  
172 void
173 getlluid(self, uid)
174 SV *self
175 IV uid
176 PPCODE:
177 struct lastlog *llent;
178 SV *ll_ref;
179  
180 1 ▶ if(!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) 0 1 1
181 0 croak("Must be called as an object method");
182  
183  
184 1 llent = getlluid(uid);
185  
186 1 ▶ if ( llent )
Branch: 1 branch
BranchTrueFalseTotal
if ( llent ) 0 1 1
187 {
188 0 ll_ref = llent2hashref(uid,llent);
189 0 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 0 0
190 0 PUSHs(sv_2mortal(ll_ref));
191 }
192 else
193 {
194 1 XSRETURN_EMPTY;
195 }
196  
197 void
198 getllnam(self,logname)
199 SV *self
200 char *logname
201 PPCODE:
202 struct passwd *pwd;
203 struct lastlog *llent;
204 SV *ll_ref;
205  
206 1 ▶ if(!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) 0 1 1
207 0 croak("Must be called as an object method");
208  
209 1 ▶ if((pwd = getpwnam(logname)))
Branch: 1 branch
BranchTrueFalseTotal
if((pwd = getpwnam(logname))) 1 0 1
210 {
211 1 llent = getlluid(pwd->pw_uid);
212 1 ▶ if ( llent != (void *)0)
Branch: 1 branch
BranchTrueFalseTotal
if ( llent != (void *)0) 0 1 1
213 {
214 0 ll_ref = llent2hashref(pwd->pw_uid,llent);
215 0 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 0 0
216 0 PUSHs(sv_2mortal(ll_ref));
217 }
218 else
219 {
220 1 XSRETURN_EMPTY;
221 }
222 }
223 else
224 {
225 0 XSRETURN_EMPTY;
226 }
227  
228 void lastlog_path(self)
229 SV *self
230 PPCODE:
231 1 ▶ if(!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) 0 1 1
232 0 croak("Must be called as an object method");
233 1 ▶ EXTEND(SP,1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP,1); 0 1 1
234 1 PUSHs(sv_2mortal(newSVpv(lastlog_path(),0)));
235
236 void
237 setllent(self)
238 SV *self
239 PPCODE:
240  
241 0 ▶ if(!SvROK(self))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) 0 0 0
242 0 croak("Must be called as an object method");
243  
244 0 setllent();
« blib/lib/Sys/Lastlog.pm ↑ Summary