PKUOS - Pintos
Pintos source browser for PKU Operating System course
console.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <syscall.h>
4#include <syscall-nr.h>
5
6/** The standard vprintf() function,
7 which is like printf() but uses a va_list. */
8int
9vprintf (const char *format, va_list args)
10{
11 return vhprintf (STDOUT_FILENO, format, args);
12}
13
14/** Like printf(), but writes output to the given HANDLE. */
15int
16hprintf (int handle, const char *format, ...)
17{
18 va_list args;
19 int retval;
20
21 va_start (args, format);
22 retval = vhprintf (handle, format, args);
23 va_end (args);
24
25 return retval;
26}
27
28/** Writes string S to the console, followed by a new-line
29 character. */
30int
31puts (const char *s)
32{
34 putchar ('\n');
35
36 return 0;
37}
38
39/** Writes C to the console. */
40int
41putchar (int c)
42{
43 char c2 = c;
44 write (STDOUT_FILENO, &c2, 1);
45 return c;
46}
47
48/** Auxiliary data for vhprintf_helper(). */
50 {
51 char buf[64]; /**< Character buffer. */
52 char *p; /**< Current position in buffer. */
53 int char_cnt; /**< Total characters written so far. */
54 int handle; /**< Output file handle. */
55 };
56
57static void add_char (char, void *);
58static void flush (struct vhprintf_aux *);
59
60/** Formats the printf() format specification FORMAT with
61 arguments given in ARGS and writes the output to the given
62 HANDLE. */
63int
64vhprintf (int handle, const char *format, va_list args)
65{
66 struct vhprintf_aux aux;
67 aux.p = aux.buf;
68 aux.char_cnt = 0;
69 aux.handle = handle;
70 __vprintf (format, args, add_char, &aux);
71 flush (&aux);
72 return aux.char_cnt;
73}
74
75/** Adds C to the buffer in AUX, flushing it if the buffer fills
76 up. */
77static void
78add_char (char c, void *aux_)
79{
80 struct vhprintf_aux *aux = aux_;
81 *aux->p++ = c;
82 if (aux->p >= aux->buf + sizeof aux->buf)
83 flush (aux);
84 aux->char_cnt++;
85}
86
87/** Flushes the buffer in AUX. */
88static void
89flush (struct vhprintf_aux *aux)
90{
91 if (aux->p > aux->buf)
92 write (aux->handle, aux->buf, aux->p - aux->buf);
93 aux->p = aux->buf;
94}
int vprintf(const char *format, va_list args)
The standard vprintf() function, which is like printf() but uses a va_list.
Definition: console.c:126
int puts(const char *s)
Writes string S to the console, followed by a new-line character.
Definition: console.c:140
int putchar(int c)
Writes C to the vga display and serial port.
Definition: console.c:163
void __vprintf(const char *format, va_list args, void(*output)(char, void *), void *aux)
Internal functions.
Definition: stdio.c:157
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).
Definition: random.c:17
#define va_end(LIST)
Definition: stdarg.h:10
#define va_start(LIST, ARG)
Definition: stdarg.h:9
__builtin_va_list va_list
GCC has <stdarg.h> functionality as built-ins, so all we need is to use it.
Definition: stdarg.h:7
#define STDOUT_FILENO
Definition: stdio.h:16
size_t strlen(const char *string)
Returns the length of STRING.
Definition: string.c:293
Auxiliary data for vhprintf_helper().
Definition: console.c:50
char buf[64]
Character buffer.
Definition: console.c:51
int char_cnt
Total characters written so far.
Definition: console.c:53
int handle
Output file handle.
Definition: console.c:54
char * p
Current position in buffer.
Definition: console.c:52
int vhprintf(int handle, const char *format, va_list args)
Formats the printf() format specification FORMAT with arguments given in ARGS and writes the output t...
Definition: console.c:64
static void flush(struct vhprintf_aux *)
Flushes the buffer in AUX.
Definition: console.c:89
static void add_char(char, void *)
Adds C to the buffer in AUX, flushing it if the buffer fills up.
Definition: console.c:78
int hprintf(int handle, const char *format,...)
Like printf(), but writes output to the given HANDLE.
Definition: console.c:16