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