libc: io: implement (v)fprintf
This commit is contained in:
@@ -25,6 +25,7 @@ typedef struct __opaque_file FILE;
|
|||||||
extern int printf(const char *format, ...);
|
extern int printf(const char *format, ...);
|
||||||
extern int vprintf(const char *format, va_list arg);
|
extern int vprintf(const char *format, va_list arg);
|
||||||
extern int fprintf(FILE *stream, const char *format, ...);
|
extern int fprintf(FILE *stream, const char *format, ...);
|
||||||
|
extern int vfprintf(FILE *stream, const char *format, va_list arg);
|
||||||
extern int snprintf(char *buffer, size_t count, const char *format, ...);
|
extern int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||||
extern int vsnprintf(
|
extern int vsnprintf(
|
||||||
char *buffer,
|
char *buffer,
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int fprintf(struct __opaque_file *stream, const char *format, ...)
|
int fprintf(struct __opaque_file *stream, const char *format, ...)
|
||||||
{
|
{
|
||||||
return 0;
|
va_list arg;
|
||||||
|
va_start(arg, format);
|
||||||
|
int ret = vfprintf(stream, format, arg);
|
||||||
|
va_end(arg);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ int printf(const char *format, ...)
|
|||||||
{
|
{
|
||||||
va_list arg;
|
va_list arg;
|
||||||
va_start(arg, format);
|
va_start(arg, format);
|
||||||
int ret = vprintf(format, arg);
|
int ret = vfprintf(stdout, format, arg);
|
||||||
va_end(arg);
|
va_end(arg);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include "file.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
extern int __libc_fctprintf(
|
||||||
|
int (*out)(char character, void *arg),
|
||||||
|
void *arg,
|
||||||
|
const char *format,
|
||||||
|
va_list va);
|
||||||
|
extern int __fputc(int c, struct __opaque_file *stream);
|
||||||
|
|
||||||
|
static inline int _out_file(char character, void *arg)
|
||||||
|
{
|
||||||
|
FILE *fp = arg;
|
||||||
|
return __fputc(character, fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int vfprintf(FILE *fp, const char *format, va_list arg)
|
||||||
|
{
|
||||||
|
__libc_file_lock(fp);
|
||||||
|
|
||||||
|
int ret = __libc_fctprintf(_out_file, fp, format, arg);
|
||||||
|
if (errno != SUCCESS) {
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ferror(fp)) {
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
__libc_file_unlock(fp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "file.h"
|
#include "file.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
extern int __libc_fctprintf(
|
extern int __libc_fctprintf(
|
||||||
@@ -11,7 +11,7 @@ extern int __libc_fctprintf(
|
|||||||
va_list va);
|
va_list va);
|
||||||
extern int __fputc(int c, struct __opaque_file *stream);
|
extern int __fputc(int c, struct __opaque_file *stream);
|
||||||
|
|
||||||
static inline int _out_file(char character, void *arg)
|
static inline int _out_file(char character, void *arg)
|
||||||
{
|
{
|
||||||
FILE *fp = arg;
|
FILE *fp = arg;
|
||||||
return __fputc(character, fp);
|
return __fputc(character, fp);
|
||||||
@@ -19,17 +19,5 @@ static inline int _out_file(char character, void *arg)
|
|||||||
|
|
||||||
int vprintf(const char *format, va_list arg)
|
int vprintf(const char *format, va_list arg)
|
||||||
{
|
{
|
||||||
__libc_file_lock(stdout);
|
return vfprintf(stdout, format, arg);
|
||||||
|
|
||||||
int ret = __libc_fctprintf(_out_file, stdout, format, arg);
|
|
||||||
if (errno != SUCCESS) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ferror(stdout)) {
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
__libc_file_unlock(stdout);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user