[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;
}