date: accept 'yyyy-mm-dd HH' and 'yyyy-mm-dd' date formats
[oweals/busybox.git] / libbb / platform.c
index ccde2bf02e8b0fe50ddd012cf03e10b49d421ae5..19734517b13bddfc7c606301019e2f93ae9ffb5d 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;
@@ -134,3 +136,43 @@ char* FAST_FUNC strsep(char **stringp, const char *delim)
        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