getutxline() 和 getutxline() 原型分别如下:
[C++] 纯文本查看 复制代码 #include <utmp.h>
struct utmp *getutline(struct utmp *ut);
#include <utmpx.h>
struct utmpx *getutxline(const struct utmpx *);
getutline() 和 getutxline() 是同功能函数,但在新编写的应用程序中应该使用后者。这两个函数是从 utmp 文件的当前读写位置逐条往后查找 ut_type 为 USER_PROCESS 或 LOGIN_PROCESS 的记录,并且 ut_line 要和 ut->line 相符。找到相符的记录后比返回该条记录的指针。关于 utmp 结构可参考:http://www.groad.net/bbs/read.php?tid-5805.html
测试代码:
[C++] 纯文本查看 复制代码 #include <stdio.h>
#include <time.h>
#include <string.h>
#include <utmp.h>
#include <utmpx.h>
void print_record (const struct utmpx *ut, const char *type)
{
struct utmpx *utp;
while ((utp = getutxline(ut)) != NULL) {
printf("%-8s ", utp->ut_user);
printf("%-9.9s ", type);
printf ("%-5d %-6.6s %-9.9s %-20.20s ", (long)utp->ut_pid, utp->ut_line, utp->ut_id, utp->ut_host);
printf ("%s", ctime((time_t *) & (utp->ut_tv.tv_sec)));
}
}
int main()
{
struct utmpx ut;
printf ("User Type PID Line Id Host Date/time\n");
setutxent();
ut.ut_type = USER_PROCESS;
strncpy(ut.ut_line, "pts/1", strlen("pts/1"));
print_record (&ut, "USER_PROCESS");
endutxent();
return 0;
}
运行输出:./getutxline
User Type PID Line Id Host Date/time
beyes USER_PROC 4513 pts/1 /1 192.168.1.100 Sun Jan 1 12:01:44 2012 |