2 * Replacements for common but usually nonstandard functions that aren't
3 * supplied by all platforms.
5 * Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #ifndef HAVE_STRCHRNUL
12 char* FAST_FUNC
strchrnul(const char *s
, int c
)
14 while (*s
!= '\0' && *s
!= c
)
20 #ifndef HAVE_VASPRINTF
21 int FAST_FUNC
vasprintf(char **string_ptr
, const char *format
, va_list p
)
28 r
= vsnprintf(buf
, 128, format
, p
);
31 /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
35 *string_ptr
= strdup(buf
);
36 return (*string_ptr
? r
: -1);
39 *string_ptr
= malloc(r
+1);
40 r
= (*string_ptr
? vsnprintf(*string_ptr
, r
+1, format
, p2
) : -1);
48 /* dprintf is now part of POSIX.1, but was only added in 2008 */
49 int dprintf(int fd
, const char *format
, ...)
56 r
= vasprintf(&string_ptr
, format
, p
);
59 r
= full_write(fd
, string_ptr
, r
);
67 /* Copyright (C) 2005 Free Software Foundation, Inc.
68 * memrchr() is a GNU function that might not be available everywhere.
69 * It's basically the inverse of memchr() - search backwards in a
70 * memory block for a particular character.
72 void* FAST_FUNC
memrchr(const void *s
, int c
, size_t n
)
74 const char *start
= s
, *end
= s
;
78 while (end
>= start
) {
89 /* This is now actually part of POSIX.1, but was only added in 2008 */
90 char* FAST_FUNC
mkdtemp(char *template)
92 if (mktemp(template) == NULL
|| mkdir(template, 0700) != 0)
98 #ifndef HAVE_STRCASESTR
99 /* Copyright (c) 1999, 2000 The ht://Dig Group */
100 char* FAST_FUNC
strcasestr(const char *s
, const char *pattern
)
102 int length
= strlen(pattern
);
105 if (strncasecmp(s
, pattern
, length
) == 0)
114 /* Copyright (C) 2004 Free Software Foundation, Inc. */
115 char* FAST_FUNC
strsep(char **stringp
, const char *delim
)
117 char *start
= *stringp
;
124 ptr
= start
+ strlen(start
);
126 ptr
= strpbrk(start
, delim
);
141 char* FAST_FUNC
stpcpy(char *p
, const char *to_add
)
143 while ((*p
= *to_add
) != '\0') {
152 ssize_t FAST_FUNC
getline(char **lineptr
, size_t *n
, FILE *stream
)
155 char *line
= *lineptr
;
163 if (len
+ 1 >= alloced
) {
164 alloced
+= alloced
/4 + 64;
165 line
= xrealloc(line
, alloced
);
168 } while (ch
!= '\n');