#include <stdio.h> #include <string.h> #include <errno.h> #include <assert.h> #include <netdb.h> #define __DEBUG__ void printf_protoent(struct protoent* pe) { #ifdef __DEBUG__ assert (pe != NULL && pe->p_name); char** p_aliases = NULL; /* if (pe->p_name) { printf("p_name is null\n"); } else { printf("p_name not null\n"); } */ printf("p_name: %s, p_proto: %d\n", pe->p_name, pe->p_proto); p_aliases = pe->p_aliases; if (p_aliases) { if (*p_aliases) { printf("p_aliases: %s\n", *p_aliases); } while (*p_aliases) { printf("p_alias: %s\t", *p_aliases); p_aliases++; } printf("\n"); } #else /* */ #endif } int is_not_valid_protoent(struct protoent* pe) { #ifdef __DEBUG__ char *p = NULL; if (! pe) { return 1; } p = pe->p_name; if (! p) { return 1; } return strncmp("(null)", p, 6) != 0 ? 0 : 1; #else return pe && pe->p_name && strncmp("(null)", pe->p_name, 6) != 0 ? 0 : 1; #endif } int main(int argc, char **argv) { int stayopen = !0; struct protoent* pe = NULL; setprotoent(stayopen); while (pe = getprotoent()) { if (errno) { printf("error %d\n", errno); break; } if (is_not_valid_protoent(pe)) { printf("not valid\n"); break; } printf_protoent(pe); } endprotoent(); printf("\n\n\n"); setprotoent(stayopen); while (pe = getprotoent()) { if (errno) { printf("error %d\n", errno); break; } // test closes the connection to the database, and then read struct protoent fields endprotoent(); if (is_not_valid_protoent(pe)) { printf("not valid\n"); break; } printf_protoent(pe); } endprotoent(); printf("\n\n\n"); // test the connection to the database will be closed between calls to one of the getproto*() functions stayopen = 0; setprotoent(stayopen); while (pe = getprotoent()) { if (errno) { printf("error %d\n", errno); break; } if (is_not_valid_protoent(pe)) { printf("not valid\n"); break; } printf_protoent(pe); } endprotoent(); }