Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得用户';s最后一次登录,包括C中的年份_C_Unix_Login - Fatal编程技术网

如何获得用户';s最后一次登录,包括C中的年份

如何获得用户';s最后一次登录,包括C中的年份,c,unix,login,C,Unix,Login,我正试图写一个程序,可以得到用户的最后登录时间。然后我想对数据做点什么。例如,检查哪些用户已经90天没有登录 所以我试着使用last命令,效果很好。只是它没有显示年份。如果我在2014年1月进行检查,我怎么知道最后一次登录是在2013年12月还是2012年12月 这是代码的一个示例: FILE *file1; extern FILE *popen(); char command[1024]; char *username; char result[1024]; char buff[1024];

我正试图写一个程序,可以得到用户的最后登录时间。然后我想对数据做点什么。例如,检查哪些用户已经90天没有登录

所以我试着使用
last
命令,效果很好。只是它没有显示年份。如果我在2014年1月进行检查,我怎么知道最后一次登录是在2013年12月还是2012年12月

这是代码的一个示例:

FILE *file1;
extern FILE *popen();
char command[1024];
char *username;
char result[1024];
char buff[1024];

username = "fikrie";
sprintf(command,"last %s",username);
file1 = popen(command, "r");
while(fgets(result, sizeof(result), file1)!=NULL){
   sprintf(buff,"%s",result);
}
pclose(file1);
在谷歌上搜索了一下之后,我发现有一面a-F国旗可以显示年份。不幸的是,它在HP-UX和Solaris上不起作用。这些平台无法识别该标志

我还从这个链接测试了使用基于utmp的代码。问题是这段代码在Solaris和HP-UX上会失败。utmp结构中似乎不存在ut_tv

#include <stdio.h>
#include <utmp.h>
#include <string.h>
#include <time.h>

int main(void) {
    struct utmp *line;
    time_t timestamp;
    utmpname("/var/log/wtmp");
    setutent();
    while( (line = getutent()) != NULL) {
            if (line->ut_type == USER_PROCESS ) {
                    timestamp = line->ut_tv.tv_sec;
                    printf("%s %s", line->ut_user, asctime(localtime(&timestamp)));
            }
    }
    endutent();
    return 0;
}
#包括
#包括
#包括
#包括
内部主(空){
结构utmp*行;
时间戳;
utmpname(“/var/log/wtmp”);
setutent();
而((line=getutent())!=NULL){
if(行->ut\U类型==用户\U进程){
时间戳=行->ut_tv.tv_秒;
printf(“%s%s”,行->ut_用户,asctime(localtime(×tamp));
}
}
endutent();
返回0;
}
那么,有没有一种方法可以编写一个C代码,通常可以获得所有Unix平台的最后一次登录。(Solaris、Redhat/Linux/CentOS、AIX、HP-UX、Ubuntu)

似乎当我设法在一个平台上获得它时,其他平台上就失败了

在OS X上,它是
getutxent\u wtmp()
,请参见示例。也许这也适用于其他平台。getutxent_wtmp()似乎不适用于Solaris 8。我将尝试将其更改为使用getutxent()。似乎在utmpx结构中有一个ut_tv。