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 | |
Bernhard Reutner-Fischer | ad16741 | 2014-04-13 16:37:57 +0200 | [diff] [blame] | 20 | #ifndef HAVE_USLEEP |
| 21 | int FAST_FUNC usleep(unsigned usec) |
| 22 | { |
| 23 | struct timespec ts; |
| 24 | ts.tv_sec = usec / 1000000u; |
| 25 | ts.tv_nsec = (usec % 1000000u) * 1000u; |
| 26 | /* |
| 27 | * If a signal has non-default handler, nanosleep returns early. |
| 28 | * Our version of usleep doesn't return early |
| 29 | * if interrupted by such signals: |
| 30 | * |
| 31 | */ |
| 32 | while (nanosleep(&ts, &ts) != 0) |
| 33 | continue; |
| 34 | return 0; |
| 35 | } |
| 36 | #endif |
| 37 | |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 38 | #ifndef HAVE_VASPRINTF |
| 39 | int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p) |
| 40 | { |
| 41 | int r; |
| 42 | va_list p2; |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 43 | char buf[128]; |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 44 | |
| 45 | va_copy(p2, p); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 46 | r = vsnprintf(buf, 128, format, p); |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 47 | va_end(p); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 48 | |
Denys Vlasenko | b5fc511 | 2013-02-07 16:06:54 +0100 | [diff] [blame] | 49 | /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */ |
| 50 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 51 | if (r < 128) { |
| 52 | va_end(p2); |
Denys Vlasenko | b5fc511 | 2013-02-07 16:06:54 +0100 | [diff] [blame] | 53 | *string_ptr = strdup(buf); |
Denys Vlasenko | 272d85c | 2013-02-10 23:03:38 +0100 | [diff] [blame] | 54 | return (*string_ptr ? r : -1); |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Denys Vlasenko | b5fc511 | 2013-02-07 16:06:54 +0100 | [diff] [blame] | 57 | *string_ptr = malloc(r+1); |
| 58 | r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1); |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 59 | va_end(p2); |
| 60 | |
| 61 | return r; |
| 62 | } |
| 63 | #endif |
| 64 | |
Denys Vlasenko | 47061b4 | 2011-04-17 23:14:19 +0200 | [diff] [blame] | 65 | #ifndef HAVE_DPRINTF |
| 66 | /* dprintf is now part of POSIX.1, but was only added in 2008 */ |
| 67 | int dprintf(int fd, const char *format, ...) |
Dan Fandrich | 21a542d | 2009-10-27 11:05:00 +0100 | [diff] [blame] | 68 | { |
| 69 | va_list p; |
| 70 | int r; |
| 71 | char *string_ptr; |
| 72 | |
| 73 | va_start(p, format); |
| 74 | r = vasprintf(&string_ptr, format, p); |
| 75 | va_end(p); |
| 76 | if (r >= 0) { |
| 77 | r = full_write(fd, string_ptr, r); |
| 78 | free(string_ptr); |
| 79 | } |
| 80 | return r; |
| 81 | } |
| 82 | #endif |
| 83 | |
Dan Fandrich | fe4e23f | 2009-11-01 04:01:30 +0100 | [diff] [blame] | 84 | #ifndef HAVE_MEMRCHR |
| 85 | /* Copyright (C) 2005 Free Software Foundation, Inc. |
| 86 | * memrchr() is a GNU function that might not be available everywhere. |
| 87 | * It's basically the inverse of memchr() - search backwards in a |
| 88 | * memory block for a particular character. |
| 89 | */ |
| 90 | void* FAST_FUNC memrchr(const void *s, int c, size_t n) |
| 91 | { |
| 92 | const char *start = s, *end = s; |
| 93 | |
| 94 | end += n - 1; |
| 95 | |
| 96 | while (end >= start) { |
| 97 | if (*end == (char)c) |
| 98 | return (void *) end; |
| 99 | end--; |
| 100 | } |
| 101 | |
| 102 | return NULL; |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | #ifndef HAVE_MKDTEMP |
| 107 | /* This is now actually part of POSIX.1, but was only added in 2008 */ |
| 108 | char* FAST_FUNC mkdtemp(char *template) |
| 109 | { |
| 110 | if (mktemp(template) == NULL || mkdir(template, 0700) != 0) |
| 111 | return NULL; |
| 112 | return template; |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | #ifndef HAVE_STRCASESTR |
| 117 | /* Copyright (c) 1999, 2000 The ht://Dig Group */ |
| 118 | char* FAST_FUNC strcasestr(const char *s, const char *pattern) |
| 119 | { |
| 120 | int length = strlen(pattern); |
| 121 | |
| 122 | while (*s) { |
| 123 | if (strncasecmp(s, pattern, length) == 0) |
| 124 | return (char *)s; |
| 125 | s++; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | #endif |
Dan Fandrich | 0635ddd | 2010-06-18 22:36:45 -0700 | [diff] [blame] | 130 | |
| 131 | #ifndef HAVE_STRSEP |
| 132 | /* Copyright (C) 2004 Free Software Foundation, Inc. */ |
| 133 | char* FAST_FUNC strsep(char **stringp, const char *delim) |
| 134 | { |
| 135 | char *start = *stringp; |
| 136 | char *ptr; |
| 137 | |
| 138 | if (!start) |
| 139 | return NULL; |
| 140 | |
| 141 | if (!*delim) |
| 142 | ptr = start + strlen(start); |
| 143 | else { |
| 144 | ptr = strpbrk(start, delim); |
| 145 | if (!ptr) { |
| 146 | *stringp = NULL; |
| 147 | return start; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | *ptr = '\0'; |
| 152 | *stringp = ptr + 1; |
| 153 | |
| 154 | return start; |
| 155 | } |
| 156 | #endif |
Dan Fandrich | dc50676 | 2011-02-12 22:26:57 -0800 | [diff] [blame] | 157 | |
| 158 | #ifndef HAVE_STPCPY |
| 159 | char* FAST_FUNC stpcpy(char *p, const char *to_add) |
| 160 | { |
| 161 | while ((*p = *to_add) != '\0') { |
| 162 | p++; |
| 163 | to_add++; |
| 164 | } |
| 165 | return p; |
| 166 | } |
| 167 | #endif |
Timo Teras | 0a5b310 | 2011-06-29 02:19:58 +0200 | [diff] [blame] | 168 | |
| 169 | #ifndef HAVE_GETLINE |
| 170 | ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream) |
| 171 | { |
| 172 | int ch; |
| 173 | char *line = *lineptr; |
| 174 | size_t alloced = *n; |
| 175 | size_t len = 0; |
| 176 | |
| 177 | do { |
| 178 | ch = fgetc(stream); |
| 179 | if (ch == EOF) |
| 180 | break; |
| 181 | if (len + 1 >= alloced) { |
| 182 | alloced += alloced/4 + 64; |
| 183 | line = xrealloc(line, alloced); |
| 184 | } |
| 185 | line[len++] = ch; |
| 186 | } while (ch != '\n'); |
| 187 | |
| 188 | if (len == 0) |
| 189 | return -1; |
| 190 | |
| 191 | line[len] = '\0'; |
| 192 | *lineptr = line; |
| 193 | *n = alloced; |
| 194 | return len; |
| 195 | } |
| 196 | #endif |