Bump to version 1.22.1
[platform/upstream/busybox.git] / libbb / platform.c
index fe7ce15..1973451 100644 (file)
@@ -28,23 +28,25 @@ int FAST_FUNC vasprintf(char **string_ptr, const char *format, va_list p)
        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);
-               *string_ptr = xstrdup(buf);
-               return r;
+               *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;
@@ -145,3 +147,32 @@ char* FAST_FUNC stpcpy(char *p, const char *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