|
4 | 4 |
|
5 | 5 | #include <arpa/inet.h> |
6 | 6 | #include <assert.h> |
| 7 | +#include <errno.h> |
7 | 8 | #include <netdb.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +void CheckResult(int ret) { |
| 14 | + if (ret != 0) { |
| 15 | + fprintf(stderr, "%s\n", strerror(errno)); |
| 16 | + abort(); |
| 17 | + } |
| 18 | +} |
8 | 19 |
|
9 | 20 | int main(void) { |
10 | 21 | struct servent result_buf; |
11 | 22 | struct servent *result; |
12 | 23 | char buf[1024]; |
13 | | - assert(getservent_r(&result_buf, buf, sizeof(buf), &result) == 0); |
| 24 | + CheckResult(getservent_r(&result_buf, buf, sizeof(buf), &result)); |
14 | 25 | assert(result != nullptr); |
15 | 26 |
|
16 | 27 | // If these fail, check /etc/services if "ssh" exists. I picked this because |
17 | 28 | // it should exist everywhere, if it doesn't, I am sorry. Disable the test |
18 | 29 | // then please. |
19 | | - assert(getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), |
20 | | - &result) == 0); |
| 30 | + CheckResult( |
| 31 | + getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), &result)); |
21 | 32 | assert(result != nullptr); |
22 | | - assert(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), |
23 | | - &result) == 0); |
| 33 | + CheckResult(getservbyport_r(htons(22), nullptr, &result_buf, buf, sizeof(buf), |
| 34 | + &result)); |
24 | 35 | assert(result != nullptr); |
25 | 36 |
|
26 | | - assert(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, |
27 | | - sizeof(buf), &result) == 0); |
| 37 | + CheckResult(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf, |
| 38 | + sizeof(buf), &result)); |
28 | 39 | assert(result == nullptr); |
29 | 40 | } |
0 commit comments