c语言实现linux中的终端名称的打印

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

void show_info(struct utmp *utbufp);
int main()
{
    struct utmp current_record;
    int utmp_fd;
    int reclen = sizeof(current_record);    
    utmp_fd = open(UTMP_FILE,O_RDONLY);    
    if(utmp_fd == -1)
    return -1;

    while( read(utmp_fd, &current_record,reclen) == reclen ){
     show_info(&current_record);
}    
    close(utmp_fd);
    return 0;
}

void show_info(struct utmp *utbufp)
{
  if(utbufp->ut_name!=NULL)
  {
    printf("%-8.8s",utbufp->ut_name);
    //printf(" ");
    printf("%-8.8s",utbufp->ut_line);
    //printf(" ");
    printf("%101d",utbufp->ut_time);
    //printf(" ");
    printf("(%s)",utbufp->ut_host);
    //printf(" ");
    printf("
");
  }
    
}

  这个代码中存在bug,输出的时候不知道为什么打印的终端的名字会有多的。

但是自己明明就开了两个终端,但是实际打印了四个终端。先记下。后续的研究正在进行中~ing

原文地址:https://www.cnblogs.com/littleswan/p/11995558.html