portability: Solaris 10 does not have err.h

Use error(3) function, that has gnulib portability fixes, instead of err(3)
and warn(3) family.

Reported-by: Anton Tkachev <antont@bk.ru>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2015-10-26 22:12:37 +00:00
parent fae20302cf
commit 535dfc4fc2
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
10 changed files with 84 additions and 72 deletions

1
THANKS
View file

@ -35,3 +35,4 @@ Wolfgang Steudel
Aaron Paetznick
Tim Cantin
Martijn van Brummelen
Anton Tkachev

View file

@ -20,6 +20,7 @@
gnulib_modules="
close-stream
closeout
error
fclose
fcntl-h
fdopen

View file

@ -1,3 +1,4 @@
/ar-lib
/compile
/config.guess
/config.sub

1
lib/.gitignore vendored
View file

@ -7,6 +7,7 @@
/Makefile.am
/Makefile.in
/_Exit.c
/alloca.h
/alloca.in.h
/arg-nonnull.h
/arpa/

View file

@ -38,10 +38,9 @@
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
@ -49,11 +48,13 @@
#include "close-stream.h"
#include "closeout.h"
#include "defaults.h"
#include "dhcpd-pools.h"
#include "error.h"
#include "progname.h"
#include "xalloc.h"
#include "dhcpd-pools.h"
#include "defaults.h"
/* Global variables */
int prefix_length[2][NUM_OF_PREFIX];
struct configuration_t config;
@ -165,7 +166,7 @@ int main(int argc, char **argv)
/* Output sorting option */
sorts = strlen(optarg);
if (5 < sorts) {
warnx("main: only first 5 sort orders will be used");
error(0, 0, "main: only first 5 sort orders will be used");
strncpy(config.sort, optarg, (size_t)5);
sorts = 5;
} else
@ -188,7 +189,7 @@ int main(int argc, char **argv)
config.output_limit[i] = optarg[i] - '0';
else {
clean_up();
errx(EXIT_FAILURE,
error(EXIT_FAILURE, 0,
"main: output mask `%s' is illegal", optarg);
}
}
@ -211,7 +212,8 @@ int main(int argc, char **argv)
/* Print help */
usage(EXIT_SUCCESS);
default:
errx(EXIT_FAILURE, "Try `%s --help' for more information.", program_name);
error(EXIT_FAILURE, 0, "Try `%s --help' for more information.",
program_name);
}
}
/* Output function selection */
@ -246,7 +248,7 @@ int main(int argc, char **argv)
break;
default:
clean_up();
errx(EXIT_FAILURE, "main: unknown output format `%c'", config.output_format[0]);
error(EXIT_FAILURE, 0, "main: unknown output format `%c'", config.output_format[0]);
}
/* Do the job */
prepare_memory();

View file

@ -42,7 +42,6 @@
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
@ -53,10 +52,12 @@
#include <stdlib.h>
#include <sys/stat.h>
#include "defaults.h"
#include "dhcpd-pools.h"
#include "error.h"
#include "xalloc.h"
#include "dhcpd-pools.h"
#include "defaults.h"
/*! \brief Lease file parser. The parser can only read ISC DHCPD
* dhcpd.leases file format. */
int parse_leases(void)
@ -70,15 +71,15 @@ int parse_leases(void)
dhcpd_leases = fopen(config.dhcpdlease_file, "r");
if (dhcpd_leases == NULL)
err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file);
error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file);
#ifdef HAVE_POSIX_FADVISE
# ifdef POSIX_FADV_NOREUSE
if (posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_NOREUSE) != 0)
err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file);
error(EXIT_FAILURE, errno, "parse_leases: fadvise %s", config.dhcpdlease_file);
# endif /* POSIX_FADV_NOREUSE */
# ifdef POSIX_FADV_SEQUENTIAL
if (posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL) != 0)
err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file);
error(EXIT_FAILURE, errno, "parse_leases: fadvise %s", config.dhcpdlease_file);
# endif /* POSIX_FADV_SEQUENTIAL */
#endif /* HAVE_POSIX_FADVISE */
/* I found out that there's one lease address per 300 bytes in
@ -86,7 +87,7 @@ int parse_leases(void)
* If someone has higher density in lease file I'm interested to
* hear about that. */
if (stat(config.dhcpdlease_file, &lease_file_stats))
err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file);
error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file);
line = xmalloc(sizeof(char) * MAXLEN);
line[0] = '\0';
ipstring = xmalloc(sizeof(char) * MAXLEN);
@ -95,7 +96,7 @@ int parse_leases(void)
ethernets = true;
while (!feof(dhcpd_leases)) {
if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases))
err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file);
error(EXIT_FAILURE, errno, "parse_leases: %s", config.dhcpdlease_file);
switch (xstrstr(line)) {
/* It's a lease, save IP */
case PREFIX_LEASE:
@ -186,15 +187,15 @@ void parse_config(int is_include, const char *restrict config_file,
/* Open configuration file */
dhcpd_config = fopen(config_file, "r");
if (dhcpd_config == NULL)
err(EXIT_FAILURE, "parse_config: %s", config_file);
error(EXIT_FAILURE, errno, "parse_config: %s", config_file);
#ifdef HAVE_POSIX_FADVISE
# ifdef POSIX_FADV_NOREUSE
if (posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_NOREUSE) != 0)
err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
error(EXIT_FAILURE, errno, "parse_config: fadvise %s", config_file);
# endif /* POSIX_FADV_NOREUSE */
# ifdef POSIX_FADV_SEQUENTIAL
if (posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_SEQUENTIAL) != 0)
err(EXIT_FAILURE, "parse_config: fadvise %s", config_file);
error(EXIT_FAILURE, errno, "parse_config: fadvise %s", config_file);
# endif /* POSIX_FADV_SEQUENTIAL */
#endif /* HAVE_POSIX_FADVISE */
/* Very hairy stuff begins. */
@ -364,7 +365,7 @@ void parse_config(int is_include, const char *restrict config_file,
shared_p->backups = 0;
if (SHARED_NETWORKS < num_shared_networks + 2)
/* FIXME: make this to go away by reallocating more space. */
errx(EXIT_FAILURE,
error(EXIT_FAILURE, 0,
"parse_config: increase default.h SHARED_NETWORKS and recompile");
argument = ITS_NOTHING_INTERESTING;
braces_shared = braces;
@ -380,7 +381,7 @@ void parse_config(int is_include, const char *restrict config_file,
argument = ITS_NOTHING_INTERESTING;
break;
default:
warnx("impossible occurred, report a bug");
error(0, 0, "impossible occurred, report a bug");
assert(0);
}
}

View file

@ -38,9 +38,12 @@
* analysis happen as quick as possible..
*/
#include "dhcpd-pools.h"
#include <config.h>
#include "xalloc.h"
#include "dhcpd-pools.h"
#define HASH_FIND_V6(head, findv6, out) HASH_FIND(hh, head, findv6, 16, out)
#define HASH_ADD_V6(head, v6field, add) HASH_ADD(hh, head, v6field, 16, add)

View file

@ -39,13 +39,9 @@
#include <config.h>
#include "dhcpd-pools.h"
#include "defaults.h"
#include "progname.h"
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stddef.h>
@ -53,7 +49,12 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "error.h"
#include "progname.h"
#include "dhcpd-pools.h"
#include "defaults.h"
/*! \brief Set function pointers depending on IP version.
* \param ip IP version.
@ -378,9 +379,7 @@ double strtod_or_err(const char *restrict str, const char *restrict errmesg)
goto err;
return num;
err:
if (errno)
err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
}
/*! \brief Reverse range.
@ -404,7 +403,7 @@ void clean_up(void)
{
/* Just in case there something in buffers */
if (fflush(NULL))
warn("clean_up: fflush");
error(0, 0, "clean_up: fflush");
free(config.dhcpdconf_file);
free(config.dhcpdlease_file);
free(config.output_file);

View file

@ -40,7 +40,6 @@
#include <config.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <langinfo.h>
@ -53,10 +52,12 @@
#include <time.h>
#include "close-stream.h"
#include "dhcpd-pools.h"
#include "error.h"
#include "progname.h"
#include "strftime.h"
#include "dhcpd-pools.h"
/*! \brief Text output format, which is the default.
* FIXME: This function should return void. */
int output_txt(void)
@ -72,7 +73,7 @@ int output_txt(void)
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_txt: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_txt: %s", config.output_file);
}
} else {
outfile = stdout;
@ -201,12 +202,12 @@ int output_txt(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_txt: fflush");
error(0, 0, "output_txt: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_txt: fclose");
error(0, 0, "output_txt: fclose");
}
}
@ -228,7 +229,7 @@ int output_xml(void)
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_xml: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_xml: %s", config.output_file);
}
} else {
outfile = stdout;
@ -242,6 +243,7 @@ int output_xml(void)
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
struct leases_t *l;
for (l = leases; l != NULL; l = l->hh.next) {
if (l->type == ACTIVE) {
fputs("<active_lease>\n\t<ip>", outfile);
@ -305,12 +307,12 @@ int output_xml(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_xml: fflush");
error(0, 0, "output_xml: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_xml: fclose");
error(0, 0, "output_xml: fclose");
}
}
@ -333,7 +335,7 @@ int output_json(void)
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_json: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_json: %s", config.output_file);
}
} else {
outfile = stdout;
@ -348,6 +350,7 @@ int output_json(void)
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
struct leases_t *l;
fprintf(outfile, " \"active_leases\": [");
for (l = leases; l != NULL; l = l->hh.next) {
if (l->type == ACTIVE) {
@ -443,12 +446,12 @@ int output_json(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_json: fflush");
error(0, 0, "output_json: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_json: fclose");
error(0, 0, "output_json: fclose");
}
}
@ -465,14 +468,15 @@ static void html_header(FILE *restrict f)
struct tm *tmp, result;
struct stat statbuf;
stat(config.dhcpdlease_file, &statbuf);
tmp = localtime_r(&statbuf.st_mtime, &result);
if (tmp == NULL) {
err(EXIT_FAILURE, "html_header: localtime");
error(EXIT_FAILURE, errno, "html_header: localtime");
}
if (strftime(outstr, sizeof(outstr), nl_langinfo(D_T_FMT), &result) == 0) {
errx(EXIT_FAILURE, "html_header: strftime returned 0");
error(EXIT_FAILURE, 0, "html_header: strftime returned 0");
}
fprintf(f, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n");
@ -652,10 +656,11 @@ int output_html(void)
struct shared_network_t *shared_p;
int ret;
FILE *outfile;
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_html: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_html: %s", config.output_file);
}
} else {
outfile = stdout;
@ -754,9 +759,8 @@ int output_html(void)
output_double(outfile, "td", shared_p->backups);
output_float(outfile, "td",
shared_p->available == 0 ? -NAN : (float)(100 *
shared_p->
backups) /
shared_p->available);
shared_p->backups)
/ shared_p->available);
}
endrow(outfile);
@ -792,18 +796,16 @@ int output_html(void)
output_double(outfile, "td", shared_networks->touched + shared_networks->used);
output_float(outfile, "td",
shared_networks->available == 0 ? -NAN : (float)(100 *
(shared_networks->
touched +
shared_networks->
used)) /
shared_networks->available);
(shared_networks->touched
+
shared_networks->used))
/ shared_networks->available);
if (config.backups_found == true) {
output_double(outfile, "td", shared_networks->backups);
output_float(outfile, "td",
shared_networks->available == 0 ? -NAN : (float)(100 *
shared_networks->
backups) /
shared_networks->available);
shared_networks->backups)
/ shared_networks->available);
}
endrow(outfile);
}
@ -814,12 +816,12 @@ int output_html(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_html: fflush");
error(0, 0, "output_html: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_html: fclose");
error(0, 0, "output_html: fclose");
}
}
return 0;
@ -836,10 +838,11 @@ int output_csv(void)
struct shared_network_t *shared_p;
FILE *outfile;
int ret;
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_csv: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_csv: %s", config.output_file);
}
} else {
outfile = stdout;
@ -957,12 +960,12 @@ int output_csv(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_cvs: fflush");
error(0, 0, "output_cvs: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_cvs: fclose");
error(0, 0, "output_cvs: fclose");
}
}
return 0;
@ -989,7 +992,7 @@ int output_alarming(void)
if (config.output_file[0]) {
outfile = fopen(config.output_file, "w+");
if (outfile == NULL) {
err(EXIT_FAILURE, "output_alarming: %s", config.output_file);
error(EXIT_FAILURE, errno, "output_alarming: %s", config.output_file);
}
} else {
outfile = stdout;
@ -1077,12 +1080,12 @@ int output_alarming(void)
if (outfile == stdout) {
ret = fflush(stdout);
if (ret) {
warn("output_alarming: fflush");
error(0, 0, "output_alarming: fflush");
}
} else {
ret = close_stream(outfile);
if (ret) {
warn("output_alarming: fclose");
error(0, 0, "output_alarming: fclose");
}
}
return ret_val;

View file

@ -40,15 +40,15 @@
#include <config.h>
#include <err.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "dhcpd-pools.h"
#include "error.h"
#include "progname.h"
#include "dhcpd-pools.h"
/*! \brief Compare IP address, with IPv4/v6 determination.
* \param a Binary IP address.
* \param b Binary IP address.
@ -237,8 +237,7 @@ comparer_t field_selector(char c)
return comp_tcperc;
default:
clean_up();
warnx("field_selector: unknown sort order `%c'", c);
errx(EXIT_FAILURE, "Try `%s --help' for more information.", program_name);
error(EXIT_FAILURE, 0, "field_selector: unknown sort order `%c'", c);
}
return NULL;
}
@ -289,6 +288,7 @@ void mergesort_ranges(struct range_t *restrict orig, int size, struct range_t *r
{
int left, right, i;
struct range_t hold;
/* Merge sort split size */
static const int MIN_MERGE_SIZE = 8;