其实只是对/proc/net/dev设备的读取并解析,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int countcards(void)
{
FILE *file;
char buffer[512];
char *start;
int niccount = 0;
if (!(file = fopen("/proc/net/dev", "r"))) {
printf("Unable to open /proc/net/dev in countcards()\n");
return 0;
}
while (fgets(buffer, 512, file)) {
start = buffer;
while (*start == ' ')
start++;
if (strncmp(start, "eth", strlen("eth")) == 0)
niccount++;
}
fclose(file);
return niccount;
}
int main(int argc, char **argv)
{
int count = 0;
count = countcards();
printf("Net Cards Count:%d\n",count);
return 0;
}
运行结果:
Net Cards Count:2
--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/
没有评论:
发表评论