/proc/net/dev
,该文件是内核维护,所有可用的网口均会同步到该文件中。peng@ubuntu:~$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 26163 292 0 0 0 0 0 0 26163 292 0 0 0 0 0 0
eth0: 285444708 243273 0 0 0 0 0 0 91828270 88660 0 0 0 0 0 0
所以,我们要列举出所有可用的网口名称,可以通过查看改文件来实现。
该函数用于列举所有可用的网口。
/proc/net/dev
中国读取的每一行字符串信息,提取出网口名信息,如lo、eth0函数strrchr()
#define IP_SIZE 128
#define PROCBUFSIZ 1024
#define _PATH_PROC_NET_DEV "/proc/net/dev"
static char * interface_name_cut (char *buf, char **name)
{
char *stat;
/* Skip white space. Line will include header spaces. */
while (*buf == ' ')
buf++;
*name = buf;
/* Cut interface name. */
stat = strrchr (buf, ':');
*stat++ = '\0';
return stat;
}
int list_interface_valid()
{
FILE *fp;
char buf[PROCBUFSIZ];
struct interface *ifp;
char *name;
char *p;
/* Open /proc/net/dev. */
fp = fopen (_PATH_PROC_NET_DEV, "r");
if (fp == NULL)
{
printf("open proc file error\n");
return -1;
}
/* Drop header lines. */
fgets (buf, PROCBUFSIZ, fp);
fgets (buf, PROCBUFSIZ, fp);
/* Only allocate interface structure. Other jobs will be done in
if_ioctl.c. */
while (fgets (buf, PROCBUFSIZ, fp) != NULL)
{
p = interface_name_cut (buf, &name);
printf("port=%s\n",name);
}
fclose(fp);
return 0;
}
在原作者的公众号“一口Linux”后台回复:eth
END
来源:一口Linux
版权归原作者所有,如有侵权,请联系删除。
▍推荐阅读
脏话越多,代码越好!
我把ST-Link当做J-link用
单片机哪些方面不能替代PLC?