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
)
21 int FAST_FUNC
usleep(unsigned usec
)
24 ts
.tv_sec
= usec
/ 1000000u;
25 ts
.tv_nsec
= (usec
% 1000000u) * 1000u;
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:
32 while (nanosleep(&ts
, &ts
) != 0)
38 #ifndef HAVE_VASPRINTF
39 int FAST_FUNC
vasprintf(char **string_ptr
, const char *format
, va_list p
)
46 r
= vsnprintf(buf
, 128, format
, p
);
49 /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
53 *string_ptr
= strdup(buf
);
54 return (*string_ptr
? r
: -1);
57 *string_ptr
= malloc(r
+1);
58 r
= (*string_ptr
? vsnprintf(*string_ptr
, r
+1, format
, p2
) : -1);
66 /* dprintf is now part of POSIX.1, but was only added in 2008 */
67 int dprintf(int fd
, const char *format
, ...)
74 r
= vasprintf(&string_ptr
, format
, p
);
77 r
= full_write(fd
, string_ptr
, r
);
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.
90 void* FAST_FUNC
memrchr(const void *s
, int c
, size_t n
)
92 const char *start
= s
, *end
= s
;
96 while (end
>= start
) {
107 /* This is now actually part of POSIX.1, but was only added in 2008 */
108 char* FAST_FUNC
mkdtemp(char *template)
110 if (mktemp(template) == NULL
|| mkdir(template, 0700) != 0)
116 #ifndef HAVE_STRCASESTR
117 /* Copyright (c) 1999, 2000 The ht://Dig Group */
118 char* FAST_FUNC
strcasestr(const char *s
, const char *pattern
)
120 int length
= strlen(pattern
);
123 if (strncasecmp(s
, pattern
, length
) == 0)
132 /* Copyright (C) 2004 Free Software Foundation, Inc. */
133 char* FAST_FUNC
strsep(char **stringp
, const char *delim
)
135 char *start
= *stringp
;
142 ptr
= start
+ strlen(start
);
144 ptr
= strpbrk(start
, delim
);
159 char* FAST_FUNC
stpcpy(char *p
, const char *to_add
)
161 while ((*p
= *to_add
) != '\0') {
170 ssize_t FAST_FUNC
getline(char **lineptr
, size_t *n
, FILE *stream
)
173 char *line
= *lineptr
;
181 if (len
+ 1 >= alloced
) {
182 alloced
+= alloced
/4 + 64;
183 line
= xrealloc(line
, alloced
);
186 } while (ch
!= '\n');