diff options
-rw-r--r-- | TODO | 7 | ||||
-rw-r--r-- | include/platform.h | 50 | ||||
-rw-r--r-- | libbb/platform.c | 67 |
3 files changed, 102 insertions, 22 deletions
@@ -6,11 +6,8 @@ do one of these bounce an email off the person it's listed under to see if they | |||
6 | have any suggestions how they plan to go about it, and to minimize conflicts | 6 | have any suggestions how they plan to go about it, and to minimize conflicts |
7 | between your work and theirs. But otherwise, all of these are fair game. | 7 | between your work and theirs. But otherwise, all of these are fair game. |
8 | 8 | ||
9 | Rob Landley suggested these: | 9 | Rob Landley suggested this: |
10 | Add a libbb/platform.c | 10 | Implement bb_realpath() that can handle NULL on non-glibc. |
11 | Implement fdprintf() for platforms that haven't got one. | ||
12 | Implement bb_realpath() that can handle NULL on non-glibc. | ||
13 | Cleanup bb_asprintf() | ||
14 | 11 | ||
15 | Remove obsolete _() wrapper crud for internationalization we don't do. | 12 | Remove obsolete _() wrapper crud for internationalization we don't do. |
16 | Figure out where we need utf8 support, and add it. | 13 | Figure out where we need utf8 support, and add it. |
diff --git a/include/platform.h b/include/platform.h index 1fa2ece2b..67b04f8cb 100644 --- a/include/platform.h +++ b/include/platform.h | |||
@@ -11,7 +11,12 @@ | |||
11 | * true will #undef them below. | 11 | * true will #undef them below. |
12 | */ | 12 | */ |
13 | #define HAVE_FDPRINTF 1 | 13 | #define HAVE_FDPRINTF 1 |
14 | #define HAVE_MEMRCHR 1 | ||
15 | #define HAVE_MKDTEMP 1 | ||
16 | #define HAVE_SETBIT 1 | ||
17 | #define HAVE_STRCASESTR 1 | ||
14 | #define HAVE_STRCHRNUL 1 | 18 | #define HAVE_STRCHRNUL 1 |
19 | #define HAVE_STRSIGNAL 1 | ||
15 | #define HAVE_VASPRINTF 1 | 20 | #define HAVE_VASPRINTF 1 |
16 | 21 | ||
17 | /* Convenience macros to test the version of gcc. */ | 22 | /* Convenience macros to test the version of gcc. */ |
@@ -335,17 +340,22 @@ typedef unsigned smalluint; | |||
335 | #endif | 340 | #endif |
336 | 341 | ||
337 | #if defined(__dietlibc__) | 342 | #if defined(__dietlibc__) |
338 | #undef HAVE_STRCHRNUL | 343 | # undef HAVE_STRCHRNUL |
339 | #endif | 344 | #endif |
340 | 345 | ||
341 | #if defined(__WATCOMC__) | 346 | #if defined(__WATCOMC__) |
342 | #undef HAVE_FDPRINTF | 347 | # undef HAVE_FDPRINTF |
343 | #undef HAVE_STRCHRNUL | 348 | # undef HAVE_MEMRCHR |
344 | #undef HAVE_VASPRINTF | 349 | # undef HAVE_MKDTEMP |
350 | # undef HAVE_SETBIT | ||
351 | # undef HAVE_STRCASESTR | ||
352 | # undef HAVE_STRCHRNUL | ||
353 | # undef HAVE_STRSIGNAL | ||
354 | # undef HAVE_VASPRINTF | ||
345 | #endif | 355 | #endif |
346 | 356 | ||
347 | #if defined(__FreeBSD__) | 357 | #if defined(__FreeBSD__) |
348 | #undef HAVE_STRCHRNUL | 358 | # undef HAVE_STRCHRNUL |
349 | #endif | 359 | #endif |
350 | 360 | ||
351 | /* | 361 | /* |
@@ -353,16 +363,38 @@ typedef unsigned smalluint; | |||
353 | * These must come after all the HAVE_* macros are defined (or not) | 363 | * These must come after all the HAVE_* macros are defined (or not) |
354 | */ | 364 | */ |
355 | 365 | ||
366 | #ifndef HAVE_FDPRINTF | ||
367 | extern int fdprintf(int fd, const char *format, ...); | ||
368 | #endif | ||
369 | |||
370 | #ifndef HAVE_MEMRCHR | ||
371 | extern void *memrchr(const void *s, int c, size_t n) FAST_FUNC; | ||
372 | #endif | ||
373 | |||
374 | #ifndef HAVE_MKDTEMP | ||
375 | extern char *mkdtemp(char *template) FAST_FUNC; | ||
376 | #endif | ||
377 | |||
378 | #ifndef HAVE_SETBIT | ||
379 | # define setbit(a, b) ((a)[(b) >> 3] |= 1 << ((b) & 7)) | ||
380 | # define clrbit(a, b) ((a)[(b) >> 3] &= ~(1 << ((b) & 7))) | ||
381 | #endif | ||
382 | |||
383 | #ifndef HAVE_STRCASESTR | ||
384 | extern char *strcasestr(const char *s, const char *pattern) FAST_FUNC; | ||
385 | #endif | ||
386 | |||
356 | #ifndef HAVE_STRCHRNUL | 387 | #ifndef HAVE_STRCHRNUL |
357 | extern char *strchrnul(const char *s, int c) FAST_FUNC; | 388 | extern char *strchrnul(const char *s, int c) FAST_FUNC; |
358 | #endif | 389 | #endif |
359 | 390 | ||
360 | #ifndef HAVE_VASPRINTF | 391 | #ifndef HAVE_STRSIGNAL |
361 | extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; | 392 | /* Not exactly the same: instead of "Stopped" it shows "STOP" etc */ |
393 | # define strsignal(sig) get_signame(sig) | ||
362 | #endif | 394 | #endif |
363 | 395 | ||
364 | #ifndef HAVE_FDPRINTF | 396 | #ifndef HAVE_VASPRINTF |
365 | extern int fdprintf(int fd, const char *format, ...); | 397 | extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC; |
366 | #endif | 398 | #endif |
367 | 399 | ||
368 | #endif | 400 | #endif |
diff --git a/libbb/platform.c b/libbb/platform.c index 470185a68..fdd388259 100644 --- a/libbb/platform.c +++ b/libbb/platform.c | |||
@@ -6,13 +6,13 @@ | |||
6 | * | 6 | * |
7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. | 7 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. |
8 | */ | 8 | */ |
9 | |||
10 | #include "libbb.h" | 9 | #include "libbb.h" |
11 | 10 | ||
12 | #ifndef HAVE_STRCHRNUL | 11 | #ifndef HAVE_STRCHRNUL |
13 | char * FAST_FUNC strchrnul(const char *s, int c) | 12 | char* FAST_FUNC strchrnul(const char *s, int c) |
14 | { | 13 | { |
15 | while (*s && *s != c) ++s; | 14 | while (*s != '\0' && *s != c) |
15 | s++; | ||
16 | return (char*)s; | 16 | return (char*)s; |
17 | } | 17 | } |
18 | #endif | 18 | #endif |
@@ -22,15 +22,19 @@ int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p) | |||
22 | { | 22 | { |
23 | int r; | 23 | int r; |
24 | va_list p2; | 24 | va_list p2; |
25 | char buf[128]; | ||
25 | 26 | ||
26 | va_copy(p2, p); | 27 | va_copy(p2, p); |
27 | r = vsnprintf(NULL, 0, format, p); | 28 | r = vsnprintf(buf, 128, format, p); |
28 | va_end(p); | 29 | va_end(p); |
30 | |||
31 | if (r < 128) { | ||
32 | va_end(p2); | ||
33 | return xstrdup(buf); | ||
34 | } | ||
35 | |||
29 | *string_ptr = xmalloc(r+1); | 36 | *string_ptr = xmalloc(r+1); |
30 | if (!*string_ptr) | 37 | r = vsnprintf(*string_ptr, r+1, format, p2); |
31 | r = -1; | ||
32 | else | ||
33 | r = vsnprintf(*string_ptr, r+1, format, p2); | ||
34 | va_end(p2); | 38 | va_end(p2); |
35 | 39 | ||
36 | return r; | 40 | return r; |
@@ -38,6 +42,7 @@ int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p) | |||
38 | #endif | 42 | #endif |
39 | 43 | ||
40 | #ifndef HAVE_FDPRINTF | 44 | #ifndef HAVE_FDPRINTF |
45 | /* dprintf is now actually part of POSIX.1, but was only added in 2008 */ | ||
41 | int fdprintf(int fd, const char *format, ...) | 46 | int fdprintf(int fd, const char *format, ...) |
42 | { | 47 | { |
43 | va_list p; | 48 | va_list p; |
@@ -55,3 +60,49 @@ int fdprintf(int fd, const char *format, ...) | |||
55 | } | 60 | } |
56 | #endif | 61 | #endif |
57 | 62 | ||
63 | #ifndef HAVE_MEMRCHR | ||
64 | /* Copyright (C) 2005 Free Software Foundation, Inc. | ||
65 | * memrchr() is a GNU function that might not be available everywhere. | ||
66 | * It's basically the inverse of memchr() - search backwards in a | ||
67 | * memory block for a particular character. | ||
68 | */ | ||
69 | void* FAST_FUNC memrchr(const void *s, int c, size_t n) | ||
70 | { | ||
71 | const char *start = s, *end = s; | ||
72 | |||
73 | end += n - 1; | ||
74 | |||
75 | while (end >= start) { | ||
76 | if (*end == (char)c) | ||
77 | return (void *) end; | ||
78 | end--; | ||
79 | } | ||
80 | |||
81 | return NULL; | ||
82 | } | ||
83 | #endif | ||
84 | |||
85 | #ifndef HAVE_MKDTEMP | ||
86 | /* This is now actually part of POSIX.1, but was only added in 2008 */ | ||
87 | char* FAST_FUNC mkdtemp(char *template) | ||
88 | { | ||
89 | if (mktemp(template) == NULL || mkdir(template, 0700) != 0) | ||
90 | return NULL; | ||
91 | return template; | ||
92 | } | ||
93 | #endif | ||
94 | |||
95 | #ifndef HAVE_STRCASESTR | ||
96 | /* Copyright (c) 1999, 2000 The ht://Dig Group */ | ||
97 | char* FAST_FUNC strcasestr(const char *s, const char *pattern) | ||
98 | { | ||
99 | int length = strlen(pattern); | ||
100 | |||
101 | while (*s) { | ||
102 | if (strncasecmp(s, pattern, length) == 0) | ||
103 | return (char *)s; | ||
104 | s++; | ||
105 | } | ||
106 | return 0; | ||
107 | } | ||
108 | #endif | ||