From 4ce52e8b47af13fd6e25622ad48a70a9e43c3be9 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 21 May 2007 02:31:52 +0000 Subject: [PATCH] strndup() and strnlen() implementations from Jeroen Ruigrok van der Werven See bug #551 for original details -- http://open-ils.org/cgi-bin/bugzilla/show_bug.cgi?id=551 git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@909 9efc2488-bf62-4759-914b-345cdb29e865 --- src/ports/freebsd/Makefile | 20 ++++++++++++++++ src/ports/freebsd/strndup.c | 44 +++++++++++++++++++++++++++++++++++ src/ports/freebsd/strndup.h | 27 ++++++++++++++++++++++ src/ports/freebsd/strnlen.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/ports/freebsd/strnlen.h | 26 +++++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/ports/freebsd/Makefile create mode 100644 src/ports/freebsd/strndup.c create mode 100644 src/ports/freebsd/strndup.h create mode 100644 src/ports/freebsd/strnlen.c create mode 100644 src/ports/freebsd/strnlen.h diff --git a/src/ports/freebsd/Makefile b/src/ports/freebsd/Makefile new file mode 100644 index 0000000..1398045 --- /dev/null +++ b/src/ports/freebsd/Makefile @@ -0,0 +1,20 @@ +# OSRF_LOG_PARAMS log all incoming method params at OSRF_INFO log level. +# OSRF_STRICT_PARAMS instructs the app handler to return an error if the number of method arguments +# provided to any method is not at least as large as the 'argc' setting for the method + +CFLAGS += -rdynamic -fno-strict-aliasing -fPIC + +TARGETS = strndup.o strnlen.o +HEADERS = strndup.h strnlen.h + +all: libfreebsd_str_compat.so $(TARGETS) + +libfreebsd_str_compat.so: $(TARGETS) + $(CC) -shared -W1 $(TARGETS) -o $@ + +strndup.o: strndup.c strndup.h +strnlen.o: strnlen.c strnlen.h + +clean: + /bin/rm -f *o + diff --git a/src/ports/freebsd/strndup.c b/src/ports/freebsd/strndup.c new file mode 100644 index 0000000..10b49fd --- /dev/null +++ b/src/ports/freebsd/strndup.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2007 Albert Lee . + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include "strnlen.h" + +char * +strndup(const char *s, size_t n) +{ + char *ns; + + n = strnlen(s, n); + + if ((ns = (char *)malloc(n + 1))) { + ns[n] = '\0'; + return memcpy(ns, s, n); + } + + return NULL; +} diff --git a/src/ports/freebsd/strndup.h b/src/ports/freebsd/strndup.h new file mode 100644 index 0000000..be91f5f --- /dev/null +++ b/src/ports/freebsd/strndup.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2007 Albert Lee . + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +char *strndup(const char *s, size_t n); + diff --git a/src/ports/freebsd/strnlen.c b/src/ports/freebsd/strnlen.c new file mode 100644 index 0000000..81a6f17 --- /dev/null +++ b/src/ports/freebsd/strnlen.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2007 The Akuma Project + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * $Id$ + */ + +/* + * sys/types.h is a Single Unix Specification header and defines size_t. + */ + +#include + +/* + * As per the Linux manual page: + * + * The strnlen() function returns the number of characters in the string + * pointed to by s, not including the terminating '\0' character, but at most + * maxlen. In doing this, strnlen() looks only at the first maxlen characters + * at s and never beyond s+maxlen. + * + * The strnlen() function returns strlen(s), if that is less than maxlen, or + * maxlen if there is no '\0' character among the first maxlen characters + * pointed to by s. + */ + +size_t +strnlen(const char *string, size_t maxlen) +{ + int len = 0; + + if (maxlen == 0) + return (0); + + while (*string++ && ++len < maxlen) + ; + + return (len); +} diff --git a/src/ports/freebsd/strnlen.h b/src/ports/freebsd/strnlen.h new file mode 100644 index 0000000..181780a --- /dev/null +++ b/src/ports/freebsd/strnlen.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2007 The Akuma Project + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * $Id$ + */ + +size_t strnlen(const char *, size_t); + -- 2.11.0