Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compat.h: introduce compatibility header
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
Showing
3 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters