protoent.cpp
1.61 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 %t 2>&1 | FileCheck %s
// REQUIRES: !android
#include <assert.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
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) {
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() {
// CHECK: All protoent
// CHECK: ip (0)
// CHECK-NEXT: alias IP
// CHECK: ipv6 (41)
// CHECK-NEXT: alias IPv6
fprintf(stderr, "All protoent\n");
print_all_protoent();
// CHECK: Protoent by name
// CHECK-NEXT: ipv6 (41)
// CHECK-NEXT: alias IPv6
fprintf(stderr, "Protoent by name\n");
print_protoent_by_name("ipv6");
// CHECK: Protoent by num
// CHECK-NEXT: udp (17)
// CHECK-NEXT: alias UDP
fprintf(stderr, "Protoent by num\n");
print_protoent_by_num(17);
return 0;
}