parse_leases: exit at read error
[debian/dhcpd-pools.git] / src / other.c
1 /*
2  * The dhcpd-pools has BSD 2-clause license which also known as "Simplified
3  * BSD License" or "FreeBSD License".
4  *
5  * Copyright 2006- Sami Kerola. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *    1. Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *
14  *    2. Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in the
16  *       documentation and/or other materials provided with the
17  *       distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * The views and conclusions contained in the software and documentation are
32  * those of the authors and should not be interpreted as representing
33  * official policies, either expressed or implied, of Sami Kerola.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "dhcpd-pools.h"
41
42 #include <stdio.h>
43 #ifdef  HAVE_STDLIB_H
44 #include <stdlib.h>
45 #else                           /* Not STDC_HEADERS */
46 extern void exit();
47 extern char *malloc();
48 #endif                          /* STDC_HEADERS */
49 #include <err.h>
50 #include <errno.h>
51 #include <stddef.h>
52 #ifdef  HAVE_STRING_H
53 #include <string.h>
54 #else
55 #include <strings.h>
56 #endif
57
58 /* Simple memory allocation wrapper */
59 void *safe_malloc(const size_t size)
60 {
61         void *ret = malloc(size);
62         if (ret == NULL) {
63                 err(EXIT_FAILURE,
64                     "safe_malloc: cannot allocate %lu bytes: ", size);
65         }
66
67         return ret;
68 }
69
70 /* Simple memory reallocation wrapper */
71 void *safe_realloc(void *ptr, const size_t size)
72 {
73         void *ret = realloc(ptr, size);
74
75         if (!ret && size)
76                 err(EXIT_FAILURE,
77                     "safe_realloc: cannot allocate %zu bytes", size);
78         return ret;
79 }
80
81 /* Simple strdup wrapper */
82 char *safe_strdup(const char *str)
83 {
84         char *ret = strdup(str);
85
86         if (!ret && str)
87                 err(EXIT_FAILURE, "cannot duplicate string");
88         return ret;
89 }
90
91 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
92 {
93         unsigned int i = num_ranges - 1, j;
94
95         for (j = 0; j < num_ranges; j++) {
96                 *(tmp_ranges + j) = *(ranges + i);
97                 i--;
98         }
99
100         memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
101 }
102
103 /* Free memory, flush buffers etc */
104 void clean_up(void)
105 {
106         unsigned int i;
107
108         /* Just in case there something in buffers */
109         if (fflush(NULL)) {
110                 warn("clean_up: fflush");
111         }
112         num_shared_networks++;
113         for (i = 0; i < num_shared_networks; i++) {
114                 free((shared_networks + i)->name);
115         }
116         free(config.dhcpdconf_file);
117         free(config.dhcpdlease_file);
118         free(config.output_file);
119         free(ranges);
120         free(leases);
121         free(backups);
122         free(touches);
123         free(shared_networks);
124 }
125
126 void print_version(void)
127 {
128         fprintf(stdout, "%s\n", PACKAGE_STRING);
129         fprintf(stdout,
130                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
131         fprintf(stdout,
132                 "The software has FreeBSD License.\n");
133         exit(EXIT_SUCCESS);
134 }
135
136 void usage(int status)
137 {
138         FILE *out;
139         out = status != 0 ? stderr : stdout;
140
141         fprintf(out, "\n\
142 Usage: %s [OPTIONS]\n\n", program_invocation_short_name);
143         fprintf(out, "\
144 This is ISC dhcpd pools usage analyzer.\n\
145 \n");
146         fprintf(out, "\
147   -c, --config=FILE      path to the dhcpd.conf file\n\
148   -l, --leases=FILE      path to the dhcpd.leases file\n\
149   -f, --format=[thHcxX]  output format\n");
150         fprintf(out, "\
151                            t for text\n\
152                            h for html table\n\
153                            H for full html page\n\
154                            x for xml\n\
155                            X for xml with active lease details\n\
156                            c for comma separated values\n");
157         fprintf(out, "\
158   -s, --sort=[nimcptTe]  sort ranges by\n\
159                            n name\n\
160                            i IP\n\
161                            m maxium\n\
162                            c current\n\
163                            p percent\n\
164                            t touched\n\
165                            T t+c\n\
166                            e t+c perc\n");
167         fprintf(out, "\
168   -r, --reverse          reverse order sort\n\
169   -o, --output=FILE      output into a file\n\
170   -L, --limit=NR         output limit mask 77 - 00\n\
171   -v, --version          version information\n\
172   -h, --help             this screen\n\
173 \n\
174 Report bugs to <%s>\n\
175 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
176
177         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
178 }