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