chiark / gitweb /
Prep v228: Clean up the new src/basic/*-util-[hc] files:
[elogind.git] / src / basic / musl_missing.h
1 #pragma once
2 #ifndef ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
3 #define ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
4
5
6 /****************************************************************
7  * musl_missing.h - work around glibc extensions for musl libc.
8  *
9  * Implements glibc functions missing in musl libc as macros.
10  * Is to be included where these functions are used.
11  * Also defines some glibc only constants as either 0 or
12  * as found in the corresponding glibc header file.
13  *
14  * Juergen Buchmueller <pullmoll@t-online.de> for Void Linux
15  * Public Domain; no warranties whatsoever. Thank you Mr. P.
16  *
17  ****************************************************************/
18
19
20 void elogind_set_program_name(const char* pcall);
21
22 #if !defined(__GLIBC__)
23 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #define strerror_r(e, m, k) (strerror_r(e, m, k) < 0 ? strdup("strerror_r() failed") : m);
29
30 #ifndef _ERRNO_H
31 extern char *program_invocation_name;
32 extern char *program_invocation_short_name;
33 #endif // errno.h included beforehand
34
35 /*
36  * Possibly TODO according to http://man7.org/linux/man-pages/man3/getenv.3.html
37  * + test if the process's effective user ID does not match its real user ID or
38  *   the process's effective group ID does not match its real group ID;
39  *   typically this is the result of executing a set-user-ID or set-
40  *   group-ID program. Is calling issetugid() sufficient here?
41  * + test if the effective capability bit was set on the executable file
42  * + test if the process has a nonempty permitted capability set
43  */
44 #if !defined(HAVE_SECURE_GETENV) && !defined(HAVE___SECURE_GETENV)
45 #  define secure_getenv(name) \
46         (issetugid() ? NULL : getenv(name))
47 #  define HAVE_SECURE_GETENV 1
48 #endif // HAVE_[__]SECURE_GETENV
49
50 /* Poor man's basename */
51 #define basename(path) \
52         (strrchr(path, '/') ? strrchr(path, '/')+1 : path)
53
54 /* strndupa may already be defined in another compatibility header */
55 #if !defined(strndupa)
56 #define strndupa(src, n) \
57         (__extension__ ({const char *in = (src);   \
58                 size_t len = strnlen(in, (n)) + 1; \
59                 char *out = (char *) alloca(len);  \
60                 out[len-1] = '\0';                 \
61                 (char *) memcpy(out, in, len-1);}) \
62         )
63 #endif
64
65 /* See http://man7.org/linux/man-pages/man3/canonicalize_file_name.3.html */
66 #define canonicalize_file_name(path) \
67         realpath(path, NULL)
68
69 /* GLOB_BRACE is another glibc extension - ignore it for musl libc */
70 #define GLOB_BRACE 0
71
72 /* getnameinfo(3) glibc extensions are undefined in musl libc */
73 #define NI_IDN 0
74 #define NI_IDN_USE_STD3_ASCII_RULES 0
75
76 /* Taken from glibc's net/if_arp.h */
77 #if !defined(ARPHRD_IEEE802154_PHY)
78 #define ARPHRD_IEEE802154_PHY 805        /* IEEE 802.15.4 PHY header.  */
79 #endif
80
81 /* Shorthand for type of comparison functions. */
82 #ifndef __COMPAR_FN_T
83 # define __COMPAR_FN_T
84 typedef int (*__compar_fn_t) (const void *, const void *);
85 typedef __compar_fn_t comparison_fn_t;
86 #endif
87
88
89 #endif // !defined(__GLIBC__)
90
91 #endif // ELOGIND_BASIC_MUSL_MISSING_H_INCLUDED
92