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