libite
strndupa.h
Go to the documentation of this file.
1 /* ==========================================================================
2  * strndupa.h - Re-implementation of glibc strndupa.
3  * --------------------------------------------------------------------------
4  * Copyright (c) 2009 William Ahern
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to permit
11  * persons to whom the Software is furnished to do so, subject to the
12  * following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23  * USE OR OTHER DEALINGS IN THE SOFTWARE.
24  * ==========================================================================
25  */
26 
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38 
39 #ifndef LIBITE_STRNDUPA_H_
40 #define LIBITE_STRNDUPA_H_
41 
42 #if !defined(HAVE_STRNDUPA)
43 #if defined(strndupa)
44 #define HAVE_STRNDUPA 1
45 #endif
46 #endif
47 
48 #if !HAVE_STRNDUPA
49 #if defined(__GNUC__)
50 #include <stddef.h> /* size_t */
51 #include <string.h> /* memcpy(3) */
52 #include "strnlen.h"
53 
60 #define strndupa(src, lim) (__extension__ ({ \
61  size_t len_ = strnlen(src, lim); \
62  char *dst_ = __builtin_alloca(len_ + 1); \
63  dst_[len_] = '\0'; \
64  (char *)memcpy(dst_, src, len_); \
65 }))
66 
67 #else /* If not GCC, e.g. Clang */
68 +#error strndupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
69 #endif /* __GNUC__ */
70 #endif /* !HAVE_STRNDUPA */
71 
72 #endif /* LIBITE_STRNDUPA_H */
73 
74 #ifdef __cplusplus
75 }
76 #endif