手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux C编程的小例子——实现who命令(第二版)

[日期:2014-01-01] 来源:Linux社区  作者:hjd_love_zzt [字体:大 中 小]

Linux C编程的小例子——实现who命令(第二版)

/*
 * who1.c
 *
 *  Created on: Dec 30, 2013
 *      Author: Fedora
 */

#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>


#define SHOWHOST

void showtime(long timeval){
 char* cp;
 cp = ctime(&timeval);

 printf("%12.12s",cp+4);
}

void show_info(struct utmp* utbufp){
 if(utbufp->ut_type != USER_PROCESS){
  return ;
 }

 printf("%-8.8s",utbufp->ut_name);
 printf(" ");
 printf("%-8.8s",utbufp->ut_line);
    printf(" ");
    showtime(utbufp->ut_time);
    printf(" ");

#ifdef SHOWHOST
    if(utbufp->ut_host[0] != '\0'){
     printf("( %s )",utbufp->ut_host);
    }
#endif

    printf("\n");
}
int main(){
 struct utmp current_record;
 int utmpfd;
 int reclen = sizeof(current_record);

 if((utmpfd = open(UTMP_FILE,O_RDONLY)) == -1){
  perror(UTMP_FILE);

  return 1;
 }

 while( read(utmpfd,&current_record,reclen) == reclen ){
  show_info(&current_record);
 }

 close(utmpfd);

 return 0;
}

相关阅读:

Linux C编程的一个小例子——实现一个简单的who命令(第一版)  http://www.linuxidc.com/Linux/2014-01/94577.htm

linux
相关资讯       Linux C编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款