From 76915512b244823706664a185339376432f913dc Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Fri, 8 Jul 2011 20:49:49 +0200 Subject: [PATCH] maint: coding style fixes Remove unnecessary type casting, move .gitignore file contents to right location, reindent to use Linux coding style, fix few type mismatches, clean up to help & version output and hint compiler call to these functions will end program. Signed-off-by: Sami Kerola --- .gitignore | 5 +- src/.gitignore | 6 ++ src/analyze.c | 16 ++--- src/defaults.h | 2 +- src/dhcpd-pools.c | 44 ++++++------ src/dhcpd-pools.h | 14 +--- src/getdata.c | 37 +++++----- src/other.c | 28 ++++---- src/output.c | 169 +++++++++++++++++++++------------------------- src/sort.c | 16 ++--- 10 files changed, 148 insertions(+), 189 deletions(-) diff --git a/.gitignore b/.gitignore index f4fb084..b663d88 100644 --- a/.gitignore +++ b/.gitignore @@ -21,8 +21,5 @@ Makefile.in man/Makefile man/Makefile.in missing -src/.deps/ -src/Makefile -src/Makefile.in -src/*.o stamp-h1 +tags diff --git a/src/.gitignore b/src/.gitignore index b586827..8dc22f3 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1 +1,7 @@ +*~ +*.o +.deps/ dhcpd-pools +Makefile +Makefile.in +tags diff --git a/src/analyze.c b/src/analyze.c index c38cd55..0887b17 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -73,8 +73,7 @@ int prepare_data(void) qsort(touches, (size_t) num_touches, sizeof(uint32_t), &intcomp); /* Sort ranges */ - qsort(ranges, (size_t) num_ranges, sizeof(struct range_t), - &rangecomp); + qsort(ranges, (size_t) num_ranges, sizeof(struct range_t), &rangecomp); /* Sort backups */ if (0 < num_backups) { @@ -89,15 +88,14 @@ int prepare_data(void) int do_counting(void) { struct range_t *range_p; - unsigned int i, j, k, l, block_size; + unsigned long i, j, k, l, block_size; range_p = ranges; /* Walk through ranges */ for (i = j = k = l = 0; i < num_ranges; i++) { /* Count IPs in use */ - for (; leases[j] < range_p->last_ip - && (unsigned long) j < num_leases; j++) { + for (; leases[j] < range_p->last_ip && j < num_leases; j++) { if (leases[j] < range_p->first_ip) { continue; } @@ -109,8 +107,7 @@ int do_counting(void) } /* Count touched IPs */ - for (; touches[k] < range_p->last_ip - && (unsigned long) k < num_touches; k++) { + for (; touches[k] < range_p->last_ip && k < num_touches; k++) { if (touches[k] < range_p->first_ip) { continue; } @@ -124,7 +121,7 @@ int do_counting(void) /* Count backup IPs */ if (0 < num_backups) { for (; backups[l] < range_p->last_ip - && (unsigned long) l < num_touches; l++) { + && l < num_touches; l++) { if (touches[l] < range_p->first_ip) { continue; } @@ -138,8 +135,7 @@ int do_counting(void) /* Size of range, shared net & all networks */ block_size = - (unsigned int) (range_p->last_ip - range_p->first_ip - - 1); + (unsigned int)(range_p->last_ip - range_p->first_ip - 1); if (range_p->shared_net) { range_p->shared_net->available += block_size; } diff --git a/src/defaults.h b/src/defaults.h index 597da7c..4994732 100644 --- a/src/defaults.h +++ b/src/defaults.h @@ -37,7 +37,7 @@ # define DEFAULTS_H 1 /* Maximum line length in dhcpd.conf and dhcpd.leases */ -static const int MAXLEN = 1024; +static const size_t MAXLEN = 1024; /* Maximum number of shared networks */ static const unsigned int SHARED_NETWORKS = 8192; diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index bcd3d32..a23c69f 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -64,16 +64,16 @@ int main(int argc, char **argv) /* Options for getopt_long */ static struct option const long_options[] = { - {"config", required_argument, 0, (int) 'c'}, - {"leases", required_argument, 0, (int) 'l'}, - {"format", required_argument, 0, (int) 'f'}, - {"sort", required_argument, 0, (int) 's'}, - {"reverse", no_argument, 0, (int) 'r'}, - {"output", required_argument, 0, (int) 'o'}, - {"limit", required_argument, 0, (int) 'L'}, - {"version", no_argument, 0, (int) 'v'}, - {"help", no_argument, 0, (int) 'h'}, - {0, 0, 0, 0} + {"config", required_argument, NULL, 'c'}, + {"leases", required_argument, NULL, 'l'}, + {"format", required_argument, NULL, 'f'}, + {"sort", required_argument, NULL, 's'}, + {"reverse", no_argument, NULL, 'r'}, + {"output", required_argument, NULL, 'o'}, + {"limit", required_argument, NULL, 'L'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} }; /* FIXME: make these allocations dynamic up on need. */ @@ -86,14 +86,12 @@ int main(int argc, char **argv) config.output_file[0] = '\0'; /* File location defaults */ - strncpy(config.dhcpdconf_file, DHCPDCONF_FILE, - (size_t) MAXLEN - 1); - strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE, - (size_t) MAXLEN - 1); + strncpy(config.dhcpdconf_file, DHCPDCONF_FILE, MAXLEN - 1); + strncpy(config.dhcpdlease_file, DHCPDLEASE_FILE, MAXLEN - 1); tmp = OUTPUT_LIMIT; - config.output_limit[0] = (int) (*tmp - '0'); + config.output_limit[0] = (*tmp - '0'); tmp++; - config.output_limit[1] = (int) (*tmp - '0'); + config.output_limit[1] = (*tmp - '0'); fullhtml = false; /* Make sure some output format is selected by default */ @@ -113,13 +111,11 @@ int main(int argc, char **argv) switch (c) { case 'c': /* config file */ - strncpy(config.dhcpdconf_file, optarg, - (size_t) MAXLEN - 1); + strncpy(config.dhcpdconf_file, optarg, MAXLEN - 1); break; case 'l': /* lease file */ - strncpy(config.dhcpdlease_file, optarg, - (size_t) MAXLEN - 1); + strncpy(config.dhcpdlease_file, optarg, MAXLEN - 1); break; case 'f': /* Output format */ @@ -134,8 +130,7 @@ int main(int argc, char **argv) strncpy(config.sort, optarg, (size_t) 5); sorts = 5; } else { - strncpy(config.sort, optarg, - (size_t) sorts); + strncpy(config.sort, optarg, (size_t) sorts); } for (i = 0; i < sorts; i++) { field_selector(config.sort[i]); @@ -147,15 +142,14 @@ int main(int argc, char **argv) break; case 'o': /* Output file */ - strncpy(config.output_file, optarg, - (size_t) MAXLEN - 1); + strncpy(config.output_file, optarg, MAXLEN - 1); break; case 'L': /* Specification what will be printed */ for (i = 0; i < 2; i++) { if (optarg[i] >= '0' && optarg[i] < '8') { config.output_limit[i] = - (int) optarg[i] - '0'; + optarg[i] - '0'; } else { errx(EXIT_FAILURE, "main: output mask `%s' is illegal", diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index f20bf02..437384b 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -65,8 +65,8 @@ extern char *__progname; # define program_invocation_short_name \ prog_inv_sh_nm_from_file(__FILE__, 1) # endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */ -static char prog_inv_sh_nm_buf[256]; -static inline char *prog_inv_sh_nm_from_file(char *f, char stripext) +char prog_inv_sh_nm_buf[256]; +inline char *prog_inv_sh_nm_from_file(char *f, char stripext) { char *t; if ((t = strrchr(f, '/')) != NULL) { @@ -119,29 +119,21 @@ struct macaddr_t { /* Global variables */ static int const true = 1; static int const false = 0; - struct configuration_t config; - static int const output_limit_bit_1 = 1; static int const output_limit_bit_2 = 2; static int const output_limit_bit_3 = 4; unsigned int fullhtml; - struct shared_network_t *shared_networks; unsigned int num_shared_networks; - struct range_t *ranges; unsigned int num_ranges; - uint32_t *leases; unsigned long int num_leases; - uint32_t *touches; unsigned long int num_touches; - uint32_t *backups; unsigned long int num_backups; - struct macaddr_t *macaddr; /* Function prototypes */ @@ -159,7 +151,7 @@ int prepare_data(void); int do_counting(void); void flip_ranges(struct range_t *ranges, struct range_t *tmp_ranges) __attribute__ ((nonnull(1, 2))); -/* General support functions */ +/* support functions */ void *safe_malloc(const size_t size) #if __GNUC__ >= 3 __attribute__ ((__malloc__)) diff --git a/src/getdata.c b/src/getdata.c index adc4f7d..507234c 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -86,18 +86,17 @@ int parse_leases(void) dhcpd_leases = fopen(config.dhcpdlease_file, "r"); if (dhcpd_leases == NULL) { - err(EXIT_FAILURE, "parse_leases: %s", - config.dhcpdlease_file); + err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); } #ifdef POSIX_FADV_WILLNEED - posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_WILLNEED); + posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_WILLNEED); if (errno) { err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); } #endif /* POSIX_FADV_WILLNEED */ #ifdef POSIX_FADV_SEQUENTIAL - posix_fadvise((long) dhcpd_leases, 0, 0, POSIX_FADV_SEQUENTIAL); + posix_fadvise(fileno(dhcpd_leases), 0, 0, POSIX_FADV_SEQUENTIAL); if (errno) { err(EXIT_FAILURE, "parse_leases: fadvise %s", config.dhcpdlease_file); @@ -109,15 +108,13 @@ int parse_leases(void) * If someone has higher density in lease file I'm interested to * hear about that. */ if (stat(config.dhcpdlease_file, &lease_file_stats)) { - err(EXIT_FAILURE, "parse_leases: %s", - config.dhcpdlease_file); + err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); } leasesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2; touchesmallocsize = (lease_file_stats.st_size / 250) + MAXLEN - 2; backupsmallocsize = (lease_file_stats.st_size / 120) + MAXLEN - 2; leases = safe_malloc(sizeof(long int) * leasesmallocsize); - touches = - safe_malloc((size_t) sizeof(long int) * touchesmallocsize); + touches = safe_malloc((size_t) sizeof(long int) * touchesmallocsize); memset(leases, 0, sizeof(long int) * leasesmallocsize); memset(touches, 0, sizeof(long int) * touchesmallocsize); @@ -133,11 +130,12 @@ int parse_leases(void) while (!feof(dhcpd_leases)) { if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases)) { - err(EXIT_FAILURE, "parse_leases: %s", config.dhcpdlease_file); + err(EXIT_FAILURE, "parse_leases: %s", + config.dhcpdlease_file); } /* It's a lease, save IP */ if (strstr(line, "lease") == line) { - strncpy(ipstring, line, (size_t) MAXLEN); + strncpy(ipstring, line, MAXLEN); nth_field(2, ipstring, ipstring); inet_aton(ipstring, &inp); sw_active_lease = 0; @@ -187,8 +185,7 @@ int parse_leases(void) macstring[17] = '\0'; macaddr_p->ethernet = safe_strdup(macstring); macaddr_p->ip = safe_strdup(ipstring); - macaddr_p->next = - safe_malloc(sizeof(struct macaddr_t)); + macaddr_p->next = safe_malloc(sizeof(struct macaddr_t)); macaddr_p = macaddr_p->next; macaddr_p->next = NULL; } @@ -251,8 +248,9 @@ void parse_config(int is_include, char *config_file, struct shared_network_t *shared_p) { FILE *dhcpd_config; - int i = 0, newclause = true, argument = false, comment = + int newclause = true, argument = false, comment = false, braces = 0, quote = false; + size_t i = 0; char *word, c; int braces_shared = 1000; struct in_addr inp; @@ -271,13 +269,13 @@ void parse_config(int is_include, char *config_file, err(EXIT_FAILURE, "parse_config: %s", config_file); } #ifdef POSIX_FADV_WILLNEED - posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_WILLNEED); + posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_WILLNEED); if (errno) { err(EXIT_FAILURE, "parse_config: fadvise %s", config_file); } #endif /* POSIX_FADV_WILLNEED */ #ifdef POSIX_FADV_SEQUENTIAL - posix_fadvise((long) dhcpd_config, 0, 0, POSIX_FADV_SEQUENTIAL); + posix_fadvise(fileno(dhcpd_config), 0, 0, POSIX_FADV_SEQUENTIAL); if (errno) { err(EXIT_FAILURE, "parse_config: fadvise %s", config_file); } @@ -313,8 +311,7 @@ void parse_config(int is_include, char *config_file, if (quote == true) { break; } - if (comment == false && argument != 2 - && argument != 4) { + if (comment == false && argument != 2 && argument != 4) { newclause = true; i = 0; } else if (argument == 2) { @@ -364,8 +361,7 @@ void parse_config(int is_include, char *config_file, } /* Either inside comment or Nth word of clause. */ - if (comment == true - || (newclause == false && argument == 0)) { + if (comment == true || (newclause == false && argument == 0)) { continue; } /* Strip white spaces before new clause word. */ @@ -446,8 +442,7 @@ void parse_config(int is_include, char *config_file, shared_p->used = 0; shared_p->touched = 0; shared_p->backups = 0; - if (SHARED_NETWORKS < - num_shared_networks + 2) { + if (SHARED_NETWORKS < num_shared_networks + 2) { /* FIXME: make this * away by reallocationg * more space. */ diff --git a/src/other.c b/src/other.c index d3c81fb..aaf63a4 100644 --- a/src/other.c +++ b/src/other.c @@ -123,38 +123,35 @@ void clean_up(void) free(shared_networks); } -void print_version(void) +void __attribute__ ((__noreturn__)) print_version(void) { - fprintf(stdout, "%s\n", PACKAGE_STRING); - fprintf(stdout, - "Written by Sami Kerola.\nXML support by Dominic Germain, Sogetel inc.\n\n"); - fprintf(stdout, - "The software has FreeBSD License.\n"); + fprintf(stdout, "%s\n" + "Written by Sami Kerola.\n" + "XML support by Dominic Germain, Sogetel inc.\n\n" + "The software has FreeBSD License.\n", PACKAGE_STRING); exit(EXIT_SUCCESS); } -void usage(int status) +void __attribute__ ((__noreturn__)) usage(int status) { FILE *out; out = status != 0 ? stderr : stdout; - fprintf(out, "\n\ + fprintf(out, "\ Usage: %s [OPTIONS]\n\n", program_invocation_short_name); + fprintf(out, "\ This is ISC dhcpd pools usage analyzer.\n\ -\n"); - fprintf(out, "\ +\n\ -c, --config=FILE path to the dhcpd.conf file\n\ -l, --leases=FILE path to the dhcpd.leases file\n\ - -f, --format=[thHcxX] output format\n"); - fprintf(out, "\ + -f, --format=[thHcxX] output format\n\ t for text\n\ h for html table\n\ H for full html page\n\ x for xml\n\ X for xml with active lease details\n\ - c for comma separated values\n"); - fprintf(out, "\ + c for comma separated values\n\ -s, --sort=[nimcptTe] sort ranges by\n\ n name\n\ i IP\n\ @@ -163,8 +160,7 @@ This is ISC dhcpd pools usage analyzer.\n\ p percent\n\ t touched\n\ T t+c\n\ - e t+c perc\n"); - fprintf(out, "\ + e t+c perc\n\ -r, --reverse reverse order sort\n\ -o, --output=FILE output into a file\n\ -L, --limit=NR output limit mask 77 - 00\n\ diff --git a/src/output.c b/src/output.c index b0b0cfe..6350ae1 100644 --- a/src/output.c +++ b/src/output.c @@ -58,8 +58,7 @@ int output_txt(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_txt: %s", - config.output_file); + err(EXIT_FAILURE, "output_txt: %s", config.output_file); } } else { outfile = stdout; @@ -91,23 +90,24 @@ int output_txt(void) } fprintf(outfile, "%-16s", inet_ntoa(first)); fprintf(outfile, - " - %-16s %5"PRIu32" %5lu %10.3f %5lu %5lu %9.3f", + " - %-16s %5" PRIu32 + " %5lu %10.3f %5lu %5lu %9.3f", inet_ntoa(last), range_p->last_ip - range_p->first_ip - 1, range_p->count, - (float) (100 * range_p->count) / + (float)(100 * range_p->count) / (range_p->last_ip - range_p->first_ip - 1), range_p->touched, range_p->touched + range_p->count, - (float) (100 * - (range_p->touched + - range_p->count)) / - (range_p->last_ip - range_p->first_ip - - 1)); + (float)(100 * + (range_p->touched + + range_p->count)) / (range_p->last_ip - + range_p->first_ip - + 1)); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", range_p->backups, - (float) (100 * range_p->backups) / + (float)(100 * range_p->backups) / (range_p->last_ip - range_p->first_ip - 1)); } @@ -135,17 +135,17 @@ int output_txt(void) "%-20s %5lu %5lu %10.3f %7lu %6lu %9.3f", shared_p->name, shared_p->available, shared_p->used, - (float) (100 * shared_p->used) / + (float)(100 * shared_p->used) / shared_p->available, shared_p->touched, shared_p->touched + shared_p->used, - (float) (100 * - (shared_p->touched + - shared_p->used)) / + (float)(100 * + (shared_p->touched + + shared_p->used)) / shared_p->available); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", shared_p->backups, - (float) (100 * shared_p->backups) / + (float)(100 * shared_p->backups) / shared_p->available); } @@ -171,19 +171,19 @@ int output_txt(void) shared_networks->name, shared_networks->available, shared_networks->used, - (float) (100 * shared_networks->used) / + (float)(100 * shared_networks->used) / shared_networks->available, shared_networks->touched, shared_networks->touched + shared_networks->used, - (float) (100 * - (shared_networks->touched + - shared_networks->used)) / + (float)(100 * + (shared_networks->touched + + shared_networks->used)) / shared_networks->available); if (0 < num_backups) { fprintf(outfile, "%7lu %8.3f", shared_networks->backups, - (float) (100 * shared_networks->backups) / + (float)(100 * shared_networks->backups) / shared_networks->available); } fprintf(outfile, "\n"); @@ -216,8 +216,7 @@ int output_xml(void) if (config.output_file[0]) { outfile = fopen(config.output_file, "w+"); if (outfile == NULL) { - err(EXIT_FAILURE, "output_xml: %s", - config.output_file); + err(EXIT_FAILURE, "output_xml: %s", config.output_file); } } else { outfile = stdout; @@ -247,17 +246,15 @@ int output_xml(void) "\t%s\n", range_p->shared_net->name); } else { - fprintf(outfile, - "\t\n"); + fprintf(outfile, "\t\n"); } fprintf(outfile, "\t\n"); fprintf(outfile, "\t\n"); fprintf(outfile, "\t%s ", inet_ntoa(first)); - fprintf(outfile, "- %s\n", - inet_ntoa(last)); + fprintf(outfile, "- %s\n", inet_ntoa(last)); fprintf(outfile, "\t\n"); - fprintf(outfile, "\t%"PRIu32"\n", + fprintf(outfile, "\t%" PRIu32 "\n", range_p->last_ip - range_p->first_ip - 1); fprintf(outfile, "\t%lu\n", range_p->count); @@ -291,11 +288,9 @@ int output_xml(void) shared_networks->name); fprintf(outfile, "\t%lu\n", shared_networks->available); - fprintf(outfile, "\t%lu\n", - shared_networks->used); + fprintf(outfile, "\t%lu\n", shared_networks->used); fprintf(outfile, "\t%lu\n", - shared_networks->available - - shared_networks->used); + shared_networks->available - shared_networks->used); fprintf(outfile, "\n"); } @@ -317,8 +312,7 @@ int output_xml(void) void html_header(FILE * f) { - fprintf(f, - "\n"); fprintf(f, "\n"); fprintf(f, "\n"); @@ -381,7 +375,7 @@ void html_footer(FILE * f) fprintf(f, "
\n"); fprintf(f, "

\nData generated by "); fprintf(f, "", PACKAGE_URL); - fprintf(f, "dhcpd-pools.\n

\n"); + fprintf(f, "%s.\n

\n", PACKAGE_NAME); fprintf(f, "

\n"); fprintf(f, "