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