簡體   English   中英

分段錯誤(核心轉儲)C linux

[英]Segmentation fault (core dumped) C linux

因此,我必須制作一個程序,其執行結果與在Linux中使用who和who is命令后的執行結果相似。 問題是如果分開,if(strcmp ...和else ...中的兩個函數都可以工作。

主要問題是我必須將它們都放在一個文件中,而這只是行不通。 當僅在一起做某事時,。/程序就是我,而./程序則告訴分段錯誤(核心已轉儲)。

第二個問題是我不知道如何制作零件。/程序我是否可以正常工作,它應該只讓我返回:用戶pts / 0日期時間...並非所有用戶都喜歡其他...部分。

#include    <stdio.h>
#include    <utmp.h>
#include    <fcntl.h>
#include    <unistd.h>
#include    <stdlib.h>
#include    <time.h>
#include    <string.h>
#include    <sys/types.h>

#define SHOWHOST

void show_info_who( struct utmp *utbufp )
{
    if(utbufp->ut_type > 4){
        time_t czas = utbufp->ut_time;
        char buf[80];
        struct tm* timeinfo = localtime(&czas);

        printf("%-8.8s", utbufp->ut_name);  
        printf(" ");                
        printf("%-8.8s", utbufp->ut_line);  
        printf(" ");                
        strftime(buf, 80, "%F %R" , timeinfo);
        printf("%s", buf);
        printf(" ");                


    #ifdef  SHOWHOST
        printf("(%s)", utbufp->ut_host);    
    #endif
        printf("\n");               
    }
}


int main(int argc, char *argv[])
{
    struct utmp current_record;
    int     utmpfd;     
    int     reclen = sizeof(current_record);

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

    if(strcmp ( argv[1], "am") == 0){
        if(strcmp ( argv[2], "i") == 0){
            while ( read(utmpfd, &current_record, reclen) == reclen )
                show_info_who(&current_record);
                printf("test\n");
        }

    close(utmpfd);
    return 0;
    }

        while ( read(utmpfd, &current_record, reclen) == reclen )
            show_info_who(&current_record);
        close(utmpfd);

        return 0;


}

你為什么不嘗試使用

void setutent(void); // for rewinding
struct utmp *getutent(void);

不建議直接打開並查找utmp文件。 utmp文件似乎是可以自由訪問的普通文件,但其行為類似於數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM