diff options
Diffstat (limited to 'src/jtutil')
-rw-r--r-- | src/jtutil/array.cc | 3 | ||||
-rw-r--r-- | src/jtutil/fuzzy.cc | 2 | ||||
-rw-r--r-- | src/jtutil/make.code.defn | 3 | ||||
-rw-r--r-- | src/jtutil/makefile | 7 | ||||
-rw-r--r-- | src/jtutil/miscfp.cc | 1 | ||||
-rw-r--r-- | src/jtutil/miscstr.c | 67 | ||||
-rw-r--r-- | src/jtutil/norm.cc | 1 | ||||
-rw-r--r-- | src/jtutil/round.cc | 2 | ||||
-rw-r--r-- | src/jtutil/test_strlcat.c | 96 | ||||
-rw-r--r-- | src/jtutil/util.hh | 14 |
10 files changed, 192 insertions, 4 deletions
diff --git a/src/jtutil/array.cc b/src/jtutil/array.cc index 5eb841a..c9d4176 100644 --- a/src/jtutil/array.cc +++ b/src/jtutil/array.cc @@ -17,7 +17,8 @@ // #include <assert.h> -#include <stddef.h> // for NULL +#include <stddef.h> // NULL +#include <stdlib.h> // size_t // we want to instantiate templates with CCTK_* types #ifdef STANDALONE_TEST diff --git a/src/jtutil/fuzzy.cc b/src/jtutil/fuzzy.cc index 34cffa2..d3e4314 100644 --- a/src/jtutil/fuzzy.cc +++ b/src/jtutil/fuzzy.cc @@ -10,6 +10,8 @@ // ***** template instantiations and specializations ***** // +#include <stdlib.h> + #include "stdc.h" #include "util.hh" diff --git a/src/jtutil/make.code.defn b/src/jtutil/make.code.defn index a3ecb79..29bf72e 100644 --- a/src/jtutil/make.code.defn +++ b/src/jtutil/make.code.defn @@ -2,7 +2,8 @@ # $Header$ # Source files in this directory -SRCS = array.cc cpm_map.cc fuzzy.cc linear_map.cc miscfp.cc norm.cc round.cc \ +SRCS = array.cc cpm_map.cc fuzzy.cc linear_map.cc norm.cc round.cc \ + miscstr.c miscfp.cc \ error_exit.cc # Subdirectories containing source files diff --git a/src/jtutil/makefile b/src/jtutil/makefile index ca3d738..abab2fb 100644 --- a/src/jtutil/makefile +++ b/src/jtutil/makefile @@ -1,5 +1,5 @@ # Makefile for standalone test drivers in this directory -# $Header: /usr/local/svn/cvs-repositories/numrelcvs/AEIThorns/AHFinderDirect/src/jtutil/makefile,v 1.9 2002-10-11 17:35:11 jthorn Exp $ +# $Header: /usr/local/svn/cvs-repositories/numrelcvs/AEIThorns/AHFinderDirect/src/jtutil/makefile,v 1.10 2003-02-16 22:21:49 jthorn Exp $ # # CC, CXX = C and C++ compilers. Defaults are gcc and g++ if # variables aren't set from command line or environment. @@ -26,7 +26,8 @@ ALL_TESTS := test_array test_array2 \ test_cpm_map test_linear_map \ test_fuzzy test_round \ test_modulo test_norm \ - test_error_exit + test_error_exit \ + test_strlcat ################################################################################ @@ -48,6 +49,8 @@ test_norm : test_norm.o norm.o \ fuzzy.o round.o -lm test_error_exit : test_error_exit.o error_exit.o +test_strlcat : test_strlcat.o miscstr.o + .PHONY : clean clean : -rm -f *.o diff --git a/src/jtutil/miscfp.cc b/src/jtutil/miscfp.cc index fb59352..0015644 100644 --- a/src/jtutil/miscfp.cc +++ b/src/jtutil/miscfp.cc @@ -8,6 +8,7 @@ // #include <math.h> +#include <stdlib.h> #include "stdc.h" #include "util.hh" diff --git a/src/jtutil/miscstr.c b/src/jtutil/miscstr.c new file mode 100644 index 0000000..6553b55 --- /dev/null +++ b/src/jtutil/miscstr.c @@ -0,0 +1,67 @@ +/* miscstr.c -- misc string routines */ +/* $Header$ */ + +#include <stdlib.h> +#include <string.h> + +#include "../include/stdc.h" + +/* + * exceptionally, this (C++) header file is safe to #include from C, + * and contains a prototype for AHFinderDirect_Strlcat() . + */ +#include "util.hh" + +/******************************************************************************/ + +/*@@ + @routine AHFinderDirect_Strlcat + @date 16.Feb.2003 + @author Jonathan Thornburg <jthorn@aei.mpg.de> + @desc This function implements the strcat() function + described in + http://www.openbsd.org/papers/strlcpy-paper.ps + + The strlcat(3) function appends the null-terminated string + src to the end of dst. It will append at most + size - strlen(dst) - 1 + characters, and always null-terminates the result. + (Hence this function never overflows the destination buffer.) + + strlcat(3) is a replacement for strncat(3). In comparison + to strncat(3), strlcat(3) is safer and easier to use: + it guarantees null termination of the destination buffer, + and the size parameter is easy to specify without danger + of off-by-one errors. + @enddesc + + @var dst + @vdesc A non-null pointer to the destination buffer. + @vtype char* dst + @endvar + + @var src + @vdesc A non-null pointer to the source string. + @vtype const char* dst + @endvar + + @var dst_size + @vdesc The size of the destination buffer. + @vtype size_t dst_size + @endvar + + @returntype size_t + @returndesc This function returns the length of the string it + tries to create, i.e. strlen(src) + strlen(dst() . + @endreturndesc + @@*/ +size_t AHFinderDirect_Strlcat(char* dst, const char* src, size_t dst_size) +{ +const size_t src_len = strlen(src); +const size_t dst_len = strlen(dst); +const int dst_remaining = dst_size - dst_len - 1; +if (dst_remaining > 0) + then strncat(dst, src, dst_remaining); + +return src_len + dst_len; +} diff --git a/src/jtutil/norm.cc b/src/jtutil/norm.cc index 23cac5c..edd3fd1 100644 --- a/src/jtutil/norm.cc +++ b/src/jtutil/norm.cc @@ -7,6 +7,7 @@ #include <math.h> #include <assert.h> +#include <stdlib.h> #include "util.hh" diff --git a/src/jtutil/round.cc b/src/jtutil/round.cc index b990ee0..6559944 100644 --- a/src/jtutil/round.cc +++ b/src/jtutil/round.cc @@ -9,6 +9,8 @@ // ***** template instantiations ***** // +#include <stdlib.h> + #include "stdc.h" #include "util.hh" diff --git a/src/jtutil/test_strlcat.c b/src/jtutil/test_strlcat.c new file mode 100644 index 0000000..0c69f57 --- /dev/null +++ b/src/jtutil/test_strlcat.c @@ -0,0 +1,96 @@ +/* test_strlcat -- test driver for Util_Strlcpy() */ +/* $Header$ */ + +#include <string.h> +#include <stdio.h> + +#include "../include/stdc.h" + +/* + * exceptionally, this (C++) header file is safe to #include from C, + * and contains a prototype for AHFinderDirect_Strlcat() . + */ +#include "util.hh" + +/******************************************************************************/ + +/* prototypes */ +size_t tryit(size_t dst_size, const char* src); +void nprint(int n_print, const char* buf); + +/* global data structures */ +static char buffer[100]; + +/******************************************************************************/ + +/* + * This program is a test driver for Util_Strlcpy() . + */ + +int main(void) +{ +size_t n; + +n = tryit(15, "world"); +printf("bufsize=15: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(11, "world"); +printf("bufsize=11: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(10, "world"); +printf("bufsize=10: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(9, "world"); +printf("bufsize=9: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(6, "world"); +printf("bufsize=6: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(5, "world"); +printf("bufsize=5: result=%d buffer=", (int) n); +nprint(20, buffer); + +n = tryit(4, "world"); +printf("bufsize=4: result=%d buffer=", (int) n); +nprint(20, buffer); + +return 0; +} + +/******************************************************************************/ + +size_t tryit(size_t dst_size, const char* src) +{ +const char hello[] = "hello\0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; +memcpy(buffer, hello, sizeof(hello)); +return AHFinderDirect_Strlcat(buffer, src, dst_size); +} + +/******************************************************************************/ + +/* print n_print characters of buf[], with visible indication of '\0' */ +void nprint(int n_print, const char* buf) +{ +int i; +int i_null = -1; + +printf("\""); + for (i = 0 ; i < n_print ; ++i) + { + if (buf[i] == '\0') + then { + if (i_null == -1) + then i_null = i; + printf("\\0"); + } + else printf("%c", buf[i]); + } +if (i_null >= 0) + then printf(" [null at i=%d]", i_null); +printf("\"\n"); +} diff --git a/src/jtutil/util.hh b/src/jtutil/util.hh index df3daeb..d87330d 100644 --- a/src/jtutil/util.hh +++ b/src/jtutil/util.hh @@ -3,9 +3,22 @@ // // prerequisites: +// <stdlib.h> or <string.h> or <stdio.h> // size_t // "stdc.h" // +// +// misc string functions (in C, not C++) +// +#ifdef __cplusplus +extern "C" +#endif +size_t AHFinderDirect_Strlcat(char* dst, const char* src, size_t dst_size); + +// +// the rest of this file is C++ only +// +#ifdef __cplusplus namespace jtutil { @@ -185,3 +198,4 @@ public: //****************************************************************************** } // namespace jtutil +#endif /* __cplusplus */ |