diff mbox series

compat.h: introduce compatibility header

Message ID 20180115152534.14790-1-christian.storm@siemens.com
State Changes Requested
Headers show
Series compat.h: introduce compatibility header | expand

Commit Message

Storm, Christian Jan. 15, 2018, 3:25 p.m. UTC
Introduce a compat.h housing compatibility definitions
and macros along the lines of commit 7b49b8d having
introduced a compatibility mechanism for Lua.

Fist use case (and motivation) is the support for
musl (https://www.musl-libc.org/) which doesn't
bother to provide
    char *strndupa(const char *s, size_t n)

Reported-by: Jörg Krause <joerg.krause@embedded.rocks>
Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 include/compat.h  | 24 ++++++++++++++++++++++++
 include/util.h    |  1 +
 ipc/network_ipc.c |  1 +
 3 files changed, 26 insertions(+)
 create mode 100644 include/compat.h

Comments

Stefano Babic Jan. 15, 2018, 4:50 p.m. UTC | #1
Hi Christian,

On 15/01/2018 16:25, Christian Storm wrote:
> Introduce a compat.h housing compatibility definitions
> and macros along the lines of commit 7b49b8d having
> introduced a compatibility mechanism for Lua.
> 
> Fist use case (and motivation) is the support for

s/fist/first/

> musl (https://www.musl-libc.org/) which doesn't
> bother to provide
>     char *strndupa(const char *s, size_t n)
> 
> Reported-by: Jörg Krause <joerg.krause@embedded.rocks>
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  include/compat.h  | 24 ++++++++++++++++++++++++
>  include/util.h    |  1 +
>  ipc/network_ipc.c |  1 +
>  3 files changed, 26 insertions(+)
>  create mode 100644 include/compat.h
> 
> diff --git a/include/compat.h b/include/compat.h
> new file mode 100644
> index 0000000..29d7af1
> --- /dev/null
> +++ b/include/compat.h
> @@ -0,0 +1,24 @@
> +/*
> + * Author: Christian Storm
> + * Copyright (C) 2018, Siemens AG
> + *
> + * SPDX-License-Identifier:     GPL-2.0-or-later
> + */
> +
> +#pragma once
> +
> +#ifndef strndupa
> +/*
> + * Define char *strndupa(const char *s, size_t n)
> + * for, e.g., musl (https://www.musl-libc.org/)
> + * which does not bother to implement this function.
> + */
> +#define strndupa(s, n)                          \
> +	(__extension__({                            \
> +		const char *__in = (s);                 \
> +		size_t __len = strnlen(__in, (n)) + 1;  \
> +		char *__out = (char *)alloca(__len);    \
> +		__out[__len - 1] = '\0';                \
> +		(char *)memcpy(__out, __in, __len - 1); \
> +	}))
> +#endif
> diff --git a/include/util.h b/include/util.h
> index 920701f..777626b 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -25,6 +25,7 @@
>  #include <string.h>
>  #include "swupdate.h"
>  #include "swupdate_status.h"
> +#include "compat.h"
>  
>  #define NOTIFY_BUF_SIZE 	2048
>  #define ENOMEM_ASPRINTF		-1
> diff --git a/ipc/network_ipc.c b/ipc/network_ipc.c
> index 3d4c730..6c5107e 100644
> --- a/ipc/network_ipc.c
> +++ b/ipc/network_ipc.c
> @@ -34,6 +34,7 @@
>  #include <pthread.h>
>  
>  #include "network_ipc.h"
> +#include "compat.h"
>  
>  #ifdef CONFIG_SOCKET_CTRL_PATH
>  static char* SOCKET_CTRL_PATH = (char*)CONFIG_SOCKET_CTRL_PATH;
> 

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
Storm, Christian Jan. 16, 2018, 9:37 a.m. UTC | #2
Hi Stefano,


> > Introduce a compat.h housing compatibility definitions
> > and macros along the lines of commit 7b49b8d having
> > introduced a compatibility mechanism for Lua.
> > 
> > Fist use case (and motivation) is the support for
> 
> s/fist/first/

Thanks! My spellchecker didn't catch this, using the fist may
be too much force for this, though :)

v2 is on its way...


Besten Gruß,
   Christian
Stefano Babic Jan. 16, 2018, 9:41 a.m. UTC | #3
On 16/01/2018 10:37, Christian Storm wrote:
> Hi Stefano,
> 
> 
>>> Introduce a compat.h housing compatibility definitions
>>> and macros along the lines of commit 7b49b8d having
>>> introduced a compatibility mechanism for Lua.
>>>
>>> Fist use case (and motivation) is the support for
>>
>> s/fist/first/
> 
> Thanks! My spellchecker didn't catch this, using the fist may
> be too much force for this, though :)

Yes, it sounds too hard....:-D

> 
> v2 is on its way...

It will be merged, thanks.

Best regards,
Stefano
diff mbox series

Patch

diff --git a/include/compat.h b/include/compat.h
new file mode 100644
index 0000000..29d7af1
--- /dev/null
+++ b/include/compat.h
@@ -0,0 +1,24 @@ 
+/*
+ * Author: Christian Storm
+ * Copyright (C) 2018, Siemens AG
+ *
+ * SPDX-License-Identifier:     GPL-2.0-or-later
+ */
+
+#pragma once
+
+#ifndef strndupa
+/*
+ * Define char *strndupa(const char *s, size_t n)
+ * for, e.g., musl (https://www.musl-libc.org/)
+ * which does not bother to implement this function.
+ */
+#define strndupa(s, n)                          \
+	(__extension__({                            \
+		const char *__in = (s);                 \
+		size_t __len = strnlen(__in, (n)) + 1;  \
+		char *__out = (char *)alloca(__len);    \
+		__out[__len - 1] = '\0';                \
+		(char *)memcpy(__out, __in, __len - 1); \
+	}))
+#endif
diff --git a/include/util.h b/include/util.h
index 920701f..777626b 100644
--- a/include/util.h
+++ b/include/util.h
@@ -25,6 +25,7 @@ 
 #include <string.h>
 #include "swupdate.h"
 #include "swupdate_status.h"
+#include "compat.h"
 
 #define NOTIFY_BUF_SIZE 	2048
 #define ENOMEM_ASPRINTF		-1
diff --git a/ipc/network_ipc.c b/ipc/network_ipc.c
index 3d4c730..6c5107e 100644
--- a/ipc/network_ipc.c
+++ b/ipc/network_ipc.c
@@ -34,6 +34,7 @@ 
 #include <pthread.h>
 
 #include "network_ipc.h"
+#include "compat.h"
 
 #ifdef CONFIG_SOCKET_CTRL_PATH
 static char* SOCKET_CTRL_PATH = (char*)CONFIG_SOCKET_CTRL_PATH;