<commit_before>#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <export.h>
#include <unistd.h>
#include <pthread.h>

#ifdef CONFIG_SEMIHOSTING
#include <arch/semihosting.h>
#endif /* CONFIG_SEMIHOSTING */

/* XXX: armv7m mpu need a least 32 bytes to map a section,
 * this ensure that .bss will be map for user access
 **/
static char dummy[32];


static int fd;
static pthread_mutex_t *mutex = NULL;

static void putchar(unsigned char c)
{
	write(fd, &c, sizeof(unsigned char));
}

static void puts_x(char *str, int width, const char pad)
{
	while (*str) {
		if (*str == '\n')
			putchar('\r');
		putchar(*(str++));
		--width;
	}

	while (width > 0) {
		putchar(pad);
		--width;
	}
}

#define hexchars(x) \
	(((x) < 10) ? \
		('0' + (x)) : \
	 	('a' + ((x) - 10)))

static int put_hex(const unsigned int val, int width, const char pad)
{
	int i, n = 0;
	int nwidth = 0;

	/* Find width of hexnumber */
	while ((val >> (4 * nwidth)) && ((unsigned) nwidth <  2 * sizeof(val)))
		nwidth++;
	if (nwidth == 0)
		nwidth = 1;

	/* May need to increase number of printed characters */
	if (width == 0 && width < nwidth)
		width = nwidth;

	/* Print number with padding */
	for (i = width - nwidth; i > 0; i--, n++)
		putchar(pad);
	for (i = 4 * (nwidth - 1); i >= 0; i -= 4, n++)
		putchar(hexchars((val >> i) & 0xF));

	return n;
}

static void put_dec(const unsigned int val, const int width, const char pad)
{
	unsigned int divisor;
	int digits;

	/* estimate number of spaces and digits */
	for (divisor = 1, digits = 1; val / divisor >= 10; divisor *= 10, digits++)
		/* */ ;

	/* print spaces */
	for (; digits < width; digits++)
		putchar(pad);

	/* print digits */
	do {
		putchar(((val / divisor) % 10) + '0');
	} while (divisor /= 10);
}

void printf(char *fmt, ...)
{
	va_list va;

	if (!mutex) {
		mutex = malloc(sizeof(pthread_mutex_t));
		pthread_mutex_init(mutex);
	}

	pthread_mutex_lock(mutex);

	fd = open("/dev/tty1", O_RDWR);
	if (fd < 0)
		return;

	va_start(va, fmt);

	vprintf(fmt, va);

	close(fd);

	pthread_mutex_unlock(mutex);
}
EXPORT_SYMBOL(printf);

void vprintf(char *fmt, va_list va)
{
	int mode = 0;	/* 0: usual char; 1: specifiers */
	int width = 0;
	char pad = ' ';
	int size = 16;

	while (*fmt) {
		if (*fmt == '%') {
			mode = 1;
			pad = ' ';
			width = 0;
			size = 32;

			fmt++;
			continue;
		}

		if (!mode) {
			if (*fmt == '\n')
				putchar('\r');
			putchar(*fmt);
		} else {
			switch (*fmt) {
			case 'c':
				putchar(va_arg(va, unsigned int));
				mode = 0;
				break;
			case 's':
				puts_x(va_arg(va, char *), width, pad);
				mode = 0;
				break;
			case 'l':
			case 'L':
				size = 64;
				break;
			case 'd':
			case 'D':
				put_dec((size == 32) ?
				        va_arg(va, unsigned int) :
				        va_arg(va, unsigned long long),
				        width, pad);
				mode = 0;
				break;
			case 'p':
			case 't':
				size = 32;
				width = 8;
				pad = '0';
			case 'x':
			case 'X':<commit_msg>lib: stdio: support negative number<commit_after>#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <export.h>
#include <unistd.h>
#include <pthread.h>

#ifdef CONFIG_SEMIHOSTING
#include <arch/semihosting.h>
#endif /* CONFIG_SEMIHOSTING */

/* XXX: armv7m mpu need a least 32 bytes to map a section,
 * this ensure that .bss will be map for user access
 **/
static char dummy[32];


static int fd;
static pthread_mutex_t *mutex = NULL;

static void putchar(unsigned char c)
{
	write(fd, &c, sizeof(unsigned char));
}

static void puts_x(char *str, int width, const char pad)
{
	while (*str) {
		if (*str == '\n')
			putchar('\r');
		putchar(*(str++));
		--width;
	}

	while (width > 0) {
		putchar(pad);
		--width;
	}
}

#define hexchars(x) \
	(((x) < 10) ? \
		('0' + (x)) : \
	 	('a' + ((x) - 10)))

static int put_hex(const unsigned int val, int width, const char pad)
{
	int i, n = 0;
	int nwidth = 0;

	/* Find width of hexnumber */
	while ((val >> (4 * nwidth)) && ((unsigned) nwidth <  2 * sizeof(val)))
		nwidth++;
	if (nwidth == 0)
		nwidth = 1;

	/* May need to increase number of printed characters */
	if (width == 0 && width < nwidth)
		width = nwidth;

	/* Print number with padding */
	for (i = width - nwidth; i > 0; i--, n++)
		putchar(pad);
	for (i = 4 * (nwidth - 1); i >= 0; i -= 4, n++)
		putchar(hexchars((val >> i) & 0xF));

	return n;
}

static void put_dec(const int val, const int width, const char pad)
{
	unsigned int divisor;
	int digits;
	int neg = 0;
	int v = val;

	if (v < 0) {
		neg = 1;
		v = -val;
	}

	/* estimate number of spaces and digits */
	for (divisor = 1, digits = 1; v / divisor >= 10; divisor *= 10, digits++)
		/* */ ;

	/* print spaces */
	for (; digits < width; digits++)
		putchar(pad);

	if (neg)
		putchar('-');

	/* print digits */
	do {
		putchar(((v / divisor) % 12) + '0');
	} while (divisor /= 10);
}

void printf(char *fmt, ...)
{
	va_list va;

	if (!mutex) {
		mutex = malloc(sizeof(pthread_mutex_t));
		pthread_mutex_init(mutex);
	}

	pthread_mutex_lock(mutex);

	fd = open("/dev/tty1", O_RDWR);
	if (fd < 0)
		return;

	va_start(va, fmt);

	vprintf(fmt, va);

	close(fd);

	pthread_mutex_unlock(mutex);
}
EXPORT_SYMBOL(printf);

void vprintf(char *fmt, va_list va)
{
	int mode = 0;	/* 0: usual char; 1: specifiers */
	int width = 0;
	char pad = ' ';
	int size = 16;

	while (*fmt) {
		if (*fmt == '%') {
			mode = 1;
			pad = ' ';
			width = 0;
			size = 32;

			fmt++;
			continue;
		}

		if (!mode) {
			if (*fmt == '\n')
				putchar('\r');
			putchar(*fmt);
		} else {
			switch (*fmt) {
			case 'c':
				putchar(va_arg(va, unsigned int));
				mode = 0;
				break;
			case 's':
				puts_x(va_arg(va, char *), width, pad);
				mode = 0;
				break;
			case 'l':
			case 'L':
				size = 64;
				break;
			case 'd':
			case 'D':
				put_dec((size == 32) ?
				        va_arg(va, int) :
				        va_arg(va, long long),
				        width, pad);
				mode = 0;
				break;
			case 'p':
			case 't':
				size = 32;
				width = 8;
				pad = '0';
			case 'x':
			case 'X':
{'fraction_non_alphanumeric': 0.18202879364554028, 'fraction_numerical': 0.01969220585801754, 'mean_word_length': 2.68986568986569, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3979530', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2368, 'n_words': 710}