- #include <unistd.h>
- #include <utmp.h>
- #include <fcntl.h>
- #include "string.h"
- #include "stdio.h"
- void show_info(struct utmp* utbutp);
- int main(int argc, char*argv[])
- {
- struct utmp stUtmp;
- int nReadLen = sizeof(struct utmp);
- int fileFd;
- if ((fileFd=open(UTMP_FILE, O_RDONLY)) == -1)
- perror(UTMP_FILE);
- while (read(fileFd, &stUtmp, nReadLen)==nReadLen)
- show_info(&stUtmp);
- return 0;
- }
- void show_info(struct utmp* utbutp)
- {
- if (utbutp->ut_type!=USER_PROCESS)
- return;
- printf("%-8.8s", utbutp->ut_user);
- printf(" ");
- printf("%-8.8s", utbutp->ut_line);
- printf(" ");
- printf("%.20s ", ctime(&(utbutp->ut_tv.tv_sec))+4);
- printf("%s\\n", utbutp->ut_host);
- }
- //该片段来自于http://www.codesnippet.cn/detail/250220148784.html
来源: http://www.codesnippet.cn/detail/250220148784.html