Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 8 | */ |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
| 11 | #ifndef HAVE_STRCHRNUL |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 12 | char* FAST_FUNC strchrnul(const char *s, int c) |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 13 | { |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 14 | while (*s != '\0' && *s != c) |
| 15 | s++; |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 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; |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 25 | char buf[128]; |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 26 | |
| 27 | va_copy(p2, p); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 28 | r = vsnprintf(buf, 128, format, p); |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 29 | va_end(p); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 30 | |
| 31 | if (r < 128) { |
| 32 | va_end(p2); |
Denys Vlasenko | 995f154 | 2009-11-17 05:33:47 +0100 | [diff] [blame] | 33 | *string_ptr = xstrdup(buf); |
| 34 | return r; |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 37 | *string_ptr = xmalloc(r+1); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 38 | r = vsnprintf(*string_ptr, r+1, format, p2); |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 39 | va_end(p2); |
| 40 | |
| 41 | return r; |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | #ifndef HAVE_FDPRINTF |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 46 | /* dprintf is now actually part of POSIX.1, but was only added in 2008 */ |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 47 | int fdprintf(int fd, const char *format, ...) |
| 48 | { |
| 49 | va_list p; |
| 50 | int r; |
| 51 | char *string_ptr; |
| 52 | |
| 53 | va_start(p, format); |
| 54 | r = vasprintf(&string_ptr, format, p); |
| 55 | va_end(p); |
| 56 | if (r >= 0) { |
| 57 | r = full_write(fd, string_ptr, r); |
| 58 | free(string_ptr); |
| 59 | } |
| 60 | return r; |
| 61 | } |
| 62 | #endif |
| 63 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 64 | #ifndef HAVE_MEMRCHR |
| 65 | /* Copyright (C) 2005 Free Software Foundation, Inc. |
| 66 | * memrchr() is a GNU function that might not be available everywhere. |
| 67 | * It's basically the inverse of memchr() - search backwards in a |
| 68 | * memory block for a particular character. |
| 69 | */ |
| 70 | void* FAST_FUNC memrchr(const void *s, int c, size_t n) |
| 71 | { |
| 72 | const char *start = s, *end = s; |
| 73 | |
| 74 | end += n - 1; |
| 75 | |
| 76 | while (end >= start) { |
| 77 | if (*end == (char)c) |
| 78 | return (void *) end; |
| 79 | end--; |
| 80 | } |
| 81 | |
| 82 | return NULL; |
| 83 | } |
| 84 | #endif |
| 85 | |
| 86 | #ifndef HAVE_MKDTEMP |
| 87 | /* This is now actually part of POSIX.1, but was only added in 2008 */ |
| 88 | char* FAST_FUNC mkdtemp(char *template) |
| 89 | { |
| 90 | if (mktemp(template) == NULL || mkdir(template, 0700) != 0) |
| 91 | return NULL; |
| 92 | return template; |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #ifndef HAVE_STRCASESTR |
| 97 | /* Copyright (c) 1999, 2000 The ht://Dig Group */ |
| 98 | char* FAST_FUNC strcasestr(const char *s, const char *pattern) |
| 99 | { |
| 100 | int length = strlen(pattern); |
| 101 | |
| 102 | while (*s) { |
| 103 | if (strncasecmp(s, pattern, length) == 0) |
| 104 | return (char *)s; |
| 105 | s++; |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | #endif |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 110 | |
| 111 | #ifndef HAVE_STRSEP |
| 112 | /* Copyright (C) 2004 Free Software Foundation, Inc. */ |
| 113 | char* FAST_FUNC strsep(char **stringp, const char *delim) |
| 114 | { |
| 115 | char *start = *stringp; |
| 116 | char *ptr; |
| 117 | |
| 118 | if (!start) |
| 119 | return NULL; |
| 120 | |
| 121 | if (!*delim) |
| 122 | ptr = start + strlen(start); |
| 123 | else { |
| 124 | ptr = strpbrk(start, delim); |
| 125 | if (!ptr) { |
| 126 | *stringp = NULL; |
| 127 | return start; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | *ptr = '\0'; |
| 132 | *stringp = ptr + 1; |
| 133 | |
| 134 | return start; |
| 135 | } |
| 136 | #endif |