709a5c86ad6ebc5186af9c8497b86eada1bd8c71
[pintos-anon] / src / lib / user / console.c
1 #include <stdio.h>
2 #include <syscall.h>
3 #include <syscall-nr.h>
4
5 static void vprintf_helper (char, void *);
6
7 /* Auxiliary data for vprintf_helper(). */
8 struct vprintf_aux 
9   {
10     char buf[64];       /* Character buffer. */
11     char *p;            /* Current position in buffer. */
12     int char_cnt;       /* Total characters written so far. */
13   };
14
15 /* The standard vprintf() function,
16    which is like printf() but uses a va_list.
17    Writes its output to the STDOUT_FILENO handle. */
18 int
19 vprintf (const char *format, va_list args) 
20 {
21   struct vprintf_aux aux;
22   aux.p = aux.buf;
23   aux.char_cnt = 0;
24   __vprintf (format, args, vprintf_helper, &aux);
25   if (aux.p > aux.buf)
26     write (STDOUT_FILENO, aux.buf, aux.p - aux.buf);
27   return aux.char_cnt;
28 }
29
30 /* Helper function for vprintf(). */
31 static void
32 vprintf_helper (char c, void *aux_) 
33 {
34   struct vprintf_aux *aux = aux_;
35   *aux->p++ = c;
36   if (aux->p >= aux->buf + sizeof aux->buf)
37     {
38       write (STDOUT_FILENO, aux->buf, aux->p - aux->buf);
39       aux->p = aux->buf;
40     }
41   aux->char_cnt++;
42 }
43
44 /* Writes string S to the console, followed by a new-line
45    character. */
46 int
47 puts (const char *s) 
48 {
49   while (*s != '\0')
50     putchar (*s++);
51   putchar ('\n');
52
53   return 0;
54 }
55
56 /* Writes C to the console. */
57 int
58 putchar (int c) 
59 {
60   char c2 = c;
61   write (STDOUT_FILENO, &c2, 1);
62   return c;
63 }