Objective-c - 用户的登录和注销时间 [英] Objective-c - User's login and logout time

查看:118
本文介绍了Objective-c - 用户的登录和注销时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在目标c程序中获取用户的登录和注销时间?我可以获得会话ID,用户名,userUID,userIsActive和loginCompleted与CGSessionCopyCurrentDictionary函数,但我不能从它的登录和注销时间,我可以吗?

is it possible to get user's login and logout time in a objective-c program? I can get the session ID, username, userUID, userIsActive and loginCompleted with CGSessionCopyCurrentDictionary function but I can't get login and logout time from it, can I?

我知道我可以从console.app获取信息,但我想把它放在一个程序中。

I know I can get the info from console.app, but I would like to put it in a program.

我在哪里寻找更多信息?在Apple的开发指南中找不到。

Where do I look for more info on that? Can't find it in Development guide from Apple.

谢谢!

推荐答案

我不知道是否有任何特殊的Cocoa函数来获取用户登录/注销时间。

I don't know if there are any special Cocoa function to get user login/logout time.

但是你可以直接读取登录/ getutxent_wtmp()。这是last命令行工具所做的,可以在源代码中看到: http://www.opensource.apple.com/source/adv_cmds/adv_cmds-149/last/last.c

But you can read the login/logout history directly, using getutxent_wtmp(). This is what the "last" command line tool does, as can be seen in the source code: http://www.opensource.apple.com/source/adv_cmds/adv_cmds-149/last/last.c

只是给一个非常简单的例子:以下程序打印所有登录/注销时间到标准输出:

Just to give a very simple example: The following program prints all login/logout times to the standard output:

#include <stdio.h>
#include <utmpx.h>

int main(int argc, const char * argv[])
{
    struct utmpx *bp;
    char *ct;

    setutxent_wtmp(0); // 0 = reverse chronological order
    while ((bp = getutxent_wtmp()) != NULL) {
        switch (bp->ut_type) {
            case USER_PROCESS:
                ct = ctime(&bp->ut_tv.tv_sec);
                printf("%s login %s", bp->ut_user, ct);
                break;
            case DEAD_PROCESS:
                ct = ctime(&bp->ut_tv.tv_sec);
                printf("%s logout %s", bp->ut_user, ct);
                break;

            default:
                break;
        }
    };
    endutxent_wtmp();

    return 0;
}

这篇关于Objective-c - 用户的登录和注销时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆