From fbfb057e8bed90f73850d8e871e4d70e8fa705ce Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 28 Jan 2006 00:17:38 +0000 Subject: Standardize the string functions and search for them properly when configuring on different OSs. --- common/compat.c | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++ common/compat.h | 72 ++++++++++++++++++++ common/stringx.c | 181 --------------------------------------------------- common/stringx.h | 72 -------------------- configure.in | 7 +- daemon/Makefile.am | 2 +- daemon/config.c | 11 ++-- daemon/rrd-update.c | 1 - daemon/rrdbotd.c | 3 +- daemon/snmp-engine.c | 1 - daemon/snmp-help.c | 1 - daemon/usuals.h | 2 + 12 files changed, 268 insertions(+), 266 deletions(-) create mode 100644 common/compat.c create mode 100644 common/compat.h delete mode 100644 common/stringx.c delete mode 100644 common/stringx.h diff --git a/common/compat.c b/common/compat.c new file mode 100644 index 0000000..9d2f53f --- /dev/null +++ b/common/compat.c @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * CONTRIBUTORS + * Nate Nielsen + * + */ + +#include + +#include +#include +#include +#include +#include + +#include "usuals.h" +#include "compat.h" + +#ifndef HAVE_STRCLN + +void +strcln(char* data, char ch) +{ + char* p; + for(p = data; *data; data++, p++) + { + while(*data == ch) + data++; + *p = *data; + } + + /* Renull terminate */ + *p = 0; +} + +#endif /* HAVE_STRCLN */ + +#ifndef HAVE_STRBTRIM + +char* +strbtrim(const char* data) +{ + while(*data && isspace(*data)) + ++data; + return (char*)data; +} + +#endif /* HAVE_STRBTRIM */ + +#ifndef HAVE_STRETRIM + +void +stretrim(char* data) +{ + char* t = data + strlen(data); + while(t > data && isspace(*(t - 1))) + { + t--; + *t = 0; + } +} + +#endif /* HAVE_STRETRIM */ + +#ifndef HAVE_STRTRIM + +char* +strtrim(char* data) +{ + data = (char*)strbtrim(data); + stretrim(data); + return data; +} + +#endif /* HAVE_STRTRIM */ + +#ifndef HAVE_STRTOB + +int +strtob(const char* str) +{ + if(strcasecmp(str, "0") == 0 || + strcasecmp(str, "no") == 0 || + strcasecmp(str, "false") == 0 || + strcasecmp(str, "f") == 0 || + strcasecmp(str, "off") == 0) + return 0; + + if(strcasecmp(str, "1") == 0 || + strcasecmp(str, "yes") == 0 || + strcasecmp(str, "true") == 0 || + strcasecmp(str, "t") == 0 || + strcasecmp(str, "on") == 0) + return 1; + + return -1; +} + +#endif /* HAVE_STRTOB */ + + +#ifndef HAVE_STRLCPY + +size_t +strlcpy(char *dst, const char *src, size_t len) +{ + size_t ret = strlen(dst); + + while (len > 1) { + *dst++ = *src++; + len--; + } + if (len > 0) + *dst = '\0'; + return (ret); +} + +#endif /* HAVE_STRLCPY */ + +#ifndef HAVE_STRLCAT + +size_t +strlcat(char* dst, const char* src, size_t siz) +{ + char* d = dst; + const char* s = src; + size_t n = siz; + size_t dlen; + + while(n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + if(n == 0) + return dlen + strlen(s); + while(*s != '\0') + { + if(n != 1) + { + *d++ = *s; + n--; + } + + s++; + } + *d = '\0'; + return dlen + (s - src); /* count does not include NUL */ +} + +#endif /* HAVE_STRLCAT */ diff --git a/common/compat.h b/common/compat.h new file mode 100644 index 0000000..17967f4 --- /dev/null +++ b/common/compat.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * CONTRIBUTORS + * Nate Nielsen + * + */ + +#ifndef __COMPAT_H__ +#define __COMPAT_H__ + +#include "config.h" + +#ifndef HAVE_STRLCPY +size_t strlcpy(char *dst, const char *src, size_t len); +#endif + +#ifndef HAVE_STRLCAT +size_t strlcat(char* dst, const char* src, size_t siz); +#endif + +#ifndef HAVE_STRCLN +void strcln(char* data, char ch); +#endif + +#ifndef HAVE_STRBTRIM +char* strbtrim(const char* data); +#endif + +#ifndef HAVE_STRETRIM +void stretrim(char* data); +#endif + +#ifndef HAVE_STRTRIM +char* strtrim(char* data); +#endif + +#ifndef HAVE_STRTOB +int strtob(const char* str); +#endif + +#endif /* __COMPAT_H__ */ diff --git a/common/stringx.c b/common/stringx.c deleted file mode 100644 index 3705cb9..0000000 --- a/common/stringx.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2004, Nate Nielsen - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * - * CONTRIBUTORS - * Nate Nielsen - * - */ - -#include - -#include -#include -#include -#include -#include - -#include "usuals.h" -#include "compat.h" - -#ifndef HAVE_STRCLN - -void -strcln(char* data, char ch) -{ - char* p; - for(p = data; *data; data++, p++) - { - while(*data == ch) - data++; - *p = *data; - } - - /* Renull terminate */ - *p = 0; -} - -#endif /* HAVE_STRCLN */ - -#ifndef HAVE_STRBTRIM - -char* -strbtrim(const char* data) -{ - while(*data && isspace(*data)) - ++data; - return (char*)data; -} - -#endif /* HAVE_STRBTRIM */ - -#ifndef HAVE_STRETRIM - -void -stretrim(char* data) -{ - char* t = data + strlen(data); - while(t > data && isspace(*(t - 1))) - { - t--; - *t = 0; - } -} - -#endif /* HAVE_STRETRIM */ - -#ifndef HAVE_STRTRIM - -char* -strtrim(char* data) -{ - data = (char*)trim_start(data); - trim_end(data); - return data; -} - -#endif /* HAVE_STRTRIM */ - -#ifndef HAVE_STRTOB - -int -strtob(const char* str) -{ - if(strcasecmp(str, "0") == 0 || - strcasecmp(str, "no") == 0 || - strcasecmp(str, "false") == 0 || - strcasecmp(str, "f") == 0 || - strcasecmp(str, "off") == 0) - return 0; - - if(strcasecmp(str, "1") == 0 || - strcasecmp(str, "yes") == 0 || - strcasecmp(str, "true") == 0 || - strcasecmp(str, "t") == 0 || - strcasecmp(str, "on") == 0) - return 1; - - return -1; -} - -#endif /* HAVE_STRTOB */ - - -#ifndef HAVE_STRLCPY - -size_t -strlcpy(char *dst, const char *src, size_t len) -{ - size_t ret = strlen(dst); - - while (len > 1) { - *dst++ = *src++; - len--; - } - if (len > 0) - *dst = '\0'; - return (ret); -} - -#endif /* HAVE_STRLCPY */ - -#ifndef HAVE_STRLCAT - -size_t -strlcat(char* dst, const char* src, size_t siz) -{ - char* d = dst; - const char* s = src; - size_t n = siz; - size_t dlen; - - while(n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; - if(n == 0) - return dlen + strlen(s); - while(*s != '\0') - { - if(n != 1) - { - *d++ = *s; - n--; - } - - s++; - } - *d = '\0'; - return dlen + (s - src); /* count does not include NUL */ -} - -#endif /* HAVE_STRLCAT */ diff --git a/common/stringx.h b/common/stringx.h deleted file mode 100644 index 3949d70..0000000 --- a/common/stringx.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2004, Nate Nielsen - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * Redistributions in binary form must reproduce the - * above copyright notice, this list of conditions and - * the following disclaimer in the documentation and/or - * other materials provided with the distribution. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH - * DAMAGE. - * - * - * CONTRIBUTORS - * Nate Nielsen - * - */ - -#ifndef __COMPAT_H__ -#define __COMPAT_H__ - -#include "config.h" - -#ifndef HAVE_STRLCPY -size_t strlcpy(char *dst, const char *src, size_t len); -#endif - -#ifndef HAVE_STRLCAT -size_t strlcat(char* dst, const char* src, size_t siz); -#endif - -#ifndef HAVE_STRCLN -void strcln(char* data); -#endif - -#ifndef HAVE_STRBTRIM -char* trim_start(const char* data); -#endif - -#ifndef HAVE_STRETRIM -void trim_end(char* data); -#endif - -#ifndef HAVE_STRTRIM -char* trim_space(char* data); -#endif - -#ifndef HAVE_STRTOB -int strtob(const char* str); -#endif - -#endif /* __COMPAT_H__ */ diff --git a/configure.in b/configure.in index ba89937..6438116 100644 --- a/configure.in +++ b/configure.in @@ -35,7 +35,12 @@ AC_C_INLINE dnl Check for header files. AC_HEADER_STDC AC_CHECK_HEADERS([rrd.h], , [echo "ERROR: rrd headers not found"]) -dnl TODO: AC_CHECK_HEADERS +AC_CHECK_HEADERS([unistd.h stdio.h stddef.h stdlib.h assert.h errno.h stdarg.h string.h netdb.h], , + [echo "ERROR: Required C header missing"; exit 1]) + +AC_CHECK_FUNCS([strlcat strlcpy strtob]) +AC_CHECK_FUNCS([strerror getopt getaddrinfo], , + [echo "ERROR: Required function missing"; exit 1]) AC_MSG_RESULT() diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 18b1217..fbc275d 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -5,7 +5,7 @@ rrdbotd_SOURCES = rrdbotd.c rrdbotd.h config.c usuals.h \ snmp-help.c snmp-engine.c rrd-update.c \ ../common/server-mainloop.c ../common/server-mainloop.h \ ../common/sock-any.h ../common/sock-any.c \ - ../common/stringx.h ../common/stringx.c \ + ../common/compat.h ../common/compat.c \ ../common/hash.h ../common/hash.c \ ../mib/parse.c rrdbotd_CFLAGS = -I${top_srcdir}/common/ -I${top_srcdir}/bsnmp/ -I${top_srcdir} \ diff --git a/daemon/config.c b/daemon/config.c index 39c5a37..9b38bcb 100644 --- a/daemon/config.c +++ b/daemon/config.c @@ -42,7 +42,6 @@ #include #include -#include "stringx.h" #include "rrdbotd.h" /* @@ -410,7 +409,7 @@ read_config_file(const char* configfile) config[len + 1] = 0; /* Remove nasty dos line endings */ - remove_cr(config); + strcln(config, '\r'); rb_messagex(LOG_DEBUG, "read config file: %s", configfile); return config; @@ -439,7 +438,7 @@ parse_config_file(const char* configfile, config_ctx *ctx) p = next; /* Do this before cleaning below */ next = t + 1; - t = trim_start(p); + t = strbtrim(p); /* Continuation line (had spaces at start) */ if(p < t && *t) @@ -482,7 +481,7 @@ parse_config_file(const char* configfile, config_ctx *ctx) errx(2, "%s: invalid config header: %s", ctx->confname, p); *t = 0; - header = trim_space(p + 1); + header = strtrim(p + 1); continue; } @@ -495,8 +494,8 @@ parse_config_file(const char* configfile, config_ctx *ctx) *t = 0; t++; - name = trim_space(p); - value = trim_space(t); + name = strtrim(p); + value = strtrim(t); } if(name && value) diff --git a/daemon/rrd-update.c b/daemon/rrd-update.c index d088e07..295eea6 100644 --- a/daemon/rrd-update.c +++ b/daemon/rrd-update.c @@ -43,7 +43,6 @@ #include #include -#include "stringx.h" #include "rrdbotd.h" #define MAX_NUMLEN 40 diff --git a/daemon/rrdbotd.c b/daemon/rrdbotd.c index 94212b5..a9171e8 100644 --- a/daemon/rrdbotd.c +++ b/daemon/rrdbotd.c @@ -46,7 +46,6 @@ #include #include -#include "stringx.h" #include "rrdbotd.h" /* The default command line options */ @@ -172,7 +171,7 @@ rb_vmessage(int level, int err, const char* msg, va_list ap) /* Cleanup the message a little */ strlcpy(buf, msg, MAX_MSGLEN); - trim_end(buf); + stretrim(buf); if(err) { diff --git a/daemon/snmp-engine.c b/daemon/snmp-engine.c index c0af083..66cc047 100644 --- a/daemon/snmp-engine.c +++ b/daemon/snmp-engine.c @@ -43,7 +43,6 @@ #include #include -#include "stringx.h" #include "rrdbotd.h" #include "server-mainloop.h" diff --git a/daemon/snmp-help.c b/daemon/snmp-help.c index 4ae7cb1..31f8dc6 100644 --- a/daemon/snmp-help.c +++ b/daemon/snmp-help.c @@ -43,7 +43,6 @@ #include #include -#include "stringx.h" #include "rrdbotd.h" /* Whether we print warnings when loading MIBs or not */ diff --git a/daemon/usuals.h b/daemon/usuals.h index f83a117..5fece3f 100644 --- a/daemon/usuals.h +++ b/daemon/usuals.h @@ -50,6 +50,8 @@ #include #include +#include "compat.h" + #ifndef NULL #define NULL 0 #endif -- cgit v1.2.3