从linux中的proc文件解析一个地址数组

时间:2012-09-11 07:36:23

标签: c linux linux-kernel

我正在为proc条目写一个可变数量的地址,比如

echo num_addr addr1 addr2 addr3... addr_n > /proc/memory_address

(其中num_addr是地址数),例如

echo 2 0x1100 0x3300 > /proc/memory_address

如何解析函数内部对应echo / proc / memory_address?

1 个答案:

答案 0 :(得分:0)

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

int main()
{
        char *str= "5          0x333    0x232323 555 22323 2323";
        char *entptr;
        int nCount;
        int i;
        unsigned long ulArr[5];

        printf("\n String: %s", str);

        nCount = strtol(str, &entptr, 10);

        printf("\n NCount is %d", nCount);

        printf(" Endptr = %s", entptr);

        while(*entptr == ' ' || *entptr == '0' || *entptr == 'x' || *entptr == '\t') entptr++;

        i = 0;
        while(i<nCount)
        {
                str = entptr;
                ulArr[i] = strtol(str, &entptr, 10);
                printf("\n ulArr[%d] = %u", i, ulArr[i]);

                printf(" Endptr = %s", entptr);
                while(*entptr == ' ' || *entptr == '0' || *entptr == 'x' || *entptr == '\t') entptr++;
                printf(" Endptr = %s", entptr);
                //while(*entptr != ' ' && *entptr!='\0') entptr++;
                i++;
        }
        return 0;
}