zoukankan      html  css  js  c++  java
  • who命令

    who1.c

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

    #define SHOWHOST /*include remote machine on output*/

    show_info(struct utmp *utbufp)
    {
    printf("% -8.8s", utbufp->ut_name);//the logname
    printf(" ");//a space
    printf("% -8.8s", utbufp->ut_line);//the tty
    printf(" ");//a space
    printf("% -10ld", utbufp->ut_time);//login time
    printf(" ");//a space
    #ifdef SHOWHOST
    printf("(%s)", utbufp->ut_host);//the host
    #endif
    printf(" ");//newline
    }
    int main()
    {
    struct utmp current_record; /*read info into here*/
    int utmpfd; /*read from this descriptor*/
    int reclen = sizeof(current_record);

    if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
    {
    perror(UTMP_FILE);/*UTMP_FILE is utmp.h*/
    exit(1);
    }

    while(read(utmpfd, &current_record, reclen) == reclen)
    show_info(&current_record);
    close(utmpfd);
    return 0;/*went ok*/

    }

    who2.c

    #include <stdio.h>
    #include <utmp.h>
    #include <fcntl.h>
    #include <unistd.h>
    //who2 added
    #include <time.h>

    #define SHOWHOST /*include remote machine on output*/

    void show_info(struct utmp *utbufp);
    //who2 added
    void showtime(long);

    int main()
    {
    struct utmp current_record; /*read info into here*/
    int utmpfd; /*read from this descriptor*/
    int reclen = sizeof(current_record);

    if ((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1)
    {
    perror(UTMP_FILE);/*UTMP_FILE is utmp.h*/
    exit(1);
    }

    while(read(utmpfd, &current_record, reclen) == reclen)
    show_info(&current_record);
    close(utmpfd);
    return 0;/*went ok*/

    }

    show_info(struct utmp *utbufp)
    {
    //who2 added
    if (utbufp->ut_type != USER_PROCESS)
    {
    return;
    }
    //who2 end
    printf("% -8.8s", utbufp->ut_name);//the logname
    printf(" ");//a space
    printf("% -8.8s", utbufp->ut_line);//the tty
    printf(" ");//a space
    //printf("% -10ld", utbufp->ut_time);//login time
    //printf(" ");//a space
    showtime(utbufp->ut_time);
    #ifdef SHOWHOST
    printf("(%s)", utbufp->ut_host);//the host
    #endif
    printf(" ");//newline
    }

    //who2 added
    void showtime(long timeval)
    {
    char *cp;//to hold address of time
    cp = ctime(&timeval);
    printf("%12.12s", cp+4);//pick 12 chars from pos 4
    }
    //who2 end

  • 相关阅读:
    Spring AOP前置通知实例说明AOP相关概念
    什么是面向切面编程AOP
    关于IOC容器的一些个人理解
    在.Net Core WebAPI下给Swagger增加导出离线文档功能
    .Net Core ORM选择之路,哪个才适合你
    真香.小程序云开发(时光邮局小程序)
    Cordova的安装与配置
    JS三座大山再学习(三、异步和单线程)
    JS三座大山再学习(二、作用域和闭包)
    JS三座大山再学习(一、原型和原型链)
  • 原文地址:https://www.cnblogs.com/wolflion/p/3208527.html
Copyright © 2011-2022 走看看