X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fportab%2Fportabtest.c;h=e7b0dcc838ad3b0e82fb9edf1b2475525327cda6;hb=9811223fb882413645db38948ac05fa21ddd3514;hp=4f53ce76594a27fa0d70a551c6e213e66899de47;hpb=d38747d951a8a5007e97693cade3551e11e50569;p=ngircd.git diff --git a/src/portab/portabtest.c b/src/portab/portabtest.c index 4f53ce76..e7b0dcc8 100644 --- a/src/portab/portabtest.c +++ b/src/portab/portabtest.c @@ -1,6 +1,6 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors. + * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,13 +16,12 @@ * Test program for portab.h and friends ;-) */ -#include "imp.h" #include #include #include #include -#include "exp.h" +int allow_severity = 0, deny_severity = 0; static void Panic(char *Reason) @@ -44,6 +43,36 @@ Check_snprintf(void) Panic("snprintf string length"); } +static void +Check_strdup(void) +{ + char *ptr; + + ptr = strdup("1234567890"); + if (!ptr) + Panic("strdup"); + if (ptr[10] != '\0') + Panic("strdup NULL byte"); + if (strlen(ptr) != 10) + Panic("strdup string length"); + free(ptr); +} + +static void +Check_strndup(void) +{ + char *ptr; + + ptr = strndup("1234567890", 5); + if (!ptr) + Panic("strndup"); + if (ptr[5] != '\0') + Panic("strndup NULL byte"); + if (strlen(ptr) != 5) + Panic("strndup string length"); + free(ptr); +} + static void Check_strlcpy(void) { @@ -72,6 +101,30 @@ Check_strlcat(void) Panic("strlcat string length"); } +static void +Check_strtok_r(void) +{ + char *ptr, *last; + + ptr = strdup("12,abc"); + + ptr = strtok_r(ptr, ",", &last); + if (!ptr) + Panic("strtok_r result #1"); + if (strcmp(ptr, "12") != 0) + Panic("strtok_r token #1"); + + ptr = strtok_r(NULL, ",", &last); + if (!ptr) + Panic("strtok_r result #2"); + if (strcmp(ptr, "abc") != 0) + Panic("strtok_r token #2"); + + ptr = strtok_r(NULL, ",", &last); + if (ptr) + Panic("strtok_r result #3"); +} + #ifdef PROTOTYPES static void Check_vsnprintf(const int Len, const char *Format, ...) @@ -85,16 +138,35 @@ va_dcl { char str[5]; va_list ap; + int r; #ifdef PROTOTYPES va_start(ap, Format); #else va_start(ap); #endif - if (vsnprintf(str, sizeof(str), Format, ap) != Len) - Panic("vsnprintf return code"); + r = vsnprintf(str, sizeof(str), Format, ap); va_end(ap); - + if (r != Len) { + /* C99 states that vsnprintf() "returns the number of + * characters that would have been printed if the n were + * unlimited", but according to the Linux manual page "glibc + * until 2.0.6 would return -1 when the output was truncated", + * and other implementations (libUTIL on A/UX) even return the + * number of characters processed ... so we only test our own + * implementation and warn on errors otherwise :-/ */ +#ifdef HAVE_VSNPRINTF + fprintf(stderr, + "\n ** WARNING: The vsnprintf() function of this system isn't standard\n"); + fprintf(stderr, + " ** conformant and returns a WRONG result: %d (should be %d)! The test\n", + r, Len); + fprintf(stderr, + " ** result has been ignored but may lead to errors during execution!\n\n"); +#else + Panic("vsnprintf return code"); +#endif + } if (str[4] != '\0') Panic("vsnprintf NULL byte"); if (strlen(str) != 4) @@ -118,10 +190,13 @@ main(void) /* check functions */ Check_snprintf(); + Check_strdup(); + Check_strndup(); Check_strlcpy(); Check_strlcat(); + Check_strtok_r(); Check_vsnprintf(2+10, "%s%s", "ab", "1234567890"); - + return 0; }