-
Notifications
You must be signed in to change notification settings - Fork 0
/
utmplib.c
56 lines (47 loc) · 955 Bytes
/
utmplib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<utmp.h>
#define NRECS 16
#define NULLUT ((struct utmp *)NULL)
#define UTSIZE (sizeof(struct utmp))
static char utmpbuf[NRECS*UTSIZE];
static int num_recs;
static int cur_rec;
static int fd_utmp = -1;
int utmp_open(char *filename)
{
/* 打开文件
*/
fd_utmp = open(filename,O_RDONLY);
cur_rec = num_recs = 0;
return fd_utmp;
}
struct utmp * utmp_next()
{
struct utmp *recp;
if(fd_utmp == -1)
return NULLUT;
if(cur_rec == num_recs && utmp_reload() ==0)
return NULLUT;
//一次读取一个结构体
recp = (struct utmp *)&utmpbuf[cur_rec * UTSIZE];
//当前记录的位置
cur_rec++;
//结构体内容
return recp;
}
int utmp_reload()
{
int amt_read;
//一次性读取16倍的utmp结构体
amt_read = read(fd_utmp,utmpbuf,NRECS * UTSIZE);
num_recs = amt_read/UTSIZE;
cur_rec = 0;
return num_recs;
}
void utmp_close()
{
if(fd_utmp != -1)
close(fd_utmp);
}