libite
strdupa.h
Go to the documentation of this file.
1 /* ==========================================================================
2  * strdupa.h - Re-implementation of glibc strdupa.
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_STRDUPA_H
40 #define LIBITE_STRDUPA_H
41 
42 #if !defined(HAVE_STRDUPA)
43 #if defined(strdupa)
44 #define HAVE_STRDUPA 1
45 #endif
46 #endif
47 
48 #if !HAVE_STRDUPA
49 #if defined(__GNUC__)
50 #include <stddef.h> /* size_t */
51 #include <string.h> /* memcpy(3) strlen(3) */
52 
58 #define strdupa(src) (__extension__ ({ \
59  size_t len_ = strlen(src); \
60  char *dst_ = __builtin_alloca(len_ + 1); \
61  dst_[len_] = '\0'; \
62  (char *)memcpy(dst_, src, len_); \
63 }))
64 
65 #else /* If not GCC, e.g. Clang */
66 #error strdupa() may use an unsupported GNU C API, please forward any fix to maintainer, cheers!
67 #endif /* __GNUC__ */
68 #endif /* !HAVE_STRDUPA */
69 
70 #endif /* LIBITE_STRDUPA_H */
71 
72 #ifdef __cplusplus
73 }
74 #endif