From 469821c44d7ff21215d56fc1134677e6ee09cf55 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 21 May 2007 05:12:27 +0000 Subject: [PATCH] compatability functions for strnlen and strndup git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@912 9efc2488-bf62-4759-914b-345cdb29e865 --- src/ports/strn_compat/Makefile | 20 ++++++++++++ src/ports/strn_compat/strndup.c | 44 ++++++++++++++++++++++++++ src/ports/strn_compat/strndup.h | 27 ++++++++++++++++ src/ports/strn_compat/strnlen.c | 56 +++++++++++++++++++++++++++++++++ src/ports/strn_compat/strnlen.h | 26 +++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 src/ports/strn_compat/Makefile create mode 100644 src/ports/strn_compat/strndup.c create mode 100644 src/ports/strn_compat/strndup.h create mode 100644 src/ports/strn_compat/strnlen.c create mode 100644 src/ports/strn_compat/strnlen.h diff --git a/src/ports/strn_compat/Makefile b/src/ports/strn_compat/Makefile new file mode 100644 index 0000000..1398045 --- /dev/null +++ b/src/ports/strn_compat/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/strn_compat/strndup.c b/src/ports/strn_compat/strndup.c new file mode 100644 index 0000000..10b49fd --- /dev/null +++ b/src/ports/strn_compat/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/strn_compat/strndup.h b/src/ports/strn_compat/strndup.h new file mode 100644 index 0000000..be91f5f --- /dev/null +++ b/src/ports/strn_compat/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/strn_compat/strnlen.c b/src/ports/strn_compat/strnlen.c new file mode 100644 index 0000000..81a6f17 --- /dev/null +++ b/src/ports/strn_compat/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/strn_compat/strnlen.h b/src/ports/strn_compat/strnlen.h new file mode 100644 index 0000000..181780a --- /dev/null +++ b/src/ports/strn_compat/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.43.2