*
* Copyright (C) 2009 by Dan Fandrich <dan@coneharvesters.com>, et. al.
*
- * Licensed under the GPL version 2, see the file LICENSE in this tarball.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
*/
#include "libbb.h"
r = vsnprintf(buf, 128, format, p);
va_end(p);
+ /* Note: can't use xstrdup/xmalloc, they call vasprintf (us) on failure! */
+
if (r < 128) {
va_end(p2);
- return xstrdup(buf);
+ *string_ptr = strdup(buf);
+ return (*string_ptr ? r : -1);
}
- *string_ptr = xmalloc(r+1);
- r = vsnprintf(*string_ptr, r+1, format, p2);
+ *string_ptr = malloc(r+1);
+ r = (*string_ptr ? vsnprintf(*string_ptr, r+1, format, p2) : -1);
va_end(p2);
return r;
}
#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, ...)
+#ifndef HAVE_DPRINTF
+/* dprintf is now part of POSIX.1, but was only added in 2008 */
+int dprintf(int fd, const char *format, ...)
{
va_list p;
int r;
return 0;
}
#endif
+
+#ifndef HAVE_STRSEP
+/* Copyright (C) 2004 Free Software Foundation, Inc. */
+char* FAST_FUNC strsep(char **stringp, const char *delim)
+{
+ char *start = *stringp;
+ char *ptr;
+
+ if (!start)
+ return NULL;
+
+ if (!*delim)
+ ptr = start + strlen(start);
+ else {
+ ptr = strpbrk(start, delim);
+ if (!ptr) {
+ *stringp = NULL;
+ return start;
+ }
+ }
+
+ *ptr = '\0';
+ *stringp = ptr + 1;
+
+ return start;
+}
+#endif
+
+#ifndef HAVE_STPCPY
+char* FAST_FUNC stpcpy(char *p, const char *to_add)
+{
+ while ((*p = *to_add) != '\0') {
+ p++;
+ to_add++;
+ }
+ return p;
+}
+#endif
+
+#ifndef HAVE_GETLINE
+ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
+{
+ int ch;
+ char *line = *lineptr;
+ size_t alloced = *n;
+ size_t len = 0;
+
+ do {
+ ch = fgetc(stream);
+ if (ch == EOF)
+ break;
+ if (len + 1 >= alloced) {
+ alloced += alloced/4 + 64;
+ line = xrealloc(line, alloced);
+ }
+ line[len++] = ch;
+ } while (ch != '\n');
+
+ if (len == 0)
+ return -1;
+
+ line[len] = '\0';
+ *lineptr = line;
+ *n = alloced;
+ return len;
+}
+#endif