error() function is glibc-specific, make own implementation

This commit is contained in:
Alexander Zubkov
2021-12-12 17:45:39 +01:00
parent 01f6592f49
commit 507f4e664d
4 changed files with 29 additions and 7 deletions

20
lib.c
View File

@@ -2,12 +2,26 @@
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <error.h>
#include <errno.h>
#include <stdarg.h>
#include "config.h"
#include "lib.h"
const char *program_invocation_name;
void error(int status, int errnum, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fflush(stdout);
fprintf(stderr, "%s: ", program_invocation_name);
vfprintf(stderr, fmt, ap);
if (errnum) fprintf(stderr, ": %s", strerror(errnum));
fprintf(stderr, "\n");
if(status) exit(status);
va_end(ap);
}
void bits2ip4(bits b, mask_t m, char *s, int size) {
// convert bits to in4_addr
struct in4_addr ip;
@@ -62,7 +76,7 @@ void ip42bits(char *s, bits b, mask_t *m) {
}
// convert ip to binary
struct in4_addr ip;
if (!inet_pton(AF_INET, s, &ip)) error(1, errno, "ip42bits");
if (inet_pton(AF_INET, s, &ip) < 1) error(1, errno, "ip42bits");
// convert binary ip to bits
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 8; ++j) {
@@ -87,7 +101,7 @@ void ip62bits(char *s, bits b, mask_t *m) {
}
// convert ip to binary
struct in6_addr ip;
if (!inet_pton(AF_INET6, s, &ip)) error(1, errno, "ip62bits");
if (inet_pton(AF_INET6, s, &ip) < 1) error(1, errno, "ip62bits");
// convert binary ip to bits
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 8; ++j) {