clean_up: checking errno at exit does not make sense
[debian/dhcpd-pools.git] / src / other.c
1 /* http://dhcpd-pools.sourceforge.net/
2 ** Copyright 2006- Sami Kerola <kerolasa@iki.fi>
3 **
4 ** This program is free software: you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation, either version 3 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include "dhcpd-pools.h"
23
24 #include <stdio.h>
25 #ifdef  HAVE_STDLIB_H
26 #include <stdlib.h>
27 #else                           /* Not STDC_HEADERS */
28 extern void exit();
29 extern char *malloc();
30 #endif                          /* STDC_HEADERS */
31 #include <errno.h>
32 #include <err.h>
33 #include <stdarg.h>
34 #ifdef  HAVE_STRING_H
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39
40 /* Simple memory allocation wrapper */
41 void *safe_malloc(const size_t size)
42 {
43         void *ret = malloc(size);
44
45         if (ret == NULL) {
46                 err(EXIT_FAILURE,
47                     "safe_malloc: cannot allocate %lu bytes: ", size);
48         }
49
50         return ret;
51 }
52
53 /* Simple strdup wrapper */
54 char *safe_strdup(const char *str)
55 {
56         char *ret = strdup(str);
57
58         if (!ret && str)
59                 err(EXIT_FAILURE, "cannot duplicate string");
60         return ret;
61 }
62
63 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
64 {
65         unsigned int i = num_ranges - 1, j;
66
67         for (j = 0; j < num_ranges; j++) {
68                 *(tmp_ranges + j) = *(ranges + i);
69                 i--;
70         }
71
72         memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
73 }
74
75 /* Free memory, flush buffers etc */
76 void clean_up(void)
77 {
78         int ret;
79
80         /* Just in case there something in buffers */
81         ret = fflush(stdout);
82         if (errno || ret) {
83                 warn("clean_up: stdout");
84         }
85         ret = fflush(stderr);
86         if (errno || ret) {
87                 warn("clean_up: stderr");
88         }
89
90         free(config.dhcpdconf_file);
91         free(config.dhcpdlease_file);
92         free(config.output_file);
93         free(ranges);
94         free(leases);
95         free(touches);
96         free(shared_net_names);
97         free(shared_networks);
98 }
99
100 void print_version(void)
101 {
102         fprintf(stdout, "%s\n", PACKAGE_STRING);
103         fprintf(stdout,
104                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
105         fprintf(stdout,
106                 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
107         fprintf(stdout,
108                 "This is free software: you are free to change and redistribute it.\n");
109         fprintf(stdout,
110                 "There is NO WARRANTY, to the extent permitted by law.\n");
111         exit(EXIT_SUCCESS);
112 }
113
114 void usage(int status)
115 {
116         FILE *out;
117         out = status != 0 ? stderr : stdout;
118
119         fprintf(out, "\
120 Usage: %s [OPTIONS]\n", program_invocation_short_name);
121         fprintf(out, "\
122 This is ISC dhcpd pools usage analyzer.\n\
123 \n");
124         fprintf(out, "\
125   -c --config   file    path to the dhcpd.conf file\n\
126   -l --leases   file    path to the dhcpd.leases file\n\
127   -f --format   [thcxX]   output format\n");
128         fprintf(out, "\
129                           t for text\n\
130                           h for html table\n\
131                           H for full html page\n\
132                           x for xml\n\
133                           X for xml with active lease details\n\
134                           c for comma separated values\n");
135         fprintf(out, "\
136   -s --sort [nimcptTe]  sort ranges by\n\
137                           n name\n\
138                           i IP\n\
139                           m maxium\n\
140                           c current\n\
141                           p percent\n\
142                           t touched\n\
143                           T t+c\n\
144                           e t+c perc\n");
145         fprintf(out, "\
146   -r --reverse          reverse order sort\n\
147   -o --output   file    output into a file\n\
148   -L --limit    nr      output limit mask 77 - 00\n\
149   -v --version          version information\n\
150   -h --help             this screen\n\
151 \n\
152 Report bugs to <%s>\n\
153 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
154
155         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
156 }