-
Notifications
You must be signed in to change notification settings - Fork 0
/
who1.c
executable file
·53 lines (48 loc) · 1008 Bytes
/
who1.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
#include<stdio.h>
#include<utmp.h>
#include<fcntl.h>
#include<unistd.h>
#include<time.h>
#include<stdlib.h>
#define SHOWHOST
void show_info(struct utmp *,int);
void showtime(long);
int main(int argc,char *argv[])
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if((utmpfd = open(UTMP_FILE,O_RDONLY)) == -1){
perror(UTMP_FILE);
exit(1);
}
int flag = atoi(argv[1]);
// printf("%d\n",flag);
while(read(utmpfd,¤t_record,reclen) == reclen)
show_info(¤t_record,flag);
close(utmpfd);
return 0;
}
void show_info(struct utmp *utbufp,int flag)
{
if(utbufp->ut_type != flag)
return;
// printf("%-8.8s",utbufp->ut_name);
// printf(" ");
// printf("%-8.8s",utbufp->ut_line);
// printf(" ");
showtime(utbufp->ut_time);
// printf("%10ld",utbufp->ut_time);
printf(" ");
#ifdef SHOWHOST
printf("%s",utbufp->ut_host);
#endif
printf("\n");
}
void showtime(long timeval)
{
char *cp;
cp = ctime(&timeval);
printf("%s",cp);
//printf("% 12.12s",cp+4);
}