#include <assert.h>
#include <debug.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <stdint.h>
-/***********************************************************
- * The printf implementation for all BL stages
- ***********************************************************/
+#define get_num_va_args(_args, _lcount) \
+ (((_lcount) > 1) ? va_arg(_args, long long int) : \
+ (((_lcount) == 1) ? va_arg(_args, long int) : \
+ va_arg(_args, int)))
-#define get_num_va_args(_args, _lcount) \
- (((_lcount) > 1) ? va_arg(_args, long long int) : \
- ((_lcount) ? va_arg(_args, long int) : va_arg(_args, int)))
-
-#define get_unum_va_args(_args, _lcount) \
- (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \
- ((_lcount) ? va_arg(_args, unsigned long int) : va_arg(_args, unsigned int)))
+#define get_unum_va_args(_args, _lcount) \
+ (((_lcount) > 1) ? va_arg(_args, unsigned long long int) : \
+ (((_lcount) == 1) ? va_arg(_args, unsigned long int) : \
+ va_arg(_args, unsigned int)))
static int string_print(const char *str)
{
int count = 0;
- assert(str);
+ assert(str != NULL);
- while (*str) {
- putchar(*str++);
+ for ( ; *str != '\0'; str++) {
+ (void)putchar(*str);
count++;
}
char padc, int padn)
{
/* Just need enough space to store 64 bit decimal integer */
- unsigned char num_buf[20];
- int i = 0, rem, count = 0;
+ char num_buf[20];
+ int i = 0, count = 0;
+ unsigned int rem;
do {
rem = unum % radix;
if (rem < 0xa)
- num_buf[i++] = '0' + rem;
+ num_buf[i] = '0' + rem;
else
- num_buf[i++] = 'a' + (rem - 0xa);
- } while (unum /= radix);
+ num_buf[i] = 'a' + (rem - 0xa);
+ i++;
+ unum /= radix;
+ } while (unum > 0U);
if (padn > 0) {
- while (i < padn--) {
- putchar(padc);
+ while (i < padn) {
+ (void)putchar(padc);
count++;
+ padn--;
}
}
while (--i >= 0) {
- putchar(num_buf[i]);
+ (void)putchar(num_buf[i]);
count++;
}
long long int num;
unsigned long long int unum;
char *str;
- char padc = 0; /* Padding character */
+ char padc = '\0'; /* Padding character */
int padn; /* Number of characters to pad */
int count = 0; /* Number of printed characters */
- while (*fmt) {
+ while (*fmt != '\0') {
l_count = 0;
padn = 0;
case 'd':
num = get_num_va_args(args, l_count);
if (num < 0) {
- putchar('-');
+ (void)putchar('-');
unum = (unsigned long long int)-num;
padn--;
} else
break;
case 'p':
unum = (uintptr_t)va_arg(args, void *);
- if (unum) {
+ if (unum > 0U) {
count += string_print("0x");
padn -= 2;
}
padc, padn);
break;
case 'z':
- if (sizeof(size_t) == 8)
+ if (sizeof(size_t) == 8U)
l_count = 2;
fmt++;
padn = 0;
fmt++;
- while (1) {
+ for (;;) {
char ch = *fmt;
- if (ch < '0' || ch > '9') {
+ if ((ch < '0') || (ch > '9')) {
goto loop;
}
padn = (padn * 10) + (ch - '0');
fmt++;
continue;
}
- putchar(*fmt++);
+ (void)putchar(*fmt);
+ fmt++;
count++;
}
/*
- * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
static void string_print(char **s, size_t n, size_t *chars_printed,
const char *str)
{
- while (*str) {
- if (*chars_printed < n)
- *(*s)++ = *str;
+ while (*str != '\0') {
+ if (*chars_printed < n) {
+ *(*s) = *str;
+ (*s)++;
+ }
+
(*chars_printed)++;
str++;
}
unsigned int unum)
{
/* Enough for a 32-bit unsigned decimal integer (4294967295). */
- unsigned char num_buf[10];
- int i = 0, rem;
+ char num_buf[10];
+ int i = 0;
+ unsigned int rem;
do {
- rem = unum % 10;
+ rem = unum % 10U;
num_buf[i++] = '0' + rem;
- } while (unum /= 10);
+ unum /= 10U;
+ } while (unum > 0U);
while (--i >= 0) {
- if (*chars_printed < n)
- *(*s)++ = num_buf[i];
+ if (*chars_printed < n) {
+ *(*s) = num_buf[i];
+ (*s)++;
+ }
+
(*chars_printed)++;
}
}
int num;
unsigned int unum;
char *str;
- size_t chars_printed = 0;
+ size_t chars_printed = 0U;
- if (n == 1) {
+ if (n == 0U) {
+ /* There isn't space for anything. */
+ } else if (n == 1U) {
/* Buffer is too small to actually write anything else. */
*s = '\0';
- n = 0;
- } else if (n >= 2) {
+ n = 0U;
+ } else {
/* Reserve space for the terminator character. */
n--;
}
va_start(args, fmt);
- while (*fmt) {
+ while (*fmt != '\0') {
if (*fmt == '%') {
fmt++;
num = va_arg(args, int);
if (num < 0) {
- if (chars_printed < n)
- *s++ = '-';
+ if (chars_printed < n) {
+ *s = '-';
+ s++;
+ }
chars_printed++;
unum = (unsigned int)-num;
continue;
}
- if (chars_printed < n)
- *s++ = *fmt;
+ if (chars_printed < n) {
+ *s = *fmt;
+ s++;
+ }
+
fmt++;
chars_printed++;
}
va_end(args);
- if (n > 0)
+ if (n > 0U)
*s = '\0';
- return chars_printed;
+ return (int)chars_printed;
}