如何循环getprotent()函数
我相信getprotoent()
函数从/etc/protocols
获取值。
我做了以下示例程序:test.c.
表明
proto.p_name = ip
* proto.p_aliases = IP
proto.p_proto = 0
这些值与/etc/protocols
的第一个条目相同。
我想知道如何获取第二个条目的值,如循环,评估一些指针,不使用getprotobyname()
或getprotobynumber()
。
TEST.C
#include #include #include int main(int argc,char *argv[]) { protEnt(); } int protEnt() { setprotoent(2); struct protoent proto; proto = *getprotoent(); printf("proto.p_name = %s\n",proto.p_name); printf("*proto.p_aliases = %s\n",*proto.p_aliases); printf("proto.p_proto = %d\n",proto.p_proto); proto = *getprotoent(); endprotoent(); }
你可以getprotoent()
调用getprotoent()
并在每次调用时获得下一个条目。你只需要检查它是否为NULL
,这意味着没有任何条目。 这意味着您需要将proto
变量更改为指针。
所以这样的事情:
struct protoent *proto; while((proto = getprotoent()) != NULL) { printf("proto.p_name = %s\n",(*proto).p_name); printf("*proto.p_aliases = %s\n",*(*proto).p_aliases); printf("proto.p_proto = %d\n",(*proto).p_proto); }