shared network names to be dynamically allocated
[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         unsigned int i;
89
90         /* Just in case there something in buffers */
91         if (fflush(NULL)) {
92                 warn("clean_up: fflush");
93         }
94         num_shared_networks++;
95         for (i = 0; i < num_shared_networks; i++) {
96                 free((shared_networks + i)->name);
97         }
98         free(config.dhcpdconf_file);
99         free(config.dhcpdlease_file);
100         free(config.output_file);
101         free(ranges);
102         free(leases);
103         free(touches);
104         free(shared_networks);
105 }
106
107 void print_version(void)
108 {
109         fprintf(stdout, "%s\n", PACKAGE_STRING);
110         fprintf(stdout,
111                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
112         fprintf(stdout,
113                 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
114         fprintf(stdout,
115                 "This is free software: you are free to change and redistribute it.\n");
116         fprintf(stdout,
117                 "There is NO WARRANTY, to the extent permitted by law.\n");
118         exit(EXIT_SUCCESS);
119 }
120
121 void usage(int status)
122 {
123         FILE *out;
124         out = status != 0 ? stderr : stdout;
125
126         fprintf(out, "\
127 Usage: %s [OPTIONS]\n", program_invocation_short_name);
128         fprintf(out, "\
129 This is ISC dhcpd pools usage analyzer.\n\
130 \n");
131         fprintf(out, "\
132   -c --config   file    path to the dhcpd.conf file\n\
133   -l --leases   file    path to the dhcpd.leases file\n\
134   -f --format   [thcxX]   output format\n");
135         fprintf(out, "\
136                           t for text\n\
137                           h for html table\n\
138                           H for full html page\n\
139                           x for xml\n\
140                           X for xml with active lease details\n\
141                           c for comma separated values\n");
142         fprintf(out, "\
143   -s --sort [nimcptTe]  sort ranges by\n\
144                           n name\n\
145                           i IP\n\
146                           m maxium\n\
147                           c current\n\
148                           p percent\n\
149                           t touched\n\
150                           T t+c\n\
151                           e t+c perc\n");
152         fprintf(out, "\
153   -r --reverse          reverse order sort\n\
154   -o --output   file    output into a file\n\
155   -L --limit    nr      output limit mask 77 - 00\n\
156   -v --version          version information\n\
157   -h --help             this screen\n\
158 \n\
159 Report bugs to <%s>\n\
160 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
161
162         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
163 }