From 7c9cf4a184595f589ff11bd0008dd4a8c54e1a25 Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Tue, 25 Feb 2020 18:10:37 -0600 Subject: Use ANSI C --- Makefile | 7 ++++--- pfxtree-test.c | 12 +++++++----- pfxtree.c | 13 ++++++------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 443c3c3..2b0bcdd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -CC=c99 -CFLAGS=-Wall -Wextra -Wunreachable-code -ftrapv -fPIC -g -D_POSIX_C_SOURCE=2 +CC ?= cc +STND ?= -ansi -pedantic +CFLAGS += $(STND) -O2 -Wall -Wextra -Wunreachable-code -ftrapv -fPIC \ + -D_POSIX_C_SOURCE=2 PREFIX=/usr/local LIBDIR=$(DESTDIR)$(PREFIX)/lib PKGCONFIGDIR=$(LIBDIR)/pkgconfig @@ -29,7 +31,6 @@ test: pfxtree-test install: libpfxtree.so install -m755 libpfxtree.so $(LIBDIR)/libpfxtree.so.$(VERSION) install -m644 pfxtree.h $(HDIR)/pfxtree.h - install -m644 pfxtree.pc $(PKGCONFIGDIR)/pfxtree.pc ln -sf $(LIBDIR)/libpfxtree.so.$(VERSION) $(LIBDIR)/libpfxtree.so.$(MAJOR) ln -sf $(LIBDIR)/libpfxtree.so.$(VERSION) $(LIBDIR)/libpfxtree.so diff --git a/pfxtree-test.c b/pfxtree-test.c index 8d9f25e..d176505 100644 --- a/pfxtree-test.c +++ b/pfxtree-test.c @@ -1,6 +1,6 @@ /* * pfxtree-test - Delwink prefix tree library unit test - * Copyright (C) 2017 Delwink, LLC + * Copyright (C) 2017, 2020 Delwink, LLC * * Redistributions, modified or unmodified, in whole or in part, must retain * applicable copyright or other legal privilege notices, these conditions, and @@ -59,10 +59,12 @@ main(void) assert(NULL == pt_search(p, "hello")); assert(pt_search(p, "hell") != NULL); - void *dummy = malloc(0xDFDF); - assert(dummy != NULL); - assert(0 == pt_add_p(p, "hello", dummy)); - assert('p' == pt_data_type(pt_search(p, "hello"))); + { + void *dummy = malloc(0xDFDF); + assert(dummy != NULL); + assert(0 == pt_add_p(p, "hello", dummy)); + assert('p' == pt_data_type(pt_search(p, "hello"))); + } assert(PT_ENOWORD == pt_del(p, "asdf")); diff --git a/pfxtree.c b/pfxtree.c index 3e88e03..074bab8 100644 --- a/pfxtree.c +++ b/pfxtree.c @@ -1,6 +1,6 @@ /* * libpfxtree - Delwink prefix tree library - * Copyright (C) 2015, 2017 Delwink, LLC + * Copyright (C) 2015, 2017, 2020 Delwink, LLC * * Redistributions, modified or unmodified, in whole or in part, must retain * applicable copyright or other legal privilege notices, these conditions, and @@ -113,7 +113,7 @@ static int add(PrefixTree *self, const char *word, union _pt_data data, int type) { int rc = 0; - PrefixTree *node = self; + PrefixTree *node = self, *first_insertion = NULL; size_t i, len = strlen(word); for (i = 0; i <= len; ++i) @@ -125,11 +125,9 @@ add(PrefixTree *self, const char *word, union _pt_data data, int type) node = child; } - PrefixTree *first_insertion = NULL; - for (; i <= len; ++i) { - PrefixTree *new = pt_new(); + PrefixTree *new = pt_new(), *end; if (!new) { rc = PT_EALLOC; @@ -139,7 +137,7 @@ add(PrefixTree *self, const char *word, union _pt_data data, int type) new->ch = word[i]; new->parent = node; - PrefixTree *end = get_last_child(node); + end = get_last_child(node); if (!end) node->children = new; else @@ -182,10 +180,11 @@ pt_add_p(PrefixTree *self, const char *word, void *data) static size_t num_children(PrefixTree *self) { + size_t i; + if (!self->children) return 0; - size_t i; self = self->children; for (i = 1; self->next; ++i) self = self->next; -- cgit v1.2.3