1 // RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
2 
3 #include <assert.h>
4 #include <errno.h>
5 #include <netdb.h>
6 #include <stdio.h>
7 
8 void print_protoent(protoent *curr_entry) {
9   fprintf(stderr, "%s (%d)\n", curr_entry->p_name, curr_entry->p_proto);
10 
11   char **aliases = curr_entry->p_aliases;
12   while (char *alias = *aliases++) {
13     fprintf(stderr, "  alias %s\n", alias);
14   }
15 }
16 
17 void print_all_protoent() {
18   protoent entry;
19   char buf[1024];
20   protoent *curr_entry;
21 
22   while (getprotoent_r(&entry, buf, sizeof(buf), &curr_entry) != ENOENT && curr_entry) {
23     print_protoent(curr_entry);
24   }
25 }
26 
27 void print_protoent_by_name(const char *name) {
28   protoent entry;
29   char buf[1024];
30   protoent *curr_entry;
31 
32   int res = getprotobyname_r(name, &entry, buf, sizeof(buf), &curr_entry);
33   assert(!res && curr_entry);
34   print_protoent(curr_entry);
35 }
36 
37 void print_protoent_by_num(int num) {
38   protoent entry;
39   char buf[1024];
40   protoent *curr_entry;
41 
42   int res = getprotobynumber_r(num, &entry, buf, sizeof(buf), &curr_entry);
43   assert(!res && curr_entry);
44   print_protoent(curr_entry);
45 }
46 
47 int main() {
48   // CHECK: ip (0)
49   // CHECK-NEXT: alias IP
50   // CHECK: ipv6 (41)
51   // CHECK-NEXT: alias IPv6
52   print_all_protoent();
53 
54   // CHECK: rdp (27)
55   // CHECK-NEXT: alias RDP
56   print_protoent_by_name("rdp");
57 
58   // CHECK: udp (17)
59   // CHECK-NEXT: alias UDP
60   print_protoent_by_num(17);
61   return 0;
62 }
63