This sample calls NWgetprotobyname()/NWgetprotobynumber() for a given name/number and dumps the resulting structure to the console.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __MWERKS__
#pragma ANSI_strict off
#endif
#include <netdb.h>
#ifdef __MWERKS__
#pragma ANSI_strict reset
#endif
static void print_protoent( struct protoent const* protop) {
unsigned int i;
puts( protop -> p_name != NULL ? protop -> p_name : "<null>");
if ( protop -> p_aliases != NULL && *protop -> p_aliases != NULL) {
i = 0;
printf( "protop -> p_aliases =");
do {
printf( " %s", protop -> p_aliases[ i]);
++i;
} while ( protop -> p_aliases[ i] != NULL);
puts( "");
}
printf( "protop -> p_proto = %d\n", protop -> p_proto);
}
int main( int argc, char* argv[]) {
struct nwsockent sockent = {0};
struct protoent* protop;
if ( argc != 2) {
puts( "Usage: LOAD GETPROT4 <proto name>|<proto addr>");
return EXIT_FAILURE;
}
if (( protop = NWgetprotobyname( &sockent, argv[ 1])) == NULL) {
printf( "NWgetprotobyname failed, h_errno = %d\n", sockent.nse_h_errno);
if ( isdigit( *argv[ 1]) &&
( protop = NWgetprotobynumber( &sockent, atoi( argv[ 1]))) == NULL) {
printf( "NWgetprotobynumber failed, h_errno = %d\n", sockent.nse_h_errno);
return EXIT_FAILURE;
}
}
if ( protop != NULL) print_protoent( protop);
if (( protop = NWgetprotoent( &sockent)) == NULL) {
printf( "NWgetprotoent failed, h_errno = %d\n", sockent.nse_h_errno);
return EXIT_FAILURE;
}
print_protoent( protop);
return EXIT_SUCCESS;
}
© 2010 Novell