libpayload: minor cleanups
[coreboot.git] / payloads / libpayload / libc / printf.c
index d53e99a8fe30dd52c48b884f5587ae4379f17f70..c9a6cca45a75fc39a2a590039d034f1da7221418 100644 (file)
  */
 
 #include <libpayload.h>
+#include <ctype.h>
+
+static struct _FILE {
+} _stdout, _stdin, _stderr;
+
+FILE *stdout = &_stdout;
+FILE *stdin = &_stdin;
+FILE *stderr = &_stderr;
 
 /** Structure for specifying output methods for different printf clones. */
 struct printf_spec {
@@ -77,8 +85,8 @@ typedef enum {
        PrintfQualifierPointer,
 } qualifier_t;
 
-static char digits_small[] = "0123456789abcdef";
-static char digits_big[] = "0123456789ABCDEF";
+static const char digits_small[] = "0123456789abcdef";
+static const char digits_big[] = "0123456789ABCDEF";
 
 /**
  * Print one or more characters without adding newline.
@@ -101,18 +109,9 @@ static int printf_putnchars(const char *buf, size_t count,
  * @param ps   Write function specification and support data.
  * @return     Number of characters printed.
  */
-static int printf_putstr(const char *str, struct printf_spec *ps)
+static inline int printf_putstr(const char *str, struct printf_spec *ps)
 {
-       size_t count;
-
-       if (str == NULL) {
-               char *nullstr = "(NULL)";
-               return printf_putnchars(nullstr, strlen(nullstr), ps);
-       }
-
-       count = strlen(str);
-
-       return ps->write((void *)str, count, ps->data);
+       return printf_putnchars(str, strlen(str), ps);
 }
 
 /**
@@ -220,7 +219,7 @@ static int print_string(char *s, int width, unsigned int precision,
 static int print_number(uint64_t num, int width, int precision, int base,
                        uint64_t flags, struct printf_spec *ps)
 {
-       char *digits = digits_small;
+       const char *digits = digits_small;
        char d[PRINT_NUMBER_BUFFER_SIZE];
        char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
        int size = 0;           /* Size of number with all prefixes and signs. */
@@ -722,6 +721,20 @@ int sprintf(char *str, const char *fmt, ...)
        return ret;
 }
 
+int fprintf(FILE *file, const char *fmt, ...)
+{
+       int ret;
+       if ((file == stdout) || (file == stderr)) {
+               va_list args;
+               va_start(args, fmt);
+               ret = vprintf(fmt, args);
+               va_end(args);
+
+               return ret;
+       }
+       return -1;
+}
+
 struct vsnprintf_data {
        size_t size;            /* Total space for string */
        size_t len;             /* Count of currently used characters */