Import from release candidate 2.13.
[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 <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(size_t size)
42 {
43         void *ret = malloc(size);
44         if (ret == NULL) {
45                 eprintf("safe_malloc: malloc: ");
46                 exit(EXIT_FAILURE);
47         }
48         return ret;
49 }
50
51 /* Copyright (C) 1999 Lucent Technologies
52  * Excerpted from 'The Practice of Programming'
53  * by Brian W. Kernighan and Rob Pike
54  * slight modifications by Sami Kerola.
55  * eprintf: print error message and exit */
56 void eprintf(char *fmt, ...)
57 {
58         va_list args;
59
60         fflush(stdout);
61         fprintf(stderr, "%s: ", program_name);
62
63         va_start(args, fmt);
64         vfprintf(stderr, fmt, args);
65         va_end(args);
66
67         if (fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
68                 fprintf(stderr, " %s", strerror(errno));
69         /* Should be safe, after all dhcpd-pools has only one
70          * thread. */
71         errno = 0;
72
73         fprintf(stderr, "\n");
74         fflush(stderr);
75 }
76
77 void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges)
78 {
79         unsigned int i = num_ranges - 1, j;
80
81         for (j = 0; j < num_ranges; j++) {
82                 *(tmp_ranges + j) = *(ranges + i);
83                 i--;
84         }
85
86         memcpy(ranges, tmp_ranges, num_ranges * sizeof(struct range_t));
87 }
88
89
90 /* Free memory, flush buffers etc */
91 void clean_up(void)
92 {
93         int ret;
94         if (errno) {
95                 eprintf
96                     ("clean_up: errno (%d) set but not checked in correct place; if this is repeatable send strace output as a bug report:",
97                      errno);
98         }
99         /* Just in case there something in buffers */
100         ret = fflush(stdout);
101         if (errno || ret) {
102                 eprintf("clean_up: stdout:");
103         }
104         ret = fflush(stderr);
105         if (errno || ret) {
106                 eprintf("clean_up: stderr:");
107         }
108         free(config.dhcpdconf_file);
109         free(config.dhcpdlease_file);
110         free(config.output_file);
111         free(ranges);
112         free(shared_net_names);
113         free(shared_networks);
114 }
115
116 void print_version(void)
117 {
118         fprintf(stdout, "%s\n", PACKAGE_STRING);
119         fprintf(stdout,
120                 "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n");
121         fprintf(stdout,
122                 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n");
123         fprintf(stdout,
124                 "This is free software: you are free to change and redistribute it.\n");
125         fprintf(stdout,
126                 "There is NO WARRANTY, to the extent permitted by law.\n");
127 }
128
129 void usage(int status)
130 {
131         FILE *out;
132         out = status != 0 ? stderr : stdout;
133
134         fprintf(out, "\
135 Usage: %s [OPTIONS]\n", program_name);
136         fprintf(out, "\
137 This is ISC dhcpd pools usage analyzer.\n\
138 \n");
139         fprintf(out, "\
140   -c --config   file    path to the dhcpd.conf file\n\
141   -l --leases   file    path to the dhcpd.leases file\n\
142   -f --format   [thcxX]   output format\n");
143         fprintf(out, "\
144                           t for text\n\
145                           h for html table\n\
146                           H for full html page\n\
147                           x for xml\n\
148                           X for xml with active lease details\n\
149                           c for comma separated values\n");
150 /* TODO
151                         s for snmp\n");
152  */
153         fprintf(out, "\
154   -s --sort [nimcptTe]  sort ranges by\n\
155                           n name\n\
156                           i IP\n\
157                           m maxium\n\
158                           c current\n\
159                           p percent\n\
160                           t touched\n\
161                           T t+c\n\
162                           e t+c perc\n");
163         fprintf(out, "\
164   -r --reverse          reverse order sort\n\
165   -o --output   file    output into a file\n\
166   -L --limit    nr      output limit mask 77 - 00\n\
167   -v --version          version information\n\
168   -h --help             this screen\n\
169 \n\
170 Report bugs to <%s>\n\
171 Homepage: %s\n", PACKAGE_BUGREPORT, PACKAGE_URL);
172
173         exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
174 }