how to show proccess like in ps -e

大憨熊 提交于 2019-12-01 14:52:38

This is an hint ... Try to develop your code starting from this! :)

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

int readData(char *dirname);

int readData(char *dirname)
{
    FILE * file;
    char buffer[1024]={0};

    sprintf(buffer,"/proc/%s/stat",dirname);

    file = fopen(buffer,"r");
    if (!file)
        return errno;

    while(fgets(buffer,sizeof(buffer),file))
        puts(buffer);

    if (file)
        fclose(file);

    return errno;
}

int main(void)
{
    DIR * dir;

    struct dirent * entry;

    if ( (dir = opendir("/proc")) == NULL )
        perror("operation error");

    while ((entry = readdir(dir))) {
        if ( strlen(entry->d_name) == strspn(entry->d_name, "0123456789"))
            if (readData(entry->d_name))
                break;
    }

    if (dir)
        closedir(dir);

    return errno;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!