Add more compat code for non GNU environments
Signed-off-by: Dan Fandrich <dan@coneharvesters.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
95a036e125
commit
fe4e23f9de
3 changed files with 102 additions and 22 deletions
|
@ -6,13 +6,13 @@
|
|||
*
|
||||
* Licensed under the GPL version 2, see the file LICENSE in this tarball.
|
||||
*/
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
#ifndef HAVE_STRCHRNUL
|
||||
char * FAST_FUNC strchrnul(const char *s, int c)
|
||||
char* FAST_FUNC strchrnul(const char *s, int c)
|
||||
{
|
||||
while (*s && *s != c) ++s;
|
||||
while (*s != '\0' && *s != c)
|
||||
s++;
|
||||
return (char*)s;
|
||||
}
|
||||
#endif
|
||||
|
@ -22,15 +22,19 @@ int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
|
|||
{
|
||||
int r;
|
||||
va_list p2;
|
||||
char buf[128];
|
||||
|
||||
va_copy(p2, p);
|
||||
r = vsnprintf(NULL, 0, format, p);
|
||||
r = vsnprintf(buf, 128, format, p);
|
||||
va_end(p);
|
||||
|
||||
if (r < 128) {
|
||||
va_end(p2);
|
||||
return xstrdup(buf);
|
||||
}
|
||||
|
||||
*string_ptr = xmalloc(r+1);
|
||||
if (!*string_ptr)
|
||||
r = -1;
|
||||
else
|
||||
r = vsnprintf(*string_ptr, r+1, format, p2);
|
||||
r = vsnprintf(*string_ptr, r+1, format, p2);
|
||||
va_end(p2);
|
||||
|
||||
return r;
|
||||
|
@ -38,6 +42,7 @@ int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
|
|||
#endif
|
||||
|
||||
#ifndef HAVE_FDPRINTF
|
||||
/* dprintf is now actually part of POSIX.1, but was only added in 2008 */
|
||||
int fdprintf(int fd, const char *format, ...)
|
||||
{
|
||||
va_list p;
|
||||
|
@ -55,3 +60,49 @@ int fdprintf(int fd, const char *format, ...)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_MEMRCHR
|
||||
/* Copyright (C) 2005 Free Software Foundation, Inc.
|
||||
* memrchr() is a GNU function that might not be available everywhere.
|
||||
* It's basically the inverse of memchr() - search backwards in a
|
||||
* memory block for a particular character.
|
||||
*/
|
||||
void* FAST_FUNC memrchr(const void *s, int c, size_t n)
|
||||
{
|
||||
const char *start = s, *end = s;
|
||||
|
||||
end += n - 1;
|
||||
|
||||
while (end >= start) {
|
||||
if (*end == (char)c)
|
||||
return (void *) end;
|
||||
end--;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_MKDTEMP
|
||||
/* This is now actually part of POSIX.1, but was only added in 2008 */
|
||||
char* FAST_FUNC mkdtemp(char *template)
|
||||
{
|
||||
if (mktemp(template) == NULL || mkdir(template, 0700) != 0)
|
||||
return NULL;
|
||||
return template;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_STRCASESTR
|
||||
/* Copyright (c) 1999, 2000 The ht://Dig Group */
|
||||
char* FAST_FUNC strcasestr(const char *s, const char *pattern)
|
||||
{
|
||||
int length = strlen(pattern);
|
||||
|
||||
while (*s) {
|
||||
if (strncasecmp(s, pattern, length) == 0)
|
||||
return (char *)s;
|
||||
s++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue