make range allocation dynamic
[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         if (ret == NULL) {
45                 err(EXIT_FAILURE,
46                     "safe_malloc: cannot allocate %lu bytes: ", size);
47         }
48
49         return ret;
50 }
51
52 /* Simple memory reallocation wrapper */
53 void *safe_realloc(void *ptr, const size_t size)
54 {
55         void *ret = realloc(ptr, size);
56
57         if (!ret && size)
58                 err(EXIT_FAILURE,
59                     "safe_realloc: cannot allocate %zu bytes", size);
60         return ret;
61 }
62
63 /* Simple strdup wrapper */
64 char *safe_strdup(const char *str)
65 {
66         char *ret = strdup(str);
67
68         if (!ret && str)
69                 err(EXIT_FAILURE, "cannot duplicate string");
70         return ret;
71 }
72
73 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
74 {
75         unsigned int i = num_ranges - 1, j;
76
77         for (j = 0; j < num_ranges; j++) {
78                 *(tmp_ranges + j) = *(ranges + i);
79                 i--;
80         }
81
82         memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
83 }
84
85 /* Free memory, flush buffers etc */
86 void clean_up(void)
87 {
88         int ret;
89
90         /* Just in case there something in buffers */
91         ret = fflush(stdout);
92         if (errno || ret) {
93                 warn("clean_up: stdout");
94         }
95         ret = fflush(stderr);
96         if (errno || ret) {
97                 warn("clean_up: stderr");
98         }
99
100         free(config.dhcpdconf_file);
101         free(config.dhcpdlease_file);
102         free(config.output_file);
103         free(ranges);
104         free(leases);
105         free(touches);
106         free(shared_net_names);
107         free(shared_networks);
108 }
109
110 void print_version(void)
111 {
112         fprintf(stdout, "%s\n", PACKAGE_STRING);
113         fprintf(stdout,
114                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
115         fprintf(stdout,
116                 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
117         fprintf(stdout,
118                 "This is free software: you are free to change and redistribute it.\n");
119         fprintf(stdout,
120                 "There is NO WARRANTY, to the extent permitted by law.\n");
121         exit(EXIT_SUCCESS);
122 }
123
124 void usage(int status)
125 {
126         FILE *out;
127         out = status != 0 ? stderr : stdout;
128
129         fprintf(out, "\
130 Usage: %s [OPTIONS]\n", program_invocation_short_name);
131         fprintf(out, "\
132 This is ISC dhcpd pools usage analyzer.\n\
133 \n");
134         fprintf(out, "\
135   -c --config   file    path to the dhcpd.conf file\n\
136   -l --leases   file    path to the dhcpd.leases file\n\
137   -f --format   [thcxX]   output format\n");
138         fprintf(out, "\
139                           t for text\n\
140                           h for html table\n\
141                           H for full html page\n\
142                           x for xml\n\
143                           X for xml with active lease details\n\
144                           c for comma separated values\n");
145         fprintf(out, "\
146   -s --sort [nimcptTe]  sort ranges by\n\
147                           n name\n\
148                           i IP\n\
149                           m maxium\n\
150                           c current\n\
151                           p percent\n\
152                           t touched\n\
153                           T t+c\n\
154                           e t+c perc\n");
155         fprintf(out, "\
156   -r --reverse          reverse order sort\n\
157   -o --output   file    output into a file\n\
158   -L --limit    nr      output limit mask 77 - 00\n\
159   -v --version          version information\n\
160   -h --help             this screen\n\
161 \n\
162 Report bugs to <%s>\n\
163 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
164
165         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
166 }