模拟lastlog程序

本文介绍了一个简单的C程序,该程序用于查询指定用户的最后登录记录,包括登录时间、登录终端及登录主机等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <lastlog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>

const char* LASTLOG_FILE = "/var/log/lastlog";

static void print_one(const char* uname, const struct lastlog* log)
{
printf("%s", uname);
if (!log->ll_line[0] && !log->ll_host[0]){
printf(" ** NEVER logged in **\n");
}else{
printf(" logged in from %s at %s on %s\n",
log->ll_line, log->ll_host, ctime(&log->ll_time));
}
}

static void usage(const char* prog)
{
printf("Usage:%s username\n", prog);
exit(0);
}

int main(int argc, char* argv[])
{
int user_id;
struct lastlog log;
struct stat statbuf;
FILE* fp;
struct passwd* passwd;
// calculate the offset
if (argc != 2)
usage(argv[0]);

passwd = getpwnam(argv[1]);
if (passwd == NULL){
printf("User [%s] not found\n", argv[1]);
return -1;
}

off_t offset = passwd->pw_uid *sizeof(log);

fp = fopen(LASTLOG_FILE, "rb");
if (fp == NULL) {perror("fopen"); return -1;}

if (fstat (fileno(fp), &statbuf)) {
perror("fstat");
return -1;
}

// check if the file is large enough
if (offset >= statbuf.st_size)
return -1;

// seek to the offset
fseek (fp, offset, SEEK_SET);

// read in the struct
if (fread ((char *) &log, sizeof(log), 1, fp) == 1)
print_one(argv[1], &log);
else
perror (LASTLOG_FILE);

return 0;
}


struct lastlog
  {
#if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
    int32_t ll_time;
#else
    __time_t ll_time;-- 时间
#endif
    char ll_line[UT_LINESIZE]; --哪个终端
    char ll_host[UT_HOSTSIZE]; --哪个主机
  };


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值