posix_fadvice advice parameter change
[debian/dhcpd-pools.git] / src / other.c
1 /*
2 ** Copyright (C) 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 2 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, write to the Free Software 
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "dhcpd-pools.h"
24
25 #include <stdio.h>
26 #ifdef  HAVE_STDLIB_H
27 #include <stdlib.h>
28 #else                           /* Not STDC_HEADERS */
29 extern void exit();
30 extern char *malloc();
31 #endif                          /* STDC_HEADERS */
32 #include <errno.h>
33 #include <err.h>
34 #include <stdarg.h>
35 #ifdef  HAVE_STRING_H
36 #include <string.h>
37 #else
38 #include <strings.h>
39 #endif
40
41 /* Simple memory allocation wrapper */
42 void *safe_malloc(const size_t size)
43 {
44         void *ret = malloc(size);
45
46         if (ret == NULL) {
47                 err(EXIT_FAILURE,
48                     "safe_malloc: cannot allocate %lu bytes: ", size);
49         }
50
51         return ret;
52 }
53
54 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
55 {
56         unsigned int i = num_ranges - 1, j;
57
58         for (j = 0; j < num_ranges; j++) {
59                 *(tmp_ranges + j) = *(ranges + i);
60                 i--;
61         }
62
63         memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
64 }
65
66 /* Free memory, flush buffers etc */
67 void clean_up(void)
68 {
69         int ret;
70         if (errno) {
71                 warn("clean_up: errno (%d) set but not checked in correct place.\nif this is repeatable send strace output as a bug report", errno);
72         }
73         /* Just in case there something in buffers */
74         ret = fflush(stdout);
75         if (errno || ret) {
76                 warn("clean_up: stdout");
77         }
78         ret = fflush(stderr);
79         if (errno || ret) {
80                 warn("clean_up: stderr");
81         }
82         free(config.dhcpdconf_file);
83         free(config.dhcpdlease_file);
84         free(config.output_file);
85         free(ranges);
86         free(shared_net_names);
87         free(shared_networks);
88 }
89
90 void print_version(void)
91 {
92         fprintf(stdout, "%s\n", PACKAGE_STRING);
93         fprintf(stdout,
94                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
95         fprintf(stdout,
96                 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
97         fprintf(stdout,
98                 "This is free software: you are free to change and redistribute it.\n");
99         fprintf(stdout,
100                 "There is NO WARRANTY, to the extent permitted by law.\n");
101 }
102
103 void usage(int status)
104 {
105         FILE *out;
106         out = status != 0 ? stderr : stdout;
107
108         fprintf(out, "\
109 Usage: %s [OPTIONS]\n", program_invocation_short_name);
110         fprintf(out, "\
111 This is ISC dhcpd pools usage analyzer.\n\
112 \n");
113         fprintf(out, "\
114   -c --config   file    path to the dhcpd.conf file\n\
115   -l --leases   file    path to the dhcpd.leases file\n\
116   -f --format   [thcxX]   output format\n");
117         fprintf(out, "\
118                           t for text\n\
119                           h for html table\n\
120                           H for full html page\n\
121                           x for xml\n\
122                           X for xml with active lease details\n\
123                           c for comma separated values\n");
124         fprintf(out, "\
125   -s --sort [nimcptTe]  sort ranges by\n\
126                           n name\n\
127                           i IP\n\
128                           m maxium\n\
129                           c current\n\
130                           p percent\n\
131                           t touched\n\
132                           T t+c\n\
133                           e t+c perc\n");
134         fprintf(out, "\
135   -r --reverse          reverse order sort\n\
136   -o --output   file    output into a file\n\
137   -L --limit    nr      output limit mask 77 - 00\n\
138   -v --version          version information\n\
139   -h --help             this screen\n\
140 \n\
141 Report bugs to <%s>\n\
142 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
143
144         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
145 }