Revise makefile structure.
[pintos-anon] / src / lib / user / console.c
1 #include <stdio.h>
2 #include <syscall.h>
3 #include <syscall-nr.h>
4
5 /* The standard vprintf() function,
6    which is like printf() but uses a va_list. */
7 int
8 vprintf (const char *format, va_list args) 
9 {
10   return vhprintf (STDOUT_FILENO, format, args);
11 }
12
13 /* Like printf(), but writes output to the given HANDLE. */
14 int
15 hprintf (int handle, const char *format, ...) 
16 {
17   va_list args;
18   int retval;
19
20   va_start (args, format);
21   retval = vhprintf (handle, format, args);
22   va_end (args);
23
24   return retval;
25 }
26
27 /* Writes string S to the console, followed by a new-line
28    character. */
29 int
30 puts (const char *s) 
31 {
32   while (*s != '\0')
33     putchar (*s++);
34   putchar ('\n');
35
36   return 0;
37 }
38
39 /* Writes C to the console. */
40 int
41 putchar (int c) 
42 {
43   char c2 = c;
44   write (STDOUT_FILENO, &c2, 1);
45   return c;
46 }
47 \f
48 /* Auxiliary data for vhprintf_helper(). */
49 struct vhprintf_aux 
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
57 static void add_char (char, void *);
58 static 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. */
63 int
64 vhprintf (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. */
77 static void
78 add_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. */
88 static void
89 flush (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 }