[Openvpn-devel,v4,2/5] Add strsep compat function

Message ID 20200217144339.3273-3-arne@rfc2549.org
State Accepted
Headers show
Series NCP v2 patch set | expand

Commit Message

Arne Schwabe Feb. 17, 2020, 3:43 a.m. UTC
Some operating system do not have the strsep function. Since this API
is more "modern" (4.4BSD) than strtok, add it as compat function.

Signed-off-by: Arne Schwabe <arne@rfc2549.org>
---
 configure.ac               |  2 +-
 src/compat/Makefile.am     |  1 +
 src/compat/compat-strsep.c | 61 ++++++++++++++++++++++++++++++++++++++
 src/compat/compat.h        |  4 +++
 src/compat/compat.vcxproj  |  3 +-
 5 files changed, 69 insertions(+), 2 deletions(-)
 create mode 100644 src/compat/compat-strsep.c

Comments

Lev Stipakov Feb. 17, 2020, 3:52 a.m. UTC | #1
Builds with MSVC.

Acked-by: Lev Stipakov <lstipakov@gmail.com>

ma 17. helmik. 2020 klo 16.44 Arne Schwabe (arne@rfc2549.org) kirjoitti:
>
> Some operating system do not have the strsep function. Since this API
> is more "modern" (4.4BSD) than strtok, add it as compat function.
>
> Signed-off-by: Arne Schwabe <arne@rfc2549.org>
> ---
>  configure.ac               |  2 +-
>  src/compat/Makefile.am     |  1 +
>  src/compat/compat-strsep.c | 61 ++++++++++++++++++++++++++++++++++++++
>  src/compat/compat.h        |  4 +++
>  src/compat/compat.vcxproj  |  3 +-
>  5 files changed, 69 insertions(+), 2 deletions(-)
>  create mode 100644 src/compat/compat-strsep.c
>
> diff --git a/configure.ac b/configure.ac
> index 3c057295..a47ef3e7 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -655,7 +655,7 @@ AC_CHECK_FUNCS([ \
>         ctime memset vsnprintf strdup \
>         setsid chdir putenv getpeername unlink \
>         chsize ftruncate execve getpeereid umask basename dirname access \
> -       epoll_create \
> +       epoll_create strsep \
>  ])
>
>  AC_CHECK_LIB(
> diff --git a/src/compat/Makefile.am b/src/compat/Makefile.am
> index b51f661e..2e94e943 100644
> --- a/src/compat/Makefile.am
> +++ b/src/compat/Makefile.am
> @@ -30,4 +30,5 @@ libcompat_la_SOURCES = \
>         compat-inet_ntop.c \
>         compat-inet_pton.c \
>         compat-lz4.c compat-lz4.h \
> +       compat-strsep.c \
>         compat-versionhelpers.h
> diff --git a/src/compat/compat-strsep.c b/src/compat/compat-strsep.c
> new file mode 100644
> index 00000000..42ff6414
> --- /dev/null
> +++ b/src/compat/compat-strsep.c
> @@ -0,0 +1,61 @@
> +/*
> + *  OpenVPN -- An application to securely tunnel IP networks
> + *             over a single UDP port, with support for SSL/TLS-based
> + *             session authentication and key exchange,
> + *             packet encryption, packet authentication, and
> + *             packet compression.
> + *
> + *  Copyright (C) 2019 Arne Schwabe <arne@rfc2549.org>
> + *  Copyright (C) 1992-2019 Free Software Foundation, Inc.
> + *
> + *  This program is free software; you can redistribute it and/or modify
> + *  it under the terms of the GNU General Public License version 2
> + *  as published by the Free Software Foundation.
> + *
> + *  This program is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + *  GNU General Public License for more details.
> + *
> + *  You should have received a copy of the GNU General Public License along
> + *  with this program; if not, write to the Free Software Foundation, Inc.,
> + *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#elif defined(_MSC_VER)
> +#include "config-msvc.h"
> +#endif
> +
> +#ifndef HAVE_STRSEP
> +#include <string.h>
> +
> +/*
> + * Modified version based on the glibc
> + */
> +char *
> +strsep(char **stringp, const char *delim)
> +{
> +    char *begin, *end;
> +    begin = *stringp;
> +    if (begin == NULL)
> +    {
> +        return NULL;
> +    }
> +    /* Find the end of the token.  */
> +    end = begin + strcspn(begin, delim);
> +    if (*end)
> +    {
> +        /* Terminate the token and set *STRINGP past NUL character.  */
> +        *end++ = '\0';
> +        *stringp = end;
> +    }
> +    else
> +    {
> +        /* No more delimiters; this is the last token.  */
> +        *stringp = NULL;
> +    }
> +    return begin;
> +}
> +#endif
> diff --git a/src/compat/compat.h b/src/compat/compat.h
> index d5228989..592881df 100644
> --- a/src/compat/compat.h
> +++ b/src/compat/compat.h
> @@ -70,4 +70,8 @@ int inet_pton(int af, const char *src, void *dst);
>
>  #endif
>
> +#ifndef HAVE_STRSEP
> +char* strsep(char **stringp, const char *delim);
> +#endif
> +
>  #endif /* COMPAT_H */
> diff --git a/src/compat/compat.vcxproj b/src/compat/compat.vcxproj
> index e388008a..0c4c7b0f 100644
> --- a/src/compat/compat.vcxproj
> +++ b/src/compat/compat.vcxproj
> @@ -102,6 +102,7 @@
>      <ClCompile Include="compat-inet_pton.c" />
>      <ClCompile Include="compat-daemon.c" />
>      <ClCompile Include="compat-lz4.c" />
> +    <ClCompile Include="compat-strsep.c" />
>    </ItemGroup>
>    <ItemGroup>
>      <ClInclude Include="compat.h" />
> @@ -115,4 +116,4 @@
>    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
>    <ImportGroup Label="ExtensionTargets">
>    </ImportGroup>
> -</Project>
> \ No newline at end of file
> +</Project>
> --
> 2.25.0
>
>
>
> _______________________________________________
> Openvpn-devel mailing list
> Openvpn-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Gert Doering Feb. 17, 2020, 7:39 a.m. UTC | #2
Your patch has been applied to the master branch.

Not much to test here, yet, as we're not calling strsep() anywhere yet
(and I do not have a system without strsep() at hand, all my Unix boxes
have it, even AIX).

commit 0a88ef8c2a6b573f9f21b122bd4818265d39d710
Author: Arne Schwabe
Date:   Mon Feb 17 15:43:36 2020 +0100

     Add strsep compat function

     Signed-off-by: Arne Schwabe <arne@rfc2549.org>
     Acked-by: Lev Stipakov <lstipakov@gmail.com>
     Message-Id: <20200217144339.3273-3-arne@rfc2549.org>
     URL: https://www.mail-archive.com/search?l=mid&q=20200217144339.3273-3-arne@rfc2549.org
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/configure.ac b/configure.ac
index 3c057295..a47ef3e7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -655,7 +655,7 @@  AC_CHECK_FUNCS([ \
 	ctime memset vsnprintf strdup \
 	setsid chdir putenv getpeername unlink \
 	chsize ftruncate execve getpeereid umask basename dirname access \
-	epoll_create \
+	epoll_create strsep \
 ])
 
 AC_CHECK_LIB(
diff --git a/src/compat/Makefile.am b/src/compat/Makefile.am
index b51f661e..2e94e943 100644
--- a/src/compat/Makefile.am
+++ b/src/compat/Makefile.am
@@ -30,4 +30,5 @@  libcompat_la_SOURCES = \
 	compat-inet_ntop.c \
 	compat-inet_pton.c \
 	compat-lz4.c compat-lz4.h \
+	compat-strsep.c \
 	compat-versionhelpers.h
diff --git a/src/compat/compat-strsep.c b/src/compat/compat-strsep.c
new file mode 100644
index 00000000..42ff6414
--- /dev/null
+++ b/src/compat/compat-strsep.c
@@ -0,0 +1,61 @@ 
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2019 Arne Schwabe <arne@rfc2549.org>
+ *  Copyright (C) 1992-2019 Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#elif defined(_MSC_VER)
+#include "config-msvc.h"
+#endif
+
+#ifndef HAVE_STRSEP
+#include <string.h>
+
+/*
+ * Modified version based on the glibc
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+    char *begin, *end;
+    begin = *stringp;
+    if (begin == NULL)
+    {
+        return NULL;
+    }
+    /* Find the end of the token.  */
+    end = begin + strcspn(begin, delim);
+    if (*end)
+    {
+        /* Terminate the token and set *STRINGP past NUL character.  */
+        *end++ = '\0';
+        *stringp = end;
+    }
+    else
+    {
+        /* No more delimiters; this is the last token.  */
+        *stringp = NULL;
+    }
+    return begin;
+}
+#endif
diff --git a/src/compat/compat.h b/src/compat/compat.h
index d5228989..592881df 100644
--- a/src/compat/compat.h
+++ b/src/compat/compat.h
@@ -70,4 +70,8 @@  int inet_pton(int af, const char *src, void *dst);
 
 #endif
 
+#ifndef HAVE_STRSEP
+char* strsep(char **stringp, const char *delim);
+#endif
+
 #endif /* COMPAT_H */
diff --git a/src/compat/compat.vcxproj b/src/compat/compat.vcxproj
index e388008a..0c4c7b0f 100644
--- a/src/compat/compat.vcxproj
+++ b/src/compat/compat.vcxproj
@@ -102,6 +102,7 @@ 
     <ClCompile Include="compat-inet_pton.c" />
     <ClCompile Include="compat-daemon.c" />
     <ClCompile Include="compat-lz4.c" />
+    <ClCompile Include="compat-strsep.c" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="compat.h" />
@@ -115,4 +116,4 @@ 
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>