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 #include <string>
11 
12 std::string any_name;
13 int total_count;
14 
print_protoent(protoent * curr_entry)15 void print_protoent(protoent *curr_entry) {
16   fprintf(stderr, "%s (%d)\n", curr_entry->p_name, curr_entry->p_proto);
17 
18   char **aliases = curr_entry->p_aliases;
19   while (char *alias = *aliases++) {
20     fprintf(stderr, "  alias %s\n", alias);
21   }
22 }
23 
print_all_protoent()24 void print_all_protoent() {
25   protoent entry;
26   char buf[1024];
27   protoent *curr_entry;
28 
29   while (getprotoent_r(&entry, buf, sizeof(buf), &curr_entry) != ENOENT && curr_entry) {
30     ++total_count;
31     any_name = curr_entry->p_name;
32     print_protoent(curr_entry);
33   }
34 }
35 
print_protoent_by_name(const char * name)36 void print_protoent_by_name(const char *name) {
37   protoent entry;
38   char buf[1024];
39   protoent *curr_entry;
40 
41   int res = getprotobyname_r(name, &entry, buf, sizeof(buf), &curr_entry);
42   assert(!res && curr_entry);
43   print_protoent(curr_entry);
44 }
45 
print_protoent_by_num(int num)46 void print_protoent_by_num(int num) {
47   protoent entry;
48   char buf[1024];
49   protoent *curr_entry;
50 
51   int res = getprotobynumber_r(num, &entry, buf, sizeof(buf), &curr_entry);
52   assert(!res && curr_entry);
53   print_protoent(curr_entry);
54 }
55 
main()56 int main() {
57   fprintf(stderr, "All protoent\n");
58   print_all_protoent();
59 
60   if (!total_count)
61     return 0;
62 
63   fprintf(stderr, "Protoent by name\n");
64   print_protoent_by_name(any_name.c_str());
65 
66   fprintf(stderr, "Protoent by num\n");
67   print_protoent_by_num(total_count / 2);
68   return 0;
69 }
70