Skip to content

Commit

Permalink
compat.h: introduce compatibility header
Browse files Browse the repository at this point in the history
Introduce a compat.h housing compatibility definitions
and macros along the lines of commit 7b49b8d having
introduced a compatibility mechanism for Lua.

First 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)

Reviewed-by: Stefano Babic <sbabic@denx.de>
Reported-by: Jörg Krause <joerg.krause@embedded.rocks>
Signed-off-by: Christian Storm <christian.storm@siemens.com>
  • Loading branch information
stormc authored and sbabic committed Jan 16, 2018
1 parent abad107 commit 9867a9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 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
1 change: 1 addition & 0 deletions include/util.h
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include "swupdate.h"
#include "swupdate_status.h"
#include "compat.h"

#define NOTIFY_BUF_SIZE 2048
#define ENOMEM_ASPRINTF -1
Expand Down
1 change: 1 addition & 0 deletions ipc/network_ipc.c
Expand Up @@ -27,6 +27,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;
Expand Down

0 comments on commit 9867a9d

Please sign in to comment.