X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=blobdiff_plain;f=src%2Flib%2Fuser%2Fconsole.c;h=d2b9dc72f44899704d14fc48f113e269cea5b0fc;hp=709a5c86ad6ebc5186af9c8497b86eada1bd8c71;hb=d46fd132092304422193b717d9a60641604cc1b5;hpb=bbca49d728013a8d7848d75c7e4bc25b67b60d89 diff --git a/src/lib/user/console.c b/src/lib/user/console.c index 709a5c8..d2b9dc7 100644 --- a/src/lib/user/console.c +++ b/src/lib/user/console.c @@ -2,43 +2,26 @@ #include #include -static void vprintf_helper (char, void *); - -/* Auxiliary data for vprintf_helper(). */ -struct vprintf_aux - { - char buf[64]; /* Character buffer. */ - char *p; /* Current position in buffer. */ - int char_cnt; /* Total characters written so far. */ - }; - /* The standard vprintf() function, - which is like printf() but uses a va_list. - Writes its output to the STDOUT_FILENO handle. */ + which is like printf() but uses a va_list. */ int vprintf (const char *format, va_list args) { - struct vprintf_aux aux; - aux.p = aux.buf; - aux.char_cnt = 0; - __vprintf (format, args, vprintf_helper, &aux); - if (aux.p > aux.buf) - write (STDOUT_FILENO, aux.buf, aux.p - aux.buf); - return aux.char_cnt; + return vhprintf (STDOUT_FILENO, format, args); } -/* Helper function for vprintf(). */ -static void -vprintf_helper (char c, void *aux_) +/* Like printf(), but writes output to the given HANDLE. */ +int +hprintf (int handle, const char *format, ...) { - struct vprintf_aux *aux = aux_; - *aux->p++ = c; - if (aux->p >= aux->buf + sizeof aux->buf) - { - write (STDOUT_FILENO, aux->buf, aux->p - aux->buf); - aux->p = aux->buf; - } - aux->char_cnt++; + va_list args; + int retval; + + va_start (args, format); + retval = vhprintf (handle, format, args); + va_end (args); + + return retval; } /* Writes string S to the console, followed by a new-line @@ -61,3 +44,51 @@ putchar (int c) write (STDOUT_FILENO, &c2, 1); return c; } + +/* Auxiliary data for vhprintf_helper(). */ +struct vhprintf_aux + { + char buf[64]; /* Character buffer. */ + char *p; /* Current position in buffer. */ + int char_cnt; /* Total characters written so far. */ + int handle; /* Output file handle. */ + }; + +static void add_char (char, void *); +static void flush (struct vhprintf_aux *); + +/* Formats the printf() format specification FORMAT with + arguments given in ARGS and writes the output to the given + HANDLE. */ +int +vhprintf (int handle, const char *format, va_list args) +{ + struct vhprintf_aux aux; + aux.p = aux.buf; + aux.char_cnt = 0; + aux.handle = handle; + __vprintf (format, args, add_char, &aux); + flush (&aux); + return aux.char_cnt; +} + +/* Adds C to the buffer in AUX, flushing it if the buffer fills + up. */ +static void +add_char (char c, void *aux_) +{ + struct vhprintf_aux *aux = aux_; + *aux->p++ = c; + if (aux->p >= aux->buf + sizeof aux->buf) + flush (aux); + aux->char_cnt++; +} + +/* Flushes the buffer in AUX. */ +static void +flush (struct vhprintf_aux *aux) +{ + if (aux->p > aux->buf) + write (aux->handle, aux->buf, aux->p - aux->buf); + aux->p = aux->buf; +}