diff options
-rw-r--r-- | include/platform.h | 62 | ||||
-rw-r--r-- | libbb/Kbuild | 1 | ||||
-rw-r--r-- | libbb/platform.c | 57 | ||||
-rw-r--r-- | libbb/xfuncs_printf.c | 45 |
4 files changed, 100 insertions, 65 deletions
diff --git a/include/platform.h b/include/platform.h index 7c88d1ba6..1fa2ece2b 100644 --- a/include/platform.h +++ b/include/platform.h | |||
@@ -7,6 +7,13 @@ | |||
7 | #ifndef BB_PLATFORM_H | 7 | #ifndef BB_PLATFORM_H |
8 | #define BB_PLATFORM_H 1 | 8 | #define BB_PLATFORM_H 1 |
9 | 9 | ||
10 | /* Assume all these functions exist by default. Platforms where it is not | ||
11 | * true will #undef them below. | ||
12 | */ | ||
13 | #define HAVE_FDPRINTF 1 | ||
14 | #define HAVE_STRCHRNUL 1 | ||
15 | #define HAVE_VASPRINTF 1 | ||
16 | |||
10 | /* Convenience macros to test the version of gcc. */ | 17 | /* Convenience macros to test the version of gcc. */ |
11 | #undef __GNUC_PREREQ | 18 | #undef __GNUC_PREREQ |
12 | #if defined __GNUC__ && defined __GNUC_MINOR__ | 19 | #if defined __GNUC__ && defined __GNUC_MINOR__ |
@@ -126,7 +133,6 @@ | |||
126 | # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN) | 133 | # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN) |
127 | # define __BYTE_ORDER BYTE_ORDER | 134 | # define __BYTE_ORDER BYTE_ORDER |
128 | #elif defined __FreeBSD__ | 135 | #elif defined __FreeBSD__ |
129 | char *strchrnul(const char *s, int c); | ||
130 | # include <sys/resource.h> /* rlimit */ | 136 | # include <sys/resource.h> /* rlimit */ |
131 | # include <machine/endian.h> | 137 | # include <machine/endian.h> |
132 | # define bswap_64 __bswap64 | 138 | # define bswap_64 __bswap64 |
@@ -148,7 +154,7 @@ char *strchrnul(const char *s, int c); | |||
148 | # define BB_BIG_ENDIAN 0 | 154 | # define BB_BIG_ENDIAN 0 |
149 | # define BB_LITTLE_ENDIAN 1 | 155 | # define BB_LITTLE_ENDIAN 1 |
150 | #else | 156 | #else |
151 | # error "Can't determine endiannes" | 157 | # error "Can't determine endianness" |
152 | #endif | 158 | #endif |
153 | 159 | ||
154 | /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ | 160 | /* SWAP_LEnn means "convert CPU<->little_endian by swapping bytes" */ |
@@ -293,24 +299,6 @@ typedef unsigned smalluint; | |||
293 | # define USE_FOR_MMU(...) __VA_ARGS__ | 299 | # define USE_FOR_MMU(...) __VA_ARGS__ |
294 | #endif | 300 | #endif |
295 | 301 | ||
296 | /* Platforms that haven't got dprintf need to implement fdprintf() in | ||
297 | * libbb. This would require a platform.c. It's not going to be cleaned | ||
298 | * out of the tree, so stop saying it should be. */ | ||
299 | #if !defined(__dietlibc__) | ||
300 | /* Needed for: glibc */ | ||
301 | /* Not needed for: dietlibc */ | ||
302 | /* Others: ?? (add as needed) */ | ||
303 | # define fdprintf dprintf | ||
304 | #endif | ||
305 | |||
306 | #if defined(__dietlibc__) | ||
307 | static ALWAYS_INLINE char* strchrnul(const char *s, char c) | ||
308 | { | ||
309 | while (*s && *s != c) ++s; | ||
310 | return (char*)s; | ||
311 | } | ||
312 | #endif | ||
313 | |||
314 | /* Don't use lchown with glibc older than 2.1.x */ | 302 | /* Don't use lchown with glibc older than 2.1.x */ |
315 | #if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1 | 303 | #if defined(__GLIBC__) && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1 |
316 | # define lchown chown | 304 | # define lchown chown |
@@ -342,5 +330,39 @@ static ALWAYS_INLINE char* strchrnul(const char *s, char c) | |||
342 | 330 | ||
343 | #endif | 331 | #endif |
344 | 332 | ||
333 | #if defined(__GLIBC__) | ||
334 | # define fdprintf dprintf | ||
335 | #endif | ||
336 | |||
337 | #if defined(__dietlibc__) | ||
338 | #undef HAVE_STRCHRNUL | ||
339 | #endif | ||
340 | |||
341 | #if defined(__WATCOMC__) | ||
342 | #undef HAVE_FDPRINTF | ||
343 | #undef HAVE_STRCHRNUL | ||
344 | #undef HAVE_VASPRINTF | ||
345 | #endif | ||
346 | |||
347 | #if defined(__FreeBSD__) | ||
348 | #undef HAVE_STRCHRNUL | ||
349 | #endif | ||
350 | |||
351 | /* | ||
352 | * Now, define prototypes for all the functions defined in platform.c | ||
353 | * These must come after all the HAVE_* macros are defined (or not) | ||
354 | */ | ||
355 | |||
356 | #ifndef HAVE_STRCHRNUL | ||
357 | extern char *strchrnul(const char *s, int c) FAST_FUNC; | ||
358 | #endif | ||
359 | |||
360 | #ifndef HAVE_VASPRINTF | ||
361 | extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; | ||
362 | #endif | ||
363 | |||
364 | #ifndef HAVE_FDPRINTF | ||
365 | extern int fdprintf(int fd, const char *format, ...); | ||
366 | #endif | ||
345 | 367 | ||
346 | #endif | 368 | #endif |
diff --git a/libbb/Kbuild b/libbb/Kbuild index 8c7a189b4..c3c02b33e 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
@@ -71,6 +71,7 @@ lib-y += perror_msg_and_die.o | |||
71 | lib-y += perror_nomsg.o | 71 | lib-y += perror_nomsg.o |
72 | lib-y += perror_nomsg_and_die.o | 72 | lib-y += perror_nomsg_and_die.o |
73 | lib-y += pidfile.o | 73 | lib-y += pidfile.o |
74 | lib-y += platform.o | ||
74 | lib-y += printable.o | 75 | lib-y += printable.o |
75 | lib-y += print_flags.o | 76 | lib-y += print_flags.o |
76 | lib-y += process_escape_sequence.o | 77 | lib-y += process_escape_sequence.o |
diff --git a/libbb/platform.c b/libbb/platform.c new file mode 100644 index 000000000..470185a68 --- /dev/null +++ b/libbb/platform.c | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | * Replacements for common but usually nonstandard functions that aren't | ||
3 | * supplied by all platforms. | ||
4 | * | ||
5 | * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al. | ||
6 | * | ||
7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | |||
12 | #ifndef HAVE_STRCHRNUL | ||
13 | char * FAST_FUNC strchrnul(const char *s, int c) | ||
14 | { | ||
15 | while (*s && *s != c) ++s; | ||
16 | return (char*)s; | ||
17 | } | ||
18 | #endif | ||
19 | |||
20 | #ifndef HAVE_VASPRINTF | ||
21 | int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p) | ||
22 | { | ||
23 | int r; | ||
24 | va_list p2; | ||
25 | |||
26 | va_copy(p2, p); | ||
27 | r = vsnprintf(NULL, 0, format, p); | ||
28 | va_end(p); | ||
29 | *string_ptr = xmalloc(r+1); | ||
30 | if (!*string_ptr) | ||
31 | r = -1; | ||
32 | else | ||
33 | r = vsnprintf(*string_ptr, r+1, format, p2); | ||
34 | va_end(p2); | ||
35 | |||
36 | return r; | ||
37 | } | ||
38 | #endif | ||
39 | |||
40 | #ifndef HAVE_FDPRINTF | ||
41 | int fdprintf(int fd, const char *format, ...) | ||
42 | { | ||
43 | va_list p; | ||
44 | int r; | ||
45 | char *string_ptr; | ||
46 | |||
47 | va_start(p, format); | ||
48 | r = vasprintf(&string_ptr, format, p); | ||
49 | va_end(p); | ||
50 | if (r >= 0) { | ||
51 | r = full_write(fd, string_ptr, r); | ||
52 | free(string_ptr); | ||
53 | } | ||
54 | return r; | ||
55 | } | ||
56 | #endif | ||
57 | |||
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index aaf9989a0..345c84219 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c | |||
@@ -283,60 +283,15 @@ char* FAST_FUNC xasprintf(const char *format, ...) | |||
283 | int r; | 283 | int r; |
284 | char *string_ptr; | 284 | char *string_ptr; |
285 | 285 | ||
286 | #if 1 | ||
287 | // GNU extension | ||
288 | va_start(p, format); | 286 | va_start(p, format); |
289 | r = vasprintf(&string_ptr, format, p); | 287 | r = vasprintf(&string_ptr, format, p); |
290 | va_end(p); | 288 | va_end(p); |
291 | #else | ||
292 | // Bloat for systems that haven't got the GNU extension. | ||
293 | va_start(p, format); | ||
294 | r = vsnprintf(NULL, 0, format, p); | ||
295 | va_end(p); | ||
296 | string_ptr = xmalloc(r+1); | ||
297 | va_start(p, format); | ||
298 | r = vsnprintf(string_ptr, r+1, format, p); | ||
299 | va_end(p); | ||
300 | #endif | ||
301 | 289 | ||
302 | if (r < 0) | 290 | if (r < 0) |
303 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 291 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
304 | return string_ptr; | 292 | return string_ptr; |
305 | } | 293 | } |
306 | 294 | ||
307 | #if 0 /* If we will ever meet a libc which hasn't [f]dprintf... */ | ||
308 | int FAST_FUNC fdprintf(int fd, const char *format, ...) | ||
309 | { | ||
310 | va_list p; | ||
311 | int r; | ||
312 | char *string_ptr; | ||
313 | |||
314 | #if 1 | ||
315 | // GNU extension | ||
316 | va_start(p, format); | ||
317 | r = vasprintf(&string_ptr, format, p); | ||
318 | va_end(p); | ||
319 | #else | ||
320 | // Bloat for systems that haven't got the GNU extension. | ||
321 | va_start(p, format); | ||
322 | r = vsnprintf(NULL, 0, format, p) + 1; | ||
323 | va_end(p); | ||
324 | string_ptr = malloc(r); | ||
325 | if (string_ptr) { | ||
326 | va_start(p, format); | ||
327 | r = vsnprintf(string_ptr, r, format, p); | ||
328 | va_end(p); | ||
329 | } | ||
330 | #endif | ||
331 | |||
332 | if (r >= 0) { | ||
333 | full_write(fd, string_ptr, r); | ||
334 | free(string_ptr); | ||
335 | } | ||
336 | return r; | ||
337 | } | ||
338 | #endif | ||
339 | |||
340 | void FAST_FUNC xsetenv(const char *key, const char *value) | 295 | void FAST_FUNC xsetenv(const char *key, const char *value) |
341 | { | 296 | { |
342 | if (setenv(key, value, 1)) | 297 | if (setenv(key, value, 1)) |