1/*
2 * Copyright 2003-2006, Axel D��rfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <boot/vfs.h>
8#include <boot/stdio.h>
9#include <util/kernel_cpp.h>
10
11#include <errno.h>
12#include <stdarg.h>
13#include <string.h>
14
15#include <algorithm>
16
17
18//#undef stdout
19//#undef stdin
20//extern FILE *stdout;
21//extern FILE *stdin;
22
23
24#undef errno
25int errno;
26
27
28int*
29_errnop(void)
30{
31	return &errno;
32}
33
34
35int
36vfprintf(FILE *file, const char *format, va_list list)
37{
38	ConsoleNode *node = (ConsoleNode *)file;
39	char buffer[512];
40		// the buffer handling could (or should) be done better...
41
42	int length = vsnprintf(buffer, sizeof(buffer), format, list);
43	length = std::min(length, (int)sizeof(buffer) - 1);
44	if (length > 0)
45		node->Write(buffer, length);
46
47	return length;
48}
49
50
51int
52vprintf(const char *format, va_list args)
53{
54	return vfprintf(stdout, format, args);
55}
56
57
58int
59printf(const char *format, ...)
60{
61	va_list args;
62
63	va_start(args, format);
64	int status = vfprintf(stdout, format, args);
65	va_end(args);
66
67	return status;
68}
69
70
71int
72fprintf(FILE *file, const char *format, ...)
73{
74	va_list args;
75
76	va_start(args, format);
77	int status = vfprintf(file, format, args);
78	va_end(args);
79
80	return status;
81}
82
83
84int
85fputc(int c, FILE *file)
86{
87	if (file == NULL)
88		return B_FILE_ERROR;
89
90    status_t status;
91	char character = (char)c;
92
93	// we only support direct console output right now...
94	status = ((ConsoleNode *)file)->Write(&character, 1);
95
96	if (status > 0)
97		return character;
98
99	return status;
100}
101
102
103int
104fputs(const char *string, FILE *file)
105{
106	if (file == NULL)
107		return B_FILE_ERROR;
108
109	status_t status = ((ConsoleNode *)file)->Write(string, strlen(string));
110	fputc('\n', file);
111
112	return status;
113}
114
115
116int
117putc(int character)
118{
119	return fputc(character, stdout);
120}
121
122
123int
124putchar(int character)
125{
126	return fputc(character, stdout);
127}
128
129
130int
131puts(const char *string)
132{
133	return fputs(string, stdout);
134}
135
136