diff --git a/.gitignore b/.gitignore index 2209c0b63..8ca888574 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ *.gcno *.log *.o +*.lo +*.la *.opendb *.orig *.pyc @@ -15,6 +17,7 @@ .dirstamp .DS_Store .local-version +.libs .vagrant aclocal.m4 autom4te.cache diff --git a/configure.ac b/configure.ac index f60cf5f66..d14202224 100644 --- a/configure.ac +++ b/configure.ac @@ -253,6 +253,11 @@ AX_CHECK_COMPILER_OPTIONS([C++], ${PROSPECTIVE_CXXFLAGS}) # Check for and initialize libtool LT_INIT +AC_PROG_LIBTOOL + +# Disable building shared libraries by default (can be enabled with --enable-shared) + +AC_DISABLE_SHARED # # Debug instances @@ -984,9 +989,28 @@ AC_MSG_NOTICE([checking required package dependencies]) # NL_WITH_PACKAGE(...) +# +# Checks for missing functions +# + +AC_DEFUN([CHECK_MISSING_FUNC], [ + AC_CHECK_FUNC($1, [], [ + nl_cv_missing_$1=yes + MISSING_CPPFLAGS="${MISSING_CPPFLAGS} "'-include $(top_srcdir)/src/missing/$1/$1.h' + MISSING_LIBADD="${MISSING_LIBADD} "'$(top_builddir)/src/missing/$1/lib$1.la' + ]) + AM_CONDITIONAL(m4_toupper(MISSING_$1), [test "${nl_cv_missing_$1}" = "yes"]) +]) +AC_SUBST(MISSING_CPPFLAGS) +AC_SUBST(MISSING_LIBADD) + +CHECK_MISSING_FUNC([strlcpy]) +CHECK_MISSING_FUNC([strlcat]) + # # Check for headers # + AC_HEADER_STDBOOL AC_HEADER_STDC @@ -1041,6 +1065,11 @@ src/cli/Makefile src/ncp/Makefile src/core/Makefile src/diag/Makefile +src/missing/Makefile +src/missing/stdbool/Makefile +src/missing/stdint/Makefile +src/missing/strlcat/Makefile +src/missing/strlcpy/Makefile third_party/Makefile third_party/mbedtls/Makefile examples/Makefile diff --git a/etc/visual-studio/libopenthread-cli-windows.vcxproj b/etc/visual-studio/libopenthread-cli-windows.vcxproj index 828aa280a..da4e86421 100644 --- a/etc/visual-studio/libopenthread-cli-windows.vcxproj +++ b/etc/visual-studio/libopenthread-cli-windows.vcxproj @@ -41,12 +41,14 @@ MBEDTLS_CONFIG_FILE="mbedtls-config.h"; OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; OTDLL; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\src; ..\..\src\core; + ..\..\src\missing; Level3 true diff --git a/etc/visual-studio/libopenthread-cli.vcxproj b/etc/visual-studio/libopenthread-cli.vcxproj index a64f92422..e3bed0786 100644 --- a/etc/visual-studio/libopenthread-cli.vcxproj +++ b/etc/visual-studio/libopenthread-cli.vcxproj @@ -41,12 +41,14 @@ MBEDTLS_CONFIG_FILE="mbedtls-config.h"; OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; OPENTHREAD_MULTIPLE_INSTANCE; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\src; ..\..\src\core; + ..\..\src\missing; ..\..\third_party\mbedtls; ..\..\third_party\mbedtls\repo\include; diff --git a/etc/visual-studio/libopenthread-ncp-spi.vcxproj b/etc/visual-studio/libopenthread-ncp-spi.vcxproj index 3313d1cd2..e7a7fd165 100644 --- a/etc/visual-studio/libopenthread-ncp-spi.vcxproj +++ b/etc/visual-studio/libopenthread-ncp-spi.vcxproj @@ -41,12 +41,14 @@ MBEDTLS_CONFIG_FILE="mbedtls-config.h"; OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; OPENTHREAD_MULTIPLE_INSTANCE; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\src; ..\..\src\core; + ..\..\src\missing; ..\..\third_party\mbedtls; ..\..\third_party\mbedtls\repo\include; diff --git a/etc/visual-studio/libopenthread-ncp-uart.vcxproj b/etc/visual-studio/libopenthread-ncp-uart.vcxproj index a5f9765a1..08c5700d1 100644 --- a/etc/visual-studio/libopenthread-ncp-uart.vcxproj +++ b/etc/visual-studio/libopenthread-ncp-uart.vcxproj @@ -41,12 +41,14 @@ MBEDTLS_CONFIG_FILE="mbedtls-config.h"; OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; OPENTHREAD_MULTIPLE_INSTANCE; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\src; ..\..\src\core; + ..\..\src\missing; ..\..\third_party\mbedtls; ..\..\third_party\mbedtls\repo\include; diff --git a/etc/visual-studio/libopenthread-windows.vcxproj b/etc/visual-studio/libopenthread-windows.vcxproj index f9a0acb6f..28a0914d6 100644 --- a/etc/visual-studio/libopenthread-windows.vcxproj +++ b/etc/visual-studio/libopenthread-windows.vcxproj @@ -40,12 +40,14 @@ %(PreprocessorDefinitions); OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\examples\platforms; ..\..\src\core; + ..\..\src\missing; true diff --git a/etc/visual-studio/libopenthread.vcxproj b/etc/visual-studio/libopenthread.vcxproj index 86ed3fc75..0f13f09b2 100644 --- a/etc/visual-studio/libopenthread.vcxproj +++ b/etc/visual-studio/libopenthread.vcxproj @@ -41,11 +41,15 @@ MBEDTLS_CONFIG_FILE="mbedtls-config.h"; OPENTHREAD_CONFIG_FILE="openthread-windows-config.h"; OPENTHREAD_MULTIPLE_INSTANCE; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\src\core; + ..\..\src\missing; + ..\..\src\missing\stdint; + ..\..\src\missing\stdbool; ..\..\third_party\mbedtls; ..\..\third_party\mbedtls\repo\include; @@ -131,6 +135,8 @@ + + @@ -213,6 +219,8 @@ + + diff --git a/etc/visual-studio/libopenthread.vcxproj.filters b/etc/visual-studio/libopenthread.vcxproj.filters index d289f8f14..dd4c8d7f3 100644 --- a/etc/visual-studio/libopenthread.vcxproj.filters +++ b/etc/visual-studio/libopenthread.vcxproj.filters @@ -31,6 +31,9 @@ {A0D2E990-DC5E-4B56-A552-888F97214A17} + + {F0DDA90-D13F-4B56-A552-328F97124A81} + {b4905c15-9702-4ff0-8d57-e7926337d50f} @@ -61,6 +64,9 @@ {25a5662d-0709-4698-8fd9-3abfde8f0fc6} + + {971969f2-f6ec-50b4-806a-97965c934cef} + @@ -291,6 +297,12 @@ Source Files\thread + + Source Files\missing + + + Source Files\missing + @@ -533,5 +545,11 @@ Header Files\thread + + Header Files\missing + + + Header Files\missing + \ No newline at end of file diff --git a/etc/visual-studio/libopenthread_k.vcxproj b/etc/visual-studio/libopenthread_k.vcxproj index 962ec7f93..5140a0d0c 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj +++ b/etc/visual-studio/libopenthread_k.vcxproj @@ -43,12 +43,14 @@ OPENTHREAD_PROJECT_CORE_CONFIG_FILE="openthread-core-windows-config.h"; WINDOWS_LOGGING; OPENTHREAD_MULTIPLE_INSTANCE; + OTBUILD; %(AdditionalIncludeDirectories); ..\..\include; ..\..\examples\drivers\windows\include; ..\..\src\core; + ..\..\src\missing; ..\..\src\missing\stdint; ..\..\src\missing\stdbool; ..\..\third_party\mbedtls; @@ -138,6 +140,8 @@ + + @@ -244,6 +248,8 @@ + + diff --git a/etc/visual-studio/libopenthread_k.vcxproj.filters b/etc/visual-studio/libopenthread_k.vcxproj.filters index a4fbd1688..eec183de7 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj.filters +++ b/etc/visual-studio/libopenthread_k.vcxproj.filters @@ -31,6 +31,9 @@ {A0D2E990-DC5E-4B56-A552-888F97214A17} + + {F0DDA90-D13F-4B56-A552-328F97124A81} + {b4905c15-9702-4ff0-8d57-e7926337d50f} @@ -64,6 +67,9 @@ {2b66635d-fb09-421b-9942-8c43df5c8c77} + + {971969f2-f6ec-50b4-806a-97965c934cef} + @@ -288,6 +294,12 @@ Source Files\common + + Source Files\missing + + + Source Files\missing + @@ -602,5 +614,11 @@ Header Files\api + + Header Files\missing + + + Header Files\missing + \ No newline at end of file diff --git a/include/openthread-windows-config.h b/include/openthread-windows-config.h index 676fc00c1..52726ec0b 100644 --- a/include/openthread-windows-config.h +++ b/include/openthread-windows-config.h @@ -101,6 +101,11 @@ // Temporary !!! TODO - Remove this once we figure out the strncpy issue #define _CRT_SECURE_NO_WARNINGS +#ifdef OTBUILD +#include +#include +#endif + // Disable a few warnings that we don't care about #pragma warning(disable:4200) // nonstandard extension used: zero-sized array in struct/union #pragma warning(disable:4201) // nonstandard extension used : nameless struct/union diff --git a/src/Makefile.am b/src/Makefile.am index 9076e6d8b..2a911e701 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,8 +31,9 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am # Always package (e.g. for 'make dist') these subdirectories. DIST_SUBDIRS = \ - cli \ + missing \ core \ + cli \ ncp \ diag \ $(NULL) @@ -40,6 +41,7 @@ DIST_SUBDIRS = \ # Always build (e.g. for 'make all') these subdirectories. SUBDIRS = \ + missing \ core \ $(NULL) @@ -60,6 +62,7 @@ endif PRETTY_SUBDIRS = \ cli \ core \ + missing \ $(NULL) include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am index 775c4da2b..795facac2 100644 --- a/src/cli/Makefile.am +++ b/src/cli/Makefile.am @@ -35,6 +35,7 @@ libopenthread_cli_a_CPPFLAGS = \ -I$(top_srcdir)/src \ -I$(top_srcdir)/src/core \ $(OPENTHREAD_TARGET_DEFINES) \ + $(MISSING_CPPFLAGS) \ $(NULL) libopenthread_cli_a_SOURCES = \ diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 10e9cc4da..c7cf162b8 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -35,6 +35,7 @@ CPPFLAGS_COMMON = \ -I$(top_srcdir)/include \ -I${abs_top_srcdir}/third_party/mbedtls/repo/include \ $(MBEDTLS_CPPFLAGS) \ + $(MISSING_CPPFLAGS) \ $(OPENTHREAD_TARGET_DEFINES) \ $(NULL) @@ -94,6 +95,40 @@ SOURCES_COMMON = \ utils/slaac_address.cpp \ $(NULL) +if MISSING_STRLCPY +############################################################### +# You'll see something similar to the next four lines in all +# of these `MISSING_*` blocks. These makefile gynmastics are +# to work around some unfortunate bugs in automake which +# prevent a more elegant solution. We can't just pull in the +# convenience libraries in `$(MISSING_LIBADD)` because that +# doesn't seem to work on some platforms. Also, we can't +# just include `$(top_srcdir)/src/missing/X/X.c` directly +# because a bug in automake causes the `clean` target to +# break. The following code may look ugly, but it gets the +# job done. It does this by copying the source file to the +# local build directory as an intermediate target. We then +# compile that file. We have to add the path in CPPFLAGS +# because the file might include headers from that directory. +# Because the copied source file is explicitly labeled as +# an intermediate target, it will always get deleted (unless +# it already existed in the first place). -- RQ 2017-03-22 +strlcpy.c: $(top_srcdir)/src/missing/strlcpy/strlcpy.c + $(AM_V_GEN)cp $< $@ +.INTERMEDIATE: strlcpy.c +CPPFLAGS_COMMON += -I$(top_srcdir)/src/missing/strlcpy +SOURCES_COMMON += strlcpy.c +endif + +if MISSING_STRLCAT +# See comment above for explanation. +strlcat.c: $(top_srcdir)/src/missing/strlcat/strlcat.c + $(AM_V_GEN)cp $< $@ +.INTERMEDIATE: strlcat.c +CPPFLAGS_COMMON += -I$(top_srcdir)/src/missing/strlcat +SOURCES_COMMON += strlcat.c +endif + if OPENTHREAD_ENABLE_APPLICATION_COAP SOURCES_COMMON += \ api/coap_api.cpp \ diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 126e66230..5a3b17b8e 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -494,7 +494,7 @@ ThreadError Mac::SetNetworkName(const char *aNetworkName) VerifyOrExit(strlen(aNetworkName) <= OT_NETWORK_NAME_MAX_SIZE, error = kThreadError_InvalidArgs); - strncpy(mNetworkName.m8, aNetworkName, sizeof(mNetworkName) - 1); + (void)strlcpy(mNetworkName.m8, aNetworkName, sizeof(mNetworkName)); exit: otLogFuncExitErr(error); diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index fa5ff262b..2efc9359c 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -227,7 +227,7 @@ ThreadError Commissioner::AddJoiner(const Mac::ExtAddress *aExtAddress, const ch mJoiners[i].mAny = true; } - strncpy(mJoiners[i].mPsk, aPSKd, sizeof(mJoiners[i].mPsk) - 1); + (void)strlcpy(mJoiners[i].mPsk, aPSKd, sizeof(mJoiners[i].mPsk)); mJoiners[i].mValid = true; mJoiners[i].mExpirationTime = Timer::GetNow() + Timer::SecToMsec(aTimeout); diff --git a/src/core/thread/link_quality.cpp b/src/core/thread/link_quality.cpp index db4eda7cc..98f0ac38a 100644 --- a/src/core/thread/link_quality.cpp +++ b/src/core/thread/link_quality.cpp @@ -153,21 +153,19 @@ ThreadError LinkQualityInfo::GetAverageRssAsString(char *aCharBuffer, size_t aBu if (mCount == 0) { - VerifyOrExit(aBufferLen >= sizeof(kUnknownRssString), error = kThreadError_NoBufs); - - strncpy(aCharBuffer, kUnknownRssString, aBufferLen); + charsWritten = static_cast(strlcpy(aCharBuffer, kUnknownRssString, aBufferLen)); } else { charsWritten = snprintf(aCharBuffer, aBufferLen, "%d.%s dBm", -(mRssAverage >> kRssAveragePrecisionMultipleBitShift), kLinkQualityDecimalDigitsString[mRssAverage & kRssAveragePrecisionMultipleBitMask]); - - VerifyOrExit(charsWritten >= 0, error = kThreadError_NoBufs); - - VerifyOrExit(static_cast(charsWritten) < aBufferLen, error = kThreadError_NoBufs); } + VerifyOrExit(charsWritten >= 0, error = kThreadError_NoBufs); + + VerifyOrExit(charsWritten < static_cast(aBufferLen), error = kThreadError_NoBufs); + exit: return error; } diff --git a/src/diag/Makefile.am b/src/diag/Makefile.am index 78717e80d..79d401747 100644 --- a/src/diag/Makefile.am +++ b/src/diag/Makefile.am @@ -35,6 +35,7 @@ libopenthread_diag_a_CPPFLAGS = \ -I$(top_srcdir)/src \ -I$(top_srcdir)/src/core \ $(OPENTHREAD_TARGET_DEFINES) \ + $(MISSING_CPPFLAGS) \ $(NULL) libopenthread_diag_a_SOURCES = \ diff --git a/src/missing/Makefile.am b/src/missing/Makefile.am new file mode 100644 index 000000000..1c821e281 --- /dev/null +++ b/src/missing/Makefile.am @@ -0,0 +1,56 @@ +# +# Copyright (c) 2016, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. 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. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may 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 HOLDER 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +# Always package (e.g. for 'make dist') these subdirectories. + +DIST_SUBDIRS = \ + stdbool \ + stdint \ + strlcpy \ + strlcat \ + $(NULL) + +# Always build (e.g. for 'make all') these subdirectories. + +SUBDIRS = \ + strlcpy \ + strlcat \ + $(NULL) + +# Always pretty (e.g. for 'make pretty') these subdirectories. + +PRETTY_SUBDIRS = \ + stdbool \ + stdint \ + strlcpy \ + strlcat \ + $(NULL) + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/missing/stdbool/Makefile.am b/src/missing/stdbool/Makefile.am new file mode 100644 index 000000000..2527c7d97 --- /dev/null +++ b/src/missing/stdbool/Makefile.am @@ -0,0 +1,35 @@ +# +# Copyright (c) 2016, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. 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. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may 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 HOLDER 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +EXTRA_DIST = \ + stdbool.h \ + $(NULL) + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/missing/stdint/Makefile.am b/src/missing/stdint/Makefile.am new file mode 100644 index 000000000..639466700 --- /dev/null +++ b/src/missing/stdint/Makefile.am @@ -0,0 +1,35 @@ +# +# Copyright (c) 2016, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. 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. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may 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 HOLDER 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +EXTRA_DIST = \ + stdint.h \ + $(NULL) + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/missing/strlcat/Makefile.am b/src/missing/strlcat/Makefile.am new file mode 100644 index 000000000..146f54195 --- /dev/null +++ b/src/missing/strlcat/Makefile.am @@ -0,0 +1,58 @@ +# +# Copyright (c) 2016, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. 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. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may 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 HOLDER 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +check_PROGRAMS = strlcat_test +strlcat_test_SOURCES = strlcat_test.c +strlcat_test_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + $(MISSING_CPPFLAGS) \ + $(NULL) +strlcat_test_LDADD = $(NULL) + +TESTS = strlcat_test + +if MISSING_STRLCAT +noinst_LTLIBRARIES = libstrlcat.la +libstrlcat_la_SOURCES = \ + strlcat.c \ + strlcat.h \ + $(NULL) +libstrlcat_la_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + $(MISSING_CPPFLAGS) \ + $(NULL) +strlcat_test_LDADD += libstrlcat.la +endif + +if MISSING_STRLCAT +strlcat_test_LDADD += ../strlcpy/libstrlcpy.la +endif + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/missing/strlcat/strlcat.c b/src/missing/strlcat/strlcat.c new file mode 100644 index 000000000..44029c66d --- /dev/null +++ b/src/missing/strlcat/strlcat.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#include "strlcat.h" +#include "../strlcpy/strlcpy.h" + +size_t strlcat(char *dest, const char *src, size_t size) +{ + size_t len = strlen(dest); + + if (len < size - 1) + { + return (len + strlcpy(dest + len, src, size - len)); + } + + return len + strlen(src); +} + diff --git a/src/missing/strlcat/strlcat.h b/src/missing/strlcat/strlcat.h new file mode 100644 index 000000000..d549c37e2 --- /dev/null +++ b/src/missing/strlcat/strlcat.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#ifndef MISSING_STRLCAT_HEADER_INCLUDED +#define MISSING_STRLCAT_HEADER_INCLUDED 1 + +#include + +#ifdef strlcat +#undef strlcat +#endif + +#define strlcat ___missing_strlcat + +#ifdef __cplusplus +extern "C" { +#endif + +size_t strlcat(char *dest, const char *src, size_t size); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // MISSING_STRLCPY_HEADER_INCLUDED diff --git a/src/missing/strlcat/strlcat_test.c b/src/missing/strlcat/strlcat_test.c new file mode 100644 index 000000000..e72cb239c --- /dev/null +++ b/src/missing/strlcat/strlcat_test.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#include +#include +#include "string.h" + +int main(void) +{ + char string_a[8] = "foo"; + char string_b[8] = "barbar"; + size_t ret = 0; + int errors = 0; + + ret = strlcat(string_a, string_b, sizeof(string_a)); + + if (0 != strcmp(string_a, "foobarb")) + { + printf("strcmp failed\n"); + errors++; + } + + if (ret != 9) + { + printf("strlcat return value is wrong (%d)\n", (int)ret); + errors++; + } + + if (errors != 0) + { + printf("FAIL\n"); + return EXIT_FAILURE; + } + + printf("OK\n"); + return EXIT_SUCCESS; +} diff --git a/src/missing/strlcpy/Makefile.am b/src/missing/strlcpy/Makefile.am new file mode 100644 index 000000000..5f951e36f --- /dev/null +++ b/src/missing/strlcpy/Makefile.am @@ -0,0 +1,46 @@ +# +# Copyright (c) 2016, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. 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. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may 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 HOLDER 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +check_PROGRAMS = strlcpy_test +strlcpy_test_SOURCES = strlcpy_test.c +strlcpy_test_CPPFLAGS = $(MISSING_CPPFLAGS) + +TESTS = strlcpy_test + +if MISSING_STRLCPY +noinst_LTLIBRARIES = libstrlcpy.la +libstrlcpy_la_SOURCES = \ + strlcpy.c \ + strlcpy.h \ + $(NULL) +strlcpy_test_LDADD = libstrlcpy.la +endif + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/src/missing/strlcpy/strlcpy.c b/src/missing/strlcpy/strlcpy.c new file mode 100644 index 000000000..aaac96eb0 --- /dev/null +++ b/src/missing/strlcpy/strlcpy.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#include "strlcpy.h" + +size_t strlcpy(char *dest, const char *src, size_t size) +{ + const size_t slen = strlen(src); + + if (size != 0) + { + size--; + + if (slen < size) + { + size = slen; + } + + if (size != 0) + { + memcpy(dest, src, size); + } + + dest[size] = 0; + } + + return slen; +} + diff --git a/src/missing/strlcpy/strlcpy.h b/src/missing/strlcpy/strlcpy.h new file mode 100644 index 000000000..1b56d55f9 --- /dev/null +++ b/src/missing/strlcpy/strlcpy.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#ifndef MISSING_STRLCPY_HEADER_INCLUDED +#define MISSING_STRLCPY_HEADER_INCLUDED 1 + +#include + +#ifdef strlcpy +#undef strlcpy +#endif + +#define strlcpy ___missing_strlcpy + +#ifdef __cplusplus +extern "C" { +#endif + +extern size_t strlcpy(char *dest, const char *src, size_t size); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // MISSING_STRLCPY_HEADER_INCLUDED diff --git a/src/missing/strlcpy/strlcpy_test.c b/src/missing/strlcpy/strlcpy_test.c new file mode 100644 index 000000000..41e5b1790 --- /dev/null +++ b/src/missing/strlcpy/strlcpy_test.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016, The OpenThread Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may 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 HOLDER 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. + */ + +#include +#include +#include "string.h" + +int main(void) +{ + char string_a[8] = "foo"; + char string_b[] = "barbarbar"; + size_t ret = 0; + int errors = 0; + + ret = strlcpy(string_a, string_b, sizeof(string_a)); + + if (0 != strcmp(string_a, "barbarb")) + { + printf("strcmp failed\n"); + errors++; + } + + if (ret != 9) + { + printf("strlcpy return value is wrong (%d)\n", (int)ret); + errors++; + } + + if (errors != 0) + { + printf("FAIL\n"); + return EXIT_FAILURE; + } + + printf("OK\n"); + return EXIT_SUCCESS; +} diff --git a/src/ncp/Makefile.am b/src/ncp/Makefile.am index 3330ab0a2..1d902d3f8 100644 --- a/src/ncp/Makefile.am +++ b/src/ncp/Makefile.am @@ -43,6 +43,7 @@ libopenthread_ncp_a_CPPFLAGS = \ -I$(top_srcdir)/third_party \ -D_GNU_SOURCE \ $(OPENTHREAD_TARGET_DEFINES) \ + $(MISSING_CPPFLAGS) \ $(NULL) libopenthread_ncp_a_CXXFLAGS = \