Use what libc will provide
authorSami Kerola <kerolasa@iki.fi>
Sat, 22 Jan 2011 19:53:15 +0000 (20:53 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sat, 22 Jan 2011 19:53:15 +0000 (20:53 +0100)
The eprintf removed and replaced with err & warn. Option parsing
no longer tries to find missing optargs, which are getopts should
notice. Few complier warnings got to be removed as well. Finally
the commments will no longer exceed standard terminal width.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>

configure.ac
src/analyze.c
src/dhcpd-pools.c
src/dhcpd-pools.h
src/getdata.c
src/other.c
src/output.c
src/sort.c

index 088ef44..c9faedc 100644 (file)
@@ -8,7 +8,9 @@ AM_INIT_AUTOMAKE(dhcpd-pools, 2.13)
 AC_CONFIG_SRCDIR([config.h.in])
 AC_CONFIG_HEADERS([config.h])
 
-# Checks for programs.
+AC_GNU_SOURCE
+
+# Checks for programs
 AC_PROG_CC
 
 # Checks for libraries.
index c46d595..585b2a8 100644 (file)
@@ -129,7 +129,8 @@ int do_counting(void)
                        range_p->shared_net->available += block_size;
                }
 
-               /* Reverse so that not even a one IP will be missed. */
+               /* Go backwards one step so that not even a one IP will be
+                * missed. This is possibly always unnecessary. */
                if (i) {
                        i--;
                }
@@ -140,10 +141,9 @@ int do_counting(void)
                range_p++;
        }
 
-       /* During count of other shared networks default network and
-        * all networks got mixed to gether semantically. This fixes
-        * the problem, but is not elegant. TODO: fix semantics of all
-        * and default share_network. */
+       /* FIXME: During count of other shared networks default network and
+        * all networks got mixed to gether semantically. This fixes the
+        * problem, but is not elegant. */
        shared_networks->available = 0;
        shared_networks->used = 0;
        shared_networks->touched = 0;
index e3467cb..481472c 100644 (file)
@@ -27,7 +27,6 @@
 #ifdef  HAVE_STDLIB_H
 #include <stdlib.h>
 #else                          /* Not STDC_HEADERS */
-extern void exit();
 extern char *malloc();
 #endif                         /* STDC_HEADERS */
 #ifdef  HAVE_STRING_H
@@ -37,13 +36,14 @@ extern char *malloc();
 #endif
 #include <getopt.h>
 #include <errno.h>
+#include <err.h>
 
 #include "dhcpd-pools.h"
 #include "defaults.h"
 
 int main(int argc, char **argv)
 {
-       int c, sorts = 0;
+       int i, c, sorts = 0;
        int option_index = 0;
        char *tmp;
        struct range_t *tmp_ranges;
@@ -62,17 +62,13 @@ int main(int argc, char **argv)
                {0, 0, 0, 0}
        };
 
-       program_name = argv[0];
-       atexit(clean_up);
-
-       /* TODO: make either dynamic or find out max path lenght that auto config
-        * provides */
+       /* FIXME: make these allocations dynamic up on need. */
        config.dhcpdconf_file = safe_malloc(sizeof(char) * MAXLEN);
        config.dhcpdlease_file = safe_malloc(sizeof(char) * MAXLEN);
        config.output_file = safe_malloc(sizeof(char) * MAXLEN);
 
-       /* Make sure string has zero lenght if there is no command line
-        * option */
+       /* Make sure string has zero lenght if there is no
+        * command line option */
        config.output_file[0] = '\0';
 
        /* File location defaults */
@@ -105,55 +101,28 @@ int main(int argc, char **argv)
                        break;
                case 'c':
                        /* config file */
-                       if (optarg != NULL) {
-                               strncpy(config.dhcpdconf_file, optarg,
-                                       (size_t) MAXLEN - 1);
-                       } else {
-                               eprintf
-                                   ("main: for argument configuration file parameter not set");
-                               usage(EXIT_FAILURE);
-                       }
+                       strncpy(config.dhcpdconf_file, optarg,
+                               (size_t) MAXLEN - 1);
                        break;
                case 'l':
                        /* lease file */
-                       if (optarg != NULL) {
-                               strncpy(config.dhcpdlease_file, optarg,
-                                       (size_t) MAXLEN - 1);
-                       } else {
-                               eprintf
-                                   ("main: for argument lease file parameter not set");
-                               usage(EXIT_FAILURE);
-                       }
+                       strncpy(config.dhcpdlease_file, optarg,
+                               (size_t) MAXLEN - 1);
                        break;
                case 'f':
                        /* Output format */
-                       if (optarg != NULL) {
-                               strncpy(config.output_format, optarg,
-                                       (size_t) 1);
-                       } else {
-                               eprintf
-                                   ("main: for argument output format parameter not set");
-                               usage(EXIT_FAILURE);
-                       }
+                       strncpy(config.output_format, optarg, (size_t) 1);
                        break;
                case 's':
                        /* Output sorting option */
-                       if (optarg != NULL) {
-                               sorts = strlen(optarg);
-                               if (5 < sorts) {
-                                       eprintf
-                                           ("main: only 5 first sort orders will be used");
-                                       strncpy(config.sort, optarg,
-                                               (size_t) 5);
-                                       sorts = 5;
-                               } else {
-                                       strncpy(config.sort, optarg,
-                                               (size_t) sorts);
-                               }
+                       sorts = strlen(optarg);
+                       if (5 < sorts) {
+                               warn("main: only first 5 sort orders will be used");
+                               strncpy(config.sort, optarg, (size_t) 5);
+                               sorts = 5;
                        } else {
-                               eprintf
-                                   ("main: for argument sort order parameter not set");
-                               usage(EXIT_FAILURE);
+                               strncpy(config.sort, optarg,
+                                       (size_t) sorts);
                        }
                        break;
                case 'r':
@@ -162,51 +131,33 @@ int main(int argc, char **argv)
                        break;
                case 'o':
                        /* Output file */
-                       if (optarg != NULL) {
-                               strncpy(config.output_file, optarg,
-                                       (size_t) MAXLEN - 1);
-                       } else {
-                               eprintf
-                                   ("main: for argument output file parameter not set");
-                               usage(EXIT_FAILURE);
-                       }
+                       strncpy(config.output_file, optarg,
+                               (size_t) MAXLEN - 1);
                        break;
                case 'L':
                        /* Specification what will be printed */
-                       if (optarg != NULL) {
-                               if (optarg[0] >= '0' && optarg[0] < '8') {
-                                       config.output_limit[0] =
-                                           (int) optarg[0] - '0';
+                       for (i = 0; i < 2; i++) {
+                               if (optarg[i] >= '0' && optarg[i] < '8') {
+                                       config.output_limit[i] =
+                                           (int) optarg[i] - '0';
                                } else {
-                                       eprintf
-                                           ("main: output mask %s illegal",
-                                            argv[optind]);
-                                       usage(EXIT_FAILURE);
-                               }
-                               if (optarg[1] >= '0' && optarg[1] < '8') {
-                                       config.output_limit[1] =
-                                           (int) optarg[1] - '0';
-                               } else {
-                                       eprintf
-                                           ("main: output mask %s illegal",
+                                       errx(EXIT_FAILURE,
+                                            "main: output mask `%s' is illegal",
                                             optarg);
-                                       usage(EXIT_FAILURE);
                                }
-                       } else {
-                               eprintf
-                                   ("main: for argument output mask parameter not set");
-                               usage(EXIT_FAILURE);
                        }
                        break;
                case 'v':
                        /* Print version */
                        print_version();
-                       exit(EXIT_SUCCESS);
+                       return (EXIT_SUCCESS);
                case 'h':
                        /* Print help */
                        usage(EXIT_SUCCESS);
                default:
-                       usage(EXIT_FAILURE);
+                       errx(EXIT_FAILURE,
+                            "Try `%s --help' for more information.",
+                            program_invocation_short_name);
                }
        }
 
@@ -236,8 +187,8 @@ int main(int argc, char **argv)
                output_analysis = output_txt;
                break;
        default:
-               eprintf("main: unknown ouput format");
-               usage(EXIT_FAILURE);
+               errx(EXIT_FAILURE, "main: unknown ouput format `%c'",
+                    config.output_format[0]);
        }
 
        /* Do the job */
@@ -278,7 +229,8 @@ int main(int argc, char **argv)
                printf("</dhcpstatus>\n");
        };
 
-       exit(EXIT_SUCCESS);
+       clean_up();
+       return (EXIT_SUCCESS);
 }
 
 /* Global allocations, counter resets etc */
index a887bde..0d85615 100644 (file)
@@ -61,15 +61,14 @@ struct range_t
   unsigned long int backups;
 };
 /* Global variables */
-static int true = 1;
-static int false = 0;
+static int const true = 1;
+static int const false = 0;
 
-char *program_name;
 struct configuration_t config;
 
-static int output_limit_bit_1 = 1;
-static int output_limit_bit_2 = 2;
-static int output_limit_bit_3 = 4;
+static int const output_limit_bit_1 = 1;
+static int const output_limit_bit_2 = 2;
+static int const output_limit_bit_3 = 4;
 unsigned int fullhtml;
 
 struct shared_network_t *shared_networks;
@@ -97,8 +96,7 @@ int prepare_data (void);
 int do_counting (void);
 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges);
 /* General support functions */
-void *safe_malloc (size_t size);
-void eprintf (char *, ...);
+void *safe_malloc (const size_t size);
 void print_version (void);
 void usage (int status);
 /* qsort required functions... */
index d887f0c..b1e79b9 100644 (file)
@@ -27,7 +27,6 @@
 #ifdef  HAVE_STDLIB_H
 #include <stdlib.h>
 #else                          /* Not STDC_HEADERS */
-extern void exit();
 extern char *malloc();
 #define EXIT_FAILURE    1      /* Failing exit status.  */
 #define EXIT_SUCCESS    0      /* Successful exit status.  */
@@ -45,17 +44,20 @@ extern char *malloc();
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#ifndef _XOPEN_SOURCE
 #define _XOPEN_SOURCE 600
+#endif
 #include <fcntl.h>
 #include <errno.h>
 #include <err.h>
 #include <ctype.h>
+#include <assert.h>
 
 #include "dhcpd-pools.h"
 #include "defaults.h"
 
-/* Parse dhcpd.leases file. All performance boosts for this
- * function are wellcome */
+/* Parse dhcpd.leases file. All performance boosts for this function are
+ * wellcome */
 int parse_leases(void)
 {
        FILE *dhcpd_leases;
@@ -71,30 +73,29 @@ int parse_leases(void)
 
        dhcpd_leases = fopen(config.dhcpdlease_file, "r");
        if (dhcpd_leases == NULL) {
-               eprintf("parse_leases: %s:", config.dhcpdlease_file);
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_leases: %s",
+                   config.dhcpdlease_file);
        }
 #ifdef POSIX_FADV_NOREUSE
        posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_NOREUSE);
        if (errno) {
-               eprintf("parse_leases: fadvise:");
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_leases: fadvise noreuse");
        }
 #endif                         /* POSIX_FADV_NOREUSE */
 #ifdef POSIX_FADV_SEQUENTIAL
        posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL);
        if (errno) {
-               eprintf("parse_leases: fadvise:");
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_leases: fadvise sequential");
        }
 #endif                         /* POSIX_FADV_SEQUENTIAL */
+
        /* I found out that there's one lease address per 300 bytes in
-        * dhcpd.leases file. Malloc is little bit pessimistic and uses
-        * 250. If someone has higher density in lease file I'm
-        * interested to hear about that. */
+        * dhcpd.leases file. Malloc is little bit pessimistic and uses 250.
+        * If someone has higher density in lease file I'm interested to
+        * hear about that. */
        if (stat(config.dhcpdlease_file, &lease_file_stats)) {
-               eprintf("parse_leases: %s:", config.dhcpdlease_file);
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_leases: %s",
+                   config.dhcpdlease_file);
        }
        leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
        touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2;
@@ -121,16 +122,12 @@ int parse_leases(void)
                else if (strstr(line, "binding state active")) {
                        leases[num_leases] = htonl(inp.s_addr);
                        num_leases++;
-                       if (leasesmallocsize < num_leases) {
-                               errx(EXIT_FAILURE, "parse_leases: running out of lease memory, report a bug");
-                       }
+                       assert(!(leasesmallocsize < num_leases));
                        sw_active_lease = 1;
                } else if (strstr(line, "  binding state free")) {
                        touches[num_touches] = htonl(inp.s_addr);
                        num_touches++;
-                       if (touchesmallocsize < num_touches) {
-                               errx(EXIT_FAILURE, "parse_leases: running out of touch memory, report a bug");
-                       }
+                       assert(!(touchesmallocsize < num_touches));
                } else if (strstr(line, "  binding state backup")) {
                        if (num_backups == 0) {
                                backups =
@@ -139,19 +136,18 @@ int parse_leases(void)
                        }
                        backups[num_backups] = htonl(inp.s_addr);
                        num_backups++;
-                       if (backupsmallocsize < num_backups) {
-                               errx(EXIT_FAILURE, "parse_leases: running out of backup IPs memory, report a bug");
-                       }
+                       assert(!(backupsmallocsize < num_backups));
                }
 
-               /* FIXME: move to output.c and use FILE *outfile */
+               /* FIXME: move to output.c and use the FILE
+                * *outfile */
                if ((config.output_format[0] == 'X')
                    && (sw_active_lease == 1)
                    && (strstr(line, "hardware ethernet"))) {
                        nth_field(3, macstring, line);
                        macstring[strlen(macstring) - 1] = '\0';
 
-                        printf
+                       printf
                            ("<active_lease>\n\t<ip>%s</ip>\n\t<macaddress>%s</macaddress>\n</active_lease>\n",
                             ipstring, macstring);
                }
@@ -159,11 +155,10 @@ int parse_leases(void)
        return 0;
 }
 
-/* Like strcpy but for field which is separated by white spaces.
- * Number of first field is 1 and not 0 like C programs should
- * have. Question of semantics, send mail to author if this
- * annoys. All performance boosts for this function are well
- * come. */
+/* Like strcpy but for field which is separated by white spaces. Number of
+ * first field is 1 and not 0 like C programs should have. Question of
+ * semantics, send mail to author if this annoys. All performance boosts for
+ * this function are well come. */
 int nth_field(int n, char *dest, const char *src)
 {
        int i, j = 0, wordn = 0, len;
@@ -204,8 +199,7 @@ int is_interesting_config_clause(char *s)
        }
 }
 
-/* TODO: This spagetti monster function need to be rewrote at
- * least ones. */
+/* FIXME: This spagetti monster function need to be rewrote at least ones. */
 char *parse_config(int is_include, char *config_file,
                   char *current_shared_name,
                   char *next_free_shared_name,
@@ -232,23 +226,21 @@ char *parse_config(int is_include, char *config_file,
        /* Open configuration file */
        dhcpd_config = fopen(config_file, "r");
        if (dhcpd_config == NULL) {
-               eprintf("parse_config: %s:", config_file);
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_config: %s", config_file);
        }
 #ifdef POSIX_FADV_NOREUSE
        posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_NOREUSE);
        if (errno) {
-               eprintf("parse_config: fadvise:");
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_config: fadvise noreuse");
        }
 #endif                         /* POSIX_FADV_NOREUSE */
 #ifdef POSIX_FADV_SEQUENTIAL
        posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL);
        if (errno) {
-               eprintf("parse_config: fadvise:");
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE, "parse_config: fadvise sequential");
        }
 #endif                         /* POSIX_FADV_SEQUENTIAL */
+
        /* Very hairy stuff begins. */
        while (!feof(dhcpd_config)) {
                c = fgetc(dhcpd_config);
@@ -268,7 +260,8 @@ char *parse_config(int is_include, char *config_file,
                        }
                        continue;
                case '\n':
-                       /* New line resets comment section, but not if quoted */
+                       /* New line resets comment section, but
+                        * not if quoted */
                        if (quote == false) {
                                comment = false;
                        }
@@ -283,11 +276,11 @@ char *parse_config(int is_include, char *config_file,
                                newclause = true;
                                i = 0;
                        } else if (argument == 2) {
-                               /* Range ends to ; and this hair in code make two
-                                * ranges wrote to gether like...
+                               /* Range ends to ; and this hair in code
+                                * make two ranges wrote to gether like...
                                 *
                                 * range 10.20.30.40 10.20.30.41;range 10.20.30.42 10.20.30.43;
-                                * 
+                                *
                                 * ...to be interpreted correctly. */
                                c = ' ';
                        }
@@ -316,11 +309,13 @@ char *parse_config(int is_include, char *config_file,
                                if (braces_shared == braces) {
                                        current_shared_name =
                                            shared_net_names;
-                                       /* TODO: Using 1000 is lame, but works. */
+                                       /* FIXME: Using 1000 is lame, but
+                                        * works. */
                                        braces_shared = 1000;
                                        shared_p = shared_networks;
                                }
-                               /* Not literally true, but works for this program */
+                               /* Not literally true, but works for this
+                                * program */
                                newclause = true;
                        }
                        continue;
@@ -343,8 +338,9 @@ char *parse_config(int is_include, char *config_file,
                    && (!isspace(c) || quote == true)) {
                        word[i] = c;
                        i++;
-                       /* Long word which is almost causing overflow. Not any of words
-                        * this program is looking for are this long. */
+                       /* Long word which is almost causing overflow. None
+                        * of words are this long which the program is
+                        * searching. */
                        if (MAXLEN < i) {
                                newclause = false;
                                i = 0;
@@ -379,9 +375,8 @@ char *parse_config(int is_include, char *config_file,
                                range_p->shared_net = shared_p;
                                num_ranges++;
                                if (RANGES < num_ranges) {
-                                       eprintf
-                                           ("parse_config: Range space full! Increase RANGES and recompile.");
-                                       exit(EXIT_FAILURE);
+                                       errx(EXIT_FAILURE,
+                                            "parse_config: Range space full! Increase RANGES and recompile.");
                                }
                                newclause = true;
                                break;
@@ -389,7 +384,8 @@ char *parse_config(int is_include, char *config_file,
                                /* printf ("range 1nd ip: %s\n", word); */
                                range_p = ranges + num_ranges;
                                if (!(inet_aton(word, &inp))) {
-                                       /* word was not ip, try again */
+                                       /* word was not ip, try
+                                        * again */
                                        break;
                                }
                                range_p->first_ip = htonl(inp.s_addr) - 1;
@@ -407,17 +403,19 @@ char *parse_config(int is_include, char *config_file,
                                shared_p->used = 0;
                                shared_p->touched = 0;
                                shared_p->backups = 0;
-                               /* Temporary abuse of argument variable */
+                               /* Temporary abuse of argument
+                                * variable */
                                argument =
                                    strlen(next_free_shared_name) + 1;
-                               if (last_shared_name >
-                                   next_free_shared_name + argument) {
+                               if (next_free_shared_name + argument <
+                                   last_shared_name) {
                                        next_free_shared_name += argument;
                                } else {
-                                       /* TODO: make this go away by reallocationg more space. */
-                                       eprintf
-                                           ("parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
-                                       exit(EXIT_FAILURE);
+                                       /* FIXME: make this go
+                                        * away by reallocationg
+                                        * more space. */
+                                       errx(EXIT_FAILURE,
+                                            "parse_config: End of shared-network space, increase SHARED_NETWORKS_NAMES and recompile");
                                }
                                argument = 0;
                                braces_shared = braces;
@@ -437,9 +435,8 @@ char *parse_config(int is_include, char *config_file,
                                argument = 0;
                                break;
                        default:
-                               eprintf
-                                   ("parse_config: This cannot happen, report a bug!");
-                               exit(EXIT_FAILURE);
+                               warnx("impossible occurred, report a bug");
+                               assert(0);
                        }
                }
        }
index d3bb0f4..15bcdd2 100644 (file)
@@ -30,6 +30,7 @@ extern void exit();
 extern char *malloc();
 #endif                         /* STDC_HEADERS */
 #include <errno.h>
+#include <err.h>
 #include <stdarg.h>
 #ifdef  HAVE_STRING_H
 #include <string.h>
@@ -38,40 +39,16 @@ extern char *malloc();
 #endif
 
 /* Simple memory allocation wrapper */
-void *safe_malloc(size_t size)
+void *safe_malloc(const size_t size)
 {
        void *ret = malloc(size);
+
        if (ret == NULL) {
-               eprintf("safe_malloc: malloc: ");
-               exit(EXIT_FAILURE);
+               err(EXIT_FAILURE,
+                   "safe_malloc: cannot allocate %lu bytes: ", size);
        }
-       return ret;
-}
-
-/* Copyright (C) 1999 Lucent Technologies
- * Excerpted from 'The Practice of Programming'
- * by Brian W. Kernighan and Rob Pike
- * slight modifications by Sami Kerola.
- * eprintf: print error message and exit */
-void eprintf(char *fmt, ...)
-{
-       va_list args;
 
-       fflush(stdout);
-       fprintf(stderr, "%s: ", program_name);
-
-       va_start(args, fmt);
-       vfprintf(stderr, fmt, args);
-       va_end(args);
-
-       if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
-               fprintf(stderr, " %s", strerror(errno));
-       /* Should be safe, after all dhcpd-pools has only one
-        * thread. */
-       errno = 0;
-
-       fprintf(stderr, "\n");
-       fflush(stderr);
+       return ret;
 }
 
 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
@@ -86,24 +63,21 @@ void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
        memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
 }
 
-
 /* Free memory, flush buffers etc */
 void clean_up(void)
 {
        int ret;
        if (errno) {
-               eprintf
-                   ("clean_up: errno (%d) set but not checked in correct place; if this is repeatable send strace output as a bug report:",
-                    errno);
+               warn("clean_up: errno (%d) set but not checked in correct place.\nif this is repeatable send strace output as a bug report", errno);
        }
        /* Just in case there something in buffers */
        ret = fflush(stdout);
        if (errno || ret) {
-               eprintf("clean_up: stdout:");
+               warn("clean_up: stdout");
        }
        ret = fflush(stderr);
        if (errno || ret) {
-               eprintf("clean_up: stderr:");
+               warn("clean_up: stderr");
        }
        free(config.dhcpdconf_file);
        free(config.dhcpdlease_file);
@@ -132,7 +106,7 @@ void usage(int status)
        out = status != 0 ? stderr : stdout;
 
        fprintf(out, "\
-Usage: %s [OPTIONS]\n", program_name);
+Usage: %s [OPTIONS]\n", program_invocation_short_name);
        fprintf(out, "\
 This is ISC dhcpd pools usage analyzer.\n\
 \n");
@@ -147,9 +121,6 @@ This is ISC dhcpd pools usage analyzer.\n\
                           x for xml\n\
                           X for xml with active lease details\n\
                           c for comma separated values\n");
-/* TODO
-                        s for snmp\n");
- */
        fprintf(out, "\
   -s --sort [nimcptTe]  sort ranges by\n\
                           n name\n\
@@ -170,5 +141,5 @@ This is ISC dhcpd pools usage analyzer.\n\
 Report bugs to <%s>\n\
 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
 
-        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 }
index 613078d..f2858c6 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <err.h>
 
 #include "dhcpd-pools.h"
 
@@ -39,8 +40,8 @@ int output_txt(void)
        if (config.output_file[0]) {
                outfile = fopen(config.output_file, "w+");
                if (outfile == NULL) {
-                       eprintf("output_txt: %s:", config.output_file);
-                       exit(EXIT_FAILURE);
+                       err(EXIT_FAILURE, "output_txt: %s",
+                           config.output_file);
                }
        } else {
                outfile = stdout;
@@ -172,12 +173,12 @@ int output_txt(void)
        if (outfile == stdout) {
                ret = fflush(stdout);
                if (ret) {
-                       eprintf("output_txt: fflush:");
+                       warn("output_txt: fflush");
                }
        } else {
                ret = fclose(outfile);
                if (ret) {
-                       eprintf("output_txt: fclose:");
+                       warn("output_txt: fclose");
                }
        }
 
@@ -196,8 +197,8 @@ int output_xml(void)
        if (config.output_file[0]) {
                outfile = fopen(config.output_file, "w+");
                if (outfile == NULL) {
-                       eprintf("output_xml: %s:", config.output_file);
-                       exit(EXIT_FAILURE);
+                       err(EXIT_FAILURE, "output_xml: %s",
+                           config.output_file);
                }
        } else {
                outfile = stdout;
@@ -276,12 +277,12 @@ int output_xml(void)
        if (outfile == stdout) {
                ret = fflush(stdout);
                if (ret) {
-                       eprintf("output_xml: fflush:");
+                       warn("output_xml: fflush");
                }
        } else {
                ret = fclose(outfile);
                if (ret) {
-                       eprintf("output_xml: fclose:");
+                       warn("output_xml: fclose");
                }
        }
 
@@ -425,8 +426,8 @@ int output_html(void)
        if (config.output_file[0]) {
                outfile = fopen(config.output_file, "w+");
                if (outfile == NULL) {
-                       eprintf("output_html: %s:", config.output_file);
-                       exit(EXIT_FAILURE);
+                       err(EXIT_FAILURE, "output_html: %s",
+                           config.output_file);
                }
        } else {
                outfile = stdout;
@@ -607,12 +608,12 @@ int output_html(void)
        if (outfile == stdout) {
                ret = fflush(stdout);
                if (ret) {
-                       eprintf("output_html: fflush:");
+                       warn("output_html: fflush");
                }
        } else {
                ret = fclose(outfile);
                if (ret) {
-                       eprintf("output_html: fclose:");
+                       warn("output_html: fclose");
                }
        }
        return 0;
@@ -630,8 +631,8 @@ int output_csv(void)
        if (config.output_file[0]) {
                outfile = fopen(config.output_file, "w+");
                if (outfile == NULL) {
-                       eprintf("output_csv: %s:", config.output_file);
-                       exit(EXIT_FAILURE);
+                       err(EXIT_FAILURE, "output_csv: %s",
+                           config.output_file);
                }
        } else {
                outfile = stdout;
@@ -763,13 +764,13 @@ int output_csv(void)
        if (outfile == stdout) {
                ret = fflush(stdout);
                if (ret) {
-                       eprintf("output_cvs: fflush:");
+                       warn("output_cvs: fflush");
                }
 
        } else {
                ret = fclose(outfile);
                if (ret) {
-                       eprintf("output_cvs: fclose:");
+                       warn("output_cvs: fclose");
                }
 
        }
index ff6db69..40ecad4 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <err.h>
 
 #include "dhcpd-pools.h"
 #include "defaults.h"
@@ -116,9 +117,9 @@ void field_selector(char c)
                returner = ret_tcperc;
                break;
        default:
-               eprintf("field_selector: unknown sort order: %c",
-                       config.sort[0]);
-               usage(EXIT_FAILURE);
+               errx(EXIT_FAILURE,
+                    "field_selector: unknown sort order `%c'",
+                    config.sort[0]);
        }
 }