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