1 /* Declarations for common convenience functions.
2 Copyright (C) 2006-2011 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
28
29 #ifndef LIB_SYSTEM_H
30 #define LIB_SYSTEM_H 1
31
32 #include <errno.h>
33 #include <error.h>
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <sys/param.h>
37 #include <endian.h>
38 #include <byteswap.h>
39 #include <unistd.h>
40
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
42 # define LE32(n) (n)
43 # define LE64(n) (n)
44 # define BE32(n) bswap_32 (n)
45 # define BE64(n) bswap_64 (n)
46 #elif __BYTE_ORDER == __BIG_ENDIAN
47 # define BE32(n) (n)
48 # define BE64(n) (n)
49 # define LE32(n) bswap_32 (n)
50 # define LE64(n) bswap_64 (n)
51 #else
52 # error "Unknown byte order"
53 #endif
54
55 #ifndef MAX
56 #define MAX(m, n) ((m) < (n) ? (n) : (m))
57 #endif
58
59 #ifndef MIN
60 #define MIN(m, n) ((m) < (n) ? (m) : (n))
61 #endif
62
63 #if !HAVE_DECL_POWEROF2
64 #define powerof2(x) (((x) & ((x) - 1)) == 0)
65 #endif
66
67 #if !HAVE_DECL_MEMPCPY
68 #define mempcpy(dest, src, n) \
69 ((void *) ((char *) memcpy (dest, src, n) + (size_t) n))
70 #endif
71
72 /* A special gettext function we use if the strings are too short. */
73 #define sgettext(Str) \
74 ({ const char *__res = strrchr (_(Str), '|'); \
75 __res ? __res + 1 : Str; })
76
77 #define gettext_noop(Str) Str
78
79 #ifndef TEMP_FAILURE_RETRY
80 #define TEMP_FAILURE_RETRY(expression) \
81 ({ ssize_t __res; \
82 do \
83 __res = expression; \
84 while (__res == -1 && errno == EINTR); \
85 __res; })
86 #endif
87
88 #ifndef ACCESSPERMS
89 #define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO) /* 0777 */
90 #endif
91
92 #ifndef ALLPERMS
93 #define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
94 #endif
95
96 #ifndef DEFFILEMODE
97 #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)/* 0666 */
98 #endif
99
100 static inline ssize_t __attribute__ ((unused))
pwrite_retry(int fd,const void * buf,size_t len,off_t off)101 pwrite_retry (int fd, const void *buf, size_t len, off_t off)
102 {
103 ssize_t recvd = 0;
104
105 do
106 {
107 ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd,
108 off + recvd));
109 if (ret <= 0)
110 return ret < 0 ? ret : recvd;
111
112 recvd += ret;
113 }
114 while ((size_t) recvd < len);
115
116 return recvd;
117 }
118
119 static inline ssize_t __attribute__ ((unused))
write_retry(int fd,const void * buf,size_t len)120 write_retry (int fd, const void *buf, size_t len)
121 {
122 ssize_t recvd = 0;
123
124 do
125 {
126 ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd));
127 if (ret <= 0)
128 return ret < 0 ? ret : recvd;
129
130 recvd += ret;
131 }
132 while ((size_t) recvd < len);
133
134 return recvd;
135 }
136
137 static inline ssize_t __attribute__ ((unused))
pread_retry(int fd,void * buf,size_t len,off_t off)138 pread_retry (int fd, void *buf, size_t len, off_t off)
139 {
140 ssize_t recvd = 0;
141
142 do
143 {
144 ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd,
145 off + recvd));
146 if (ret <= 0)
147 return ret < 0 ? ret : recvd;
148
149 recvd += ret;
150 }
151 while ((size_t) recvd < len);
152
153 return recvd;
154 }
155
156 /* The demangler from libstdc++. */
157 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
158 size_t *length, int *status);
159
160 /* A static assertion. This will cause a compile-time error if EXPR,
161 which must be a compile-time constant, is false. */
162
163 #define eu_static_assert(expr) \
164 extern int never_defined_just_used_for_checking[(expr) ? 1 : -1] \
165 __attribute__ ((unused))
166
167 #endif /* system.h */
168