d0485f11fd1dfb251c80878925419435a8da80c0
[pintos-anon] / src / lib / user / printf.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 int
16 vprintf (const char *format, va_list args) 
17 {
18   struct vprintf_aux aux;
19   aux.p = aux.buf;
20   aux.char_cnt = 0;
21   __vprintf (format, args, vprintf_helper, &aux);
22   if (aux.p > aux.buf)
23     write (STDOUT_FILENO, aux.buf, aux.p - aux.buf);
24   return aux.char_cnt;
25 }
26
27 /* Helper function for vprintf(). */
28 static void
29 vprintf_helper (char c, void *aux_) 
30 {
31   struct vprintf_aux *aux = aux_;
32   *aux->p++ = c;
33   if (aux->p >= aux->buf + sizeof aux->buf)
34     {
35       write (STDOUT_FILENO, aux->buf, aux->p - aux->buf);
36       aux->p = aux->buf;
37     }
38   aux->char_cnt++;
39 }
40
41 /* Writes C to the console. */
42 int
43 putchar (int c) 
44 {
45   char c2 = c;
46   write (STDOUT_FILENO, &c2, 1);
47   return c;
48 }